From c830bda16d81ddb2292155fef38ea4715109ffac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Uhl=C3=AD=C5=99?= Date: Mon, 4 Nov 2024 07:28:10 +0100 Subject: [PATCH 1/4] feat: expose underlying nim-ethers errors to logs --- codex/contracts/clock.nim | 7 ++- codex/contracts/market.nim | 66 ++++++++++++++++++++++----- tests/integration/testecbug.nim | 5 +- tests/integration/testmarketplace.nim | 5 +- tests/integration/testproofs.nim | 12 ++--- vendor/nim-ethers | 2 +- 6 files changed, 74 insertions(+), 23 deletions(-) diff --git a/codex/contracts/clock.nim b/codex/contracts/clock.nim index 937745bf4..284996ffd 100644 --- a/codex/contracts/clock.nim +++ b/codex/contracts/clock.nim @@ -1,5 +1,6 @@ import std/times import pkg/ethers +import pkg/questionable import pkg/chronos import pkg/stint import ../clock @@ -45,7 +46,11 @@ method start*(clock: OnChainClock) {.async.} = if clock.started: return - proc onBlock(_: Block) = + proc onBlock(blckResult: ?!Block) = + if eventError =? blckResult.errorOption: + error "There was an error in block subscription", msg=eventError.msg + return + # ignore block parameter; hardhat may call this with pending blocks asyncSpawn clock.update() diff --git a/codex/contracts/market.nim b/codex/contracts/market.nim index fdddcb220..e1e36d9b5 100644 --- a/codex/contracts/market.nim +++ b/codex/contracts/market.nim @@ -277,7 +277,11 @@ method canReserveSlot*( method subscribeRequests*(market: OnChainMarket, callback: OnRequest): Future[MarketSubscription] {.async.} = - proc onEvent(event: StorageRequested) {.upraises:[].} = + proc onEvent(eventResult: ?!StorageRequested) {.upraises:[].} = + without event =? eventResult, eventErr: + error "There was an error in Request subscription", msg = eventErr.msg + return + callback(event.requestId, event.ask, event.expiry) @@ -289,7 +293,11 @@ method subscribeRequests*(market: OnChainMarket, method subscribeSlotFilled*(market: OnChainMarket, callback: OnSlotFilled): Future[MarketSubscription] {.async.} = - proc onEvent(event: SlotFilled) {.upraises:[].} = + proc onEvent(eventResult: ?!SlotFilled) {.upraises:[].} = + without event =? eventResult, eventErr: + error "There was an error in SlotFilled subscription", msg = eventErr.msg + return + callback(event.requestId, event.slotIndex) convertEthersError: @@ -311,7 +319,11 @@ method subscribeSlotFilled*(market: OnChainMarket, method subscribeSlotFreed*(market: OnChainMarket, callback: OnSlotFreed): Future[MarketSubscription] {.async.} = - proc onEvent(event: SlotFreed) {.upraises:[].} = + proc onEvent(eventResult: ?!SlotFreed) {.upraises:[].} = + without event =? eventResult, eventErr: + error "There was an error in SlotFreed subscription", msg = eventErr.msg + return + callback(event.requestId, event.slotIndex) convertEthersError: @@ -322,7 +334,11 @@ method subscribeSlotReservationsFull*( market: OnChainMarket, callback: OnSlotReservationsFull): Future[MarketSubscription] {.async.} = - proc onEvent(event: SlotReservationsFull) {.upraises:[].} = + proc onEvent(eventResult: ?!SlotReservationsFull) {.upraises:[].} = + without event =? eventResult, eventErr: + error "There was an error in SlotReservationsFull subscription", msg = eventErr.msg + return + callback(event.requestId, event.slotIndex) convertEthersError: @@ -332,7 +348,11 @@ method subscribeSlotReservationsFull*( method subscribeFulfillment(market: OnChainMarket, callback: OnFulfillment): Future[MarketSubscription] {.async.} = - proc onEvent(event: RequestFulfilled) {.upraises:[].} = + proc onEvent(eventResult: ?!RequestFulfilled) {.upraises:[].} = + without event =? eventResult, eventErr: + error "There was an error in RequestFulfillment subscription", msg = eventErr.msg + return + callback(event.requestId) convertEthersError: @@ -343,7 +363,11 @@ method subscribeFulfillment(market: OnChainMarket, requestId: RequestId, callback: OnFulfillment): Future[MarketSubscription] {.async.} = - proc onEvent(event: RequestFulfilled) {.upraises:[].} = + proc onEvent(eventResult: ?!RequestFulfilled) {.upraises:[].} = + without event =? eventResult, eventErr: + error "There was an error in RequestFulfillment subscription", msg = eventErr.msg + return + if event.requestId == requestId: callback(event.requestId) @@ -354,7 +378,11 @@ method subscribeFulfillment(market: OnChainMarket, method subscribeRequestCancelled*(market: OnChainMarket, callback: OnRequestCancelled): Future[MarketSubscription] {.async.} = - proc onEvent(event: RequestCancelled) {.upraises:[].} = + proc onEvent(eventResult: ?!RequestCancelled) {.upraises:[].} = + without event =? eventResult, eventErr: + error "There was an error in RequestCancelled subscription", msg = eventErr.msg + return + callback(event.requestId) convertEthersError: @@ -365,7 +393,11 @@ method subscribeRequestCancelled*(market: OnChainMarket, requestId: RequestId, callback: OnRequestCancelled): Future[MarketSubscription] {.async.} = - proc onEvent(event: RequestCancelled) {.upraises:[].} = + proc onEvent(eventResult: ?!RequestCancelled) {.upraises:[].} = + without event =? eventResult, eventErr: + error "There was an error in RequestCancelled subscription", msg = eventErr.msg + return + if event.requestId == requestId: callback(event.requestId) @@ -376,7 +408,11 @@ method subscribeRequestCancelled*(market: OnChainMarket, method subscribeRequestFailed*(market: OnChainMarket, callback: OnRequestFailed): Future[MarketSubscription] {.async.} = - proc onEvent(event: RequestFailed) {.upraises:[]} = + proc onEvent(eventResult: ?!RequestFailed) {.upraises:[]} = + without event =? eventResult, eventErr: + error "There was an error in RequestFailed subscription", msg = eventErr.msg + return + callback(event.requestId) convertEthersError: @@ -387,7 +423,11 @@ method subscribeRequestFailed*(market: OnChainMarket, requestId: RequestId, callback: OnRequestFailed): Future[MarketSubscription] {.async.} = - proc onEvent(event: RequestFailed) {.upraises:[]} = + proc onEvent(eventResult: ?!RequestFailed) {.upraises:[]} = + without event =? eventResult, eventErr: + error "There was an error in RequestFailed subscription", msg = eventErr.msg + return + if event.requestId == requestId: callback(event.requestId) @@ -398,7 +438,11 @@ method subscribeRequestFailed*(market: OnChainMarket, method subscribeProofSubmission*(market: OnChainMarket, callback: OnProofSubmitted): Future[MarketSubscription] {.async.} = - proc onEvent(event: ProofSubmitted) {.upraises: [].} = + proc onEvent(eventResult: ?!ProofSubmitted) {.upraises: [].} = + without event =? eventResult, eventErr: + error "There was an error in ProofSubmitted subscription", msg = eventErr.msg + return + callback(event.id) convertEthersError: diff --git a/tests/integration/testecbug.nim b/tests/integration/testecbug.nim index d33c857a1..ecdcacdd6 100644 --- a/tests/integration/testecbug.nim +++ b/tests/integration/testecbug.nim @@ -33,8 +33,9 @@ marketplacesuite "Bug #821 - node crashes during erasure coding": let cid = clientApi.upload(data).get var requestId = none RequestId - proc onStorageRequested(event: StorageRequested) {.raises:[].} = - requestId = event.requestId.some + proc onStorageRequested(eventRes: ?!StorageRequested)= + if event =? eventRes: + requestId = event.requestId.some let subscription = await marketplace.subscribe(StorageRequested, onStorageRequested) diff --git a/tests/integration/testmarketplace.nim b/tests/integration/testmarketplace.nim index 6e62c58b3..d36c2732a 100644 --- a/tests/integration/testmarketplace.nim +++ b/tests/integration/testmarketplace.nim @@ -135,8 +135,9 @@ marketplacesuite "Marketplace payouts": let cid = clientApi.upload(data).get var slotIdxFilled = none UInt256 - proc onSlotFilled(event: SlotFilled) = - slotIdxFilled = some event.slotIndex + proc onSlotFilled(eventRes: ?!SlotFilled) = + if event =? eventRes: + slotIdxFilled = some event.slotIndex let subscription = await marketplace.subscribe(SlotFilled, onSlotFilled) diff --git a/tests/integration/testproofs.nim b/tests/integration/testproofs.nim index 0d8b9bd3e..b75505202 100644 --- a/tests/integration/testproofs.nim +++ b/tests/integration/testproofs.nim @@ -54,8 +54,8 @@ marketplacesuite "Hosts submit regular proofs": check eventually(client0.purchaseStateIs(purchaseId, "started"), timeout = expiry.int * 1000) var proofWasSubmitted = false - proc onProofSubmitted(event: ProofSubmitted) = - proofWasSubmitted = true + proc onProofSubmitted(event: ?!ProofSubmitted) = + proofWasSubmitted = event.isOk let subscription = await marketplace.subscribe(ProofSubmitted, onProofSubmitted) @@ -120,8 +120,8 @@ marketplacesuite "Simulate invalid proofs": check eventually(client0.purchaseStateIs(purchaseId, "started"), timeout = expiry.int * 1000) var slotWasFreed = false - proc onSlotFreed(event: SlotFreed) = - if event.requestId == requestId: + proc onSlotFreed(event: ?!SlotFreed) = + if event.isOk and event.value.requestId == requestId: slotWasFreed = true let subscription = await marketplace.subscribe(SlotFreed, onSlotFreed) @@ -185,8 +185,8 @@ marketplacesuite "Simulate invalid proofs": check eventually(slotWasFilled, timeout = expiry.int * 1000) var slotWasFreed = false - proc onSlotFreed(event: SlotFreed) = - if event.requestId == requestId: + proc onSlotFreed(event: ?!SlotFreed) = + if event.isOk and event.value.requestId == requestId: slotWasFreed = true let freedSubscription = await marketplace.subscribe(SlotFreed, onSlotFreed) diff --git a/vendor/nim-ethers b/vendor/nim-ethers index 1ae2cd4a3..d88e4614b 160000 --- a/vendor/nim-ethers +++ b/vendor/nim-ethers @@ -1 +1 @@ -Subproject commit 1ae2cd4a35aa7c7ca21ca750fb7f951b3a6a97c0 +Subproject commit d88e4614b11ca750fac9efc645b8807bf82e1c15 From 8a0e3ac1cfa0108f3db20721659079bc8a56936c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Uhl=C3=AD=C5=99?= Date: Fri, 29 Nov 2024 15:23:06 +0100 Subject: [PATCH 2/4] chore: bump nim-ethers --- vendor/nim-ethers | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vendor/nim-ethers b/vendor/nim-ethers index d88e4614b..2808a0548 160000 --- a/vendor/nim-ethers +++ b/vendor/nim-ethers @@ -1 +1 @@ -Subproject commit d88e4614b11ca750fac9efc645b8807bf82e1c15 +Subproject commit 2808a05488152c8b438d947dc871445164fa1278 From e6e6b153cf355e4d435fca9854df9a56738920ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Uhl=C3=AD=C5=99?= Date: Tue, 3 Dec 2024 10:03:10 +0100 Subject: [PATCH 3/4] test: fix testproof compilation --- tests/integration/testproofs.nim | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/integration/testproofs.nim b/tests/integration/testproofs.nim index b75505202..2e462d2cb 100644 --- a/tests/integration/testproofs.nim +++ b/tests/integration/testproofs.nim @@ -1,4 +1,5 @@ from std/times import inMilliseconds +import pkg/questionable import pkg/codex/logutils import pkg/stew/byteutils import ../contracts/time @@ -176,7 +177,10 @@ marketplacesuite "Simulate invalid proofs": let requestId = client0.requestId(purchaseId).get var slotWasFilled = false - proc onSlotFilled(event: SlotFilled) = + proc onSlotFilled(eventResult: ?!SlotFilled) = + assert not eventResult.isErr + let event = !eventResult + if event.requestId == requestId: slotWasFilled = true let filledSubscription = await marketplace.subscribe(SlotFilled, onSlotFilled) From 3756c2b9571c58a7efbdf5bc1f59af2607b77b3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Uhl=C3=AD=C5=99?= Date: Tue, 3 Dec 2024 10:49:10 +0100 Subject: [PATCH 4/4] test: raise defects on results error --- tests/integration/testecbug.nim | 6 +++--- tests/integration/testmarketplace.nim | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/integration/testecbug.nim b/tests/integration/testecbug.nim index ecdcacdd6..0545d1d09 100644 --- a/tests/integration/testecbug.nim +++ b/tests/integration/testecbug.nim @@ -33,9 +33,9 @@ marketplacesuite "Bug #821 - node crashes during erasure coding": let cid = clientApi.upload(data).get var requestId = none RequestId - proc onStorageRequested(eventRes: ?!StorageRequested)= - if event =? eventRes: - requestId = event.requestId.some + proc onStorageRequested(eventResult: ?!StorageRequested)= + assert not eventResult.isErr + requestId = some (!eventResult).requestId let subscription = await marketplace.subscribe(StorageRequested, onStorageRequested) diff --git a/tests/integration/testmarketplace.nim b/tests/integration/testmarketplace.nim index d36c2732a..0c18ff1a9 100644 --- a/tests/integration/testmarketplace.nim +++ b/tests/integration/testmarketplace.nim @@ -135,9 +135,9 @@ marketplacesuite "Marketplace payouts": let cid = clientApi.upload(data).get var slotIdxFilled = none UInt256 - proc onSlotFilled(eventRes: ?!SlotFilled) = - if event =? eventRes: - slotIdxFilled = some event.slotIndex + proc onSlotFilled(eventResult: ?!SlotFilled) = + assert not eventResult.isErr + slotIdxFilled = some (!eventResult).slotIndex let subscription = await marketplace.subscribe(SlotFilled, onSlotFilled)