2023-02-14 08:38:15 +04:00
|
|
|
import "@fluencelabs/aqua-lib/builtin.aqua"
|
|
|
|
|
|
|
|
export randomLoadBalancingEth, roundRobinEth, Counter, Logger
|
|
|
|
|
|
|
|
data EthResult:
|
2023-02-09 14:07:18 +04:00
|
|
|
value: string
|
|
|
|
success: bool
|
|
|
|
error: string
|
|
|
|
|
|
|
|
service Logger("logger"):
|
|
|
|
log(s: []string)
|
2023-02-14 08:38:15 +04:00
|
|
|
logCall(s: string)
|
|
|
|
|
|
|
|
service NumOp("op"):
|
|
|
|
identity(n: u64) -> i64
|
|
|
|
|
|
|
|
service Counter("counter"):
|
|
|
|
incrementAndReturn() -> u32
|
|
|
|
|
|
|
|
func empty() -> EthResult:
|
|
|
|
<- EthResult(value = "", success = true, error = "")
|
2023-02-09 14:07:18 +04:00
|
|
|
|
|
|
|
service EthCaller:
|
2023-02-14 08:38:15 +04:00
|
|
|
eth_call(uri: string, method: string, jsonArgs: []string) -> EthResult
|
2023-02-09 14:07:18 +04:00
|
|
|
|
2023-02-14 08:38:15 +04:00
|
|
|
func call(uri: string, method: string, jsonArgs: []string, serviceId: string) -> EthResult:
|
2023-02-09 14:07:18 +04:00
|
|
|
EthCaller serviceId
|
|
|
|
on HOST_PEER_ID:
|
2023-02-14 08:38:15 +04:00
|
|
|
res <- EthCaller.eth_call(uri, method, jsonArgs)
|
|
|
|
<- res
|
|
|
|
|
|
|
|
func randomLoadBalancing(uris: []string, method: string, jsonArgs: []string, serviceId: string, callFunc: string, string, []string, string -> EthResult) -> EthResult:
|
|
|
|
on INIT_PEER_ID:
|
|
|
|
time <- NumOp.identity(Peer.timestamp_sec())
|
|
|
|
providerNumber = time % Op.array_length(uris)
|
|
|
|
Logger.logCall(uris[providerNumber])
|
|
|
|
<- callFunc(uris[providerNumber], method, jsonArgs, serviceId)
|
|
|
|
|
|
|
|
func randomLoadBalancingEth(uris: []string, method: string, jsonArgs: []string, serviceId: string) -> EthResult:
|
|
|
|
<- randomLoadBalancing(uris, method, jsonArgs, serviceId, call)
|
|
|
|
|
|
|
|
func roundRobin(uris: []string, method: string, jsonArgs: []string, serviceId: string, counterServiceId: string, counterPeerId: string, callFunc: string, string, []string, string -> EthResult) -> EthResult:
|
|
|
|
on counterPeerId:
|
|
|
|
Counter counterServiceId
|
|
|
|
requestNumber <- Counter.incrementAndReturn()
|
|
|
|
on INIT_PEER_ID:
|
|
|
|
providerNumber = requestNumber % Op.array_length(uris)
|
|
|
|
Logger.logCall(uris[providerNumber])
|
|
|
|
<- callFunc(uris[providerNumber], method, jsonArgs, serviceId)
|
|
|
|
|
|
|
|
func roundRobinEth(uris: []string, method: string, jsonArgs: []string, serviceId: string, counterServiceId: string, counterPeerId: string) -> EthResult:
|
|
|
|
<- roundRobin(uris, method, jsonArgs, serviceId, counterServiceId, counterPeerId, call)
|