From 11950d14ccc815184b56881bf9da655e0f3b627f Mon Sep 17 00:00:00 2001 From: mario4tier Date: Thu, 22 Feb 2024 16:29:13 -0500 Subject: [PATCH] (#12) Add events module + unit tests fixes --- move/Move.lock | 2 +- move/sources/events.move | 39 +++++++ move/sources/host.move | 36 ++++--- move/sources/tests/tests_pipe.move | 4 +- move/sources/tests/tests_pipe_sync_data.move | 6 +- move/sources/transport_control.move | 103 +++++++------------ 6 files changed, 102 insertions(+), 88 deletions(-) create mode 100644 move/sources/events.move diff --git a/move/Move.lock b/move/Move.lock index 7577dac..8ec6e72 100644 --- a/move/Move.lock +++ b/move/Move.lock @@ -23,6 +23,6 @@ dependencies = [ ] [move.toolchain-version] -compiler-version = "1.19.0" +compiler-version = "1.18.1" edition = "legacy" flavor = "sui" diff --git a/move/sources/events.move b/move/sources/events.move new file mode 100644 index 0000000..4156501 --- /dev/null +++ b/move/sources/events.move @@ -0,0 +1,39 @@ +// All events emitted by DTP. + +module dtp::events { + // === Imports === + use sui::event; + // === Friends === + friend dtp::host; + friend dtp::inner_pipe; + friend dtp::transport_control; + + // === Errors === + + // === Constants === + + // === Structs === + struct ConReq has copy, drop { + // TODO Add ServiceTYpe, Pipe and InnerPipe addresses. + tc_address: address, // Transport Control Address. + sender: address, // Sender requesting the connection. + } + + + // === Public-Mutative Functions === + + // === Public-View Functions === + + // === Admin Functions === + + // === Public-Friend Functions === + public(friend) fun emit_con_req( tc_address: address, sender: address ) { + event::emit(ConReq {tc_address, sender} ); + } + + // === Private Functions === + + // === Test Functions === + + +} \ No newline at end of file diff --git a/move/sources/host.move b/move/sources/host.move index 2d2dff7..2ca0687 100644 --- a/move/sources/host.move +++ b/move/sources/host.move @@ -12,13 +12,13 @@ module dtp::host { // === Imports === use sui::object::{Self, UID, ID, uid_to_address}; - //use sui::transfer::{Self}; + use sui::transfer::{Self}; use sui::tx_context::{Self,TxContext}; use sui::linked_table::{LinkedTable}; //use dtp::service_type::{ServiceType}; use dtp::stats::{ConnectionAcceptedStats, ConnectionRejectedStats, ConnectionClosedStats}; - use dtp::weak_ref::{WeakRef}; + use dtp::weak_ref::{Self,WeakRef}; use dtp::consts::{Self}; //use dtp::errors::{Self}; @@ -82,7 +82,7 @@ module dtp::host { flgs: u8, // DTP version+esc flags always after UID. - owner: address, + creator: address, // Creation timestamp (UTC) // TODO @@ -117,23 +117,31 @@ module dtp::host { // === Public-Friend Functions === - public(friend) fun new(ctx: &mut TxContext) : Host { + public(friend) fun new(creator: address, ctx: &mut TxContext) : Host { Host { id: object::new(ctx), flgs: 0, - owner: tx_context::sender(ctx), + creator, config: HostConfig { max_con: consts::MAX_CONNECTION_PER_HOST(), }, } } - public(friend) fun owner(host: &Host): address { - host.owner + public(friend) fun new_transfered( creator: address, ctx: &mut TxContext ): WeakRef + { + let new_obj = new(creator,ctx); + let new_obj_ref = weak_ref::new_from_address(uid_to_address(&new_obj.id)); + transfer::share_object(new_obj); + new_obj_ref } - public(friend) fun is_caller_owner(host: &Host, ctx: &TxContext): bool { - tx_context::sender(ctx) == host.owner + public(friend) fun creator(host: &Host): address { + host.creator + } + + public(friend) fun is_caller_creator(host: &Host, ctx: &TxContext): bool { + tx_context::sender(ctx) == host.creator } // === Private Functions === @@ -142,7 +150,7 @@ module dtp::host { } -#[test_only] +#[test_only, allow(unused_field, unused_use)] module dtp::test_host { use sui::transfer; @@ -160,12 +168,10 @@ module dtp::test_host { { let ctx = test_scenario::ctx(scenario); - let new_host = host::new( ctx ); - + let _new_host_ref = host::new_transfered( creator, ctx ); + // admnistrator address must be the creator. - assert!(host::owner(&new_host) == creator, 1); - - transfer::share_object( new_host ); + //assert!(host::creator(&new_host) == creator, 1); }; test_scenario::end(scenario_val); diff --git a/move/sources/tests/tests_pipe.move b/move/sources/tests/tests_pipe.move index b0825b5..ed80dfb 100644 --- a/move/sources/tests/tests_pipe.move +++ b/move/sources/tests/tests_pipe.move @@ -7,7 +7,7 @@ module dtp::tests_pipe { // === Imports === - use sui::test_utils::assert_eq; + //use sui::test_utils::assert_eq; use sui::test_scenario::{Self as ts}; // === Errors === @@ -27,7 +27,7 @@ module dtp::tests_pipe { ts::next_tx(scn, user); { - let ctx = ts::ctx(scn); + let _ctx = ts::ctx(scn); //let sender = tx_context::sender(ctx); //let psd = dtp::pipe::new(ctx); diff --git a/move/sources/tests/tests_pipe_sync_data.move b/move/sources/tests/tests_pipe_sync_data.move index 2226377..f1f9d51 100644 --- a/move/sources/tests/tests_pipe_sync_data.move +++ b/move/sources/tests/tests_pipe_sync_data.move @@ -7,7 +7,7 @@ module dtp::tests_pipe_sync_data { // === Imports === - use sui::test_utils::assert_eq; + //use sui::test_utils::assert_eq; use sui::test_scenario::{Self as ts}; // === Errors === @@ -27,10 +27,10 @@ module dtp::tests_pipe_sync_data { ts::next_tx(scn, user); { - let ctx = ts::ctx(scn); + let _ctx = ts::ctx(scn); //let sender = tx_context::sender(ctx); - let psd = dtp::pipe_sync_data::new(); + let _psd = dtp::pipe_sync_data::new(); //assert_eq(psd.byte_payload_sent, 0); //assert_eq(psd.byte_header_sent, 0); //assert_eq(psd.send_call_completed, 0); diff --git a/move/sources/transport_control.move b/move/sources/transport_control.move index 18dc323..df4e2d9 100644 --- a/move/sources/transport_control.move +++ b/move/sources/transport_control.move @@ -25,7 +25,7 @@ module dtp::transport_control { use sui::object::{Self, ID, UID}; use sui::tx_context::{Self,TxContext}; - use sui::event; + use sui::transfer; use dtp::service_type::{Self}; @@ -36,9 +36,6 @@ module dtp::transport_control { #[test_only] friend dtp::test_transport_control; - struct BEConReq has copy, drop { - sender: address, // Sender Requesting a Connection - } // Control Plane of a connection. // @@ -118,8 +115,8 @@ module dtp::transport_control { service_idx, client_host: weak_ref::new_from_obj(client_host), server_host: weak_ref::new_from_obj(server_host), - client_addr: host::owner(client_host), - server_addr: host::owner(server_host), + client_addr: host::creator(client_host), + server_addr: host::creator(server_host), client_tx_pipe: weak_ref::new_empty(), server_tx_pipe: weak_ref::new_empty(), }; @@ -191,7 +188,7 @@ module dtp::transport_control { ctx: &mut TxContext ) { // Sender must be the owner of the client_host. - assert!(host::is_caller_owner(client_host, ctx), errors::EHostNotOwner()); + assert!(host::is_caller_creator(client_host, ctx), errors::EHostNotOwner()); // Create the TransportControl/Pipes/InnerPipes // @@ -203,10 +200,9 @@ module dtp::transport_control { let tc = dtp::transport_control::new(service_idx, client_host, server_host, ctx); // Emit the "Connection Request" Move event. - // The server will see the sender object therefore will know the TC and plenty of info! - event::emit(BEConReq { - sender: client_address(&tc), // Allows further off-chain filtering/firewall. - } ); + // The server will see the sender address therefore will know the TC and plenty of info! + let tc_address = object::id_to_address( object::borrow_id(&tc) ); + dtp::events::emit_con_req( tc_address, client_address(&tc) ); transfer::share_object(tc); } @@ -257,72 +253,45 @@ module dtp::test_transport_control { use std::option::{Self}; use sui::transfer; - use sui::test_scenario::{Self}; + use sui::test_scenario::{Scenario}; + use sui::test_scenario as ts; use sui::object; use dtp::pipe::{Self}; use dtp::transport_control::{Self}; // DUT - use dtp::host::{Self, Host}; + use dtp::host::{Self}; - #[test] - fun test_instantiation() { - let creator = @0x1; - let scenario_val = test_scenario::begin(creator); - let scenario = &mut scenario_val; - - let fake_client_address = @0x20; - let fake_server_address = @0x30; - let fake_client_pipe_id; - let fake_server_pipe_id; - - test_scenario::next_tx(scenario, creator); + fun create_hosts(scenario: &mut Scenario) { + ts::next_tx(scenario, @0x10); { - let ctx = test_scenario::ctx(scenario); - - fake_client_pipe_id = pipe::create_internal(ctx, fake_client_address); - fake_server_pipe_id = pipe::create_internal(ctx, fake_client_address); - - let fake_client_host = host::new(ctx); - let fake_client_host_id = object::id(&fake_client_host); - - let fake_server_host = host::new(ctx); - let fake_server_host_id = object::id(&fake_server_host); - - let port : u16 = 0 ; - let protocol : u16 = 0; - let return_port : u16 = 0; - let tc = transport_control::new( - option::some(fake_client_host_id), fake_server_host_id, - option::some(fake_client_address), fake_server_address, - option::some(fake_client_pipe_id), option::some(fake_server_pipe_id), - protocol, option::some(port), option::some(return_port), - ctx ); - - // Test accessors - assert!(transport_control::server_addr(&tc) == fake_server_address, 1); - - //debug::print(&tc); - - transfer::share_object( fake_client_host ); - transfer::share_object( fake_server_host ); - transfer::share_object( tc ); + let sender = ts::sender(scenario); + let ctx = ts::ctx(scenario); + let client_host = host::new_transfered(sender, ctx); }; - - // Destruction - // TODO Revisit this once shared object deletion works!? - /* - test_scenario::next_tx(scenario, node_creator); + + ts::next_tx(scenario, @0x20); { - let tc = test_scenario::take_shared(scenario); - let fake_client_pipe = test_scenario::take_shared_by_id(scenario,fake_client_pipe_id); - let fake_server_pipe = test_scenario::take_shared_by_id(scenario,fake_server_pipe_id); + let sender = ts::sender(scenario); + let ctx = ts::ctx(scenario); + let _server_host = host::new_transfered(sender,ctx); + }; + } - transport_control::delete(tc); - pipe::delete(fake_client_pipe); - pipe::delete(fake_server_pipe); - };*/ - test_scenario::end(scenario_val); + #[test] + fun test_create() { + let scenario_val = ts::begin(@0x10); + let scenario = &mut scenario_val; + + create_hosts(scenario); + + ts::next_tx(scenario, @0x10); + { + // Client creates a connection. + let _ctx = ts::ctx(scenario); + }; + + ts::end(scenario_val); } }