mirror of
https://github.com/fluencelabs/examples
synced 2024-12-04 19:20:17 +00:00
.. | ||
aqua | ||
dist/src | ||
docker | ||
src | ||
package-lock.json | ||
package.json | ||
README.md | ||
tsconfig.json |
Test Client
Overview
In aqua/timestamp_gatherer.aqua
we provide a simple timestamp getter function, ts_getter
. ts_getter
illustrates how to use the builtin timstamp function with Aqua.
-- aqua/timestamp_gatherer.aqua
-- simple timestamp getter for kademlia neighborhood which is max size 20
func ts_getter() -> []u64:
-- on this peer
on HOST_PEER_ID:
-- convert peer id to b58
k <- Op.string_to_b58(HOST_PEER_ID)
-- get all neighbors
nodes <- Kademlia.neighborhood(k, nil, nil)
res: *u64
-- for each neighbor
for n <- nodes par:
-- on selected neighbor peer
on n:
-- get the timestamp from that node
res <- Peer.timestamp_ms()
-- hot fix to force switching to peer
Op.noop()
-- join the results, which is tricky right now since we can't use array len
-- the testnet size n is 10 so n -1
join res[9]
<- res
This function works well under optimal conditions but is problematic in reality where peers drop-off or disappear. We can use timeouts and some creative array filling to accommodate response variability while still getting our join to complete:
-- timestamp getter with error collector over neighborhood
func ts_getter_with_timeout()-> []u64, []string:
-- timeout in ms
rtt = 1000
res: *u64
-- error value for no timestamp
err_value = 0
-- neighborhood n = 20 decr by 1 for array
n_neighborhood = 19
-- err message
msg = "timeout"
-- collect non-responsive peer ids, if any
dead_peers: *string
on HOST_PEER_ID:
k <- Op.string_to_b58(HOST_PEER_ID)
nodes <- Kademlia.neighborhood(k, nil, nil)
for n <- nodes par:
status: *string
on n:
res <- Peer.timestamp_ms()
status <<- "success"
par status <- Peer.timeout(rtt, msg)
if status! != "success":
res <<- err_value
dead_peers <<- n
Op.noop()
join res[n_neighborhood]
<- res, dead_peers
We can execute Aqua with the Fluence JS client:
npm i
npm run compile-aqua
npm start
or from the command line:
aqua run \
--addr /dns4/kras-05.fluence.dev/tcp/19001/wss/p2p/12D3KooWCMr9mU894i8JXAFqpgoFtx6qnV1LFPSfVc3Y34N4h4LS \
-i aqua \
-f 'ts_getter()'
and:
aqua run \
--addr /dns4/kras-05.fluence.dev/tcp/19001/wss/p2p/12D3KooWCMr9mU894i8JXAFqpgoFtx6qnV1LFPSfVc3Y34N4h4LS \
-i aqua \
-f 'ts_getter_with_timeout()'