Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(decider): Refactor aqua code #83

Merged
merged 10 commits into from
Oct 26, 2023
52 changes: 22 additions & 30 deletions src/aqua/chain/blocks.aqua
Original file line number Diff line number Diff line change
Expand Up @@ -16,43 +16,35 @@ func get_latest(spell_id: SpellId, chain: ChainInfo) -> ?string:
spell_log(spell_id, ["error retrieving latest block number", latest_block.error])
<- latest

-- implements Less Than or Equal for two hex strings, i.e. `hex_a <= hex_b`
-- Implements comparison for two hex strings
-- spell_id - spell id for error log
-- hex_a - left hex string
-- hex_b - right hex string
-- pred - func from ordering to result
-- ordering is -1 for <, 0 for =, 1 for >
-- returns nil if either `hex_a` or `hex_b` is an invalid hex
func lte(spell_id: string, hex_a: string, hex_b: string) -> ?bool:
lte: ?bool
func hex_cmp(spell_id: SpellId, hex_a: string, hex_b: string, pred: i8 -> bool) -> ?bool:
result: ?bool

cmp <- ChainConnector.hex_cmp(hex_a, hex_b)

if cmp.success:
-- `hex_a` is less than `hex_b`
if cmp.ordering == -1:
lte <<- true
-- `hex_a` equals to `hex_b`
if cmp.ordering == 0:
lte <<- true
-- `hex_a` is greater than `hex_b`
if cmp.ordering == 1:
lte <<- false
result <- pred(cmp.ordering)
else:
spell_log(spell_id, ["hex_cmp error", cmp.error])

<- lte
<- result

-- `hex_a > hex_b`
func gt(spell_id: string, hex_a: string, hex_b: string) -> ?bool:
gt: ?bool
-- LTE for two hex strings, i.e. `hex_a <= hex_b`
func lte(spell_id: SpellId, hex_a: string, hex_b: string) -> ?bool:
pred = (ord: i8) -> bool:
<- ord <= 0

cmp <- ChainConnector.hex_cmp(hex_a, hex_b)
if cmp.success:
-- `hex_a` is less than `hex_b`
if cmp.ordering == -1:
gt <<- false
-- `hex_a` equals to `hex_b`
if cmp.ordering == 0:
gt <<- false
-- `hex_a` is greater than `hex_b`
if cmp.ordering == 1:
gt <<- true
else:
spell_log(spell_id, ["hex_cmp error", cmp.error])
<- hex_cmp(spell_id, hex_a, hex_b, pred)

-- GT for two hex strings, i.e. `hex_a > hex_b`
func gt(spell_id: string, hex_a: string, hex_b: string) -> ?bool:
pred = (ord: i8) -> bool:
<- ord > 0

<- gt
<- hex_cmp(spell_id, hex_a, hex_b, pred)
3 changes: 1 addition & 2 deletions src/aqua/chain/new_deals.aqua
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module NewDeals declares poll_new_deals

import or, not from "@fluencelabs/aqua-lib/binary.aqua"
import Spell, TriggerConfig from "@fluencelabs/spell/spell_service.aqua"

import ChainConnector, DealMatched from "services.aqua"
Expand Down Expand Up @@ -99,7 +98,7 @@ func poll_logs(spell_id: SpellId, chain: ChainInfo, left: string) -> ?Poll:
poll: ?Poll

result <- ChainConnector.poll_deal_matches(chain, left)
if result.success == false:
if !result.success:
spell_log(spell_id, ["error polling deal created events", result.error])
else:
logs = result.logs
Expand Down
4 changes: 2 additions & 2 deletions src/aqua/decider/deal_spell.aqua
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ export main

import deal_install from "@fluencelabs/installation-spell/src/aqua/deal_spell.aqua"

import SpellId, DealId from "../types.aqua"
import SpellId, DealId, CID from "../types.aqua"

service Console("run-console"):
print(msg: []string)

func main(spell_id: SpellId, ipfs: string, deal_id: DealId, worker_def_cid: string):
func main(spell_id: SpellId, ipfs: string, deal_id: DealId, worker_def_cid: CID):
Console.print(["worker", spell_id, deal_id, worker_def_cid, ipfs])
deal_install(ipfs, worker_def_cid, deal_id)
8 changes: 3 additions & 5 deletions src/aqua/decider/deal_storage.aqua
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module DealStorage declares DealState, JoinedDeal, JsonDealState, JsonJoinedDeal, store_deal_state, get_deal_state, store_joined_deal, get_joined_deals, make_change_reqs, get_joined_deals

import Spell from "@fluencelabs/spell/spell_service.aqua"
import or, not, and from "@fluencelabs/aqua-lib/binary.aqua"

import Json from "../fluence/peer.aqua"
import spell_log, get_string, deal_log from "../fluence/spell.aqua"
Expand Down Expand Up @@ -36,7 +35,7 @@ func get_deal_state(decider_id: SpellId, deal_id: DealId) -> ?DealState:
state: ?DealState

json <- Spell.get_string(deal_id)
if and(json.success, not(json.absent)):
if json.success && !json.absent:
try:
state <- JsonDealState.parse(json.str)
catch e:
Expand All @@ -58,13 +57,12 @@ func get_joined_deals(spell_id: SpellId) -> []JoinedDeal:
joined_deals: *JoinedDeal

list <- Spell.list_get_strings("joined_deals")
if list.success == false:
if !list.success:
spell_log(spell_id, ["can't restrive joined deals", list.error])
else:
for joined_deal_str <- list.strings:
try:
joined_deal <- JsonJoinedDeal.parse(joined_deal_str)
joined_deals <<- joined_deal
joined_deals <- JsonJoinedDeal.parse(joined_deal_str)
catch e:
spell_log(spell_id, ["error parsing JoinedDeal", joined_deal_str, e])

Expand Down
7 changes: 2 additions & 5 deletions src/aqua/decider/mailbox.aqua
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,8 @@ func poll_mailbox(spell_id: SpellId):
process_message(message)
spell_log(spell_id, "will pop")
popped <- Spell.pop_mailbox()
if popped.message.length == 0:
spell_log(spell_id, ["произошла чудовищная ошибка. expected", msg, "popped nothing"])
else:
if popped.message!.message != msg.message:
spell_log(spell_id, ["произошла чудовищная ошибка. expected", msg, "popped", popped])
if popped.message.length == 0 || popped.message!.message != msg.message:
spell_log(spell_id, ["произошла чудовищная ошибка. expected", msg, "popped", popped])

func push_remove_worker(spell_id: SpellId, host_id: PeerId, worker_id: WorkerId):
Spell spell_id
Expand Down