diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1745146a09..464a2fe8e4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -141,3 +141,25 @@ jobs: nim_wakunode_image: ${{ needs.build-docker-image.outputs.image }} test_type: node-optional debug: waku* + + lint: + name: "Lint" + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 2 # In PR, has extra merge commit: ^1 = PR, ^2 = base + + - name: Check nph formatting + # Pin nph to a specific version to avoid sudden style differences. + # Updating nph version should be accompanied with running the new + # version on the fluffy directory. + run: | + VERSION="v0.5.1" + ARCHIVE="nph-linux_x64.tar.gz" + curl -L "https://github.com/arnetheduck/nph/releases/download/${VERSION}/${ARCHIVE}" -o ${ARCHIVE} + tar -xzf ${ARCHIVE} + shopt -s extglob # Enable extended globbing + ./nph examples waku tests tools apps *.@(nim|nims|nimble) + git diff --exit-code diff --git a/config.nims b/config.nims index 55591558eb..4c3d8dfacf 100644 --- a/config.nims +++ b/config.nims @@ -9,7 +9,8 @@ if defined(windows): # increase stack size switch("passL", "-Wl,--stack,8388608") # https://github.com/nim-lang/Nim/issues/4057 - --tlsEmulation:off + --tlsEmulation: + off if defined(i386): # set the IMAGE_FILE_LARGE_ADDRESS_AWARE flag so we can use PAE, if enabled, and access more than 2 GiB of RAM switch("passL", "-Wl,--large-address-aware") @@ -60,14 +61,18 @@ else: switch("passC", "-mno-avx512f") switch("passL", "-mno-avx512f") - ---threads:on ---opt:speed ---excessiveStackTrace:on +--threads: + on +--opt: + speed +--excessiveStackTrace: + on # enable metric collection ---define:metrics +--define: + metrics # for heap-usage-by-instance-type metrics and object base-type strings ---define:nimTypeNames +--define: + nimTypeNames switch("define", "withoutPCRE") @@ -75,13 +80,17 @@ switch("define", "withoutPCRE") # "--debugger:native" build. It can be increased with `ulimit -n 1024`. if not defined(macosx) and not defined(android): # add debugging symbols and original files and line numbers - --debugger:native + --debugger: + native if not (defined(windows) and defined(i386)) and not defined(disable_libbacktrace): # light-weight stack traces using libbacktrace and libunwind - --define:nimStackTraceOverride + --define: + nimStackTraceOverride switch("import", "libbacktrace") ---define:nimOldCaseObjects # https://github.com/status-im/nim-confutils/issues/9 +--define: + nimOldCaseObjects + # https://github.com/status-im/nim-confutils/issues/9 # `switch("warning[CaseTransition]", "off")` fails with "Error: invalid command line option: '--warning[CaseTransition]'" switch("warning", "CaseTransition:off") diff --git a/examples/wakustealthcommitments/node_spec.nim b/examples/wakustealthcommitments/node_spec.nim index 6edb66cc3a..4416f565fb 100644 --- a/examples/wakustealthcommitments/node_spec.nim +++ b/examples/wakustealthcommitments/node_spec.nim @@ -1,7 +1,6 @@ {.push raises: [].} -import - waku/[common/logging, factory/[waku, networks_config, external_config]] +import waku/[common/logging, factory/[waku, networks_config, external_config]] import std/[options, strutils, os, sequtils], stew/shims/net as stewNet, diff --git a/examples/wakustealthcommitments/stealth_commitment_protocol.nim b/examples/wakustealthcommitments/stealth_commitment_protocol.nim index 262955adda..c6e6d6b9c3 100644 --- a/examples/wakustealthcommitments/stealth_commitment_protocol.nim +++ b/examples/wakustealthcommitments/stealth_commitment_protocol.nim @@ -2,9 +2,7 @@ import results, - waku/[common/logging, - waku_node, - waku_rln_relay,], + waku/[common/logging, waku_node, waku_rln_relay], ./erc_5564_interface as StealthCommitmentFFI, ./node_spec, ./wire_spec diff --git a/tests/incentivization/test_all.nim b/tests/incentivization/test_all.nim index 4a38f788b3..3efdc7d6e7 100644 --- a/tests/incentivization/test_all.nim +++ b/tests/incentivization/test_all.nim @@ -1,2 +1 @@ -import - ./test_rpc_codec \ No newline at end of file +import ./test_rpc_codec diff --git a/tests/waku_rln_relay/test_rln_group_manager_onchain.nim b/tests/waku_rln_relay/test_rln_group_manager_onchain.nim index 8d0a48eaab..bb44838015 100644 --- a/tests/waku_rln_relay/test_rln_group_manager_onchain.nim +++ b/tests/waku_rln_relay/test_rln_group_manager_onchain.nim @@ -215,8 +215,9 @@ proc setup(): Future[OnchainGroupManager] {.async.} = chainId: CHAIN_ID, ethPrivateKey: pk, rlnInstance: rlnInstance, - onFatalErrorAction: proc (errStr: string) = + onFatalErrorAction: proc(errStr: string) = raiseAssert errStr + , ) return manager @@ -277,8 +278,9 @@ suite "Onchain group manager": ethClientUrl: EthClient, ethContractAddress: $differentContractAddress, rlnInstance: manager.rlnInstance, - onFatalErrorAction: proc (errStr: string) = + onFatalErrorAction: proc(errStr: string) = raiseAssert errStr + , ) (await manager2.init()).isErrOr: raiseAssert "Expected error when contract address doesn't match" @@ -779,7 +781,7 @@ suite "Onchain group manager": isReady == true await manager.stop() - + asyncTest "rln-relay-max-message-limit testing": let nodekey = generateSecp256k1Key() @@ -797,9 +799,9 @@ suite "Onchain group manager": try: await node.mountRlnRelay(wakuRlnConfig) except CatchableError as e: - check e.msg == "failed to mount WakuRlnRelay: rln-relay-user-message-limit can't be exceed then MAX_MESSAGE_LIMIT set by rln contract" + check e.msg == + "failed to mount WakuRlnRelay: rln-relay-user-message-limit can't be exceed then MAX_MESSAGE_LIMIT set by rln contract" - ################################ ## Terminating/removing Anvil ################################ diff --git a/tools/rln_keystore_generator/rln_keystore_generator.nim b/tools/rln_keystore_generator/rln_keystore_generator.nim index 5e8c5e6423..1bde9ae01e 100644 --- a/tools/rln_keystore_generator/rln_keystore_generator.nim +++ b/tools/rln_keystore_generator/rln_keystore_generator.nim @@ -63,7 +63,7 @@ proc doRlnKeystoreGenerator*(conf: WakuNodeConf) = keystorePath: none(string), keystorePassword: none(string), ethPrivateKey: some(conf.rlnRelayEthPrivateKey), - onFatalErrorAction: onFatalErrorAction + onFatalErrorAction: onFatalErrorAction, ) try: (waitFor groupManager.init()).isOkOr: diff --git a/waku/common/rate_limit/per_peer_limiter.nim b/waku/common/rate_limit/per_peer_limiter.nim index 0d02fe0a8b..59921beee3 100644 --- a/waku/common/rate_limit/per_peer_limiter.nim +++ b/waku/common/rate_limit/per_peer_limiter.nim @@ -4,8 +4,6 @@ ## Rate limit is applied separately by each peer upon first use. Also time period is counted distinct per peer. ## It will use compensating replenish mode for peers to balance the load and allow fair usage of a service. - - {.push raises: [].} import std/[options, tables], chronos/timer, libp2p/stream/connection, libp2p/utility diff --git a/waku/incentivization/rpc.nim b/waku/incentivization/rpc.nim index 5b0d0e99bc..d16ca75a8d 100644 --- a/waku/incentivization/rpc.nim +++ b/waku/incentivization/rpc.nim @@ -1,14 +1,10 @@ -import - json_serialization, - std/options -import - ../waku_core +import json_serialization, std/options +import ../waku_core # Implementing the RFC: # https://github.com/vacp2p/rfc/tree/master/content/docs/rfcs/73 type - EligibilityProof* = object proofOfPayment*: Option[seq[byte]] diff --git a/waku/incentivization/rpc_codec.nim b/waku/incentivization/rpc_codec.nim index dbe6e9daff..5d3ce48d53 100644 --- a/waku/incentivization/rpc_codec.nim +++ b/waku/incentivization/rpc_codec.nim @@ -1,14 +1,9 @@ -import - std/options -import - ../common/protobuf, - ../waku_core, - ./rpc - +import std/options +import ../common/protobuf, ../waku_core, ./rpc # Codec for EligibilityProof -proc encode*(epRpc: EligibilityProof): ProtoBuffer = +proc encode*(epRpc: EligibilityProof): ProtoBuffer = var pb = initProtoBuffer() if epRpc.proofOfPayment.isSome(): let proofOfPayment = epRpc.proofOfPayment.get() @@ -18,7 +13,7 @@ proc encode*(epRpc: EligibilityProof): ProtoBuffer = discard pb -proc decode*(T: type EligibilityProof, buffer: seq[byte]): ProtobufResult[T] = +proc decode*(T: type EligibilityProof, buffer: seq[byte]): ProtobufResult[T] = let pb = initProtoBuffer(buffer) var epRpc = EligibilityProof() var proofOfPayment = newSeq[byte]() @@ -28,17 +23,16 @@ proc decode*(T: type EligibilityProof, buffer: seq[byte]): ProtobufResult[T] = epRpc.proofOfPayment = some(proofOfPayment) ok(epRpc) - # Codec for EligibilityStatus -proc encode*(esRpc: EligibilityStatus): ProtoBuffer = +proc encode*(esRpc: EligibilityStatus): ProtoBuffer = var pb = initProtoBuffer() pb.write3(1, esRpc.statusCode) if esRpc.statusDesc.isSome(): pb.write3(2, esRpc.statusDesc.get()) pb -proc decode*(T: type EligibilityStatus, buffer: seq[byte]): ProtobufResult[T] = +proc decode*(T: type EligibilityStatus, buffer: seq[byte]): ProtobufResult[T] = let pb = initProtoBuffer(buffer) var esRpc = EligibilityStatus() # status code @@ -55,5 +49,3 @@ proc decode*(T: type EligibilityStatus, buffer: seq[byte]): ProtobufResult[T] = else: esRpc.statusDesc = some(description) ok(esRpc) - - diff --git a/waku/waku_api/rest/filter/types.nim b/waku/waku_api/rest/filter/types.nim index a4b69521fc..7497d6a78e 100644 --- a/waku/waku_api/rest/filter/types.nim +++ b/waku/waku_api/rest/filter/types.nim @@ -426,6 +426,5 @@ proc readValue*( reader.raiseUnexpectedValue("Field `requestId` is missing") value = FilterSubscriptionResponse( - requestId: requestId.get(), - statusDesc: statusDesc.get(""), + requestId: requestId.get(), statusDesc: statusDesc.get("") ) diff --git a/waku/waku_api/rest/store/types.nim b/waku/waku_api/rest/store/types.nim index a3db8fbaa1..e19bcf6be9 100644 --- a/waku/waku_api/rest/store/types.nim +++ b/waku/waku_api/rest/store/types.nim @@ -33,7 +33,8 @@ proc parseHash*(input: Option[string]): Result[Option[WakuMessageHash], string] return err("waku message hash parsing error: " & error) if decodedBytes.len != 32: - return err("waku message hash parsing error: invalid hash length: " & $decodedBytes.len) + return + err("waku message hash parsing error: invalid hash length: " & $decodedBytes.len) let hash: WakuMessageHash = fromBytes(decodedBytes) diff --git a/waku/waku_rln_relay/constants.nim b/waku/waku_rln_relay/constants.nim index 16678ad5c7..4918a16a4a 100644 --- a/waku/waku_rln_relay/constants.nim +++ b/waku/waku_rln_relay/constants.nim @@ -30,8 +30,8 @@ const DefaultRlnTreePath* = "rln_tree.db" const # pre-processed "rln/waku-rln-relay/v2.0.0" to array[32, byte] DefaultRlnIdentifier*: RlnIdentifier = [ - 114, 108, 110, 47, 119, 97, 107, 117, 45, 114, 108, 110, 45, 114, 101, 108, 97, - 121, 47, 118, 50, 46, 48, 46, 48, 0, 0, 0, 0, 0, 0, 0, + 114, 108, 110, 47, 119, 97, 107, 117, 45, 114, 108, 110, 45, 114, 101, 108, 97, 121, + 47, 118, 50, 46, 48, 46, 48, 0, 0, 0, 0, 0, 0, 0, ] DefaultUserMessageLimit* = UserMessageLimit(20) @@ -60052,7 +60052,6 @@ const StaticGroupKeys* = const StaticGroupMerkleRoot* = "2c149e48886b5ba3da2edf8db8d7a364ae7a25618489c04cf0c0380f7cdd4d6f" - const MaxClockGapSeconds* = 20.0 # the maximum clock difference between peers in seconds # RLN Keystore defaults diff --git a/waku/waku_rln_relay/contract.nim b/waku/waku_rln_relay/contract.nim index 360df6ea70..385a6d0ef1 100644 --- a/waku/waku_rln_relay/contract.nim +++ b/waku/waku_rln_relay/contract.nim @@ -1,4 +1,3 @@ - # This contract code is used in deployment, note: this is not the deployedBytecode, it includes constructor args. # Ref: https://github.com/waku-org/waku-rlnv2-contract const PoseidonT3* = diff --git a/waku/waku_rln_relay/conversion_utils.nim b/waku/waku_rln_relay/conversion_utils.nim index f6ce81bdae..d8be773d69 100644 --- a/waku/waku_rln_relay/conversion_utils.nim +++ b/waku/waku_rln_relay/conversion_utils.nim @@ -78,7 +78,6 @@ proc serialize*( ) return output - proc serialize*(proof: RateLimitProof, data: openArray[byte]): seq[byte] = ## a private proc to convert RateLimitProof and data to a byte seq ## this conversion is used in the proof verification proc diff --git a/waku/waku_rln_relay/group_manager/on_chain/group_manager.nim b/waku/waku_rln_relay/group_manager/on_chain/group_manager.nim index 70c69b554b..1fada3a956 100644 --- a/waku/waku_rln_relay/group_manager/on_chain/group_manager.nim +++ b/waku/waku_rln_relay/group_manager/on_chain/group_manager.nim @@ -30,15 +30,11 @@ logScope: # using the when predicate does not work within the contract macro, hence need to dupe contract(WakuRlnContract): # this serves as an entrypoint into the rln membership set - proc register( - idCommitment: UInt256, userMessageLimit: UInt32 - ) + proc register(idCommitment: UInt256, userMessageLimit: UInt32) # Initializes the implementation contract (only used in unit tests) proc initialize(maxMessageLimit: UInt256) # this event is raised when a new member is registered - proc MemberRegistered( - rateCommitment: UInt256, index: Uint32 - ) {.event.} + proc MemberRegistered(rateCommitment: UInt256, index: Uint32) {.event.} # this function denotes existence of a given user proc memberExists(idCommitment: Uint256): UInt256 {.view.} @@ -136,7 +132,8 @@ method atomicBatch*( var membersSeq = newSeq[Membership]() for i in 0 ..< rateCommitments.len: var index = start + MembershipIndex(i) - debug "registering member to callback", rateCommitment = rateCommitments[i], index = index + debug "registering member to callback", + rateCommitment = rateCommitments[i], index = index let member = Membership(rateCommitment: rateCommitments[i], index: index) membersSeq.add(member) await g.registerCb.get()(membersSeq) @@ -154,7 +151,6 @@ method register*( except CatchableError: raise newException(ValueError, getCurrentExceptionMsg()) - method registerBatch*( g: OnchainGroupManager, rateCommitments: seq[RawRateCommitment] ): Future[void] {.async: (raises: [Exception]).} = @@ -179,13 +175,12 @@ method register*( let idCommitment = identityCredential.idCommitment.toUInt256() debug "registering the member", - idCommitment = idCommitment, - userMessageLimit = userMessageLimit + idCommitment = idCommitment, userMessageLimit = userMessageLimit var txHash: TxHash g.retryWrapper(txHash, "Failed to register the member"): - await wakuRlnContract - .register(idCommitment, userMessageLimit.stuint(32)) - .send(gasPrice = gasPrice) + await wakuRlnContract.register(idCommitment, userMessageLimit.stuint(32)).send( + gasPrice = gasPrice + ) # wait for the transaction to be mined var tsReceipt: ReceiptObject @@ -199,9 +194,7 @@ method register*( let firstTopic = tsReceipt.logs[0].topics[0] # the hash of the signature of MemberRegistered(uint256,uint32) event is equal to the following hex value if firstTopic != - cast[FixedBytes[32]](keccak256.digest( - "MemberRegistered(uint256,uint32)" - ).data): + cast[FixedBytes[32]](keccak256.digest("MemberRegistered(uint256,uint32)").data): raise newException(ValueError, "unexpected event signature") # the arguments of the raised event i.e., MemberRegistered are encoded inside the data field @@ -219,7 +212,6 @@ method register*( # don't handle member insertion into the tree here, it will be handled by the event listener return - method withdraw*( g: OnchainGroupManager, idCommitment: IDCommitment ): Future[void] {.async: (raises: [Exception]).} = @@ -260,7 +252,6 @@ proc parseEvent( index: index.toMembershipIndex(), ) ) - except CatchableError: return err("failed to parse the data field of the MemberRegistered event") @@ -350,8 +341,8 @@ proc handleEvents( toRemoveIndices = removalIndices, ) g.latestIndex = startIndex + MembershipIndex(rateCommitments.len) - trace "new members added to the Merkle tree", commitments = rateCommitments.mapIt(it.inHex) - + trace "new members added to the Merkle tree", + commitments = rateCommitments.mapIt(it.inHex) except CatchableError: error "failed to insert members into the tree", error = getCurrentExceptionMsg() raise newException(ValueError, "failed to insert members into the tree") @@ -545,10 +536,14 @@ method init*(g: OnchainGroupManager): Future[GroupManagerResult[void]] {.async.} # Set the chain id if g.chainId == 0: - warn "Chain ID not set in config, using RPC Provider's Chain ID", providerChainId = fetchedChainId + warn "Chain ID not set in config, using RPC Provider's Chain ID", + providerChainId = fetchedChainId if g.chainId != 0 and g.chainId != fetchedChainId: - return err("The RPC Provided a Chain ID which is different than the provided Chain ID: provided = " & $g.chainId & ", actual = " & $fetchedChainId) + return err( + "The RPC Provided a Chain ID which is different than the provided Chain ID: provided = " & + $g.chainId & ", actual = " & $fetchedChainId + ) g.chainId = fetchedChainId @@ -562,7 +557,7 @@ method init*(g: OnchainGroupManager): Future[GroupManagerResult[void]] {.async.} let contractAddress = web3.fromHex(web3.Address, g.ethContractAddress) let wakuRlnContract = ethRpc.contractSender(WakuRlnContract, contractAddress) - + g.ethRpc = some(ethRpc) g.wakuRlnContract = some(wakuRlnContract) @@ -614,12 +609,16 @@ method init*(g: OnchainGroupManager): Future[GroupManagerResult[void]] {.async.} g.validRoots = metadata.validRoots.toDeque() var deployedBlockNumber: Uint256 - g.retryWrapper(deployedBlockNumber, "Failed to get the deployed block number. Have you set the correct contract address?"): + g.retryWrapper( + deployedBlockNumber, + "Failed to get the deployed block number. Have you set the correct contract address?", + ): await wakuRlnContract.deployedBlockNumber().call() debug "using rln contract", deployedBlockNumber, rlnContractAddress = contractAddress g.rlnContractDeployedBlockNumber = cast[BlockNumber](deployedBlockNumber) g.latestProcessedBlock = max(g.latestProcessedBlock, g.rlnContractDeployedBlockNumber) - g.rlnRelayMaxMessageLimit = cast[uint64](await wakuRlnContract.MAX_MESSAGE_LIMIT().call()) + g.rlnRelayMaxMessageLimit = + cast[uint64](await wakuRlnContract.MAX_MESSAGE_LIMIT().call()) proc onDisconnect() {.async.} = error "Ethereum client disconnected" diff --git a/waku/waku_rln_relay/group_manager/static/group_manager.nim b/waku/waku_rln_relay/group_manager/static/group_manager.nim index e8590325c9..8eb0b0a71e 100644 --- a/waku/waku_rln_relay/group_manager/static/group_manager.nim +++ b/waku/waku_rln_relay/group_manager/static/group_manager.nim @@ -70,7 +70,6 @@ method register*( await g.registerBatch(@[leaf]) - method registerBatch*( g: StaticGroupManager, rateCommitments: seq[RawRateCommitment] ): Future[void] {.async: (raises: [Exception]).} = diff --git a/waku/waku_rln_relay/protocol_types.nim b/waku/waku_rln_relay/protocol_types.nim index 17db65d90e..97b1c34ea8 100644 --- a/waku/waku_rln_relay/protocol_types.nim +++ b/waku/waku_rln_relay/protocol_types.nim @@ -23,6 +23,7 @@ type RateCommitment* = object idCommitment*: IDCommitment userMessageLimit*: UserMessageLimit + RawRateCommitment* = seq[byte] proc toRateCommitment*(rateCommitmentUint: UInt256): RawRateCommitment =