From a5a28d96424bf1897395a0fb32d0035b27115ad2 Mon Sep 17 00:00:00 2001 From: gcranju <134275268+gcranju@users.noreply.github.com> Date: Wed, 21 Aug 2024 13:53:16 +0545 Subject: [PATCH] fix: mock dapp fixes (#353) * fix: mock dapp fixes * fix: mock dapp execute params change --------- Co-authored-by: gcranju <075bct064.ranju@pcampus.edu.np> --- .../sui/mock_dapp/sources/dapp_state.move | 38 ++++++++++++++----- .../sui/mock_dapp/sources/mock_dapp.move | 38 ++++++++++++++++--- .../sui/mock_dapp/tests/mock_dapp_tests.move | 10 ++--- 3 files changed, 65 insertions(+), 21 deletions(-) diff --git a/contracts/sui/mock_dapp/sources/dapp_state.move b/contracts/sui/mock_dapp/sources/dapp_state.move index 6c97e2ef..6eb38db2 100644 --- a/contracts/sui/mock_dapp/sources/dapp_state.move +++ b/contracts/sui/mock_dapp/sources/dapp_state.move @@ -7,33 +7,51 @@ module mock_dapp::dapp_state { use xcall::execute_ticket::{Self}; public struct Connection has store,copy,drop{ - source:String, - destination:String, + source:vector, + destination:vector, + } + + public struct ExecuteParams has drop { + type_args: vector, + args: vector, + } + + public fun create_execute_params(type_args: vector, args: vector): ExecuteParams { + ExecuteParams{ + type_args:type_args, + args:args + } + } + + public fun get_config_id(config: &DappState): ID { + config.id.to_inner() + } + + public fun get_xcall_id(config: &DappState): ID{ + config.xcall_id } public fun get_connection_source(connection:&Connection):vector{ - let mut sources=vector::empty(); - sources.push_back(connection.source); - sources + connection.source } public fun get_connection_dest(connection:&Connection):vector{ - let mut sources=vector::empty(); - sources.push_back(connection.destination); - sources + connection.destination } public struct DappState has key{ id:UID, xcall_cap:IDCap, + xcall_id:ID, connections:VecMap } - public(package) fun new(cap:IDCap,ctx: &mut TxContext):DappState{ + public(package) fun new(cap:IDCap, xcall_id: ID, ctx: &mut TxContext):DappState{ DappState { id: object::new(ctx), xcall_cap:cap, + xcall_id:xcall_id, connections:vec_map::empty(), } @@ -61,7 +79,7 @@ module mock_dapp::dapp_state { } - public fun add_connection(self:&mut DappState,net_id:String,source:String,dest:String,ctx:&mut TxContext){ + public fun add_connection(self:&mut DappState,net_id:String,source:vector,dest:vector,ctx:&mut TxContext){ if (vec_map::contains(&self.connections,&net_id)){ vec_map::remove(&mut self.connections,&net_id); }; diff --git a/contracts/sui/mock_dapp/sources/mock_dapp.move b/contracts/sui/mock_dapp/sources/mock_dapp.move index 51e0068e..e0a1204b 100644 --- a/contracts/sui/mock_dapp/sources/mock_dapp.move +++ b/contracts/sui/mock_dapp/sources/mock_dapp.move @@ -1,8 +1,9 @@ module mock_dapp::mock_dapp { use xcall::main::{Self as xcall}; - use xcall::xcall_state::{Storage as XCallState}; - use xcall::network_address::{Self,NetworkAddress}; - use mock_dapp::dapp_state::{Self,DappState}; + use xcall::xcall_utils; + use xcall::xcall_state::{Self, Storage as XCallState}; + use xcall::network_address::{Self}; + use mock_dapp::dapp_state::{Self,DappState, ExecuteParams, create_execute_params, get_xcall_id}; use xcall::execute_ticket::{Self}; use xcall::envelope::{Self}; use sui::coin::{Self, Coin}; @@ -25,7 +26,10 @@ public struct WitnessCarrier has key { id: UID, witness: REGISTER_WITNESS } entry public fun register_xcall(xcall:&XCallState,carrier:WitnessCarrier,ctx:&mut TxContext){ let w= get_witness(carrier); let cap= xcall::register_dapp(xcall,w,ctx); - let state=dapp_state::new(cap,ctx); + let xcall_id = xcall_state::get_id_cap_xcall(&cap); + let state=dapp_state::new(cap,xcall_id,ctx); + + dapp_state::share(state); } @@ -36,6 +40,28 @@ public struct WitnessCarrier has key { id: UID, witness: REGISTER_WITNESS } witness } + entry fun get_execute_params(config: &DappState, _msg:vector): ExecuteParams{ + let type_args:vector = vector::empty(); + + let mut result:vector = vector::empty(); + result.push_back(xcall_utils::id_to_hex_string(&dapp_state::get_config_id(config))); + result.push_back(xcall_utils::id_to_hex_string(&get_xcall_id(config))); + result.push_back(b"coin".to_string()); + result.push_back(b"request_id".to_string()); + result.push_back(b"data".to_string()); + create_execute_params(type_args, result) + } + + entry fun get_rollback_params(config: &DappState, _msg:vector): ExecuteParams{ + let type_args:vector = vector::empty(); + + let mut result:vector = vector::empty(); + result.push_back(xcall_utils::id_to_hex_string(&dapp_state::get_config_id(config))); + result.push_back(xcall_utils::id_to_hex_string(&get_xcall_id(config))); + result.push_back(b"sn".to_string()); + create_execute_params(type_args, result) + } + entry public fun execute_call(state:&mut DappState,xcall:&mut XCallState,mut fee: Coin,request_id:u128,data:vector,ctx:&mut TxContext){ let ticket= xcall::execute_call(xcall,dapp_state::get_xcall_cap(state),request_id,data,ctx); let msg= execute_ticket::message(&ticket); @@ -61,7 +87,7 @@ public struct WitnessCarrier has key { id: UID, witness: REGISTER_WITNESS } } - entry public fun add_connection(state:&mut DappState,net_id:String,source:String,destination:String,ctx:&mut TxContext){ + entry public fun add_connection(state:&mut DappState,net_id:String,source:vector,destination:vector,ctx:&mut TxContext){ dapp_state::add_connection(state,net_id,source,destination,ctx); } @@ -70,7 +96,7 @@ public struct WitnessCarrier has key { id: UID, witness: REGISTER_WITNESS } 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); - let mut envelope; + let envelope; if(rollback==vector::empty()){ envelope =envelope::wrap_call_message(data,sources,destinations); } else { diff --git a/contracts/sui/mock_dapp/tests/mock_dapp_tests.move b/contracts/sui/mock_dapp/tests/mock_dapp_tests.move index 7d68740b..59e149ba 100644 --- a/contracts/sui/mock_dapp/tests/mock_dapp_tests.move +++ b/contracts/sui/mock_dapp/tests/mock_dapp_tests.move @@ -50,17 +50,17 @@ module mock_dapp::mock_dapp_tests { scenario.return_to_sender(adminCap); scenario.next_tx(admin); let mut dapp_state=scenario.take_shared(); - mock_dapp::add_connection(&mut dapp_state,string::utf8(b"netid"),string::utf8(b"centralized-1"),string::utf8(b"destconn"),scenario.ctx()); + let mut sources = vector::empty(); + sources.push_back(string::utf8(b"centralized-1")); + let mut dests = vector::empty(); + dests.push_back(string::utf8(b"destconn")); + mock_dapp::add_connection(&mut dapp_state,string::utf8(b"netid"),sources,dests,scenario.ctx()); test_scenario::return_shared(dapp_state); scenario - - } - - #[test]