diff --git a/contracts/sui/xcall/sources/centralized_connection/centralized_connection.move b/contracts/sui/xcall/sources/centralized_connection/centralized_connection.move index 34e1c357..45241d1c 100644 --- a/contracts/sui/xcall/sources/centralized_connection/centralized_connection.move +++ b/contracts/sui/xcall/sources/centralized_connection/centralized_connection.move @@ -15,55 +15,45 @@ module xcall::centralized_connection { const ENotEnoughFee: u64 = 10; - /* ================= events ================= */ + /* ================= events ================= */ public struct Message has copy, drop { - to:String, - conn_sn:u128, - msg:vector, - // same package can instantiate multiple connection so this is required - connection_id:String, - - } - - - - - public(package) fun connect():State{ - centralized_state::create() - } - - 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) - } - - public(package) fun get_next_connection_sn(state:&mut State):u128 { - 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_mut(states,connection_id),balance); - let conn_sn = get_next_connection_sn(get_state_mut(states,connection_id)); + to:String, + conn_sn:u128, + msg:vector, + // same package can instantiate multiple connection so this is required + connection_id:String, - event::emit(Message { - to, - conn_sn, - msg, - connection_id - }); - } - - - + } + + public(package) fun connect():State{ + centralized_state::create() + } - + 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) + } - + public(package) fun get_next_connection_sn(state:&mut State):u128 { + 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_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 921f5949..1e1e5301 100644 --- a/contracts/sui/xcall/sources/centralized_connection/centralized_entry.move +++ b/contracts/sui/xcall/sources/centralized_connection/centralized_entry.move @@ -27,7 +27,7 @@ module xcall::centralized_entry{ centralized_state::get_receipt(state,net_id,sn) } -entry fun get_fee(states: &XCallState,connection_id:String,net_id:String,response:bool,_ctx: &TxContext):u64{ + 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); centralized_state::get_fee(state,&net_id,response) } diff --git a/contracts/sui/xcall/sources/centralized_connection/centralized_state.move b/contracts/sui/xcall/sources/centralized_connection/centralized_state.move index d195b7dc..e9b0bb34 100644 --- a/contracts/sui/xcall/sources/centralized_connection/centralized_state.move +++ b/contracts/sui/xcall/sources/centralized_connection/centralized_state.move @@ -10,12 +10,12 @@ module xcall::centralized_state { - public fun get_state_mut(states:&mut Bag,connection_id:String):&mut State { + public(package) 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 { + public fun get_state(states:&Bag,connection_id:String):&State { let state:&State=bag::borrow(states,connection_id); state } @@ -86,9 +86,7 @@ module xcall::centralized_state { balance::join(&mut self.balance,balance); } - - - + public(package) fun claim_fees(self:&mut State,ctx:&mut TxContext){ let total= self.balance.withdraw_all(); let coin= coin::from_balance(total,ctx); diff --git a/contracts/sui/xcall/sources/messages/call_message.move b/contracts/sui/xcall/sources/messages/call_message.move index 6ae69f1a..554062ea 100644 --- a/contracts/sui/xcall/sources/messages/call_message.move +++ b/contracts/sui/xcall/sources/messages/call_message.move @@ -6,35 +6,35 @@ module xcall::call_message { const MSG_TYPE:u8=0; - public struct CallMessage has drop{ - data:vector - } - - public fun new(data:vector):CallMessage{ - CallMessage { - data, - } - } - - public fun msg_type():u8 { - MSG_TYPE - } - - public fun encode(self:CallMessage):vector{ - let mut list=vector::empty>(); + public struct CallMessage has drop{ + data:vector + } + + public fun new(data:vector):CallMessage{ + CallMessage { + data, + } + } + + public fun msg_type():u8 { + MSG_TYPE + } + + public fun encode(self:CallMessage):vector{ + let mut list=vector::empty>(); vector::push_back(&mut list,encoder::encode(&self.data)); - - let encoded=encoder::encode_list(&list,false); - encoded - - } - - public fun decode(bytes:&vector):CallMessage{ - let decoded=decoder::decode_list(bytes); - let data= *vector::borrow(&decoded,0); - + + let encoded=encoder::encode_list(&list,false); + encoded + + } + + public fun decode(bytes:&vector):CallMessage{ + let decoded=decoder::decode_list(bytes); + let data= *vector::borrow(&decoded,0); + CallMessage { - data, - } - } + data, + } + } } \ No newline at end of file diff --git a/contracts/sui/xcall/sources/messages/call_message_rollback.move b/contracts/sui/xcall/sources/messages/call_message_rollback.move index 78995813..273e68ad 100644 --- a/contracts/sui/xcall/sources/messages/call_message_rollback.move +++ b/contracts/sui/xcall/sources/messages/call_message_rollback.move @@ -1,48 +1,45 @@ #[allow(unused_field,unused_use,unused_const,unused_mut_parameter,unused_variable,unused_assignment)] module xcall::call_message_rollback { - use std::string::{Self, String}; - use sui_rlp::encoder::{Self}; - use sui_rlp::decoder::{Self}; - - const MSG_TYPE:u8=1; + use std::string::{Self, String}; + use sui_rlp::encoder::{Self}; + use sui_rlp::decoder::{Self}; + + const MSG_TYPE:u8=1; public struct CallMessageWithRollback has drop{ data:vector, rollback:vector, - } + } + - public fun create(data:vector,rollback:vector):CallMessageWithRollback{ - CallMessageWithRollback { + CallMessageWithRollback { data:data, rollback:rollback, } } - public fun encode(self:CallMessageWithRollback):vector{ - let mut list=vector::empty>(); - vector::push_back(&mut list,encoder::encode(&self.data)); - vector::push_back(&mut list,encoder::encode(&self.rollback)); - let encoded=encoder::encode_list(&list,false); - encoded - - } - - - + public fun encode(self:CallMessageWithRollback):vector{ + let mut list=vector::empty>(); + vector::push_back(&mut list,encoder::encode(&self.data)); + vector::push_back(&mut list,encoder::encode(&self.rollback)); + let encoded=encoder::encode_list(&list,false); + encoded - public fun decode(bytes:&vector):CallMessageWithRollback{ - let decoded=decoder::decode_list(bytes); - let data= *vector::borrow(&decoded,0); - let rollback= *vector::borrow(&decoded,1); - CallMessageWithRollback { - data, - rollback, + } + + public fun decode(bytes:&vector):CallMessageWithRollback{ + let decoded=decoder::decode_list(bytes); + let data= *vector::borrow(&decoded,0); + let rollback= *vector::borrow(&decoded,1); + CallMessageWithRollback { + data, + rollback, - } + } } public fun msg_type():u8 { - MSG_TYPE + MSG_TYPE } public fun rollback(self:&CallMessageWithRollback):vector{ diff --git a/contracts/sui/xcall/sources/messages/envelope.move b/contracts/sui/xcall/sources/messages/envelope.move index 191b1190..42b7aff7 100644 --- a/contracts/sui/xcall/sources/messages/envelope.move +++ b/contracts/sui/xcall/sources/messages/envelope.move @@ -16,7 +16,7 @@ use sui_rlp::decoder; destinations:vector, } - public fun encode(req:&XCallEnvelope):vector{ + public fun encode(req:&XCallEnvelope):vector{ let mut list=vector::empty>(); vector::push_back(&mut list,encoder::encode_u8(req.message_type)); vector::push_back(&mut list,encoder::encode(&req.message)); @@ -43,7 +43,7 @@ use sui_rlp::decoder; } - public fun wrap_call_message(data:vector,sources:vector,destinations:vector): XCallEnvelope { + public fun wrap_call_message(data:vector,sources:vector,destinations:vector): XCallEnvelope { let envelope= XCallEnvelope { message_type:call_message::msg_type(), message:data, diff --git a/contracts/sui/xcall/sources/messages/persistent_message.move b/contracts/sui/xcall/sources/messages/persistent_message.move index 0cdf252a..542592df 100644 --- a/contracts/sui/xcall/sources/messages/persistent_message.move +++ b/contracts/sui/xcall/sources/messages/persistent_message.move @@ -1,43 +1,42 @@ #[allow(unused_field,unused_use,unused_const,unused_mut_parameter,unused_variable,unused_assignment)] module xcall::persistent_message { -use std::string::{Self, String}; -use sui_rlp::encoder::{Self}; -use sui_rlp::decoder::{Self}; + use std::string::{Self, String}; + use sui_rlp::encoder::{Self}; + use sui_rlp::decoder::{Self}; -const MSG_TYPE:u8=2; - public struct PersistentMessage has drop{ - data:vector + const MSG_TYPE:u8=2; + + public struct PersistentMessage has drop{ + data:vector } public fun new(data:vector):PersistentMessage{ - PersistentMessage { - data, - } + PersistentMessage { + data, + } } public fun get_data(self: &PersistentMessage){ self.data; } - public fun msg_type():u8 { - MSG_TYPE - } - public fun encode(self:PersistentMessage):vector{ - let mut list=vector::empty>(); - vector::push_back(&mut list,encoder::encode(&self.data)); - let encoded=encoder::encode_list(&list,false); - encoded - + public fun msg_type():u8 { + MSG_TYPE } + public fun encode(self:PersistentMessage):vector{ + let mut list=vector::empty>(); + vector::push_back(&mut list,encoder::encode(&self.data)); + let encoded=encoder::encode_list(&list,false); + encoded - + } - public fun decode(bytes:&vector):PersistentMessage{ - let decoded=decoder::decode_list(bytes); - let data= *vector::borrow(&decoded,0); - PersistentMessage { - data, + public fun decode(bytes:&vector):PersistentMessage{ + let decoded=decoder::decode_list(bytes); + let data= *vector::borrow(&decoded,0); + PersistentMessage { + data, } } } \ No newline at end of file diff --git a/contracts/sui/xcall/sources/states/xcall_state.move b/contracts/sui/xcall/sources/states/xcall_state.move index decf774e..54abe027 100644 --- a/contracts/sui/xcall/sources/states/xcall_state.move +++ b/contracts/sui/xcall/sources/states/xcall_state.move @@ -240,7 +240,7 @@ module xcall::xcall_state { sn } - public fun get_rollback(self: &mut Storage, sequence_no: u128): RollbackData { + public fun get_rollback(self: &Storage, sequence_no: u128): RollbackData { *table::borrow(&self.rollbacks, sequence_no) } @@ -248,7 +248,7 @@ module xcall::xcall_state { table::borrow_mut(&mut self.rollbacks, sequence_no) } - public fun has_rollback(self: &mut Storage , sequence_no: u128): bool { + public fun has_rollback(self: &Storage , sequence_no: u128): bool { table::contains(&self.rollbacks, sequence_no) }