Skip to content

Commit

Permalink
Merge pull request #348 from icon-project/fix/additional-issues
Browse files Browse the repository at this point in the history
fix: fix public mutable and format
  • Loading branch information
gcranju authored Aug 9, 2024
2 parents d28a108 + e603ae7 commit e18df70
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>,
// 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<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_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<u8>,
// 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<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_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
});
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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);
Expand Down
58 changes: 29 additions & 29 deletions contracts/sui/xcall/sources/messages/call_message.move
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,35 @@ module xcall::call_message {

const MSG_TYPE:u8=0;

public struct CallMessage has drop{
data:vector<u8>
}

public fun new(data:vector<u8>):CallMessage{
CallMessage {
data,
}
}

public fun msg_type():u8 {
MSG_TYPE
}

public fun encode(self:CallMessage):vector<u8>{
let mut list=vector::empty<vector<u8>>();
public struct CallMessage has drop{
data:vector<u8>
}

public fun new(data:vector<u8>):CallMessage{
CallMessage {
data,
}
}

public fun msg_type():u8 {
MSG_TYPE
}

public fun encode(self:CallMessage):vector<u8>{
let mut list=vector::empty<vector<u8>>();
vector::push_back(&mut list,encoder::encode(&self.data));
let encoded=encoder::encode_list(&list,false);
encoded
}
public fun decode(bytes:&vector<u8>):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<u8>):CallMessage{
let decoded=decoder::decode_list(bytes);
let data= *vector::borrow(&decoded,0);

CallMessage {
data,
}
}
data,
}
}
}
53 changes: 25 additions & 28 deletions contracts/sui/xcall/sources/messages/call_message_rollback.move
Original file line number Diff line number Diff line change
@@ -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<u8>,
rollback:vector<u8>,
}
}



public fun create(data:vector<u8>,rollback:vector<u8>):CallMessageWithRollback{
CallMessageWithRollback {
CallMessageWithRollback {
data:data,
rollback:rollback,
}
}

public fun encode(self:CallMessageWithRollback):vector<u8>{
let mut list=vector::empty<vector<u8>>();
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<u8>{
let mut list=vector::empty<vector<u8>>();
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<u8>):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<u8>):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<u8>{
Expand Down
4 changes: 2 additions & 2 deletions contracts/sui/xcall/sources/messages/envelope.move
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use sui_rlp::decoder;
destinations:vector<String>,
}

public fun encode(req:&XCallEnvelope):vector<u8>{
public fun encode(req:&XCallEnvelope):vector<u8>{
let mut list=vector::empty<vector<u8>>();
vector::push_back(&mut list,encoder::encode_u8(req.message_type));
vector::push_back(&mut list,encoder::encode(&req.message));
Expand All @@ -43,7 +43,7 @@ use sui_rlp::decoder;

}

public fun wrap_call_message(data:vector<u8>,sources:vector<String>,destinations:vector<String>): XCallEnvelope {
public fun wrap_call_message(data:vector<u8>,sources:vector<String>,destinations:vector<String>): XCallEnvelope {
let envelope= XCallEnvelope {
message_type:call_message::msg_type(),
message:data,
Expand Down
47 changes: 23 additions & 24 deletions contracts/sui/xcall/sources/messages/persistent_message.move
Original file line number Diff line number Diff line change
@@ -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<u8>
const MSG_TYPE:u8=2;

public struct PersistentMessage has drop{
data:vector<u8>
}

public fun new(data:vector<u8>):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<u8>{
let mut list=vector::empty<vector<u8>>();
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<u8>{
let mut list=vector::empty<vector<u8>>();
vector::push_back(&mut list,encoder::encode(&self.data));
let encoded=encoder::encode_list(&list,false);
encoded


}

public fun decode(bytes:&vector<u8>):PersistentMessage{
let decoded=decoder::decode_list(bytes);
let data= *vector::borrow(&decoded,0);
PersistentMessage {
data,
public fun decode(bytes:&vector<u8>):PersistentMessage{
let decoded=decoder::decode_list(bytes);
let data= *vector::borrow(&decoded,0);
PersistentMessage {
data,
}
}
}
4 changes: 2 additions & 2 deletions contracts/sui/xcall/sources/states/xcall_state.move
Original file line number Diff line number Diff line change
Expand Up @@ -240,15 +240,15 @@ 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)
}

public(package) fun get_mut_rollback(self: &mut Storage, sequence_no: u128): &mut RollbackData {
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)
}

Expand Down

0 comments on commit e18df70

Please sign in to comment.