2022-07-11 19:26:08 -05:00
|
|
|
import "@fluencelabs/aqua-lib/math.aqua"
|
|
|
|
import "@fluencelabs/aqua-ipfs/ipfs-api.aqua"
|
|
|
|
import "@fluencelabs/aqua-ipfs/ipfs.aqua"
|
|
|
|
|
2022-06-29 20:42:36 -05:00
|
|
|
data FunctionAddress:
|
|
|
|
peer_id: string
|
|
|
|
service_id: string
|
|
|
|
|
|
|
|
data ProviderInfo:
|
|
|
|
name: string
|
|
|
|
url: string
|
|
|
|
|
|
|
|
data EVMResult:
|
|
|
|
provider: string
|
|
|
|
stdout: string
|
|
|
|
stderr: string
|
|
|
|
|
2023-02-21 11:30:20 +04:00
|
|
|
service I64ToF64("op"):
|
2022-06-29 20:42:36 -05:00
|
|
|
identity(x: i64) -> f64
|
|
|
|
|
2023-02-21 11:30:20 +04:00
|
|
|
service ProviderInfoOp("op"):
|
|
|
|
array_length(providers: []ProviderInfo) -> i64
|
|
|
|
|
|
|
|
service FuncAddrOp("op"):
|
2022-06-29 20:42:36 -05:00
|
|
|
array_length(providers: []FunctionAddress) -> i64
|
|
|
|
|
|
|
|
service MultiProviderQuery("service-id"):
|
2023-02-10 18:58:45 +07:00
|
|
|
get_block_number(provider: ProviderInfo) -> EVMResult
|
2022-06-29 20:42:36 -05:00
|
|
|
|
2022-07-11 19:26:08 -05:00
|
|
|
service Utilities("service_id"):
|
|
|
|
kv_to_u64(kv: string, k: string) -> u64
|
|
|
|
|
|
|
|
service Console("run-console"):
|
2023-02-21 11:30:20 +04:00
|
|
|
print(msg: string)
|
|
|
|
|
|
|
|
alias QuorumService: FunctionAddress
|
|
|
|
|
|
|
|
data Quorum:
|
|
|
|
n: u32
|
|
|
|
mode: u64
|
|
|
|
freq: u32
|
|
|
|
err_str: string
|
|
|
|
|
|
|
|
service SimpleQuorum("service-id"):
|
|
|
|
point_estimate(data: []EVMResult, min_points: u32) -> Quorum
|
|
|
|
is_quorum(x:u64, y:u64, threshold:f64) -> bool
|
2022-07-11 19:26:08 -05:00
|
|
|
|
|
|
|
|
2022-06-29 20:42:36 -05:00
|
|
|
func get_block_heights(providers: []ProviderInfo, addrs: []FunctionAddress) -> []EVMResult:
|
|
|
|
result: *EVMResult
|
2023-02-10 18:58:45 +07:00
|
|
|
|
2023-02-21 11:30:20 +04:00
|
|
|
n <- ProviderInfoOp.array_length(providers)
|
|
|
|
m <- FuncAddrOp.array_length(addrs)
|
2022-06-29 20:42:36 -05:00
|
|
|
|
|
|
|
if n > 0:
|
|
|
|
for addr <- addrs par:
|
|
|
|
on addr.peer_id:
|
|
|
|
MultiProviderQuery addr.service_id
|
|
|
|
for provider <- providers:
|
|
|
|
result <- MultiProviderQuery.get_block_number(provider)
|
2022-09-06 15:17:25 -05:00
|
|
|
join result[n*m-1]
|
2022-06-29 20:42:36 -05:00
|
|
|
<- result
|
|
|
|
|
|
|
|
|
|
|
|
func get_block_height(providers: []ProviderInfo, addr: FunctionAddress) ->[]EVMResult:
|
|
|
|
result: *EVMResult
|
2023-02-21 11:30:20 +04:00
|
|
|
n <- ProviderInfoOp.array_length(providers)
|
2023-02-10 18:58:45 +07:00
|
|
|
|
2022-06-29 20:42:36 -05:00
|
|
|
if n > 0:
|
|
|
|
on addr.peer_id:
|
|
|
|
MultiProviderQuery addr.service_id
|
|
|
|
for provider <- providers:
|
|
|
|
result <- MultiProviderQuery.get_block_number(provider)
|
|
|
|
join result[n]
|
|
|
|
<- result
|
|
|
|
|
|
|
|
|
|
|
|
func provider_test(providers: []ProviderInfo) -> []string:
|
|
|
|
result: *string
|
2023-02-21 11:30:20 +04:00
|
|
|
n <- ProviderInfoOp.array_length(providers)
|
2023-02-10 18:58:45 +07:00
|
|
|
|
2022-06-29 20:42:36 -05:00
|
|
|
if n > 0:
|
|
|
|
for provider <- providers:
|
|
|
|
result <<- provider.name
|
|
|
|
join result[n-1]
|
|
|
|
<- result
|
|
|
|
|
|
|
|
func get_block_height_raw_quorum(providers: []ProviderInfo, addrs: []FunctionAddress, q_addr: QuorumService) -> Quorum:
|
|
|
|
result: *EVMResult
|
|
|
|
result2: *string
|
|
|
|
quorum: *Quorum
|
2023-02-10 18:58:45 +07:00
|
|
|
|
2023-02-21 11:30:20 +04:00
|
|
|
n <- ProviderInfoOp.array_length(providers)
|
|
|
|
n2 <- FuncAddrOp.array_length(addrs)
|
2022-06-29 20:42:36 -05:00
|
|
|
|
|
|
|
if n > 0:
|
|
|
|
for addr <- addrs par:
|
|
|
|
on addr.peer_id:
|
|
|
|
MultiProviderQuery addr.service_id
|
|
|
|
for provider <- providers:
|
|
|
|
result <- MultiProviderQuery.get_block_number(provider)
|
|
|
|
result2 <<- provider.name
|
|
|
|
-- join result[n2-1]
|
|
|
|
join result[n*n2-1]
|
2023-02-10 18:58:45 +07:00
|
|
|
|
2022-06-29 20:42:36 -05:00
|
|
|
on q_addr.peer_id:
|
|
|
|
SimpleQuorum q_addr.service_id
|
|
|
|
quorum <-SimpleQuorum.point_estimate(result, 3)
|
|
|
|
|
|
|
|
<- quorum[0]
|
|
|
|
|
|
|
|
|
2022-07-11 19:26:08 -05:00
|
|
|
service ConsoleEVMResults("run-console"):
|
|
|
|
print(result :[]EVMResult)
|
|
|
|
|
|
|
|
service ConsoleEVMResult("run-console"):
|
|
|
|
print(result :EVMResult)
|
|
|
|
|
|
|
|
service ConsoleQuorum("run-console"):
|
|
|
|
print(quorum :[]Quorum)
|
|
|
|
|
2022-06-29 20:42:36 -05:00
|
|
|
func get_block_height_quorum(providers: []ProviderInfo, addrs: []FunctionAddress, q_addr: QuorumService, t_quorum: f64) -> Quorum, bool:
|
|
|
|
result: *EVMResult
|
|
|
|
quorum: *Quorum
|
|
|
|
is_quorum: *bool
|
2023-02-10 18:58:45 +07:00
|
|
|
min_points = 3 -- minimum points we want in order to calculate an oracle
|
2022-06-29 20:42:36 -05:00
|
|
|
|
2023-02-21 11:30:20 +04:00
|
|
|
n <- ProviderInfoOp.array_length(providers)
|
|
|
|
n2 <- FuncAddrOp.array_length(addrs)
|
2022-06-29 20:42:36 -05:00
|
|
|
|
|
|
|
if n > 0:
|
|
|
|
for addr <- addrs par:
|
|
|
|
on addr.peer_id:
|
|
|
|
MultiProviderQuery addr.service_id
|
|
|
|
for provider <- providers:
|
|
|
|
result <- MultiProviderQuery.get_block_number(provider)
|
2022-07-11 19:26:08 -05:00
|
|
|
join result[n*n2-2]
|
2023-02-10 18:58:45 +07:00
|
|
|
|
2022-06-29 20:42:36 -05:00
|
|
|
on q_addr.peer_id:
|
|
|
|
SimpleQuorum q_addr.service_id
|
|
|
|
quorum <-SimpleQuorum.point_estimate(result, min_points)
|
|
|
|
is_quorum <- SimpleQuorum.is_quorum(quorum[0].freq, quorum[0].n, t_quorum)
|
2023-02-10 18:58:45 +07:00
|
|
|
|
2022-06-29 20:42:36 -05:00
|
|
|
<- quorum[0], is_quorum[0]
|
|
|
|
|
2022-07-11 19:26:08 -05:00
|
|
|
func get_block_height_quorum_with_mapper(providers: []ProviderInfo, addrs: []FunctionAddress, q_addr: QuorumService, u_addr: FunctionAddress, t_quorum: f64) -> Quorum, bool:
|
2022-06-29 20:42:36 -05:00
|
|
|
result: *EVMResult
|
|
|
|
quorum: *Quorum
|
2022-07-11 19:26:08 -05:00
|
|
|
is_quorum: *bool
|
|
|
|
|
2023-02-10 18:58:45 +07:00
|
|
|
min_points = 3 -- minimum points we want in order to calculate an oracle
|
2022-07-11 19:26:08 -05:00
|
|
|
|
2023-02-21 11:30:20 +04:00
|
|
|
n <- ProviderInfoOp.array_length(providers)
|
|
|
|
n2 <- FuncAddrOp.array_length(addrs)
|
2022-06-29 20:42:36 -05:00
|
|
|
|
|
|
|
if n > 0:
|
|
|
|
for addr <- addrs par:
|
|
|
|
on addr.peer_id:
|
|
|
|
MultiProviderQuery addr.service_id
|
|
|
|
for provider <- providers:
|
|
|
|
result <- MultiProviderQuery.get_block_number(provider)
|
2022-07-11 19:26:08 -05:00
|
|
|
-- result2 <<- provider.name
|
2022-06-29 20:42:36 -05:00
|
|
|
-- join result[n2-1]
|
2022-07-11 19:26:08 -05:00
|
|
|
-- join result[n-1]
|
|
|
|
join result[n*n2-2]
|
|
|
|
|
2022-06-29 20:42:36 -05:00
|
|
|
on q_addr.peer_id:
|
|
|
|
SimpleQuorum q_addr.service_id
|
2022-07-11 19:26:08 -05:00
|
|
|
quorum <-SimpleQuorum.point_estimate(result, min_points)
|
|
|
|
if quorum[0].mode == 0:
|
|
|
|
is_quorum <<- false
|
|
|
|
else:
|
|
|
|
is_quorum <- SimpleQuorum.is_quorum(quorum[0].freq, quorum[0].n, t_quorum)
|
2023-02-10 18:58:45 +07:00
|
|
|
|
2022-07-11 19:26:08 -05:00
|
|
|
deviations: *EVMResult
|
|
|
|
n_dev = 1
|
|
|
|
if quorum[0].freq != quorum[0].n:
|
|
|
|
on u_addr.peer_id:
|
|
|
|
Utilities u_addr.service_id
|
|
|
|
for res <- result:
|
|
|
|
v <- Utilities.kv_to_u64(res.stdout, "block-height")
|
|
|
|
if v != quorum[0].mode:
|
|
|
|
deviations <<- res
|
|
|
|
on %init_peer_id% via u_addr.peer_id:
|
|
|
|
co ConsoleEVMResult.print(res) --< placeholder for future processing of divergent responses
|
|
|
|
Math.add(n_dev, 1)
|
|
|
|
-- ConsoleEVMResults.print(deviations)
|
|
|
|
<- quorum[0], is_quorum[0]
|
|
|
|
|
|
|
|
data IpfsObj:
|
|
|
|
cid: string
|
|
|
|
multiaddr: string
|
|
|
|
|
|
|
|
service IpfsCli("service-is"):
|
|
|
|
params_from_cid(multiaddr: string, cid: string) -> []FunctionAddress
|
|
|
|
|
|
|
|
|
|
|
|
func get_block_height_quorum_with_cid(providers: []ProviderInfo, services_cid: IpfsObj, quorum_cid: IpfsObj, utility_cid: IpfsObj, ipfs_service: FunctionAddress, t_quorum: f64) -> Quorum, bool:
|
|
|
|
result: *EVMResult
|
|
|
|
quorum: *Quorum
|
|
|
|
is_quorum: *bool
|
|
|
|
|
2023-02-10 18:58:45 +07:00
|
|
|
min_points = 3 -- minimum points we want in order to calculate an oracle
|
2022-06-29 20:42:36 -05:00
|
|
|
|
2022-07-11 19:26:08 -05:00
|
|
|
on ipfs_service.peer_id:
|
|
|
|
IpfsCli ipfs_service.service_id
|
|
|
|
addrs <- IpfsCli.params_from_cid(services_cid.multiaddr, services_cid.cid)
|
|
|
|
q_addrs <- IpfsCli.params_from_cid(quorum_cid.multiaddr, quorum_cid.cid)
|
|
|
|
u_addrs <- IpfsCli.params_from_cid(utility_cid.multiaddr, utility_cid.cid)
|
|
|
|
|
2023-02-21 11:30:20 +04:00
|
|
|
n <- ProviderInfoOp.array_length(providers)
|
|
|
|
m <- FuncAddrOp.array_length(addrs)
|
2022-07-11 19:26:08 -05:00
|
|
|
|
|
|
|
if n > 0:
|
|
|
|
for addr <- addrs par:
|
|
|
|
on addr.peer_id:
|
|
|
|
MultiProviderQuery addr.service_id
|
|
|
|
for provider <- providers:
|
|
|
|
result <- MultiProviderQuery.get_block_number(provider)
|
2022-09-07 00:52:51 -05:00
|
|
|
join result[n*m-2]
|
2022-07-11 19:26:08 -05:00
|
|
|
|
|
|
|
on q_addrs[0].peer_id:
|
|
|
|
SimpleQuorum q_addrs[0].service_id
|
|
|
|
quorum <-SimpleQuorum.point_estimate(result, min_points)
|
|
|
|
if quorum[0].mode == 0:
|
|
|
|
is_quorum <<- false
|
|
|
|
else:
|
|
|
|
is_quorum <- SimpleQuorum.is_quorum(quorum[0].freq, quorum[0].n, t_quorum)
|
2023-02-10 18:58:45 +07:00
|
|
|
|
2022-07-11 19:26:08 -05:00
|
|
|
deviations: *EVMResult
|
|
|
|
n_dev = 1
|
|
|
|
if quorum[0].freq != quorum[0].n:
|
|
|
|
on u_addrs[0].peer_id:
|
|
|
|
Utilities u_addrs[0].service_id
|
|
|
|
for res <- result:
|
|
|
|
v <- Utilities.kv_to_u64(res.stdout, "block-height")
|
|
|
|
if v != quorum[0].mode:
|
|
|
|
deviations <<- res
|
|
|
|
on %init_peer_id% via u_addrs[0].peer_id:
|
|
|
|
co ConsoleEVMResult.print(res)
|
|
|
|
Math.add(n_dev, 1)
|
|
|
|
<- quorum[0], is_quorum[0]
|