Skip to content

Commit

Permalink
fix: refactor mutability
Browse files Browse the repository at this point in the history
  • Loading branch information
ibrizsabin committed Jun 5, 2024
1 parent 42e02c5 commit a3b8c42
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -21,18 +21,19 @@ module xcall::centralized_connection {
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{

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)
}
Expand All @@ -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<SUI>,to:String,sn:u128,msg:vector<u8>,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
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>,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());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 3 additions & 5 deletions contracts/sui/xcall/sources/connections.move
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const ConnCentralized:vector<u8> =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();
Expand All @@ -30,7 +30,7 @@ const ConnCentralized:vector<u8> =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);
Expand All @@ -40,7 +40,7 @@ const ConnCentralized:vector<u8> =b"centralized";
}
}

public fun send_message(states:&mut Bag,
public(package) fun send_message(states:&mut Bag,
connection_id:String,
coin:Coin<SUI>,
netId:String,
Expand All @@ -60,8 +60,6 @@ const ConnCentralized:vector<u8> =b"centralized";
let separator_index=string::index_of(connection_id,&string::utf8(b"-"));
let connType=string::sub_string(connection_id,0,separator_index);
connType


}

}
10 changes: 5 additions & 5 deletions contracts/sui/xcall/sources/main.move
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -152,7 +152,7 @@ module xcall::main {
fee
}

entry public fun get_fee_sources(self:&mut Storage, net_id:String, rollback:bool, sources:vector<String>):u64{
entry public fun get_fee_sources(self:&Storage, net_id:String, rollback:bool, sources:vector<String>):u64{
let mut fee = xcall_state::get_protocol_fee(self);

if(isReply(self,net_id,sources) && !rollback){
Expand Down Expand Up @@ -507,7 +507,7 @@ module xcall::main {
rollback_ticket::consume(ticket);
}

fun is_valid_source(self:&mut Storage,nid:String,source:String,protocols:vector<String>):bool{
fun is_valid_source(self:&Storage,nid:String,source:String,protocols:vector<String>):bool{

if(vector::contains(&protocols,&source)){
return true
Expand All @@ -516,7 +516,7 @@ module xcall::main {
(connection == source)
}

fun isReply(self:&mut Storage,net_id:String, sources: vector<String>):bool{
fun isReply(self:&Storage,net_id:String, sources: vector<String>):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
Expand Down
4 changes: 4 additions & 0 deletions contracts/sui/xcall/sources/states/xcall_state.move
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit a3b8c42

Please sign in to comment.