Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: sui cluster connection #393

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#[allow(unused_field,unused_use,unused_const,unused_mut_parameter,unused_variable,unused_assignment)]
module xcall::cluster_connection {
use xcall::cluster_state::{Self,State, ReceiptKey,get_state,get_state_mut};
use std::string::{Self, String};
use sui::bag::{Bag, Self};
use sui::event;
use sui::table::{Self, Table};
use sui::sui::SUI;
use sui::coin::{Self, Coin};
use sui::balance;
use xcall::xcall_utils::{Self as utils};
use xcall::xcall_state::{Self,ConnCap};
use xcall::xcall_state::{Storage as XCallState};

const ENotEnoughFee: u64 = 10;
/* ================= events ================= */

public struct Message has copy, drop {
to:String,
conn_sn:u128,
msg:vector<u8>,
// same package can instantiate multiple connection so this is required
connection_id:String,
}

public(package) fun connect():State{
cluster_state::create()
}

public fun get_fee(states:&Bag,connection_id:String,netId:String,response:bool):u64{
let state= get_state(states,connection_id);
cluster_state::get_fee(state,&netId,response)
}

public(package) fun get_next_connection_sn(state:&mut State):u128 {
let sn = cluster_state::get_next_conn_sn(state);
sn
}
// this is safe because only package can call this other xcall will call other deployed instance
public(package) fun send_message(states:&mut Bag,connection_id:String,coin:Coin<SUI>,to:String,sn:u128,msg:vector<u8>,is_response:bool,ctx: &mut TxContext){
let mut fee = 0;
if(!is_response){
fee = get_fee(states,connection_id, to, sn>0);
};
assert!(coin.value() >= fee, ENotEnoughFee);
let balance = coin.into_balance();
cluster_state::deposit(get_state_mut(states,connection_id),balance);
let conn_sn = get_next_connection_sn(get_state_mut(states,connection_id));

event::emit(Message { to, conn_sn, msg, connection_id });
}

}

65 changes: 65 additions & 0 deletions contracts/sui/xcall/sources/cluster_connection/cluster_entry.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
module xcall::cluster_entry{

use xcall::main::{Self as xcall};
use xcall::xcall_state::{Self,Storage as XCallState,ConnCap};
use xcall::cluster_state::{Self,get_state,get_state_mut,validate_admin_cap, Validator, AdminCap};
use xcall::xcall_utils::{Self as utils};
use std::string::{String};

entry public fun receive_message(xcall:&mut XCallState,cap:&ConnCap,src_net_id:String,sn:u128,msg:vector<u8>,ctx: &mut TxContext){
let state=get_state_mut(xcall_state::get_connection_states_mut(xcall),cap.connection_id());
cluster_state::check_save_receipt(state, src_net_id, sn);
xcall::handle_message(xcall, cap,src_net_id, msg,ctx);
}


entry fun claim_fees(xcall:&mut XCallState,cap:&ConnCap,ctx: &mut TxContext){
let state=get_state_mut(xcall_state::get_connection_states_mut(xcall),cap.connection_id());
cluster_state::claim_fees(state,ctx);
}

entry fun set_fee(xcall:&mut XCallState,cap:&ConnCap,net_id:String,message_fee:u64,response_fee:u64, ctx: &TxContext){
let state=get_state_mut(xcall_state::get_connection_states_mut(xcall),cap.connection_id());
cluster_state::set_fee(state,net_id,message_fee,response_fee,ctx.sender());
}

entry fun get_receipt(states: &XCallState,connection_id:String,net_id:String,sn:u128,_ctx: &TxContext):bool{
let state = get_state(states.get_connection_states(),connection_id);
cluster_state::get_receipt(state,net_id,sn)
}

entry fun get_fee(states: &XCallState,connection_id:String,net_id:String,response:bool,_ctx: &TxContext):u64{
let state = get_state(states.get_connection_states(),connection_id);
cluster_state::get_fee(state,&net_id,response)
}

entry fun set_validators(xcall:&mut XCallState,cap:&AdminCap,connection_id:String,validator_pubkey:vector<String>,threshold:u64,_ctx: &mut TxContext){
validate_admin_cap(cap,connection_id);
let state=get_state_mut(xcall_state::get_connection_states_mut(xcall),connection_id);
cluster_state::set_validators(state,validator_pubkey,threshold);
}

entry fun set_validator_threshold(xcall:&mut XCallState,cap:&AdminCap,connection_id:String,threshold:u64,_ctx: &mut TxContext){
validate_admin_cap(cap,connection_id);
let state=get_state_mut(xcall_state::get_connection_states_mut(xcall),connection_id);
cluster_state::set_validator_threshold(state,threshold);
}

entry fun get_validators(states: &XCallState,connection_id:String,_ctx: &TxContext):vector<Validator>{
let state = get_state(states.get_connection_states(),connection_id);
cluster_state::get_validators(state)
}

entry fun get_validators_threshold(states: &XCallState,connection_id:String,_ctx: &TxContext):u64{
let state = get_state(states.get_connection_states(),connection_id);
cluster_state::get_validator_threshold(state)
}

entry fun recieve_message_with_signatures(xcall:&mut XCallState,cap:&ConnCap,src_net_id:String,sn:u128,msg:vector<u8>,signatures:vector<vector<u8>>,ctx: &mut TxContext){
let state=get_state_mut(xcall_state::get_connection_states_mut(xcall),cap.connection_id());
let message_hash = utils::get_message_hash(src_net_id, sn, msg);
cluster_state::verify_signatures(state, message_hash, signatures);
cluster_state::check_save_receipt(state, src_net_id, sn);
xcall::handle_message(xcall, cap,src_net_id, msg,ctx);
}
}
Loading
Loading