mirror of
https://github.com/logos-messaging/logos-messaging-nim.git
synced 2026-01-02 14:03:06 +00:00
Merge dce45ba9e876d3ade9f8b6da06d9fa26634c875b into 96196ab8bc05f31b09dac2403f9d5de3bc05f31b
This commit is contained in:
commit
1fcc4c00d8
@ -86,10 +86,9 @@ suite "Onchain group manager":
|
||||
|
||||
let merkleRootBefore = waitFor manager.fetchMerkleRoot()
|
||||
|
||||
try:
|
||||
waitFor manager.register(credentials, UserMessageLimit(20))
|
||||
except Exception, CatchableError:
|
||||
assert false, "exception raised: " & getCurrentExceptionMsg()
|
||||
(waitFor manager.register(credentials, UserMessageLimit(20))).isOkOr:
|
||||
assert false,
|
||||
"error returned when calling register: " & error
|
||||
|
||||
discard waitFor withTimeout(trackRootChanges(manager), 15.seconds)
|
||||
|
||||
@ -110,13 +109,11 @@ suite "Onchain group manager":
|
||||
|
||||
let merkleRootBefore = waitFor manager.fetchMerkleRoot()
|
||||
|
||||
try:
|
||||
for i in 0 ..< credentials.len():
|
||||
info "Registering credential", index = i, credential = credentials[i]
|
||||
waitFor manager.register(credentials[i], UserMessageLimit(20))
|
||||
discard waitFor manager.updateRoots()
|
||||
except Exception, CatchableError:
|
||||
assert false, "exception raised: " & getCurrentExceptionMsg()
|
||||
for i in 0 ..< credentials.len():
|
||||
info "Registering credential", index = i, credential = credentials[i]
|
||||
(waitFor manager.register(credentials[i], UserMessageLimit(20))).isOkOr:
|
||||
assert false, "Failed to register credential " & $i & ": " & error
|
||||
discard waitFor manager.updateRoots()
|
||||
|
||||
let merkleRootAfter = waitFor manager.fetchMerkleRoot()
|
||||
|
||||
@ -127,16 +124,15 @@ suite "Onchain group manager":
|
||||
test "register: should guard against uninitialized state":
|
||||
let dummyCommitment = default(IDCommitment)
|
||||
|
||||
try:
|
||||
waitFor manager.register(
|
||||
RateCommitment(
|
||||
idCommitment: dummyCommitment, userMessageLimit: UserMessageLimit(20)
|
||||
)
|
||||
let res = waitFor manager.register(
|
||||
RateCommitment(
|
||||
idCommitment: dummyCommitment, userMessageLimit: UserMessageLimit(20)
|
||||
)
|
||||
except CatchableError:
|
||||
assert true
|
||||
except Exception:
|
||||
assert false, "exception raised: " & getCurrentExceptionMsg()
|
||||
)
|
||||
|
||||
check:
|
||||
res.isErr()
|
||||
res.error == "Not initialized: OnchainGroupManager is not initialized"
|
||||
|
||||
test "register: should register successfully":
|
||||
# TODO :- similar to ```trackRootChanges: should fetch history correctly```
|
||||
@ -146,11 +142,9 @@ suite "Onchain group manager":
|
||||
let idCredentials = generateCredentials()
|
||||
let merkleRootBefore = waitFor manager.fetchMerkleRoot()
|
||||
|
||||
try:
|
||||
waitFor manager.register(idCredentials, UserMessageLimit(20))
|
||||
except Exception, CatchableError:
|
||||
(waitFor manager.register(idCredentials, UserMessageLimit(20))).isOkOr:
|
||||
assert false,
|
||||
"exception raised when calling register: " & getCurrentExceptionMsg()
|
||||
"error returned when calling register: " & error
|
||||
|
||||
let merkleRootAfter = waitFor manager.fetchMerkleRoot()
|
||||
|
||||
@ -177,26 +171,24 @@ suite "Onchain group manager":
|
||||
|
||||
manager.onRegister(callback)
|
||||
|
||||
try:
|
||||
waitFor manager.register(
|
||||
RateCommitment(
|
||||
idCommitment: idCommitment, userMessageLimit: UserMessageLimit(20)
|
||||
)
|
||||
(waitFor manager.register(
|
||||
RateCommitment(
|
||||
idCommitment: idCommitment, userMessageLimit: UserMessageLimit(20)
|
||||
)
|
||||
except Exception, CatchableError:
|
||||
assert false, "exception raised: " & getCurrentExceptionMsg()
|
||||
)).isOkOr:
|
||||
assert false,
|
||||
"error returned when calling register: " & error
|
||||
|
||||
waitFor fut
|
||||
|
||||
test "withdraw: should guard against uninitialized state":
|
||||
let idSecretHash = generateCredentials().idSecretHash
|
||||
|
||||
try:
|
||||
waitFor manager.withdraw(idSecretHash)
|
||||
except CatchableError:
|
||||
assert true
|
||||
except Exception:
|
||||
assert false, "exception raised: " & getCurrentExceptionMsg()
|
||||
let res = waitFor manager.withdraw(idSecretHash)
|
||||
|
||||
check:
|
||||
res.isErr()
|
||||
res.error == "Not initialized: OnchainGroupManager is not initialized"
|
||||
|
||||
test "validateRoot: should validate good root":
|
||||
let idCredentials = generateCredentials()
|
||||
@ -217,10 +209,8 @@ suite "Onchain group manager":
|
||||
(waitFor manager.init()).isOkOr:
|
||||
raiseAssert $error
|
||||
|
||||
try:
|
||||
waitFor manager.register(idCredentials, UserMessageLimit(20))
|
||||
except Exception, CatchableError:
|
||||
assert false, "exception raised: " & getCurrentExceptionMsg()
|
||||
(waitFor manager.register(idCredentials, UserMessageLimit(20))).isOkOr:
|
||||
assert false, "error returned : " & getCurrentExceptionMsg()
|
||||
|
||||
waitFor fut
|
||||
|
||||
@ -299,10 +289,10 @@ suite "Onchain group manager":
|
||||
|
||||
manager.onRegister(callback)
|
||||
|
||||
try:
|
||||
waitFor manager.register(credentials, UserMessageLimit(20))
|
||||
except Exception, CatchableError:
|
||||
assert false, "exception raised: " & getCurrentExceptionMsg()
|
||||
|
||||
(waitFor manager.register(credentials, UserMessageLimit(20))).isOkOr:
|
||||
assert false,
|
||||
"error returned when calling register: " & error
|
||||
waitFor fut
|
||||
|
||||
let rootUpdated = waitFor manager.updateRoots()
|
||||
@ -337,11 +327,9 @@ suite "Onchain group manager":
|
||||
|
||||
let idCredential = generateCredentials()
|
||||
|
||||
try:
|
||||
waitFor manager.register(idCredential, UserMessageLimit(20))
|
||||
except Exception, CatchableError:
|
||||
assert false,
|
||||
"exception raised when calling startGroupSync: " & getCurrentExceptionMsg()
|
||||
(waitFor manager.register(idCredential, UserMessageLimit(20))).isOkOr:
|
||||
assert false,
|
||||
"error returned when calling register: " & error
|
||||
|
||||
let messageBytes = "Hello".toBytes()
|
||||
|
||||
@ -395,14 +383,12 @@ suite "Onchain group manager":
|
||||
|
||||
return callback
|
||||
|
||||
try:
|
||||
manager.onRegister(generateCallback(futures, credentials))
|
||||
manager.onRegister(generateCallback(futures, credentials))
|
||||
|
||||
for i in 0 ..< credentials.len():
|
||||
waitFor manager.register(credentials[i], UserMessageLimit(20))
|
||||
discard waitFor manager.updateRoots()
|
||||
except Exception, CatchableError:
|
||||
assert false, "exception raised: " & getCurrentExceptionMsg()
|
||||
for i in 0 ..< credentials.len():
|
||||
(waitFor manager.register(credentials[i], UserMessageLimit(20))).isOkOr:
|
||||
assert false, "Failed to register credential " & $i & ": " & error
|
||||
discard waitFor manager.updateRoots()
|
||||
|
||||
waitFor allFutures(futures)
|
||||
|
||||
|
||||
@ -239,11 +239,8 @@ suite "Waku rln relay":
|
||||
let manager = cast[OnchainGroupManager](wakuRlnRelay.groupManager)
|
||||
let idCredentials = generateCredentials()
|
||||
|
||||
try:
|
||||
waitFor manager.register(idCredentials, UserMessageLimit(20))
|
||||
except Exception, CatchableError:
|
||||
assert false,
|
||||
"exception raised when calling register: " & getCurrentExceptionMsg()
|
||||
(waitFor manager.register(idCredentials, UserMessageLimit(20))).isOkOr:
|
||||
assert false, "error returned when calling register: " & error
|
||||
|
||||
let epoch1 = wakuRlnRelay.getCurrentEpoch()
|
||||
|
||||
@ -296,11 +293,8 @@ suite "Waku rln relay":
|
||||
let manager = cast[OnchainGroupManager](wakuRlnRelay.groupManager)
|
||||
let idCredentials = generateCredentials()
|
||||
|
||||
try:
|
||||
waitFor manager.register(idCredentials, UserMessageLimit(20))
|
||||
except Exception, CatchableError:
|
||||
assert false,
|
||||
"exception raised when calling register: " & getCurrentExceptionMsg()
|
||||
(waitFor manager.register(idCredentials, UserMessageLimit(20))).isOkOr:
|
||||
assert false, "error returned when calling register: " & error
|
||||
|
||||
# usually it's 20 seconds but we set it to 1 for testing purposes which make the test faster
|
||||
wakuRlnRelay.rlnMaxTimestampGap = 1
|
||||
@ -346,11 +340,9 @@ suite "Waku rln relay":
|
||||
let manager1 = cast[OnchainGroupManager](wakuRlnRelay1.groupManager)
|
||||
let idCredentials1 = generateCredentials()
|
||||
|
||||
try:
|
||||
waitFor manager1.register(idCredentials1, UserMessageLimit(20))
|
||||
except Exception, CatchableError:
|
||||
(waitFor manager1.register(idCredentials1, UserMessageLimit(20))).isOkOr:
|
||||
assert false,
|
||||
"exception raised when calling register: " & getCurrentExceptionMsg()
|
||||
"error returned when calling register: " & error
|
||||
|
||||
let index2 = MembershipIndex(6)
|
||||
let rlnConf2 = getWakuRlnConfig(manager = manager, index = index2)
|
||||
@ -360,11 +352,9 @@ suite "Waku rln relay":
|
||||
let manager2 = cast[OnchainGroupManager](wakuRlnRelay2.groupManager)
|
||||
let idCredentials2 = generateCredentials()
|
||||
|
||||
try:
|
||||
waitFor manager2.register(idCredentials2, UserMessageLimit(20))
|
||||
except Exception, CatchableError:
|
||||
(waitFor manager2.register(idCredentials2, UserMessageLimit(20))).isOkOr:
|
||||
assert false,
|
||||
"exception raised when calling register: " & getCurrentExceptionMsg()
|
||||
"error returned when calling register: " & error
|
||||
|
||||
# get the current epoch time
|
||||
let epoch = wakuRlnRelay1.getCurrentEpoch()
|
||||
|
||||
@ -65,11 +65,9 @@ procSuite "WakuNode - RLN relay":
|
||||
let manager1 = cast[OnchainGroupManager](node1.wakuRlnRelay.groupManager)
|
||||
let idCredentials1 = generateCredentials()
|
||||
|
||||
try:
|
||||
waitFor manager1.register(idCredentials1, UserMessageLimit(20))
|
||||
except Exception, CatchableError:
|
||||
(waitFor manager1.register(idCredentials1, UserMessageLimit(20))).isOkOr:
|
||||
assert false,
|
||||
"exception raised when calling register: " & getCurrentExceptionMsg()
|
||||
"error returned when calling register: " & error
|
||||
|
||||
let rootUpdated1 = waitFor manager1.updateRoots()
|
||||
info "Updated root for node1", rootUpdated1
|
||||
@ -182,11 +180,9 @@ procSuite "WakuNode - RLN relay":
|
||||
let manager = cast[OnchainGroupManager](node.wakuRlnRelay.groupManager)
|
||||
let idCredentials = generateCredentials()
|
||||
|
||||
try:
|
||||
waitFor manager.register(idCredentials, UserMessageLimit(20))
|
||||
except Exception, CatchableError:
|
||||
(waitFor manager.register(idCredentials, UserMessageLimit(20))).isOkOr:
|
||||
assert false,
|
||||
"exception raised when calling register: " & getCurrentExceptionMsg()
|
||||
"error returned when calling register: " & error
|
||||
|
||||
let rootUpdated = waitFor manager.updateRoots()
|
||||
info "Updated root for node", node = index + 1, rootUpdated = rootUpdated
|
||||
@ -301,11 +297,9 @@ procSuite "WakuNode - RLN relay":
|
||||
let manager1 = cast[OnchainGroupManager](node1.wakuRlnRelay.groupManager)
|
||||
let idCredentials1 = generateCredentials()
|
||||
|
||||
try:
|
||||
waitFor manager1.register(idCredentials1, UserMessageLimit(20))
|
||||
except Exception, CatchableError:
|
||||
(waitFor manager1.register(idCredentials1, UserMessageLimit(20))).isOkOr:
|
||||
assert false,
|
||||
"exception raised when calling register: " & getCurrentExceptionMsg()
|
||||
"error returned when calling register: " & error
|
||||
|
||||
let rootUpdated1 = waitFor manager1.updateRoots()
|
||||
info "Updated root for node1", rootUpdated1
|
||||
@ -418,11 +412,9 @@ procSuite "WakuNode - RLN relay":
|
||||
let manager1 = cast[OnchainGroupManager](node1.wakuRlnRelay.groupManager)
|
||||
let idCredentials1 = generateCredentials()
|
||||
|
||||
try:
|
||||
waitFor manager1.register(idCredentials1, UserMessageLimit(20))
|
||||
except Exception, CatchableError:
|
||||
(waitFor manager1.register(idCredentials1, UserMessageLimit(20))).isOkOr:
|
||||
assert false,
|
||||
"exception raised when calling register: " & getCurrentExceptionMsg()
|
||||
"error returned when calling register: " & error
|
||||
|
||||
let rootUpdated1 = waitFor manager1.updateRoots()
|
||||
info "Updated root for node1", rootUpdated1
|
||||
@ -585,11 +577,10 @@ procSuite "WakuNode - RLN relay":
|
||||
let manager1 = cast[OnchainGroupManager](node1.wakuRlnRelay.groupManager)
|
||||
let idCredentials1 = generateCredentials()
|
||||
|
||||
try:
|
||||
waitFor manager1.register(idCredentials1, UserMessageLimit(20))
|
||||
except Exception, CatchableError:
|
||||
assert false,
|
||||
"exception raised when calling register: " & getCurrentExceptionMsg()
|
||||
(waitFor manager1.register(idCredentials1, UserMessageLimit(20))).isOkOr:
|
||||
assert false,
|
||||
"error returned when calling register: " & error
|
||||
|
||||
|
||||
let rootUpdated1 = waitFor manager1.updateRoots()
|
||||
info "Updated root for node1", rootUpdated1
|
||||
|
||||
@ -73,7 +73,10 @@ proc doRlnKeystoreGenerator*(conf: RlnKeystoreGeneratorConf) =
|
||||
|
||||
# 4. register on-chain
|
||||
try:
|
||||
waitFor groupManager.register(credential, conf.userMessageLimit)
|
||||
let registerResult = waitFor groupManager.register(credential, conf.userMessageLimit)
|
||||
if registerResult.isErr():
|
||||
error "Failed to register on-chain", error = registerResult.error
|
||||
quit(QuitFailure)
|
||||
except Exception, CatchableError:
|
||||
error "failure while registering credentials on-chain",
|
||||
error = getCurrentExceptionMsg()
|
||||
|
||||
@ -148,12 +148,6 @@ template initializedGuard(g: OnchainGroupManager): untyped =
|
||||
if not g.initialized:
|
||||
raise newException(CatchableError, "OnchainGroupManager is not initialized")
|
||||
|
||||
template retryWrapper(
|
||||
g: OnchainGroupManager, res: auto, errStr: string, body: untyped
|
||||
): auto =
|
||||
retryWrapper(res, RetryStrategy.new(), errStr, g.onFatalErrorAction):
|
||||
body
|
||||
|
||||
proc updateRoots*(g: OnchainGroupManager): Future[bool] {.async.} =
|
||||
let rootRes = (await g.fetchMerkleRoot()).valueOr:
|
||||
return false
|
||||
@ -172,7 +166,7 @@ proc updateRoots*(g: OnchainGroupManager): Future[bool] {.async.} =
|
||||
|
||||
return false
|
||||
|
||||
proc trackRootChanges*(g: OnchainGroupManager) {.async: (raises: [CatchableError]).} =
|
||||
proc trackRootChanges*(g: OnchainGroupManager): Future[Result[void, string]] {.async.} =
|
||||
try:
|
||||
initializedGuard(g)
|
||||
const rpcDelay = 5.seconds
|
||||
@ -194,18 +188,21 @@ proc trackRootChanges*(g: OnchainGroupManager) {.async: (raises: [CatchableError
|
||||
|
||||
let nextFreeIndex = (await g.fetchNextFreeIndex()).valueOr:
|
||||
error "Failed to fetch next free index", error = error
|
||||
raise
|
||||
newException(CatchableError, "Failed to fetch next free index: " & error)
|
||||
return err("Failed to fetch next free index: " & error)
|
||||
|
||||
let memberCount = cast[int64](nextFreeIndex)
|
||||
waku_rln_number_registered_memberships.set(float64(memberCount))
|
||||
except CatchableError:
|
||||
error "Fatal error in trackRootChanges", error = getCurrentExceptionMsg()
|
||||
return err("Fatal error in trackRootChanges" & getCurrentExceptionMsg())
|
||||
|
||||
method register*(
|
||||
g: OnchainGroupManager, rateCommitment: RateCommitment
|
||||
): Future[void] {.async: (raises: [Exception]).} =
|
||||
initializedGuard(g)
|
||||
): Future[Result[void, string]] {.async.} =
|
||||
try:
|
||||
initializedGuard(g)
|
||||
except CatchableError as e:
|
||||
return err("Not initialized: " & e.msg)
|
||||
|
||||
try:
|
||||
let leaf = rateCommitment.toLeaf().get()
|
||||
@ -214,33 +211,43 @@ method register*(
|
||||
info "registering member via callback", rateCommitment = leaf, index = idx
|
||||
await g.registerCb.get()(@[Membership(rateCommitment: leaf, index: idx)])
|
||||
g.latestIndex.inc()
|
||||
except CatchableError:
|
||||
raise newException(ValueError, getCurrentExceptionMsg())
|
||||
except Exception as e:
|
||||
return err("Failed to call register callback: " & e.msg)
|
||||
|
||||
return ok()
|
||||
|
||||
method register*(
|
||||
g: OnchainGroupManager,
|
||||
identityCredential: IdentityCredential,
|
||||
userMessageLimit: UserMessageLimit,
|
||||
): Future[void] {.async: (raises: [Exception]).} =
|
||||
initializedGuard(g)
|
||||
): Future[Result[void, string]] {.async.} =
|
||||
try:
|
||||
initializedGuard(g)
|
||||
except CatchableError as e:
|
||||
return err("Not initialized: " & e.msg)
|
||||
|
||||
let ethRpc = g.ethRpc.get()
|
||||
let wakuRlnContract = g.wakuRlnContract.get()
|
||||
|
||||
var gasPrice: int
|
||||
g.retryWrapper(gasPrice, "Failed to get gas price"):
|
||||
let fetchedGasPrice = uint64(await ethRpc.provider.eth_gasPrice())
|
||||
## Multiply by 2 to speed up the transaction
|
||||
## Check for overflow when casting to int
|
||||
if fetchedGasPrice > uint64(high(int) div 2):
|
||||
warn "Gas price overflow detected, capping at maximum int value",
|
||||
fetchedGasPrice = fetchedGasPrice, maxInt = high(int)
|
||||
high(int)
|
||||
else:
|
||||
let calculatedGasPrice = int(fetchedGasPrice) * 2
|
||||
debug "Gas price calculated",
|
||||
fetchedGasPrice = fetchedGasPrice, gasPrice = calculatedGasPrice
|
||||
calculatedGasPrice
|
||||
let gasPrice = (
|
||||
await retryWrapper(
|
||||
RetryStrategy.new(),
|
||||
"Failed to get gas price",
|
||||
proc(): Future[int] {.async.} =
|
||||
let fetchedGasPrice = uint64(await ethRpc.provider.eth_gasPrice())
|
||||
if fetchedGasPrice > uint64(high(int) div 2):
|
||||
warn "Gas price overflow detected, capping at maximum int value",
|
||||
fetchedGasPrice = fetchedGasPrice, maxInt = high(int)
|
||||
return high(int)
|
||||
else:
|
||||
let calculatedGasPrice = int(fetchedGasPrice) * 2
|
||||
debug "Gas price calculated",
|
||||
fetchedGasPrice = fetchedGasPrice, gasPrice = calculatedGasPrice
|
||||
return calculatedGasPrice,
|
||||
)
|
||||
).valueOr:
|
||||
return err("Failed to get gas price: " & error)
|
||||
|
||||
let idCommitmentHex = identityCredential.idCommitment.inHex()
|
||||
debug "identityCredential idCommitmentHex", idCommitment = idCommitmentHex
|
||||
let idCommitment = identityCredential.idCommitment.toUInt256()
|
||||
@ -249,27 +256,37 @@ method register*(
|
||||
idCommitment = idCommitment,
|
||||
userMessageLimit = userMessageLimit,
|
||||
idCommitmentsToErase = idCommitmentsToErase
|
||||
var txHash: TxHash
|
||||
g.retryWrapper(txHash, "Failed to register the member"):
|
||||
await wakuRlnContract
|
||||
.register(idCommitment, userMessageLimit.stuint(32), idCommitmentsToErase)
|
||||
.send(gasPrice = gasPrice)
|
||||
let txHash = (
|
||||
await retryWrapper(
|
||||
RetryStrategy.new(),
|
||||
"Failed to register the member",
|
||||
proc(): Future[TxHash] {.async.} =
|
||||
return await wakuRlnContract
|
||||
.register(idCommitment, userMessageLimit.stuint(32), idCommitmentsToErase)
|
||||
.send(gasPrice = gasPrice),
|
||||
)
|
||||
).valueOr:
|
||||
return err("Failed to register member: " & error)
|
||||
|
||||
# wait for the transaction to be mined
|
||||
var tsReceipt: ReceiptObject
|
||||
g.retryWrapper(tsReceipt, "Failed to get the transaction receipt"):
|
||||
await ethRpc.getMinedTransactionReceipt(txHash)
|
||||
let tsReceipt = (
|
||||
await retryWrapper(
|
||||
RetryStrategy.new(),
|
||||
"Failed to get the transaction receipt",
|
||||
proc(): Future[ReceiptObject] {.async.} =
|
||||
return await ethRpc.getMinedTransactionReceipt(txHash),
|
||||
)
|
||||
).valueOr:
|
||||
return err("Failed to get transaction receipt: " & error)
|
||||
debug "registration transaction mined", txHash = txHash
|
||||
g.registrationTxHash = some(txHash)
|
||||
# the receipt topic holds the hash of signature of the raised events
|
||||
debug "ts receipt", receipt = tsReceipt[]
|
||||
|
||||
if tsReceipt.status.isNone():
|
||||
raise newException(ValueError, "Transaction failed: status is None")
|
||||
return err("Transaction failed: status is None")
|
||||
if tsReceipt.status.get() != 1.Quantity:
|
||||
raise newException(
|
||||
ValueError, "Transaction failed with status: " & $tsReceipt.status.get()
|
||||
)
|
||||
return err("Transaction failed with status: " & $tsReceipt.status.get())
|
||||
|
||||
## Search through all transaction logs to find the MembershipRegistered event
|
||||
let expectedEventSignature = cast[FixedBytes[32]](keccak.keccak256.digest(
|
||||
@ -283,9 +300,7 @@ method register*(
|
||||
break
|
||||
|
||||
if membershipRegisteredLog.isNone():
|
||||
raise newException(
|
||||
ValueError, "register: MembershipRegistered event not found in transaction logs"
|
||||
)
|
||||
return err("register: MembershipRegistered event not found in transaction logs")
|
||||
|
||||
let registrationLog = membershipRegisteredLog.get()
|
||||
|
||||
@ -309,20 +324,33 @@ method register*(
|
||||
|
||||
if g.registerCb.isSome():
|
||||
let member = Membership(rateCommitment: rateCommitment, index: g.latestIndex)
|
||||
await g.registerCb.get()(@[member])
|
||||
try:
|
||||
await g.registerCb.get()(@[member])
|
||||
except Exception as e:
|
||||
return err("Failed to call register callback: " & e.msg)
|
||||
g.latestIndex.inc()
|
||||
|
||||
return
|
||||
return ok()
|
||||
|
||||
method withdraw*(
|
||||
g: OnchainGroupManager, idCommitment: IDCommitment
|
||||
): Future[void] {.async: (raises: [Exception]).} =
|
||||
initializedGuard(g) # TODO: after slashing is enabled on the contract
|
||||
): Future[Result[void, string]] {.async.} =
|
||||
try:
|
||||
initializedGuard(g)
|
||||
except CatchableError as e:
|
||||
return err("Not initialized: " & e.msg)
|
||||
|
||||
return ok()
|
||||
|
||||
method withdrawBatch*(
|
||||
g: OnchainGroupManager, idCommitments: seq[IDCommitment]
|
||||
): Future[void] {.async: (raises: [Exception]).} =
|
||||
initializedGuard(g)
|
||||
): Future[Result[void, string]] {.async.} =
|
||||
try:
|
||||
initializedGuard(g)
|
||||
except CatchableError as e:
|
||||
return err("Not initialized: " & e.msg)
|
||||
|
||||
return ok()
|
||||
|
||||
proc getRootFromProofAndIndex(
|
||||
g: OnchainGroupManager, elements: seq[byte], bits: seq[byte]
|
||||
@ -492,25 +520,30 @@ method onWithdraw*(g: OnchainGroupManager, cb: OnWithdrawCallback) {.gcsafe.} =
|
||||
proc establishConnection(
|
||||
g: OnchainGroupManager
|
||||
): Future[GroupManagerResult[Web3]] {.async.} =
|
||||
var ethRpc: Web3
|
||||
let ethRpc = (
|
||||
await retryWrapper(
|
||||
RetryStrategy.new(),
|
||||
"Failed to connect to the Ethereum client",
|
||||
proc(): Future[Web3] {.async.} =
|
||||
var innerEthRpc: Web3
|
||||
var connected = false
|
||||
for clientUrl in g.ethClientUrls:
|
||||
## We give a chance to the user to provide multiple clients
|
||||
## and we try to connect to each of them
|
||||
try:
|
||||
innerEthRpc = await newWeb3(clientUrl)
|
||||
connected = true
|
||||
break
|
||||
except CatchableError:
|
||||
error "failed connect Eth client", error = getCurrentExceptionMsg()
|
||||
|
||||
g.retryWrapper(ethRpc, "Failed to connect to the Ethereum client"):
|
||||
var innerEthRpc: Web3
|
||||
var connected = false
|
||||
for clientUrl in g.ethClientUrls:
|
||||
## We give a chance to the user to provide multiple clients
|
||||
## and we try to connect to each of them
|
||||
try:
|
||||
innerEthRpc = await newWeb3(clientUrl)
|
||||
connected = true
|
||||
break
|
||||
except CatchableError:
|
||||
error "failed connect Eth client", error = getCurrentExceptionMsg()
|
||||
if not connected:
|
||||
raise newException(CatchableError, "all failed")
|
||||
|
||||
if not connected:
|
||||
raise newException(CatchableError, "all failed")
|
||||
|
||||
innerEthRpc
|
||||
return innerEthRpc,
|
||||
)
|
||||
).valueOr:
|
||||
return err("Failed to establish Ethereum connection: " & error)
|
||||
|
||||
return ok(ethRpc)
|
||||
|
||||
@ -519,9 +552,15 @@ method init*(g: OnchainGroupManager): Future[GroupManagerResult[void]] {.async.}
|
||||
let ethRpc: Web3 = (await establishConnection(g)).valueOr:
|
||||
return err("failed to connect to Ethereum clients: " & $error)
|
||||
|
||||
var fetchedChainId: UInt256
|
||||
g.retryWrapper(fetchedChainId, "Failed to get the chain id"):
|
||||
await ethRpc.provider.eth_chainId()
|
||||
let fetchedChainId = (
|
||||
await retryWrapper(
|
||||
RetryStrategy.new(),
|
||||
"Failed to get the chain id",
|
||||
proc(): Future[UInt256] {.async.} =
|
||||
return await ethRpc.provider.eth_chainId(),
|
||||
)
|
||||
).valueOr:
|
||||
return err("Failed to get chain id: " & error)
|
||||
|
||||
# Set the chain id
|
||||
if g.chainId == 0:
|
||||
@ -595,8 +634,10 @@ method init*(g: OnchainGroupManager): Future[GroupManagerResult[void]] {.async.}
|
||||
proc onDisconnect() {.async.} =
|
||||
error "Ethereum client disconnected"
|
||||
|
||||
var newEthRpc: Web3 = (await g.establishConnection()).valueOr:
|
||||
g.onFatalErrorAction("failed to connect to Ethereum clients onDisconnect")
|
||||
let newEthRpc: Web3 = (await g.establishConnection()).valueOr:
|
||||
error "Fatal: failed to reconnect to Ethereum clients after disconnect",
|
||||
error = error
|
||||
g.onFatalErrorAction("failed to reconnect to Ethereum clients: " & error)
|
||||
return
|
||||
|
||||
newEthRpc.ondisconnect = ethRpc.ondisconnect
|
||||
|
||||
@ -1,36 +1,33 @@
|
||||
import ../../../common/error_handling
|
||||
import chronos
|
||||
import results
|
||||
|
||||
const
|
||||
DefaultRetryDelay* = 4000.millis
|
||||
DefaultRetryCount* = 15'u
|
||||
|
||||
type RetryStrategy* = object
|
||||
shouldRetry*: bool
|
||||
retryDelay*: Duration
|
||||
retryCount*: uint
|
||||
|
||||
proc new*(T: type RetryStrategy): RetryStrategy =
|
||||
return RetryStrategy(shouldRetry: true, retryDelay: 4000.millis, retryCount: 15)
|
||||
return RetryStrategy(retryDelay: DefaultRetryDelay, retryCount: DefaultRetryCount)
|
||||
|
||||
template retryWrapper*(
|
||||
res: auto,
|
||||
proc retryWrapper*[T](
|
||||
retryStrategy: RetryStrategy,
|
||||
errStr: string,
|
||||
errCallback: OnFatalErrorHandler,
|
||||
body: untyped,
|
||||
): auto =
|
||||
if errCallback == nil:
|
||||
raise newException(CatchableError, "Ensure that the errCallback is set")
|
||||
body: proc(): Future[T] {.async.},
|
||||
): Future[Result[T, string]] {.async.} =
|
||||
var retryCount = retryStrategy.retryCount
|
||||
var shouldRetry = retryStrategy.shouldRetry
|
||||
var exceptionMessage = ""
|
||||
var lastError = ""
|
||||
|
||||
while shouldRetry and retryCount > 0:
|
||||
while retryCount > 0:
|
||||
try:
|
||||
res = body
|
||||
shouldRetry = false
|
||||
except:
|
||||
let value = await body()
|
||||
return ok(value)
|
||||
except CatchableError as e:
|
||||
retryCount -= 1
|
||||
exceptionMessage = getCurrentExceptionMsg()
|
||||
await sleepAsync(retryStrategy.retryDelay)
|
||||
if shouldRetry:
|
||||
errCallback(errStr & ": " & exceptionMessage)
|
||||
return
|
||||
lastError = e.msg
|
||||
if retryCount > 0:
|
||||
await sleepAsync(retryStrategy.retryDelay)
|
||||
|
||||
return err(errStr & ": " & lastError)
|
||||
|
||||
@ -64,7 +64,7 @@ type WakuRLNRelay* = ref object of RootObj
|
||||
onFatalErrorAction*: OnFatalErrorHandler
|
||||
nonceManager*: NonceManager
|
||||
epochMonitorFuture*: Future[void]
|
||||
rootChangesFuture*: Future[void]
|
||||
rootChangesFuture*: Future[Result[void, string]]
|
||||
|
||||
proc calcEpoch*(rlnPeer: WakuRLNRelay, t: float64): Epoch =
|
||||
## gets time `t` as `flaot64` with subseconds resolution in the fractional part
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user