Skip to content

Commit

Permalink
fix: add dapp entry
Browse files Browse the repository at this point in the history
  • Loading branch information
ibrizsabin committed Apr 30, 2024
1 parent 36409af commit 412a438
Show file tree
Hide file tree
Showing 13 changed files with 97 additions and 37 deletions.
2 changes: 1 addition & 1 deletion contracts/sui/mock_dapp/sources/mock_dapp.move
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public struct WitnessCarrier has key { id: UID, witness: REGISTER_WITNESS }
dapp_state::add_connection(state,net_id,source,destination,ctx);
}

fun send_message(state:&DappState,xcall:&mut XCallState,fee: Coin<SUI>,to:NetworkAddress,data:vector<u8>,ctx: &mut TxContext){
entry public fun send_message(state:&DappState,xcall:&mut XCallState,fee: Coin<SUI>,to:NetworkAddress,data:vector<u8>,ctx: &mut TxContext){
let connection= dapp_state::get_connection(state,network_address::net_id(&to));
let sources=dapp_state::get_connection_source(&connection);
let destinations=dapp_state::get_connection_dest(&connection);
Expand Down
11 changes: 8 additions & 3 deletions contracts/sui/mock_dapp/tests/mock_dapp_tests.move
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module mock_dapp::mock_dapp_tests {
use xcall::main::{Self as xcall,init_xcall_state};
use mock_dapp::dapp_state::{Self,DappState};
use mock_dapp::mock_dapp::{Self as mock_dapp,WitnessCarrier};
use xcall::xcall_state::{Self,Storage as XCallState};
use xcall::xcall_state::{Self,Storage as XCallState,AdminCap};
use std::string::{Self,String};
use xcall::centralized_entry::{Self as connection_in};
use xcall::call_message::{Self};
Expand Down Expand Up @@ -43,8 +43,10 @@ module mock_dapp::mock_dapp_tests {
#[test_only]
fun setup_connection(admin:address,mut scenario:Scenario):Scenario {
let mut xcall_state= scenario.take_shared<XCallState>();
xcall::register_connection(&mut xcall_state,string::utf8(b"netid"),string::utf8(b"centralized"),scenario.ctx());
let adminCap=scenario.take_from_sender<AdminCap>();
xcall::register_connection(&mut xcall_state,&adminCap,string::utf8(b"netid"),string::utf8(b"centralized"),scenario.ctx());
test_scenario::return_shared<XCallState>(xcall_state);
scenario.return_to_sender(adminCap);
scenario.next_tx(admin);
let mut dapp_state=scenario.take_shared<DappState>();
mock_dapp::add_connection(&mut dapp_state,string::utf8(b"netid"),string::utf8(b"centralized"),string::utf8(b"destconn"),scenario.ctx());
Expand Down Expand Up @@ -99,10 +101,13 @@ module mock_dapp::mock_dapp_tests {
let mut xcall_state= scenario.take_shared<XCallState>();
debug::print(&xcall_state);
let payload= create_message_request_payload(b"somedata",dapp_state.id_str());
connection_in::receive_message(&mut xcall_state,string::utf8(b"source"),1,payload,scenario.ctx());
connection_in::receive_message(&mut xcall_state,string::utf8(b"dnetId"),1,payload,scenario.ctx());
debug::print(&dapp_state);
test_scenario::return_shared<DappState>(dapp_state);
debug::print(&xcall_state);
assert!(xcall_state::get_proxy_requests_size(&xcall_state)==1,0x01);
test_scenario::return_shared<XCallState>(xcall_state);

scenario.end();

}
Expand Down
4 changes: 2 additions & 2 deletions contracts/sui/xcall/Move.lock
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ dependencies = [
]

[move.toolchain-version]
compiler-version = "1.22.0"
edition = "legacy"
compiler-version = "1.23.1"
edition = "2024.alpha"
flavor = "sui"
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,23 @@ module xcall::centralized_connection {


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

centralized_state::create(cap,admin)
centralized_state::create(cap,admin)
}

public fun get_fee(states:&mut Bag,netId:String,response:bool):u64{
let state= get_state(states);
centralized_state::get_fee(state,&netId,response)

}

fun get_next_connection_sn(state:&mut State):u128 {
public(package) fun get_next_connection_sn(state:&mut State):u128 {
let sn = centralized_state::get_next_conn_sn(state);
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);
std::debug::print(&fee);
std::debug::print(&coin.value());
assert!(coin.value() >= fee, ENotEnoughFee);
let balance = coin.into_balance();
std::debug::print(&balance);
centralized_state::deposit(get_state(states),balance);
let conn_sn = get_next_connection_sn(get_state(states));
event::emit(Message {
Expand All @@ -64,10 +58,7 @@ module xcall::centralized_connection {



entry fun get_receipt(states: &mut Bag,net_id:String,sn:u128,ctx: &mut TxContext):bool{
let state = get_state(states);
centralized_state::get_receipt(state,net_id,sn)
}




Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ use xcall::main::{Self as xcall};
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());
centralized_state::get_receipt(state,net_id,sn)
}




Expand Down
2 changes: 2 additions & 0 deletions contracts/sui/xcall/sources/connections.move
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const EConnectionNotFound:u64=0;
const ConnCentralized:vector<u8> =b"centralized";




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

if (package_id==centralized_state::package_id_str()){
Expand Down
6 changes: 3 additions & 3 deletions contracts/sui/xcall/sources/main.move
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ module xcall::main {
string::utf8(NID)
}

entry public fun register_connection(self:&mut Storage,net_id:String,package_id:String,ctx: &mut TxContext){
entry public fun register_connection(self:&mut Storage,admin:&AdminCap,net_id:String,package_id:String,ctx: &mut TxContext){
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);
Expand Down Expand Up @@ -178,7 +178,7 @@ module xcall::main {
data = envelope::message(&envelope);
}
else if(msg_type == call_message_rollback::msg_type()){
let msg = call_message_rollback::decode(envelope::message(&envelope));
let msg = call_message_rollback::decode(&envelope::message(&envelope));
std::debug::print(&network_address::addr(&from));
let from_id = utils::id_from_hex_string(&network_address::addr(&from));

Expand Down Expand Up @@ -291,7 +291,7 @@ module xcall::main {
xcall_state::set_protocol_fee_handler(self,fee_handler);
}

entry public fun send_call(self:&mut Storage,fee: Coin<SUI>,idCap:&IDCap,to:String,envelope_bytes:vector<u8>,ctx: &mut TxContext){
public fun send_call(self:&mut Storage,fee: Coin<SUI>,idCap:&IDCap,to:String,envelope_bytes:vector<u8>,ctx: &mut TxContext){
let envelope=envelope::decode(&envelope_bytes);
let to = network_address::from_string(to);
let from= network_address::create(string::utf8(NID),address::to_string(object::id_to_address(&object::id(idCap))));
Expand Down
22 changes: 22 additions & 0 deletions contracts/sui/xcall/sources/messages/call_message.move
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module xcall::call_message {
use std::string::{Self, String};
use std::vector;
use std::option::{Self, Option,some,none};
use sui_rlp::encoder::{Self};
use sui_rlp::decoder::{Self};

const MSG_TYPE:u8=0;
public struct CallMessage has drop{
Expand All @@ -18,4 +20,24 @@ const MSG_TYPE:u8=0;
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);

CallMessage {
data,
}
}
}
34 changes: 23 additions & 11 deletions contracts/sui/xcall/sources/messages/call_message_rollback.move
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,41 @@ module xcall::call_message_rollback {
use std::string::{Self, String};
use std::vector;
use std::option::{Self, Option,some,none};
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 encode(msg:CallMessageWithRollback):vector<u8>{
vector::empty<u8>()
}


public fun create(data:vector<u8>,rollback:vector<u8>):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 decode(bytes:vector<u8>):CallMessageWithRollback{
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:vector::empty<u8>(),
rollback:vector::empty<u8>(),
data,
rollback,

}
}
Expand All @@ -34,11 +46,11 @@ use std::option::{Self, Option,some,none};
MSG_TYPE
}

public fun rollback(msg:&CallMessageWithRollback):vector<u8>{
msg.rollback
public fun rollback(self:&CallMessageWithRollback):vector<u8>{
self.rollback
}

public fun data(msg:&CallMessageWithRollback):vector<u8>{
msg.data
public fun data(self:&CallMessageWithRollback):vector<u8>{
self.data
}
}
2 changes: 1 addition & 1 deletion contracts/sui/xcall/sources/messages/envelope.move
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ use sui_rlp::decoder;

public fun rollback(self:&XCallEnvelope):Option<vector<u8>>{
if (self.message_type==call_message_rollback::msg_type()) {
let msg= call_message_rollback::decode(self.message);
let msg= call_message_rollback::decode(&self.message);
some(call_message_rollback::rollback(&msg))

}else {
Expand Down
20 changes: 20 additions & 0 deletions contracts/sui/xcall/sources/messages/persistent_message.move
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#[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};

const MSG_TYPE:u8=2;
public struct PersistentMessage has drop{
Expand All @@ -20,4 +22,22 @@ const MSG_TYPE:u8=2;
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,
}
}
}
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 @@ -318,6 +318,10 @@ module xcall::xcall_state {
id
}

public fun get_proxy_requests_size(self:&Storage):u64{
self.proxy_requests.length()
}

#[test_only]
public fun create_id_cap_for_testing(storage: &mut Storage,ctx: &mut TxContext): IDCap {
let idcap = create_id_cap(storage,ctx);
Expand Down
7 changes: 3 additions & 4 deletions contracts/sui/xcall/tests/xcall_test.move
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ module xcall::xcall_tests {
let scenario = &mut scenario_val;
{
let mut storage = test_scenario::take_shared<Storage>(scenario);
let ctx = test_scenario::ctx(scenario);
main::register_connection(&mut storage, from_nid, string::utf8(b"centralized"), ctx);
let adminCap = scenario.take_from_sender<AdminCap>();
main::register_connection(&mut storage, &adminCap,from_nid, string::utf8(b"centralized"), scenario.ctx());
test_scenario::return_shared(storage);
scenario.return_to_sender(adminCap);
};
test_scenario::next_tx(scenario, admin);
scenario_val
Expand Down Expand Up @@ -113,9 +114,7 @@ module xcall::xcall_tests {
let request = message_request::create(icon_dapp, sui_dapp, 1, 1, data, sources);
let message = cs_message::encode(&cs_message::new(cs_message::request_code(), message_request::encode(&request)));
let conn_cap = xcall_state::create_conn_cap_for_testing(&mut storage);

main::handle_message(&mut storage,&conn_cap,from_nid,message,ctx);
// main::send_call(&mut storage,&mut fee,&idCap,string::utf8(b"dnetId/daddress"),envelope_bytes,ctx);
xcall_state::delete_id_cap_for_testing(idCap, ctx);
test_scenario::return_to_sender(&scenario, adminCap);
test_scenario::return_shared( storage);
Expand Down

0 comments on commit 412a438

Please sign in to comment.