2022-04-11 16:24:51 +04:00
|
|
|
module Registry.ResourcesAPI declares *
|
|
|
|
|
|
|
|
import "registry-service.aqua"
|
|
|
|
import "registry-api.aqua"
|
2022-09-20 19:06:21 +04:00
|
|
|
import "misc.aqua"
|
|
|
|
import "constants.aqua"
|
2022-04-11 16:24:51 +04:00
|
|
|
import "@fluencelabs/aqua-lib/builtin.aqua"
|
|
|
|
|
|
|
|
alias ResourceId: string
|
2022-09-20 19:06:21 +04:00
|
|
|
alias Resource: Key
|
2022-04-11 16:24:51 +04:00
|
|
|
alias Error: string
|
|
|
|
|
2022-09-20 19:06:21 +04:00
|
|
|
func getResource(resource_id: ResourceId) -> ?Resource, *Error:
|
2022-09-29 21:05:02 +04:00
|
|
|
on HOST_PEER_ID:
|
|
|
|
result, error <- getResourceHelper(resource_id)
|
2022-04-13 01:11:26 +04:00
|
|
|
<- result, error
|
2022-04-11 16:24:51 +04:00
|
|
|
|
2022-09-29 21:05:02 +04:00
|
|
|
func getResourceId(label: string, peer_id: string) -> ResourceId:
|
|
|
|
on HOST_PEER_ID:
|
|
|
|
resource_id <- Registry.get_key_id(label, peer_id)
|
|
|
|
<- resource_id
|
|
|
|
|
2022-04-11 16:24:51 +04:00
|
|
|
-- Create a resource: register it on the closest peers
|
|
|
|
func createResource(label: string) -> ?ResourceId, *Error:
|
|
|
|
t <- Peer.timestamp_sec()
|
|
|
|
|
|
|
|
resource_id: ?ResourceId
|
|
|
|
error: *Error
|
|
|
|
on HOST_PEER_ID:
|
|
|
|
sig_result <- getKeySignature(label, t)
|
2022-04-13 01:11:26 +04:00
|
|
|
if sig_result.success == false:
|
|
|
|
error <<- sig_result.error!
|
|
|
|
else:
|
2022-04-11 16:24:51 +04:00
|
|
|
signature = sig_result.signature!
|
2022-09-29 21:05:02 +04:00
|
|
|
id <- Registry.get_key_id(label, INIT_PEER_ID)
|
|
|
|
nodes <- getNeighbors(id)
|
2022-04-11 16:24:51 +04:00
|
|
|
|
2022-04-13 01:11:26 +04:00
|
|
|
successful: *bool
|
2022-04-11 16:24:51 +04:00
|
|
|
for n <- nodes par:
|
|
|
|
on n:
|
|
|
|
try:
|
|
|
|
res <- registerKey(label, t, signature)
|
2022-04-13 01:11:26 +04:00
|
|
|
|
2022-04-11 16:24:51 +04:00
|
|
|
if res.success:
|
|
|
|
successful <<- true
|
|
|
|
else:
|
|
|
|
error <<- res.error
|
|
|
|
|
2022-10-17 12:39:39 +04:00
|
|
|
success <- wait(successful, INITIAL_REPLICATION_FACTOR, DEFAULT_TIMEOUT)
|
2022-04-11 16:24:51 +04:00
|
|
|
|
2022-06-21 13:12:29 +04:00
|
|
|
if success == false:
|
|
|
|
error <<- "resource wasn't created: timeout exceeded"
|
2022-04-13 01:11:26 +04:00
|
|
|
else:
|
2022-06-21 13:12:29 +04:00
|
|
|
resource_id <<- id
|
2022-04-11 16:24:51 +04:00
|
|
|
|
|
|
|
<- resource_id, error
|
|
|
|
|
2022-09-20 19:06:21 +04:00
|
|
|
-- Note: resource must be already created
|
|
|
|
func registerServiceRecord(resource_id: ResourceId, value: string, peer_id: PeerId, service_id: ?string) -> bool, *Error:
|
2022-04-11 16:24:51 +04:00
|
|
|
relay_id: ?string
|
2022-09-20 19:06:21 +04:00
|
|
|
if peer_id == INIT_PEER_ID:
|
|
|
|
relay_id <<- HOST_PEER_ID
|
2022-04-11 16:24:51 +04:00
|
|
|
|
2022-09-20 19:06:21 +04:00
|
|
|
success: *bool
|
2022-04-11 16:24:51 +04:00
|
|
|
error: *Error
|
|
|
|
|
2022-09-20 19:06:21 +04:00
|
|
|
on HOST_PEER_ID:
|
|
|
|
metadata, err <- getRecordMetadata(resource_id, value, peer_id, relay_id, service_id, nil)
|
|
|
|
if metadata == nil:
|
|
|
|
success <<- false
|
|
|
|
error <<- err!
|
2022-04-11 16:24:51 +04:00
|
|
|
else:
|
2022-09-20 19:06:21 +04:00
|
|
|
t <- Peer.timestamp_sec()
|
|
|
|
sig_result = getRecordSignature(metadata!, t)
|
|
|
|
if sig_result.success == false:
|
|
|
|
error <<- sig_result.error!
|
|
|
|
success <<- false
|
2022-04-11 16:24:51 +04:00
|
|
|
else:
|
2022-09-29 21:05:02 +04:00
|
|
|
key, error_get <- getResourceHelper(resource_id)
|
2022-09-20 19:06:21 +04:00
|
|
|
if key == nil:
|
|
|
|
appendErrors(error, error_get)
|
|
|
|
success <<- false
|
2022-04-11 16:24:51 +04:00
|
|
|
else:
|
2022-09-20 19:06:21 +04:00
|
|
|
if peer_id != INIT_PEER_ID:
|
|
|
|
on peer_id via HOST_PEER_ID:
|
|
|
|
republish_result <- republishKey(key!)
|
|
|
|
if republish_result.success == false:
|
|
|
|
error <<- republish_result.error
|
|
|
|
else:
|
|
|
|
p_res <- putRecord(metadata!, t, sig_result.signature!)
|
|
|
|
if p_res.success == false:
|
|
|
|
error <<- p_res.error
|
|
|
|
success <<- false
|
|
|
|
|
2022-09-29 21:05:02 +04:00
|
|
|
nodes <- getNeighbors(resource_id)
|
2022-09-20 19:06:21 +04:00
|
|
|
successful: *bool
|
2022-04-11 16:24:51 +04:00
|
|
|
for n <- nodes par:
|
|
|
|
on n:
|
|
|
|
try:
|
2022-09-20 19:06:21 +04:00
|
|
|
republish_res <- republishKey(key!)
|
|
|
|
if republish_res.success == false:
|
|
|
|
error <<- republish_res.error
|
|
|
|
else:
|
|
|
|
put_res <- putRecord(metadata!, t, sig_result.signature!)
|
|
|
|
if put_res.success:
|
2022-04-11 16:24:51 +04:00
|
|
|
successful <<- true
|
|
|
|
else:
|
2022-09-20 19:06:21 +04:00
|
|
|
error <<- put_res.error
|
2022-10-17 12:39:39 +04:00
|
|
|
success <- wait(successful, INITIAL_REPLICATION_FACTOR, DEFAULT_TIMEOUT)
|
2022-04-11 16:24:51 +04:00
|
|
|
|
2022-09-20 19:06:21 +04:00
|
|
|
succ = success!
|
|
|
|
if succ == false:
|
|
|
|
error <<- "service hasn't registered: timeout exceeded"
|
2022-04-11 16:24:51 +04:00
|
|
|
|
2022-09-20 19:06:21 +04:00
|
|
|
<- succ, error
|
2022-04-11 16:24:51 +04:00
|
|
|
|
|
|
|
|
2022-09-20 19:06:21 +04:00
|
|
|
func unregisterService(resource_id: ResourceId, peer_id: PeerId) -> bool, *Error:
|
2022-10-10 17:25:10 +04:00
|
|
|
success: ?bool
|
2022-09-20 19:06:21 +04:00
|
|
|
error: *Error
|
2022-04-11 16:24:51 +04:00
|
|
|
|
2022-09-23 22:58:12 +04:00
|
|
|
on HOST_PEER_ID:
|
2022-09-20 19:06:21 +04:00
|
|
|
t <- Peer.timestamp_sec()
|
|
|
|
sig_result = getTombstoneSignature(resource_id, peer_id, t, nil)
|
|
|
|
if sig_result.success == false:
|
|
|
|
error <<- sig_result.error!
|
2022-04-13 01:11:26 +04:00
|
|
|
success <<- false
|
2022-04-11 16:24:51 +04:00
|
|
|
else:
|
2022-10-10 17:25:10 +04:00
|
|
|
key, error_get <- getResourceHelper(resource_id)
|
2022-04-13 01:11:26 +04:00
|
|
|
if key == nil:
|
|
|
|
appendErrors(error, error_get)
|
|
|
|
success <<- false
|
|
|
|
else:
|
2022-09-23 22:58:12 +04:00
|
|
|
|
|
|
|
if peer_id != INIT_PEER_ID:
|
|
|
|
on peer_id:
|
|
|
|
republish_result <- republishKey(key!)
|
|
|
|
if republish_result.success == false:
|
|
|
|
error <<- republish_result.error
|
|
|
|
else:
|
|
|
|
res <- addTombstone(resource_id, peer_id, t, nil, sig_result.signature!)
|
|
|
|
if res.success == false:
|
|
|
|
error <<- res.error
|
|
|
|
success <<- false
|
2022-09-20 19:06:21 +04:00
|
|
|
|
2022-09-29 21:05:02 +04:00
|
|
|
nodes <- getNeighbors(resource_id)
|
2022-04-13 01:11:26 +04:00
|
|
|
successful: *bool
|
2022-04-11 16:24:51 +04:00
|
|
|
for n <- nodes par:
|
|
|
|
on n:
|
|
|
|
try:
|
|
|
|
republish_res <- republishKey(key!)
|
|
|
|
if republish_res.success == false:
|
|
|
|
error <<- republish_res.error
|
|
|
|
else:
|
2022-09-20 19:06:21 +04:00
|
|
|
add_res <- addTombstone(resource_id, peer_id, t, nil, sig_result.signature!)
|
|
|
|
if add_res.success:
|
2022-04-11 16:24:51 +04:00
|
|
|
successful <<- true
|
|
|
|
else:
|
2022-09-20 19:06:21 +04:00
|
|
|
error <<- add_res.error
|
2022-10-17 12:39:39 +04:00
|
|
|
success <- wait(successful, INITIAL_REPLICATION_FACTOR, DEFAULT_TIMEOUT)
|
2022-06-24 19:57:58 +04:00
|
|
|
|
2022-09-20 19:06:21 +04:00
|
|
|
succ = success!
|
2022-06-24 19:57:58 +04:00
|
|
|
if succ == false:
|
2022-09-20 19:06:21 +04:00
|
|
|
error <<- "unregisterService failed: timeout exceeded"
|
2022-04-11 16:24:51 +04:00
|
|
|
|
2022-09-20 19:06:21 +04:00
|
|
|
<- succ, error
|
2022-04-11 16:24:51 +04:00
|
|
|
|
2022-10-10 17:25:10 +04:00
|
|
|
func resolveResource(resource_id: ResourceId, ack: i16) -> ?[]Record, *Error:
|
2022-04-11 16:24:51 +04:00
|
|
|
on HOST_PEER_ID:
|
2022-09-29 21:05:02 +04:00
|
|
|
nodes <- getNeighbors(resource_id)
|
2022-10-10 17:25:10 +04:00
|
|
|
result: ?[]Record
|
|
|
|
records: *[]Record
|
2022-04-11 16:24:51 +04:00
|
|
|
error: *Error
|
2022-06-21 13:12:29 +04:00
|
|
|
successful: *bool
|
2022-04-11 16:24:51 +04:00
|
|
|
for n <- nodes par:
|
|
|
|
on n:
|
|
|
|
try:
|
|
|
|
t <- Peer.timestamp_sec()
|
|
|
|
get_result <- Registry.get_records(resource_id, t)
|
|
|
|
if get_result.success:
|
2022-10-10 17:25:10 +04:00
|
|
|
records <<- get_result.result
|
2022-06-21 13:12:29 +04:00
|
|
|
successful <<- true
|
2022-04-11 16:24:51 +04:00
|
|
|
else:
|
|
|
|
error <<- get_result.error
|
|
|
|
|
2022-06-21 13:12:29 +04:00
|
|
|
success <- wait(successful, ack, DEFAULT_TIMEOUT)
|
|
|
|
if success == false:
|
|
|
|
error <<- "timeout exceeded"
|
2022-10-10 17:25:10 +04:00
|
|
|
else:
|
|
|
|
merged <- Registry.merge(records)
|
|
|
|
if merged.success == false:
|
|
|
|
error <<- merged.error
|
|
|
|
else:
|
|
|
|
result <<- merged.result
|
|
|
|
<- result, error
|
2022-04-11 16:24:51 +04:00
|
|
|
|
|
|
|
-- Execute the given call on providers
|
|
|
|
-- Note that you can provide another Aqua function as an argument to this one
|
2022-10-10 17:25:10 +04:00
|
|
|
func executeOnResource(resource_id: ResourceId, ack: i16, call: Record -> ()) -> bool, *Error:
|
|
|
|
success: ?bool
|
|
|
|
result, error <- resolveResource(resource_id, ack)
|
|
|
|
|
|
|
|
if result == nil:
|
|
|
|
success <<- false
|
|
|
|
else:
|
|
|
|
for r <- result! par:
|
|
|
|
on r.metadata.peer_id via r.metadata.relay_id:
|
|
|
|
call(r)
|
|
|
|
success <<- true
|
|
|
|
<- success!, error
|