diff --git a/src/aqua/builtin.aqua b/src/aqua/builtin.aqua new file mode 100644 index 0000000..21cc653 --- /dev/null +++ b/src/aqua/builtin.aqua @@ -0,0 +1,150 @@ +alias Field : []string +alias Argument : []string +alias Bytes : []u8 +alias PeerId : string + +data Service: + id: string + blueprint_id: string + owner_id: string + +data FunctionSignature: + arguments: []Argument + name: string + output_types: []string + +data RecordType: + fields: []Field + id: u64 + name: string + +data Interface: + function_signatures: []FunctionSignature + record_types: []RecordType + +data ServiceInfo: + blueprint_id: string + service_id: string + interface: Interface + +data Info: + external_addresses: []string + +data ModuleConfig: + name: string + +data Module: + name: string + hash: string + config: ModuleConfig + +data AddBlueprint: + name: string + dependencies: []string + +data Blueprint: + id: string + name: string + dependencies: []string + +data ScriptInfo: + id: string + src: string + failures: u32 + interval: string + owner: string + +data Contact: + peer_id: string + addresses: []string + +service Op("op"): + identity: -> () + +service Peer("peer"): + -- Checks if there is a direct connection to the peer identified by a given PeerId + -- Argument: PeerId – id of the peer to check if there's a connection with + -- Returns: bool - true if connected to the peer, false otherwise + is_connected: PeerId -> bool + + -- Initiates a connection to the specified peer + -- Arguments: + -- PeerId – id of the target peer + -- [Multiaddr] – an array of target peer's addresses + -- Returns: bool - true if connection was successful + connect: PeerId, []string -> bool + -- Resolves the contact of a peer via Kademlia + -- Argument: PeerId – id of the target peer + -- Returns: Contact - true if connection was successful + get_contact: PeerId -> Contact + + -- Get information about the peer + identify: -> Info + + -- Get Unix timestamp in milliseconds + timestamp_ms: -> u64 + + -- Get Unix timestamp in seconds + timestamp_sec: -> u64 + +service Kademlia("kad"): + -- Instructs node to return the locally-known nodes + -- in the Kademlia neighborhood for a given key + neighborhood: PeerId -> []PeerId + +service Srv("srv"): + -- Used to create a service on a certain node + -- Arguments: + -- blueprint_id – ID of the blueprint that has been added to the node specified in the service call by the dist add_blueprint service. + -- Returns: service_id – the service ID of the created service. + create: string -> string + + -- Returns a list of services running on a peer + list: -> []Service + + -- Adds an alias on service, so, service could be called + -- not only by service_id but by alias as well. + -- Argument: + -- alias - settable service name + -- service_id – ID of the service whose interface you want to name. + add_alias: string, string -> () + + -- Retrieves the functional interface of a service running + -- on the node specified in the service call + -- Argument: service_id – ID of the service whose interface you want to retrieve. + get_interface: string -> ServiceInfo + +service Dist("dist"): + -- Used to add modules to the node specified in the service call + -- Arguments: + -- bytes – a base64 string containing the .wasm module to add. + -- config – module info + -- Returns: blake3 hash of the module + add_module: Bytes, ModuleConfig -> string + + -- Get a list of modules available on the node + list_modules: -> []Module + + -- Get the interface of a module + get_interface: string -> Interface + + -- Used to add a blueprint to the node specified in the service call + add_blueprint: AddBlueprint -> string + + -- Used to get the blueprints available on the node specified in the service call. + -- A blueprint is an object of the following structure + list_blueprints: -> []Blueprint + +service Script("script"): + -- Adds the given script to a node + add: string, string -> string + + -- Removes recurring script from a node. Only a creator of the script can delete it + remove: string -> bool + + -- Returns a list of existing scripts on the node. + -- Each object in the list is of the following structure + list: -> ScriptInfo + +func id(): + Op.identity() diff --git a/src/aqua/helloWorld.aqua b/src/aqua/helloWorld.aqua index 43a8b94..c61ee17 100644 --- a/src/aqua/helloWorld.aqua +++ b/src/aqua/helloWorld.aqua @@ -1,10 +1,11 @@ -data ExternalAddresses: - external_addresses: []string - -service Peer("peer"): - identify: -> ExternalAddresses +import "builtin.aqua" func getPeerExternalAddresses(otherNodePeerId: string) -> []string: on otherNodePeerId: res <- Peer.identify() - <- res.external_addresses \ No newline at end of file + <- res.external_addresses + +-- func getPeerExternalTimestamp(otherNodePeerId: string) -> u64: +-- on otherNodePeerId: +-- res <- Peer.timestamp_sec() +-- <- res diff --git a/src/compiled/builtin.ts b/src/compiled/builtin.ts new file mode 100644 index 0000000..0ad9162 --- /dev/null +++ b/src/compiled/builtin.ts @@ -0,0 +1,46 @@ +import { FluenceClient, PeerIdB58 } from '@fluencelabs/fluence'; +import { RequestFlowBuilder } from '@fluencelabs/fluence/dist/api.unstable'; + + + +export async function id(client: FluenceClient): Promise { + let request; + const promise = new Promise((resolve, reject) => { + request = new RequestFlowBuilder() + .withRawScript( + ` +(xor + (seq + (call %init_peer_id% ("getDataSrv" "relay") [] relay) + (call %init_peer_id% ("op" "identity") []) + ) + (call %init_peer_id% ("errorHandlingSrv" "error") [%last_error%]) +) + + `, + ) + .configHandler((h) => { + h.on('getDataSrv', 'relay', () => { + return client.relayPeerId; + }); + h.on('getRelayService', 'hasReleay', () => {// Not Used + return client.relayPeerId !== undefined; + }); + + + h.on('errorHandlingSrv', 'error', (args) => { + // assuming error is the single argument + const [err] = args; + reject(err); + }); + }) + .handleScriptError(reject) + .handleTimeout(() => { + reject('Request timed out'); + }) + .build(); + }); + await client.initiateFlow(request); + return promise; +} + \ No newline at end of file diff --git a/src/compiled/helloWorld.ts b/src/compiled/helloWorld.ts index bb93657..c2f822e 100644 --- a/src/compiled/helloWorld.ts +++ b/src/compiled/helloWorld.ts @@ -3,6 +3,49 @@ import { RequestFlowBuilder } from '@fluencelabs/fluence/dist/api.unstable'; +export async function id(client: FluenceClient): Promise { + let request; + const promise = new Promise((resolve, reject) => { + request = new RequestFlowBuilder() + .withRawScript( + ` +(xor + (seq + (call %init_peer_id% ("getDataSrv" "relay") [] relay) + (call %init_peer_id% ("op" "identity") []) + ) + (call %init_peer_id% ("errorHandlingSrv" "error") [%last_error%]) +) + + `, + ) + .configHandler((h) => { + h.on('getDataSrv', 'relay', () => { + return client.relayPeerId; + }); + h.on('getRelayService', 'hasReleay', () => {// Not Used + return client.relayPeerId !== undefined; + }); + + + h.on('errorHandlingSrv', 'error', (args) => { + // assuming error is the single argument + const [err] = args; + reject(err); + }); + }) + .handleScriptError(reject) + .handleTimeout(() => { + reject('Request timed out'); + }) + .build(); + }); + await client.initiateFlow(request); + return promise; +} + + + export async function getPeerExternalAddresses(client: FluenceClient, otherNodePeerId: string): Promise { let request; const promise = new Promise((resolve, reject) => { diff --git a/src/index.ts b/src/index.ts index dd6391b..f81f633 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,12 +2,20 @@ import { createClient } from "@fluencelabs/fluence"; import { testNet } from "@fluencelabs/fluence-network-environment"; -import { getPeerExternalAddresses } from "./compiled/helloWorld"; +import { + getPeerExternalAddresses, + // getPeerExternalTimestamp, +} from "./compiled/helloWorld"; const main = async () => { const client = await createClient(testNet[0]); + const addresses = await getPeerExternalAddresses(client, client.relayPeerId!); console.log("Relay external addresses: ", addresses); + + // const timestamp = await getPeerExternalTimestamp(client, client.relayPeerId!); + // console.log("Relay timestamp: ", timestamp); + client.disconnect(); };