Skip to content

Commit

Permalink
feat: add conncap
Browse files Browse the repository at this point in the history
  • Loading branch information
ibrizsabin committed Jun 5, 2024
1 parent 36ca68f commit 585a0be
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 99 deletions.
2 changes: 1 addition & 1 deletion contracts/evm/lib/openzeppelin-contracts-upgradeable
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ module xcall::centralized_connection {



public(package) fun connect(cap:ConnCap,admin:address):State{
centralized_state::create(cap,admin)
public(package) fun connect():State{

centralized_state::create()
}

public fun get_fee(states:&mut Bag,netId:String,response:bool):u64{
let state= get_state(states);
public fun get_fee(states:&mut 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,14 +42,12 @@ module xcall::centralized_connection {
sn
}

public(package) fun send_message(states:&mut Bag,coin:Coin<SUI>,to:String,sn:u128,msg:vector<u8>,is_response:bool,ctx: &mut TxContext){
let fee = get_fee(states, to, is_response);
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),balance);
let conn_sn = get_next_connection_sn(get_state(states));
//std::debug::print(&b"MESSAGE".to_string());
//std::debug::print(&msg);
centralized_state::deposit(get_state(states,connection_id),balance);
let conn_sn = get_next_connection_sn(get_state(states,connection_id));

event::emit(Message {
to,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,30 @@ module xcall::centralized_entry{
use xcall::centralized_state::{Self,get_state};
use std::string::{String};

entry public fun receive_message(xcall:&mut XCallState,src_net_id:String,sn:u128,msg:vector<u8>,ctx: &mut TxContext){
let state=get_state(xcall_state::get_connection_states_mut(xcall));
centralized_state::ensure_admin(state, ctx.sender());
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());
centralized_state::check_save_receipt(state, src_net_id, sn);
let cap:ConnCap=* state.conn_cap();
xcall::handle_message(xcall, &cap,src_net_id, msg,ctx);
xcall::handle_message(xcall, cap,src_net_id, msg,ctx);
}

entry fun set_admin(xcall:&mut XCallState,addr:address, ctx: &TxContext){
let state=get_state(xcall_state::get_connection_states_mut(xcall));
state.set_admin(addr,ctx.sender());

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());
centralized_state::claim_fees(state,ctx);
}

entry fun claim_fees(xcall:&mut XCallState,ctx: &mut TxContext){
let state=get_state(xcall_state::get_connection_states_mut(xcall));
centralized_state::claim_fees(state,ctx.sender(),ctx);
}

entry fun set_fee(xcall:&mut XCallState,net_id:String,message_fee:u64,response_fee:u64, ctx: &TxContext){
let state=get_state(xcall_state::get_connection_states_mut(xcall));
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());
centralized_state::set_fee(state,net_id,message_fee,response_fee,ctx.sender());
}

entry fun get_receipt(states: &mut XCallState,net_id:String,sn:u128,_ctx: &TxContext):bool{
let state = get_state(states.get_connection_states_mut());
entry fun get_receipt(states: &mut XCallState,cap:&ConnCap,net_id:String,sn:u128,_ctx: &TxContext):bool{
let state = get_state(states.get_connection_states_mut(),cap.connection_id());
centralized_state::get_receipt(state,net_id,sn)
}

entry fun get_fee(states: &mut XCallState,net_id:String,response:bool,_ctx: &TxContext):u64{
let state = get_state(states.get_connection_states_mut());
entry fun get_fee(states: &mut XCallState,cap:&ConnCap,net_id:String,response:bool,_ctx: &TxContext):u64{
let state = get_state(states.get_connection_states_mut(),cap.connection_id());
centralized_state::get_fee(state,&net_id,response)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@ module xcall::centralized_state {
use xcall::xcall_state::{ConnCap};
use sui::bag::{Bag, Self};

const PackageId:vector<u8> =b"centralized";
// const PackageId:vector<u8> =b"centralized";
const ENotAdmin:u64=1;

public fun package_id_str():String {
string::utf8(PackageId)
}
// public fun package_id_str():String {
// string::utf8(PackageId)
// }

public fun get_state(states:&mut Bag):&mut State {
let package_id= package_id_str();
let state:&mut State=bag::borrow_mut(states,package_id);
public fun get_state(states:&mut Bag,connection_id:String):&mut State {
let state:&mut State=bag::borrow_mut(states,connection_id);
state
}

Expand All @@ -31,23 +30,17 @@ module xcall::centralized_state {
message_fee: VecMap<String, u64>,
response_fee: VecMap<String, u64>,
receipts: VecMap<ReceiptKey, bool>,
xcall: String,
admin: address,
conn_sn: u128,
balance: Balance<SUI>,
cap:ConnCap,
}

public fun create(cap:ConnCap,admin:address): State {
public(package) fun create(): State {
State {
message_fee: vec_map::empty<String, u64>(),
response_fee: vec_map::empty<String, u64>(),
conn_sn: 0,
receipts: vec_map::empty(),
xcall: string::utf8(b""),
admin: admin,
balance:balance::zero(),
cap
}
}

Expand All @@ -68,7 +61,6 @@ module xcall::centralized_state {
}

public(package) fun set_fee(self: &mut State, net_id: String, message_fee: u64, response_fee: u64,caller:address) {
ensure_admin(self,caller);
if (vec_map::contains(&self.message_fee,&net_id)){
vec_map::remove(&mut self.message_fee,&net_id);
};
Expand All @@ -95,28 +87,14 @@ module xcall::centralized_state {

}

public(package) fun conn_cap(self:&State):&ConnCap{
&self.cap
}

public(package) fun get_admin(self:&State):address{
self.admin
}


public(package) fun set_admin(self:&mut State,admin:address,caller:address){
ensure_admin(self,caller);
self.admin=admin;
}

public(package) fun claim_fees(self:&mut State,caller:address,ctx:&mut TxContext){
ensure_admin(self,caller);
public(package) fun claim_fees(self:&mut State,ctx:&mut TxContext){
let total= self.balance.withdraw_all();
let coin= coin::from_balance(total,ctx);
transfer::public_transfer(coin,caller);
transfer::public_transfer(coin,ctx.sender());

}

public(package) fun ensure_admin(self:&State,caller:address){
assert!(self.admin==caller,ENotAdmin);
}

}
28 changes: 18 additions & 10 deletions contracts/sui/xcall/sources/connections.move
Original file line number Diff line number Diff line change
Expand Up @@ -18,42 +18,50 @@ const ConnCentralized:vector<u8> =b"centralized";



public fun register(states:&mut Bag,package_id:String,cap:ConnCap,ctx:&mut TxContext){
public fun register(states:&mut Bag,connection_id:String,ctx:&mut TxContext){

if (package_id==centralized_state::package_id_str()){
let state= centralized_connection::connect(cap,ctx.sender());
bag::add(states, package_id, state);
if (get_connection_type(&connection_id).bytes()==ConnCentralized){
let state= centralized_connection::connect();
bag::add(states, connection_id, state);
}else{
abort EConnectionNotFound
}


}

public fun get_fee(states:&mut Bag,package_id:String,netId:String,response:bool):u64{
public fun get_fee(states:&mut Bag,connection_id:String,netId:String,response:bool):u64{

if (package_id==centralized_state::package_id_str()){
let fee= centralized_connection::get_fee(states,netId,response);
if (get_connection_type(&connection_id).bytes()==ConnCentralized){
let fee= centralized_connection::get_fee(states,connection_id,netId,response);
fee
}else{
abort EConnectionNotFound
}
}

public fun send_message(states:&mut Bag,
package_id:String,
connection_id:String,
coin:Coin<SUI>,
netId:String,
sn:u128,
msg:vector<u8>,
is_response:bool,
ctx:&mut TxContext){

if (package_id==centralized_state::package_id_str()){
centralized_connection::send_message(states,coin,netId,sn,msg,is_response,ctx);
if (get_connection_type(&connection_id).bytes()==ConnCentralized){
centralized_connection::send_message(states,connection_id,coin,netId,sn,msg,is_response,ctx);
}else{
abort EConnectionNotFound
}
}

fun get_connection_type(connection_id:&String):String{
let separator_index=string::index_of(connection_id,&string::utf8(b"-"));
let connType=string::sub_string(connection_id,0,separator_index);
connType


}

}
16 changes: 9 additions & 7 deletions contracts/sui/xcall/sources/main.move
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,17 @@ module xcall::main {
}
}

entry fun get_nid(self: &mut Storage): String{
entry public fun get_nid(self: &mut Storage): String{
xcall_state::get_net_id(self)
}


entry public fun register_connection(self:&mut Storage,admin:&AdminCap,net_id:String,package_id:String,ctx: &mut TxContext){
entry public fun register_connection(self:&mut Storage,admin:&AdminCap,net_id:String,connection_id:String,relayer:address,ctx: &mut TxContext){
self.enforce_version(CURRENT_VERSION);
self.set_connection(net_id,package_id);
let cap= xcall_state::new_conn_cap(self.get_id(),package_id);
register(self.get_connection_states_mut(),package_id,cap,ctx);
self.set_connection(net_id,connection_id);
let cap= xcall_state::new_conn_cap(self.get_id(),connection_id,ctx);
xcall_state::transfer_conn_cap(cap,relayer,ctx);
register(self.get_connection_states_mut(),connection_id,ctx);
}

public fun admin(self:&mut Storage):ID{
Expand Down Expand Up @@ -299,6 +300,7 @@ module xcall::main {
let remaining = send_call_inner(self,fee,from,to,envelope,ctx);
transfer::public_transfer(remaining, ctx.sender());
}


public fun handle_message(self:&mut Storage,
cap:&ConnCap,
Expand Down Expand Up @@ -330,7 +332,7 @@ module xcall::main {

assert!(from_nid == net_from,EInvalidNID);

let source = cap.package_id();
let source = cap.connection_id();
let to = message_request::to(&req);
let protocols = message_request::protocols(&req);

Expand Down Expand Up @@ -365,7 +367,7 @@ module xcall::main {
let sources = rollback_data::sources(&rollback);
let to = rollback_data::to(&rollback);

let source = cap.package_id();
let source = cap.connection_id();

let source_valid = is_valid_source(self, to, source, sources);

Expand Down
24 changes: 15 additions & 9 deletions contracts/sui/xcall/sources/states/xcall_state.move
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,25 @@ module xcall::xcall_state {
id: UID
}

public struct ConnCap has store,copy,drop{
public struct ConnCap has key {
id:UID,
xcall_id:ID,
package_id:String,
connection_id:String,
}

public(package) fun new_conn_cap(xcall_id:ID,package_id:String):ConnCap{
public(package) fun new_conn_cap(xcall_id:ID,connection_id:String,ctx: &mut TxContext):ConnCap{
ConnCap {
id:object::new(ctx),
xcall_id,
package_id
connection_id
}
}

public fun xcall_id(self:&ConnCap):ID {
self.xcall_id
}
public fun package_id(self:&ConnCap):String {
self.package_id
public fun connection_id(self:&ConnCap):String {
self.connection_id
}


Expand Down Expand Up @@ -246,6 +248,10 @@ module xcall::xcall_state {
transfer::transfer(admin, tx_context::sender(ctx));
}

public(package) fun transfer_conn_cap(cap:ConnCap,relayer:address,ctx: &mut TxContext){
transfer::transfer(cap,relayer);
}

public(package) fun share(self:Storage){
transfer::share_object(self);
}
Expand Down Expand Up @@ -352,10 +358,10 @@ module xcall::xcall_state {
}

#[test_only]
public fun create_conn_cap_for_testing(storage: &mut Storage): ConnCap {
let package_id =string::utf8(b"centralized");
public fun create_conn_cap_for_testing(storage: &mut Storage,ctx: &mut TxContext): ConnCap {
let connection_id =string::utf8(b"centralized-1");
let xcall_id=object::id(storage);
let idcap = new_conn_cap(xcall_id,package_id);
let idcap = new_conn_cap(xcall_id,connection_id,ctx);
idcap
}

Expand Down
Loading

0 comments on commit 585a0be

Please sign in to comment.