Skip to content

Commit

Permalink
(#12) Add events module + unit tests fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mario4tier committed Feb 22, 2024
1 parent 75f8f56 commit 11950d1
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 88 deletions.
2 changes: 1 addition & 1 deletion move/Move.lock
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ dependencies = [
]

[move.toolchain-version]
compiler-version = "1.19.0"
compiler-version = "1.18.1"
edition = "legacy"
flavor = "sui"
39 changes: 39 additions & 0 deletions move/sources/events.move
Original file line number Diff line number Diff line change
@@ -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 ===


}
36 changes: 21 additions & 15 deletions move/sources/host.move
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -82,7 +82,7 @@ module dtp::host {

flgs: u8, // DTP version+esc flags always after UID.

owner: address,
creator: address,

// Creation timestamp (UTC)
// TODO
Expand Down Expand Up @@ -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 ===
Expand All @@ -142,7 +150,7 @@ module dtp::host {

}

#[test_only]
#[test_only, allow(unused_field, unused_use)]
module dtp::test_host {

use sui::transfer;
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions move/sources/tests/tests_pipe.move
Original file line number Diff line number Diff line change
Expand Up @@ -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 ===
Expand All @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions move/sources/tests/tests_pipe_sync_data.move
Original file line number Diff line number Diff line change
Expand Up @@ -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 ===
Expand All @@ -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);
Expand Down
103 changes: 36 additions & 67 deletions move/sources/transport_control.move
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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.
//
Expand Down Expand Up @@ -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(),
};
Expand Down Expand Up @@ -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
//
Expand All @@ -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<TransportControl>(&tc) );
dtp::events::emit_con_req( tc_address, client_address(&tc) );
transfer::share_object(tc);
}

Expand Down Expand Up @@ -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<Host>(&fake_client_host);

let fake_server_host = host::new(ctx);
let fake_server_host_id = object::id<Host>(&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<TransportControl>(scenario);
let fake_client_pipe = test_scenario::take_shared_by_id<Pipe>(scenario,fake_client_pipe_id);
let fake_server_pipe = test_scenario::take_shared_by_id<Pipe>(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);
}

}

0 comments on commit 11950d1

Please sign in to comment.