diff --git a/contracts/sui/xcall/sources/centralized_connection/centralized_connection.move b/contracts/sui/xcall/sources/centralized_connection/centralized_connection.move index 6456baa2..34e1c357 100644 --- a/contracts/sui/xcall/sources/centralized_connection/centralized_connection.move +++ b/contracts/sui/xcall/sources/centralized_connection/centralized_connection.move @@ -1,6 +1,6 @@ #[allow(unused_field,unused_use,unused_const,unused_mut_parameter,unused_variable,unused_assignment)] module xcall::centralized_connection { - use xcall::centralized_state::{Self,State, ReceiptKey,get_state}; + use xcall::centralized_state::{Self,State, ReceiptKey,get_state,get_state_mut}; use std::string::{Self, String}; use sui::bag::{Bag, Self}; use sui::event; @@ -21,6 +21,8 @@ module xcall::centralized_connection { to:String, conn_sn:u128, msg:vector, + // same package can instantiate multiple connection so this is required + connection_id:String, } @@ -28,11 +30,10 @@ module xcall::centralized_connection { public(package) fun connect():State{ - - centralized_state::create() + centralized_state::create() } - public fun get_fee(states:&mut Bag,connection_id:String,netId:String,response:bool):u64{ + public fun get_fee(states:&Bag,connection_id:String,netId:String,response:bool):u64{ let state= get_state(states,connection_id); centralized_state::get_fee(state,&netId,response) } @@ -41,18 +42,19 @@ module xcall::centralized_connection { let sn = centralized_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,to:String,sn:u128,msg:vector,is_response:bool,ctx: &mut TxContext){ let fee = get_fee(states,connection_id, to, is_response); assert!(coin.value() >= fee, ENotEnoughFee); let balance = coin.into_balance(); - centralized_state::deposit(get_state(states,connection_id),balance); - let conn_sn = get_next_connection_sn(get_state(states,connection_id)); + centralized_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 }); } diff --git a/contracts/sui/xcall/sources/centralized_connection/centralized_entry.move b/contracts/sui/xcall/sources/centralized_connection/centralized_entry.move index ee947266..d46cb95c 100644 --- a/contracts/sui/xcall/sources/centralized_connection/centralized_entry.move +++ b/contracts/sui/xcall/sources/centralized_connection/centralized_entry.move @@ -2,23 +2,23 @@ module xcall::centralized_entry{ use xcall::main::{Self as xcall}; use xcall::xcall_state::{Self,Storage as XCallState,ConnCap}; - use xcall::centralized_state::{Self,get_state}; + use xcall::centralized_state::{Self,get_state,get_state_mut}; use std::string::{String}; entry public fun receive_message(xcall:&mut XCallState,cap:&ConnCap,src_net_id:String,sn:u128,msg:vector,ctx: &mut TxContext){ - let state=get_state(xcall_state::get_connection_states_mut(xcall),cap.connection_id()); + let state=get_state_mut(xcall_state::get_connection_states_mut(xcall),cap.connection_id()); centralized_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(xcall_state::get_connection_states_mut(xcall),cap.connection_id()); + let state=get_state_mut(xcall_state::get_connection_states_mut(xcall),cap.connection_id()); centralized_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(xcall_state::get_connection_states_mut(xcall),cap.connection_id()); + let state=get_state_mut(xcall_state::get_connection_states_mut(xcall),cap.connection_id()); centralized_state::set_fee(state,net_id,message_fee,response_fee,ctx.sender()); } diff --git a/contracts/sui/xcall/sources/centralized_connection/centralized_state.move b/contracts/sui/xcall/sources/centralized_connection/centralized_state.move index 062d7f61..ea2741cf 100644 --- a/contracts/sui/xcall/sources/centralized_connection/centralized_state.move +++ b/contracts/sui/xcall/sources/centralized_connection/centralized_state.move @@ -15,10 +15,15 @@ module xcall::centralized_state { // string::utf8(PackageId) // } - public fun get_state(states:&mut Bag,connection_id:String):&mut State { + public fun get_state_mut(states:&mut Bag,connection_id:String):&mut State { let state:&mut State=bag::borrow_mut(states,connection_id); state } + + public fun get_state(states:&Bag,connection_id:String):&State { + let state:&State=bag::borrow(states,connection_id); + state + } public struct ReceiptKey has copy, drop, store { diff --git a/contracts/sui/xcall/sources/connections.move b/contracts/sui/xcall/sources/connections.move index 6953d65c..32683470 100644 --- a/contracts/sui/xcall/sources/connections.move +++ b/contracts/sui/xcall/sources/connections.move @@ -18,7 +18,7 @@ const ConnCentralized:vector =b"centralized"; - public fun register(states:&mut Bag,connection_id:String,ctx:&mut TxContext){ + public(package) fun register(states:&mut Bag,connection_id:String,ctx:&mut TxContext){ if (get_connection_type(&connection_id).bytes()==ConnCentralized){ let state= centralized_connection::connect(); @@ -30,7 +30,7 @@ const ConnCentralized:vector =b"centralized"; } - public fun get_fee(states:&mut Bag,connection_id:String,netId:String,response:bool):u64{ + public(package) fun get_fee(states:&Bag,connection_id:String,netId:String,response:bool):u64{ if (get_connection_type(&connection_id).bytes()==ConnCentralized){ let fee= centralized_connection::get_fee(states,connection_id,netId,response); @@ -40,7 +40,7 @@ const ConnCentralized:vector =b"centralized"; } } - public fun send_message(states:&mut Bag, + public(package) fun send_message(states:&mut Bag, connection_id:String, coin:Coin, netId:String, @@ -60,8 +60,6 @@ const ConnCentralized:vector =b"centralized"; let separator_index=string::index_of(connection_id,&string::utf8(b"-")); let connType=string::sub_string(connection_id,0,separator_index); connType - - } } \ No newline at end of file diff --git a/contracts/sui/xcall/sources/main.move b/contracts/sui/xcall/sources/main.move index a89a495d..6ba52314 100644 --- a/contracts/sui/xcall/sources/main.move +++ b/contracts/sui/xcall/sources/main.move @@ -136,9 +136,9 @@ module xcall::main { xcall_state::get_protocol_fee_handler(self) } - fun get_connection_fee(self:&mut Storage,connection:String,net_id:String, need_response:bool ):u64{ + fun get_connection_fee(self:&Storage,connection:String,net_id:String, need_response:bool ):u64{ connections::get_fee( - xcall_state::get_connection_states_mut(self), + xcall_state::get_connection_states(self), connection, net_id, need_response @@ -152,7 +152,7 @@ module xcall::main { fee } - entry public fun get_fee_sources(self:&mut Storage, net_id:String, rollback:bool, sources:vector):u64{ + entry public fun get_fee_sources(self:&Storage, net_id:String, rollback:bool, sources:vector):u64{ let mut fee = xcall_state::get_protocol_fee(self); if(isReply(self,net_id,sources) && !rollback){ @@ -507,7 +507,7 @@ module xcall::main { rollback_ticket::consume(ticket); } - fun is_valid_source(self:&mut Storage,nid:String,source:String,protocols:vector):bool{ + fun is_valid_source(self:&Storage,nid:String,source:String,protocols:vector):bool{ if(vector::contains(&protocols,&source)){ return true @@ -516,7 +516,7 @@ module xcall::main { (connection == source) } - fun isReply(self:&mut Storage,net_id:String, sources: vector):bool{ + fun isReply(self:&Storage,net_id:String, sources: vector):bool{ let reply_state = xcall_state::get_reply_state(self); return message_request::from_nid(&reply_state) == net_id && message_request::protocols(&reply_state) == sources diff --git a/contracts/sui/xcall/sources/states/xcall_state.move b/contracts/sui/xcall/sources/states/xcall_state.move index f790882d..8ad37d97 100644 --- a/contracts/sui/xcall/sources/states/xcall_state.move +++ b/contracts/sui/xcall/sources/states/xcall_state.move @@ -209,6 +209,10 @@ module xcall::xcall_state { &mut self.connection_states } + public fun get_connection_states(self:&Storage):&Bag{ + &self.connection_states + } + public fun get_protocol_fee(self:&Storage):u64{ self.protocol_fee }