Topology
Define where the code is to be executed and how to get there
Aqua lets developers describe the whole distributed workflow in a single script, link data, recover from errors, implement complex patterns like backpressure, and more. Hence, the network topology is at the heart of Aqua.
Topology in Aqua is declarative: You just need to say where (on what peer) a piece of code must be executed, and optionally how to get there. The Aqua compiler will add all the required network hops.

On expression

on expression moves execution to the specified peer:
1
on "my peer":
2
foo()
Copied!
Here, foo is instructed to be executed on a peer with id my peer. on supports variables of type string :
1
-- foo, bar, baz are instructed to be executed on myPeer
2
on myPeer:
3
foo()
4
bar()
5
baz()
Copied!
on does not add network hops on its own: if there are no service calls inside the on scope, the node will not be reached. Use via to affect the topology without service calls.

INIT_PEER_ID

There is one custom peer ID that is always in scope: INIT_PEER_ID. It points to the peer that initiated this request.
Using on INIT_PEER_ID is an anti-pattern: There is no way to ensure that init peer is accessible from the currently used part of the network.

HOST_PEER_ID

This constant is resolved on compilation time to point on the relay (the host the client is connected to) if Aqua is compiled to be used behind the relay (default mode, targets web browsers and other devices that needs a relay to receive incoming connections), and on INIT_PEER_ID otherwise.

More complex scenarios

Consider this example:
1
func foo():
2
on "peer foo":
3
do_foo()
4
​
5
func bar(i: i32):
6
do_bar()
7
​
8
func baz():
9
bar(1)
10
on "peer baz":
11
foo()
12
bar(2)
13
bar(3)
Copied!
Take a minute to think about:
    Where is do_foo executed?
    Where is bar(1) executed?
    On what node bar(2) runs?
    What about bar(3)?
Declarative topology definition always works the same way.
    do_foo is executed on "peer foo", always.
    bar(1) is executed on the same node where baz was running. If baz is the first called function, then it's INIT_PEER_ID.
    bar(2) is executed on "peer baz", despite the fact that foo does topologic transition. bar(2) is in the scope of on "peer baz", so it will be executed there
    bar(3) is executed where bar(1) was: in the root scope of baz, wherever it was called from

Accessing peers via other peers

In a distributed network it is quite common that a peer is not directly accessible. For example, a browser has no public network interface and you cannot open a socket to a browser at will. Such constraints warrant a relay pattern: there should be a well-connected peer that relays requests from a peer to the network and vice versa.
Relays are handled with via:
1
-- When we go to some peer from some other peer,
2
-- the compiler will add an additional hop to some relay
3
on "some peer" via "some relay":
4
foo()
5
​
6
-- More complex path: first go to relay2, then to relay1,
7
-- then to peer. When going out of peer, do it in reverse
8
on "peer" via relay1 via relay2:
9
foo()
10
​
11
-- You can pass any collection of strings to relay,
12
-- and it will go through it if it's defined,
13
-- or directly if not
14
func doViaRelayMaybe(peer: string, relayMaybe: ?string):
15
on peer via relayMaybe:
16
foo()
Copied!
ons nested or delegated in functions work just as you expect:
1
-- From where we are, -> relay1 -> peer1
2
on "peer1" via "relay1":
3
-- On peer1
4
foo()
5
-- now go -> relay1 -> relay2 -> peer2
6
-- going to relay1 to exit peer1
7
-- going to relay2 to enable access to peer2
8
on "peer2" via "relay2":
9
-- On peer2
10
foo()
11
-- This is executed in the root scope, after we were on peer2
12
-- How to get there?
13
-- Compiler knows the path that just worked
14
-- So it goes -> relay2 -> relay1 -> (where we were)
15
foo()
Copied!
With on and on ... via, significant indentation changes the place where the code will be executed, and paths that are taken when execution flow "bubbles up" (see the last call of foo). It's more efficient to keep the flow as flat as it could. Consider the following change of indentation in the previous script, and how it affects execution:
1
-- From where we are, -> relay1 -> peer1
2
on "peer1" via "relay1":
3
-- On peer1
4
foo()
5
-- now go -> relay1 -> relay2 -> peer2
6
-- going to relay1 to exit peer1
7
-- going to relay2 to enable access to peer2
8
on "peer2" via "relay2":
9
-- On peer2
10
foo()
11
-- This is executed in the root scope, after we were on peer2
12
-- How to get there?
13
-- Compiler knows the path that just worked
14
-- So it goes -> relay2 -> (where we were)
15
foo()
Copied!
When the on scope is ended, it does not affect any further topology moves. Until you stop indentation, on affects the topology and may add additional topology moves, which means more roundtrips and unnecessary latency.

Callbacks

What if you want to return something to the initial peer? For example, implement a request-response pattern. Or send a bunch of requests to different peers, and render responses as they come, in any order.
This can be done with callback arguments in the entry function:
1
func run(updateModel: Model -> (), logMessage: string -> ()):
2
on "some peer":
3
m <- fetchModel()
4
updateModel(m)
5
par on "other peer":
6
x <- getMessage()
7
logMessage(x)
Copied!
Callbacks have the arrow type.
If you pass just ordinary functions as arrow-type arguments, they will work as if you hardcode them.
1
func foo():
2
on "peer 1":
3
doFoo()
4
​
5
func bar(cb: -> ()):
6
on "peer2":
7
cb()
8
​
9
func baz():
10
-- foo will go to peer 1
11
-- bar will go to peer 2
12
bar(foo)
Copied!
If you pass a service call as a callback, it will be executed locally on the node where you called it. That might change.
Functions that capture the topologic context of the definition site are planned, not yet there. Proposed syntax:
1
func baz():
2
foo = do (x: u32):
3
-- Executed there, where foo is called
4
Srv.call(x)
5
<- x
6
-- When foo is called, it will get back to this context
7
bar(foo)
Copied!
`do` expression for closures Β· Issue #183 Β· fluencelabs/aqua
GitHub
Issue for adding `do` expression
Passing service function calls as arguments is very fragile as it does not track that the service is resolved in the scope of the call. Abilities variance may fix that.

Parallel execution and topology

When blocks are executed in parallel, it is not always necessary to resolve the topology to get to the next peer. The compiler will add topologic hops from the par branch only if data defined in that branch is used down the flow.
What if all branches do not return? Execution will halt. Be careful, use co if you don't care about the returned data.
Last modified 1mo ago