Skip to content

Commit

Permalink
fix: mock dapp fixes (#353)
Browse files Browse the repository at this point in the history
* fix: mock dapp fixes

* fix: mock dapp execute params change

---------

Co-authored-by: gcranju <[email protected]>
  • Loading branch information
gcranju and Itshyphen authored Aug 21, 2024
1 parent 3668c70 commit a5a28d9
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 21 deletions.
38 changes: 28 additions & 10 deletions contracts/sui/mock_dapp/sources/dapp_state.move
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
destination:vector<String>,
}

public struct ExecuteParams has drop {
type_args: vector<String>,
args: vector<String>,
}

public fun create_execute_params(type_args: vector<String>, args: vector<String>): 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<String>{
let mut sources=vector::empty<String>();
sources.push_back(connection.source);
sources
connection.source
}
public fun get_connection_dest(connection:&Connection):vector<String>{
let mut sources=vector::empty<String>();
sources.push_back(connection.destination);
sources
connection.destination
}

public struct DappState has key{
id:UID,
xcall_cap:IDCap,
xcall_id:ID,
connections:VecMap<String,Connection>

}

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<String,Connection>(),
}

Expand Down Expand Up @@ -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<String>,dest:vector<String>,ctx:&mut TxContext){
if (vec_map::contains(&self.connections,&net_id)){
vec_map::remove(&mut self.connections,&net_id);
};
Expand Down
38 changes: 32 additions & 6 deletions contracts/sui/mock_dapp/sources/mock_dapp.move
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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);

}
Expand All @@ -36,6 +40,28 @@ public struct WitnessCarrier has key { id: UID, witness: REGISTER_WITNESS }
witness
}

entry fun get_execute_params(config: &DappState, _msg:vector<u8>): ExecuteParams{
let type_args:vector<String> = vector::empty();

let mut result:vector<String> = 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<u8>): ExecuteParams{
let type_args:vector<String> = vector::empty();

let mut result:vector<String> = 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<SUI>,request_id:u128,data:vector<u8>,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);
Expand All @@ -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<String>,destination:vector<String>,ctx:&mut TxContext){
dapp_state::add_connection(state,net_id,source,destination,ctx);
}

Expand All @@ -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<u8>()){
envelope =envelope::wrap_call_message(data,sources,destinations);
} else {
Expand Down
10 changes: 5 additions & 5 deletions contracts/sui/mock_dapp/tests/mock_dapp_tests.move
Original file line number Diff line number Diff line change
Expand Up @@ -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<DappState>();
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<DappState>(dapp_state);

scenario



}





#[test]
Expand Down

0 comments on commit a5a28d9

Please sign in to comment.