Skip to content

Commit

Permalink
(#12) Add create_host to Move API
Browse files Browse the repository at this point in the history
  • Loading branch information
mario4tier committed Feb 29, 2024
1 parent dd382fa commit 0a11c3d
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 47 deletions.
36 changes: 18 additions & 18 deletions move/sources/api.move
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

module dtp::api {

// === Imports ===
Expand Down Expand Up @@ -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<u8>, ctx: &mut TxContext) : (address, vector<u8>)
{
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<u8>, ctx: &mut TxContext) : vector<u8>
{
let kvargs = kvalues::from_bytes(args);
Expand Down Expand Up @@ -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<u8>, args: &vector<u8>, ctx: &mut TxContext): vector<u8>
public fun send_request(service_idx: u8, data: &vector<u8>, ipipe: &mut InnerPipe, args: &vector<u8>, ctx: &mut TxContext): vector<u8>
{
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<u8>, seq_number: u64, args: &vector<u8>, ctx: &mut TxContext): vector<u8>
public fun send_response(service_idx: u8, data: &vector<u8>, seq_number: u64, ipipe: &mut InnerPipe, args: &vector<u8>, ctx: &mut TxContext): vector<u8>
{
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<u8>, args: &vector<u8>, ctx: &mut TxContext): vector<u8>
public fun send_notification(service_idx: u8, data: &vector<u8>, ipipe: &mut InnerPipe, args: &vector<u8>, ctx: &mut TxContext): vector<u8>
{
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)
}

Expand Down
19 changes: 14 additions & 5 deletions move/sources/api_impl.move
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ 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};
use dtp::pipe::{Pipe};
use dtp::inner_pipe::{Self,InnerPipe};
use dtp::kvalues::{Self,KValues};

use dtp::weak_ref::{Self};

// === Friends ===
friend dtp::api;

Expand All @@ -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()
}

Expand Down Expand Up @@ -88,23 +97,23 @@ 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<u8>, _kvargs: &KValues, _ctx: &mut TxContext): KValues
public(friend) fun send_request(_service_idx: u8, _data: &vector<u8>, _ipipe: &InnerPipe, _kvargs: &KValues, _ctx: &mut TxContext): KValues
{
kvalues::new()
}

// 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<u8>, _seq_number: u64, _kvargs: &KValues, _ctx: &mut TxContext): KValues
public(friend) fun send_response(_service_idx: u8, _data: &vector<u8>, _seq_number: u64, _ipipe: &InnerPipe, _kvargs: &KValues, _ctx: &mut TxContext): KValues
{
kvalues::new()
}

// 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<u8>, _kvargs: &KValues, _ctx: &mut TxContext): KValues
public(friend) fun send_notification(_service_idx: u8, _data: &vector<u8>, _ipipe: &InnerPipe, _kvargs: &KValues, _ctx: &mut TxContext): KValues
{
kvalues::new()
}
Expand Down
2 changes: 1 addition & 1 deletion move/sources/errors.move
Original file line number Diff line number Diff line change
Expand Up @@ -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 ===
Expand Down
41 changes: 23 additions & 18 deletions move/sources/host.move
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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.
//
Expand All @@ -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
Expand Down Expand Up @@ -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(),
},
Expand Down Expand Up @@ -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 ===
Expand All @@ -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);
Expand Down
21 changes: 19 additions & 2 deletions move/sources/kvalues.move
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>}
// TODO Implement BCS + typescript and rust codec (helpers).
struct KValues has copy, store, drop {
keys_bool: vector<String>,

Expand Down Expand Up @@ -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 );
Expand All @@ -71,6 +71,7 @@ module dtp::kvalues {
}

public fun get_u64( self: &KValues, key: &String ): Option<u64> {
// Linear search, acceptable assuming small number of keys.
let i: u64 = 0;
let ret_value = option::none<u64>();
let length = vector::length( &self.keys_u64 );
Expand All @@ -85,6 +86,22 @@ module dtp::kvalues {
ret_value
}

public fun get_str( self: &KValues, key: &String ): Option<String> {
// Linear search, acceptable assuming small number of keys.
let i: u64 = 0;
let ret_value = option::none<String>();
let length = vector::length( &self.keys_str );
while (i < length) {
if (vector::borrow<String>(&self.values_str, i) == key) {
let value = vector::borrow<String>(&self.values_str, i);
ret_value = option::some<String>(*value);
break
};
i = i + 1;
};
ret_value
}

// === Private Functions ===

// === Test Functions ===
Expand Down
6 changes: 3 additions & 3 deletions move/sources/transport_control.move
Original file line number Diff line number Diff line change
Expand Up @@ -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()
};
Expand Down Expand Up @@ -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
//
Expand Down
1 change: 1 addition & 0 deletions move/sources/weak_ref.move
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ module dtp::weak_ref {
friend dtp::pipe;
friend dtp::inner_pipe;
friend dtp::host;
friend dtp::api_impl;

// === Errors ===

Expand Down

0 comments on commit 0a11c3d

Please sign in to comment.