Skip to content

Commit

Permalink
(#12) Add dtp::api layer
Browse files Browse the repository at this point in the history
  • Loading branch information
mario4tier committed Feb 26, 2024
1 parent 30301f2 commit 8b875cb
Show file tree
Hide file tree
Showing 13 changed files with 537 additions and 196 deletions.
8 changes: 4 additions & 4 deletions crates/dtp-core/src/network/host_internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ pub struct HostMoveRaw {
id: UID,
flgs: u8,
adm: SuiAddress,
con_req: u64,
con_sdd: u64,
con_del: u64,
con_rcy: u64,
conn_req: u64,
conn_sdd: u64,
conn_del: u64,
conn_rcy: u64,
max_con: u16,
}

Expand Down
10 changes: 5 additions & 5 deletions crates/dtp-core/src/network/transport_control_internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub(crate) async fn create_best_effort_transport_control_on_network(
rpc: &SuiSDKParamsRPC,
txn: &SuiSDKParamsTxn,
localhost: &LocalhostInternal,
server_host: &HostInternal,
srv_host: &HostInternal,
_server_protocol: u16,
_server_port: Option<u16>,
_return_port: Option<u16>,
Expand All @@ -39,14 +39,14 @@ pub(crate) async fn create_best_effort_transport_control_on_network(
None => bail!(DTPError::DTPMissingSuiClient),
};

let server_adm = match server_host.admin_address() {
let server_adm = match srv_host.admin_address() {
Some(x) => x,
None => bail!(DTPError::DTPMissingServerAdminAddress),
};

/* Params must match. See tranport_control.move
client_host: ID,
server_host: ID,
cli_host: ID,
srv_host: ID,
server_adm: address,
protocol: u16,
port: u16,
Expand All @@ -55,7 +55,7 @@ pub(crate) async fn create_best_effort_transport_control_on_network(

let call_args = vec![
SuiJsonValue::from_object_id(localhost.object_id()),
SuiJsonValue::from_object_id(server_host.object_id()),
SuiJsonValue::from_object_id(srv_host.object_id()),
SuiJsonValue::from_str(&server_adm.to_string()).unwrap(),
SuiJsonValue::from_str("0").unwrap(),
SuiJsonValue::from_str("0").unwrap(),
Expand Down
16 changes: 8 additions & 8 deletions crates/dtp-core/src/types/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ pub struct PingStats {
pub ping_count_attempted: u8,
pub success_request: u8,
pub success_reply: u8,
pub connection_creation_time: u64, // milliseconds (can be zero)
pub min_round_trip_time: u64, // milliseconds
pub max_round_trip_time: u64, // milliseconds
pub avg_round_trip_time: u64, // milliseconds
pub min_gas_cost: u32, // Mist
pub avg_gas_cost: u32, // Mist
pub max_gas_cost: u32, // Mist
pub total_gas_cost: u32, // Mist
pub conn_creation_time: u64, // milliseconds (can be zero)
pub min_round_trip_time: u64, // milliseconds
pub max_round_trip_time: u64, // milliseconds
pub avg_round_trip_time: u64, // milliseconds
pub min_gas_cost: u32, // Mist
pub avg_gas_cost: u32, // Mist
pub max_gas_cost: u32, // Mist
pub total_gas_cost: u32, // Mist
}
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.18.0"
compiler-version = "1.18.1"
edition = "legacy"
flavor = "sui"
54 changes: 54 additions & 0 deletions move/sources/api.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
module dtp::api {

// === Imports ===
use sui::tx_context::{TxContext};

use dtp::kvalues::{Self};
use dtp::host::{Host};
use dtp::conn_objects::{ConnObjects};

// === Friends ===

// === Errors ===

// === Constants ===

// === Structs ===

// === Public-Mutative Functions ===

// === Public-View Functions ===

// === Admin Functions ===

// === Public-Friend Functions ===

// Functions to add services to an Host.
public entry fun add_service_ping(host: &mut Host, args: vector<u8>, ctx: &mut TxContext) : vector<u8>
{
let kvargs = kvalues::from_bytes(&args);
let ret_value = dtp::api_impl::add_service_ping(host, &kvargs, ctx);
kvalues::to_bytes(&ret_value)
}

// JSON-RPC 2.0 service
public entry fun add_service_json_rpc(host: &mut Host, args: vector<u8>, ctx: &mut TxContext) : vector<u8>
{
let kvargs = kvalues::from_bytes(&args);
let ret_value = dtp::api_impl::add_service_json_rpc(host, &kvargs, ctx);
kvalues::to_bytes(&ret_value)
}

// Returns IDs of objects needed to start exchanging data (TransportControl, Pipes, InnerPipes...).
public entry fun open_connection(service_idx: u8, cli_host: &mut Host, srv_host: &mut Host, args: vector<u8>, ctx: &mut TxContext): (ConnObjects, vector<u8>)
{
let kvargs = kvalues::from_bytes(&args);
let (conn_objects, ret_value) = dtp::api_impl::open_connection(service_idx, cli_host, srv_host, &kvargs, ctx);
(conn_objects, kvalues::to_bytes(&ret_value))
}

// === Private Functions ===

// === Test Functions ===

}
61 changes: 61 additions & 0 deletions move/sources/api_impl.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
module dtp::api_impl {

// === Imports ===
use sui::tx_context::{TxContext};

use dtp::host::{Host};
use dtp::transport_control::{Self};
use dtp::conn_objects::{Self,ConnObjects};
use dtp::kvalues::{Self,KValues};

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

// === Errors ===

// === Constants ===

// === Structs ===

// === Public-Mutative Functions ===

// === Public-View Functions ===

// === Admin Functions ===

// === Public-Friend Functions ===


// Functions to add services to an Host.
public(friend) fun add_service_ping(_host: &mut Host, _kvargs: &KValues, _ctx: &mut TxContext) : KValues
{
kvalues::new()
}

// JSON-RPC 2.0 service
public(friend) fun add_service_json_rpc(_host: &mut Host, _kvargs: &KValues, _ctx: &mut TxContext) : KValues
{
kvalues::new()
}


// Returns IDs of objects needed to start exchanging data (TransportControl, Pipes, InnerPipes...).
public(friend) fun open_connection(service_idx: u8, cli_host: &mut Host, srv_host: &mut Host, _kvargs: &KValues, ctx: &mut TxContext): (ConnObjects, KValues)
{
let conn = conn_objects::new();

// Create the connection. Will emit an event on success
transport_control::create_best_effort(service_idx, cli_host, srv_host, &mut conn, ctx);

// TODO Add references in Host object for slow discovery.
//host::add_connection(cli_host, &conn.transport_control);
//host::add_connection(srv_host, &conn.transport_control);

(conn, kvalues::new())
}

// === Private Functions ===

// === Test Functions ===

}
71 changes: 71 additions & 0 deletions move/sources/conn_objects.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
module dtp::conn_objects {
// === Imports ===
use std::vector;

// === Friends ===
friend dtp::api_impl;
friend dtp::host;
friend dtp::transport_control;
friend dtp::pipe;
friend dtp::inner_pipe;

// === Errors ===

// === Constants ===

// === Structs ===
struct ConnObjects has drop, copy, store {
// References to all objects needed to exchange data
// through a connection.
//
// If an end-point loose these references, they can be
// re-discovered using one of the related Host object.
tc: address, // TransportControl
cli_tx_pipe: address,
srv_tx_pipe: address,
cli_tx_ipipes: vector<address>,
srv_tx_ipipes: vector<address>,
}

// === Public-Mutative Functions ===

// === Public-View Functions ===

// === Admin Functions ===

// === Public-Friend Functions ===
public(friend) fun new(): ConnObjects {
ConnObjects{
tc: @0x0,
cli_tx_pipe: @0x0,
srv_tx_pipe: @0x0,
cli_tx_ipipes: vector::empty(),
srv_tx_ipipes: vector::empty(),
}
}

public(friend) fun set_tc(self: &mut ConnObjects, tc: address) {
self.tc = tc;
}

public(friend) fun set_cli_tx_pipe(self: &mut ConnObjects, cli_tx_pipe: address) {
self.cli_tx_pipe = cli_tx_pipe;
}

public(friend) fun set_srv_tx_pipe(self: &mut ConnObjects, srv_tx_pipe: address) {
self.srv_tx_pipe = srv_tx_pipe;
}

public(friend) fun add_cli_tx_ipipe(self: &mut ConnObjects, cli_tx_ipipe: address) {
vector::push_back(&mut self.cli_tx_ipipes, cli_tx_ipipe);
}

public(friend) fun add_srv_tx_ipipe(self: &mut ConnObjects, srv_tx_ipipe: address) {
vector::push_back(&mut self.srv_tx_ipipes, srv_tx_ipipe);
}

// === Private Functions ===

// === Test Functions ===

}
20 changes: 5 additions & 15 deletions move/sources/events.move
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
module dtp::events {
// === Imports ===
use sui::event;
use dtp::conn_objects::ConnObjects;

// === Friends ===
friend dtp::host;
friend dtp::inner_pipe;
Expand All @@ -15,19 +17,7 @@ module dtp::events {
// === Structs ===
struct ConReq has copy, drop {
service_idx: u8, // Service Type
cli_haddr: address, // Client Host address (requester of the connection).
srv_haddr: address, // Server Host address
tc_addr: address, // Transport Control

// Address of the first inner pipe addresses to use.
//
// This is for faster initial response time
// for some services (e.g. first ping).
//
// The server should find out about additional
// inner pipes with a tc_addr object read.
client_tx_ipipe: address,
server_tx_ipipe: address,
conn: ConnObjects, // All Connection Objects (TransportControl, Pipe etc...)
}


Expand All @@ -38,8 +28,8 @@ module dtp::events {
// === Admin Functions ===

// === Public-Friend Functions ===
public(friend) fun emit_con_req( service_idx: u8, cli_haddr: address, srv_haddr: address, tc_addr: address, client_tx_ipipe: address, server_tx_ipipe: address ) {
event::emit(ConReq { service_idx, cli_haddr, srv_haddr, tc_addr, client_tx_ipipe, server_tx_ipipe});
public(friend) fun emit_conn_req( service_idx: u8, conn: ConnObjects ) {
event::emit(ConReq { service_idx, conn });
}

// === Private Functions ===
Expand Down
Loading

0 comments on commit 8b875cb

Please sign in to comment.