-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmisc.aqua
69 lines (56 loc) · 1.71 KB
/
misc.aqua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
aqua Misc declares *
import "@fluencelabs/aqua-lib/builtin.aqua"
import "registry-service.aqua"
import "constants.aqua"
alias ResourceId: string
alias Resource: Key
alias Error: string
func wait(successful: *bool, len: i16, timeout: u16) -> bool:
status: *string
waiting = (arr: *bool, s: *string):
join arr[len - 1]
s <<- "ok"
waiting(successful, status)
par status <- Peer.timeout(timeout, "timeout")
result: *bool
stat = status!
if stat == "ok":
result <<- true
else:
result <<- false
<- result!
-- Get peers closest to the resource_id's hash in Kademlia network
-- These peers are expected to store list of providers for this key
func getNeighbors(resource_id: ResourceId) -> []PeerId:
k <- Op.string_to_b58(resource_id)
nodes <- Kademlia.neighborhood(k, nil, nil)
<- nodes
func appendErrors(error1: *Error, error2: *Error):
for e <- error2:
error1 <<- e
func getResourceHelper(resource_id: ResourceId) -> ?Resource, *Error:
nodes <- getNeighbors(resource_id)
result: *Resource
error: *Error
resources: *Key
successful: *bool
for n <- nodes par:
on n:
try:
get_result <- Registry.get_key_metadata(resource_id)
if get_result.success:
resources <<- get_result.key
successful <<- true
else:
e <- Op.concat_strings(get_result.error, " on ")
error <- Op.concat_strings(e, n)
success <- wait(successful, CONSISTENCY_LEVEL, DEFAULT_TIMEOUT)
if success == false:
error <<- "resource not found: timeout exceeded"
else:
merge_result <- Registry.merge_keys(resources)
if merge_result.success:
result <<- merge_result.key
else:
error <<- merge_result.error
<- result, error