From 0a11c3dfcee67e74fb251a33a470206eba7c47f5 Mon Sep 17 00:00:00 2001 From: mario4tier Date: Wed, 28 Feb 2024 19:14:42 -0500 Subject: [PATCH] (#12) Add create_host to Move API --- move/sources/api.move | 36 ++++++++++++------------- move/sources/api_impl.move | 19 +++++++++---- move/sources/errors.move | 2 +- move/sources/host.move | 41 ++++++++++++++++------------- move/sources/kvalues.move | 21 +++++++++++++-- move/sources/transport_control.move | 6 ++--- move/sources/weak_ref.move | 1 + 7 files changed, 79 insertions(+), 47 deletions(-) diff --git a/move/sources/api.move b/move/sources/api.move index fa89c74..7162a57 100644 --- a/move/sources/api.move +++ b/move/sources/api.move @@ -1,3 +1,4 @@ + module dtp::api { // === Imports === @@ -27,19 +28,18 @@ module dtp::api { // === Public-Friend Functions === - // Functions to add services to an Host. - // Cannot change the signature. Can you change the implementation? - - // WebApp Old Contract 1 <--- Still working with Contract 2, just by changing the address of the contract. - // New Contract 2 - // - // - // public: more composeable - // - // entry: Cannot be called by other packages. - // - // public entry: Callable from CLI, Explorer etc... + + // Create a new Host // + // Returns the "Host Address", which serve a similar purpose as an "IP address". + public fun create_host( args: &vector, ctx: &mut TxContext) : (address, vector) + { + let kvargs = kvalues::from_bytes(args); + let (host_addr, kvalues) = dtp::api_impl::create_host(&kvargs,ctx); + (host_addr, kvalues::to_bytes(&kvalues)) + } + + // Functions to add services to an Host. public fun add_service_ping(host: &mut Host, args: &vector, ctx: &mut TxContext) : vector { let kvargs = kvalues::from_bytes(args); @@ -75,30 +75,30 @@ module dtp::api { // Transmit a request toward the server. // // The encoding of the 'data' depends on the service. - public fun send_request(service_idx: u8, data: &vector, args: &vector, ctx: &mut TxContext): vector + public fun send_request(service_idx: u8, data: &vector, ipipe: &mut InnerPipe, args: &vector, ctx: &mut TxContext): vector { let kvargs = kvalues::from_bytes(args); - let ret_value = dtp::api_impl::send_request(service_idx, data, &kvargs, ctx); + let ret_value = dtp::api_impl::send_request(service_idx, data, ipipe, &kvargs, ctx); kvalues::to_bytes(&ret_value) } // Transmit a response toward the client. // // The encoding of the 'data' depends on the service. - public fun send_response(service_idx: u8, data: &vector, seq_number: u64, args: &vector, ctx: &mut TxContext): vector + public fun send_response(service_idx: u8, data: &vector, seq_number: u64, ipipe: &mut InnerPipe, args: &vector, ctx: &mut TxContext): vector { let kvargs = kvalues::from_bytes(args); - let ret_value = dtp::api_impl::send_response(service_idx, data, seq_number, &kvargs, ctx); + let ret_value = dtp::api_impl::send_response(service_idx, data, seq_number, ipipe, &kvargs, ctx); kvalues::to_bytes(&ret_value) } // Transmit a notification toward the peer (no response expected). // // The encoding of the 'data' depends on the service. - public fun send_notification(service_idx: u8, data: &vector, args: &vector, ctx: &mut TxContext): vector + public fun send_notification(service_idx: u8, data: &vector, ipipe: &mut InnerPipe, args: &vector, ctx: &mut TxContext): vector { let kvargs = kvalues::from_bytes(args); - let ret_value = dtp::api_impl::send_notification(service_idx, data, &kvargs, ctx); + let ret_value = dtp::api_impl::send_notification(service_idx, data, ipipe,&kvargs, ctx); kvalues::to_bytes(&ret_value) } diff --git a/move/sources/api_impl.move b/move/sources/api_impl.move index 44ce6cc..8430909 100644 --- a/move/sources/api_impl.move +++ b/move/sources/api_impl.move @@ -3,7 +3,7 @@ module dtp::api_impl { // === Imports === use sui::tx_context::{TxContext}; - use dtp::host::{Host}; + use dtp::host::{Self,Host}; use dtp::transport_control::{Self}; use dtp::conn_objects::{Self,ConnObjects}; use dtp::transport_control::{TransportControl}; @@ -11,6 +11,8 @@ module dtp::api_impl { use dtp::inner_pipe::{Self,InnerPipe}; use dtp::kvalues::{Self,KValues}; + use dtp::weak_ref::{Self}; + // === Friends === friend dtp::api; @@ -28,10 +30,17 @@ module dtp::api_impl { // === Public-Friend Functions === - + // Functions to add services to an Host. + public(friend) fun create_host(_kvargs: &KValues, ctx: &mut TxContext) : (address, KValues) + { + let host_ref = host::new_transfered(ctx); + (weak_ref::get_address(&host_ref), kvalues::new()) + } + // Functions to add services to an Host. public(friend) fun add_service_ping(_host: &mut Host, _kvargs: &KValues, _ctx: &mut TxContext) : KValues { + kvalues::new() } @@ -88,7 +97,7 @@ module dtp::api_impl { // Transmit a request toward the server. // // The encoding of the 'data' depends on the service. - public(friend) fun send_request(_service_idx: u8, _data: &vector, _kvargs: &KValues, _ctx: &mut TxContext): KValues + public(friend) fun send_request(_service_idx: u8, _data: &vector, _ipipe: &InnerPipe, _kvargs: &KValues, _ctx: &mut TxContext): KValues { kvalues::new() } @@ -96,7 +105,7 @@ module dtp::api_impl { // Transmit a response toward the client. // // The encoding of the 'data' depends on the service. - public(friend) fun send_response(_service_idx: u8, _data: &vector, _seq_number: u64, _kvargs: &KValues, _ctx: &mut TxContext): KValues + public(friend) fun send_response(_service_idx: u8, _data: &vector, _seq_number: u64, _ipipe: &InnerPipe, _kvargs: &KValues, _ctx: &mut TxContext): KValues { kvalues::new() } @@ -104,7 +113,7 @@ module dtp::api_impl { // Transmit a notification toward the peer (no response expected). // // The encoding of the 'data' depends on the service. - public(friend) fun send_notification(_service_idx: u8, _data: &vector, _kvargs: &KValues, _ctx: &mut TxContext): KValues + public(friend) fun send_notification(_service_idx: u8, _data: &vector, _ipipe: &InnerPipe, _kvargs: &KValues, _ctx: &mut TxContext): KValues { kvalues::new() } diff --git a/move/sources/errors.move b/move/sources/errors.move index 68da776..3881260 100644 --- a/move/sources/errors.move +++ b/move/sources/errors.move @@ -15,7 +15,7 @@ module dtp::errors { public fun EPipeInstanceSame() : u64 { 4 } public fun EServiceIdxOutOfRange() : u64 { 5 } public fun EInvalidAccessOnNone() : u64 { 6 } - public fun EHostNotOwner() : u64 { 7 } + public fun ENotClientHostAuthority() : u64 { 7 } public fun EInvalidPipeCount() : u64 { 8 } // === Structs === diff --git a/move/sources/host.move b/move/sources/host.move index 1034722..f87b9d4 100644 --- a/move/sources/host.move +++ b/move/sources/host.move @@ -21,16 +21,15 @@ module dtp::host { // dtp::transport_control // dtp::pipe // dtp::inner_pipe - use dtp::stats::{Self,ConnAcceptedStats, ConnRejectedStats, ConnClosedStats}; use dtp::weak_ref::{Self,WeakRef}; use dtp::consts::{Self}; use dtp::kvalues::{KValues}; //use dtp::errors::{Self}; - // === Friends === + friend dtp::api_impl; friend dtp::transport_control; #[test_only] @@ -69,7 +68,7 @@ module dtp::host { } struct HostConfig has copy, drop, store { - // Configurations that can be changed only by the AdminCap. + // Configurations that can be changed only by the admin authority. // Maximum number of connection allowed for the whole host. // @@ -88,9 +87,12 @@ module dtp::host { struct Host has key, store { id: UID, - flgs: u8, // DTP version+esc flags always after UID. - - creator: address, + // Admin authority has additional capability to modify this Host object. + // + // Initialized with the sender address in host::new(). + // + // There is no plan to transfer authority. + authority: address, // Creation timestamp (UTC) // TODO @@ -132,9 +134,8 @@ module dtp::host { public(friend) fun new(ctx: &mut TxContext) : Host { Host { - id: object::new(ctx), - flgs: 0, - creator: tx_context::sender(ctx), + id: object::new(ctx), + authority: tx_context::sender(ctx), config: HostConfig { max_con: consts::MAX_CONNECTION_PER_HOST(), }, @@ -168,12 +169,16 @@ module dtp::host { // TODO Update/replace when already in table. } - public(friend) fun creator(host: &Host): address { - host.creator + public(friend) fun get_address(self: &Host): address { + uid_to_address(&self.id) + } + + public(friend) fun authority(host: &Host): address { + host.authority } - public(friend) fun is_caller_creator(host: &Host, ctx: &TxContext): bool { - tx_context::sender(ctx) == host.creator + public(friend) fun is_caller_authority(host: &Host, ctx: &TxContext): bool { + tx_context::sender(ctx) == host.authority } // === Private Functions === @@ -192,18 +197,18 @@ module dtp::test_host { #[test] fun test_instantiation() { - let creator = @0x1; - let scenario_val = test_scenario::begin(creator); + let authority = @0x1; + let scenario_val = test_scenario::begin(authority); let scenario = &mut scenario_val; - test_scenario::next_tx(scenario, creator); + test_scenario::next_tx(scenario, authority); { let ctx = test_scenario::ctx(scenario); let _new_host_ref = host::new_transfered( ctx ); - // admnistrator address must be the creator. - //assert!(host::creator(&new_host) == creator, 1); + // admnistrator address must be the authority. + //assert!(host::authority(&new_host) == authority, 1); }; test_scenario::end(scenario_val); diff --git a/move/sources/kvalues.move b/move/sources/kvalues.move index 6f29591..c0a9a42 100644 --- a/move/sources/kvalues.move +++ b/move/sources/kvalues.move @@ -15,8 +15,7 @@ module dtp::kvalues { // === Structs === - // TODO Consider BCS + typescript and rust codec (helpers). - // TODO Consider TLV bytes encoding --> {type: u8, length: u8, vector} + // TODO Implement BCS + typescript and rust codec (helpers). struct KValues has copy, store, drop { keys_bool: vector, @@ -57,6 +56,7 @@ module dtp::kvalues { } public fun get_bool( self: &KValues, key: &String ): bool { + // Linear search, acceptable assuming small number of keys. let i: u64 = 0; let ret_value = false; let length = vector::length( &self.keys_bool ); @@ -71,6 +71,7 @@ module dtp::kvalues { } public fun get_u64( self: &KValues, key: &String ): Option { + // Linear search, acceptable assuming small number of keys. let i: u64 = 0; let ret_value = option::none(); let length = vector::length( &self.keys_u64 ); @@ -85,6 +86,22 @@ module dtp::kvalues { ret_value } + public fun get_str( self: &KValues, key: &String ): Option { + // Linear search, acceptable assuming small number of keys. + let i: u64 = 0; + let ret_value = option::none(); + let length = vector::length( &self.keys_str ); + while (i < length) { + if (vector::borrow(&self.values_str, i) == key) { + let value = vector::borrow(&self.values_str, i); + ret_value = option::some(*value); + break + }; + i = i + 1; + }; + ret_value + } + // === Private Functions === // === Test Functions === diff --git a/move/sources/transport_control.move b/move/sources/transport_control.move index c187792..67ab55f 100644 --- a/move/sources/transport_control.move +++ b/move/sources/transport_control.move @@ -115,8 +115,8 @@ module dtp::transport_control { service_idx, cli_host: weak_ref::new_from_obj(cli_host), srv_host: weak_ref::new_from_obj(srv_host), - cli_addr: host::creator(cli_host), - srv_addr: host::creator(srv_host), + cli_addr: host::authority(cli_host), + srv_addr: host::authority(srv_host), cli_tx_pipe: weak_ref::new_empty(), srv_tx_pipe: weak_ref::new_empty() }; @@ -207,7 +207,7 @@ module dtp::transport_control { ctx: &mut TxContext ) { // Sender must be the owner of the cli_host. - assert!(host::is_caller_creator(cli_host, ctx), errors::EHostNotOwner()); + assert!(host::is_caller_authority(cli_host, ctx), errors::ENotClientHostAuthority()); // Create the TransportControl/Pipes/InnerPipes // diff --git a/move/sources/weak_ref.move b/move/sources/weak_ref.move index bbff0e1..11943ea 100644 --- a/move/sources/weak_ref.move +++ b/move/sources/weak_ref.move @@ -31,6 +31,7 @@ module dtp::weak_ref { friend dtp::pipe; friend dtp::inner_pipe; friend dtp::host; + friend dtp::api_impl; // === Errors ===