From 225accd11bb3dba08a878f0a69f8ef556fc96745 Mon Sep 17 00:00:00 2001 From: Tanguy Date: Thu, 18 May 2023 10:24:17 +0200 Subject: [PATCH] Less warnings (#813) Co-authored-by: Diego --- .pinned | 10 +- libp2p.nim | 3 - libp2p/builders.nim | 3 +- libp2p/connmanager.nim | 37 +- libp2p/crypto/chacha20poly1305.nim | 2 +- libp2p/crypto/curve25519.nim | 2 +- libp2p/crypto/ecnist.nim | 31 +- libp2p/crypto/ed25519/ed25519.nim | 447 +++++++++--------- libp2p/crypto/hkdf.nim | 2 +- libp2p/crypto/minasn1.nim | 140 +++--- libp2p/crypto/rsa.nim | 2 +- libp2p/crypto/secp.nim | 3 - libp2p/daemon/daemonapi.nim | 96 ++-- libp2p/dialer.nim | 2 +- libp2p/discovery/rendezvousinterface.nim | 1 - libp2p/multiaddress.nim | 18 +- libp2p/multicodec.nim | 2 - libp2p/muxers/mplex/coder.nim | 2 +- libp2p/muxers/mplex/lpchannel.nim | 2 +- libp2p/muxers/mplex/mplex.nim | 3 +- libp2p/muxers/muxer.nim | 3 +- libp2p/muxers/yamux/yamux.nim | 2 +- libp2p/nameresolving/dnsresolver.nim | 24 +- libp2p/nameresolving/mockresolver.nim | 2 +- libp2p/nameresolving/nameresolver.nim | 2 +- libp2p/peerstore.nim | 2 +- libp2p/protobuf/minprotobuf.nim | 12 +- .../protocols/connectivity/autonat/client.nim | 6 +- .../protocols/connectivity/autonat/core.nim | 2 +- .../protocols/connectivity/autonat/server.nim | 2 +- .../protocols/connectivity/relay/messages.nim | 17 +- libp2p/protocols/connectivity/relay/relay.nim | 3 +- libp2p/protocols/ping.nim | 2 +- .../protocols/pubsub/gossipsub/behavior.nim | 2 +- libp2p/protocols/pubsub/gossipsub/scoring.nim | 4 +- libp2p/protocols/pubsub/rpc/message.nim | 1 - libp2p/protocols/secure/noise.nim | 2 +- libp2p/services/autorelayservice.nim | 2 +- libp2p/stream/bufferstream.nim | 3 - libp2p/stream/chronosstream.nim | 2 +- libp2p/switch.nim | 8 +- libp2p/transports/tcptransport.nim | 3 +- libp2p/upgrademngrs/muxedupgrade.nim | 2 +- libp2p/upgrademngrs/upgrade.nim | 3 +- libp2p/utility.nim | 21 + libp2p/utils/heartbeat.nim | 1 - tests/commontransport.nim | 4 +- tests/helpers.nim | 7 +- tests/testautonat.nim | 11 + tests/testautonatservice.nim | 4 +- tests/testautorelay.nim | 12 +- tests/testbufferstream.nim | 13 +- tests/testcid.nim | 13 +- tests/testconnection.nim | 13 +- tests/testconnmngr.nim | 11 + tests/testcrypto.nim | 6 +- tests/testdcutr.nim | 4 +- tests/testdiscovery.nim | 10 +- tests/testecnist.nim | 5 +- tests/tested25519.nim | 4 +- tests/testfuture.nim | 2 + tests/testheartbeat.nim | 17 +- tests/testidentify.nim | 15 +- tests/testminasn1.nim | 5 +- tests/testminprotobuf.nim | 4 +- tests/testmplex.nim | 15 +- tests/testmultiaddress.nim | 13 +- tests/testmultibase.nim | 13 +- tests/testmultihash.nim | 13 +- tests/testmultistream.nim | 16 +- tests/testnameresolve.nim | 22 +- tests/testnative.nim | 14 +- tests/testnoise.nim | 7 +- tests/testobservedaddrmanager.nim | 11 + tests/testpeerid.nim | 3 +- tests/testpeerinfo.nim | 11 +- tests/testpeerstore.nim | 13 +- tests/testping.nim | 20 +- tests/testrelayv1.nim | 10 +- tests/testrelayv2.nim | 9 + tests/testrendezvous.nim | 13 +- tests/testrouting_record.nim | 11 + tests/testrsa.nim | 5 +- tests/testsecp256k1.nim | 6 +- tests/testsemaphore.nim | 11 + tests/testsigned_envelope.nim | 11 + tests/teststreamseq.nim | 9 + tests/testswitch.nim | 13 +- tests/testtcptransport.nim | 10 +- tests/testtortransport.nim | 9 + tests/testutility.nim | 76 +++ tests/testvarint.nim | 13 +- tests/testwstransport.nim | 16 +- tests/testyamux.nim | 11 + 94 files changed, 918 insertions(+), 587 deletions(-) create mode 100644 tests/testutility.nim diff --git a/.pinned b/.pinned index 7df527b2c..465e3da70 100644 --- a/.pinned +++ b/.pinned @@ -1,16 +1,16 @@ bearssl;https://github.com/status-im/nim-bearssl@#acf9645e328bdcab481cfda1c158e07ecd46bd7b -chronicles;https://github.com/status-im/nim-chronicles@#32ac8679680ea699f7dbc046e8e0131cac97d41a +chronicles;https://github.com/status-im/nim-chronicles@#1e6350870855541b381d77d4659688bc0d2c4227 chronos;https://github.com/status-im/nim-chronos@#ab5a8c2e0f6941fe3debd61dff0293790079d1b0 dnsclient;https://github.com/ba0f3/dnsclient.nim@#fcd7443634b950eaea574e5eaa00a628ae029823 faststreams;https://github.com/status-im/nim-faststreams@#814f8927e1f356f39219f37f069b83066bcc893a httputils;https://github.com/status-im/nim-http-utils@#a85bd52ae0a956983ca6b3267c72961d2ec0245f json_serialization;https://github.com/status-im/nim-json-serialization@#a7d815ed92f200f490c95d3cfd722089cc923ce6 -metrics;https://github.com/status-im/nim-metrics@#21e99a2e9d9f80e68bef65c80ef781613005fccb +metrics;https://github.com/status-im/nim-metrics@#abf3acc7f06cee9ee2c287d2f31413dc3df4c04e nimcrypto;https://github.com/cheatfate/nimcrypto@#4014ef939b51e02053c2e16dd3481d47bc9267dd secp256k1;https://github.com/status-im/nim-secp256k1@#fd173fdff863ce2e211cf64c9a03bc7539fe40b0 serialization;https://github.com/status-im/nim-serialization@#5b7cea55efeb074daa8abd8146a03a34adb4521a -stew;https://github.com/status-im/nim-stew@#407a59883691d362db2fe8eab7f7c3b1f75112ff +stew;https://github.com/status-im/nim-stew@#8caa9771995b266e10b2e7c0de6cbfa698902e68 testutils;https://github.com/status-im/nim-testutils@#dfc4c1b39f9ded9baf6365014de2b4bfb4dafc34 -unittest2;https://github.com/status-im/nim-unittest2@#da8398c45cafd5bd7772da1fc96e3924a18d3823 +unittest2;https://github.com/status-im/nim-unittest2@#883c7a50ad3b82158e64d074c5578fe33ab3c452 websock;https://github.com/status-im/nim-websock@#fea05cde8b123b38d1a0a8524b77efbc84daa848 -zlib;https://github.com/status-im/nim-zlib@#826e2fc013f55b4478802d4f2e39f187c50d520a \ No newline at end of file +zlib;https://github.com/status-im/nim-zlib@#826e2fc013f55b4478802d4f2e39f187c50d520a diff --git a/libp2p.nim b/libp2p.nim index e498972aa..093f13274 100644 --- a/libp2p.nim +++ b/libp2p.nim @@ -48,12 +48,9 @@ else: stream/connection, transports/transport, transports/tcptransport, - transports/wstransport, protocols/secure/noise, - protocols/ping, cid, multihash, - multibase, multicodec, errors, switch, diff --git a/libp2p/builders.nim b/libp2p/builders.nim index 8dd10913b..a0d91261c 100644 --- a/libp2p/builders.nim +++ b/libp2p/builders.nim @@ -298,9 +298,10 @@ proc newStandardSwitch*( peerStoreCapacity = 1000): Switch {.raises: [Defect, LPError], public.} = ## Helper for common switch configurations. - + {.push warning[Deprecated]:off.} if SecureProtocol.Secio in secureManagers: quit("Secio is deprecated!") # use of secio is unsafe + {.pop.} let addrs = when addrs is MultiAddress: @[addrs] else: addrs var b = SwitchBuilder diff --git a/libp2p/connmanager.nim b/libp2p/connmanager.nim index a0f65b11f..48d3ead46 100644 --- a/libp2p/connmanager.nim +++ b/libp2p/connmanager.nim @@ -121,28 +121,13 @@ proc addConnEventHandler*(c: ConnManager, kind: ConnEventKind) = ## Add peer event handler - handlers must not raise exceptions! ## - - try: - if isNil(handler): return - c.connEvents[kind].incl(handler) - except Exception as exc: - # TODO: there is an Exception being raised - # somewhere in the depths of the std. - # Might be related to https://github.com/nim-lang/Nim/issues/17382 - - raiseAssert exc.msg + if isNil(handler): return + c.connEvents[kind].incl(handler) proc removeConnEventHandler*(c: ConnManager, handler: ConnEventHandler, kind: ConnEventKind) = - try: c.connEvents[kind].excl(handler) - except Exception as exc: - # TODO: there is an Exception being raised - # somewhere in the depths of the std. - # Might be related to https://github.com/nim-lang/Nim/issues/17382 - - raiseAssert exc.msg proc triggerConnEvent*(c: ConnManager, peerId: PeerId, @@ -169,26 +154,12 @@ proc addPeerEventHandler*(c: ConnManager, ## if isNil(handler): return - try: - c.peerEvents[kind].incl(handler) - except Exception as exc: - # TODO: there is an Exception being raised - # somewhere in the depths of the std. - # Might be related to https://github.com/nim-lang/Nim/issues/17382 - - raiseAssert exc.msg + c.peerEvents[kind].incl(handler) proc removePeerEventHandler*(c: ConnManager, handler: PeerEventHandler, kind: PeerEventKind) = - try: - c.peerEvents[kind].excl(handler) - except Exception as exc: - # TODO: there is an Exception being raised - # somewhere in the depths of the std. - # Might be related to https://github.com/nim-lang/Nim/issues/17382 - - raiseAssert exc.msg + c.peerEvents[kind].excl(handler) proc triggerPeerEvents*(c: ConnManager, peerId: PeerId, diff --git a/libp2p/crypto/chacha20poly1305.nim b/libp2p/crypto/chacha20poly1305.nim index f97a6d482..d26f31ce9 100644 --- a/libp2p/crypto/chacha20poly1305.nim +++ b/libp2p/crypto/chacha20poly1305.nim @@ -22,7 +22,7 @@ else: import bearssl/blockx from stew/assign2 import assign -from stew/ranges/ptr_arith import baseAddr +from stew/ptrops import baseAddr const ChaChaPolyKeySize = 32 diff --git a/libp2p/crypto/curve25519.nim b/libp2p/crypto/curve25519.nim index e317dd0ce..94255ac37 100644 --- a/libp2p/crypto/curve25519.nim +++ b/libp2p/crypto/curve25519.nim @@ -20,7 +20,7 @@ when (NimMajor, NimMinor) < (1, 4): else: {.push raises: [].} -import bearssl/[ec, rand, hash] +import bearssl/[ec, rand] import stew/results from stew/assign2 import assign export results diff --git a/libp2p/crypto/ecnist.nim b/libp2p/crypto/ecnist.nim index 2213875fa..cbc30e6f9 100644 --- a/libp2p/crypto/ecnist.nim +++ b/libp2p/crypto/ecnist.nim @@ -25,6 +25,9 @@ import nimcrypto/utils as ncrutils import minasn1 export minasn1.Asn1Error import stew/[results, ctops] + +import ../utility + export results const @@ -74,7 +77,7 @@ type EcResult*[T] = Result[T, EcError] const - EcSupportedCurvesCint* = {cint(Secp256r1), cint(Secp384r1), cint(Secp521r1)} + EcSupportedCurvesCint* = @[cint(Secp256r1), cint(Secp384r1), cint(Secp521r1)] proc `-`(x: uint32): uint32 {.inline.} = result = (0xFFFF_FFFF'u32 - x) + 1'u32 @@ -243,7 +246,7 @@ proc random*( var res = new EcPrivateKey if ecKeygen(addr rng.vtable, ecimp, addr res.key, addr res.buffer[0], - cast[cint](kind)) == 0: + safeConvert[cint](kind)) == 0: err(EcKeyGenError) else: ok(res) @@ -630,11 +633,11 @@ proc init*(key: var EcPrivateKey, data: openArray[byte]): Result[void, Asn1Error return err(Asn1Error.Incorrect) if oid == Asn1OidSecp256r1: - curve = cast[cint](Secp256r1) + curve = safeConvert[cint](Secp256r1) elif oid == Asn1OidSecp384r1: - curve = cast[cint](Secp384r1) + curve = safeConvert[cint](Secp384r1) elif oid == Asn1OidSecp521r1: - curve = cast[cint](Secp521r1) + curve = safeConvert[cint](Secp521r1) else: return err(Asn1Error.Incorrect) @@ -684,11 +687,11 @@ proc init*(pubkey: var EcPublicKey, data: openArray[byte]): Result[void, Asn1Err return err(Asn1Error.Incorrect) if oid == Asn1OidSecp256r1: - curve = cast[cint](Secp256r1) + curve = safeConvert[cint](Secp256r1) elif oid == Asn1OidSecp384r1: - curve = cast[cint](Secp384r1) + curve = safeConvert[cint](Secp384r1) elif oid == Asn1OidSecp521r1: - curve = cast[cint](Secp521r1) + curve = safeConvert[cint](Secp521r1) else: return err(Asn1Error.Incorrect) @@ -774,13 +777,13 @@ proc initRaw*(key: var EcPrivateKey, data: openArray[byte]): bool = ## Procedure returns ``true`` on success, ``false`` otherwise. var curve: cint if len(data) == SecKey256Length: - curve = cast[cint](Secp256r1) + curve = safeConvert[cint](Secp256r1) result = true elif len(data) == SecKey384Length: - curve = cast[cint](Secp384r1) + curve = safeConvert[cint](Secp384r1) result = true elif len(data) == SecKey521Length: - curve = cast[cint](Secp521r1) + curve = safeConvert[cint](Secp521r1) result = true if result: result = false @@ -805,13 +808,13 @@ proc initRaw*(pubkey: var EcPublicKey, data: openArray[byte]): bool = if len(data) > 0: if data[0] == 0x04'u8: if len(data) == PubKey256Length: - curve = cast[cint](Secp256r1) + curve = safeConvert[cint](Secp256r1) result = true elif len(data) == PubKey384Length: - curve = cast[cint](Secp384r1) + curve = safeConvert[cint](Secp384r1) result = true elif len(data) == PubKey521Length: - curve = cast[cint](Secp521r1) + curve = safeConvert[cint](Secp521r1) result = true if result: result = false diff --git a/libp2p/crypto/ed25519/ed25519.nim b/libp2p/crypto/ed25519/ed25519.nim index 391a4bb1e..003341705 100644 --- a/libp2p/crypto/ed25519/ed25519.nim +++ b/libp2p/crypto/ed25519/ed25519.nim @@ -22,6 +22,9 @@ import nimcrypto/[hash, sha2] # We use `ncrutils` for constant-time hexadecimal encoding/decoding procedures. import nimcrypto/utils as ncrutils import stew/[results, ctops] + +import ../../utility + export results # This workaround needed because of some bugs in Nim Static[T]. @@ -170,15 +173,15 @@ proc feCopy(h: var Fe, f: Fe) = h[9] = f9 proc load_3(inp: openArray[byte]): uint64 = - result = cast[uint64](inp[0]) - result = result or (cast[uint64](inp[1]) shl 8) - result = result or (cast[uint64](inp[2]) shl 16) + result = safeConvert[uint64](inp[0]) + result = result or (safeConvert[uint64](inp[1]) shl 8) + result = result or (safeConvert[uint64](inp[2]) shl 16) proc load_4(inp: openArray[byte]): uint64 = - result = cast[uint64](inp[0]) - result = result or (cast[uint64](inp[1]) shl 8) - result = result or (cast[uint64](inp[2]) shl 16) - result = result or (cast[uint64](inp[3]) shl 24) + result = safeConvert[uint64](inp[0]) + result = result or (safeConvert[uint64](inp[1]) shl 8) + result = result or (safeConvert[uint64](inp[2]) shl 16) + result = result or (safeConvert[uint64](inp[3]) shl 24) proc feFromBytes(h: var Fe, s: openArray[byte]) = var c0, c1, c2, c3, c4, c5, c6, c7, c8, c9: int64 @@ -299,106 +302,106 @@ proc feMul(h: var Fe, f, g: Fe) = var f5_2 = 2 * f5 var f7_2 = 2 * f7 var f9_2 = 2 * f9 - var f0g0 = cast[int64](f0) * cast[int64](g0) - var f0g1 = cast[int64](f0) * cast[int64](g1) - var f0g2 = cast[int64](f0) * cast[int64](g2) - var f0g3 = cast[int64](f0) * cast[int64](g3) - var f0g4 = cast[int64](f0) * cast[int64](g4) - var f0g5 = cast[int64](f0) * cast[int64](g5) - var f0g6 = cast[int64](f0) * cast[int64](g6) - var f0g7 = cast[int64](f0) * cast[int64](g7) - var f0g8 = cast[int64](f0) * cast[int64](g8) - var f0g9 = cast[int64](f0) * cast[int64](g9) - var f1g0 = cast[int64](f1) * cast[int64](g0) - var f1g1_2 = cast[int64](f1_2) * cast[int64](g1) - var f1g2 = cast[int64](f1) * cast[int64](g2) - var f1g3_2 = cast[int64](f1_2) * cast[int64](g3) - var f1g4 = cast[int64](f1) * cast[int64](g4) - var f1g5_2 = cast[int64](f1_2) * cast[int64](g5) - var f1g6 = cast[int64](f1) * cast[int64](g6) - var f1g7_2 = cast[int64](f1_2) * cast[int64](g7) - var f1g8 = cast[int64](f1) * cast[int64](g8) - var f1g9_38 = cast[int64](f1_2) * cast[int64](g9_19) - var f2g0 = cast[int64](f2) * cast[int64](g0) - var f2g1 = cast[int64](f2) * cast[int64](g1) - var f2g2 = cast[int64](f2) * cast[int64](g2) - var f2g3 = cast[int64](f2) * cast[int64](g3) - var f2g4 = cast[int64](f2) * cast[int64](g4) - var f2g5 = cast[int64](f2) * cast[int64](g5) - var f2g6 = cast[int64](f2) * cast[int64](g6) - var f2g7 = cast[int64](f2) * cast[int64](g7) - var f2g8_19 = cast[int64](f2) * cast[int64](g8_19) - var f2g9_19 = cast[int64](f2) * cast[int64](g9_19) - var f3g0 = cast[int64](f3) * cast[int64](g0) - var f3g1_2 = cast[int64](f3_2) * cast[int64](g1) - var f3g2 = cast[int64](f3) * cast[int64](g2) - var f3g3_2 = cast[int64](f3_2) * cast[int64](g3) - var f3g4 = cast[int64](f3) * cast[int64](g4) - var f3g5_2 = cast[int64](f3_2) * cast[int64](g5) - var f3g6 = cast[int64](f3) * cast[int64](g6) - var f3g7_38 = cast[int64](f3_2) * cast[int64](g7_19) - var f3g8_19 = cast[int64](f3) * cast[int64](g8_19) - var f3g9_38 = cast[int64](f3_2) * cast[int64](g9_19) - var f4g0 = cast[int64](f4) * cast[int64](g0) - var f4g1 = cast[int64](f4) * cast[int64](g1) - var f4g2 = cast[int64](f4) * cast[int64](g2) - var f4g3 = cast[int64](f4) * cast[int64](g3) - var f4g4 = cast[int64](f4) * cast[int64](g4) - var f4g5 = cast[int64](f4) * cast[int64](g5) - var f4g6_19 = cast[int64](f4) * cast[int64](g6_19) - var f4g7_19 = cast[int64](f4) * cast[int64](g7_19) - var f4g8_19 = cast[int64](f4) * cast[int64](g8_19) - var f4g9_19 = cast[int64](f4) * cast[int64](g9_19) - var f5g0 = cast[int64](f5) * cast[int64](g0) - var f5g1_2 = cast[int64](f5_2) * cast[int64](g1) - var f5g2 = cast[int64](f5) * cast[int64](g2) - var f5g3_2 = cast[int64](f5_2) * cast[int64](g3) - var f5g4 = cast[int64](f5) * cast[int64](g4) - var f5g5_38 = cast[int64](f5_2) * cast[int64](g5_19) - var f5g6_19 = cast[int64](f5) * cast[int64](g6_19) - var f5g7_38 = cast[int64](f5_2) * cast[int64](g7_19) - var f5g8_19 = cast[int64](f5) * cast[int64](g8_19) - var f5g9_38 = cast[int64](f5_2) * cast[int64](g9_19) - var f6g0 = cast[int64](f6) * cast[int64](g0) - var f6g1 = cast[int64](f6) * cast[int64](g1) - var f6g2 = cast[int64](f6) * cast[int64](g2) - var f6g3 = cast[int64](f6) * cast[int64](g3) - var f6g4_19 = cast[int64](f6) * cast[int64](g4_19) - var f6g5_19 = cast[int64](f6) * cast[int64](g5_19) - var f6g6_19 = cast[int64](f6) * cast[int64](g6_19) - var f6g7_19 = cast[int64](f6) * cast[int64](g7_19) - var f6g8_19 = cast[int64](f6) * cast[int64](g8_19) - var f6g9_19 = cast[int64](f6) * cast[int64](g9_19) - var f7g0 = cast[int64](f7) * cast[int64](g0) - var f7g1_2 = cast[int64](f7_2) * cast[int64](g1) - var f7g2 = cast[int64](f7) * cast[int64](g2) - var f7g3_38 = cast[int64](f7_2) * cast[int64](g3_19) - var f7g4_19 = cast[int64](f7) * cast[int64](g4_19) - var f7g5_38 = cast[int64](f7_2) * cast[int64](g5_19) - var f7g6_19 = cast[int64](f7) * cast[int64](g6_19) - var f7g7_38 = cast[int64](f7_2) * cast[int64](g7_19) - var f7g8_19 = cast[int64](f7) * cast[int64](g8_19) - var f7g9_38 = cast[int64](f7_2) * cast[int64](g9_19) - var f8g0 = cast[int64](f8) * cast[int64](g0) - var f8g1 = cast[int64](f8) * cast[int64](g1) - var f8g2_19 = cast[int64](f8) * cast[int64](g2_19) - var f8g3_19 = cast[int64](f8) * cast[int64](g3_19) - var f8g4_19 = cast[int64](f8) * cast[int64](g4_19) - var f8g5_19 = cast[int64](f8) * cast[int64](g5_19) - var f8g6_19 = cast[int64](f8) * cast[int64](g6_19) - var f8g7_19 = cast[int64](f8) * cast[int64](g7_19) - var f8g8_19 = cast[int64](f8) * cast[int64](g8_19) - var f8g9_19 = cast[int64](f8) * cast[int64](g9_19) - var f9g0 = cast[int64](f9) * cast[int64](g0) - var f9g1_38 = cast[int64](f9_2) * cast[int64](g1_19) - var f9g2_19 = cast[int64](f9) * cast[int64](g2_19) - var f9g3_38 = cast[int64](f9_2) * cast[int64](g3_19) - var f9g4_19 = cast[int64](f9) * cast[int64](g4_19) - var f9g5_38 = cast[int64](f9_2) * cast[int64](g5_19) - var f9g6_19 = cast[int64](f9) * cast[int64](g6_19) - var f9g7_38 = cast[int64](f9_2) * cast[int64](g7_19) - var f9g8_19 = cast[int64](f9) * cast[int64](g8_19) - var f9g9_38 = cast[int64](f9_2) * cast[int64](g9_19) + var f0g0 = safeConvert[int64](f0) * safeConvert[int64](g0) + var f0g1 = safeConvert[int64](f0) * safeConvert[int64](g1) + var f0g2 = safeConvert[int64](f0) * safeConvert[int64](g2) + var f0g3 = safeConvert[int64](f0) * safeConvert[int64](g3) + var f0g4 = safeConvert[int64](f0) * safeConvert[int64](g4) + var f0g5 = safeConvert[int64](f0) * safeConvert[int64](g5) + var f0g6 = safeConvert[int64](f0) * safeConvert[int64](g6) + var f0g7 = safeConvert[int64](f0) * safeConvert[int64](g7) + var f0g8 = safeConvert[int64](f0) * safeConvert[int64](g8) + var f0g9 = safeConvert[int64](f0) * safeConvert[int64](g9) + var f1g0 = safeConvert[int64](f1) * safeConvert[int64](g0) + var f1g1_2 = safeConvert[int64](f1_2) * safeConvert[int64](g1) + var f1g2 = safeConvert[int64](f1) * safeConvert[int64](g2) + var f1g3_2 = safeConvert[int64](f1_2) * safeConvert[int64](g3) + var f1g4 = safeConvert[int64](f1) * safeConvert[int64](g4) + var f1g5_2 = safeConvert[int64](f1_2) * safeConvert[int64](g5) + var f1g6 = safeConvert[int64](f1) * safeConvert[int64](g6) + var f1g7_2 = safeConvert[int64](f1_2) * safeConvert[int64](g7) + var f1g8 = safeConvert[int64](f1) * safeConvert[int64](g8) + var f1g9_38 = safeConvert[int64](f1_2) * safeConvert[int64](g9_19) + var f2g0 = safeConvert[int64](f2) * safeConvert[int64](g0) + var f2g1 = safeConvert[int64](f2) * safeConvert[int64](g1) + var f2g2 = safeConvert[int64](f2) * safeConvert[int64](g2) + var f2g3 = safeConvert[int64](f2) * safeConvert[int64](g3) + var f2g4 = safeConvert[int64](f2) * safeConvert[int64](g4) + var f2g5 = safeConvert[int64](f2) * safeConvert[int64](g5) + var f2g6 = safeConvert[int64](f2) * safeConvert[int64](g6) + var f2g7 = safeConvert[int64](f2) * safeConvert[int64](g7) + var f2g8_19 = safeConvert[int64](f2) * safeConvert[int64](g8_19) + var f2g9_19 = safeConvert[int64](f2) * safeConvert[int64](g9_19) + var f3g0 = safeConvert[int64](f3) * safeConvert[int64](g0) + var f3g1_2 = safeConvert[int64](f3_2) * safeConvert[int64](g1) + var f3g2 = safeConvert[int64](f3) * safeConvert[int64](g2) + var f3g3_2 = safeConvert[int64](f3_2) * safeConvert[int64](g3) + var f3g4 = safeConvert[int64](f3) * safeConvert[int64](g4) + var f3g5_2 = safeConvert[int64](f3_2) * safeConvert[int64](g5) + var f3g6 = safeConvert[int64](f3) * safeConvert[int64](g6) + var f3g7_38 = safeConvert[int64](f3_2) * safeConvert[int64](g7_19) + var f3g8_19 = safeConvert[int64](f3) * safeConvert[int64](g8_19) + var f3g9_38 = safeConvert[int64](f3_2) * safeConvert[int64](g9_19) + var f4g0 = safeConvert[int64](f4) * safeConvert[int64](g0) + var f4g1 = safeConvert[int64](f4) * safeConvert[int64](g1) + var f4g2 = safeConvert[int64](f4) * safeConvert[int64](g2) + var f4g3 = safeConvert[int64](f4) * safeConvert[int64](g3) + var f4g4 = safeConvert[int64](f4) * safeConvert[int64](g4) + var f4g5 = safeConvert[int64](f4) * safeConvert[int64](g5) + var f4g6_19 = safeConvert[int64](f4) * safeConvert[int64](g6_19) + var f4g7_19 = safeConvert[int64](f4) * safeConvert[int64](g7_19) + var f4g8_19 = safeConvert[int64](f4) * safeConvert[int64](g8_19) + var f4g9_19 = safeConvert[int64](f4) * safeConvert[int64](g9_19) + var f5g0 = safeConvert[int64](f5) * safeConvert[int64](g0) + var f5g1_2 = safeConvert[int64](f5_2) * safeConvert[int64](g1) + var f5g2 = safeConvert[int64](f5) * safeConvert[int64](g2) + var f5g3_2 = safeConvert[int64](f5_2) * safeConvert[int64](g3) + var f5g4 = safeConvert[int64](f5) * safeConvert[int64](g4) + var f5g5_38 = safeConvert[int64](f5_2) * safeConvert[int64](g5_19) + var f5g6_19 = safeConvert[int64](f5) * safeConvert[int64](g6_19) + var f5g7_38 = safeConvert[int64](f5_2) * safeConvert[int64](g7_19) + var f5g8_19 = safeConvert[int64](f5) * safeConvert[int64](g8_19) + var f5g9_38 = safeConvert[int64](f5_2) * safeConvert[int64](g9_19) + var f6g0 = safeConvert[int64](f6) * safeConvert[int64](g0) + var f6g1 = safeConvert[int64](f6) * safeConvert[int64](g1) + var f6g2 = safeConvert[int64](f6) * safeConvert[int64](g2) + var f6g3 = safeConvert[int64](f6) * safeConvert[int64](g3) + var f6g4_19 = safeConvert[int64](f6) * safeConvert[int64](g4_19) + var f6g5_19 = safeConvert[int64](f6) * safeConvert[int64](g5_19) + var f6g6_19 = safeConvert[int64](f6) * safeConvert[int64](g6_19) + var f6g7_19 = safeConvert[int64](f6) * safeConvert[int64](g7_19) + var f6g8_19 = safeConvert[int64](f6) * safeConvert[int64](g8_19) + var f6g9_19 = safeConvert[int64](f6) * safeConvert[int64](g9_19) + var f7g0 = safeConvert[int64](f7) * safeConvert[int64](g0) + var f7g1_2 = safeConvert[int64](f7_2) * safeConvert[int64](g1) + var f7g2 = safeConvert[int64](f7) * safeConvert[int64](g2) + var f7g3_38 = safeConvert[int64](f7_2) * safeConvert[int64](g3_19) + var f7g4_19 = safeConvert[int64](f7) * safeConvert[int64](g4_19) + var f7g5_38 = safeConvert[int64](f7_2) * safeConvert[int64](g5_19) + var f7g6_19 = safeConvert[int64](f7) * safeConvert[int64](g6_19) + var f7g7_38 = safeConvert[int64](f7_2) * safeConvert[int64](g7_19) + var f7g8_19 = safeConvert[int64](f7) * safeConvert[int64](g8_19) + var f7g9_38 = safeConvert[int64](f7_2) * safeConvert[int64](g9_19) + var f8g0 = safeConvert[int64](f8) * safeConvert[int64](g0) + var f8g1 = safeConvert[int64](f8) * safeConvert[int64](g1) + var f8g2_19 = safeConvert[int64](f8) * safeConvert[int64](g2_19) + var f8g3_19 = safeConvert[int64](f8) * safeConvert[int64](g3_19) + var f8g4_19 = safeConvert[int64](f8) * safeConvert[int64](g4_19) + var f8g5_19 = safeConvert[int64](f8) * safeConvert[int64](g5_19) + var f8g6_19 = safeConvert[int64](f8) * safeConvert[int64](g6_19) + var f8g7_19 = safeConvert[int64](f8) * safeConvert[int64](g7_19) + var f8g8_19 = safeConvert[int64](f8) * safeConvert[int64](g8_19) + var f8g9_19 = safeConvert[int64](f8) * safeConvert[int64](g9_19) + var f9g0 = safeConvert[int64](f9) * safeConvert[int64](g0) + var f9g1_38 = safeConvert[int64](f9_2) * safeConvert[int64](g1_19) + var f9g2_19 = safeConvert[int64](f9) * safeConvert[int64](g2_19) + var f9g3_38 = safeConvert[int64](f9_2) * safeConvert[int64](g3_19) + var f9g4_19 = safeConvert[int64](f9) * safeConvert[int64](g4_19) + var f9g5_38 = safeConvert[int64](f9_2) * safeConvert[int64](g5_19) + var f9g6_19 = safeConvert[int64](f9) * safeConvert[int64](g6_19) + var f9g7_38 = safeConvert[int64](f9_2) * safeConvert[int64](g7_19) + var f9g8_19 = safeConvert[int64](f9) * safeConvert[int64](g8_19) + var f9g9_38 = safeConvert[int64](f9_2) * safeConvert[int64](g9_19) var c0, c1, c2, c3, c4, c5, c6, c7, c8, c9: int64 h0: int64 = f0g0 + f1g9_38 + f2g8_19 + f3g7_38 + f4g6_19 + f5g5_38 + @@ -493,7 +496,7 @@ proc verify32(x: openArray[byte], y: openArray[byte]): int32 = proc feIsNegative(f: Fe): int32 = var s: array[32, byte] feToBytes(s, f) - result = cast[int32](s[0] and 1'u8) + result = safeConvert[int32](s[0] and 1'u8) proc feIsNonZero(f: Fe): int32 = var s: array[32, byte] @@ -516,61 +519,61 @@ proc feSq(h: var Fe, f: Fe) = var f7_38: int32 = 38 * f7 var f8_19: int32 = 19 * f8 var f9_38: int32 = 38 * f9 - var f0f0: int64 = f0 * cast[int64](f0) - var f0f1_2: int64 = f0_2 * cast[int64](f1) - var f0f2_2: int64 = f0_2 * cast[int64](f2) - var f0f3_2: int64 = f0_2 * cast[int64](f3) - var f0f4_2: int64 = f0_2 * cast[int64](f4) - var f0f5_2: int64 = f0_2 * cast[int64](f5) - var f0f6_2: int64 = f0_2 * cast[int64](f6) - var f0f7_2: int64 = f0_2 * cast[int64](f7) - var f0f8_2: int64 = f0_2 * cast[int64](f8) - var f0f9_2: int64 = f0_2 * cast[int64](f9) - var f1f1_2: int64 = f1_2 * cast[int64](f1) - var f1f2_2: int64 = f1_2 * cast[int64](f2) - var f1f3_4: int64 = f1_2 * cast[int64](f3_2) - var f1f4_2: int64 = f1_2 * cast[int64](f4) - var f1f5_4: int64 = f1_2 * cast[int64](f5_2) - var f1f6_2: int64 = f1_2 * cast[int64](f6) - var f1f7_4: int64 = f1_2 * cast[int64](f7_2) - var f1f8_2: int64 = f1_2 * cast[int64](f8) - var f1f9_76: int64 = f1_2 * cast[int64](f9_38) - var f2f2: int64 = f2 * cast[int64](f2) - var f2f3_2: int64 = f2_2 * cast[int64](f3) - var f2f4_2: int64 = f2_2 * cast[int64](f4) - var f2f5_2: int64 = f2_2 * cast[int64](f5) - var f2f6_2: int64 = f2_2 * cast[int64](f6) - var f2f7_2: int64 = f2_2 * cast[int64](f7) - var f2f8_38: int64 = f2_2 * cast[int64](f8_19) - var f2f9_38: int64 = f2 * cast[int64](f9_38) - var f3f3_2: int64 = f3_2 * cast[int64](f3) - var f3f4_2: int64 = f3_2 * cast[int64](f4) - var f3f5_4: int64 = f3_2 * cast[int64](f5_2) - var f3f6_2: int64 = f3_2 * cast[int64](f6) - var f3f7_76: int64 = f3_2 * cast[int64](f7_38) - var f3f8_38: int64 = f3_2 * cast[int64](f8_19) - var f3f9_76: int64 = f3_2 * cast[int64](f9_38) - var f4f4: int64 = f4 * cast[int64](f4) - var f4f5_2: int64 = f4_2 * cast[int64](f5) - var f4f6_38: int64 = f4_2 * cast[int64](f6_19) - var f4f7_38: int64 = f4 * cast[int64](f7_38) - var f4f8_38: int64 = f4_2 * cast[int64](f8_19) - var f4f9_38: int64 = f4 * cast[int64](f9_38) - var f5f5_38: int64 = f5 * cast[int64](f5_38) - var f5f6_38: int64 = f5_2 * cast[int64](f6_19) - var f5f7_76: int64 = f5_2 * cast[int64](f7_38) - var f5f8_38: int64 = f5_2 * cast[int64](f8_19) - var f5f9_76: int64 = f5_2 * cast[int64](f9_38) - var f6f6_19: int64 = f6 * cast[int64](f6_19) - var f6f7_38: int64 = f6 * cast[int64](f7_38) - var f6f8_38: int64 = f6_2 * cast[int64](f8_19) - var f6f9_38: int64 = f6 * cast[int64](f9_38) - var f7f7_38: int64 = f7 * cast[int64](f7_38) - var f7f8_38: int64 = f7_2 * cast[int64](f8_19) - var f7f9_76: int64 = f7_2 * cast[int64](f9_38) - var f8f8_19: int64 = f8 * cast[int64](f8_19) - var f8f9_38: int64 = f8 * cast[int64](f9_38) - var f9f9_38: int64 = f9 * cast[int64](f9_38) + var f0f0: int64 = f0 * safeConvert[int64](f0) + var f0f1_2: int64 = f0_2 * safeConvert[int64](f1) + var f0f2_2: int64 = f0_2 * safeConvert[int64](f2) + var f0f3_2: int64 = f0_2 * safeConvert[int64](f3) + var f0f4_2: int64 = f0_2 * safeConvert[int64](f4) + var f0f5_2: int64 = f0_2 * safeConvert[int64](f5) + var f0f6_2: int64 = f0_2 * safeConvert[int64](f6) + var f0f7_2: int64 = f0_2 * safeConvert[int64](f7) + var f0f8_2: int64 = f0_2 * safeConvert[int64](f8) + var f0f9_2: int64 = f0_2 * safeConvert[int64](f9) + var f1f1_2: int64 = f1_2 * safeConvert[int64](f1) + var f1f2_2: int64 = f1_2 * safeConvert[int64](f2) + var f1f3_4: int64 = f1_2 * safeConvert[int64](f3_2) + var f1f4_2: int64 = f1_2 * safeConvert[int64](f4) + var f1f5_4: int64 = f1_2 * safeConvert[int64](f5_2) + var f1f6_2: int64 = f1_2 * safeConvert[int64](f6) + var f1f7_4: int64 = f1_2 * safeConvert[int64](f7_2) + var f1f8_2: int64 = f1_2 * safeConvert[int64](f8) + var f1f9_76: int64 = f1_2 * safeConvert[int64](f9_38) + var f2f2: int64 = f2 * safeConvert[int64](f2) + var f2f3_2: int64 = f2_2 * safeConvert[int64](f3) + var f2f4_2: int64 = f2_2 * safeConvert[int64](f4) + var f2f5_2: int64 = f2_2 * safeConvert[int64](f5) + var f2f6_2: int64 = f2_2 * safeConvert[int64](f6) + var f2f7_2: int64 = f2_2 * safeConvert[int64](f7) + var f2f8_38: int64 = f2_2 * safeConvert[int64](f8_19) + var f2f9_38: int64 = f2 * safeConvert[int64](f9_38) + var f3f3_2: int64 = f3_2 * safeConvert[int64](f3) + var f3f4_2: int64 = f3_2 * safeConvert[int64](f4) + var f3f5_4: int64 = f3_2 * safeConvert[int64](f5_2) + var f3f6_2: int64 = f3_2 * safeConvert[int64](f6) + var f3f7_76: int64 = f3_2 * safeConvert[int64](f7_38) + var f3f8_38: int64 = f3_2 * safeConvert[int64](f8_19) + var f3f9_76: int64 = f3_2 * safeConvert[int64](f9_38) + var f4f4: int64 = f4 * safeConvert[int64](f4) + var f4f5_2: int64 = f4_2 * safeConvert[int64](f5) + var f4f6_38: int64 = f4_2 * safeConvert[int64](f6_19) + var f4f7_38: int64 = f4 * safeConvert[int64](f7_38) + var f4f8_38: int64 = f4_2 * safeConvert[int64](f8_19) + var f4f9_38: int64 = f4 * safeConvert[int64](f9_38) + var f5f5_38: int64 = f5 * safeConvert[int64](f5_38) + var f5f6_38: int64 = f5_2 * safeConvert[int64](f6_19) + var f5f7_76: int64 = f5_2 * safeConvert[int64](f7_38) + var f5f8_38: int64 = f5_2 * safeConvert[int64](f8_19) + var f5f9_76: int64 = f5_2 * safeConvert[int64](f9_38) + var f6f6_19: int64 = f6 * safeConvert[int64](f6_19) + var f6f7_38: int64 = f6 * safeConvert[int64](f7_38) + var f6f8_38: int64 = f6_2 * safeConvert[int64](f8_19) + var f6f9_38: int64 = f6 * safeConvert[int64](f9_38) + var f7f7_38: int64 = f7 * safeConvert[int64](f7_38) + var f7f8_38: int64 = f7_2 * safeConvert[int64](f8_19) + var f7f9_76: int64 = f7_2 * safeConvert[int64](f9_38) + var f8f8_19: int64 = f8 * safeConvert[int64](f8_19) + var f8f9_38: int64 = f8 * safeConvert[int64](f9_38) + var f9f9_38: int64 = f9 * safeConvert[int64](f9_38) var h0: int64 = f0f0 + f1f9_76 + f2f8_38 + f3f7_76 + f4f6_38 + f5f5_38 var h1: int64 = f0f1_2 + f2f9_38 + f3f8_38 + f4f7_38 + f5f6_38 var h2: int64 = f0f2_2 + f1f1_2 + f3f9_76 + f4f8_38 + f5f7_76 + f6f6_19 @@ -623,61 +626,61 @@ proc feSq2(h: var Fe, f: Fe) = var f7_38 = 38 * f7 var f8_19 = 19 * f8 var f9_38 = 38 * f9 - var f0f0 = cast[int64](f0) * cast[int64](f0) - var f0f1_2 = cast[int64](f0_2) * cast[int64](f1) - var f0f2_2 = cast[int64](f0_2) * cast[int64](f2) - var f0f3_2 = cast[int64](f0_2) * cast[int64](f3) - var f0f4_2 = cast[int64](f0_2) * cast[int64](f4) - var f0f5_2 = cast[int64](f0_2) * cast[int64](f5) - var f0f6_2 = cast[int64](f0_2) * cast[int64](f6) - var f0f7_2 = cast[int64](f0_2) * cast[int64](f7) - var f0f8_2 = cast[int64](f0_2) * cast[int64](f8) - var f0f9_2 = cast[int64](f0_2) * cast[int64](f9) - var f1f1_2 = cast[int64](f1_2) * cast[int64](f1) - var f1f2_2 = cast[int64](f1_2) * cast[int64](f2) - var f1f3_4 = cast[int64](f1_2) * cast[int64](f3_2) - var f1f4_2 = cast[int64](f1_2) * cast[int64](f4) - var f1f5_4 = cast[int64](f1_2) * cast[int64](f5_2) - var f1f6_2 = cast[int64](f1_2) * cast[int64](f6) - var f1f7_4 = cast[int64](f1_2) * cast[int64](f7_2) - var f1f8_2 = cast[int64](f1_2) * cast[int64](f8) - var f1f9_76 = cast[int64](f1_2) * cast[int64](f9_38) - var f2f2 = cast[int64](f2) * cast[int64](f2) - var f2f3_2 = cast[int64](f2_2) * cast[int64](f3) - var f2f4_2 = cast[int64](f2_2) * cast[int64](f4) - var f2f5_2 = cast[int64](f2_2) * cast[int64](f5) - var f2f6_2 = cast[int64](f2_2) * cast[int64](f6) - var f2f7_2 = cast[int64](f2_2) * cast[int64](f7) - var f2f8_38 = cast[int64](f2_2) * cast[int64](f8_19) - var f2f9_38 = cast[int64](f2) * cast[int64](f9_38) - var f3f3_2 = cast[int64](f3_2) * cast[int64](f3) - var f3f4_2 = cast[int64](f3_2) * cast[int64](f4) - var f3f5_4 = cast[int64](f3_2) * cast[int64](f5_2) - var f3f6_2 = cast[int64](f3_2) * cast[int64](f6) - var f3f7_76 = cast[int64](f3_2) * cast[int64](f7_38) - var f3f8_38 = cast[int64](f3_2) * cast[int64](f8_19) - var f3f9_76 = cast[int64](f3_2) * cast[int64](f9_38) - var f4f4 = cast[int64](f4) * cast[int64](f4) - var f4f5_2 = cast[int64](f4_2) * cast[int64](f5) - var f4f6_38 = cast[int64](f4_2) * cast[int64](f6_19) - var f4f7_38 = cast[int64](f4) * cast[int64](f7_38) - var f4f8_38 = cast[int64](f4_2) * cast[int64](f8_19) - var f4f9_38 = cast[int64](f4) * cast[int64](f9_38) - var f5f5_38 = cast[int64](f5) * cast[int64](f5_38) - var f5f6_38 = cast[int64](f5_2) * cast[int64](f6_19) - var f5f7_76 = cast[int64](f5_2) * cast[int64](f7_38) - var f5f8_38 = cast[int64](f5_2) * cast[int64](f8_19) - var f5f9_76 = cast[int64](f5_2) * cast[int64](f9_38) - var f6f6_19 = cast[int64](f6) * cast[int64](f6_19) - var f6f7_38 = cast[int64](f6) * cast[int64](f7_38) - var f6f8_38 = cast[int64](f6_2) * cast[int64](f8_19) - var f6f9_38 = cast[int64](f6) * cast[int64](f9_38) - var f7f7_38 = cast[int64](f7) * cast[int64](f7_38) - var f7f8_38 = cast[int64](f7_2) * cast[int64](f8_19) - var f7f9_76 = cast[int64](f7_2) * cast[int64](f9_38) - var f8f8_19 = cast[int64](f8) * cast[int64](f8_19) - var f8f9_38 = cast[int64](f8) * cast[int64](f9_38) - var f9f9_38 = cast[int64](f9) * cast[int64](f9_38) + var f0f0 = safeConvert[int64](f0) * safeConvert[int64](f0) + var f0f1_2 = safeConvert[int64](f0_2) * safeConvert[int64](f1) + var f0f2_2 = safeConvert[int64](f0_2) * safeConvert[int64](f2) + var f0f3_2 = safeConvert[int64](f0_2) * safeConvert[int64](f3) + var f0f4_2 = safeConvert[int64](f0_2) * safeConvert[int64](f4) + var f0f5_2 = safeConvert[int64](f0_2) * safeConvert[int64](f5) + var f0f6_2 = safeConvert[int64](f0_2) * safeConvert[int64](f6) + var f0f7_2 = safeConvert[int64](f0_2) * safeConvert[int64](f7) + var f0f8_2 = safeConvert[int64](f0_2) * safeConvert[int64](f8) + var f0f9_2 = safeConvert[int64](f0_2) * safeConvert[int64](f9) + var f1f1_2 = safeConvert[int64](f1_2) * safeConvert[int64](f1) + var f1f2_2 = safeConvert[int64](f1_2) * safeConvert[int64](f2) + var f1f3_4 = safeConvert[int64](f1_2) * safeConvert[int64](f3_2) + var f1f4_2 = safeConvert[int64](f1_2) * safeConvert[int64](f4) + var f1f5_4 = safeConvert[int64](f1_2) * safeConvert[int64](f5_2) + var f1f6_2 = safeConvert[int64](f1_2) * safeConvert[int64](f6) + var f1f7_4 = safeConvert[int64](f1_2) * safeConvert[int64](f7_2) + var f1f8_2 = safeConvert[int64](f1_2) * safeConvert[int64](f8) + var f1f9_76 = safeConvert[int64](f1_2) * safeConvert[int64](f9_38) + var f2f2 = safeConvert[int64](f2) * safeConvert[int64](f2) + var f2f3_2 = safeConvert[int64](f2_2) * safeConvert[int64](f3) + var f2f4_2 = safeConvert[int64](f2_2) * safeConvert[int64](f4) + var f2f5_2 = safeConvert[int64](f2_2) * safeConvert[int64](f5) + var f2f6_2 = safeConvert[int64](f2_2) * safeConvert[int64](f6) + var f2f7_2 = safeConvert[int64](f2_2) * safeConvert[int64](f7) + var f2f8_38 = safeConvert[int64](f2_2) * safeConvert[int64](f8_19) + var f2f9_38 = safeConvert[int64](f2) * safeConvert[int64](f9_38) + var f3f3_2 = safeConvert[int64](f3_2) * safeConvert[int64](f3) + var f3f4_2 = safeConvert[int64](f3_2) * safeConvert[int64](f4) + var f3f5_4 = safeConvert[int64](f3_2) * safeConvert[int64](f5_2) + var f3f6_2 = safeConvert[int64](f3_2) * safeConvert[int64](f6) + var f3f7_76 = safeConvert[int64](f3_2) * safeConvert[int64](f7_38) + var f3f8_38 = safeConvert[int64](f3_2) * safeConvert[int64](f8_19) + var f3f9_76 = safeConvert[int64](f3_2) * safeConvert[int64](f9_38) + var f4f4 = safeConvert[int64](f4) * safeConvert[int64](f4) + var f4f5_2 = safeConvert[int64](f4_2) * safeConvert[int64](f5) + var f4f6_38 = safeConvert[int64](f4_2) * safeConvert[int64](f6_19) + var f4f7_38 = safeConvert[int64](f4) * safeConvert[int64](f7_38) + var f4f8_38 = safeConvert[int64](f4_2) * safeConvert[int64](f8_19) + var f4f9_38 = safeConvert[int64](f4) * safeConvert[int64](f9_38) + var f5f5_38 = safeConvert[int64](f5) * safeConvert[int64](f5_38) + var f5f6_38 = safeConvert[int64](f5_2) * safeConvert[int64](f6_19) + var f5f7_76 = safeConvert[int64](f5_2) * safeConvert[int64](f7_38) + var f5f8_38 = safeConvert[int64](f5_2) * safeConvert[int64](f8_19) + var f5f9_76 = safeConvert[int64](f5_2) * safeConvert[int64](f9_38) + var f6f6_19 = safeConvert[int64](f6) * safeConvert[int64](f6_19) + var f6f7_38 = safeConvert[int64](f6) * safeConvert[int64](f7_38) + var f6f8_38 = safeConvert[int64](f6_2) * safeConvert[int64](f8_19) + var f6f9_38 = safeConvert[int64](f6) * safeConvert[int64](f9_38) + var f7f7_38 = safeConvert[int64](f7) * safeConvert[int64](f7_38) + var f7f8_38 = safeConvert[int64](f7_2) * safeConvert[int64](f8_19) + var f7f9_76 = safeConvert[int64](f7_2) * safeConvert[int64](f9_38) + var f8f8_19 = safeConvert[int64](f8) * safeConvert[int64](f8_19) + var f8f9_38 = safeConvert[int64](f8) * safeConvert[int64](f9_38) + var f9f9_38 = safeConvert[int64](f9) * safeConvert[int64](f9_38) var c0, c1, c2, c3, c4, c5, c6, c7, c8, c9: int64 h0: int64 = f0f0 + f1f9_76 + f2f8_38 + f3f7_76 + f4f6_38 + f5f5_38 @@ -834,7 +837,7 @@ proc geFromBytesNegateVartime(h: var GeP3, s: openArray[byte]): int32 = return -1; feMul(h.x, h.x, SqrTm1) - if feIsNegative(h.x) == cast[int32](s[31] shr 7): + if feIsNegative(h.x) == safeConvert[int32](s[31] shr 7): feNeg(h.x, h.x) feMul(h.t, h.x, h.y) @@ -956,14 +959,14 @@ proc equal(b, c: int8): byte = var ub = cast[byte](b) var uc = cast[byte](c) var x = ub xor uc - var y = cast[uint32](x) + var y = safeConvert[uint32](x) y = y - 1 y = y shr 31 result = cast[byte](y) proc negative(b: int8): byte = - var x = cast[uint64](b) - x = x shr 63 + var x = cast[uint8](b) + x = x shr 7 result = cast[byte](x) proc cmov(t: var GePrecomp, u: GePrecomp, b: byte) = diff --git a/libp2p/crypto/hkdf.nim b/libp2p/crypto/hkdf.nim index 7855cef5d..f29d34b66 100644 --- a/libp2p/crypto/hkdf.nim +++ b/libp2p/crypto/hkdf.nim @@ -15,7 +15,7 @@ else: {.push raises: [].} import nimcrypto -import bearssl/[kdf, rand, hash] +import bearssl/[kdf, hash] type HkdfResult*[len: static int] = array[len, byte] diff --git a/libp2p/crypto/minasn1.nim b/libp2p/crypto/minasn1.nim index 5535577a1..8185a0f73 100644 --- a/libp2p/crypto/minasn1.nim +++ b/libp2p/crypto/minasn1.nim @@ -18,6 +18,7 @@ import stew/[endians2, results, ctops] export results # We use `ncrutils` for constant-time hexadecimal encoding/decoding procedures. import nimcrypto/utils as ncrutils +import ../utility type Asn1Error* {.pure.} = enum @@ -119,7 +120,7 @@ template toOpenArray*(af: Asn1Field): untyped = template isEmpty*(ab: Asn1Buffer): bool = ab.offset >= len(ab.buffer) -template isEnough*(ab: Asn1Buffer, length: int): bool = +template isEnough*(ab: Asn1Buffer, length: int64): bool = len(ab.buffer) >= ab.offset + length proc len*[T: Asn1Buffer|Asn1Composite](abc: T): int {.inline.} = @@ -344,32 +345,6 @@ proc asn1EncodeTag[T: SomeUnsignedInt](dest: var openArray[byte], dest[k - 1] = dest[k - 1] and 0x7F'u8 res -proc asn1EncodeOid*(dest: var openArray[byte], value: openArray[int]): int = - ## Encode array of integers ``value`` as ASN.1 DER `OBJECT IDENTIFIER` and - ## return number of bytes (octets) used. - ## - ## If length of ``dest`` is less then number of required bytes to encode - ## ``value``, then result of encoding will not be stored in ``dest`` - ## but number of bytes (octets) required will be returned. - var buffer: array[16, byte] - var res = 1 - var oidlen = 1 - for i in 2..= res: - let last = dest.high - var offset = 1 - dest[0] = Asn1Tag.Oid.code() - offset += asn1EncodeLength(dest.toOpenArray(offset, last), uint64(oidlen)) - dest[offset] = cast[byte](value[0] * 40 + value[1]) - offset += 1 - for i in 2.. 8'u64: + let octets = safeConvert[int](b and 0x7F'u8) + if octets > 8: return err(Asn1Error.Overflow) - if ab.isEnough(int(octets)): - var length: uint64 = 0 - for i in 0.. uint64(int64.high): + return err(Asn1Error.Overflow) + let length = int(lengthU) + ab.offset = ab.offset + octets + 1 return ok(length) else: return err(Asn1Error.Incomplete) @@ -474,8 +452,8 @@ proc getTag(ab: var Asn1Buffer, tag: var int): Asn1Result[Asn1Class] = if not ab.isEmpty(): let b = ab.buffer[ab.offset] - c = int((b and 0xC0'u8) shr 6) - tag = int(b and 0x3F) + c = safeConvert[int]((b and 0xC0'u8) shr 6) + tag = safeConvert[int](b and 0x3F) ab.offset += 1 if c >= 0 and c < 4: ok(cast[Asn1Class](c)) @@ -489,7 +467,7 @@ proc read*(ab: var Asn1Buffer): Asn1Result[Asn1Field] = var field: Asn1Field tag, ttag, offset: int - length, tlength: uint64 + length, tlength: int aclass: Asn1Class inclass: bool @@ -519,7 +497,7 @@ proc read*(ab: var Asn1Buffer): Asn1Result[Asn1Field] = if length != 1: return err(Asn1Error.Incorrect) - if not ab.isEnough(int(length)): + if not ab.isEnough(length): return err(Asn1Error.Incomplete) let b = ab.buffer[ab.offset] @@ -527,7 +505,7 @@ proc read*(ab: var Asn1Buffer): Asn1Result[Asn1Field] = return err(Asn1Error.Incorrect) field = Asn1Field(kind: Asn1Tag.Boolean, klass: aclass, - index: ttag, offset: int(ab.offset), + index: ttag, offset: ab.offset, length: 1, buffer: ab.buffer) field.vbool = (b == 0xFF'u8) ab.offset += 1 @@ -538,12 +516,12 @@ proc read*(ab: var Asn1Buffer): Asn1Result[Asn1Field] = if length == 0: return err(Asn1Error.Incorrect) - if not ab.isEnough(int(length)): + if not ab.isEnough(length): return err(Asn1Error.Incomplete) # Count number of leading zeroes var zc = 0 - while (zc < int(length)) and (ab.buffer[ab.offset + zc] == 0x00'u8): + while (zc < length) and (ab.buffer[ab.offset + zc] == 0x00'u8): inc(zc) if zc > 1: @@ -552,45 +530,45 @@ proc read*(ab: var Asn1Buffer): Asn1Result[Asn1Field] = if zc == 0: # Negative or Positive integer field = Asn1Field(kind: Asn1Tag.Integer, klass: aclass, - index: ttag, offset: int(ab.offset), - length: int(length), buffer: ab.buffer) + index: ttag, offset: ab.offset, + length: length, buffer: ab.buffer) if (ab.buffer[ab.offset] and 0x80'u8) == 0x80'u8: # Negative integer if length <= 8: # We need this transformation because our field.vint is uint64. for i in 0 ..< 8: - if i < 8 - int(length): + if i < 8 - length: field.vint = (field.vint shl 8) or 0xFF'u64 else: - let offset = ab.offset + i - (8 - int(length)) - field.vint = (field.vint shl 8) or uint64(ab.buffer[offset]) + let offset = ab.offset + i - (8 - length) + field.vint = (field.vint shl 8) or safeConvert[uint64](ab.buffer[offset]) else: # Positive integer if length <= 8: - for i in 0 ..< int(length): + for i in 0 ..< length: field.vint = (field.vint shl 8) or - uint64(ab.buffer[ab.offset + i]) - ab.offset += int(length) + safeConvert[uint64](ab.buffer[ab.offset + i]) + ab.offset += length return ok(field) else: if length == 1: # Zero value integer field = Asn1Field(kind: Asn1Tag.Integer, klass: aclass, - index: ttag, offset: int(ab.offset), - length: int(length), vint: 0'u64, + index: ttag, offset: ab.offset, + length: length, vint: 0'u64, buffer: ab.buffer) - ab.offset += int(length) + ab.offset += length return ok(field) else: # Positive integer with leading zero field = Asn1Field(kind: Asn1Tag.Integer, klass: aclass, - index: ttag, offset: int(ab.offset) + 1, - length: int(length) - 1, buffer: ab.buffer) + index: ttag, offset: ab.offset + 1, + length: length - 1, buffer: ab.buffer) if length <= 9: - for i in 1 ..< int(length): + for i in 1 ..< length: field.vint = (field.vint shl 8) or - uint64(ab.buffer[ab.offset + i]) - ab.offset += int(length) + safeConvert[uint64](ab.buffer[ab.offset + i]) + ab.offset += length return ok(field) of Asn1Tag.BitString.code(): @@ -606,13 +584,13 @@ proc read*(ab: var Asn1Buffer): Asn1Result[Asn1Field] = else: # Zero-length BIT STRING. field = Asn1Field(kind: Asn1Tag.BitString, klass: aclass, - index: ttag, offset: int(ab.offset + 1), + index: ttag, offset: ab.offset + 1, length: 0, ubits: 0, buffer: ab.buffer) - ab.offset += int(length) + ab.offset += length return ok(field) else: - if not ab.isEnough(int(length)): + if not ab.isEnough(length): return err(Asn1Error.Incomplete) let unused = ab.buffer[ab.offset] @@ -620,27 +598,27 @@ proc read*(ab: var Asn1Buffer): Asn1Result[Asn1Field] = # Number of unused bits should not be bigger then `7`. return err(Asn1Error.Incorrect) - let mask = (1'u8 shl int(unused)) - 1'u8 - if (ab.buffer[ab.offset + int(length) - 1] and mask) != 0x00'u8: + let mask = (1'u8 shl safeConvert[int](unused)) - 1'u8 + if (ab.buffer[ab.offset + length - 1] and mask) != 0x00'u8: ## All unused bits should be set to `0`. return err(Asn1Error.Incorrect) field = Asn1Field(kind: Asn1Tag.BitString, klass: aclass, - index: ttag, offset: int(ab.offset + 1), - length: int(length - 1), ubits: int(unused), + index: ttag, offset: ab.offset + 1, + length: length - 1, ubits: safeConvert[int](unused), buffer: ab.buffer) - ab.offset += int(length) + ab.offset += length return ok(field) of Asn1Tag.OctetString.code(): # OCTET STRING - if not ab.isEnough(int(length)): + if not ab.isEnough(length): return err(Asn1Error.Incomplete) field = Asn1Field(kind: Asn1Tag.OctetString, klass: aclass, - index: ttag, offset: int(ab.offset), - length: int(length), buffer: ab.buffer) - ab.offset += int(length) + index: ttag, offset: ab.offset, + length: length, buffer: ab.buffer) + ab.offset += length return ok(field) of Asn1Tag.Null.code(): @@ -649,30 +627,30 @@ proc read*(ab: var Asn1Buffer): Asn1Result[Asn1Field] = return err(Asn1Error.Incorrect) field = Asn1Field(kind: Asn1Tag.Null, klass: aclass, index: ttag, - offset: int(ab.offset), length: 0, buffer: ab.buffer) - ab.offset += int(length) + offset: ab.offset, length: 0, buffer: ab.buffer) + ab.offset += length return ok(field) of Asn1Tag.Oid.code(): # OID - if not ab.isEnough(int(length)): + if not ab.isEnough(length): return err(Asn1Error.Incomplete) field = Asn1Field(kind: Asn1Tag.Oid, klass: aclass, - index: ttag, offset: int(ab.offset), - length: int(length), buffer: ab.buffer) - ab.offset += int(length) + index: ttag, offset: ab.offset, + length: length, buffer: ab.buffer) + ab.offset += length return ok(field) of Asn1Tag.Sequence.code(): # SEQUENCE - if not ab.isEnough(int(length)): + if not ab.isEnough(length): return err(Asn1Error.Incomplete) field = Asn1Field(kind: Asn1Tag.Sequence, klass: aclass, - index: ttag, offset: int(ab.offset), - length: int(length), buffer: ab.buffer) - ab.offset += int(length) + index: ttag, offset: ab.offset, + length: length, buffer: ab.buffer) + ab.offset += length return ok(field) else: diff --git a/libp2p/crypto/rsa.nim b/libp2p/crypto/rsa.nim index 74e078533..5c5890a3c 100644 --- a/libp2p/crypto/rsa.nim +++ b/libp2p/crypto/rsa.nim @@ -686,7 +686,7 @@ proc `==`*(a, b: RsaPrivateKey): bool = false else: if a.seck.nBitlen == b.seck.nBitlen: - if cast[int](a.seck.nBitlen) > 0: + if a.seck.nBitlen > 0'u: let r1 = CT.isEqual(getArray(a.buffer, a.seck.p, a.seck.plen), getArray(b.buffer, b.seck.p, b.seck.plen)) let r2 = CT.isEqual(getArray(a.buffer, a.seck.q, a.seck.qlen), diff --git a/libp2p/crypto/secp.nim b/libp2p/crypto/secp.nim index 6fbfc3750..e7be08667 100644 --- a/libp2p/crypto/secp.nim +++ b/libp2p/crypto/secp.nim @@ -35,9 +35,6 @@ type SkSignature* = distinct secp256k1.SkSignature SkKeyPair* = distinct secp256k1.SkKeyPair -template pubkey*(v: SkKeyPair): SkPublicKey = SkPublicKey(secp256k1.SkKeyPair(v).pubkey) -template seckey*(v: SkKeyPair): SkPrivateKey = SkPrivateKey(secp256k1.SkKeyPair(v).seckey) - proc random*(t: typedesc[SkPrivateKey], rng: var HmacDrbgContext): SkPrivateKey = #TODO is there a better way? var rngPtr = addr rng diff --git a/libp2p/daemon/daemonapi.nim b/libp2p/daemon/daemonapi.nim index 79522a2ff..bced3eb05 100644 --- a/libp2p/daemon/daemonapi.nim +++ b/libp2p/daemon/daemonapi.nim @@ -17,7 +17,7 @@ import std/[os, osproc, strutils, tables, strtabs, sequtils] import pkg/[chronos, chronicles] import ../varint, ../multiaddress, ../multicodec, ../cid, ../peerid import ../wire, ../multihash, ../protobuf/minprotobuf, ../errors -import ../crypto/crypto +import ../crypto/crypto, ../utility export peerid, multiaddress, multicodec, multihash, cid, crypto, wire, errors @@ -170,7 +170,7 @@ proc requestIdentity(): ProtoBuffer = ## https://github.com/libp2p/go-libp2p-daemon/blob/master/conn.go ## Processing function `doIdentify(req *pb.Request)`. result = initProtoBuffer({WithVarintLength}) - result.write(1, cast[uint](RequestType.IDENTIFY)) + result.write(1, safeConvert[uint](RequestType.IDENTIFY)) result.finish() proc requestConnect(peerid: PeerId, @@ -185,7 +185,7 @@ proc requestConnect(peerid: PeerId, msg.write(2, item.data.buffer) if timeout > 0: msg.write(3, hint64(timeout)) - result.write(1, cast[uint](RequestType.CONNECT)) + result.write(1, safeConvert[uint](RequestType.CONNECT)) result.write(2, msg) result.finish() @@ -195,7 +195,7 @@ proc requestDisconnect(peerid: PeerId): ProtoBuffer = result = initProtoBuffer({WithVarintLength}) var msg = initProtoBuffer() msg.write(1, peerid) - result.write(1, cast[uint](RequestType.DISCONNECT)) + result.write(1, safeConvert[uint](RequestType.DISCONNECT)) result.write(7, msg) result.finish() @@ -211,7 +211,7 @@ proc requestStreamOpen(peerid: PeerId, msg.write(2, item) if timeout > 0: msg.write(3, hint64(timeout)) - result.write(1, cast[uint](RequestType.STREAM_OPEN)) + result.write(1, safeConvert[uint](RequestType.STREAM_OPEN)) result.write(3, msg) result.finish() @@ -224,7 +224,7 @@ proc requestStreamHandler(address: MultiAddress, msg.write(1, address.data.buffer) for item in protocols: msg.write(2, item) - result.write(1, cast[uint](RequestType.STREAM_HANDLER)) + result.write(1, safeConvert[uint](RequestType.STREAM_HANDLER)) result.write(4, msg) result.finish() @@ -232,13 +232,13 @@ proc requestListPeers(): ProtoBuffer = ## https://github.com/libp2p/go-libp2p-daemon/blob/master/conn.go ## Processing function `doListPeers(req *pb.Request)` result = initProtoBuffer({WithVarintLength}) - result.write(1, cast[uint](RequestType.LIST_PEERS)) + result.write(1, safeConvert[uint](RequestType.LIST_PEERS)) result.finish() proc requestDHTFindPeer(peer: PeerId, timeout = 0): ProtoBuffer = ## https://github.com/libp2p/go-libp2p-daemon/blob/master/dht.go ## Processing function `doDHTFindPeer(req *pb.DHTRequest)`. - let msgid = cast[uint](DHTRequestType.FIND_PEER) + let msgid = safeConvert[uint](DHTRequestType.FIND_PEER) result = initProtoBuffer({WithVarintLength}) var msg = initProtoBuffer() msg.write(1, msgid) @@ -246,7 +246,7 @@ proc requestDHTFindPeer(peer: PeerId, timeout = 0): ProtoBuffer = if timeout > 0: msg.write(7, hint64(timeout)) msg.finish() - result.write(1, cast[uint](RequestType.DHT)) + result.write(1, safeConvert[uint](RequestType.DHT)) result.write(5, msg) result.finish() @@ -254,7 +254,7 @@ proc requestDHTFindPeersConnectedToPeer(peer: PeerId, timeout = 0): ProtoBuffer = ## https://github.com/libp2p/go-libp2p-daemon/blob/master/dht.go ## Processing function `doDHTFindPeersConnectedToPeer(req *pb.DHTRequest)`. - let msgid = cast[uint](DHTRequestType.FIND_PEERS_CONNECTED_TO_PEER) + let msgid = safeConvert[uint](DHTRequestType.FIND_PEERS_CONNECTED_TO_PEER) result = initProtoBuffer({WithVarintLength}) var msg = initProtoBuffer() msg.write(1, msgid) @@ -262,7 +262,7 @@ proc requestDHTFindPeersConnectedToPeer(peer: PeerId, if timeout > 0: msg.write(7, hint64(timeout)) msg.finish() - result.write(1, cast[uint](RequestType.DHT)) + result.write(1, safeConvert[uint](RequestType.DHT)) result.write(5, msg) result.finish() @@ -270,7 +270,7 @@ proc requestDHTFindProviders(cid: Cid, count: uint32, timeout = 0): ProtoBuffer = ## https://github.com/libp2p/go-libp2p-daemon/blob/master/dht.go ## Processing function `doDHTFindProviders(req *pb.DHTRequest)`. - let msgid = cast[uint](DHTRequestType.FIND_PROVIDERS) + let msgid = safeConvert[uint](DHTRequestType.FIND_PROVIDERS) result = initProtoBuffer({WithVarintLength}) var msg = initProtoBuffer() msg.write(1, msgid) @@ -279,14 +279,14 @@ proc requestDHTFindProviders(cid: Cid, if timeout > 0: msg.write(7, hint64(timeout)) msg.finish() - result.write(1, cast[uint](RequestType.DHT)) + result.write(1, safeConvert[uint](RequestType.DHT)) result.write(5, msg) result.finish() proc requestDHTGetClosestPeers(key: string, timeout = 0): ProtoBuffer = ## https://github.com/libp2p/go-libp2p-daemon/blob/master/dht.go ## Processing function `doDHTGetClosestPeers(req *pb.DHTRequest)`. - let msgid = cast[uint](DHTRequestType.GET_CLOSEST_PEERS) + let msgid = safeConvert[uint](DHTRequestType.GET_CLOSEST_PEERS) result = initProtoBuffer({WithVarintLength}) var msg = initProtoBuffer() msg.write(1, msgid) @@ -294,14 +294,14 @@ proc requestDHTGetClosestPeers(key: string, timeout = 0): ProtoBuffer = if timeout > 0: msg.write(7, hint64(timeout)) msg.finish() - result.write(1, cast[uint](RequestType.DHT)) + result.write(1, safeConvert[uint](RequestType.DHT)) result.write(5, msg) result.finish() proc requestDHTGetPublicKey(peer: PeerId, timeout = 0): ProtoBuffer = ## https://github.com/libp2p/go-libp2p-daemon/blob/master/dht.go ## Processing function `doDHTGetPublicKey(req *pb.DHTRequest)`. - let msgid = cast[uint](DHTRequestType.GET_PUBLIC_KEY) + let msgid = safeConvert[uint](DHTRequestType.GET_PUBLIC_KEY) result = initProtoBuffer({WithVarintLength}) var msg = initProtoBuffer() msg.write(1, msgid) @@ -309,14 +309,14 @@ proc requestDHTGetPublicKey(peer: PeerId, timeout = 0): ProtoBuffer = if timeout > 0: msg.write(7, hint64(timeout)) msg.finish() - result.write(1, cast[uint](RequestType.DHT)) + result.write(1, safeConvert[uint](RequestType.DHT)) result.write(5, msg) result.finish() proc requestDHTGetValue(key: string, timeout = 0): ProtoBuffer = ## https://github.com/libp2p/go-libp2p-daemon/blob/master/dht.go ## Processing function `doDHTGetValue(req *pb.DHTRequest)`. - let msgid = cast[uint](DHTRequestType.GET_VALUE) + let msgid = safeConvert[uint](DHTRequestType.GET_VALUE) result = initProtoBuffer({WithVarintLength}) var msg = initProtoBuffer() msg.write(1, msgid) @@ -324,14 +324,14 @@ proc requestDHTGetValue(key: string, timeout = 0): ProtoBuffer = if timeout > 0: msg.write(7, hint64(timeout)) msg.finish() - result.write(1, cast[uint](RequestType.DHT)) + result.write(1, safeConvert[uint](RequestType.DHT)) result.write(5, msg) result.finish() proc requestDHTSearchValue(key: string, timeout = 0): ProtoBuffer = ## https://github.com/libp2p/go-libp2p-daemon/blob/master/dht.go ## Processing function `doDHTSearchValue(req *pb.DHTRequest)`. - let msgid = cast[uint](DHTRequestType.SEARCH_VALUE) + let msgid = safeConvert[uint](DHTRequestType.SEARCH_VALUE) result = initProtoBuffer({WithVarintLength}) var msg = initProtoBuffer() msg.write(1, msgid) @@ -339,7 +339,7 @@ proc requestDHTSearchValue(key: string, timeout = 0): ProtoBuffer = if timeout > 0: msg.write(7, hint64(timeout)) msg.finish() - result.write(1, cast[uint](RequestType.DHT)) + result.write(1, safeConvert[uint](RequestType.DHT)) result.write(5, msg) result.finish() @@ -347,7 +347,7 @@ proc requestDHTPutValue(key: string, value: openArray[byte], timeout = 0): ProtoBuffer = ## https://github.com/libp2p/go-libp2p-daemon/blob/master/dht.go ## Processing function `doDHTPutValue(req *pb.DHTRequest)`. - let msgid = cast[uint](DHTRequestType.PUT_VALUE) + let msgid = safeConvert[uint](DHTRequestType.PUT_VALUE) result = initProtoBuffer({WithVarintLength}) var msg = initProtoBuffer() msg.write(1, msgid) @@ -356,14 +356,14 @@ proc requestDHTPutValue(key: string, value: openArray[byte], if timeout > 0: msg.write(7, hint64(timeout)) msg.finish() - result.write(1, cast[uint](RequestType.DHT)) + result.write(1, uint(RequestType.DHT)) result.write(5, msg) result.finish() proc requestDHTProvide(cid: Cid, timeout = 0): ProtoBuffer = ## https://github.com/libp2p/go-libp2p-daemon/blob/master/dht.go ## Processing function `doDHTProvide(req *pb.DHTRequest)`. - let msgid = cast[uint](DHTRequestType.PROVIDE) + let msgid = safeConvert[uint](DHTRequestType.PROVIDE) result = initProtoBuffer({WithVarintLength}) var msg = initProtoBuffer() msg.write(1, msgid) @@ -371,13 +371,13 @@ proc requestDHTProvide(cid: Cid, timeout = 0): ProtoBuffer = if timeout > 0: msg.write(7, hint64(timeout)) msg.finish() - result.write(1, cast[uint](RequestType.DHT)) + result.write(1, safeConvert[uint](RequestType.DHT)) result.write(5, msg) result.finish() proc requestCMTagPeer(peer: PeerId, tag: string, weight: int): ProtoBuffer = ## https://github.com/libp2p/go-libp2p-daemon/blob/master/connmgr.go#L18 - let msgid = cast[uint](ConnManagerRequestType.TAG_PEER) + let msgid = safeConvert[uint](ConnManagerRequestType.TAG_PEER) result = initProtoBuffer({WithVarintLength}) var msg = initProtoBuffer() msg.write(1, msgid) @@ -385,83 +385,83 @@ proc requestCMTagPeer(peer: PeerId, tag: string, weight: int): ProtoBuffer = msg.write(3, tag) msg.write(4, hint64(weight)) msg.finish() - result.write(1, cast[uint](RequestType.CONNMANAGER)) + result.write(1, safeConvert[uint](RequestType.CONNMANAGER)) result.write(6, msg) result.finish() proc requestCMUntagPeer(peer: PeerId, tag: string): ProtoBuffer = ## https://github.com/libp2p/go-libp2p-daemon/blob/master/connmgr.go#L33 - let msgid = cast[uint](ConnManagerRequestType.UNTAG_PEER) + let msgid = safeConvert[uint](ConnManagerRequestType.UNTAG_PEER) result = initProtoBuffer({WithVarintLength}) var msg = initProtoBuffer() msg.write(1, msgid) msg.write(2, peer) msg.write(3, tag) msg.finish() - result.write(1, cast[uint](RequestType.CONNMANAGER)) + result.write(1, safeConvert[uint](RequestType.CONNMANAGER)) result.write(6, msg) result.finish() proc requestCMTrim(): ProtoBuffer = ## https://github.com/libp2p/go-libp2p-daemon/blob/master/connmgr.go#L47 - let msgid = cast[uint](ConnManagerRequestType.TRIM) + let msgid = safeConvert[uint](ConnManagerRequestType.TRIM) result = initProtoBuffer({WithVarintLength}) var msg = initProtoBuffer() msg.write(1, msgid) msg.finish() - result.write(1, cast[uint](RequestType.CONNMANAGER)) + result.write(1, safeConvert[uint](RequestType.CONNMANAGER)) result.write(6, msg) result.finish() proc requestPSGetTopics(): ProtoBuffer = ## https://github.com/libp2p/go-libp2p-daemon/blob/master/pubsub.go ## Processing function `doPubsubGetTopics(req *pb.PSRequest)`. - let msgid = cast[uint](PSRequestType.GET_TOPICS) + let msgid = safeConvert[uint](PSRequestType.GET_TOPICS) result = initProtoBuffer({WithVarintLength}) var msg = initProtoBuffer() msg.write(1, msgid) msg.finish() - result.write(1, cast[uint](RequestType.PUBSUB)) + result.write(1, safeConvert[uint](RequestType.PUBSUB)) result.write(8, msg) result.finish() proc requestPSListPeers(topic: string): ProtoBuffer = ## https://github.com/libp2p/go-libp2p-daemon/blob/master/pubsub.go ## Processing function `doPubsubListPeers(req *pb.PSRequest)`. - let msgid = cast[uint](PSRequestType.LIST_PEERS) + let msgid = safeConvert[uint](PSRequestType.LIST_PEERS) result = initProtoBuffer({WithVarintLength}) var msg = initProtoBuffer() msg.write(1, msgid) msg.write(2, topic) msg.finish() - result.write(1, cast[uint](RequestType.PUBSUB)) + result.write(1, safeConvert[uint](RequestType.PUBSUB)) result.write(8, msg) result.finish() proc requestPSPublish(topic: string, data: openArray[byte]): ProtoBuffer = ## https://github.com/libp2p/go-libp2p-daemon/blob/master/pubsub.go ## Processing function `doPubsubPublish(req *pb.PSRequest)`. - let msgid = cast[uint](PSRequestType.PUBLISH) + let msgid = safeConvert[uint](PSRequestType.PUBLISH) result = initProtoBuffer({WithVarintLength}) var msg = initProtoBuffer() msg.write(1, msgid) msg.write(2, topic) msg.write(3, data) msg.finish() - result.write(1, cast[uint](RequestType.PUBSUB)) + result.write(1, safeConvert[uint](RequestType.PUBSUB)) result.write(8, msg) result.finish() proc requestPSSubscribe(topic: string): ProtoBuffer = ## https://github.com/libp2p/go-libp2p-daemon/blob/master/pubsub.go ## Processing function `doPubsubSubscribe(req *pb.PSRequest)`. - let msgid = cast[uint](PSRequestType.SUBSCRIBE) + let msgid = safeConvert[uint](PSRequestType.SUBSCRIBE) result = initProtoBuffer({WithVarintLength}) var msg = initProtoBuffer() msg.write(1, msgid) msg.write(2, topic) msg.finish() - result.write(1, cast[uint](RequestType.PUBSUB)) + result.write(1, safeConvert[uint](RequestType.PUBSUB)) result.write(8, msg) result.finish() @@ -515,7 +515,7 @@ proc socketExists(address: MultiAddress): Future[bool] {.async.} = var transp = await connect(address) await transp.closeWait() result = true - except: + except CatchableError, Defect: result = false when defined(windows): @@ -525,7 +525,7 @@ when defined(windows): result = cast[int](getCurrentProcessId()) else: proc getProcessId(): int = - result = cast[int](posix.getpid()) + result = int(posix.getpid()) proc getSocket(pattern: string, count: ptr int): Future[MultiAddress] {.async.} = @@ -759,12 +759,8 @@ proc newDaemonApi*(flags: set[P2PDaemonFlags] = {}, # Starting daemon process # echo "Starting ", cmd, " ", args.join(" ") api.process = - try: + exceptionToAssert: startProcess(cmd, "", args, env, {poParentStreams}) - except CatchableError as exc: - raise exc - except Exception as exc: - raiseAssert exc.msg # Waiting until daemon will not be bound to control socket. while true: if not api.process.running(): @@ -872,7 +868,7 @@ proc connect*(api: DaemonAPI, peer: PeerId, timeout)) pb.withMessage() do: discard - except: + except CatchableError, Defect: await api.closeConnection(transp) proc disconnect*(api: DaemonAPI, peer: PeerId) {.async.} = @@ -1033,7 +1029,7 @@ proc enterDhtMessage(pb: ProtoBuffer, rt: DHTResponseType): ProtoBuffer var dtype: uint if pbDhtResponse.getRequiredField(1, dtype).isErr(): raise newException(DaemonLocalError, "Missing required DHT field `type`!") - if dtype != cast[uint](rt): + if dtype != safeConvert[uint](rt): raise newException(DaemonLocalError, "Wrong DHT answer type! ") var value: seq[byte] @@ -1057,9 +1053,9 @@ proc getDhtMessageType(pb: ProtoBuffer): DHTResponseType var dtype: uint if pb.getRequiredField(1, dtype).isErr(): raise newException(DaemonLocalError, "Missing required DHT field `type`!") - if dtype == cast[uint](DHTResponseType.VALUE): + if dtype == safeConvert[uint](DHTResponseType.VALUE): result = DHTResponseType.VALUE - elif dtype == cast[uint](DHTResponseType.END): + elif dtype == safeConvert[uint](DHTResponseType.END): result = DHTResponseType.END else: raise newException(DaemonLocalError, "Wrong DHT answer type!") diff --git a/libp2p/dialer.nim b/libp2p/dialer.nim index a96af4d0e..2ff052c7c 100644 --- a/libp2p/dialer.nim +++ b/libp2p/dialer.nim @@ -7,7 +7,7 @@ # This file may not be copied, modified, or distributed except according to # those terms. -import std/[sugar, tables, sequtils] +import std/tables import stew/results import pkg/[chronos, diff --git a/libp2p/discovery/rendezvousinterface.nim b/libp2p/discovery/rendezvousinterface.nim index 429eb41d4..c903244a2 100644 --- a/libp2p/discovery/rendezvousinterface.nim +++ b/libp2p/discovery/rendezvousinterface.nim @@ -12,7 +12,6 @@ when (NimMajor, NimMinor) < (1, 4): else: {.push raises: [].} -import sequtils import chronos import ./discoverymngr, ../protocols/rendezvous, diff --git a/libp2p/multiaddress.nim b/libp2p/multiaddress.nim index c0d76bb64..7ed76682d 100644 --- a/libp2p/multiaddress.nim +++ b/libp2p/multiaddress.nim @@ -83,7 +83,7 @@ proc ip4StB(s: string, vb: var VBuffer): bool = if a.family == IpAddressFamily.IPv4: vb.writeArray(a.address_v4) result = true - except: + except CatchableError: discard proc ip4BtS(vb: var VBuffer, s: var string): bool = @@ -106,7 +106,7 @@ proc ip6StB(s: string, vb: var VBuffer): bool = if a.family == IpAddressFamily.IPv6: vb.writeArray(a.address_v6) result = true - except: + except CatchableError: discard proc ip6BtS(vb: var VBuffer, s: var string): bool = @@ -150,14 +150,14 @@ proc portStB(s: string, vb: var VBuffer): bool = port[1] = cast[byte](nport and 0xFF) vb.writeArray(port) result = true - except: + except CatchableError: discard proc portBtS(vb: var VBuffer, s: var string): bool = ## Port number bufferToString() implementation. var port: array[2, byte] if vb.readArray(port) == 2: - var nport = (cast[uint16](port[0]) shl 8) or cast[uint16](port[1]) + var nport = (safeConvert[uint16](port[0]) shl 8) or safeConvert[uint16](port[1]) s = $nport result = true @@ -175,7 +175,7 @@ proc p2pStB(s: string, vb: var VBuffer): bool = if MultiHash.decode(data, mh).isOk: vb.writeSeq(data) result = true - except: + except CatchableError: discard proc p2pBtS(vb: var VBuffer, s: var string): bool = @@ -210,14 +210,14 @@ proc onionStB(s: string, vb: var VBuffer): bool = address[11] = cast[byte](nport and 0xFF) vb.writeArray(address) result = true - except: + except CatchableError: discard proc onionBtS(vb: var VBuffer, s: var string): bool = ## ONION address bufferToString() implementation. var buf: array[12, byte] if vb.readArray(buf) == 12: - var nport = (cast[uint16](buf[10]) shl 8) or cast[uint16](buf[11]) + var nport = (safeConvert[uint16](buf[10]) shl 8) or safeConvert[uint16](buf[11]) s = Base32Lower.encode(buf.toOpenArray(0, 9)) s.add(":") s.add($nport) @@ -244,14 +244,14 @@ proc onion3StB(s: string, vb: var VBuffer): bool = address[36] = cast[byte](nport and 0xFF) vb.writeArray(address) result = true - except: + except CatchableError: discard proc onion3BtS(vb: var VBuffer, s: var string): bool = ## ONION address bufferToString() implementation. var buf: array[37, byte] if vb.readArray(buf) == 37: - var nport = (cast[uint16](buf[35]) shl 8) or cast[uint16](buf[36]) + var nport = (safeConvert[uint16](buf[35]) shl 8) or safeConvert[uint16](buf[36]) s = Base32Lower.encode(buf.toOpenArray(0, 34)) s.add(":") s.add($nport) diff --git a/libp2p/multicodec.nim b/libp2p/multicodec.nim index 2795221c2..a40676360 100644 --- a/libp2p/multicodec.nim +++ b/libp2p/multicodec.nim @@ -19,8 +19,6 @@ import varint, vbuffer import stew/results export results -{.deadCodeElim: on.} - ## List of officially supported codecs can BE found here ## https://github.com/multiformats/multicodec/blob/master/table.csv const MultiCodecList = [ diff --git a/libp2p/muxers/mplex/coder.nim b/libp2p/muxers/mplex/coder.nim index d2b76761a..8a05b20ef 100644 --- a/libp2p/muxers/mplex/coder.nim +++ b/libp2p/muxers/mplex/coder.nim @@ -12,7 +12,7 @@ when (NimMajor, NimMinor) < (1, 4): else: {.push raises: [].} -import pkg/[chronos, nimcrypto/utils, chronicles, stew/byteutils] +import pkg/[chronos, chronicles, stew/byteutils] import ../../stream/connection, ../../utility, ../../varint, diff --git a/libp2p/muxers/mplex/lpchannel.nim b/libp2p/muxers/mplex/lpchannel.nim index 9c4e0a97e..bbd10d664 100644 --- a/libp2p/muxers/mplex/lpchannel.nim +++ b/libp2p/muxers/mplex/lpchannel.nim @@ -13,7 +13,7 @@ else: {.push raises: [].} import std/[oids, strformat] -import pkg/[chronos, chronicles, metrics, nimcrypto/utils] +import pkg/[chronos, chronicles, metrics] import ./coder, ../muxer, ../../stream/[bufferstream, connection, streamseq], diff --git a/libp2p/muxers/mplex/mplex.nim b/libp2p/muxers/mplex/mplex.nim index 13d988b47..fed8e678e 100644 --- a/libp2p/muxers/mplex/mplex.nim +++ b/libp2p/muxers/mplex/mplex.nim @@ -203,7 +203,8 @@ method handle*(m: Mplex) {.async, gcsafe.} = proc new*(M: type Mplex, conn: Connection, - inTimeout, outTimeout: Duration = DefaultChanTimeout, + inTimeout: Duration = DefaultChanTimeout, + outTimeout: Duration = DefaultChanTimeout, maxChannCount: int = MaxChannelCount): Mplex = M(connection: conn, inChannTimeout: inTimeout, diff --git a/libp2p/muxers/muxer.nim b/libp2p/muxers/muxer.nim index ac00759b1..5b93c5745 100644 --- a/libp2p/muxers/muxer.nim +++ b/libp2p/muxers/muxer.nim @@ -13,8 +13,7 @@ else: {.push raises: [].} import chronos, chronicles -import ../protocols/protocol, - ../stream/connection, +import ../stream/connection, ../errors logScope: diff --git a/libp2p/muxers/yamux/yamux.nim b/libp2p/muxers/yamux/yamux.nim index 9caa365c7..9e15844c4 100644 --- a/libp2p/muxers/yamux/yamux.nim +++ b/libp2p/muxers/yamux/yamux.nim @@ -84,7 +84,7 @@ proc `$`(header: YamuxHeader): string = proc encode(header: YamuxHeader): array[12, byte] = result[0] = header.version result[1] = uint8(header.msgType) - result[2..3] = toBytesBE(cast[uint16](header.flags)) + result[2..3] = toBytesBE(uint16(cast[uint8](header.flags))) # workaround https://github.com/nim-lang/Nim/issues/21789 result[4..7] = toBytesBE(header.streamId) result[8..11] = toBytesBE(header.length) diff --git a/libp2p/nameresolving/dnsresolver.nim b/libp2p/nameresolving/dnsresolver.nim index 4589a269b..92313b158 100644 --- a/libp2p/nameresolving/dnsresolver.nim +++ b/libp2p/nameresolving/dnsresolver.nim @@ -15,7 +15,8 @@ else: import std/[streams, strutils, sets, sequtils], chronos, chronicles, stew/byteutils, - dnsclientpkg/[protocol, types] + dnsclientpkg/[protocol, types], + ../utility import nameresolver @@ -80,9 +81,7 @@ proc getDnsResponse( # parseResponse can has a raises: [Exception, ..] because of # https://github.com/nim-lang/Nim/commit/035134de429b5d99c5607c5fae912762bebb6008 # it can't actually raise though - return parseResponse(string.fromBytes(rawResponse)) - except CatchableError as exc: raise exc - except Exception as exc: raiseAssert exc.msg + return exceptionToAssert: parseResponse(string.fromBytes(rawResponse)) finally: await sock.closeWait() @@ -118,9 +117,7 @@ method resolveIp*( # https://github.com/nim-lang/Nim/commit/035134de429b5d99c5607c5fae912762bebb6008 # it can't actually raise though resolvedAddresses.incl( - try: answer.toString() - except CatchableError as exc: raise exc - except Exception as exc: raiseAssert exc.msg + exceptionToAssert(answer.toString()) ) except CancelledError as e: raise e @@ -151,9 +148,13 @@ method resolveTxt*( for _ in 0 ..< self.nameServers.len: let server = self.nameServers[0] try: + # toString can has a raises: [Exception, ..] because of + # https://github.com/nim-lang/Nim/commit/035134de429b5d99c5607c5fae912762bebb6008 + # it can't actually raise though let response = await getDnsResponse(server, address, TXT) - trace "Got TXT response", server = $server, answer=response.answers.mapIt(it.toString()) - return response.answers.mapIt(it.toString()) + return exceptionToAssert: + trace "Got TXT response", server = $server, answer=response.answers.mapIt(it.toString()) + response.answers.mapIt(it.toString()) except CancelledError as e: raise e except CatchableError as e: @@ -161,11 +162,6 @@ method resolveTxt*( self.nameServers.add(self.nameServers[0]) self.nameServers.delete(0) continue - except Exception as e: - # toString can has a raises: [Exception, ..] because of - # https://github.com/nim-lang/Nim/commit/035134de429b5d99c5607c5fae912762bebb6008 - # it can't actually raise though - raiseAssert e.msg debug "Failed to resolve TXT, returning empty set" return @[] diff --git a/libp2p/nameresolving/mockresolver.nim b/libp2p/nameresolving/mockresolver.nim index 6aa9a9adf..266933b35 100644 --- a/libp2p/nameresolving/mockresolver.nim +++ b/libp2p/nameresolving/mockresolver.nim @@ -13,7 +13,7 @@ else: {.push raises: [].} import - std/[streams, strutils, tables], + std/tables, chronos, chronicles import nameresolver diff --git a/libp2p/nameresolving/nameresolver.nim b/libp2p/nameresolving/nameresolver.nim index 343bc00bf..81ecef6d0 100644 --- a/libp2p/nameresolving/nameresolver.nim +++ b/libp2p/nameresolving/nameresolver.nim @@ -16,7 +16,7 @@ import std/[sugar, sets, sequtils, strutils] import chronos, chronicles, - stew/[endians2, byteutils] + stew/endians2 import ".."/[multiaddress, multicodec] logScope: diff --git a/libp2p/peerstore.nim b/libp2p/peerstore.nim index 26a8349c5..67f71d1d2 100644 --- a/libp2p/peerstore.nim +++ b/libp2p/peerstore.nim @@ -28,7 +28,7 @@ else: import std/[tables, sets, options, macros], - chronos, chronicles, + chronos, ./crypto/crypto, ./protocols/identify, ./protocols/protocol, diff --git a/libp2p/protobuf/minprotobuf.nim b/libp2p/protobuf/minprotobuf.nim index 77821194d..5d28cd5ff 100644 --- a/libp2p/protobuf/minprotobuf.nim +++ b/libp2p/protobuf/minprotobuf.nim @@ -72,12 +72,12 @@ type hint | hint32 | hint64 | float32 | float64 const - SupportedWireTypes* = { - int(ProtoFieldKind.Varint), - int(ProtoFieldKind.Fixed64), - int(ProtoFieldKind.Length), - int(ProtoFieldKind.Fixed32) - } + SupportedWireTypes* = @[ + uint64(ProtoFieldKind.Varint), + uint64(ProtoFieldKind.Fixed64), + uint64(ProtoFieldKind.Length), + uint64(ProtoFieldKind.Fixed32) + ] template checkFieldNumber*(i: int) = doAssert((i > 0 and i < (1 shl 29)) and not(i >= 19000 and i <= 19999), diff --git a/libp2p/protocols/connectivity/autonat/client.nim b/libp2p/protocols/connectivity/autonat/client.nim index 0322a1edf..e6ec928ff 100644 --- a/libp2p/protocols/connectivity/autonat/client.nim +++ b/libp2p/protocols/connectivity/autonat/client.nim @@ -12,8 +12,8 @@ when (NimMajor, NimMinor) < (1, 4): else: {.push raises: [].} -import std/[options, sets, sequtils] -import stew/[results, objects] +import std/options +import stew/results import chronos, chronicles import ../../../switch, ../../../multiaddress, @@ -36,7 +36,7 @@ proc sendDial(conn: Connection, pid: PeerId, addrs: seq[MultiAddress]) {.async.} method dialMe*(self: AutonatClient, switch: Switch, pid: PeerId, addrs: seq[MultiAddress] = newSeq[MultiAddress]()): Future[MultiAddress] {.base, async.} = - proc getResponseOrRaise(autonatMsg: Option[AutonatMsg]): AutonatDialResponse {.raises: [UnpackError, AutonatError].} = + proc getResponseOrRaise(autonatMsg: Option[AutonatMsg]): AutonatDialResponse {.raises: [Defect, AutonatError].} = if autonatMsg.isNone() or autonatMsg.get().msgType != DialResponse or autonatMsg.get().response.isNone() or diff --git a/libp2p/protocols/connectivity/autonat/core.nim b/libp2p/protocols/connectivity/autonat/core.nim index 703ff9120..c3d1a22e7 100644 --- a/libp2p/protocols/connectivity/autonat/core.nim +++ b/libp2p/protocols/connectivity/autonat/core.nim @@ -12,7 +12,7 @@ when (NimMajor, NimMinor) < (1, 4): else: {.push raises: [].} -import std/[options, sets, sequtils] +import std/[options] import stew/[results, objects] import chronos, chronicles import ../../../multiaddress, diff --git a/libp2p/protocols/connectivity/autonat/server.nim b/libp2p/protocols/connectivity/autonat/server.nim index 1440393e7..0927369fd 100644 --- a/libp2p/protocols/connectivity/autonat/server.nim +++ b/libp2p/protocols/connectivity/autonat/server.nim @@ -14,7 +14,7 @@ else: import std/[options, sets, sequtils] import stew/results -import chronos, chronicles, stew/objects +import chronos, chronicles import ../../protocol, ../../../switch, ../../../multiaddress, diff --git a/libp2p/protocols/connectivity/relay/messages.nim b/libp2p/protocols/connectivity/relay/messages.nim index 7f05f78a1..4b4a0fb3a 100644 --- a/libp2p/protocols/connectivity/relay/messages.nim +++ b/libp2p/protocols/connectivity/relay/messages.nim @@ -12,7 +12,7 @@ when (NimMajor, NimMinor) < (1, 4): else: {.push raises: [].} -import options, macros, sequtils +import options, macros import stew/objects import ../../../peerinfo, ../../../signed_envelope @@ -115,9 +115,10 @@ proc decode*(_: typedesc[RelayMessage], buf: seq[byte]): Option[RelayMessage] = if r2.get(): rMsg.srcPeer = some(src) if r3.get(): rMsg.dstPeer = some(dst) if r4.get(): - if statusOrd.int notin StatusV1: + var status: StatusV1 + if not checkedEnumAssign(status, statusOrd): return none(RelayMessage) - rMsg.status = some(StatusV1(statusOrd)) + rMsg.status = some(status) some(rMsg) # Voucher @@ -285,9 +286,10 @@ proc decode*(_: typedesc[HopMessage], buf: seq[byte]): Option[HopMessage] = if r3.get(): msg.reservation = some(reservation) if r4.get(): msg.limit = limit if r5.get(): - if statusOrd.int notin StatusV2: + var status: StatusV2 + if not checkedEnumAssign(status, statusOrd): return none(HopMessage) - msg.status = some(StatusV2(statusOrd)) + msg.status = some(status) some(msg) # Circuit Relay V2 Stop Message @@ -364,7 +366,8 @@ proc decode*(_: typedesc[StopMessage], buf: seq[byte]): Option[StopMessage] = if r2.get(): msg.peer = some(peer) if r3.get(): msg.limit = limit if r4.get(): - if statusOrd.int notin StatusV2: + var status: StatusV2 + if not checkedEnumAssign(status, statusOrd): return none(StopMessage) - msg.status = some(StatusV2(statusOrd)) + msg.status = some(status) some(msg) diff --git a/libp2p/protocols/connectivity/relay/relay.nim b/libp2p/protocols/connectivity/relay/relay.nim index d099ff87b..d9b1bd928 100644 --- a/libp2p/protocols/connectivity/relay/relay.nim +++ b/libp2p/protocols/connectivity/relay/relay.nim @@ -12,7 +12,7 @@ when (NimMajor, NimMinor) < (1, 4): else: {.push raises: [].} -import options, sequtils, tables, sugar +import options, sequtils, tables import chronos, chronicles @@ -25,7 +25,6 @@ import ./messages, ../../../multicodec, ../../../stream/connection, ../../../protocols/protocol, - ../../../transports/transport, ../../../errors, ../../../utils/heartbeat, ../../../signed_envelope diff --git a/libp2p/protocols/ping.nim b/libp2p/protocols/ping.nim index 4b181b2f0..cfa6c59bd 100644 --- a/libp2p/protocols/ping.nim +++ b/libp2p/protocols/ping.nim @@ -15,7 +15,7 @@ else: {.push raises: [].} import chronos, chronicles -import bearssl/[rand, hash] +import bearssl/rand import ../protobuf/minprotobuf, ../peerinfo, ../stream/connection, diff --git a/libp2p/protocols/pubsub/gossipsub/behavior.nim b/libp2p/protocols/pubsub/gossipsub/behavior.nim index c4ab0015f..13a041245 100644 --- a/libp2p/protocols/pubsub/gossipsub/behavior.nim +++ b/libp2p/protocols/pubsub/gossipsub/behavior.nim @@ -15,7 +15,7 @@ else: import std/[tables, sequtils, sets, algorithm, deques] import chronos, chronicles, metrics import "."/[types, scoring] -import ".."/[pubsubpeer, peertable, timedcache, mcache, floodsub, pubsub] +import ".."/[pubsubpeer, peertable, mcache, floodsub, pubsub] import "../rpc"/[messages] import "../../.."/[peerid, multiaddress, utility, switch, routing_record, signed_envelope, utils/heartbeat] diff --git a/libp2p/protocols/pubsub/gossipsub/scoring.nim b/libp2p/protocols/pubsub/gossipsub/scoring.nim index fdc34cc74..3606059db 100644 --- a/libp2p/protocols/pubsub/gossipsub/scoring.nim +++ b/libp2p/protocols/pubsub/gossipsub/scoring.nim @@ -16,7 +16,7 @@ import std/[tables, sets, options] import chronos, chronicles, metrics import "."/[types] import ".."/[pubsubpeer] -import "../../.."/[peerid, multiaddress, utility, switch, utils/heartbeat] +import "../../.."/[peerid, multiaddress, switch, utils/heartbeat] logScope: topics = "libp2p gossipsub" @@ -250,7 +250,7 @@ proc updateScores*(g: GossipSub) = # avoid async if g.parameters.disconnectBadPeers and stats.score < g.parameters.graylistThreshold and peer.peerId notin g.parameters.directPeers: debug "disconnecting bad score peer", peer, score = peer.score - asyncSpawn(try: g.disconnectPeer(peer) except Exception as exc: raiseAssert exc.msg) + asyncSpawn(g.disconnectPeer(peer)) libp2p_gossipsub_peers_scores.inc(peer.score, labelValues = [agent]) diff --git a/libp2p/protocols/pubsub/rpc/message.nim b/libp2p/protocols/pubsub/rpc/message.nim index 2246f8094..a37cc7380 100644 --- a/libp2p/protocols/pubsub/rpc/message.nim +++ b/libp2p/protocols/pubsub/rpc/message.nim @@ -12,7 +12,6 @@ when (NimMajor, NimMinor) < (1, 4): else: {.push raises: [].} -import hashes import chronicles, metrics, stew/[byteutils, endians2] import ./messages, ./protobuf, diff --git a/libp2p/protocols/secure/noise.nim b/libp2p/protocols/secure/noise.nim index 757d57ebc..41a8ed02d 100644 --- a/libp2p/protocols/secure/noise.nim +++ b/libp2p/protocols/secure/noise.nim @@ -12,7 +12,7 @@ when (NimMajor, NimMinor) < (1, 4): else: {.push raises: [].} -import std/[oids, strformat] +import std/strformat import chronos import chronicles import bearssl/[rand, hash] diff --git a/libp2p/services/autorelayservice.nim b/libp2p/services/autorelayservice.nim index 2c5a66096..5dd71faa2 100644 --- a/libp2p/services/autorelayservice.nim +++ b/libp2p/services/autorelayservice.nim @@ -12,7 +12,7 @@ when (NimMajor, NimMinor) < (1, 4): else: {.push raises: [].} -import chronos, chronicles, times, tables, sequtils, options +import chronos, chronicles, times, tables, sequtils import ../switch, ../protocols/connectivity/relay/[client, utils] diff --git a/libp2p/stream/bufferstream.nim b/libp2p/stream/bufferstream.nim index a56bee3cd..3dcfbdb0c 100644 --- a/libp2p/stream/bufferstream.nim +++ b/libp2p/stream/bufferstream.nim @@ -18,9 +18,6 @@ import chronos, chronicles, metrics import ../stream/connection import ./streamseq -when chronicles.enabledLogLevel == LogLevel.TRACE: - import oids - export connection logScope: diff --git a/libp2p/stream/chronosstream.nim b/libp2p/stream/chronosstream.nim index d2db6cebf..f842886e4 100644 --- a/libp2p/stream/chronosstream.nim +++ b/libp2p/stream/chronosstream.nim @@ -12,7 +12,7 @@ when (NimMajor, NimMinor) < (1, 4): else: {.push raises: [].} -import std/[oids, strformat] +import std/[strformat] import stew/results import chronos, chronicles, metrics import connection diff --git a/libp2p/switch.nim b/libp2p/switch.nim index eb2d6f80c..231857899 100644 --- a/libp2p/switch.nim +++ b/libp2p/switch.nim @@ -20,9 +20,7 @@ import std/[tables, options, sequtils, sets, - oids, - sugar, - math] + oids] import chronos, chronicles, @@ -30,14 +28,12 @@ import chronos, import stream/connection, transports/transport, - upgrademngrs/[upgrade, muxedupgrade], + upgrademngrs/upgrade, multistream, multiaddress, protocols/protocol, protocols/secure/secure, peerinfo, - protocols/identify, - muxers/muxer, utils/semaphore, connmanager, nameresolving/nameresolver, diff --git a/libp2p/transports/tcptransport.nim b/libp2p/transports/tcptransport.nim index 4af207ee1..6407557db 100644 --- a/libp2p/transports/tcptransport.nim +++ b/libp2p/transports/tcptransport.nim @@ -14,14 +14,13 @@ when (NimMajor, NimMinor) < (1, 4): else: {.push raises: [].} -import std/[oids, sequtils] +import std/[sequtils] import stew/results import chronos, chronicles import transport, ../errors, ../wire, ../multicodec, - ../multistream, ../connmanager, ../multiaddress, ../stream/connection, diff --git a/libp2p/upgrademngrs/muxedupgrade.nim b/libp2p/upgrademngrs/muxedupgrade.nim index 6205923ac..bde7e3f58 100644 --- a/libp2p/upgrademngrs/muxedupgrade.nim +++ b/libp2p/upgrademngrs/muxedupgrade.nim @@ -12,7 +12,7 @@ when (NimMajor, NimMinor) < (1, 4): else: {.push raises: [].} -import std/[tables, sequtils] +import std/sequtils import pkg/[chronos, chronicles, metrics] import ../upgrademngrs/upgrade, diff --git a/libp2p/upgrademngrs/upgrade.nim b/libp2p/upgrademngrs/upgrade.nim index 3a2cc6687..8a191dea3 100644 --- a/libp2p/upgrademngrs/upgrade.nim +++ b/libp2p/upgrademngrs/upgrade.nim @@ -13,7 +13,7 @@ when (NimMajor, NimMinor) < (1, 4): else: {.push raises: [].} -import std/[options, sequtils, strutils] +import std/[sequtils, strutils] import pkg/[chronos, chronicles, metrics] import ../stream/connection, @@ -21,7 +21,6 @@ import ../stream/connection, ../protocols/identify, ../muxers/muxer, ../multistream, - ../peerstore, ../connmanager, ../errors, ../utility diff --git a/libp2p/utility.nim b/libp2p/utility.nim index 15050b10f..b22887dcc 100644 --- a/libp2p/utility.nim +++ b/libp2p/utility.nim @@ -65,3 +65,24 @@ when defined(libp2p_agents_metrics): const KnownLibP2PAgents* {.strdefine.} = "nim-libp2p" KnownLibP2PAgentsSeq* = KnownLibP2PAgents.safeToLowerAscii().tryGet().split(",") + +template safeConvert*[T: SomeInteger, S: Ordinal](value: S): T = + ## Converts `value` from S to `T` iff `value` is guaranteed to be preserved. + when int64(T.low) <= int64(S.low()) and uint64(T.high) >= uint64(S.high): + T(value) + else: + {.error: "Source and target types have an incompatible range low..high".} + +template exceptionToAssert*(body: untyped): untyped = + block: + var res: type(body) + when defined(nimHasWarnBareExcept): + {.push warning[BareExcept]:off.} + try: + res = body + except CatchableError as exc: raise exc + except Defect as exc: raise exc + except Exception as exc: raiseAssert exc.msg + when defined(nimHasWarnBareExcept): + {.pop.} + res diff --git a/libp2p/utils/heartbeat.nim b/libp2p/utils/heartbeat.nim index 42789c4ba..6f99008ff 100644 --- a/libp2p/utils/heartbeat.nim +++ b/libp2p/utils/heartbeat.nim @@ -12,7 +12,6 @@ when (NimMajor, NimMinor) < (1, 4): else: {.push raises: [].} -import sequtils import chronos, chronicles export chronicles diff --git a/tests/commontransport.nim b/tests/commontransport.nim index b46479b54..e18dc1978 100644 --- a/tests/commontransport.nim +++ b/tests/commontransport.nim @@ -1,13 +1,11 @@ {.used.} -import sequtils import chronos, stew/[byteutils, results] import ../libp2p/[stream/connection, transports/transport, upgrademngrs/upgrade, multiaddress, - errors, - wire] + errors] import ./helpers diff --git a/tests/helpers.nim b/tests/helpers.nim index ec7657500..af6129b23 100644 --- a/tests/helpers.nim +++ b/tests/helpers.nim @@ -56,9 +56,14 @@ template checkTrackers*() = checkpoint tracker.dump() fail() # Also test the GC is not fooling with us + when defined(nimHasWarnBareExcept): + {.push warning[BareExcept]:off.} try: GC_fullCollect() - except: discard + except: + discard + when defined(nimHasWarnBareExcept): + {.pop.} type RngWrap = object rng: ref HmacDrbgContext diff --git a/tests/testautonat.nim b/tests/testautonat.nim index fcdcb3b4b..738f15921 100644 --- a/tests/testautonat.nim +++ b/tests/testautonat.nim @@ -1,3 +1,14 @@ +{.used.} + +# Nim-Libp2p +# Copyright (c) 2023 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. + import std/options import chronos import diff --git a/tests/testautonatservice.nim b/tests/testautonatservice.nim index 7479a1c13..5f6289f3a 100644 --- a/tests/testautonatservice.nim +++ b/tests/testautonatservice.nim @@ -1,4 +1,6 @@ -# Nim-LibP2P +{.used.} + +# Nim-Libp2p # Copyright (c) 2023 Status Research & Development GmbH # Licensed under either of # * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) diff --git a/tests/testautorelay.nim b/tests/testautorelay.nim index 3117387ee..09e19da86 100644 --- a/tests/testautorelay.nim +++ b/tests/testautorelay.nim @@ -1,15 +1,21 @@ {.used.} +# Nim-Libp2p +# Copyright (c) 2023 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. + import chronos, options import ../libp2p import ../libp2p/[crypto/crypto, protocols/connectivity/relay/relay, - protocols/connectivity/relay/messages, - protocols/connectivity/relay/utils, protocols/connectivity/relay/client, services/autorelayservice] import ./helpers -import stew/byteutils proc createSwitch(r: Relay, autorelay: Service = nil): Switch = var builder = SwitchBuilder.new() diff --git a/tests/testbufferstream.nim b/tests/testbufferstream.nim index a31997e01..a0f83f32d 100644 --- a/tests/testbufferstream.nim +++ b/tests/testbufferstream.nim @@ -1,3 +1,14 @@ +{.used.} + +# Nim-Libp2p +# Copyright (c) 2023 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. + import chronos, stew/byteutils import ../libp2p/stream/bufferstream, ../libp2p/stream/lpstream, @@ -5,8 +16,6 @@ import ../libp2p/stream/bufferstream, import ./helpers -{.used.} - suite "BufferStream": teardown: # echo getTracker(BufferStreamTrackerName).dump() diff --git a/tests/testcid.nim b/tests/testcid.nim index 9d038ed53..d273b14d3 100644 --- a/tests/testcid.nim +++ b/tests/testcid.nim @@ -1,8 +1,17 @@ +{.used.} + +# Nim-Libp2p +# Copyright (c) 2023 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. + import unittest2 import ../libp2p/[cid, multihash, multicodec] -when defined(nimHasUsed): {.used.} - suite "Content identifier CID test suite": test "CIDv0 test vector": diff --git a/tests/testconnection.nim b/tests/testconnection.nim index 2502d04da..35e1757c6 100644 --- a/tests/testconnection.nim +++ b/tests/testconnection.nim @@ -1,4 +1,15 @@ -import chronos, nimcrypto/utils +{.used.} + +# Nim-Libp2p +# Copyright (c) 2023 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. + +import chronos import ../libp2p/[stream/connection, stream/bufferstream] diff --git a/tests/testconnmngr.nim b/tests/testconnmngr.nim index 4f2b246b4..60068b384 100644 --- a/tests/testconnmngr.nim +++ b/tests/testconnmngr.nim @@ -1,3 +1,14 @@ +{.used.} + +# Nim-Libp2p +# Copyright (c) 2023 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. + import std/[sequtils,tables] import stew/results import chronos diff --git a/tests/testcrypto.nim b/tests/testcrypto.nim index 16d2939f7..082ac3f1a 100644 --- a/tests/testcrypto.nim +++ b/tests/testcrypto.nim @@ -1,3 +1,5 @@ +{.used.} + # Nim-Libp2p # Copyright (c) 2023 Status Research & Development GmbH # Licensed under either of @@ -11,11 +13,9 @@ ## https://github.com/libp2p/go-libp2p-crypto/blob/master/key.go import unittest2 import bearssl/hash -import nimcrypto/[utils, sysrand] +import nimcrypto/utils import ../libp2p/crypto/[crypto, chacha20poly1305, curve25519, hkdf] -when defined(nimHasUsed): {.used.} - const PrivateKeys = [ """080012A809308204A40201000282010100C8B014EC01E135D635F7E246BA7D42 diff --git a/tests/testdcutr.nim b/tests/testdcutr.nim index a5a196402..5207a972a 100644 --- a/tests/testdcutr.nim +++ b/tests/testdcutr.nim @@ -1,4 +1,6 @@ -# Nim-LibP2P +{.used.} + +# Nim-Libp2p # Copyright (c) 2023 Status Research & Development GmbH # Licensed under either of # * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) diff --git a/tests/testdiscovery.nim b/tests/testdiscovery.nim index 58ebf9310..06885a392 100644 --- a/tests/testdiscovery.nim +++ b/tests/testdiscovery.nim @@ -1,7 +1,15 @@ {.used.} +# Nim-Libp2p +# Copyright (c) 2023 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. + import options, chronos, sets -import stew/byteutils import ../libp2p/[protocols/rendezvous, switch, builders, diff --git a/tests/testecnist.nim b/tests/testecnist.nim index 8ff35b02b..f97ba7076 100644 --- a/tests/testecnist.nim +++ b/tests/testecnist.nim @@ -1,3 +1,5 @@ +{.used.} + # Nim-Libp2p # Copyright (c) 2023 Status Research & Development GmbH # Licensed under either of @@ -6,13 +8,12 @@ # at your option. # This file may not be copied, modified, or distributed except according to # those terms. + import unittest2 import nimcrypto/utils import ../libp2p/crypto/[crypto, ecnist] import stew/results -when defined(nimHasUsed): {.used.} - const TestsCount = 10 # number of random tests diff --git a/tests/tested25519.nim b/tests/tested25519.nim index 5b98a29b6..ee89ac3b0 100644 --- a/tests/tested25519.nim +++ b/tests/tested25519.nim @@ -1,3 +1,5 @@ +{.used.} + # Nim-Libp2p # Copyright (c) 2023 Status Research & Development GmbH # Licensed under either of @@ -13,8 +15,6 @@ import nimcrypto/utils import ../libp2p/crypto/crypto import ../libp2p/crypto/ed25519/ed25519 -when defined(nimHasUsed): {.used.} - const TestsCount = 20 const SecretKeys = [ diff --git a/tests/testfuture.nim b/tests/testfuture.nim index 8ea378f4b..c9d4d3787 100644 --- a/tests/testfuture.nim +++ b/tests/testfuture.nim @@ -1,3 +1,5 @@ +{.used.} + # Nim-Libp2p # Copyright (c) 2023 Status Research & Development GmbH # Licensed under either of diff --git a/tests/testheartbeat.nim b/tests/testheartbeat.nim index 6a748f90f..27f8debf4 100644 --- a/tests/testheartbeat.nim +++ b/tests/testheartbeat.nim @@ -1,11 +1,22 @@ -import chronos +{.used.} -import ../libp2p/utils/heartbeat -import ./helpers +# Nim-Libp2p +# Copyright (c) 2023 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. # MacOs has some nasty jitter when sleeping # (up to 7 ms), so we skip test there when not defined(macosx): + import chronos + + import ../libp2p/utils/heartbeat + import ./helpers + suite "Heartbeat": asyncTest "simple heartbeat": diff --git a/tests/testidentify.nim b/tests/testidentify.nim index 8b43237f1..26f3c497e 100644 --- a/tests/testidentify.nim +++ b/tests/testidentify.nim @@ -1,5 +1,16 @@ +{.used.} + +# Nim-Libp2p +# Copyright (c) 2023 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. + import options -import chronos, strutils, sequtils, sets, algorithm +import chronos import ../libp2p/[protocols/identify, multiaddress, peerinfo, @@ -14,8 +25,6 @@ import ../libp2p/[protocols/identify, upgrademngrs/upgrade] import ./helpers -when defined(nimHasUsed): {.used.} - suite "Identify": teardown: checkTrackers() diff --git a/tests/testminasn1.nim b/tests/testminasn1.nim index c43d44486..393e7d901 100644 --- a/tests/testminasn1.nim +++ b/tests/testminasn1.nim @@ -1,3 +1,5 @@ +{.used.} + # Nim-Libp2p # Copyright (c) 2023 Status Research & Development GmbH # Licensed under either of @@ -6,12 +8,11 @@ # at your option. # This file may not be copied, modified, or distributed except according to # those terms. + import unittest2 import ../libp2p/crypto/minasn1 import nimcrypto/utils as ncrutils -when defined(nimHasUsed): {.used.} - const Asn1EdgeValues = [ 0'u64, (1'u64 shl 7) - 1'u64, (1'u64 shl 7), (1'u64 shl 8) - 1'u64, diff --git a/tests/testminprotobuf.nim b/tests/testminprotobuf.nim index b342fd0a6..085ad758a 100644 --- a/tests/testminprotobuf.nim +++ b/tests/testminprotobuf.nim @@ -1,3 +1,5 @@ +{.used.} + # Nim-Libp2p # Copyright (c) 2023 Status Research & Development GmbH # Licensed under either of @@ -11,8 +13,6 @@ import unittest2 import ../libp2p/protobuf/minprotobuf import stew/byteutils, strutils -when defined(nimHasUsed): {.used.} - suite "MinProtobuf test suite": const VarintVectors = [ "0800", "0801", "08ffffffff07", "08ffffffff0f", "08ffffffffffffffff7f", diff --git a/tests/testmplex.nim b/tests/testmplex.nim index 661ff3b9a..c9cab1d04 100644 --- a/tests/testmplex.nim +++ b/tests/testmplex.nim @@ -1,4 +1,15 @@ -import strformat, random, oids, sequtils +{.used.} + +# Nim-Libp2p +# Copyright (c) 2023 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. + +import strformat, random, sequtils import chronos, nimcrypto/utils, chronicles, stew/byteutils import ../libp2p/[errors, stream/connection, @@ -15,8 +26,6 @@ import ../libp2p/[errors, import ./helpers -{.used.} - suite "Mplex": teardown: checkTrackers() diff --git a/tests/testmultiaddress.nim b/tests/testmultiaddress.nim index 8114dba97..b7bcebc6d 100644 --- a/tests/testmultiaddress.nim +++ b/tests/testmultiaddress.nim @@ -1,4 +1,6 @@ -# Nim-LibP2P +{.used.} + +# Nim-Libp2p # Copyright (c) 2023 Status Research & Development GmbH # Licensed under either of # * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) @@ -18,8 +20,6 @@ import unittest2 import stew/byteutils import ../libp2p/[multicodec, multiaddress] -when defined(nimHasUsed): {.used.} - type PatternVector = object pattern: MaPattern @@ -329,12 +329,7 @@ suite "MultiAddress test suite": test "rust-multiaddr failure test vectors": for item in RustFailureVectors: - var r = false - try: - discard MultiAddress.init(item).get() - except: - r = true - check r == true + check MultiAddress.init(item).isErr() test "Concatenation test": var ma1 = MultiAddress.init() diff --git a/tests/testmultibase.nim b/tests/testmultibase.nim index e0e590ac0..fd59e4ec9 100644 --- a/tests/testmultibase.nim +++ b/tests/testmultibase.nim @@ -1,9 +1,18 @@ +{.used.} + +# Nim-Libp2p +# Copyright (c) 2023 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. + import unittest2 import ../libp2p/multibase import stew/results -when defined(nimHasUsed): {.used.} - const GoTestVectors = [ [ "identity", diff --git a/tests/testmultihash.nim b/tests/testmultihash.nim index e678150f1..ab454e651 100644 --- a/tests/testmultihash.nim +++ b/tests/testmultihash.nim @@ -1,9 +1,18 @@ +{.used.} + +# Nim-Libp2p +# Copyright (c) 2023 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. + import unittest2 import nimcrypto/utils import ../libp2p/multihash -when defined(nimHasUsed): {.used.} - const RustTestVectors = [ [ diff --git a/tests/testmultistream.nim b/tests/testmultistream.nim index 1f10a9255..f9913e585 100644 --- a/tests/testmultistream.nim +++ b/tests/testmultistream.nim @@ -1,7 +1,17 @@ -import strutils, strformat, stew/byteutils +{.used.} + +# Nim-Libp2p +# Copyright (c) 2023 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. + +import strformat, stew/byteutils import chronos -import ../libp2p/errors, - ../libp2p/multistream, +import ../libp2p/multistream, ../libp2p/stream/bufferstream, ../libp2p/stream/connection, ../libp2p/multiaddress, diff --git a/tests/testnameresolve.nim b/tests/testnameresolve.nim index 4c5da1fff..44900c46b 100644 --- a/tests/testnameresolve.nim +++ b/tests/testnameresolve.nim @@ -1,17 +1,23 @@ {.used.} -import std/[streams, strutils, sets, sequtils, tables, algorithm] -import chronos, stew/byteutils +# Nim-Libp2p +# Copyright (c) 2023 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. + +import std/[strutils, sequtils, tables] +import chronos import ../libp2p/[stream/connection, - transports/transport, transports/tcptransport, upgrademngrs/upgrade, multiaddress, - errors, nameresolving/nameresolver, nameresolving/dnsresolver, - nameresolving/mockresolver, - wire] + nameresolving/mockresolver] import ./helpers # @@ -41,8 +47,8 @@ proc guessOsNameServers(): seq[TransportAddress] = resultSeq.add(initTAddress(lineParsed[1], Port(53))) if resultSeq.len > 2: break #3 nameserver max on linux - except Exception as e: - echo "Failed to get unix nameservers ", e.msg + except CatchableError as err: + echo "Failed to get unix nameservers ", err.msg finally: if resultSeq.len > 0: return resultSeq diff --git a/tests/testnative.nim b/tests/testnative.nim index 48e971c1d..9c6b2dc0e 100644 --- a/tests/testnative.nim +++ b/tests/testnative.nim @@ -1,3 +1,14 @@ +{.used.} + +# Nim-Libp2p +# Copyright (c) 2023 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. + import testvarint, testconnection, testminprotobuf, @@ -45,4 +56,5 @@ import testtcptransport, testautonatservice, testautorelay, testdcutr, - testhpservice + testhpservice, + testutility diff --git a/tests/testnoise.nim b/tests/testnoise.nim index 1019eb1d7..074d85044 100644 --- a/tests/testnoise.nim +++ b/tests/testnoise.nim @@ -1,4 +1,6 @@ -# Nim-LibP2P +{.used.} + +# Nim-Libp2p # Copyright (c) 2023 Status Research & Development GmbH # Licensed under either of # * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) @@ -7,9 +9,6 @@ # This file may not be copied, modified, or distributed except according to # those terms. -{.used.} - -import tables import chronos, stew/byteutils import chronicles import ../libp2p/[switch, diff --git a/tests/testobservedaddrmanager.nim b/tests/testobservedaddrmanager.nim index a5b60df70..a92ec80b2 100644 --- a/tests/testobservedaddrmanager.nim +++ b/tests/testobservedaddrmanager.nim @@ -1,3 +1,14 @@ +{.used.} + +# Nim-Libp2p +# Copyright (c) 2023 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. + import unittest2, ../libp2p/multiaddress, ../libp2p/observedaddrmanager, diff --git a/tests/testpeerid.nim b/tests/testpeerid.nim index d1bbf71ee..64b47db52 100644 --- a/tests/testpeerid.nim +++ b/tests/testpeerid.nim @@ -1,3 +1,5 @@ +{.used.} + # Nim-Libp2p # Copyright (c) 2023 Status Research & Development GmbH # Licensed under either of @@ -13,7 +15,6 @@ import unittest2 import nimcrypto/utils, stew/base58 import ../libp2p/crypto/crypto, ../libp2p/peerid import bearssl/hash -when defined(nimHasUsed): {.used.} const # Test vectors are generated using diff --git a/tests/testpeerinfo.nim b/tests/testpeerinfo.nim index e4f273997..907178127 100644 --- a/tests/testpeerinfo.nim +++ b/tests/testpeerinfo.nim @@ -1,7 +1,16 @@ {.used.} +# Nim-Libp2p +# Copyright (c) 2023 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. + import options -import chronos, stew/byteutils +import chronos import ../libp2p/crypto/crypto, ../libp2p/multicodec, ../libp2p/peerinfo, diff --git a/tests/testpeerstore.nim b/tests/testpeerstore.nim index b6ce7cdd5..058af49c2 100644 --- a/tests/testpeerstore.nim +++ b/tests/testpeerstore.nim @@ -1,6 +1,17 @@ +{.used.} + +# Nim-Libp2p +# Copyright (c) 2023 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. + import unittest2, - std/[tables, sequtils, sets], + std/[tables, sequtils], ../libp2p/crypto/crypto, ../libp2p/multiaddress, ../libp2p/peerid, diff --git a/tests/testping.nim b/tests/testping.nim index 88cf69ebc..f38874b2c 100644 --- a/tests/testping.nim +++ b/tests/testping.nim @@ -1,10 +1,18 @@ -import options -import chronos, strutils -import ../libp2p/[protocols/identify, - protocols/ping, +{.used.} + +# Nim-Libp2p +# Copyright (c) 2023 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. + +import chronos +import ../libp2p/[protocols/ping, multiaddress, peerinfo, - wire, peerid, stream/connection, multistream, @@ -14,8 +22,6 @@ import ../libp2p/[protocols/identify, upgrademngrs/upgrade] import ./helpers -when defined(nimHasUsed): {.used.} - suite "Ping": var ma {.threadvar.}: MultiAddress diff --git a/tests/testrelayv1.nim b/tests/testrelayv1.nim index 64656e939..cda512e99 100644 --- a/tests/testrelayv1.nim +++ b/tests/testrelayv1.nim @@ -1,5 +1,14 @@ {.used.} +# Nim-Libp2p +# Copyright (c) 2023 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. + import options, bearssl, chronos import stew/byteutils import ../libp2p/[protocols/connectivity/relay/relay, @@ -12,7 +21,6 @@ import ../libp2p/[protocols/connectivity/relay/relay, peerid, stream/connection, multistream, - transports/transport, switch, builders, upgrademngrs/upgrade, diff --git a/tests/testrelayv2.nim b/tests/testrelayv2.nim index 6bad95b30..41267aaa4 100644 --- a/tests/testrelayv2.nim +++ b/tests/testrelayv2.nim @@ -1,5 +1,14 @@ {.used.} +# Nim-Libp2p +# Copyright (c) 2023 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. + import bearssl, chronos, options import ../libp2p import ../libp2p/[protocols/connectivity/relay/relay, diff --git a/tests/testrendezvous.nim b/tests/testrendezvous.nim index 74ee3c422..14adc00cf 100644 --- a/tests/testrendezvous.nim +++ b/tests/testrendezvous.nim @@ -1,7 +1,16 @@ {.used.} -import options, sequtils, strutils -import stew/byteutils, chronos +# Nim-Libp2p +# Copyright (c) 2023 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. + +import sequtils, strutils +import chronos import ../libp2p/[protocols/rendezvous, switch, builders,] diff --git a/tests/testrouting_record.nim b/tests/testrouting_record.nim index 112efe442..65bb81e95 100644 --- a/tests/testrouting_record.nim +++ b/tests/testrouting_record.nim @@ -1,3 +1,14 @@ +{.used.} + +# Nim-Libp2p +# Copyright (c) 2023 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. + import unittest2 import stew/byteutils import ../libp2p/[routing_record, crypto/crypto] diff --git a/tests/testrsa.nim b/tests/testrsa.nim index 8100272d8..8f83c6ef3 100644 --- a/tests/testrsa.nim +++ b/tests/testrsa.nim @@ -1,3 +1,5 @@ +{.used.} + # Nim-Libp2p # Copyright (c) 2023 Status Research & Development GmbH # Licensed under either of @@ -6,12 +8,11 @@ # at your option. # This file may not be copied, modified, or distributed except according to # those terms. + import unittest2 import nimcrypto/utils import ../libp2p/crypto/[crypto, rsa] -when defined(nimHasUsed): {.used.} - const NotAllowedPrivateKeys = [ """3082013902010002410093405660EDBF5DADAF93DD91E1B38F1EF086A3A0C6EA diff --git a/tests/testsecp256k1.nim b/tests/testsecp256k1.nim index 83515aeaf..88d60dfe7 100644 --- a/tests/testsecp256k1.nim +++ b/tests/testsecp256k1.nim @@ -1,3 +1,5 @@ +{.used.} + # Nim-Libp2p # Copyright (c) 2023 Status Research & Development GmbH # Licensed under either of @@ -6,11 +8,9 @@ # at your option. # This file may not be copied, modified, or distributed except according to # those terms. + import unittest2 import ../libp2p/crypto/[crypto, secp] -import nimcrypto/utils - -when defined(nimHasUsed): {.used.} let rng = newRng() diff --git a/tests/testsemaphore.nim b/tests/testsemaphore.nim index 09a7d293d..b8688df66 100644 --- a/tests/testsemaphore.nim +++ b/tests/testsemaphore.nim @@ -1,3 +1,14 @@ +{.used.} + +# Nim-Libp2p +# Copyright (c) 2023 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. + import random import chronos diff --git a/tests/testsigned_envelope.nim b/tests/testsigned_envelope.nim index 510db90fb..48bb02e0f 100644 --- a/tests/testsigned_envelope.nim +++ b/tests/testsigned_envelope.nim @@ -1,3 +1,14 @@ +{.used.} + +# Nim-Libp2p +# Copyright (c) 2023 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. + import unittest2 import stew/byteutils import ../libp2p/[signed_envelope] diff --git a/tests/teststreamseq.nim b/tests/teststreamseq.nim index 4050633e6..f5f58b942 100644 --- a/tests/teststreamseq.nim +++ b/tests/teststreamseq.nim @@ -1,5 +1,14 @@ {.used.} +# Nim-Libp2p +# Copyright (c) 2023 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. + import unittest2 import stew/byteutils import ../libp2p/stream/streamseq diff --git a/tests/testswitch.nim b/tests/testswitch.nim index 92a7d9f53..221b48f8e 100644 --- a/tests/testswitch.nim +++ b/tests/testswitch.nim @@ -1,9 +1,17 @@ {.used.} -import options, sequtils, sets +# Nim-Libp2p +# Copyright (c) 2023 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. + +import options, sequtils import chronos import stew/byteutils -import nimcrypto/sysrand import ../libp2p/[errors, switch, multistream, @@ -19,7 +27,6 @@ import ../libp2p/[errors, muxers/muxer, muxers/mplex/lpchannel, stream/lpstream, - nameresolving/nameresolver, nameresolving/mockresolver, stream/chronosstream, utils/semaphore, diff --git a/tests/testtcptransport.nim b/tests/testtcptransport.nim index d765bfa5c..51829dbd4 100644 --- a/tests/testtcptransport.nim +++ b/tests/testtcptransport.nim @@ -1,6 +1,14 @@ {.used.} -import sequtils +# Nim-Libp2p +# Copyright (c) 2023 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. + import chronos, stew/byteutils import ../libp2p/[stream/connection, transports/transport, diff --git a/tests/testtortransport.nim b/tests/testtortransport.nim index 2431458e6..213606479 100644 --- a/tests/testtortransport.nim +++ b/tests/testtortransport.nim @@ -1,5 +1,14 @@ {.used.} +# Nim-Libp2p +# Copyright (c) 2023 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. + when (NimMajor, NimMinor) < (1, 4): {.push raises: [Defect].} else: diff --git a/tests/testutility.nim b/tests/testutility.nim new file mode 100644 index 000000000..18c8ce141 --- /dev/null +++ b/tests/testutility.nim @@ -0,0 +1,76 @@ +{.used.} + +# Nim-Libp2p +# Copyright (c) 2023 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. + +import strformat +import ./helpers +import ../libp2p/utility + +suite "Utility": + + test "successful safeConvert from int8 to int16": + var result: int16 = safeConvert[int16, int8](-128) + assert result == -128, fmt"Expected -128 but got {result}" + + test "unsuccessful safeConvert from int16 to int8": + doAssert not (compiles do: + var result: int8 = safeConvert[int8, int16](32767)) + + test "successful safeConvert from uint8 to uint16": + var result: uint16 = safeConvert[uint16, uint8](255) + assert result == 255, fmt"Expected 255 but got {result}" + + test "unsuccessful safeConvert from uint16 to uint8": + doAssert not (compiles do: + var result: uint8 = safeConvert[uint8, uint16](256)) + + test "successful safeConvert from char to int": + var result: int = safeConvert[int, char]('A') + assert result == 65, fmt"Expected 65 but got {result}" + + test "unsuccessful safeConvert from int to char": + doAssert not (compiles do: + var result: char = safeConvert[char, int](128)) + + test "successful safeConvert from bool to int": + var result: int = safeConvert[int, bool](true) + assert result == 1, fmt"Expected 1 but got {result}" + + test "unsuccessful safeConvert from int to bool": + doAssert not (compiles do: + var result: bool = safeConvert[bool, int](2)) + + test "successful safeConvert from enum to int": + type Color = enum red, green, blue + var result: int = safeConvert[int, Color](green) + assert result == 1, fmt"Expected 1 but got {result}" + + test "unsuccessful safeConvert from int to enum": + type Color = enum red, green, blue + doAssert not (compiles do: + var result: Color = safeConvert[Color, int](3)) + + test "successful safeConvert from range to int": + var result: int = safeConvert[int, range[1..10]](5) + assert result == 5, fmt"Expected 5 but got {result}" + + test "unsuccessful safeConvert from int to range": + doAssert not (compiles do: + var result: range[1..10] = safeConvert[range[1..10], int](11)) + + test "unsuccessful safeConvert from int to uint": + doAssert not (compiles do: + var result: uint = safeConvert[uint, int](11)) + + test "unsuccessful safeConvert from uint to int": + doAssert not (compiles do: + var result: uint = safeConvert[int, uint](11)) + + diff --git a/tests/testvarint.nim b/tests/testvarint.nim index c9d915aba..4af6fb201 100644 --- a/tests/testvarint.nim +++ b/tests/testvarint.nim @@ -1,8 +1,17 @@ +{.used.} + +# Nim-Libp2p +# Copyright (c) 2023 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. + import unittest2 import ../libp2p/varint -when defined(nimHasUsed): {.used.} - const PBedgeValues = [ 0'u64, (1'u64 shl 7) - 1'u64, (1'u64 shl 7), (1'u64 shl 14) - 1'u64, diff --git a/tests/testwstransport.nim b/tests/testwstransport.nim index c2ac88751..b53f27308 100644 --- a/tests/testwstransport.nim +++ b/tests/testwstransport.nim @@ -1,14 +1,21 @@ {.used.} -import sequtils +# Nim-Libp2p +# Copyright (c) 2023 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. + import chronos, stew/byteutils import ../libp2p/[stream/connection, transports/transport, transports/wstransport, upgrademngrs/upgrade, multiaddress, - errors, - wire] + errors] import ./helpers, ./commontransport @@ -68,7 +75,8 @@ suite "WebSocket transport": TLSPrivateKey.init(SecureKey), TLSCertificate.init(SecureCert), {TLSFlags.NoVerifyHost, TLSFlags.NoVerifyServerName}) - except Exception: check(false) + except CatchableError: + check(false) commonTransportTest( wsSecureTranspProvider, diff --git a/tests/testyamux.nim b/tests/testyamux.nim index 86aee697b..c0c13624e 100644 --- a/tests/testyamux.nim +++ b/tests/testyamux.nim @@ -1,3 +1,14 @@ +{.used.} + +# Nim-Libp2p +# Copyright (c) 2023 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. + import sugar import chronos import