From dc83a1e9b68f00b3be7e09febdb1a3f877321b9a Mon Sep 17 00:00:00 2001 From: diegomrsantos Date: Tue, 11 Jun 2024 17:18:06 +0200 Subject: [PATCH] chore(formatting): format the whole codebase using nph 0.5.1 (#1118) --- .github/workflows/ci.yml | 22 + README.md | 2 +- config.nims | 9 +- examples/circuitrelay.nim | 29 +- examples/directchat.nim | 51 +- examples/go-daemon/bootstrap.nim | 13 +- examples/go-daemon/chat.nim | 18 +- examples/go-daemon/node.nim | 19 +- examples/helloworld.nim | 29 +- examples/hexdump.nim | 22 +- examples/tutorial_1_connect.nim | 23 +- examples/tutorial_2_customproto.nim | 10 +- examples/tutorial_3_protobuf.nim | 31 +- examples/tutorial_4_gossipsub.nim | 43 +- examples/tutorial_5_discovery.nim | 24 +- examples/tutorial_6_game.nim | 68 +- libp2p.nim | 55 +- libp2p.nimble | 95 +- libp2p/builders.nim | 169 +- libp2p/cid.nim | 120 +- libp2p/connmanager.nim | 175 +- libp2p/crypto/chacha20poly1305.nim | 58 +- libp2p/crypto/crypto.nim | 306 +- libp2p/crypto/curve25519.nim | 42 +- libp2p/crypto/ecnist.nim | 214 +- libp2p/crypto/ed25519/constants.nim | 3792 ++++++++--------- libp2p/crypto/ed25519/ed25519.nim | 1232 ++++-- libp2p/crypto/hkdf.nim | 43 +- libp2p/crypto/minasn1.nim | 334 +- libp2p/crypto/rsa.nim | 243 +- libp2p/crypto/secp.nim | 49 +- libp2p/daemon/daemonapi.nim | 399 +- libp2p/daemon/transpool.nim | 30 +- libp2p/debugutils.nim | 88 +- libp2p/dial.nim | 51 +- libp2p/dialer.nim | 185 +- libp2p/discovery/discoverymngr.nim | 24 +- libp2p/discovery/rendezvousinterface.nim | 16 +- libp2p/errors.nim | 18 +- libp2p/multiaddress.nim | 428 +- libp2p/multibase.nim | 448 +- libp2p/multicodec.nim | 450 +- libp2p/multihash.nim | 450 +- libp2p/multistream.nim | 109 +- libp2p/muxers/mplex/coder.nim | 49 +- libp2p/muxers/mplex/lpchannel.nim | 88 +- libp2p/muxers/mplex/mplex.nim | 84 +- libp2p/muxers/muxer.nim | 35 +- libp2p/muxers/yamux/yamux.nim | 301 +- libp2p/nameresolving/dnsresolver.nim | 57 +- libp2p/nameresolving/mockresolver.nim | 17 +- libp2p/nameresolving/nameresolver.nim | 59 +- libp2p/observedaddrmanager.nim | 52 +- libp2p/peerid.nim | 38 +- libp2p/peerinfo.nim | 54 +- libp2p/peerstore.nim | 48 +- libp2p/protobuf/minprotobuf.nim | 235 +- .../protocols/connectivity/autonat/client.nim | 47 +- .../protocols/connectivity/autonat/core.nim | 30 +- .../protocols/connectivity/autonat/server.nim | 85 +- .../connectivity/autonat/service.nim | 94 +- .../protocols/connectivity/dcutr/client.nim | 69 +- libp2p/protocols/connectivity/dcutr/core.nim | 17 +- .../protocols/connectivity/dcutr/server.nim | 41 +- .../protocols/connectivity/relay/client.nim | 230 +- .../protocols/connectivity/relay/messages.nim | 124 +- libp2p/protocols/connectivity/relay/rconn.nim | 25 +- libp2p/protocols/connectivity/relay/relay.nim | 292 +- .../connectivity/relay/rtransport.nim | 52 +- libp2p/protocols/connectivity/relay/utils.nim | 26 +- libp2p/protocols/identify.nim | 93 +- libp2p/protocols/perf/client.nim | 11 +- libp2p/protocols/perf/server.nim | 7 +- libp2p/protocols/ping.nim | 35 +- libp2p/protocols/protocol.nim | 42 +- libp2p/protocols/pubsub/errors.nim | 7 +- libp2p/protocols/pubsub/floodsub.nim | 77 +- libp2p/protocols/pubsub/gossipsub.nim | 411 +- .../protocols/pubsub/gossipsub/behavior.nim | 438 +- libp2p/protocols/pubsub/gossipsub/scoring.nim | 187 +- libp2p/protocols/pubsub/gossipsub/types.nim | 47 +- libp2p/protocols/pubsub/mcache.nim | 16 +- libp2p/protocols/pubsub/peertable.nim | 25 +- libp2p/protocols/pubsub/pubsub.nim | 292 +- libp2p/protocols/pubsub/pubsubpeer.nim | 217 +- libp2p/protocols/pubsub/rpc/message.nim | 39 +- libp2p/protocols/pubsub/rpc/messages.nim | 90 +- libp2p/protocols/pubsub/rpc/protobuf.nim | 118 +- libp2p/protocols/pubsub/timedcache.nim | 37 +- libp2p/protocols/rendezvous.nim | 330 +- libp2p/protocols/secure/noise.nim | 336 +- libp2p/protocols/secure/plaintext.nim | 7 +- libp2p/protocols/secure/secure.nim | 102 +- libp2p/routing_record.nim | 45 +- libp2p/services/autorelayservice.nim | 47 +- libp2p/services/hpservice.nim | 50 +- libp2p/signed_envelope.nim | 112 +- libp2p/stream/bufferstream.nim | 63 +- libp2p/stream/chronosstream.nim | 56 +- libp2p/stream/connection.nim | 46 +- libp2p/stream/lpstream.nim | 109 +- libp2p/stream/streamseq.nim | 22 +- libp2p/switch.nim | 199 +- libp2p/transcoder.nim | 15 +- libp2p/transports/tcptransport.nim | 2 +- libp2p/transports/tortransport.nim | 192 +- libp2p/transports/transport.nim | 57 +- libp2p/transports/wstransport.nim | 166 +- libp2p/upgrademngrs/muxedupgrade.nim | 48 +- libp2p/upgrademngrs/upgrade.nim | 38 +- libp2p/utility.nim | 33 +- libp2p/utils/future.nim | 7 +- libp2p/utils/heartbeat.nim | 7 +- libp2p/utils/offsettedseq.nim | 29 +- libp2p/utils/semaphore.nim | 20 +- libp2p/varint.nim | 107 +- libp2p/vbuffer.nim | 45 +- libp2p/wire.nim | 116 +- tests/asyncunit.nim | 46 +- tests/commoninterop.nim | 98 +- tests/commontransport.nim | 59 +- tests/config.nims | 26 +- tests/errorhelpers.nim | 35 +- tests/helpers.nim | 78 +- tests/hole-punching-interop/hole_punching.nim | 46 +- tests/pubsub/config.nims | 4 +- tests/pubsub/testfloodsub.nim | 180 +- tests/pubsub/testgossipinternal.nim | 222 +- tests/pubsub/testgossipsub.nim | 489 +-- tests/pubsub/testgossipsub2.nim | 207 +- tests/pubsub/testmcache.nim | 63 +- tests/pubsub/testmessage.nim | 93 +- tests/pubsub/testpubsub.nim | 8 +- tests/pubsub/testtimedcache.nim | 8 +- tests/pubsub/utils.nim | 172 +- tests/stublogger.nim | 4 +- tests/stubs/autonatclientstub.nim | 46 +- tests/stubs/switchstub.nim | 34 +- tests/stubs/torstub.nim | 57 +- tests/testautonat.nim | 40 +- tests/testautonatservice.nim | 196 +- tests/testautorelay.nim | 37 +- tests/testbufferstream.nim | 25 +- tests/testcid.nim | 30 +- tests/testconnection.nim | 3 +- tests/testconnmngr.nim | 104 +- tests/testcrypto.nim | 202 +- tests/testdaemon.nim | 32 +- tests/testdcutr.nim | 167 +- tests/testdialer.nim | 4 +- tests/testdiscovery.nim | 30 +- tests/testecnist.nim | 105 +- tests/tested25519.nim | 26 +- tests/testheartbeat.nim | 9 +- tests/testhelpers.nim | 1 - tests/testhpservice.nim | 195 +- tests/testidentify.nim | 80 +- tests/testinterop.nim | 81 +- tests/testminasn1.nim | 84 +- tests/testminprotobuf.nim | 32 +- tests/testmplex.nim | 471 +- tests/testmultiaddress.nim | 367 +- tests/testmultibase.nim | 116 +- tests/testmultihash.nim | 121 +- tests/testmultistream.nim | 156 +- tests/testnameresolve.nim | 236 +- tests/testnative.nim | 60 +- tests/testnoise.nim | 93 +- tests/testobservedaddrmanager.nim | 19 +- tests/testpeerid.nim | 10 +- tests/testpeerinfo.nim | 27 +- tests/testpeerstore.nim | 17 +- tests/testping.nim | 25 +- tests/testpkifilter.nim | 53 +- tests/testrelayv1.nim | 165 +- tests/testrelayv2.nim | 237 +- tests/testrendezvous.nim | 45 +- tests/testrendezvousinterface.nim | 31 +- tests/testrouting_record.nim | 29 +- tests/testrsa.nim | 22 +- tests/testsecp256k1.nim | 6 +- tests/testsemaphore.nim | 13 +- tests/testsigned_envelope.nim | 26 +- tests/teststreamseq.nim | 11 +- tests/testswitch.nim | 406 +- tests/testtcptransport.nim | 45 +- tests/testtortransport.nim | 45 +- tests/testutility.nim | 1 + tests/testvarint.nim | 286 +- tests/testwildcardresolverservice.nim | 2 +- tests/testwire.nim | 75 +- tests/testwstransport.nim | 58 +- tests/testyamux.nim | 72 +- tests/transport-interop/main.nim | 71 +- tools/markdown_builder.nim | 8 +- tools/markdown_runner.nim | 3 +- tools/pbcap_parser.nim | 26 +- tools/pinner.nim | 9 +- 198 files changed, 12735 insertions(+), 11066 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index eb48f2e428..3df33b4430 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -90,3 +90,25 @@ jobs: nim --version nimble --version nimble test + + lint: + name: "Lint" + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 2 # In PR, has extra merge commit: ^1 = PR, ^2 = base + + - name: Check nph formatting + # Pin nph to a specific version to avoid sudden style differences. + # Updating nph version should be accompanied with running the new + # version on the fluffy directory. + run: | + VERSION="v0.5.1" + ARCHIVE="nph-linux_x64.tar.gz" + curl -L "https://github.com/arnetheduck/nph/releases/download/${VERSION}/${ARCHIVE}" -o ${ARCHIVE} + tar -xzf ${ARCHIVE} + shopt -s extglob # Enable extended globbing + ./nph examples libp2p tests tools *.@(nim|nims|nimble) + git diff --exit-code diff --git a/README.md b/README.md index ff8a5f8716..ace2ab34ba 100644 --- a/README.md +++ b/README.md @@ -135,7 +135,7 @@ The libp2p implementation in Nim is a work in progress. We welcome contributors - **Perform code reviews**. Feel free to let us know if you found anything that can a) speed up the project development b) ensure better quality and c) reduce possible future bugs. - **Add tests**. Help nim-libp2p to be more robust by adding more tests to the [tests folder](tests/). - **Small PRs**. Try to keep PRs atomic and digestible. This makes the review process and pinpointing bugs easier. -- **Code format**. Please format code using [nph](https://github.com/arnetheduck/nph). +- **Code format**. Please format code using [nph](https://github.com/arnetheduck/nph) v0.5.1. This will ensure a consistent codebase and make PRs easier to review. A CI rule has been added to ensure that future commits are all formatted using the same nph version. The code follows the [Status Nim Style Guide](https://status-im.github.io/nim-style-guide/). ### Contributors diff --git a/config.nims b/config.nims index e7b14c4395..0414dfc755 100644 --- a/config.nims +++ b/config.nims @@ -7,14 +7,17 @@ if dirExists("nimbledeps/pkgs2"): switch("warning", "CaseTransition:off") switch("warning", "ObservableStores:off") switch("warning", "LockLevel:off") ---styleCheck:usages +--styleCheck: + usages switch("warningAsError", "UseBase:on") ---styleCheck:error +--styleCheck: + error # Avoid some rare stack corruption while using exceptions with a SEH-enabled # toolchain: https://github.com/status-im/nimbus-eth2/issues/3121 if defined(windows) and not defined(vcc): - --define:nimRawSetjmp + --define: + nimRawSetjmp # begin Nimble config (version 1) when fileExists("nimble.paths"): diff --git a/examples/circuitrelay.nim b/examples/circuitrelay.nim index b609609626..66e2af285f 100644 --- a/examples/circuitrelay.nim +++ b/examples/circuitrelay.nim @@ -7,19 +7,19 @@ ## ## More informations [here](https://docs.libp2p.io/concepts/circuit-relay/). import chronos, stew/byteutils -import libp2p, - libp2p/protocols/connectivity/relay/[relay, client] +import libp2p, libp2p/protocols/connectivity/relay/[relay, client] # Helper to create a circuit relay node proc createCircuitRelaySwitch(r: Relay): Switch = - SwitchBuilder.new() - .withRng(newRng()) - .withAddresses(@[ MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet() ]) - .withTcpTransport() - .withMplex() - .withNoise() - .withCircuitRelay(r) - .build() + SwitchBuilder + .new() + .withRng(newRng()) + .withAddresses(@[MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet()]) + .withTcpTransport() + .withMplex() + .withNoise() + .withCircuitRelay(r) + .build() proc main() {.async.} = # Create a custom protocol @@ -56,8 +56,11 @@ proc main() {.async.} = let # Create a relay address to swDst using swRel as the relay - addrs = MultiAddress.init($swRel.peerInfo.addrs[0] & "/p2p/" & - $swRel.peerInfo.peerId & "/p2p-circuit").get() + addrs = MultiAddress + .init( + $swRel.peerInfo.addrs[0] & "/p2p/" & $swRel.peerInfo.peerId & "/p2p-circuit" + ) + .get() # Connect Dst to the relay await swDst.connect(swRel.peerInfo.peerId, swRel.peerInfo.addrs) @@ -66,7 +69,7 @@ proc main() {.async.} = let rsvp = await clDst.reserve(swRel.peerInfo.peerId, swRel.peerInfo.addrs) # Src dial Dst using the relay - let conn = await swSrc.dial(swDst.peerInfo.peerId, @[ addrs ], customProtoCodec) + let conn = await swSrc.dial(swDst.peerInfo.peerId, @[addrs], customProtoCodec) await conn.writeLp("test1") var msg = string.fromBytes(await conn.readLp(1024)) diff --git a/examples/directchat.nim b/examples/directchat.nim index b550d48054..6c65d3278d 100644 --- a/examples/directchat.nim +++ b/examples/directchat.nim @@ -1,15 +1,12 @@ -when not(compileOption("threads")): +when not (compileOption("threads")): {.fatal: "Please, compile this program with the --threads:on option!".} -import - strformat, strutils, - stew/byteutils, - chronos, - libp2p +import strformat, strutils, stew/byteutils, chronos, libp2p const DefaultAddr = "/ip4/127.0.0.1/tcp/0" -const Help = """ +const Help = + """ Commands: /[?|help|connect|disconnect|exit] help: Prints this help connect: dials a remote peer @@ -17,12 +14,11 @@ const Help = """ exit: closes the chat """ -type - Chat = ref object - switch: Switch # a single entry point for dialing and listening to peer - stdinReader: StreamTransport # transport streams between read & write file descriptor - conn: Connection # connection to the other peer - connected: bool # if the node is connected to another peer +type Chat = ref object + switch: Switch # a single entry point for dialing and listening to peer + stdinReader: StreamTransport # transport streams between read & write file descriptor + conn: Connection # connection to the other peer + connected: bool # if the node is connected to another peer ## # Stdout helpers, to write the prompt @@ -41,8 +37,7 @@ proc writeStdout(c: Chat, str: string) = ## const ChatCodec = "/nim-libp2p/chat/1.0.0" -type - ChatProto = ref object of LPProtocol +type ChatProto = ref object of LPProtocol proc new(T: typedesc[ChatProto], c: Chat): T = let chatproto = T() @@ -77,9 +72,9 @@ proc handlePeer(c: Chat, conn: Connection) {.async.} = strData = await conn.readLp(1024) str = string.fromBytes(strData) c.writeStdout $conn.peerId & ": " & $str - except LPStreamEOFError: - defer: c.writeStdout $conn.peerId & " disconnected" + defer: + c.writeStdout $conn.peerId & " disconnected" await c.conn.close() c.connected = false @@ -88,10 +83,7 @@ proc dialPeer(c: Chat, address: string) {.async.} = let multiAddr = MultiAddress.init(address).tryGet() # split the peerId part /p2p/... - peerIdBytes = multiAddr[multiCodec("p2p")] - .tryGet() - .protoAddress() - .tryGet() + peerIdBytes = multiAddr[multiCodec("p2p")].tryGet().protoAddress().tryGet() remotePeer = PeerId.init(peerIdBytes).tryGet() # split the wire address ip4Addr = multiAddr[multiCodec("ip4")].tryGet() @@ -124,7 +116,6 @@ proc readLoop(c: Chat) {.async.} = let address = await c.stdinReader.readLine() if address.len > 0: await c.dialPeer(address) - elif line.startsWith("/exit"): if c.connected and c.conn.closed.not: await c.conn.close() @@ -171,16 +162,18 @@ proc main() {.async.} = var switch = SwitchBuilder .new() - .withRng(rng) # Give the application RNG + .withRng(rng) + # Give the application RNG .withAddress(localAddress) - .withTcpTransport() # Use TCP as transport - .withMplex() # Use Mplex as muxer - .withNoise() # Use Noise as secure manager + .withTcpTransport() + # Use TCP as transport + .withMplex() + # Use Mplex as muxer + .withNoise() + # Use Noise as secure manager .build() - let chat = Chat( - switch: switch, - stdinReader: stdinReader) + let chat = Chat(switch: switch, stdinReader: stdinReader) switch.mount(ChatProto.new(chat)) diff --git a/examples/go-daemon/bootstrap.nim b/examples/go-daemon/bootstrap.nim index 69523edd29..1d0ff97edd 100644 --- a/examples/go-daemon/bootstrap.nim +++ b/examples/go-daemon/bootstrap.nim @@ -2,8 +2,7 @@ import chronos, nimcrypto, strutils import ../../libp2p/daemon/daemonapi import ../hexdump -const - PubSubTopic = "test-net" +const PubSubTopic = "test-net" proc dumpSubscribedPeers(api: DaemonAPI) {.async.} = var peers = await api.pubsubListPeers(PubSubTopic) @@ -37,12 +36,12 @@ proc main() {.async.} = asyncSpawn monitor(api) - proc pubsubLogger(api: DaemonAPI, - ticket: PubsubTicket, - message: PubSubMessage): Future[bool] {.async.} = + proc pubsubLogger( + api: DaemonAPI, ticket: PubsubTicket, message: PubSubMessage + ): Future[bool] {.async.} = let msglen = len(message.data) - echo "= Recieved pubsub message with length ", msglen, - " bytes from peer ", message.peer.pretty() + echo "= Recieved pubsub message with length ", + msglen, " bytes from peer ", message.peer.pretty() echo dumpHex(message.data) await api.dumpSubscribedPeers() result = true diff --git a/examples/go-daemon/chat.nim b/examples/go-daemon/chat.nim index f15667d20b..0e249993b7 100644 --- a/examples/go-daemon/chat.nim +++ b/examples/go-daemon/chat.nim @@ -2,18 +2,16 @@ import chronos, nimcrypto, strutils import ../../libp2p/daemon/daemonapi ## nim c -r --threads:on chat.nim -when not(compileOption("threads")): +when not (compileOption("threads")): {.fatal: "Please, compile this program with the --threads:on option!".} -const - ServerProtocols = @["/test-chat-stream"] +const ServerProtocols = @["/test-chat-stream"] -type - CustomData = ref object - api: DaemonAPI - remotes: seq[StreamTransport] - consoleFd: AsyncFD - serveFut: Future[void] +type CustomData = ref object + api: DaemonAPI + remotes: seq[StreamTransport] + consoleFd: AsyncFD + serveFut: Future[void] proc threadMain(wfd: AsyncFD) {.thread.} = ## This procedure performs reading from `stdin` and sends data over @@ -82,7 +80,7 @@ proc serveThread(udata: CustomData) {.async.} = relay = true break if relay: - echo peer.pretty(), " * ", " [", addresses.join(", "), "]" + echo peer.pretty(), " * ", " [", addresses.join(", "), "]" else: echo peer.pretty(), " [", addresses.join(", "), "]" elif line.startsWith("/exit"): diff --git a/examples/go-daemon/node.nim b/examples/go-daemon/node.nim index 0f3495d807..4f44fe3366 100644 --- a/examples/go-daemon/node.nim +++ b/examples/go-daemon/node.nim @@ -1,26 +1,25 @@ import chronos, nimcrypto, strutils, os import ../../libp2p/daemon/daemonapi -const - PubSubTopic = "test-net" +const PubSubTopic = "test-net" proc main(bn: string) {.async.} = echo "= Starting P2P node" var bootnodes = bn.split(",") - var api = await newDaemonApi({DHTFull, PSGossipSub, WaitBootstrap}, - bootstrapNodes = bootnodes, - peersRequired = 1) + var api = await newDaemonApi( + {DHTFull, PSGossipSub, WaitBootstrap}, bootstrapNodes = bootnodes, peersRequired = 1 + ) var id = await api.identity() echo "= P2P node ", id.peer.pretty(), " started:" for item in id.addresses: echo item - proc pubsubLogger(api: DaemonAPI, - ticket: PubsubTicket, - message: PubSubMessage): Future[bool] {.async.} = + proc pubsubLogger( + api: DaemonAPI, ticket: PubsubTicket, message: PubSubMessage + ): Future[bool] {.async.} = let msglen = len(message.data) - echo "= Recieved pubsub message with length ", msglen, - " bytes from peer ", message.peer.pretty(), ": " + echo "= Recieved pubsub message with length ", + msglen, " bytes from peer ", message.peer.pretty(), ": " var strdata = cast[string](message.data) echo strdata result = true diff --git a/examples/helloworld.nim b/examples/helloworld.nim index 1844a96ea2..3c40e1fa4d 100644 --- a/examples/helloworld.nim +++ b/examples/helloworld.nim @@ -1,5 +1,5 @@ -import chronos # an efficient library for async -import stew/byteutils # various utils +import chronos # an efficient library for async +import stew/byteutils # various utils import libp2p ## @@ -7,11 +7,9 @@ import libp2p ## const TestCodec = "/test/proto/1.0.0" # custom protocol string identifier -type - TestProto = ref object of LPProtocol # declare a custom protocol +type TestProto = ref object of LPProtocol # declare a custom protocol proc new(T: typedesc[TestProto]): T = - # every incoming connections will be in handled in this closure proc handle(conn: Connection, proto: string) {.async.} = echo "Got from remote - ", string.fromBytes(await conn.readLp(1024)) @@ -28,11 +26,16 @@ proc new(T: typedesc[TestProto]): T = proc createSwitch(ma: MultiAddress, rng: ref HmacDrbgContext): Switch = var switch = SwitchBuilder .new() - .withRng(rng) # Give the application RNG - .withAddress(ma) # Our local address(es) - .withTcpTransport() # Use TCP as transport - .withMplex() # Use Mplex as muxer - .withNoise() # Use Noise as secure manager + .withRng(rng) + # Give the application RNG + .withAddress(ma) + # Our local address(es) + .withTcpTransport() + # Use TCP as transport + .withMplex() + # Use Mplex as muxer + .withNoise() + # Use Noise as secure manager .build() result = switch @@ -73,7 +76,8 @@ proc main() {.async.} = # use the second node to dial the first node # using the first node peerid and address # and specify our custom protocol codec - let conn = await switch2.dial(switch1.peerInfo.peerId, switch1.peerInfo.addrs, TestCodec) + let conn = + await switch2.dial(switch1.peerInfo.peerId, switch1.peerInfo.addrs, TestCodec) # conn is now a fully setup connection, we talk directly to the node1 custom protocol handler await conn.writeLp("Hello p2p!") # writeLp send a length prefixed buffer over the wire @@ -84,6 +88,7 @@ proc main() {.async.} = # We must close the connection ourselves when we're done with it await conn.close() - await allFutures(switch1.stop(), switch2.stop()) # close connections and shutdown all transports + await allFutures(switch1.stop(), switch2.stop()) + # close connections and shutdown all transports waitFor(main()) diff --git a/examples/hexdump.nim b/examples/hexdump.nim index 2fb8d766fa..15cf4da926 100644 --- a/examples/hexdump.nim +++ b/examples/hexdump.nim @@ -35,26 +35,24 @@ proc dumpHex*(pbytes: pointer, nbytes: int, items = 1, ascii = true): string = var asciiText = "" while i < nbytes: if i %% 16 == 0: - result = result & toHex(cast[BiggestInt](slider), - sizeof(BiggestInt) * 2) & ": " + result = result & toHex(cast[BiggestInt](slider), sizeof(BiggestInt) * 2) & ": " var k = 0 while k < items: var ch = cast[ptr char](cast[uint](slider) + k.uint)[] - if ord(ch) > 31 and ord(ch) < 127: asciiText &= ch else: asciiText &= "." + if ord(ch) > 31 and ord(ch) < 127: + asciiText &= ch + else: + asciiText &= "." inc(k) - case items: + case items of 1: - result = result & toHex(cast[BiggestInt](cast[ptr uint8](slider)[]), - hexSize) + result = result & toHex(cast[BiggestInt](cast[ptr uint8](slider)[]), hexSize) of 2: - result = result & toHex(cast[BiggestInt](cast[ptr uint16](slider)[]), - hexSize) + result = result & toHex(cast[BiggestInt](cast[ptr uint16](slider)[]), hexSize) of 4: - result = result & toHex(cast[BiggestInt](cast[ptr uint32](slider)[]), - hexSize) + result = result & toHex(cast[BiggestInt](cast[ptr uint32](slider)[]), hexSize) of 8: - result = result & toHex(cast[BiggestInt](cast[ptr uint64](slider)[]), - hexSize) + result = result & toHex(cast[BiggestInt](cast[ptr uint64](slider)[]), hexSize) else: raise newException(ValueError, "Wrong items size!") result = result & " " diff --git a/examples/tutorial_1_connect.nim b/examples/tutorial_1_connect.nim index 17e73b2f86..a3dae63e56 100644 --- a/examples/tutorial_1_connect.nim +++ b/examples/tutorial_1_connect.nim @@ -38,11 +38,16 @@ import libp2p/protocols/ping proc createSwitch(ma: MultiAddress, rng: ref HmacDrbgContext): Switch = var switch = SwitchBuilder .new() - .withRng(rng) # Give the application RNG - .withAddress(ma) # Our local address(es) - .withTcpTransport() # Use TCP as transport - .withMplex() # Use Mplex as muxer - .withNoise() # Use Noise as secure manager + .withRng(rng) + # Give the application RNG + .withAddress(ma) + # Our local address(es) + .withTcpTransport() + # Use TCP as transport + .withMplex() + # Use Mplex as muxer + .withNoise() + # Use Noise as secure manager .build() return switch @@ -57,7 +62,7 @@ proc main() {.async.} = let rng = newRng() localAddress = MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet() - pingProtocol = Ping.new(rng=rng) + pingProtocol = Ping.new(rng = rng) ## We created some variables that we'll need for the rest of the application: the global `rng` instance, our `localAddress`, and an instance of the `Ping` protocol. ## The address is in the [MultiAddress](https://github.com/multiformats/multiaddr) format. The port `0` means "take any port available". ## @@ -78,7 +83,8 @@ proc main() {.async.} = ## We can find out which port was attributed, and the resulting local addresses, by using `switch1.peerInfo.addrs`. ## ## We'll **dial** the first switch from the second one, by specifying its **Peer ID**, its **MultiAddress** and the **`Ping` protocol codec**: - let conn = await switch2.dial(switch1.peerInfo.peerId, switch1.peerInfo.addrs, PingCodec) + let conn = + await switch2.dial(switch1.peerInfo.peerId, switch1.peerInfo.addrs, PingCodec) ## We now have a `Ping` connection setup between the second and the first switch, we can use it to actually ping the node: # ping the other node and echo the ping duration echo "ping: ", await pingProtocol.ping(conn) @@ -86,7 +92,8 @@ proc main() {.async.} = # We must close the connection ourselves when we're done with it await conn.close() ## And that's it! Just a little bit of cleanup: shutting down the switches, waiting for them to stop, and we'll call our `main` procedure: - await allFutures(switch1.stop(), switch2.stop()) # close connections and shutdown all transports + await allFutures(switch1.stop(), switch2.stop()) + # close connections and shutdown all transports waitFor(main()) diff --git a/examples/tutorial_2_customproto.nim b/examples/tutorial_2_customproto.nim index 2fff7090c9..8bcb0817b4 100644 --- a/examples/tutorial_2_customproto.nim +++ b/examples/tutorial_2_customproto.nim @@ -48,22 +48,24 @@ proc main() {.async.} = let rng = newRng() testProto = TestProto.new() - switch1 = newStandardSwitch(rng=rng) - switch2 = newStandardSwitch(rng=rng) + switch1 = newStandardSwitch(rng = rng) + switch2 = newStandardSwitch(rng = rng) switch1.mount(testProto) await switch1.start() await switch2.start() - let conn = await switch2.dial(switch1.peerInfo.peerId, switch1.peerInfo.addrs, TestCodec) + let conn = + await switch2.dial(switch1.peerInfo.peerId, switch1.peerInfo.addrs, TestCodec) await testProto.hello(conn) # We must close the connection ourselves when we're done with it await conn.close() - await allFutures(switch1.stop(), switch2.stop()) # close connections and shutdown all transports + await allFutures(switch1.stop(), switch2.stop()) + # close connections and shutdown all transports ## This is very similar to the first tutorial's `main`, the only noteworthy difference is that we use `newStandardSwitch`, which is similar to the `createSwitch` of the first tutorial, but is bundled directly in libp2p ## diff --git a/examples/tutorial_3_protobuf.nim b/examples/tutorial_3_protobuf.nim index 7df5cf409b..a0a95d5264 100644 --- a/examples/tutorial_3_protobuf.nim +++ b/examples/tutorial_3_protobuf.nim @@ -57,8 +57,8 @@ proc decode(_: type Metric, buf: seq[byte]): Result[Metric, ProtoError] = # # We are just checking the error, and ignoring whether the value # is present or not (default values are valid). - discard ? pb.getField(1, res.name) - discard ? pb.getField(2, res.value) + discard ?pb.getField(1, res.name) + discard ?pb.getField(2, res.value) ok(res) proc encode(m: MetricList): ProtoBuffer = @@ -72,10 +72,10 @@ proc decode(_: type MetricList, buf: seq[byte]): Result[MetricList, ProtoError] res: MetricList metrics: seq[seq[byte]] let pb = initProtoBuffer(buf) - discard ? pb.getRepeatedField(1, metrics) + discard ?pb.getRepeatedField(1, metrics) for metric in metrics: - res.metrics &= ? Metric.decode(metric) + res.metrics &= ?Metric.decode(metric) ok(res) ## ## Results instead of exceptions @@ -102,7 +102,7 @@ proc decode(_: type MetricList, buf: seq[byte]): Result[MetricList, ProtoError] ## ## Creating the protocol ## We'll next create a protocol, like in the last tutorial, to request these metrics from our host type - MetricCallback = proc: Future[MetricList] {.raises: [], gcsafe.} + MetricCallback = proc(): Future[MetricList] {.raises: [], gcsafe.} MetricProto = ref object of LPProtocol metricGetter: MetricCallback @@ -128,19 +128,19 @@ proc fetch(p: MetricProto, conn: Connection): Future[MetricList] {.async.} = ## We can now create our main procedure: proc main() {.async.} = let rng = newRng() - proc randomMetricGenerator: Future[MetricList] {.async.} = + proc randomMetricGenerator(): Future[MetricList] {.async.} = let metricCount = rng[].generate(uint32) mod 16 for i in 0 ..< metricCount + 1: - result.metrics.add(Metric( - name: "metric_" & $i, - value: float(rng[].generate(uint16)) / 1000.0 - )) + result.metrics.add( + Metric(name: "metric_" & $i, value: float(rng[].generate(uint16)) / 1000.0) + ) return result + let metricProto1 = MetricProto.new(randomMetricGenerator) metricProto2 = MetricProto.new(randomMetricGenerator) - switch1 = newStandardSwitch(rng=rng) - switch2 = newStandardSwitch(rng=rng) + switch1 = newStandardSwitch(rng = rng) + switch2 = newStandardSwitch(rng = rng) switch1.mount(metricProto1) @@ -148,14 +148,17 @@ proc main() {.async.} = await switch2.start() let - conn = await switch2.dial(switch1.peerInfo.peerId, switch1.peerInfo.addrs, metricProto2.codecs) + conn = await switch2.dial( + switch1.peerInfo.peerId, switch1.peerInfo.addrs, metricProto2.codecs + ) metrics = await metricProto2.fetch(conn) await conn.close() for metric in metrics.metrics: echo metric.name, " = ", metric.value - await allFutures(switch1.stop(), switch2.stop()) # close connections and shutdown all transports + await allFutures(switch1.stop(), switch2.stop()) + # close connections and shutdown all transports waitFor(main()) diff --git a/examples/tutorial_4_gossipsub.nim b/examples/tutorial_4_gossipsub.nim index abb10f2f39..d51cdfdf89 100644 --- a/examples/tutorial_4_gossipsub.nim +++ b/examples/tutorial_4_gossipsub.nim @@ -40,8 +40,8 @@ proc encode(m: Metric): ProtoBuffer = proc decode(_: type Metric, buf: seq[byte]): Result[Metric, ProtoError] = var res: Metric let pb = initProtoBuffer(buf) - discard ? pb.getField(1, res.name) - discard ? pb.getField(2, res.value) + discard ?pb.getField(1, res.name) + discard ?pb.getField(2, res.value) ok(res) proc encode(m: MetricList): ProtoBuffer = @@ -56,11 +56,11 @@ proc decode(_: type MetricList, buf: seq[byte]): Result[MetricList, ProtoError] res: MetricList metrics: seq[seq[byte]] let pb = initProtoBuffer(buf) - discard ? pb.getRepeatedField(1, metrics) + discard ?pb.getRepeatedField(1, metrics) for metric in metrics: - res.metrics &= ? Metric.decode(metric) - ? pb.getRequiredField(2, res.hostname) + res.metrics &= ?Metric.decode(metric) + ?pb.getRequiredField(2, res.hostname) ok(res) ## This is exactly like the previous structure, except that we added @@ -73,11 +73,14 @@ type Node = tuple[switch: Switch, gossip: GossipSub, hostname: string] proc oneNode(node: Node, rng: ref HmacDrbgContext) {.async.} = # This procedure will handle one of the node of the network - node.gossip.addValidator(["metrics"], + node.gossip.addValidator( + ["metrics"], proc(topic: string, message: Message): Future[ValidationResult] {.async.} = let decoded = MetricList.decode(message.data) - if decoded.isErr: return ValidationResult.Reject + if decoded.isErr: + return ValidationResult.Reject return ValidationResult.Accept + , ) # This "validator" will attach to the `metrics` topic and make sure # that every message in this topic is valid. This allows us to stop @@ -87,23 +90,24 @@ proc oneNode(node: Node, rng: ref HmacDrbgContext) {.async.} = # `John` will be responsible to log the metrics, the rest of the nodes # will just forward them in the network if node.hostname == "John": - node.gossip.subscribe("metrics", - proc (topic: string, data: seq[byte]) {.async.} = + node.gossip.subscribe( + "metrics", + proc(topic: string, data: seq[byte]) {.async.} = echo MetricList.decode(data).tryGet() + , ) else: node.gossip.subscribe("metrics", nil) # Create random metrics 10 times and broadcast them - for _ in 0..<10: + for _ in 0 ..< 10: await sleepAsync(500.milliseconds) var metricList = MetricList(hostname: node.hostname) let metricCount = rng[].generate(uint32) mod 4 for i in 0 ..< metricCount + 1: - metricList.metrics.add(Metric( - name: "metric_" & $i, - value: float(rng[].generate(uint16)) / 1000.0 - )) + metricList.metrics.add( + Metric(name: "metric_" & $i, value: float(rng[].generate(uint16)) / 1000.0) + ) discard await node.gossip.publish("metrics", encode(metricList).buffer) await node.switch.stop() @@ -111,13 +115,13 @@ proc oneNode(node: Node, rng: ref HmacDrbgContext) {.async.} = ## For our main procedure, we'll create a few nodes, and connect them together. ## Note that they are not all interconnected, but GossipSub will take care of ## broadcasting to the full network nonetheless. -proc main {.async.} = +proc main() {.async.} = let rng = newRng() var nodes: seq[Node] for hostname in ["John", "Walter", "David", "Thuy", "Amy"]: let - switch = newStandardSwitch(rng=rng) + switch = newStandardSwitch(rng = rng) gossip = GossipSub.init(switch = switch, triggerSelf = true) switch.mount(gossip) await switch.start() @@ -127,11 +131,12 @@ proc main {.async.} = for index, node in nodes: # Connect to a few neighbors for otherNodeIdx in index - 1 .. index + 2: - if otherNodeIdx notin 0 ..< nodes.len or otherNodeIdx == index: continue + if otherNodeIdx notin 0 ..< nodes.len or otherNodeIdx == index: + continue let otherNode = nodes[otherNodeIdx] await node.switch.connect( - otherNode.switch.peerInfo.peerId, - otherNode.switch.peerInfo.addrs) + otherNode.switch.peerInfo.peerId, otherNode.switch.peerInfo.addrs + ) var allFuts: seq[Future[void]] for node in nodes: diff --git a/examples/tutorial_5_discovery.nim b/examples/tutorial_5_discovery.nim index a059d1ca30..9e0f99e6a7 100644 --- a/examples/tutorial_5_discovery.nim +++ b/examples/tutorial_5_discovery.nim @@ -20,14 +20,15 @@ import libp2p/discovery/discoverymngr ## ## Note that other discovery methods such as [Kademlia](https://github.com/libp2p/specs/blob/master/kad-dht/README.md) or [discv5](https://github.com/ethereum/devp2p/blob/master/discv5/discv5.md) exist. proc createSwitch(rdv: RendezVous = RendezVous.new()): Switch = - SwitchBuilder.new() - .withRng(newRng()) - .withAddresses(@[ MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet() ]) - .withTcpTransport() - .withYamux() - .withNoise() - .withRendezVous(rdv) - .build() + SwitchBuilder + .new() + .withRng(newRng()) + .withAddresses(@[MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet()]) + .withTcpTransport() + .withYamux() + .withNoise() + .withRendezVous(rdv) + .build() # Create a really simple protocol to log one message received then close the stream const DumbCodec = "/dumb/proto/1.0.0" @@ -36,6 +37,7 @@ proc new(T: typedesc[DumbProto], nodeNumber: int): T = proc handle(conn: Connection, proto: string) {.async.} = echo "Node", nodeNumber, " received: ", string.fromBytes(await conn.readLp(1024)) await conn.close() + return T.new(codecs = @[DumbCodec], handler = handle) ## ## Bootnodes @@ -58,7 +60,7 @@ proc main() {.async.} = switches: seq[Switch] = @[] discManagers: seq[DiscoveryManager] = @[] - for i in 0..5: + for i in 0 .. 5: let rdv = RendezVous.new() # Create a remote future to await at the end of the program let switch = createSwitch(rdv) @@ -93,7 +95,7 @@ proc main() {.async.} = # Use the discovery manager to find peers on the OddClub topic to greet them let queryOddClub = dm.request(RdvNamespace("OddClub")) - for _ in 0..2: + for _ in 0 .. 2: let # getPeer give you a PeerAttribute containing informations about the peer. res = await queryOddClub.getPeer() @@ -109,7 +111,7 @@ proc main() {.async.} = # Maybe it was because he wanted to join the EvenGang let queryEvenGang = dm.request(RdvNamespace("EvenGang")) - for _ in 0..2: + for _ in 0 .. 2: let res = await queryEvenGang.getPeer() conn = await newcomer.dial(res[PeerId], res.getAll(MultiAddress), DumbCodec) diff --git a/examples/tutorial_6_game.nim b/examples/tutorial_6_game.nim index 046c5b3244..996494b266 100644 --- a/examples/tutorial_6_game.nim +++ b/examples/tutorial_6_game.nim @@ -51,7 +51,7 @@ proc new(_: type[Game]): Game = tickTime: -3.0, # 3 seconds of "warm-up" time localPlayer: Player(x: 4, y: 16, currentDir: 3, nextDir: 3, color: 8), remotePlayer: Player(x: 27, y: 16, currentDir: 1, nextDir: 1, color: 12), - peerFound: newFuture[Connection]() + peerFound: newFuture[Connection](), ) for pos in 0 .. result.gameMap.high: if pos mod mapSize in [0, mapSize - 1] or pos div mapSize in [0, mapSize - 1]: @@ -81,7 +81,8 @@ proc update(g: Game, dt: float32) = # This is a hacky way to make this happen waitFor(sleepAsync(1.milliseconds)) # Don't do anything if we are still waiting for an opponent - if not(g.peerFound.finished()) or isNil(g.tickFinished): return + if not (g.peerFound.finished()) or isNil(g.tickFinished): + return g.tickTime += dt # Update the wanted direction, making sure we can't go backward @@ -98,7 +99,8 @@ proc tick(g: Game, p: Player) = # Move player and check if he lost p.x += directions[p.currentDir][1] p.y += directions[p.currentDir][2] - if g.gameMap[p.y * mapSize + p.x] != 0: p.lost = true + if g.gameMap[p.y * mapSize + p.x] != 0: + p.lost = true g.gameMap[p.y * mapSize + p.x] = p.color proc mainLoop(g: Game, peer: Connection) {.async.} = @@ -123,16 +125,23 @@ proc draw(g: Game) = for pos, color in g.gameMap: setColor(color) boxFill(pos mod 32 * 4, pos div 32 * 4, 4, 4) - let text = if not(g.peerFound.finished()): "Matchmaking.." - elif g.tickTime < -1.5: "Welcome to Etron" - elif g.tickTime < 0.0: "- " & $(int(abs(g.tickTime) / 0.5) + 1) & " -" - elif g.remotePlayer.lost and g.localPlayer.lost: "DEUCE" - elif g.localPlayer.lost: "YOU LOOSE" - elif g.remotePlayer.lost: "YOU WON" - else: "" + let text = + if not (g.peerFound.finished()): + "Matchmaking.." + elif g.tickTime < -1.5: + "Welcome to Etron" + elif g.tickTime < 0.0: + "- " & $(int(abs(g.tickTime) / 0.5) + 1) & " -" + elif g.remotePlayer.lost and g.localPlayer.lost: + "DEUCE" + elif g.localPlayer.lost: + "YOU LOOSE" + elif g.remotePlayer.lost: + "YOU WON" + else: + "" printc(text, screenWidth div 2, screenHeight div 2) - ## ## Matchmaking ## To find an opponent, we will broadcast our address on a ## GossipSub topic, and wait for someone to connect to us. @@ -144,7 +153,8 @@ proc draw(g: Game) = ## and launch the game. proc new(T: typedesc[GameProto], g: Game): T = proc handle(conn: Connection, proto: string) {.async.} = - defer: await conn.closeWithEof() + defer: + await conn.closeWithEof() if g.peerFound.finished or g.hasCandidate: await conn.close() return @@ -157,6 +167,7 @@ proc new(T: typedesc[GameProto], g: Game): T = # The handler of a protocol must wait for the stream to # be finished before returning await conn.join() + return T.new(codecs = @["/tron/1.0.0"], handler = handle) proc networking(g: Game) {.async.} = @@ -164,9 +175,10 @@ proc networking(g: Game) {.async.} = # the Discovery examples combined let rdv = RendezVous.new() - switch = SwitchBuilder.new() + switch = SwitchBuilder + .new() .withRng(newRng()) - .withAddresses(@[ MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet() ]) + .withAddresses(@[MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet()]) .withTcpTransport() .withYamux() .withNoise() @@ -174,9 +186,7 @@ proc networking(g: Game) {.async.} = .build() dm = DiscoveryManager() gameProto = GameProto.new(g) - gossip = GossipSub.init( - switch = switch, - triggerSelf = false) + gossip = GossipSub.init(switch = switch, triggerSelf = false) dm.add(RendezVousInterface.new(rdv)) switch.mount(gossip) @@ -184,10 +194,11 @@ proc networking(g: Game) {.async.} = gossip.subscribe( "/tron/matchmaking", - proc (topic: string, data: seq[byte]) {.async.} = + proc(topic: string, data: seq[byte]) {.async.} = # If we are still looking for an opponent, # try to match anyone broadcasting its address - if g.peerFound.finished or g.hasCandidate: return + if g.peerFound.finished or g.hasCandidate: + return g.hasCandidate = true try: @@ -204,10 +215,12 @@ proc networking(g: Game) {.async.} = swap(g.localPlayer, g.remotePlayer) except CatchableError as exc: discard + , ) await switch.start() - defer: await switch.stop() + defer: + await switch.stop() # As explained in the last tutorial, we need a bootnode to be able # to find peers. We could use any libp2p running rendezvous (or any @@ -243,7 +256,8 @@ proc networking(g: Game) {.async.} = # We now wait for someone to connect to us (or for us to connect to someone) let peerConn = await g.peerFound - defer: await peerConn.closeWithEof() + defer: + await peerConn.closeWithEof() await g.mainLoop(peerConn) @@ -252,7 +266,17 @@ let netFut = networking(game) nico.init("Status", "Tron") nico.createWindow("Tron", mapSize * 4, mapSize * 4, 4, false) -nico.run(proc = discard, proc(dt: float32) = game.update(dt), proc = game.draw()) +nico.run( + proc() = + discard + , + proc(dt: float32) = + game.update(dt) + , + proc() = + game.draw() + , +) waitFor(netFut.cancelAndWait()) ## And that's it! If you want to run this code locally, the simplest way is to use the diff --git a/libp2p.nim b/libp2p.nim index 093f13274d..cbfcac9752 100644 --- a/libp2p.nim +++ b/libp2p.nim @@ -21,7 +21,8 @@ when defined(nimdoc): ## that can help you get started. # Import stuff for doc - import libp2p/[ + import + libp2p/[ protobuf/minprotobuf, switch, stream/lpstream, @@ -33,37 +34,39 @@ when defined(nimdoc): peerid, peerinfo, peerstore, - multiaddress] + multiaddress, + ] proc dummyPrivateProc*() = ## A private proc example discard + else: import - libp2p/[protobuf/minprotobuf, - muxers/muxer, - muxers/mplex/mplex, - stream/lpstream, - stream/bufferstream, - stream/connection, - transports/transport, - transports/tcptransport, - protocols/secure/noise, - cid, - multihash, - multicodec, - errors, - switch, - peerid, - peerinfo, - multiaddress, - builders, - crypto/crypto, - protocols/pubsub] + libp2p/[ + protobuf/minprotobuf, + muxers/muxer, + muxers/mplex/mplex, + stream/lpstream, + stream/bufferstream, + stream/connection, + transports/transport, + transports/tcptransport, + protocols/secure/noise, + cid, + multihash, + multicodec, + errors, + switch, + peerid, + peerinfo, + multiaddress, + builders, + crypto/crypto, + protocols/pubsub, + ] export - minprotobuf, switch, peerid, peerinfo, - connection, multiaddress, crypto, lpstream, - bufferstream, muxer, mplex, transport, - tcptransport, noise, errors, cid, multihash, + minprotobuf, switch, peerid, peerinfo, connection, multiaddress, crypto, lpstream, + bufferstream, muxer, mplex, transport, tcptransport, noise, errors, cid, multihash, multicodec, builders, pubsub diff --git a/libp2p.nimble b/libp2p.nimble index 214a6e00ac..95dd85c43a 100644 --- a/libp2p.nimble +++ b/libp2p.nimble @@ -1,23 +1,16 @@ mode = ScriptMode.Verbose -packageName = "libp2p" -version = "1.3.0" -author = "Status Research & Development GmbH" -description = "LibP2P implementation" -license = "MIT" -skipDirs = @["tests", "examples", "Nim", "tools", "scripts", "docs"] +packageName = "libp2p" +version = "1.3.0" +author = "Status Research & Development GmbH" +description = "LibP2P implementation" +license = "MIT" +skipDirs = @["tests", "examples", "Nim", "tools", "scripts", "docs"] requires "nim >= 1.6.0", - "nimcrypto >= 0.4.1", - "dnsclient >= 0.3.0 & < 0.4.0", - "bearssl >= 0.1.4", - "chronicles >= 0.10.2", - "chronos >= 4.0.0", - "metrics", - "secp256k1", - "stew#head", - "websock", - "unittest2" + "nimcrypto >= 0.4.1", "dnsclient >= 0.3.0 & < 0.4.0", "bearssl >= 0.1.4", + "chronicles >= 0.10.2", "chronos >= 4.0.0", "metrics", "secp256k1", "stew#head", + "websock", "unittest2" let nimc = getEnv("NIMC", "nim") # Which nim compiler to use let lang = getEnv("NIMLANG", "c") # Which backend (c/cpp/js) @@ -27,13 +20,13 @@ let verbose = getEnv("V", "") notin ["", "0"] let cfg = " --styleCheck:usages --styleCheck:error" & (if verbose: "" else: " --verbosity:0 --hints:off") & - " --skipParentCfg --skipUserCfg -f" & - " --threads:on --opt:speed" + " --skipParentCfg --skipUserCfg -f" & " --threads:on --opt:speed" import hashes, strutils -proc runTest(filename: string, verify: bool = true, sign: bool = true, - moreoptions: string = "") = +proc runTest( + filename: string, verify: bool = true, sign: bool = true, moreoptions: string = "" +) = var excstr = nimc & " " & lang & " -d:debug " & cfg & " " & flags excstr.add(" -d:libp2p_pubsub_sign=" & $sign) excstr.add(" -d:libp2p_pubsub_verify=" & $verify) @@ -52,7 +45,8 @@ proc buildSample(filename: string, run = false, extraFlags = "") = rmFile "examples/" & filename.toExe proc tutorialToMd(filename: string) = - let markdown = gorge "cat " & filename & " | " & nimc & " " & lang & " -r --verbosity:0 --hints:off tools/markdown_builder.nim " + let markdown = gorge "cat " & filename & " | " & nimc & " " & lang & + " -r --verbosity:0 --hints:off tools/markdown_builder.nim " writeFile(filename.replace(".nim", ".md"), markdown) task testnative, "Runs libp2p native tests": @@ -65,24 +59,37 @@ task testinterop, "Runs interop tests": runTest("testinterop") task testpubsub, "Runs pubsub tests": - runTest("pubsub/testgossipinternal", sign = false, verify = false, moreoptions = "-d:pubsub_internal_testing") + runTest( + "pubsub/testgossipinternal", + sign = false, + verify = false, + moreoptions = "-d:pubsub_internal_testing", + ) runTest("pubsub/testpubsub") runTest("pubsub/testpubsub", sign = false, verify = false) - runTest("pubsub/testpubsub", sign = false, verify = false, moreoptions = "-d:libp2p_pubsub_anonymize=true") + runTest( + "pubsub/testpubsub", + sign = false, + verify = false, + moreoptions = "-d:libp2p_pubsub_anonymize=true", + ) task testpubsub_slim, "Runs pubsub tests": - runTest("pubsub/testgossipinternal", sign = false, verify = false, moreoptions = "-d:pubsub_internal_testing") + runTest( + "pubsub/testgossipinternal", + sign = false, + verify = false, + moreoptions = "-d:pubsub_internal_testing", + ) runTest("pubsub/testpubsub") task testfilter, "Run PKI filter test": - runTest("testpkifilter", - moreoptions = "-d:libp2p_pki_schemes=\"secp256k1\"") - runTest("testpkifilter", - moreoptions = "-d:libp2p_pki_schemes=\"secp256k1;ed25519\"") - runTest("testpkifilter", - moreoptions = "-d:libp2p_pki_schemes=\"secp256k1;ed25519;ecnist\"") - runTest("testpkifilter", - moreoptions = "-d:libp2p_pki_schemes=") + runTest("testpkifilter", moreoptions = "-d:libp2p_pki_schemes=\"secp256k1\"") + runTest("testpkifilter", moreoptions = "-d:libp2p_pki_schemes=\"secp256k1;ed25519\"") + runTest( + "testpkifilter", moreoptions = "-d:libp2p_pki_schemes=\"secp256k1;ed25519;ecnist\"" + ) + runTest("testpkifilter", moreoptions = "-d:libp2p_pki_schemes=") task test, "Runs the test suite": exec "nimble testnative" @@ -117,7 +124,8 @@ task examples_build, "Build the samples": buildSample("tutorial_3_protobuf", true) buildSample("tutorial_4_gossipsub", true) buildSample("tutorial_5_discovery", true) - exec "nimble install -y nimpng@#HEAD" # this is to fix broken build on 1.7.3, remove it when nimpng version 0.3.2 or later is released + exec "nimble install -y nimpng@#HEAD" + # this is to fix broken build on 1.7.3, remove it when nimpng version 0.3.2 or later is released exec "nimble install -y nico" buildSample("tutorial_6_game", false, "--styleCheck:off") @@ -135,7 +143,9 @@ task pin, "Create a lockfile": import sequtils import os task install_pinned, "Reads the lockfile": - let toInstall = readFile(PinFile).splitWhitespace().mapIt((it.split(";", 1)[0], it.split(";", 1)[1])) + let toInstall = readFile(PinFile).splitWhitespace().mapIt( + (it.split(";", 1)[0], it.split(";", 1)[1]) + ) # [('packageName', 'packageFullUri')] rmDir("nimbledeps") @@ -145,8 +155,7 @@ task install_pinned, "Reads the lockfile": # Remove the automatically installed deps # (inefficient you say?) let nimblePkgs = - if system.dirExists("nimbledeps/pkgs"): "nimbledeps/pkgs" - else: "nimbledeps/pkgs2" + if system.dirExists("nimbledeps/pkgs"): "nimbledeps/pkgs" else: "nimbledeps/pkgs2" for dependency in listDirs(nimblePkgs): let fileName = dependency.extractFilename @@ -154,14 +163,12 @@ task install_pinned, "Reads the lockfile": packageName = fileName.split('-')[0] if toInstall.anyIt( - it[0] == packageName and - ( - it[1].split('#')[^1] in fileContent or # nimble for nim 2.X - fileName.endsWith(it[1].split('#')[^1]) # nimble for nim 1.X - ) - ) == false or - fileName.split('-')[^1].len < 20: # safegard for nimble for nim 1.X - rmDir(dependency) + it[0] == packageName and ( + it[1].split('#')[^1] in fileContent or # nimble for nim 2.X + fileName.endsWith(it[1].split('#')[^1]) # nimble for nim 1.X + ) + ) == false or fileName.split('-')[^1].len < 20: # safegard for nimble for nim 1.X + rmDir(dependency) task unpin, "Restore global package use": rmDir("nimbledeps") diff --git a/libp2p/builders.nim b/libp2p/builders.nim index f5293573c4..d1623f108e 100644 --- a/libp2p/builders.nim +++ b/libp2p/builders.nim @@ -9,30 +9,33 @@ ## This module contains a Switch Building helper. runnableExamples: - let switch = - SwitchBuilder.new() - .withRng(rng) - .withAddresses(multiaddress) - # etc - .build() + let switch = SwitchBuilder.new().withRng(rng).withAddresses(multiaddress) + # etc + .build() {.push raises: [].} +import options, tables, chronos, chronicles, sequtils import - options, tables, chronos, chronicles, sequtils -import - switch, peerid, peerinfo, stream/connection, multiaddress, - crypto/crypto, transports/[transport, tcptransport], + switch, + peerid, + peerinfo, + stream/connection, + multiaddress, + crypto/crypto, + transports/[transport, tcptransport], muxers/[muxer, mplex/mplex, yamux/yamux], protocols/[identify, secure/secure, secure/noise, rendezvous], protocols/connectivity/[autonat/server, relay/relay, relay/client, relay/rtransport], - connmanager, upgrademngrs/muxedupgrade, observedaddrmanager, + connmanager, + upgrademngrs/muxedupgrade, + observedaddrmanager, nameresolving/nameresolver, - errors, utility + errors, + utility import services/wildcardresolverservice -export - switch, peerid, peerinfo, connection, multiaddress, crypto, errors +export switch, peerid, peerinfo, connection, multiaddress, crypto, errors type TransportProvider* {.public.} = proc(upgr: Upgrade): Transport {.gcsafe, raises: [].} @@ -66,9 +69,8 @@ type proc new*(T: type[SwitchBuilder]): T {.public.} = ## Creates a SwitchBuilder - let address = MultiAddress - .init("/ip4/127.0.0.1/tcp/0") - .expect("Should initialize to default") + let address = + MultiAddress.init("/ip4/127.0.0.1/tcp/0").expect("Should initialize to default") SwitchBuilder( privKey: none(PrivateKey), @@ -80,23 +82,30 @@ proc new*(T: type[SwitchBuilder]): T {.public.} = maxConnsPerPeer: MaxConnectionsPerPeer, protoVersion: ProtoVersion, agentVersion: AgentVersion, - enableWildcardResolver: true) + enableWildcardResolver: true, + ) -proc withPrivateKey*(b: SwitchBuilder, privateKey: PrivateKey): SwitchBuilder {.public.} = +proc withPrivateKey*( + b: SwitchBuilder, privateKey: PrivateKey +): SwitchBuilder {.public.} = ## Set the private key of the switch. Will be used to ## generate a PeerId b.privKey = some(privateKey) b -proc withAddresses*(b: SwitchBuilder, addresses: seq[MultiAddress], enableWildcardResolver: bool = true): SwitchBuilder {.public.} = +proc withAddresses*( + b: SwitchBuilder, addresses: seq[MultiAddress], enableWildcardResolver: bool = true +): SwitchBuilder {.public.} = ## | Set the listening addresses of the switch ## | Calling it multiple time will override the value b.addresses = addresses b.enableWildcardResolver = enableWildcardResolver b -proc withAddress*(b: SwitchBuilder, address: MultiAddress, enableWildcardResolver: bool = true): SwitchBuilder {.public.} = +proc withAddress*( + b: SwitchBuilder, address: MultiAddress, enableWildcardResolver: bool = true +): SwitchBuilder {.public.} = ## | Set the listening address of the switch ## | Calling it multiple time will override the value b.withAddresses(@[address], enableWildcardResolver) @@ -106,27 +115,23 @@ proc withSignedPeerRecord*(b: SwitchBuilder, sendIt = true): SwitchBuilder {.pub b proc withMplex*( - b: SwitchBuilder, - inTimeout = 5.minutes, - outTimeout = 5.minutes, - maxChannCount = 200): SwitchBuilder {.public.} = + b: SwitchBuilder, inTimeout = 5.minutes, outTimeout = 5.minutes, maxChannCount = 200 +): SwitchBuilder {.public.} = ## | Uses `Mplex `_ as a multiplexer ## | `Timeout` is the duration after which a inactive connection will be closed proc newMuxer(conn: Connection): Muxer = - Mplex.new( - conn, - inTimeout, - outTimeout, - maxChannCount) + Mplex.new(conn, inTimeout, outTimeout, maxChannCount) assert b.muxers.countIt(it.codec == MplexCodec) == 0, "Mplex build multiple times" b.muxers.add(MuxerProvider.new(newMuxer, MplexCodec)) b -proc withYamux*(b: SwitchBuilder, +proc withYamux*( + b: SwitchBuilder, windowSize: int = YamuxDefaultWindowSize, inTimeout: Duration = 5.minutes, - outTimeout: Duration = 5.minutes): SwitchBuilder = + outTimeout: Duration = 5.minutes, +): SwitchBuilder = proc newMuxer(conn: Connection): Muxer = Yamux.new(conn, windowSize, inTimeout = inTimeout, outTimeout = outTimeout) @@ -138,24 +143,36 @@ proc withNoise*(b: SwitchBuilder): SwitchBuilder {.public.} = b.secureManagers.add(SecureProtocol.Noise) b -proc withTransport*(b: SwitchBuilder, prov: TransportProvider): SwitchBuilder {.public.} = +proc withTransport*( + b: SwitchBuilder, prov: TransportProvider +): SwitchBuilder {.public.} = ## Use a custom transport runnableExamples: - let switch = - SwitchBuilder.new() - .withTransport(proc(upgr: Upgrade): Transport = TcpTransport.new(flags, upgr)) + let switch = SwitchBuilder + .new() + .withTransport( + proc(upgr: Upgrade): Transport = + TcpTransport.new(flags, upgr) + ) .build() b.transports.add(prov) b -proc withTcpTransport*(b: SwitchBuilder, flags: set[ServerFlags] = {}): SwitchBuilder {.public.} = - b.withTransport(proc(upgr: Upgrade): Transport = TcpTransport.new(flags, upgr)) +proc withTcpTransport*( + b: SwitchBuilder, flags: set[ServerFlags] = {} +): SwitchBuilder {.public.} = + b.withTransport( + proc(upgr: Upgrade): Transport = + TcpTransport.new(flags, upgr) + ) proc withRng*(b: SwitchBuilder, rng: ref HmacDrbgContext): SwitchBuilder {.public.} = b.rng = rng b -proc withMaxConnections*(b: SwitchBuilder, maxConnections: int): SwitchBuilder {.public.} = +proc withMaxConnections*( + b: SwitchBuilder, maxConnections: int +): SwitchBuilder {.public.} = ## Maximum concurrent connections of the switch. You should either use this, or ## `withMaxIn <#withMaxIn,SwitchBuilder,int>`_ & `withMaxOut<#withMaxOut,SwitchBuilder,int>`_ b.maxConnections = maxConnections @@ -171,7 +188,9 @@ proc withMaxOut*(b: SwitchBuilder, maxOut: int): SwitchBuilder {.public.} = b.maxOut = maxOut b -proc withMaxConnsPerPeer*(b: SwitchBuilder, maxConnsPerPeer: int): SwitchBuilder {.public.} = +proc withMaxConnsPerPeer*( + b: SwitchBuilder, maxConnsPerPeer: int +): SwitchBuilder {.public.} = b.maxConnsPerPeer = maxConnsPerPeer b @@ -179,15 +198,21 @@ proc withPeerStore*(b: SwitchBuilder, capacity: int): SwitchBuilder {.public.} = b.peerStoreCapacity = Opt.some(capacity) b -proc withProtoVersion*(b: SwitchBuilder, protoVersion: string): SwitchBuilder {.public.} = +proc withProtoVersion*( + b: SwitchBuilder, protoVersion: string +): SwitchBuilder {.public.} = b.protoVersion = protoVersion b -proc withAgentVersion*(b: SwitchBuilder, agentVersion: string): SwitchBuilder {.public.} = +proc withAgentVersion*( + b: SwitchBuilder, agentVersion: string +): SwitchBuilder {.public.} = b.agentVersion = agentVersion b -proc withNameResolver*(b: SwitchBuilder, nameResolver: NameResolver): SwitchBuilder {.public.} = +proc withNameResolver*( + b: SwitchBuilder, nameResolver: NameResolver +): SwitchBuilder {.public.} = b.nameResolver = nameResolver b @@ -199,7 +224,9 @@ proc withCircuitRelay*(b: SwitchBuilder, r: Relay = Relay.new()): SwitchBuilder b.circuitRelay = r b -proc withRendezVous*(b: SwitchBuilder, rdv: RendezVous = RendezVous.new()): SwitchBuilder = +proc withRendezVous*( + b: SwitchBuilder, rdv: RendezVous = RendezVous.new() +): SwitchBuilder = b.rdv = rdv b @@ -207,31 +234,26 @@ proc withServices*(b: SwitchBuilder, services: seq[Service]): SwitchBuilder = b.services = services b -proc withObservedAddrManager*(b: SwitchBuilder, observedAddrManager: ObservedAddrManager): SwitchBuilder = +proc withObservedAddrManager*( + b: SwitchBuilder, observedAddrManager: ObservedAddrManager +): SwitchBuilder = b.observedAddrManager = observedAddrManager b -proc build*(b: SwitchBuilder): Switch - {.raises: [LPError], public.} = - +proc build*(b: SwitchBuilder): Switch {.raises: [LPError], public.} = if b.rng == nil: # newRng could fail raise newException(Defect, "Cannot initialize RNG") let pkRes = PrivateKey.random(b.rng[]) - let - seckey = b.privKey.get(otherwise = pkRes.expect("Expected default Private Key")) + let seckey = b.privKey.get(otherwise = pkRes.expect("Expected default Private Key")) - var - secureManagerInstances: seq[Secure] + var secureManagerInstances: seq[Secure] if SecureProtocol.Noise in b.secureManagers: secureManagerInstances.add(Noise.new(b.rng, seckey).Secure) - let - peerInfo = PeerInfo.new( - seckey, - b.addresses, - protoVersion = b.protoVersion, - agentVersion = b.agentVersion) + let peerInfo = PeerInfo.new( + seckey, b.addresses, protoVersion = b.protoVersion, agentVersion = b.agentVersion + ) let identify = if b.observedAddrManager != nil: @@ -240,16 +262,16 @@ proc build*(b: SwitchBuilder): Switch Identify.new(peerInfo, b.sendSignedPeerRecord) let - connManager = ConnManager.new(b.maxConnsPerPeer, b.maxConnections, b.maxIn, b.maxOut) + connManager = + ConnManager.new(b.maxConnsPerPeer, b.maxConnections, b.maxIn, b.maxOut) ms = MultistreamSelect.new() muxedUpgrade = MuxedUpgrade.new(b.muxers, secureManagerInstances, ms) - let - transports = block: - var transports: seq[Transport] - for tProvider in b.transports: - transports.add(tProvider(muxedUpgrade)) - transports + let transports = block: + var transports: seq[Transport] + for tProvider in b.transports: + transports.add(tProvider(muxedUpgrade)) + transports if b.secureManagers.len == 0: b.secureManagers &= SecureProtocol.Noise @@ -274,7 +296,8 @@ proc build*(b: SwitchBuilder): Switch ms = ms, nameResolver = b.nameResolver, peerStore = peerStore, - services = b.services) + services = b.services, + ) switch.mount(identify) @@ -298,9 +321,7 @@ proc newStandardSwitch*( privKey = none(PrivateKey), addrs: MultiAddress | seq[MultiAddress] = MultiAddress.init("/ip4/127.0.0.1/tcp/0").expect("valid address"), - secureManagers: openArray[SecureProtocol] = [ - SecureProtocol.Noise, - ], + secureManagers: openArray[SecureProtocol] = [SecureProtocol.Noise], transportFlags: set[ServerFlags] = {}, rng = newRng(), inTimeout: Duration = 5.minutes, @@ -311,10 +332,14 @@ proc newStandardSwitch*( maxConnsPerPeer = MaxConnectionsPerPeer, nameResolver: NameResolver = nil, sendSignedPeerRecord = false, - peerStoreCapacity = 1000 + peerStoreCapacity = 1000, ): Switch {.raises: [LPError], public.} = ## Helper for common switch configurations. - let addrs = when addrs is MultiAddress: @[addrs] else: addrs + let addrs = + when addrs is MultiAddress: + @[addrs] + else: + addrs var b = SwitchBuilder .new() .withAddresses(addrs) @@ -324,7 +349,7 @@ proc newStandardSwitch*( .withMaxIn(maxIn) .withMaxOut(maxOut) .withMaxConnsPerPeer(maxConnsPerPeer) - .withPeerStore(capacity=peerStoreCapacity) + .withPeerStore(capacity = peerStoreCapacity) .withMplex(inTimeout, outTimeout) .withTcpTransport(transportFlags) .withNameResolver(nameResolver) diff --git a/libp2p/cid.nim b/libp2p/cid.nim index 203a63ddcb..280f9dc992 100644 --- a/libp2p/cid.nim +++ b/libp2p/cid.nim @@ -19,10 +19,16 @@ export results type CidError* {.pure.} = enum - Error, Incorrect, Unsupported, Overrun + Error + Incorrect + Unsupported + Overrun CidVersion* = enum - CIDvIncorrect, CIDv0, CIDv1, CIDvReserved + CIDvIncorrect + CIDv0 + CIDv1 + CIDvReserved Cid* = object cidver*: CidVersion @@ -30,54 +36,51 @@ type hpos*: int data*: VBuffer -const - ContentIdsList = [ - multiCodec("raw"), - multiCodec("dag-pb"), - multiCodec("dag-cbor"), - multiCodec("dag-json"), - multiCodec("git-raw"), - multiCodec("eth-block"), - multiCodec("eth-block-list"), - multiCodec("eth-tx-trie"), - multiCodec("eth-tx"), - multiCodec("eth-tx-receipt-trie"), - multiCodec("eth-tx-receipt"), - multiCodec("eth-state-trie"), - multiCodec("eth-account-snapshot"), - multiCodec("eth-storage-trie"), - multiCodec("bitcoin-block"), - multiCodec("bitcoin-tx"), - multiCodec("zcash-block"), - multiCodec("zcash-tx"), - multiCodec("stellar-block"), - multiCodec("stellar-tx"), - multiCodec("decred-block"), - multiCodec("decred-tx"), - multiCodec("dash-block"), - multiCodec("dash-tx"), - multiCodec("torrent-info"), - multiCodec("torrent-file"), - multiCodec("ed25519-pub") - ] +const ContentIdsList = [ + multiCodec("raw"), + multiCodec("dag-pb"), + multiCodec("dag-cbor"), + multiCodec("dag-json"), + multiCodec("git-raw"), + multiCodec("eth-block"), + multiCodec("eth-block-list"), + multiCodec("eth-tx-trie"), + multiCodec("eth-tx"), + multiCodec("eth-tx-receipt-trie"), + multiCodec("eth-tx-receipt"), + multiCodec("eth-state-trie"), + multiCodec("eth-account-snapshot"), + multiCodec("eth-storage-trie"), + multiCodec("bitcoin-block"), + multiCodec("bitcoin-tx"), + multiCodec("zcash-block"), + multiCodec("zcash-tx"), + multiCodec("stellar-block"), + multiCodec("stellar-tx"), + multiCodec("decred-block"), + multiCodec("decred-tx"), + multiCodec("dash-block"), + multiCodec("dash-tx"), + multiCodec("torrent-info"), + multiCodec("torrent-file"), + multiCodec("ed25519-pub"), +] proc initCidCodeTable(): Table[int, MultiCodec] {.compileTime.} = for item in ContentIdsList: result[int(item)] = item -const - CodeContentIds = initCidCodeTable() +const CodeContentIds = initCidCodeTable() template orError*(exp: untyped, err: untyped): untyped = - (exp.mapErr do (_: auto) -> auto: err) + exp.mapErr do(_: auto) -> auto: + err proc decode(data: openArray[byte]): Result[Cid, CidError] = if len(data) == 34 and data[0] == 0x12'u8 and data[1] == 0x20'u8: - ok(Cid( - cidver: CIDv0, - mcodec: multiCodec("dag-pb"), - hpos: 0, - data: initVBuffer(data))) + ok( + Cid(cidver: CIDv0, mcodec: multiCodec("dag-pb"), hpos: 0, data: initVBuffer(data)) + ) else: var version, codec: uint64 var res, offset: int @@ -98,21 +101,18 @@ proc decode(data: openArray[byte]): Result[Cid, CidError] = err(CidError.Incorrect) else: offset += res - var mcodec = CodeContentIds.getOrDefault(cast[int](codec), - InvalidMultiCodec) + var mcodec = + CodeContentIds.getOrDefault(cast[int](codec), InvalidMultiCodec) if mcodec == InvalidMultiCodec: err(CidError.Incorrect) else: - if not MultiHash.validate(vb.buffer.toOpenArray(vb.offset, - vb.buffer.high)): + if not MultiHash.validate( + vb.buffer.toOpenArray(vb.offset, vb.buffer.high) + ): err(CidError.Incorrect) else: vb.finish() - ok(Cid( - cidver: CIDv1, - mcodec: mcodec, - hpos: offset, - data: vb)) + ok(Cid(cidver: CIDv1, mcodec: mcodec, hpos: offset, data: vb)) proc decode(data: openArray[char]): Result[Cid, CidError] = var buffer: seq[byte] @@ -172,7 +172,9 @@ proc mhash*(cid: Cid): Result[MultiHash, CidError] = if cid.cidver notin {CIDv0, CIDv1}: err(CidError.Incorrect) else: - MultiHash.init(cid.data.buffer.toOpenArray(cid.hpos, cid.data.high)).orError(CidError.Incorrect) + MultiHash.init(cid.data.buffer.toOpenArray(cid.hpos, cid.data.high)).orError( + CidError.Incorrect + ) proc contentType*(cid: Cid): Result[MultiCodec, CidError] = ## Returns content type part of CID @@ -185,12 +187,15 @@ proc version*(cid: Cid): CidVersion = ## Returns CID version result = cid.cidver -proc init*[T: char|byte](ctype: typedesc[Cid], data: openArray[T]): Result[Cid, CidError] = +proc init*[T: char | byte]( + ctype: typedesc[Cid], data: openArray[T] +): Result[Cid, CidError] = ## Create new content identifier using array of bytes or string ``data``. decode(data) -proc init*(ctype: typedesc[Cid], version: CidVersion, content: MultiCodec, - hash: MultiHash): Result[Cid, CidError] = +proc init*( + ctype: typedesc[Cid], version: CidVersion, content: MultiCodec, hash: MultiHash +): Result[Cid, CidError] = ## Create new content identifier using content type ``content`` and ## MultiHash ``hash`` using version ``version``. ## @@ -213,8 +218,7 @@ proc init*(ctype: typedesc[Cid], version: CidVersion, content: MultiCodec, res.data.finish() return ok(res) elif version == CIDv1: - let mcodec = CodeContentIds.getOrDefault(cast[int](content), - InvalidMultiCodec) + let mcodec = CodeContentIds.getOrDefault(cast[int](content), InvalidMultiCodec) if mcodec == InvalidMultiCodec: return err(CidError.Incorrect) res.mcodec = mcodec @@ -233,11 +237,9 @@ proc `==`*(a: Cid, b: Cid): bool = ## are equal, ``false`` otherwise. if a.mcodec == b.mcodec: var ah, bh: MultiHash - if MultiHash.decode( - a.data.buffer.toOpenArray(a.hpos, a.data.high), ah).isErr: + if MultiHash.decode(a.data.buffer.toOpenArray(a.hpos, a.data.high), ah).isErr: return false - if MultiHash.decode( - b.data.buffer.toOpenArray(b.hpos, b.data.high), bh).isErr: + if MultiHash.decode(b.data.buffer.toOpenArray(b.hpos, b.data.high), bh).isErr: return false result = (ah == bh) diff --git a/libp2p/connmanager.nim b/libp2p/connmanager.nim index 210dc660b9..dd9a02d299 100644 --- a/libp2p/connmanager.nim +++ b/libp2p/connmanager.nim @@ -11,12 +11,7 @@ import std/[tables, sequtils, sets] import pkg/[chronos, chronicles, metrics] -import peerinfo, - peerstore, - stream/connection, - muxers/muxer, - utils/semaphore, - errors +import peerinfo, peerstore, stream/connection, muxers/muxer, utils/semaphore, errors logScope: topics = "libp2p connmanager" @@ -32,12 +27,13 @@ type AlreadyExpectingConnectionError* = object of LPError ConnEventKind* {.pure.} = enum - Connected, # A connection was made and securely upgraded - there may be - # more than one concurrent connection thus more than one upgrade - # event per peer. - - Disconnected # Peer disconnected - this event is fired once per upgrade - # when the associated connection is terminated. + Connected + # A connection was made and securely upgraded - there may be + # more than one concurrent connection thus more than one upgrade + # event per peer. + Disconnected + # Peer disconnected - this event is fired once per upgrade + # when the associated connection is terminated. ConnEvent* = object case kind*: ConnEventKind @@ -47,19 +43,18 @@ type discard ConnEventHandler* = - proc(peerId: PeerId, event: ConnEvent): Future[void] - {.gcsafe, raises: [].} + proc(peerId: PeerId, event: ConnEvent): Future[void] {.gcsafe, raises: [].} PeerEventKind* {.pure.} = enum - Left, + Left Joined PeerEvent* = object case kind*: PeerEventKind - of PeerEventKind.Joined: - initiator*: bool - else: - discard + of PeerEventKind.Joined: + initiator*: bool + else: + discard PeerEventHandler* = proc(peerId: PeerId, event: PeerEvent): Future[void] {.gcsafe, raises: [].} @@ -81,11 +76,13 @@ type proc newTooManyConnectionsError(): ref TooManyConnectionsError {.inline.} = result = newException(TooManyConnectionsError, "Too many connections") -proc new*(C: type ConnManager, - maxConnsPerPeer = MaxConnectionsPerPeer, - maxConnections = MaxConnections, - maxIn = -1, - maxOut = -1): ConnManager = +proc new*( + C: type ConnManager, + maxConnsPerPeer = MaxConnectionsPerPeer, + maxConnections = MaxConnections, + maxIn = -1, + maxOut = -1, +): ConnManager = var inSema, outSema: AsyncSemaphore if maxIn > 0 or maxOut > 0: inSema = newAsyncSemaphore(maxIn) @@ -96,9 +93,7 @@ proc new*(C: type ConnManager, else: raiseAssert "Invalid connection counts!" - C(maxConnsPerPeer: maxConnsPerPeer, - inSema: inSema, - outSema: outSema) + C(maxConnsPerPeer: maxConnsPerPeer, inSema: inSema, outSema: outSema) proc connCount*(c: ConnManager, peerId: PeerId): int = c.muxed.getOrDefault(peerId).len @@ -113,22 +108,21 @@ proc connectedPeers*(c: ConnManager, dir: Direction): seq[PeerId] = proc getConnections*(c: ConnManager): Table[PeerId, seq[Muxer]] = return c.muxed -proc addConnEventHandler*(c: ConnManager, - handler: ConnEventHandler, - kind: ConnEventKind) = +proc addConnEventHandler*( + c: ConnManager, handler: ConnEventHandler, kind: ConnEventKind +) = ## Add peer event handler - handlers must not raise exceptions! ## - if isNil(handler): return + if isNil(handler): + return c.connEvents[kind].incl(handler) -proc removeConnEventHandler*(c: ConnManager, - handler: ConnEventHandler, - kind: ConnEventKind) = - c.connEvents[kind].excl(handler) +proc removeConnEventHandler*( + c: ConnManager, handler: ConnEventHandler, kind: ConnEventKind +) = + c.connEvents[kind].excl(handler) -proc triggerConnEvent*(c: ConnManager, - peerId: PeerId, - event: ConnEvent) {.async.} = +proc triggerConnEvent*(c: ConnManager, peerId: PeerId, event: ConnEvent) {.async.} = try: trace "About to trigger connection events", peer = peerId if c.connEvents[event.kind].len() > 0: @@ -141,27 +135,24 @@ proc triggerConnEvent*(c: ConnManager, except CancelledError as exc: raise exc except CatchableError as exc: - warn "Exception in triggerConnEvents", - msg = exc.msg, peer = peerId, event = $event + warn "Exception in triggerConnEvents", msg = exc.msg, peer = peerId, event = $event -proc addPeerEventHandler*(c: ConnManager, - handler: PeerEventHandler, - kind: PeerEventKind) = +proc addPeerEventHandler*( + c: ConnManager, handler: PeerEventHandler, kind: PeerEventKind +) = ## Add peer event handler - handlers must not raise exceptions! ## - if isNil(handler): return + if isNil(handler): + return c.peerEvents[kind].incl(handler) -proc removePeerEventHandler*(c: ConnManager, - handler: PeerEventHandler, - kind: PeerEventKind) = +proc removePeerEventHandler*( + c: ConnManager, handler: PeerEventHandler, kind: PeerEventKind +) = c.peerEvents[kind].excl(handler) -proc triggerPeerEvents*(c: ConnManager, - peerId: PeerId, - event: PeerEvent) {.async.} = - +proc triggerPeerEvents*(c: ConnManager, peerId: PeerId, event: PeerEvent) {.async.} = trace "About to trigger peer events", peer = peerId if c.peerEvents[event.kind].len == 0: return @@ -179,11 +170,16 @@ proc triggerPeerEvents*(c: ConnManager, except CatchableError as exc: # handlers should not raise! warn "Exception in triggerPeerEvents", exc = exc.msg, peer = peerId -proc expectConnection*(c: ConnManager, p: PeerId, dir: Direction): Future[Muxer] {.async.} = +proc expectConnection*( + c: ConnManager, p: PeerId, dir: Direction +): Future[Muxer] {.async.} = ## Wait for a peer to connect to us. This will bypass the `MaxConnectionsPerPeer` let key = (p, dir) if key in c.expectedConnectionsOverLimit: - raise newException(AlreadyExpectingConnectionError, "Already expecting an incoming connection from that peer") + raise newException( + AlreadyExpectingConnectionError, + "Already expecting an incoming connection from that peer", + ) let future = newFuture[Muxer]() c.expectedConnectionsOverLimit[key] = future @@ -211,7 +207,7 @@ proc closeMuxer(muxer: Muxer) {.async.} = trace "Cleaning up muxer", m = muxer await muxer.close() - if not(isNil(muxer.handler)): + if not (isNil(muxer.handler)): try: await muxer.handler # TODO noraises? except CatchableError as exc: @@ -231,16 +227,14 @@ proc muxCleanup(c: ConnManager, mux: Muxer) {.async.} = libp2p_peers.set(c.muxed.len.int64) await c.triggerPeerEvents(peerId, PeerEvent(kind: PeerEventKind.Left)) - if not(c.peerStore.isNil): + if not (c.peerStore.isNil): c.peerStore.cleanup(peerId) - await c.triggerConnEvent( - peerId, ConnEvent(kind: ConnEventKind.Disconnected)) + await c.triggerConnEvent(peerId, ConnEvent(kind: ConnEventKind.Disconnected)) except CatchableError as exc: # This is top-level procedure which will work as separate task, so it # do not need to propagate CancelledError and should handle other errors - warn "Unexpected exception peer cleanup handler", - mux, msg = exc.msg + warn "Unexpected exception peer cleanup handler", mux, msg = exc.msg proc onClose(c: ConnManager, mux: Muxer) {.async.} = ## connection close even handler @@ -251,19 +245,14 @@ proc onClose(c: ConnManager, mux: Muxer) {.async.} = await mux.connection.join() trace "Connection closed, cleaning up", mux except CatchableError as exc: - debug "Unexpected exception in connection manager's cleanup", - errMsg = exc.msg, mux + debug "Unexpected exception in connection manager's cleanup", errMsg = exc.msg, mux finally: await c.muxCleanup(mux) -proc selectMuxer*(c: ConnManager, - peerId: PeerId, - dir: Direction): Muxer = +proc selectMuxer*(c: ConnManager, peerId: PeerId, dir: Direction): Muxer = ## Select a connection for the provided peer and direction ## - let conns = toSeq( - c.muxed.getOrDefault(peerId)) - .filterIt( it.connection.dir == dir ) + let conns = toSeq(c.muxed.getOrDefault(peerId)).filterIt(it.connection.dir == dir) if conns.len > 0: return conns[0] @@ -280,9 +269,7 @@ proc selectMuxer*(c: ConnManager, peerId: PeerId): Muxer = trace "connection not found", peerId return mux -proc storeMuxer*(c: ConnManager, - muxer: Muxer) - {.raises: [CatchableError].} = +proc storeMuxer*(c: ConnManager, muxer: Muxer) {.raises: [CatchableError].} = ## store the connection and muxer ## @@ -304,10 +291,9 @@ proc storeMuxer*(c: ConnManager, let key = (peerId, dir) let expectedConn = c.expectedConnectionsOverLimit.getOrDefault(key) if expectedConn != nil and not expectedConn.finished: - expectedConn.complete(muxer) + expectedConn.complete(muxer) else: - debug "Too many connections for peer", - conns = c.muxed.getOrDefault(peerId).len + debug "Too many connections for peer", conns = c.muxed.getOrDefault(peerId).len raise newTooManyConnectionsError() @@ -322,36 +308,39 @@ proc storeMuxer*(c: ConnManager, libp2p_peers.set(c.muxed.len.int64) asyncSpawn c.triggerConnEvent( - peerId, ConnEvent(kind: ConnEventKind.Connected, incoming: dir == Direction.In)) + peerId, ConnEvent(kind: ConnEventKind.Connected, incoming: dir == Direction.In) + ) if newPeer: asyncSpawn c.triggerPeerEvents( - peerId, PeerEvent(kind: PeerEventKind.Joined, initiator: dir == Direction.Out)) + peerId, PeerEvent(kind: PeerEventKind.Joined, initiator: dir == Direction.Out) + ) asyncSpawn c.onClose(muxer) - trace "Stored muxer", - muxer, direction = $muxer.connection.dir, peers = c.muxed.len + trace "Stored muxer", muxer, direction = $muxer.connection.dir, peers = c.muxed.len proc getIncomingSlot*(c: ConnManager): Future[ConnectionSlot] {.async.} = await c.inSema.acquire() return ConnectionSlot(connManager: c, direction: In) -proc getOutgoingSlot*(c: ConnManager, forceDial = false): ConnectionSlot {.raises: [TooManyConnectionsError].} = +proc getOutgoingSlot*( + c: ConnManager, forceDial = false +): ConnectionSlot {.raises: [TooManyConnectionsError].} = if forceDial: c.outSema.forceAcquire() elif not c.outSema.tryAcquire(): - trace "Too many outgoing connections!", count = c.outSema.count, - max = c.outSema.size + trace "Too many outgoing connections!", + count = c.outSema.count, max = c.outSema.size raise newTooManyConnectionsError() return ConnectionSlot(connManager: c, direction: Out) proc slotsAvailable*(c: ConnManager, dir: Direction): int = - case dir: - of Direction.In: - return c.inSema.count - of Direction.Out: - return c.outSema.count + case dir + of Direction.In: + return c.inSema.count + of Direction.Out: + return c.outSema.count proc release*(cs: ConnectionSlot) = if cs.direction == In: @@ -380,30 +369,27 @@ proc trackMuxer*(cs: ConnectionSlot, mux: Muxer) = return cs.trackConnection(mux.connection) -proc getStream*(c: ConnManager, - muxer: Muxer): Future[Connection] {.async.} = +proc getStream*(c: ConnManager, muxer: Muxer): Future[Connection] {.async.} = ## get a muxed stream for the passed muxer ## - if not(isNil(muxer)): + if not (isNil(muxer)): return await muxer.newStream() -proc getStream*(c: ConnManager, - peerId: PeerId): Future[Connection] {.async.} = +proc getStream*(c: ConnManager, peerId: PeerId): Future[Connection] {.async.} = ## get a muxed stream for the passed peer from any connection ## return await c.getStream(c.selectMuxer(peerId)) -proc getStream*(c: ConnManager, - peerId: PeerId, - dir: Direction): Future[Connection] {.async.} = +proc getStream*( + c: ConnManager, peerId: PeerId, dir: Direction +): Future[Connection] {.async.} = ## get a muxed stream for the passed peer from a connection with `dir` ## return await c.getStream(c.selectMuxer(peerId, dir)) - proc dropPeer*(c: ConnManager, peerId: PeerId) {.async.} = ## drop connections and cleanup resources for peer ## @@ -435,4 +421,3 @@ proc close*(c: ConnManager) {.async.} = await closeMuxer(mux) trace "Closed ConnManager" - diff --git a/libp2p/crypto/chacha20poly1305.nim b/libp2p/crypto/chacha20poly1305.nim index bddbed81c9..e1cf22e69f 100644 --- a/libp2p/crypto/chacha20poly1305.nim +++ b/libp2p/crypto/chacha20poly1305.nim @@ -48,17 +48,19 @@ proc intoChaChaPolyTag*(s: openArray[byte]): ChaChaPolyTag = # this is reconciled at runtime # we do this in the global scope / module init -proc encrypt*(_: type[ChaChaPoly], - key: ChaChaPolyKey, - nonce: ChaChaPolyNonce, - tag: var ChaChaPolyTag, - data: var openArray[byte], - aad: openArray[byte]) = - let - ad = if aad.len > 0: - unsafeAddr aad[0] - else: - nil +proc encrypt*( + _: type[ChaChaPoly], + key: ChaChaPolyKey, + nonce: ChaChaPolyNonce, + tag: var ChaChaPolyTag, + data: var openArray[byte], + aad: openArray[byte], +) = + let ad = + if aad.len > 0: + unsafeAddr aad[0] + else: + nil poly1305CtmulRun( unsafeAddr key[0], @@ -69,20 +71,23 @@ proc encrypt*(_: type[ChaChaPoly], uint(aad.len), baseAddr(tag), # cast is required to workaround https://github.com/nim-lang/Nim/issues/13905 - cast[Chacha20Run](chacha20CtRun), - #[encrypt]# 1.cint) + cast[Chacha20Run](chacha20CtRun), #[encrypt]# + 1.cint, + ) -proc decrypt*(_: type[ChaChaPoly], - key: ChaChaPolyKey, - nonce: ChaChaPolyNonce, - tag: var ChaChaPolyTag, - data: var openArray[byte], - aad: openArray[byte]) = - let - ad = if aad.len > 0: - unsafeAddr aad[0] - else: - nil +proc decrypt*( + _: type[ChaChaPoly], + key: ChaChaPolyKey, + nonce: ChaChaPolyNonce, + tag: var ChaChaPolyTag, + data: var openArray[byte], + aad: openArray[byte], +) = + let ad = + if aad.len > 0: + unsafeAddr aad[0] + else: + nil poly1305CtmulRun( unsafeAddr key[0], @@ -93,5 +98,6 @@ proc decrypt*(_: type[ChaChaPoly], uint(aad.len), baseAddr(tag), # cast is required to workaround https://github.com/nim-lang/Nim/issues/13905 - cast[Chacha20Run](chacha20CtRun), - #[decrypt]# 0.cint) + cast[Chacha20Run](chacha20CtRun), #[decrypt]# + 0.cint, + ) diff --git a/libp2p/crypto/crypto.nim b/libp2p/crypto/crypto.nim index 38d60657f9..1ffcca5d7c 100644 --- a/libp2p/crypto/crypto.nim +++ b/libp2p/crypto/crypto.nim @@ -14,12 +14,11 @@ from strutils import split, strip, cmpIgnoreCase const libp2p_pki_schemes* {.strdefine.} = "rsa,ed25519,secp256k1,ecnist" -type - PKScheme* = enum - RSA = 0, - Ed25519, - Secp256k1, - ECDSA +type PKScheme* = enum + RSA = 0 + Ed25519 + Secp256k1 + ECDSA proc initSupportedSchemes(list: static string): set[PKScheme] = var res: set[PKScheme] @@ -85,7 +84,7 @@ export rijndael, twofish, sha2, hash, hmac, ncrutils, rand type DigestSheme* = enum - Sha256, + Sha256 Sha512 PublicKey* = object @@ -148,15 +147,16 @@ type data*: seq[byte] CryptoError* = enum - KeyError, - SigError, - HashError, + KeyError + SigError + HashError SchemeError CryptoResult*[T] = Result[T, CryptoError] template orError*(exp: untyped, err: untyped): untyped = - (exp.mapErr do (_: auto) -> auto: err) + exp.mapErr do(_: auto) -> auto: + err proc newRng*(): ref HmacDrbgContext = # You should only create one instance of the RNG per application / library @@ -172,11 +172,9 @@ proc newRng*(): ref HmacDrbgContext = return nil rng -proc shuffle*[T]( - rng: ref HmacDrbgContext, - x: var openArray[T]) = - - if x.len == 0: return +proc shuffle*[T](rng: ref HmacDrbgContext, x: var openArray[T]) = + if x.len == 0: + return var randValues = newSeqUninitialized[byte](len(x) * 2) hmacDrbgGenerate(rng[], randValues) @@ -187,9 +185,12 @@ proc shuffle*[T]( y = rand mod i swap(x[i], x[y]) -proc random*(T: typedesc[PrivateKey], scheme: PKScheme, - rng: var HmacDrbgContext, - bits = RsaDefaultKeySize): CryptoResult[PrivateKey] = +proc random*( + T: typedesc[PrivateKey], + scheme: PKScheme, + rng: var HmacDrbgContext, + bits = RsaDefaultKeySize, +): CryptoResult[PrivateKey] = ## Generate random private key for scheme ``scheme``. ## ## ``bits`` is number of bits for RSA key, ``bits`` value must be in @@ -197,7 +198,7 @@ proc random*(T: typedesc[PrivateKey], scheme: PKScheme, case scheme of PKScheme.RSA: when supported(PKScheme.RSA): - let rsakey = ? RsaPrivateKey.random(rng, bits).orError(KeyError) + let rsakey = ?RsaPrivateKey.random(rng, bits).orError(KeyError) ok(PrivateKey(scheme: scheme, rsakey: rsakey)) else: err(SchemeError) @@ -209,7 +210,7 @@ proc random*(T: typedesc[PrivateKey], scheme: PKScheme, err(SchemeError) of PKScheme.ECDSA: when supported(PKScheme.ECDSA): - let eckey = ? ecnist.EcPrivateKey.random(Secp256r1, rng).orError(KeyError) + let eckey = ?ecnist.EcPrivateKey.random(Secp256r1, rng).orError(KeyError) ok(PrivateKey(scheme: scheme, eckey: eckey)) else: err(SchemeError) @@ -220,8 +221,9 @@ proc random*(T: typedesc[PrivateKey], scheme: PKScheme, else: err(SchemeError) -proc random*(T: typedesc[PrivateKey], rng: var HmacDrbgContext, - bits = RsaDefaultKeySize): CryptoResult[PrivateKey] = +proc random*( + T: typedesc[PrivateKey], rng: var HmacDrbgContext, bits = RsaDefaultKeySize +): CryptoResult[PrivateKey] = ## Generate random private key using default public-key cryptography scheme. ## ## Default public-key cryptography schemes are following order: @@ -235,17 +237,20 @@ proc random*(T: typedesc[PrivateKey], rng: var HmacDrbgContext, let skkey = SkPrivateKey.random(rng) ok(PrivateKey(scheme: PKScheme.Secp256k1, skkey: skkey)) elif supported(PKScheme.RSA): - let rsakey = ? RsaPrivateKey.random(rng, bits).orError(KeyError) + let rsakey = ?RsaPrivateKey.random(rng, bits).orError(KeyError) ok(PrivateKey(scheme: PKScheme.RSA, rsakey: rsakey)) elif supported(PKScheme.ECDSA): - let eckey = ? ecnist.EcPrivateKey.random(Secp256r1, rng).orError(KeyError) + let eckey = ?ecnist.EcPrivateKey.random(Secp256r1, rng).orError(KeyError) ok(PrivateKey(scheme: PKScheme.ECDSA, eckey: eckey)) else: err(SchemeError) -proc random*(T: typedesc[KeyPair], scheme: PKScheme, - rng: var HmacDrbgContext, - bits = RsaDefaultKeySize): CryptoResult[KeyPair] = +proc random*( + T: typedesc[KeyPair], + scheme: PKScheme, + rng: var HmacDrbgContext, + bits = RsaDefaultKeySize, +): CryptoResult[KeyPair] = ## Generate random key pair for scheme ``scheme``. ## ## ``bits`` is number of bits for RSA key, ``bits`` value must be in @@ -253,39 +258,52 @@ proc random*(T: typedesc[KeyPair], scheme: PKScheme, case scheme of PKScheme.RSA: when supported(PKScheme.RSA): - let pair = ? RsaKeyPair.random(rng, bits).orError(KeyError) - ok(KeyPair( - seckey: PrivateKey(scheme: scheme, rsakey: pair.seckey), - pubkey: PublicKey(scheme: scheme, rsakey: pair.pubkey))) + let pair = ?RsaKeyPair.random(rng, bits).orError(KeyError) + ok( + KeyPair( + seckey: PrivateKey(scheme: scheme, rsakey: pair.seckey), + pubkey: PublicKey(scheme: scheme, rsakey: pair.pubkey), + ) + ) else: err(SchemeError) of PKScheme.Ed25519: when supported(PKScheme.Ed25519): let pair = EdKeyPair.random(rng) - ok(KeyPair( - seckey: PrivateKey(scheme: scheme, edkey: pair.seckey), - pubkey: PublicKey(scheme: scheme, edkey: pair.pubkey))) + ok( + KeyPair( + seckey: PrivateKey(scheme: scheme, edkey: pair.seckey), + pubkey: PublicKey(scheme: scheme, edkey: pair.pubkey), + ) + ) else: err(SchemeError) of PKScheme.ECDSA: when supported(PKScheme.ECDSA): - let pair = ? EcKeyPair.random(Secp256r1, rng).orError(KeyError) - ok(KeyPair( - seckey: PrivateKey(scheme: scheme, eckey: pair.seckey), - pubkey: PublicKey(scheme: scheme, eckey: pair.pubkey))) + let pair = ?EcKeyPair.random(Secp256r1, rng).orError(KeyError) + ok( + KeyPair( + seckey: PrivateKey(scheme: scheme, eckey: pair.seckey), + pubkey: PublicKey(scheme: scheme, eckey: pair.pubkey), + ) + ) else: err(SchemeError) of PKScheme.Secp256k1: when supported(PKScheme.Secp256k1): let pair = SkKeyPair.random(rng) - ok(KeyPair( - seckey: PrivateKey(scheme: scheme, skkey: pair.seckey), - pubkey: PublicKey(scheme: scheme, skkey: pair.pubkey))) + ok( + KeyPair( + seckey: PrivateKey(scheme: scheme, skkey: pair.seckey), + pubkey: PublicKey(scheme: scheme, skkey: pair.pubkey), + ) + ) else: err(SchemeError) -proc random*(T: typedesc[KeyPair], rng: var HmacDrbgContext, - bits = RsaDefaultKeySize): CryptoResult[KeyPair] = +proc random*( + T: typedesc[KeyPair], rng: var HmacDrbgContext, bits = RsaDefaultKeySize +): CryptoResult[KeyPair] = ## Generate random private pair of keys using default public-key cryptography ## scheme. ## @@ -295,24 +313,36 @@ proc random*(T: typedesc[KeyPair], rng: var HmacDrbgContext, ## So will be used first available (supported) method. when supported(PKScheme.Ed25519): let pair = EdKeyPair.random(rng) - ok(KeyPair( - seckey: PrivateKey(scheme: PKScheme.Ed25519, edkey: pair.seckey), - pubkey: PublicKey(scheme: PKScheme.Ed25519, edkey: pair.pubkey))) + ok( + KeyPair( + seckey: PrivateKey(scheme: PKScheme.Ed25519, edkey: pair.seckey), + pubkey: PublicKey(scheme: PKScheme.Ed25519, edkey: pair.pubkey), + ) + ) elif supported(PKScheme.Secp256k1): let pair = SkKeyPair.random(rng) - ok(KeyPair( - seckey: PrivateKey(scheme: PKScheme.Secp256k1, skkey: pair.seckey), - pubkey: PublicKey(scheme: PKScheme.Secp256k1, skkey: pair.pubkey))) + ok( + KeyPair( + seckey: PrivateKey(scheme: PKScheme.Secp256k1, skkey: pair.seckey), + pubkey: PublicKey(scheme: PKScheme.Secp256k1, skkey: pair.pubkey), + ) + ) elif supported(PKScheme.RSA): - let pair = ? RsaKeyPair.random(rng, bits).orError(KeyError) - ok(KeyPair( - seckey: PrivateKey(scheme: PKScheme.RSA, rsakey: pair.seckey), - pubkey: PublicKey(scheme: PKScheme.RSA, rsakey: pair.pubkey))) + let pair = ?RsaKeyPair.random(rng, bits).orError(KeyError) + ok( + KeyPair( + seckey: PrivateKey(scheme: PKScheme.RSA, rsakey: pair.seckey), + pubkey: PublicKey(scheme: PKScheme.RSA, rsakey: pair.pubkey), + ) + ) elif supported(PKScheme.ECDSA): - let pair = ? EcKeyPair.random(Secp256r1, rng).orError(KeyError) - ok(KeyPair( - seckey: PrivateKey(scheme: PKScheme.ECDSA, eckey: pair.seckey), - pubkey: PublicKey(scheme: PKScheme.ECDSA, eckey: pair.pubkey))) + let pair = ?EcKeyPair.random(Secp256r1, rng).orError(KeyError) + ok( + KeyPair( + seckey: PrivateKey(scheme: PKScheme.ECDSA, eckey: pair.seckey), + pubkey: PublicKey(scheme: PKScheme.ECDSA, eckey: pair.pubkey), + ) + ) else: err(SchemeError) @@ -333,7 +363,7 @@ proc getPublicKey*(key: PrivateKey): CryptoResult[PublicKey] = err(SchemeError) of PKScheme.ECDSA: when supported(PKScheme.ECDSA): - let eckey = ? key.eckey.getPublicKey().orError(KeyError) + let eckey = ?key.eckey.getPublicKey().orError(KeyError) ok(PublicKey(scheme: ECDSA, eckey: eckey)) else: err(SchemeError) @@ -344,8 +374,9 @@ proc getPublicKey*(key: PrivateKey): CryptoResult[PublicKey] = else: err(SchemeError) -proc toRawBytes*(key: PrivateKey | PublicKey, - data: var openArray[byte]): CryptoResult[int] = +proc toRawBytes*( + key: PrivateKey | PublicKey, data: var openArray[byte] +): CryptoResult[int] = ## Serialize private key ``key`` (using scheme's own serialization) and store ## it to ``data``. ## @@ -404,7 +435,7 @@ proc toBytes*(key: PrivateKey, data: var openArray[byte]): CryptoResult[int] = ## Returns number of bytes (octets) needed to store private key ``key``. var msg = initProtoBuffer() msg.write(1, uint64(key.scheme)) - msg.write(2, ? key.getRawBytes()) + msg.write(2, ?key.getRawBytes()) msg.finish() var blen = len(msg.buffer) if len(data) >= blen: @@ -418,7 +449,7 @@ proc toBytes*(key: PublicKey, data: var openArray[byte]): CryptoResult[int] = ## Returns number of bytes (octets) needed to store public key ``key``. var msg = initProtoBuffer() msg.write(1, uint64(key.scheme)) - msg.write(2, ? key.getRawBytes()) + msg.write(2, ?key.getRawBytes()) msg.finish() var blen = len(msg.buffer) if len(data) >= blen and blen > 0: @@ -438,7 +469,7 @@ proc getBytes*(key: PrivateKey): CryptoResult[seq[byte]] = ## serialization). var msg = initProtoBuffer() msg.write(1, uint64(key.scheme)) - msg.write(2, ? key.getRawBytes()) + msg.write(2, ?key.getRawBytes()) msg.finish() ok(msg.buffer) @@ -447,7 +478,7 @@ proc getBytes*(key: PublicKey): CryptoResult[seq[byte]] = ## serialization). var msg = initProtoBuffer() msg.write(1, uint64(key.scheme)) - msg.write(2, ? key.getRawBytes()) + msg.write(2, ?key.getRawBytes()) msg.finish() ok(msg.buffer) @@ -455,8 +486,7 @@ proc getBytes*(sig: Signature): seq[byte] = ## Return signature ``sig`` in binary form. result = sig.data -template initImpl[T: PrivateKey|PublicKey]( - key: var T, data: openArray[byte]): bool = +template initImpl[T: PrivateKey | PublicKey](key: var T, data: openArray[byte]): bool = ## Initialize private key ``key`` from libp2p's protobuf serialized raw ## binary form. ## @@ -469,7 +499,7 @@ template initImpl[T: PrivateKey|PublicKey]( var pb = initProtoBuffer(@data) let r1 = pb.getField(1, id) let r2 = pb.getField(2, buffer) - if not(r1.get(false) and r2.get(false)): + if not (r1.get(false) and r2.get(false)): false else: if cast[int8](id) notin SupportedSchemesInt or len(buffer) <= 0: @@ -480,7 +510,7 @@ template initImpl[T: PrivateKey|PublicKey]( var nkey = PrivateKey(scheme: scheme) else: var nkey = PublicKey(scheme: scheme) - case scheme: + case scheme of PKScheme.RSA: when supported(PKScheme.RSA): if init(nkey.rsakey, buffer).isOk: @@ -518,12 +548,13 @@ template initImpl[T: PrivateKey|PublicKey]( else: false -{.push warning[ProveField]:off.} # https://github.com/nim-lang/Nim/issues/22060 +{.push warning[ProveField]: off.} # https://github.com/nim-lang/Nim/issues/22060 proc init*(key: var PrivateKey, data: openArray[byte]): bool = initImpl(key, data) proc init*(key: var PublicKey, data: openArray[byte]): bool = initImpl(key, data) + {.pop.} proc init*(sig: var Signature, data: openArray[byte]): bool = @@ -534,7 +565,7 @@ proc init*(sig: var Signature, data: openArray[byte]): bool = sig.data = @data result = true -proc init*[T: PrivateKey|PublicKey](key: var T, data: string): bool = +proc init*[T: PrivateKey | PublicKey](key: var T, data: string): bool = ## Initialize private/public key ``key`` from libp2p's protobuf serialized ## hexadecimal string representation. ## @@ -548,8 +579,7 @@ proc init*(sig: var Signature, data: string): bool = ## Returns ``true`` on success. sig.init(ncrutils.fromHex(data)) -proc init*(t: typedesc[PrivateKey], - data: openArray[byte]): CryptoResult[PrivateKey] = +proc init*(t: typedesc[PrivateKey], data: openArray[byte]): CryptoResult[PrivateKey] = ## Create new private key from libp2p's protobuf serialized binary form. var res: t if not res.init(data): @@ -557,8 +587,7 @@ proc init*(t: typedesc[PrivateKey], else: ok(res) -proc init*(t: typedesc[PublicKey], - data: openArray[byte]): CryptoResult[PublicKey] = +proc init*(t: typedesc[PublicKey], data: openArray[byte]): CryptoResult[PublicKey] = ## Create new public key from libp2p's protobuf serialized binary form. var res: t if not res.init(data): @@ -566,8 +595,7 @@ proc init*(t: typedesc[PublicKey], else: ok(res) -proc init*(t: typedesc[Signature], - data: openArray[byte]): CryptoResult[Signature] = +proc init*(t: typedesc[Signature], data: openArray[byte]): CryptoResult[Signature] = ## Create new public key from libp2p's protobuf serialized binary form. var res: t if not res.init(data): @@ -583,24 +611,28 @@ proc init*(t: typedesc[PrivateKey], data: string): CryptoResult[PrivateKey] = when supported(PKScheme.RSA): proc init*(t: typedesc[PrivateKey], key: rsa.RsaPrivateKey): PrivateKey = PrivateKey(scheme: RSA, rsakey: key) + proc init*(t: typedesc[PublicKey], key: rsa.RsaPublicKey): PublicKey = PublicKey(scheme: RSA, rsakey: key) when supported(PKScheme.Ed25519): proc init*(t: typedesc[PrivateKey], key: EdPrivateKey): PrivateKey = PrivateKey(scheme: Ed25519, edkey: key) + proc init*(t: typedesc[PublicKey], key: EdPublicKey): PublicKey = PublicKey(scheme: Ed25519, edkey: key) when supported(PKScheme.Secp256k1): proc init*(t: typedesc[PrivateKey], key: SkPrivateKey): PrivateKey = PrivateKey(scheme: Secp256k1, skkey: key) + proc init*(t: typedesc[PublicKey], key: SkPublicKey): PublicKey = PublicKey(scheme: Secp256k1, skkey: key) when supported(PKScheme.ECDSA): proc init*(t: typedesc[PrivateKey], key: ecnist.EcPrivateKey): PrivateKey = PrivateKey(scheme: ECDSA, eckey: key) + proc init*(t: typedesc[PublicKey], key: ecnist.EcPublicKey): PublicKey = PublicKey(scheme: ECDSA, eckey: key) @@ -669,9 +701,9 @@ proc `==`*(key1, key2: PrivateKey): bool = else: false -proc `$`*(key: PrivateKey|PublicKey): string = +proc `$`*(key: PrivateKey | PublicKey): string = ## Get string representation of private/public key ``key``. - case key.scheme: + case key.scheme of PKScheme.RSA: when supported(PKScheme.RSA): $(key.rsakey) @@ -693,9 +725,9 @@ proc `$`*(key: PrivateKey|PublicKey): string = else: "unsupported secp256k1 key" -func shortLog*(key: PrivateKey|PublicKey): string = +func shortLog*(key: PrivateKey | PublicKey): string = ## Get short string representation of private/public key ``key``. - case key.scheme: + case key.scheme of PKScheme.RSA: when supported(PKScheme.RSA): ($key.rsakey).shortLog @@ -721,16 +753,15 @@ proc `$`*(sig: Signature): string = ## Get string representation of signature ``sig``. result = ncrutils.toHex(sig.data) -proc sign*(key: PrivateKey, - data: openArray[byte]): CryptoResult[Signature] {.gcsafe.} = +proc sign*(key: PrivateKey, data: openArray[byte]): CryptoResult[Signature] {.gcsafe.} = ## Sign message ``data`` using private key ``key`` and return generated ## signature in raw binary form. var res: Signature - case key.scheme: + case key.scheme of PKScheme.RSA: when supported(PKScheme.RSA): - let sig = ? key.rsakey.sign(data).orError(SigError) - res.data = ? sig.getBytes().orError(SigError) + let sig = ?key.rsakey.sign(data).orError(SigError) + res.data = ?sig.getBytes().orError(SigError) ok(res) else: err(SchemeError) @@ -743,8 +774,8 @@ proc sign*(key: PrivateKey, err(SchemeError) of PKScheme.ECDSA: when supported(PKScheme.ECDSA): - let sig = ? key.eckey.sign(data).orError(SigError) - res.data = ? sig.getBytes().orError(SigError) + let sig = ?key.eckey.sign(data).orError(SigError) + res.data = ?sig.getBytes().orError(SigError) ok(res) else: err(SchemeError) @@ -759,7 +790,7 @@ proc sign*(key: PrivateKey, proc verify*(sig: Signature, message: openArray[byte], key: PublicKey): bool = ## Verify signature ``sig`` using message ``message`` and public key ``key``. ## Return ``true`` if message signature is valid. - case key.scheme: + case key.scheme of PKScheme.RSA: when supported(PKScheme.RSA): var signature: RsaSignature @@ -797,12 +828,12 @@ proc verify*(sig: Signature, message: openArray[byte], key: PublicKey): bool = else: false -template makeSecret(buffer, hmactype, secret, seed: untyped) {.dirty.}= +template makeSecret(buffer, hmactype, secret, seed: untyped) {.dirty.} = var ctx: hmactype var j = 0 # We need to strip leading zeros, because Go bigint serialization do it. var offset = 0 - for i in 0.. getPublicKeyLength(cast[EcCurveKind](seckey.key.curve)) - if ecComputePub(ecimp, addr res.key, - addr res.buffer[0], unsafeAddr seckey.key) == 0: + if ecComputePub(ecimp, addr res.key, addr res.buffer[0], unsafeAddr seckey.key) == 0: err(EcKeyIncorrectError) else: ok(res) @@ -266,23 +257,23 @@ proc getPublicKey*(seckey: EcPrivateKey): EcResult[EcPublicKey] = err(EcKeyIncorrectError) proc random*( - T: typedesc[EcKeyPair], kind: EcCurveKind, - rng: var HmacDrbgContext): EcResult[T] = + T: typedesc[EcKeyPair], kind: EcCurveKind, rng: var HmacDrbgContext +): EcResult[T] = ## Generate new random EC private and public keypair using BearSSL's ## HMAC-SHA256-DRBG algorithm. ## ## ``kind`` elliptic curve kind of your choice (secp256r1, secp384r1 or ## secp521r1). let - seckey = ? EcPrivateKey.random(kind, rng) - pubkey = ? seckey.getPublicKey() + seckey = ?EcPrivateKey.random(kind, rng) + pubkey = ?seckey.getPublicKey() key = EcKeyPair(seckey: seckey, pubkey: pubkey) ok(key) proc `$`*(seckey: EcPrivateKey): string = ## Return string representation of EC private key. if isNil(seckey) or seckey.key.curve == 0 or seckey.key.xlen == 0 or - len(seckey.buffer) == 0: + len(seckey.buffer) == 0: result = "Empty or uninitialized ECNIST key" else: if seckey.key.curve notin EcSupportedCurvesCint: @@ -298,7 +289,7 @@ proc `$`*(seckey: EcPrivateKey): string = proc `$`*(pubkey: EcPublicKey): string = ## Return string representation of EC public key. if isNil(pubkey) or pubkey.key.curve == 0 or pubkey.key.qlen == 0 or - len(pubkey.buffer) == 0: + len(pubkey.buffer) == 0: result = "Empty or uninitialized ECNIST key" else: if pubkey.key.curve notin EcSupportedCurvesCint: @@ -371,7 +362,7 @@ proc toBytes*(seckey: EcPrivateKey, data: var openArray[byte]): EcResult[int] = return err(EcKeyIncorrectError) if seckey.key.curve in EcSupportedCurvesCint: var offset, length: int - var pubkey = ? seckey.getPublicKey() + var pubkey = ?seckey.getPublicKey() var b = Asn1Buffer.init() var p = Asn1Composite.init(Asn1Tag.Sequence) var c0 = Asn1Composite.init(0) @@ -387,16 +378,14 @@ proc toBytes*(seckey: EcPrivateKey, data: var openArray[byte]): EcResult[int] = if offset < 0: return err(EcKeyIncorrectError) length = int(pubkey.key.qlen) - c1.write(Asn1Tag.BitString, - pubkey.buffer.toOpenArray(offset, offset + length - 1)) + c1.write(Asn1Tag.BitString, pubkey.buffer.toOpenArray(offset, offset + length - 1)) c1.finish() offset = seckey.getOffset() if offset < 0: return err(EcKeyIncorrectError) length = int(seckey.key.xlen) p.write(1'u64) - p.write(Asn1Tag.OctetString, - seckey.buffer.toOpenArray(offset, offset + length - 1)) + p.write(Asn1Tag.OctetString, seckey.buffer.toOpenArray(offset, offset + length - 1)) p.write(c0) p.write(c1) p.finish() @@ -410,7 +399,6 @@ proc toBytes*(seckey: EcPrivateKey, data: var openArray[byte]): EcResult[int] = else: err(EcKeyIncorrectError) - proc toBytes*(pubkey: EcPublicKey, data: var openArray[byte]): EcResult[int] = ## Serialize EC public key ``pubkey`` to ASN.1 DER binary form and store it ## to ``data``. @@ -436,8 +424,7 @@ proc toBytes*(pubkey: EcPublicKey, data: var openArray[byte]): EcResult[int] = if offset < 0: return err(EcKeyIncorrectError) let length = int(pubkey.key.qlen) - p.write(Asn1Tag.BitString, - pubkey.buffer.toOpenArray(offset, offset + length - 1)) + p.write(Asn1Tag.BitString, pubkey.buffer.toOpenArray(offset, offset + length - 1)) p.finish() b.write(p) b.finish() @@ -467,9 +454,9 @@ proc getBytes*(seckey: EcPrivateKey): EcResult[seq[byte]] = return err(EcKeyIncorrectError) if seckey.key.curve in EcSupportedCurvesCint: var res = newSeq[byte]() - let length = ? seckey.toBytes(res) + let length = ?seckey.toBytes(res) res.setLen(length) - discard ? seckey.toBytes(res) + discard ?seckey.toBytes(res) ok(res) else: err(EcKeyIncorrectError) @@ -480,9 +467,9 @@ proc getBytes*(pubkey: EcPublicKey): EcResult[seq[byte]] = return err(EcKeyIncorrectError) if pubkey.key.curve in EcSupportedCurvesCint: var res = newSeq[byte]() - let length = ? pubkey.toBytes(res) + let length = ?pubkey.toBytes(res) res.setLen(length) - discard ? pubkey.toBytes(res) + discard ?pubkey.toBytes(res) ok(res) else: err(EcKeyIncorrectError) @@ -492,9 +479,9 @@ proc getBytes*(sig: EcSignature): EcResult[seq[byte]] = if isNil(sig): return err(EcSignatureError) var res = newSeq[byte]() - let length = ? sig.toBytes(res) + let length = ?sig.toBytes(res) res.setLen(length) - discard ? sig.toBytes(res) + discard ?sig.toBytes(res) ok(res) proc getRawBytes*(seckey: EcPrivateKey): EcResult[seq[byte]] = @@ -503,9 +490,9 @@ proc getRawBytes*(seckey: EcPrivateKey): EcResult[seq[byte]] = return err(EcKeyIncorrectError) if seckey.key.curve in EcSupportedCurvesCint: var res = newSeq[byte]() - let length = ? seckey.toRawBytes(res) + let length = ?seckey.toRawBytes(res) res.setLen(length) - discard ? seckey.toRawBytes(res) + discard ?seckey.toRawBytes(res) ok(res) else: err(EcKeyIncorrectError) @@ -516,9 +503,9 @@ proc getRawBytes*(pubkey: EcPublicKey): EcResult[seq[byte]] = return err(EcKeyIncorrectError) if pubkey.key.curve in EcSupportedCurvesCint: var res = newSeq[byte]() - let length = ? pubkey.toRawBytes(res) + let length = ?pubkey.toRawBytes(res) res.setLen(length) - discard ? pubkey.toRawBytes(res) + discard ?pubkey.toRawBytes(res) return ok(res) else: return err(EcKeyIncorrectError) @@ -528,9 +515,9 @@ proc getRawBytes*(sig: EcSignature): EcResult[seq[byte]] = if isNil(sig): return err(EcSignatureError) var res = newSeq[byte]() - let length = ? sig.toBytes(res) + let length = ?sig.toBytes(res) res.setLen(length) - discard ? sig.toBytes(res) + discard ?sig.toBytes(res) ok(res) proc `==`*(pubkey1, pubkey2: EcPublicKey): bool = @@ -550,8 +537,10 @@ proc `==`*(pubkey1, pubkey2: EcPublicKey): bool = let op2 = pubkey2.getOffset() if op1 == -1 or op2 == -1: return false - return CT.isEqual(pubkey1.buffer.toOpenArray(op1, pubkey1.key.qlen - 1), - pubkey2.buffer.toOpenArray(op2, pubkey2.key.qlen - 1)) + return CT.isEqual( + pubkey1.buffer.toOpenArray(op1, pubkey1.key.qlen - 1), + pubkey2.buffer.toOpenArray(op2, pubkey2.key.qlen - 1), + ) proc `==`*(seckey1, seckey2: EcPrivateKey): bool = ## Returns ``true`` if both keys ``seckey1`` and ``seckey2`` are equal. @@ -570,8 +559,10 @@ proc `==`*(seckey1, seckey2: EcPrivateKey): bool = let op2 = seckey2.getOffset() if op1 == -1 or op2 == -1: return false - return CT.isEqual(seckey1.buffer.toOpenArray(op1, seckey1.key.xlen - 1), - seckey2.buffer.toOpenArray(op2, seckey2.key.xlen - 1)) + return CT.isEqual( + seckey1.buffer.toOpenArray(op1, seckey1.key.xlen - 1), + seckey2.buffer.toOpenArray(op2, seckey2.key.xlen - 1), + ) proc `==`*(a, b: EcSignature): bool = ## Return ``true`` if both signatures ``sig1`` and ``sig2`` are equal. @@ -605,26 +596,26 @@ proc init*(key: var EcPrivateKey, data: openArray[byte]): Result[void, Asn1Error var ab = Asn1Buffer.init(data) - field = ? ab.read() + field = ?ab.read() if field.kind != Asn1Tag.Sequence: return err(Asn1Error.Incorrect) var ib = field.getBuffer() - field = ? ib.read() + field = ?ib.read() if field.kind != Asn1Tag.Integer: return err(Asn1Error.Incorrect) if field.vint != 1'u64: return err(Asn1Error.Incorrect) - raw = ? ib.read() + raw = ?ib.read() if raw.kind != Asn1Tag.OctetString: return err(Asn1Error.Incorrect) - oid = ? ib.read() + oid = ?ib.read() if oid.kind != Asn1Tag.Oid: return err(Asn1Error.Incorrect) @@ -658,19 +649,19 @@ proc init*(pubkey: var EcPublicKey, data: openArray[byte]): Result[void, Asn1Err var ab = Asn1Buffer.init(data) - field = ? ab.read() + field = ?ab.read() if field.kind != Asn1Tag.Sequence: return err(Asn1Error.Incorrect) var ib = field.getBuffer() - field = ? ib.read() + field = ?ib.read() if field.kind != Asn1Tag.Sequence: return err(Asn1Error.Incorrect) var ob = field.getBuffer() - oid = ? ob.read() + oid = ?ob.read() if oid.kind != Asn1Tag.Oid: return err(Asn1Error.Incorrect) @@ -678,7 +669,7 @@ proc init*(pubkey: var EcPublicKey, data: openArray[byte]): Result[void, Asn1Err if oid != Asn1OidEcPublicKey: return err(Asn1Error.Incorrect) - oid = ? ob.read() + oid = ?ob.read() if oid.kind != Asn1Tag.Oid: return err(Asn1Error.Incorrect) @@ -692,7 +683,7 @@ proc init*(pubkey: var EcPublicKey, data: openArray[byte]): Result[void, Asn1Err else: return err(Asn1Error.Incorrect) - raw = ? ib.read() + raw = ?ib.read() if raw.kind != Asn1Tag.BitString: return err(Asn1Error.Incorrect) @@ -718,16 +709,14 @@ proc init*(sig: var EcSignature, data: openArray[byte]): Result[void, Asn1Error] else: err(Asn1Error.Incorrect) -proc init*[T: EcPKI](sospk: var T, - data: string): Result[void, Asn1Error] {.inline.} = +proc init*[T: EcPKI](sospk: var T, data: string): Result[void, Asn1Error] {.inline.} = ## Initialize EC `private key`, `public key` or `signature` ``sospk`` from ## ASN.1 DER hexadecimal string representation ``data``. ## ## Procedure returns ``Asn1Status``. sospk.init(ncrutils.fromHex(data)) -proc init*(t: typedesc[EcPrivateKey], - data: openArray[byte]): EcResult[EcPrivateKey] = +proc init*(t: typedesc[EcPrivateKey], data: openArray[byte]): EcResult[EcPrivateKey] = ## Initialize EC private key from ASN.1 DER binary representation ``data`` and ## return constructed object. var key: EcPrivateKey @@ -737,8 +726,7 @@ proc init*(t: typedesc[EcPrivateKey], else: ok(key) -proc init*(t: typedesc[EcPublicKey], - data: openArray[byte]): EcResult[EcPublicKey] = +proc init*(t: typedesc[EcPublicKey], data: openArray[byte]): EcResult[EcPublicKey] = ## Initialize EC public key from ASN.1 DER binary representation ``data`` and ## return constructed object. var key: EcPublicKey @@ -748,8 +736,7 @@ proc init*(t: typedesc[EcPublicKey], else: ok(key) -proc init*(t: typedesc[EcSignature], - data: openArray[byte]): EcResult[EcSignature] = +proc init*(t: typedesc[EcSignature], data: openArray[byte]): EcResult[EcSignature] = ## Initialize EC signature from raw binary representation ``data`` and ## return constructed object. var sig: EcSignature @@ -832,8 +819,7 @@ proc initRaw*(sig: var EcSignature, data: openArray[byte]): bool = ## ## Procedure returns ``true`` on success, ``false`` otherwise. let length = len(data) - if (length == Sig256Length) or (length == Sig384Length) or - (length == Sig521Length): + if (length == Sig256Length) or (length == Sig384Length) or (length == Sig521Length): result = true if result: sig = new EcSignature @@ -846,8 +832,9 @@ proc initRaw*[T: EcPKI](sospk: var T, data: string): bool {.inline.} = ## Procedure returns ``true`` on success, ``false`` otherwise. result = sospk.initRaw(ncrutils.fromHex(data)) -proc initRaw*(t: typedesc[EcPrivateKey], - data: openArray[byte]): EcResult[EcPrivateKey] = +proc initRaw*( + t: typedesc[EcPrivateKey], data: openArray[byte] +): EcResult[EcPrivateKey] = ## Initialize EC private key from raw binary representation ``data`` and ## return constructed object. var res: EcPrivateKey @@ -856,8 +843,7 @@ proc initRaw*(t: typedesc[EcPrivateKey], else: ok(res) -proc initRaw*(t: typedesc[EcPublicKey], - data: openArray[byte]): EcResult[EcPublicKey] = +proc initRaw*(t: typedesc[EcPublicKey], data: openArray[byte]): EcResult[EcPublicKey] = ## Initialize EC public key from raw binary representation ``data`` and ## return constructed object. var res: EcPublicKey @@ -866,8 +852,7 @@ proc initRaw*(t: typedesc[EcPublicKey], else: ok(res) -proc initRaw*(t: typedesc[EcSignature], - data: openArray[byte]): EcResult[EcSignature] = +proc initRaw*(t: typedesc[EcSignature], data: openArray[byte]): EcResult[EcSignature] = ## Initialize EC signature from raw binary representation ``data`` and ## return constructed object. var res: EcSignature @@ -894,16 +879,19 @@ proc scalarMul*(pub: EcPublicKey, sec: EcPrivateKey): EcPublicKey = let poffset = key.getOffset() let soffset = sec.getOffset() if poffset >= 0 and soffset >= 0: - let res = impl.mul(addr key.buffer[poffset], - key.key.qlen, - unsafeAddr sec.buffer[soffset], - sec.key.xlen, - key.key.curve) + let res = impl.mul( + addr key.buffer[poffset], + key.key.qlen, + unsafeAddr sec.buffer[soffset], + sec.key.xlen, + key.key.curve, + ) if res != 0: result = key -proc toSecret*(pubkey: EcPublicKey, seckey: EcPrivateKey, - data: var openArray[byte]): int = +proc toSecret*( + pubkey: EcPublicKey, seckey: EcPrivateKey, data: var openArray[byte] +): int = ## Calculate ECDHE shared secret using Go's elliptic/curve approach, using ## remote public key ``pubkey`` and local private key ``seckey`` and store ## shared secret to ``data``. @@ -939,8 +927,9 @@ proc getSecret*(pubkey: EcPublicKey, seckey: EcPrivateKey): seq[byte] = result = newSeq[byte](res) copyMem(addr result[0], addr data[0], res) -proc sign*[T: byte|char](seckey: EcPrivateKey, - message: openArray[T]): EcResult[EcSignature] {.gcsafe.} = +proc sign*[T: byte | char]( + seckey: EcPrivateKey, message: openArray[T] +): EcResult[EcSignature] {.gcsafe.} = ## Get ECDSA signature of data ``message`` using private key ``seckey``. if isNil(seckey): return err(EcKeyIncorrectError) @@ -957,8 +946,8 @@ proc sign*[T: byte|char](seckey: EcPrivateKey, else: kv.update(addr hc.vtable, nil, 0) kv.out(addr hc.vtable, addr hash[0]) - let res = ecdsaI31SignAsn1(impl, kv, addr hash[0], addr seckey.key, - addr sig.buffer[0]) + let res = + ecdsaI31SignAsn1(impl, kv, addr hash[0], addr seckey.key, addr sig.buffer[0]) # Clear context with initial value kv.init(addr hc.vtable) if res != 0: @@ -969,8 +958,9 @@ proc sign*[T: byte|char](seckey: EcPrivateKey, else: err(EcKeyIncorrectError) -proc verify*[T: byte|char](sig: EcSignature, message: openArray[T], - pubkey: EcPublicKey): bool {.inline.} = +proc verify*[T: byte | char]( + sig: EcSignature, message: openArray[T], pubkey: EcPublicKey +): bool {.inline.} = ## Verify ECDSA signature ``sig`` using public key ``pubkey`` and data ## ``message``. ## @@ -988,30 +978,32 @@ proc verify*[T: byte|char](sig: EcSignature, message: openArray[T], else: kv.update(addr hc.vtable, nil, 0) kv.out(addr hc.vtable, addr hash[0]) - let res = ecdsaI31VrfyAsn1(impl, addr hash[0], uint(len(hash)), - unsafeAddr pubkey.key, - addr sig.buffer[0], uint(len(sig.buffer))) + let res = ecdsaI31VrfyAsn1( + impl, + addr hash[0], + uint(len(hash)), + unsafeAddr pubkey.key, + addr sig.buffer[0], + uint(len(sig.buffer)), + ) # Clear context with initial value kv.init(addr hc.vtable) result = (res == 1) type ECDHEScheme* = EcCurveKind -proc ephemeral*( - scheme: ECDHEScheme, - rng: var HmacDrbgContext): EcResult[EcKeyPair] = +proc ephemeral*(scheme: ECDHEScheme, rng: var HmacDrbgContext): EcResult[EcKeyPair] = ## Generate ephemeral keys used to perform ECDHE. var keypair: EcKeyPair if scheme == Secp256r1: - keypair = ? EcKeyPair.random(Secp256r1, rng) + keypair = ?EcKeyPair.random(Secp256r1, rng) elif scheme == Secp384r1: - keypair = ? EcKeyPair.random(Secp384r1, rng) + keypair = ?EcKeyPair.random(Secp384r1, rng) elif scheme == Secp521r1: - keypair = ? EcKeyPair.random(Secp521r1, rng) + keypair = ?EcKeyPair.random(Secp521r1, rng) ok(keypair) -proc ephemeral*( - scheme: string, rng: var HmacDrbgContext): EcResult[EcKeyPair] = +proc ephemeral*(scheme: string, rng: var HmacDrbgContext): EcResult[EcKeyPair] = ## Generate ephemeral keys used to perform ECDHE using string encoding. ## ## Currently supported encoding strings are P-256, P-384, P-521, if encoding diff --git a/libp2p/crypto/ed25519/constants.nim b/libp2p/crypto/ed25519/constants.nim index bea3a9d666..71b2a7ee3d 100644 --- a/libp2p/crypto/ed25519/constants.nim +++ b/libp2p/crypto/ed25519/constants.nim @@ -30,3796 +30,3796 @@ type const CurveOrder* = [ - 0xED'u8, 0xD3'u8, 0xF5'u8, 0x5C'u8, 0x1A'u8, 0x63'u8, 0x12'u8, 0x58'u8, - 0xD6'u8, 0x9C'u8, 0xF7'u8, 0xA2'u8, 0xDE'u8, 0xF9'u8, 0xDE'u8, 0x14'u8, - 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8, - 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8, 0x10'u8 + 0xED'u8, 0xD3'u8, 0xF5'u8, 0x5C'u8, 0x1A'u8, 0x63'u8, 0x12'u8, 0x58'u8, 0xD6'u8, + 0x9C'u8, 0xF7'u8, 0xA2'u8, 0xDE'u8, 0xF9'u8, 0xDE'u8, 0x14'u8, 0x00'u8, 0x00'u8, + 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8, + 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8, 0x10'u8, ] ZeroFe* = [ - 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8, - 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8, - 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8, - 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8 + 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8, + 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8, + 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8, + 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8, 0x00'u8, ] BasePrecomp* = [ [ GePrecomp( yplusx: [ - 25967493'i32, -14356035, 29566456, 3660896, -12694345, - 4014787, 27544626, -11754271, -6079156, 2047605 + 25967493'i32, -14356035, 29566456, 3660896, -12694345, 4014787, 27544626, + -11754271, -6079156, 2047605, ], yminusx: [ - -12545711'i32, 934262, -2722910, 3049990, -727428, - 9406986, 12720692, 5043384, 19500929, -15469378 + -12545711'i32, 934262, -2722910, 3049990, -727428, 9406986, 12720692, 5043384, + 19500929, -15469378, ], xy2d: [ - -8738181'i32, 4489570, 9688441, -14785194, 10184609, - -12363380, 29287919, 11864899, -24514362, -4438546 - ] + -8738181'i32, 4489570, 9688441, -14785194, 10184609, -12363380, 29287919, + 11864899, -24514362, -4438546, + ], ), GePrecomp( yplusx: [ - -12815894'i32, -12976347, -21581243, 11784320, -25355658, - -2750717, -11717903, -3814571, -358445, -10211303 + -12815894'i32, -12976347, -21581243, 11784320, -25355658, -2750717, -11717903, + -3814571, -358445, -10211303, ], yminusx: [ - -21703237'i32, 6903825, 27185491, 6451973, -29577724, - -9554005, -15616551, 11189268, -26829678, -5319081 + -21703237'i32, 6903825, 27185491, 6451973, -29577724, -9554005, -15616551, + 11189268, -26829678, -5319081, ], xy2d: [ - 26966642'i32, 11152617, 32442495, 15396054, 14353839, - -12752335, -3128826, -9541118, -15472047, -4166697 - ] + 26966642'i32, 11152617, 32442495, 15396054, 14353839, -12752335, -3128826, + -9541118, -15472047, -4166697, + ], ), GePrecomp( yplusx: [ - 15636291'i32, -9688557, 24204773, -7912398, 616977, - -16685262, 27787600, -14772189, 28944400, -1550024 + 15636291'i32, -9688557, 24204773, -7912398, 616977, -16685262, 27787600, + -14772189, 28944400, -1550024, ], yminusx: [ - 16568933'i32, 4717097, -11556148, -1102322, 15682896, - -11807043, 16354577, -11775962, 7689662, 11199574 + 16568933'i32, 4717097, -11556148, -1102322, 15682896, -11807043, 16354577, + -11775962, 7689662, 11199574, ], xy2d: [ - 30464156'i32, -5976125, -11779434, -15670865, 23220365, - 15915852, 7512774, 10017326, -17749093, -9920357 - ] + 30464156'i32, -5976125, -11779434, -15670865, 23220365, 15915852, 7512774, + 10017326, -17749093, -9920357, + ], ), GePrecomp( yplusx: [ - -17036878'i32, 13921892, 10945806, -6033431, 27105052, - -16084379, -28926210, 15006023, 3284568, -6276540 + -17036878'i32, 13921892, 10945806, -6033431, 27105052, -16084379, -28926210, + 15006023, 3284568, -6276540, ], yminusx: [ - 23599295'i32, -8306047, -11193664, -7687416, 13236774, - 10506355, 7464579, 9656445, 13059162, 10374397 + 23599295'i32, -8306047, -11193664, -7687416, 13236774, 10506355, 7464579, + 9656445, 13059162, 10374397, ], xy2d: [ - 7798556'i32, 16710257, 3033922, 2874086, 28997861, - 2835604, 32406664, -3839045, -641708, -101325 - ] + 7798556'i32, 16710257, 3033922, 2874086, 28997861, 2835604, 32406664, + -3839045, -641708, -101325, + ], ), GePrecomp( yplusx: [ - 10861363'i32, 11473154, 27284546, 1981175, -30064349, - 12577861, 32867885, 14515107, -15438304, 10819380 + 10861363'i32, 11473154, 27284546, 1981175, -30064349, 12577861, 32867885, + 14515107, -15438304, 10819380, ], yminusx: [ - 4708026'i32, 6336745, 20377586, 9066809, -11272109, - 6594696, -25653668, 12483688, -12668491, 5581306 + 4708026'i32, 6336745, 20377586, 9066809, -11272109, 6594696, -25653668, + 12483688, -12668491, 5581306, ], xy2d: [ - 19563160'i32, 16186464, -29386857, 4097519, 10237984, - -4348115, 28542350, 13850243, -23678021, -15815942 - ] + 19563160'i32, 16186464, -29386857, 4097519, 10237984, -4348115, 28542350, + 13850243, -23678021, -15815942, + ], ), GePrecomp( yplusx: [ - -15371964'i32, -12862754, 32573250, 4720197, -26436522, - 5875511, -19188627, -15224819, -9818940, -12085777 + -15371964'i32, -12862754, 32573250, 4720197, -26436522, 5875511, -19188627, + -15224819, -9818940, -12085777, ], yminusx: [ - -8549212'i32, 109983, 15149363, 2178705, 22900618, - 4543417, 3044240, -15689887, 1762328, 14866737 + -8549212'i32, 109983, 15149363, 2178705, 22900618, 4543417, 3044240, + -15689887, 1762328, 14866737, ], xy2d: [ - -18199695'i32, -15951423, -10473290, 1707278, -17185920, - 3916101, -28236412, 3959421, 27914454, 4383652 - ] + -18199695'i32, -15951423, -10473290, 1707278, -17185920, 3916101, -28236412, + 3959421, 27914454, 4383652, + ], ), GePrecomp( yplusx: [ - 5153746'i32, 9909285, 1723747, -2777874, 30523605, - 5516873, 19480852, 5230134, -23952439, -15175766 + 5153746'i32, 9909285, 1723747, -2777874, 30523605, 5516873, 19480852, 5230134, + -23952439, -15175766, ], yminusx: [ - -30269007'i32, -3463509, 7665486, 10083793, 28475525, - 1649722, 20654025, 16520125, 30598449, 7715701 + -30269007'i32, -3463509, 7665486, 10083793, 28475525, 1649722, 20654025, + 16520125, 30598449, 7715701, ], xy2d: [ - 28881845'i32, 14381568, 9657904, 3680757, -20181635, - 7843316, -31400660, 1370708, 29794553, -1409300 - ] + 28881845'i32, 14381568, 9657904, 3680757, -20181635, 7843316, -31400660, + 1370708, 29794553, -1409300, + ], ), GePrecomp( yplusx: [ - 14499471'i32, -2729599, -33191113, -4254652, 28494862, - 14271267, 30290735, 10876454, -33154098, 2381726 + 14499471'i32, -2729599, -33191113, -4254652, 28494862, 14271267, 30290735, + 10876454, -33154098, 2381726, ], yminusx: [ - -7195431'i32, -2655363, -14730155, 462251, -27724326, - 3941372, -6236617, 3696005, -32300832, 15351955 + -7195431'i32, -2655363, -14730155, 462251, -27724326, 3941372, -6236617, + 3696005, -32300832, 15351955, ], xy2d: [ - 27431194'i32, 8222322, 16448760, -3907995, -18707002, - 11938355, -32961401, -2970515, 29551813, 10109425 - ] - ) + 27431194'i32, 8222322, 16448760, -3907995, -18707002, 11938355, -32961401, + -2970515, 29551813, 10109425, + ], + ), ], [ GePrecomp( yplusx: [ - -13657040'i32, -13155431, -31283750, 11777098, 21447386, - 6519384, -2378284, -1627556, 10092783, -4764171 + -13657040'i32, -13155431, -31283750, 11777098, 21447386, 6519384, -2378284, + -1627556, 10092783, -4764171, ], yminusx: [ - 27939166'i32, 14210322, 4677035, 16277044, -22964462, - -12398139, -32508754, 12005538, -17810127, 12803510 + 27939166'i32, 14210322, 4677035, 16277044, -22964462, -12398139, -32508754, + 12005538, -17810127, 12803510, ], xy2d: [ - 17228999'i32, -15661624, -1233527, 300140, -1224870, - -11714777, 30364213, -9038194, 18016357, 4397660 - ] + 17228999'i32, -15661624, -1233527, 300140, -1224870, -11714777, 30364213, + -9038194, 18016357, 4397660, + ], ), GePrecomp( yplusx: [ - -10958843'i32, -7690207, 4776341, -14954238, 27850028, - -15602212, -26619106, 14544525, -17477504, 982639 + -10958843'i32, -7690207, 4776341, -14954238, 27850028, -15602212, -26619106, + 14544525, -17477504, 982639, ], yminusx: [ - 29253598'i32, 15796703, -2863982, -9908884, 10057023, - 3163536, 7332899, -4120128, -21047696, 9934963 + 29253598'i32, 15796703, -2863982, -9908884, 10057023, 3163536, 7332899, + -4120128, -21047696, 9934963, ], xy2d: [ - 5793303'i32, 16271923, -24131614, -10116404, 29188560, - 1206517, -14747930, 4559895, -30123922, -10897950 - ] + 5793303'i32, 16271923, -24131614, -10116404, 29188560, 1206517, -14747930, + 4559895, -30123922, -10897950, + ], ), GePrecomp( yplusx: [ - -27643952'i32, -11493006, 16282657, -11036493, 28414021, - -15012264, 24191034, 4541697, -13338309,5500568 + -27643952'i32, -11493006, 16282657, -11036493, 28414021, -15012264, 24191034, + 4541697, -13338309, 5500568, ], yminusx: [ - 12650548'i32, -1497113, 9052871, 11355358, -17680037, - -8400164, -17430592, 12264343, 10874051, 13524335 + 12650548'i32, -1497113, 9052871, 11355358, -17680037, -8400164, -17430592, + 12264343, 10874051, 13524335, ], xy2d: [ - 25556948'i32, -3045990, 714651, 2510400, 23394682, - -10415330, 33119038, 5080568, -22528059, 5376628 - ] + 25556948'i32, -3045990, 714651, 2510400, 23394682, -10415330, 33119038, + 5080568, -22528059, 5376628, + ], ), GePrecomp( yplusx: [ - -26088264'i32, -4011052, -17013699, -3537628, -6726793, - 1920897, -22321305, -9447443, 4535768, 1569007 + -26088264'i32, -4011052, -17013699, -3537628, -6726793, 1920897, -22321305, + -9447443, 4535768, 1569007, ], yminusx: [ - -2255422'i32, 14606630, -21692440, -8039818, 28430649, - 8775819, -30494562, 3044290, 31848280, 12543772 + -2255422'i32, 14606630, -21692440, -8039818, 28430649, 8775819, -30494562, + 3044290, 31848280, 12543772, ], xy2d: [ - -22028579'i32, 2943893, -31857513, 6777306, 13784462, - -4292203, -27377195, -2062731, 7718482, 14474653 - ] + -22028579'i32, 2943893, -31857513, 6777306, 13784462, -4292203, -27377195, + -2062731, 7718482, 14474653, + ], ), GePrecomp( yplusx: [ - 2385315'i32, 2454213, -22631320, 46603, -4437935, - -15680415, 656965, -7236665, 24316168, -5253567 + 2385315'i32, 2454213, -22631320, 46603, -4437935, -15680415, 656965, -7236665, + 24316168, -5253567, ], yminusx: [ - 13741529'i32, 10911568, -33233417, -8603737, -20177830, - -1033297, 33040651, -13424532, -20729456, 8321686 + 13741529'i32, 10911568, -33233417, -8603737, -20177830, -1033297, 33040651, + -13424532, -20729456, 8321686, ], xy2d: [ - 21060490'i32, -2212744, 15712757, -4336099, 1639040, - 10656336, 23845965, -11874838, -9984458, 608372 - ] + 21060490'i32, -2212744, 15712757, -4336099, 1639040, 10656336, 23845965, + -11874838, -9984458, 608372, + ], ), GePrecomp( yplusx: [ - -13672732'i32, -15087586, -10889693, -7557059, -6036909, - 11305547, 1123968, -6780577, 27229399, 23887 + -13672732'i32, -15087586, -10889693, -7557059, -6036909, 11305547, 1123968, + -6780577, 27229399, 23887, ], yminusx: [ - -23244140'i32, -294205, -11744728, 14712571, -29465699, - -2029617, 12797024, -6440308, -1633405, 16678954 + -23244140'i32, -294205, -11744728, 14712571, -29465699, -2029617, 12797024, + -6440308, -1633405, 16678954, ], xy2d: [ - -29500620'i32, 4770662, -16054387, 14001338, 7830047, - 9564805, -1508144, -4795045, -17169265, 4904953 - ] + -29500620'i32, 4770662, -16054387, 14001338, 7830047, 9564805, -1508144, + -4795045, -17169265, 4904953, + ], ), GePrecomp( yplusx: [ - 24059557'i32, 14617003, 19037157, -15039908, 19766093, - -14906429, 5169211, 16191880, 2128236, -4326833 + 24059557'i32, 14617003, 19037157, -15039908, 19766093, -14906429, 5169211, + 16191880, 2128236, -4326833, ], yminusx: [ - -16981152'i32, 4124966, -8540610, -10653797, 30336522, - -14105247, -29806336, 916033, -6882542, -2986532 + -16981152'i32, 4124966, -8540610, -10653797, 30336522, -14105247, -29806336, + 916033, -6882542, -2986532, ], xy2d: [ - -22630907'i32, 12419372, -7134229, -7473371, -16478904, - 16739175, 285431, 2763829, 15736322, 4143876 - ] + -22630907'i32, 12419372, -7134229, -7473371, -16478904, 16739175, 285431, + 2763829, 15736322, 4143876, + ], ), GePrecomp( yplusx: [ - 2379352'i32, 11839345, -4110402, -5988665, 11274298, - 794957, 212801, -14594663, 23527084, -16458268 + 2379352'i32, 11839345, -4110402, -5988665, 11274298, 794957, 212801, + -14594663, 23527084, -16458268, ], yminusx: [ - 33431127'i32, -11130478, -17838966, -15626900, 8909499, - 8376530, -32625340, 4087881, -15188911, -14416214 + 33431127'i32, -11130478, -17838966, -15626900, 8909499, 8376530, -32625340, + 4087881, -15188911, -14416214, ], xy2d: [ - 1767683'i32, 7197987, -13205226, -2022635, -13091350, - 448826, 5799055, 4357868, -4774191, -16323038 - ] - ) + 1767683'i32, 7197987, -13205226, -2022635, -13091350, 448826, 5799055, + 4357868, -4774191, -16323038, + ], + ), ], [ GePrecomp( yplusx: [ - 6721966'i32, 13833823, -23523388, -1551314, 26354293, - -11863321, 23365147, -3949732, 7390890, 2759800 + 6721966'i32, 13833823, -23523388, -1551314, 26354293, -11863321, 23365147, + -3949732, 7390890, 2759800, ], yminusx: [ - 4409041'i32, 2052381, 23373853, 10530217, 7676779, - -12885954, 21302353, -4264057, 1244380, -12919645 + 4409041'i32, 2052381, 23373853, 10530217, 7676779, -12885954, 21302353, + -4264057, 1244380, -12919645, ], xy2d: [ - -4421239'i32, 7169619, 4982368, -2957590, 30256825, - -2777540, 14086413, 9208236, 15886429, 16489664 - ] + -4421239'i32, 7169619, 4982368, -2957590, 30256825, -2777540, 14086413, + 9208236, 15886429, 16489664, + ], ), GePrecomp( yplusx: [ - 1996075'i32, 10375649, 14346367, 13311202, -6874135, - -16438411, -13693198, 398369, -30606455, -712933 + 1996075'i32, 10375649, 14346367, 13311202, -6874135, -16438411, -13693198, + 398369, -30606455, -712933, ], yminusx: [ - -25307465'i32, 9795880, -2777414, 14878809, -33531835, - 14780363, 13348553, 12076947, -30836462, 5113182 + -25307465'i32, 9795880, -2777414, 14878809, -33531835, 14780363, 13348553, + 12076947, -30836462, 5113182, ], xy2d: [ - -17770784'i32, 11797796, 31950843, 13929123, -25888302, - 12288344, -30341101, -7336386, 13847711, 5387222 - ] + -17770784'i32, 11797796, 31950843, 13929123, -25888302, 12288344, -30341101, + -7336386, 13847711, 5387222, + ], ), GePrecomp( yplusx: [ - -18582163'i32, -3416217, 17824843, -2340966, 22744343, - -10442611, 8763061, 3617786, -19600662, 10370991 + -18582163'i32, -3416217, 17824843, -2340966, 22744343, -10442611, 8763061, + 3617786, -19600662, 10370991, ], yminusx: [ - 20246567'i32, -14369378, 22358229, -543712, 18507283, - -10413996, 14554437, -8746092, 32232924, 16763880 + 20246567'i32, -14369378, 22358229, -543712, 18507283, -10413996, 14554437, + -8746092, 32232924, 16763880, ], xy2d: [ - 9648505'i32, 10094563, 26416693, 14745928, -30374318, - -6472621, 11094161, 15689506, 3140038, -16510092 - ] + 9648505'i32, 10094563, 26416693, 14745928, -30374318, -6472621, 11094161, + 15689506, 3140038, -16510092, + ], ), GePrecomp( yplusx: [ - -16160072'i32, 5472695, 31895588, 4744994, 8823515, - 10365685, -27224800, 9448613, -28774454, 366295 + -16160072'i32, 5472695, 31895588, 4744994, 8823515, 10365685, -27224800, + 9448613, -28774454, 366295, ], yminusx: [ - 19153450'i32, 11523972, -11096490, -6503142, -24647631, - 5420647, 28344573, 8041113, 719605, 11671788 + 19153450'i32, 11523972, -11096490, -6503142, -24647631, 5420647, 28344573, + 8041113, 719605, 11671788, ], xy2d: [ - 8678025'i32, 2694440, -6808014, 2517372, 4964326, - 11152271, -15432916, -15266516, 27000813, -10195553 - ] + 8678025'i32, 2694440, -6808014, 2517372, 4964326, 11152271, -15432916, + -15266516, 27000813, -10195553, + ], ), GePrecomp( yplusx: [ - -15157904'i32, 7134312, 8639287, -2814877, -7235688, - 10421742, 564065, 5336097, 6750977, -14521026 + -15157904'i32, 7134312, 8639287, -2814877, -7235688, 10421742, 564065, + 5336097, 6750977, -14521026, ], yminusx: [ - 11836410'i32, -3979488, 26297894, 16080799, 23455045, - 15735944, 1695823, -8819122, 8169720, 16220347 + 11836410'i32, -3979488, 26297894, 16080799, 23455045, 15735944, 1695823, + -8819122, 8169720, 16220347, ], xy2d: [ - -18115838'i32, 8653647, 17578566, -6092619, -8025777, - -16012763, -11144307, -2627664, -5990708, -14166033 - ] + -18115838'i32, 8653647, 17578566, -6092619, -8025777, -16012763, -11144307, + -2627664, -5990708, -14166033, + ], ), GePrecomp( yplusx: [ - -23308498'i32, -10968312, 15213228, -10081214, -30853605, - -11050004, 27884329, 2847284, 2655861, 1738395 + -23308498'i32, -10968312, 15213228, -10081214, -30853605, -11050004, 27884329, + 2847284, 2655861, 1738395, ], yminusx: [ - -27537433'i32, -14253021, -25336301, -8002780, -9370762, - 8129821, 21651608, -3239336, -19087449, -11005278 + -27537433'i32, -14253021, -25336301, -8002780, -9370762, 8129821, 21651608, + -3239336, -19087449, -11005278, ], xy2d: [ - 1533110'i32, 3437855, 23735889, 459276, 29970501, - 11335377, 26030092, 5821408, 10478196, 8544890 - ] + 1533110'i32, 3437855, 23735889, 459276, 29970501, 11335377, 26030092, 5821408, + 10478196, 8544890, + ], ), GePrecomp( yplusx: [ - 32173121'i32, -16129311, 24896207, 3921497, 22579056, - -3410854, 19270449, 12217473, 17789017, -3395995 + 32173121'i32, -16129311, 24896207, 3921497, 22579056, -3410854, 19270449, + 12217473, 17789017, -3395995, ], yminusx: [ - -30552961'i32, -2228401, -15578829, -10147201, 13243889, - 517024, 15479401, -3853233, 30460520, 1052596 + -30552961'i32, -2228401, -15578829, -10147201, 13243889, 517024, 15479401, + -3853233, 30460520, 1052596, ], xy2d: [ - -11614875'i32, 13323618, 32618793, 8175907, -15230173, - 12596687, 27491595, -4612359, 3179268, -9478891 - ] + -11614875'i32, 13323618, 32618793, 8175907, -15230173, 12596687, 27491595, + -4612359, 3179268, -9478891, + ], ), GePrecomp( yplusx: [ - 31947069'i32, -14366651, -4640583, -15339921, -15125977, - -6039709, -14756777, -16411740, 19072640, -9511060 + 31947069'i32, -14366651, -4640583, -15339921, -15125977, -6039709, -14756777, + -16411740, 19072640, -9511060, ], yminusx: [ - 11685058'i32, 11822410, 3158003, -13952594, 33402194, - -4165066, 5977896, -5215017, 473099, 5040608 + 11685058'i32, 11822410, 3158003, -13952594, 33402194, -4165066, 5977896, + -5215017, 473099, 5040608, ], xy2d: [ - -20290863'i32, 8198642, -27410132, 11602123, 1290375, - -2799760, 28326862, 1721092, -19558642, -3131606 - ] - ) + -20290863'i32, 8198642, -27410132, 11602123, 1290375, -2799760, 28326862, + 1721092, -19558642, -3131606, + ], + ), ], [ GePrecomp( yplusx: [ - 7881532'i32, 10687937, 7578723, 7738378, -18951012, - -2553952, 21820786, 8076149, -27868496, 11538389 + 7881532'i32, 10687937, 7578723, 7738378, -18951012, -2553952, 21820786, + 8076149, -27868496, 11538389, ], yminusx: [ - -19935666'i32, 3899861, 18283497, -6801568, -15728660, - -11249211, 8754525, 7446702, -5676054, 5797016 + -19935666'i32, 3899861, 18283497, -6801568, -15728660, -11249211, 8754525, + 7446702, -5676054, 5797016, ], xy2d: [ - -11295600'i32, -3793569, -15782110, -7964573, 12708869, - -8456199, 2014099, -9050574, -2369172, -5877341 - ] + -11295600'i32, -3793569, -15782110, -7964573, 12708869, -8456199, 2014099, + -9050574, -2369172, -5877341, + ], ), GePrecomp( yplusx: [ - -22472376'i32, -11568741, -27682020, 1146375, 18956691, - 16640559, 1192730, -3714199, 15123619, 10811505 + -22472376'i32, -11568741, -27682020, 1146375, 18956691, 16640559, 1192730, + -3714199, 15123619, 10811505, ], yminusx: [ - 14352098'i32, -3419715, -18942044, 10822655, 32750596, - 4699007, -70363, 15776356, -28886779, -11974553 + 14352098'i32, -3419715, -18942044, 10822655, 32750596, 4699007, -70363, + 15776356, -28886779, -11974553, ], xy2d: [ - -28241164'i32, -8072475, -4978962, -5315317, 29416931, - 1847569, -20654173, -16484855, 4714547, -9600655 - ] + -28241164'i32, -8072475, -4978962, -5315317, 29416931, 1847569, -20654173, + -16484855, 4714547, -9600655, + ], ), GePrecomp( yplusx: [ - 15200332'i32, 8368572, 19679101, 15970074, -31872674, - 1959451, 24611599, -4543832, -11745876, 12340220 + 15200332'i32, 8368572, 19679101, 15970074, -31872674, 1959451, 24611599, + -4543832, -11745876, 12340220, ], yminusx: [ - 12876937'i32, -10480056, 33134381, 6590940, -6307776, - 14872440, 9613953, 8241152, 15370987, 9608631 + 12876937'i32, -10480056, 33134381, 6590940, -6307776, 14872440, 9613953, + 8241152, 15370987, 9608631, ], xy2d: [ - -4143277'i32, -12014408, 8446281, -391603, 4407738, - 13629032, -7724868, 15866074, -28210621, -8814099 - ] + -4143277'i32, -12014408, 8446281, -391603, 4407738, 13629032, -7724868, + 15866074, -28210621, -8814099, + ], ), GePrecomp( yplusx: [ - 26660628'i32, -15677655, 8393734, 358047, -7401291, - 992988, -23904233, 858697, 20571223, 8420556 + 26660628'i32, -15677655, 8393734, 358047, -7401291, 992988, -23904233, 858697, + 20571223, 8420556, ], yminusx: [ - 14620715'i32, 13067227, -15447274, 8264467, 14106269, - 15080814, 33531827, 12516406, -21574435, -12476749 + 14620715'i32, 13067227, -15447274, 8264467, 14106269, 15080814, 33531827, + 12516406, -21574435, -12476749, ], xy2d: [ - 236881'i32, 10476226, 57258, -14677024, 6472998, - 2466984, 17258519, 7256740, 8791136, 15069930 - ] + 236881'i32, 10476226, 57258, -14677024, 6472998, 2466984, 17258519, 7256740, + 8791136, 15069930, + ], ), GePrecomp( yplusx: [ - 1276410'i32, -9371918, 22949635, -16322807, -23493039, - -5702186, 14711875, 4874229, -30663140, -2331391 + 1276410'i32, -9371918, 22949635, -16322807, -23493039, -5702186, 14711875, + 4874229, -30663140, -2331391, ], yminusx: [ - 5855666'i32, 4990204, -13711848, 7294284, -7804282, - 1924647, -1423175, -7912378, -33069337, 9234253 + 5855666'i32, 4990204, -13711848, 7294284, -7804282, 1924647, -1423175, + -7912378, -33069337, 9234253, ], xy2d: [ - 20590503'i32, -9018988, 31529744, -7352666, -2706834, - 10650548, 31559055, -11609587, 18979186, 13396066 - ] + 20590503'i32, -9018988, 31529744, -7352666, -2706834, 10650548, 31559055, + -11609587, 18979186, 13396066, + ], ), GePrecomp( yplusx: [ - 24474287'i32, 4968103, 22267082, 4407354, 24063882, - -8325180, -18816887, 13594782, 33514650, 7021958 + 24474287'i32, 4968103, 22267082, 4407354, 24063882, -8325180, -18816887, + 13594782, 33514650, 7021958, ], yminusx: [ - -11566906'i32, -6565505, -21365085, 15928892, -26158305, - 4315421, -25948728, -3916677, -21480480, 12868082 + -11566906'i32, -6565505, -21365085, 15928892, -26158305, 4315421, -25948728, + -3916677, -21480480, 12868082, ], xy2d: [ - -28635013'i32, 13504661, 19988037, -2132761, 21078225, - 6443208, -21446107, 2244500, -12455797, -8089383 - ] + -28635013'i32, 13504661, 19988037, -2132761, 21078225, 6443208, -21446107, + 2244500, -12455797, -8089383, + ], ), GePrecomp( yplusx: [ - -30595528'i32, 13793479, -5852820, 319136, -25723172, - -6263899, 33086546, 8957937, -15233648, 5540521 + -30595528'i32, 13793479, -5852820, 319136, -25723172, -6263899, 33086546, + 8957937, -15233648, 5540521, ], yminusx: [ - -11630176'i32, -11503902, -8119500, -7643073, 2620056, - 1022908, -23710744, -1568984, -16128528, -14962807 + -11630176'i32, -11503902, -8119500, -7643073, 2620056, 1022908, -23710744, + -1568984, -16128528, -14962807, ], xy2d: [ - 23152971'i32, 775386, 27395463, 14006635, -9701118, - 4649512, 1689819, 892185, -11513277, -15205948 - ] + 23152971'i32, 775386, 27395463, 14006635, -9701118, 4649512, 1689819, 892185, + -11513277, -15205948, + ], ), GePrecomp( yplusx: [ - 9770129'i32, 9586738, 26496094, 4324120, 1556511, - -3550024, 27453819, 4763127, -19179614, 5867134 + 9770129'i32, 9586738, 26496094, 4324120, 1556511, -3550024, 27453819, 4763127, + -19179614, 5867134, ], yminusx: [ - -32765025'i32, 1927590, 31726409, -4753295, 23962434, - -16019500, 27846559, 5931263, -29749703, -16108455 + -32765025'i32, 1927590, 31726409, -4753295, 23962434, -16019500, 27846559, + 5931263, -29749703, -16108455, ], xy2d: [ - 27461885'i32, -2977536, 22380810, 1815854, -23033753, - -3031938, 7283490, -15148073, -19526700, 7734629 - ] - ) + 27461885'i32, -2977536, 22380810, 1815854, -23033753, -3031938, 7283490, + -15148073, -19526700, 7734629, + ], + ), ], [ GePrecomp( yplusx: [ - -8010264'i32, -9590817, -11120403, 6196038, 29344158, - -13430885, 7585295, -3176626, 18549497, 15302069 + -8010264'i32, -9590817, -11120403, 6196038, 29344158, -13430885, 7585295, + -3176626, 18549497, 15302069, ], yminusx: [ - -32658337'i32, -6171222, -7672793, -11051681, 6258878, - 13504381, 10458790, -6418461, -8872242, 8424746 + -32658337'i32, -6171222, -7672793, -11051681, 6258878, 13504381, 10458790, + -6418461, -8872242, 8424746, ], xy2d: [ - 24687205'i32, 8613276, -30667046, -3233545, 1863892, - -1830544, 19206234, 7134917, -11284482, -828919 - ] + 24687205'i32, 8613276, -30667046, -3233545, 1863892, -1830544, 19206234, + 7134917, -11284482, -828919, + ], ), GePrecomp( yplusx: [ - 11334899'i32, -9218022, 8025293, 12707519, 17523892, - -10476071, 10243738, -14685461, -5066034, 16498837 + 11334899'i32, -9218022, 8025293, 12707519, 17523892, -10476071, 10243738, + -14685461, -5066034, 16498837, ], yminusx: [ - 8911542'i32, 6887158, -9584260, -6958590, 11145641, - -9543680, 17303925, -14124238, 6536641, 10543906 + 8911542'i32, 6887158, -9584260, -6958590, 11145641, -9543680, 17303925, + -14124238, 6536641, 10543906, ], xy2d: [ - -28946384'i32, 15479763, -17466835, 568876, -1497683, - 11223454, -2669190, -16625574, -27235709, 8876771 - ] + -28946384'i32, 15479763, -17466835, 568876, -1497683, 11223454, -2669190, + -16625574, -27235709, 8876771, + ], ), GePrecomp( yplusx: [ - -25742899'i32, -12566864, -15649966, -846607, -33026686, - -796288, -33481822, 15824474, -604426, -9039817 + -25742899'i32, -12566864, -15649966, -846607, -33026686, -796288, -33481822, + 15824474, -604426, -9039817, ], yminusx: [ - 10330056'i32, 70051, 7957388, -9002667, 9764902, - 15609756, 27698697, -4890037, 1657394, 3084098 + 10330056'i32, 70051, 7957388, -9002667, 9764902, 15609756, 27698697, -4890037, + 1657394, 3084098, ], xy2d: [ - 10477963'i32, -7470260, 12119566, -13250805, 29016247, - -5365589, 31280319, 14396151, -30233575, 15272409 - ] + 10477963'i32, -7470260, 12119566, -13250805, 29016247, -5365589, 31280319, + 14396151, -30233575, 15272409, + ], ), GePrecomp( yplusx: [ - -12288309'i32, 3169463, 28813183, 16658753, 25116432, - -5630466, -25173957, -12636138, -25014757, 1950504 + -12288309'i32, 3169463, 28813183, 16658753, 25116432, -5630466, -25173957, + -12636138, -25014757, 1950504, ], yminusx: [ - -26180358'i32, 9489187, 11053416, -14746161, -31053720, - 5825630, -8384306, -8767532, 15341279, 8373727 + -26180358'i32, 9489187, 11053416, -14746161, -31053720, 5825630, -8384306, + -8767532, 15341279, 8373727, ], xy2d: [ - 28685821'i32, 7759505, -14378516, -12002860, -31971820, - 4079242, 298136, -10232602, -2878207, 15190420 - ] + 28685821'i32, 7759505, -14378516, -12002860, -31971820, 4079242, 298136, + -10232602, -2878207, 15190420, + ], ), GePrecomp( yplusx: [ - -32932876'i32, 13806336, -14337485, -15794431, -24004620, - 10940928, 8669718, 2742393, -26033313, -6875003 + -32932876'i32, 13806336, -14337485, -15794431, -24004620, 10940928, 8669718, + 2742393, -26033313, -6875003, ], yminusx: [ - -1580388'i32, -11729417, -25979658, -11445023, -17411874, - -10912854, 9291594, -16247779, -12154742, 6048605 + -1580388'i32, -11729417, -25979658, -11445023, -17411874, -10912854, 9291594, + -16247779, -12154742, 6048605, ], xy2d: [ - -30305315'i32, 14843444, 1539301, 11864366, 20201677, - 1900163, 13934231, 5128323, 11213262, 9168384 - ] + -30305315'i32, 14843444, 1539301, 11864366, 20201677, 1900163, 13934231, + 5128323, 11213262, 9168384, + ], ), GePrecomp( yplusx: [ - -26280513'i32, 11007847, 19408960, -940758, -18592965, - -4328580, -5088060, -11105150, 20470157, -16398701 + -26280513'i32, 11007847, 19408960, -940758, -18592965, -4328580, -5088060, + -11105150, 20470157, -16398701, ], yminusx: [ - -23136053'i32, 9282192, 14855179, -15390078, -7362815, - -14408560, -22783952, 14461608, 14042978, 5230683 + -23136053'i32, 9282192, 14855179, -15390078, -7362815, -14408560, -22783952, + 14461608, 14042978, 5230683, ], xy2d: [ - 29969567'i32, -2741594, -16711867, -8552442, 9175486, - -2468974, 21556951, 3506042, -5933891, -12449708 - ] + 29969567'i32, -2741594, -16711867, -8552442, 9175486, -2468974, 21556951, + 3506042, -5933891, -12449708, + ], ), GePrecomp( yplusx: [ - -3144746'i32, 8744661, 19704003, 4581278, -20430686, - 6830683, -21284170, 8971513, -28539189, 15326563 + -3144746'i32, 8744661, 19704003, 4581278, -20430686, 6830683, -21284170, + 8971513, -28539189, 15326563, ], yminusx: [ - -19464629'i32, 10110288, -17262528, -3503892, -23500387, - 1355669, -15523050, 15300988, -20514118, 9168260 + -19464629'i32, 10110288, -17262528, -3503892, -23500387, 1355669, -15523050, + 15300988, -20514118, 9168260, ], xy2d: [ - -5353335'i32, 4488613, -23803248, 16314347, 7780487, - -15638939, -28948358, 9601605, 33087103, -9011387 - ] + -5353335'i32, 4488613, -23803248, 16314347, 7780487, -15638939, -28948358, + 9601605, 33087103, -9011387, + ], ), GePrecomp( yplusx: [ - -19443170'i32, -15512900, -20797467, -12445323, -29824447, - 10229461, -27444329, -15000531, -5996870, 15664672 + -19443170'i32, -15512900, -20797467, -12445323, -29824447, 10229461, + -27444329, -15000531, -5996870, 15664672, ], yminusx: [ - 23294591'i32, -16632613, -22650781, -8470978, 27844204, - 11461195, 13099750, -2460356, 18151676, 13417686 + 23294591'i32, -16632613, -22650781, -8470978, 27844204, 11461195, 13099750, + -2460356, 18151676, 13417686, ], xy2d: [ - -24722913'i32, -4176517, -31150679, 5988919, -26858785, - 6685065, 1661597, -12551441, 15271676, -15452665 - ] - ) + -24722913'i32, -4176517, -31150679, 5988919, -26858785, 6685065, 1661597, + -12551441, 15271676, -15452665, + ], + ), ], [ GePrecomp( yplusx: [ - 11433042'i32, -13228665, 8239631, -5279517, -1985436, - -725718, -18698764, 2167544, -6921301, -13440182 + 11433042'i32, -13228665, 8239631, -5279517, -1985436, -725718, -18698764, + 2167544, -6921301, -13440182, ], yminusx: [ - -31436171'i32, 15575146, 30436815, 12192228, -22463353, - 9395379, -9917708, -8638997, 12215110, 12028277 + -31436171'i32, 15575146, 30436815, 12192228, -22463353, 9395379, -9917708, + -8638997, 12215110, 12028277, ], xy2d: [ - 14098400'i32, 6555944, 23007258, 5757252, -15427832, - -12950502, 30123440, 4617780, -16900089, -655628 - ] + 14098400'i32, 6555944, 23007258, 5757252, -15427832, -12950502, 30123440, + 4617780, -16900089, -655628, + ], ), GePrecomp( yplusx: [ - -4026201'i32, -15240835, 11893168, 13718664, -14809462, - 1847385, -15819999, 10154009, 23973261, -12684474 + -4026201'i32, -15240835, 11893168, 13718664, -14809462, 1847385, -15819999, + 10154009, 23973261, -12684474, ], yminusx: [ - -26531820'i32, -3695990, -1908898, 2534301, -31870557, - -16550355, 18341390, -11419951, 32013174, -10103539 + -26531820'i32, -3695990, -1908898, 2534301, -31870557, -16550355, 18341390, + -11419951, 32013174, -10103539, ], xy2d: [ - -25479301'i32, 10876443, -11771086, -14625140, -12369567, - 1838104, 21911214, 6354752, 4425632, -837822 - ] + -25479301'i32, 10876443, -11771086, -14625140, -12369567, 1838104, 21911214, + 6354752, 4425632, -837822, + ], ), GePrecomp( yplusx: [ - -10433389'i32, -14612966, 22229858, -3091047, -13191166, - 776729, -17415375, -12020462, 4725005, 14044970 + -10433389'i32, -14612966, 22229858, -3091047, -13191166, 776729, -17415375, + -12020462, 4725005, 14044970, ], yminusx: [ - 19268650'i32, -7304421, 1555349, 8692754, -21474059, - -9910664, 6347390, -1411784, -19522291, -16109756 + 19268650'i32, -7304421, 1555349, 8692754, -21474059, -9910664, 6347390, + -1411784, -19522291, -16109756, ], xy2d: [ - -24864089'i32, 12986008, -10898878, -5558584, -11312371, - -148526, 19541418, 8180106, 9282262, 10282508 - ] + -24864089'i32, 12986008, -10898878, -5558584, -11312371, -148526, 19541418, + 8180106, 9282262, 10282508, + ], ), GePrecomp( yplusx: [ - -26205082'i32, 4428547, -8661196, -13194263, 4098402, - -14165257, 15522535, 8372215, 5542595, -10702683 + -26205082'i32, 4428547, -8661196, -13194263, 4098402, -14165257, 15522535, + 8372215, 5542595, -10702683, ], yminusx: [ - -10562541'i32, 14895633, 26814552, -16673850, -17480754, - -2489360, -2781891, 6993761, -18093885, 10114655 + -10562541'i32, 14895633, 26814552, -16673850, -17480754, -2489360, -2781891, + 6993761, -18093885, 10114655, ], xy2d: [ - -20107055'i32, -929418, 31422704, 10427861, -7110749, - 6150669, -29091755, -11529146, 25953725, -106158 - ] + -20107055'i32, -929418, 31422704, 10427861, -7110749, 6150669, -29091755, + -11529146, 25953725, -106158, + ], ), GePrecomp( yplusx: [ - -4234397'i32, -8039292, -9119125, 3046000, 2101609, - -12607294, 19390020, 6094296, -3315279, 12831125 + -4234397'i32, -8039292, -9119125, 3046000, 2101609, -12607294, 19390020, + 6094296, -3315279, 12831125, ], yminusx: [ - -15998678'i32, 7578152, 5310217, 14408357, -33548620, - -224739, 31575954, 6326196, 7381791, -2421839 + -15998678'i32, 7578152, 5310217, 14408357, -33548620, -224739, 31575954, + 6326196, 7381791, -2421839, ], xy2d: [ - -20902779'i32, 3296811, 24736065, -16328389, 18374254, - 7318640, 6295303, 8082724, -15362489, 12339664 - ] + -20902779'i32, 3296811, 24736065, -16328389, 18374254, 7318640, 6295303, + 8082724, -15362489, 12339664, + ], ), GePrecomp( yplusx: [ - 27724736'i32, 2291157, 6088201, -14184798, 1792727, - 5857634, 13848414, 15768922, 25091167, 14856294 + 27724736'i32, 2291157, 6088201, -14184798, 1792727, 5857634, 13848414, + 15768922, 25091167, 14856294, ], yminusx: [ - -18866652'i32, 8331043, 24373479, 8541013, -701998, - -9269457, 12927300, -12695493, -22182473, -9012899 + -18866652'i32, 8331043, 24373479, 8541013, -701998, -9269457, 12927300, + -12695493, -22182473, -9012899, ], xy2d: [ - -11423429'i32, -5421590, 11632845, 3405020, 30536730, - -11674039, -27260765, 13866390, 30146206, 9142070 - ] + -11423429'i32, -5421590, 11632845, 3405020, 30536730, -11674039, -27260765, + 13866390, 30146206, 9142070, + ], ), GePrecomp( yplusx: [ - 3924129'i32, -15307516, -13817122, -10054960, 12291820, - -668366, -27702774, 9326384, -8237858, 4171294 + 3924129'i32, -15307516, -13817122, -10054960, 12291820, -668366, -27702774, + 9326384, -8237858, 4171294, ], yminusx: [ - -15921940'i32, 16037937, 6713787, 16606682, -21612135, - 2790944, 26396185, 3731949, 345228, -5462949 + -15921940'i32, 16037937, 6713787, 16606682, -21612135, 2790944, 26396185, + 3731949, 345228, -5462949, ], xy2d: [ - -21327538'i32, 13448259, 25284571, 1143661, 20614966, - -8849387, 2031539, -12391231, -16253183, -13582083 - ] + -21327538'i32, 13448259, 25284571, 1143661, 20614966, -8849387, 2031539, + -12391231, -16253183, -13582083, + ], ), GePrecomp( yplusx: [ - 31016211'i32, -16722429, 26371392, -14451233, -5027349, - 14854137, 17477601, 3842657, 28012650, -16405420 + 31016211'i32, -16722429, 26371392, -14451233, -5027349, 14854137, 17477601, + 3842657, 28012650, -16405420, ], yminusx: [ - -5075835'i32, 9368966, -8562079, -4600902, -15249953, - 6970560, -9189873, 16292057, -8867157, 3507940 + -5075835'i32, 9368966, -8562079, -4600902, -15249953, 6970560, -9189873, + 16292057, -8867157, 3507940, ], xy2d: [ - 29439664'i32, 3537914, 23333589, 6997794, -17555561, - -11018068, -15209202, -15051267, -9164929, 6580396 - ] - ) + 29439664'i32, 3537914, 23333589, 6997794, -17555561, -11018068, -15209202, + -15051267, -9164929, 6580396, + ], + ), ], [ GePrecomp( yplusx: [ - -12185861'i32, -7679788, 16438269, 10826160, -8696817, - -6235611, 17860444, -9273846, -2095802, 9304567 + -12185861'i32, -7679788, 16438269, 10826160, -8696817, -6235611, 17860444, + -9273846, -2095802, 9304567, ], yminusx: [ - 20714564'i32, -4336911, 29088195, 7406487, 11426967, - -5095705, 14792667, -14608617, 5289421, -477127 + 20714564'i32, -4336911, 29088195, 7406487, 11426967, -5095705, 14792667, + -14608617, 5289421, -477127, ], xy2d: [ - -16665533'i32, -10650790, -6160345, -13305760, 9192020, - -1802462, 17271490, 12349094, 26939669, -3752294 - ] + -16665533'i32, -10650790, -6160345, -13305760, 9192020, -1802462, 17271490, + 12349094, 26939669, -3752294, + ], ), GePrecomp( yplusx: [ - -12889898'i32, 9373458, 31595848, 16374215, 21471720, - 13221525, -27283495, -12348559, -3698806, 117887 + -12889898'i32, 9373458, 31595848, 16374215, 21471720, 13221525, -27283495, + -12348559, -3698806, 117887, ], yminusx: [ - 22263325'i32, -6560050, 3984570, -11174646, -15114008, - -566785, 28311253, 5358056, -23319780, 541964 + 22263325'i32, -6560050, 3984570, -11174646, -15114008, -566785, 28311253, + 5358056, -23319780, 541964, ], xy2d: [ - 16259219'i32, 3261970, 2309254, -15534474, -16885711, - -4581916, 24134070, -16705829, -13337066, -13552195 - ] + 16259219'i32, 3261970, 2309254, -15534474, -16885711, -4581916, 24134070, + -16705829, -13337066, -13552195, + ], ), GePrecomp( yplusx: [ - 9378160'i32, -13140186, -22845982, -12745264, 28198281, - -7244098, -2399684, -717351, 690426, 14876244 + 9378160'i32, -13140186, -22845982, -12745264, 28198281, -7244098, -2399684, + -717351, 690426, 14876244, ], yminusx: [ - 24977353'i32, -314384, -8223969, -13465086, 28432343, - -1176353, -13068804, -12297348, -22380984, 6618999 + 24977353'i32, -314384, -8223969, -13465086, 28432343, -1176353, -13068804, + -12297348, -22380984, 6618999, ], xy2d: [ - -1538174'i32, 11685646, 12944378, 13682314, -24389511, - -14413193, 8044829, -13817328, 32239829, -5652762 - ] + -1538174'i32, 11685646, 12944378, 13682314, -24389511, -14413193, 8044829, + -13817328, 32239829, -5652762, + ], ), GePrecomp( yplusx: [ - -18603066'i32, 4762990, -926250, 8885304, -28412480, - -3187315, 9781647, -10350059, 32779359, 5095274 + -18603066'i32, 4762990, -926250, 8885304, -28412480, -3187315, 9781647, + -10350059, 32779359, 5095274, ], yminusx: [ - -33008130'i32, -5214506, -32264887, -3685216, 9460461, - -9327423, -24601656, 14506724, 21639561, -2630236 + -33008130'i32, -5214506, -32264887, -3685216, 9460461, -9327423, -24601656, + 14506724, 21639561, -2630236, ], xy2d: [ - -16400943'i32, -13112215, 25239338, 15531969, 3987758, - -4499318, -1289502, -6863535, 17874574, 558605 - ] + -16400943'i32, -13112215, 25239338, 15531969, 3987758, -4499318, -1289502, + -6863535, 17874574, 558605, + ], ), GePrecomp( yplusx: [ - -13600129'i32, 10240081, 9171883, 16131053, -20869254, - 9599700, 33499487, 5080151, 2085892, 5119761 + -13600129'i32, 10240081, 9171883, 16131053, -20869254, 9599700, 33499487, + 5080151, 2085892, 5119761, ], yminusx: [ - -22205145'i32, -2519528, -16381601, 414691, -25019550, - 2170430, 30634760, -8363614, -31999993, -5759884 + -22205145'i32, -2519528, -16381601, 414691, -25019550, 2170430, 30634760, + -8363614, -31999993, -5759884, ], xy2d: [ - -6845704'i32, 15791202, 8550074, -1312654, 29928809, - -12092256, 27534430, -7192145, -22351378, 12961482 - ] + -6845704'i32, 15791202, 8550074, -1312654, 29928809, -12092256, 27534430, + -7192145, -22351378, 12961482, + ], ), GePrecomp( yplusx: [ - -24492060'i32, -9570771, 10368194, 11582341, -23397293, - -2245287, 16533930, 8206996, -30194652, -5159638 + -24492060'i32, -9570771, 10368194, 11582341, -23397293, -2245287, 16533930, + 8206996, -30194652, -5159638, ], yminusx: [ - -11121496'i32, -3382234, 2307366, 6362031, -135455, - 8868177, -16835630, 7031275, 7589640, 8945490 + -11121496'i32, -3382234, 2307366, 6362031, -135455, 8868177, -16835630, + 7031275, 7589640, 8945490, ], xy2d: [ - -32152748'i32, 8917967, 6661220, -11677616, -1192060, - -15793393, 7251489, -11182180, 24099109, -14456170 - ] + -32152748'i32, 8917967, 6661220, -11677616, -1192060, -15793393, 7251489, + -11182180, 24099109, -14456170, + ], ), GePrecomp( yplusx: [ - 5019558'i32, -7907470, 4244127, -14714356, -26933272, - 6453165, -19118182, -13289025, -6231896, -10280736 + 5019558'i32, -7907470, 4244127, -14714356, -26933272, 6453165, -19118182, + -13289025, -6231896, -10280736, ], yminusx: [ - 10853594'i32, 10721687, 26480089, 5861829, -22995819, - 1972175, -1866647, -10557898, -3363451, -6441124 + 10853594'i32, 10721687, 26480089, 5861829, -22995819, 1972175, -1866647, + -10557898, -3363451, -6441124, ], xy2d: [ - -17002408'i32, 5906790, 221599, -6563147, 7828208, - -13248918, 24362661, -2008168, -13866408, 7421392 - ] + -17002408'i32, 5906790, 221599, -6563147, 7828208, -13248918, 24362661, + -2008168, -13866408, 7421392, + ], ), GePrecomp( yplusx: [ - 8139927'i32, -6546497, 32257646, -5890546, 30375719, - 1886181, -21175108, 15441252, 28826358, -4123029 + 8139927'i32, -6546497, 32257646, -5890546, 30375719, 1886181, -21175108, + 15441252, 28826358, -4123029, ], yminusx: [ - 6267086'i32, 9695052, 7709135, -16603597, -32869068, - -1886135, 14795160, -7840124, 13746021, -1742048 + 6267086'i32, 9695052, 7709135, -16603597, -32869068, -1886135, 14795160, + -7840124, 13746021, -1742048, ], xy2d: [ - 28584902'i32, 7787108, -6732942, -15050729, 22846041, - -7571236, -3181936, -363524, 4771362, -8419958 - ] - ) + 28584902'i32, 7787108, -6732942, -15050729, 22846041, -7571236, -3181936, + -363524, 4771362, -8419958, + ], + ), ], [ GePrecomp( yplusx: [ - 24949256'i32, 6376279, -27466481, -8174608, -18646154, - -9930606, 33543569, -12141695, 3569627, 11342593 + 24949256'i32, 6376279, -27466481, -8174608, -18646154, -9930606, 33543569, + -12141695, 3569627, 11342593, ], yminusx: [ - 26514989'i32, 4740088, 27912651, 3697550, 19331575, - -11472339, 6809886, 4608608, 7325975, -14801071 + 26514989'i32, 4740088, 27912651, 3697550, 19331575, -11472339, 6809886, + 4608608, 7325975, -14801071, ], xy2d: [ - -11618399'i32, -14554430, -24321212, 7655128, -1369274, - 5214312, -27400540, 10258390, -17646694, -8186692 - ] + -11618399'i32, -14554430, -24321212, 7655128, -1369274, 5214312, -27400540, + 10258390, -17646694, -8186692, + ], ), GePrecomp( yplusx: [ - 11431204'i32, 15823007, 26570245, 14329124, 18029990, - 4796082, -31446179, 15580664, 9280358, -3973687 + 11431204'i32, 15823007, 26570245, 14329124, 18029990, 4796082, -31446179, + 15580664, 9280358, -3973687, ], yminusx: [ - -160783'i32, -10326257, -22855316, -4304997, -20861367, - -13621002, -32810901, -11181622, -15545091, 4387441 + -160783'i32, -10326257, -22855316, -4304997, -20861367, -13621002, -32810901, + -11181622, -15545091, 4387441, ], xy2d: [ - -20799378'i32, 12194512, 3937617, -5805892, -27154820, - 9340370, -24513992, 8548137, 20617071, -7482001 - ] + -20799378'i32, 12194512, 3937617, -5805892, -27154820, 9340370, -24513992, + 8548137, 20617071, -7482001, + ], ), GePrecomp( yplusx: [ - -938825'i32, -3930586, -8714311, 16124718, 24603125, - -6225393, -13775352, -11875822, 24345683, 10325460 + -938825'i32, -3930586, -8714311, 16124718, 24603125, -6225393, -13775352, + -11875822, 24345683, 10325460, ], yminusx: [ - -19855277'i32, -1568885, -22202708, 8714034, 14007766, - 6928528, 16318175, -1010689, 4766743, 3552007 + -19855277'i32, -1568885, -22202708, 8714034, 14007766, 6928528, 16318175, + -1010689, 4766743, 3552007, ], xy2d: [ - -21751364'i32, -16730916, 1351763, -803421, -4009670, - 3950935, 3217514, 14481909, 10988822, -3994762 - ] + -21751364'i32, -16730916, 1351763, -803421, -4009670, 3950935, 3217514, + 14481909, 10988822, -3994762, + ], ), GePrecomp( yplusx: [ - 15564307'i32, -14311570, 3101243, 5684148, 30446780, - -8051356, 12677127, -6505343, -8295852, 13296005 + 15564307'i32, -14311570, 3101243, 5684148, 30446780, -8051356, 12677127, + -6505343, -8295852, 13296005, ], yminusx: [ - -9442290'i32, 6624296, -30298964, -11913677, -4670981, - -2057379, 31521204, 9614054, -30000824, 12074674 + -9442290'i32, 6624296, -30298964, -11913677, -4670981, -2057379, 31521204, + 9614054, -30000824, 12074674, ], xy2d: [ - 4771191'i32, -135239, 14290749, -13089852, 27992298, - 14998318, -1413936, -1556716, 29832613, -16391035 - ] + 4771191'i32, -135239, 14290749, -13089852, 27992298, 14998318, -1413936, + -1556716, 29832613, -16391035, + ], ), GePrecomp( yplusx: [ - 7064884'i32, -7541174, -19161962, -5067537, -18891269, - -2912736, 25825242, 5293297, -27122660, 13101590 + 7064884'i32, -7541174, -19161962, -5067537, -18891269, -2912736, 25825242, + 5293297, -27122660, 13101590, ], yminusx: [ - -2298563'i32, 2439670, -7466610, 1719965, -27267541, - -16328445, 32512469, -5317593, -30356070, -4190957 + -2298563'i32, 2439670, -7466610, 1719965, -27267541, -16328445, 32512469, + -5317593, -30356070, -4190957, ], xy2d: [ - -30006540'i32, 10162316, -33180176, 3981723, -16482138, - -13070044, 14413974, 9515896, 19568978, 9628812 - ] + -30006540'i32, 10162316, -33180176, 3981723, -16482138, -13070044, 14413974, + 9515896, 19568978, 9628812, + ], ), GePrecomp( yplusx: [ - 33053803'i32, 199357, 15894591, 1583059, 27380243, - -4580435, -17838894, -6106839, -6291786, 3437740 + 33053803'i32, 199357, 15894591, 1583059, 27380243, -4580435, -17838894, + -6106839, -6291786, 3437740, ], yminusx: [ - -18978877'i32, 3884493, 19469877, 12726490, 15913552, - 13614290, -22961733, 70104, 7463304, 4176122 + -18978877'i32, 3884493, 19469877, 12726490, 15913552, 13614290, -22961733, + 70104, 7463304, 4176122, ], xy2d: [ - -27124001'i32, 10659917, 11482427, -16070381, 12771467, - -6635117, -32719404, -5322751, 24216882, 5944158 - ] + -27124001'i32, 10659917, 11482427, -16070381, 12771467, -6635117, -32719404, + -5322751, 24216882, 5944158, + ], ), GePrecomp( yplusx: [ - 8894125'i32, 7450974, -2664149, -9765752, -28080517, - -12389115, 19345746, 14680796, 11632993, 5847885 + 8894125'i32, 7450974, -2664149, -9765752, -28080517, -12389115, 19345746, + 14680796, 11632993, 5847885, ], yminusx: [ - 26942781'i32, -2315317, 9129564, -4906607, 26024105, - 11769399, -11518837, 6367194, -9727230, 4782140 + 26942781'i32, -2315317, 9129564, -4906607, 26024105, 11769399, -11518837, + 6367194, -9727230, 4782140, ], xy2d: [ - 19916461'i32, -4828410, -22910704, -11414391, 25606324, - -5972441, 33253853, 8220911, 6358847, -1873857 - ] + 19916461'i32, -4828410, -22910704, -11414391, 25606324, -5972441, 33253853, + 8220911, 6358847, -1873857, + ], ), GePrecomp( yplusx: [ - 801428'i32, -2081702, 16569428, 11065167, 29875704, - 96627, 7908388, -4480480, -13538503, 1387155 + 801428'i32, -2081702, 16569428, 11065167, 29875704, 96627, 7908388, -4480480, + -13538503, 1387155, ], yminusx: [ - 19646058'i32, 5720633, -11416706, 12814209, 11607948, - 12749789, 14147075, 15156355, -21866831, 11835260 + 19646058'i32, 5720633, -11416706, 12814209, 11607948, 12749789, 14147075, + 15156355, -21866831, 11835260, ], xy2d: [ - 19299512'i32, 1155910, 28703737, 14890794, 2925026, - 7269399, 26121523, 15467869, -26560550, 5052483 - ] - ) + 19299512'i32, 1155910, 28703737, 14890794, 2925026, 7269399, 26121523, + 15467869, -26560550, 5052483, + ], + ), ], [ GePrecomp( yplusx: [ - -3017432'i32, 10058206, 1980837, 3964243, 22160966, - 12322533, -6431123, -12618185, 12228557, -7003677 + -3017432'i32, 10058206, 1980837, 3964243, 22160966, 12322533, -6431123, + -12618185, 12228557, -7003677, ], yminusx: [ - 32944382'i32, 14922211, -22844894, 5188528, 21913450, - -8719943, 4001465, 13238564, -6114803, 8653815 + 32944382'i32, 14922211, -22844894, 5188528, 21913450, -8719943, 4001465, + 13238564, -6114803, 8653815, ], xy2d: [ - 22865569'i32, -4652735, 27603668, -12545395, 14348958, - 8234005, 24808405, 5719875, 28483275, 2841751 - ] + 22865569'i32, -4652735, 27603668, -12545395, 14348958, 8234005, 24808405, + 5719875, 28483275, 2841751, + ], ), GePrecomp( yplusx: [ - -16420968'i32, -1113305, -327719, -12107856, 21886282, - -15552774, -1887966, -315658, 19932058, -12739203 + -16420968'i32, -1113305, -327719, -12107856, 21886282, -15552774, -1887966, + -315658, 19932058, -12739203, ], yminusx: [ - -11656086'i32, 10087521, -8864888, -5536143, -19278573, - -3055912, 3999228, 13239134, -4777469, -13910208 + -11656086'i32, 10087521, -8864888, -5536143, -19278573, -3055912, 3999228, + 13239134, -4777469, -13910208, ], xy2d: [ - 1382174'i32, -11694719, 17266790, 9194690, -13324356, - 9720081, 20403944, 11284705, -14013818, 3093230 - ] + 1382174'i32, -11694719, 17266790, 9194690, -13324356, 9720081, 20403944, + 11284705, -14013818, 3093230, + ], ), GePrecomp( yplusx: [ - 16650921'i32, -11037932, -1064178, 1570629, -8329746, - 7352753, -302424, 16271225, -24049421, -6691850 + 16650921'i32, -11037932, -1064178, 1570629, -8329746, 7352753, -302424, + 16271225, -24049421, -6691850, ], yminusx: [ - -21911077'i32, -5927941, -4611316, -5560156, -31744103, - -10785293, 24123614, 15193618, -21652117, -16739389 + -21911077'i32, -5927941, -4611316, -5560156, -31744103, -10785293, 24123614, + 15193618, -21652117, -16739389, ], xy2d: [ - -9935934'i32, -4289447, -25279823, 4372842, 2087473, - 10399484, 31870908, 14690798, 17361620, 11864968 - ] + -9935934'i32, -4289447, -25279823, 4372842, 2087473, 10399484, 31870908, + 14690798, 17361620, 11864968, + ], ), GePrecomp( yplusx: [ - -11307610'i32, 6210372, 13206574, 5806320, -29017692, - -13967200, -12331205, -7486601, -25578460, -16240689 + -11307610'i32, 6210372, 13206574, 5806320, -29017692, -13967200, -12331205, + -7486601, -25578460, -16240689, ], yminusx: [ - 14668462'i32, -12270235, 26039039, 15305210, 25515617, - 4542480, 10453892, 6577524, 9145645, -6443880 + 14668462'i32, -12270235, 26039039, 15305210, 25515617, 4542480, 10453892, + 6577524, 9145645, -6443880, ], xy2d: [ - 5974874'i32, 3053895, -9433049, -10385191, -31865124, - 3225009, -7972642, 3936128, -5652273, -3050304 - ] + 5974874'i32, 3053895, -9433049, -10385191, -31865124, 3225009, -7972642, + 3936128, -5652273, -3050304, + ], ), GePrecomp( yplusx: [ - 30625386'i32, -4729400, -25555961, -12792866, -20484575, - 7695099, 17097188, -16303496, -27999779, 1803632 + 30625386'i32, -4729400, -25555961, -12792866, -20484575, 7695099, 17097188, + -16303496, -27999779, 1803632, ], yminusx: [ - -3553091'i32, 9865099, -5228566, 4272701, -5673832, - -16689700, 14911344, 12196514, -21405489, 7047412 + -3553091'i32, 9865099, -5228566, 4272701, -5673832, -16689700, 14911344, + 12196514, -21405489, 7047412, ], xy2d: [ - 20093277'i32, 9920966, -11138194, -5343857, 13161587, - 12044805, -32856851, 4124601, -32343828, -10257566 - ] + 20093277'i32, 9920966, -11138194, -5343857, 13161587, 12044805, -32856851, + 4124601, -32343828, -10257566, + ], ), GePrecomp( yplusx: [ - -20788824'i32, 14084654, -13531713, 7842147, 19119038, - -13822605, 4752377, -8714640, -21679658, 2288038 + -20788824'i32, 14084654, -13531713, 7842147, 19119038, -13822605, 4752377, + -8714640, -21679658, 2288038, ], yminusx: [ - -26819236'i32, -3283715, 29965059, 3039786, -14473765, - 2540457, 29457502, 14625692, -24819617, 12570232 + -26819236'i32, -3283715, 29965059, 3039786, -14473765, 2540457, 29457502, + 14625692, -24819617, 12570232, ], xy2d: [ - -1063558'i32, -11551823, 16920318, 12494842, 1278292, - -5869109, -21159943, -3498680, -11974704, 4724943 - ] + -1063558'i32, -11551823, 16920318, 12494842, 1278292, -5869109, -21159943, + -3498680, -11974704, 4724943, + ], ), GePrecomp( yplusx: [ - 17960970'i32, -11775534, -4140968, -9702530, -8876562, - -1410617, -12907383, -8659932, -29576300, 1903856 + 17960970'i32, -11775534, -4140968, -9702530, -8876562, -1410617, -12907383, + -8659932, -29576300, 1903856, ], yminusx: [ - 23134274'i32, -14279132, -10681997, -1611936, 20684485, - 15770816, -12989750, 3190296, 26955097, 14109738 + 23134274'i32, -14279132, -10681997, -1611936, 20684485, 15770816, -12989750, + 3190296, 26955097, 14109738, ], xy2d: [ - 15308788'i32, 5320727, -30113809, -14318877, 22902008, - 7767164, 29425325, -11277562, 31960942, 11934971 - ] + 15308788'i32, 5320727, -30113809, -14318877, 22902008, 7767164, 29425325, + -11277562, 31960942, 11934971, + ], ), GePrecomp( yplusx: [ - -27395711'i32, 8435796, 4109644, 12222639, -24627868, - 14818669, 20638173, 4875028, 10491392, 1379718 + -27395711'i32, 8435796, 4109644, 12222639, -24627868, 14818669, 20638173, + 4875028, 10491392, 1379718, ], yminusx: [ - -13159415'i32, 9197841, 3875503, -8936108, -1383712, - -5879801, 33518459, 16176658, 21432314, 12180697 + -13159415'i32, 9197841, 3875503, -8936108, -1383712, -5879801, 33518459, + 16176658, 21432314, 12180697, ], xy2d: [ - -11787308'i32, 11500838, 13787581, -13832590, -22430679, - 10140205, 1465425, 12689540, -10301319, -13872883 - ] - ) + -11787308'i32, 11500838, 13787581, -13832590, -22430679, 10140205, 1465425, + 12689540, -10301319, -13872883, + ], + ), ], [ GePrecomp( yplusx: [ - 5414091'i32, -15386041, -21007664, 9643570, 12834970, - 1186149, -2622916, -1342231, 26128231, 6032912 + 5414091'i32, -15386041, -21007664, 9643570, 12834970, 1186149, -2622916, + -1342231, 26128231, 6032912, ], yminusx: [ - -26337395'i32, -13766162, 32496025, -13653919, 17847801, - -12669156, 3604025, 8316894, -25875034, -10437358 + -26337395'i32, -13766162, 32496025, -13653919, 17847801, -12669156, 3604025, + 8316894, -25875034, -10437358, ], xy2d: [ - 3296484'i32, 6223048, 24680646, -12246460, -23052020, - 5903205, -8862297, -4639164, 12376617, 3188849 - ] + 3296484'i32, 6223048, 24680646, -12246460, -23052020, 5903205, -8862297, + -4639164, 12376617, 3188849, + ], ), GePrecomp( yplusx: [ - 29190488'i32, -14659046, 27549113, -1183516, 3520066, - -10697301, 32049515, -7309113, -16109234, -9852307 + 29190488'i32, -14659046, 27549113, -1183516, 3520066, -10697301, 32049515, + -7309113, -16109234, -9852307, ], yminusx: [ - -14744486'i32, -9309156, 735818, -598978, -20407687, - -5057904, 25246078, -15795669, 18640741, -960977 + -14744486'i32, -9309156, 735818, -598978, -20407687, -5057904, 25246078, + -15795669, 18640741, -960977, ], xy2d: [ - -6928835'i32, -16430795, 10361374, 5642961, 4910474, - 12345252, -31638386, -494430, 10530747, 1053335 - ] + -6928835'i32, -16430795, 10361374, 5642961, 4910474, 12345252, -31638386, + -494430, 10530747, 1053335, + ], ), GePrecomp( yplusx: [ - -29265967'i32, -14186805, -13538216, -12117373, -19457059, - -10655384, -31462369, -2948985, 24018831, 15026644 + -29265967'i32, -14186805, -13538216, -12117373, -19457059, -10655384, + -31462369, -2948985, 24018831, 15026644, ], yminusx: [ - -22592535'i32, -3145277, -2289276, 5953843, -13440189, - 9425631, 25310643, 13003497, -2314791, -15145616 + -22592535'i32, -3145277, -2289276, 5953843, -13440189, 9425631, 25310643, + 13003497, -2314791, -15145616, ], xy2d: [ - -27419985'i32, -603321, -8043984, -1669117, -26092265, - 13987819, -27297622, 187899, -23166419, -2531735 - ] + -27419985'i32, -603321, -8043984, -1669117, -26092265, 13987819, -27297622, + 187899, -23166419, -2531735, + ], ), GePrecomp( yplusx: [ - -21744398'i32, -13810475, 1844840, 5021428, -10434399, - -15911473, 9716667, 16266922, -5070217, 726099 + -21744398'i32, -13810475, 1844840, 5021428, -10434399, -15911473, 9716667, + 16266922, -5070217, 726099, ], yminusx: [ - 29370922'i32, -6053998, 7334071, -15342259, 9385287, - 2247707, -13661962, -4839461, 30007388, -15823341 + 29370922'i32, -6053998, 7334071, -15342259, 9385287, 2247707, -13661962, + -4839461, 30007388, -15823341, ], xy2d: [ - -936379'i32, 16086691, 23751945, -543318, -1167538, - -5189036, 9137109, 730663, 9835848, 4555336 - ] + -936379'i32, 16086691, 23751945, -543318, -1167538, -5189036, 9137109, 730663, + 9835848, 4555336, + ], ), GePrecomp( yplusx: [ - -23376435'i32, 1410446, -22253753, -12899614, 30867635, - 15826977, 17693930, 544696, -11985298, 12422646 + -23376435'i32, 1410446, -22253753, -12899614, 30867635, 15826977, 17693930, + 544696, -11985298, 12422646, ], yminusx: [ - 31117226'i32, -12215734, -13502838, 6561947, -9876867, - -12757670, -5118685, -4096706, 29120153, 13924425 + 31117226'i32, -12215734, -13502838, 6561947, -9876867, -12757670, -5118685, + -4096706, 29120153, 13924425, ], xy2d: [ - -17400879'i32, -14233209, 19675799, -2734756, -11006962, - -5858820, -9383939, -11317700, 7240931, -237388 - ] + -17400879'i32, -14233209, 19675799, -2734756, -11006962, -5858820, -9383939, + -11317700, 7240931, -237388, + ], ), GePrecomp( yplusx: [ - -31361739'i32, -11346780, -15007447, -5856218, -22453340, - -12152771, 1222336, 4389483, 3293637, -15551743 + -31361739'i32, -11346780, -15007447, -5856218, -22453340, -12152771, 1222336, + 4389483, 3293637, -15551743, ], yminusx: [ - -16684801'i32, -14444245, 11038544, 11054958, -13801175, - -3338533, -24319580, 7733547, 12796905, -6335822 + -16684801'i32, -14444245, 11038544, 11054958, -13801175, -3338533, -24319580, + 7733547, 12796905, -6335822, ], xy2d: [ - -8759414'i32, -10817836, -25418864, 10783769, -30615557, - -9746811, -28253339, 3647836, 3222231, -11160462 - ] + -8759414'i32, -10817836, -25418864, 10783769, -30615557, -9746811, -28253339, + 3647836, 3222231, -11160462, + ], ), GePrecomp( yplusx: [ - 18606113'i32, 1693100, -25448386, -15170272, 4112353, - 10045021, 23603893, -2048234, -7550776, 2484985 + 18606113'i32, 1693100, -25448386, -15170272, 4112353, 10045021, 23603893, + -2048234, -7550776, 2484985, ], yminusx: [ - 9255317'i32, -3131197, -12156162, -1004256, 13098013, - -9214866, 16377220, -2102812, -19802075, -3034702 + 9255317'i32, -3131197, -12156162, -1004256, 13098013, -9214866, 16377220, + -2102812, -19802075, -3034702, ], xy2d: [ - -22729289'i32, 7496160, -5742199, 11329249, 19991973, - -3347502, -31718148, 9936966, -30097688, -10618797 - ] + -22729289'i32, 7496160, -5742199, 11329249, 19991973, -3347502, -31718148, + 9936966, -30097688, -10618797, + ], ), GePrecomp( yplusx: [ - 21878590'i32, -5001297, 4338336, 13643897, -3036865, - 13160960, 19708896, 5415497, -7360503, -4109293 + 21878590'i32, -5001297, 4338336, 13643897, -3036865, 13160960, 19708896, + 5415497, -7360503, -4109293, ], yminusx: [ - 27736861'i32, 10103576, 12500508, 8502413, -3413016, - -9633558, 10436918, -1550276, -23659143, -8132100 + 27736861'i32, 10103576, 12500508, 8502413, -3413016, -9633558, 10436918, + -1550276, -23659143, -8132100, ], xy2d: [ - 19492550'i32, -12104365, -29681976, -852630, -3208171, - 12403437, 30066266, 8367329, 13243957, 8709688 - ] - ) + 19492550'i32, -12104365, -29681976, -852630, -3208171, 12403437, 30066266, + 8367329, 13243957, 8709688, + ], + ), ], [ GePrecomp( yplusx: [ - 12015105'i32, 2801261, 28198131, 10151021, 24818120, - -4743133, -11194191, -5645734, 5150968, 7274186 + 12015105'i32, 2801261, 28198131, 10151021, 24818120, -4743133, -11194191, + -5645734, 5150968, 7274186, ], yminusx: [ - 2831366'i32, -12492146, 1478975, 6122054, 23825128, - -12733586, 31097299, 6083058, 31021603, -9793610 + 2831366'i32, -12492146, 1478975, 6122054, 23825128, -12733586, 31097299, + 6083058, 31021603, -9793610, ], xy2d: [ - -2529932'i32, -2229646, 445613, 10720828, -13849527, - -11505937, -23507731, 16354465, 15067285, -14147707 - ] + -2529932'i32, -2229646, 445613, 10720828, -13849527, -11505937, -23507731, + 16354465, 15067285, -14147707, + ], ), GePrecomp( yplusx: [ - 7840942'i32, 14037873, -33364863, 15934016, -728213, - -3642706, 21403988, 1057586, -19379462, -12403220 + 7840942'i32, 14037873, -33364863, 15934016, -728213, -3642706, 21403988, + 1057586, -19379462, -12403220, ], yminusx: [ - 915865'i32, -16469274, 15608285, -8789130, -24357026, - 6060030, -17371319, 8410997, -7220461, 16527025 + 915865'i32, -16469274, 15608285, -8789130, -24357026, 6060030, -17371319, + 8410997, -7220461, 16527025, ], xy2d: [ - 32922597'i32, -556987, 20336074, -16184568, 10903705, - -5384487, 16957574, 52992, 23834301, 6588044 - ] + 32922597'i32, -556987, 20336074, -16184568, 10903705, -5384487, 16957574, + 52992, 23834301, 6588044, + ], ), GePrecomp( yplusx: [ - 32752030'i32, 11232950, 3381995, -8714866, 22652988, - -10744103, 17159699, 16689107, -20314580, -1305992 + 32752030'i32, 11232950, 3381995, -8714866, 22652988, -10744103, 17159699, + 16689107, -20314580, -1305992, ], yminusx: [ - -4689649'i32, 9166776, -25710296, -10847306, 11576752, - 12733943, 7924251, -2752281, 1976123, -7249027 + -4689649'i32, 9166776, -25710296, -10847306, 11576752, 12733943, 7924251, + -2752281, 1976123, -7249027, ], xy2d: [ - 21251222'i32, 16309901, -2983015, -6783122, 30810597, - 12967303, 156041, -3371252, 12331345, -8237197 - ] + 21251222'i32, 16309901, -2983015, -6783122, 30810597, 12967303, 156041, + -3371252, 12331345, -8237197, + ], ), GePrecomp( yplusx: [ - 8651614'i32, -4477032, -16085636, -4996994, 13002507, - 2950805, 29054427, -5106970, 10008136, -4667901 + 8651614'i32, -4477032, -16085636, -4996994, 13002507, 2950805, 29054427, + -5106970, 10008136, -4667901, ], yminusx: [ - 31486080'i32, 15114593, -14261250, 12951354, 14369431, - -7387845, 16347321, -13662089, 8684155, -10532952 + 31486080'i32, 15114593, -14261250, 12951354, 14369431, -7387845, 16347321, + -13662089, 8684155, -10532952, ], xy2d: [ - 19443825'i32, 11385320, 24468943, -9659068, -23919258, - 2187569, -26263207, -6086921, 31316348, 14219878 - ] + 19443825'i32, 11385320, 24468943, -9659068, -23919258, 2187569, -26263207, + -6086921, 31316348, 14219878, + ], ), GePrecomp( yplusx: [ - -28594490'i32, 1193785, 32245219, 11392485, 31092169, - 15722801, 27146014, 6992409, 29126555, 9207390 + -28594490'i32, 1193785, 32245219, 11392485, 31092169, 15722801, 27146014, + 6992409, 29126555, 9207390, ], yminusx: [ - 32382935'i32, 1110093, 18477781, 11028262, -27411763, - -7548111, -4980517, 10843782, -7957600, -14435730 + 32382935'i32, 1110093, 18477781, 11028262, -27411763, -7548111, -4980517, + 10843782, -7957600, -14435730, ], xy2d: [ - 2814918'i32, 7836403, 27519878, -7868156, -20894015, - -11553689, -21494559, 8550130, 28346258, 1994730 - ] + 2814918'i32, 7836403, 27519878, -7868156, -20894015, -11553689, -21494559, + 8550130, 28346258, 1994730, + ], ), GePrecomp( yplusx: [ - -19578299'i32, 8085545, -14000519, -3948622, 2785838, - -16231307, -19516951, 7174894, 22628102, 8115180 + -19578299'i32, 8085545, -14000519, -3948622, 2785838, -16231307, -19516951, + 7174894, 22628102, 8115180, ], yminusx: [ - -30405132'i32, 955511, -11133838, -15078069, -32447087, - -13278079, -25651578, 3317160, -9943017, 930272 + -30405132'i32, 955511, -11133838, -15078069, -32447087, -13278079, -25651578, + 3317160, -9943017, 930272, ], xy2d: [ - -15303681'i32, -6833769, 28856490, 1357446, 23421993, - 1057177, 24091212, -1388970, -22765376, -10650715 - ] + -15303681'i32, -6833769, 28856490, 1357446, 23421993, 1057177, 24091212, + -1388970, -22765376, -10650715, + ], ), GePrecomp( yplusx: [ - -22751231'i32, -5303997, -12907607, -12768866, -15811511, - -7797053, -14839018, -16554220, -1867018, 8398970 + -22751231'i32, -5303997, -12907607, -12768866, -15811511, -7797053, -14839018, + -16554220, -1867018, 8398970, ], yminusx: [ - -31969310'i32, 2106403, -4736360, 1362501, 12813763, - 16200670, 22981545, -6291273, 18009408, -15772772 + -31969310'i32, 2106403, -4736360, 1362501, 12813763, 16200670, 22981545, + -6291273, 18009408, -15772772, ], xy2d: [ - -17220923'i32, -9545221, -27784654, 14166835, 29815394, - 7444469, 29551787, -3727419, 19288549, 1325865 - ] + -17220923'i32, -9545221, -27784654, 14166835, 29815394, 7444469, 29551787, + -3727419, 19288549, 1325865, + ], ), GePrecomp( yplusx: [ - 15100157'i32, -15835752, -23923978, -1005098, -26450192, - 15509408, 12376730, -3479146, 33166107, -8042750 + 15100157'i32, -15835752, -23923978, -1005098, -26450192, 15509408, 12376730, + -3479146, 33166107, -8042750, ], yminusx: [ - 20909231'i32, 13023121, -9209752, 16251778, -5778415, - -8094914, 12412151, 10018715, 2213263, -13878373 + 20909231'i32, 13023121, -9209752, 16251778, -5778415, -8094914, 12412151, + 10018715, 2213263, -13878373, ], xy2d: [ - 32529814'i32, -11074689, 30361439, -16689753, -9135940, - 1513226, 22922121, 6382134, -5766928, 8371348 - ] - ) + 32529814'i32, -11074689, 30361439, -16689753, -9135940, 1513226, 22922121, + 6382134, -5766928, 8371348, + ], + ), ], [ GePrecomp( yplusx: [ - 9923462'i32, 11271500, 12616794, 3544722, -29998368, - -1721626, 12891687, -8193132, -26442943, 10486144 + 9923462'i32, 11271500, 12616794, 3544722, -29998368, -1721626, 12891687, + -8193132, -26442943, 10486144, ], yminusx: [ - -22597207'i32, -7012665, 8587003, -8257861, 4084309, - -12970062, 361726, 2610596, -23921530, -11455195 + -22597207'i32, -7012665, 8587003, -8257861, 4084309, -12970062, 361726, + 2610596, -23921530, -11455195, ], xy2d: [ - 5408411'i32, -1136691, -4969122, 10561668, 24145918, - 14240566, 31319731, -4235541, 19985175, -3436086 - ] + 5408411'i32, -1136691, -4969122, 10561668, 24145918, 14240566, 31319731, + -4235541, 19985175, -3436086, + ], ), GePrecomp( yplusx: [ - -13994457'i32, 16616821, 14549246, 3341099, 32155958, - 13648976, -17577068, 8849297, 65030, 8370684 + -13994457'i32, 16616821, 14549246, 3341099, 32155958, 13648976, -17577068, + 8849297, 65030, 8370684, ], yminusx: [ - -8320926'i32, -12049626, 31204563, 5839400, -20627288, - -1057277, -19442942, 6922164, 12743482, -9800518 + -8320926'i32, -12049626, 31204563, 5839400, -20627288, -1057277, -19442942, + 6922164, 12743482, -9800518, ], xy2d: [ - -2361371'i32, 12678785, 28815050, 4759974, -23893047, - 4884717, 23783145, 11038569, 18800704, 255233 - ] + -2361371'i32, 12678785, 28815050, 4759974, -23893047, 4884717, 23783145, + 11038569, 18800704, 255233, + ], ), GePrecomp( yplusx: [ - -5269658'i32, -1773886, 13957886, 7990715, 23132995, - 728773, 13393847, 9066957, 19258688, -14753793 + -5269658'i32, -1773886, 13957886, 7990715, 23132995, 728773, 13393847, + 9066957, 19258688, -14753793, ], yminusx: [ - -2936654'i32, -10827535, -10432089, 14516793, -3640786, - 4372541, -31934921, 2209390, -1524053, 2055794 + -2936654'i32, -10827535, -10432089, 14516793, -3640786, 4372541, -31934921, + 2209390, -1524053, 2055794, ], xy2d: [ - 580882'i32, 16705327, 5468415, -2683018, -30926419, - -14696000, -7203346, -8994389, -30021019, 7394435 - ] + 580882'i32, 16705327, 5468415, -2683018, -30926419, -14696000, -7203346, + -8994389, -30021019, 7394435, + ], ), GePrecomp( yplusx: [ - 23838809'i32, 1822728, -15738443, 15242727, 8318092, - -3733104, -21672180, -3492205, -4821741, 14799921 + 23838809'i32, 1822728, -15738443, 15242727, 8318092, -3733104, -21672180, + -3492205, -4821741, 14799921, ], yminusx: [ - 13345610'i32, 9759151, 3371034, -16137791, 16353039, - 8577942, 31129804, 13496856, -9056018, 7402518 + 13345610'i32, 9759151, 3371034, -16137791, 16353039, 8577942, 31129804, + 13496856, -9056018, 7402518, ], xy2d: [ - 2286874'i32, -4435931, -20042458, -2008336, -13696227, - 5038122, 11006906, -15760352, 8205061, 1607563 - ] + 2286874'i32, -4435931, -20042458, -2008336, -13696227, 5038122, 11006906, + -15760352, 8205061, 1607563, + ], ), GePrecomp( yplusx: [ - 14414086'i32, -8002132, 3331830, -3208217, 22249151, - -5594188, 18364661, -2906958, 30019587, -9029278 + 14414086'i32, -8002132, 3331830, -3208217, 22249151, -5594188, 18364661, + -2906958, 30019587, -9029278, ], yminusx: [ - -27688051'i32, 1585953, -10775053, 931069, -29120221, - -11002319, -14410829, 12029093, 9944378, 8024 + -27688051'i32, 1585953, -10775053, 931069, -29120221, -11002319, -14410829, + 12029093, 9944378, 8024, ], xy2d: [ - 4368715'i32, -3709630, 29874200, -15022983, -20230386, - -11410704, -16114594, -999085, -8142388, 5640030 - ] + 4368715'i32, -3709630, 29874200, -15022983, -20230386, -11410704, -16114594, + -999085, -8142388, 5640030, + ], ), GePrecomp( yplusx: [ - 10299610'i32, 13746483, 11661824, 16234854, 7630238, - 5998374, 9809887, -16694564, 15219798, -14327783 + 10299610'i32, 13746483, 11661824, 16234854, 7630238, 5998374, 9809887, + -16694564, 15219798, -14327783, ], yminusx: [ - 27425505'i32, -5719081, 3055006, 10660664, 23458024, - 595578, -15398605, -1173195, -18342183, 9742717 + 27425505'i32, -5719081, 3055006, 10660664, 23458024, 595578, -15398605, + -1173195, -18342183, 9742717, ], xy2d: [ - 6744077'i32, 2427284, 26042789, 2720740, -847906, - 1118974, 32324614, 7406442, 12420155, 1994844 - ] + 6744077'i32, 2427284, 26042789, 2720740, -847906, 1118974, 32324614, 7406442, + 12420155, 1994844, + ], ), GePrecomp( yplusx: [ - 14012521'i32, -5024720, -18384453, -9578469, -26485342, - -3936439, -13033478, -10909803, 24319929, -6446333 + 14012521'i32, -5024720, -18384453, -9578469, -26485342, -3936439, -13033478, + -10909803, 24319929, -6446333, ], yminusx: [ - 16412690'i32, -4507367, 10772641, 15929391, -17068788, - -4658621, 10555945, -10484049, -30102368, -4739048 + 16412690'i32, -4507367, 10772641, 15929391, -17068788, -4658621, 10555945, + -10484049, -30102368, -4739048, ], xy2d: [ - 22397382'i32, -7767684, -9293161, -12792868, 17166287, - -9755136, -27333065, 6199366, 21880021, -12250760 - ] + 22397382'i32, -7767684, -9293161, -12792868, 17166287, -9755136, -27333065, + 6199366, 21880021, -12250760, + ], ), GePrecomp( yplusx: [ - -4283307'i32, 5368523, -31117018, 8163389, -30323063, - 3209128, 16557151, 8890729, 8840445, 4957760 + -4283307'i32, 5368523, -31117018, 8163389, -30323063, 3209128, 16557151, + 8890729, 8840445, 4957760, ], yminusx: [ - -15447727'i32, 709327, -6919446, -10870178, -29777922, - 6522332, -21720181, 12130072, -14796503, 5005757 + -15447727'i32, 709327, -6919446, -10870178, -29777922, 6522332, -21720181, + 12130072, -14796503, 5005757, ], xy2d: [ - -2114751'i32, -14308128, 23019042, 15765735, -25269683, - 6002752, 10183197, -13239326, -16395286, -2176112 - ] - ) + -2114751'i32, -14308128, 23019042, 15765735, -25269683, 6002752, 10183197, + -13239326, -16395286, -2176112, + ], + ), ], [ GePrecomp( yplusx: [ - -19025756'i32, 1632005, 13466291, -7995100, -23640451, - 16573537, -32013908, -3057104, 22208662, 2000468 + -19025756'i32, 1632005, 13466291, -7995100, -23640451, 16573537, -32013908, + -3057104, 22208662, 2000468, ], yminusx: [ - 3065073'i32, -1412761, -25598674, -361432, -17683065, - -5703415, -8164212, 11248527, -3691214, -7414184 + 3065073'i32, -1412761, -25598674, -361432, -17683065, -5703415, -8164212, + 11248527, -3691214, -7414184, ], xy2d: [ - 10379208'i32, -6045554, 8877319, 1473647, -29291284, - -12507580, 16690915, 2553332, -3132688, 16400289 - ] + 10379208'i32, -6045554, 8877319, 1473647, -29291284, -12507580, 16690915, + 2553332, -3132688, 16400289, + ], ), GePrecomp( yplusx: [ - 15716668'i32, 1254266, -18472690, 7446274, -8448918, - 6344164, -22097271, -7285580, 26894937, 9132066 + 15716668'i32, 1254266, -18472690, 7446274, -8448918, 6344164, -22097271, + -7285580, 26894937, 9132066, ], yminusx: [ - 24158887'i32, 12938817, 11085297, -8177598, -28063478, - -4457083, -30576463, 64452, -6817084, -2692882 + 24158887'i32, 12938817, 11085297, -8177598, -28063478, -4457083, -30576463, + 64452, -6817084, -2692882, ], xy2d: [ - 13488534'i32, 7794716, 22236231, 5989356, 25426474, - -12578208, 2350710, -3418511, -4688006, 2364226 - ] + 13488534'i32, 7794716, 22236231, 5989356, 25426474, -12578208, 2350710, + -3418511, -4688006, 2364226, + ], ), GePrecomp( yplusx: [ - 16335052'i32, 9132434, 25640582, 6678888, 1725628, - 8517937, -11807024, -11697457, 15445875, -7798101 + 16335052'i32, 9132434, 25640582, 6678888, 1725628, 8517937, -11807024, + -11697457, 15445875, -7798101, ], yminusx: [ - 29004207'i32, -7867081, 28661402, -640412, -12794003, - -7943086, 31863255, -4135540, -278050, -15759279 + 29004207'i32, -7867081, 28661402, -640412, -12794003, -7943086, 31863255, + -4135540, -278050, -15759279, ], xy2d: [ - -6122061'i32, -14866665, -28614905, 14569919, -10857999, - -3591829, 10343412, -6976290, -29828287, -10815811 - ] + -6122061'i32, -14866665, -28614905, 14569919, -10857999, -3591829, 10343412, + -6976290, -29828287, -10815811, + ], ), GePrecomp( yplusx: [ - 27081650'i32, 3463984, 14099042, -4517604, 1616303, - -6205604, 29542636, 15372179, 17293797, 960709 + 27081650'i32, 3463984, 14099042, -4517604, 1616303, -6205604, 29542636, + 15372179, 17293797, 960709, ], yminusx: [ - 20263915'i32, 11434237, -5765435, 11236810, 13505955, - -10857102, -16111345, 6493122, -19384511, 7639714 + 20263915'i32, 11434237, -5765435, 11236810, 13505955, -10857102, -16111345, + 6493122, -19384511, 7639714, ], xy2d: [ - -2830798'i32, -14839232, 25403038, -8215196, -8317012, - -16173699, 18006287, -16043750, 29994677, -15808121 - ] + -2830798'i32, -14839232, 25403038, -8215196, -8317012, -16173699, 18006287, + -16043750, 29994677, -15808121, + ], ), GePrecomp( yplusx: [ - 9769828'i32, 5202651, -24157398, -13631392, -28051003, - -11561624, -24613141, -13860782, -31184575, 709464 + 9769828'i32, 5202651, -24157398, -13631392, -28051003, -11561624, -24613141, + -13860782, -31184575, 709464, ], yminusx: [ - 12286395'i32, 13076066, -21775189, -1176622, -25003198, - 4057652, -32018128, -8890874, 16102007, 13205847 + 12286395'i32, 13076066, -21775189, -1176622, -25003198, 4057652, -32018128, + -8890874, 16102007, 13205847, ], xy2d: [ - 13733362'i32, 5599946, 10557076, 3195751, -5557991, - 8536970, -25540170, 8525972, 10151379, 10394400 - ] + 13733362'i32, 5599946, 10557076, 3195751, -5557991, 8536970, -25540170, + 8525972, 10151379, 10394400, + ], ), GePrecomp( yplusx: [ - 4024660'i32, -16137551, 22436262, 12276534, -9099015, - -2686099, 19698229, 11743039, -33302334, 8934414 + 4024660'i32, -16137551, 22436262, 12276534, -9099015, -2686099, 19698229, + 11743039, -33302334, 8934414, ], yminusx: [ - -15879800'i32, -4525240, -8580747, -2934061, 14634845, - -698278, -9449077, 3137094, -11536886, 11721158 + -15879800'i32, -4525240, -8580747, -2934061, 14634845, -698278, -9449077, + 3137094, -11536886, 11721158, ], xy2d: [ - 17555939'i32, -5013938, 8268606, 2331751, -22738815, - 9761013, 9319229, 8835153, -9205489, -1280045 - ] + 17555939'i32, -5013938, 8268606, 2331751, -22738815, 9761013, 9319229, + 8835153, -9205489, -1280045, + ], ), GePrecomp( yplusx: [ - -461409'i32, -7830014, 20614118, 16688288, -7514766, - -4807119, 22300304, 505429, 6108462, -6183415 + -461409'i32, -7830014, 20614118, 16688288, -7514766, -4807119, 22300304, + 505429, 6108462, -6183415, ], yminusx: [ - -5070281'i32, 12367917, -30663534, 3234473, 32617080, - -8422642, 29880583, -13483331, -26898490, -7867459 + -5070281'i32, 12367917, -30663534, 3234473, 32617080, -8422642, 29880583, + -13483331, -26898490, -7867459, ], xy2d: [ - -31975283'i32, 5726539, 26934134, 10237677, -3173717, - -605053, 24199304, 3795095, 7592688, -14992079 - ] + -31975283'i32, 5726539, 26934134, 10237677, -3173717, -605053, 24199304, + 3795095, 7592688, -14992079, + ], ), GePrecomp( yplusx: [ - 21594432'i32, -14964228, 17466408, -4077222, 32537084, - 2739898, 6407723, 12018833, -28256052, 4298412 + 21594432'i32, -14964228, 17466408, -4077222, 32537084, 2739898, 6407723, + 12018833, -28256052, 4298412, ], yminusx: [ - -20650503'i32, -11961496, -27236275, 570498, 3767144, - -1717540, 13891942, -1569194, 13717174, 10805743 + -20650503'i32, -11961496, -27236275, 570498, 3767144, -1717540, 13891942, + -1569194, 13717174, 10805743, ], xy2d: [ - -14676630'i32, -15644296, 15287174, 11927123, 24177847, - -8175568, -796431, 14860609, -26938930, -5863836 - ] - ) + -14676630'i32, -15644296, 15287174, 11927123, 24177847, -8175568, -796431, + 14860609, -26938930, -5863836, + ], + ), ], [ GePrecomp( yplusx: [ - 12962541'i32, 5311799, -10060768, 11658280, 18855286, - -7954201, 13286263, -12808704, -4381056, 9882022 + 12962541'i32, 5311799, -10060768, 11658280, 18855286, -7954201, 13286263, + -12808704, -4381056, 9882022, ], yminusx: [ - 18512079'i32, 11319350, -20123124, 15090309, 18818594, - 5271736, -22727904, 3666879, -23967430, -3299429 + 18512079'i32, 11319350, -20123124, 15090309, 18818594, 5271736, -22727904, + 3666879, -23967430, -3299429, ], xy2d: [ - -6789020'i32, -3146043, 16192429, 13241070, 15898607, - -14206114, -10084880, -6661110, -2403099, 5276065 - ] + -6789020'i32, -3146043, 16192429, 13241070, 15898607, -14206114, -10084880, + -6661110, -2403099, 5276065, + ], ), GePrecomp( yplusx: [ - 30169808'i32, -5317648, 26306206, -11750859, 27814964, - 7069267, 7152851, 3684982, 1449224, 13082861 + 30169808'i32, -5317648, 26306206, -11750859, 27814964, 7069267, 7152851, + 3684982, 1449224, 13082861, ], yminusx: [ - 10342826'i32, 3098505, 2119311, 193222, 25702612, - 12233820, 23697382, 15056736, -21016438, -8202000 + 10342826'i32, 3098505, 2119311, 193222, 25702612, 12233820, 23697382, + 15056736, -21016438, -8202000, ], xy2d: [ - -33150110'i32, 3261608, 22745853, 7948688, 19370557, - -15177665, -26171976, 6482814, -10300080, -11060101 - ] + -33150110'i32, 3261608, 22745853, 7948688, 19370557, -15177665, -26171976, + 6482814, -10300080, -11060101, + ], ), GePrecomp( yplusx: [ - 32869458'i32, -5408545, 25609743, 15678670, -10687769, - -15471071, 26112421, 2521008, -22664288, 6904815 + 32869458'i32, -5408545, 25609743, 15678670, -10687769, -15471071, 26112421, + 2521008, -22664288, 6904815, ], yminusx: [ - 29506923'i32, 4457497, 3377935, -9796444, -30510046, - 12935080, 1561737, 3841096, -29003639, -6657642 + 29506923'i32, 4457497, 3377935, -9796444, -30510046, 12935080, 1561737, + 3841096, -29003639, -6657642, ], xy2d: [ - 10340844'i32, -6630377, -18656632, -2278430, 12621151, - -13339055, 30878497, -11824370, -25584551, 5181966 - ] + 10340844'i32, -6630377, -18656632, -2278430, 12621151, -13339055, 30878497, + -11824370, -25584551, 5181966, + ], ), GePrecomp( yplusx: [ - 25940115'i32, -12658025, 17324188, -10307374, -8671468, - 15029094, 24396252, -16450922, -2322852, -12388574 + 25940115'i32, -12658025, 17324188, -10307374, -8671468, 15029094, 24396252, + -16450922, -2322852, -12388574, ], yminusx: [ - -21765684'i32, 9916823, -1300409, 4079498, -1028346, - 11909559, 1782390, 12641087, 20603771, -6561742 + -21765684'i32, 9916823, -1300409, 4079498, -1028346, 11909559, 1782390, + 12641087, 20603771, -6561742, ], xy2d: [ - -18882287'i32, -11673380, 24849422, 11501709, 13161720, - -4768874, 1925523, 11914390, 4662781, 7820689 - ] + -18882287'i32, -11673380, 24849422, 11501709, 13161720, -4768874, 1925523, + 11914390, 4662781, 7820689, + ], ), GePrecomp( yplusx: [ - 12241050'i32, -425982, 8132691, 9393934, 32846760, - -1599620, 29749456, 12172924, 16136752, 15264020 + 12241050'i32, -425982, 8132691, 9393934, 32846760, -1599620, 29749456, + 12172924, 16136752, 15264020, ], yminusx: [ - -10349955'i32, -14680563, -8211979, 2330220, -17662549, - -14545780, 10658213, 6671822, 19012087, 3772772 + -10349955'i32, -14680563, -8211979, 2330220, -17662549, -14545780, 10658213, + 6671822, 19012087, 3772772, ], xy2d: [ - 3753511'i32, -3421066, 10617074, 2028709, 14841030, - -6721664, 28718732, -15762884, 20527771, 12988982 - ] + 3753511'i32, -3421066, 10617074, 2028709, 14841030, -6721664, 28718732, + -15762884, 20527771, 12988982, + ], ), GePrecomp( yplusx: [ - -14822485'i32, -5797269, -3707987, 12689773, -898983, - -10914866, -24183046, -10564943, 3299665, -12424953 + -14822485'i32, -5797269, -3707987, 12689773, -898983, -10914866, -24183046, + -10564943, 3299665, -12424953, ], yminusx: [ - -16777703'i32, -15253301, -9642417, 4978983, 3308785, - 8755439, 6943197, 6461331, -25583147, 8991218 + -16777703'i32, -15253301, -9642417, 4978983, 3308785, 8755439, 6943197, + 6461331, -25583147, 8991218, ], xy2d: [ - -17226263'i32, 1816362, -1673288, -6086439, 31783888, - -8175991, -32948145, 7417950, -30242287, 1507265 - ] + -17226263'i32, 1816362, -1673288, -6086439, 31783888, -8175991, -32948145, + 7417950, -30242287, 1507265, + ], ), GePrecomp( yplusx: [ - 29692663'i32, 6829891, -10498800, 4334896, 20945975, - -11906496, -28887608, 8209391, 14606362, -10647073 + 29692663'i32, 6829891, -10498800, 4334896, 20945975, -11906496, -28887608, + 8209391, 14606362, -10647073, ], yminusx: [ - -3481570'i32, 8707081, 32188102, 5672294, 22096700, - 1711240, -33020695, 9761487, 4170404, -2085325 + -3481570'i32, 8707081, 32188102, 5672294, 22096700, 1711240, -33020695, + 9761487, 4170404, -2085325, ], xy2d: [ - -11587470'i32, 14855945, -4127778, -1531857, -26649089, - 15084046, 22186522, 16002000, -14276837, -8400798 - ] + -11587470'i32, 14855945, -4127778, -1531857, -26649089, 15084046, 22186522, + 16002000, -14276837, -8400798, + ], ), GePrecomp( yplusx: [ - -4811456'i32, 13761029, -31703877, -2483919, -3312471, - 7869047, -7113572, -9620092, 13240845, 10965870 + -4811456'i32, 13761029, -31703877, -2483919, -3312471, 7869047, -7113572, + -9620092, 13240845, 10965870, ], yminusx: [ - -7742563'i32, -8256762, -14768334, -13656260, -23232383, - 12387166, 4498947, 14147411, 29514390, 4302863 + -7742563'i32, -8256762, -14768334, -13656260, -23232383, 12387166, 4498947, + 14147411, 29514390, 4302863, ], xy2d: [ - -13413405'i32, -12407859, 20757302, -13801832, 14785143, - 8976368, -5061276, -2144373, 17846988, -13971927 - ] - ) + -13413405'i32, -12407859, 20757302, -13801832, 14785143, 8976368, -5061276, + -2144373, 17846988, -13971927, + ], + ), ], [ GePrecomp( yplusx: [ - -2244452'i32, -754728, -4597030, -1066309, -6247172, - 1455299, -21647728, -9214789, -5222701, 12650267 + -2244452'i32, -754728, -4597030, -1066309, -6247172, 1455299, -21647728, + -9214789, -5222701, 12650267, ], yminusx: [ - -9906797'i32, -16070310, 21134160, 12198166, -27064575, - 708126, 387813, 13770293, -19134326, 10958663 + -9906797'i32, -16070310, 21134160, 12198166, -27064575, 708126, 387813, + 13770293, -19134326, 10958663, ], xy2d: [ - 22470984'i32, 12369526, 23446014, -5441109, -21520802, - -9698723, -11772496, -11574455, -25083830, 4271862 - ] + 22470984'i32, 12369526, 23446014, -5441109, -21520802, -9698723, -11772496, + -11574455, -25083830, 4271862, + ], ), GePrecomp( yplusx: [ - -25169565'i32, -10053642, -19909332, 15361595, -5984358, - 2159192, 75375, -4278529, -32526221, 8469673 + -25169565'i32, -10053642, -19909332, 15361595, -5984358, 2159192, 75375, + -4278529, -32526221, 8469673, ], yminusx: [ - 15854970'i32, 4148314, -8893890, 7259002, 11666551, - 13824734, -30531198, 2697372, 24154791, -9460943 + 15854970'i32, 4148314, -8893890, 7259002, 11666551, 13824734, -30531198, + 2697372, 24154791, -9460943, ], xy2d: [ - 15446137'i32, -15806644, 29759747, 14019369, 30811221, - -9610191, -31582008, 12840104, 24913809, 9815020 - ] + 15446137'i32, -15806644, 29759747, 14019369, 30811221, -9610191, -31582008, + 12840104, 24913809, 9815020, + ], ), GePrecomp( yplusx: [ - -4709286'i32, -5614269, -31841498, -12288893, -14443537, - 10799414, -9103676, 13438769, 18735128, 9466238 + -4709286'i32, -5614269, -31841498, -12288893, -14443537, 10799414, -9103676, + 13438769, 18735128, 9466238, ], yminusx: [ - 11933045'i32, 9281483, 5081055, -5183824, -2628162, - -4905629, -7727821, -10896103, -22728655, 16199064 + 11933045'i32, 9281483, 5081055, -5183824, -2628162, -4905629, -7727821, + -10896103, -22728655, 16199064, ], xy2d: [ - 14576810'i32, 379472, -26786533, -8317236, -29426508, - -10812974, -102766, 1876699, 30801119, 2164795 - ] + 14576810'i32, 379472, -26786533, -8317236, -29426508, -10812974, -102766, + 1876699, 30801119, 2164795, + ], ), GePrecomp( yplusx: [ - 15995086'i32, 3199873, 13672555, 13712240, -19378835, - -4647646, -13081610, -15496269, -13492807, 1268052 + 15995086'i32, 3199873, 13672555, 13712240, -19378835, -4647646, -13081610, + -15496269, -13492807, 1268052, ], yminusx: [ - -10290614'i32, -3659039, -3286592, 10948818, 23037027, - 3794475, -3470338, -12600221, -17055369, 3565904 + -10290614'i32, -3659039, -3286592, 10948818, 23037027, 3794475, -3470338, + -12600221, -17055369, 3565904, ], xy2d: [ - 29210088'i32, -9419337, -5919792, -4952785, 10834811, - -13327726, -16512102, -10820713, -27162222, -14030531 - ] + 29210088'i32, -9419337, -5919792, -4952785, 10834811, -13327726, -16512102, + -10820713, -27162222, -14030531, + ], ), GePrecomp( yplusx: [ - -13161890'i32, 15508588, 16663704, -8156150, -28349942, - 9019123, -29183421, -3769423, 2244111, -14001979 + -13161890'i32, 15508588, 16663704, -8156150, -28349942, 9019123, -29183421, + -3769423, 2244111, -14001979, ], yminusx: [ - -5152875'i32, -3800936, -9306475, -6071583, 16243069, - 14684434, -25673088, -16180800, 13491506, 4641841 + -5152875'i32, -3800936, -9306475, -6071583, 16243069, 14684434, -25673088, + -16180800, 13491506, 4641841, ], xy2d: [ - 10813417'i32, 643330, -19188515, -728916, 30292062, - -16600078, 27548447, -7721242, 14476989, -12767431 - ] + 10813417'i32, 643330, -19188515, -728916, 30292062, -16600078, 27548447, + -7721242, 14476989, -12767431, + ], ), GePrecomp( yplusx: [ - 10292079'i32, 9984945, 6481436, 8279905, -7251514, - 7032743, 27282937, -1644259, -27912810, 12651324 + 10292079'i32, 9984945, 6481436, 8279905, -7251514, 7032743, 27282937, + -1644259, -27912810, 12651324, ], yminusx: [ - -31185513'i32, -813383, 22271204, 11835308, 10201545, - 15351028, 17099662, 3988035, 21721536, -3148940 + -31185513'i32, -813383, 22271204, 11835308, 10201545, 15351028, 17099662, + 3988035, 21721536, -3148940, ], xy2d: [ - 10202177'i32, -6545839, -31373232, -9574638, -32150642, - -8119683, -12906320, 3852694, 13216206, 14842320 - ] + 10202177'i32, -6545839, -31373232, -9574638, -32150642, -8119683, -12906320, + 3852694, 13216206, 14842320, + ], ), GePrecomp( yplusx: [ - -15815640'i32, -10601066, -6538952, -7258995, -6984659, - -6581778, -31500847, 13765824, -27434397, 9900184 + -15815640'i32, -10601066, -6538952, -7258995, -6984659, -6581778, -31500847, + 13765824, -27434397, 9900184, ], yminusx: [ - 14465505'i32, -13833331, -32133984, -14738873, -27443187, - 12990492, 33046193, 15796406, -7051866, -8040114 + 14465505'i32, -13833331, -32133984, -14738873, -27443187, 12990492, 33046193, + 15796406, -7051866, -8040114, ], xy2d: [ - 30924417'i32, -8279620, 6359016, -12816335, 16508377, - 9071735, -25488601, 15413635, 9524356, -7018878 - ] + 30924417'i32, -8279620, 6359016, -12816335, 16508377, 9071735, -25488601, + 15413635, 9524356, -7018878, + ], ), GePrecomp( yplusx: [ - 12274201'i32, -13175547, 32627641, -1785326, 6736625, - 13267305, 5237659, -5109483, 15663516, 4035784 + 12274201'i32, -13175547, 32627641, -1785326, 6736625, 13267305, 5237659, + -5109483, 15663516, 4035784, ], yminusx: [ - -2951309'i32, 8903985, 17349946, 601635, -16432815, - -4612556, -13732739, -15889334, -22258478, 4659091 + -2951309'i32, 8903985, 17349946, 601635, -16432815, -4612556, -13732739, + -15889334, -22258478, 4659091, ], xy2d: [ - -16916263'i32, -4952973, -30393711, -15158821, 20774812, - 15897498, 5736189, 15026997, -2178256, -13455585 - ] - ) + -16916263'i32, -4952973, -30393711, -15158821, 20774812, 15897498, 5736189, + 15026997, -2178256, -13455585, + ], + ), ], [ GePrecomp( yplusx: [ - -8858980'i32, -2219056, 28571666, -10155518, -474467, - -10105698, -3801496, 278095, 23440562, -290208 + -8858980'i32, -2219056, 28571666, -10155518, -474467, -10105698, -3801496, + 278095, 23440562, -290208, ], yminusx: [ - 10226241'i32, -5928702, 15139956, 120818, -14867693, - 5218603, 32937275, 11551483, -16571960, -7442864 + 10226241'i32, -5928702, 15139956, 120818, -14867693, 5218603, 32937275, + 11551483, -16571960, -7442864, ], xy2d: [ - 17932739'i32, -12437276, -24039557, 10749060, 11316803, - 7535897, 22503767, 5561594, -3646624, 3898661 - ] + 17932739'i32, -12437276, -24039557, 10749060, 11316803, 7535897, 22503767, + 5561594, -3646624, 3898661, + ], ), GePrecomp( yplusx: [ - 7749907'i32, -969567, -16339731, -16464, -25018111, - 15122143, -1573531, 7152530, 21831162, 1245233 + 7749907'i32, -969567, -16339731, -16464, -25018111, 15122143, -1573531, + 7152530, 21831162, 1245233, ], yminusx: [ - 26958459'i32, -14658026, 4314586, 8346991, -5677764, - 11960072, -32589295, -620035, -30402091, -16716212 + 26958459'i32, -14658026, 4314586, 8346991, -5677764, 11960072, -32589295, + -620035, -30402091, -16716212, ], xy2d: [ - -12165896'i32, 9166947, 33491384, 13673479, 29787085, - 13096535, 6280834, 14587357, -22338025, 13987525 - ] + -12165896'i32, 9166947, 33491384, 13673479, 29787085, 13096535, 6280834, + 14587357, -22338025, 13987525, + ], ), GePrecomp( yplusx: [ - -24349909'i32, 7778775, 21116000, 15572597, -4833266, - -5357778, -4300898, -5124639, -7469781, -2858068 + -24349909'i32, 7778775, 21116000, 15572597, -4833266, -5357778, -4300898, + -5124639, -7469781, -2858068, ], yminusx: [ - 9681908'i32, -6737123, -31951644, 13591838, -6883821, - 386950, 31622781, 6439245, -14581012, 4091397 + 9681908'i32, -6737123, -31951644, 13591838, -6883821, 386950, 31622781, + 6439245, -14581012, 4091397, ], xy2d: [ - -8426427'i32, 1470727, -28109679, -1596990, 3978627, - -5123623, -19622683, 12092163, 29077877, -14741988 - ] + -8426427'i32, 1470727, -28109679, -1596990, 3978627, -5123623, -19622683, + 12092163, 29077877, -14741988, + ], ), GePrecomp( yplusx: [ - 5269168'i32, -6859726, -13230211, -8020715, 25932563, - 1763552, -5606110, -5505881, -20017847, 2357889 + 5269168'i32, -6859726, -13230211, -8020715, 25932563, 1763552, -5606110, + -5505881, -20017847, 2357889, ], yminusx: [ - 32264008'i32, -15407652, -5387735, -1160093, -2091322, - -3946900, 23104804, -12869908, 5727338, 189038 + 32264008'i32, -15407652, -5387735, -1160093, -2091322, -3946900, 23104804, + -12869908, 5727338, 189038, ], xy2d: [ - 14609123'i32, -8954470, -6000566, -16622781, -14577387, - -7743898, -26745169, 10942115, -25888931, -14884697 - ] + 14609123'i32, -8954470, -6000566, -16622781, -14577387, -7743898, -26745169, + 10942115, -25888931, -14884697, + ], ), GePrecomp( yplusx: [ - 20513500'i32, 5557931, -15604613, 7829531, 26413943, - -2019404, -21378968, 7471781, 13913677, -5137875 + 20513500'i32, 5557931, -15604613, 7829531, 26413943, -2019404, -21378968, + 7471781, 13913677, -5137875, ], yminusx: [ - -25574376'i32, 11967826, 29233242, 12948236, -6754465, - 4713227, -8940970, 14059180, 12878652, 8511905 + -25574376'i32, 11967826, 29233242, 12948236, -6754465, 4713227, -8940970, + 14059180, 12878652, 8511905, ], xy2d: [ - -25656801'i32, 3393631, -2955415, -7075526, -2250709, - 9366908, -30223418, 6812974, 5568676, -3127656 - ] + -25656801'i32, 3393631, -2955415, -7075526, -2250709, 9366908, -30223418, + 6812974, 5568676, -3127656, + ], ), GePrecomp( yplusx: [ - 11630004'i32, 12144454, 2116339, 13606037, 27378885, - 15676917, -17408753, -13504373, -14395196, 8070818 + 11630004'i32, 12144454, 2116339, 13606037, 27378885, 15676917, -17408753, + -13504373, -14395196, 8070818, ], yminusx: [ - 27117696'i32, -10007378, -31282771, -5570088, 1127282, - 12772488, -29845906, 10483306, -11552749, -1028714 + 27117696'i32, -10007378, -31282771, -5570088, 1127282, 12772488, -29845906, + 10483306, -11552749, -1028714, ], xy2d: [ - 10637467'i32, -5688064, 5674781, 1072708, -26343588, - -6982302, -1683975, 9177853, -27493162, 15431203 - ] + 10637467'i32, -5688064, 5674781, 1072708, -26343588, -6982302, -1683975, + 9177853, -27493162, 15431203, + ], ), GePrecomp( yplusx: [ - 20525145'i32, 10892566, -12742472, 12779443, -29493034, - 16150075, -28240519, 14943142, -15056790, -7935931 + 20525145'i32, 10892566, -12742472, 12779443, -29493034, 16150075, -28240519, + 14943142, -15056790, -7935931, ], yminusx: [ - -30024462'i32, 5626926, -551567, -9981087, 753598, - 11981191, 25244767, -3239766, -3356550, 9594024 + -30024462'i32, 5626926, -551567, -9981087, 753598, 11981191, 25244767, + -3239766, -3356550, 9594024, ], xy2d: [ - -23752644'i32, 2636870, -5163910, -10103818, 585134, - 7877383, 11345683, -6492290, 13352335, -10977084 - ] + -23752644'i32, 2636870, -5163910, -10103818, 585134, 7877383, 11345683, + -6492290, 13352335, -10977084, + ], ), GePrecomp( yplusx: [ - -1931799'i32, -5407458, 3304649, -12884869, 17015806, - -4877091, -29783850, -7752482, -13215537, -319204 + -1931799'i32, -5407458, 3304649, -12884869, 17015806, -4877091, -29783850, + -7752482, -13215537, -319204, ], yminusx: [ - 20239939'i32, 6607058, 6203985, 3483793, -18386976, - -779229, -20723742, 15077870, -22750759, 14523817 + 20239939'i32, 6607058, 6203985, 3483793, -18386976, -779229, -20723742, + 15077870, -22750759, 14523817, ], xy2d: [ - 27406042'i32, -6041657, 27423596, -4497394, 4996214, - 10002360, -28842031, -4545494, -30172742, -4805667 - ] - ) + 27406042'i32, -6041657, 27423596, -4497394, 4996214, 10002360, -28842031, + -4545494, -30172742, -4805667, + ], + ), ], [ GePrecomp( yplusx: [ - 11374242'i32, 12660715, 17861383, -12540833, 10935568, - 1099227, -13886076, -9091740, -27727044, 11358504 + 11374242'i32, 12660715, 17861383, -12540833, 10935568, 1099227, -13886076, + -9091740, -27727044, 11358504, ], yminusx: [ - -12730809'i32, 10311867, 1510375, 10778093, -2119455, - -9145702, 32676003, 11149336, -26123651, 4985768 + -12730809'i32, 10311867, 1510375, 10778093, -2119455, -9145702, 32676003, + 11149336, -26123651, 4985768, ], xy2d: [ - -19096303'i32, 341147, -6197485, -239033, 15756973, - -8796662, -983043, 13794114, -19414307, -15621255 - ] + -19096303'i32, 341147, -6197485, -239033, 15756973, -8796662, -983043, + 13794114, -19414307, -15621255, + ], ), GePrecomp( yplusx: [ - 6490081'i32, 11940286, 25495923, -7726360, 8668373, - -8751316, 3367603, 6970005, -1691065, -9004790 + 6490081'i32, 11940286, 25495923, -7726360, 8668373, -8751316, 3367603, + 6970005, -1691065, -9004790, ], yminusx: [ - 1656497'i32, 13457317, 15370807, 6364910, 13605745, - 8362338, -19174622, -5475723, -16796596, -5031438 + 1656497'i32, 13457317, 15370807, 6364910, 13605745, 8362338, -19174622, + -5475723, -16796596, -5031438, ], xy2d: [ - -22273315'i32, -13524424, -64685, -4334223, -18605636, - -10921968, -20571065, -7007978, -99853, -10237333 - ] + -22273315'i32, -13524424, -64685, -4334223, -18605636, -10921968, -20571065, + -7007978, -99853, -10237333, + ], ), GePrecomp( yplusx: [ - 17747465'i32, 10039260, 19368299, -4050591, -20630635, - -16041286, 31992683, -15857976, -29260363, -5511971 + 17747465'i32, 10039260, 19368299, -4050591, -20630635, -16041286, 31992683, + -15857976, -29260363, -5511971, ], yminusx: [ - 31932027'i32, -4986141, -19612382, 16366580, 22023614, - 88450, 11371999, -3744247, 4882242, -10626905 + 31932027'i32, -4986141, -19612382, 16366580, 22023614, 88450, 11371999, + -3744247, 4882242, -10626905, ], xy2d: [ - 29796507'i32, 37186, 19818052, 10115756, -11829032, - 3352736, 18551198, 3272828, -5190932, -4162409 - ] + 29796507'i32, 37186, 19818052, 10115756, -11829032, 3352736, 18551198, + 3272828, -5190932, -4162409, + ], ), GePrecomp( yplusx: [ - 12501286'i32, 4044383, -8612957, -13392385, -32430052, - 5136599, -19230378, -3529697, 330070, -3659409 + 12501286'i32, 4044383, -8612957, -13392385, -32430052, 5136599, -19230378, + -3529697, 330070, -3659409, ], yminusx: [ - 6384877'i32, 2899513, 17807477, 7663917, -2358888, - 12363165, 25366522, -8573892, -271295, 12071499 + 6384877'i32, 2899513, 17807477, 7663917, -2358888, 12363165, 25366522, + -8573892, -271295, 12071499, ], xy2d: [ - -8365515'i32, -4042521, 25133448, -4517355, -6211027, - 2265927, -32769618, 1936675, -5159697, 3829363 - ] + -8365515'i32, -4042521, 25133448, -4517355, -6211027, 2265927, -32769618, + 1936675, -5159697, 3829363, + ], ), GePrecomp( yplusx: [ - 28425966'i32, -5835433, -577090, -4697198, -14217555, - 6870930, 7921550, -6567787, 26333140, 14267664 + 28425966'i32, -5835433, -577090, -4697198, -14217555, 6870930, 7921550, + -6567787, 26333140, 14267664, ], yminusx: [ - -11067219'i32, 11871231, 27385719, -10559544, -4585914, - -11189312, 10004786, -8709488, -21761224, 8930324 + -11067219'i32, 11871231, 27385719, -10559544, -4585914, -11189312, 10004786, + -8709488, -21761224, 8930324, ], xy2d: [ - -21197785'i32, -16396035, 25654216, -1725397, 12282012, - 11008919, 1541940, 4757911, -26491501, -16408940 - ] + -21197785'i32, -16396035, 25654216, -1725397, 12282012, 11008919, 1541940, + 4757911, -26491501, -16408940, + ], ), GePrecomp( yplusx: [ - 13537262'i32, -7759490, -20604840, 10961927, -5922820, - -13218065, -13156584, 6217254, -15943699, 13814990 + 13537262'i32, -7759490, -20604840, 10961927, -5922820, -13218065, -13156584, + 6217254, -15943699, 13814990, ], yminusx: [ - -17422573'i32, 15157790, 18705543, 29619, 24409717, - -260476, 27361681, 9257833, -1956526, -1776914 + -17422573'i32, 15157790, 18705543, 29619, 24409717, -260476, 27361681, + 9257833, -1956526, -1776914, ], xy2d: [ - -25045300'i32, -10191966, 15366585, 15166509, -13105086, - 8423556, -29171540, 12361135, -18685978, 4578290 - ] + -25045300'i32, -10191966, 15366585, 15166509, -13105086, 8423556, -29171540, + 12361135, -18685978, 4578290, + ], ), GePrecomp( yplusx: [ - 24579768'i32, 3711570, 1342322, -11180126, -27005135, - 14124956, -22544529, 14074919, 21964432, 8235257 + 24579768'i32, 3711570, 1342322, -11180126, -27005135, 14124956, -22544529, + 14074919, 21964432, 8235257, ], yminusx: [ - -6528613'i32, -2411497, 9442966, -5925588, 12025640, - -1487420, -2981514, -1669206, 13006806, 2355433 + -6528613'i32, -2411497, 9442966, -5925588, 12025640, -1487420, -2981514, + -1669206, 13006806, 2355433, ], xy2d: [ - -16304899'i32, -13605259, -6632427, -5142349, 16974359, - -10911083, 27202044, 1719366, 1141648, -12796236 - ] + -16304899'i32, -13605259, -6632427, -5142349, 16974359, -10911083, 27202044, + 1719366, 1141648, -12796236, + ], ), GePrecomp( yplusx: [ - -12863944'i32, -13219986, -8318266, -11018091, -6810145, - -4843894, 13475066, -3133972, 32674895, 13715045 + -12863944'i32, -13219986, -8318266, -11018091, -6810145, -4843894, 13475066, + -3133972, 32674895, 13715045, ], yminusx: [ - 11423335'i32, -5468059, 32344216, 8962751, 24989809, - 9241752, -13265253, 16086212, -28740881, -15642093 + 11423335'i32, -5468059, 32344216, 8962751, 24989809, 9241752, -13265253, + 16086212, -28740881, -15642093, ], xy2d: [ - -1409668'i32, 12530728, -6368726, 10847387, 19531186, - -14132160, -11709148, 7791794, -27245943, 4383347 - ] - ) + -1409668'i32, 12530728, -6368726, 10847387, 19531186, -14132160, -11709148, + 7791794, -27245943, 4383347, + ], + ), ], [ GePrecomp( yplusx: [ - -28970898'i32, 5271447, -1266009, -9736989, -12455236, - 16732599, -4862407, -4906449, 27193557, 6245191 + -28970898'i32, 5271447, -1266009, -9736989, -12455236, 16732599, -4862407, + -4906449, 27193557, 6245191, ], yminusx: [ - -15193956'i32, 5362278, -1783893, 2695834, 4960227, - 12840725, 23061898, 3260492, 22510453, 8577507 + -15193956'i32, 5362278, -1783893, 2695834, 4960227, 12840725, 23061898, + 3260492, 22510453, 8577507, ], xy2d: [ - -12632451'i32, 11257346, -32692994, 13548177, -721004, - 10879011, 31168030, 13952092, -29571492, -3635906 - ] + -12632451'i32, 11257346, -32692994, 13548177, -721004, 10879011, 31168030, + 13952092, -29571492, -3635906, + ], ), GePrecomp( yplusx: [ - 3877321'i32, -9572739, 32416692, 5405324, -11004407, - -13656635, 3759769, 11935320, 5611860, 8164018 + 3877321'i32, -9572739, 32416692, 5405324, -11004407, -13656635, 3759769, + 11935320, 5611860, 8164018, ], yminusx: [ - -16275802'i32, 14667797, 15906460, 12155291, -22111149, - -9039718, 32003002, -8832289, 5773085, -8422109 + -16275802'i32, 14667797, 15906460, 12155291, -22111149, -9039718, 32003002, + -8832289, 5773085, -8422109, ], xy2d: [ - -23788118'i32, -8254300, 1950875, 8937633, 18686727, - 16459170, -905725, 12376320, 31632953, 190926 - ] + -23788118'i32, -8254300, 1950875, 8937633, 18686727, 16459170, -905725, + 12376320, 31632953, 190926, + ], ), GePrecomp( yplusx: [ - -24593607'i32, -16138885, -8423991, 13378746, 14162407, - 6901328, -8288749, 4508564, -25341555, -3627528 + -24593607'i32, -16138885, -8423991, 13378746, 14162407, 6901328, -8288749, + 4508564, -25341555, -3627528, ], yminusx: [ - 8884438'i32, -5884009, 6023974, 10104341, -6881569, - -4941533, 18722941, -14786005, -1672488, 827625 + 8884438'i32, -5884009, 6023974, 10104341, -6881569, -4941533, 18722941, + -14786005, -1672488, 827625, ], xy2d: [ - -32720583'i32, -16289296, -32503547, 7101210, 13354605, - 2659080, -1800575, -14108036, -24878478, 1541286 - ] + -32720583'i32, -16289296, -32503547, 7101210, 13354605, 2659080, -1800575, + -14108036, -24878478, 1541286, + ], ), GePrecomp( yplusx: [ - 2901347'i32, -1117687, 3880376, -10059388, -17620940, - -3612781, -21802117, -3567481, 20456845, -1885033 + 2901347'i32, -1117687, 3880376, -10059388, -17620940, -3612781, -21802117, + -3567481, 20456845, -1885033, ], yminusx: [ - 27019610'i32, 12299467, -13658288, -1603234, -12861660, - -4861471, -19540150, -5016058, 29439641, 15138866 + 27019610'i32, 12299467, -13658288, -1603234, -12861660, -4861471, -19540150, + -5016058, 29439641, 15138866, ], xy2d: [ - 21536104'i32, -6626420, -32447818, -10690208, -22408077, - 5175814, -5420040, -16361163, 7779328, 109896 - ] + 21536104'i32, -6626420, -32447818, -10690208, -22408077, 5175814, -5420040, + -16361163, 7779328, 109896, + ], ), GePrecomp( yplusx: [ - 30279744'i32, 14648750, -8044871, 6425558, 13639621, - -743509, 28698390, 12180118, 23177719, -554075 + 30279744'i32, 14648750, -8044871, 6425558, 13639621, -743509, 28698390, + 12180118, 23177719, -554075, ], yminusx: [ - 26572847'i32, 3405927, -31701700, 12890905, -19265668, - 5335866, -6493768, 2378492, 4439158, -13279347 + 26572847'i32, 3405927, -31701700, 12890905, -19265668, 5335866, -6493768, + 2378492, 4439158, -13279347, ], xy2d: [ - -22716706'i32, 3489070, -9225266, -332753, 18875722, - -1140095, 14819434, -12731527, -17717757, -5461437 - ] + -22716706'i32, 3489070, -9225266, -332753, 18875722, -1140095, 14819434, + -12731527, -17717757, -5461437, + ], ), GePrecomp( yplusx: [ - -5056483'i32, 16566551, 15953661, 3767752, -10436499, - 15627060, -820954, 2177225, 8550082, -15114165 + -5056483'i32, 16566551, 15953661, 3767752, -10436499, 15627060, -820954, + 2177225, 8550082, -15114165, ], yminusx: [ - -18473302'i32, 16596775, -381660, 15663611, 22860960, - 15585581, -27844109, -3582739, -23260460, -8428588 + -18473302'i32, 16596775, -381660, 15663611, 22860960, 15585581, -27844109, + -3582739, -23260460, -8428588, ], xy2d: [ - -32480551'i32, 15707275, -8205912, -5652081, 29464558, - 2713815, -22725137, 15860482, -21902570, 1494193 - ] + -32480551'i32, 15707275, -8205912, -5652081, 29464558, 2713815, -22725137, + 15860482, -21902570, 1494193, + ], ), GePrecomp( yplusx: [ - -19562091'i32, -14087393, -25583872, -9299552, 13127842, - 759709, 21923482, 16529112, 8742704, 12967017 + -19562091'i32, -14087393, -25583872, -9299552, 13127842, 759709, 21923482, + 16529112, 8742704, 12967017, ], yminusx: [ - -28464899'i32, 1553205, 32536856, -10473729, -24691605, - -406174, -8914625, -2933896, -29903758, 15553883 + -28464899'i32, 1553205, 32536856, -10473729, -24691605, -406174, -8914625, + -2933896, -29903758, 15553883, ], xy2d: [ - 21877909'i32, 3230008, 9881174, 10539357, -4797115, - 2841332, 11543572, 14513274, 19375923, -12647961 - ] + 21877909'i32, 3230008, 9881174, 10539357, -4797115, 2841332, 11543572, + 14513274, 19375923, -12647961, + ], ), GePrecomp( yplusx: [ - 8832269'i32, -14495485, 13253511, 5137575, 5037871, - 4078777, 24880818, -6222716, 2862653, 9455043 + 8832269'i32, -14495485, 13253511, 5137575, 5037871, 4078777, 24880818, + -6222716, 2862653, 9455043, ], yminusx: [ - 29306751'i32, 5123106, 20245049, -14149889, 9592566, - 8447059, -2077124, -2990080, 15511449, 4789663 + 29306751'i32, 5123106, 20245049, -14149889, 9592566, 8447059, -2077124, + -2990080, 15511449, 4789663, ], xy2d: [ - -20679756'i32, 7004547, 8824831, -9434977, -4045704, - -3750736, -5754762, 108893, 23513200, 16652362 - ] - ) + -20679756'i32, 7004547, 8824831, -9434977, -4045704, -3750736, -5754762, + 108893, 23513200, 16652362, + ], + ), ], [ GePrecomp( yplusx: [ - -33256173'i32, 4144782, -4476029, -6579123, 10770039, - -7155542, -6650416, -12936300, -18319198, 10212860 + -33256173'i32, 4144782, -4476029, -6579123, 10770039, -7155542, -6650416, + -12936300, -18319198, 10212860, ], yminusx: [ - 2756081'i32, 8598110, 7383731, -6859892, 22312759, - -1105012, 21179801, 2600940, -9988298, -12506466 + 2756081'i32, 8598110, 7383731, -6859892, 22312759, -1105012, 21179801, + 2600940, -9988298, -12506466, ], xy2d: [ - -24645692'i32, 13317462, -30449259, -15653928, 21365574, - -10869657, 11344424, 864440, -2499677, -16710063 - ] + -24645692'i32, 13317462, -30449259, -15653928, 21365574, -10869657, 11344424, + 864440, -2499677, -16710063, + ], ), GePrecomp( yplusx: [ - -26432803'i32, 6148329, -17184412, -14474154, 18782929, - -275997, -22561534, 211300, 2719757, 4940997 + -26432803'i32, 6148329, -17184412, -14474154, 18782929, -275997, -22561534, + 211300, 2719757, 4940997, ], yminusx: [ - -1323882'i32, 3911313, -6948744, 14759765, -30027150, - 7851207, 21690126, 8518463, 26699843, 5276295 + -1323882'i32, 3911313, -6948744, 14759765, -30027150, 7851207, 21690126, + 8518463, 26699843, 5276295, ], xy2d: [ - -13149873'i32, -6429067, 9396249, 365013, 24703301, - -10488939, 1321586, 149635, -15452774, 7159369 - ] + -13149873'i32, -6429067, 9396249, 365013, 24703301, -10488939, 1321586, + 149635, -15452774, 7159369, + ], ), GePrecomp( yplusx: [ - 9987780'i32, -3404759, 17507962, 9505530, 9731535, - -2165514, 22356009, 8312176, 22477218, -8403385 + 9987780'i32, -3404759, 17507962, 9505530, 9731535, -2165514, 22356009, + 8312176, 22477218, -8403385, ], yminusx: [ - 18155857'i32, -16504990, 19744716, 9006923, 15154154, - -10538976, 24256460, -4864995, -22548173, 9334109 + 18155857'i32, -16504990, 19744716, 9006923, 15154154, -10538976, 24256460, + -4864995, -22548173, 9334109, ], xy2d: [ - 2986088'i32, -4911893, 10776628, -3473844, 10620590, - -7083203, -21413845, 14253545, -22587149, 536906 - ] + 2986088'i32, -4911893, 10776628, -3473844, 10620590, -7083203, -21413845, + 14253545, -22587149, 536906, + ], ), GePrecomp( yplusx: [ - 4377756'i32, 8115836, 24567078, 15495314, 11625074, - 13064599, 7390551, 10589625, 10838060, -15420424 + 4377756'i32, 8115836, 24567078, 15495314, 11625074, 13064599, 7390551, + 10589625, 10838060, -15420424, ], yminusx: [ - -19342404'i32, 867880, 9277171, -3218459, -14431572, - -1986443, 19295826, -15796950, 6378260, 699185 + -19342404'i32, 867880, 9277171, -3218459, -14431572, -1986443, 19295826, + -15796950, 6378260, 699185, ], xy2d: [ - 7895026'i32, 4057113, -7081772, -13077756, -17886831, - -323126, -716039, 15693155, -5045064, -13373962 - ] + 7895026'i32, 4057113, -7081772, -13077756, -17886831, -323126, -716039, + 15693155, -5045064, -13373962, + ], ), GePrecomp( yplusx: [ - -7737563'i32, -5869402, -14566319, -7406919, 11385654, - 13201616, 31730678, -10962840, -3918636, -9669325 + -7737563'i32, -5869402, -14566319, -7406919, 11385654, 13201616, 31730678, + -10962840, -3918636, -9669325, ], yminusx: [ - 10188286'i32, -15770834, -7336361, 13427543, 22223443, - 14896287, 30743455, 7116568, -21786507, 5427593 + 10188286'i32, -15770834, -7336361, 13427543, 22223443, 14896287, 30743455, + 7116568, -21786507, 5427593, ], xy2d: [ - 696102'i32, 13206899, 27047647, -10632082, 15285305, - -9853179, 10798490, -4578720, 19236243, 12477404 - ] + 696102'i32, 13206899, 27047647, -10632082, 15285305, -9853179, 10798490, + -4578720, 19236243, 12477404, + ], ), GePrecomp( yplusx: [ - -11229439'i32, 11243796, -17054270, -8040865, -788228, - -8167967, -3897669, 11180504, -23169516, 7733644 + -11229439'i32, 11243796, -17054270, -8040865, -788228, -8167967, -3897669, + 11180504, -23169516, 7733644, ], yminusx: [ - 17800790'i32, -14036179, -27000429, -11766671, 23887827, - 3149671, 23466177, -10538171, 10322027, 15313801 + 17800790'i32, -14036179, -27000429, -11766671, 23887827, 3149671, 23466177, + -10538171, 10322027, 15313801, ], xy2d: [ - 26246234'i32, 11968874, 32263343, -5468728, 6830755, - -13323031, -15794704, -101982, -24449242, 10890804 - ] + 26246234'i32, 11968874, 32263343, -5468728, 6830755, -13323031, -15794704, + -101982, -24449242, 10890804, + ], ), GePrecomp( yplusx: [ - -31365647'i32, 10271363, -12660625, -6267268, 16690207, - -13062544, -14982212, 16484931, 25180797, -5334884 + -31365647'i32, 10271363, -12660625, -6267268, 16690207, -13062544, -14982212, + 16484931, 25180797, -5334884, ], yminusx: [ - -586574'i32, 10376444, -32586414, -11286356, 19801893, - 10997610, 2276632, 9482883, 316878, 13820577 + -586574'i32, 10376444, -32586414, -11286356, 19801893, 10997610, 2276632, + 9482883, 316878, 13820577, ], xy2d: [ - -9882808'i32, -4510367, -2115506, 16457136, -11100081, - 11674996, 30756178, -7515054, 30696930, -3712849 - ] + -9882808'i32, -4510367, -2115506, 16457136, -11100081, 11674996, 30756178, + -7515054, 30696930, -3712849, + ], ), GePrecomp( yplusx: [ - 32988917'i32, -9603412, 12499366, 7910787, -10617257, - -11931514, -7342816, -9985397, -32349517, 7392473 + 32988917'i32, -9603412, 12499366, 7910787, -10617257, -11931514, -7342816, + -9985397, -32349517, 7392473, ], yminusx: [ - -8855661'i32, 15927861, 9866406, -3649411, -2396914, - -16655781, -30409476, -9134995, 25112947, -2926644 + -8855661'i32, 15927861, 9866406, -3649411, -2396914, -16655781, -30409476, + -9134995, 25112947, -2926644, ], xy2d: [ - -2504044'i32, -436966, 25621774, -5678772, 15085042, - -5479877, -24884878, -13526194, 5537438, -13914319 - ] - ) + -2504044'i32, -436966, 25621774, -5678772, 15085042, -5479877, -24884878, + -13526194, 5537438, -13914319, + ], + ), ], [ GePrecomp( yplusx: [ - -11225584'i32, 2320285, -9584280, 10149187, -33444663, - 5808648, -14876251, -1729667, 31234590, 6090599 + -11225584'i32, 2320285, -9584280, 10149187, -33444663, 5808648, -14876251, + -1729667, 31234590, 6090599, ], yminusx: [ - -9633316'i32, 116426, 26083934, 2897444, -6364437, - -2688086, 609721, 15878753, -6970405, -9034768 + -9633316'i32, 116426, 26083934, 2897444, -6364437, -2688086, 609721, 15878753, + -6970405, -9034768, ], xy2d: [ - -27757857'i32, 247744, -15194774, -9002551, 23288161, - -10011936, -23869595, 6503646, 20650474, 1804084 - ] + -27757857'i32, 247744, -15194774, -9002551, 23288161, -10011936, -23869595, + 6503646, 20650474, 1804084, + ], ), GePrecomp( yplusx: [ - -27589786'i32, 15456424, 8972517, 8469608, 15640622, - 4439847, 3121995, -10329713, 27842616, -202328 + -27589786'i32, 15456424, 8972517, 8469608, 15640622, 4439847, 3121995, + -10329713, 27842616, -202328, ], yminusx: [ - -15306973'i32, 2839644, 22530074, 10026331, 4602058, - 5048462, 28248656, 5031932, -11375082, 12714369 + -15306973'i32, 2839644, 22530074, 10026331, 4602058, 5048462, 28248656, + 5031932, -11375082, 12714369, ], xy2d: [ - 20807691'i32, -7270825, 29286141, 11421711, -27876523, - -13868230, -21227475, 1035546, -19733229, 12796920 - ] + 20807691'i32, -7270825, 29286141, 11421711, -27876523, -13868230, -21227475, + 1035546, -19733229, 12796920, + ], ), GePrecomp( yplusx: [ - 12076899'i32, -14301286, -8785001, -11848922, -25012791, - 16400684, -17591495, -12899438, 3480665, -15182815 + 12076899'i32, -14301286, -8785001, -11848922, -25012791, 16400684, -17591495, + -12899438, 3480665, -15182815, ], yminusx: [ - -32361549'i32, 5457597, 28548107, 7833186, 7303070, - -11953545, -24363064, -15921875, -33374054, 2771025 + -32361549'i32, 5457597, 28548107, 7833186, 7303070, -11953545, -24363064, + -15921875, -33374054, 2771025, ], xy2d: [ - -21389266'i32, 421932, 26597266, 6860826, 22486084, - -6737172, -17137485, -4210226, -24552282, 15673397 - ] + -21389266'i32, 421932, 26597266, 6860826, 22486084, -6737172, -17137485, + -4210226, -24552282, 15673397, + ], ), GePrecomp( yplusx: [ - -20184622'i32, 2338216, 19788685, -9620956, -4001265, - -8740893, -20271184, 4733254, 3727144, -12934448 + -20184622'i32, 2338216, 19788685, -9620956, -4001265, -8740893, -20271184, + 4733254, 3727144, -12934448, ], yminusx: [ - 6120119'i32, 814863, -11794402, -622716, 6812205, - -15747771, 2019594, 7975683, 31123697, -10958981 + 6120119'i32, 814863, -11794402, -622716, 6812205, -15747771, 2019594, 7975683, + 31123697, -10958981, ], xy2d: [ - 30069250'i32, -11435332, 30434654, 2958439, 18399564, - -976289, 12296869, 9204260, -16432438, 9648165 - ] + 30069250'i32, -11435332, 30434654, 2958439, 18399564, -976289, 12296869, + 9204260, -16432438, 9648165, + ], ), GePrecomp( yplusx: [ - 32705432'i32, -1550977, 30705658, 7451065, -11805606, - 9631813, 3305266, 5248604, -26008332, -11377501 + 32705432'i32, -1550977, 30705658, 7451065, -11805606, 9631813, 3305266, + 5248604, -26008332, -11377501, ], yminusx: [ - 17219865'i32, 2375039, -31570947, -5575615, -19459679, - 9219903, 294711, 15298639, 2662509, -16297073 + 17219865'i32, 2375039, -31570947, -5575615, -19459679, 9219903, 294711, + 15298639, 2662509, -16297073, ], xy2d: [ - -1172927'i32, -7558695, -4366770, -4287744, -21346413, - -8434326, 32087529, -1222777, 32247248, -14389861 - ] + -1172927'i32, -7558695, -4366770, -4287744, -21346413, -8434326, 32087529, + -1222777, 32247248, -14389861, + ], ), GePrecomp( yplusx: [ - 14312628'i32, 1221556, 17395390, -8700143, -4945741, - -8684635, -28197744, -9637817, -16027623, -13378845 + 14312628'i32, 1221556, 17395390, -8700143, -4945741, -8684635, -28197744, + -9637817, -16027623, -13378845, ], yminusx: [ - -1428825'i32, -9678990, -9235681, 6549687, -7383069, - -468664, 23046502, 9803137, 17597934, 2346211 + -1428825'i32, -9678990, -9235681, 6549687, -7383069, -468664, 23046502, + 9803137, 17597934, 2346211, ], xy2d: [ - 18510800'i32, 15337574, 26171504, 981392, -22241552, - 7827556, -23491134, -11323352, 3059833, -11782870 - ] + 18510800'i32, 15337574, 26171504, 981392, -22241552, 7827556, -23491134, + -11323352, 3059833, -11782870, + ], ), GePrecomp( yplusx: [ - 10141598'i32, 6082907, 17829293, -1947643, 9830092, - 13613136, -25556636, -5544586, -33502212, 3592096 + 10141598'i32, 6082907, 17829293, -1947643, 9830092, 13613136, -25556636, + -5544586, -33502212, 3592096, ], yminusx: [ - 33114168'i32, -15889352, -26525686, -13343397, 33076705, - 8716171, 1151462, 1521897, -982665, -6837803 + 33114168'i32, -15889352, -26525686, -13343397, 33076705, 8716171, 1151462, + 1521897, -982665, -6837803, ], xy2d: [ - -32939165'i32, -4255815, 23947181, -324178, -33072974, - -12305637, -16637686, 3891704, 26353178, 693168 - ] + -32939165'i32, -4255815, 23947181, -324178, -33072974, -12305637, -16637686, + 3891704, 26353178, 693168, + ], ), GePrecomp( yplusx: [ - 30374239'i32, 1595580, -16884039, 13186931, 4600344, - 406904, 9585294, -400668, 31375464, 14369965 + 30374239'i32, 1595580, -16884039, 13186931, 4600344, 406904, 9585294, -400668, + 31375464, 14369965, ], yminusx: [ - -14370654'i32, -7772529, 1510301, 6434173, -18784789, - -6262728, 32732230, -13108839, 17901441, 16011505 + -14370654'i32, -7772529, 1510301, 6434173, -18784789, -6262728, 32732230, + -13108839, 17901441, 16011505, ], xy2d: [ - 18171223'i32, -11934626, -12500402, 15197122, -11038147, - -15230035, -19172240, -16046376, 8764035, 12309598 - ] - ) + 18171223'i32, -11934626, -12500402, 15197122, -11038147, -15230035, -19172240, + -16046376, 8764035, 12309598, + ], + ), ], [ GePrecomp( yplusx: [ - 5975908'i32, -5243188, -19459362, -9681747, -11541277, - 14015782, -23665757, 1228319, 17544096, -10593782 + 5975908'i32, -5243188, -19459362, -9681747, -11541277, 14015782, -23665757, + 1228319, 17544096, -10593782, ], yminusx: [ - 5811932'i32, -1715293, 3442887, -2269310, -18367348, - -8359541, -18044043, -15410127, -5565381, 12348900 + 5811932'i32, -1715293, 3442887, -2269310, -18367348, -8359541, -18044043, + -15410127, -5565381, 12348900, ], xy2d: [ - -31399660'i32, 11407555, 25755363, 6891399, -3256938, - 14872274, -24849353, 8141295, -10632534, -585479 - ] + -31399660'i32, 11407555, 25755363, 6891399, -3256938, 14872274, -24849353, + 8141295, -10632534, -585479, + ], ), GePrecomp( yplusx: [ - -12675304'i32, 694026, -5076145, 13300344, 14015258, - -14451394, -9698672, -11329050, 30944593, 1130208 + -12675304'i32, 694026, -5076145, 13300344, 14015258, -14451394, -9698672, + -11329050, 30944593, 1130208, ], yminusx: [ - 8247766'i32, -6710942, -26562381, -7709309, -14401939, - -14648910, 4652152, 2488540, 23550156, -271232 + 8247766'i32, -6710942, -26562381, -7709309, -14401939, -14648910, 4652152, + 2488540, 23550156, -271232, ], xy2d: [ - 17294316'i32, -3788438, 7026748, 15626851, 22990044, - 113481, 2267737, -5908146, -408818, -137719 - ] + 17294316'i32, -3788438, 7026748, 15626851, 22990044, 113481, 2267737, + -5908146, -408818, -137719, + ], ), GePrecomp( yplusx: [ - 16091085'i32, -16253926, 18599252, 7340678, 2137637, - -1221657, -3364161, 14550936, 3260525, -7166271 + 16091085'i32, -16253926, 18599252, 7340678, 2137637, -1221657, -3364161, + 14550936, 3260525, -7166271, ], yminusx: [ - -4910104'i32, -13332887, 18550887, 10864893, -16459325, - -7291596, -23028869, -13204905, -12748722, 2701326 + -4910104'i32, -13332887, 18550887, 10864893, -16459325, -7291596, -23028869, + -13204905, -12748722, 2701326, ], xy2d: [ - -8574695'i32, 16099415, 4629974, -16340524, -20786213, - -6005432, -10018363, 9276971, 11329923, 1862132 - ] + -8574695'i32, 16099415, 4629974, -16340524, -20786213, -6005432, -10018363, + 9276971, 11329923, 1862132, + ], ), GePrecomp( yplusx: [ - 14763076'i32, -15903608, -30918270, 3689867, 3511892, - 10313526, -21951088, 12219231, -9037963, -940300 + 14763076'i32, -15903608, -30918270, 3689867, 3511892, 10313526, -21951088, + 12219231, -9037963, -940300, ], yminusx: [ - 8894987'i32, -3446094, 6150753, 3013931, 301220, - 15693451, -31981216, -2909717, -15438168, 11595570 + 8894987'i32, -3446094, 6150753, 3013931, 301220, 15693451, -31981216, + -2909717, -15438168, 11595570, ], xy2d: [ - 15214962'i32, 3537601, -26238722, -14058872, 4418657, - -15230761, 13947276, 10730794, -13489462, -4363670 - ] + 15214962'i32, 3537601, -26238722, -14058872, 4418657, -15230761, 13947276, + 10730794, -13489462, -4363670, + ], ), GePrecomp( yplusx: [ - -2538306'i32, 7682793, 32759013, 263109, -29984731, - -7955452, -22332124, -10188635, 977108, 699994 + -2538306'i32, 7682793, 32759013, 263109, -29984731, -7955452, -22332124, + -10188635, 977108, 699994, ], yminusx: [ - -12466472'i32, 4195084, -9211532, 550904, -15565337, - 12917920, 19118110, -439841, -30534533, -14337913 + -12466472'i32, 4195084, -9211532, 550904, -15565337, 12917920, 19118110, + -439841, -30534533, -14337913, ], xy2d: [ - 31788461'i32, -14507657, 4799989, 7372237, 8808585, - -14747943, 9408237, -10051775, 12493932, -5409317 - ] + 31788461'i32, -14507657, 4799989, 7372237, 8808585, -14747943, 9408237, + -10051775, 12493932, -5409317, + ], ), GePrecomp( yplusx: [ - -25680606'i32, 5260744, -19235809, -6284470, -3695942, - 16566087, 27218280, 2607121, 29375955, 6024730 + -25680606'i32, 5260744, -19235809, -6284470, -3695942, 16566087, 27218280, + 2607121, 29375955, 6024730, ], yminusx: [ - 842132'i32, -2794693, -4763381, -8722815, 26332018, - -12405641, 11831880, 6985184, -9940361, 2854096 + 842132'i32, -2794693, -4763381, -8722815, 26332018, -12405641, 11831880, + 6985184, -9940361, 2854096, ], xy2d: [ - -4847262'i32, -7969331, 2516242, -5847713, 9695691, - -7221186, 16512645, 960770, 12121869, 16648078 - ] + -4847262'i32, -7969331, 2516242, -5847713, 9695691, -7221186, 16512645, + 960770, 12121869, 16648078, + ], ), GePrecomp( yplusx: [ - -15218652'i32, 14667096, -13336229, 2013717, 30598287, - -464137, -31504922, -7882064, 20237806, 2838411 + -15218652'i32, 14667096, -13336229, 2013717, 30598287, -464137, -31504922, + -7882064, 20237806, 2838411, ], yminusx: [ - -19288047'i32, 4453152, 15298546, -16178388, 22115043, - -15972604, 12544294, -13470457, 1068881, -12499905 + -19288047'i32, 4453152, 15298546, -16178388, 22115043, -15972604, 12544294, + -13470457, 1068881, -12499905, ], xy2d: [ - -9558883'i32, -16518835, 33238498, 13506958, 30505848, - -1114596, -8486907, -2630053, 12521378, 4845654 - ] + -9558883'i32, -16518835, 33238498, 13506958, 30505848, -1114596, -8486907, + -2630053, 12521378, 4845654, + ], ), GePrecomp( yplusx: [ - -28198521'i32, 10744108, -2958380, 10199664, 7759311, - -13088600, 3409348, -873400, -6482306, -12885870 + -28198521'i32, 10744108, -2958380, 10199664, 7759311, -13088600, 3409348, + -873400, -6482306, -12885870, ], yminusx: [ - -23561822'i32, 6230156, -20382013, 10655314, -24040585, - -11621172, 10477734, -1240216, -3113227, 13974498 + -23561822'i32, 6230156, -20382013, 10655314, -24040585, -11621172, 10477734, + -1240216, -3113227, 13974498, ], xy2d: [ - 12966261'i32, 15550616, -32038948, -1615346, 21025980, - -629444, 5642325, 7188737, 18895762, 12629579 - ] - ) + 12966261'i32, 15550616, -32038948, -1615346, 21025980, -629444, 5642325, + 7188737, 18895762, 12629579, + ], + ), ], [ GePrecomp( yplusx: [ - 14741879'i32, -14946887, 22177208, -11721237, 1279741, - 8058600, 11758140, 789443, 32195181, 3895677 + 14741879'i32, -14946887, 22177208, -11721237, 1279741, 8058600, 11758140, + 789443, 32195181, 3895677, ], yminusx: [ - 10758205'i32, 15755439, -4509950, 9243698, -4879422, - 6879879, -2204575, -3566119, -8982069, 4429647 + 10758205'i32, 15755439, -4509950, 9243698, -4879422, 6879879, -2204575, + -3566119, -8982069, 4429647, ], xy2d: [ - -2453894'i32, 15725973, -20436342, -10410672, -5803908, - -11040220, -7135870, -11642895, 18047436, -15281743 - ] + -2453894'i32, 15725973, -20436342, -10410672, -5803908, -11040220, -7135870, + -11642895, 18047436, -15281743, + ], ), GePrecomp( yplusx: [ - -25173001'i32, -11307165, 29759956, 11776784, -22262383, - -15820455, 10993114, -12850837, -17620701, -9408468 + -25173001'i32, -11307165, 29759956, 11776784, -22262383, -15820455, 10993114, + -12850837, -17620701, -9408468, ], yminusx: [ - 21987233'i32, 700364, -24505048, 14972008, -7774265, - -5718395, 32155026, 2581431, -29958985, 8773375 + 21987233'i32, 700364, -24505048, 14972008, -7774265, -5718395, 32155026, + 2581431, -29958985, 8773375, ], xy2d: [ - -25568350'i32, 454463, -13211935, 16126715, 25240068, - 8594567, 20656846, 12017935, -7874389, -13920155 - ] + -25568350'i32, 454463, -13211935, 16126715, 25240068, 8594567, 20656846, + 12017935, -7874389, -13920155, + ], ), GePrecomp( yplusx: [ - 6028182'i32, 6263078, -31011806, -11301710, -818919, - 2461772, -31841174, -5468042, -1721788, -2776725 + 6028182'i32, 6263078, -31011806, -11301710, -818919, 2461772, -31841174, + -5468042, -1721788, -2776725, ], yminusx: [ - -12278994'i32, 16624277, 987579, -5922598, 32908203, - 1248608, 7719845, -4166698, 28408820, 6816612 + -12278994'i32, 16624277, 987579, -5922598, 32908203, 1248608, 7719845, + -4166698, 28408820, 6816612, ], xy2d: [ - -10358094'i32, -8237829, 19549651, -12169222, 22082623, - 16147817, 20613181, 13982702, -10339570, 5067943 - ] + -10358094'i32, -8237829, 19549651, -12169222, 22082623, 16147817, 20613181, + 13982702, -10339570, 5067943, + ], ), GePrecomp( yplusx: [ - -30505967'i32, -3821767, 12074681, 13582412, -19877972, - 2443951, -19719286, 12746132, 5331210, -10105944 + -30505967'i32, -3821767, 12074681, 13582412, -19877972, 2443951, -19719286, + 12746132, 5331210, -10105944, ], yminusx: [ - 30528811'i32, 3601899, -1957090, 4619785, -27361822, - -15436388, 24180793, -12570394, 27679908, -1648928 + 30528811'i32, 3601899, -1957090, 4619785, -27361822, -15436388, 24180793, + -12570394, 27679908, -1648928, ], xy2d: [ - 9402404'i32, -13957065, 32834043, 10838634, -26580150, - -13237195, 26653274, -8685565, 22611444, -12715406 - ] + 9402404'i32, -13957065, 32834043, 10838634, -26580150, -13237195, 26653274, + -8685565, 22611444, -12715406, + ], ), GePrecomp( yplusx: [ - 22190590'i32, 1118029, 22736441, 15130463, -30460692, - -5991321, 19189625, -4648942, 4854859, 6622139 + 22190590'i32, 1118029, 22736441, 15130463, -30460692, -5991321, 19189625, + -4648942, 4854859, 6622139, ], yminusx: [ - -8310738'i32, -2953450, -8262579, -3388049, -10401731, - -271929, 13424426, -3567227, 26404409, 13001963 + -8310738'i32, -2953450, -8262579, -3388049, -10401731, -271929, 13424426, + -3567227, 26404409, 13001963, ], xy2d: [ - -31241838'i32, -15415700, -2994250, 8939346, 11562230, - -12840670, -26064365, -11621720, -15405155, 11020693 - ] + -31241838'i32, -15415700, -2994250, 8939346, 11562230, -12840670, -26064365, + -11621720, -15405155, 11020693, + ], ), GePrecomp( yplusx: [ - 1866042'i32, -7949489, -7898649, -10301010, 12483315, - 13477547, 3175636, -12424163, 28761762, 1406734 + 1866042'i32, -7949489, -7898649, -10301010, 12483315, 13477547, 3175636, + -12424163, 28761762, 1406734, ], yminusx: [ - -448555'i32, -1777666, 13018551, 3194501, -9580420, - -11161737, 24760585, -4347088, 25577411, -13378680 + -448555'i32, -1777666, 13018551, 3194501, -9580420, -11161737, 24760585, + -4347088, 25577411, -13378680, ], xy2d: [ - -24290378'i32, 4759345, -690653, -1852816, 2066747, - 10693769, -29595790, 9884936, -9368926, 4745410 - ] + -24290378'i32, 4759345, -690653, -1852816, 2066747, 10693769, -29595790, + 9884936, -9368926, 4745410, + ], ), GePrecomp( yplusx: [ - -9141284'i32, 6049714, -19531061, -4341411, -31260798, - 9944276, -15462008, -11311852, 10931924, -11931931 + -9141284'i32, 6049714, -19531061, -4341411, -31260798, 9944276, -15462008, + -11311852, 10931924, -11931931, ], yminusx: [ - -16561513'i32, 14112680, -8012645, 4817318, -8040464, - -11414606, -22853429, 10856641, -20470770, 13434654 + -16561513'i32, 14112680, -8012645, 4817318, -8040464, -11414606, -22853429, + 10856641, -20470770, 13434654, ], xy2d: [ - 22759489'i32, -10073434, -16766264, -1871422, 13637442, - -10168091, 1765144, -12654326, 28445307, -5364710 - ] + 22759489'i32, -10073434, -16766264, -1871422, 13637442, -10168091, 1765144, + -12654326, 28445307, -5364710, + ], ), GePrecomp( yplusx: [ - 29875063'i32, 12493613, 2795536, -3786330, 1710620, - 15181182, -10195717, -8788675, 9074234, 1167180 + 29875063'i32, 12493613, 2795536, -3786330, 1710620, 15181182, -10195717, + -8788675, 9074234, 1167180, ], yminusx: [ - -26205683'i32, 11014233, -9842651, -2635485, -26908120, - 7532294, -18716888, -9535498, 3843903, 9367684 + -26205683'i32, 11014233, -9842651, -2635485, -26908120, 7532294, -18716888, + -9535498, 3843903, 9367684, ], xy2d: [ - -10969595'i32, -6403711, 9591134, 9582310, 11349256, - 108879, 16235123, 8601684, -139197, 4242895 - ] - ) + -10969595'i32, -6403711, 9591134, 9582310, 11349256, 108879, 16235123, + 8601684, -139197, 4242895, + ], + ), ], [ GePrecomp( yplusx: [ - 22092954'i32, -13191123, -2042793, -11968512, 32186753, - -11517388, -6574341, 2470660, -27417366, 16625501 + 22092954'i32, -13191123, -2042793, -11968512, 32186753, -11517388, -6574341, + 2470660, -27417366, 16625501, ], yminusx: [ - -11057722'i32, 3042016, 13770083, -9257922, 584236, - -544855, -7770857, 2602725, -27351616, 14247413 + -11057722'i32, 3042016, 13770083, -9257922, 584236, -544855, -7770857, + 2602725, -27351616, 14247413, ], xy2d: [ - 6314175'i32, -10264892, -32772502, 15957557, -10157730, - 168750, -8618807, 14290061, 27108877, -1180880 - ] + 6314175'i32, -10264892, -32772502, 15957557, -10157730, 168750, -8618807, + 14290061, 27108877, -1180880, + ], ), GePrecomp( yplusx: [ - -8586597'i32, -7170966, 13241782, 10960156, -32991015, - -13794596, 33547976, -11058889, -27148451, 981874 + -8586597'i32, -7170966, 13241782, 10960156, -32991015, -13794596, 33547976, + -11058889, -27148451, 981874, ], yminusx: [ - 22833440'i32, 9293594, -32649448, -13618667, -9136966, - 14756819, -22928859, -13970780, -10479804, -16197962 + 22833440'i32, 9293594, -32649448, -13618667, -9136966, 14756819, -22928859, + -13970780, -10479804, -16197962, ], xy2d: [ - -7768587'i32, 3326786, -28111797, 10783824, 19178761, - 14905060, 22680049, 13906969, -15933690, 3797899 - ] + -7768587'i32, 3326786, -28111797, 10783824, 19178761, 14905060, 22680049, + 13906969, -15933690, 3797899, + ], ), GePrecomp( yplusx: [ - 21721356'i32, -4212746, -12206123, 9310182, -3882239, - -13653110, 23740224, -2709232, 20491983, -8042152 + 21721356'i32, -4212746, -12206123, 9310182, -3882239, -13653110, 23740224, + -2709232, 20491983, -8042152, ], yminusx: [ - 9209270'i32, -15135055, -13256557, -6167798, -731016, - 15289673, 25947805, 15286587, 30997318, -6703063 + 9209270'i32, -15135055, -13256557, -6167798, -731016, 15289673, 25947805, + 15286587, 30997318, -6703063, ], xy2d: [ - 7392032'i32, 16618386, 23946583, -8039892, -13265164, - -1533858, -14197445, -2321576, 17649998, -250080 - ] + 7392032'i32, 16618386, 23946583, -8039892, -13265164, -1533858, -14197445, + -2321576, 17649998, -250080, + ], ), GePrecomp( yplusx: [ - -9301088'i32, -14193827, 30609526, -3049543, -25175069, - -1283752, -15241566, -9525724, -2233253, 7662146 + -9301088'i32, -14193827, 30609526, -3049543, -25175069, -1283752, -15241566, + -9525724, -2233253, 7662146, ], yminusx: [ - -17558673'i32, 1763594, -33114336, 15908610, -30040870, - -12174295, 7335080, -8472199, -3174674, 3440183 + -17558673'i32, 1763594, -33114336, 15908610, -30040870, -12174295, 7335080, + -8472199, -3174674, 3440183, ], xy2d: [ - -19889700'i32, -5977008, -24111293, -9688870, 10799743, - -16571957, 40450, -4431835, 4862400, 1133 - ] + -19889700'i32, -5977008, -24111293, -9688870, 10799743, -16571957, 40450, + -4431835, 4862400, 1133, + ], ), GePrecomp( yplusx: [ - -32856209'i32, -7873957, -5422389, 14860950, -16319031, - 7956142, 7258061, 311861, -30594991, -7379421 + -32856209'i32, -7873957, -5422389, 14860950, -16319031, 7956142, 7258061, + 311861, -30594991, -7379421, ], yminusx: [ - -3773428'i32, -1565936, 28985340, 7499440, 24445838, - 9325937, 29727763, 16527196, 18278453, 15405622 + -3773428'i32, -1565936, 28985340, 7499440, 24445838, 9325937, 29727763, + 16527196, 18278453, 15405622, ], xy2d: [ - -4381906'i32, 8508652, -19898366, -3674424, -5984453, - 15149970, -13313598, 843523, -21875062, 13626197 - ] + -4381906'i32, 8508652, -19898366, -3674424, -5984453, 15149970, -13313598, + 843523, -21875062, 13626197, + ], ), GePrecomp( yplusx: [ - 2281448'i32, -13487055, -10915418, -2609910, 1879358, - 16164207, -10783882, 3953792, 13340839, 15928663 + 2281448'i32, -13487055, -10915418, -2609910, 1879358, 16164207, -10783882, + 3953792, 13340839, 15928663, ], yminusx: [ - 31727126'i32, -7179855, -18437503, -8283652, 2875793, - -16390330, -25269894, -7014826, -23452306, 5964753 + 31727126'i32, -7179855, -18437503, -8283652, 2875793, -16390330, -25269894, + -7014826, -23452306, 5964753, ], xy2d: [ - 4100420'i32, -5959452, -17179337, 6017714, -18705837, - 12227141, -26684835, 11344144, 2538215, -7570755 - ] + 4100420'i32, -5959452, -17179337, 6017714, -18705837, 12227141, -26684835, + 11344144, 2538215, -7570755, + ], ), GePrecomp( yplusx: [ - -9433605'i32, 6123113, 11159803, -2156608, 30016280, - 14966241, -20474983, 1485421, -629256, -15958862 + -9433605'i32, 6123113, 11159803, -2156608, 30016280, 14966241, -20474983, + 1485421, -629256, -15958862, ], yminusx: [ - -26804558'i32, 4260919, 11851389, 9658551, -32017107, - 16367492, -20205425, -13191288, 11659922, -11115118 + -26804558'i32, 4260919, 11851389, 9658551, -32017107, 16367492, -20205425, + -13191288, 11659922, -11115118, ], xy2d: [ - 26180396'i32, 10015009, -30844224, -8581293, 5418197, - 9480663, 2231568, -10170080, 33100372, -1306171 - ] + 26180396'i32, 10015009, -30844224, -8581293, 5418197, 9480663, 2231568, + -10170080, 33100372, -1306171, + ], ), GePrecomp( yplusx: [ - 15121113'i32, -5201871, -10389905, 15427821, -27509937, - -15992507, 21670947, 4486675, -5931810, -14466380 + 15121113'i32, -5201871, -10389905, 15427821, -27509937, -15992507, 21670947, + 4486675, -5931810, -14466380, ], yminusx: [ - 16166486'i32, -9483733, -11104130, 6023908, -31926798, - -1364923, 2340060, -16254968, -10735770, -10039824 + 16166486'i32, -9483733, -11104130, 6023908, -31926798, -1364923, 2340060, + -16254968, -10735770, -10039824, ], xy2d: [ - 28042865'i32, -3557089, -12126526, 12259706, -3717498, - -6945899, 6766453, -8689599, 18036436, 5803270 - ] - ) + 28042865'i32, -3557089, -12126526, 12259706, -3717498, -6945899, 6766453, + -8689599, 18036436, 5803270, + ], + ), ], [ GePrecomp( yplusx: [ - -817581'i32, 6763912, 11803561, 1585585, 10958447, - -2671165, 23855391, 4598332, -6159431, -14117438 + -817581'i32, 6763912, 11803561, 1585585, 10958447, -2671165, 23855391, + 4598332, -6159431, -14117438, ], yminusx: [ - -31031306'i32, -14256194, 17332029, -2383520, 31312682, - -5967183, 696309, 50292, -20095739, 11763584 + -31031306'i32, -14256194, 17332029, -2383520, 31312682, -5967183, 696309, + 50292, -20095739, 11763584, ], xy2d: [ - -594563'i32, -2514283, -32234153, 12643980, 12650761, - 14811489, 665117, -12613632, -19773211, -10713562 - ] + -594563'i32, -2514283, -32234153, 12643980, 12650761, 14811489, 665117, + -12613632, -19773211, -10713562, + ], ), GePrecomp( yplusx: [ - 30464590'i32, -11262872, -4127476, -12734478, 19835327, - -7105613, -24396175, 2075773, -17020157, 992471 + 30464590'i32, -11262872, -4127476, -12734478, 19835327, -7105613, -24396175, + 2075773, -17020157, 992471, ], yminusx: [ - 18357185'i32, -6994433, 7766382, 16342475, -29324918, - 411174, 14578841, 8080033, -11574335, -10601610 + 18357185'i32, -6994433, 7766382, 16342475, -29324918, 411174, 14578841, + 8080033, -11574335, -10601610, ], xy2d: [ - 19598397'i32, 10334610, 12555054, 2555664, 18821899, - -10339780, 21873263, 16014234, 26224780, 16452269 - ] + 19598397'i32, 10334610, 12555054, 2555664, 18821899, -10339780, 21873263, + 16014234, 26224780, 16452269, + ], ), GePrecomp( yplusx: [ - -30223925'i32, 5145196, 5944548, 16385966, 3976735, - 2009897, -11377804, -7618186, -20533829, 3698650 + -30223925'i32, 5145196, 5944548, 16385966, 3976735, 2009897, -11377804, + -7618186, -20533829, 3698650, ], yminusx: [ - 14187449'i32, 3448569, -10636236, -10810935, -22663880, - -3433596, 7268410, -10890444, 27394301, 12015369 + 14187449'i32, 3448569, -10636236, -10810935, -22663880, -3433596, 7268410, + -10890444, 27394301, 12015369, ], xy2d: [ - 19695761'i32, 16087646, 28032085, 12999827, 6817792, - 11427614, 20244189, -1312777, -13259127, -3402461 - ] + 19695761'i32, 16087646, 28032085, 12999827, 6817792, 11427614, 20244189, + -1312777, -13259127, -3402461, + ], ), GePrecomp( yplusx: [ - 30860103'i32, 12735208, -1888245, -4699734, -16974906, - 2256940, -8166013, 12298312, -8550524, -10393462 + 30860103'i32, 12735208, -1888245, -4699734, -16974906, 2256940, -8166013, + 12298312, -8550524, -10393462, ], yminusx: [ - -5719826'i32, -11245325, -1910649, 15569035, 26642876, - -7587760, -5789354, -15118654, -4976164, 12651793 + -5719826'i32, -11245325, -1910649, 15569035, 26642876, -7587760, -5789354, + -15118654, -4976164, 12651793, ], xy2d: [ - -2848395'i32, 9953421, 11531313, -5282879, 26895123, - -12697089, -13118820, -16517902, 9768698, -2533218 - ] + -2848395'i32, 9953421, 11531313, -5282879, 26895123, -12697089, -13118820, + -16517902, 9768698, -2533218, + ], ), GePrecomp( yplusx: [ - -24719459'i32, 1894651, -287698, -4704085, 15348719, - -8156530, 32767513, 12765450, 4940095, 10678226 + -24719459'i32, 1894651, -287698, -4704085, 15348719, -8156530, 32767513, + 12765450, 4940095, 10678226, ], yminusx: [ - 18860224'i32, 15980149, -18987240, -1562570, -26233012, - -11071856, -7843882, 13944024, -24372348, 16582019 + 18860224'i32, 15980149, -18987240, -1562570, -26233012, -11071856, -7843882, + 13944024, -24372348, 16582019, ], xy2d: [ - -15504260'i32, 4970268, -29893044, 4175593, -20993212, - -2199756, -11704054, 15444560, -11003761, 7989037 - ] + -15504260'i32, 4970268, -29893044, 4175593, -20993212, -2199756, -11704054, + 15444560, -11003761, 7989037, + ], ), GePrecomp( yplusx: [ - 31490452'i32, 5568061, -2412803, 2182383, -32336847, - 4531686, -32078269, 6200206, -19686113, -14800171 + 31490452'i32, 5568061, -2412803, 2182383, -32336847, 4531686, -32078269, + 6200206, -19686113, -14800171, ], yminusx: [ - -17308668'i32, -15879940, -31522777, -2831, -32887382, - 16375549, 8680158, -16371713, 28550068, -6857132 + -17308668'i32, -15879940, -31522777, -2831, -32887382, 16375549, 8680158, + -16371713, 28550068, -6857132, ], xy2d: [ - -28126887'i32, -5688091, 16837845, -1820458, -6850681, - 12700016, -30039981, 4364038, 1155602, 5988841 - ] + -28126887'i32, -5688091, 16837845, -1820458, -6850681, 12700016, -30039981, + 4364038, 1155602, 5988841, + ], ), GePrecomp( yplusx: [ - 21890435'i32, -13272907, -12624011, 12154349, -7831873, - 15300496, 23148983, -4470481, 24618407, 8283181 + 21890435'i32, -13272907, -12624011, 12154349, -7831873, 15300496, 23148983, + -4470481, 24618407, 8283181, ], yminusx: [ - -33136107'i32, -10512751, 9975416, 6841041, -31559793, - 16356536, 3070187, -7025928, 1466169, 10740210 + -33136107'i32, -10512751, 9975416, 6841041, -31559793, 16356536, 3070187, + -7025928, 1466169, 10740210, ], xy2d: [ - -1509399'i32, -15488185, -13503385, -10655916, 32799044, - 909394, -13938903, -5779719, -32164649, -15327040 - ] + -1509399'i32, -15488185, -13503385, -10655916, 32799044, 909394, -13938903, + -5779719, -32164649, -15327040, + ], ), GePrecomp( yplusx: [ - 3960823'i32, -14267803, -28026090, -15918051, -19404858, - 13146868, 15567327, 951507, -3260321, -573935 + 3960823'i32, -14267803, -28026090, -15918051, -19404858, 13146868, 15567327, + 951507, -3260321, -573935, ], yminusx: [ - 24740841'i32, 5052253, -30094131, 8961361, 25877428, - 6165135, -24368180, 14397372, -7380369, -6144105 + 24740841'i32, 5052253, -30094131, 8961361, 25877428, 6165135, -24368180, + 14397372, -7380369, -6144105, ], xy2d: [ - -28888365'i32, 3510803, -28103278, -1158478, -11238128, - -10631454, -15441463, -14453128, -1625486, -6494814 - ] - ) + -28888365'i32, 3510803, -28103278, -1158478, -11238128, -10631454, -15441463, + -14453128, -1625486, -6494814, + ], + ), ], [ GePrecomp( yplusx: [ - 793299'i32, -9230478, 8836302, -6235707, -27360908, - -2369593, 33152843, -4885251, -9906200, -621852 + 793299'i32, -9230478, 8836302, -6235707, -27360908, -2369593, 33152843, + -4885251, -9906200, -621852, ], yminusx: [ - 5666233'i32, 525582, 20782575, -8038419, -24538499, - 14657740, 16099374, 1468826, -6171428, -15186581 + 5666233'i32, 525582, 20782575, -8038419, -24538499, 14657740, 16099374, + 1468826, -6171428, -15186581, ], xy2d: [ - -4859255'i32, -3779343, -2917758, -6748019, 7778750, - 11688288, -30404353, -9871238, -1558923, -9863646 - ] + -4859255'i32, -3779343, -2917758, -6748019, 7778750, 11688288, -30404353, + -9871238, -1558923, -9863646, + ], ), GePrecomp( yplusx: [ - 10896332'i32, -7719704, 824275, 472601, -19460308, - 3009587, 25248958, 14783338, -30581476, -15757844 + 10896332'i32, -7719704, 824275, 472601, -19460308, 3009587, 25248958, + 14783338, -30581476, -15757844, ], yminusx: [ - 10566929'i32, 12612572, -31944212, 11118703, -12633376, - 12362879, 21752402, 8822496, 24003793, 14264025 + 10566929'i32, 12612572, -31944212, 11118703, -12633376, 12362879, 21752402, + 8822496, 24003793, 14264025, ], xy2d: [ - 27713862'i32, -7355973, -11008240, 9227530, 27050101, - 2504721, 23886875, -13117525, 13958495, -5732453 - ] + 27713862'i32, -7355973, -11008240, 9227530, 27050101, 2504721, 23886875, + -13117525, 13958495, -5732453, + ], ), GePrecomp( yplusx: [ - -23481610'i32, 4867226, -27247128, 3900521, 29838369, - -8212291, -31889399, -10041781, 7340521, -15410068 + -23481610'i32, 4867226, -27247128, 3900521, 29838369, -8212291, -31889399, + -10041781, 7340521, -15410068, ], yminusx: [ - 4646514'i32, -8011124, -22766023, -11532654, 23184553, - 8566613, 31366726, -1381061, -15066784, -10375192 + 4646514'i32, -8011124, -22766023, -11532654, 23184553, 8566613, 31366726, + -1381061, -15066784, -10375192, ], xy2d: [ - -17270517'i32, 12723032, -16993061, 14878794, 21619651, - -6197576, 27584817, 3093888, -8843694, 3849921 - ] + -17270517'i32, 12723032, -16993061, 14878794, 21619651, -6197576, 27584817, + 3093888, -8843694, 3849921, + ], ), GePrecomp( yplusx: [ - -9064912'i32, 2103172, 25561640, -15125738, -5239824, - 9582958, 32477045, -9017955, 5002294, -15550259 + -9064912'i32, 2103172, 25561640, -15125738, -5239824, 9582958, 32477045, + -9017955, 5002294, -15550259, ], yminusx: [ - -12057553'i32, -11177906, 21115585, -13365155, 8808712, - -12030708, 16489530, 13378448, -25845716, 12741426 + -12057553'i32, -11177906, 21115585, -13365155, 8808712, -12030708, 16489530, + 13378448, -25845716, 12741426, ], xy2d: [ - -5946367'i32, 10645103, -30911586, 15390284, -3286982, - -7118677, 24306472, 15852464, 28834118, -7646072 - ] + -5946367'i32, 10645103, -30911586, 15390284, -3286982, -7118677, 24306472, + 15852464, 28834118, -7646072, + ], ), GePrecomp( yplusx: [ - -17335748'i32, -9107057, -24531279, 9434953, -8472084, - -583362, -13090771, 455841, 20461858, 5491305 + -17335748'i32, -9107057, -24531279, 9434953, -8472084, -583362, -13090771, + 455841, 20461858, 5491305, ], yminusx: [ - 13669248'i32, -16095482, -12481974, -10203039, -14569770, - -11893198, -24995986, 11293807, -28588204, -9421832 + 13669248'i32, -16095482, -12481974, -10203039, -14569770, -11893198, + -24995986, 11293807, -28588204, -9421832, ], xy2d: [ - 28497928'i32, 6272777, -33022994, 14470570, 8906179, - -1225630, 18504674, -14165166, 29867745, -8795943 - ] + 28497928'i32, 6272777, -33022994, 14470570, 8906179, -1225630, 18504674, + -14165166, 29867745, -8795943, + ], ), GePrecomp( yplusx: [ - -16207023'i32, 13517196, -27799630, -13697798, 24009064, - -6373891, -6367600, -13175392, 22853429, -4012011 + -16207023'i32, 13517196, -27799630, -13697798, 24009064, -6373891, -6367600, + -13175392, 22853429, -4012011, ], yminusx: [ - 24191378'i32, 16712145, -13931797, 15217831, 14542237, - 1646131, 18603514, -11037887, 12876623, -2112447 + 24191378'i32, 16712145, -13931797, 15217831, 14542237, 1646131, 18603514, + -11037887, 12876623, -2112447, ], xy2d: [ - 17902668'i32, 4518229, -411702, -2829247, 26878217, - 5258055, -12860753, 608397, 16031844, 3723494 - ] + 17902668'i32, 4518229, -411702, -2829247, 26878217, 5258055, -12860753, + 608397, 16031844, 3723494, + ], ), GePrecomp( yplusx: [ - -28632773'i32, 12763728, -20446446, 7577504, 33001348, - -13017745, 17558842, -7872890, 23896954, -4314245 + -28632773'i32, 12763728, -20446446, 7577504, 33001348, -13017745, 17558842, + -7872890, 23896954, -4314245, ], yminusx: [ - -20005381'i32, -12011952, 31520464, 605201, 2543521, - 5991821, -2945064, 7229064, -9919646, -8826859 + -20005381'i32, -12011952, 31520464, 605201, 2543521, 5991821, -2945064, + 7229064, -9919646, -8826859, ], xy2d: [ - 28816045'i32, 298879, -28165016, -15920938, 19000928, - -1665890, -12680833, -2949325, -18051778, -2082915 - ] + 28816045'i32, 298879, -28165016, -15920938, 19000928, -1665890, -12680833, + -2949325, -18051778, -2082915, + ], ), GePrecomp( yplusx: [ - 16000882'i32, -344896, 3493092, -11447198, -29504595, - -13159789, 12577740, 16041268, -19715240, 7847707 + 16000882'i32, -344896, 3493092, -11447198, -29504595, -13159789, 12577740, + 16041268, -19715240, 7847707, ], yminusx: [ - 10151868'i32, 10572098, 27312476, 7922682, 14825339, - 4723128, -32855931, -6519018, -10020567, 3852848 + 10151868'i32, 10572098, 27312476, 7922682, 14825339, 4723128, -32855931, + -6519018, -10020567, 3852848, ], xy2d: [ - -11430470'i32, 15697596, -21121557, -4420647, 5386314, - 15063598, 16514493, -15932110, 29330899, -15076224 - ] - ) + -11430470'i32, 15697596, -21121557, -4420647, 5386314, 15063598, 16514493, + -15932110, 29330899, -15076224, + ], + ), ], [ GePrecomp( yplusx: [ - -25499735'i32, -4378794, -15222908, -6901211, 16615731, - 2051784, 3303702, 15490, -27548796, 12314391 + -25499735'i32, -4378794, -15222908, -6901211, 16615731, 2051784, 3303702, + 15490, -27548796, 12314391, ], yminusx: [ - 15683520'i32, -6003043, 18109120, -9980648, 15337968, - -5997823, -16717435, 15921866, 16103996, -3731215 + 15683520'i32, -6003043, 18109120, -9980648, 15337968, -5997823, -16717435, + 15921866, 16103996, -3731215, ], xy2d: [ - -23169824'i32, -10781249, 13588192, -1628807, -3798557, - -1074929, -19273607, 5402699, -29815713, -9841101 - ] + -23169824'i32, -10781249, 13588192, -1628807, -3798557, -1074929, -19273607, + 5402699, -29815713, -9841101, + ], ), GePrecomp( yplusx: [ - 23190676'i32, 2384583, -32714340, 3462154, -29903655, - -1529132, -11266856, 8911517, -25205859, 2739713 + 23190676'i32, 2384583, -32714340, 3462154, -29903655, -1529132, -11266856, + 8911517, -25205859, 2739713, ], yminusx: [ - 21374101'i32, -3554250, -33524649, 9874411, 15377179, - 11831242, -33529904, 6134907, 4931255, 11987849 + 21374101'i32, -3554250, -33524649, 9874411, 15377179, 11831242, -33529904, + 6134907, 4931255, 11987849, ], xy2d: [ - -7732'i32, -2978858, -16223486, 7277597, 105524, - -322051, -31480539, 13861388, -30076310, 10117930 - ] + -7732'i32, -2978858, -16223486, 7277597, 105524, -322051, -31480539, 13861388, + -30076310, 10117930, + ], ), GePrecomp( yplusx: [ - -29501170'i32, -10744872, -26163768, 13051539, -25625564, - 5089643, -6325503, 6704079, 12890019, 15728940 + -29501170'i32, -10744872, -26163768, 13051539, -25625564, 5089643, -6325503, + 6704079, 12890019, 15728940, ], yminusx: [ - -21972360'i32, -11771379, -951059, -4418840, 14704840, - 2695116, 903376, -10428139, 12885167, 8311031 + -21972360'i32, -11771379, -951059, -4418840, 14704840, 2695116, 903376, + -10428139, 12885167, 8311031, ], xy2d: [ - -17516482'i32, 5352194, 10384213, -13811658, 7506451, - 13453191, 26423267, 4384730, 1888765, -5435404 - ] + -17516482'i32, 5352194, 10384213, -13811658, 7506451, 13453191, 26423267, + 4384730, 1888765, -5435404, + ], ), GePrecomp( yplusx: [ - -25817338'i32, -3107312, -13494599, -3182506, 30896459, - -13921729, -32251644, -12707869, -19464434, -3340243 + -25817338'i32, -3107312, -13494599, -3182506, 30896459, -13921729, -32251644, + -12707869, -19464434, -3340243, ], yminusx: [ - -23607977'i32, -2665774, -526091, 4651136, 5765089, - 4618330, 6092245, 14845197, 17151279, -9854116 + -23607977'i32, -2665774, -526091, 4651136, 5765089, 4618330, 6092245, + 14845197, 17151279, -9854116, ], xy2d: [ - -24830458'i32, -12733720, -15165978, 10367250, -29530908, - -265356, 22825805, -7087279, -16866484, 16176525 - ] + -24830458'i32, -12733720, -15165978, 10367250, -29530908, -265356, 22825805, + -7087279, -16866484, 16176525, + ], ), GePrecomp( yplusx: [ - -23583256'i32, 6564961, 20063689, 3798228, -4740178, - 7359225, 2006182, -10363426, -28746253, -10197509 + -23583256'i32, 6564961, 20063689, 3798228, -4740178, 7359225, 2006182, + -10363426, -28746253, -10197509, ], yminusx: [ - -10626600'i32, -4486402, -13320562, -5125317, 3432136, - -6393229, 23632037, -1940610, 32808310, 1099883 + -10626600'i32, -4486402, -13320562, -5125317, 3432136, -6393229, 23632037, + -1940610, 32808310, 1099883, ], xy2d: [ - 15030977'i32, 5768825, -27451236, -2887299, -6427378, - -15361371, -15277896, -6809350, 2051441, -15225865 - ] + 15030977'i32, 5768825, -27451236, -2887299, -6427378, -15361371, -15277896, + -6809350, 2051441, -15225865, + ], ), GePrecomp( yplusx: [ - -3362323'i32, -7239372, 7517890, 9824992, 23555850, - 295369, 5148398, -14154188, -22686354, 16633660 + -3362323'i32, -7239372, 7517890, 9824992, 23555850, 295369, 5148398, + -14154188, -22686354, 16633660, ], yminusx: [ - 4577086'i32, -16752288, 13249841, -15304328, 19958763, - -14537274, 18559670, -10759549, 8402478, -9864273 + 4577086'i32, -16752288, 13249841, -15304328, 19958763, -14537274, 18559670, + -10759549, 8402478, -9864273, ], xy2d: [ - -28406330'i32, -1051581, -26790155, -907698, -17212414, - -11030789, 9453451, -14980072, 17983010, 9967138 - ] + -28406330'i32, -1051581, -26790155, -907698, -17212414, -11030789, 9453451, + -14980072, 17983010, 9967138, + ], ), GePrecomp( yplusx: [ - -25762494'i32, 6524722, 26585488, 9969270, 24709298, - 1220360, -1677990, 7806337, 17507396, 3651560 + -25762494'i32, 6524722, 26585488, 9969270, 24709298, 1220360, -1677990, + 7806337, 17507396, 3651560, ], yminusx: [ - -10420457'i32, -4118111, 14584639, 15971087, -15768321, - 8861010, 26556809, -5574557, -18553322, -11357135 + -10420457'i32, -4118111, 14584639, 15971087, -15768321, 8861010, 26556809, + -5574557, -18553322, -11357135, ], xy2d: [ - 2839101'i32, 14284142, 4029895, 3472686, 14402957, - 12689363, -26642121, 8459447, -5605463, -7621941 - ] + 2839101'i32, 14284142, 4029895, 3472686, 14402957, 12689363, -26642121, + 8459447, -5605463, -7621941, + ], ), GePrecomp( yplusx: [ - -4839289'i32, -3535444, 9744961, 2871048, 25113978, - 3187018, -25110813, -849066, 17258084, -7977739 + -4839289'i32, -3535444, 9744961, 2871048, 25113978, 3187018, -25110813, + -849066, 17258084, -7977739, ], yminusx: [ - 18164541'i32, -10595176, -17154882, -1542417, 19237078, - -9745295, 23357533, -15217008, 26908270, 12150756 + 18164541'i32, -10595176, -17154882, -1542417, 19237078, -9745295, 23357533, + -15217008, 26908270, 12150756, ], xy2d: [ - -30264870'i32, -7647865, 5112249, -7036672, -1499807, - -6974257, 43168, -5537701, -32302074, 16215819 - ] - ) + -30264870'i32, -7647865, 5112249, -7036672, -1499807, -6974257, 43168, + -5537701, -32302074, 16215819, + ], + ), ], [ GePrecomp( yplusx: [ - -6898905'i32, 9824394, -12304779, -4401089, -31397141, - -6276835, 32574489, 12532905, -7503072, -8675347 + -6898905'i32, 9824394, -12304779, -4401089, -31397141, -6276835, 32574489, + 12532905, -7503072, -8675347, ], yminusx: [ - -27343522'i32, -16515468, -27151524, -10722951, 946346, - 16291093, 254968, 7168080, 21676107, -1943028 + -27343522'i32, -16515468, -27151524, -10722951, 946346, 16291093, 254968, + 7168080, 21676107, -1943028, ], xy2d: [ - 21260961'i32, -8424752, -16831886, -11920822, -23677961, - 3968121, -3651949, -6215466, -3556191, -7913075 - ] + 21260961'i32, -8424752, -16831886, -11920822, -23677961, 3968121, -3651949, + -6215466, -3556191, -7913075, + ], ), GePrecomp( yplusx: [ - 16544754'i32, 13250366, -16804428, 15546242, -4583003, - 12757258, -2462308, -8680336, -18907032, -9662799 + 16544754'i32, 13250366, -16804428, 15546242, -4583003, 12757258, -2462308, + -8680336, -18907032, -9662799, ], yminusx: [ - -2415239'i32, -15577728, 18312303, 4964443, -15272530, - -12653564, 26820651, 16690659, 25459437, -4564609 + -2415239'i32, -15577728, 18312303, 4964443, -15272530, -12653564, 26820651, + 16690659, 25459437, -4564609, ], xy2d: [ - -25144690'i32, 11425020, 28423002, -11020557, -6144921, - -15826224, 9142795, -2391602, -6432418, -1644817 - ] + -25144690'i32, 11425020, 28423002, -11020557, -6144921, -15826224, 9142795, + -2391602, -6432418, -1644817, + ], ), GePrecomp( yplusx: [ - -23104652'i32, 6253476, 16964147, -3768872, -25113972, - -12296437, -27457225, -16344658, 6335692, 7249989 + -23104652'i32, 6253476, 16964147, -3768872, -25113972, -12296437, -27457225, + -16344658, 6335692, 7249989, ], yminusx: [ - -30333227'i32, 13979675, 7503222, -12368314, -11956721, - -4621693, -30272269, 2682242, 25993170, -12478523 + -30333227'i32, 13979675, 7503222, -12368314, -11956721, -4621693, -30272269, + 2682242, 25993170, -12478523, ], xy2d: [ - 4364628'i32, 5930691, 32304656, -10044554, -8054781, - 15091131, 22857016, -10598955, 31820368, 15075278 - ] + 4364628'i32, 5930691, 32304656, -10044554, -8054781, 15091131, 22857016, + -10598955, 31820368, 15075278, + ], ), GePrecomp( yplusx: [ - 31879134'i32, -8918693, 17258761, 90626, -8041836, - -4917709, 24162788, -9650886, -17970238, 12833045 + 31879134'i32, -8918693, 17258761, 90626, -8041836, -4917709, 24162788, + -9650886, -17970238, 12833045, ], yminusx: [ - 19073683'i32, 14851414, -24403169, -11860168, 7625278, - 11091125, -19619190, 2074449, -9413939, 14905377 + 19073683'i32, 14851414, -24403169, -11860168, 7625278, 11091125, -19619190, + 2074449, -9413939, 14905377, ], xy2d: [ - 24483667'i32, -11935567, -2518866, -11547418, -1553130, - 15355506, -25282080, 9253129, 27628530, -7555480 - ] + 24483667'i32, -11935567, -2518866, -11547418, -1553130, 15355506, -25282080, + 9253129, 27628530, -7555480, + ], ), GePrecomp( yplusx: [ - 17597607'i32, 8340603, 19355617, 552187, 26198470, - -3176583, 4593324, -9157582, -14110875, 15297016 + 17597607'i32, 8340603, 19355617, 552187, 26198470, -3176583, 4593324, + -9157582, -14110875, 15297016, ], yminusx: [ - 510886'i32, 14337390, -31785257, 16638632, 6328095, - 2713355, -20217417, -11864220, 8683221, 2921426 + 510886'i32, 14337390, -31785257, 16638632, 6328095, 2713355, -20217417, + -11864220, 8683221, 2921426, ], xy2d: [ - 18606791'i32, 11874196, 27155355, -5281482, -24031742, - 6265446, -25178240, -1278924, 4674690, 13890525 - ] + 18606791'i32, 11874196, 27155355, -5281482, -24031742, 6265446, -25178240, + -1278924, 4674690, 13890525, + ], ), GePrecomp( yplusx: [ - 13609624'i32, 13069022, -27372361, -13055908, 24360586, - 9592974, 14977157, 9835105, 4389687, 288396 + 13609624'i32, 13069022, -27372361, -13055908, 24360586, 9592974, 14977157, + 9835105, 4389687, 288396, ], yminusx: [ - 9922506'i32, -519394, 13613107, 5883594, -18758345, - -434263, -12304062, 8317628, 23388070, 16052080 + 9922506'i32, -519394, 13613107, 5883594, -18758345, -434263, -12304062, + 8317628, 23388070, 16052080, ], xy2d: [ - 12720016'i32, 11937594, -31970060, -5028689, 26900120, - 8561328, -20155687, -11632979, -14754271, -10812892 - ] + 12720016'i32, 11937594, -31970060, -5028689, 26900120, 8561328, -20155687, + -11632979, -14754271, -10812892, + ], ), GePrecomp( yplusx: [ - 15961858'i32, 14150409, 26716931, -665832, -22794328, - 13603569, 11829573, 7467844, -28822128, 929275 + 15961858'i32, 14150409, 26716931, -665832, -22794328, 13603569, 11829573, + 7467844, -28822128, 929275, ], yminusx: [ - 11038231'i32, -11582396, -27310482, -7316562, -10498527, - -16307831, -23479533, -9371869, -21393143, 2465074 + 11038231'i32, -11582396, -27310482, -7316562, -10498527, -16307831, -23479533, + -9371869, -21393143, 2465074, ], xy2d: [ - 20017163'i32, -4323226, 27915242, 1529148, 12396362, - 15675764, 13817261, -9658066, 2463391, -4622140 - ] + 20017163'i32, -4323226, 27915242, 1529148, 12396362, 15675764, 13817261, + -9658066, 2463391, -4622140, + ], ), GePrecomp( yplusx: [ - -16358878'i32, -12663911, -12065183, 4996454, -1256422, - 1073572, 9583558, 12851107, 4003896, 12673717 + -16358878'i32, -12663911, -12065183, 4996454, -1256422, 1073572, 9583558, + 12851107, 4003896, 12673717, ], yminusx: [ - -1731589'i32, -15155870, -3262930, 16143082, 19294135, - 13385325, 14741514, -9103726, 7903886, 2348101 + -1731589'i32, -15155870, -3262930, 16143082, 19294135, 13385325, 14741514, + -9103726, 7903886, 2348101, ], xy2d: [ - 24536016'i32, -16515207, 12715592, -3862155, 1511293, - 10047386, -3842346, -7129159, -28377538, 10048127 - ] - ) + 24536016'i32, -16515207, 12715592, -3862155, 1511293, 10047386, -3842346, + -7129159, -28377538, 10048127, + ], + ), ], [ GePrecomp( yplusx: [ - -12622226'i32, -6204820, 30718825, 2591312, -10617028, - 12192840, 18873298, -7297090, -32297756, 15221632 + -12622226'i32, -6204820, 30718825, 2591312, -10617028, 12192840, 18873298, + -7297090, -32297756, 15221632, ], yminusx: [ - -26478122'i32, -11103864, 11546244, -1852483, 9180880, - 7656409, -21343950, 2095755, 29769758, 6593415 + -26478122'i32, -11103864, 11546244, -1852483, 9180880, 7656409, -21343950, + 2095755, 29769758, 6593415, ], xy2d: [ - -31994208'i32, -2907461, 4176912, 3264766, 12538965, - -868111, 26312345, -6118678, 30958054, 8292160 - ] + -31994208'i32, -2907461, 4176912, 3264766, 12538965, -868111, 26312345, + -6118678, 30958054, 8292160, + ], ), GePrecomp( yplusx: [ - 31429822'i32, -13959116, 29173532, 15632448, 12174511, - -2760094, 32808831, 3977186, 26143136, -3148876 + 31429822'i32, -13959116, 29173532, 15632448, 12174511, -2760094, 32808831, + 3977186, 26143136, -3148876, ], yminusx: [ - 22648901'i32, 1402143, -22799984, 13746059, 7936347, - 365344, -8668633, -1674433, -3758243, -2304625 + 22648901'i32, 1402143, -22799984, 13746059, 7936347, 365344, -8668633, + -1674433, -3758243, -2304625, ], xy2d: [ - -15491917'i32, 8012313, -2514730, -12702462, -23965846, - -10254029, -1612713, -1535569, -16664475, 8194478 - ] + -15491917'i32, 8012313, -2514730, -12702462, -23965846, -10254029, -1612713, + -1535569, -16664475, 8194478, + ], ), GePrecomp( yplusx: [ - 27338066'i32, -7507420, -7414224, 10140405, -19026427, - -6589889, 27277191, 8855376, 28572286, 3005164 + 27338066'i32, -7507420, -7414224, 10140405, -19026427, -6589889, 27277191, + 8855376, 28572286, 3005164, ], yminusx: [ - 26287124'i32, 4821776, 25476601, -4145903, -3764513, - -15788984, -18008582, 1182479, -26094821, -13079595 + 26287124'i32, 4821776, 25476601, -4145903, -3764513, -15788984, -18008582, + 1182479, -26094821, -13079595, ], xy2d: [ - -7171154'i32, 3178080, 23970071, 6201893, -17195577, - -4489192, -21876275, -13982627, 32208683, -1198248 - ] + -7171154'i32, 3178080, 23970071, 6201893, -17195577, -4489192, -21876275, + -13982627, 32208683, -1198248, + ], ), GePrecomp( yplusx: [ - -16657702'i32, 2817643, -10286362, 14811298, 6024667, - 13349505, -27315504, -10497842, -27672585, -11539858 + -16657702'i32, 2817643, -10286362, 14811298, 6024667, 13349505, -27315504, + -10497842, -27672585, -11539858, ], yminusx: [ - 15941029'i32, -9405932, -21367050, 8062055, 31876073, - -238629, -15278393, -1444429, 15397331, -4130193 + 15941029'i32, -9405932, -21367050, 8062055, 31876073, -238629, -15278393, + -1444429, 15397331, -4130193, ], xy2d: [ - 8934485'i32, -13485467, -23286397, -13423241, -32446090, - 14047986, 31170398, -1441021, -27505566, 15087184 - ] + 8934485'i32, -13485467, -23286397, -13423241, -32446090, 14047986, 31170398, + -1441021, -27505566, 15087184, + ], ), GePrecomp( yplusx: [ - -18357243'i32, -2156491, 24524913, -16677868, 15520427, - -6360776, -15502406, 11461896, 16788528, -5868942 + -18357243'i32, -2156491, 24524913, -16677868, 15520427, -6360776, -15502406, + 11461896, 16788528, -5868942, ], yminusx: [ - -1947386'i32, 16013773, 21750665, 3714552, -17401782, - -16055433, -3770287, -10323320, 31322514, -11615635 + -1947386'i32, 16013773, 21750665, 3714552, -17401782, -16055433, -3770287, + -10323320, 31322514, -11615635, ], xy2d: [ - 21426655'i32, -5650218, -13648287, -5347537, -28812189, - -4920970, -18275391, -14621414, 13040862, -12112948 - ] + 21426655'i32, -5650218, -13648287, -5347537, -28812189, -4920970, -18275391, + -14621414, 13040862, -12112948, + ], ), GePrecomp( yplusx: [ - 11293895'i32, 12478086, -27136401, 15083750, -29307421, - 14748872, 14555558, -13417103, 1613711, 4896935 + 11293895'i32, 12478086, -27136401, 15083750, -29307421, 14748872, 14555558, + -13417103, 1613711, 4896935, ], yminusx: [ - -25894883'i32, 15323294, -8489791, -8057900, 25967126, - -13425460, 2825960, -4897045, -23971776, -11267415 + -25894883'i32, 15323294, -8489791, -8057900, 25967126, -13425460, 2825960, + -4897045, -23971776, -11267415, ], xy2d: [ - -15924766'i32, -5229880, -17443532, 6410664, 3622847, - 10243618, 20615400, 12405433, -23753030, -8436416 - ] + -15924766'i32, -5229880, -17443532, 6410664, 3622847, 10243618, 20615400, + 12405433, -23753030, -8436416, + ], ), GePrecomp( yplusx: [ - -7091295'i32, 12556208, -20191352, 9025187, -17072479, - 4333801, 4378436, 2432030, 23097949, -566018 + -7091295'i32, 12556208, -20191352, 9025187, -17072479, 4333801, 4378436, + 2432030, 23097949, -566018, ], yminusx: [ - 4565804'i32, -16025654, 20084412, -7842817, 1724999, - 189254, 24767264, 10103221, -18512313, 2424778 + 4565804'i32, -16025654, 20084412, -7842817, 1724999, 189254, 24767264, + 10103221, -18512313, 2424778, ], xy2d: [ - 366633'i32, -11976806, 8173090, -6890119, 30788634, - 5745705, -7168678, 1344109, -3642553, 12412659 - ] + 366633'i32, -11976806, 8173090, -6890119, 30788634, 5745705, -7168678, + 1344109, -3642553, 12412659, + ], ), GePrecomp( yplusx: [ - -24001791'i32, 7690286, 14929416, -168257, -32210835, - -13412986, 24162697, -15326504, -3141501, 11179385 + -24001791'i32, 7690286, 14929416, -168257, -32210835, -13412986, 24162697, + -15326504, -3141501, 11179385, ], yminusx: [ - 18289522'i32, -14724954, 8056945, 16430056, -21729724, - 7842514, -6001441, -1486897, -18684645, -11443503 + 18289522'i32, -14724954, 8056945, 16430056, -21729724, 7842514, -6001441, + -1486897, -18684645, -11443503, ], xy2d: [ - 476239'i32, 6601091, -6152790, -9723375, 17503545, - -4863900, 27672959, 13403813, 11052904, 5219329 - ] - ) + 476239'i32, 6601091, -6152790, -9723375, 17503545, -4863900, 27672959, + 13403813, 11052904, 5219329, + ], + ), ], [ GePrecomp( yplusx: [ - 20678546'i32, -8375738, -32671898, 8849123, -5009758, - 14574752, 31186971, -3973730, 9014762, -8579056 + 20678546'i32, -8375738, -32671898, 8849123, -5009758, 14574752, 31186971, + -3973730, 9014762, -8579056, ], yminusx: [ - -13644050'i32, -10350239, -15962508, 5075808, -1514661, - -11534600, -33102500, 9160280, 8473550, -3256838 + -13644050'i32, -10350239, -15962508, 5075808, -1514661, -11534600, -33102500, + 9160280, 8473550, -3256838, ], xy2d: [ - 24900749'i32, 14435722, 17209120, -15292541, -22592275, - 9878983, -7689309, -16335821, -24568481, 11788948 - ] + 24900749'i32, 14435722, 17209120, -15292541, -22592275, 9878983, -7689309, + -16335821, -24568481, 11788948, + ], ), GePrecomp( yplusx: [ - -3118155'i32, -11395194, -13802089, 14797441, 9652448, - -6845904, -20037437, 10410733, -24568470, -1458691 + -3118155'i32, -11395194, -13802089, 14797441, 9652448, -6845904, -20037437, + 10410733, -24568470, -1458691, ], yminusx: [ - -15659161'i32, 16736706, -22467150, 10215878, -9097177, - 7563911, 11871841, -12505194, -18513325, 8464118 + -15659161'i32, 16736706, -22467150, 10215878, -9097177, 7563911, 11871841, + -12505194, -18513325, 8464118, ], xy2d: [ - -23400612'i32, 8348507, -14585951, -861714, -3950205, - -6373419, 14325289, 8628612, 33313881, -8370517 - ] + -23400612'i32, 8348507, -14585951, -861714, -3950205, -6373419, 14325289, + 8628612, 33313881, -8370517, + ], ), GePrecomp( yplusx: [ - -20186973'i32, -4967935, 22367356, 5271547, -1097117, - -4788838, -24805667, -10236854, -8940735, -5818269 + -20186973'i32, -4967935, 22367356, 5271547, -1097117, -4788838, -24805667, + -10236854, -8940735, -5818269, ], yminusx: [ - -6948785'i32, -1795212, -32625683, -16021179, 32635414, - -7374245, 15989197, -12838188, 28358192, -4253904 + -6948785'i32, -1795212, -32625683, -16021179, 32635414, -7374245, 15989197, + -12838188, 28358192, -4253904, ], xy2d: [ - -23561781'i32, -2799059, -32351682, -1661963, -9147719, - 10429267, -16637684, 4072016, -5351664, 5596589 - ] + -23561781'i32, -2799059, -32351682, -1661963, -9147719, 10429267, -16637684, + 4072016, -5351664, 5596589, + ], ), GePrecomp( yplusx: [ - -28236598'i32, -3390048, 12312896, 6213178, 3117142, - 16078565, 29266239, 2557221, 1768301, 15373193 + -28236598'i32, -3390048, 12312896, 6213178, 3117142, 16078565, 29266239, + 2557221, 1768301, 15373193, ], yminusx: [ - -7243358'i32, -3246960, -4593467, -7553353, -127927, - -912245, -1090902, -4504991, -24660491, 3442910 + -7243358'i32, -3246960, -4593467, -7553353, -127927, -912245, -1090902, + -4504991, -24660491, 3442910, ], xy2d: [ - -30210571'i32, 5124043, 14181784, 8197961, 18964734, - -11939093, 22597931, 7176455, -18585478, 13365930 - ] + -30210571'i32, 5124043, 14181784, 8197961, 18964734, -11939093, 22597931, + 7176455, -18585478, 13365930, + ], ), GePrecomp( yplusx: [ - -7877390'i32, -1499958, 8324673, 4690079, 6261860, - 890446, 24538107, -8570186, -9689599, -3031667 + -7877390'i32, -1499958, 8324673, 4690079, 6261860, 890446, 24538107, -8570186, + -9689599, -3031667, ], yminusx: [ - 25008904'i32, -10771599, -4305031, -9638010, 16265036, - 15721635, 683793, -11823784, 15723479, -15163481 + 25008904'i32, -10771599, -4305031, -9638010, 16265036, 15721635, 683793, + -11823784, 15723479, -15163481, ], xy2d: [ - -9660625'i32, 12374379, -27006999, -7026148, -7724114, - -12314514, 11879682, 5400171, 519526, -1235876 - ] + -9660625'i32, 12374379, -27006999, -7026148, -7724114, -12314514, 11879682, + 5400171, 519526, -1235876, + ], ), GePrecomp( yplusx: [ - 22258397'i32, -16332233, -7869817, 14613016, -22520255, - -2950923, -20353881, 7315967, 16648397, 7605640 + 22258397'i32, -16332233, -7869817, 14613016, -22520255, -2950923, -20353881, + 7315967, 16648397, 7605640, ], yminusx: [ - -8081308'i32, -8464597, -8223311, 9719710, 19259459, - -15348212, 23994942, -5281555, -9468848, 4763278 + -8081308'i32, -8464597, -8223311, 9719710, 19259459, -15348212, 23994942, + -5281555, -9468848, 4763278, ], xy2d: [ - -21699244'i32, 9220969, -15730624, 1084137, -25476107, - -2852390, 31088447, -7764523, -11356529, 728112 - ] + -21699244'i32, 9220969, -15730624, 1084137, -25476107, -2852390, 31088447, + -7764523, -11356529, 728112, + ], ), GePrecomp( yplusx: [ - 26047220'i32, -11751471, -6900323, -16521798, 24092068, - 9158119, -4273545, -12555558, -29365436, -5498272 + 26047220'i32, -11751471, -6900323, -16521798, 24092068, 9158119, -4273545, + -12555558, -29365436, -5498272, ], yminusx: [ - 17510331'i32, -322857, 5854289, 8403524, 17133918, - -3112612, -28111007, 12327945, 10750447, 10014012 + 17510331'i32, -322857, 5854289, 8403524, 17133918, -3112612, -28111007, + 12327945, 10750447, 10014012, ], xy2d: [ - -10312768'i32, 3936952, 9156313, -8897683, 16498692, - -994647, -27481051, -666732, 3424691, 7540221 - ] + -10312768'i32, 3936952, 9156313, -8897683, 16498692, -994647, -27481051, + -666732, 3424691, 7540221, + ], ), GePrecomp( yplusx: [ - 30322361'i32, -6964110, 11361005, -4143317, 7433304, - 4989748, -7071422, -16317219, -9244265, 15258046 + 30322361'i32, -6964110, 11361005, -4143317, 7433304, 4989748, -7071422, + -16317219, -9244265, 15258046, ], yminusx: [ - 13054562'i32, -2779497, 19155474, 469045, -12482797, - 4566042, 5631406, 2711395, 1062915, -5136345 + 13054562'i32, -2779497, 19155474, 469045, -12482797, 4566042, 5631406, + 2711395, 1062915, -5136345, ], xy2d: [ - -19240248'i32, -11254599, -29509029, -7499965, -5835763, - 13005411, -6066489, 12194497, 32960380, 1459310 - ] - ) + -19240248'i32, -11254599, -29509029, -7499965, -5835763, 13005411, -6066489, + 12194497, 32960380, 1459310, + ], + ), ], [ GePrecomp( yplusx: [ - 19852034'i32, 7027924, 23669353, 10020366, 8586503, - -6657907, 394197, -6101885, 18638003, -11174937 + 19852034'i32, 7027924, 23669353, 10020366, 8586503, -6657907, 394197, + -6101885, 18638003, -11174937, ], yminusx: [ - 31395534'i32, 15098109, 26581030, 8030562, -16527914, - -5007134, 9012486, -7584354, -6643087, -5442636 + 31395534'i32, 15098109, 26581030, 8030562, -16527914, -5007134, 9012486, + -7584354, -6643087, -5442636, ], xy2d: [ - -9192165'i32, -2347377, -1997099, 4529534, 25766844, - 607986, -13222, 9677543, -32294889, -6456008 - ] + -9192165'i32, -2347377, -1997099, 4529534, 25766844, 607986, -13222, 9677543, + -32294889, -6456008, + ], ), GePrecomp( yplusx: [ - -2444496'i32, -149937, 29348902, 8186665, 1873760, - 12489863, -30934579, -7839692, -7852844, -8138429 + -2444496'i32, -149937, 29348902, 8186665, 1873760, 12489863, -30934579, + -7839692, -7852844, -8138429, ], yminusx: [ - -15236356'i32, -15433509, 7766470, 746860, 26346930, - -10221762, -27333451, 10754588, -9431476, 5203576 + -15236356'i32, -15433509, 7766470, 746860, 26346930, -10221762, -27333451, + 10754588, -9431476, 5203576, ], xy2d: [ - 31834314'i32, 14135496, -770007, 5159118, 20917671, - -16768096, -7467973, -7337524, 31809243, 7347066 - ] + 31834314'i32, 14135496, -770007, 5159118, 20917671, -16768096, -7467973, + -7337524, 31809243, 7347066, + ], ), GePrecomp( yplusx: [ - -9606723'i32, -11874240, 20414459, 13033986, 13716524, - -11691881, 19797970, -12211255, 15192876, -2087490 + -9606723'i32, -11874240, 20414459, 13033986, 13716524, -11691881, 19797970, + -12211255, 15192876, -2087490, ], yminusx: [ - -12663563'i32, -2181719, 1168162, -3804809, 26747877, - -14138091, 10609330, 12694420, 33473243, -13382104 + -12663563'i32, -2181719, 1168162, -3804809, 26747877, -14138091, 10609330, + 12694420, 33473243, -13382104, ], xy2d: [ - 33184999'i32, 11180355, 15832085, -11385430, -1633671, - 225884, 15089336, -11023903, -6135662, 14480053 - ] + 33184999'i32, 11180355, 15832085, -11385430, -1633671, 225884, 15089336, + -11023903, -6135662, 14480053, + ], ), GePrecomp( yplusx: [ - 31308717'i32, -5619998, 31030840, -1897099, 15674547, - -6582883, 5496208, 13685227, 27595050, 8737275 + 31308717'i32, -5619998, 31030840, -1897099, 15674547, -6582883, 5496208, + 13685227, 27595050, 8737275, ], yminusx: [ - -20318852'i32, -15150239, 10933843, -16178022, 8335352, - -7546022, -31008351, -12610604, 26498114, 66511 + -20318852'i32, -15150239, 10933843, -16178022, 8335352, -7546022, -31008351, + -12610604, 26498114, 66511, ], xy2d: [ - 22644454'i32, -8761729, -16671776, 4884562, -3105614, - -13559366, 30540766, -4286747, -13327787, -7515095 - ] + 22644454'i32, -8761729, -16671776, 4884562, -3105614, -13559366, 30540766, + -4286747, -13327787, -7515095, + ], ), GePrecomp( yplusx: [ - -28017847'i32, 9834845, 18617207, -2681312, -3401956, - -13307506, 8205540, 13585437, -17127465, 15115439 + -28017847'i32, 9834845, 18617207, -2681312, -3401956, -13307506, 8205540, + 13585437, -17127465, 15115439, ], yminusx: [ - 23711543'i32, -672915, 31206561, -8362711, 6164647, - -9709987, -33535882, -1426096, 8236921, 16492939 + 23711543'i32, -672915, 31206561, -8362711, 6164647, -9709987, -33535882, + -1426096, 8236921, 16492939, ], xy2d: [ - -23910559'i32, -13515526, -26299483, -4503841, 25005590, - -7687270, 19574902, 10071562, 6708380, -6222424 - ] + -23910559'i32, -13515526, -26299483, -4503841, 25005590, -7687270, 19574902, + 10071562, 6708380, -6222424, + ], ), GePrecomp( yplusx: [ - 2101391'i32, -4930054, 19702731, 2367575, -15427167, - 1047675, 5301017, 9328700, 29955601, -11678310 + 2101391'i32, -4930054, 19702731, 2367575, -15427167, 1047675, 5301017, + 9328700, 29955601, -11678310, ], yminusx: [ - 3096359'i32, 9271816, -21620864, -15521844, -14847996, - -7592937, -25892142, -12635595, -9917575, 6216608 + 3096359'i32, 9271816, -21620864, -15521844, -14847996, -7592937, -25892142, + -12635595, -9917575, 6216608, ], xy2d: [ - -32615849'i32, 338663, -25195611, 2510422, -29213566, - -13820213, 24822830, -6146567, -26767480, 7525079 - ] + -32615849'i32, 338663, -25195611, 2510422, -29213566, -13820213, 24822830, + -6146567, -26767480, 7525079, + ], ), GePrecomp( yplusx: [ - -23066649'i32, -13985623, 16133487, -7896178, -3389565, - 778788, -910336, -2782495, -19386633, 11994101 + -23066649'i32, -13985623, 16133487, -7896178, -3389565, 778788, -910336, + -2782495, -19386633, 11994101, ], yminusx: [ - 21691500'i32, -13624626, -641331, -14367021, 3285881, - -3483596, -25064666, 9718258, -7477437, 13381418 + 21691500'i32, -13624626, -641331, -14367021, 3285881, -3483596, -25064666, + 9718258, -7477437, 13381418, ], xy2d: [ - 18445390'i32, -4202236, 14979846, 11622458, -1727110, - -3582980, 23111648, -6375247, 28535282, 15779576 - ] + 18445390'i32, -4202236, 14979846, 11622458, -1727110, -3582980, 23111648, + -6375247, 28535282, 15779576, + ], ), GePrecomp( yplusx: [ - 30098053'i32, 3089662, -9234387, 16662135, -21306940, - 11308411, -14068454, 12021730, 9955285, -16303356 + 30098053'i32, 3089662, -9234387, 16662135, -21306940, 11308411, -14068454, + 12021730, 9955285, -16303356, ], yminusx: [ - 9734894'i32, -14576830, -7473633, -9138735, 2060392, - 11313496, -18426029, 9924399, 20194861, 13380996 + 9734894'i32, -14576830, -7473633, -9138735, 2060392, 11313496, -18426029, + 9924399, 20194861, 13380996, ], xy2d: [ - -26378102'i32, -7965207, -22167821, 15789297, -18055342, - -6168792, -1984914, 15707771, 26342023, 10146099 - ] - ) + -26378102'i32, -7965207, -22167821, 15789297, -18055342, -6168792, -1984914, + 15707771, 26342023, 10146099, + ], + ), ], [ GePrecomp( yplusx: [ - -26016874'i32, -219943, 21339191, -41388, 19745256, - -2878700, -29637280, 2227040, 21612326, -545728 + -26016874'i32, -219943, 21339191, -41388, 19745256, -2878700, -29637280, + 2227040, 21612326, -545728, ], yminusx: [ - -13077387'i32, 1184228, 23562814, -5970442, -20351244, - -6348714, 25764461, 12243797, -20856566, 11649658 + -13077387'i32, 1184228, 23562814, -5970442, -20351244, -6348714, 25764461, + 12243797, -20856566, 11649658, ], xy2d: [ - -10031494'i32, 11262626, 27384172, 2271902, 26947504, - -15997771, 39944, 6114064, 33514190, 2333242 - ] + -10031494'i32, 11262626, 27384172, 2271902, 26947504, -15997771, 39944, + 6114064, 33514190, 2333242, + ], ), GePrecomp( yplusx: [ - -21433588'i32, -12421821, 8119782, 7219913, -21830522, - -9016134, -6679750, -12670638, 24350578, -13450001 + -21433588'i32, -12421821, 8119782, 7219913, -21830522, -9016134, -6679750, + -12670638, 24350578, -13450001, ], yminusx: [ - -4116307'i32, -11271533, -23886186, 4843615, -30088339, - 690623, -31536088, -10406836, 8317860, 12352766 + -4116307'i32, -11271533, -23886186, 4843615, -30088339, 690623, -31536088, + -10406836, 8317860, 12352766, ], xy2d: [ - 18200138'i32, -14475911, -33087759, -2696619, -23702521, - -9102511, -23552096, -2287550, 20712163, 6719373 - ] + 18200138'i32, -14475911, -33087759, -2696619, -23702521, -9102511, -23552096, + -2287550, 20712163, 6719373, + ], ), GePrecomp( yplusx: [ - 26656208'i32, 6075253, -7858556, 1886072, -28344043, - 4262326, 11117530, -3763210, 26224235, -3297458 + 26656208'i32, 6075253, -7858556, 1886072, -28344043, 4262326, 11117530, + -3763210, 26224235, -3297458, ], yminusx: [ - -17168938'i32, -14854097, -3395676, -16369877, -19954045, - 14050420, 21728352, 9493610, 18620611, -16428628 + -17168938'i32, -14854097, -3395676, -16369877, -19954045, 14050420, 21728352, + 9493610, 18620611, -16428628, ], xy2d: [ - -13323321'i32, 13325349, 11432106, 5964811, 18609221, - 6062965, -5269471, -9725556, -30701573, -16479657 - ] + -13323321'i32, 13325349, 11432106, 5964811, 18609221, 6062965, -5269471, + -9725556, -30701573, -16479657, + ], ), GePrecomp( yplusx: [ - -23860538'i32, -11233159, 26961357, 1640861, -32413112, - -16737940, 12248509, -5240639, 13735342, 1934062 + -23860538'i32, -11233159, 26961357, 1640861, -32413112, -16737940, 12248509, + -5240639, 13735342, 1934062, ], yminusx: [ - 25089769'i32, 6742589, 17081145, -13406266, 21909293, - -16067981, -15136294, -3765346, -21277997, 5473616 + 25089769'i32, 6742589, 17081145, -13406266, 21909293, -16067981, -15136294, + -3765346, -21277997, 5473616, ], xy2d: [ - 31883677'i32, -7961101, 1083432, -11572403, 22828471, - 13290673, -7125085, 12469656, 29111212, -5451014 - ] + 31883677'i32, -7961101, 1083432, -11572403, 22828471, 13290673, -7125085, + 12469656, 29111212, -5451014, + ], ), GePrecomp( yplusx: [ - 24244947'i32, -15050407, -26262976, 2791540, -14997599, - 16666678, 24367466, 6388839, -10295587, 452383 + 24244947'i32, -15050407, -26262976, 2791540, -14997599, 16666678, 24367466, + 6388839, -10295587, 452383, ], yminusx: [ - -25640782'i32, -3417841, 5217916, 16224624, 19987036, - -4082269, -24236251, -5915248, 15766062, 8407814 + -25640782'i32, -3417841, 5217916, 16224624, 19987036, -4082269, -24236251, + -5915248, 15766062, 8407814, ], xy2d: [ - -20406999'i32, 13990231, 15495425, 16395525, 5377168, - 15166495, -8917023, -4388953, -8067909, 2276718 - ] + -20406999'i32, 13990231, 15495425, 16395525, 5377168, 15166495, -8917023, + -4388953, -8067909, 2276718, + ], ), GePrecomp( yplusx: [ - 30157918'i32, 12924066, -17712050, 9245753, 19895028, - 3368142, -23827587, 5096219, 22740376, -7303417 + 30157918'i32, 12924066, -17712050, 9245753, 19895028, 3368142, -23827587, + 5096219, 22740376, -7303417, ], yminusx: [ - 2041139'i32, -14256350, 7783687, 13876377, -25946985, - -13352459, 24051124, 13742383, -15637599, 13295222 + 2041139'i32, -14256350, 7783687, 13876377, -25946985, -13352459, 24051124, + 13742383, -15637599, 13295222, ], xy2d: [ - 33338237'i32, -8505733, 12532113, 7977527, 9106186, - -1715251, -17720195, -4612972, -4451357, -14669444 - ] + 33338237'i32, -8505733, 12532113, 7977527, 9106186, -1715251, -17720195, + -4612972, -4451357, -14669444, + ], ), GePrecomp( yplusx: [ - -20045281'i32, 5454097, -14346548, 6447146, 28862071, - 1883651, -2469266, -4141880, 7770569, 9620597 + -20045281'i32, 5454097, -14346548, 6447146, 28862071, 1883651, -2469266, + -4141880, 7770569, 9620597, ], yminusx: [ - 23208068'i32, 7979712, 33071466, 8149229, 1758231, - -10834995, 30945528, -1694323, -33502340, -14767970 + 23208068'i32, 7979712, 33071466, 8149229, 1758231, -10834995, 30945528, + -1694323, -33502340, -14767970, ], xy2d: [ - 1439958'i32, -16270480, -1079989, -793782, 4625402, - 10647766, -5043801, 1220118, 30494170, -11440799 - ] + 1439958'i32, -16270480, -1079989, -793782, 4625402, 10647766, -5043801, + 1220118, 30494170, -11440799, + ], ), GePrecomp( yplusx: [ - -5037580'i32, -13028295, -2970559, -3061767, 15640974, - -6701666, -26739026, 926050, -1684339, -13333647 + -5037580'i32, -13028295, -2970559, -3061767, 15640974, -6701666, -26739026, + 926050, -1684339, -13333647, ], yminusx: [ - 13908495'i32, -3549272, 30919928, -6273825, -21521863, - 7989039, 9021034, 9078865, 3353509, 4033511 + 13908495'i32, -3549272, 30919928, -6273825, -21521863, 7989039, 9021034, + 9078865, 3353509, 4033511, ], xy2d: [ - -29663431'i32, -15113610, 32259991, -344482, 24295849, - -12912123, 23161163, 8839127, 27485041, 7356032 - ] - ) + -29663431'i32, -15113610, 32259991, -344482, 24295849, -12912123, 23161163, + 8839127, 27485041, 7356032, + ], + ), ], [ GePrecomp( yplusx: [ - 9661027'i32, 705443, 11980065, -5370154, -1628543, - 14661173, -6346142, 2625015, 28431036, -16771834 + 9661027'i32, 705443, 11980065, -5370154, -1628543, 14661173, -6346142, + 2625015, 28431036, -16771834, ], yminusx: [ - -23839233'i32, -8311415, -25945511, 7480958, -17681669, - -8354183, -22545972, 14150565, 15970762, 4099461 + -23839233'i32, -8311415, -25945511, 7480958, -17681669, -8354183, -22545972, + 14150565, 15970762, 4099461, ], xy2d: [ - 29262576'i32, 16756590, 26350592, -8793563, 8529671, - -11208050, 13617293, -9937143, 11465739, 8317062 - ] + 29262576'i32, 16756590, 26350592, -8793563, 8529671, -11208050, 13617293, + -9937143, 11465739, 8317062, + ], ), GePrecomp( yplusx: [ - -25493081'i32, -6962928, 32500200, -9419051, -23038724, - -2302222, 14898637, 3848455, 20969334, -5157516 + -25493081'i32, -6962928, 32500200, -9419051, -23038724, -2302222, 14898637, + 3848455, 20969334, -5157516, ], yminusx: [ - -20384450'i32, -14347713, -18336405, 13884722, -33039454, - 2842114, -21610826, -3649888, 11177095, 14989547 + -20384450'i32, -14347713, -18336405, 13884722, -33039454, 2842114, -21610826, + -3649888, 11177095, 14989547, ], xy2d: [ - -24496721'i32, -11716016, 16959896, 2278463, 12066309, - 10137771, 13515641, 2581286, -28487508, 9930240 - ] + -24496721'i32, -11716016, 16959896, 2278463, 12066309, 10137771, 13515641, + 2581286, -28487508, 9930240, + ], ), GePrecomp( yplusx: [ - -17751622'i32, -2097826, 16544300, -13009300, -15914807, - -14949081, 18345767, -13403753, 16291481, -5314038 + -17751622'i32, -2097826, 16544300, -13009300, -15914807, -14949081, 18345767, + -13403753, 16291481, -5314038, ], yminusx: [ - -33229194'i32, 2553288, 32678213, 9875984, 8534129, - 6889387, -9676774, 6957617, 4368891, 9788741 + -33229194'i32, 2553288, 32678213, 9875984, 8534129, 6889387, -9676774, + 6957617, 4368891, 9788741, ], xy2d: [ - 16660756'i32, 7281060, -10830758, 12911820, 20108584, - -8101676, -21722536, -8613148, 16250552, -11111103 - ] + 16660756'i32, 7281060, -10830758, 12911820, 20108584, -8101676, -21722536, + -8613148, 16250552, -11111103, + ], ), GePrecomp( yplusx: [ - -19765507'i32, 2390526, -16551031, 14161980, 1905286, - 6414907, 4689584, 10604807, -30190403, 4782747 + -19765507'i32, 2390526, -16551031, 14161980, 1905286, 6414907, 4689584, + 10604807, -30190403, 4782747, ], yminusx: [ - -1354539'i32, 14736941, -7367442, -13292886, 7710542, - -14155590, -9981571, 4383045, 22546403, 437323 + -1354539'i32, 14736941, -7367442, -13292886, 7710542, -14155590, -9981571, + 4383045, 22546403, 437323, ], xy2d: [ - 31665577'i32, -12180464, -16186830, 1491339, -18368625, - 3294682, 27343084, 2786261, -30633590, -14097016 - ] + 31665577'i32, -12180464, -16186830, 1491339, -18368625, 3294682, 27343084, + 2786261, -30633590, -14097016, + ], ), GePrecomp( yplusx: [ - -14467279'i32, -683715, -33374107, 7448552, 19294360, - 14334329, -19690631, 2355319, -19284671, -6114373 + -14467279'i32, -683715, -33374107, 7448552, 19294360, 14334329, -19690631, + 2355319, -19284671, -6114373, ], yminusx: [ - 15121312'i32, -15796162, 6377020, -6031361, -10798111, - -12957845, 18952177, 15496498, -29380133, 11754228 + 15121312'i32, -15796162, 6377020, -6031361, -10798111, -12957845, 18952177, + 15496498, -29380133, 11754228, ], xy2d: [ - -2637277'i32, -13483075, 8488727, -14303896, 12728761, - -1622493, 7141596, 11724556, 22761615, -10134141 - ] + -2637277'i32, -13483075, 8488727, -14303896, 12728761, -1622493, 7141596, + 11724556, 22761615, -10134141, + ], ), GePrecomp( yplusx: [ - 16918416'i32, 11729663, -18083579, 3022987, -31015732, - -13339659, -28741185, -12227393, 32851222, 11717399 + 16918416'i32, 11729663, -18083579, 3022987, -31015732, -13339659, -28741185, + -12227393, 32851222, 11717399, ], yminusx: [ - 11166634'i32, 7338049, -6722523, 4531520, -29468672, - -7302055, 31474879, 3483633, -1193175, -4030831 + 11166634'i32, 7338049, -6722523, 4531520, -29468672, -7302055, 31474879, + 3483633, -1193175, -4030831, ], xy2d: [ - -185635'i32, 9921305, 31456609, -13536438, -12013818, - 13348923, 33142652, 6546660, -19985279, -3948376 - ] + -185635'i32, 9921305, 31456609, -13536438, -12013818, 13348923, 33142652, + 6546660, -19985279, -3948376, + ], ), GePrecomp( yplusx: [ - -32460596'i32, 11266712, -11197107, -7899103, 31703694, - 3855903, -8537131, -12833048, -30772034, -15486313 + -32460596'i32, 11266712, -11197107, -7899103, 31703694, 3855903, -8537131, + -12833048, -30772034, -15486313, ], yminusx: [ - -18006477'i32, 12709068, 3991746, -6479188, -21491523, - -10550425, -31135347, -16049879, 10928917, 3011958 + -18006477'i32, 12709068, 3991746, -6479188, -21491523, -10550425, -31135347, + -16049879, 10928917, 3011958, ], xy2d: [ - -6957757'i32, -15594337, 31696059, 334240, 29576716, - 14796075, -30831056, -12805180, 18008031, 10258577 - ] + -6957757'i32, -15594337, 31696059, 334240, 29576716, 14796075, -30831056, + -12805180, 18008031, 10258577, + ], ), GePrecomp( yplusx: [ - -22448644'i32, 15655569, 7018479, -4410003, -30314266, - -1201591, -1853465, 1367120, 25127874, 6671743 + -22448644'i32, 15655569, 7018479, -4410003, -30314266, -1201591, -1853465, + 1367120, 25127874, 6671743, ], yminusx: [ - 29701166'i32, -14373934, -10878120, 9279288, -17568, - 13127210, 21382910, 11042292, 25838796, 4642684 + 29701166'i32, -14373934, -10878120, 9279288, -17568, 13127210, 21382910, + 11042292, 25838796, 4642684, ], xy2d: [ - -20430234'i32, 14955537, -24126347, 8124619, -5369288, - -5990470, 30468147, -13900640, 18423289, 4177476 - ] - ) - ] + -20430234'i32, 14955537, -24126347, 8124619, -5369288, -5990470, 30468147, + -13900640, 18423289, 4177476, + ], + ), + ], ] BiPrecomp* = [ GePrecomp( yplusx: [ - 25967493'i32, -14356035, 29566456, 3660896, -12694345, - 4014787, 27544626, -11754271, -6079156, 2047605 + 25967493'i32, -14356035, 29566456, 3660896, -12694345, 4014787, 27544626, + -11754271, -6079156, 2047605, ], yminusx: [ - -12545711'i32, 934262, -2722910, 3049990, -727428, - 9406986, 12720692, 5043384, 19500929, -15469378 + -12545711'i32, 934262, -2722910, 3049990, -727428, 9406986, 12720692, 5043384, + 19500929, -15469378, ], xy2d: [ - -8738181'i32, 4489570, 9688441, -14785194, 10184609, - -12363380, 29287919, 11864899, -24514362, -4438546 - ] + -8738181'i32, 4489570, 9688441, -14785194, 10184609, -12363380, 29287919, + 11864899, -24514362, -4438546, + ], ), GePrecomp( yplusx: [ - 15636291'i32, -9688557, 24204773, -7912398, 616977, - -16685262, 27787600, -14772189, 28944400, -1550024 + 15636291'i32, -9688557, 24204773, -7912398, 616977, -16685262, 27787600, + -14772189, 28944400, -1550024, ], yminusx: [ - 16568933'i32, 4717097, -11556148, -1102322, 15682896, - -11807043, 16354577, -11775962, 7689662, 11199574 + 16568933'i32, 4717097, -11556148, -1102322, 15682896, -11807043, 16354577, + -11775962, 7689662, 11199574, ], xy2d: [ - 30464156'i32, -5976125, -11779434, -15670865, 23220365, - 15915852, 7512774, 10017326, -17749093, -9920357 - ] + 30464156'i32, -5976125, -11779434, -15670865, 23220365, 15915852, 7512774, + 10017326, -17749093, -9920357, + ], ), GePrecomp( yplusx: [ - 10861363'i32, 11473154, 27284546, 1981175, -30064349, - 12577861, 32867885, 14515107, -15438304, 10819380 + 10861363'i32, 11473154, 27284546, 1981175, -30064349, 12577861, 32867885, + 14515107, -15438304, 10819380, ], yminusx: [ - 4708026'i32, 6336745, 20377586, 9066809, -11272109, - 6594696, -25653668, 12483688, -12668491, 5581306 + 4708026'i32, 6336745, 20377586, 9066809, -11272109, 6594696, -25653668, + 12483688, -12668491, 5581306, ], xy2d: [ - 19563160'i32, 16186464, -29386857, 4097519, 10237984, - -4348115, 28542350, 13850243, -23678021, -15815942 - ] + 19563160'i32, 16186464, -29386857, 4097519, 10237984, -4348115, 28542350, + 13850243, -23678021, -15815942, + ], ), GePrecomp( yplusx: [ - 5153746'i32, 9909285, 1723747, -2777874, 30523605, - 5516873, 19480852, 5230134, -23952439, -15175766 + 5153746'i32, 9909285, 1723747, -2777874, 30523605, 5516873, 19480852, 5230134, + -23952439, -15175766, ], yminusx: [ - -30269007'i32, -3463509, 7665486, 10083793, 28475525, - 1649722, 20654025, 16520125, 30598449, 7715701 + -30269007'i32, -3463509, 7665486, 10083793, 28475525, 1649722, 20654025, + 16520125, 30598449, 7715701, ], xy2d: [ - 28881845'i32, 14381568, 9657904, 3680757, -20181635, - 7843316, -31400660, 1370708, 29794553, -1409300 - ] + 28881845'i32, 14381568, 9657904, 3680757, -20181635, 7843316, -31400660, + 1370708, 29794553, -1409300, + ], ), GePrecomp( yplusx: [ - -22518993'i32, -6692182, 14201702, -8745502, -23510406, - 8844726, 18474211, -1361450, -13062696, 13821877 + -22518993'i32, -6692182, 14201702, -8745502, -23510406, 8844726, 18474211, + -1361450, -13062696, 13821877, ], yminusx: [ - -6455177'i32, -7839871, 3374702, -4740862, -27098617, - -10571707, 31655028, -7212327, 18853322, -14220951 + -6455177'i32, -7839871, 3374702, -4740862, -27098617, -10571707, 31655028, + -7212327, 18853322, -14220951, ], xy2d: [ - 4566830'i32, -12963868, -28974889, -12240689, -7602672, - -2830569, -8514358, -10431137, 2207753, -3209784 - ] + 4566830'i32, -12963868, -28974889, -12240689, -7602672, -2830569, -8514358, + -10431137, 2207753, -3209784, + ], ), GePrecomp( yplusx: [ - -25154831'i32, -4185821, 29681144, 7868801, -6854661, - -9423865, -12437364, -663000, -31111463, -16132436 + -25154831'i32, -4185821, 29681144, 7868801, -6854661, -9423865, -12437364, + -663000, -31111463, -16132436, ], yminusx: [ - 25576264'i32, -2703214, 7349804, -11814844, 16472782, - 9300885, 3844789, 15725684, 171356, 6466918 + 25576264'i32, -2703214, 7349804, -11814844, 16472782, 9300885, 3844789, + 15725684, 171356, 6466918, ], xy2d: [ - 23103977'i32, 13316479, 9739013, -16149481, 817875, - -15038942, 8965339, -14088058, -30714912, 16193877 - ] + 23103977'i32, 13316479, 9739013, -16149481, 817875, -15038942, 8965339, + -14088058, -30714912, 16193877, + ], ), GePrecomp( yplusx: [ - -33521811'i32, 3180713, -2394130, 14003687, -16903474, - -16270840, 17238398, 4729455, -18074513, 9256800 + -33521811'i32, 3180713, -2394130, 14003687, -16903474, -16270840, 17238398, + 4729455, -18074513, 9256800, ], yminusx: [ - -25182317'i32, -4174131, 32336398, 5036987, -21236817, - 11360617, 22616405, 9761698, -19827198, 630305 + -25182317'i32, -4174131, 32336398, 5036987, -21236817, 11360617, 22616405, + 9761698, -19827198, 630305, ], xy2d: [ - -13720693'i32, 2639453, -24237460, -7406481, 9494427, - -5774029, -6554551, -15960994, -2449256, -14291300 - ] + -13720693'i32, 2639453, -24237460, -7406481, 9494427, -5774029, -6554551, + -15960994, -2449256, -14291300, + ], ), GePrecomp( yplusx: [ - -3151181'i32, -5046075, 9282714, 6866145, -31907062, - -863023, -18940575, 15033784, 25105118, -7894876 + -3151181'i32, -5046075, 9282714, 6866145, -31907062, -863023, -18940575, + 15033784, 25105118, -7894876, ], yminusx: [ - -24326370'i32, 15950226, -31801215, -14592823, -11662737, - -5090925, 1573892, -2625887, 2198790, -15804619 + -24326370'i32, 15950226, -31801215, -14592823, -11662737, -5090925, 1573892, + -2625887, 2198790, -15804619, ], xy2d: [ - -3099351'i32, 10324967, -2241613, 7453183, -5446979, - -2735503, -13812022, -16236442, -32461234, -12290683 - ] - ) + -3099351'i32, 10324967, -2241613, 7453183, -5446979, -2735503, -13812022, + -16236442, -32461234, -12290683, + ], + ), ] DConst* = [ - -10913610'i32, 13857413, -15372611, 6949391, 114729, -8787816, - -6275908, -3247719, -18696448, -12055116 + -10913610'i32, 13857413, -15372611, 6949391, 114729, -8787816, -6275908, -3247719, + -18696448, -12055116, ] D2Const* = [ - -21827239'i32, -5839606, -30745221, 13898782, 229458, 15978800, - -12551817, -6495438, 29715968, 9444199 + -21827239'i32, -5839606, -30745221, 13898782, 229458, 15978800, -12551817, -6495438, + 29715968, 9444199, ] SqrTm1* = [ - -32595792'i32, -7943725, 9377950, 3500415, 12389472, -272473, - -25146209, -2005654, 326686, 11406482 + -32595792'i32, -7943725, 9377950, 3500415, 12389472, -272473, -25146209, -2005654, + 326686, 11406482, ] diff --git a/libp2p/crypto/ed25519/ed25519.nim b/libp2p/crypto/ed25519/ed25519.nim index dc49d47134..7fae3cf2dd 100644 --- a/libp2p/crypto/ed25519/ed25519.nim +++ b/libp2p/crypto/ed25519/ed25519.nim @@ -28,12 +28,9 @@ export results export hash, sha2, rand const - EdPrivateKeySize* = 64 - ## Size in octets (bytes) of serialized ED25519 private key. - EdPublicKeySize* = 32 - ## Size in octets (bytes) of serialized ED25519 public key. - EdSignatureSize* = 64 - ## Size in octets (bytes) of serialized ED25519 signature. + EdPrivateKeySize* = 64 ## Size in octets (bytes) of serialized ED25519 private key. + EdPublicKeySize* = 32 ## Size in octets (bytes) of serialized ED25519 public key. + EdSignatureSize* = 64 ## Size in octets (bytes) of serialized ED25519 signature. type EdPrivateKey* = object @@ -59,18 +56,50 @@ proc `-`(x: uint8): uint8 {.inline.} = result = (0xFF'u8 - x) + 1'u8 proc fe0(h: var Fe) = - h[0] = 0; h[1] = 0; h[2] = 0; h[3] = 0; h[4] = 0 - h[5] = 0; h[6] = 0; h[7] = 0; h[8] = 0; h[9] = 0 + h[0] = 0 + h[1] = 0 + h[2] = 0 + h[3] = 0 + h[4] = 0 + h[5] = 0 + h[6] = 0 + h[7] = 0 + h[8] = 0 + h[9] = 0 proc fe1(h: var Fe) = - h[0] = 1; h[1] = 0; h[2] = 0; h[3] = 0; h[4] = 0 - h[5] = 0; h[6] = 0; h[7] = 0; h[8] = 0; h[9] = 0 + h[0] = 1 + h[1] = 0 + h[2] = 0 + h[3] = 0 + h[4] = 0 + h[5] = 0 + h[6] = 0 + h[7] = 0 + h[8] = 0 + h[9] = 0 proc feAdd(h: var Fe, f, g: Fe) = - var f0 = f[0]; var f1 = f[1]; var f2 = f[2]; var f3 = f[3]; var f4 = f[4] - var f5 = f[5]; var f6 = f[6]; var f7 = f[7]; var f8 = f[8]; var f9 = f[9] - var g0 = g[0]; var g1 = g[1]; var g2 = g[2]; var g3 = g[3]; var g4 = g[4] - var g5 = g[5]; var g6 = g[6]; var g7 = g[7]; var g8 = g[8]; var g9 = g[9] + var f0 = f[0] + var f1 = f[1] + var f2 = f[2] + var f3 = f[3] + var f4 = f[4] + var f5 = f[5] + var f6 = f[6] + var f7 = f[7] + var f8 = f[8] + var f9 = f[9] + var g0 = g[0] + var g1 = g[1] + var g2 = g[2] + var g3 = g[3] + var g4 = g[4] + var g5 = g[5] + var g6 = g[6] + var g7 = g[7] + var g8 = g[8] + var g9 = g[9] var h0 = f0 + g0 var h1 = f1 + g1 var h2 = f2 + g2 @@ -93,10 +122,26 @@ proc feAdd(h: var Fe, f, g: Fe) = h[9] = h9 proc feSub(h: var Fe, f, g: Fe) = - var f0 = f[0]; var f1 = f[1]; var f2 = f[2]; var f3 = f[3]; var f4 = f[4] - var f5 = f[5]; var f6 = f[6]; var f7 = f[7]; var f8 = f[8]; var f9 = f[9] - var g0 = g[0]; var g1 = g[1]; var g2 = g[2]; var g3 = g[3]; var g4 = g[4] - var g5 = g[5]; var g6 = g[6]; var g7 = g[7]; var g8 = g[8]; var g9 = g[9] + var f0 = f[0] + var f1 = f[1] + var f2 = f[2] + var f3 = f[3] + var f4 = f[4] + var f5 = f[5] + var f6 = f[6] + var f7 = f[7] + var f8 = f[8] + var f9 = f[9] + var g0 = g[0] + var g1 = g[1] + var g2 = g[2] + var g3 = g[3] + var g4 = g[4] + var g5 = g[5] + var g6 = g[6] + var g7 = g[7] + var g8 = g[8] + var g9 = g[9] var h0 = f0 - g0 var h1 = f1 - g1 var h2 = f2 - g2 @@ -119,10 +164,26 @@ proc feSub(h: var Fe, f, g: Fe) = h[9] = h9 proc feCmov(f: var Fe, g: Fe, b: uint32) = - var f0 = f[0]; var f1 = f[1]; var f2 = f[2]; var f3 = f[3]; var f4 = f[4] - var f5 = f[5]; var f6 = f[6]; var f7 = f[7]; var f8 = f[8]; var f9 = f[9] - var g0 = g[0]; var g1 = g[1]; var g2 = g[2]; var g3 = g[3]; var g4 = g[4] - var g5 = g[5]; var g6 = g[6]; var g7 = g[7]; var g8 = g[8]; var g9 = g[9] + var f0 = f[0] + var f1 = f[1] + var f2 = f[2] + var f3 = f[3] + var f4 = f[4] + var f5 = f[5] + var f6 = f[6] + var f7 = f[7] + var f8 = f[8] + var f9 = f[9] + var g0 = g[0] + var g1 = g[1] + var g2 = g[2] + var g3 = g[3] + var g4 = g[4] + var g5 = g[5] + var g6 = g[6] + var g7 = g[7] + var g8 = g[8] + var g9 = g[9] var x0 = f0 xor g0 var x1 = f1 xor g1 var x2 = f2 xor g2 @@ -156,8 +217,16 @@ proc feCmov(f: var Fe, g: Fe, b: uint32) = f[9] = f9 xor x9 proc feCopy(h: var Fe, f: Fe) = - var f0 = f[0]; var f1 = f[1]; var f2 = f[2]; var f3 = f[3]; var f4 = f[4] - var f5 = f[5]; var f6 = f[6]; var f7 = f[7]; var f8 = f[8]; var f9 = f[9] + var f0 = f[0] + var f1 = f[1] + var f2 = f[2] + var f3 = f[3] + var f4 = f[4] + var f5 = f[5] + var f6 = f[6] + var f7 = f[7] + var f8 = f[8] + var f9 = f[9] h[0] = f0 h[1] = f1 h[2] = f2 @@ -194,17 +263,37 @@ proc feFromBytes(h: var Fe, s: openArray[byte]) = var h8 = cast[int64](load_3(s.toOpenArray(26, 28))) shl 4 var h9 = (cast[int64](load_3(s.toOpenArray(29, 31))) and 8388607'i32) shl 2 - c9 = ashr((h9 + (1'i64 shl 24)), 25); h0 = h0 + (c9 * 19); h9 -= (c9 shl 25) - c1 = ashr((h1 + (1'i64 shl 24)), 25); h2 = h2 + c1; h1 -= (c1 shl 25) - c3 = ashr((h3 + (1'i64 shl 24)), 25); h4 = h4 + c3; h3 -= (c3 shl 25) - c5 = ashr((h5 + (1'i64 shl 24)), 25); h6 = h6 + c5; h5 -= (c5 shl 25) - c7 = ashr((h7 + (1'i64 shl 24)), 25); h8 = h8 + c7; h7 -= (c7 shl 25) - - c0 = ashr((h0 + (1'i64 shl 25)), 26); h1 = h1 + c0; h0 -= (c0 shl 26) - c2 = ashr((h2 + (1'i64 shl 25)), 26); h3 = h3 + c2; h2 -= (c2 shl 26) - c4 = ashr((h4 + (1'i64 shl 25)), 26); h5 = h5 + c4; h4 -= (c4 shl 26) - c6 = ashr((h6 + (1'i64 shl 25)), 26); h7 = h7 + c6; h6 -= (c6 shl 26) - c8 = ashr((h8 + (1'i64 shl 25)), 26); h9 = h9 + c8; h8 -= (c8 shl 26) + c9 = ashr((h9 + (1'i64 shl 24)), 25) + h0 = h0 + (c9 * 19) + h9 -= (c9 shl 25) + c1 = ashr((h1 + (1'i64 shl 24)), 25) + h2 = h2 + c1 + h1 -= (c1 shl 25) + c3 = ashr((h3 + (1'i64 shl 24)), 25) + h4 = h4 + c3 + h3 -= (c3 shl 25) + c5 = ashr((h5 + (1'i64 shl 24)), 25) + h6 = h6 + c5 + h5 -= (c5 shl 25) + c7 = ashr((h7 + (1'i64 shl 24)), 25) + h8 = h8 + c7 + h7 -= (c7 shl 25) + + c0 = ashr((h0 + (1'i64 shl 25)), 26) + h1 = h1 + c0 + h0 -= (c0 shl 26) + c2 = ashr((h2 + (1'i64 shl 25)), 26) + h3 = h3 + c2 + h2 -= (c2 shl 26) + c4 = ashr((h4 + (1'i64 shl 25)), 26) + h5 = h5 + c4 + h4 -= (c4 shl 26) + c6 = ashr((h6 + (1'i64 shl 25)), 26) + h7 = h7 + c6 + h6 -= (c6 shl 26) + c8 = ashr((h8 + (1'i64 shl 25)), 26) + h9 = h9 + c8 + h8 -= (c8 shl 26) h[0] = cast[int32](h0) h[1] = cast[int32](h1) @@ -218,8 +307,16 @@ proc feFromBytes(h: var Fe, s: openArray[byte]) = h[9] = cast[int32](h9) proc feToBytes(s: var openArray[byte], h: Fe) = - var h0 = h[0]; var h1 = h[1]; var h2 = h[2]; var h3 = h[3]; var h4 = h[4] - var h5 = h[5]; var h6 = h[6]; var h7 = h[7]; var h8 = h[8]; var h9 = h[9] + var h0 = h[0] + var h1 = h[1] + var h2 = h[2] + var h3 = h[3] + var h4 = h[4] + var h5 = h[5] + var h6 = h[6] + var h7 = h[7] + var h8 = h[8] + var h9 = h[9] var q, c0, c1, c2, c3, c4, c5, c6, c7, c8, c9: int32 q = ashr((19 * h9 + (1'i32 shl 24)), 25) @@ -236,16 +333,35 @@ proc feToBytes(s: var openArray[byte], h: Fe) = h0 = h0 + 19 * q - c0 = ashr(h0, 26); h1 += c0; h0 -= c0 shl 26; - c1 = ashr(h1, 25); h2 += c1; h1 -= c1 shl 25; - c2 = ashr(h2, 26); h3 += c2; h2 -= c2 shl 26; - c3 = ashr(h3, 25); h4 += c3; h3 -= c3 shl 25; - c4 = ashr(h4, 26); h5 += c4; h4 -= c4 shl 26; - c5 = ashr(h5, 25); h6 += c5; h5 -= c5 shl 25; - c6 = ashr(h6, 26); h7 += c6; h6 -= c6 shl 26; - c7 = ashr(h7, 25); h8 += c7; h7 -= c7 shl 25; - c8 = ashr(h8, 26); h9 += c8; h8 -= c8 shl 26; - c9 = ashr(h9, 25); h9 -= c9 shl 25; + c0 = ashr(h0, 26) + h1 += c0 + h0 -= c0 shl 26 + c1 = ashr(h1, 25) + h2 += c1 + h1 -= c1 shl 25 + c2 = ashr(h2, 26) + h3 += c2 + h2 -= c2 shl 26 + c3 = ashr(h3, 25) + h4 += c3 + h3 -= c3 shl 25 + c4 = ashr(h4, 26) + h5 += c4 + h4 -= c4 shl 26 + c5 = ashr(h5, 25) + h6 += c5 + h5 -= c5 shl 25 + c6 = ashr(h6, 26) + h7 += c6 + h6 -= c6 shl 26 + c7 = ashr(h7, 25) + h8 += c7 + h7 -= c7 shl 25 + c8 = ashr(h8, 26) + h9 += c8 + h8 -= c8 shl 26 + c9 = ashr(h9, 25) + h9 -= c9 shl 25 s[0] = cast[byte](ashr(h0, 0)) s[1] = cast[byte](ashr(h0, 8)) @@ -281,10 +397,26 @@ proc feToBytes(s: var openArray[byte], h: Fe) = s[31] = cast[byte](ashr(h9, 18)) proc feMul(h: var Fe, f, g: Fe) = - var f0 = f[0]; var f1 = f[1]; var f2 = f[2]; var f3 = f[3]; var f4 = f[4] - var f5 = f[5]; var f6 = f[6]; var f7 = f[7]; var f8 = f[8]; var f9 = f[9] - var g0 = g[0]; var g1 = g[1]; var g2 = g[2]; var g3 = g[3]; var g4 = g[4] - var g5 = g[5]; var g6 = g[6]; var g7 = g[7]; var g8 = g[8]; var g9 = g[9] + var f0 = f[0] + var f1 = f[1] + var f2 = f[2] + var f3 = f[3] + var f4 = f[4] + var f5 = f[5] + var f6 = f[6] + var f7 = f[7] + var f8 = f[8] + var f9 = f[9] + var g0 = g[0] + var g1 = g[1] + var g2 = g[2] + var g3 = g[3] + var g4 = g[4] + var g5 = g[5] + var g6 = g[6] + var g7 = g[7] + var g8 = g[8] + var g9 = g[9] var g1_19 = 19 * g1 var g2_19 = 19 * g2 var g3_19 = 19 * g3 @@ -330,11 +462,11 @@ proc feMul(h: var Fe, f, g: Fe) = 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 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 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 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) @@ -401,39 +533,67 @@ proc feMul(h: var Fe, f, g: Fe) = 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 + - f6g4_19 + f7g3_38 + f8g2_19 + f9g1_38 - h1: int64 = f0g1 + f1g0 + f2g9_19 + f3g8_19 + f4g7_19 + f5g6_19 + - f6g5_19 + f7g4_19 + f8g3_19 + f9g2_19 - h2: int64 = f0g2 + f1g1_2 + f2g0 + f3g9_38 + f4g8_19 + f5g7_38 + f6g6_19 + - f7g5_38 + f8g4_19 + f9g3_38 - h3: int64 = f0g3 + f1g2 + f2g1 + f3g0 + f4g9_19 + f5g8_19 + f6g7_19 + - f7g6_19 + f8g5_19 + f9g4_19 - h4: int64 = f0g4 + f1g3_2 + f2g2 + f3g1_2 + f4g0 + f5g9_38 + f6g8_19 + - f7g7_38 + f8g6_19 + f9g5_38 - h5: int64 = f0g5 + f1g4 + f2g3 + f3g2 + f4g1 + f5g0 + f6g9_19 + f7g8_19 + - f8g7_19 + f9g6_19 - h6: int64 = f0g6 + f1g5_2 + f2g4 + f3g3_2 + f4g2 + f5g1_2 + f6g0 + - f7g9_38 + f8g8_19 + f9g7_38 - h7: int64 = f0g7 + f1g6 + f2g5 + f3g4 + f4g3 + f5g2 + f6g1 + f7g0 + - f8g9_19 + f9g8_19 - h8: int64 = f0g8 + f1g7_2 + f2g6 + f3g5_2 + f4g4 + f5g3_2 + f6g2 + - f7g1_2 + f8g0 + f9g9_38 - h9: int64 = f0g9 + f1g8 + f2g7 + f3g6 + f4g5 + f5g4 + f6g3 + f7g2 + - f8g1 + f9g0 - - c0 = ashr((h0 + (1'i64 shl 25)), 26); h1 = h1 + c0; h0 -= (c0 shl 26) - c4 = ashr((h4 + (1'i64 shl 25)), 26); h5 = h5 + c4; h4 -= (c4 shl 26) - c1 = ashr((h1 + (1'i64 shl 24)), 25); h2 = h2 + c1; h1 -= (c1 shl 25) - c5 = ashr((h5 + (1'i64 shl 24)), 25); h6 = h6 + c5; h5 -= (c5 shl 25) - c2 = ashr((h2 + (1'i64 shl 25)), 26); h3 = h3 + c2; h2 -= (c2 shl 26) - c6 = ashr((h6 + (1'i64 shl 25)), 26); h7 = h7 + c6; h6 -= (c6 shl 26) - c3 = ashr((h3 + (1'i64 shl 24)), 25); h4 = h4 + c3; h3 -= (c3 shl 25) - c7 = ashr((h7 + (1'i64 shl 24)), 25); h8 = h8 + c7; h7 -= (c7 shl 25) - c4 = ashr((h4 + (1'i64 shl 25)), 26); h5 = h5 + c4; h4 -= (c4 shl 26) - c8 = ashr((h8 + (1'i64 shl 25)), 26); h9 = h9 + c8; h8 -= (c8 shl 26) - c9 = ashr((h9 + (1'i64 shl 24)), 25); h0 = h0 + (c9 * 19); h9 -= (c9 shl 25) - c0 = ashr((h0 + (1'i64 shl 25)), 26); h1 = h1 + c0; h0 -= (c0 shl 26) + h0: int64 = + f0g0 + f1g9_38 + f2g8_19 + f3g7_38 + f4g6_19 + f5g5_38 + f6g4_19 + f7g3_38 + + f8g2_19 + f9g1_38 + h1: int64 = + f0g1 + f1g0 + f2g9_19 + f3g8_19 + f4g7_19 + f5g6_19 + f6g5_19 + f7g4_19 + f8g3_19 + + f9g2_19 + h2: int64 = + f0g2 + f1g1_2 + f2g0 + f3g9_38 + f4g8_19 + f5g7_38 + f6g6_19 + f7g5_38 + f8g4_19 + + f9g3_38 + h3: int64 = + f0g3 + f1g2 + f2g1 + f3g0 + f4g9_19 + f5g8_19 + f6g7_19 + f7g6_19 + f8g5_19 + + f9g4_19 + h4: int64 = + f0g4 + f1g3_2 + f2g2 + f3g1_2 + f4g0 + f5g9_38 + f6g8_19 + f7g7_38 + f8g6_19 + + f9g5_38 + h5: int64 = + f0g5 + f1g4 + f2g3 + f3g2 + f4g1 + f5g0 + f6g9_19 + f7g8_19 + f8g7_19 + f9g6_19 + h6: int64 = + f0g6 + f1g5_2 + f2g4 + f3g3_2 + f4g2 + f5g1_2 + f6g0 + f7g9_38 + f8g8_19 + f9g7_38 + h7: int64 = + f0g7 + f1g6 + f2g5 + f3g4 + f4g3 + f5g2 + f6g1 + f7g0 + f8g9_19 + f9g8_19 + h8: int64 = + f0g8 + f1g7_2 + f2g6 + f3g5_2 + f4g4 + f5g3_2 + f6g2 + f7g1_2 + f8g0 + f9g9_38 + h9: int64 = f0g9 + f1g8 + f2g7 + f3g6 + f4g5 + f5g4 + f6g3 + f7g2 + f8g1 + f9g0 + + c0 = ashr((h0 + (1'i64 shl 25)), 26) + h1 = h1 + c0 + h0 -= (c0 shl 26) + c4 = ashr((h4 + (1'i64 shl 25)), 26) + h5 = h5 + c4 + h4 -= (c4 shl 26) + c1 = ashr((h1 + (1'i64 shl 24)), 25) + h2 = h2 + c1 + h1 -= (c1 shl 25) + c5 = ashr((h5 + (1'i64 shl 24)), 25) + h6 = h6 + c5 + h5 -= (c5 shl 25) + c2 = ashr((h2 + (1'i64 shl 25)), 26) + h3 = h3 + c2 + h2 -= (c2 shl 26) + c6 = ashr((h6 + (1'i64 shl 25)), 26) + h7 = h7 + c6 + h6 -= (c6 shl 26) + c3 = ashr((h3 + (1'i64 shl 24)), 25) + h4 = h4 + c3 + h3 -= (c3 shl 25) + c7 = ashr((h7 + (1'i64 shl 24)), 25) + h8 = h8 + c7 + h7 -= (c7 shl 25) + c4 = ashr((h4 + (1'i64 shl 25)), 26) + h5 = h5 + c4 + h4 -= (c4 shl 26) + c8 = ashr((h8 + (1'i64 shl 25)), 26) + h9 = h9 + c8 + h8 -= (c8 shl 26) + c9 = ashr((h9 + (1'i64 shl 24)), 25) + h0 = h0 + (c9 * 19) + h9 -= (c9 shl 25) + c0 = ashr((h0 + (1'i64 shl 25)), 26) + h1 = h1 + c0 + h0 -= (c0 shl 26) h[0] = cast[int32](h0) h[1] = cast[int32](h1) @@ -447,12 +607,36 @@ proc feMul(h: var Fe, f, g: Fe) = h[9] = cast[int32](h9) proc feNeg(h: var Fe, f: Fe) = - var f0 = f[0]; var f1 = f[1]; var f2 = f[2]; var f3 = f[3]; var f4 = f[4] - var f5 = f[5]; var f6 = f[6]; var f7 = f[7]; var f8 = f[8]; var f9 = f[9] - var h0 = -f0; var h1 = -f1; var h2 = -f2; var h3 = -f3; var h4 = -f4 - var h5 = -f5; var h6 = -f6; var h7 = -f7; var h8 = -f8; var h9 = -f9 - h[0] = h0; h[1] = h1; h[2] = h2; h[3] = h3; h[4] = h4 - h[5] = h5; h[6] = h6; h[7] = h7; h[8] = h8; h[9] = h9 + var f0 = f[0] + var f1 = f[1] + var f2 = f[2] + var f3 = f[3] + var f4 = f[4] + var f5 = f[5] + var f6 = f[6] + var f7 = f[7] + var f8 = f[8] + var f9 = f[9] + var h0 = -f0 + var h1 = -f1 + var h2 = -f2 + var h3 = -f3 + var h4 = -f4 + var h5 = -f5 + var h6 = -f6 + var h7 = -f7 + var h8 = -f8 + var h9 = -f9 + h[0] = h0 + h[1] = h1 + h[2] = h2 + h[3] = h3 + h[4] = h4 + h[5] = h5 + h[6] = h6 + h[7] = h7 + h[8] = h8 + h[9] = h9 proc verify32(x: openArray[byte], y: openArray[byte]): int32 = var d = 0'u32 @@ -501,8 +685,16 @@ proc feIsNonZero(f: Fe): int32 = result = verify32(s, ZeroFe) proc feSq(h: var Fe, f: Fe) = - var f0 = f[0]; var f1 = f[1]; var f2 = f[2]; var f3 = f[3]; var f4 = f[4]; - var f5 = f[5]; var f6 = f[6]; var f7 = f[7]; var f8 = f[8]; var f9 = f[9]; + var f0 = f[0] + var f1 = f[1] + var f2 = f[2] + var f3 = f[3] + var f4 = f[4] + var f5 = f[5] + var f6 = f[6] + var f7 = f[7] + var f8 = f[8] + var f9 = f[9] var f0_2: int32 = 2 * f0 var f1_2: int32 = 2 * f1 var f2_2: int32 = 2 * f2 @@ -583,18 +775,42 @@ proc feSq(h: var Fe, f: Fe) = var h9: int64 = f0f9_2 + f1f8_2 + f2f7_2 + f3f6_2 + f4f5_2 var c0, c1, c2, c3, c4, c5, c6, c7, c8, c9: int64 - c0 = ashr((h0 + (1'i64 shl 25)), 26); h1 += c0; h0 -= c0 shl 26 - c4 = ashr((h4 + (1'i64 shl 25)), 26); h5 += c4; h4 -= c4 shl 26 - c1 = ashr((h1 + (1'i64 shl 24)), 25); h2 += c1; h1 -= c1 shl 25 - c5 = ashr((h5 + (1'i64 shl 24)), 25); h6 += c5; h5 -= c5 shl 25 - c2 = ashr((h2 + (1'i64 shl 25)), 26); h3 += c2; h2 -= c2 shl 26 - c6 = ashr((h6 + (1'i64 shl 25)), 26); h7 += c6; h6 -= c6 shl 26 - c3 = ashr((h3 + (1'i64 shl 24)), 25); h4 += c3; h3 -= c3 shl 25 - c7 = ashr((h7 + (1'i64 shl 24)), 25); h8 += c7; h7 -= c7 shl 25 - c4 = ashr((h4 + (1'i64 shl 25)), 26); h5 += c4; h4 -= c4 shl 26 - c8 = ashr((h8 + (1'i64 shl 25)), 26); h9 += c8; h8 -= c8 shl 26 - c9 = ashr((h9 + (1'i64 shl 24)), 25); h0 += c9 * 19; h9 -= c9 shl 25 - c0 = ashr((h0 + (1'i64 shl 25)), 26); h1 += c0; h0 -= c0 shl 26 + c0 = ashr((h0 + (1'i64 shl 25)), 26) + h1 += c0 + h0 -= c0 shl 26 + c4 = ashr((h4 + (1'i64 shl 25)), 26) + h5 += c4 + h4 -= c4 shl 26 + c1 = ashr((h1 + (1'i64 shl 24)), 25) + h2 += c1 + h1 -= c1 shl 25 + c5 = ashr((h5 + (1'i64 shl 24)), 25) + h6 += c5 + h5 -= c5 shl 25 + c2 = ashr((h2 + (1'i64 shl 25)), 26) + h3 += c2 + h2 -= c2 shl 26 + c6 = ashr((h6 + (1'i64 shl 25)), 26) + h7 += c6 + h6 -= c6 shl 26 + c3 = ashr((h3 + (1'i64 shl 24)), 25) + h4 += c3 + h3 -= c3 shl 25 + c7 = ashr((h7 + (1'i64 shl 24)), 25) + h8 += c7 + h7 -= c7 shl 25 + c4 = ashr((h4 + (1'i64 shl 25)), 26) + h5 += c4 + h4 -= c4 shl 26 + c8 = ashr((h8 + (1'i64 shl 25)), 26) + h9 += c8 + h8 -= c8 shl 26 + c9 = ashr((h9 + (1'i64 shl 24)), 25) + h0 += c9 * 19 + h9 -= c9 shl 25 + c0 = ashr((h0 + (1'i64 shl 25)), 26) + h1 += c0 + h0 -= c0 shl 26 h[0] = cast[int32](h0) h[1] = cast[int32](h1) @@ -608,8 +824,16 @@ proc feSq(h: var Fe, f: Fe) = h[9] = cast[int32](h9) proc feSq2(h: var Fe, f: Fe) = - var f0 = f[0]; var f1 = f[1]; var f2 = f[2]; var f3 = f[3]; var f4 = f[4] - var f5 = f[5]; var f6 = f[6]; var f7 = f[7]; var f8 = f[8]; var f9 = f[9] + var f0 = f[0] + var f1 = f[1] + var f2 = f[2] + var f3 = f[3] + var f4 = f[4] + var f5 = f[5] + var f6 = f[6] + var f7 = f[7] + var f8 = f[8] + var f9 = f[9] var f0_2 = 2 * f0 var f1_2 = 2 * f1 var f2_2 = 2 * f2 @@ -691,21 +915,53 @@ proc feSq2(h: var Fe, f: Fe) = h8: int64 = f0f8_2 + f1f7_4 + f2f6_2 + f3f5_4 + f4f4 + f9f9_38 h9: int64 = f0f9_2 + f1f8_2 + f2f7_2 + f3f6_2 + f4f5_2 - h0 += h0; h1 += h1; h2 += h2; h3 += h3; h4 += h4; - h5 += h5; h6 += h6; h7 += h7; h8 += h8; h9 += h9; - - c0 = ashr((h0 + (1'i64 shl 25)), 26); h1 += c0; h0 -= c0 shl 26 - c4 = ashr((h4 + (1'i64 shl 25)), 26); h5 += c4; h4 -= c4 shl 26 - c1 = ashr((h1 + (1'i64 shl 24)), 25); h2 += c1; h1 -= c1 shl 25 - c5 = ashr((h5 + (1'i64 shl 24)), 25); h6 += c5; h5 -= c5 shl 25 - c2 = ashr((h2 + (1'i64 shl 25)), 26); h3 += c2; h2 -= c2 shl 26 - c6 = ashr((h6 + (1'i64 shl 25)), 26); h7 += c6; h6 -= c6 shl 26 - c3 = ashr((h3 + (1'i64 shl 24)), 25); h4 += c3; h3 -= c3 shl 25 - c7 = ashr((h7 + (1'i64 shl 24)), 25); h8 += c7; h7 -= c7 shl 25 - c4 = ashr((h4 + (1'i64 shl 25)), 26); h5 += c4; h4 -= c4 shl 26 - c8 = ashr((h8 + (1'i64 shl 25)), 26); h9 += c8; h8 -= c8 shl 26 - c9 = ashr((h9 + (1'i64 shl 24)), 25); h0 += c9 * 19; h9 -= c9 shl 25 - c0 = ashr((h0 + (1'i64 shl 25)), 26); h1 += c0; h0 -= c0 shl 26 + h0 += h0 + h1 += h1 + h2 += h2 + h3 += h3 + h4 += h4 + h5 += h5 + h6 += h6 + h7 += h7 + h8 += h8 + h9 += h9 + + c0 = ashr((h0 + (1'i64 shl 25)), 26) + h1 += c0 + h0 -= c0 shl 26 + c4 = ashr((h4 + (1'i64 shl 25)), 26) + h5 += c4 + h4 -= c4 shl 26 + c1 = ashr((h1 + (1'i64 shl 24)), 25) + h2 += c1 + h1 -= c1 shl 25 + c5 = ashr((h5 + (1'i64 shl 24)), 25) + h6 += c5 + h5 -= c5 shl 25 + c2 = ashr((h2 + (1'i64 shl 25)), 26) + h3 += c2 + h2 -= c2 shl 26 + c6 = ashr((h6 + (1'i64 shl 25)), 26) + h7 += c6 + h6 -= c6 shl 26 + c3 = ashr((h3 + (1'i64 shl 24)), 25) + h4 += c3 + h3 -= c3 shl 25 + c7 = ashr((h7 + (1'i64 shl 24)), 25) + h8 += c7 + h7 -= c7 shl 25 + c4 = ashr((h4 + (1'i64 shl 25)), 26) + h5 += c4 + h4 -= c4 shl 26 + c8 = ashr((h8 + (1'i64 shl 25)), 26) + h9 += c8 + h8 -= c8 shl 26 + c9 = ashr((h9 + (1'i64 shl 24)), 25) + h0 += c9 * 19 + h9 -= c9 shl 25 + c0 = ashr((h0 + (1'i64 shl 25)), 26) + h1 += c0 + h0 -= c0 shl 26 h[0] = cast[int32](h0) h[1] = cast[int32](h1) @@ -721,73 +977,95 @@ proc feSq2(h: var Fe, f: Fe) = proc feInvert(outfe: var Fe, z: Fe) = var t0, t1, t2, t3: Fe feSq(t0, z) - for i in 1..<1: feSq(t0, t0) + for i in 1 ..< 1: + feSq(t0, t0) feSq(t1, t0) - for i in 1..<2: feSq(t1, t1) + for i in 1 ..< 2: + feSq(t1, t1) feMul(t1, z, t1) feMul(t0, t0, t1) feSq(t2, t0) - for i in 1..<1: feSq(t2, t2) + for i in 1 ..< 1: + feSq(t2, t2) feMul(t1, t1, t2) feSq(t2, t1) - for i in 1..<5: feSq(t2, t2) + for i in 1 ..< 5: + feSq(t2, t2) feMul(t1, t2, t1) feSq(t2, t1) - for i in 1..<10: feSq(t2, t2) + for i in 1 ..< 10: + feSq(t2, t2) feMul(t2, t2, t1) feSq(t3, t2) - for i in 1..<20: feSq(t3, t3) + for i in 1 ..< 20: + feSq(t3, t3) feMul(t2, t3, t2) feSq(t2, t2) - for i in 1..<10: feSq(t2, t2) + for i in 1 ..< 10: + feSq(t2, t2) feMul(t1, t2, t1) feSq(t2, t1) - for i in 1..<50: feSq(t2, t2) + for i in 1 ..< 50: + feSq(t2, t2) feMul(t2, t2, t1) feSq(t3, t2) - for i in 1..<100: feSq(t3, t3) + for i in 1 ..< 100: + feSq(t3, t3) feMul(t2, t3, t2) feSq(t2, t2) - for i in 1..<50: feSq(t2, t2) + for i in 1 ..< 50: + feSq(t2, t2) feMul(t1, t2, t1) feSq(t1, t1) - for i in 1..<5: feSq(t1, t1) + for i in 1 ..< 5: + feSq(t1, t1) feMul(outfe, t1, t0) proc fePow22523(outfe: var Fe, z: Fe) = var t0, t1, t2: Fe feSq(t0, z) - for i in 1..<1: feSq(t0, t0) + for i in 1 ..< 1: + feSq(t0, t0) feSq(t1, t0) - for i in 1..<2: feSq(t1, t1) + for i in 1 ..< 2: + feSq(t1, t1) feMul(t1, z, t1) feMul(t0, t0, t1) feSq(t0, t0) - for i in 1..<1: feSq(t0, t0) + for i in 1 ..< 1: + feSq(t0, t0) feMul(t0, t1, t0) feSq(t1, t0) - for i in 1..<5: feSq(t1, t1) + for i in 1 ..< 5: + feSq(t1, t1) feMul(t0, t1, t0) feSq(t1, t0) - for i in 1..<10: feSq(t1, t1) + for i in 1 ..< 10: + feSq(t1, t1) feMul(t1, t1, t0) feSq(t2, t1) - for i in 1..<20: feSq(t2, t2) + for i in 1 ..< 20: + feSq(t2, t2) feMul(t1, t2, t1) feSq(t1, t1) - for i in 1..<10: feSq(t1, t1) + for i in 1 ..< 10: + feSq(t1, t1) feMul(t0, t1, t0) feSq(t1, t0) - for i in 1..<50: feSq(t1, t1) + for i in 1 ..< 50: + feSq(t1, t1) feMul(t1, t1, t0) feSq(t2, t1) - for i in 1..<100: feSq(t2, t2) + for i in 1 ..< 100: + feSq(t2, t2) feMul(t1, t2, t1) feSq(t1, t1) - for i in 1..<50: feSq(t1, t1) + for i in 1 ..< 50: + feSq(t1, t1) feMul(t0, t1, t0) feSq(t0, t0) - for i in 1..<2: feSq(t0, t0) + for i in 1 ..< 2: + feSq(t0, t0) feMul(outfe, t0, z) proc geAdd(r: var GeP1P1, p: GeP3, q: GeCached) = @@ -798,7 +1076,7 @@ proc geAdd(r: var GeP1P1, p: GeP3, q: GeCached) = feMul(r.y, r.y, q.yminusx) feMul(r.t, q.t2d, p.t) feMul(r.x, p.z, q.z) - feAdd(t0,r.x, r.x) + feAdd(t0, r.x, r.x) feSub(r.x, r.z, r.y) feAdd(r.y, r.z, r.y) feAdd(r.z, t0, r.t) @@ -831,7 +1109,7 @@ proc geFromBytesNegateVartime(h: var GeP3, s: openArray[byte]): int32 = if feIsNonZero(check) != 0: feAdd(check, vxx, u) if feIsNonZero(check) != 0: - return -1; + return -1 feMul(h.x, h.x, SqrTm1) if feIsNegative(h.x) == safeConvert[int32](s[31] shr 7): @@ -935,10 +1213,10 @@ proc geP3dbl(r: var GeP1P1, p: GeP3) = proc geP3ToBytes(s: var openArray[byte], h: GeP3) = var recip, x, y: Fe - feInvert(recip, h.z); - feMul(x, h.x, recip); - feMul(y, h.y, recip); - feToBytes(s, y); + feInvert(recip, h.z) + feMul(x, h.x, recip) + feMul(y, h.y, recip) + feToBytes(s, y) s[31] = s[31] xor cast[byte](feIsNegative(x) shl 7) proc geP3ToCached(r: var GeCached, p: GeP3) = @@ -996,12 +1274,12 @@ proc geScalarMultBase(h: var GeP3, a: openArray[byte]) = var s: GeP2 var t: GePrecomp - for i in 0..<32: + for i in 0 ..< 32: e[2 * i + 0] = cast[int8]((a[i] shr 0) and 15) e[2 * i + 1] = cast[int8]((a[i] shr 4) and 15) carry = 0 - for i in 0..<63: + for i in 0 ..< 63: e[i] += carry carry = e[i] + 8 carry = carry shr 4 @@ -1014,10 +1292,14 @@ proc geScalarMultBase(h: var GeP3, a: openArray[byte]) = geMadd(r, h, t) geP1P1toP3(h, r) - geP3dbl(r, h); geP1P1toP2(s, r) - geP2dbl(r, s); geP1P1toP2(s, r) - geP2dbl(r, s); geP1P1toP2(s, r) - geP2dbl(r, s); geP1P1toP3(h, r) + geP3dbl(r, h) + geP1P1toP2(s, r) + geP2dbl(r, s) + geP1P1toP2(s, r) + geP2dbl(r, s) + geP1P1toP2(s, r) + geP2dbl(r, s) + geP1P1toP3(h, r) for i in countup(0, 63, 2): select(t, i div 2, e[i]) @@ -1073,26 +1355,32 @@ proc scMulAdd(s: var openArray[byte], a, b, c: openArray[byte]) = s3 = c3 + a0 * b3 + a1 * b2 + a2 * b1 + a3 * b0 s4 = c4 + a0 * b4 + a1 * b3 + a2 * b2 + a3 * b1 + a4 * b0 s5 = c5 + a0 * b5 + a1 * b4 + a2 * b3 + a3 * b2 + a4 * b1 + a5 * b0 - s6 = c6 + a0 * b6 + a1 * b5 + a2 * b4 + a3 * b3 + a4 * b2 + a5 * b1 + - a6 * b0 - s7 = c7 + a0 * b7 + a1 * b6 + a2 * b5 + a3 * b4 + a4 * b3 + a5 * b2 + - a6 * b1 + a7 * b0 - s8 = c8 + a0 * b8 + a1 * b7 + a2 * b6 + a3 * b5 + a4 * b4 + a5 * b3 + - a6 * b2 + a7 * b1 + a8 * b0 - s9 = c9 + a0 * b9 + a1 * b8 + a2 * b7 + a3 * b6 + a4 * b5 + a5 * b4 + - a6 * b3 + a7 * b2 + a8 * b1 + a9 * b0 - s10 = c10 + a0 * b10 + a1 * b9 + a2 * b8 + a3 * b7 + a4 * b6 + a5 * b5 + - a6 * b4 + a7 * b3 + a8 * b2 + a9 * b1 + a10 * b0 - s11 = c11 + a0 * b11 + a1 * b10 + a2 * b9 + a3 * b8 + a4 * b7 + a5 * b6 + - a6 * b5 + a7 * b4 + a8 * b3 + a9 * b2 + a10 * b1 + a11 * b0 - s12 = a1 * b11 + a2 * b10 + a3 * b9 + a4 * b8 + a5 * b7 + a6 * b6 + a7 * b5 + - a8 * b4 + a9 * b3 + a10 * b2 + a11 * b1 - s13 = a2 * b11 + a3 * b10 + a4 * b9 + a5 * b8 + a6 * b7 + a7 * b6 + a8 * b5 + - a9 * b4 + a10 * b3 + a11 * b2 - s14 = a3 * b11 + a4 * b10 + a5 * b9 + a6 * b8 + a7 * b7 + a8 * b6 + a9 * b5 + - a10 * b4 + a11 * b3 - s15 = a4 * b11 + a5 * b10 + a6 * b9 + a7 * b8 + a8 * b7 + a9 * b6 + a10 * b5 + - a11 * b4 + s6 = c6 + a0 * b6 + a1 * b5 + a2 * b4 + a3 * b3 + a4 * b2 + a5 * b1 + a6 * b0 + s7 = + c7 + a0 * b7 + a1 * b6 + a2 * b5 + a3 * b4 + a4 * b3 + a5 * b2 + a6 * b1 + a7 * b0 + s8 = + c8 + a0 * b8 + a1 * b7 + a2 * b6 + a3 * b5 + a4 * b4 + a5 * b3 + a6 * b2 + a7 * b1 + + a8 * b0 + s9 = + c9 + a0 * b9 + a1 * b8 + a2 * b7 + a3 * b6 + a4 * b5 + a5 * b4 + a6 * b3 + a7 * b2 + + a8 * b1 + a9 * b0 + s10 = + c10 + a0 * b10 + a1 * b9 + a2 * b8 + a3 * b7 + a4 * b6 + a5 * b5 + a6 * b4 + a7 * b3 + + a8 * b2 + a9 * b1 + a10 * b0 + s11 = + c11 + a0 * b11 + a1 * b10 + a2 * b9 + a3 * b8 + a4 * b7 + a5 * b6 + a6 * b5 + a7 * b4 + + a8 * b3 + a9 * b2 + a10 * b1 + a11 * b0 + s12 = + a1 * b11 + a2 * b10 + a3 * b9 + a4 * b8 + a5 * b7 + a6 * b6 + a7 * b5 + a8 * b4 + + a9 * b3 + a10 * b2 + a11 * b1 + s13 = + a2 * b11 + a3 * b10 + a4 * b9 + a5 * b8 + a6 * b7 + a7 * b6 + a8 * b5 + a9 * b4 + + a10 * b3 + a11 * b2 + s14 = + a3 * b11 + a4 * b10 + a5 * b9 + a6 * b8 + a7 * b7 + a8 * b6 + a9 * b5 + a10 * b4 + + a11 * b3 + s15 = + a4 * b11 + a5 * b10 + a6 * b9 + a7 * b8 + a8 * b7 + a9 * b6 + a10 * b5 + a11 * b4 s16 = a5 * b11 + a6 * b10 + a7 * b9 + a8 * b8 + a9 * b7 + a10 * b6 + a11 * b5 s17 = a6 * b11 + a7 * b10 + a8 * b9 + a9 * b8 + a10 * b7 + a11 * b6 s18 = a7 * b11 + a8 * b10 + a9 * b9 + a10 * b8 + a11 * b7 @@ -1102,30 +1390,76 @@ proc scMulAdd(s: var openArray[byte], a, b, c: openArray[byte]) = s22 = a11 * b11 s23 = 0 - cr0 = ashr((s0 + (1'i64 shl 20)), 21); s1 += cr0; s0 -= cr0 shl 21 - cr2 = ashr((s2 + (1'i64 shl 20)), 21); s3 += cr2; s2 -= cr2 shl 21 - cr4 = ashr((s4 + (1'i64 shl 20)), 21); s5 += cr4; s4 -= cr4 shl 21 - cr6 = ashr((s6 + (1'i64 shl 20)), 21); s7 += cr6; s6 -= cr6 shl 21 - cr8 = ashr((s8 + (1'i64 shl 20)), 21); s9 += cr8; s8 -= cr8 shl 21 - cr10 = ashr((s10 + (1'i64 shl 20)), 21); s11 += cr10; s10 -= cr10 shl 21 - cr12 = ashr((s12 + (1'i64 shl 20)), 21); s13 += cr12; s12 -= cr12 shl 21 - cr14 = ashr((s14 + (1'i64 shl 20)), 21); s15 += cr14; s14 -= cr14 shl 21 - cr16 = ashr((s16 + (1'i64 shl 20)), 21); s17 += cr16; s16 -= cr16 shl 21 - cr18 = ashr((s18 + (1'i64 shl 20)), 21); s19 += cr18; s18 -= cr18 shl 21 - cr20 = ashr((s20 + (1'i64 shl 20)), 21); s21 += cr20; s20 -= cr20 shl 21 - cr22 = ashr((s22 + (1'i64 shl 20)), 21); s23 += cr22; s22 -= cr22 shl 21 - - cr1 = ashr((s1 + (1'i64 shl 20)), 21); s2 += cr1; s1 -= cr1 shl 21 - cr3 = ashr((s3 + (1'i64 shl 20)), 21); s4 += cr3; s3 -= cr3 shl 21 - cr5 = ashr((s5 + (1'i64 shl 20)), 21); s6 += cr5; s5 -= cr5 shl 21 - cr7 = ashr((s7 + (1'i64 shl 20)), 21); s8 += cr7; s7 -= cr7 shl 21 - cr9 = ashr((s9 + (1'i64 shl 20)), 21); s10 += cr9; s9 -= cr9 shl 21 - cr11 = ashr((s11 + (1'i64 shl 20)), 21); s12 += cr11; s11 -= cr11 shl 21 - cr13 = ashr((s13 + (1'i64 shl 20)), 21); s14 += cr13; s13 -= cr13 shl 21 - cr15 = ashr((s15 + (1'i64 shl 20)), 21); s16 += cr15; s15 -= cr15 shl 21 - cr17 = ashr((s17 + (1'i64 shl 20)), 21); s18 += cr17; s17 -= cr17 shl 21 - cr19 = ashr((s19 + (1'i64 shl 20)), 21); s20 += cr19; s19 -= cr19 shl 21 - cr21 = ashr((s21 + (1'i64 shl 20)), 21); s22 += cr21; s21 -= cr21 shl 21 + cr0 = ashr((s0 + (1'i64 shl 20)), 21) + s1 += cr0 + s0 -= cr0 shl 21 + cr2 = ashr((s2 + (1'i64 shl 20)), 21) + s3 += cr2 + s2 -= cr2 shl 21 + cr4 = ashr((s4 + (1'i64 shl 20)), 21) + s5 += cr4 + s4 -= cr4 shl 21 + cr6 = ashr((s6 + (1'i64 shl 20)), 21) + s7 += cr6 + s6 -= cr6 shl 21 + cr8 = ashr((s8 + (1'i64 shl 20)), 21) + s9 += cr8 + s8 -= cr8 shl 21 + cr10 = ashr((s10 + (1'i64 shl 20)), 21) + s11 += cr10 + s10 -= cr10 shl 21 + cr12 = ashr((s12 + (1'i64 shl 20)), 21) + s13 += cr12 + s12 -= cr12 shl 21 + cr14 = ashr((s14 + (1'i64 shl 20)), 21) + s15 += cr14 + s14 -= cr14 shl 21 + cr16 = ashr((s16 + (1'i64 shl 20)), 21) + s17 += cr16 + s16 -= cr16 shl 21 + cr18 = ashr((s18 + (1'i64 shl 20)), 21) + s19 += cr18 + s18 -= cr18 shl 21 + cr20 = ashr((s20 + (1'i64 shl 20)), 21) + s21 += cr20 + s20 -= cr20 shl 21 + cr22 = ashr((s22 + (1'i64 shl 20)), 21) + s23 += cr22 + s22 -= cr22 shl 21 + + cr1 = ashr((s1 + (1'i64 shl 20)), 21) + s2 += cr1 + s1 -= cr1 shl 21 + cr3 = ashr((s3 + (1'i64 shl 20)), 21) + s4 += cr3 + s3 -= cr3 shl 21 + cr5 = ashr((s5 + (1'i64 shl 20)), 21) + s6 += cr5 + s5 -= cr5 shl 21 + cr7 = ashr((s7 + (1'i64 shl 20)), 21) + s8 += cr7 + s7 -= cr7 shl 21 + cr9 = ashr((s9 + (1'i64 shl 20)), 21) + s10 += cr9 + s9 -= cr9 shl 21 + cr11 = ashr((s11 + (1'i64 shl 20)), 21) + s12 += cr11 + s11 -= cr11 shl 21 + cr13 = ashr((s13 + (1'i64 shl 20)), 21) + s14 += cr13 + s13 -= cr13 shl 21 + cr15 = ashr((s15 + (1'i64 shl 20)), 21) + s16 += cr15 + s15 -= cr15 shl 21 + cr17 = ashr((s17 + (1'i64 shl 20)), 21) + s18 += cr17 + s17 -= cr17 shl 21 + cr19 = ashr((s19 + (1'i64 shl 20)), 21) + s20 += cr19 + s19 -= cr19 shl 21 + cr21 = ashr((s21 + (1'i64 shl 20)), 21) + s22 += cr21 + s21 -= cr21 shl 21 s11 += s23 * 666643 s12 += s23 * 470296 @@ -1175,18 +1509,40 @@ proc scMulAdd(s: var openArray[byte], a, b, c: openArray[byte]) = s11 -= s18 * 683901 s18 = 0 - cr6 = ashr((s6 + (1'i64 shl 20)), 21); s7 += cr6; s6 -= cr6 shl 21 - cr8 = ashr((s8 + (1'i64 shl 20)), 21); s9 += cr8; s8 -= cr8 shl 21 - cr10 = ashr((s10 + (1'i64 shl 20)), 21); s11 += cr10; s10 -= cr10 shl 21 - cr12 = ashr((s12 + (1'i64 shl 20)), 21); s13 += cr12; s12 -= cr12 shl 21 - cr14 = ashr((s14 + (1'i64 shl 20)), 21); s15 += cr14; s14 -= cr14 shl 21 - cr16 = ashr((s16 + (1'i64 shl 20)), 21); s17 += cr16; s16 -= cr16 shl 21 - - cr7 = ashr((s7 + (1'i64 shl 20)), 21); s8 += cr7; s7 -= cr7 shl 21 - cr9 = ashr((s9 + (1'i64 shl 20)), 21); s10 += cr9; s9 -= cr9 shl 21 - cr11 = ashr((s11 + (1'i64 shl 20)), 21); s12 += cr11; s11 -= cr11 shl 21 - cr13 = ashr((s13 + (1'i64 shl 20)), 21); s14 += cr13; s13 -= cr13 shl 21 - cr15 = ashr((s15 + (1'i64 shl 20)), 21); s16 += cr15; s15 -= cr15 shl 21 + cr6 = ashr((s6 + (1'i64 shl 20)), 21) + s7 += cr6 + s6 -= cr6 shl 21 + cr8 = ashr((s8 + (1'i64 shl 20)), 21) + s9 += cr8 + s8 -= cr8 shl 21 + cr10 = ashr((s10 + (1'i64 shl 20)), 21) + s11 += cr10 + s10 -= cr10 shl 21 + cr12 = ashr((s12 + (1'i64 shl 20)), 21) + s13 += cr12 + s12 -= cr12 shl 21 + cr14 = ashr((s14 + (1'i64 shl 20)), 21) + s15 += cr14 + s14 -= cr14 shl 21 + cr16 = ashr((s16 + (1'i64 shl 20)), 21) + s17 += cr16 + s16 -= cr16 shl 21 + + cr7 = ashr((s7 + (1'i64 shl 20)), 21) + s8 += cr7 + s7 -= cr7 shl 21 + cr9 = ashr((s9 + (1'i64 shl 20)), 21) + s10 += cr9 + s9 -= cr9 shl 21 + cr11 = ashr((s11 + (1'i64 shl 20)), 21) + s12 += cr11 + s11 -= cr11 shl 21 + cr13 = ashr((s13 + (1'i64 shl 20)), 21) + s14 += cr13 + s13 -= cr13 shl 21 + cr15 = ashr((s15 + (1'i64 shl 20)), 21) + s16 += cr15 + s15 -= cr15 shl 21 s5 += s17 * 666643 s6 += s17 * 470296 @@ -1236,19 +1592,43 @@ proc scMulAdd(s: var openArray[byte], a, b, c: openArray[byte]) = s5 -= s12 * 683901 s12 = 0 - cr0 = ashr((s0 + (1'i64 shl 20)), 21); s1 += cr0; s0 -= cr0 shl 21 - cr2 = ashr((s2 + (1'i64 shl 20)), 21); s3 += cr2; s2 -= cr2 shl 21 - cr4 = ashr((s4 + (1'i64 shl 20)), 21); s5 += cr4; s4 -= cr4 shl 21 - cr6 = ashr((s6 + (1'i64 shl 20)), 21); s7 += cr6; s6 -= cr6 shl 21 - cr8 = ashr((s8 + (1'i64 shl 20)), 21); s9 += cr8; s8 -= cr8 shl 21 - cr10 = ashr((s10 + (1'i64 shl 20)), 21); s11 += cr10; s10 -= cr10 shl 21 - - cr1 = ashr((s1 + (1'i64 shl 20)), 21); s2 += cr1; s1 -= cr1 shl 21 - cr3 = ashr((s3 + (1'i64 shl 20)), 21); s4 += cr3; s3 -= cr3 shl 21 - cr5 = ashr((s5 + (1'i64 shl 20)), 21); s6 += cr5; s5 -= cr5 shl 21 - cr7 = ashr((s7 + (1'i64 shl 20)), 21); s8 += cr7; s7 -= cr7 shl 21 - cr9 = ashr((s9 + (1'i64 shl 20)), 21); s10 += cr9; s9 -= cr9 shl 21 - cr11 = ashr((s11 + (1'i64 shl 20)), 21); s12 += cr11; s11 -= cr11 shl 21 + cr0 = ashr((s0 + (1'i64 shl 20)), 21) + s1 += cr0 + s0 -= cr0 shl 21 + cr2 = ashr((s2 + (1'i64 shl 20)), 21) + s3 += cr2 + s2 -= cr2 shl 21 + cr4 = ashr((s4 + (1'i64 shl 20)), 21) + s5 += cr4 + s4 -= cr4 shl 21 + cr6 = ashr((s6 + (1'i64 shl 20)), 21) + s7 += cr6 + s6 -= cr6 shl 21 + cr8 = ashr((s8 + (1'i64 shl 20)), 21) + s9 += cr8 + s8 -= cr8 shl 21 + cr10 = ashr((s10 + (1'i64 shl 20)), 21) + s11 += cr10 + s10 -= cr10 shl 21 + + cr1 = ashr((s1 + (1'i64 shl 20)), 21) + s2 += cr1 + s1 -= cr1 shl 21 + cr3 = ashr((s3 + (1'i64 shl 20)), 21) + s4 += cr3 + s3 -= cr3 shl 21 + cr5 = ashr((s5 + (1'i64 shl 20)), 21) + s6 += cr5 + s5 -= cr5 shl 21 + cr7 = ashr((s7 + (1'i64 shl 20)), 21) + s8 += cr7 + s7 -= cr7 shl 21 + cr9 = ashr((s9 + (1'i64 shl 20)), 21) + s10 += cr9 + s9 -= cr9 shl 21 + cr11 = ashr((s11 + (1'i64 shl 20)), 21) + s12 += cr11 + s11 -= cr11 shl 21 s0 += s12 * 666643 s1 += s12 * 470296 @@ -1258,18 +1638,42 @@ proc scMulAdd(s: var openArray[byte], a, b, c: openArray[byte]) = s5 -= s12 * 683901 s12 = 0 - cr0 = ashr(s0, 21); s1 += cr0; s0 -= cr0 shl 21 - cr1 = ashr(s1, 21); s2 += cr1; s1 -= cr1 shl 21 - cr2 = ashr(s2, 21); s3 += cr2; s2 -= cr2 shl 21 - cr3 = ashr(s3, 21); s4 += cr3; s3 -= cr3 shl 21 - cr4 = ashr(s4, 21); s5 += cr4; s4 -= cr4 shl 21 - cr5 = ashr(s5, 21); s6 += cr5; s5 -= cr5 shl 21 - cr6 = ashr(s6, 21); s7 += cr6; s6 -= cr6 shl 21 - cr7 = ashr(s7, 21); s8 += cr7; s7 -= cr7 shl 21 - cr8 = ashr(s8, 21); s9 += cr8; s8 -= cr8 shl 21 - cr9 = ashr(s9, 21); s10 += cr9; s9 -= cr9 shl 21 - cr10 = ashr(s10, 21); s11 += cr10; s10 -= cr10 shl 21 - cr11 = ashr(s11, 21); s12 += cr11; s11 -= cr11 shl 21 + cr0 = ashr(s0, 21) + s1 += cr0 + s0 -= cr0 shl 21 + cr1 = ashr(s1, 21) + s2 += cr1 + s1 -= cr1 shl 21 + cr2 = ashr(s2, 21) + s3 += cr2 + s2 -= cr2 shl 21 + cr3 = ashr(s3, 21) + s4 += cr3 + s3 -= cr3 shl 21 + cr4 = ashr(s4, 21) + s5 += cr4 + s4 -= cr4 shl 21 + cr5 = ashr(s5, 21) + s6 += cr5 + s5 -= cr5 shl 21 + cr6 = ashr(s6, 21) + s7 += cr6 + s6 -= cr6 shl 21 + cr7 = ashr(s7, 21) + s8 += cr7 + s7 -= cr7 shl 21 + cr8 = ashr(s8, 21) + s9 += cr8 + s8 -= cr8 shl 21 + cr9 = ashr(s9, 21) + s10 += cr9 + s9 -= cr9 shl 21 + cr10 = ashr(s10, 21) + s11 += cr10 + s10 -= cr10 shl 21 + cr11 = ashr(s11, 21) + s12 += cr11 + s11 -= cr11 shl 21 s0 += s12 * 666643 s1 += s12 * 470296 @@ -1279,17 +1683,39 @@ proc scMulAdd(s: var openArray[byte], a, b, c: openArray[byte]) = s5 -= s12 * 683901 s12 = 0 - cr0 = ashr(s0, 21); s1 += cr0; s0 -= cr0 shl 21 - cr1 = ashr(s1, 21); s2 += cr1; s1 -= cr1 shl 21 - cr2 = ashr(s2, 21); s3 += cr2; s2 -= cr2 shl 21 - cr3 = ashr(s3, 21); s4 += cr3; s3 -= cr3 shl 21 - cr4 = ashr(s4, 21); s5 += cr4; s4 -= cr4 shl 21 - cr5 = ashr(s5, 21); s6 += cr5; s5 -= cr5 shl 21 - cr6 = ashr(s6, 21); s7 += cr6; s6 -= cr6 shl 21 - cr7 = ashr(s7, 21); s8 += cr7; s7 -= cr7 shl 21 - cr8 = ashr(s8, 21); s9 += cr8; s8 -= cr8 shl 21 - cr9 = ashr(s9, 21); s10 += cr9; s9 -= cr9 shl 21 - cr10 = ashr(s10, 21); s11 += cr10; s10 -= cr10 shl 21 + cr0 = ashr(s0, 21) + s1 += cr0 + s0 -= cr0 shl 21 + cr1 = ashr(s1, 21) + s2 += cr1 + s1 -= cr1 shl 21 + cr2 = ashr(s2, 21) + s3 += cr2 + s2 -= cr2 shl 21 + cr3 = ashr(s3, 21) + s4 += cr3 + s3 -= cr3 shl 21 + cr4 = ashr(s4, 21) + s5 += cr4 + s4 -= cr4 shl 21 + cr5 = ashr(s5, 21) + s6 += cr5 + s5 -= cr5 shl 21 + cr6 = ashr(s6, 21) + s7 += cr6 + s6 -= cr6 shl 21 + cr7 = ashr(s7, 21) + s8 += cr7 + s7 -= cr7 shl 21 + cr8 = ashr(s8, 21) + s9 += cr8 + s8 -= cr8 shl 21 + cr9 = ashr(s9, 21) + s10 += cr9 + s9 -= cr9 shl 21 + cr10 = ashr(s10, 21) + s11 += cr10 + s10 -= cr10 shl 21 s[0] = cast[uint8](ashr(s0, 0)) s[1] = cast[uint8](ashr(s0, 8)) @@ -1325,7 +1751,7 @@ proc scMulAdd(s: var openArray[byte], a, b, c: openArray[byte]) = s[31] = cast[uint8](ashr(s11, 17)) proc scReduce(s: var openArray[byte]) = - var s0 = 2097151'i64 and cast[int64](load_3(s.toOpenArray(0, 2))); + var s0 = 2097151'i64 and cast[int64](load_3(s.toOpenArray(0, 2))) var s1 = 2097151'i64 and cast[int64](load_4(s.toOpenArray(2, 5)) shr 5) var s2 = 2097151'i64 and cast[int64](load_3(s.toOpenArray(5, 7)) shr 2) var s3 = 2097151'i64 and cast[int64](load_4(s.toOpenArray(7, 10)) shr 7) @@ -1401,18 +1827,40 @@ proc scReduce(s: var openArray[byte]) = s11 -= s18 * 683901 s18 = 0 - cr6 = ashr((s6 + (1'i64 shl 20)), 21); s7 += cr6; s6 -= cr6 shl 21 - cr8 = ashr((s8 + (1'i64 shl 20)), 21); s9 += cr8; s8 -= cr8 shl 21 - cr10 = ashr((s10 + (1'i64 shl 20)), 21); s11 += cr10; s10 -= cr10 shl 21 - cr12 = ashr((s12 + (1'i64 shl 20)), 21); s13 += cr12; s12 -= cr12 shl 21 - cr14 = ashr((s14 + (1'i64 shl 20)), 21); s15 += cr14; s14 -= cr14 shl 21 - cr16 = ashr((s16 + (1'i64 shl 20)), 21); s17 += cr16; s16 -= cr16 shl 21 - - cr7 = ashr((s7 + (1'i64 shl 20)), 21); s8 += cr7; s7 -= cr7 shl 21 - cr9 = ashr((s9 + (1'i64 shl 20)), 21); s10 += cr9; s9 -= cr9 shl 21 - cr11 = ashr((s11 + (1'i64 shl 20)), 21); s12 += cr11; s11 -= cr11 shl 21 - cr13 = ashr((s13 + (1'i64 shl 20)), 21); s14 += cr13; s13 -= cr13 shl 21 - cr15 = ashr((s15 + (1'i64 shl 20)), 21); s16 += cr15; s15 -= cr15 shl 21 + cr6 = ashr((s6 + (1'i64 shl 20)), 21) + s7 += cr6 + s6 -= cr6 shl 21 + cr8 = ashr((s8 + (1'i64 shl 20)), 21) + s9 += cr8 + s8 -= cr8 shl 21 + cr10 = ashr((s10 + (1'i64 shl 20)), 21) + s11 += cr10 + s10 -= cr10 shl 21 + cr12 = ashr((s12 + (1'i64 shl 20)), 21) + s13 += cr12 + s12 -= cr12 shl 21 + cr14 = ashr((s14 + (1'i64 shl 20)), 21) + s15 += cr14 + s14 -= cr14 shl 21 + cr16 = ashr((s16 + (1'i64 shl 20)), 21) + s17 += cr16 + s16 -= cr16 shl 21 + + cr7 = ashr((s7 + (1'i64 shl 20)), 21) + s8 += cr7 + s7 -= cr7 shl 21 + cr9 = ashr((s9 + (1'i64 shl 20)), 21) + s10 += cr9 + s9 -= cr9 shl 21 + cr11 = ashr((s11 + (1'i64 shl 20)), 21) + s12 += cr11 + s11 -= cr11 shl 21 + cr13 = ashr((s13 + (1'i64 shl 20)), 21) + s14 += cr13 + s13 -= cr13 shl 21 + cr15 = ashr((s15 + (1'i64 shl 20)), 21) + s16 += cr15 + s15 -= cr15 shl 21 s5 += s17 * 666643 s6 += s17 * 470296 @@ -1462,19 +1910,43 @@ proc scReduce(s: var openArray[byte]) = s5 -= s12 * 683901 s12 = 0 - cr0 = ashr((s0 + (1'i64 shl 20)), 21); s1 += cr0; s0 -= cr0 shl 21 - cr2 = ashr((s2 + (1'i64 shl 20)), 21); s3 += cr2; s2 -= cr2 shl 21 - cr4 = ashr((s4 + (1'i64 shl 20)), 21); s5 += cr4; s4 -= cr4 shl 21 - cr6 = ashr((s6 + (1'i64 shl 20)), 21); s7 += cr6; s6 -= cr6 shl 21 - cr8 = ashr((s8 + (1'i64 shl 20)), 21); s9 += cr8; s8 -= cr8 shl 21 - cr10 = ashr((s10 + (1'i64 shl 20)), 21); s11 += cr10; s10 -= cr10 shl 21 - - cr1 = ashr((s1 + (1'i64 shl 20)), 21); s2 += cr1; s1 -= cr1 shl 21 - cr3 = ashr((s3 + (1'i64 shl 20)), 21); s4 += cr3; s3 -= cr3 shl 21 - cr5 = ashr((s5 + (1'i64 shl 20)), 21); s6 += cr5; s5 -= cr5 shl 21 - cr7 = ashr((s7 + (1'i64 shl 20)), 21); s8 += cr7; s7 -= cr7 shl 21 - cr9 = ashr((s9 + (1'i64 shl 20)), 21); s10 += cr9; s9 -= cr9 shl 21 - cr11 = ashr((s11 + (1'i64 shl 20)), 21); s12 += cr11; s11 -= cr11 shl 21 + cr0 = ashr((s0 + (1'i64 shl 20)), 21) + s1 += cr0 + s0 -= cr0 shl 21 + cr2 = ashr((s2 + (1'i64 shl 20)), 21) + s3 += cr2 + s2 -= cr2 shl 21 + cr4 = ashr((s4 + (1'i64 shl 20)), 21) + s5 += cr4 + s4 -= cr4 shl 21 + cr6 = ashr((s6 + (1'i64 shl 20)), 21) + s7 += cr6 + s6 -= cr6 shl 21 + cr8 = ashr((s8 + (1'i64 shl 20)), 21) + s9 += cr8 + s8 -= cr8 shl 21 + cr10 = ashr((s10 + (1'i64 shl 20)), 21) + s11 += cr10 + s10 -= cr10 shl 21 + + cr1 = ashr((s1 + (1'i64 shl 20)), 21) + s2 += cr1 + s1 -= cr1 shl 21 + cr3 = ashr((s3 + (1'i64 shl 20)), 21) + s4 += cr3 + s3 -= cr3 shl 21 + cr5 = ashr((s5 + (1'i64 shl 20)), 21) + s6 += cr5 + s5 -= cr5 shl 21 + cr7 = ashr((s7 + (1'i64 shl 20)), 21) + s8 += cr7 + s7 -= cr7 shl 21 + cr9 = ashr((s9 + (1'i64 shl 20)), 21) + s10 += cr9 + s9 -= cr9 shl 21 + cr11 = ashr((s11 + (1'i64 shl 20)), 21) + s12 += cr11 + s11 -= cr11 shl 21 s0 += s12 * 666643 s1 += s12 * 470296 @@ -1482,20 +1954,44 @@ proc scReduce(s: var openArray[byte]) = s3 -= s12 * 997805 s4 += s12 * 136657 s5 -= s12 * 683901 - s12 = 0; - - cr0 = ashr(s0, 21); s1 += cr0; s0 -= cr0 shl 21 - cr1 = ashr(s1, 21); s2 += cr1; s1 -= cr1 shl 21 - cr2 = ashr(s2, 21); s3 += cr2; s2 -= cr2 shl 21 - cr3 = ashr(s3, 21); s4 += cr3; s3 -= cr3 shl 21 - cr4 = ashr(s4, 21); s5 += cr4; s4 -= cr4 shl 21 - cr5 = ashr(s5, 21); s6 += cr5; s5 -= cr5 shl 21 - cr6 = ashr(s6, 21); s7 += cr6; s6 -= cr6 shl 21 - cr7 = ashr(s7, 21); s8 += cr7; s7 -= cr7 shl 21 - cr8 = ashr(s8, 21); s9 += cr8; s8 -= cr8 shl 21 - cr9 = ashr(s9, 21); s10 += cr9; s9 -= cr9 shl 21 - cr10 = ashr(s10, 21); s11 += cr10; s10 -= cr10 shl 21 - cr11 = ashr(s11, 21); s12 += cr11; s11 -= cr11 shl 21 + s12 = 0 + + cr0 = ashr(s0, 21) + s1 += cr0 + s0 -= cr0 shl 21 + cr1 = ashr(s1, 21) + s2 += cr1 + s1 -= cr1 shl 21 + cr2 = ashr(s2, 21) + s3 += cr2 + s2 -= cr2 shl 21 + cr3 = ashr(s3, 21) + s4 += cr3 + s3 -= cr3 shl 21 + cr4 = ashr(s4, 21) + s5 += cr4 + s4 -= cr4 shl 21 + cr5 = ashr(s5, 21) + s6 += cr5 + s5 -= cr5 shl 21 + cr6 = ashr(s6, 21) + s7 += cr6 + s6 -= cr6 shl 21 + cr7 = ashr(s7, 21) + s8 += cr7 + s7 -= cr7 shl 21 + cr8 = ashr(s8, 21) + s9 += cr8 + s8 -= cr8 shl 21 + cr9 = ashr(s9, 21) + s10 += cr9 + s9 -= cr9 shl 21 + cr10 = ashr(s10, 21) + s11 += cr10 + s10 -= cr10 shl 21 + cr11 = ashr(s11, 21) + s12 += cr11 + s11 -= cr11 shl 21 s0 += s12 * 666643 s1 += s12 * 470296 @@ -1505,17 +2001,39 @@ proc scReduce(s: var openArray[byte]) = s5 -= s12 * 683901 s12 = 0 - cr0 = ashr(s0, 21); s1 += cr0; s0 -= cr0 shl 21 - cr1 = ashr(s1, 21); s2 += cr1; s1 -= cr1 shl 21 - cr2 = ashr(s2, 21); s3 += cr2; s2 -= cr2 shl 21 - cr3 = ashr(s3, 21); s4 += cr3; s3 -= cr3 shl 21 - cr4 = ashr(s4, 21); s5 += cr4; s4 -= cr4 shl 21 - cr5 = ashr(s5, 21); s6 += cr5; s5 -= cr5 shl 21 - cr6 = ashr(s6, 21); s7 += cr6; s6 -= cr6 shl 21 - cr7 = ashr(s7, 21); s8 += cr7; s7 -= cr7 shl 21 - cr8 = ashr(s8, 21); s9 += cr8; s8 -= cr8 shl 21 - cr9 = ashr(s9, 21); s10 += cr9; s9 -= cr9 shl 21 - cr10 = ashr(s10, 21); s11 += cr10; s10 -= cr10 shl 21 + cr0 = ashr(s0, 21) + s1 += cr0 + s0 -= cr0 shl 21 + cr1 = ashr(s1, 21) + s2 += cr1 + s1 -= cr1 shl 21 + cr2 = ashr(s2, 21) + s3 += cr2 + s2 -= cr2 shl 21 + cr3 = ashr(s3, 21) + s4 += cr3 + s3 -= cr3 shl 21 + cr4 = ashr(s4, 21) + s5 += cr4 + s4 -= cr4 shl 21 + cr5 = ashr(s5, 21) + s6 += cr5 + s5 -= cr5 shl 21 + cr6 = ashr(s6, 21) + s7 += cr6 + s6 -= cr6 shl 21 + cr7 = ashr(s7, 21) + s8 += cr7 + s7 -= cr7 shl 21 + cr8 = ashr(s8, 21) + s9 += cr8 + s8 -= cr8 shl 21 + cr9 = ashr(s9, 21) + s10 += cr9 + s9 -= cr9 shl 21 + cr10 = ashr(s10, 21) + s11 += cr10 + s10 -= cr10 shl 21 s[0] = cast[byte](ashr(s0, 0)) s[1] = cast[byte](ashr(s0, 8)) @@ -1551,18 +2069,19 @@ proc scReduce(s: var openArray[byte]) = s[31] = cast[byte](ashr(s11, 17)) proc slide(r: var openArray[int8], a: openArray[byte]) = - for i in 0..<256: + for i in 0 ..< 256: r[i] = cast[int8](1'u8 and (a[i shr 3] shr (i and 7))) - for i in 0..<256: + for i in 0 ..< 256: if r[i] != 0'i8: var b = 1 while (b <= 6) and (i + b < 256): if r[i + b] != 0'i8: if r[i] + (r[i + b] shl b) <= 15: - r[i] += r[i + b] shl b; r[i + b] = 0'i8 + r[i] += r[i + b] shl b + r[i + b] = 0'i8 elif (r[i] - (r[i + b] shl b)) >= -15: r[i] -= r[i + b] shl b - for k in (i + b)..<256: + for k in (i + b) ..< 256: if r[k] == 0'i8: r[k] = 1'i8 break @@ -1571,8 +2090,9 @@ proc slide(r: var openArray[int8], a: openArray[byte]) = break inc(b) -proc geDoubleScalarMultVartime(r: var GeP2, a: openArray[byte], A: GeP3, - b: openArray[byte]) = +proc geDoubleScalarMultVartime( + r: var GeP2, a: openArray[byte], A: GeP3, b: openArray[byte] +) = var aslide: array[256, int8] bslide: array[256, int8] @@ -1585,14 +2105,29 @@ proc geDoubleScalarMultVartime(r: var GeP2, a: openArray[byte], A: GeP3, slide(bslide, b) geP3ToCached(ai[0], A) - geP3dbl(t, A); geP1P1toP3(a2, t) - geAdd(t, a2, ai[0]); geP1P1toP3(u, t); geP3ToCached(ai[1], u) - geAdd(t, a2, ai[1]); geP1P1toP3(u, t); geP3ToCached(ai[2], u) - geAdd(t, a2, ai[2]); geP1P1toP3(u, t); geP3ToCached(ai[3], u) - geAdd(t, a2, ai[3]); geP1P1toP3(u, t); geP3ToCached(ai[4], u) - geAdd(t, a2, ai[4]); geP1P1toP3(u, t); geP3ToCached(ai[5], u) - geAdd(t, a2, ai[5]); geP1P1toP3(u, t); geP3ToCached(ai[6], u) - geAdd(t, a2, ai[6]); geP1P1toP3(u, t); geP3ToCached(ai[7], u) + geP3dbl(t, A) + geP1P1toP3(a2, t) + geAdd(t, a2, ai[0]) + geP1P1toP3(u, t) + geP3ToCached(ai[1], u) + geAdd(t, a2, ai[1]) + geP1P1toP3(u, t) + geP3ToCached(ai[2], u) + geAdd(t, a2, ai[2]) + geP1P1toP3(u, t) + geP3ToCached(ai[3], u) + geAdd(t, a2, ai[3]) + geP1P1toP3(u, t) + geP3ToCached(ai[4], u) + geAdd(t, a2, ai[4]) + geP1P1toP3(u, t) + geP3ToCached(ai[5], u) + geAdd(t, a2, ai[5]) + geP1P1toP3(u, t) + geP3ToCached(ai[6], u) + geAdd(t, a2, ai[6]) + geP1P1toP3(u, t) + geP3ToCached(ai[7], u) geP20(r) var k = 255 @@ -1627,7 +2162,7 @@ proc CMP(x, y: uint32): int32 {.inline.} = proc EQ0(x: int32): uint32 {.inline.} = var q = cast[uint32](x) - result = not(q or -q) shr 31 + result = not (q or -q) shr 31 proc NEQ(x, y: uint32): uint32 {.inline.} = var q = cast[uint32](x xor y) @@ -1663,7 +2198,7 @@ proc random*(t: typedesc[EdPrivateKey], rng: var HmacDrbgContext): EdPrivateKey hh.data[31] = hh.data[31] or 0x40'u8 geScalarMultBase(point, hh.data) geP3ToBytes(pk, point) - res.data[32..63] = pk + res.data[32 .. 63] = pk res @@ -1682,7 +2217,7 @@ proc random*(t: typedesc[EdKeyPair], rng: var HmacDrbgContext): EdKeyPair = hh.data[31] = hh.data[31] or 0x40'u8 geScalarMultBase(point, hh.data) geP3ToBytes(res.pubkey.data, point) - res.seckey.data[32..63] = res.pubkey.data + res.seckey.data[32 .. 63] = res.pubkey.data res @@ -1720,14 +2255,14 @@ proc toBytes*(sig: EdSignature, data: var openArray[byte]): int = if len(data) >= result: copyMem(addr data[0], unsafeAddr sig.data[0], len(sig.data)) -proc getBytes*(key: EdPrivateKey): seq[byte] = @(key.data) - ## Serialize ED25519 `private key` and return it. +proc getBytes*(key: EdPrivateKey): seq[byte] = + @(key.data) ## Serialize ED25519 `private key` and return it. -proc getBytes*(key: EdPublicKey): seq[byte] = @(key.data) - ## Serialize ED25519 `public key` and return it. +proc getBytes*(key: EdPublicKey): seq[byte] = + @(key.data) ## Serialize ED25519 `public key` and return it. -proc getBytes*(sig: EdSignature): seq[byte] = @(sig.data) - ## Serialize ED25519 `signature` and return it. +proc getBytes*(sig: EdSignature): seq[byte] = + @(sig.data) ## Serialize ED25519 `signature` and return it. proc `==`*(eda, edb: EdPrivateKey): bool = ## Compare ED25519 `private key` objects for equality. @@ -1804,8 +2339,9 @@ proc init*(sig: var EdSignature, data: string): bool = ## Procedure returns ``true`` on success. init(sig, ncrutils.fromHex(data)) -proc init*(t: typedesc[EdPrivateKey], - data: openArray[byte]): Result[EdPrivateKey, EdError] = +proc init*( + t: typedesc[EdPrivateKey], data: openArray[byte] +): Result[EdPrivateKey, EdError] = ## Initialize ED25519 `private key` from raw binary representation ``data`` ## and return constructed object. var res: t @@ -1814,8 +2350,9 @@ proc init*(t: typedesc[EdPrivateKey], else: ok(res) -proc init*(t: typedesc[EdPublicKey], - data: openArray[byte]): Result[EdPublicKey, EdError] = +proc init*( + t: typedesc[EdPublicKey], data: openArray[byte] +): Result[EdPublicKey, EdError] = ## Initialize ED25519 `public key` from raw binary representation ``data`` ## and return constructed object. var res: t @@ -1824,8 +2361,9 @@ proc init*(t: typedesc[EdPublicKey], else: ok(res) -proc init*(t: typedesc[EdSignature], - data: openArray[byte]): Result[EdSignature, EdError] = +proc init*( + t: typedesc[EdSignature], data: openArray[byte] +): Result[EdSignature, EdError] = ## Initialize ED25519 `signature` from raw binary representation ``data`` ## and return constructed object. var res: t @@ -1834,8 +2372,7 @@ proc init*(t: typedesc[EdSignature], else: ok(res) -proc init*(t: typedesc[EdPrivateKey], - data: string): Result[EdPrivateKey, EdError] = +proc init*(t: typedesc[EdPrivateKey], data: string): Result[EdPrivateKey, EdError] = ## Initialize ED25519 `private key` from hexadecimal string representation ## ``data`` and return constructed object. var res: t @@ -1844,8 +2381,7 @@ proc init*(t: typedesc[EdPrivateKey], else: ok(res) -proc init*(t: typedesc[EdPublicKey], - data: string): Result[EdPublicKey, EdError] = +proc init*(t: typedesc[EdPublicKey], data: string): Result[EdPublicKey, EdError] = ## Initialize ED25519 `public key` from hexadecimal string representation ## ``data`` and return constructed object. var res: t @@ -1854,8 +2390,7 @@ proc init*(t: typedesc[EdPublicKey], else: ok(res) -proc init*(t: typedesc[EdSignature], - data: string): Result[EdSignature, EdError] = +proc init*(t: typedesc[EdSignature], data: string): Result[EdSignature, EdError] = ## Initialize ED25519 `signature` from hexadecimal string representation ## ``data`` and return constructed object. var res: t @@ -1881,8 +2416,9 @@ proc clear*(pair: var EdKeyPair) = burnMem(pair.seckey.data) burnMem(pair.pubkey.data) -proc sign*[T: byte|char](key: EdPrivateKey, - message: openArray[T]): EdSignature {.gcsafe, noinit.} = +proc sign*[T: byte | char]( + key: EdPrivateKey, message: openArray[T] +): EdSignature {.gcsafe, noinit.} = ## Create ED25519 signature of data ``message`` using private key ``key``. var ctx: sha512 var r: GeP3 @@ -1912,11 +2448,16 @@ proc sign*[T: byte|char](key: EdPrivateKey, ctx.clear() scReduce(hram.data) - scMulAdd(result.data.toOpenArray(32, 63), hram.data.toOpenArray(0, 31), - hash.data.toOpenArray(0, 31), nonce.data.toOpenArray(0, 31)) - -proc verify*[T: byte|char](sig: EdSignature, message: openArray[T], - key: EdPublicKey): bool = + scMulAdd( + result.data.toOpenArray(32, 63), + hram.data.toOpenArray(0, 31), + hash.data.toOpenArray(0, 31), + nonce.data.toOpenArray(0, 31), + ) + +proc verify*[T: byte | char]( + sig: EdSignature, message: openArray[T], key: EdPublicKey +): bool = ## Verify ED25519 signature ``sig`` using public key ``key`` and data ## ``message``. ## @@ -1941,8 +2482,9 @@ proc verify*[T: byte|char](sig: EdSignature, message: openArray[T], var hash = ctx.finish() scReduce(hash.data) - geDoubleScalarMultVartime(r, hash.data.toOpenArray(0, 31), - a, sig.data.toOpenArray(32, 63)) + geDoubleScalarMultVartime( + r, hash.data.toOpenArray(0, 31), a, sig.data.toOpenArray(32, 63) + ) geToBytes(rcheck, r) result = (verify32(sig.data.toOpenArray(0, 31), rcheck) == 0) diff --git a/libp2p/crypto/hkdf.nim b/libp2p/crypto/hkdf.nim index 4808120dcb..9dd271c619 100644 --- a/libp2p/crypto/hkdf.nim +++ b/libp2p/crypto/hkdf.nim @@ -16,18 +16,41 @@ import bearssl/[kdf, hash] type HkdfResult*[len: static int] = array[len, byte] -proc hkdf*[T: sha256; len: static int](_: type[T]; salt, ikm, info: openArray[byte]; outputs: var openArray[HkdfResult[len]]) = - var - ctx: HkdfContext +proc hkdf*[T: sha256, len: static int]( + _: type[T], + salt, ikm, info: openArray[byte], + outputs: var openArray[HkdfResult[len]], +) = + var ctx: HkdfContext hkdfInit( - ctx, addr sha256Vtable, - if salt.len > 0: unsafeAddr salt[0] else: nil, csize_t(salt.len)) + ctx, + addr sha256Vtable, + if salt.len > 0: + unsafeAddr salt[0] + else: + nil + , + csize_t(salt.len), + ) hkdfInject( - ctx, if ikm.len > 0: unsafeAddr ikm[0] else: nil, csize_t(ikm.len)) + ctx, + if ikm.len > 0: + unsafeAddr ikm[0] + else: + nil + , + csize_t(ikm.len), + ) hkdfFlip(ctx) - for i in 0..outputs.high: + for i in 0 .. outputs.high: discard hkdfProduce( ctx, - if info.len > 0: unsafeAddr info[0] - else: nil, csize_t(info.len), - addr outputs[i][0], csize_t(outputs[i].len)) + if info.len > 0: + unsafeAddr info[0] + else: + nil + , + csize_t(info.len), + addr outputs[i][0], + csize_t(outputs[i].len), + ) diff --git a/libp2p/crypto/minasn1.nim b/libp2p/crypto/minasn1.nim index 2c06d3db39..9700d1b2e5 100644 --- a/libp2p/crypto/minasn1.nim +++ b/libp2p/crypto/minasn1.nim @@ -19,35 +19,34 @@ import ../utility type Asn1Error* {.pure.} = enum - Overflow, - Incomplete, - Indefinite, - Incorrect, - NoSupport, + Overflow + Incomplete + Indefinite + Incorrect + NoSupport Overrun Asn1Result*[T] = Result[T, Asn1Error] Asn1Class* {.pure.} = enum - Universal = 0x00, + Universal = 0x00 Application = 0x01 ContextSpecific = 0x02 Private = 0x03 Asn1Tag* {.pure.} = enum ## Protobuf's field types enum - NoSupport, - Boolean, - Integer, - BitString, - OctetString, - Null, - Oid, - Sequence, + NoSupport + Boolean + Integer + BitString + OctetString + Null + Oid + Sequence Context - Asn1Buffer* = object of RootObj - ## ASN.1's message representation object + Asn1Buffer* = object of RootObj ## ASN.1's message representation object buffer*: seq[byte] offset*: int length*: int @@ -73,37 +72,23 @@ type idx*: int const - Asn1OidSecp256r1* = [ - 0x2A'u8, 0x86'u8, 0x48'u8, 0xCE'u8, 0x3D'u8, 0x03'u8, 0x01'u8, 0x07'u8 - ] + Asn1OidSecp256r1* = + [0x2A'u8, 0x86'u8, 0x48'u8, 0xCE'u8, 0x3D'u8, 0x03'u8, 0x01'u8, 0x07'u8] ## Encoded OID for `secp256r1` curve (1.2.840.10045.3.1.7) - Asn1OidSecp384r1* = [ - 0x2B'u8, 0x81'u8, 0x04'u8, 0x00'u8, 0x22'u8 - ] + Asn1OidSecp384r1* = [0x2B'u8, 0x81'u8, 0x04'u8, 0x00'u8, 0x22'u8] ## Encoded OID for `secp384r1` curve (1.3.132.0.34) - Asn1OidSecp521r1* = [ - 0x2B'u8, 0x81'u8, 0x04'u8, 0x00'u8, 0x23'u8 - ] + Asn1OidSecp521r1* = [0x2B'u8, 0x81'u8, 0x04'u8, 0x00'u8, 0x23'u8] ## Encoded OID for `secp521r1` curve (1.3.132.0.35) - Asn1OidSecp256k1* = [ - 0x2B'u8, 0x81'u8, 0x04'u8, 0x00'u8, 0x0A'u8 - ] + Asn1OidSecp256k1* = [0x2B'u8, 0x81'u8, 0x04'u8, 0x00'u8, 0x0A'u8] ## Encoded OID for `secp256k1` curve (1.3.132.0.10) - Asn1OidEcPublicKey* = [ - 0x2A'u8, 0x86'u8, 0x48'u8, 0xCE'u8, 0x3D'u8, 0x02'u8, 0x01'u8 - ] + Asn1OidEcPublicKey* = [0x2A'u8, 0x86'u8, 0x48'u8, 0xCE'u8, 0x3D'u8, 0x02'u8, 0x01'u8] ## Encoded OID for Elliptic Curve Public Key (1.2.840.10045.2.1) - Asn1OidRsaEncryption* = [ - 0x2A'u8, 0x86'u8, 0x48'u8, 0x86'u8, 0xF7'u8, 0x0D'u8, 0x01'u8, - 0x01'u8, 0x01'u8 - ] + Asn1OidRsaEncryption* = + [0x2A'u8, 0x86'u8, 0x48'u8, 0x86'u8, 0xF7'u8, 0x0D'u8, 0x01'u8, 0x01'u8, 0x01'u8] ## Encoded OID for RSA Encryption (1.2.840.113549.1.1.1) - Asn1True* = [0x01'u8, 0x01'u8, 0xFF'u8] - ## Encoded boolean ``TRUE``. - Asn1False* = [0x01'u8, 0x01'u8, 0x00'u8] - ## Encoded boolean ``FALSE``. - Asn1Null* = [0x05'u8, 0x00'u8] - ## Encoded ``NULL`` value. + Asn1True* = [0x01'u8, 0x01'u8, 0xFF'u8] ## Encoded boolean ``TRUE``. + Asn1False* = [0x01'u8, 0x01'u8, 0x00'u8] ## Encoded boolean ``FALSE``. + Asn1Null* = [0x05'u8, 0x00'u8] ## Encoded ``NULL`` value. template toOpenArray*(ab: Asn1Buffer): untyped = toOpenArray(ab.buffer, ab.offset, ab.buffer.high) @@ -120,7 +105,7 @@ template isEmpty*(ab: Asn1Buffer): bool = template isEnough*(ab: Asn1Buffer, length: int64): bool = len(ab.buffer) >= ab.offset + length -proc len*[T: Asn1Buffer|Asn1Composite](abc: T): int {.inline.} = +proc len*[T: Asn1Buffer | Asn1Composite](abc: T): int {.inline.} = len(abc.buffer) - abc.offset proc len*(field: Asn1Field): int {.inline.} = @@ -129,31 +114,22 @@ proc len*(field: Asn1Field): int {.inline.} = template getPtr*(field: untyped): pointer = cast[pointer](unsafeAddr field.buffer[field.offset]) -proc extend*[T: Asn1Buffer|Asn1Composite](abc: var T, length: int) {.inline.} = +proc extend*[T: Asn1Buffer | Asn1Composite](abc: var T, length: int) {.inline.} = ## Extend buffer or composite's internal buffer by ``length`` octets. abc.buffer.setLen(len(abc.buffer) + length) proc code*(tag: Asn1Tag): byte {.inline.} = ## Converts Nim ``tag`` enum to ASN.1 tag code. - case tag: - of Asn1Tag.NoSupport: - 0x00'u8 - of Asn1Tag.Boolean: - 0x01'u8 - of Asn1Tag.Integer: - 0x02'u8 - of Asn1Tag.BitString: - 0x03'u8 - of Asn1Tag.OctetString: - 0x04'u8 - of Asn1Tag.Null: - 0x05'u8 - of Asn1Tag.Oid: - 0x06'u8 - of Asn1Tag.Sequence: - 0x30'u8 - of Asn1Tag.Context: - 0xA0'u8 + case tag + of Asn1Tag.NoSupport: 0x00'u8 + of Asn1Tag.Boolean: 0x01'u8 + of Asn1Tag.Integer: 0x02'u8 + of Asn1Tag.BitString: 0x03'u8 + of Asn1Tag.OctetString: 0x04'u8 + of Asn1Tag.Null: 0x05'u8 + of Asn1Tag.Oid: 0x06'u8 + of Asn1Tag.Sequence: 0x30'u8 + of Asn1Tag.Context: 0xA0'u8 proc asn1EncodeLength*(dest: var openArray[byte], length: uint64): int = ## Encode ASN.1 DER length part of TLV triple and return number of bytes @@ -182,8 +158,7 @@ proc asn1EncodeLength*(dest: var openArray[byte], length: uint64): int = # then 9, so it is safe to convert it to `int`. int(res) -proc asn1EncodeInteger*(dest: var openArray[byte], - value: openArray[byte]): int = +proc asn1EncodeInteger*(dest: var openArray[byte], value: openArray[byte]): int = ## Encode big-endian binary representation of integer as ASN.1 DER `INTEGER` ## and return number of bytes (octets) used. ## @@ -193,17 +168,16 @@ proc asn1EncodeInteger*(dest: var openArray[byte], var buffer: array[16, byte] var lenlen = 0 - let offset = - block: - var o = 0 - for i in 0 ..< len(value): - if value[o] != 0x00: - break - inc(o) - if o < len(value): - o - else: - o - 1 + let offset = block: + var o = 0 + for i in 0 ..< len(value): + if value[o] != 0x00: + break + inc(o) + if o < len(value): + o + else: + o - 1 let destlen = if len(value) > 0: @@ -225,12 +199,10 @@ proc asn1EncodeInteger*(dest: var openArray[byte], if value[offset] >= 0x80'u8: dest[1 + lenlen] = 0x00'u8 shift = 2 - copyMem(addr dest[shift + lenlen], unsafeAddr value[offset], - len(value) - offset) + copyMem(addr dest[shift + lenlen], unsafeAddr value[offset], len(value) - offset) destlen -proc asn1EncodeInteger*[T: SomeUnsignedInt](dest: var openArray[byte], - value: T): int = +proc asn1EncodeInteger*[T: SomeUnsignedInt](dest: var openArray[byte], value: T): int = ## Encode Nim's unsigned integer as ASN.1 DER `INTEGER` and return number of ## bytes (octets) used. ## @@ -265,8 +237,7 @@ proc asn1EncodeNull*(dest: var openArray[byte]): int = dest[1] = 0x00'u8 res -proc asn1EncodeOctetString*(dest: var openArray[byte], - value: openArray[byte]): int = +proc asn1EncodeOctetString*(dest: var openArray[byte], value: openArray[byte]): int = ## Encode array of bytes as ASN.1 DER `OCTET STRING` and return number of ## bytes (octets) used. ## @@ -283,8 +254,9 @@ proc asn1EncodeOctetString*(dest: var openArray[byte], copyMem(addr dest[1 + lenlen], unsafeAddr value[0], len(value)) res -proc asn1EncodeBitString*(dest: var openArray[byte], - value: openArray[byte], bits = 0): int = +proc asn1EncodeBitString*( + dest: var openArray[byte], value: openArray[byte], bits = 0 +): int = ## Encode array of bytes as ASN.1 DER `BIT STRING` and return number of bytes ## (octets) used. ## @@ -305,7 +277,7 @@ proc asn1EncodeBitString*(dest: var openArray[byte], let bytelen = (bitlen + 7) shr 3 # Number of unused bits let unused = (8 - (bitlen and 7)) and 7 - let mask = not((1'u8 shl unused) - 1'u8) + let mask = not ((1'u8 shl unused) - 1'u8) var lenlen = asn1EncodeLength(buffer, uint64(bytelen + 1)) let res = 1 + lenlen + 1 + len(value) if len(dest) >= res: @@ -319,8 +291,7 @@ proc asn1EncodeBitString*(dest: var openArray[byte], dest[2 + lenlen + bytelen - 1] = lastbyte and mask res -proc asn1EncodeTag[T: SomeUnsignedInt](dest: var openArray[byte], - value: T): int = +proc asn1EncodeTag[T: SomeUnsignedInt](dest: var openArray[byte], value: T): int = var v = value if value <= cast[T](0x7F): if len(dest) >= 1: @@ -361,8 +332,7 @@ proc asn1EncodeOid*(dest: var openArray[byte], value: openArray[byte]): int = copyMem(addr dest[1 + lenlen], unsafeAddr value[0], len(value)) res -proc asn1EncodeSequence*(dest: var openArray[byte], - value: openArray[byte]): int = +proc asn1EncodeSequence*(dest: var openArray[byte], value: openArray[byte]): int = ## Encode ``value`` as ASN.1 DER `SEQUENCE` and return number of bytes ## (octets) used. ## @@ -378,8 +348,7 @@ proc asn1EncodeSequence*(dest: var openArray[byte], copyMem(addr dest[1 + lenlen], unsafeAddr value[0], len(value)) res -proc asn1EncodeComposite*(dest: var openArray[byte], - value: Asn1Composite): int = +proc asn1EncodeComposite*(dest: var openArray[byte], value: Asn1Composite): int = ## Encode composite value and return number of bytes (octets) used. ## ## If length of ``dest`` is less then number of required bytes to encode @@ -391,12 +360,12 @@ proc asn1EncodeComposite*(dest: var openArray[byte], if len(dest) >= res: dest[0] = value.tag.code() copyMem(addr dest[1], addr buffer[0], lenlen) - copyMem(addr dest[1 + lenlen], unsafeAddr value.buffer[0], - len(value.buffer)) + copyMem(addr dest[1 + lenlen], unsafeAddr value.buffer[0], len(value.buffer)) res -proc asn1EncodeContextTag*(dest: var openArray[byte], value: openArray[byte], - tag: int): int = +proc asn1EncodeContextTag*( + dest: var openArray[byte], value: openArray[byte], tag: int +): int = ## Encode ASN.1 DER `CONTEXT SPECIFIC TAG` ``tag`` for value ``value`` and ## return number of bytes (octets) used. ## @@ -432,7 +401,7 @@ proc getLength(ab: var Asn1Buffer): Asn1Result[int] = return err(Asn1Error.Overflow) if ab.isEnough(octets): var lengthU: uint64 = 0 - for i in 0.. uint64(int64.high): return err(Asn1Error.Overflow) @@ -471,7 +440,7 @@ proc read*(ab: var Asn1Buffer): Asn1Result[Asn1Field] = inclass = false while true: offset = ab.offset - aclass = ? ab.getTag(tag) + aclass = ?ab.getTag(tag) case aclass of Asn1Class.ContextSpecific: @@ -480,9 +449,9 @@ proc read*(ab: var Asn1Buffer): Asn1Result[Asn1Field] = else: inclass = true ttag = tag - tlength = ? ab.getLength() + tlength = ?ab.getLength() of Asn1Class.Universal: - length = ? ab.getLength() + length = ?ab.getLength() if inclass: if length >= tlength: @@ -499,22 +468,26 @@ proc read*(ab: var Asn1Buffer): Asn1Result[Asn1Field] = let b = ab.buffer[ab.offset] if b != 0xFF'u8 and b != 0x00'u8: - return err(Asn1Error.Incorrect) + return err(Asn1Error.Incorrect) - field = Asn1Field(kind: Asn1Tag.Boolean, klass: aclass, - index: ttag, offset: ab.offset, - length: 1, buffer: ab.buffer) + field = Asn1Field( + kind: Asn1Tag.Boolean, + klass: aclass, + index: ttag, + offset: ab.offset, + length: 1, + buffer: ab.buffer, + ) field.vbool = (b == 0xFF'u8) ab.offset += 1 return ok(field) - of Asn1Tag.Integer.code(): # INTEGER if length == 0: return err(Asn1Error.Incorrect) if not ab.isEnough(length): - return err(Asn1Error.Incomplete) + return err(Asn1Error.Incomplete) # Count number of leading zeroes var zc = 0 @@ -526,9 +499,14 @@ proc read*(ab: var Asn1Buffer): Asn1Result[Asn1Field] = if zc == 0: # Negative or Positive integer - field = Asn1Field(kind: Asn1Tag.Integer, klass: aclass, - index: ttag, offset: ab.offset, - length: length, buffer: ab.buffer) + field = Asn1Field( + kind: Asn1Tag.Integer, + klass: aclass, + 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: @@ -538,54 +516,68 @@ proc read*(ab: var Asn1Buffer): Asn1Result[Asn1Field] = field.vint = (field.vint shl 8) or 0xFF'u64 else: let offset = ab.offset + i - (8 - length) - field.vint = (field.vint shl 8) or safeConvert[uint64](ab.buffer[offset]) + field.vint = + (field.vint shl 8) or safeConvert[uint64](ab.buffer[offset]) else: # Positive integer if length <= 8: for i in 0 ..< length: - field.vint = (field.vint shl 8) or - safeConvert[uint64](ab.buffer[ab.offset + i]) + field.vint = + (field.vint shl 8) or 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: ab.offset, - length: length, vint: 0'u64, - buffer: ab.buffer) + field = Asn1Field( + kind: Asn1Tag.Integer, + klass: aclass, + index: ttag, + offset: ab.offset, + length: length, + vint: 0'u64, + buffer: ab.buffer, + ) ab.offset += length return ok(field) else: # Positive integer with leading zero - field = Asn1Field(kind: Asn1Tag.Integer, klass: aclass, - index: ttag, offset: ab.offset + 1, - length: length - 1, buffer: ab.buffer) + field = Asn1Field( + kind: Asn1Tag.Integer, + klass: aclass, + index: ttag, + offset: ab.offset + 1, + length: length - 1, + buffer: ab.buffer, + ) if length <= 9: for i in 1 ..< length: - field.vint = (field.vint shl 8) or - safeConvert[uint64](ab.buffer[ab.offset + i]) + field.vint = + (field.vint shl 8) or safeConvert[uint64](ab.buffer[ab.offset + i]) ab.offset += length return ok(field) - of Asn1Tag.BitString.code(): # BIT STRING if length == 0: # BIT STRING should include `unused` bits field, so length should be # bigger then 1. return err(Asn1Error.Incorrect) - elif length == 1: if ab.buffer[ab.offset] != 0x00'u8: return err(Asn1Error.Incorrect) else: # Zero-length BIT STRING. - field = Asn1Field(kind: Asn1Tag.BitString, klass: aclass, - index: ttag, offset: ab.offset + 1, - length: 0, ubits: 0, buffer: ab.buffer) + field = Asn1Field( + kind: Asn1Tag.BitString, + klass: aclass, + index: ttag, + offset: ab.offset + 1, + length: 0, + ubits: 0, + buffer: ab.buffer, + ) ab.offset += length return ok(field) - else: if not ab.isEnough(length): return err(Asn1Error.Incomplete) @@ -600,56 +592,77 @@ proc read*(ab: var Asn1Buffer): Asn1Result[Asn1Field] = ## All unused bits should be set to `0`. return err(Asn1Error.Incorrect) - field = Asn1Field(kind: Asn1Tag.BitString, klass: aclass, - index: ttag, offset: ab.offset + 1, - length: length - 1, ubits: safeConvert[int](unused), - buffer: ab.buffer) + field = Asn1Field( + kind: Asn1Tag.BitString, + klass: aclass, + index: ttag, + offset: ab.offset + 1, + length: length - 1, + ubits: safeConvert[int](unused), + buffer: ab.buffer, + ) ab.offset += length return ok(field) - of Asn1Tag.OctetString.code(): # OCTET STRING if not ab.isEnough(length): return err(Asn1Error.Incomplete) - field = Asn1Field(kind: Asn1Tag.OctetString, klass: aclass, - index: ttag, offset: ab.offset, - length: length, buffer: ab.buffer) + field = Asn1Field( + kind: Asn1Tag.OctetString, + klass: aclass, + index: ttag, + offset: ab.offset, + length: length, + buffer: ab.buffer, + ) ab.offset += length return ok(field) - of Asn1Tag.Null.code(): # NULL if length != 0: return err(Asn1Error.Incorrect) - field = Asn1Field(kind: Asn1Tag.Null, klass: aclass, index: ttag, - offset: ab.offset, length: 0, buffer: ab.buffer) + field = Asn1Field( + kind: Asn1Tag.Null, + klass: aclass, + index: ttag, + offset: ab.offset, + length: 0, + buffer: ab.buffer, + ) ab.offset += length return ok(field) - of Asn1Tag.Oid.code(): # OID if not ab.isEnough(length): return err(Asn1Error.Incomplete) - field = Asn1Field(kind: Asn1Tag.Oid, klass: aclass, - index: ttag, offset: ab.offset, - length: length, buffer: ab.buffer) + field = Asn1Field( + kind: Asn1Tag.Oid, + klass: aclass, + 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(length): return err(Asn1Error.Incomplete) - field = Asn1Field(kind: Asn1Tag.Sequence, klass: aclass, - index: ttag, offset: ab.offset, - length: length, buffer: ab.buffer) + field = Asn1Field( + kind: Asn1Tag.Sequence, + klass: aclass, + index: ttag, + offset: ab.offset, + length: length, + buffer: ab.buffer, + ) ab.offset += length return ok(field) - else: return err(Asn1Error.NoSupport) @@ -672,9 +685,9 @@ proc `==`*(field: Asn1Field, data: openArray[byte]): bool = if length > 0: if field.length == len(data): CT.isEqual( - field.buffer.toOpenArray(field.offset, - field.offset + field.length - 1), - data.toOpenArray(0, field.length - 1)) + field.buffer.toOpenArray(field.offset, field.offset + field.length - 1), + data.toOpenArray(0, field.length - 1), + ) else: false else: @@ -752,13 +765,14 @@ proc `$`*(field: Asn1Field): string = res.add(ncrutils.toHex(field.toOpenArray())) res -proc write*[T: Asn1Buffer|Asn1Composite](abc: var T, tag: Asn1Tag) = +proc write*[T: Asn1Buffer | Asn1Composite](abc: var T, tag: Asn1Tag) = ## Write empty value to buffer or composite with ``tag``. ## ## This procedure must be used to write `NULL`, `0` or empty `BIT STRING`, ## `OCTET STRING` types. - doAssert(tag in {Asn1Tag.Null, Asn1Tag.Integer, Asn1Tag.BitString, - Asn1Tag.OctetString}) + doAssert( + tag in {Asn1Tag.Null, Asn1Tag.Integer, Asn1Tag.BitString, Asn1Tag.OctetString} + ) var length: int if tag == Asn1Tag.Null: length = asn1EncodeNull(abc.toOpenArray()) @@ -780,22 +794,23 @@ proc write*[T: Asn1Buffer|Asn1Composite](abc: var T, tag: Asn1Tag) = discard asn1EncodeOctetString(abc.toOpenArray(), tmp.toOpenArray(0, -1)) abc.offset += length -proc write*[T: Asn1Buffer|Asn1Composite](abc: var T, value: uint64) = +proc write*[T: Asn1Buffer | Asn1Composite](abc: var T, value: uint64) = ## Write uint64 ``value`` to buffer or composite as ASN.1 `INTEGER`. let length = asn1EncodeInteger(abc.toOpenArray(), value) abc.extend(length) discard asn1EncodeInteger(abc.toOpenArray(), value) abc.offset += length -proc write*[T: Asn1Buffer|Asn1Composite](abc: var T, value: bool) = +proc write*[T: Asn1Buffer | Asn1Composite](abc: var T, value: bool) = ## Write bool ``value`` to buffer or composite as ASN.1 `BOOLEAN`. let length = asn1EncodeBoolean(abc.toOpenArray(), value) abc.extend(length) discard asn1EncodeBoolean(abc.toOpenArray(), value) abc.offset += length -proc write*[T: Asn1Buffer|Asn1Composite](abc: var T, tag: Asn1Tag, - value: openArray[byte], bits = 0) = +proc write*[T: Asn1Buffer | Asn1Composite]( + abc: var T, tag: Asn1Tag, value: openArray[byte], bits = 0 +) = ## Write array ``value`` using ``tag``. ## ## This procedure is used to write ASN.1 `INTEGER`, `OCTET STRING`, @@ -803,8 +818,9 @@ proc write*[T: Asn1Buffer|Asn1Composite](abc: var T, tag: Asn1Tag, ## ## For `BIT STRING` you can use ``bits`` argument to specify number of used ## bits. - doAssert(tag in {Asn1Tag.Integer, Asn1Tag.OctetString, Asn1Tag.BitString, - Asn1Tag.Oid}) + doAssert( + tag in {Asn1Tag.Integer, Asn1Tag.OctetString, Asn1Tag.BitString, Asn1Tag.Oid} + ) var length: int if tag == Asn1Tag.Integer: length = asn1EncodeInteger(abc.toOpenArray(), value) @@ -824,7 +840,7 @@ proc write*[T: Asn1Buffer|Asn1Composite](abc: var T, tag: Asn1Tag, discard asn1EncodeOid(abc.toOpenArray(), value) abc.offset += length -proc write*[T: Asn1Buffer|Asn1Composite](abc: var T, value: Asn1Composite) = +proc write*[T: Asn1Buffer | Asn1Composite](abc: var T, value: Asn1Composite) = doAssert(len(value) > 0, "Composite value not finished") var length: int if value.tag == Asn1Tag.Sequence: @@ -841,6 +857,6 @@ proc write*[T: Asn1Buffer|Asn1Composite](abc: var T, value: Asn1Composite) = discard asn1EncodeContextTag(abc.toOpenArray(), value.buffer, value.idx) abc.offset += length -proc finish*[T: Asn1Buffer|Asn1Composite](abc: var T) {.inline.} = +proc finish*[T: Asn1Buffer | Asn1Composite](abc: var T) {.inline.} = ## Finishes buffer or composite and prepares it for writing. abc.offset = 0 diff --git a/libp2p/crypto/rsa.nim b/libp2p/crypto/rsa.nim index 53f0985b1c..ba31e05ae4 100644 --- a/libp2p/crypto/rsa.nim +++ b/libp2p/crypto/rsa.nim @@ -30,32 +30,17 @@ const MinKeySize* = 2048 ## Minimal allowed RSA key size in bits. ## https://github.com/libp2p/go-libp2p-core/blob/master/crypto/rsa_common.go#L13 - DefaultKeySize* = 3072 - ## Default RSA key size in bits. + DefaultKeySize* = 3072 ## Default RSA key size in bits. - RsaOidSha1* = [ - byte 0x05, 0x2B, 0x0E, 0x03, 0x02, 0x1A - ] + RsaOidSha1* = [byte 0x05, 0x2B, 0x0E, 0x03, 0x02, 0x1A] ## RSA PKCS#1.5 SHA-1 hash object identifier. - RsaOidSha224* = [ - byte 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, - 0x02, 0x04 - ] + RsaOidSha224* = [byte 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04] ## RSA PKCS#1.5 SHA-224 hash object identifier. - RsaOidSha256* = [ - byte 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, - 0x02, 0x01 - ] + RsaOidSha256* = [byte 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01] ## RSA PKCS#1.5 SHA-256 hash object identifier. - RsaOidSha384* = [ - byte 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, - 0x02, 0x02 - ] + RsaOidSha384* = [byte 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02] ## RSA PKCS#1.5 SHA-384 hash object identifier. - RsaOidSha512* = [ - byte 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, - 0x02, 0x03 - ] + RsaOidSha512* = [byte 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03] ## RSA PKCS#1.5 SHA-512 hash object identifier. type @@ -79,9 +64,9 @@ type RsaKP* = RsaPrivateKey | RsaKeyPair RsaError* = enum - RsaGenError, - RsaKeyIncorrectError, - RsaSignatureError, + RsaGenError + RsaKeyIncorrectError + RsaSignatureError RsaLowSecurityError RsaResult*[T] = Result[T, RsaError] @@ -109,15 +94,18 @@ template getArray*(bs, os, ls: untyped): untyped = template trimZeroes(b: seq[byte], pt, ptlen: untyped) = var length = ptlen - for i in 0.. 0: - let length = key.seck.plen.uint + key.seck.qlen.uint + key.seck.dplen.uint + - key.seck.dqlen.uint + key.seck.iqlen.uint + key.pubk.nlen.uint + - key.pubk.elen.uint + key.pexplen.uint + let length = + key.seck.plen.uint + key.seck.qlen.uint + key.seck.dplen.uint + + key.seck.dqlen.uint + key.seck.iqlen.uint + key.pubk.nlen.uint + + key.pubk.elen.uint + key.pexplen.uint result = new RsaPrivateKey result.buffer = newSeq[byte](length) let po: uint = 0 @@ -235,8 +229,7 @@ proc getPublicKey*(key: RsaPrivateKey): RsaPublicKey = result.key.n = addr result.buffer[0] result.key.e = addr result.buffer[key.pubk.nlen] copyMem(addr result.buffer[0], cast[pointer](key.pubk.n), key.pubk.nlen) - copyMem(addr result.buffer[key.pubk.nlen], cast[pointer](key.pubk.e), - key.pubk.elen) + copyMem(addr result.buffer[key.pubk.nlen], cast[pointer](key.pubk.e), key.pubk.elen) result.key.nlen = key.pubk.nlen result.key.elen = key.pubk.elen @@ -248,7 +241,7 @@ proc pubkey*(pair: RsaKeyPair): RsaPublicKey {.inline.} = ## Get RSA public key from pair ``pair``. result = cast[RsaPrivateKey](pair).getPublicKey() -proc clear*[T: RsaPKI|RsaKeyPair](pki: var T) = +proc clear*[T: RsaPKI | RsaKeyPair](pki: var T) = ## Wipe and clear EC private key, public key or scalar object. doAssert(not isNil(pki)) when T is RsaPrivateKey: @@ -292,21 +285,14 @@ proc toBytes*(key: RsaPrivateKey, data: var openArray[byte]): RsaResult[int] = var b = Asn1Buffer.init() var p = Asn1Composite.init(Asn1Tag.Sequence) p.write(0'u64) - p.write(Asn1Tag.Integer, getArray(key.buffer, key.pubk.n, - key.pubk.nlen)) - p.write(Asn1Tag.Integer, getArray(key.buffer, key.pubk.e, - key.pubk.elen)) + p.write(Asn1Tag.Integer, getArray(key.buffer, key.pubk.n, key.pubk.nlen)) + p.write(Asn1Tag.Integer, getArray(key.buffer, key.pubk.e, key.pubk.elen)) p.write(Asn1Tag.Integer, getArray(key.buffer, key.pexp, key.pexplen)) - p.write(Asn1Tag.Integer, getArray(key.buffer, key.seck.p, - key.seck.plen)) - p.write(Asn1Tag.Integer, getArray(key.buffer, key.seck.q, - key.seck.qlen)) - p.write(Asn1Tag.Integer, getArray(key.buffer, key.seck.dp, - key.seck.dplen)) - p.write(Asn1Tag.Integer, getArray(key.buffer, key.seck.dq, - key.seck.dqlen)) - p.write(Asn1Tag.Integer, getArray(key.buffer, key.seck.iq, - key.seck.iqlen)) + p.write(Asn1Tag.Integer, getArray(key.buffer, key.seck.p, key.seck.plen)) + p.write(Asn1Tag.Integer, getArray(key.buffer, key.seck.q, key.seck.qlen)) + p.write(Asn1Tag.Integer, getArray(key.buffer, key.seck.dp, key.seck.dplen)) + p.write(Asn1Tag.Integer, getArray(key.buffer, key.seck.dq, key.seck.dqlen)) + p.write(Asn1Tag.Integer, getArray(key.buffer, key.seck.iq, key.seck.iqlen)) p.finish() b.write(p) b.finish() @@ -371,7 +357,7 @@ proc getBytes*(key: RsaPrivateKey): RsaResult[seq[byte]] = if isNil(key): return err(RsaKeyIncorrectError) var res = newSeq[byte](4096) - let length = ? key.toBytes(res) + let length = ?key.toBytes(res) if length > 0: res.setLen(length) ok(res) @@ -384,7 +370,7 @@ proc getBytes*(key: RsaPublicKey): RsaResult[seq[byte]] = if isNil(key): return err(RsaKeyIncorrectError) var res = newSeq[byte](4096) - let length = ? key.toBytes(res) + let length = ?key.toBytes(res) if length > 0: res.setLen(length) ok(res) @@ -396,7 +382,7 @@ proc getBytes*(sig: RsaSignature): RsaResult[seq[byte]] = if isNil(sig): return err(RsaSignatureError) var res = newSeq[byte](4096) - let length = ? sig.toBytes(res) + let length = ?sig.toBytes(res) if length > 0: res.setLen(length) ok(res) @@ -408,20 +394,19 @@ proc init*(key: var RsaPrivateKey, data: openArray[byte]): Result[void, Asn1Erro ## ``data``. ## ## Procedure returns ``Asn1Status``. - var - field, rawn, rawpube, rawprie, rawp, rawq, rawdp, rawdq, rawiq: Asn1Field + var field, rawn, rawpube, rawprie, rawp, rawq, rawdp, rawdq, rawiq: Asn1Field # Asn1Field is not trivial so avoid too much Result var ab = Asn1Buffer.init(data) - field = ? ab.read() + field = ?ab.read() if field.kind != Asn1Tag.Sequence: return err(Asn1Error.Incorrect) var ib = field.getBuffer() - field = ? ib.read() + field = ?ib.read() if field.kind != Asn1Tag.Integer: return err(Asn1Error.Incorrect) @@ -429,48 +414,48 @@ proc init*(key: var RsaPrivateKey, data: openArray[byte]): Result[void, Asn1Erro if field.vint != 0'u64: return err(Asn1Error.Incorrect) - rawn = ? ib.read() + rawn = ?ib.read() if rawn.kind != Asn1Tag.Integer: return err(Asn1Error.Incorrect) - rawpube = ? ib.read() + rawpube = ?ib.read() if rawpube.kind != Asn1Tag.Integer: return err(Asn1Error.Incorrect) - rawprie = ? ib.read() + rawprie = ?ib.read() if rawprie.kind != Asn1Tag.Integer: return err(Asn1Error.Incorrect) - rawp = ? ib.read() + rawp = ?ib.read() if rawp.kind != Asn1Tag.Integer: return err(Asn1Error.Incorrect) - rawq = ? ib.read() + rawq = ?ib.read() if rawq.kind != Asn1Tag.Integer: return err(Asn1Error.Incorrect) - rawdp = ? ib.read() + rawdp = ?ib.read() if rawdp.kind != Asn1Tag.Integer: return err(Asn1Error.Incorrect) - rawdq = ? ib.read() + rawdq = ?ib.read() if rawdq.kind != Asn1Tag.Integer: return err(Asn1Error.Incorrect) - rawiq = ? ib.read() + rawiq = ?ib.read() if rawiq.kind != Asn1Tag.Integer: return err(Asn1Error.Incorrect) if len(rawn) >= (MinKeySize shr 3) and len(rawp) > 0 and len(rawq) > 0 and - len(rawdp) > 0 and len(rawdq) > 0 and len(rawiq) > 0: + len(rawdp) > 0 and len(rawdq) > 0 and len(rawiq) > 0: key = new RsaPrivateKey key.buffer = @data key.pubk.n = addr key.buffer[rawn.offset] @@ -502,52 +487,52 @@ proc init*(key: var RsaPublicKey, data: openArray[byte]): Result[void, Asn1Error var field, rawn, rawe: Asn1Field var ab = Asn1Buffer.init(data) - field = ? ab.read() + field = ?ab.read() if field.kind != Asn1Tag.Sequence: return err(Asn1Error.Incorrect) var ib = field.getBuffer() - field = ? ib.read() + field = ?ib.read() if field.kind != Asn1Tag.Sequence: return err(Asn1Error.Incorrect) var ob = field.getBuffer() - field = ? ob.read() + field = ?ob.read() if field.kind != Asn1Tag.Oid: return err(Asn1Error.Incorrect) elif field != Asn1OidRsaEncryption: return err(Asn1Error.Incorrect) - field = ? ob.read() + field = ?ob.read() if field.kind != Asn1Tag.Null: return err(Asn1Error.Incorrect) - field = ? ib.read() + field = ?ib.read() if field.kind != Asn1Tag.BitString: return err(Asn1Error.Incorrect) var vb = field.getBuffer() - field = ? vb.read() + field = ?vb.read() if field.kind != Asn1Tag.Sequence: return err(Asn1Error.Incorrect) var sb = field.getBuffer() - rawn = ? sb.read() + rawn = ?sb.read() if rawn.kind != Asn1Tag.Integer: return err(Asn1Error.Incorrect) - rawe = ? sb.read() + rawe = ?sb.read() if rawe.kind != Asn1Tag.Integer: return err(Asn1Error.Incorrect) @@ -575,16 +560,16 @@ proc init*(sig: var RsaSignature, data: openArray[byte]): Result[void, Asn1Error else: err(Asn1Error.Incorrect) -proc init*[T: RsaPKI](sospk: var T, - data: string): Result[void, Asn1Error] {.inline.} = +proc init*[T: RsaPKI](sospk: var T, data: string): Result[void, Asn1Error] {.inline.} = ## Initialize EC `private key`, `public key` or `scalar` ``sospk`` from ## hexadecimal string representation ``data``. ## ## Procedure returns ``Result[void, Asn1Status]``. sospk.init(ncrutils.fromHex(data)) -proc init*(t: typedesc[RsaPrivateKey], - data: openArray[byte]): RsaResult[RsaPrivateKey] = +proc init*( + t: typedesc[RsaPrivateKey], data: openArray[byte] +): RsaResult[RsaPrivateKey] = ## Initialize RSA private key from ASN.1 DER binary representation ``data`` ## and return constructed object. var res: RsaPrivateKey @@ -593,8 +578,7 @@ proc init*(t: typedesc[RsaPrivateKey], else: ok(res) -proc init*(t: typedesc[RsaPublicKey], - data: openArray[byte]): RsaResult[RsaPublicKey] = +proc init*(t: typedesc[RsaPublicKey], data: openArray[byte]): RsaResult[RsaPublicKey] = ## Initialize RSA public key from ASN.1 DER binary representation ``data`` ## and return constructed object. var res: RsaPublicKey @@ -603,8 +587,7 @@ proc init*(t: typedesc[RsaPublicKey], else: ok(res) -proc init*(t: typedesc[RsaSignature], - data: openArray[byte]): RsaResult[RsaSignature] = +proc init*(t: typedesc[RsaSignature], data: openArray[byte]): RsaResult[RsaSignature] = ## Initialize RSA signature from raw binary representation ``data`` and ## return constructed object. var res: RsaSignature @@ -631,14 +614,11 @@ proc `$`*(key: RsaPrivateKey): string = result.add("\nq = ") result.add(ncrutils.toHex(getArray(key.buffer, key.seck.q, key.seck.qlen))) result.add("\ndp = ") - result.add(ncrutils.toHex(getArray(key.buffer, key.seck.dp, - key.seck.dplen))) + result.add(ncrutils.toHex(getArray(key.buffer, key.seck.dp, key.seck.dplen))) result.add("\ndq = ") - result.add(ncrutils.toHex(getArray(key.buffer, key.seck.dq, - key.seck.dqlen))) + result.add(ncrutils.toHex(getArray(key.buffer, key.seck.dq, key.seck.dqlen))) result.add("\niq = ") - result.add(ncrutils.toHex(getArray(key.buffer, key.seck.iq, - key.seck.iqlen))) + result.add(ncrutils.toHex(getArray(key.buffer, key.seck.iq, key.seck.iqlen))) result.add("\npre = ") result.add(ncrutils.toHex(getArray(key.buffer, key.pexp, key.pexplen))) result.add("\nm = ") @@ -684,22 +664,37 @@ proc `==`*(a, b: RsaPrivateKey): bool = else: if a.seck.nBitlen == b.seck.nBitlen: 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), - getArray(b.buffer, b.seck.q, b.seck.qlen)) - let r3 = CT.isEqual(getArray(a.buffer, a.seck.dp, a.seck.dplen), - getArray(b.buffer, b.seck.dp, b.seck.dplen)) - let r4 = CT.isEqual(getArray(a.buffer, a.seck.dq, a.seck.dqlen), - getArray(b.buffer, b.seck.dq, b.seck.dqlen)) - let r5 = CT.isEqual(getArray(a.buffer, a.seck.iq, a.seck.iqlen), - getArray(b.buffer, b.seck.iq, b.seck.iqlen)) - let r6 = CT.isEqual(getArray(a.buffer, a.pexp, a.pexplen), - getArray(b.buffer, b.pexp, b.pexplen)) - let r7 = CT.isEqual(getArray(a.buffer, a.pubk.n, a.pubk.nlen), - getArray(b.buffer, b.pubk.n, b.pubk.nlen)) - let r8 = CT.isEqual(getArray(a.buffer, a.pubk.e, a.pubk.elen), - getArray(b.buffer, b.pubk.e, b.pubk.elen)) + 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), + getArray(b.buffer, b.seck.q, b.seck.qlen), + ) + let r3 = CT.isEqual( + getArray(a.buffer, a.seck.dp, a.seck.dplen), + getArray(b.buffer, b.seck.dp, b.seck.dplen), + ) + let r4 = CT.isEqual( + getArray(a.buffer, a.seck.dq, a.seck.dqlen), + getArray(b.buffer, b.seck.dq, b.seck.dqlen), + ) + let r5 = CT.isEqual( + getArray(a.buffer, a.seck.iq, a.seck.iqlen), + getArray(b.buffer, b.seck.iq, b.seck.iqlen), + ) + let r6 = CT.isEqual( + getArray(a.buffer, a.pexp, a.pexplen), getArray(b.buffer, b.pexp, b.pexplen) + ) + let r7 = CT.isEqual( + getArray(a.buffer, a.pubk.n, a.pubk.nlen), + getArray(b.buffer, b.pubk.n, b.pubk.nlen), + ) + let r8 = CT.isEqual( + getArray(a.buffer, a.pubk.e, a.pubk.elen), + getArray(b.buffer, b.pubk.e, b.pubk.elen), + ) r1 and r2 and r3 and r4 and r5 and r6 and r7 and r8 else: true @@ -737,14 +732,17 @@ proc `==`*(a, b: RsaPublicKey): bool = elif isNil(b) and (not isNil(a)): false else: - let r1 = CT.isEqual(getArray(a.buffer, a.key.n, a.key.nlen), - getArray(b.buffer, b.key.n, b.key.nlen)) - let r2 = CT.isEqual(getArray(a.buffer, a.key.e, a.key.elen), - getArray(b.buffer, b.key.e, b.key.elen)) + let r1 = CT.isEqual( + getArray(a.buffer, a.key.n, a.key.nlen), getArray(b.buffer, b.key.n, b.key.nlen) + ) + let r2 = CT.isEqual( + getArray(a.buffer, a.key.e, a.key.elen), getArray(b.buffer, b.key.e, b.key.elen) + ) (r1 and r2) -proc sign*[T: byte|char](key: RsaPrivateKey, - message: openArray[T]): RsaResult[RsaSignature] {.gcsafe.} = +proc sign*[T: byte | char]( + key: RsaPrivateKey, message: openArray[T] +): RsaResult[RsaSignature] {.gcsafe.} = ## Get RSA PKCS1.5 signature of data ``message`` using SHA256 and private ## key ``key``. if isNil(key): @@ -763,16 +761,16 @@ proc sign*[T: byte|char](key: RsaPrivateKey, kv.update(addr hc.vtable, nil, 0) kv.out(addr hc.vtable, addr hash[0]) var oid = RsaOidSha256 - let implRes = impl(addr oid[0], - addr hash[0], uint(len(hash)), - addr key.seck, addr res.buffer[0]) + let implRes = + impl(addr oid[0], addr hash[0], uint(len(hash)), addr key.seck, addr res.buffer[0]) if implRes == 0: err(RsaSignatureError) else: ok(res) -proc verify*[T: byte|char](sig: RsaSignature, message: openArray[T], - pubkey: RsaPublicKey): bool {.inline.} = +proc verify*[T: byte | char]( + sig: RsaSignature, message: openArray[T], pubkey: RsaPublicKey +): bool {.inline.} = ## Verify RSA signature ``sig`` using public key ``pubkey`` and data ## ``message``. ## @@ -792,8 +790,13 @@ proc verify*[T: byte|char](sig: RsaSignature, message: openArray[T], kv.update(addr hc.vtable, nil, 0) kv.out(addr hc.vtable, addr hash[0]) var oid = RsaOidSha256 - let res = impl(addr sig.buffer[0], uint(len(sig.buffer)), - addr oid[0], - uint(len(check)), addr pubkey.key, addr check[0]) + let res = impl( + addr sig.buffer[0], + uint(len(sig.buffer)), + addr oid[0], + uint(len(check)), + addr pubkey.key, + addr check[0], + ) if res == 1: result = equalMem(addr check[0], addr hash[0], len(hash)) diff --git a/libp2p/crypto/secp.nim b/libp2p/crypto/secp.nim index caf17c2eed..b79fe78da8 100644 --- a/libp2p/crypto/secp.nim +++ b/libp2p/crypto/secp.nim @@ -10,20 +10,15 @@ {.push raises: [].} import bearssl/rand -import - secp256k1, - stew/[byteutils, results], - nimcrypto/[hash, sha2] +import secp256k1, stew/[byteutils, results], nimcrypto/[hash, sha2] export sha2, results, rand const - SkRawPrivateKeySize* = 256 div 8 - ## Size of private key in octets (bytes) + SkRawPrivateKeySize* = 256 div 8 ## Size of private key in octets (bytes) SkRawSignatureSize* = SkRawPrivateKeySize * 2 + 1 ## Size of signature in octets (bytes) - SkRawPublicKeySize* = SkRawPrivateKeySize + 1 - ## Size of public key in octets (bytes) + SkRawPublicKeySize* = SkRawPrivateKeySize + 1 ## Size of public key in octets (bytes) # This is extremely confusing but it's to avoid.. confusion between Eth standard and Secp standard type @@ -56,31 +51,31 @@ template pubkey*(v: SkKeyPair): SkPublicKey = proc init*(key: var SkPrivateKey, data: openArray[byte]): SkResult[void] = ## Initialize Secp256k1 `private key` ``key`` from raw binary ## representation ``data``. - key = SkPrivateKey(? secp256k1.SkSecretKey.fromRaw(data)) + key = SkPrivateKey(?secp256k1.SkSecretKey.fromRaw(data)) ok() proc init*(key: var SkPrivateKey, data: string): SkResult[void] = ## Initialize Secp256k1 `private key` ``key`` from hexadecimal string ## representation ``data``. - key = SkPrivateKey(? secp256k1.SkSecretKey.fromHex(data)) + key = SkPrivateKey(?secp256k1.SkSecretKey.fromHex(data)) ok() proc init*(key: var SkPublicKey, data: openArray[byte]): SkResult[void] = ## Initialize Secp256k1 `public key` ``key`` from raw binary ## representation ``data``. - key = SkPublicKey(? secp256k1.SkPublicKey.fromRaw(data)) + key = SkPublicKey(?secp256k1.SkPublicKey.fromRaw(data)) ok() proc init*(key: var SkPublicKey, data: string): SkResult[void] = ## Initialize Secp256k1 `public key` ``key`` from hexadecimal string ## representation ``data``. - key = SkPublicKey(? secp256k1.SkPublicKey.fromHex(data)) + key = SkPublicKey(?secp256k1.SkPublicKey.fromHex(data)) ok() proc init*(sig: var SkSignature, data: openArray[byte]): SkResult[void] = ## Initialize Secp256k1 `signature` ``sig`` from raw binary ## representation ``data``. - sig = SkSignature(? secp256k1.SkSignature.fromDer(data)) + sig = SkSignature(?secp256k1.SkSignature.fromDer(data)) ok() proc init*(sig: var SkSignature, data: string): SkResult[void] = @@ -151,7 +146,7 @@ proc toBytes*(key: SkPrivateKey, data: var openArray[byte]): SkResult[int] = ## Procedure returns number of bytes (octets) needed to store ## Secp256k1 private key. if len(data) >= SkRawPrivateKeySize: - data[0..= SkRawPublicKeySize: - data[0.. 0: - patternSock - else: - DefaultIpSocketPattern - var patternForChild = if len(patternHandler) > 0: - patternHandler - else: - DefaultIpChildPattern + var patternForSocket = + if len(patternSock) > 0: patternSock else: DefaultIpSocketPattern + var patternForChild = + if len(patternHandler) > 0: patternHandler else: DefaultIpChildPattern else: - var patternForSocket = if len(patternSock) > 0: - patternSock - else: - DefaultUnixSocketPattern - var patternForChild = if len(patternHandler) > 0: - patternHandler - else: - DefaultUnixChildPattern + var patternForSocket = + if len(patternSock) > 0: patternSock else: DefaultUnixSocketPattern + var patternForChild = + if len(patternHandler) > 0: patternHandler else: DefaultUnixChildPattern api.flags = flags api.servers = newSeq[P2PServer]() @@ -734,13 +737,15 @@ proc newDaemonApi*(flags: set[P2PDaemonFlags] = {}, if len(hostAddresses) > 0: var opt = "-hostAddrs=" for i, address in hostAddresses: - if i > 0: opt.add "," + if i > 0: + opt.add "," opt.add $address args.add(opt) if len(announcedAddresses) > 0: var opt = "-announceAddrs=" for i, address in announcedAddresses: - if i > 0: opt.add "," + if i > 0: + opt.add "," opt.add $address args.add(opt) args.add("-noise=true") @@ -755,14 +760,12 @@ proc newDaemonApi*(flags: set[P2PDaemonFlags] = {}, # Starting daemon process # echo "Starting ", cmd, " ", args.join(" ") - api.process = - exceptionToAssert: - startProcess(cmd, "", args, env, {poParentStreams}) + api.process = exceptionToAssert: + startProcess(cmd, "", args, env, {poParentStreams}) # Waiting until daemon will not be bound to control socket. while true: if not api.process.running(): - raise newException(DaemonLocalError, - "Daemon executable could not be started!") + raise newException(DaemonLocalError, "Daemon executable could not be started!") let res = await socketExists(api.address) if res: break @@ -822,8 +825,9 @@ template withMessage(m, body: untyped): untyped = else: body -proc transactMessage(transp: StreamTransport, - pb: ProtoBuffer): Future[ProtoBuffer] {.async.} = +proc transactMessage( + transp: StreamTransport, pb: ProtoBuffer +): Future[ProtoBuffer] {.async.} = let length = pb.getLen() let res = await transp.write(pb.getPtr(), length) if res != length: @@ -833,8 +837,7 @@ proc transactMessage(transp: StreamTransport, raise newException(DaemonLocalError, "Incorrect or empty message received!") result = initProtoBuffer(message) -proc getPeerInfo(pb: ProtoBuffer): PeerInfo - {.raises: [DaemonLocalError].} = +proc getPeerInfo(pb: ProtoBuffer): PeerInfo {.raises: [DaemonLocalError].} = ## Get PeerInfo object from ``pb``. result.addresses = newSeq[MultiAddress]() if pb.getRequiredField(1, result.peer).isErr(): @@ -847,7 +850,7 @@ proc identity*(api: DaemonAPI): Future[PeerInfo] {.async.} = var transp = await api.newConnection() try: var pb = await transactMessage(transp, requestIdentity()) - pb.withMessage() do: + pb.withMessage: var res: seq[byte] if pb.getRequiredField(ResponseType.IDENTITY.int, res).isOk(): var resPb = initProtoBuffer(res) @@ -855,15 +858,14 @@ proc identity*(api: DaemonAPI): Future[PeerInfo] {.async.} = finally: await api.closeConnection(transp) -proc connect*(api: DaemonAPI, peer: PeerId, - addresses: seq[MultiAddress], - timeout = 0) {.async.} = +proc connect*( + api: DaemonAPI, peer: PeerId, addresses: seq[MultiAddress], timeout = 0 +) {.async.} = ## Connect to remote peer with id ``peer`` and addresses ``addresses``. var transp = await api.newConnection() try: - var pb = await transp.transactMessage(requestConnect(peer, addresses, - timeout)) - pb.withMessage() do: + var pb = await transp.transactMessage(requestConnect(peer, addresses, timeout)) + pb.withMessage: discard except CatchableError: await api.closeConnection(transp) @@ -873,22 +875,21 @@ proc disconnect*(api: DaemonAPI, peer: PeerId) {.async.} = var transp = await api.newConnection() try: var pb = await transp.transactMessage(requestDisconnect(peer)) - pb.withMessage() do: + pb.withMessage: discard finally: await api.closeConnection(transp) -proc openStream*(api: DaemonAPI, peer: PeerId, - protocols: seq[string], - timeout = 0): Future[P2PStream] {.async.} = +proc openStream*( + api: DaemonAPI, peer: PeerId, protocols: seq[string], timeout = 0 +): Future[P2PStream] {.async.} = ## Open new stream to peer ``peer`` using one of the protocols in ## ``protocols``. Returns ``StreamTransport`` for the stream. var transp = await api.newConnection() var stream = new P2PStream try: - var pb = await transp.transactMessage(requestStreamOpen(peer, protocols, - timeout)) - pb.withMessage() do: + var pb = await transp.transactMessage(requestStreamOpen(peer, protocols, timeout)) + pb.withMessage: var res: seq[byte] if pb.getRequiredField(ResponseType.STREAMINFO.int, res).isOk(): let resPb = initProtoBuffer(res) @@ -924,8 +925,9 @@ proc streamHandler(server: StreamServer, transp: StreamTransport) {.async.} = if not isNil(handler): asyncSpawn handler(api, stream) -proc addHandler*(api: DaemonAPI, protocols: seq[string], - handler: P2PStreamCallback) {.async, raises: [LPError].} = +proc addHandler*( + api: DaemonAPI, protocols: seq[string], handler: P2PStreamCallback +) {.async, raises: [LPError].} = ## Add stream handler ``handler`` for set of protocols ``protocols``. var transp = await api.newConnection() let maddress = await getSocket(api.pattern, addr api.ucounter) @@ -934,9 +936,8 @@ proc addHandler*(api: DaemonAPI, protocols: seq[string], for item in protocols: api.handlers[item] = handler server.start() - var pb = await transp.transactMessage(requestStreamHandler(maddress, - protocols)) - pb.withMessage() do: + var pb = await transp.transactMessage(requestStreamHandler(maddress, protocols)) + pb.withMessage: api.servers.add(P2PServer(server: server, address: maddress)) except CatchableError as exc: for item in protocols: @@ -953,7 +954,7 @@ proc listPeers*(api: DaemonAPI): Future[seq[PeerInfo]] {.async.} = var transp = await api.newConnection() try: var pb = await transp.transactMessage(requestListPeers()) - pb.withMessage() do: + pb.withMessage: result = newSeq[PeerInfo]() var ress: seq[seq[byte]] if pb.getRequiredRepeatedField(ResponseType.PEERINFO.int, ress).isOk(): @@ -963,13 +964,12 @@ proc listPeers*(api: DaemonAPI): Future[seq[PeerInfo]] {.async.} = finally: await api.closeConnection(transp) -proc cmTagPeer*(api: DaemonAPI, peer: PeerId, tag: string, - weight: int) {.async.} = +proc cmTagPeer*(api: DaemonAPI, peer: PeerId, tag: string, weight: int) {.async.} = ## Tag peer with id ``peer`` using ``tag`` and ``weight``. var transp = await api.newConnection() try: var pb = await transp.transactMessage(requestCMTagPeer(peer, tag, weight)) - withMessage(pb) do: + withMessage(pb): discard finally: await api.closeConnection(transp) @@ -979,7 +979,7 @@ proc cmUntagPeer*(api: DaemonAPI, peer: PeerId, tag: string) {.async.} = var transp = await api.newConnection() try: var pb = await transp.transactMessage(requestCMUntagPeer(peer, tag)) - withMessage(pb) do: + withMessage(pb): discard finally: await api.closeConnection(transp) @@ -989,37 +989,34 @@ proc cmTrimPeers*(api: DaemonAPI) {.async.} = var transp = await api.newConnection() try: var pb = await transp.transactMessage(requestCMTrim()) - withMessage(pb) do: + withMessage(pb): discard finally: await api.closeConnection(transp) -proc dhtGetSinglePeerInfo(pb: ProtoBuffer): PeerInfo - {.raises: [DaemonLocalError].} = +proc dhtGetSinglePeerInfo(pb: ProtoBuffer): PeerInfo {.raises: [DaemonLocalError].} = var res: seq[byte] if pb.getRequiredField(2, res).isOk(): result = initProtoBuffer(res).getPeerInfo() else: raise newException(DaemonLocalError, "Missing required field `peer`!") -proc dhtGetSingleValue(pb: ProtoBuffer): seq[byte] - {.raises: [DaemonLocalError].} = +proc dhtGetSingleValue(pb: ProtoBuffer): seq[byte] {.raises: [DaemonLocalError].} = result = newSeq[byte]() if pb.getRequiredField(3, result).isErr(): raise newException(DaemonLocalError, "Missing field `value`!") -proc dhtGetSinglePublicKey(pb: ProtoBuffer): PublicKey - {.raises: [DaemonLocalError].} = +proc dhtGetSinglePublicKey(pb: ProtoBuffer): PublicKey {.raises: [DaemonLocalError].} = if pb.getRequiredField(3, result).isErr(): raise newException(DaemonLocalError, "Missing field `value`!") -proc dhtGetSinglePeerId(pb: ProtoBuffer): PeerId - {.raises: [DaemonLocalError].} = +proc dhtGetSinglePeerId(pb: ProtoBuffer): PeerId {.raises: [DaemonLocalError].} = if pb.getRequiredField(3, result).isErr(): raise newException(DaemonLocalError, "Missing field `value`!") -proc enterDhtMessage(pb: ProtoBuffer, rt: DHTResponseType): ProtoBuffer - {.inline, raises: [DaemonLocalError].} = +proc enterDhtMessage( + pb: ProtoBuffer, rt: DHTResponseType +): ProtoBuffer {.inline, raises: [DaemonLocalError].} = var dhtResponse: seq[byte] if pb.getRequiredField(ResponseType.DHT.int, dhtResponse).isOk(): var pbDhtResponse = initProtoBuffer(dhtResponse) @@ -1037,16 +1034,18 @@ proc enterDhtMessage(pb: ProtoBuffer, rt: DHTResponseType): ProtoBuffer else: raise newException(DaemonLocalError, "Wrong message type!") -proc enterPsMessage(pb: ProtoBuffer): ProtoBuffer - {.inline, raises: [DaemonLocalError].} = +proc enterPsMessage( + pb: ProtoBuffer +): ProtoBuffer {.inline, raises: [DaemonLocalError].} = var res: seq[byte] if pb.getRequiredField(ResponseType.PUBSUB.int, res).isErr(): raise newException(DaemonLocalError, "Wrong message type!") initProtoBuffer(res) -proc getDhtMessageType(pb: ProtoBuffer): DHTResponseType - {.inline, raises: [DaemonLocalError].} = +proc getDhtMessageType( + pb: ProtoBuffer +): DHTResponseType {.inline, raises: [DaemonLocalError].} = var dtype: uint if pb.getRequiredField(1, dtype).isErr(): raise newException(DaemonLocalError, "Missing required DHT field `type`!") @@ -1057,8 +1056,9 @@ proc getDhtMessageType(pb: ProtoBuffer): DHTResponseType else: raise newException(DaemonLocalError, "Wrong DHT answer type!") -proc dhtFindPeer*(api: DaemonAPI, peer: PeerId, - timeout = 0): Future[PeerInfo] {.async.} = +proc dhtFindPeer*( + api: DaemonAPI, peer: PeerId, timeout = 0 +): Future[PeerInfo] {.async.} = ## Find peer with id ``peer`` and return peer information ``PeerInfo``. ## ## You can specify timeout for DHT request with ``timeout`` value. ``0`` value @@ -1066,13 +1066,14 @@ proc dhtFindPeer*(api: DaemonAPI, peer: PeerId, var transp = await api.newConnection() try: var pb = await transp.transactMessage(requestDHTFindPeer(peer, timeout)) - withMessage(pb) do: + withMessage(pb): result = pb.enterDhtMessage(DHTResponseType.VALUE).dhtGetSinglePeerInfo() finally: await api.closeConnection(transp) -proc dhtGetPublicKey*(api: DaemonAPI, peer: PeerId, - timeout = 0): Future[PublicKey] {.async.} = +proc dhtGetPublicKey*( + api: DaemonAPI, peer: PeerId, timeout = 0 +): Future[PublicKey] {.async.} = ## Get peer's public key from peer with id ``peer``. ## ## You can specify timeout for DHT request with ``timeout`` value. ``0`` value @@ -1080,13 +1081,14 @@ proc dhtGetPublicKey*(api: DaemonAPI, peer: PeerId, var transp = await api.newConnection() try: var pb = await transp.transactMessage(requestDHTGetPublicKey(peer, timeout)) - withMessage(pb) do: + withMessage(pb): result = pb.enterDhtMessage(DHTResponseType.VALUE).dhtGetSinglePublicKey() finally: await api.closeConnection(transp) -proc dhtGetValue*(api: DaemonAPI, key: string, - timeout = 0): Future[seq[byte]] {.async.} = +proc dhtGetValue*( + api: DaemonAPI, key: string, timeout = 0 +): Future[seq[byte]] {.async.} = ## Get value associated with ``key``. ## ## You can specify timeout for DHT request with ``timeout`` value. ``0`` value @@ -1094,22 +1096,22 @@ proc dhtGetValue*(api: DaemonAPI, key: string, var transp = await api.newConnection() try: var pb = await transp.transactMessage(requestDHTGetValue(key, timeout)) - withMessage(pb) do: + withMessage(pb): result = pb.enterDhtMessage(DHTResponseType.VALUE).dhtGetSingleValue() finally: await api.closeConnection(transp) -proc dhtPutValue*(api: DaemonAPI, key: string, value: seq[byte], - timeout = 0) {.async.} = +proc dhtPutValue*( + api: DaemonAPI, key: string, value: seq[byte], timeout = 0 +) {.async.} = ## Associate ``value`` with ``key``. ## ## You can specify timeout for DHT request with ``timeout`` value. ``0`` value ## means no timeout. var transp = await api.newConnection() try: - var pb = await transp.transactMessage(requestDHTPutValue(key, value, - timeout)) - withMessage(pb) do: + var pb = await transp.transactMessage(requestDHTPutValue(key, value, timeout)) + withMessage(pb): discard finally: await api.closeConnection(transp) @@ -1122,13 +1124,14 @@ proc dhtProvide*(api: DaemonAPI, cid: Cid, timeout = 0) {.async.} = var transp = await api.newConnection() try: var pb = await transp.transactMessage(requestDHTProvide(cid, timeout)) - withMessage(pb) do: + withMessage(pb): discard finally: await api.closeConnection(transp) -proc dhtFindPeersConnectedToPeer*(api: DaemonAPI, peer: PeerId, - timeout = 0): Future[seq[PeerInfo]] {.async.} = +proc dhtFindPeersConnectedToPeer*( + api: DaemonAPI, peer: PeerId, timeout = 0 +): Future[seq[PeerInfo]] {.async.} = ## Find peers which are connected to peer with id ``peer``. ## ## You can specify timeout for DHT request with ``timeout`` value. ``0`` value @@ -1138,7 +1141,7 @@ proc dhtFindPeersConnectedToPeer*(api: DaemonAPI, peer: PeerId, try: let spb = requestDHTFindPeersConnectedToPeer(peer, timeout) var pb = await transp.transactMessage(spb) - withMessage(pb) do: + withMessage(pb): discard pb.enterDhtMessage(DHTResponseType.BEGIN) while true: var message = await transp.recvMessage() @@ -1152,8 +1155,9 @@ proc dhtFindPeersConnectedToPeer*(api: DaemonAPI, peer: PeerId, finally: await api.closeConnection(transp) -proc dhtGetClosestPeers*(api: DaemonAPI, key: string, - timeout = 0): Future[seq[PeerId]] {.async.} = +proc dhtGetClosestPeers*( + api: DaemonAPI, key: string, timeout = 0 +): Future[seq[PeerId]] {.async.} = ## Get closest peers for ``key``. ## ## You can specify timeout for DHT request with ``timeout`` value. ``0`` value @@ -1163,7 +1167,7 @@ proc dhtGetClosestPeers*(api: DaemonAPI, key: string, try: let spb = requestDHTGetClosestPeers(key, timeout) var pb = await transp.transactMessage(spb) - withMessage(pb) do: + withMessage(pb): discard pb.enterDhtMessage(DHTResponseType.BEGIN) while true: var message = await transp.recvMessage() @@ -1177,8 +1181,9 @@ proc dhtGetClosestPeers*(api: DaemonAPI, key: string, finally: await api.closeConnection(transp) -proc dhtFindProviders*(api: DaemonAPI, cid: Cid, count: uint32, - timeout = 0): Future[seq[PeerInfo]] {.async.} = +proc dhtFindProviders*( + api: DaemonAPI, cid: Cid, count: uint32, timeout = 0 +): Future[seq[PeerInfo]] {.async.} = ## Get ``count`` providers for content with id ``cid``. ## ## You can specify timeout for DHT request with ``timeout`` value. ``0`` value @@ -1188,7 +1193,7 @@ proc dhtFindProviders*(api: DaemonAPI, cid: Cid, count: uint32, try: let spb = requestDHTFindProviders(cid, count, timeout) var pb = await transp.transactMessage(spb) - withMessage(pb) do: + withMessage(pb): discard pb.enterDhtMessage(DHTResponseType.BEGIN) while true: var message = await transp.recvMessage() @@ -1202,8 +1207,9 @@ proc dhtFindProviders*(api: DaemonAPI, cid: Cid, count: uint32, finally: await api.closeConnection(transp) -proc dhtSearchValue*(api: DaemonAPI, key: string, - timeout = 0): Future[seq[seq[byte]]] {.async.} = +proc dhtSearchValue*( + api: DaemonAPI, key: string, timeout = 0 +): Future[seq[seq[byte]]] {.async.} = ## Search for value with ``key``, return list of values found. ## ## You can specify timeout for DHT request with ``timeout`` value. ``0`` value @@ -1212,7 +1218,7 @@ proc dhtSearchValue*(api: DaemonAPI, key: string, var list = newSeq[seq[byte]]() try: var pb = await transp.transactMessage(requestDHTSearchValue(key, timeout)) - withMessage(pb) do: + withMessage(pb): discard pb.enterDhtMessage(DHTResponseType.BEGIN) while true: var message = await transp.recvMessage() @@ -1231,7 +1237,7 @@ proc pubsubGetTopics*(api: DaemonAPI): Future[seq[string]] {.async.} = var transp = await api.newConnection() try: var pb = await transp.transactMessage(requestPSGetTopics()) - withMessage(pb) do: + withMessage(pb): let innerPb = pb.enterPsMessage() var topics = newSeq[string]() discard innerPb.getRepeatedField(1, topics) @@ -1239,14 +1245,13 @@ proc pubsubGetTopics*(api: DaemonAPI): Future[seq[string]] {.async.} = finally: await api.closeConnection(transp) -proc pubsubListPeers*(api: DaemonAPI, - topic: string): Future[seq[PeerId]] {.async.} = +proc pubsubListPeers*(api: DaemonAPI, topic: string): Future[seq[PeerId]] {.async.} = ## Get list of peers we are connected to and which also subscribed to topic ## ``topic``. var transp = await api.newConnection() try: var pb = await transp.transactMessage(requestPSListPeers(topic)) - withMessage(pb) do: + withMessage(pb): var peer: PeerId let innerPb = pb.enterPsMessage() var peers = newSeq[seq[byte]]() @@ -1255,13 +1260,12 @@ proc pubsubListPeers*(api: DaemonAPI, finally: await api.closeConnection(transp) -proc pubsubPublish*(api: DaemonAPI, topic: string, - value: seq[byte]) {.async.} = +proc pubsubPublish*(api: DaemonAPI, topic: string, value: seq[byte]) {.async.} = ## Get list of peer identifiers which are subscribed to topic ``topic``. var transp = await api.newConnection() try: var pb = await transp.transactMessage(requestPSPublish(topic, value)) - withMessage(pb) do: + withMessage(pb): discard finally: await api.closeConnection(transp) @@ -1290,13 +1294,14 @@ proc pubsubLoop(api: DaemonAPI, ticket: PubsubTicket) {.async.} = await ticket.transp.join() break -proc pubsubSubscribe*(api: DaemonAPI, topic: string, - handler: P2PPubSubCallback): Future[PubsubTicket] {.async.} = +proc pubsubSubscribe*( + api: DaemonAPI, topic: string, handler: P2PPubSubCallback +): Future[PubsubTicket] {.async.} = ## Subscribe to topic ``topic``. var transp = await api.newConnection() try: var pb = await transp.transactMessage(requestPSSubscribe(topic)) - pb.withMessage() do: + pb.withMessage: var ticket = new PubsubTicket ticket.topic = topic ticket.handler = handler @@ -1314,7 +1319,7 @@ proc shortLog*(pinfo: PeerInfo): string = result.add($pinfo.peer.shortLog()) result.add("' Addresses: [") let length = len(pinfo.addresses) - for i in 0..``. +const libp2p_dump_dir* {.strdefine.} = "nim-libp2p" + ## default directory where all the dumps will be stored, if the path + ## relative it will be created in home directory. You can overload this path + ## using ``-d:libp2p_dump_dir=``. proc getTimestamp(): uint64 = ## This procedure is present because `stdlib.times` missing it. @@ -65,8 +71,7 @@ proc getTimedate(value: uint64): string = let time = initTime(int64(value div 1_000_000_000), value mod 1_000_000_000) time.format("yyyy-MM-dd HH:mm:ss'.'fffzzz") -proc dumpMessage*(conn: SecureConn, direction: FlowDirection, - data: openArray[byte]) = +proc dumpMessage*(conn: SecureConn, direction: FlowDirection, data: openArray[byte]) = ## Store unencrypted message ``data`` to dump file, all the metadata will be ## extracted from ``conn`` instance. var pb = initProtoBuffer(options = {WithVarintLength}) @@ -87,7 +92,7 @@ proc dumpMessage*(conn: SecureConn, direction: FlowDirection, # This is debugging procedure so it should not generate any exceptions, # and we going to return at every possible OS error. - if not(dirExists(dirName)): + if not (dirExists(dirName)): try: createDir(dirName) except CatchableError: @@ -153,13 +158,11 @@ iterator messages*(data: seq[byte]): Opt[ProtoMessage] = while offset < len(data): value = 0 size = 0 - let res = PB.getUVarint(data.toOpenArray(offset, len(data) - 1), - size, value) + let res = PB.getUVarint(data.toOpenArray(offset, len(data) - 1), size, value) if res.isOk(): if (value > 0'u64) and (value < uint64(len(data) - offset)): offset += size - yield decodeDumpMessage(data.toOpenArray(offset, - offset + int(value) - 1)) + yield decodeDumpMessage(data.toOpenArray(offset, offset + int(value) - 1)) # value is previously checked to be less then len(data) which is `int`. offset += int(value) else: @@ -179,10 +182,15 @@ proc dumpHex*(pbytes: openArray[byte], groupBy = 1, ascii = true): string = for k in 0 ..< groupBy: let ch = pbytes[offset + k] - ascii.add(if ord(ch) > 31 and ord(ch) < 127: char(ch) else: '.') + ascii.add( + if ord(ch) > 31 and ord(ch) < 127: + char(ch) + else: + '.' + ) let item = - case groupBy: + case groupBy of 1: toHex(pbytes[offset]) of 2: @@ -204,8 +212,7 @@ proc dumpHex*(pbytes: openArray[byte], groupBy = 1, ascii = true): string = res.add("\p") if (offset mod 16) != 0: - let spacesCount = ((16 - (offset mod 16)) div groupBy) * - (groupBy * 2 + 1) + 1 + let spacesCount = ((16 - (offset mod 16)) div groupBy) * (groupBy * 2 + 1) + 1 res = res & repeat(' ', spacesCount) res = res & ascii @@ -233,25 +240,30 @@ proc toString*(msg: ProtoMessage, dump = true): string = var res = getTimedate(msg.timestamp) let direction = case msg.direction - of Incoming: - " << " - of Outgoing: - " >> " - let address = - block: - let local = block: - msg.local.withValue(loc): "[" & $loc & "]" - else: "[LOCAL]" - let remote = block: - msg.remote.withValue(rem): "[" & $rem & "]" - else: "[REMOTE]" - local & direction & remote + of Incoming: " << " + of Outgoing: " >> " + let address = block: + let local = block: + msg.local.withValue(loc): + "[" & $loc & "]" + else: + "[LOCAL]" + let remote = block: + msg.remote.withValue(rem): + "[" & $rem & "]" + else: + "[REMOTE]" + local & direction & remote let seqid = block: - msg.seqID.withValue(seqid): "seqID = " & $seqid & " " - else: "" + msg.seqID.withValue(seqid): + "seqID = " & $seqid & " " + else: + "" let mtype = block: - msg.mtype.withValue(typ): "type = " & $typ & " " - else: "" + msg.mtype.withValue(typ): + "type = " & $typ & " " + else: + "" res.add(" ") res.add(address) res.add(" ") diff --git a/libp2p/dial.nim b/libp2p/dial.nim index 5d46cf5fc4..9d5012e321 100644 --- a/libp2p/dial.nim +++ b/libp2p/dial.nim @@ -11,22 +11,20 @@ import chronos import stew/results -import peerid, - stream/connection, - transports/transport +import peerid, stream/connection, transports/transport export results -type - Dial* = ref object of RootObj +type Dial* = ref object of RootObj method connect*( - self: Dial, - peerId: PeerId, - addrs: seq[MultiAddress], - forceDial = false, - reuseConnection = true, - dir = Direction.Out) {.async, base.} = + self: Dial, + peerId: PeerId, + addrs: seq[MultiAddress], + forceDial = false, + reuseConnection = true, + dir = Direction.Out, +) {.async, base.} = ## connect remote peer without negotiating ## a protocol ## @@ -34,18 +32,15 @@ method connect*( doAssert(false, "Not implemented!") method connect*( - self: Dial, - address: MultiAddress, - allowUnknownPeerId = false): Future[PeerId] {.async, base.} = + self: Dial, address: MultiAddress, allowUnknownPeerId = false +): Future[PeerId] {.async, base.} = ## Connects to a peer and retrieve its PeerId doAssert(false, "Not implemented!") method dial*( - self: Dial, - peerId: PeerId, - protos: seq[string], - ): Future[Connection] {.async, base.} = + self: Dial, peerId: PeerId, protos: seq[string] +): Future[Connection] {.async, base.} = ## create a protocol stream over an ## existing connection ## @@ -53,24 +48,22 @@ method dial*( doAssert(false, "Not implemented!") method dial*( - self: Dial, - peerId: PeerId, - addrs: seq[MultiAddress], - protos: seq[string], - forceDial = false): Future[Connection] {.async, base.} = + self: Dial, + peerId: PeerId, + addrs: seq[MultiAddress], + protos: seq[string], + forceDial = false, +): Future[Connection] {.async, base.} = ## create a protocol stream and establish ## a connection if one doesn't exist already ## doAssert(false, "Not implemented!") -method addTransport*( - self: Dial, - transport: Transport) {.base.} = +method addTransport*(self: Dial, transport: Transport) {.base.} = doAssert(false, "Not implemented!") method tryDial*( - self: Dial, - peerId: PeerId, - addrs: seq[MultiAddress]): Future[Opt[MultiAddress]] {.async, base.} = + self: Dial, peerId: PeerId, addrs: seq[MultiAddress] +): Future[Opt[MultiAddress]] {.async, base.} = doAssert(false, "Not implemented!") diff --git a/libp2p/dialer.nim b/libp2p/dialer.nim index ade169bdf3..ad3d7feb02 100644 --- a/libp2p/dialer.nim +++ b/libp2p/dialer.nim @@ -10,23 +10,22 @@ import std/tables import stew/results -import pkg/[chronos, - chronicles, - metrics] - -import dial, - peerid, - peerinfo, - peerstore, - multicodec, - muxers/muxer, - multistream, - connmanager, - stream/connection, - transports/transport, - nameresolving/nameresolver, - upgrademngrs/upgrade, - errors +import pkg/[chronos, chronicles, metrics] + +import + dial, + peerid, + peerinfo, + peerstore, + multicodec, + muxers/muxer, + multistream, + connmanager, + stream/connection, + transports/transport, + nameresolving/nameresolver, + upgrademngrs/upgrade, + errors export dial, errors, results @@ -49,15 +48,14 @@ type nameResolver: NameResolver proc dialAndUpgrade( - self: Dialer, - peerId: Opt[PeerId], - hostname: string, - address: MultiAddress, - dir = Direction.Out): - Future[Muxer] {.async.} = - + self: Dialer, + peerId: Opt[PeerId], + hostname: string, + address: MultiAddress, + dir = Direction.Out, +): Future[Muxer] {.async.} = for transport in self.transports: # for each transport - if transport.handles(address): # check if it can dial it + if transport.handles(address): # check if it can dial it trace "Dialing address", address, peerId = peerId.get(default(PeerId)), hostname let dialed = try: @@ -88,7 +86,8 @@ proc dialAndUpgrade( # If we failed to establish the connection through one transport, # we won't succeeded through another - no use in trying again await dialed.close() - debug "Connection upgrade failed", err = exc.msg, peerId = peerId.get(default(PeerId)) + debug "Connection upgrade failed", + err = exc.msg, peerId = peerId.get(default(PeerId)) if dialed.dir == Direction.Out: libp2p_failed_upgrades_outgoing.inc() else: @@ -103,13 +102,12 @@ proc dialAndUpgrade( return nil proc expandDnsAddr( - self: Dialer, - peerId: Opt[PeerId], - address: MultiAddress): Future[seq[(MultiAddress, Opt[PeerId])]] {.async.} = - - if not DNSADDR.matchPartial(address): return @[(address, peerId)] + self: Dialer, peerId: Opt[PeerId], address: MultiAddress +): Future[seq[(MultiAddress, Opt[PeerId])]] {.async.} = + if not DNSADDR.matchPartial(address): + return @[(address, peerId)] if isNil(self.nameResolver): - info "Can't resolve DNSADDR without NameResolver", ma=address + info "Can't resolve DNSADDR without NameResolver", ma = address return @[] let @@ -126,17 +124,13 @@ proc expandDnsAddr( let peerIdBytes = lastPart.protoArgument().tryGet() addrPeerId = PeerId.init(peerIdBytes).tryGet() - result.add((resolvedAddress[0..^2].tryGet(), Opt.some(addrPeerId))) + result.add((resolvedAddress[0 ..^ 2].tryGet(), Opt.some(addrPeerId))) else: result.add((resolvedAddress, peerId)) proc dialAndUpgrade( - self: Dialer, - peerId: Opt[PeerId], - addrs: seq[MultiAddress], - dir = Direction.Out): - Future[Muxer] {.async.} = - + self: Dialer, peerId: Opt[PeerId], addrs: seq[MultiAddress], dir = Direction.Out +): Future[Muxer] {.async.} = debug "Dialing peer", peerId = peerId.get(default(PeerId)), addrs for rawAddress in addrs: @@ -148,8 +142,10 @@ proc dialAndUpgrade( let hostname = expandedAddress.getHostname() resolvedAddresses = - if isNil(self.nameResolver): @[expandedAddress] - else: await self.nameResolver.resolveMAddress(expandedAddress) + if isNil(self.nameResolver): + @[expandedAddress] + else: + await self.nameResolver.resolveMAddress(expandedAddress) for resolvedAddress in resolvedAddresses: result = await self.dialAndUpgrade(addrPeerId, hostname, resolvedAddress, dir) @@ -165,13 +161,13 @@ proc tryReusingConnection(self: Dialer, peerId: PeerId): Opt[Muxer] = return Opt.some(muxer) proc internalConnect( - self: Dialer, - peerId: Opt[PeerId], - addrs: seq[MultiAddress], - forceDial: bool, - reuseConnection = true, - dir = Direction.Out): - Future[Muxer] {.async.} = + self: Dialer, + peerId: Opt[PeerId], + addrs: seq[MultiAddress], + forceDial: bool, + reuseConnection = true, + dir = Direction.Out, +): Future[Muxer] {.async.} = if Opt.some(self.localPeerId) == peerId: raise newException(CatchableError, "can't dial self!") @@ -200,7 +196,7 @@ proc internalConnect( self.connManager.storeMuxer(muxed) await self.peerStore.identify(muxed) except CatchableError as exc: - trace "Failed to finish outgoung upgrade", err=exc.msg + trace "Failed to finish outgoung upgrade", err = exc.msg await muxed.close() raise exc @@ -210,12 +206,13 @@ proc internalConnect( lock.release() method connect*( - self: Dialer, - peerId: PeerId, - addrs: seq[MultiAddress], - forceDial = false, - reuseConnection = true, - dir = Direction.Out) {.async.} = + self: Dialer, + peerId: PeerId, + addrs: seq[MultiAddress], + forceDial = false, + reuseConnection = true, + dir = Direction.Out, +) {.async.} = ## connect remote peer without negotiating ## a protocol ## @@ -223,32 +220,30 @@ method connect*( if self.connManager.connCount(peerId) > 0 and reuseConnection: return - discard await self.internalConnect(Opt.some(peerId), addrs, forceDial, reuseConnection, dir) + discard + await self.internalConnect(Opt.some(peerId), addrs, forceDial, reuseConnection, dir) method connect*( - self: Dialer, - address: MultiAddress, - allowUnknownPeerId = false): Future[PeerId] {.async.} = + self: Dialer, address: MultiAddress, allowUnknownPeerId = false +): Future[PeerId] {.async.} = ## Connects to a peer and retrieve its PeerId parseFullAddress(address).toOpt().withValue(fullAddress): - return (await self.internalConnect( - Opt.some(fullAddress[0]), - @[fullAddress[1]], - false)).connection.peerId + return ( + await self.internalConnect(Opt.some(fullAddress[0]), @[fullAddress[1]], false) + ).connection.peerId if allowUnknownPeerId == false: - raise newException(DialFailedError, "Address without PeerID and unknown peer id disabled!") + raise newException( + DialFailedError, "Address without PeerID and unknown peer id disabled!" + ) - return (await self.internalConnect( - Opt.none(PeerId), - @[address], - false)).connection.peerId + return + (await self.internalConnect(Opt.none(PeerId), @[address], false)).connection.peerId proc negotiateStream( - self: Dialer, - conn: Connection, - protos: seq[string]): Future[Connection] {.async.} = + self: Dialer, conn: Connection, protos: seq[string] +): Future[Connection] {.async.} = trace "Negotiating stream", conn, protos let selected = await MultistreamSelect.select(conn, protos) if not protos.contains(selected): @@ -258,9 +253,8 @@ proc negotiateStream( return conn method tryDial*( - self: Dialer, - peerId: PeerId, - addrs: seq[MultiAddress]): Future[Opt[MultiAddress]] {.async.} = + self: Dialer, peerId: PeerId, addrs: seq[MultiAddress] +): Future[Opt[MultiAddress]] {.async.} = ## Create a protocol stream in order to check ## if a connection is possible. ## Doesn't use the Connection Manager to save it. @@ -279,9 +273,8 @@ method tryDial*( raise newException(DialFailedError, exc.msg) method dial*( - self: Dialer, - peerId: PeerId, - protos: seq[string]): Future[Connection] {.async.} = + self: Dialer, peerId: PeerId, protos: seq[string] +): Future[Connection] {.async.} = ## create a protocol stream over an ## existing connection ## @@ -294,11 +287,12 @@ method dial*( return await self.negotiateStream(stream, protos) method dial*( - self: Dialer, - peerId: PeerId, - addrs: seq[MultiAddress], - protos: seq[string], - forceDial = false): Future[Connection] {.async.} = + self: Dialer, + peerId: PeerId, + addrs: seq[MultiAddress], + protos: seq[string], + forceDial = false, +): Future[Connection] {.async.} = ## create a protocol stream and establish ## a connection if one doesn't exist already ## @@ -308,10 +302,10 @@ method dial*( stream: Connection proc cleanup() {.async.} = - if not(isNil(stream)): + if not (isNil(stream)): await stream.closeWithEOF() - if not(isNil(conn)): + if not (isNil(conn)): await conn.close() try: @@ -321,8 +315,7 @@ method dial*( stream = await self.connManager.getStream(conn) if isNil(stream): - raise newException(DialFailedError, - "Couldn't get muxed stream") + raise newException(DialFailedError, "Couldn't get muxed stream") return await self.negotiateStream(stream, protos) except CancelledError as exc: @@ -338,15 +331,17 @@ method addTransport*(self: Dialer, t: Transport) = self.transports &= t proc new*( - T: type Dialer, - localPeerId: PeerId, - connManager: ConnManager, - peerStore: PeerStore, - transports: seq[Transport], - nameResolver: NameResolver = nil): Dialer = - - T(localPeerId: localPeerId, + T: type Dialer, + localPeerId: PeerId, + connManager: ConnManager, + peerStore: PeerStore, + transports: seq[Transport], + nameResolver: NameResolver = nil, +): Dialer = + T( + localPeerId: localPeerId, connManager: connManager, transports: transports, peerStore: peerStore, - nameResolver: nameResolver) + nameResolver: nameResolver, + ) diff --git a/libp2p/discovery/discoverymngr.nim b/libp2p/discovery/discoverymngr.nim index bff1c9bd5c..87a650d967 100644 --- a/libp2p/discovery/discoverymngr.nim +++ b/libp2p/discovery/discoverymngr.nim @@ -33,12 +33,13 @@ proc ofType*[T](f: BaseAttr, _: type[T]): bool = proc to*[T](f: BaseAttr, _: type[T]): T = Attribute[T](f).value -proc add*[T](pa: var PeerAttributes, - value: T) = - pa.attributes.add(Attribute[T]( +proc add*[T](pa: var PeerAttributes, value: T) = + pa.attributes.add( + Attribute[T]( value: value, comparator: proc(f: BaseAttr, c: BaseAttr): bool = f.ofType(T) and c.ofType(T) and f.to(T) == c.to(T) + , ) ) @@ -58,7 +59,8 @@ proc `{}`*[T](pa: PeerAttributes, t: typedesc[T]): Opt[T] = Opt.none(T) proc `[]`*[T](pa: PeerAttributes, t: typedesc[T]): T {.raises: [KeyError].} = - pa{T}.valueOr: raise newException(KeyError, "Attritute not found") + pa{T}.valueOr: + raise newException(KeyError, "Attritute not found") proc match*(pa, candidate: PeerAttributes): bool = for f in pa.attributes: @@ -101,7 +103,7 @@ type proc add*(dm: DiscoveryManager, di: DiscoveryInterface) = dm.interfaces &= di - di.onPeerFound = proc (pa: PeerAttributes) = + di.onPeerFound = proc(pa: PeerAttributes) = for query in dm.queries: if query.attr.match(pa): try: @@ -139,8 +141,10 @@ template forEach*(query: DiscoveryQuery, code: untyped) = proc forEachInternal(q: DiscoveryQuery) {.async.} = while true: let peer {.inject.} = - try: await q.getPeer() - except DiscoveryFinished: return + try: + await q.getPeer() + except DiscoveryFinished: + return code asyncSpawn forEachInternal(query) @@ -148,13 +152,15 @@ template forEach*(query: DiscoveryQuery, code: untyped) = proc stop*(query: DiscoveryQuery) = query.finished = true for r in query.futs: - if not r.finished(): r.cancel() + if not r.finished(): + r.cancel() proc stop*(dm: DiscoveryManager) = for q in dm.queries: q.stop() for i in dm.interfaces: - if isNil(i.advertiseLoop): continue + if isNil(i.advertiseLoop): + continue i.advertiseLoop.cancel() proc getPeer*(query: DiscoveryQuery): Future[PeerAttributes] {.async.} = diff --git a/libp2p/discovery/rendezvousinterface.nim b/libp2p/discovery/rendezvousinterface.nim index e857466cd2..d276a772ea 100644 --- a/libp2p/discovery/rendezvousinterface.nim +++ b/libp2p/discovery/rendezvousinterface.nim @@ -10,9 +10,7 @@ {.push raises: [].} import chronos -import ./discoverymngr, - ../protocols/rendezvous, - ../peerid +import ./discoverymngr, ../protocols/rendezvous, ../peerid type RendezVousInterface* = ref object of DiscoveryInterface @@ -70,9 +68,11 @@ method advertise*(self: RendezVousInterface) {.async.} = await sleepAsync(self.timeToAdvertise) or self.advertisementUpdated.wait() -proc new*(T: typedesc[RendezVousInterface], - rdv: RendezVous, - ttr: Duration = 1.minutes, - tta: Duration = 1.minutes, - ttl: Duration = MinimumDuration): RendezVousInterface = +proc new*( + T: typedesc[RendezVousInterface], + rdv: RendezVous, + ttr: Duration = 1.minutes, + tta: Duration = 1.minutes, + ttl: Duration = MinimumDuration, +): RendezVousInterface = T(rdv: rdv, timeToRequest: ttr, timeToAdvertise: tta, ttl: ttl) diff --git a/libp2p/errors.nim b/libp2p/errors.nim index 2eadd372fa..543856430f 100644 --- a/libp2p/errors.nim +++ b/libp2p/errors.nim @@ -24,23 +24,25 @@ macro checkFutures*[F](futs: seq[F], exclude: untyped = []): untyped = let nexclude = exclude.len case nexclude of 0: - quote do: + quote: for res in `futs`: if res.failed: let exc = res.readError() # We still don't abort but warn - debug "A future has failed, enable trace logging for details", error = exc.name - trace "Exception message", msg= exc.msg, stack = getStackTrace() + debug "A future has failed, enable trace logging for details", + error = exc.name + trace "Exception message", msg = exc.msg, stack = getStackTrace() else: - quote do: + quote: for res in `futs`: block check: if res.failed: let exc = res.readError() - for i in 0..<`nexclude`: + for i in 0 ..< `nexclude`: if exc of `exclude`[i]: - trace "A future has failed", error=exc.name, msg=exc.msg + trace "A future has failed", error = exc.name, msg = exc.msg break check # We still don't abort but warn - debug "A future has failed, enable trace logging for details", error=exc.name - trace "Exception details", msg=exc.msg + debug "A future has failed, enable trace logging for details", + error = exc.name + trace "Exception details", msg = exc.msg diff --git a/libp2p/multiaddress.nim b/libp2p/multiaddress.nim index c0b9f8741e..69915bd0d9 100644 --- a/libp2p/multiaddress.nim +++ b/libp2p/multiaddress.nim @@ -15,8 +15,16 @@ import pkg/chronos, chronicles import std/[nativesockets, net, hashes] import tables, strutils, sets -import multicodec, multihash, multibase, transcoder, vbuffer, peerid, - protobuf/minprotobuf, errors, utility +import + multicodec, + multihash, + multibase, + transcoder, + vbuffer, + peerid, + protobuf/minprotobuf, + errors, + utility import stew/[base58, base32, endians2, results] export results, minprotobuf, vbuffer, utility @@ -25,7 +33,11 @@ logScope: type MAKind* = enum - None, Fixed, Length, Path, Marker + None + Fixed + Length + Path + Marker MAProtocol* = object mcodec*: MultiCodec @@ -37,7 +49,9 @@ type data: VBuffer MaPatternOp* = enum - Eq, Or, And + Eq + Or + And MaPattern* = object operator*: MaPatternOp @@ -128,17 +142,11 @@ template pathStringToBuffer(s: string, vb: var VBuffer): bool = template pathBufferToString(vb: var VBuffer, s: var string): bool = s = "" - if (vb.readSeq(s) > 0) and (len(s) > 0): - true - else: - false + if (vb.readSeq(s) > 0) and (len(s) > 0): true else: false template pathBufferToStringNoSlash(vb: var VBuffer, s: var string): bool = s = "" - if (vb.readSeq(s) > 0) and (len(s) > 0) and (s.find('/') == -1): - true - else: - false + if (vb.readSeq(s) > 0) and (len(s) > 0) and (s.find('/') == -1): true else: false template pathValidateBuffer(vb: var VBuffer): bool = var s = "" @@ -177,8 +185,7 @@ proc portBtS(vb: var VBuffer, s: var string): bool = ## Port number bufferToString() implementation. var port: array[2, byte] if vb.readArray(port) == 2: - let nport = - (safeConvert[uint16](port[0]) shl 8) or safeConvert[uint16](port[1]) + let nport = (safeConvert[uint16](port[0]) shl 8) or safeConvert[uint16](port[1]) s = $nport result = true @@ -238,8 +245,7 @@ proc onionBtS(vb: var VBuffer, s: var string): bool = ## ONION address bufferToString() implementation. var buf: array[12, byte] if vb.readArray(buf) == 12: - let nport = - (safeConvert[uint16](buf[10]) shl 8) or safeConvert[uint16](buf[11]) + let nport = (safeConvert[uint16](buf[10]) shl 8) or safeConvert[uint16](buf[11]) s = Base32Lower.encode(buf.toOpenArray(0, 9)) s.add(":") s.add($nport) @@ -273,8 +279,7 @@ 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 = - (safeConvert[uint16](buf[35]) shl 8) or safeConvert[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) @@ -326,155 +331,65 @@ proc mapAnd*(args: varargs[MaPattern]): MaPattern = result.args = @args const - TranscoderIP4* = Transcoder( - stringToBuffer: ip4StB, - bufferToString: ip4BtS, - validateBuffer: ip4VB - ) - TranscoderIP6* = Transcoder( - stringToBuffer: ip6StB, - bufferToString: ip6BtS, - validateBuffer: ip6VB - ) + TranscoderIP4* = + Transcoder(stringToBuffer: ip4StB, bufferToString: ip4BtS, validateBuffer: ip4VB) + TranscoderIP6* = + Transcoder(stringToBuffer: ip6StB, bufferToString: ip6BtS, validateBuffer: ip6VB) TranscoderIP6Zone* = Transcoder( - stringToBuffer: ip6zoneStB, - bufferToString: ip6zoneBtS, - validateBuffer: ip6zoneVB - ) - TranscoderUnix* = Transcoder( - stringToBuffer: unixStB, - bufferToString: unixBtS, - validateBuffer: unixVB - ) - TranscoderP2P* = Transcoder( - stringToBuffer: p2pStB, - bufferToString: p2pBtS, - validateBuffer: p2pVB - ) - TranscoderPort* = Transcoder( - stringToBuffer: portStB, - bufferToString: portBtS, - validateBuffer: portVB + stringToBuffer: ip6zoneStB, bufferToString: ip6zoneBtS, validateBuffer: ip6zoneVB ) + TranscoderUnix* = + Transcoder(stringToBuffer: unixStB, bufferToString: unixBtS, validateBuffer: unixVB) + TranscoderP2P* = + Transcoder(stringToBuffer: p2pStB, bufferToString: p2pBtS, validateBuffer: p2pVB) + TranscoderPort* = + Transcoder(stringToBuffer: portStB, bufferToString: portBtS, validateBuffer: portVB) TranscoderOnion* = Transcoder( - stringToBuffer: onionStB, - bufferToString: onionBtS, - validateBuffer: onionVB + stringToBuffer: onionStB, bufferToString: onionBtS, validateBuffer: onionVB ) TranscoderOnion3* = Transcoder( - stringToBuffer: onion3StB, - bufferToString: onion3BtS, - validateBuffer: onion3VB - ) - TranscoderDNS* = Transcoder( - stringToBuffer: dnsStB, - bufferToString: dnsBtS, - validateBuffer: dnsVB + stringToBuffer: onion3StB, bufferToString: onion3BtS, validateBuffer: onion3VB ) + TranscoderDNS* = + Transcoder(stringToBuffer: dnsStB, bufferToString: dnsBtS, validateBuffer: dnsVB) ProtocolsList = [ + MAProtocol(mcodec: multiCodec("ip4"), kind: Fixed, size: 4, coder: TranscoderIP4), + MAProtocol(mcodec: multiCodec("tcp"), kind: Fixed, size: 2, coder: TranscoderPort), + MAProtocol(mcodec: multiCodec("udp"), kind: Fixed, size: 2, coder: TranscoderPort), + MAProtocol(mcodec: multiCodec("ip6"), kind: Fixed, size: 16, coder: TranscoderIP6), + MAProtocol(mcodec: multiCodec("dccp"), kind: Fixed, size: 2, coder: TranscoderPort), + MAProtocol(mcodec: multiCodec("sctp"), kind: Fixed, size: 2, coder: TranscoderPort), + MAProtocol(mcodec: multiCodec("udt"), kind: Marker, size: 0), + MAProtocol(mcodec: multiCodec("utp"), kind: Marker, size: 0), + MAProtocol(mcodec: multiCodec("http"), kind: Marker, size: 0), + MAProtocol(mcodec: multiCodec("https"), kind: Marker, size: 0), + MAProtocol(mcodec: multiCodec("quic"), kind: Marker, size: 0), + MAProtocol(mcodec: multiCodec("quic-v1"), kind: Marker, size: 0), MAProtocol( - mcodec: multiCodec("ip4"), kind: Fixed, size: 4, - coder: TranscoderIP4 - ), - MAProtocol( - mcodec: multiCodec("tcp"), kind: Fixed, size: 2, - coder: TranscoderPort - ), - MAProtocol( - mcodec: multiCodec("udp"), kind: Fixed, size: 2, - coder: TranscoderPort - ), - MAProtocol( - mcodec: multiCodec("ip6"), kind: Fixed, size: 16, - coder: TranscoderIP6 - ), - MAProtocol( - mcodec: multiCodec("dccp"), kind: Fixed, size: 2, - coder: TranscoderPort + mcodec: multiCodec("ip6zone"), kind: Length, size: 0, coder: TranscoderIP6Zone ), MAProtocol( - mcodec: multiCodec("sctp"), kind: Fixed, size: 2, - coder: TranscoderPort + mcodec: multiCodec("onion"), kind: Fixed, size: 10, coder: TranscoderOnion ), MAProtocol( - mcodec: multiCodec("udt"), kind: Marker, size: 0 + mcodec: multiCodec("onion3"), kind: Fixed, size: 37, coder: TranscoderOnion3 ), + MAProtocol(mcodec: multiCodec("ws"), kind: Marker, size: 0), + MAProtocol(mcodec: multiCodec("wss"), kind: Marker, size: 0), + MAProtocol(mcodec: multiCodec("tls"), kind: Marker, size: 0), + MAProtocol(mcodec: multiCodec("ipfs"), kind: Length, size: 0, coder: TranscoderP2P), + MAProtocol(mcodec: multiCodec("p2p"), kind: Length, size: 0, coder: TranscoderP2P), + MAProtocol(mcodec: multiCodec("unix"), kind: Path, size: 0, coder: TranscoderUnix), + MAProtocol(mcodec: multiCodec("dns"), kind: Length, size: 0, coder: TranscoderDNS), + MAProtocol(mcodec: multiCodec("dns4"), kind: Length, size: 0, coder: TranscoderDNS), + MAProtocol(mcodec: multiCodec("dns6"), kind: Length, size: 0, coder: TranscoderDNS), MAProtocol( - mcodec: multiCodec("utp"), kind: Marker, size: 0 + mcodec: multiCodec("dnsaddr"), kind: Length, size: 0, coder: TranscoderDNS ), - MAProtocol( - mcodec: multiCodec("http"), kind: Marker, size: 0 - ), - MAProtocol( - mcodec: multiCodec("https"), kind: Marker, size: 0 - ), - MAProtocol( - mcodec: multiCodec("quic"), kind: Marker, size: 0 - ), - MAProtocol( - mcodec: multiCodec("quic-v1"), kind: Marker, size: 0 - ), - MAProtocol( - mcodec: multiCodec("ip6zone"), kind: Length, size: 0, - coder: TranscoderIP6Zone - ), - MAProtocol( - mcodec: multiCodec("onion"), kind: Fixed, size: 10, - coder: TranscoderOnion - ), - MAProtocol( - mcodec: multiCodec("onion3"), kind: Fixed, size: 37, - coder: TranscoderOnion3 - ), - MAProtocol( - mcodec: multiCodec("ws"), kind: Marker, size: 0 - ), - MAProtocol( - mcodec: multiCodec("wss"), kind: Marker, size: 0 - ), - MAProtocol( - mcodec: multiCodec("tls"), kind: Marker, size: 0 - ), - MAProtocol( - mcodec: multiCodec("ipfs"), kind: Length, size: 0, - coder: TranscoderP2P - ), - MAProtocol( - mcodec: multiCodec("p2p"), kind: Length, size: 0, - coder: TranscoderP2P - ), - MAProtocol( - mcodec: multiCodec("unix"), kind: Path, size: 0, - coder: TranscoderUnix - ), - MAProtocol( - mcodec: multiCodec("dns"), kind: Length, size: 0, - coder: TranscoderDNS - ), - MAProtocol( - mcodec: multiCodec("dns4"), kind: Length, size: 0, - coder: TranscoderDNS - ), - MAProtocol( - mcodec: multiCodec("dns6"), kind: Length, size: 0, - coder: TranscoderDNS - ), - MAProtocol( - mcodec: multiCodec("dnsaddr"), kind: Length, size: 0, - coder: TranscoderDNS - ), - MAProtocol( - mcodec: multiCodec("p2p-circuit"), kind: Marker, size: 0 - ), - MAProtocol( - mcodec: multiCodec("p2p-websocket-star"), kind: Marker, size: 0 - ), - MAProtocol( - mcodec: multiCodec("p2p-webrtc-star"), kind: Marker, size: 0 - ), - MAProtocol( - mcodec: multiCodec("p2p-webrtc-direct"), kind: Marker, size: 0 - ) + MAProtocol(mcodec: multiCodec("p2p-circuit"), kind: Marker, size: 0), + MAProtocol(mcodec: multiCodec("p2p-websocket-star"), kind: Marker, size: 0), + MAProtocol(mcodec: multiCodec("p2p-webrtc-star"), kind: Marker, size: 0), + MAProtocol(mcodec: multiCodec("p2p-webrtc-direct"), kind: Marker, size: 0), ] DNSANY* = mapEq("dns") @@ -517,31 +432,24 @@ const IPFS* = mapAnd(Reliable, P2PPattern) HTTP* = mapOr( - mapAnd(TCP, mapEq("http")), - mapAnd(IP, mapEq("http")), - mapAnd(DNS, mapEq("http")) + mapAnd(TCP, mapEq("http")), mapAnd(IP, mapEq("http")), mapAnd(DNS, mapEq("http")) ) HTTPS* = mapOr( - mapAnd(TCP, mapEq("https")), - mapAnd(IP, mapEq("https")), - mapAnd(DNS, mapEq("https")) + mapAnd(TCP, mapEq("https")), mapAnd(IP, mapEq("https")), mapAnd(DNS, mapEq("https")) ) WebRTCDirect* = mapOr( - mapAnd(HTTP, mapEq("p2p-webrtc-direct")), - mapAnd(HTTPS, mapEq("p2p-webrtc-direct")) + mapAnd(HTTP, mapEq("p2p-webrtc-direct")), mapAnd(HTTPS, mapEq("p2p-webrtc-direct")) ) CircuitRelay* = mapEq("p2p-circuit") -proc initMultiAddressCodeTable(): Table[MultiCodec, - MAProtocol] {.compileTime.} = +proc initMultiAddressCodeTable(): Table[MultiCodec, MAProtocol] {.compileTime.} = for item in ProtocolsList: result[item.mcodec] = item -const - CodeAddresses = initMultiAddressCodeTable() +const CodeAddresses = initMultiAddressCodeTable() proc trimRight(s: string, ch: char): string = ## Consume trailing characters ``ch`` from string ``s`` and return result. @@ -551,7 +459,7 @@ proc trimRight(s: string, ch: char): string = inc(m) else: break - result = s[0..(s.high - m)] + result = s[0 .. (s.high - m)] proc protoCode*(ma: MultiAddress): MaResult[MultiCodec] = ## Returns MultiAddress ``ma`` protocol code. @@ -579,8 +487,7 @@ proc protoName*(ma: MultiAddress): MaResult[string] = else: ok($(proto.mcodec)) -proc protoArgument*(ma: MultiAddress, - value: var openArray[byte]): MaResult[int] = +proc protoArgument*(ma: MultiAddress, value: var openArray[byte]): MaResult[int] = ## Returns MultiAddress ``ma`` protocol argument value. ## ## If current MultiAddress do not have argument value, then result will be @@ -599,7 +506,7 @@ proc protoArgument*(ma: MultiAddress, if proto.kind == Fixed: res = proto.size if len(value) >= res and - vb.data.readArray(value.toOpenArray(0, proto.size - 1)) != proto.size: + vb.data.readArray(value.toOpenArray(0, proto.size - 1)) != proto.size: err("multiaddress: Decoding protocol error") else: ok(res) @@ -620,7 +527,7 @@ proc protoAddress*(ma: MultiAddress): MaResult[seq[byte]] = ## If current MultiAddress do not have argument value, then result array will ## be empty. var buffer = newSeq[byte](len(ma.data.buffer)) - let res = ? protoArgument(ma, buffer) + let res = ?protoArgument(ma, buffer) buffer.setLen(res) ok(buffer) @@ -639,7 +546,8 @@ proc getPart(ma: MultiAddress, index: int): MaResult[MultiAddress] = var res: MultiAddress res.data = initVBuffer() - if index < 0: return err("multiaddress: negative index gived to getPart") + if index < 0: + return err("multiaddress: negative index gived to getPart") while offset <= index: if vb.data.readVarint(header) == -1: @@ -648,7 +556,6 @@ proc getPart(ma: MultiAddress, index: int): MaResult[MultiAddress] = let proto = CodeAddresses.getOrDefault(MultiCodec(header)) if proto.kind == None: return err("multiaddress: Unsupported protocol '" & $header & "'") - elif proto.kind == Fixed: data.setLen(proto.size) if vb.data.readArray(data) != proto.size: @@ -673,26 +580,29 @@ proc getPart(ma: MultiAddress, index: int): MaResult[MultiAddress] = inc(offset) ok(res) -proc getParts[U, V](ma: MultiAddress, - slice: HSlice[U, V]): MaResult[MultiAddress] = +proc getParts[U, V](ma: MultiAddress, slice: HSlice[U, V]): MaResult[MultiAddress] = when slice.a is BackwardsIndex or slice.b is BackwardsIndex: - let maLength = ? len(ma) + let maLength = ?len(ma) template normalizeIndex(index): int = - when index is BackwardsIndex: maLength - int(index) - else: int(index) + when index is BackwardsIndex: + maLength - int(index) + else: + int(index) + let indexStart = normalizeIndex(slice.a) indexEnd = normalizeIndex(slice.b) var res: MultiAddress - for i in indexStart..indexEnd: - ? res.append(? ma[i]) + for i in indexStart .. indexEnd: + ?res.append(?ma[i]) ok(res) -proc `[]`*(ma: MultiAddress, - i: int | BackwardsIndex): MaResult[MultiAddress] {.inline.} = +proc `[]`*( + ma: MultiAddress, i: int | BackwardsIndex +): MaResult[MultiAddress] {.inline.} = ## Returns part with index ``i`` of MultiAddress ``ma``. when i is BackwardsIndex: - let maLength = ? len(ma) + let maLength = ?len(ma) ma.getPart(maLength - int(i)) else: ma.getPart(i) @@ -716,9 +626,7 @@ iterator items*(ma: MultiAddress): MaResult[MultiAddress] = let proto = CodeAddresses.getOrDefault(MultiCodec(header)) if proto.kind == None: - yield err(MaResult[MultiAddress], "Unsupported protocol '" & - $header & "'") - + yield err(MaResult[MultiAddress], "Unsupported protocol '" & $header & "'") elif proto.kind == Fixed: data.setLen(proto.size) if vb.data.readArray(data) != proto.size: @@ -740,7 +648,8 @@ iterator items*(ma: MultiAddress): MaResult[MultiAddress] = proc len*(ma: MultiAddress): MaResult[int] = var counter: int for part in ma: - if part.isErr: return err(part.error) + if part.isErr: + return err(part.error) counter.inc() ok(counter) @@ -753,8 +662,7 @@ proc contains*(ma: MultiAddress, codec: MultiCodec): MaResult[bool] {.inline.} = return ok(true) ok(false) -proc `[]`*(ma: MultiAddress, - codec: MultiCodec): MaResult[MultiAddress] {.inline.} = +proc `[]`*(ma: MultiAddress, codec: MultiCodec): MaResult[MultiAddress] {.inline.} = ## Returns partial MultiAddress with MultiCodec ``codec`` and present in ## MultiAddress ``ma``. for item in ma.items: @@ -779,13 +687,12 @@ proc toString*(value: MultiAddress): MaResult[string] = return err("multiaddress: Unsupported protocol '" & $header & "'") if proto.kind in {Fixed, Length, Path}: if isNil(proto.coder.bufferToString): - return err("multiaddress: Missing protocol '" & $(proto.mcodec) & - "' coder") + return err("multiaddress: Missing protocol '" & $(proto.mcodec) & "' coder") if not proto.coder.bufferToString(vb.data, part): return err("multiaddress: Decoding protocol error") parts.add($(proto.mcodec)) if len(part) > 0 and (proto.kind == Path) and (part[0] == '/'): - parts.add(part[1..^1]) + parts.add(part[1 ..^ 1]) else: parts.add(part) elif proto.kind == Marker: @@ -797,8 +704,10 @@ proc toString*(value: MultiAddress): MaResult[string] = proc `$`*(value: MultiAddress): string = ## Return string representation of MultiAddress ``value``. let s = value.toString() - if s.isErr: s.error - else: s[] + if s.isErr: + s.error + else: + s[] proc protocols*(value: MultiAddress): MaResult[seq[MultiCodec]] = ## Returns list of protocol codecs inside of MultiAddress ``value``. @@ -815,8 +724,9 @@ proc write*(vb: var VBuffer, ma: MultiAddress) {.inline.} = ## Write MultiAddress value ``ma`` to buffer ``vb``. vb.writeArray(ma.data.buffer) -proc encode*(mbtype: typedesc[MultiBase], encoding: string, - ma: MultiAddress): string {.inline.} = +proc encode*( + mbtype: typedesc[MultiBase], encoding: string, ma: MultiAddress +): string {.inline.} = ## Get MultiBase encoded representation of ``ma`` using encoding ## ``encoding``. result = MultiBase.encode(encoding, ma.data.buffer) @@ -843,8 +753,8 @@ proc validate*(ma: MultiAddress): bool = result = true proc init*( - mtype: typedesc[MultiAddress], protocol: MultiCodec, - value: openArray[byte] = []): MaResult[MultiAddress] = + mtype: typedesc[MultiAddress], protocol: MultiCodec, value: openArray[byte] = [] +): MaResult[MultiAddress] = ## Initialize MultiAddress object from protocol id ``protocol`` and array ## of bytes ``value``. let proto = CodeAddresses.getOrDefault(protocol) @@ -874,19 +784,21 @@ proc init*( of None: raiseAssert "None checked above" -proc init*(mtype: typedesc[MultiAddress], protocol: MultiCodec, - value: PeerId): MaResult[MultiAddress] {.inline.} = +proc init*( + mtype: typedesc[MultiAddress], protocol: MultiCodec, value: PeerId +): MaResult[MultiAddress] {.inline.} = ## Initialize MultiAddress object from protocol id ``protocol`` and peer id ## ``value``. init(mtype, protocol, value.data) -proc init*(mtype: typedesc[MultiAddress], protocol: MultiCodec, - value: int): MaResult[MultiAddress] = +proc init*( + mtype: typedesc[MultiAddress], protocol: MultiCodec, value: int +): MaResult[MultiAddress] = ## Initialize MultiAddress object from protocol id ``protocol`` and integer ## ``value``. This procedure can be used to instantiate ``tcp``, ``udp``, ## ``dccp`` and ``sctp`` MultiAddresses. - var allowed = [multiCodec("tcp"), multiCodec("udp"), multiCodec("dccp"), - multiCodec("sctp")] + var allowed = + [multiCodec("tcp"), multiCodec("udp"), multiCodec("dccp"), multiCodec("sctp")] if protocol notin allowed: err("multiaddress: Incorrect protocol for integer value") else: @@ -906,8 +818,7 @@ proc getProtocol(name: string): MAProtocol {.inline.} = if mc != InvalidMultiCodec: result = CodeAddresses.getOrDefault(mc) -proc init*(mtype: typedesc[MultiAddress], - value: string): MaResult[MultiAddress] = +proc init*(mtype: typedesc[MultiAddress], value: string): MaResult[MultiAddress] = ## Initialize MultiAddress object from string representation ``value``. if len(value) == 0 or value == "/": return err("multiaddress: Address must not be empty!") @@ -926,8 +837,7 @@ proc init*(mtype: typedesc[MultiAddress], else: if proto.kind in {Fixed, Length, Path}: if isNil(proto.coder.stringToBuffer): - return err("multiaddress: Missing protocol '" & - part & "' transcoder") + return err("multiaddress: Missing protocol '" & part & "' transcoder") if offset + 1 >= len(parts): return err("multiaddress: Missing protocol '" & part & "' argument") @@ -936,16 +846,15 @@ proc init*(mtype: typedesc[MultiAddress], res.data.write(proto.mcodec) let res = proto.coder.stringToBuffer(parts[offset + 1], res.data) if not res: - return err("multiaddress: Error encoding `" & part & "/" & - parts[offset + 1] & "`") + return err( + "multiaddress: Error encoding `" & part & "/" & parts[offset + 1] & "`" + ) offset += 2 - elif proto.kind == Path: - var path = "/" & (parts[(offset + 1)..^1].join("/")) + var path = "/" & (parts[(offset + 1) ..^ 1].join("/")) res.data.write(proto.mcodec) if not proto.coder.stringToBuffer(path, res.data): - return err("multiaddress: Error encoding `" & part & "/" & - path & "`") + return err("multiaddress: Error encoding `" & part & "/" & path & "`") break elif proto.kind == Marker: @@ -954,8 +863,9 @@ proc init*(mtype: typedesc[MultiAddress], res.data.finish() ok(res) -proc init*(mtype: typedesc[MultiAddress], - data: openArray[byte]): MaResult[MultiAddress] = +proc init*( + mtype: typedesc[MultiAddress], data: openArray[byte] +): MaResult[MultiAddress] = ## Initialize MultiAddress with array of bytes ``data``. if len(data) == 0: err("multiaddress: Address must not be empty!") @@ -973,38 +883,55 @@ proc init*(mtype: typedesc[MultiAddress]): MultiAddress = ## Initialize empty MultiAddress. result.data = initVBuffer() -proc init*(mtype: typedesc[MultiAddress], address: IpAddress, - protocol: IpTransportProtocol, port: Port): MultiAddress = +proc init*( + mtype: typedesc[MultiAddress], + address: IpAddress, + protocol: IpTransportProtocol, + port: Port, +): MultiAddress = var res: MultiAddress res.data = initVBuffer() let - networkProto = case address.family - of IpAddressFamily.IPv4: getProtocol("ip4") - of IpAddressFamily.IPv6: getProtocol("ip6") - - transportProto = case protocol - of tcpProtocol: getProtocol("tcp") - of udpProtocol: getProtocol("udp") + networkProto = + case address.family + of IpAddressFamily.IPv4: + getProtocol("ip4") + of IpAddressFamily.IPv6: + getProtocol("ip6") + + transportProto = + case protocol + of tcpProtocol: + getProtocol("tcp") + of udpProtocol: + getProtocol("udp") res.data.write(networkProto.mcodec) case address.family - of IpAddressFamily.IPv4: res.data.writeArray(address.address_v4) - of IpAddressFamily.IPv6: res.data.writeArray(address.address_v6) + of IpAddressFamily.IPv4: + res.data.writeArray(address.address_v4) + of IpAddressFamily.IPv6: + res.data.writeArray(address.address_v6) res.data.write(transportProto.mcodec) res.data.writeArray(toBytesBE(uint16(port))) res.data.finish() res -proc init*(mtype: typedesc[MultiAddress], address: TransportAddress, - protocol = IPPROTO_TCP): MaResult[MultiAddress] = +proc init*( + mtype: typedesc[MultiAddress], address: TransportAddress, protocol = IPPROTO_TCP +): MaResult[MultiAddress] = ## Initialize MultiAddress using chronos.TransportAddress (IPv4/IPv6/Unix) ## and protocol information (UDP/TCP). var res: MultiAddress res.data = initVBuffer() - let protoProto = case protocol - of IPPROTO_TCP: getProtocol("tcp") - of IPPROTO_UDP: getProtocol("udp") - else: default(MAProtocol) + let protoProto = + case protocol + of IPPROTO_TCP: + getProtocol("tcp") + of IPPROTO_UDP: + getProtocol("udp") + else: + default(MAProtocol) if protoProto.size == 0: return err("multiaddress: protocol should be either TCP or UDP") if address.family == AddressFamily.IPv4: @@ -1043,8 +970,7 @@ proc append*(m1: var MultiAddress, m2: MultiAddress): MaResult[void] = else: ok() -proc `&`*(m1, m2: MultiAddress): MultiAddress {. - raises: [LPError].} = +proc `&`*(m1, m2: MultiAddress): MultiAddress {.raises: [LPError].} = ## Concatenates two addresses ``m1`` and ``m2``, and returns result. ## ## This procedure performs validation of concatenated result and can raise @@ -1053,8 +979,7 @@ proc `&`*(m1, m2: MultiAddress): MultiAddress {. concat(m1, m2).tryGet() -proc `&=`*(m1: var MultiAddress, m2: MultiAddress) {. - raises: [LPError].} = +proc `&=`*(m1: var MultiAddress, m2: MultiAddress) {.raises: [LPError].} = ## Concatenates two addresses ``m1`` and ``m2``. ## ## This procedure performs validation of concatenated result and can raise @@ -1076,13 +1001,12 @@ proc matchPart(pat: MaPattern, protos: seq[MultiCodec]): MaPatResult = let res = a.matchPart(pcs) if res.flag: #Greedy Or - if result.flag == false or - result.rem.len > res.rem.len: + if result.flag == false or result.rem.len > res.rem.len: result = res elif pat.operator == And: if len(pcs) < len(pat.args): return MaPatResult(flag: false, rem: empty) - for i in 0.. len(outbytes): outlen = length @@ -49,9 +55,9 @@ proc idd(inbytes: openArray[char], outbytes: var openArray[byte], outlen = length result = MultiBaseStatus.Success -proc ide(inbytes: openArray[byte], - outbytes: var openArray[char], - outlen: var int): MultiBaseStatus = +proc ide( + inbytes: openArray[byte], outbytes: var openArray[char], outlen: var int +): MultiBaseStatus = let length = len(inbytes) if length > len(outbytes): outlen = length @@ -61,31 +67,37 @@ proc ide(inbytes: openArray[byte], outlen = length result = MultiBaseStatus.Success -proc idel(length: int): int = length -proc iddl(length: int): int = length +proc idel(length: int): int = + length -proc b16d(inbytes: openArray[char], - outbytes: var openArray[byte], - outlen: var int): MultiBaseStatus = +proc iddl(length: int): int = + length + +proc b16d( + inbytes: openArray[char], outbytes: var openArray[byte], outlen: var int +): MultiBaseStatus = discard -proc b16e(inbytes: openArray[byte], - outbytes: var openArray[char], - outlen: var int): MultiBaseStatus = +proc b16e( + inbytes: openArray[byte], outbytes: var openArray[char], outlen: var int +): MultiBaseStatus = discard -proc b16ud(inbytes: openArray[char], - outbytes: var openArray[byte], - outlen: var int): MultiBaseStatus = +proc b16ud( + inbytes: openArray[char], outbytes: var openArray[byte], outlen: var int +): MultiBaseStatus = discard -proc b16ue(inbytes: openArray[byte], - outbytes: var openArray[char], - outlen: var int): MultiBaseStatus = +proc b16ue( + inbytes: openArray[byte], outbytes: var openArray[char], outlen: var int +): MultiBaseStatus = discard -proc b16el(length: int): int = length shl 1 -proc b16dl(length: int): int = (length + 1) div 2 +proc b16el(length: int): int = + length shl 1 + +proc b16dl(length: int): int = + (length + 1) div 2 proc b32ce(r: Base32Status): MultiBaseStatus {.inline.} = result = MultiBaseStatus.Error @@ -114,218 +126,253 @@ proc b64ce(r: Base64Status): MultiBaseStatus {.inline.} = elif r == Base64Status.Success: result = MultiBaseStatus.Success -proc b32hd(inbytes: openArray[char], - outbytes: var openArray[byte], - outlen: var int): MultiBaseStatus = +proc b32hd( + inbytes: openArray[char], outbytes: var openArray[byte], outlen: var int +): MultiBaseStatus = result = b32ce(HexBase32Lower.decode(inbytes, outbytes, outlen)) -proc b32he(inbytes: openArray[byte], - outbytes: var openArray[char], - outlen: var int): MultiBaseStatus = +proc b32he( + inbytes: openArray[byte], outbytes: var openArray[char], outlen: var int +): MultiBaseStatus = result = b32ce(HexBase32Lower.encode(inbytes, outbytes, outlen)) -proc b32hud(inbytes: openArray[char], - outbytes: var openArray[byte], - outlen: var int): MultiBaseStatus = +proc b32hud( + inbytes: openArray[char], outbytes: var openArray[byte], outlen: var int +): MultiBaseStatus = result = b32ce(HexBase32Upper.decode(inbytes, outbytes, outlen)) -proc b32hue(inbytes: openArray[byte], - outbytes: var openArray[char], - outlen: var int): MultiBaseStatus = +proc b32hue( + inbytes: openArray[byte], outbytes: var openArray[char], outlen: var int +): MultiBaseStatus = result = b32ce(HexBase32Upper.encode(inbytes, outbytes, outlen)) -proc b32hpd(inbytes: openArray[char], - outbytes: var openArray[byte], - outlen: var int): MultiBaseStatus = +proc b32hpd( + inbytes: openArray[char], outbytes: var openArray[byte], outlen: var int +): MultiBaseStatus = result = b32ce(HexBase32LowerPad.decode(inbytes, outbytes, outlen)) -proc b32hpe(inbytes: openArray[byte], - outbytes: var openArray[char], - outlen: var int): MultiBaseStatus = +proc b32hpe( + inbytes: openArray[byte], outbytes: var openArray[char], outlen: var int +): MultiBaseStatus = result = b32ce(HexBase32LowerPad.encode(inbytes, outbytes, outlen)) -proc b32hpud(inbytes: openArray[char], - outbytes: var openArray[byte], - outlen: var int): MultiBaseStatus = +proc b32hpud( + inbytes: openArray[char], outbytes: var openArray[byte], outlen: var int +): MultiBaseStatus = result = b32ce(HexBase32UpperPad.decode(inbytes, outbytes, outlen)) -proc b32hpue(inbytes: openArray[byte], - outbytes: var openArray[char], - outlen: var int): MultiBaseStatus = +proc b32hpue( + inbytes: openArray[byte], outbytes: var openArray[char], outlen: var int +): MultiBaseStatus = result = b32ce(HexBase32UpperPad.encode(inbytes, outbytes, outlen)) -proc b32d(inbytes: openArray[char], - outbytes: var openArray[byte], - outlen: var int): MultiBaseStatus = +proc b32d( + inbytes: openArray[char], outbytes: var openArray[byte], outlen: var int +): MultiBaseStatus = result = b32ce(Base32Lower.decode(inbytes, outbytes, outlen)) -proc b32e(inbytes: openArray[byte], - outbytes: var openArray[char], - outlen: var int): MultiBaseStatus = +proc b32e( + inbytes: openArray[byte], outbytes: var openArray[char], outlen: var int +): MultiBaseStatus = result = b32ce(Base32Lower.encode(inbytes, outbytes, outlen)) -proc b32ud(inbytes: openArray[char], - outbytes: var openArray[byte], - outlen: var int): MultiBaseStatus = +proc b32ud( + inbytes: openArray[char], outbytes: var openArray[byte], outlen: var int +): MultiBaseStatus = result = b32ce(Base32Upper.decode(inbytes, outbytes, outlen)) -proc b32ue(inbytes: openArray[byte], - outbytes: var openArray[char], - outlen: var int): MultiBaseStatus = +proc b32ue( + inbytes: openArray[byte], outbytes: var openArray[char], outlen: var int +): MultiBaseStatus = result = b32ce(Base32Upper.encode(inbytes, outbytes, outlen)) -proc b32pd(inbytes: openArray[char], - outbytes: var openArray[byte], - outlen: var int): MultiBaseStatus = +proc b32pd( + inbytes: openArray[char], outbytes: var openArray[byte], outlen: var int +): MultiBaseStatus = result = b32ce(Base32LowerPad.decode(inbytes, outbytes, outlen)) -proc b32pe(inbytes: openArray[byte], - outbytes: var openArray[char], - outlen: var int): MultiBaseStatus = +proc b32pe( + inbytes: openArray[byte], outbytes: var openArray[char], outlen: var int +): MultiBaseStatus = result = b32ce(Base32LowerPad.encode(inbytes, outbytes, outlen)) -proc b32pud(inbytes: openArray[char], - outbytes: var openArray[byte], - outlen: var int): MultiBaseStatus = +proc b32pud( + inbytes: openArray[char], outbytes: var openArray[byte], outlen: var int +): MultiBaseStatus = result = b32ce(Base32UpperPad.decode(inbytes, outbytes, outlen)) -proc b32pue(inbytes: openArray[byte], - outbytes: var openArray[char], - outlen: var int): MultiBaseStatus = +proc b32pue( + inbytes: openArray[byte], outbytes: var openArray[char], outlen: var int +): MultiBaseStatus = result = b32ce(Base32UpperPad.encode(inbytes, outbytes, outlen)) -proc b32el(length: int): int = Base32Lower.encodedLength(length) -proc b32dl(length: int): int = Base32Lower.decodedLength(length) -proc b32pel(length: int): int = Base32LowerPad.encodedLength(length) -proc b32pdl(length: int): int = Base32LowerPad.decodedLength(length) +proc b32el(length: int): int = + Base32Lower.encodedLength(length) + +proc b32dl(length: int): int = + Base32Lower.decodedLength(length) + +proc b32pel(length: int): int = + Base32LowerPad.encodedLength(length) + +proc b32pdl(length: int): int = + Base32LowerPad.decodedLength(length) -proc b58fd(inbytes: openArray[char], - outbytes: var openArray[byte], - outlen: var int): MultiBaseStatus = +proc b58fd( + inbytes: openArray[char], outbytes: var openArray[byte], outlen: var int +): MultiBaseStatus = result = b58ce(FLCBase58.decode(inbytes, outbytes, outlen)) -proc b58fe(inbytes: openArray[byte], - outbytes: var openArray[char], - outlen: var int): MultiBaseStatus = +proc b58fe( + inbytes: openArray[byte], outbytes: var openArray[char], outlen: var int +): MultiBaseStatus = result = b58ce(FLCBase58.encode(inbytes, outbytes, outlen)) -proc b58bd(inbytes: openArray[char], - outbytes: var openArray[byte], - outlen: var int): MultiBaseStatus = +proc b58bd( + inbytes: openArray[char], outbytes: var openArray[byte], outlen: var int +): MultiBaseStatus = result = b58ce(BTCBase58.decode(inbytes, outbytes, outlen)) -proc b58be(inbytes: openArray[byte], - outbytes: var openArray[char], - outlen: var int): MultiBaseStatus = +proc b58be( + inbytes: openArray[byte], outbytes: var openArray[char], outlen: var int +): MultiBaseStatus = result = b58ce(BTCBase58.encode(inbytes, outbytes, outlen)) -proc b58el(length: int): int = Base58.encodedLength(length) -proc b58dl(length: int): int = Base58.decodedLength(length) +proc b58el(length: int): int = + Base58.encodedLength(length) -proc b64el(length: int): int = Base64.encodedLength(length) -proc b64dl(length: int): int = Base64.decodedLength(length) -proc b64pel(length: int): int = Base64Pad.encodedLength(length) -proc b64pdl(length: int): int = Base64Pad.decodedLength(length) +proc b58dl(length: int): int = + Base58.decodedLength(length) -proc b64e(inbytes: openArray[byte], - outbytes: var openArray[char], - outlen: var int): MultiBaseStatus = +proc b64el(length: int): int = + Base64.encodedLength(length) + +proc b64dl(length: int): int = + Base64.decodedLength(length) + +proc b64pel(length: int): int = + Base64Pad.encodedLength(length) + +proc b64pdl(length: int): int = + Base64Pad.decodedLength(length) + +proc b64e( + inbytes: openArray[byte], outbytes: var openArray[char], outlen: var int +): MultiBaseStatus = result = b64ce(Base64.encode(inbytes, outbytes, outlen)) -proc b64d(inbytes: openArray[char], - outbytes: var openArray[byte], - outlen: var int): MultiBaseStatus = +proc b64d( + inbytes: openArray[char], outbytes: var openArray[byte], outlen: var int +): MultiBaseStatus = result = b64ce(Base64.decode(inbytes, outbytes, outlen)) -proc b64pe(inbytes: openArray[byte], - outbytes: var openArray[char], - outlen: var int): MultiBaseStatus = +proc b64pe( + inbytes: openArray[byte], outbytes: var openArray[char], outlen: var int +): MultiBaseStatus = result = b64ce(Base64Pad.encode(inbytes, outbytes, outlen)) -proc b64pd(inbytes: openArray[char], - outbytes: var openArray[byte], - outlen: var int): MultiBaseStatus = +proc b64pd( + inbytes: openArray[char], outbytes: var openArray[byte], outlen: var int +): MultiBaseStatus = result = b64ce(Base64Pad.decode(inbytes, outbytes, outlen)) -proc b64ue(inbytes: openArray[byte], - outbytes: var openArray[char], - outlen: var int): MultiBaseStatus = +proc b64ue( + inbytes: openArray[byte], outbytes: var openArray[char], outlen: var int +): MultiBaseStatus = result = b64ce(Base64Url.encode(inbytes, outbytes, outlen)) -proc b64ud(inbytes: openArray[char], - outbytes: var openArray[byte], - outlen: var int): MultiBaseStatus = +proc b64ud( + inbytes: openArray[char], outbytes: var openArray[byte], outlen: var int +): MultiBaseStatus = result = b64ce(Base64Url.decode(inbytes, outbytes, outlen)) -proc b64upe(inbytes: openArray[byte], - outbytes: var openArray[char], - outlen: var int): MultiBaseStatus = +proc b64upe( + inbytes: openArray[byte], outbytes: var openArray[char], outlen: var int +): MultiBaseStatus = result = b64ce(Base64UrlPad.encode(inbytes, outbytes, outlen)) -proc b64upd(inbytes: openArray[char], - outbytes: var openArray[byte], - outlen: var int): MultiBaseStatus = +proc b64upd( + inbytes: openArray[char], outbytes: var openArray[byte], outlen: var int +): MultiBaseStatus = result = b64ce(Base64UrlPad.decode(inbytes, outbytes, outlen)) -const - MultiBaseCodecs = [ - MBCodec(name: "identity", code: chr(0x00), - decr: idd, encr: ide, decl: iddl, encl: idel - ), - MBCodec(name: "base1", code: '1'), - MBCodec(name: "base2", code: '0'), - MBCodec(name: "base8", code: '7'), - MBCodec(name: "base10", code: '9'), - MBCodec(name: "base16", code: 'f', - decr: b16d, encr: b16e, decl: b16dl, encl: b16el - ), - MBCodec(name: "base16upper", code: 'F', - decr: b16ud, encr: b16ue, decl: b16dl, encl: b16el - ), - MBCodec(name: "base32hex", code: 'v', - decr: b32hd, encr: b32he, decl: b32dl, encl: b32el - ), - MBCodec(name: "base32hexupper", code: 'V', - decr: b32hud, encr: b32hue, decl: b32dl, encl: b32el - ), - MBCodec(name: "base32hexpad", code: 't', - decr: b32hpd, encr: b32hpe, decl: b32pdl, encl: b32pel - ), - MBCodec(name: "base32hexpadupper", code: 'T', - decr: b32hpud, encr: b32hpue, decl: b32pdl, encl: b32pel - ), - MBCodec(name: "base32", code: 'b', - decr: b32d, encr: b32e, decl: b32dl, encl: b32el - ), - MBCodec(name: "base32upper", code: 'B', - decr: b32ud, encr: b32ue, decl: b32dl, encl: b32el - ), - MBCodec(name: "base32pad", code: 'c', - decr: b32pd, encr: b32pe, decl: b32pdl, encl: b32pel - ), - MBCodec(name: "base32padupper", code: 'C', - decr: b32pud, encr: b32pue, decl: b32pdl, encl: b32pel - ), - MBCodec(name: "base32z", code: 'h'), - MBCodec(name: "base58flickr", code: 'Z', - decr: b58fd, encr: b58fe, decl: b58dl, encl: b58el - ), - MBCodec(name: "base58btc", code: 'z', - decr: b58bd, encr: b58be, decl: b58dl, encl: b58el - ), - MBCodec(name: "base64", code: 'm', - decr: b64d, encr: b64e, decl: b64dl, encl: b64el - ), - MBCodec(name: "base64pad", code: 'M', - decr: b64pd, encr: b64pe, decl: b64pdl, encl: b64pel - ), - MBCodec(name: "base64url", code: 'u', - decr: b64ud, encr: b64ue, decl: b64dl, encl: b64el - ), - MBCodec(name: "base64urlpad", code: 'U', - decr: b64upd, encr: b64upe, decl: b64pdl, encl: b64pel - ) - ] +const MultiBaseCodecs = [ + MBCodec( + name: "identity", code: chr(0x00), decr: idd, encr: ide, decl: iddl, encl: idel + ), + MBCodec(name: "base1", code: '1'), + MBCodec(name: "base2", code: '0'), + MBCodec(name: "base8", code: '7'), + MBCodec(name: "base10", code: '9'), + MBCodec(name: "base16", code: 'f', decr: b16d, encr: b16e, decl: b16dl, encl: b16el), + MBCodec( + name: "base16upper", code: 'F', decr: b16ud, encr: b16ue, decl: b16dl, encl: b16el + ), + MBCodec( + name: "base32hex", code: 'v', decr: b32hd, encr: b32he, decl: b32dl, encl: b32el + ), + MBCodec( + name: "base32hexupper", + code: 'V', + decr: b32hud, + encr: b32hue, + decl: b32dl, + encl: b32el, + ), + MBCodec( + name: "base32hexpad", + code: 't', + decr: b32hpd, + encr: b32hpe, + decl: b32pdl, + encl: b32pel, + ), + MBCodec( + name: "base32hexpadupper", + code: 'T', + decr: b32hpud, + encr: b32hpue, + decl: b32pdl, + encl: b32pel, + ), + MBCodec(name: "base32", code: 'b', decr: b32d, encr: b32e, decl: b32dl, encl: b32el), + MBCodec( + name: "base32upper", code: 'B', decr: b32ud, encr: b32ue, decl: b32dl, encl: b32el + ), + MBCodec( + name: "base32pad", code: 'c', decr: b32pd, encr: b32pe, decl: b32pdl, encl: b32pel + ), + MBCodec( + name: "base32padupper", + code: 'C', + decr: b32pud, + encr: b32pue, + decl: b32pdl, + encl: b32pel, + ), + MBCodec(name: "base32z", code: 'h'), + MBCodec( + name: "base58flickr", code: 'Z', decr: b58fd, encr: b58fe, decl: b58dl, encl: b58el + ), + MBCodec( + name: "base58btc", code: 'z', decr: b58bd, encr: b58be, decl: b58dl, encl: b58el + ), + MBCodec(name: "base64", code: 'm', decr: b64d, encr: b64e, decl: b64dl, encl: b64el), + MBCodec( + name: "base64pad", code: 'M', decr: b64pd, encr: b64pe, decl: b64pdl, encl: b64pel + ), + MBCodec( + name: "base64url", code: 'u', decr: b64ud, encr: b64ue, decl: b64dl, encl: b64el + ), + MBCodec( + name: "base64urlpad", + code: 'U', + decr: b64upd, + encr: b64upe, + decl: b64pdl, + encl: b64pel, + ), +] proc initMultiBaseCodeTable(): Table[char, MBCodec] {.compileTime.} = for item in MultiBaseCodecs: @@ -339,8 +386,7 @@ const CodeMultiBases = initMultiBaseCodeTable() NameMultiBases = initMultiBaseNameTable() -proc encodedLength*(mbtype: typedesc[MultiBase], encoding: string, - length: int): int = +proc encodedLength*(mbtype: typedesc[MultiBase], encoding: string, length: int): int = ## Return estimated size of buffer to store MultiBase encoded value with ## encoding ``encoding`` of length ``length``. ## @@ -355,8 +401,7 @@ proc encodedLength*(mbtype: typedesc[MultiBase], encoding: string, else: result = mb.encl(length) + 1 -proc decodedLength*(mbtype: typedesc[MultiBase], encoding: char, - length: int): int = +proc decodedLength*(mbtype: typedesc[MultiBase], encoding: char, length: int): int = ## Return estimated size of buffer to store MultiBase decoded value with ## encoding character ``encoding`` of length ``length``. let mb = CodeMultiBases.getOrDefault(encoding) @@ -368,9 +413,13 @@ proc decodedLength*(mbtype: typedesc[MultiBase], encoding: char, else: result = mb.decl(length - 1) -proc encode*(mbtype: typedesc[MultiBase], encoding: string, - inbytes: openArray[byte], outbytes: var openArray[char], - outlen: var int): MultiBaseStatus = +proc encode*( + mbtype: typedesc[MultiBase], + encoding: string, + inbytes: openArray[byte], + outbytes: var openArray[char], + outlen: var int, +): MultiBaseStatus = ## Encode array ``inbytes`` using MultiBase encoding scheme ``encoding`` and ## store encoded value to ``outbytes``. ## @@ -392,8 +441,7 @@ proc encode*(mbtype: typedesc[MultiBase], encoding: string, if isNil(mb.encr) or isNil(mb.encl): return MultiBaseStatus.NotSupported if len(outbytes) > 1: - result = mb.encr(inbytes, outbytes.toOpenArray(1, outbytes.high), - outlen) + result = mb.encr(inbytes, outbytes.toOpenArray(1, outbytes.high), outlen) if result == MultiBaseStatus.Overrun: outlen += 1 elif result == MultiBaseStatus.Success: @@ -408,8 +456,12 @@ proc encode*(mbtype: typedesc[MultiBase], encoding: string, result = MultiBaseStatus.Overrun outlen = mb.encl(len(inbytes)) + 1 -proc decode*(mbtype: typedesc[MultiBase], inbytes: openArray[char], - outbytes: var openArray[byte], outlen: var int): MultiBaseStatus = +proc decode*( + mbtype: typedesc[MultiBase], + inbytes: openArray[char], + outbytes: var openArray[byte], + outlen: var int, +): MultiBaseStatus = ## Decode array ``inbytes`` using MultiBase encoding and store decoded value ## to ``outbytes``. ## @@ -438,8 +490,9 @@ proc decode*(mbtype: typedesc[MultiBase], inbytes: openArray[char], else: result = mb.decr(inbytes.toOpenArray(1, length - 1), outbytes, outlen) -proc encode*(mbtype: typedesc[MultiBase], encoding: string, - inbytes: openArray[byte]): Result[string, string] = +proc encode*( + mbtype: typedesc[MultiBase], encoding: string, inbytes: openArray[byte] +): Result[string, string] = ## Encode array ``inbytes`` using MultiBase encoding scheme ``encoding`` and ## return encoded string. let length = len(inbytes) @@ -462,7 +515,9 @@ proc encode*(mbtype: typedesc[MultiBase], encoding: string, buffer[0] = mb.code ok(buffer) -proc decode*(mbtype: typedesc[MultiBase], inbytes: openArray[char]): Result[seq[byte], string] = +proc decode*( + mbtype: typedesc[MultiBase], inbytes: openArray[char] +): Result[seq[byte], string] = ## Decode MultiBase encoded array ``inbytes`` and return decoded sequence of ## bytes. let length = len(inbytes) @@ -479,8 +534,7 @@ proc decode*(mbtype: typedesc[MultiBase], inbytes: openArray[char]): Result[seq[ else: var buffer = newSeq[byte](mb.decl(length - 1)) var outlen = 0 - let res = mb.decr(inbytes.toOpenArray(1, length - 1), - buffer, outlen) + let res = mb.decr(inbytes.toOpenArray(1, length - 1), buffer, outlen) if res != MultiBaseStatus.Success: err("multibase: Decoding error [" & $res & "]") else: diff --git a/libp2p/multicodec.nim b/libp2p/multicodec.nim index 2e8105d9be..4d292f8c0e 100644 --- a/libp2p/multicodec.nim +++ b/libp2p/multicodec.nim @@ -49,132 +49,325 @@ const MultiCodecList = [ ("keccak-384", 0x1C), ("keccak-512", 0x1D), ("murmur3", 0x22), - ("blake2b-8", 0xB201), ("blake2b-16", 0xB202), ("blake2b-24", 0xB203), - ("blake2b-32", 0xB204), ("blake2b-40", 0xB205), ("blake2b-48", 0xB206), - ("blake2b-56", 0xB207), ("blake2b-64", 0xB208), ("blake2b-72", 0xB209), - ("blake2b-80", 0xB20A), ("blake2b-88", 0xB20B), ("blake2b-96", 0xB20C), - ("blake2b-104", 0xB20D), ("blake2b-112", 0xB20E), ("blake2b-120", 0xB20F), - ("blake2b-128", 0xB210), ("blake2b-136", 0xB211), ("blake2b-144", 0xB212), - ("blake2b-152", 0xB213), ("blake2b-160", 0xB214), ("blake2b-168", 0xB215), - ("blake2b-176", 0xB216), ("blake2b-184", 0xB217), ("blake2b-192", 0xB218), - ("blake2b-200", 0xB219), ("blake2b-208", 0xB21A), ("blake2b-216", 0xB21B), - ("blake2b-224", 0xB21C), ("blake2b-232", 0xB21D), ("blake2b-240", 0xB21E), - ("blake2b-248", 0xB21F), ("blake2b-256", 0xB220), ("blake2b-264", 0xB221), - ("blake2b-272", 0xB222), ("blake2b-280", 0xB223), ("blake2b-288", 0xB224), - ("blake2b-296", 0xB225), ("blake2b-304", 0xB226), ("blake2b-312", 0xB227), - ("blake2b-320", 0xB228), ("blake2b-328", 0xB229), ("blake2b-336", 0xB22A), - ("blake2b-344", 0xB22B), ("blake2b-352", 0xB22C), ("blake2b-360", 0xB22D), - ("blake2b-368", 0xB22E), ("blake2b-376", 0xB22F), ("blake2b-384", 0xB230), - ("blake2b-392", 0xB231), ("blake2b-400", 0xB232), ("blake2b-408", 0xB233), - ("blake2b-416", 0xB234), ("blake2b-424", 0xB235), ("blake2b-432", 0xB236), - ("blake2b-440", 0xB237), ("blake2b-448", 0xB238), ("blake2b-456", 0xB239), - ("blake2b-464", 0xB23A), ("blake2b-472", 0xB23B), ("blake2b-480", 0xB23C), - ("blake2b-488", 0xB23D), ("blake2b-496", 0xB23E), ("blake2b-504", 0xB23F), - ("blake2b-512", 0xB240), ("blake2s-8", 0xB241), ("blake2s-16", 0xB242), - ("blake2s-24", 0xB243), ("blake2s-32", 0xB244), ("blake2s-40", 0xB245), - ("blake2s-48", 0xB246), ("blake2s-56", 0xB247), ("blake2s-64", 0xB248), - ("blake2s-72", 0xB249), ("blake2s-80", 0xB24A), ("blake2s-88", 0xB24B), - ("blake2s-96", 0xB24C), ("blake2s-104", 0xB24D), ("blake2s-112", 0xB24E), - ("blake2s-120", 0xB24F), ("blake2s-128", 0xB250), ("blake2s-136", 0xB251), - ("blake2s-144", 0xB252), ("blake2s-152", 0xB253), ("blake2s-160", 0xB254), - ("blake2s-168", 0xB255), ("blake2s-176", 0xB256), ("blake2s-184", 0xB257), - ("blake2s-192", 0xB258), ("blake2s-200", 0xB259), ("blake2s-208", 0xB25A), - ("blake2s-216", 0xB25B), ("blake2s-224", 0xB25C), ("blake2s-232", 0xB25D), - ("blake2s-240", 0xB25E), ("blake2s-248", 0xB25F), ("blake2s-256", 0xB260), - ("skein256-8", 0xB301), ("skein256-16", 0xB302), ("skein256-24", 0xB303), - ("skein256-32", 0xB304), ("skein256-40", 0xB305), ("skein256-48", 0xB306), - ("skein256-56", 0xB307), ("skein256-64", 0xB308), ("skein256-72", 0xB309), - ("skein256-80", 0xB30A), ("skein256-88", 0xB30B), ("skein256-96", 0xB30C), - ("skein256-104", 0xB30D), ("skein256-112", 0xB30E), ("skein256-120", 0xB30F), - ("skein256-128", 0xB310), ("skein256-136", 0xB311), ("skein256-144", 0xB312), - ("skein256-152", 0xB313), ("skein256-160", 0xB314), ("skein256-168", 0xB315), - ("skein256-176", 0xB316), ("skein256-184", 0xB317), ("skein256-192", 0xB318), - ("skein256-200", 0xB319), ("skein256-208", 0xB31A), ("skein256-216", 0xB31B), - ("skein256-224", 0xB31C), ("skein256-232", 0xB31D), ("skein256-240", 0xB31E), - ("skein256-248", 0xB31F), ("skein256-256", 0xB320), - ("skein512-8", 0xB321), ("skein512-16", 0xB322), ("skein512-24", 0xB323), - ("skein512-32", 0xB324), ("skein512-40", 0xB325), ("skein512-48", 0xB326), - ("skein512-56", 0xB327), ("skein512-64", 0xB328), ("skein512-72", 0xB329), - ("skein512-80", 0xB32A), ("skein512-88", 0xB32B), ("skein512-96", 0xB32C), - ("skein512-104", 0xB32D), ("skein512-112", 0xB32E), ("skein512-120", 0xB32F), - ("skein512-128", 0xB330), ("skein512-136", 0xB331), ("skein512-144", 0xB332), - ("skein512-152", 0xB333), ("skein512-160", 0xB334), ("skein512-168", 0xB335), - ("skein512-176", 0xB336), ("skein512-184", 0xB337), ("skein512-192", 0xB338), - ("skein512-200", 0xB339), ("skein512-208", 0xB33A), ("skein512-216", 0xB33B), - ("skein512-224", 0xB33C), ("skein512-232", 0xB33D), ("skein512-240", 0xB33E), - ("skein512-248", 0xB33F), ("skein512-256", 0xB340), ("skein512-264", 0xB341), - ("skein512-272", 0xB342), ("skein512-280", 0xB343), ("skein512-288", 0xB344), - ("skein512-296", 0xB345), ("skein512-304", 0xB346), ("skein512-312", 0xB347), - ("skein512-320", 0xB348), ("skein512-328", 0xB349), ("skein512-336", 0xB34A), - ("skein512-344", 0xB34B), ("skein512-352", 0xB34C), ("skein512-360", 0xB34D), - ("skein512-368", 0xB34E), ("skein512-376", 0xB34F), ("skein512-384", 0xB350), - ("skein512-392", 0xB351), ("skein512-400", 0xB352), ("skein512-408", 0xB353), - ("skein512-416", 0xB354), ("skein512-424", 0xB355), ("skein512-432", 0xB356), - ("skein512-440", 0xB357), ("skein512-448", 0xB358), ("skein512-456", 0xB359), - ("skein512-464", 0xB35A), ("skein512-472", 0xB35B), ("skein512-480", 0xB35C), - ("skein512-488", 0xB35D), ("skein512-496", 0xB35E), ("skein512-504", 0xB35F), - ("skein512-512", 0xB360), ("skein1024-8", 0xB361), ("skein1024-16", 0xB362), - ("skein1024-24", 0xB363), ("skein1024-32", 0xB364), ("skein1024-40", 0xB365), - ("skein1024-48", 0xB366), ("skein1024-56", 0xB367), ("skein1024-64", 0xB368), - ("skein1024-72", 0xB369), ("skein1024-80", 0xB36A), ("skein1024-88", 0xB36B), - ("skein1024-96", 0xB36C), ("skein1024-104", 0xB36D), - ("skein1024-112", 0xB36E), ("skein1024-120", 0xB36F), - ("skein1024-128", 0xB370), ("skein1024-136", 0xB371), - ("skein1024-144", 0xB372), ("skein1024-152", 0xB373), - ("skein1024-160", 0xB374), ("skein1024-168", 0xB375), - ("skein1024-176", 0xB376), ("skein1024-184", 0xB377), - ("skein1024-192", 0xB378), ("skein1024-200", 0xB379), - ("skein1024-208", 0xB37A), ("skein1024-216", 0xB37B), - ("skein1024-224", 0xB37C), ("skein1024-232", 0xB37D), - ("skein1024-240", 0xB37E), ("skein1024-248", 0xB37F), - ("skein1024-256", 0xB380), ("skein1024-264", 0xB381), - ("skein1024-272", 0xB382), ("skein1024-280", 0xB383), - ("skein1024-288", 0xB384), ("skein1024-296", 0xB385), - ("skein1024-304", 0xB386), ("skein1024-312", 0xB387), - ("skein1024-320", 0xB388), ("skein1024-328", 0xB389), - ("skein1024-336", 0xB38A), ("skein1024-344", 0xB38B), - ("skein1024-352", 0xB38C), ("skein1024-360", 0xB38D), - ("skein1024-368", 0xB38E), ("skein1024-376", 0xB38F), - ("skein1024-384", 0xB390), ("skein1024-392", 0xB391), - ("skein1024-400", 0xB392), ("skein1024-408", 0xB393), - ("skein1024-416", 0xB394), ("skein1024-424", 0xB395), - ("skein1024-432", 0xB396), ("skein1024-440", 0xB397), - ("skein1024-448", 0xB398), ("skein1024-456", 0xB399), - ("skein1024-464", 0xB39A), ("skein1024-472", 0xB39B), - ("skein1024-480", 0xB39C), ("skein1024-488", 0xB39D), - ("skein1024-496", 0xB39E), ("skein1024-504", 0xB39F), - ("skein1024-512", 0xB3A0), ("skein1024-520", 0xB3A1), - ("skein1024-528", 0xB3A2), ("skein1024-536", 0xB3A3), - ("skein1024-544", 0xB3A4), ("skein1024-552", 0xB3A5), - ("skein1024-560", 0xB3A6), ("skein1024-568", 0xB3A7), - ("skein1024-576", 0xB3A8), ("skein1024-584", 0xB3A9), - ("skein1024-592", 0xB3AA), ("skein1024-600", 0xB3AB), - ("skein1024-608", 0xB3AC), ("skein1024-616", 0xB3AD), - ("skein1024-624", 0xB3AE), ("skein1024-632", 0xB3AF), - ("skein1024-640", 0xB3B0), ("skein1024-648", 0xB3B1), - ("skein1024-656", 0xB3B2), ("skein1024-664", 0xB3B3), - ("skein1024-672", 0xB3B4), ("skein1024-680", 0xB3B5), - ("skein1024-688", 0xB3B6), ("skein1024-696", 0xB3B7), - ("skein1024-704", 0xB3B8), ("skein1024-712", 0xB3B9), - ("skein1024-720", 0xB3BA), ("skein1024-728", 0xB3BB), - ("skein1024-736", 0xB3BC), ("skein1024-744", 0xB3BD), - ("skein1024-752", 0xB3BE), ("skein1024-760", 0xB3BF), - ("skein1024-768", 0xB3C0), ("skein1024-776", 0xB3C1), - ("skein1024-784", 0xB3C2), ("skein1024-792", 0xB3C3), - ("skein1024-800", 0xB3C4), ("skein1024-808", 0xB3C5), - ("skein1024-816", 0xB3C6), ("skein1024-824", 0xB3C7), - ("skein1024-832", 0xB3C8), ("skein1024-840", 0xB3C9), - ("skein1024-848", 0xB3CA), ("skein1024-856", 0xB3CB), - ("skein1024-864", 0xB3CC), ("skein1024-872", 0xB3CD), - ("skein1024-880", 0xB3CE), ("skein1024-888", 0xB3CF), - ("skein1024-896", 0xB3D0), ("skein1024-904", 0xB3D1), - ("skein1024-912", 0xB3D2), ("skein1024-920", 0xB3D3), - ("skein1024-928", 0xB3D4), ("skein1024-936", 0xB3D5), - ("skein1024-944", 0xB3D6), ("skein1024-952", 0xB3D7), - ("skein1024-960", 0xB3D8), ("skein1024-968", 0xB3D9), - ("skein1024-976", 0xB3DA), ("skein1024-984", 0xB3DB), - ("skein1024-992", 0xB3DC), ("skein1024-1000", 0xB3DD), - ("skein1024-1008", 0xB3DE), ("skein1024-1016", 0xB3DF), + ("blake2b-8", 0xB201), + ("blake2b-16", 0xB202), + ("blake2b-24", 0xB203), + ("blake2b-32", 0xB204), + ("blake2b-40", 0xB205), + ("blake2b-48", 0xB206), + ("blake2b-56", 0xB207), + ("blake2b-64", 0xB208), + ("blake2b-72", 0xB209), + ("blake2b-80", 0xB20A), + ("blake2b-88", 0xB20B), + ("blake2b-96", 0xB20C), + ("blake2b-104", 0xB20D), + ("blake2b-112", 0xB20E), + ("blake2b-120", 0xB20F), + ("blake2b-128", 0xB210), + ("blake2b-136", 0xB211), + ("blake2b-144", 0xB212), + ("blake2b-152", 0xB213), + ("blake2b-160", 0xB214), + ("blake2b-168", 0xB215), + ("blake2b-176", 0xB216), + ("blake2b-184", 0xB217), + ("blake2b-192", 0xB218), + ("blake2b-200", 0xB219), + ("blake2b-208", 0xB21A), + ("blake2b-216", 0xB21B), + ("blake2b-224", 0xB21C), + ("blake2b-232", 0xB21D), + ("blake2b-240", 0xB21E), + ("blake2b-248", 0xB21F), + ("blake2b-256", 0xB220), + ("blake2b-264", 0xB221), + ("blake2b-272", 0xB222), + ("blake2b-280", 0xB223), + ("blake2b-288", 0xB224), + ("blake2b-296", 0xB225), + ("blake2b-304", 0xB226), + ("blake2b-312", 0xB227), + ("blake2b-320", 0xB228), + ("blake2b-328", 0xB229), + ("blake2b-336", 0xB22A), + ("blake2b-344", 0xB22B), + ("blake2b-352", 0xB22C), + ("blake2b-360", 0xB22D), + ("blake2b-368", 0xB22E), + ("blake2b-376", 0xB22F), + ("blake2b-384", 0xB230), + ("blake2b-392", 0xB231), + ("blake2b-400", 0xB232), + ("blake2b-408", 0xB233), + ("blake2b-416", 0xB234), + ("blake2b-424", 0xB235), + ("blake2b-432", 0xB236), + ("blake2b-440", 0xB237), + ("blake2b-448", 0xB238), + ("blake2b-456", 0xB239), + ("blake2b-464", 0xB23A), + ("blake2b-472", 0xB23B), + ("blake2b-480", 0xB23C), + ("blake2b-488", 0xB23D), + ("blake2b-496", 0xB23E), + ("blake2b-504", 0xB23F), + ("blake2b-512", 0xB240), + ("blake2s-8", 0xB241), + ("blake2s-16", 0xB242), + ("blake2s-24", 0xB243), + ("blake2s-32", 0xB244), + ("blake2s-40", 0xB245), + ("blake2s-48", 0xB246), + ("blake2s-56", 0xB247), + ("blake2s-64", 0xB248), + ("blake2s-72", 0xB249), + ("blake2s-80", 0xB24A), + ("blake2s-88", 0xB24B), + ("blake2s-96", 0xB24C), + ("blake2s-104", 0xB24D), + ("blake2s-112", 0xB24E), + ("blake2s-120", 0xB24F), + ("blake2s-128", 0xB250), + ("blake2s-136", 0xB251), + ("blake2s-144", 0xB252), + ("blake2s-152", 0xB253), + ("blake2s-160", 0xB254), + ("blake2s-168", 0xB255), + ("blake2s-176", 0xB256), + ("blake2s-184", 0xB257), + ("blake2s-192", 0xB258), + ("blake2s-200", 0xB259), + ("blake2s-208", 0xB25A), + ("blake2s-216", 0xB25B), + ("blake2s-224", 0xB25C), + ("blake2s-232", 0xB25D), + ("blake2s-240", 0xB25E), + ("blake2s-248", 0xB25F), + ("blake2s-256", 0xB260), + ("skein256-8", 0xB301), + ("skein256-16", 0xB302), + ("skein256-24", 0xB303), + ("skein256-32", 0xB304), + ("skein256-40", 0xB305), + ("skein256-48", 0xB306), + ("skein256-56", 0xB307), + ("skein256-64", 0xB308), + ("skein256-72", 0xB309), + ("skein256-80", 0xB30A), + ("skein256-88", 0xB30B), + ("skein256-96", 0xB30C), + ("skein256-104", 0xB30D), + ("skein256-112", 0xB30E), + ("skein256-120", 0xB30F), + ("skein256-128", 0xB310), + ("skein256-136", 0xB311), + ("skein256-144", 0xB312), + ("skein256-152", 0xB313), + ("skein256-160", 0xB314), + ("skein256-168", 0xB315), + ("skein256-176", 0xB316), + ("skein256-184", 0xB317), + ("skein256-192", 0xB318), + ("skein256-200", 0xB319), + ("skein256-208", 0xB31A), + ("skein256-216", 0xB31B), + ("skein256-224", 0xB31C), + ("skein256-232", 0xB31D), + ("skein256-240", 0xB31E), + ("skein256-248", 0xB31F), + ("skein256-256", 0xB320), + ("skein512-8", 0xB321), + ("skein512-16", 0xB322), + ("skein512-24", 0xB323), + ("skein512-32", 0xB324), + ("skein512-40", 0xB325), + ("skein512-48", 0xB326), + ("skein512-56", 0xB327), + ("skein512-64", 0xB328), + ("skein512-72", 0xB329), + ("skein512-80", 0xB32A), + ("skein512-88", 0xB32B), + ("skein512-96", 0xB32C), + ("skein512-104", 0xB32D), + ("skein512-112", 0xB32E), + ("skein512-120", 0xB32F), + ("skein512-128", 0xB330), + ("skein512-136", 0xB331), + ("skein512-144", 0xB332), + ("skein512-152", 0xB333), + ("skein512-160", 0xB334), + ("skein512-168", 0xB335), + ("skein512-176", 0xB336), + ("skein512-184", 0xB337), + ("skein512-192", 0xB338), + ("skein512-200", 0xB339), + ("skein512-208", 0xB33A), + ("skein512-216", 0xB33B), + ("skein512-224", 0xB33C), + ("skein512-232", 0xB33D), + ("skein512-240", 0xB33E), + ("skein512-248", 0xB33F), + ("skein512-256", 0xB340), + ("skein512-264", 0xB341), + ("skein512-272", 0xB342), + ("skein512-280", 0xB343), + ("skein512-288", 0xB344), + ("skein512-296", 0xB345), + ("skein512-304", 0xB346), + ("skein512-312", 0xB347), + ("skein512-320", 0xB348), + ("skein512-328", 0xB349), + ("skein512-336", 0xB34A), + ("skein512-344", 0xB34B), + ("skein512-352", 0xB34C), + ("skein512-360", 0xB34D), + ("skein512-368", 0xB34E), + ("skein512-376", 0xB34F), + ("skein512-384", 0xB350), + ("skein512-392", 0xB351), + ("skein512-400", 0xB352), + ("skein512-408", 0xB353), + ("skein512-416", 0xB354), + ("skein512-424", 0xB355), + ("skein512-432", 0xB356), + ("skein512-440", 0xB357), + ("skein512-448", 0xB358), + ("skein512-456", 0xB359), + ("skein512-464", 0xB35A), + ("skein512-472", 0xB35B), + ("skein512-480", 0xB35C), + ("skein512-488", 0xB35D), + ("skein512-496", 0xB35E), + ("skein512-504", 0xB35F), + ("skein512-512", 0xB360), + ("skein1024-8", 0xB361), + ("skein1024-16", 0xB362), + ("skein1024-24", 0xB363), + ("skein1024-32", 0xB364), + ("skein1024-40", 0xB365), + ("skein1024-48", 0xB366), + ("skein1024-56", 0xB367), + ("skein1024-64", 0xB368), + ("skein1024-72", 0xB369), + ("skein1024-80", 0xB36A), + ("skein1024-88", 0xB36B), + ("skein1024-96", 0xB36C), + ("skein1024-104", 0xB36D), + ("skein1024-112", 0xB36E), + ("skein1024-120", 0xB36F), + ("skein1024-128", 0xB370), + ("skein1024-136", 0xB371), + ("skein1024-144", 0xB372), + ("skein1024-152", 0xB373), + ("skein1024-160", 0xB374), + ("skein1024-168", 0xB375), + ("skein1024-176", 0xB376), + ("skein1024-184", 0xB377), + ("skein1024-192", 0xB378), + ("skein1024-200", 0xB379), + ("skein1024-208", 0xB37A), + ("skein1024-216", 0xB37B), + ("skein1024-224", 0xB37C), + ("skein1024-232", 0xB37D), + ("skein1024-240", 0xB37E), + ("skein1024-248", 0xB37F), + ("skein1024-256", 0xB380), + ("skein1024-264", 0xB381), + ("skein1024-272", 0xB382), + ("skein1024-280", 0xB383), + ("skein1024-288", 0xB384), + ("skein1024-296", 0xB385), + ("skein1024-304", 0xB386), + ("skein1024-312", 0xB387), + ("skein1024-320", 0xB388), + ("skein1024-328", 0xB389), + ("skein1024-336", 0xB38A), + ("skein1024-344", 0xB38B), + ("skein1024-352", 0xB38C), + ("skein1024-360", 0xB38D), + ("skein1024-368", 0xB38E), + ("skein1024-376", 0xB38F), + ("skein1024-384", 0xB390), + ("skein1024-392", 0xB391), + ("skein1024-400", 0xB392), + ("skein1024-408", 0xB393), + ("skein1024-416", 0xB394), + ("skein1024-424", 0xB395), + ("skein1024-432", 0xB396), + ("skein1024-440", 0xB397), + ("skein1024-448", 0xB398), + ("skein1024-456", 0xB399), + ("skein1024-464", 0xB39A), + ("skein1024-472", 0xB39B), + ("skein1024-480", 0xB39C), + ("skein1024-488", 0xB39D), + ("skein1024-496", 0xB39E), + ("skein1024-504", 0xB39F), + ("skein1024-512", 0xB3A0), + ("skein1024-520", 0xB3A1), + ("skein1024-528", 0xB3A2), + ("skein1024-536", 0xB3A3), + ("skein1024-544", 0xB3A4), + ("skein1024-552", 0xB3A5), + ("skein1024-560", 0xB3A6), + ("skein1024-568", 0xB3A7), + ("skein1024-576", 0xB3A8), + ("skein1024-584", 0xB3A9), + ("skein1024-592", 0xB3AA), + ("skein1024-600", 0xB3AB), + ("skein1024-608", 0xB3AC), + ("skein1024-616", 0xB3AD), + ("skein1024-624", 0xB3AE), + ("skein1024-632", 0xB3AF), + ("skein1024-640", 0xB3B0), + ("skein1024-648", 0xB3B1), + ("skein1024-656", 0xB3B2), + ("skein1024-664", 0xB3B3), + ("skein1024-672", 0xB3B4), + ("skein1024-680", 0xB3B5), + ("skein1024-688", 0xB3B6), + ("skein1024-696", 0xB3B7), + ("skein1024-704", 0xB3B8), + ("skein1024-712", 0xB3B9), + ("skein1024-720", 0xB3BA), + ("skein1024-728", 0xB3BB), + ("skein1024-736", 0xB3BC), + ("skein1024-744", 0xB3BD), + ("skein1024-752", 0xB3BE), + ("skein1024-760", 0xB3BF), + ("skein1024-768", 0xB3C0), + ("skein1024-776", 0xB3C1), + ("skein1024-784", 0xB3C2), + ("skein1024-792", 0xB3C3), + ("skein1024-800", 0xB3C4), + ("skein1024-808", 0xB3C5), + ("skein1024-816", 0xB3C6), + ("skein1024-824", 0xB3C7), + ("skein1024-832", 0xB3C8), + ("skein1024-840", 0xB3C9), + ("skein1024-848", 0xB3CA), + ("skein1024-856", 0xB3CB), + ("skein1024-864", 0xB3CC), + ("skein1024-872", 0xB3CD), + ("skein1024-880", 0xB3CE), + ("skein1024-888", 0xB3CF), + ("skein1024-896", 0xB3D0), + ("skein1024-904", 0xB3D1), + ("skein1024-912", 0xB3D2), + ("skein1024-920", 0xB3D3), + ("skein1024-928", 0xB3D4), + ("skein1024-936", 0xB3D5), + ("skein1024-944", 0xB3D6), + ("skein1024-952", 0xB3D7), + ("skein1024-960", 0xB3D8), + ("skein1024-968", 0xB3D9), + ("skein1024-976", 0xB3DA), + ("skein1024-984", 0xB3DB), + ("skein1024-992", 0xB3DC), + ("skein1024-1000", 0xB3DD), + ("skein1024-1008", 0xB3DE), + ("skein1024-1016", 0xB3DF), ("skein1024-1024", 0xB3E0), # multiaddrs ("ip4", 0x04), @@ -233,7 +426,7 @@ const MultiCodecList = [ ("dash-tx", 0xF1), ("torrent-info", 0x7B), ("torrent-file", 0x7C), - ("ed25519-pub", 0xED) + ("ed25519-pub", 0xED), ] type @@ -241,8 +434,7 @@ type MultiCodecError* = enum MultiCodecNotSupported -const - InvalidMultiCodec* = MultiCodec(-1) +const InvalidMultiCodec* = MultiCodec(-1) proc initMultiCodecNameTable(): Table[string, int] {.compileTime.} = for item in MultiCodecList: diff --git a/libp2p/multihash.nim b/libp2p/multihash.nim index 4d890d1548..3d96d0506a 100644 --- a/libp2p/multihash.nim +++ b/libp2p/multihash.nim @@ -41,8 +41,9 @@ const ErrParseError = "Parse error fromHex" type - MHashCoderProc* = proc(data: openArray[byte], - output: var openArray[byte]) {.nimcall, gcsafe, noSideEffect, raises: [].} + MHashCoderProc* = proc(data: openArray[byte], output: var openArray[byte]) {. + nimcall, gcsafe, noSideEffect, raises: [] + .} MHash* = object mcodec*: MultiCodec size*: int @@ -58,107 +59,152 @@ type proc identhash(data: openArray[byte], output: var openArray[byte]) = if len(output) > 0: - var length = if len(data) > len(output): len(output) - else: len(data) + var length = + if len(data) > len(output): + len(output) + else: + len(data) copyMem(addr output[0], unsafeAddr data[0], length) proc sha1hash(data: openArray[byte], output: var openArray[byte]) = if len(output) > 0: var digest = sha1.digest(data) - var length = if sha1.sizeDigest > len(output): len(output) - else: sha1.sizeDigest + var length = + if sha1.sizeDigest > len(output): + len(output) + else: + sha1.sizeDigest copyMem(addr output[0], addr digest.data[0], length) proc dblsha2_256hash(data: openArray[byte], output: var openArray[byte]) = if len(output) > 0: var digest1 = sha256.digest(data) var digest2 = sha256.digest(digest1.data) - var length = if sha256.sizeDigest > len(output): len(output) - else: sha256.sizeDigest + var length = + if sha256.sizeDigest > len(output): + len(output) + else: + sha256.sizeDigest copyMem(addr output[0], addr digest2.data[0], length) proc blake2Bhash(data: openArray[byte], output: var openArray[byte]) = if len(output) > 0: var digest = blake2_512.digest(data) - var length = if blake2_512.sizeDigest > len(output): len(output) - else: blake2_512.sizeDigest + var length = + if blake2_512.sizeDigest > len(output): + len(output) + else: + blake2_512.sizeDigest copyMem(addr output[0], addr digest.data[0], length) proc blake2Shash(data: openArray[byte], output: var openArray[byte]) = if len(output) > 0: var digest = blake2_256.digest(data) - var length = if blake2_256.sizeDigest > len(output): len(output) - else: blake2_256.sizeDigest + var length = + if blake2_256.sizeDigest > len(output): + len(output) + else: + blake2_256.sizeDigest copyMem(addr output[0], addr digest.data[0], length) proc sha2_256hash(data: openArray[byte], output: var openArray[byte]) = if len(output) > 0: var digest = sha256.digest(data) - var length = if sha256.sizeDigest > len(output): len(output) - else: sha256.sizeDigest + var length = + if sha256.sizeDigest > len(output): + len(output) + else: + sha256.sizeDigest copyMem(addr output[0], addr digest.data[0], length) proc sha2_512hash(data: openArray[byte], output: var openArray[byte]) = if len(output) > 0: var digest = sha512.digest(data) - var length = if sha512.sizeDigest > len(output): len(output) - else: sha512.sizeDigest + var length = + if sha512.sizeDigest > len(output): + len(output) + else: + sha512.sizeDigest copyMem(addr output[0], addr digest.data[0], length) proc sha3_224hash(data: openArray[byte], output: var openArray[byte]) = if len(output) > 0: var digest = sha3_224.digest(data) - var length = if sha3_224.sizeDigest > len(output): len(output) - else: sha3_224.sizeDigest + var length = + if sha3_224.sizeDigest > len(output): + len(output) + else: + sha3_224.sizeDigest copyMem(addr output[0], addr digest.data[0], length) proc sha3_256hash(data: openArray[byte], output: var openArray[byte]) = if len(output) > 0: var digest = sha3_256.digest(data) - var length = if sha3_256.sizeDigest > len(output): len(output) - else: sha3_256.sizeDigest + var length = + if sha3_256.sizeDigest > len(output): + len(output) + else: + sha3_256.sizeDigest copyMem(addr output[0], addr digest.data[0], length) proc sha3_384hash(data: openArray[byte], output: var openArray[byte]) = if len(output) > 0: var digest = sha3_384.digest(data) - var length = if sha3_384.sizeDigest > len(output): len(output) - else: sha3_384.sizeDigest + var length = + if sha3_384.sizeDigest > len(output): + len(output) + else: + sha3_384.sizeDigest copyMem(addr output[0], addr digest.data[0], length) proc sha3_512hash(data: openArray[byte], output: var openArray[byte]) = if len(output) > 0: var digest = sha3_512.digest(data) - var length = if sha3_512.sizeDigest > len(output): len(output) - else: sha3_512.sizeDigest + var length = + if sha3_512.sizeDigest > len(output): + len(output) + else: + sha3_512.sizeDigest copyMem(addr output[0], addr digest.data[0], length) proc keccak_224hash(data: openArray[byte], output: var openArray[byte]) = if len(output) > 0: var digest = keccak224.digest(data) - var length = if keccak224.sizeDigest > len(output): len(output) - else: keccak224.sizeDigest + var length = + if keccak224.sizeDigest > len(output): + len(output) + else: + keccak224.sizeDigest copyMem(addr output[0], addr digest.data[0], length) proc keccak_256hash(data: openArray[byte], output: var openArray[byte]) = if len(output) > 0: var digest = keccak256.digest(data) - var length = if keccak256.sizeDigest > len(output): len(output) - else: keccak256.sizeDigest + var length = + if keccak256.sizeDigest > len(output): + len(output) + else: + keccak256.sizeDigest copyMem(addr output[0], addr digest.data[0], length) proc keccak_384hash(data: openArray[byte], output: var openArray[byte]) = if len(output) > 0: var digest = keccak384.digest(data) - var length = if keccak384.sizeDigest > len(output): len(output) - else: keccak384.sizeDigest + var length = + if keccak384.sizeDigest > len(output): + len(output) + else: + keccak384.sizeDigest copyMem(addr output[0], addr digest.data[0], length) proc keccak_512hash(data: openArray[byte], output: var openArray[byte]) = if len(output) > 0: var digest = keccak512.digest(data) - var length = if keccak512.sizeDigest > len(output): len(output) - else: keccak512.sizeDigest + var length = + if keccak512.sizeDigest > len(output): + len(output) + else: + keccak512.sizeDigest copyMem(addr output[0], addr digest.data[0], length) proc shake_128hash(data: openArray[byte], output: var openArray[byte]) = @@ -179,151 +225,135 @@ proc shake_256hash(data: openArray[byte], output: var openArray[byte]) = discard sctx.output(addr output[0], uint(len(output))) sctx.clear() -const - HashesList = [ - MHash(mcodec: multiCodec("identity"), size: 0, - coder: identhash), - MHash(mcodec: multiCodec("sha1"), size: sha1.sizeDigest, - coder: sha1hash), - MHash(mcodec: multiCodec("dbl-sha2-256"), size: sha256.sizeDigest, - coder: dblsha2_256hash - ), - MHash(mcodec: multiCodec("sha2-256"), size: sha256.sizeDigest, - coder: sha2_256hash - ), - MHash(mcodec: multiCodec("sha2-512"), size: sha512.sizeDigest, - coder: sha2_512hash - ), - MHash(mcodec: multiCodec("sha3-224"), size: sha3_224.sizeDigest, - coder: sha3_224hash - ), - MHash(mcodec: multiCodec("sha3-256"), size: sha3_256.sizeDigest, - coder: sha3_256hash - ), - MHash(mcodec: multiCodec("sha3-384"), size: sha3_384.sizeDigest, - coder: sha3_384hash - ), - MHash(mcodec: multiCodec("sha3-512"), size: sha3_512.sizeDigest, - coder: sha3_512hash - ), - MHash(mcodec: multiCodec("shake-128"), size: 32, coder: shake_128hash), - MHash(mcodec: multiCodec("shake-256"), size: 64, coder: shake_256hash), - MHash(mcodec: multiCodec("keccak-224"), size: keccak224.sizeDigest, - coder: keccak_224hash - ), - MHash(mcodec: multiCodec("keccak-256"), size: keccak256.sizeDigest, - coder: keccak_256hash - ), - MHash(mcodec: multiCodec("keccak-384"), size: keccak384.sizeDigest, - coder: keccak_384hash - ), - MHash(mcodec: multiCodec("keccak-512"), size: keccak512.sizeDigest, - coder: keccak_512hash - ), - MHash(mcodec: multiCodec("blake2b-8"), size: 1, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-16"), size: 2, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-24"), size: 3, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-32"), size: 4, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-40"), size: 5, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-48"), size: 6, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-56"), size: 7, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-64"), size: 8, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-72"), size: 9, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-80"), size: 10, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-88"), size: 11, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-96"), size: 12, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-104"), size: 13, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-112"), size: 14, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-120"), size: 15, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-128"), size: 16, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-136"), size: 17, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-144"), size: 18, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-152"), size: 19, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-160"), size: 20, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-168"), size: 21, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-176"), size: 22, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-184"), size: 23, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-192"), size: 24, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-200"), size: 25, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-208"), size: 26, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-216"), size: 27, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-224"), size: 28, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-232"), size: 29, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-240"), size: 30, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-248"), size: 31, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-256"), size: 32, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-264"), size: 33, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-272"), size: 34, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-280"), size: 35, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-288"), size: 36, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-296"), size: 37, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-304"), size: 38, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-312"), size: 39, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-320"), size: 40, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-328"), size: 41, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-336"), size: 42, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-344"), size: 43, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-352"), size: 44, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-360"), size: 45, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-368"), size: 46, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-376"), size: 47, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-384"), size: 48, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-392"), size: 49, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-400"), size: 50, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-408"), size: 51, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-416"), size: 52, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-424"), size: 53, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-432"), size: 54, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-440"), size: 55, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-448"), size: 56, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-456"), size: 57, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-464"), size: 58, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-472"), size: 59, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-480"), size: 60, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-488"), size: 61, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-496"), size: 62, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-504"), size: 63, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2b-512"), size: 64, coder: blake2Bhash), - MHash(mcodec: multiCodec("blake2s-8"), size: 1, coder: blake2Shash), - MHash(mcodec: multiCodec("blake2s-16"), size: 2, coder: blake2Shash), - MHash(mcodec: multiCodec("blake2s-24"), size: 3, coder: blake2Shash), - MHash(mcodec: multiCodec("blake2s-32"), size: 4, coder: blake2Shash), - MHash(mcodec: multiCodec("blake2s-40"), size: 5, coder: blake2Shash), - MHash(mcodec: multiCodec("blake2s-48"), size: 6, coder: blake2Shash), - MHash(mcodec: multiCodec("blake2s-56"), size: 7, coder: blake2Shash), - MHash(mcodec: multiCodec("blake2s-64"), size: 8, coder: blake2Shash), - MHash(mcodec: multiCodec("blake2s-72"), size: 9, coder: blake2Shash), - MHash(mcodec: multiCodec("blake2s-80"), size: 10, coder: blake2Shash), - MHash(mcodec: multiCodec("blake2s-88"), size: 11, coder: blake2Shash), - MHash(mcodec: multiCodec("blake2s-96"), size: 12, coder: blake2Shash), - MHash(mcodec: multiCodec("blake2s-104"), size: 13, coder: blake2Shash), - MHash(mcodec: multiCodec("blake2s-112"), size: 14, coder: blake2Shash), - MHash(mcodec: multiCodec("blake2s-120"), size: 15, coder: blake2Shash), - MHash(mcodec: multiCodec("blake2s-128"), size: 16, coder: blake2Shash), - MHash(mcodec: multiCodec("blake2s-136"), size: 17, coder: blake2Shash), - MHash(mcodec: multiCodec("blake2s-144"), size: 18, coder: blake2Shash), - MHash(mcodec: multiCodec("blake2s-152"), size: 19, coder: blake2Shash), - MHash(mcodec: multiCodec("blake2s-160"), size: 20, coder: blake2Shash), - MHash(mcodec: multiCodec("blake2s-168"), size: 21, coder: blake2Shash), - MHash(mcodec: multiCodec("blake2s-176"), size: 22, coder: blake2Shash), - MHash(mcodec: multiCodec("blake2s-184"), size: 23, coder: blake2Shash), - MHash(mcodec: multiCodec("blake2s-192"), size: 24, coder: blake2Shash), - MHash(mcodec: multiCodec("blake2s-200"), size: 25, coder: blake2Shash), - MHash(mcodec: multiCodec("blake2s-208"), size: 26, coder: blake2Shash), - MHash(mcodec: multiCodec("blake2s-216"), size: 27, coder: blake2Shash), - MHash(mcodec: multiCodec("blake2s-224"), size: 28, coder: blake2Shash), - MHash(mcodec: multiCodec("blake2s-232"), size: 29, coder: blake2Shash), - MHash(mcodec: multiCodec("blake2s-240"), size: 30, coder: blake2Shash), - MHash(mcodec: multiCodec("blake2s-248"), size: 31, coder: blake2Shash), - MHash(mcodec: multiCodec("blake2s-256"), size: 32, coder: blake2Shash) - ] +const HashesList = [ + MHash(mcodec: multiCodec("identity"), size: 0, coder: identhash), + MHash(mcodec: multiCodec("sha1"), size: sha1.sizeDigest, coder: sha1hash), + MHash( + mcodec: multiCodec("dbl-sha2-256"), size: sha256.sizeDigest, coder: dblsha2_256hash + ), + MHash(mcodec: multiCodec("sha2-256"), size: sha256.sizeDigest, coder: sha2_256hash), + MHash(mcodec: multiCodec("sha2-512"), size: sha512.sizeDigest, coder: sha2_512hash), + MHash(mcodec: multiCodec("sha3-224"), size: sha3_224.sizeDigest, coder: sha3_224hash), + MHash(mcodec: multiCodec("sha3-256"), size: sha3_256.sizeDigest, coder: sha3_256hash), + MHash(mcodec: multiCodec("sha3-384"), size: sha3_384.sizeDigest, coder: sha3_384hash), + MHash(mcodec: multiCodec("sha3-512"), size: sha3_512.sizeDigest, coder: sha3_512hash), + MHash(mcodec: multiCodec("shake-128"), size: 32, coder: shake_128hash), + MHash(mcodec: multiCodec("shake-256"), size: 64, coder: shake_256hash), + MHash( + mcodec: multiCodec("keccak-224"), size: keccak224.sizeDigest, coder: keccak_224hash + ), + MHash( + mcodec: multiCodec("keccak-256"), size: keccak256.sizeDigest, coder: keccak_256hash + ), + MHash( + mcodec: multiCodec("keccak-384"), size: keccak384.sizeDigest, coder: keccak_384hash + ), + MHash( + mcodec: multiCodec("keccak-512"), size: keccak512.sizeDigest, coder: keccak_512hash + ), + MHash(mcodec: multiCodec("blake2b-8"), size: 1, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-16"), size: 2, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-24"), size: 3, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-32"), size: 4, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-40"), size: 5, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-48"), size: 6, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-56"), size: 7, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-64"), size: 8, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-72"), size: 9, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-80"), size: 10, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-88"), size: 11, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-96"), size: 12, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-104"), size: 13, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-112"), size: 14, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-120"), size: 15, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-128"), size: 16, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-136"), size: 17, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-144"), size: 18, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-152"), size: 19, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-160"), size: 20, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-168"), size: 21, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-176"), size: 22, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-184"), size: 23, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-192"), size: 24, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-200"), size: 25, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-208"), size: 26, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-216"), size: 27, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-224"), size: 28, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-232"), size: 29, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-240"), size: 30, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-248"), size: 31, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-256"), size: 32, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-264"), size: 33, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-272"), size: 34, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-280"), size: 35, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-288"), size: 36, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-296"), size: 37, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-304"), size: 38, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-312"), size: 39, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-320"), size: 40, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-328"), size: 41, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-336"), size: 42, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-344"), size: 43, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-352"), size: 44, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-360"), size: 45, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-368"), size: 46, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-376"), size: 47, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-384"), size: 48, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-392"), size: 49, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-400"), size: 50, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-408"), size: 51, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-416"), size: 52, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-424"), size: 53, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-432"), size: 54, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-440"), size: 55, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-448"), size: 56, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-456"), size: 57, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-464"), size: 58, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-472"), size: 59, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-480"), size: 60, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-488"), size: 61, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-496"), size: 62, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-504"), size: 63, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2b-512"), size: 64, coder: blake2Bhash), + MHash(mcodec: multiCodec("blake2s-8"), size: 1, coder: blake2Shash), + MHash(mcodec: multiCodec("blake2s-16"), size: 2, coder: blake2Shash), + MHash(mcodec: multiCodec("blake2s-24"), size: 3, coder: blake2Shash), + MHash(mcodec: multiCodec("blake2s-32"), size: 4, coder: blake2Shash), + MHash(mcodec: multiCodec("blake2s-40"), size: 5, coder: blake2Shash), + MHash(mcodec: multiCodec("blake2s-48"), size: 6, coder: blake2Shash), + MHash(mcodec: multiCodec("blake2s-56"), size: 7, coder: blake2Shash), + MHash(mcodec: multiCodec("blake2s-64"), size: 8, coder: blake2Shash), + MHash(mcodec: multiCodec("blake2s-72"), size: 9, coder: blake2Shash), + MHash(mcodec: multiCodec("blake2s-80"), size: 10, coder: blake2Shash), + MHash(mcodec: multiCodec("blake2s-88"), size: 11, coder: blake2Shash), + MHash(mcodec: multiCodec("blake2s-96"), size: 12, coder: blake2Shash), + MHash(mcodec: multiCodec("blake2s-104"), size: 13, coder: blake2Shash), + MHash(mcodec: multiCodec("blake2s-112"), size: 14, coder: blake2Shash), + MHash(mcodec: multiCodec("blake2s-120"), size: 15, coder: blake2Shash), + MHash(mcodec: multiCodec("blake2s-128"), size: 16, coder: blake2Shash), + MHash(mcodec: multiCodec("blake2s-136"), size: 17, coder: blake2Shash), + MHash(mcodec: multiCodec("blake2s-144"), size: 18, coder: blake2Shash), + MHash(mcodec: multiCodec("blake2s-152"), size: 19, coder: blake2Shash), + MHash(mcodec: multiCodec("blake2s-160"), size: 20, coder: blake2Shash), + MHash(mcodec: multiCodec("blake2s-168"), size: 21, coder: blake2Shash), + MHash(mcodec: multiCodec("blake2s-176"), size: 22, coder: blake2Shash), + MHash(mcodec: multiCodec("blake2s-184"), size: 23, coder: blake2Shash), + MHash(mcodec: multiCodec("blake2s-192"), size: 24, coder: blake2Shash), + MHash(mcodec: multiCodec("blake2s-200"), size: 25, coder: blake2Shash), + MHash(mcodec: multiCodec("blake2s-208"), size: 26, coder: blake2Shash), + MHash(mcodec: multiCodec("blake2s-216"), size: 27, coder: blake2Shash), + MHash(mcodec: multiCodec("blake2s-224"), size: 28, coder: blake2Shash), + MHash(mcodec: multiCodec("blake2s-232"), size: 29, coder: blake2Shash), + MHash(mcodec: multiCodec("blake2s-240"), size: 30, coder: blake2Shash), + MHash(mcodec: multiCodec("blake2s-248"), size: 31, coder: blake2Shash), + MHash(mcodec: multiCodec("blake2s-256"), size: 32, coder: blake2Shash), +] proc initMultiHashCodeTable(): Table[MultiCodec, MHash] {.compileTime.} = for item in HashesList: result[item.mcodec] = item -const - CodeHashes = initMultiHashCodeTable() +const CodeHashes = initMultiHashCodeTable() proc digestImplWithHash(hash: MHash, data: openArray[byte]): MultiHash = var buffer: array[MaxHashSize, byte] @@ -353,8 +383,9 @@ proc digestImplWithoutHash(hash: MHash, data: openArray[byte]): MultiHash = result.data.writeArray(data) result.data.finish() -proc digest*(mhtype: typedesc[MultiHash], hashname: string, - data: openArray[byte]): MhResult[MultiHash] {.inline.} = +proc digest*( + mhtype: typedesc[MultiHash], hashname: string, data: openArray[byte] +): MhResult[MultiHash] {.inline.} = ## Perform digest calculation using hash algorithm with name ``hashname`` on ## data array ``data``. let mc = MultiCodec.codec(hashname) @@ -367,8 +398,9 @@ proc digest*(mhtype: typedesc[MultiHash], hashname: string, else: ok(digestImplWithHash(hash, data)) -proc digest*(mhtype: typedesc[MultiHash], hashcode: int, - data: openArray[byte]): MhResult[MultiHash] {.inline.} = +proc digest*( + mhtype: typedesc[MultiHash], hashcode: int, data: openArray[byte] +): MhResult[MultiHash] {.inline.} = ## Perform digest calculation using hash algorithm with code ``hashcode`` on ## data array ``data``. let hash = CodeHashes.getOrDefault(hashcode) @@ -377,8 +409,9 @@ proc digest*(mhtype: typedesc[MultiHash], hashcode: int, else: ok(digestImplWithHash(hash, data)) -proc init*[T](mhtype: typedesc[MultiHash], hashname: string, - mdigest: MDigest[T]): MhResult[MultiHash] {.inline.} = +proc init*[T]( + mhtype: typedesc[MultiHash], hashname: string, mdigest: MDigest[T] +): MhResult[MultiHash] {.inline.} = ## Create MultiHash from nimcrypto's `MDigest` object and hash algorithm name ## ``hashname``. let mc = MultiCodec.codec(hashname) @@ -393,8 +426,9 @@ proc init*[T](mhtype: typedesc[MultiHash], hashname: string, else: ok(digestImplWithoutHash(hash, mdigest.data)) -proc init*[T](mhtype: typedesc[MultiHash], hashcode: MultiCodec, - mdigest: MDigest[T]): MhResult[MultiHash] {.inline.} = +proc init*[T]( + mhtype: typedesc[MultiHash], hashcode: MultiCodec, mdigest: MDigest[T] +): MhResult[MultiHash] {.inline.} = ## Create MultiHash from nimcrypto's `MDigest` and hash algorithm code ## ``hashcode``. let hash = CodeHashes.getOrDefault(hashcode) @@ -405,8 +439,9 @@ proc init*[T](mhtype: typedesc[MultiHash], hashcode: MultiCodec, else: ok(digestImplWithoutHash(hash, mdigest.data)) -proc init*(mhtype: typedesc[MultiHash], hashname: string, - bdigest: openArray[byte]): MhResult[MultiHash] {.inline.} = +proc init*( + mhtype: typedesc[MultiHash], hashname: string, bdigest: openArray[byte] +): MhResult[MultiHash] {.inline.} = ## Create MultiHash from array of bytes ``bdigest`` and hash algorithm code ## ``hashcode``. let mc = MultiCodec.codec(hashname) @@ -421,8 +456,9 @@ proc init*(mhtype: typedesc[MultiHash], hashname: string, else: ok(digestImplWithoutHash(hash, bdigest)) -proc init*(mhtype: typedesc[MultiHash], hashcode: MultiCodec, - bdigest: openArray[byte]): MhResult[MultiHash] {.inline.} = +proc init*( + mhtype: typedesc[MultiHash], hashcode: MultiCodec, bdigest: openArray[byte] +): MhResult[MultiHash] {.inline.} = ## Create MultiHash from array of bytes ``bdigest`` and hash algorithm code ## ``hashcode``. let hash = CodeHashes.getOrDefault(hashcode) @@ -433,8 +469,9 @@ proc init*(mhtype: typedesc[MultiHash], hashcode: MultiCodec, else: ok(digestImplWithoutHash(hash, bdigest)) -proc decode*(mhtype: typedesc[MultiHash], data: openArray[byte], - mhash: var MultiHash): MhResult[int] = +proc decode*( + mhtype: typedesc[MultiHash], data: openArray[byte], mhash: var MultiHash +): MhResult[int] = ## Decode MultiHash value from array of bytes ``data``. ## ## On success decoded MultiHash will be stored into ``mhash`` and number of @@ -473,9 +510,10 @@ proc decode*(mhtype: typedesc[MultiHash], data: openArray[byte], if not vb.isEnough(int(size)): return err(ErrDecodeError) - mhash = ? MultiHash.init(MultiCodec(code), - vb.buffer.toOpenArray(vb.offset, - vb.offset + int(size) - 1)) + mhash = + ?MultiHash.init( + MultiCodec(code), vb.buffer.toOpenArray(vb.offset, vb.offset + int(size) - 1) + ) ok(vb.offset + int(size)) proc validate*(mhtype: typedesc[MultiHash], data: openArray[byte]): bool = @@ -508,24 +546,24 @@ proc validate*(mhtype: typedesc[MultiHash], data: openArray[byte]): bool = return false result = true -proc init*(mhtype: typedesc[MultiHash], - data: openArray[byte]): MhResult[MultiHash] {.inline.} = +proc init*( + mhtype: typedesc[MultiHash], data: openArray[byte] +): MhResult[MultiHash] {.inline.} = ## Create MultiHash from bytes array ``data``. var hash: MultiHash - discard ? MultiHash.decode(data, hash) + discard ?MultiHash.decode(data, hash) ok(hash) proc init*(mhtype: typedesc[MultiHash], data: string): MhResult[MultiHash] {.inline.} = ## Create MultiHash from hexadecimal string representation ``data``. var hash: MultiHash try: - discard ? MultiHash.decode(fromHex(data), hash) + discard ?MultiHash.decode(fromHex(data), hash) ok(hash) except ValueError: err(ErrParseError) -proc init58*(mhtype: typedesc[MultiHash], - data: string): MultiHash {.inline.} = +proc init58*(mhtype: typedesc[MultiHash], data: string): MultiHash {.inline.} = ## Create MultiHash from BASE58 encoded string representation ``data``. if MultiHash.decode(Base58.decode(data), result) == -1: raise newException(MultihashError, "Incorrect MultiHash binary format") @@ -538,7 +576,7 @@ proc cmp(a: openArray[byte], b: openArray[byte]): bool {.inline.} = while n > 0: dec(n) diff = int(a[n]) - int(b[n]) - res = (res and -not(diff)) or diff + res = (res and - not (diff)) or diff result = (res == 0) proc `==`*[T](mh: MultiHash, mdigest: MDigest[T]): bool = @@ -548,8 +586,10 @@ proc `==`*[T](mh: MultiHash, mdigest: MDigest[T]): bool = return false if len(mdigest.data) != mh.size: return false - result = cmp(mh.data.buffer.toOpenArray(mh.dpos, mh.dpos + mh.size - 1), - mdigest.data.toOpenArray(0, mdigest.data.high)) + result = cmp( + mh.data.buffer.toOpenArray(mh.dpos, mh.dpos + mh.size - 1), + mdigest.data.toOpenArray(0, mdigest.data.high), + ) proc `==`*[T](mdigest: MDigest[T], mh: MultiHash): bool {.inline.} = ## Compares MultiHash with nimcrypto's MDigest[T], returns ``true`` if @@ -565,8 +605,10 @@ proc `==`*(a: MultiHash, b: MultiHash): bool = return false if a.size != b.size: return false - result = cmp(a.data.buffer.toOpenArray(a.dpos, a.dpos + a.size - 1), - b.data.buffer.toOpenArray(b.dpos, b.dpos + b.size - 1)) + result = cmp( + a.data.buffer.toOpenArray(a.dpos, a.dpos + a.size - 1), + b.data.buffer.toOpenArray(b.dpos, b.dpos + b.size - 1), + ) proc hex*(value: MultiHash): string = ## Return hexadecimal string representation of MultiHash ``value``. @@ -578,16 +620,16 @@ proc base58*(value: MultiHash): string = proc `$`*(mh: MultiHash): string = ## Return string representation of MultiHash ``value``. - let digest = toHex(mh.data.buffer.toOpenArray(mh.dpos, - mh.dpos + mh.size - 1)) + let digest = toHex(mh.data.buffer.toOpenArray(mh.dpos, mh.dpos + mh.size - 1)) result = $(mh.mcodec) & "/" & digest proc write*(vb: var VBuffer, mh: MultiHash) {.inline.} = ## Write MultiHash value ``mh`` to buffer ``vb``. vb.writeArray(mh.data.buffer) -proc encode*(mbtype: typedesc[MultiBase], encoding: string, - mh: MultiHash): string {.inline.} = +proc encode*( + mbtype: typedesc[MultiBase], encoding: string, mh: MultiHash +): string {.inline.} = ## Get MultiBase encoded representation of ``mh`` using encoding ## ``encoding``. result = MultiBase.encode(encoding, mh.data.buffer) diff --git a/libp2p/multistream.nim b/libp2p/multistream.nim index 51931041af..4e6bf8d819 100644 --- a/libp2p/multistream.nim +++ b/libp2p/multistream.nim @@ -11,8 +11,7 @@ import std/[strutils, sequtils, tables] import chronos, chronicles, stew/byteutils -import stream/connection, - protocols/protocol +import stream/connection, protocols/protocol logScope: topics = "libp2p multistream" @@ -25,7 +24,7 @@ const Ls = "ls\n" type - Matcher* = proc (proto: string): bool {.gcsafe, raises: [].} + Matcher* = proc(proto: string): bool {.gcsafe, raises: [].} MultiStreamError* = object of LPError @@ -40,23 +39,17 @@ type codec*: string proc new*(T: typedesc[MultistreamSelect]): T = - T( - codec: Codec, - ) + T(codec: Codec) template validateSuffix(str: string): untyped = if str.endsWith("\n"): str.removeSuffix("\n") else: - raise (ref MultiStreamError)(msg: - "MultistreamSelect failed, malformed message") + raise (ref MultiStreamError)(msg: "MultistreamSelect failed, malformed message") proc select*( - _: MultistreamSelect | type MultistreamSelect, - conn: Connection, - proto: seq[string] -): Future[string] {.async: (raises: [ - CancelledError, LPStreamError, MultiStreamError]).} = + _: MultistreamSelect | type MultistreamSelect, conn: Connection, proto: seq[string] +): Future[string] {.async: (raises: [CancelledError, LPStreamError, MultiStreamError]).} = trace "initiating handshake", conn, codec = Codec ## select a remote protocol await conn.writeLp(Codec & "\n") # write handshake @@ -85,7 +78,7 @@ proc select*( return proto[0] elif proto.len > 1: # Try to negotiate alternatives - let protos = proto[1.. 0: (await MultistreamSelect.select(conn, @[proto])) == proto else: (await MultistreamSelect.select(conn, @[])) == Codec proc select*( - m: MultistreamSelect, - conn: Connection -): Future[bool] {.async: (raises: [ - CancelledError, LPStreamError, MultiStreamError], raw: true).} = + m: MultistreamSelect, conn: Connection +): Future[bool] {. + async: (raises: [CancelledError, LPStreamError, MultiStreamError], raw: true) +.} = m.select(conn, "") proc list*( - m: MultistreamSelect, - conn: Connection -): Future[seq[string]] {.async: (raises: [ - CancelledError, LPStreamError, MultiStreamError]).} = + m: MultistreamSelect, conn: Connection +): Future[seq[string]] {. + async: (raises: [CancelledError, LPStreamError, MultiStreamError]) +.} = ## list remote protos requests on connection if not await m.select(conn): return @@ -143,9 +133,8 @@ proc handle*( conn: Connection, protos: seq[string], matchers = newSeq[Matcher](), - active: bool = false -): Future[string] {.async: (raises: [ - CancelledError, LPStreamError, MultiStreamError]).} = + active: bool = false, +): Future[string] {.async: (raises: [CancelledError, LPStreamError, MultiStreamError]).} = trace "Starting multistream negotiation", conn, handshaked = active var handshaked = active while not conn.atEof: @@ -153,16 +142,17 @@ proc handle*( validateSuffix(ms) if not handshaked and ms != Codec: - debug "expected handshake message", conn, instead=ms - raise (ref MultiStreamError)(msg: - "MultistreamSelect handling failed, invalid first message") + debug "expected handshake message", conn, instead = ms + raise (ref MultiStreamError)( + msg: "MultistreamSelect handling failed, invalid first message" + ) trace "handle: got request", conn, ms if ms.len() <= 0: trace "handle: invalid proto", conn await conn.writeLp(Na) - case ms: + case ms of "ls": trace "handle: listing protos", conn #TODO this doens't seem to follow spec, each protocol @@ -174,8 +164,7 @@ proc handle*( await conn.writeLp(Codec & "\n") handshaked = true else: - trace "handle: sending `na` for duplicate handshake while handshaked", - conn + trace "handle: sending `na` for duplicate handshake while handshaked", conn await conn.writeLp(Na) elif ms in protos or matchers.anyIt(it(ms)): trace "found handler", conn, protocol = ms @@ -187,9 +176,8 @@ proc handle*( await conn.writeLp(Na) proc handle*( - m: MultistreamSelect, - conn: Connection, - active: bool = false) {.async: (raises: [CancelledError]).} = + m: MultistreamSelect, conn: Connection, active: bool = false +) {.async: (raises: [CancelledError]).} = trace "Starting multistream handler", conn, handshaked = active var protos: seq[string] @@ -208,8 +196,7 @@ proc handle*( var protocolHolder = h let maxIncomingStreams = protocolHolder.protocol.maxIncomingStreams - if protocolHolder.openedStreams.getOrDefault(conn.peerId) >= - maxIncomingStreams: + if protocolHolder.openedStreams.getOrDefault(conn.peerId) >= maxIncomingStreams: debug "Max streams for protocol reached, blocking new stream", conn, protocol = ms, maxIncomingStreams return @@ -231,38 +218,35 @@ proc handle*( trace "Stopped multistream handler", conn -proc addHandler*(m: MultistreamSelect, - codecs: seq[string], - protocol: LPProtocol, - matcher: Matcher = nil) = +proc addHandler*( + m: MultistreamSelect, + codecs: seq[string], + protocol: LPProtocol, + matcher: Matcher = nil, +) = trace "registering protocols", protos = codecs - m.handlers.add(HandlerHolder(protos: codecs, - protocol: protocol, - match: matcher)) - -proc addHandler*(m: MultistreamSelect, - codec: string, - protocol: LPProtocol, - matcher: Matcher = nil) = + m.handlers.add(HandlerHolder(protos: codecs, protocol: protocol, match: matcher)) + +proc addHandler*( + m: MultistreamSelect, codec: string, protocol: LPProtocol, matcher: Matcher = nil +) = addHandler(m, @[codec], protocol, matcher) proc addHandler*[E]( m: MultistreamSelect, codec: string, - handler: LPProtoHandler | - proc ( - conn: Connection, - proto: string): InternalRaisesFuture[void, E], - matcher: Matcher = nil) = + handler: + LPProtoHandler | + proc(conn: Connection, proto: string): InternalRaisesFuture[void, E], + matcher: Matcher = nil, +) = ## helper to allow registering pure handlers trace "registering proto handler", proto = codec let protocol = new LPProtocol protocol.codec = codec protocol.handler = handler - m.handlers.add(HandlerHolder(protos: @[codec], - protocol: protocol, - match: matcher)) + m.handlers.add(HandlerHolder(protos: @[codec], protocol: protocol, match: matcher)) proc start*(m: MultistreamSelect) {.async: (raises: [CancelledError]).} = # Nim 1.6.18: Using `mapIt` results in a seq of `.Raising([])` @@ -283,7 +267,8 @@ proc start*(m: MultistreamSelect) {.async: (raises: [CancelledError]).} = elif fut.completed: pending.add m.handlers[i].protocol.stop() else: - static: doAssert typeof(fut).E is (CancelledError,) + static: + doAssert typeof(fut).E is (CancelledError,) await noCancel allFutures(pending) raise exc diff --git a/libp2p/muxers/mplex/coder.nim b/libp2p/muxers/mplex/coder.nim index 701879a2fe..445d486001 100644 --- a/libp2p/muxers/mplex/coder.nim +++ b/libp2p/muxers/mplex/coder.nim @@ -10,29 +10,22 @@ {.push raises: [].} import pkg/[chronos, chronicles, stew/byteutils] -import ../../stream/connection, - ../../utility, - ../../varint, - ../../vbuffer, - ../muxer +import ../../stream/connection, ../../utility, ../../varint, ../../vbuffer, ../muxer logScope: topics = "libp2p mplexcoder" type MessageType* {.pure.} = enum - New, - MsgIn, - MsgOut, - CloseIn, - CloseOut, - ResetIn, + New + MsgIn + MsgOut + CloseIn + CloseOut + ResetIn ResetOut - Msg* = tuple - id: uint64 - msgType: MessageType - data: seq[byte] + Msg* = tuple[id: uint64, msgType: MessageType, data: seq[byte]] InvalidMplexMsgType* = object of MuxerError @@ -44,8 +37,7 @@ proc newInvalidMplexMsgType*(): ref InvalidMplexMsgType = proc readMsg*( conn: Connection -): Future[Msg] {.async: (raises: [ - CancelledError, LPStreamError, MuxerError]).} = +): Future[Msg] {.async: (raises: [CancelledError, LPStreamError, MuxerError]).} = let header = await conn.readVarint() trace "read header varint", varint = header, conn @@ -59,12 +51,8 @@ proc readMsg*( return (header shr 3, MessageType(msgType), data) proc writeMsg*( - conn: Connection, - id: uint64, - msgType: MessageType, - data: seq[byte] = @[] -): Future[void] {.async: (raises: [ - CancelledError, LPStreamError], raw: true).} = + conn: Connection, id: uint64, msgType: MessageType, data: seq[byte] = @[] +): Future[void] {.async: (raises: [CancelledError, LPStreamError], raw: true).} = var left = data.len offset = 0 @@ -72,8 +60,11 @@ proc writeMsg*( # Split message into length-prefixed chunks while left > 0 or data.len == 0: - let - chunkSize = if left > MaxMsgSize: MaxMsgSize - 64 else: left + let chunkSize = + if left > MaxMsgSize: + MaxMsgSize - 64 + else: + left buf.writePBVarint(id shl 3 or ord(msgType).uint64) buf.writeSeq(data.toOpenArray(offset, offset + chunkSize - 1)) @@ -91,10 +82,6 @@ proc writeMsg*( conn.write(buf.buffer) proc writeMsg*( - conn: Connection, - id: uint64, - msgType: MessageType, - data: string -): Future[void] {.async: (raises: [ - CancelledError, LPStreamError], raw: true).} = + conn: Connection, id: uint64, msgType: MessageType, data: string +): Future[void] {.async: (raises: [CancelledError, LPStreamError], raw: true).} = conn.writeMsg(id, msgType, data.toBytes()) diff --git a/libp2p/muxers/mplex/lpchannel.nim b/libp2p/muxers/mplex/lpchannel.nim index f43c092a2e..a6b5c51c6d 100644 --- a/libp2p/muxers/mplex/lpchannel.nim +++ b/libp2p/muxers/mplex/lpchannel.nim @@ -11,10 +11,8 @@ import std/[oids, strformat] import pkg/[chronos, chronicles, metrics] -import ./coder, - ../muxer, - ../../stream/[bufferstream, connection, streamseq], - ../../peerinfo +import + ./coder, ../muxer, ../../stream/[bufferstream, connection, streamseq], ../../peerinfo export connection @@ -22,7 +20,8 @@ logScope: topics = "libp2p mplexchannel" when defined(libp2p_mplex_metrics): - declareHistogram libp2p_mplex_qlen, "message queue length", + declareHistogram libp2p_mplex_qlen, + "message queue length", buckets = [0.0, 1.0, 2.0, 4.0, 8.0, 16.0, 32.0, 64.0, 128.0, 256.0, 512.0] declareCounter libp2p_mplex_qlenclose, "closed because of max queuelen" declareHistogram libp2p_mplex_qtime, "message queuing time" @@ -43,36 +42,39 @@ when defined(libp2p_network_protocols_metrics): ## EOF marker const - MaxWrites = 1024 ##\ + MaxWrites = 1024 + ##\ ## Maximum number of in-flight writes - after this, we disconnect the peer LPChannelTrackerName* = "LPChannel" -type - LPChannel* = ref object of BufferStream - id*: uint64 # channel id - name*: string # name of the channel (for debugging) - conn*: Connection # wrapped connection used to for writing - initiator*: bool # initiated remotely or locally flag - isOpen*: bool # has channel been opened - closedLocal*: bool # has channel been closed locally - remoteReset*: bool # has channel been remotely reset - localReset*: bool # has channel been reset locally - msgCode*: MessageType # cached in/out message code - closeCode*: MessageType # cached in/out close code - resetCode*: MessageType # cached in/out reset code - writes*: int # In-flight writes +type LPChannel* = ref object of BufferStream + id*: uint64 # channel id + name*: string # name of the channel (for debugging) + conn*: Connection # wrapped connection used to for writing + initiator*: bool # initiated remotely or locally flag + isOpen*: bool # has channel been opened + closedLocal*: bool # has channel been closed locally + remoteReset*: bool # has channel been remotely reset + localReset*: bool # has channel been reset locally + msgCode*: MessageType # cached in/out message code + closeCode*: MessageType # cached in/out close code + resetCode*: MessageType # cached in/out reset code + writes*: int # In-flight writes func shortLog*(s: LPChannel): auto = try: - if s == nil: "LPChannel(nil)" + if s == nil: + "LPChannel(nil)" elif s.name != $s.oid and s.name.len > 0: &"{shortLog(s.conn.peerId)}:{s.oid}:{s.name}" - else: &"{shortLog(s.conn.peerId)}:{s.oid}" + else: + &"{shortLog(s.conn.peerId)}:{s.oid}" except ValueError as exc: raiseAssert(exc.msg) -chronicles.formatIt(LPChannel): shortLog(it) +chronicles.formatIt(LPChannel): + shortLog(it) proc open*(s: LPChannel) {.async: (raises: [CancelledError, LPStreamError]).} = trace "Opening channel", s, conn = s.conn @@ -160,9 +162,7 @@ method initStream*(s: LPChannel) = procCall BufferStream(s).initStream() method readOnce*( - s: LPChannel, - pbytes: pointer, - nbytes: int + s: LPChannel, pbytes: pointer, nbytes: int ): Future[int] {.async: (raises: [CancelledError, LPStreamError]).} = ## Mplex relies on reading being done regularly from every channel, or all ## channels are blocked - in particular, this means that reading from one @@ -180,7 +180,7 @@ method readOnce*( let bytes = await procCall BufferStream(s).readOnce(pbytes, nbytes) when defined(libp2p_network_protocols_metrics): if s.protocol.len > 0: - libp2p_protocols_bytes.inc(bytes.int64, labelValues=[s.protocol, "in"]) + libp2p_protocols_bytes.inc(bytes.int64, labelValues = [s.protocol, "in"]) trace "readOnce", s, bytes if bytes == 0: @@ -196,8 +196,7 @@ method readOnce*( raise newLPStreamConnDownError(exc) proc prepareWrite( - s: LPChannel, - msg: seq[byte] + s: LPChannel, msg: seq[byte] ): Future[void] {.async: (raises: [CancelledError, LPStreamError]).} = # prepareWrite is the slow path of writing a message - see conditions in # write @@ -215,7 +214,7 @@ proc prepareWrite( debug "Closing connection, too many in-flight writes on channel", s, conn = s.conn, writes = s.writes when defined(libp2p_mplex_metrics): - libp2p_mplex_qlenclose.inc() + libp2p_mplex_qlenclose.inc() await s.reset() await s.conn.close() return @@ -228,7 +227,7 @@ proc prepareWrite( proc completeWrite( s: LPChannel, fut: Future[void].Raising([CancelledError, LPStreamError]), - msgLen: int + msgLen: int, ): Future[void] {.async: (raises: [CancelledError, LPStreamError]).} = try: s.writes += 1 @@ -244,8 +243,7 @@ proc completeWrite( if s.protocol.len > 0: # This crashes on Nim 2.0.2 with `--mm:orc` during `nimble test` # https://github.com/status-im/nim-metrics/issues/79 - libp2p_protocols_bytes.inc( - msgLen.int64, labelValues = [s.protocol, "out"]) + libp2p_protocols_bytes.inc(msgLen.int64, labelValues = [s.protocol, "out"]) s.activity = true except CancelledError as exc: @@ -266,15 +264,12 @@ proc completeWrite( s.writes -= 1 method write*( - s: LPChannel, - msg: seq[byte] -): Future[void] {.async: (raises: [ - CancelledError, LPStreamError], raw: true).} = + s: LPChannel, msg: seq[byte] +): Future[void] {.async: (raises: [CancelledError, LPStreamError], raw: true).} = ## Write to mplex channel - there may be up to MaxWrite concurrent writes ## pending after which the peer is disconnected - let - closed = s.closedLocal or s.conn.closed + let closed = s.closedLocal or s.conn.closed let fut = if (not closed) and msg.len > 0 and s.writes < MaxWrites and s.isOpen: @@ -287,7 +282,8 @@ method write*( s.completeWrite(fut, msg.len) -method getWrapped*(s: LPChannel): Connection = s.conn +method getWrapped*(s: LPChannel): Connection = + s.conn proc init*( L: type LPChannel, @@ -295,7 +291,8 @@ proc init*( conn: Connection, initiator: bool, name: string = "", - timeout: Duration = DefaultChanTimeout): LPChannel = + timeout: Duration = DefaultChanTimeout, +): LPChannel = let chann = L( id: id, name: name, @@ -306,12 +303,17 @@ proc init*( msgCode: if initiator: MessageType.MsgOut else: MessageType.MsgIn, closeCode: if initiator: MessageType.CloseOut else: MessageType.CloseIn, resetCode: if initiator: MessageType.ResetOut else: MessageType.ResetIn, - dir: if initiator: Direction.Out else: Direction.In) + dir: if initiator: Direction.Out else: Direction.In, + ) chann.initStream() when chronicles.enabledLogLevel == LogLevel.TRACE: - chann.name = if chann.name.len > 0: chann.name else: $chann.oid + chann.name = + if chann.name.len > 0: + chann.name + else: + $chann.oid trace "Created new lpchannel", s = chann, id, initiator diff --git a/libp2p/muxers/mplex/mplex.nim b/libp2p/muxers/mplex/mplex.nim index d9970b3e66..42e8f4f5da 100644 --- a/libp2p/muxers/mplex/mplex.nim +++ b/libp2p/muxers/mplex/mplex.nim @@ -11,13 +11,14 @@ import tables, sequtils, oids import chronos, chronicles, stew/byteutils, metrics -import ../muxer, - ../../stream/connection, - ../../stream/bufferstream, - ../../utility, - ../../peerinfo, - ./coder, - ./lpchannel +import + ../muxer, + ../../stream/connection, + ../../stream/bufferstream, + ../../utility, + ../../peerinfo, + ./coder, + ./lpchannel export muxer @@ -26,12 +27,10 @@ logScope: const MplexCodec* = "/mplex/6.7.0" -const - MaxChannelCount = 200 +const MaxChannelCount = 200 when defined(libp2p_expensive_metrics): - declareGauge(libp2p_mplex_channels, - "mplex channels", labels = ["initiator", "peer"]) + declareGauge(libp2p_mplex_channels, "mplex channels", labels = ["initiator", "peer"]) type InvalidChannelIdError* = object of MuxerError @@ -48,7 +47,8 @@ type func shortLog*(m: Mplex): auto = shortLog(m.connection) -chronicles.formatIt(Mplex): shortLog(it) +chronicles.formatIt(Mplex): + shortLog(it) proc newTooManyChannels(): ref TooManyChannels = newException(TooManyChannels, "max allowed channel count exceeded") @@ -67,7 +67,8 @@ proc cleanupChann(m: Mplex, chann: LPChannel) {.async: (raises: []), inline.} = when defined(libp2p_expensive_metrics): libp2p_mplex_channels.set( m.channels[chann.initiator].len.int64, - labelValues = [$chann.initiator, $m.connection.peerId]) + labelValues = [$chann.initiator, $m.connection.peerId], + ) except CancelledError as exc: warn "Error cleaning up mplex channel", m, chann, msg = exc.msg @@ -76,22 +77,21 @@ proc newStreamInternal*( initiator: bool = true, chanId: uint64 = 0, name: string = "", - timeout: Duration): LPChannel {.gcsafe, raises: [InvalidChannelIdError].} = + timeout: Duration, +): LPChannel {.gcsafe, raises: [InvalidChannelIdError].} = ## create new channel/stream ## let id = - if initiator: m.currentId.inc(); m.currentId - else: chanId + if initiator: + m.currentId.inc() + m.currentId + else: + chanId if id in m.channels[initiator]: raise newInvalidChannelIdError() - result = LPChannel.init( - id, - m.connection, - initiator, - name, - timeout = timeout) + result = LPChannel.init(id, m.connection, initiator, name, timeout = timeout) result.peerId = m.connection.peerId result.observedAddr = m.connection.observedAddr @@ -108,8 +108,8 @@ proc newStreamInternal*( when defined(libp2p_expensive_metrics): libp2p_mplex_channels.set( - m.channels[initiator].len.int64, - labelValues = [$initiator, $m.connection.peerId]) + m.channels[initiator].len.int64, labelValues = [$initiator, $m.connection.peerId] + ) proc handleStream(m: Mplex, chann: LPChannel) {.async: (raises: []).} = ## call the muxer stream handler for this channel @@ -146,7 +146,7 @@ method handle*(m: Mplex) {.async: (raises: []).} = else: if m.channels[false].len > m.maxChannCount - 1: warn "too many channels created by remote peer", - allowedMax = MaxChannelCount, m + allowedMax = MaxChannelCount, m raise newTooManyChannels() let name = string.fromBytes(data) @@ -154,7 +154,7 @@ method handle*(m: Mplex) {.async: (raises: []).} = trace "Processing channel message", m, channel, data = data.shortLog - case msgType: + case msgType of MessageType.New: trace "created channel", m, channel @@ -162,11 +162,10 @@ method handle*(m: Mplex) {.async: (raises: []).} = # Launch handler task # All the errors are handled inside `handleStream()` procedure. asyncSpawn m.handleStream(channel) - of MessageType.MsgIn, MessageType.MsgOut: if data.len > MaxMsgSize: warn "attempting to send a packet larger than allowed", - allowed = MaxMsgSize, channel + allowed = MaxMsgSize, channel raise newLPStreamLimitError() trace "pushing data to channel", m, channel, len = data.len @@ -175,10 +174,9 @@ method handle*(m: Mplex) {.async: (raises: []).} = trace "pushed data to channel", m, channel, len = data.len except LPStreamClosedError as exc: # Channel is being closed, but `cleanupChann` was not yet triggered. - trace "pushing data to channel failed", m, channel, len = data.len, - msg = exc.msg - discard # Ignore message, same as if `cleanupChann` had completed. - + trace "pushing data to channel failed", + m, channel, len = data.len, msg = exc.msg + discard # Ignore message, same as if `cleanupChann` had completed. of MessageType.CloseIn, MessageType.CloseOut: await channel.pushEof() of MessageType.ResetIn, MessageType.ResetOut: @@ -201,19 +199,19 @@ proc new*( conn: Connection, inTimeout: Duration = DefaultChanTimeout, outTimeout: Duration = DefaultChanTimeout, - maxChannCount: int = MaxChannelCount): Mplex = - M(connection: conn, + maxChannCount: int = MaxChannelCount, +): Mplex = + M( + connection: conn, inChannTimeout: inTimeout, outChannTimeout: outTimeout, oid: genOid(), - maxChannCount: maxChannCount) + maxChannCount: maxChannCount, + ) method newStream*( - m: Mplex, - name: string = "", - lazy: bool = false -): Future[Connection] {.async: (raises: [ - CancelledError, LPStreamError, MuxerError]).} = + m: Mplex, name: string = "", lazy: bool = false +): Future[Connection] {.async: (raises: [CancelledError, LPStreamError, MuxerError]).} = let channel = m.newStreamInternal(timeout = m.inChannTimeout) if not lazy: @@ -250,5 +248,7 @@ method close*(m: Mplex) {.async: (raises: []).} = trace "Closed mplex", m method getStreams*(m: Mplex): seq[Connection] = - for c in m.channels[false].values: result.add(c) - for c in m.channels[true].values: result.add(c) + for c in m.channels[false].values: + result.add(c) + for c in m.channels[true].values: + result.add(c) diff --git a/libp2p/muxers/muxer.nim b/libp2p/muxers/muxer.nim index fca06ee751..9929ba3dab 100644 --- a/libp2p/muxers/muxer.nim +++ b/libp2p/muxers/muxer.nim @@ -10,14 +10,12 @@ {.push raises: [].} import chronos, chronicles -import ../stream/connection, - ../errors +import ../stream/connection, ../errors logScope: topics = "libp2p muxer" -const - DefaultChanTimeout* = 5.minutes +const DefaultChanTimeout* = 5.minutes type MuxerError* = object of LPError @@ -32,8 +30,7 @@ type connection*: Connection # user provider proc that returns a constructed Muxer - MuxerConstructor* = - proc(conn: Connection): Muxer {.gcsafe, closure, raises: [].} + MuxerConstructor* = proc(conn: Connection): Muxer {.gcsafe, closure, raises: [].} # this wraps a creator proc that knows how to make muxers MuxerProvider* = object @@ -41,30 +38,32 @@ type codec*: string func shortLog*(m: Muxer): auto = - if m == nil: "nil" - else: shortLog(m.connection) + if m == nil: + "nil" + else: + shortLog(m.connection) -chronicles.formatIt(Muxer): shortLog(it) +chronicles.formatIt(Muxer): + shortLog(it) # muxer interface method newStream*( - m: Muxer, - name: string = "", - lazy: bool = false -): Future[Connection] {.base, async: (raises: [ - CancelledError, LPStreamError, MuxerError], raw: true).} = + m: Muxer, name: string = "", lazy: bool = false +): Future[Connection] {. + base, async: (raises: [CancelledError, LPStreamError, MuxerError], raw: true) +.} = raiseAssert("Not implemented!") method close*(m: Muxer) {.base, async: (raises: []).} = if m.connection != nil: await m.connection.close() -method handle*(m: Muxer): Future[void] {.base, async: (raises: []).} = discard +method handle*(m: Muxer): Future[void] {.base, async: (raises: []).} = + discard proc new*( - T: typedesc[MuxerProvider], - creator: MuxerConstructor, - codec: string): T {.gcsafe.} = + T: typedesc[MuxerProvider], creator: MuxerConstructor, codec: string +): T {.gcsafe.} = let muxerProvider = T(newMuxer: creator, codec: codec) muxerProvider diff --git a/libp2p/muxers/yamux/yamux.nim b/libp2p/muxers/yamux/yamux.nim index c052c4dc66..08acab38b4 100644 --- a/libp2p/muxers/yamux/yamux.nim +++ b/libp2p/muxers/yamux/yamux.nim @@ -11,8 +11,7 @@ import sequtils, std/[tables] import chronos, chronicles, metrics, stew/[endians2, byteutils, objects] -import ../muxer, - ../../stream/connection +import ../muxer, ../../stream/connection export muxer @@ -27,14 +26,13 @@ const MaxChannelCount = 200 when defined(libp2p_yamux_metrics): - declareGauge libp2p_yamux_channels, - "yamux channels", labels = ["initiator", "peer"] + declareGauge libp2p_yamux_channels, "yamux channels", labels = ["initiator", "peer"] declareHistogram libp2p_yamux_send_queue, - "message send queue length (in byte)", buckets = [ - 0.0, 100.0, 250.0, 1000.0, 2000.0, 3200.0, 6400.0, 25600.0, 256000.0] + "message send queue length (in byte)", + buckets = [0.0, 100.0, 250.0, 1000.0, 2000.0, 3200.0, 6400.0, 25600.0, 256000.0] declareHistogram libp2p_yamux_recv_queue, - "message recv queue length (in byte)", buckets = [ - 0.0, 100.0, 250.0, 1000.0, 2000.0, 3200.0, 6400.0, 25600.0, 256000.0] + "message recv queue length (in byte)", + buckets = [0.0, 100.0, 250.0, 1000.0, 2000.0, 3200.0, 6400.0, 25600.0, 256000.0] type YamuxError* = object of MuxerError @@ -52,9 +50,9 @@ type Rst GoAwayStatus = enum - NormalTermination = 0x0, - ProtocolError = 0x1, - InternalError = 0x2, + NormalTermination = 0x0 + ProtocolError = 0x1 + InternalError = 0x2 YamuxHeader = object version: uint8 @@ -65,88 +63,84 @@ type proc readHeader( conn: LPStream -): Future[YamuxHeader] {.async: (raises: [ - CancelledError, LPStreamError, MuxerError]).} = +): Future[YamuxHeader] {.async: (raises: [CancelledError, LPStreamError, MuxerError]).} = var buffer: array[12, byte] await conn.readExactly(addr buffer[0], 12) result.version = buffer[0] - let flags = fromBytesBE(uint16, buffer[2..3]) - if not result.msgType.checkedEnumAssign(buffer[1]) or flags notin 0'u16..15'u16: - raise newException(YamuxError, "Wrong header") + let flags = fromBytesBE(uint16, buffer[2 .. 3]) + if not result.msgType.checkedEnumAssign(buffer[1]) or flags notin 0'u16 .. 15'u16: + raise newException(YamuxError, "Wrong header") result.flags = cast[set[MsgFlags]](flags) - result.streamId = fromBytesBE(uint32, buffer[4..7]) - result.length = fromBytesBE(uint32, buffer[8..11]) + result.streamId = fromBytesBE(uint32, buffer[4 .. 7]) + result.length = fromBytesBE(uint32, buffer[8 .. 11]) return result proc `$`(header: YamuxHeader): string = - "{" & $header.msgType & ", " & - "{" & header.flags.foldl(if a != "": a & ", " & $b else: $b, "") & "}, " & - "streamId: " & $header.streamId & ", " & - "length: " & $header.length & "}" + "{" & $header.msgType & ", " & "{" & + header.flags.foldl( + if a != "": + a & ", " & $b + else: + $b + , + "", + ) & "}, " & "streamId: " & $header.streamId & ", " & "length: " & $header.length & + "}" proc encode(header: YamuxHeader): array[12, byte] = result[0] = header.version result[1] = uint8(header.msgType) - 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) + 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) proc write( - conn: LPStream, - header: YamuxHeader -): Future[void] {.async: (raises: [ - CancelledError, LPStreamError], raw: true).} = + conn: LPStream, header: YamuxHeader +): Future[void] {.async: (raises: [CancelledError, LPStreamError], raw: true).} = trace "write directly on stream", h = $header var buffer = header.encode() conn.write(@buffer) proc ping(T: type[YamuxHeader], flag: MsgFlags, pingData: uint32): T = - T( - version: YamuxVersion, - msgType: MsgType.Ping, - flags: {flag}, - length: pingData - ) + T(version: YamuxVersion, msgType: MsgType.Ping, flags: {flag}, length: pingData) proc goAway(T: type[YamuxHeader], status: GoAwayStatus): T = - T( - version: YamuxVersion, - msgType: MsgType.GoAway, - length: uint32(status) - ) + T(version: YamuxVersion, msgType: MsgType.GoAway, length: uint32(status)) proc data( T: type[YamuxHeader], streamId: uint32, length: uint32 = 0, - flags: set[MsgFlags] = {}): T = + flags: set[MsgFlags] = {}, +): T = T( version: YamuxVersion, msgType: MsgType.Data, length: length, flags: flags, - streamId: streamId + streamId: streamId, ) proc windowUpdate( - T: type[YamuxHeader], - streamId: uint32, - delta: uint32, - flags: set[MsgFlags] = {}): T = + T: type[YamuxHeader], streamId: uint32, delta: uint32, flags: set[MsgFlags] = {} +): T = T( version: YamuxVersion, msgType: MsgType.WindowUpdate, length: delta, flags: flags, - streamId: streamId + streamId: streamId, ) type - ToSend = tuple - data: seq[byte] - sent: int - fut: Future[void].Raising([CancelledError, LPStreamError]) + ToSend = + tuple[ + data: seq[byte], + sent: int, + fut: Future[void].Raising([CancelledError, LPStreamError]), + ] YamuxChannel* = ref object of Connection id: uint32 recvWindow: int @@ -176,7 +170,16 @@ proc `$`(channel: YamuxChannel): string = if channel.isReset: s.add("Reset") if s.len > 0: - result &= " {" & s.foldl(if a != "": a & ", " & b else: b, "") & "}" + result &= + " {" & + s.foldl( + if a != "": + a & ", " & b + else: + b + , + "", + ) & "}" proc lengthSendQueue(channel: YamuxChannel): int = ## Returns the length of what remains to be sent @@ -189,11 +192,13 @@ proc lengthSendQueueWithLimit(channel: YamuxChannel): int = # For leniency, limit big messages size to the third of maxSendQueueSize # This value is arbitrary, it's not in the specs, it permits to store up to # 3 big messages if the peer is stalling. - channel.sendQueue.foldl(a + min(b.data.len - b.sent, channel.maxSendQueueSize div 3), 0) + channel.sendQueue.foldl( + a + min(b.data.len - b.sent, channel.maxSendQueueSize div 3), 0 + ) proc actuallyClose(channel: YamuxChannel) {.async: (raises: []).} = if channel.closedLocally and channel.sendQueue.len == 0 and - channel.closedRemotely.completed(): + channel.closedRemotely.completed(): await procCall Connection(channel).closeImpl() proc remoteClosed(channel: YamuxChannel) {.async: (raises: []).} = @@ -207,12 +212,13 @@ method closeImpl*(channel: YamuxChannel) {.async: (raises: []).} = channel.closedLocally = true if not channel.isReset and channel.sendQueue.len == 0: - try: await channel.conn.write(YamuxHeader.data(channel.id, 0, {Fin})) - except CancelledError, LPStreamError: discard + try: + await channel.conn.write(YamuxHeader.data(channel.id, 0, {Fin})) + except CancelledError, LPStreamError: + discard await channel.actuallyClose() -proc reset( - channel: YamuxChannel, isLocal: bool = false) {.async: (raises: []).} = +proc reset(channel: YamuxChannel, isLocal: bool = false) {.async: (raises: []).} = # If we reset locally, we want to flush up to a maximum of recvWindow # bytes. It's because the peer we're connected to can send us data before # it receives the reset. @@ -228,8 +234,10 @@ proc reset( channel.sendWindow = 0 if not channel.closedLocally: if isLocal and not channel.isSending: - try: await channel.conn.write(YamuxHeader.data(channel.id, 0, {Rst})) - except CancelledError, LPStreamError: discard + try: + await channel.conn.write(YamuxHeader.data(channel.id, 0, {Rst})) + except CancelledError, LPStreamError: + discard await channel.close() if not channel.closedRemotely.completed(): await channel.remoteClosed() @@ -251,16 +259,11 @@ proc updateRecvWindow( let delta = channel.maxRecvWindow - inWindow channel.recvWindow.inc(delta) - await channel.conn.write(YamuxHeader.windowUpdate( - channel.id, - delta.uint32 - )) + await channel.conn.write(YamuxHeader.windowUpdate(channel.id, delta.uint32)) trace "increasing the recvWindow", delta method readOnce*( - channel: YamuxChannel, - pbytes: pointer, - nbytes: int + channel: YamuxChannel, pbytes: pointer, nbytes: int ): Future[int] {.async: (raises: [CancelledError, LPStreamError]).} = ## Read from a yamux channel @@ -276,19 +279,21 @@ method readOnce*( raise newLPStreamRemoteClosedError() if channel.recvQueue.len == 0: channel.receivedData.clear() - try: # https://github.com/status-im/nim-chronos/issues/516 + try: # https://github.com/status-im/nim-chronos/issues/516 discard await race(channel.closedRemotely, channel.receivedData.wait()) - except ValueError: raiseAssert("Futures list is not empty") + except ValueError: + raiseAssert("Futures list is not empty") if channel.closedRemotely.completed() and channel.recvQueue.len == 0: channel.isEof = true - return 0 # we return 0 to indicate that the channel is closed for reading from now on + return + 0 # we return 0 to indicate that the channel is closed for reading from now on let toRead = min(channel.recvQueue.len, nbytes) var p = cast[ptr UncheckedArray[byte]](pbytes) - toOpenArray(p, 0, nbytes - 1)[0..= data.len: # if every byte of the message is in the buffer, add the write future to the @@ -378,10 +386,8 @@ proc trySend( channel.activity = true method write*( - channel: YamuxChannel, - msg: seq[byte] -): Future[void] {.async: (raises: [ - CancelledError, LPStreamError], raw: true).} = + channel: YamuxChannel, msg: seq[byte] +): Future[void] {.async: (raises: [CancelledError, LPStreamError], raw: true).} = ## Write to yamux channel ## result = newFuture[void]("Yamux Send") @@ -399,37 +405,39 @@ method write*( libp2p_yamux_send_queue.observe(channel.lengthSendQueue().int64) asyncSpawn channel.trySend() -proc open( - channel: YamuxChannel -) {.async: (raises: [CancelledError, LPStreamError]).} = +proc open(channel: YamuxChannel) {.async: (raises: [CancelledError, LPStreamError]).} = ## Open a yamux channel by sending a window update with Syn or Ack flag ## if channel.opened: trace "Try to open channel twice" return channel.opened = true - await channel.conn.write(YamuxHeader.windowUpdate( - channel.id, - uint32(max(channel.maxRecvWindow - YamuxDefaultWindowSize, 0)), - {if channel.isSrc: Syn else: Ack})) + await channel.conn.write( + YamuxHeader.windowUpdate( + channel.id, + uint32(max(channel.maxRecvWindow - YamuxDefaultWindowSize, 0)), + {if channel.isSrc: Syn else: Ack}, + ) + ) -method getWrapped*(channel: YamuxChannel): Connection = channel.conn +method getWrapped*(channel: YamuxChannel): Connection = + channel.conn -type - Yamux* = ref object of Muxer - channels: Table[uint32, YamuxChannel] - flushed: Table[uint32, int] - currentId: uint32 - isClosed: bool - maxChannCount: int - windowSize: int - maxSendQueueSize: int - inTimeout: Duration - outTimeout: Duration +type Yamux* = ref object of Muxer + channels: Table[uint32, YamuxChannel] + flushed: Table[uint32, int] + currentId: uint32 + isClosed: bool + maxChannCount: int + windowSize: int + maxSendQueueSize: int + inTimeout: Duration + outTimeout: Duration proc lenBySrc(m: Yamux, isSrc: bool): int = for v in m.channels.values(): - if v.isSrc == isSrc: result += 1 + if v.isSrc == isSrc: + result += 1 proc cleanupChannel(m: Yamux, channel: YamuxChannel) {.async: (raises: []).} = try: @@ -439,13 +447,14 @@ proc cleanupChannel(m: Yamux, channel: YamuxChannel) {.async: (raises: []).} = m.channels.del(channel.id) when defined(libp2p_yamux_metrics): libp2p_yamux_channels.set( - m.lenBySrc(channel.isSrc).int64, [$channel.isSrc, $channel.peerId]) + m.lenBySrc(channel.isSrc).int64, [$channel.isSrc, $channel.peerId] + ) if channel.isReset and channel.recvWindow > 0: m.flushed[channel.id] = channel.recvWindow proc createStream( - m: Yamux, id: uint32, isSrc: bool, - recvWindow: int, maxSendQueueSize: int): YamuxChannel = + m: Yamux, id: uint32, isSrc: bool, recvWindow: int, maxSendQueueSize: int +): YamuxChannel = # During initialization, recvWindow can be larger than maxRecvWindow. # This is because the peer we're connected to will always assume # that the initial recvWindow is 256k. @@ -453,16 +462,18 @@ proc createStream( # recvWindow is less than maxRecvWindow proc newClosedRemotelyFut(): Future[void] {.async: (raises: [], raw: true).} = newFuture[void]() + var stream = YamuxChannel( id: id, maxRecvWindow: recvWindow, - recvWindow: if recvWindow > YamuxDefaultWindowSize: recvWindow else: YamuxDefaultWindowSize, + recvWindow: + if recvWindow > YamuxDefaultWindowSize: recvWindow else: YamuxDefaultWindowSize, sendWindow: YamuxDefaultWindowSize, maxSendQueueSize: maxSendQueueSize, isSrc: isSrc, conn: m.connection, receivedData: newAsyncEvent(), - closedRemotely: newClosedRemotelyFut() + closedRemotely: newClosedRemotelyFut(), ) stream.objName = "YamuxStream" if isSrc: @@ -471,10 +482,9 @@ proc createStream( else: stream.dir = Direction.In stream.timeout = m.inTimeout - stream.timeoutHandler = - proc(): Future[void] {.async: (raises: [], raw: true).} = - trace "Idle timeout expired, resetting YamuxChannel" - stream.reset(isLocal = true) + stream.timeoutHandler = proc(): Future[void] {.async: (raises: [], raw: true).} = + trace "Idle timeout expired, resetting YamuxChannel" + stream.reset(isLocal = true) stream.initStream() stream.peerId = m.connection.peerId stream.observedAddr = m.connection.observedAddr @@ -483,7 +493,7 @@ proc createStream( stream.shortAgent = m.connection.shortAgent m.channels[id] = stream asyncSpawn m.cleanupChannel(stream) - trace "created channel", id, pid=m.connection.peerId + trace "created channel", id, pid = m.connection.peerId when defined(libp2p_yamux_metrics): libp2p_yamux_channels.set(m.lenBySrc(isSrc).int64, [$isSrc, $stream.peerId]) return stream @@ -498,9 +508,12 @@ method close*(m: Yamux) {.async: (raises: []).} = let channels = toSeq(m.channels.values()) for channel in channels: await channel.reset(isLocal = true) - try: await m.connection.write(YamuxHeader.goAway(NormalTermination)) - except CancelledError as exc: trace "cancelled sending goAway", msg = exc.msg - except LPStreamError as exc: trace "failed to send goAway", msg = exc.msg + try: + await m.connection.write(YamuxHeader.goAway(NormalTermination)) + except CancelledError as exc: + trace "cancelled sending goAway", msg = exc.msg + except LPStreamError as exc: + trace "failed to send goAway", msg = exc.msg await m.connection.close() trace "Closed yamux" @@ -512,34 +525,40 @@ proc handleStream(m: Yamux, channel: YamuxChannel) {.async: (raises: []).} = doAssert(channel.isClosed, "connection not closed by handler!") method handle*(m: Yamux) {.async: (raises: []).} = - trace "Starting yamux handler", pid=m.connection.peerId + trace "Starting yamux handler", pid = m.connection.peerId try: while not m.connection.atEof: trace "waiting for header" let header = await m.connection.readHeader() trace "got message", h = $header - case header.msgType: + case header.msgType of Ping: if MsgFlags.Syn in header.flags: await m.connection.write(YamuxHeader.ping(MsgFlags.Ack, header.length)) of GoAway: var status: GoAwayStatus - if status.checkedEnumAssign(header.length): trace "Received go away", status - else: trace "Received unexpected error go away" + if status.checkedEnumAssign(header.length): + trace "Received go away", status + else: + trace "Received unexpected error go away" break of Data, WindowUpdate: if MsgFlags.Syn in header.flags: if header.streamId in m.channels: - debug "Trying to create an existing channel, skipping", id=header.streamId + debug "Trying to create an existing channel, skipping", id = header.streamId else: if header.streamId in m.flushed: m.flushed.del(header.streamId) if header.streamId mod 2 == m.currentId mod 2: - debug "Peer used our reserved stream id, skipping", id=header.streamId, currentId=m.currentId, peerId=m.connection.peerId + debug "Peer used our reserved stream id, skipping", + id = header.streamId, + currentId = m.currentId, + peerId = m.connection.peerId raise newException(YamuxError, "Peer used our reserved stream id") - let newStream = m.createStream(header.streamId, false, m.windowSize, m.maxSendQueueSize) + let newStream = + m.createStream(header.streamId, false, m.windowSize, m.maxSendQueueSize) if m.channels.len >= m.maxChannCount: await newStream.reset() continue @@ -551,23 +570,23 @@ method handle*(m: Yamux) {.async: (raises: []).} = if header.msgType == Data: flushed[].dec(int(header.length)) if flushed[] < 0: - raise newException(YamuxError, - "Peer exhausted the recvWindow after reset") + raise + newException(YamuxError, "Peer exhausted the recvWindow after reset") if header.length > 0: var buffer = newSeqUninitialized[byte](header.length) - await m.connection.readExactly( - addr buffer[0], int(header.length)) + await m.connection.readExactly(addr buffer[0], int(header.length)) do: - raise newException(YamuxError, - "Unknown stream ID: " & $header.streamId) + raise newException(YamuxError, "Unknown stream ID: " & $header.streamId) continue let channel = try: m.channels[header.streamId] except KeyError: - raise newException(YamuxError, - "Stream was cleaned up before handling data: " & $header.streamId) + raise newException( + YamuxError, + "Stream was cleaned up before handling data: " & $header.streamId, + ) if header.msgType == WindowUpdate: channel.sendWindow += int(header.length) @@ -580,7 +599,7 @@ method handle*(m: Yamux) {.async: (raises: []).} = if header.length > 0: var buffer = newSeqUninitialized[byte](header.length) await m.connection.readExactly(addr buffer[0], int(header.length)) - trace "Msg Rcv", msg=shortLog(buffer) + trace "Msg Rcv", msg = shortLog(buffer) await channel.gotDataFromRemote(buffer) if MsgFlags.Fin in header.flags: @@ -596,7 +615,7 @@ method handle*(m: Yamux) {.async: (raises: []).} = except LPStreamError as exc: debug "Unexpected stream exception in yamux read loop", msg = exc.msg except YamuxError as exc: - trace "Closing yamux connection", error=exc.msg + trace "Closing yamux connection", error = exc.msg try: await m.connection.write(YamuxHeader.goAway(ProtocolError)) except CancelledError, LPStreamError: @@ -612,14 +631,12 @@ method handle*(m: Yamux) {.async: (raises: []).} = trace "Stopped yamux handler" method getStreams*(m: Yamux): seq[Connection] = - for c in m.channels.values: result.add(c) + for c in m.channels.values: + result.add(c) method newStream*( - m: Yamux, - name: string = "", - lazy: bool = false -): Future[Connection] {.async: (raises: [ - CancelledError, LPStreamError, MuxerError]).} = + m: Yamux, name: string = "", lazy: bool = false +): Future[Connection] {.async: (raises: [CancelledError, LPStreamError, MuxerError]).} = if m.channels.len > m.maxChannCount - 1: raise newException(TooManyChannels, "max allowed channel count exceeded") let stream = m.createStream(m.currentId, true, m.windowSize, m.maxSendQueueSize) @@ -629,12 +646,14 @@ method newStream*( return stream proc new*( - T: type[Yamux], conn: Connection, + T: type[Yamux], + conn: Connection, maxChannCount: int = MaxChannelCount, windowSize: int = YamuxDefaultWindowSize, maxSendQueueSize: int = MaxSendQueueSize, inTimeout: Duration = 5.minutes, - outTimeout: Duration = 5.minutes): T = + outTimeout: Duration = 5.minutes, +): T = T( connection: conn, currentId: if conn.dir == Out: 1 else: 2, @@ -642,5 +661,5 @@ proc new*( windowSize: windowSize, maxSendQueueSize: maxSendQueueSize, inTimeout: inTimeout, - outTimeout: outTimeout + outTimeout: outTimeout, ) diff --git a/libp2p/nameresolving/dnsresolver.nim b/libp2p/nameresolving/dnsresolver.nim index 58bfdf208e..8ad905ed89 100644 --- a/libp2p/nameresolving/dnsresolver.nim +++ b/libp2p/nameresolving/dnsresolver.nim @@ -11,19 +11,19 @@ import std/[streams, strutils, sets, sequtils], - chronos, chronicles, stew/byteutils, + chronos, + chronicles, + stew/byteutils, dnsclientpkg/[protocol, types], ../utility -import - nameresolver +import nameresolver logScope: topics = "libp2p dnsresolver" -type - DnsResolver* = ref object of NameResolver - nameServers*: seq[TransportAddress] +type DnsResolver* = ref object of NameResolver + nameServers*: seq[TransportAddress] proc questionToBuf(address: string, kind: QKind): seq[byte] = try: @@ -45,10 +45,8 @@ proc questionToBuf(address: string, kind: QKind): seq[byte] = return newSeq[byte](0) proc getDnsResponse( - dnsServer: TransportAddress, - address: string, - kind: QKind): Future[Response] {.async.} = - + dnsServer: TransportAddress, address: string, kind: QKind +): Future[Response] {.async.} = var sendBuf = questionToBuf(address, kind) if sendBuf.len == 0: @@ -56,9 +54,10 @@ proc getDnsResponse( let receivedDataFuture = newFuture[void]() - proc datagramDataReceived(transp: DatagramTransport, - raddr: TransportAddress): Future[void] {.async, closure.} = - receivedDataFuture.complete() + proc datagramDataReceived( + transp: DatagramTransport, raddr: TransportAddress + ): Future[void] {.async, closure.} = + receivedDataFuture.complete() let sock = if dnsServer.family == AddressFamily.IPv6: @@ -78,16 +77,14 @@ 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 exceptionToAssert: parseResponse(string.fromBytes(rawResponse)) + return exceptionToAssert: + parseResponse(string.fromBytes(rawResponse)) finally: await sock.closeWait() method resolveIp*( - self: DnsResolver, - address: string, - port: Port, - domain: Domain = Domain.AF_UNSPEC): Future[seq[TransportAddress]] {.async.} = - + self: DnsResolver, address: string, port: Port, domain: Domain = Domain.AF_UNSPEC +): Future[seq[TransportAddress]] {.async.} = trace "Resolving IP using DNS", address, servers = self.nameServers.mapIt($it), domain for _ in 0 ..< self.nameServers.len: let server = self.nameServers[0] @@ -113,16 +110,14 @@ method resolveIp*( # toString can has a raises: [Exception, ..] because of # https://github.com/nim-lang/Nim/commit/035134de429b5d99c5607c5fae912762bebb6008 # it can't actually raise though - resolvedAddresses.incl( - exceptionToAssert(answer.toString()) - ) + resolvedAddresses.incl(exceptionToAssert(answer.toString())) except CancelledError as e: raise e except ValueError as e: - info "Invalid DNS query", address, error=e.msg + info "Invalid DNS query", address, error = e.msg return @[] except CatchableError as e: - info "Failed to query DNS", address, error=e.msg + info "Failed to query DNS", address, error = e.msg resolveFailed = true break @@ -137,10 +132,7 @@ method resolveIp*( debug "Failed to resolve address, returning empty set" return @[] -method resolveTxt*( - self: DnsResolver, - address: string): Future[seq[string]] {.async.} = - +method resolveTxt*(self: DnsResolver, address: string): Future[seq[string]] {.async.} = trace "Resolving TXT using DNS", address, servers = self.nameServers.mapIt($it) for _ in 0 ..< self.nameServers.len: let server = self.nameServers[0] @@ -150,12 +142,13 @@ method resolveTxt*( # it can't actually raise though let response = await getDnsResponse(server, address, TXT) return exceptionToAssert: - trace "Got TXT response", server = $server, answer=response.answers.mapIt(it.toString()) + 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: - info "Failed to query DNS", address, error=e.msg + info "Failed to query DNS", address, error = e.msg self.nameServers.add(self.nameServers[0]) self.nameServers.delete(0) continue @@ -163,7 +156,5 @@ method resolveTxt*( debug "Failed to resolve TXT, returning empty set" return @[] -proc new*( - T: typedesc[DnsResolver], - nameServers: seq[TransportAddress]): T = +proc new*(T: typedesc[DnsResolver], nameServers: seq[TransportAddress]): T = T(nameServers: nameServers) diff --git a/libp2p/nameresolving/mockresolver.nim b/libp2p/nameresolving/mockresolver.nim index 492dc1c421..5a63daf771 100644 --- a/libp2p/nameresolving/mockresolver.nim +++ b/libp2p/nameresolving/mockresolver.nim @@ -9,9 +9,7 @@ {.push raises: [].} -import - std/tables, - chronos, chronicles +import std/tables, chronos, chronicles import nameresolver @@ -26,10 +24,8 @@ type MockResolver* = ref object of NameResolver ipResponses*: Table[(string, bool), seq[string]] method resolveIp*( - self: MockResolver, - address: string, - port: Port, - domain: Domain = Domain.AF_UNSPEC): Future[seq[TransportAddress]] {.async.} = + self: MockResolver, address: string, port: Port, domain: Domain = Domain.AF_UNSPEC +): Future[seq[TransportAddress]] {.async.} = if domain == Domain.AF_INET or domain == Domain.AF_UNSPEC: for resp in self.ipResponses.getOrDefault((address, false)): result.add(initTAddress(resp, port)) @@ -38,9 +34,8 @@ method resolveIp*( for resp in self.ipResponses.getOrDefault((address, true)): result.add(initTAddress(resp, port)) -method resolveTxt*( - self: MockResolver, - address: string): Future[seq[string]] {.async.} = +method resolveTxt*(self: MockResolver, address: string): Future[seq[string]] {.async.} = return self.txtResponses.getOrDefault(address) -proc new*(T: typedesc[MockResolver]): T = T() +proc new*(T: typedesc[MockResolver]): T = + T() diff --git a/libp2p/nameresolving/nameresolver.nim b/libp2p/nameresolving/nameresolver.nim index be30b46cd2..90c44f77f9 100644 --- a/libp2p/nameresolving/nameresolver.nim +++ b/libp2p/nameresolving/nameresolver.nim @@ -10,31 +10,25 @@ {.push raises: [].} import std/[sugar, sets, sequtils, strutils] -import - chronos, - chronicles, - stew/endians2 +import chronos, chronicles, stew/endians2 import ".."/[multiaddress, multicodec] logScope: topics = "libp2p nameresolver" -type - NameResolver* = ref object of RootObj +type NameResolver* = ref object of RootObj method resolveTxt*( - self: NameResolver, - address: string): Future[seq[string]] {.async, base.} = + self: NameResolver, address: string +): Future[seq[string]] {.async, base.} = ## Get TXT record ## doAssert(false, "Not implemented!") method resolveIp*( - self: NameResolver, - address: string, - port: Port, - domain: Domain = Domain.AF_UNSPEC): Future[seq[TransportAddress]] {.async, base.} = + self: NameResolver, address: string, port: Port, domain: Domain = Domain.AF_UNSPEC +): Future[seq[TransportAddress]] {.async, base.} = ## Resolve the specified address ## @@ -42,17 +36,17 @@ method resolveIp*( proc getHostname*(ma: MultiAddress): string = let - firstPart = ma[0].valueOr: return "" + firstPart = ma[0].valueOr: + return "" fpSplitted = ($firstPart).split('/', 2) - if fpSplitted.len > 2: fpSplitted[2] - else: "" + if fpSplitted.len > 2: + fpSplitted[2] + else: + "" proc resolveOneAddress( - self: NameResolver, - ma: MultiAddress, - domain: Domain = Domain.AF_UNSPEC, - prefix = ""): Future[seq[MultiAddress]] - {.async.} = + self: NameResolver, ma: MultiAddress, domain: Domain = Domain.AF_UNSPEC, prefix = "" +): Future[seq[MultiAddress]] {.async.} = #Resolve a single address var pbuf: array[2, byte] @@ -68,15 +62,14 @@ proc resolveOneAddress( for address in resolvedAddresses: var createdAddress = MultiAddress.init(address).tryGet()[0].tryGet() for part in ma: - if DNS.match(part.tryGet()): continue + if DNS.match(part.tryGet()): + continue createdAddress &= part.tryGet() createdAddress proc resolveDnsAddr*( - self: NameResolver, - ma: MultiAddress, - depth: int = 0): Future[seq[MultiAddress]] {.async.} = - + self: NameResolver, ma: MultiAddress, depth: int = 0 +): Future[seq[MultiAddress]] {.async.} = if not DNSADDR.matchPartial(ma): return @[ma] @@ -93,10 +86,12 @@ proc resolveDnsAddr*( var result: seq[MultiAddress] for entry in txt: - if not entry.startsWith("dnsaddr="): continue - let entryValue = MultiAddress.init(entry[8..^1]).tryGet() + if not entry.startsWith("dnsaddr="): + continue + let entryValue = MultiAddress.init(entry[8 ..^ 1]).tryGet() - if entryValue.contains(multiCodec("p2p")).tryGet() and ma.contains(multiCodec("p2p")).tryGet(): + if entryValue.contains(multiCodec("p2p")).tryGet() and + ma.contains(multiCodec("p2p")).tryGet(): if entryValue[multiCodec("p2p")] != ma[multiCodec("p2p")]: continue @@ -109,17 +104,17 @@ proc resolveDnsAddr*( return @[] return result - proc resolveMAddress*( - self: NameResolver, - address: MultiAddress): Future[seq[MultiAddress]] {.async.} = + self: NameResolver, address: MultiAddress +): Future[seq[MultiAddress]] {.async.} = var res = initOrderedSet[MultiAddress]() if not DNS.matchPartial(address): res.incl(address) else: let code = address[0].tryGet().protoCode().tryGet() - let seq = case code: + let seq = + case code of multiCodec("dns"): await self.resolveOneAddress(address) of multiCodec("dns4"): diff --git a/libp2p/observedaddrmanager.nim b/libp2p/observedaddrmanager.nim index e1644aba1a..7b07b9efea 100644 --- a/libp2p/observedaddrmanager.nim +++ b/libp2p/observedaddrmanager.nim @@ -20,31 +20,39 @@ type maxSize: int minCount: int -proc addObservation*(self:ObservedAddrManager, observedAddr: MultiAddress): bool = +proc addObservation*(self: ObservedAddrManager, observedAddr: MultiAddress): bool = ## Adds a new observed MultiAddress. If the number of observations exceeds maxSize, the oldest one is removed. if self.observedIPsAndPorts.len >= self.maxSize: - self.observedIPsAndPorts.del(0) + self.observedIPsAndPorts.del(0) self.observedIPsAndPorts.add(observedAddr) return true -proc getProtocol(self: ObservedAddrManager, observations: seq[MultiAddress], multiCodec: MultiCodec): Opt[MultiAddress] = +proc getProtocol( + self: ObservedAddrManager, observations: seq[MultiAddress], multiCodec: MultiCodec +): Opt[MultiAddress] = var countTable = toCountTable(observations) countTable.sort() var orderedPairs = toSeq(countTable.pairs) for (ma, count) in orderedPairs: - let protoCode = (ma[0].flatMap(protoCode)).valueOr: continue + let protoCode = (ma[0].flatMap(protoCode)).valueOr: + continue if protoCode == multiCodec and count >= self.minCount: return Opt.some(ma) return Opt.none(MultiAddress) -proc getMostObservedProtocol(self: ObservedAddrManager, multiCodec: MultiCodec): Opt[MultiAddress] = +proc getMostObservedProtocol( + self: ObservedAddrManager, multiCodec: MultiCodec +): Opt[MultiAddress] = ## Returns the most observed IP address or none if the number of observations are less than minCount. let observedIPs = collect: for observedIp in self.observedIPsAndPorts: - observedIp[0].valueOr: continue + observedIp[0].valueOr: + continue return self.getProtocol(observedIPs, multiCodec) -proc getMostObservedProtoAndPort(self: ObservedAddrManager, multiCodec: MultiCodec): Opt[MultiAddress] = +proc getMostObservedProtoAndPort( + self: ObservedAddrManager, multiCodec: MultiCodec +): Opt[MultiAddress] = ## Returns the most observed IP/Port address or none if the number of observations are less than minCount. return self.getProtocol(self.observedIPsAndPorts, multiCodec) @@ -58,29 +66,27 @@ proc getMostObservedProtosAndPorts*(self: ObservedAddrManager): seq[MultiAddress res.add(ip6) return res -proc guessDialableAddr*( - self: ObservedAddrManager, - ma: MultiAddress): MultiAddress = +proc guessDialableAddr*(self: ObservedAddrManager, ma: MultiAddress): MultiAddress = ## Replaces the first proto value of each listen address by the corresponding (matching the proto code) most observed value. ## If the most observed value is not available, the original MultiAddress is returned. let - maFirst = ma[0].valueOr: return ma - maRest = ma[1..^1].valueOr: return ma - maFirstProto = maFirst.protoCode().valueOr: return ma + maFirst = ma[0].valueOr: + return ma + maRest = ma[1 ..^ 1].valueOr: + return ma + maFirstProto = maFirst.protoCode().valueOr: + return ma - let observedIP = self.getMostObservedProtocol(maFirstProto).valueOr: return ma - return concat(observedIP, maRest).valueOr: ma + let observedIP = self.getMostObservedProtocol(maFirstProto).valueOr: + return ma + return concat(observedIP, maRest).valueOr: + ma proc `$`*(self: ObservedAddrManager): string = ## Returns a string representation of the ObservedAddrManager. return "IPs and Ports: " & $self.observedIPsAndPorts -proc new*( - T: typedesc[ObservedAddrManager], - maxSize = 10, - minCount = 3): T = +proc new*(T: typedesc[ObservedAddrManager], maxSize = 10, minCount = 3): T = ## Creates a new ObservedAddrManager. - return T( - observedIPsAndPorts: newSeq[MultiAddress](), - maxSize: maxSize, - minCount: minCount) + return + T(observedIPsAndPorts: newSeq[MultiAddress](), maxSize: maxSize, minCount: minCount) diff --git a/libp2p/peerid.nim b/libp2p/peerid.nim index 4f7e6f9d14..b5ea78a4f5 100644 --- a/libp2p/peerid.nim +++ b/libp2p/peerid.nim @@ -18,17 +18,18 @@ import chronicles, nimcrypto/utils, utility, - ./crypto/crypto, ./multicodec, ./multihash, ./vbuffer, + ./crypto/crypto, + ./multicodec, + ./multihash, + ./vbuffer, ./protobuf/minprotobuf export results, utility -const - maxInlineKeyLength* = 42 +const maxInlineKeyLength* = 42 -type - PeerId* = object - data*: seq[byte] +type PeerId* = object + data*: seq[byte] func `$`*(pid: PeerId): string = ## Return base58 encoded ``pid`` representation. @@ -45,7 +46,8 @@ func shortLog*(pid: PeerId): string = spid -chronicles.formatIt(PeerId): shortLog(it) +chronicles.formatIt(PeerId): + shortLog(it) func toBytes*(pid: PeerId, data: var openArray[byte]): int = ## Store PeerId ``pid`` to array of bytes ``data``. @@ -78,7 +80,8 @@ func cmp*(a, b: PeerId): int = var m = min(len(a.data), len(b.data)) while i < m: result = ord(a.data[i]) - ord(b.data[i]) - if result != 0: return + if result != 0: + return inc(i) result = len(a.data) - len(b.data) @@ -165,18 +168,18 @@ func init*(t: typedesc[PeerId], data: string): Result[PeerId, cstring] = func init*(t: typedesc[PeerId], pubkey: PublicKey): Result[PeerId, cstring] = ## Create new peer id from public key ``pubkey``. - var pubraw = ? pubkey.getBytes().orError( - cstring("peerid: failed to get bytes from given key")) + var pubraw = + ?pubkey.getBytes().orError(cstring("peerid: failed to get bytes from given key")) var mh: MultiHash if len(pubraw) <= maxInlineKeyLength: - mh = ? MultiHash.digest("identity", pubraw) + mh = ?MultiHash.digest("identity", pubraw) else: - mh = ? MultiHash.digest("sha2-256", pubraw) + mh = ?MultiHash.digest("sha2-256", pubraw) ok(PeerId(data: mh.data.buffer)) func init*(t: typedesc[PeerId], seckey: PrivateKey): Result[PeerId, cstring] = ## Create new peer id from private key ``seckey``. - PeerId.init(? seckey.getPublicKey().orError(cstring("invalid private key"))) + PeerId.init(?seckey.getPublicKey().orError(cstring("invalid private key"))) proc random*(t: typedesc[PeerId], rng = newRng()): Result[PeerId, cstring] = ## Create new peer id with random public key. @@ -201,12 +204,13 @@ func write*(pb: var ProtoBuffer, field: int, pid: PeerId) = ## Write PeerId value ``peerid`` to object ``pb`` using ProtoBuf's encoding. write(pb, field, pid.data) -func getField*(pb: ProtoBuffer, field: int, - pid: var PeerId): ProtoResult[bool] {.inline.} = +func getField*( + pb: ProtoBuffer, field: int, pid: var PeerId +): ProtoResult[bool] {.inline.} = ## Read ``PeerId`` from ProtoBuf's message and validate it var buffer: seq[byte] - let res = ? pb.getField(field, buffer) - if not(res): + let res = ?pb.getField(field, buffer) + if not (res): ok(false) else: var peerId: PeerId diff --git a/libp2p/peerinfo.nim b/libp2p/peerinfo.nim index c290894edf..ba8f00a083 100644 --- a/libp2p/peerinfo.nim +++ b/libp2p/peerinfo.nim @@ -21,10 +21,9 @@ export peerid, multiaddress, crypto, routing_record, errors, results type PeerInfoError* = object of LPError - AddressMapper* = - proc(listenAddrs: seq[MultiAddress]): Future[seq[MultiAddress]] - {.gcsafe, raises: [].} - ## A proc that expected to resolve the listen addresses into dialable addresses + AddressMapper* = proc(listenAddrs: seq[MultiAddress]): Future[seq[MultiAddress]] {. + gcsafe, raises: [] + .} ## A proc that expected to resolve the listen addresses into dialable addresses PeerInfo* {.public.} = ref object peerId*: PeerId @@ -50,7 +49,8 @@ func shortLog*(p: PeerInfo): auto = protoVersion: p.protoVersion, agentVersion: p.agentVersion, ) -chronicles.formatIt(PeerInfo): shortLog(it) +chronicles.formatIt(PeerInfo): + shortLog(it) proc update*(p: PeerInfo) {.async.} = # p.addrs.len == 0 overrides addrs only if it is the first time update is being executed or if the field is empty. @@ -62,9 +62,8 @@ proc update*(p: PeerInfo) {.async.} = p.addrs = await mapper(p.addrs) p.signedPeerRecord = SignedPeerRecord.init( - p.privateKey, - PeerRecord.init(p.peerId, p.addrs) - ).valueOr(): + p.privateKey, PeerRecord.init(p.peerId, p.addrs) + ).valueOr: info "Can't update the signed peer record" return @@ -72,38 +71,35 @@ proc addrs*(p: PeerInfo): seq[MultiAddress] = p.addrs proc fullAddrs*(p: PeerInfo): MaResult[seq[MultiAddress]] = - let peerIdPart = ? MultiAddress.init(multiCodec("p2p"), p.peerId.data) + let peerIdPart = ?MultiAddress.init(multiCodec("p2p"), p.peerId.data) var res: seq[MultiAddress] for address in p.addrs: - res.add(? concat(address, peerIdPart)) + res.add(?concat(address, peerIdPart)) ok(res) proc parseFullAddress*(ma: MultiAddress): MaResult[(PeerId, MultiAddress)] = - let p2pPart = ? ma[^1] - if ? p2pPart.protoCode != multiCodec("p2p"): + let p2pPart = ?ma[^1] + if ?p2pPart.protoCode != multiCodec("p2p"): return err("Missing p2p part from multiaddress!") - let res = ( - ? PeerId.init(? p2pPart.protoArgument()).orErr("invalid peerid"), - ? ma[0 .. ^2] - ) + let res = + (?PeerId.init(?p2pPart.protoArgument()).orErr("invalid peerid"), ?ma[0 .. ^2]) ok(res) proc parseFullAddress*(ma: string | seq[byte]): MaResult[(PeerId, MultiAddress)] = - parseFullAddress(? MultiAddress.init(ma)) + parseFullAddress(?MultiAddress.init(ma)) proc new*( - p: typedesc[PeerInfo], - key: PrivateKey, - listenAddrs: openArray[MultiAddress] = [], - protocols: openArray[string] = [], - protoVersion: string = "", - agentVersion: string = "", - addressMappers = newSeq[AddressMapper](), - ): PeerInfo - {.raises: [LPError].} = - - let pubkey = try: + p: typedesc[PeerInfo], + key: PrivateKey, + listenAddrs: openArray[MultiAddress] = [], + protocols: openArray[string] = [], + protoVersion: string = "", + agentVersion: string = "", + addressMappers = newSeq[AddressMapper](), +): PeerInfo {.raises: [LPError].} = + let pubkey = + try: key.getPublicKey().tryGet() except CatchableError: raise newException(PeerInfoError, "invalid private key") @@ -118,7 +114,7 @@ proc new*( agentVersion: agentVersion, listenAddrs: @listenAddrs, protocols: @protocols, - addressMappers: addressMappers + addressMappers: addressMappers, ) return peerInfo diff --git a/libp2p/peerstore.nim b/libp2p/peerstore.nim index 41698b75de..c9d245b078 100644 --- a/libp2p/peerstore.nim +++ b/libp2p/peerstore.nim @@ -29,7 +29,8 @@ import ./crypto/crypto, ./protocols/identify, ./protocols/protocol, - ./peerid, ./peerinfo, + ./peerid, + ./peerinfo, ./routing_record, ./multiaddress, ./stream/connection, @@ -41,7 +42,6 @@ type ################# # Handler types # ################# - PeerBookChangeHandler* = proc(peerId: PeerId) {.gcsafe, raises: [].} ######### @@ -69,31 +69,26 @@ type #################### # Peer store types # #################### - PeerStore* {.public.} = ref object books: Table[string, BasePeerBook] identify: Identify capacity*: int toClean*: seq[PeerId] -proc new*(T: type PeerStore, identify: Identify, capacity = 1000): PeerStore {.public.} = - T( - identify: identify, - capacity: capacity - ) +proc new*( + T: type PeerStore, identify: Identify, capacity = 1000 +): PeerStore {.public.} = + T(identify: identify, capacity: capacity) ######################### # Generic Peer Book API # ######################### -proc `[]`*[T](peerBook: PeerBook[T], - peerId: PeerId): T {.public.} = +proc `[]`*[T](peerBook: PeerBook[T], peerId: PeerId): T {.public.} = ## Get all known metadata of a provided peer, or default(T) if missing peerBook.book.getOrDefault(peerId) -proc `[]=`*[T](peerBook: PeerBook[T], - peerId: PeerId, - entry: T) {.public.} = +proc `[]=`*[T](peerBook: PeerBook[T], peerId: PeerId, entry: T) {.public.} = ## Set metadata for a given peerId. peerBook.book[peerId] = entry @@ -102,8 +97,7 @@ proc `[]=`*[T](peerBook: PeerBook[T], for handler in peerBook.changeHandlers: handler(peerId) -proc del*[T](peerBook: PeerBook[T], - peerId: PeerId): bool {.public.} = +proc del*[T](peerBook: PeerBook[T], peerId: PeerId): bool {.public.} = ## Delete the provided peer from the book. Returns whether the peer was in the book if peerId notin peerBook.book: @@ -122,7 +116,8 @@ proc addHandler*[T](peerBook: PeerBook[T], handler: PeerBookChangeHandler) {.pub ## Adds a callback that will be called everytime the book changes peerBook.changeHandlers.add(handler) -proc len*[T](peerBook: PeerBook[T]): int {.public.} = peerBook.book.len +proc len*[T](peerBook: PeerBook[T]): int {.public.} = + peerBook.book.len ################## # Peer Store API # @@ -145,16 +140,12 @@ proc `[]`*[T](p: PeerStore, typ: type[T]): T {.public.} = p.books[name] = result return result -proc del*(peerStore: PeerStore, - peerId: PeerId) {.public.} = +proc del*(peerStore: PeerStore, peerId: PeerId) {.public.} = ## Delete the provided peer from every book. for _, book in peerStore.books: book.deletor(peerId) -proc updatePeerInfo*( - peerStore: PeerStore, - info: IdentifyInfo) = - +proc updatePeerInfo*(peerStore: PeerStore, info: IdentifyInfo) = if info.addrs.len > 0: peerStore[AddressBook][info.peerId] = info.addrs @@ -177,10 +168,7 @@ proc updatePeerInfo*( if cleanupPos >= 0: peerStore.toClean.delete(cleanupPos) -proc cleanup*( - peerStore: PeerStore, - peerId: PeerId) = - +proc cleanup*(peerStore: PeerStore, peerId: PeerId) = if peerStore.capacity == 0: peerStore.del(peerId) return @@ -193,10 +181,7 @@ proc cleanup*( peerStore.del(peerStore.toClean[0]) peerStore.toClean.delete(0) -proc identify*( - peerStore: PeerStore, - muxer: Muxer) {.async.} = - +proc identify*(peerStore: PeerStore, muxer: Muxer) {.async.} = # new stream for identify var stream = await muxer.newStream() if stream == nil: @@ -209,7 +194,8 @@ proc identify*( when defined(libp2p_agents_metrics): var knownAgent = "unknown" - shortAgent = info.agentVersion.get("").split("/")[0].safeToLowerAscii().get("") + shortAgent = + info.agentVersion.get("").split("/")[0].safeToLowerAscii().get("") if KnownLibP2PAgentsSeq.contains(shortAgent): knownAgent = shortAgent muxer.connection.setShortAgent(knownAgent) diff --git a/libp2p/protobuf/minprotobuf.nim b/libp2p/protobuf/minprotobuf.nim index 31c98d1cfd..af8485b4e7 100644 --- a/libp2p/protobuf/minprotobuf.nim +++ b/libp2p/protobuf/minprotobuf.nim @@ -21,14 +21,20 @@ const MaxMessageSize = 1'u shl 22 type ProtoFieldKind* = enum ## Protobuf's field types enum - Varint, Fixed64, Length, StartGroup, EndGroup, Fixed32 + Varint + Fixed64 + Length + StartGroup + EndGroup + Fixed32 ProtoFlags* = enum ## Protobuf's encoding types - WithVarintLength, WithUint32BeLength, WithUint32LeLength + WithVarintLength + WithUint32BeLength + WithUint32LeLength - ProtoBuffer* = object - ## Protobuf's message representation object + ProtoBuffer* = object ## Protobuf's message representation object options: set[ProtoFlags] buffer*: seq[byte] offset*: int @@ -39,8 +45,7 @@ type wire*: ProtoFieldKind index*: uint64 - ProtoField* = object - ## Protobuf's message field representation object + ProtoField* = object ## Protobuf's message field representation object index*: int case kind*: ProtoFieldKind of Varint: @@ -55,30 +60,33 @@ type discard ProtoError* {.pure.} = enum - VarintDecode, - MessageIncomplete, - BufferOverflow, - MessageTooBig, - BadWireType, - IncorrectBlob, + VarintDecode + MessageIncomplete + BufferOverflow + MessageTooBig + BadWireType + IncorrectBlob RequiredFieldMissing ProtoResult*[T] = Result[T, ProtoError] - ProtoScalar* = uint | uint32 | uint64 | zint | zint32 | zint64 | - hint | hint32 | hint64 | float32 | float64 + ProtoScalar* = + uint | uint32 | uint64 | zint | zint32 | zint64 | hint | hint32 | hint64 | float32 | + float64 -const - SupportedWireTypes* = @[ +const SupportedWireTypes* = + @[ uint64(ProtoFieldKind.Varint), uint64(ProtoFieldKind.Fixed64), uint64(ProtoFieldKind.Length), - uint64(ProtoFieldKind.Fixed32) + uint64(ProtoFieldKind.Fixed32), ] template checkFieldNumber*(i: int) = - doAssert((i > 0 and i < (1 shl 29)) and not(i >= 19000 and i <= 19999), - "Incorrect or reserved field number") + doAssert( + (i > 0 and i < (1 shl 29)) and not (i >= 19000 and i <= 19999), + "Incorrect or reserved field number", + ) template getProtoHeader*(index: int, wire: ProtoFieldKind): uint64 = ## Get protobuf's field header integer for ``index`` and ``wire``. @@ -114,29 +122,34 @@ proc vsizeof*(field: ProtoField): int {.inline.} = vsizeof(getProtoHeader(field)) + sizeof(field.vfloat32) of ProtoFieldKind.Length: vsizeof(getProtoHeader(field)) + vsizeof(uint64(len(field.vbuffer))) + - len(field.vbuffer) + len(field.vbuffer) else: 0 -proc initProtoBuffer*(data: seq[byte], offset = 0, - options: set[ProtoFlags] = {}, - maxSize = MaxMessageSize): ProtoBuffer = +proc initProtoBuffer*( + data: seq[byte], offset = 0, options: set[ProtoFlags] = {}, maxSize = MaxMessageSize +): ProtoBuffer = ## Initialize ProtoBuffer with shallow copy of ``data``. result.buffer = data result.offset = offset result.options = options result.maxSize = maxSize -proc initProtoBuffer*(data: openArray[byte], offset = 0, - options: set[ProtoFlags] = {}, - maxSize = MaxMessageSize): ProtoBuffer = +proc initProtoBuffer*( + data: openArray[byte], + offset = 0, + options: set[ProtoFlags] = {}, + maxSize = MaxMessageSize, +): ProtoBuffer = ## Initialize ProtoBuffer with copy of ``data``. result.buffer = @data result.offset = offset result.options = options result.maxSize = maxSize -proc initProtoBuffer*(options: set[ProtoFlags] = {}, maxSize = MaxMessageSize): ProtoBuffer = +proc initProtoBuffer*( + options: set[ProtoFlags] = {}, maxSize = MaxMessageSize +): ProtoBuffer = ## Initialize ProtoBuffer with new sequence of capacity ``cap``. result.buffer = newSeq[byte]() result.options = options @@ -152,37 +165,31 @@ proc initProtoBuffer*(options: set[ProtoFlags] = {}, maxSize = MaxMessageSize): result.buffer.setLen(4) result.offset = 4 -proc write*[T: ProtoScalar](pb: var ProtoBuffer, - field: int, value: T) = +proc write*[T: ProtoScalar](pb: var ProtoBuffer, field: int, value: T) = checkFieldNumber(field) var length = 0 - when (T is uint64) or (T is uint32) or (T is uint) or - (T is zint64) or (T is zint32) or (T is zint) or - (T is hint64) or (T is hint32) or (T is hint): - let flength = vsizeof(getProtoHeader(field, ProtoFieldKind.Varint)) + - vsizeof(value) + when (T is uint64) or (T is uint32) or (T is uint) or (T is zint64) or (T is zint32) or + (T is zint) or (T is hint64) or (T is hint32) or (T is hint): + let flength = vsizeof(getProtoHeader(field, ProtoFieldKind.Varint)) + vsizeof(value) let header = ProtoFieldKind.Varint elif T is float32: - let flength = vsizeof(getProtoHeader(field, ProtoFieldKind.Fixed32)) + - sizeof(T) + let flength = vsizeof(getProtoHeader(field, ProtoFieldKind.Fixed32)) + sizeof(T) let header = ProtoFieldKind.Fixed32 elif T is float64: - let flength = vsizeof(getProtoHeader(field, ProtoFieldKind.Fixed64)) + - sizeof(T) + let flength = vsizeof(getProtoHeader(field, ProtoFieldKind.Fixed64)) + sizeof(T) let header = ProtoFieldKind.Fixed64 pb.buffer.setLen(len(pb.buffer) + flength) - let hres = PB.putUVarint(pb.toOpenArray(), length, - getProtoHeader(field, header)) + let hres = PB.putUVarint(pb.toOpenArray(), length, getProtoHeader(field, header)) doAssert(hres.isOk()) pb.offset += length when (T is uint64) or (T is uint32) or (T is uint): let vres = PB.putUVarint(pb.toOpenArray(), length, value) doAssert(vres.isOk()) pb.offset += length - elif (T is zint64) or (T is zint32) or (T is zint) or - (T is hint64) or (T is hint32) or (T is hint): + elif (T is zint64) or (T is zint32) or (T is zint) or (T is hint64) or (T is hint32) or + (T is hint): let vres = putSVarint(pb.toOpenArray(), length, value) doAssert(vres.isOk()) pb.offset += length @@ -197,14 +204,14 @@ proc write*[T: ProtoScalar](pb: var ProtoBuffer, pb.buffer[pb.offset ..< pb.offset + sizeof(T)] = u64.toBytesLE() pb.offset += sizeof(T) -proc writePacked*[T: ProtoScalar](pb: var ProtoBuffer, field: int, - value: openArray[T]) = +proc writePacked*[T: ProtoScalar]( + pb: var ProtoBuffer, field: int, value: openArray[T] +) = checkFieldNumber(field) var length = 0 let dlength = - when (T is uint64) or (T is uint32) or (T is uint) or - (T is zint64) or (T is zint32) or (T is zint) or - (T is hint64) or (T is hint32) or (T is hint): + when (T is uint64) or (T is uint32) or (T is uint) or (T is zint64) or (T is zint32) or + (T is zint) or (T is hint64) or (T is hint32) or (T is hint): var res = 0 for item in value: res += vsizeof(item) @@ -228,8 +235,8 @@ proc writePacked*[T: ProtoScalar](pb: var ProtoBuffer, field: int, let vres = PB.putUVarint(pb.toOpenArray(), length, item) doAssert(vres.isOk()) pb.offset += length - elif (T is zint64) or (T is zint32) or (T is zint) or - (T is hint64) or (T is hint32) or (T is hint): + elif (T is zint64) or (T is zint32) or (T is zint) or (T is hint64) or (T is hint32) or + (T is hint): length = 0 let vres = PB.putSVarint(pb.toOpenArray(), length, item) doAssert(vres.isOk()) @@ -245,19 +252,19 @@ proc writePacked*[T: ProtoScalar](pb: var ProtoBuffer, field: int, pb.buffer[pb.offset ..< pb.offset + sizeof(T)] = u64.toBytesLE() pb.offset += sizeof(T) -proc write*[T: byte|char](pb: var ProtoBuffer, field: int, - value: openArray[T]) = +proc write*[T: byte | char](pb: var ProtoBuffer, field: int, value: openArray[T]) = checkFieldNumber(field) var length = 0 - let flength = vsizeof(getProtoHeader(field, ProtoFieldKind.Length)) + - vsizeof(uint64(len(value))) + len(value) + let flength = + vsizeof(getProtoHeader(field, ProtoFieldKind.Length)) + vsizeof(uint64(len(value))) + + len(value) pb.buffer.setLen(len(pb.buffer) + flength) - let hres = PB.putUVarint(pb.toOpenArray(), length, - getProtoHeader(field, ProtoFieldKind.Length)) + let hres = PB.putUVarint( + pb.toOpenArray(), length, getProtoHeader(field, ProtoFieldKind.Length) + ) doAssert(hres.isOk()) pb.offset += length - let lres = PB.putUVarint(pb.toOpenArray(), length, - uint64(len(value))) + let lres = PB.putUVarint(pb.toOpenArray(), length, uint64(len(value))) doAssert(lres.isOk()) pb.offset += length if len(value) > 0: @@ -294,8 +301,7 @@ proc finish*(pb: var ProtoBuffer) = doAssert(len(pb.buffer) > 0) pb.offset = 0 -proc getHeader(data: var ProtoBuffer, - header: var ProtoHeader): ProtoResult[void] = +proc getHeader(data: var ProtoBuffer, header: var ProtoHeader): ProtoResult[void] = var length = 0 var hdr = 0'u64 if PB.getUVarint(data.toOpenArray(), length, hdr).isOk(): @@ -350,9 +356,9 @@ proc skipValue(data: var ProtoBuffer, header: ProtoHeader): ProtoResult[void] = of ProtoFieldKind.StartGroup, ProtoFieldKind.EndGroup: err(ProtoError.BadWireType) -proc getValue[T: ProtoScalar](data: var ProtoBuffer, - header: ProtoHeader, - outval: var T): ProtoResult[void] = +proc getValue[T: ProtoScalar]( + data: var ProtoBuffer, header: ProtoHeader, outval: var T +): ProtoResult[void] = when (T is uint64) or (T is uint32) or (T is uint): doAssert(header.wire == ProtoFieldKind.Varint) var length = 0 @@ -363,8 +369,8 @@ proc getValue[T: ProtoScalar](data: var ProtoBuffer, ok() else: err(ProtoError.VarintDecode) - elif (T is zint64) or (T is zint32) or (T is zint) or - (T is hint64) or (T is hint32) or (T is hint): + elif (T is zint64) or (T is zint32) or (T is zint) or (T is hint64) or (T is hint32) or + (T is hint): doAssert(header.wire == ProtoFieldKind.Varint) var length = 0 var value = T(0) @@ -391,9 +397,12 @@ proc getValue[T: ProtoScalar](data: var ProtoBuffer, else: err(ProtoError.MessageIncomplete) -proc getValue[T:byte|char](data: var ProtoBuffer, header: ProtoHeader, - outBytes: var openArray[T], - outLength: var int): ProtoResult[void] = +proc getValue[T: byte | char]( + data: var ProtoBuffer, + header: ProtoHeader, + outBytes: var openArray[T], + outLength: var int, +): ProtoResult[void] = doAssert(header.wire == ProtoFieldKind.Length) var length = 0 var bsize = 0'u64 @@ -420,8 +429,9 @@ proc getValue[T:byte|char](data: var ProtoBuffer, header: ProtoHeader, else: err(ProtoError.VarintDecode) -proc getValue[T:seq[byte]|string](data: var ProtoBuffer, header: ProtoHeader, - outBytes: var T): ProtoResult[void] = +proc getValue[T: seq[byte] | string]( + data: var ProtoBuffer, header: ProtoHeader, outBytes: var T +): ProtoResult[void] = doAssert(header.wire == ProtoFieldKind.Length) var length = 0 var bsize = 0'u64 @@ -443,20 +453,20 @@ proc getValue[T:seq[byte]|string](data: var ProtoBuffer, header: ProtoHeader, else: err(ProtoError.VarintDecode) -proc getField*[T: ProtoScalar](data: ProtoBuffer, field: int, - output: var T): ProtoResult[bool] = +proc getField*[T: ProtoScalar]( + data: ProtoBuffer, field: int, output: var T +): ProtoResult[bool] = checkFieldNumber(field) var current: T var res = false var pb = data - while not(pb.isEmpty()): + while not (pb.isEmpty()): var header: ProtoHeader - ? pb.getHeader(header) + ?pb.getHeader(header) let wireCheck = - when (T is uint64) or (T is uint32) or (T is uint) or - (T is zint64) or (T is zint32) or (T is zint) or - (T is hint64) or (T is hint32) or (T is hint): + when (T is uint64) or (T is uint32) or (T is uint) or (T is zint64) or + (T is zint32) or (T is zint) or (T is hint64) or (T is hint32) or (T is hint): header.wire == ProtoFieldKind.Varint elif T is float32: header.wire == ProtoFieldKind.Fixed32 @@ -474,9 +484,9 @@ proc getField*[T: ProtoScalar](data: ProtoBuffer, field: int, else: # We are ignoring wire types different from what we expect, because it # is how `protoc` is working. - ? pb.skipValue(header) + ?pb.skipValue(header) else: - ? pb.skipValue(header) + ?pb.skipValue(header) if res: output = current @@ -484,16 +494,16 @@ proc getField*[T: ProtoScalar](data: ProtoBuffer, field: int, else: ok(false) -proc getField*[T: byte|char](data: ProtoBuffer, field: int, - output: var openArray[T], - outlen: var int): ProtoResult[bool] = +proc getField*[T: byte | char]( + data: ProtoBuffer, field: int, output: var openArray[T], outlen: var int +): ProtoResult[bool] = checkFieldNumber(field) var pb = data var res = false outlen = 0 - while not(pb.isEmpty()): + while not (pb.isEmpty()): var header: ProtoHeader let hres = pb.getHeader(header) if hres.isErr(): @@ -536,13 +546,14 @@ proc getField*[T: byte|char](data: ProtoBuffer, field: int, else: ok(false) -proc getField*[T: seq[byte]|string](data: ProtoBuffer, field: int, - output: var T): ProtoResult[bool] = +proc getField*[T: seq[byte] | string]( + data: ProtoBuffer, field: int, output: var T +): ProtoResult[bool] = checkFieldNumber(field) var res = false var pb = data - while not(pb.isEmpty()): + while not (pb.isEmpty()): var header: ProtoHeader let hres = pb.getHeader(header) if hres.isErr(): @@ -573,29 +584,32 @@ proc getField*[T: seq[byte]|string](data: ProtoBuffer, field: int, else: ok(false) -proc getField*(pb: ProtoBuffer, field: int, - output: var ProtoBuffer): ProtoResult[bool] {.inline.} = +proc getField*( + pb: ProtoBuffer, field: int, output: var ProtoBuffer +): ProtoResult[bool] {.inline.} = var buffer: seq[byte] - if ? pb.getField(field, buffer): + if ?pb.getField(field, buffer): output = initProtoBuffer(buffer) ok(true) else: ok(false) -proc getRequiredField*[T](pb: ProtoBuffer, field: int, - output: var T): ProtoResult[void] {.inline.} = - if ? pb.getField(field, output): +proc getRequiredField*[T]( + pb: ProtoBuffer, field: int, output: var T +): ProtoResult[void] {.inline.} = + if ?pb.getField(field, output): ok() else: err(RequiredFieldMissing) -proc getRepeatedField*[T: seq[byte]|string](data: ProtoBuffer, field: int, - output: var seq[T]): ProtoResult[bool] = +proc getRepeatedField*[T: seq[byte] | string]( + data: ProtoBuffer, field: int, output: var seq[T] +): ProtoResult[bool] = checkFieldNumber(field) var pb = data output.setLen(0) - while not(pb.isEmpty()): + while not (pb.isEmpty()): var header: ProtoHeader let hres = pb.getHeader(header) if hres.isErr(): @@ -626,13 +640,14 @@ proc getRepeatedField*[T: seq[byte]|string](data: ProtoBuffer, field: int, else: ok(false) -proc getRepeatedField*[T: ProtoScalar](data: ProtoBuffer, field: int, - output: var seq[T]): ProtoResult[bool] = +proc getRepeatedField*[T: ProtoScalar]( + data: ProtoBuffer, field: int, output: var seq[T] +): ProtoResult[bool] = checkFieldNumber(field) var pb = data output.setLen(0) - while not(pb.isEmpty()): + while not (pb.isEmpty()): var header: ProtoHeader let hres = pb.getHeader(header) if hres.isErr(): @@ -640,8 +655,8 @@ proc getRepeatedField*[T: ProtoScalar](data: ProtoBuffer, field: int, return err(hres.error) if header.index == uint64(field): - if header.wire in {ProtoFieldKind.Varint, ProtoFieldKind.Fixed32, - ProtoFieldKind.Fixed64}: + if header.wire in + {ProtoFieldKind.Varint, ProtoFieldKind.Fixed32, ProtoFieldKind.Fixed64}: var item: T let vres = getValue(pb, header, item) if vres.isOk(): @@ -665,20 +680,22 @@ proc getRepeatedField*[T: ProtoScalar](data: ProtoBuffer, field: int, else: ok(false) -proc getRequiredRepeatedField*[T](pb: ProtoBuffer, field: int, - output: var seq[T]): ProtoResult[void] {.inline.} = - if ? pb.getRepeatedField(field, output): +proc getRequiredRepeatedField*[T]( + pb: ProtoBuffer, field: int, output: var seq[T] +): ProtoResult[void] {.inline.} = + if ?pb.getRepeatedField(field, output): ok() else: err(RequiredFieldMissing) -proc getPackedRepeatedField*[T: ProtoScalar](data: ProtoBuffer, field: int, - output: var seq[T]): ProtoResult[bool] = +proc getPackedRepeatedField*[T: ProtoScalar]( + data: ProtoBuffer, field: int, output: var seq[T] +): ProtoResult[bool] = checkFieldNumber(field) var pb = data output.setLen(0) - while not(pb.isEmpty()): + while not (pb.isEmpty()): var header: ProtoHeader let hres = pb.getHeader(header) if hres.isErr(): @@ -692,15 +709,15 @@ proc getPackedRepeatedField*[T: ProtoScalar](data: ProtoBuffer, field: int, if ares.isOk(): var pbarr = initProtoBuffer(arritem) let itemHeader = - when (T is uint64) or (T is uint32) or (T is uint) or - (T is zint64) or (T is zint32) or (T is zint) or - (T is hint64) or (T is hint32) or (T is hint): + when (T is uint64) or (T is uint32) or (T is uint) or (T is zint64) or + (T is zint32) or (T is zint) or (T is hint64) or (T is hint32) or + (T is hint): ProtoHeader(wire: ProtoFieldKind.Varint) elif T is float32: ProtoHeader(wire: ProtoFieldKind.Fixed32) elif T is float64: ProtoHeader(wire: ProtoFieldKind.Fixed64) - while not(pbarr.isEmpty()): + while not (pbarr.isEmpty()): var item: T let vres = getValue(pbarr, itemHeader, item) if vres.isOk(): diff --git a/libp2p/protocols/connectivity/autonat/client.nim b/libp2p/protocols/connectivity/autonat/client.nim index 81a4efe2ca..c20b724109 100644 --- a/libp2p/protocols/connectivity/autonat/client.nim +++ b/libp2p/protocols/connectivity/autonat/client.nim @@ -11,28 +11,29 @@ import stew/results import chronos, chronicles -import ../../../switch, - ../../../multiaddress, - ../../../peerid +import ../../../switch, ../../../multiaddress, ../../../peerid import core logScope: topics = "libp2p autonat" -type - AutonatClient* = ref object of RootObj +type AutonatClient* = ref object of RootObj proc sendDial(conn: Connection, pid: PeerId, addrs: seq[MultiAddress]) {.async.} = - let pb = AutonatDial(peerInfo: Opt.some(AutonatPeerInfo( - id: Opt.some(pid), - addrs: addrs - ))).encode() + let pb = AutonatDial( + peerInfo: Opt.some(AutonatPeerInfo(id: Opt.some(pid), addrs: addrs)) + ).encode() await conn.writeLp(pb.buffer) -method dialMe*(self: AutonatClient, switch: Switch, pid: PeerId, addrs: seq[MultiAddress] = newSeq[MultiAddress]()): - Future[MultiAddress] {.base, async.} = - - proc getResponseOrRaise(autonatMsg: Opt[AutonatMsg]): AutonatDialResponse {.raises: [AutonatError].} = +method dialMe*( + self: AutonatClient, + switch: Switch, + pid: PeerId, + addrs: seq[MultiAddress] = newSeq[MultiAddress](), +): Future[MultiAddress] {.base, async.} = + proc getResponseOrRaise( + autonatMsg: Opt[AutonatMsg] + ): AutonatDialResponse {.raises: [AutonatError].} = autonatMsg.withValue(msg): if msg.msgType == DialResponse: msg.response.withValue(res): @@ -47,24 +48,32 @@ method dialMe*(self: AutonatClient, switch: Switch, pid: PeerId, addrs: seq[Mult else: await switch.dial(pid, addrs, AutonatCodec) except CatchableError as err: - raise newException(AutonatError, "Unexpected error when dialling: " & err.msg, err) + raise + newException(AutonatError, "Unexpected error when dialling: " & err.msg, err) # To bypass maxConnectionsPerPeer let incomingConnection = switch.connManager.expectConnection(pid, In) - if incomingConnection.failed() and incomingConnection.error of AlreadyExpectingConnectionError: + if incomingConnection.failed() and + incomingConnection.error of AlreadyExpectingConnectionError: raise newException(AutonatError, incomingConnection.error.msg) defer: await conn.close() - incomingConnection.cancel() # Safer to always try to cancel cause we aren't sure if the peer dialled us or not + incomingConnection.cancel() + # Safer to always try to cancel cause we aren't sure if the peer dialled us or not if incomingConnection.completed(): await (await incomingConnection).connection.close() trace "sending Dial", addrs = switch.peerInfo.addrs await conn.sendDial(switch.peerInfo.peerId, switch.peerInfo.addrs) let response = getResponseOrRaise(AutonatMsg.decode(await conn.readLp(1024))) - return case response.status: + return + case response.status of ResponseStatus.Ok: response.ma.tryGet() of ResponseStatus.DialError: - raise newException(AutonatUnreachableError, "Peer could not dial us back: " & response.text.get("")) + raise newException( + AutonatUnreachableError, "Peer could not dial us back: " & response.text.get("") + ) else: - raise newException(AutonatError, "Bad status " & $response.status & " " & response.text.get("")) + raise newException( + AutonatError, "Bad status " & $response.status & " " & response.text.get("") + ) diff --git a/libp2p/protocols/connectivity/autonat/core.nim b/libp2p/protocols/connectivity/autonat/core.nim index bfb9beaaaa..ab108267c4 100644 --- a/libp2p/protocols/connectivity/autonat/core.nim +++ b/libp2p/protocols/connectivity/autonat/core.nim @@ -11,9 +11,7 @@ import stew/[results, objects] import chronos, chronicles -import ../../../multiaddress, - ../../../peerid, - ../../../errors +import ../../../multiaddress, ../../../peerid, ../../../errors logScope: topics = "libp2p autonat" @@ -55,7 +53,9 @@ type response*: Opt[AutonatDialResponse] NetworkReachability* {.pure.} = enum - Unknown, NotReachable, Reachable + Unknown + NotReachable + Reachable proc encode(p: AutonatPeerInfo): ProtoBuffer = result = initProtoBuffer() @@ -103,37 +103,39 @@ proc decode*(_: typedesc[AutonatMsg], buf: seq[byte]): Opt[AutonatMsg] = let pb = initProtoBuffer(buf) - if ? pb.getField(1, msgTypeOrd).toOpt() and not checkedEnumAssign(msg.msgType, msgTypeOrd): + if ?pb.getField(1, msgTypeOrd).toOpt() and + not checkedEnumAssign(msg.msgType, msgTypeOrd): return Opt.none(AutonatMsg) - if ? pb.getField(2, pbDial).toOpt(): + if ?pb.getField(2, pbDial).toOpt(): var pbPeerInfo: ProtoBuffer dial: AutonatDial - let r4 = ? pbDial.getField(1, pbPeerInfo).toOpt() + let r4 = ?pbDial.getField(1, pbPeerInfo).toOpt() var peerInfo: AutonatPeerInfo if r4: var pid: PeerId let - r5 = ? pbPeerInfo.getField(1, pid).toOpt() - r6 = ? pbPeerInfo.getRepeatedField(2, peerInfo.addrs).toOpt() - if r5: peerInfo.id = Opt.some(pid) + r5 = ?pbPeerInfo.getField(1, pid).toOpt() + r6 = ?pbPeerInfo.getRepeatedField(2, peerInfo.addrs).toOpt() + if r5: + peerInfo.id = Opt.some(pid) dial.peerInfo = Opt.some(peerInfo) msg.dial = Opt.some(dial) - if ? pb.getField(3, pbResponse).toOpt(): + if ?pb.getField(3, pbResponse).toOpt(): var statusOrd: uint text: string ma: MultiAddress response: AutonatDialResponse - if ? pbResponse.getField(1, statusOrd).optValue(): + if ?pbResponse.getField(1, statusOrd).optValue(): if not checkedEnumAssign(response.status, statusOrd): return Opt.none(AutonatMsg) - if ? pbResponse.getField(2, text).optValue(): + if ?pbResponse.getField(2, text).optValue(): response.text = Opt.some(text) - if ? pbResponse.getField(3, ma).optValue(): + if ?pbResponse.getField(3, ma).optValue(): response.ma = Opt.some(ma) msg.response = Opt.some(response) return Opt.some(msg) diff --git a/libp2p/protocols/connectivity/autonat/server.nim b/libp2p/protocols/connectivity/autonat/server.nim index b58a912998..fdf3a15997 100644 --- a/libp2p/protocols/connectivity/autonat/server.nim +++ b/libp2p/protocols/connectivity/autonat/server.nim @@ -12,13 +12,14 @@ import std/[sets, sequtils] import stew/results import chronos, chronicles -import ../../protocol, - ../../../switch, - ../../../multiaddress, - ../../../multicodec, - ../../../peerid, - ../../../utils/[semaphore, future], - ../../../errors +import + ../../protocol, + ../../../switch, + ../../../multiaddress, + ../../../multicodec, + ../../../peerid, + ../../../utils/[semaphore, future], + ../../../errors import core export core @@ -26,33 +27,36 @@ export core logScope: topics = "libp2p autonat" -type - Autonat* = ref object of LPProtocol - sem: AsyncSemaphore - switch*: Switch - dialTimeout: Duration +type Autonat* = ref object of LPProtocol + sem: AsyncSemaphore + switch*: Switch + dialTimeout: Duration proc sendDial(conn: Connection, pid: PeerId, addrs: seq[MultiAddress]) {.async.} = - let pb = AutonatDial(peerInfo: Opt.some(AutonatPeerInfo( - id: Opt.some(pid), - addrs: addrs - ))).encode() + let pb = AutonatDial( + peerInfo: Opt.some(AutonatPeerInfo(id: Opt.some(pid), addrs: addrs)) + ).encode() await conn.writeLp(pb.buffer) -proc sendResponseError(conn: Connection, status: ResponseStatus, text: string = "") {.async.} = +proc sendResponseError( + conn: Connection, status: ResponseStatus, text: string = "" +) {.async.} = let pb = AutonatDialResponse( - status: status, - text: if text == "": Opt.none(string) else: Opt.some(text), - ma: Opt.none(MultiAddress) - ).encode() + status: status, + text: + if text == "": + Opt.none(string) + else: + Opt.some(text) + , + ma: Opt.none(MultiAddress), + ).encode() await conn.writeLp(pb.buffer) proc sendResponseOk(conn: Connection, ma: MultiAddress) {.async.} = let pb = AutonatDialResponse( - status: ResponseStatus.Ok, - text: Opt.some("Ok"), - ma: Opt.some(ma) - ).encode() + status: ResponseStatus.Ok, text: Opt.some("Ok"), ma: Opt.some(ma) + ).encode() await conn.writeLp(pb.buffer) proc tryDial(autonat: Autonat, conn: Connection, addrs: seq[MultiAddress]) {.async.} = @@ -60,12 +64,15 @@ proc tryDial(autonat: Autonat, conn: Connection, addrs: seq[MultiAddress]) {.asy var futs: seq[Future[Opt[MultiAddress]]] try: # This is to bypass the per peer max connections limit - let outgoingConnection = autonat.switch.connManager.expectConnection(conn.peerId, Out) - if outgoingConnection.failed() and outgoingConnection.error of AlreadyExpectingConnectionError: + let outgoingConnection = + autonat.switch.connManager.expectConnection(conn.peerId, Out) + if outgoingConnection.failed() and + outgoingConnection.error of AlreadyExpectingConnectionError: await conn.sendResponseError(DialRefused, outgoingConnection.error.msg) return # Safer to always try to cancel cause we aren't sure if the connection was established - defer: outgoingConnection.cancel() + defer: + outgoingConnection.cancel() # tryDial is to bypass the global max connections limit futs = addrs.mapIt(autonat.switch.dialer.tryDial(conn.peerId, @[it])) let fut = await anyCompleted(futs).wait(autonat.dialTimeout) @@ -106,7 +113,8 @@ proc handleDial(autonat: Autonat, conn: Connection, msg: AutonatMsg): Future[voi var isRelayed = observedAddr.contains(multiCodec("p2p-circuit")).valueOr: return conn.sendResponseError(DialRefused, "Invalid observed address") if isRelayed: - return conn.sendResponseError(DialRefused, "Refused to dial a relayed observed address") + return + conn.sendResponseError(DialRefused, "Refused to dial a relayed observed address") let hostIp = observedAddr[0].valueOr: return conn.sendResponseError(InternalError, "Wrong observed address") if not IP.match(hostIp): @@ -115,16 +123,20 @@ proc handleDial(autonat: Autonat, conn: Connection, msg: AutonatMsg): Future[voi addrs.incl(observedAddr) trace "addrs received", addrs = peerInfo.addrs for ma in peerInfo.addrs: - isRelayed = ma.contains(multiCodec("p2p-circuit")).valueOr: continue - let maFirst = ma[0].valueOr: continue - if not DNS_OR_IP.match(maFirst): continue + isRelayed = ma.contains(multiCodec("p2p-circuit")).valueOr: + continue + let maFirst = ma[0].valueOr: + continue + if not DNS_OR_IP.match(maFirst): + continue try: addrs.incl( if maFirst == hostIp: ma else: - let maEnd = ma[1..^1].valueOr: continue + let maEnd = ma[1 ..^ 1].valueOr: + continue hostIp & maEnd ) except LPError as exc: @@ -138,8 +150,11 @@ proc handleDial(autonat: Autonat, conn: Connection, msg: AutonatMsg): Future[voi trace "trying to dial", addrs = addrsSeq return autonat.tryDial(conn, addrsSeq) -proc new*(T: typedesc[Autonat], switch: Switch, semSize: int = 1, dialTimeout = 15.seconds): T = - let autonat = T(switch: switch, sem: newAsyncSemaphore(semSize), dialTimeout: dialTimeout) +proc new*( + T: typedesc[Autonat], switch: Switch, semSize: int = 1, dialTimeout = 15.seconds +): T = + let autonat = + T(switch: switch, sem: newAsyncSemaphore(semSize), dialTimeout: dialTimeout) proc handleStream(conn: Connection, proto: string) {.async.} = try: let msg = AutonatMsg.decode(await conn.readLp(1024)).valueOr: diff --git a/libp2p/protocols/connectivity/autonat/service.nim b/libp2p/protocols/connectivity/autonat/service.nim index 0a6e0026a4..8fe352b7cb 100644 --- a/libp2p/protocols/connectivity/autonat/service.nim +++ b/libp2p/protocols/connectivity/autonat/service.nim @@ -23,7 +23,11 @@ export core.NetworkReachability logScope: topics = "libp2p autonatservice" -declarePublicGauge(libp2p_autonat_reachability_confidence, "autonat reachability confidence", labels = ["reachability"]) +declarePublicGauge( + libp2p_autonat_reachability_confidence, + "autonat reachability confidence", + labels = ["reachability"], +) type AutonatService* = ref object of Service @@ -44,19 +48,22 @@ type dialTimeout: Duration enableAddressMapper: bool - StatusAndConfidenceHandler* = proc (networkReachability: NetworkReachability, confidence: Opt[float]): Future[void] {.gcsafe, raises: [].} + StatusAndConfidenceHandler* = proc( + networkReachability: NetworkReachability, confidence: Opt[float] + ): Future[void] {.gcsafe, raises: [].} proc new*( - T: typedesc[AutonatService], - autonatClient: AutonatClient, - rng: ref HmacDrbgContext, - scheduleInterval: Opt[Duration] = Opt.none(Duration), - askNewConnectedPeers = true, - numPeersToAsk: int = 5, - maxQueueSize: int = 10, - minConfidence: float = 0.3, - dialTimeout = 30.seconds, - enableAddressMapper = true): T = + T: typedesc[AutonatService], + autonatClient: AutonatClient, + rng: ref HmacDrbgContext, + scheduleInterval: Opt[Duration] = Opt.none(Duration), + askNewConnectedPeers = true, + numPeersToAsk: int = 5, + maxQueueSize: int = 10, + minConfidence: float = 0.3, + dialTimeout = 30.seconds, + enableAddressMapper = true, +): T = return T( scheduleInterval: scheduleInterval, networkReachability: Unknown, @@ -69,7 +76,8 @@ proc new*( maxQueueSize: maxQueueSize, minConfidence: minConfidence, dialTimeout: dialTimeout, - enableAddressMapper: enableAddressMapper) + enableAddressMapper: enableAddressMapper, + ) proc callHandler(self: AutonatService) {.async.} = if not isNil(self.statusAndConfidenceHandler): @@ -82,8 +90,9 @@ proc hasEnoughIncomingSlots(switch: Switch): bool = proc doesPeerHaveIncomingConn(switch: Switch, peerId: PeerId): bool = return switch.connManager.selectMuxer(peerId, In) != nil -proc handleAnswer(self: AutonatService, ans: NetworkReachability): Future[bool] {.async.} = - +proc handleAnswer( + self: AutonatService, ans: NetworkReachability +): Future[bool] {.async.} = if ans == Unknown: return @@ -99,17 +108,26 @@ proc handleAnswer(self: AutonatService, ans: NetworkReachability): Future[bool] const reachabilityPriority = [Reachable, NotReachable] for reachability in reachabilityPriority: let confidence = self.answers.countIt(it == reachability) / self.maxQueueSize - libp2p_autonat_reachability_confidence.set(value = confidence, labelValues = [$reachability]) + libp2p_autonat_reachability_confidence.set( + value = confidence, labelValues = [$reachability] + ) if self.confidence.isNone and confidence >= self.minConfidence: self.networkReachability = reachability self.confidence = Opt.some(confidence) - debug "Current status", currentStats = $self.networkReachability, confidence = $self.confidence, answers = self.answers + debug "Current status", + currentStats = $self.networkReachability, + confidence = $self.confidence, + answers = self.answers # Return whether anything has changed - return self.networkReachability != oldNetworkReachability or self.confidence != oldConfidence + return + self.networkReachability != oldNetworkReachability or + self.confidence != oldConfidence -proc askPeer(self: AutonatService, switch: Switch, peerId: PeerId): Future[NetworkReachability] {.async.} = +proc askPeer( + self: AutonatService, switch: Switch, peerId: PeerId +): Future[NetworkReachability] {.async.} = logScope: peerId = $peerId @@ -117,7 +135,8 @@ proc askPeer(self: AutonatService, switch: Switch, peerId: PeerId): Future[Netwo return Unknown if not hasEnoughIncomingSlots(switch): - debug "No incoming slots available, not asking peer", incomingSlotsAvailable=switch.connManager.slotsAvailable(In) + debug "No incoming slots available, not asking peer", + incomingSlotsAvailable = switch.connManager.slotsAvailable(In) return Unknown trace "Asking peer for reachability" @@ -150,7 +169,8 @@ proc askConnectedPeers(self: AutonatService, switch: Switch) {.async.} = if answersFromPeers >= self.numPeersToAsk: break if not hasEnoughIncomingSlots(switch): - debug "No incoming slots available, not asking peers", incomingSlotsAvailable=switch.connManager.slotsAvailable(In) + debug "No incoming slots available, not asking peers", + incomingSlotsAvailable = switch.connManager.slotsAvailable(In) break if (await askPeer(self, switch, peer)) != Unknown: answersFromPeers.inc() @@ -160,10 +180,8 @@ proc schedule(service: AutonatService, switch: Switch, interval: Duration) {.asy await service.run(switch) proc addressMapper( - self: AutonatService, - peerStore: PeerStore, - listenAddrs: seq[MultiAddress]): Future[seq[MultiAddress]] {.async.} = - + self: AutonatService, peerStore: PeerStore, listenAddrs: seq[MultiAddress] +): Future[seq[MultiAddress]] {.async.} = if self.networkReachability != NetworkReachability.Reachable: return listenAddrs @@ -171,24 +189,32 @@ proc addressMapper( for listenAddr in listenAddrs: var processedMA = listenAddr try: - if not listenAddr.isPublicMA() and self.networkReachability == NetworkReachability.Reachable: - processedMA = peerStore.guessDialableAddr(listenAddr) # handle manual port forwarding + if not listenAddr.isPublicMA() and + self.networkReachability == NetworkReachability.Reachable: + processedMA = peerStore.guessDialableAddr(listenAddr) + # handle manual port forwarding except CatchableError as exc: debug "Error while handling address mapper", msg = exc.msg addrs.add(processedMA) return addrs method setup*(self: AutonatService, switch: Switch): Future[bool] {.async.} = - self.addressMapper = proc (listenAddrs: seq[MultiAddress]): Future[seq[MultiAddress]] {.async.} = + self.addressMapper = proc( + listenAddrs: seq[MultiAddress] + ): Future[seq[MultiAddress]] {.async.} = return await addressMapper(self, switch.peerStore, listenAddrs) info "Setting up AutonatService" let hasBeenSetup = await procCall Service(self).setup(switch) if hasBeenSetup: if self.askNewConnectedPeers: - self.newConnectedPeerHandler = proc (peerId: PeerId, event: PeerEvent): Future[void] {.async.} = + self.newConnectedPeerHandler = proc( + peerId: PeerId, event: PeerEvent + ): Future[void] {.async.} = discard askPeer(self, switch, peerId) - switch.connManager.addPeerEventHandler(self.newConnectedPeerHandler, PeerEventKind.Joined) + switch.connManager.addPeerEventHandler( + self.newConnectedPeerHandler, PeerEventKind.Joined + ) self.scheduleInterval.withValue(interval): self.scheduleHandle = schedule(self, switch, interval) if self.enableAddressMapper: @@ -207,11 +233,15 @@ method stop*(self: AutonatService, switch: Switch): Future[bool] {.async, public self.scheduleHandle.cancel() self.scheduleHandle = nil if not isNil(self.newConnectedPeerHandler): - switch.connManager.removePeerEventHandler(self.newConnectedPeerHandler, PeerEventKind.Joined) + switch.connManager.removePeerEventHandler( + self.newConnectedPeerHandler, PeerEventKind.Joined + ) if self.enableAddressMapper: switch.peerInfo.addressMappers.keepItIf(it != self.addressMapper) await switch.peerInfo.update() return hasBeenStopped -proc statusAndConfidenceHandler*(self: AutonatService, statusAndConfidenceHandler: StatusAndConfidenceHandler) = +proc statusAndConfidenceHandler*( + self: AutonatService, statusAndConfidenceHandler: StatusAndConfidenceHandler +) = self.statusAndConfidenceHandler = statusAndConfidenceHandler diff --git a/libp2p/protocols/connectivity/dcutr/client.nim b/libp2p/protocols/connectivity/dcutr/client.nim index d760218020..1c9fcb3259 100644 --- a/libp2p/protocols/connectivity/dcutr/client.nim +++ b/libp2p/protocols/connectivity/dcutr/client.nim @@ -15,25 +15,26 @@ import stew/results import chronos, chronicles import core -import ../../protocol, - ../../../stream/connection, - ../../../switch, - ../../../utils/future +import + ../../protocol, ../../../stream/connection, ../../../switch, ../../../utils/future export DcutrError -type - DcutrClient* = ref object - connectTimeout: Duration - maxDialableAddrs: int +type DcutrClient* = ref object + connectTimeout: Duration + maxDialableAddrs: int logScope: topics = "libp2p dcutrclient" -proc new*(T: typedesc[DcutrClient], connectTimeout = 15.seconds, maxDialableAddrs = 8): T = +proc new*( + T: typedesc[DcutrClient], connectTimeout = 15.seconds, maxDialableAddrs = 8 +): T = return T(connectTimeout: connectTimeout, maxDialableAddrs: maxDialableAddrs) -proc startSync*(self: DcutrClient, switch: Switch, remotePeerId: PeerId, addrs: seq[MultiAddress]) {.async.} = +proc startSync*( + self: DcutrClient, switch: Switch, remotePeerId: PeerId, addrs: seq[MultiAddress] +) {.async.} = logScope: peerId = switch.peerInfo.peerId @@ -43,7 +44,8 @@ proc startSync*(self: DcutrClient, switch: Switch, remotePeerId: PeerId, addrs: try: var ourDialableAddrs = getHolePunchableAddrs(addrs) if ourDialableAddrs.len == 0: - debug "Dcutr initiator has no supported dialable addresses. Aborting Dcutr.", addrs + debug "Dcutr initiator has no supported dialable addresses. Aborting Dcutr.", + addrs return stream = await switch.dial(remotePeerId, DcutrCodec) @@ -54,7 +56,8 @@ proc startSync*(self: DcutrClient, switch: Switch, remotePeerId: PeerId, addrs: peerDialableAddrs = getHolePunchableAddrs(connectAnswer.addrs) if peerDialableAddrs.len == 0: - debug "Dcutr receiver has no supported dialable addresses to connect to. Aborting Dcutr.", addrs=connectAnswer.addrs + debug "Dcutr receiver has no supported dialable addresses to connect to. Aborting Dcutr.", + addrs = connectAnswer.addrs return let rttEnd = Moment.now() @@ -65,25 +68,47 @@ proc startSync*(self: DcutrClient, switch: Switch, remotePeerId: PeerId, addrs: await sleepAsync(halfRtt) if peerDialableAddrs.len > self.maxDialableAddrs: - peerDialableAddrs = peerDialableAddrs[0.. maxDialableAddrs: - peerDialableAddrs = peerDialableAddrs[0.. int64.high().uint64 or - now().utc > reservation.expire.int64.fromUnix.utc: + now().utc > reservation.expire.int64.fromUnix.utc: raise newException(ReservationError, "Bad expiration date") result.expire = reservation.expire result.addrs = reservation.addrs @@ -115,43 +120,49 @@ proc reserve*(cl: RelayClient, result.limitData = msg.limit.data proc dialPeerV1*( - cl: RelayClient, - conn: Connection, - dstPeerId: PeerId, - dstAddrs: seq[MultiAddress]): Future[Connection] {.async.} = + cl: RelayClient, conn: Connection, dstPeerId: PeerId, dstAddrs: seq[MultiAddress] +): Future[Connection] {.async.} = var msg = RelayMessage( msgType: Opt.some(RelayType.Hop), - srcPeer: Opt.some(RelayPeer(peerId: cl.switch.peerInfo.peerId, addrs: cl.switch.peerInfo.addrs)), - dstPeer: Opt.some(RelayPeer(peerId: dstPeerId, addrs: dstAddrs))) + srcPeer: Opt.some( + RelayPeer(peerId: cl.switch.peerInfo.peerId, addrs: cl.switch.peerInfo.addrs) + ), + dstPeer: Opt.some(RelayPeer(peerId: dstPeerId, addrs: dstAddrs)), + ) pb = encode(msg) - trace "Dial peer", msgSend=msg + trace "Dial peer", msgSend = msg try: await conn.writeLp(pb.buffer) except CancelledError as exc: raise exc except CatchableError as exc: - trace "error writing hop request", exc=exc.msg + trace "error writing hop request", exc = exc.msg raise exc - let msgRcvFromRelayOpt = try: - RelayMessage.decode(await conn.readLp(RelayClientMsgSize)) - except CancelledError as exc: - raise exc - except CatchableError as exc: - trace "error reading stop response", exc=exc.msg - await sendStatus(conn, StatusV1.HopCantOpenDstStream) - raise exc + let msgRcvFromRelayOpt = + try: + RelayMessage.decode(await conn.readLp(RelayClientMsgSize)) + except CancelledError as exc: + raise exc + except CatchableError as exc: + trace "error reading stop response", exc = exc.msg + await sendStatus(conn, StatusV1.HopCantOpenDstStream) + raise exc try: let msgRcvFromRelay = msgRcvFromRelayOpt.valueOr: raise newException(RelayV1DialError, "Hop can't open destination stream") if msgRcvFromRelay.msgType.tryGet() != RelayType.Status: - raise newException(RelayV1DialError, "Hop can't open destination stream: wrong message type") + raise newException( + RelayV1DialError, "Hop can't open destination stream: wrong message type" + ) if msgRcvFromRelay.status.tryGet() != StatusV1.Success: - raise newException(RelayV1DialError, "Hop can't open destination stream: status failed") + raise newException( + RelayV1DialError, "Hop can't open destination stream: status failed" + ) except RelayV1DialError as exc: await sendStatus(conn, StatusV1.HopCantOpenDstStream) raise exc @@ -164,21 +175,23 @@ proc dialPeerV2*( cl: RelayClient, conn: RelayConnection, dstPeerId: PeerId, - dstAddrs: seq[MultiAddress]): Future[Connection] {.async.} = + dstAddrs: seq[MultiAddress], +): Future[Connection] {.async.} = let p = Peer(peerId: dstPeerId, addrs: dstAddrs) pb = encode(HopMessage(msgType: HopMessageType.Connect, peer: Opt.some(p))) trace "Dial peer", p - let msgRcvFromRelay = try: - await conn.writeLp(pb.buffer) - HopMessage.decode(await conn.readLp(RelayClientMsgSize)).tryGet() - except CancelledError as exc: - raise exc - except CatchableError as exc: - trace "error reading stop response", exc=exc.msg - raise newException(RelayV2DialError, exc.msg) + let msgRcvFromRelay = + try: + await conn.writeLp(pb.buffer) + HopMessage.decode(await conn.readLp(RelayClientMsgSize)).tryGet() + except CancelledError as exc: + raise exc + except CatchableError as exc: + trace "error reading stop response", exc = exc.msg + raise newException(RelayV2DialError, exc.msg) if msgRcvFromRelay.msgType != HopMessageType.Status: raise newException(RelayV2DialError, "Unexpected stop response") @@ -198,7 +211,7 @@ proc handleStopStreamV2(cl: RelayClient, conn: Connection) {.async.} = if msg.msgType == StopMessageType.Connect: await cl.handleRelayedConnect(conn, msg) else: - trace "Unexpected client / relayv2 handshake", msgType=msg.msgType + trace "Unexpected client / relayv2 handshake", msgType = msg.msgType await sendStopError(conn, MalformedMessage) proc handleStop(cl: RelayClient, conn: Connection, msg: RelayMessage) {.async.} = @@ -223,8 +236,10 @@ proc handleStop(cl: RelayClient, conn: Connection, msg: RelayMessage) {.async.} await sendStatus(conn, StatusV1.Success) # This sound redundant but the callback could, in theory, be set to nil during # sendStatus(Success) so it's safer to double check - if cl.onNewConnection != nil: await cl.onNewConnection(conn, 0, 0) - else: await conn.close() + if cl.onNewConnection != nil: + await cl.onNewConnection(conn, 0, 0) + else: + await conn.close() proc handleStreamV1(cl: RelayClient, conn: Connection) {.async.} = let msg = RelayMessage.decode(await conn.readLp(RelayClientMsgSize)).valueOr: @@ -236,42 +251,54 @@ proc handleStreamV1(cl: RelayClient, conn: Connection) {.async.} = trace "Message type not set" await sendStatus(conn, StatusV1.MalformedMessage) return - case typ: - of RelayType.Hop: - if cl.canHop: await cl.handleHop(conn, msg) - else: await sendStatus(conn, StatusV1.HopCantSpeakRelay) - of RelayType.Stop: await cl.handleStop(conn, msg) - of RelayType.CanHop: - if cl.canHop: await sendStatus(conn, StatusV1.Success) - else: await sendStatus(conn, StatusV1.HopCantSpeakRelay) + case typ + of RelayType.Hop: + if cl.canHop: + await cl.handleHop(conn, msg) + else: + await sendStatus(conn, StatusV1.HopCantSpeakRelay) + of RelayType.Stop: + await cl.handleStop(conn, msg) + of RelayType.CanHop: + if cl.canHop: + await sendStatus(conn, StatusV1.Success) else: - trace "Unexpected relay handshake", msgType=msg.msgType - await sendStatus(conn, StatusV1.MalformedMessage) - -proc new*(T: typedesc[RelayClient], canHop: bool = false, - reservationTTL: times.Duration = DefaultReservationTTL, - limitDuration: uint32 = DefaultLimitDuration, - limitData: uint64 = DefaultLimitData, - heartbeatSleepTime: uint32 = DefaultHeartbeatSleepTime, - maxCircuit: int = MaxCircuit, - maxCircuitPerPeer: int = MaxCircuitPerPeer, - msgSize: int = RelayClientMsgSize, - circuitRelayV1: bool = false): T = - - let cl = T(canHop: canHop, - reservationTTL: reservationTTL, - limit: Limit(duration: limitDuration, data: limitData), - heartbeatSleepTime: heartbeatSleepTime, - maxCircuit: maxCircuit, - maxCircuitPerPeer: maxCircuitPerPeer, - msgSize: msgSize, - isCircuitRelayV1: circuitRelayV1) + await sendStatus(conn, StatusV1.HopCantSpeakRelay) + else: + trace "Unexpected relay handshake", msgType = msg.msgType + await sendStatus(conn, StatusV1.MalformedMessage) + +proc new*( + T: typedesc[RelayClient], + canHop: bool = false, + reservationTTL: times.Duration = DefaultReservationTTL, + limitDuration: uint32 = DefaultLimitDuration, + limitData: uint64 = DefaultLimitData, + heartbeatSleepTime: uint32 = DefaultHeartbeatSleepTime, + maxCircuit: int = MaxCircuit, + maxCircuitPerPeer: int = MaxCircuitPerPeer, + msgSize: int = RelayClientMsgSize, + circuitRelayV1: bool = false, +): T = + let cl = T( + canHop: canHop, + reservationTTL: reservationTTL, + limit: Limit(duration: limitDuration, data: limitData), + heartbeatSleepTime: heartbeatSleepTime, + maxCircuit: maxCircuit, + maxCircuitPerPeer: maxCircuitPerPeer, + msgSize: msgSize, + isCircuitRelayV1: circuitRelayV1, + ) proc handleStream(conn: Connection, proto: string) {.async.} = try: - case proto: - of RelayV1Codec: await cl.handleStreamV1(conn) - of RelayV2StopCodec: await cl.handleStopStreamV2(conn) - of RelayV2HopCodec: await cl.handleHopStreamV2(conn) + case proto + of RelayV1Codec: + await cl.handleStreamV1(conn) + of RelayV2StopCodec: + await cl.handleStopStreamV2(conn) + of RelayV2HopCodec: + await cl.handleHopStreamV2(conn) except CancelledError as exc: raise exc except CatchableError as exc: @@ -281,8 +308,9 @@ proc new*(T: typedesc[RelayClient], canHop: bool = false, await conn.close() cl.handler = handleStream - cl.codecs = if cl.canHop: - @[RelayV1Codec, RelayV2HopCodec, RelayV2StopCodec] - else: - @[RelayV1Codec, RelayV2StopCodec] + cl.codecs = + if cl.canHop: + @[RelayV1Codec, RelayV2HopCodec, RelayV2StopCodec] + else: + @[RelayV1Codec, RelayV2StopCodec] cl diff --git a/libp2p/protocols/connectivity/relay/messages.nim b/libp2p/protocols/connectivity/relay/messages.nim index 8cb2bfa654..f3d92f4611 100644 --- a/libp2p/protocols/connectivity/relay/messages.nim +++ b/libp2p/protocols/connectivity/relay/messages.nim @@ -11,8 +11,7 @@ import macros import stew/[objects, results] -import ../../../peerinfo, - ../../../signed_envelope +import ../../../peerinfo, ../../../signed_envelope # Circuit Relay V1 Message @@ -87,22 +86,22 @@ proc decode*(_: typedesc[RelayMessage], buf: seq[byte]): Opt[RelayMessage] = let pb = initProtoBuffer(buf) - if ? pb.getField(1, msgTypeOrd).toOpt(): + if ?pb.getField(1, msgTypeOrd).toOpt(): if msgTypeOrd.int notin RelayType: return Opt.none(RelayMessage) rMsg.msgType = Opt.some(RelayType(msgTypeOrd)) - if ? pb.getField(2, pbSrc).toOpt(): - discard ? pbSrc.getField(1, src.peerId).toOpt() - discard ? pbSrc.getRepeatedField(2, src.addrs).toOpt() + if ?pb.getField(2, pbSrc).toOpt(): + discard ?pbSrc.getField(1, src.peerId).toOpt() + discard ?pbSrc.getRepeatedField(2, src.addrs).toOpt() rMsg.srcPeer = Opt.some(src) - if ? pb.getField(3, pbDst).toOpt(): - discard ? pbDst.getField(1, dst.peerId).toOpt() - discard ? pbDst.getRepeatedField(2, dst.addrs).toOpt() + if ?pb.getField(3, pbDst).toOpt(): + discard ?pbDst.getField(1, dst.peerId).toOpt() + discard ?pbDst.getRepeatedField(2, dst.addrs).toOpt() rMsg.dstPeer = Opt.some(dst) - if ? pb.getField(4, statusOrd).toOpt(): + if ?pb.getField(4, statusOrd).toOpt(): var status: StatusV1 if not checkedEnumAssign(status, statusOrd): return Opt.none(RelayMessage) @@ -111,19 +110,18 @@ proc decode*(_: typedesc[RelayMessage], buf: seq[byte]): Opt[RelayMessage] = # Voucher -type - Voucher* = object - relayPeerId*: PeerId # peer ID of the relay - reservingPeerId*: PeerId # peer ID of the reserving peer - expiration*: uint64 # UNIX UTC expiration time for the reservation +type Voucher* = object + relayPeerId*: PeerId # peer ID of the relay + reservingPeerId*: PeerId # peer ID of the reserving peer + expiration*: uint64 # UNIX UTC expiration time for the reservation proc decode*(_: typedesc[Voucher], buf: seq[byte]): Result[Voucher, ProtoError] = let pb = initProtoBuffer(buf) var v = Voucher() - ? pb.getRequiredField(1, v.relayPeerId) - ? pb.getRequiredField(2, v.reservingPeerId) - ? pb.getRequiredField(3, v.expiration) + ?pb.getRequiredField(1, v.relayPeerId) + ?pb.getRequiredField(2, v.reservingPeerId) + ?pb.getRequiredField(3, v.expiration) ok(v) @@ -137,20 +135,23 @@ proc encode*(v: Voucher): seq[byte] = pb.finish() pb.buffer -proc init*(T: typedesc[Voucher], - relayPeerId: PeerId, - reservingPeerId: PeerId, - expiration: uint64): T = +proc init*( + T: typedesc[Voucher], + relayPeerId: PeerId, + reservingPeerId: PeerId, + expiration: uint64, +): T = T( - relayPeerId = relayPeerId, - reservingPeerId = reservingPeerId, - expiration: expiration + relayPeerId = relayPeerId, reservingPeerId = reservingPeerId, expiration: expiration ) type SignedVoucher* = SignedPayload[Voucher] -proc payloadDomain*(_: typedesc[Voucher]): string = "libp2p-relay-rsvp" -proc payloadType*(_: typedesc[Voucher]): seq[byte] = @[ (byte)0x03, (byte)0x02 ] +proc payloadDomain*(_: typedesc[Voucher]): string = + "libp2p-relay-rsvp" + +proc payloadType*(_: typedesc[Voucher]): seq[byte] = + @[(byte) 0x03, (byte) 0x02] proc checkValid*(spr: SignedVoucher): Result[void, EnvelopeError] = if not spr.data.relayPeerId.match(spr.envelope.publicKey): @@ -164,13 +165,15 @@ type Peer* = object peerId*: PeerId addrs*: seq[MultiAddress] + Reservation* = object - expire*: uint64 # required, Unix expiration time (UTC) - addrs*: seq[MultiAddress] # relay address for reserving peer - svoucher*: Opt[seq[byte]] # optional, reservation voucher + expire*: uint64 # required, Unix expiration time (UTC) + addrs*: seq[MultiAddress] # relay address for reserving peer + svoucher*: Opt[seq[byte]] # optional, reservation voucher + Limit* = object - duration*: uint32 # seconds - data*: uint64 # bytes + duration*: uint32 # seconds + data*: uint64 # bytes StatusV2* = enum Ok = 100 @@ -181,10 +184,12 @@ type NoReservation = 204 MalformedMessage = 400 UnexpectedMessage = 401 + HopMessageType* {.pure.} = enum Reserve = 0 Connect = 1 Status = 2 + HopMessage* = object msgType*: HopMessageType peer*: Opt[Peer] @@ -214,8 +219,10 @@ proc encode*(msg: HopMessage): ProtoBuffer = pb.write(3, rpb.buffer) if msg.limit.duration > 0 or msg.limit.data > 0: var lpb = initProtoBuffer() - if msg.limit.duration > 0: lpb.write(1, msg.limit.duration) - if msg.limit.data > 0: lpb.write(2, msg.limit.data) + if msg.limit.duration > 0: + lpb.write(1, msg.limit.duration) + if msg.limit.data > 0: + lpb.write(2, msg.limit.data) lpb.finish() pb.write(4, lpb.buffer) msg.status.withValue(status): @@ -229,35 +236,35 @@ proc decode*(_: typedesc[HopMessage], buf: seq[byte]): Opt[HopMessage] = let pb = initProtoBuffer(buf) var msgTypeOrd: uint32 - ? pb.getRequiredField(1, msgTypeOrd).toOpt() + ?pb.getRequiredField(1, msgTypeOrd).toOpt() if not checkedEnumAssign(msg.msgType, msgTypeOrd): return Opt.none(HopMessage) var pbPeer: ProtoBuffer - if ? pb.getField(2, pbPeer).toOpt(): + if ?pb.getField(2, pbPeer).toOpt(): var peer: Peer - ? pbPeer.getRequiredField(1, peer.peerId).toOpt() - discard ? pbPeer.getRepeatedField(2, peer.addrs).toOpt() + ?pbPeer.getRequiredField(1, peer.peerId).toOpt() + discard ?pbPeer.getRepeatedField(2, peer.addrs).toOpt() msg.peer = Opt.some(peer) var pbReservation: ProtoBuffer - if ? pb.getField(3, pbReservation).toOpt(): + if ?pb.getField(3, pbReservation).toOpt(): var svoucher: seq[byte] reservation: Reservation - if ? pbReservation.getField(3, svoucher).toOpt(): + if ?pbReservation.getField(3, svoucher).toOpt(): reservation.svoucher = Opt.some(svoucher) - ? pbReservation.getRequiredField(1, reservation.expire).toOpt() - discard ? pbReservation.getRepeatedField(2, reservation.addrs).toOpt() + ?pbReservation.getRequiredField(1, reservation.expire).toOpt() + discard ?pbReservation.getRepeatedField(2, reservation.addrs).toOpt() msg.reservation = Opt.some(reservation) var pbLimit: ProtoBuffer - if ? pb.getField(4, pbLimit).toOpt(): - discard ? pbLimit.getField(1, msg.limit.duration).toOpt() - discard ? pbLimit.getField(2, msg.limit.data).toOpt() + if ?pb.getField(4, pbLimit).toOpt(): + discard ?pbLimit.getField(1, msg.limit.duration).toOpt() + discard ?pbLimit.getField(2, msg.limit.data).toOpt() var statusOrd: uint32 - if ? pb.getField(5, statusOrd).toOpt(): + if ?pb.getField(5, statusOrd).toOpt(): var status: StatusV2 if not checkedEnumAssign(status, statusOrd): return Opt.none(HopMessage) @@ -270,13 +277,13 @@ type StopMessageType* {.pure.} = enum Connect = 0 Status = 1 + StopMessage* = object msgType*: StopMessageType peer*: Opt[Peer] limit*: Limit status*: Opt[StatusV2] - proc encode*(msg: StopMessage): ProtoBuffer = var pb = initProtoBuffer() @@ -290,8 +297,10 @@ proc encode*(msg: StopMessage): ProtoBuffer = pb.write(2, ppb.buffer) if msg.limit.duration > 0 or msg.limit.data > 0: var lpb = initProtoBuffer() - if msg.limit.duration > 0: lpb.write(1, msg.limit.duration) - if msg.limit.data > 0: lpb.write(2, msg.limit.data) + if msg.limit.duration > 0: + lpb.write(1, msg.limit.duration) + if msg.limit.data > 0: + lpb.write(2, msg.limit.data) lpb.finish() pb.write(3, lpb.buffer) msg.status.withValue(status): @@ -306,26 +315,25 @@ proc decode*(_: typedesc[StopMessage], buf: seq[byte]): Opt[StopMessage] = let pb = initProtoBuffer(buf) var msgTypeOrd: uint32 - ? pb.getRequiredField(1, msgTypeOrd).toOpt() + ?pb.getRequiredField(1, msgTypeOrd).toOpt() if msgTypeOrd.int notin StopMessageType: return Opt.none(StopMessage) msg.msgType = StopMessageType(msgTypeOrd) - var pbPeer: ProtoBuffer - if ? pb.getField(2, pbPeer).toOpt(): + if ?pb.getField(2, pbPeer).toOpt(): var peer: Peer - ? pbPeer.getRequiredField(1, peer.peerId).toOpt() - discard ? pbPeer.getRepeatedField(2, peer.addrs).toOpt() + ?pbPeer.getRequiredField(1, peer.peerId).toOpt() + discard ?pbPeer.getRepeatedField(2, peer.addrs).toOpt() msg.peer = Opt.some(peer) var pbLimit: ProtoBuffer - if ? pb.getField(3, pbLimit).toOpt(): - discard ? pbLimit.getField(1, msg.limit.duration).toOpt() - discard ? pbLimit.getField(2, msg.limit.data).toOpt() + if ?pb.getField(3, pbLimit).toOpt(): + discard ?pbLimit.getField(1, msg.limit.duration).toOpt() + discard ?pbLimit.getField(2, msg.limit.data).toOpt() var statusOrd: uint32 - if ? pb.getField(4, statusOrd).toOpt(): + if ?pb.getField(4, statusOrd).toOpt(): var status: StatusV2 if not checkedEnumAssign(status, statusOrd): return Opt.none(StopMessage) diff --git a/libp2p/protocols/connectivity/relay/rconn.nim b/libp2p/protocols/connectivity/relay/rconn.nim index a4699a0773..cb416ea5e1 100644 --- a/libp2p/protocols/connectivity/relay/rconn.nim +++ b/libp2p/protocols/connectivity/relay/rconn.nim @@ -13,24 +13,20 @@ import chronos import ../../../stream/connection -type - RelayConnection* = ref object of Connection - conn*: Connection - limitDuration*: uint32 - limitData*: uint64 - dataSent*: uint64 +type RelayConnection* = ref object of Connection + conn*: Connection + limitDuration*: uint32 + limitData*: uint64 + dataSent*: uint64 method readOnce*( - self: RelayConnection, - pbytes: pointer, - nbytes: int + self: RelayConnection, pbytes: pointer, nbytes: int ): Future[int] {.async: (raises: [CancelledError, LPStreamError], raw: true).} = self.activity = true self.conn.readOnce(pbytes, nbytes) method write*( - self: RelayConnection, - msg: seq[byte] + self: RelayConnection, msg: seq[byte] ): Future[void] {.async: (raises: [CancelledError, LPStreamError]).} = self.dataSent.inc(msg.len) if self.limitData != 0 and self.dataSent > self.limitData: @@ -43,13 +39,15 @@ method closeImpl*(self: RelayConnection): Future[void] {.async: (raises: []).} = await self.conn.close() await procCall Connection(self).closeImpl() -method getWrapped*(self: RelayConnection): Connection = self.conn +method getWrapped*(self: RelayConnection): Connection = + self.conn proc new*( T: typedesc[RelayConnection], conn: Connection, limitDuration: uint32, - limitData: uint64): T = + limitData: uint64, +): T = let rc = T(conn: conn, limitDuration: limitDuration, limitData: limitData) rc.dir = conn.dir rc.initStream() @@ -59,5 +57,6 @@ proc new*( await noCancel conn.join().wait(limitDuration.seconds()) except AsyncTimeoutError: await conn.close() + asyncSpawn checkDurationConnection() return rc diff --git a/libp2p/protocols/connectivity/relay/relay.nim b/libp2p/protocols/connectivity/relay/relay.nim index 6fa9894f8d..a6c76125a2 100644 --- a/libp2p/protocols/connectivity/relay/relay.nim +++ b/libp2p/protocols/connectivity/relay/relay.nim @@ -13,18 +13,19 @@ import sequtils, tables import chronos, chronicles -import ./messages, - ./rconn, - ./utils, - ../../../peerinfo, - ../../../switch, - ../../../multiaddress, - ../../../multicodec, - ../../../stream/connection, - ../../../protocols/protocol, - ../../../errors, - ../../../utils/heartbeat, - ../../../signed_envelope +import + ./messages, + ./rconn, + ./utils, + ../../../peerinfo, + ../../../switch, + ../../../multiaddress, + ../../../multicodec, + ../../../stream/connection, + ../../../protocols/protocol, + ../../../errors, + ../../../utils/heartbeat, + ../../../signed_envelope # TODO: # * Eventually replace std/times by chronos/timer. Currently chronos/timer @@ -54,47 +55,53 @@ type # Relay Side -type - Relay* = ref object of LPProtocol - switch*: Switch - peerCount: CountTable[PeerId] - - # number of reservation (relayv2) + number of connection (relayv1) - maxCircuit*: int - - maxCircuitPerPeer*: int - msgSize*: int - # RelayV1 - isCircuitRelayV1*: bool - streamCount: int - # RelayV2 - rsvp: Table[PeerId, DateTime] - reservationLoop: Future[void] - reservationTTL*: times.Duration - heartbeatSleepTime*: uint32 - limit*: Limit +type Relay* = ref object of LPProtocol + switch*: Switch + peerCount: CountTable[PeerId] + + # number of reservation (relayv2) + number of connection (relayv1) + maxCircuit*: int + + maxCircuitPerPeer*: int + msgSize*: int + # RelayV1 + isCircuitRelayV1*: bool + streamCount: int + # RelayV2 + rsvp: Table[PeerId, DateTime] + reservationLoop: Future[void] + reservationTTL*: times.Duration + heartbeatSleepTime*: uint32 + limit*: Limit # Relay V2 proc createReserveResponse( - r: Relay, - pid: PeerId, - expire: DateTime): Result[HopMessage, CryptoError] = + r: Relay, pid: PeerId, expire: DateTime +): Result[HopMessage, CryptoError] = let expireUnix = expire.toTime.toUnix.uint64 - v = Voucher(relayPeerId: r.switch.peerInfo.peerId, - reservingPeerId: pid, - expiration: expireUnix) - sv = ? SignedVoucher.init(r.switch.peerInfo.privateKey, v) - ma = ? MultiAddress.init("/p2p/" & $r.switch.peerInfo.peerId).orErr(CryptoError.KeyError) - rsrv = Reservation(expire: expireUnix, - addrs: r.switch.peerInfo.addrs.mapIt( - ? it.concat(ma).orErr(CryptoError.KeyError)), - svoucher: Opt.some(? sv.encode)) - msg = HopMessage(msgType: HopMessageType.Status, - reservation: Opt.some(rsrv), - limit: r.limit, - status: Opt.some(Ok)) + v = Voucher( + relayPeerId: r.switch.peerInfo.peerId, + reservingPeerId: pid, + expiration: expireUnix, + ) + sv = ?SignedVoucher.init(r.switch.peerInfo.privateKey, v) + ma = + ?MultiAddress.init("/p2p/" & $r.switch.peerInfo.peerId).orErr( + CryptoError.KeyError + ) + rsrv = Reservation( + expire: expireUnix, + addrs: r.switch.peerInfo.addrs.mapIt(?it.concat(ma).orErr(CryptoError.KeyError)), + svoucher: Opt.some(?sv.encode), + ) + msg = HopMessage( + msgType: HopMessageType.Status, + reservation: Opt.some(rsrv), + limit: r.limit, + status: Opt.some(Ok), + ) return ok(msg) proc isRelayed*(conn: Connection): bool = @@ -126,9 +133,7 @@ proc handleReserve(r: Relay, conn: Connection) {.async.} = r.rsvp[pid] = expire await conn.writeLp(encode(msg).buffer) -proc handleConnect(r: Relay, - connSrc: Connection, - msg: HopMessage) {.async.} = +proc handleConnect(r: Relay, connSrc: Connection, msg: HopMessage) {.async.} = if connSrc.isRelayed(): trace "connection attempt over relay connection" await sendHopStatus(connSrc, PermissionDenied) @@ -150,38 +155,42 @@ proc handleConnect(r: Relay, r.peerCount.inc(src, -1) r.peerCount.inc(dst, -1) - if r.peerCount[src] > r.maxCircuitPerPeer or - r.peerCount[dst] > r.maxCircuitPerPeer: - trace "too many connections", src = r.peerCount[src], - dst = r.peerCount[dst], - max = r.maxCircuitPerPeer + if r.peerCount[src] > r.maxCircuitPerPeer or r.peerCount[dst] > r.maxCircuitPerPeer: + trace "too many connections", + src = r.peerCount[src], dst = r.peerCount[dst], max = r.maxCircuitPerPeer await sendHopStatus(connSrc, ResourceLimitExceeded) return - let connDst = try: - await r.switch.dial(dst, RelayV2StopCodec) - except CancelledError as exc: - raise exc - except CatchableError as exc: - trace "error opening relay stream", dst, exc=exc.msg - await sendHopStatus(connSrc, ConnectionFailed) - return + let connDst = + try: + await r.switch.dial(dst, RelayV2StopCodec) + except CancelledError as exc: + raise exc + except CatchableError as exc: + trace "error opening relay stream", dst, exc = exc.msg + await sendHopStatus(connSrc, ConnectionFailed) + return defer: await connDst.close() proc sendStopMsg() {.async.} = - let stopMsg = StopMessage(msgType: StopMessageType.Connect, - peer: Opt.some(Peer(peerId: src, addrs: @[])), - limit: r.limit) + let stopMsg = StopMessage( + msgType: StopMessageType.Connect, + peer: Opt.some(Peer(peerId: src, addrs: @[])), + limit: r.limit, + ) await connDst.writeLp(encode(stopMsg).buffer) let msg = StopMessage.decode(await connDst.readLp(r.msgSize)).valueOr: raise newException(SendStopError, "Malformed message") if msg.msgType != StopMessageType.Status: - raise newException(SendStopError, "Unexpected stop response, not a status message") + raise + newException(SendStopError, "Unexpected stop response, not a status message") if msg.status.get(UnexpectedMessage) != Ok: raise newException(SendStopError, "Relay stop failure") - await connSrc.writeLp(encode(HopMessage(msgType: HopMessageType.Status, - status: Opt.some(Ok))).buffer) + await connSrc.writeLp( + encode(HopMessage(msgType: HopMessageType.Status, status: Opt.some(Ok))).buffer + ) + try: await sendStopMsg() except CancelledError as exc: @@ -205,20 +214,24 @@ proc handleHopStreamV2*(r: Relay, conn: Connection) {.async.} = await sendHopStatus(conn, MalformedMessage) return trace "relayv2 handle stream", msg = msg - case msg.msgType: - of HopMessageType.Reserve: await r.handleReserve(conn) - of HopMessageType.Connect: await r.handleConnect(conn, msg) + case msg.msgType + of HopMessageType.Reserve: + await r.handleReserve(conn) + of HopMessageType.Connect: + await r.handleConnect(conn, msg) else: - trace "Unexpected relayv2 handshake", msgType=msg.msgType + trace "Unexpected relayv2 handshake", msgType = msg.msgType await sendHopStatus(conn, MalformedMessage) # Relay V1 proc handleHop*(r: Relay, connSrc: Connection, msg: RelayMessage) {.async.} = r.streamCount.inc() - defer: r.streamCount.dec() + defer: + r.streamCount.dec() if r.streamCount + r.rsvp.len() >= r.maxCircuit: - trace "refusing connection; too many active circuit", streamCount = r.streamCount, rsvp = r.rsvp.len() + trace "refusing connection; too many active circuit", + streamCount = r.streamCount, rsvp = r.rsvp.len() await sendStatus(connSrc, StatusV1.HopCantSpeakRelay) return @@ -236,13 +249,14 @@ proc handleHop*(r: Relay, connSrc: Connection, msg: RelayMessage) {.async.} = trace "relay not connected to dst", dst return err(StatusV1.HopNoConnToDst) ok(msg) + let check = checkMsg() if check.isErr: await sendStatus(connSrc, check.error()) return if r.peerCount[src.peerId] >= r.maxCircuitPerPeer or - r.peerCount[dst.peerId] >= r.maxCircuitPerPeer: + r.peerCount[dst.peerId] >= r.maxCircuitPerPeer: trace "refusing connection; too many connection from src or to dst", src, dst await sendStatus(connSrc, StatusV1.HopCantSpeakRelay) return @@ -252,31 +266,32 @@ proc handleHop*(r: Relay, connSrc: Connection, msg: RelayMessage) {.async.} = r.peerCount.inc(src.peerId, -1) r.peerCount.inc(dst.peerId, -1) - let connDst = try: - await r.switch.dial(dst.peerId, RelayV1Codec) - except CancelledError as exc: - raise exc - except CatchableError as exc: - trace "error opening relay stream", dst, exc=exc.msg - await sendStatus(connSrc, StatusV1.HopCantDialDst) - return + let connDst = + try: + await r.switch.dial(dst.peerId, RelayV1Codec) + except CancelledError as exc: + raise exc + except CatchableError as exc: + trace "error opening relay stream", dst, exc = exc.msg + await sendStatus(connSrc, StatusV1.HopCantDialDst) + return defer: await connDst.close() let msgToSend = RelayMessage( - msgType: Opt.some(RelayType.Stop), - srcPeer: Opt.some(src), - dstPeer: Opt.some(dst)) + msgType: Opt.some(RelayType.Stop), srcPeer: Opt.some(src), dstPeer: Opt.some(dst) + ) - let msgRcvFromDstOpt = try: - await connDst.writeLp(encode(msgToSend).buffer) - RelayMessage.decode(await connDst.readLp(r.msgSize)) - except CancelledError as exc: - raise exc - except CatchableError as exc: - trace "error writing stop handshake or reading stop response", exc=exc.msg - await sendStatus(connSrc, StatusV1.HopCantOpenDstStream) - return + let msgRcvFromDstOpt = + try: + await connDst.writeLp(encode(msgToSend).buffer) + RelayMessage.decode(await connDst.readLp(r.msgSize)) + except CancelledError as exc: + raise exc + except CatchableError as exc: + trace "error writing stop handshake or reading stop response", exc = exc.msg + await sendStatus(connSrc, StatusV1.HopCantOpenDstStream) + return let msgRcvFromDst = msgRcvFromDstOpt.valueOr: trace "error reading stop response", msg = msgRcvFromDstOpt @@ -284,7 +299,7 @@ proc handleHop*(r: Relay, connSrc: Connection, msg: RelayMessage) {.async.} = return if msgRcvFromDst.msgType.get(RelayType.Stop) != RelayType.Status or - msgRcvFromDst.status.get(StatusV1.StopRelayRefused) != StatusV1.Success: + msgRcvFromDst.status.get(StatusV1.StopRelayRefused) != StatusV1.Success: trace "unexcepted relay stop response", msgRcvFromDst await sendStatus(connSrc, StatusV1.HopCantOpenDstStream) return @@ -303,44 +318,54 @@ proc handleStreamV1(r: Relay, conn: Connection) {.async.} = trace "Message type not set" await sendStatus(conn, StatusV1.MalformedMessage) return - case typ: - of RelayType.Hop: await r.handleHop(conn, msg) - of RelayType.Stop: await sendStatus(conn, StatusV1.StopRelayRefused) - of RelayType.CanHop: await sendStatus(conn, StatusV1.Success) - else: - trace "Unexpected relay handshake", msgType=msg.msgType - await sendStatus(conn, StatusV1.MalformedMessage) + case typ + of RelayType.Hop: + await r.handleHop(conn, msg) + of RelayType.Stop: + await sendStatus(conn, StatusV1.StopRelayRefused) + of RelayType.CanHop: + await sendStatus(conn, StatusV1.Success) + else: + trace "Unexpected relay handshake", msgType = msg.msgType + await sendStatus(conn, StatusV1.MalformedMessage) proc setup*(r: Relay, switch: Switch) = r.switch = switch r.switch.addPeerEventHandler( - proc (peerId: PeerId, event: PeerEvent) {.async.} = - r.rsvp.del(peerId), - Left) - -proc new*(T: typedesc[Relay], - reservationTTL: times.Duration = DefaultReservationTTL, - limitDuration: uint32 = DefaultLimitDuration, - limitData: uint64 = DefaultLimitData, - heartbeatSleepTime: uint32 = DefaultHeartbeatSleepTime, - maxCircuit: int = MaxCircuit, - maxCircuitPerPeer: int = MaxCircuitPerPeer, - msgSize: int = RelayMsgSize, - circuitRelayV1: bool = false): T = - - let r = T(reservationTTL: reservationTTL, - limit: Limit(duration: limitDuration, data: limitData), - heartbeatSleepTime: heartbeatSleepTime, - maxCircuit: maxCircuit, - maxCircuitPerPeer: maxCircuitPerPeer, - msgSize: msgSize, - isCircuitRelayV1: circuitRelayV1) + proc(peerId: PeerId, event: PeerEvent) {.async.} = + r.rsvp.del(peerId) + , + Left, + ) + +proc new*( + T: typedesc[Relay], + reservationTTL: times.Duration = DefaultReservationTTL, + limitDuration: uint32 = DefaultLimitDuration, + limitData: uint64 = DefaultLimitData, + heartbeatSleepTime: uint32 = DefaultHeartbeatSleepTime, + maxCircuit: int = MaxCircuit, + maxCircuitPerPeer: int = MaxCircuitPerPeer, + msgSize: int = RelayMsgSize, + circuitRelayV1: bool = false, +): T = + let r = T( + reservationTTL: reservationTTL, + limit: Limit(duration: limitDuration, data: limitData), + heartbeatSleepTime: heartbeatSleepTime, + maxCircuit: maxCircuit, + maxCircuitPerPeer: maxCircuitPerPeer, + msgSize: msgSize, + isCircuitRelayV1: circuitRelayV1, + ) proc handleStream(conn: Connection, proto: string) {.async.} = try: - case proto: - of RelayV2HopCodec: await r.handleHopStreamV2(conn) - of RelayV1Codec: await r.handleStreamV1(conn) + case proto + of RelayV2HopCodec: + await r.handleHopStreamV2(conn) + of RelayV1Codec: + await r.handleStreamV1(conn) except CancelledError as exc: raise exc except CatchableError as exc: @@ -349,8 +374,11 @@ proc new*(T: typedesc[Relay], trace "exiting relayv2 handler", conn await conn.close() - r.codecs = if r.isCircuitRelayV1: @[RelayV1Codec] - else: @[RelayV2HopCodec, RelayV1Codec] + r.codecs = + if r.isCircuitRelayV1: + @[RelayV1Codec] + else: + @[RelayV2HopCodec, RelayV1Codec] r.handler = handleStream r @@ -361,9 +389,7 @@ proc deletesReservation(r: Relay) {.async.} = if n > r.rsvp[k]: r.rsvp.del(k) -method start*( - r: Relay -): Future[void] {.async: (raises: [CancelledError], raw: true).} = +method start*(r: Relay): Future[void] {.async: (raises: [CancelledError], raw: true).} = let fut = newFuture[void]() fut.complete() if not r.reservationLoop.isNil: diff --git a/libp2p/protocols/connectivity/relay/rtransport.nim b/libp2p/protocols/connectivity/relay/rtransport.nim index fac56d904b..007d462267 100644 --- a/libp2p/protocols/connectivity/relay/rtransport.nim +++ b/libp2p/protocols/connectivity/relay/rtransport.nim @@ -13,21 +13,21 @@ import sequtils, strutils import chronos, chronicles -import ./client, - ./rconn, - ./utils, - ../../../switch, - ../../../stream/connection, - ../../../transports/transport +import + ./client, + ./rconn, + ./utils, + ../../../switch, + ../../../stream/connection, + ../../../transports/transport logScope: topics = "libp2p relay relay-transport" -type - RelayTransport* = ref object of Transport - client*: RelayClient - queue: AsyncQueue[Connection] - selfRunning: bool +type RelayTransport* = ref object of Transport + client*: RelayClient + queue: AsyncQueue[Connection] + selfRunning: bool method start*(self: RelayTransport, ma: seq[MultiAddress]) {.async.} = if self.selfRunning: @@ -35,11 +35,10 @@ method start*(self: RelayTransport, ma: seq[MultiAddress]) {.async.} = return self.client.onNewConnection = proc( - conn: Connection, - duration: uint32 = 0, - data: uint64 = 0) {.async.} = - await self.queue.addLast(RelayConnection.new(conn, duration, data)) - await conn.join() + conn: Connection, duration: uint32 = 0, data: uint64 = 0 + ) {.async.} = + await self.queue.addLast(RelayConnection.new(conn, duration, data)) + await conn.join() self.selfRunning = true await procCall Transport(self).start(ma) trace "Starting Relay transport" @@ -57,7 +56,7 @@ method accept*(self: RelayTransport): Future[Connection] {.async.} = proc dial*(self: RelayTransport, ma: MultiAddress): Future[Connection] {.async.} = let sma = toSeq(ma.items()) - relayAddrs = sma[0..sma.len-4].mapIt(it.tryGet()).foldl(a & b) + relayAddrs = sma[0 .. sma.len - 4].mapIt(it.tryGet()).foldl(a & b) var relayPeerId: PeerId dstPeerId: PeerId @@ -68,13 +67,12 @@ proc dial*(self: RelayTransport, ma: MultiAddress): Future[Connection] {.async.} trace "Dial", relayPeerId, dstPeerId let conn = await self.client.switch.dial( - relayPeerId, - @[ relayAddrs ], - @[ RelayV2HopCodec, RelayV1Codec ]) + relayPeerId, @[relayAddrs], @[RelayV2HopCodec, RelayV1Codec] + ) conn.dir = Direction.Out var rc: RelayConnection try: - case conn.protocol: + case conn.protocol of RelayV1Codec: return await self.client.dialPeerV1(conn, dstPeerId, @[]) of RelayV2HopCodec: @@ -83,14 +81,16 @@ proc dial*(self: RelayTransport, ma: MultiAddress): Future[Connection] {.async.} except CancelledError as exc: raise exc except CatchableError as exc: - if not rc.isNil: await rc.close() + if not rc.isNil: + await rc.close() raise exc method dial*( - self: RelayTransport, - hostname: string, - ma: MultiAddress, - peerId: Opt[PeerId] = Opt.none(PeerId)): Future[Connection] {.async.} = + self: RelayTransport, + hostname: string, + ma: MultiAddress, + peerId: Opt[PeerId] = Opt.none(PeerId), +): Future[Connection] {.async.} = peerId.withValue(pid): let address = MultiAddress.init($ma & "/p2p/" & $pid).tryGet() result = await self.dial(address) diff --git a/libp2p/protocols/connectivity/relay/utils.nim b/libp2p/protocols/connectivity/relay/utils.nim index 46ba9cf795..1440f73b7e 100644 --- a/libp2p/protocols/connectivity/relay/utils.nim +++ b/libp2p/protocols/connectivity/relay/utils.nim @@ -10,8 +10,7 @@ {.push raises: [].} import chronos, chronicles -import ./messages, - ../../../stream/connection +import ./messages, ../../../stream/connection logScope: topics = "libp2p relay relay-utils" @@ -22,19 +21,16 @@ const RelayV2StopCodec* = "/libp2p/circuit/relay/0.2.0/stop" proc sendStatus*( - conn: Connection, - code: StatusV1 + conn: Connection, code: StatusV1 ) {.async: (raises: [CancelledError, LPStreamError], raw: true).} = trace "send relay/v1 status", status = $code & "(" & $ord(code) & ")" let - msg = RelayMessage( - msgType: Opt.some(RelayType.Status), status: Opt.some(code)) + msg = RelayMessage(msgType: Opt.some(RelayType.Status), status: Opt.some(code)) pb = encode(msg) conn.writeLp(pb.buffer) proc sendHopStatus*( - conn: Connection, - code: StatusV2 + conn: Connection, code: StatusV2 ) {.async: (raises: [CancelledError, LPStreamError], raw: true).} = trace "send hop relay/v2 status", status = $code & "(" & $ord(code) & ")" let @@ -43,8 +39,7 @@ proc sendHopStatus*( conn.writeLp(pb.buffer) proc sendStopStatus*( - conn: Connection, - code: StatusV2 + conn: Connection, code: StatusV2 ) {.async: (raises: [CancelledError, LPStreamError], raw: true).} = trace "send stop relay/v2 status", status = $code & " (" & $ord(code) & ")" let @@ -53,8 +48,8 @@ proc sendStopStatus*( conn.writeLp(pb.buffer) proc bridge*( - connSrc: Connection, - connDst: Connection) {.async: (raises: [CancelledError]).} = + connSrc: Connection, connDst: Connection +) {.async: (raises: [CancelledError]).} = const bufferSize = 4096 var bufSrcToDst: array[bufferSize, byte] @@ -67,9 +62,10 @@ proc bridge*( try: while not connSrc.closed() and not connDst.closed(): - try: # https://github.com/status-im/nim-chronos/issues/516 + try: # https://github.com/status-im/nim-chronos/issues/516 discard await race(futSrc, futDst) - except ValueError: raiseAssert("Futures list is not empty") + except ValueError: + raiseAssert("Futures list is not empty") if futSrc.finished(): bufRead = await futSrc if bufRead > 0: @@ -91,7 +87,7 @@ proc bridge*( trace "relay src closed connection", src = connSrc.peerId if connDst.closed() or connDst.atEof(): trace "relay dst closed connection", dst = connDst.peerId - trace "relay error", exc=exc.msg + trace "relay error", exc = exc.msg trace "end relaying", bytesSentFromSrcToDst, bytesSentFromDstToSrc await futSrc.cancelAndWait() await futDst.cancelAndWait() diff --git a/libp2p/protocols/identify.nim b/libp2p/protocols/identify.nim index 3a1f6f925c..db7c4d38e5 100644 --- a/libp2p/protocols/identify.nim +++ b/libp2p/protocols/identify.nim @@ -15,17 +15,18 @@ import std/[sequtils, options, strutils, sugar] import stew/results import chronos, chronicles -import ../protobuf/minprotobuf, - ../peerinfo, - ../stream/connection, - ../peerid, - ../crypto/crypto, - ../multiaddress, - ../multicodec, - ../protocols/protocol, - ../utility, - ../errors, - ../observedaddrmanager +import + ../protobuf/minprotobuf, + ../peerinfo, + ../stream/connection, + ../peerid, + ../crypto/crypto, + ../multiaddress, + ../multicodec, + ../protocols/protocol, + ../utility, + ../errors, + ../observedaddrmanager export observedaddrmanager @@ -59,11 +60,9 @@ type sendSignedPeerRecord*: bool observedAddrManager*: ObservedAddrManager - IdentifyPushHandler* = proc ( - peer: PeerId, - newInfo: IdentifyInfo): - Future[void] - {.gcsafe, raises: [], public.} + IdentifyPushHandler* = proc(peer: PeerId, newInfo: IdentifyInfo): Future[void] {. + gcsafe, raises: [], public + .} IdentifyPush* = ref object of LPProtocol identifyHandler: IdentifyPushHandler @@ -78,11 +77,11 @@ chronicles.expandIt(IdentifyInfo): signedPeerRecord = # The SPR contains the same data as the identify message # would be cumbersome to log - if it.signedPeerRecord.isSome(): "Some" - else: "None" + if it.signedPeerRecord.isSome(): "Some" else: "None" -proc encodeMsg(peerInfo: PeerInfo, observedAddr: Opt[MultiAddress], sendSpr: bool): ProtoBuffer - {.raises: [].} = +proc encodeMsg( + peerInfo: PeerInfo, observedAddr: Opt[MultiAddress], sendSpr: bool +): ProtoBuffer {.raises: [].} = result = initProtoBuffer() let pkey = peerInfo.publicKey @@ -96,10 +95,8 @@ proc encodeMsg(peerInfo: PeerInfo, observedAddr: Opt[MultiAddress], sendSpr: boo result.write(4, observed.data.buffer) let protoVersion = ProtoVersion result.write(5, protoVersion) - let agentVersion = if peerInfo.agentVersion.len <= 0: - AgentVersion - else: - peerInfo.agentVersion + let agentVersion = + if peerInfo.agentVersion.len <= 0: AgentVersion else: peerInfo.agentVersion result.write(6, agentVersion) ## Optionally populate signedPeerRecord field. @@ -120,28 +117,28 @@ proc decodeMsg*(buf: seq[byte]): Opt[IdentifyInfo] = signedPeerRecord: SignedPeerRecord var pb = initProtoBuffer(buf) - if ? pb.getField(1, pubkey).toOpt(): + if ?pb.getField(1, pubkey).toOpt(): iinfo.pubkey = some(pubkey) - if ? pb.getField(8, signedPeerRecord).toOpt() and - pubkey == signedPeerRecord.envelope.publicKey: + if ?pb.getField(8, signedPeerRecord).toOpt() and + pubkey == signedPeerRecord.envelope.publicKey: iinfo.signedPeerRecord = some(signedPeerRecord.envelope) - discard ? pb.getRepeatedField(2, iinfo.addrs).toOpt() - discard ? pb.getRepeatedField(3, iinfo.protos).toOpt() - if ? pb.getField(4, oaddr).toOpt(): + discard ?pb.getRepeatedField(2, iinfo.addrs).toOpt() + discard ?pb.getRepeatedField(3, iinfo.protos).toOpt() + if ?pb.getField(4, oaddr).toOpt(): iinfo.observedAddr = some(oaddr) - if ? pb.getField(5, protoVersion).toOpt(): + if ?pb.getField(5, protoVersion).toOpt(): iinfo.protoVersion = some(protoVersion) - if ? pb.getField(6, agentVersion).toOpt(): + if ?pb.getField(6, agentVersion).toOpt(): iinfo.agentVersion = some(agentVersion) Opt.some(iinfo) proc new*( - T: typedesc[Identify], - peerInfo: PeerInfo, - sendSignedPeerRecord = false, - observedAddrManager = ObservedAddrManager.new(), - ): T = + T: typedesc[Identify], + peerInfo: PeerInfo, + sendSignedPeerRecord = false, + observedAddrManager = ObservedAddrManager.new(), +): T = let identify = T( peerInfo: peerInfo, sendSignedPeerRecord: sendSignedPeerRecord, @@ -167,20 +164,23 @@ method init*(p: Identify) = p.handler = handle p.codec = IdentifyCodec -proc identify*(self: Identify, - conn: Connection, - remotePeerId: PeerId): Future[IdentifyInfo] {.async.} = +proc identify*( + self: Identify, conn: Connection, remotePeerId: PeerId +): Future[IdentifyInfo] {.async.} = trace "initiating identify", conn - var message = await conn.readLp(64*1024) + var message = await conn.readLp(64 * 1024) if len(message) == 0: trace "identify: Empty message received!", conn raise newException(IdentityInvalidMsgError, "Empty message received!") - var info = decodeMsg(message).valueOr: raise newException(IdentityInvalidMsgError, "Incorrect message received!") + var info = decodeMsg(message).valueOr: + raise newException(IdentityInvalidMsgError, "Incorrect message received!") debug "identify: decoded message", conn, info let - pubkey = info.pubkey.valueOr: raise newException(IdentityInvalidMsgError, "No pubkey in identify") - peer = PeerId.init(pubkey).valueOr: raise newException(IdentityInvalidMsgError, $error) + pubkey = info.pubkey.valueOr: + raise newException(IdentityInvalidMsgError, "No pubkey in identify") + peer = PeerId.init(pubkey).valueOr: + raise newException(IdentityInvalidMsgError, $error) if peer != remotePeerId: trace "Peer ids don't match", remote = peer, local = remotePeerId @@ -190,7 +190,8 @@ proc identify*(self: Identify, info.observedAddr.withValue(observed): # Currently, we use the ObservedAddrManager only to find our dialable external NAT address. Therefore, addresses # like "...\p2p-circuit\p2p\..." and "\p2p\..." are not useful to us. - if observed.contains(multiCodec("p2p-circuit")).get(false) or P2PPattern.matchPartial(observed): + if observed.contains(multiCodec("p2p-circuit")).get(false) or + P2PPattern.matchPartial(observed): trace "Not adding address to ObservedAddrManager.", observed elif not self.observedAddrManager.addObservation(observed): trace "Observed address is not valid.", observedAddr = observed @@ -207,7 +208,7 @@ proc init*(p: IdentifyPush) = proc handle(conn: Connection, proto: string) {.async.} = trace "handling identify push", conn try: - var message = await conn.readLp(64*1024) + var message = await conn.readLp(64 * 1024) var identInfo = decodeMsg(message).valueOr: raise newException(IdentityInvalidMsgError, "Incorrect message received!") diff --git a/libp2p/protocols/perf/client.nim b/libp2p/protocols/perf/client.nim index 467aac4303..dc34220089 100644 --- a/libp2p/protocols/perf/client.nim +++ b/libp2p/protocols/perf/client.nim @@ -18,9 +18,12 @@ logScope: type PerfClient* = ref object of RootObj -proc perf*(_: typedesc[PerfClient], conn: Connection, - sizeToWrite: uint64 = 0, sizeToRead: uint64 = 0): - Future[Duration] {.async, public.} = +proc perf*( + _: typedesc[PerfClient], + conn: Connection, + sizeToWrite: uint64 = 0, + sizeToRead: uint64 = 0, +): Future[Duration] {.async, public.} = var size = sizeToWrite buf: array[PerfSize, byte] @@ -30,7 +33,7 @@ proc perf*(_: typedesc[PerfClient], conn: Connection, await conn.write(toSeq(toBytesBE(sizeToRead))) while size > 0: let toWrite = min(size, PerfSize) - await conn.write(buf[0.. 0: let toWrite = min(size, PerfSize) - await conn.write(buf[0..= parameters.dLow) or - (parameters.dOut > (parameters.d div 2)): - err("gossipsub: dOut parameter error, Number of outbound connections to keep in the mesh. Must be less than D_lo and at most D/2") + if (parameters.dOut >= parameters.dLow) or (parameters.dOut > (parameters.d div 2)): + err( + "gossipsub: dOut parameter error, Number of outbound connections to keep in the mesh. Must be less than D_lo and at most D/2" + ) elif parameters.gossipThreshold >= 0: err("gossipsub: gossipThreshold parameter error, Must be < 0") elif parameters.unsubscribeBackoff.seconds <= 0: @@ -163,17 +181,29 @@ proc validateParameters*(parameters: TopicParams): Result[void, cstring] = elif parameters.timeInMeshCap <= 0.0: err("gossipsub: timeInMeshCap parameter error, Should be a positive value") elif parameters.firstMessageDeliveriesWeight <= 0.0: - err("gossipsub: firstMessageDeliveriesWeight parameter error, Should be a positive value") + err( + "gossipsub: firstMessageDeliveriesWeight parameter error, Should be a positive value" + ) elif parameters.meshMessageDeliveriesWeight >= 0.0: - err("gossipsub: meshMessageDeliveriesWeight parameter error, Should be a negative value") + err( + "gossipsub: meshMessageDeliveriesWeight parameter error, Should be a negative value" + ) elif parameters.meshMessageDeliveriesThreshold <= 0.0: - err("gossipsub: meshMessageDeliveriesThreshold parameter error, Should be a positive value") + err( + "gossipsub: meshMessageDeliveriesThreshold parameter error, Should be a positive value" + ) elif parameters.meshMessageDeliveriesCap < parameters.meshMessageDeliveriesThreshold: - err("gossipsub: meshMessageDeliveriesCap parameter error, Should be >= meshMessageDeliveriesThreshold") + err( + "gossipsub: meshMessageDeliveriesCap parameter error, Should be >= meshMessageDeliveriesThreshold" + ) elif parameters.meshFailurePenaltyWeight >= 0.0: - err("gossipsub: meshFailurePenaltyWeight parameter error, Should be a negative value") + err( + "gossipsub: meshFailurePenaltyWeight parameter error, Should be a negative value" + ) elif parameters.invalidMessageDeliveriesWeight >= 0.0: - err("gossipsub: invalidMessageDeliveriesWeight parameter error, Should be a negative value") + err( + "gossipsub: invalidMessageDeliveriesWeight parameter error, Should be a negative value" + ) else: ok() @@ -197,7 +227,7 @@ method init*(g: GossipSub) = g.codecs &= GossipSubCodec_10 method onNewPeer*(g: GossipSub, peer: PubSubPeer) = - g.withPeerStats(peer.peerId) do (stats: var PeerStats): + g.withPeerStats(peer.peerId) do(stats: var PeerStats): # Make sure stats and peer information match, even when reloading peer stats # from a previous connection peer.score = stats.score @@ -210,7 +240,9 @@ method onNewPeer*(g: GossipSub, peer: PubSubPeer) = peer.iHaveBudget = IHavePeerBudget peer.pingBudget = PingsPeerBudget -method onPubSubPeerEvent*(p: GossipSub, peer: PubSubPeer, event: PubSubPeerEvent) {.gcsafe.} = +method onPubSubPeerEvent*( + p: GossipSub, peer: PubSubPeer, event: PubSubPeerEvent +) {.gcsafe.} = case event.kind of PubSubPeerEventKind.StreamOpened: discard @@ -266,10 +298,7 @@ method unsubscribePeer*(g: GossipSub, peer: PeerId) = procCall FloodSub(g).unsubscribePeer(peer) -proc handleSubscribe(g: GossipSub, - peer: PubSubPeer, - topic: string, - subscribe: bool) = +proc handleSubscribe(g: GossipSub, peer: PubSubPeer, topic: string, subscribe: bool) = logScope: peer topic @@ -283,7 +312,7 @@ proc handleSubscribe(g: GossipSub, trace "ignoring unknown peer" return - if not(isNil(g.subscriptionValidator)) and not(g.subscriptionValidator(topic)): + if not (isNil(g.subscriptionValidator)) and not (g.subscriptionValidator(topic)): # this is a violation, so warn should be in order trace "ignoring invalid topic subscription", topic, peer libp2p_gossipsub_invalid_topic_subscription.inc() @@ -328,7 +357,6 @@ proc handleControl(g: GossipSub, peer: PubSubPeer, control: ControlMessage) = isIWantNotEmpty = respControl.iwant.len > 0 if isPruneNotEmpty or isIWantNotEmpty: - if isIWantNotEmpty: libp2p_pubsub_broadcast_iwant.inc(respControl.iwant.len.int64) @@ -340,9 +368,7 @@ proc handleControl(g: GossipSub, peer: PubSubPeer, control: ControlMessage) = libp2p_pubsub_broadcast_prune.inc(labelValues = ["generic"]) trace "sending control message", msg = shortLog(respControl), peer - g.send( - peer, - RPCMsg(control: some(respControl)), isHighPriority = true) + g.send(peer, RPCMsg(control: some(respControl)), isHighPriority = true) if messages.len > 0: for smsg in messages: @@ -354,21 +380,22 @@ proc handleControl(g: GossipSub, peer: PubSubPeer, control: ControlMessage) = # iwant replies have lower priority trace "sending iwant reply messages", peer - g.send( - peer, - RPCMsg(messages: messages), isHighPriority = false) - -proc validateAndRelay(g: GossipSub, - msg: Message, - msgId: MessageId, saltedId: SaltedId, - peer: PubSubPeer) {.async.} = + g.send(peer, RPCMsg(messages: messages), isHighPriority = false) + +proc validateAndRelay( + g: GossipSub, msg: Message, msgId: MessageId, saltedId: SaltedId, peer: PubSubPeer +) {.async.} = try: - template topic: string = msg.topic + template topic(): string = + msg.topic proc addToSendPeers(toSendPeers: var HashSet[PubSubPeer]) = - g.floodsub.withValue(topic, peers): toSendPeers.incl(peers[]) - g.mesh.withValue(topic, peers): toSendPeers.incl(peers[]) - g.subscribedDirectPeers.withValue(topic, peers): toSendPeers.incl(peers[]) + g.floodsub.withValue(topic, peers): + toSendPeers.incl(peers[]) + g.mesh.withValue(topic, peers): + toSendPeers.incl(peers[]) + g.subscribedDirectPeers.withValue(topic, peers): + toSendPeers.incl(peers[]) toSendPeers.excl(peer) if msg.data.len > max(512, msgId.len * 10): @@ -383,16 +410,23 @@ proc validateAndRelay(g: GossipSub, # small). var toSendPeers = HashSet[PubSubPeer]() addToSendPeers(toSendPeers) - g.broadcast(toSendPeers, RPCMsg(control: some(ControlMessage( - idontwant: @[ControlIWant(messageIDs: @[msgId])] - ))), isHighPriority = true) + g.broadcast( + toSendPeers, + RPCMsg( + control: + some(ControlMessage(idontwant: @[ControlIWant(messageIDs: @[msgId])])) + ), + isHighPriority = true, + ) let validation = await g.validate(msg) var seenPeers: HashSet[PubSubPeer] discard g.validationSeen.pop(saltedId, seenPeers) libp2p_gossipsub_duplicate_during_validation.inc(seenPeers.len.int64) - libp2p_gossipsub_saved_bytes.inc((msg.data.len * seenPeers.len).int64, labelValues = ["validation_duplicate"]) + libp2p_gossipsub_saved_bytes.inc( + (msg.data.len * seenPeers.len).int64, labelValues = ["validation_duplicate"] + ) case validation of ValidationResult.Reject: @@ -428,9 +462,12 @@ proc validateAndRelay(g: GossipSub, if saltedId in heDontWant: peersWhoSentIdontwant.incl(peer) libp2p_gossipsub_idontwant_saved_messages.inc - libp2p_gossipsub_saved_bytes.inc(msg.data.len.int64, labelValues = ["idontwant"]) + libp2p_gossipsub_saved_bytes.inc( + msg.data.len.int64, labelValues = ["idontwant"] + ) break - toSendPeers.excl(peersWhoSentIdontwant) # avoids len(s) == length` the length of the HashSet changed while iterating over it [AssertionDefect] + toSendPeers.excl(peersWhoSentIdontwant) + # avoids len(s) == length` the length of the HashSet changed while iterating over it [AssertionDefect] # In theory, if topics are the same in all messages, we could batch - we'd # also have to be careful to only include validated messages @@ -438,13 +475,17 @@ proc validateAndRelay(g: GossipSub, trace "forwarded message to peers", peers = toSendPeers.len, msgId, peer if g.knownTopics.contains(topic): - libp2p_pubsub_messages_rebroadcasted.inc(toSendPeers.len.int64, labelValues = [topic]) + libp2p_pubsub_messages_rebroadcasted.inc( + toSendPeers.len.int64, labelValues = [topic] + ) else: - libp2p_pubsub_messages_rebroadcasted.inc(toSendPeers.len.int64, labelValues = ["generic"]) + libp2p_pubsub_messages_rebroadcasted.inc( + toSendPeers.len.int64, labelValues = ["generic"] + ) await handleData(g, topic, msg.data) except CatchableError as exc: - info "validateAndRelay failed", msg=exc.msg + info "validateAndRelay failed", msg = exc.msg proc dataAndTopicsIdSize(msgs: seq[Message]): int = msgs.mapIt(it.data.len + it.topic.len).foldl(a + b, 0) @@ -459,7 +500,7 @@ proc messageOverhead(g: GossipSub, msg: RPCMsg, msgSize: int): int = dataAndTopicsIdSize(msg.messages) controlSize = msg.control.withValue(control): byteSize(control.ihave) + byteSize(control.iwant) - do: # no control message + do: 0 msgSize - payloadSize - controlSize @@ -467,15 +508,17 @@ proc messageOverhead(g: GossipSub, msg: RPCMsg, msgSize: int): int = proc rateLimit*(g: GossipSub, peer: PubSubPeer, overhead: int) {.async.} = peer.overheadRateLimitOpt.withValue(overheadRateLimit): if not overheadRateLimit.tryConsume(overhead): - libp2p_gossipsub_peers_rate_limit_hits.inc(labelValues = [peer.getAgent()]) # let's just measure at the beginning for test purposes. - debug "Peer sent too much useless application data and it's above rate limit.", peer, overhead + libp2p_gossipsub_peers_rate_limit_hits.inc(labelValues = [peer.getAgent()]) + # let's just measure at the beginning for test purposes. + debug "Peer sent too much useless application data and it's above rate limit.", + peer, overhead if g.parameters.disconnectPeerAboveRateLimit: await g.disconnectPeer(peer) - raise newException(PeerRateLimitError, "Peer disconnected because it's above rate limit.") + raise newException( + PeerRateLimitError, "Peer disconnected because it's above rate limit." + ) -method rpcHandler*(g: GossipSub, - peer: PubSubPeer, - data: seq[byte]) {.async.} = +method rpcHandler*(g: GossipSub, peer: PubSubPeer, data: seq[byte]) {.async.} = let msgSize = data.len var rpcMsg = decodeRpcMsg(data).valueOr: debug "failed to decode msg from peer", peer, err = error @@ -497,25 +540,29 @@ method rpcHandler*(g: GossipSub, # trigger hooks - these may modify the message peer.recvObservers(rpcMsg) - if rpcMsg.ping.len in 1..<64 and peer.pingBudget > 0: + if rpcMsg.ping.len in 1 ..< 64 and peer.pingBudget > 0: g.send(peer, RPCMsg(pong: rpcMsg.ping), isHighPriority = true) peer.pingBudget.dec - for i in 0.. g.topicsHigh: - debug "received an rpc message with an oversized amount of subscriptions", peer, - size = rpcMsg.subscriptions.len, - limit = g.topicsHigh + debug "received an rpc message with an oversized amount of subscriptions", + peer, size = rpcMsg.subscriptions.len, limit = g.topicsHigh peer.behaviourPenalty += 0.1 - for i in 0.. 0 or g.verifySignature) and not msg.verify(): @@ -581,7 +629,7 @@ method rpcHandler*(g: GossipSub, g.handleControl(peer, rpcMsg.control.unsafeGet()) # Now, check subscription to update the meshes if required - for i in 0.. 0: let - bandwidth = (g.parameters.bandwidthEstimatebps) div 8 div 1000 # Divisions are to convert it to Bytes per ms TODO replace with bandwidth estimate + bandwidth = (g.parameters.bandwidthEstimatebps) div 8 div 1000 + # Divisions are to convert it to Bytes per ms TODO replace with bandwidth estimate msToTransmit = max(data.len div bandwidth, 1) - max(g.parameters.heartbeatInterval.milliseconds div msToTransmit, g.parameters.dLow) + max( + g.parameters.heartbeatInterval.milliseconds div msToTransmit, + g.parameters.dLow, + ) else: int.high() # unlimited for peer in g.gossipsub.getOrDefault(topic): - if peers.len >= maxPeersToFlood: break + if peers.len >= maxPeersToFlood: + break if peer.score >= g.parameters.publishThreshold: trace "publish: including flood/high score peer", peer peers.incl(peer) - elif peers.len < g.parameters.dLow: # not subscribed or bad mesh, send to fanout peers # when flood-publishing, fanout won't help since all potential peers have @@ -675,7 +734,8 @@ method publish*(g: GossipSub, for fanPeer in fanoutPeers: peers.incl(fanPeer) - if peers.len > g.parameters.d: break + if peers.len > g.parameters.d: + break # Attempting to publish counts as fanout send (even if the message # ultimately is not sent) @@ -683,9 +743,10 @@ method publish*(g: GossipSub, if peers.len == 0: let topicPeers = g.gossipsub.getOrDefault(topic).toSeq() - debug "No peers for topic, skipping publish", peersOnTopic = topicPeers.len, - connectedPeers = topicPeers.filterIt(it.connected).len, - topic + debug "No peers for topic, skipping publish", + peersOnTopic = topicPeers.len, + connectedPeers = topicPeers.filterIt(it.connected).len, + topic libp2p_gossipsub_failed_publish.inc() return 0 @@ -697,12 +758,12 @@ method publish*(g: GossipSub, inc g.msgSeqno Message.init(some(g.peerInfo), data, topic, some(g.msgSeqno), g.sign) msgId = g.msgIdProvider(msg).valueOr: - trace "Error generating message id, skipping publish", - error = error + trace "Error generating message id, skipping publish", error = error libp2p_gossipsub_failed_publish.inc() return 0 - logScope: msgId = shortLog(msgId) + logScope: + msgId = shortLog(msgId) trace "Created new message", msg = shortLog(msg), peers = peers.len @@ -722,7 +783,7 @@ method publish*(g: GossipSub, else: libp2p_pubsub_messages_published.inc(peers.len.int64, labelValues = ["generic"]) - trace "Published message to peers", peers=peers.len + trace "Published message to peers", peers = peers.len return peers.len proc maintainDirectPeer(g: GossipSub, id: PeerId, addrs: seq[MultiAddress]) {.async.} = @@ -785,8 +846,7 @@ method stop*(g: GossipSub): Future[void] {.async: (raises: [], raw: true).} = g.heartbeatFut = nil fut -method initPubSub*(g: GossipSub) - {.raises: [InitializationError].} = +method initPubSub*(g: GossipSub) {.raises: [InitializationError].} = procCall FloodSub(g).initPubSub() if not g.parameters.explicit: @@ -802,13 +862,10 @@ method initPubSub*(g: GossipSub) # init gossip stuff g.mcache = MCache.init(g.parameters.historyGossip, g.parameters.historyLength) -method getOrCreatePeer*( - g: GossipSub, - peerId: PeerId, - protos: seq[string]): PubSubPeer = - +method getOrCreatePeer*(g: GossipSub, peerId: PeerId, protos: seq[string]): PubSubPeer = let peer = procCall PubSub(g).getOrCreatePeer(peerId, protos) g.parameters.overheadRateLimit.withValue(overheadRateLimit): - peer.overheadRateLimitOpt = Opt.some(TokenBucket.new(overheadRateLimit.bytes, overheadRateLimit.interval)) + peer.overheadRateLimitOpt = + Opt.some(TokenBucket.new(overheadRateLimit.bytes, overheadRateLimit.interval)) peer.maxNumElementsInNonPriorityQueue = g.parameters.maxNumElementsInNonPriorityQueue return peer diff --git a/libp2p/protocols/pubsub/gossipsub/behavior.nim b/libp2p/protocols/pubsub/gossipsub/behavior.nim index 9b82d33dae..889b73ac38 100644 --- a/libp2p/protocols/pubsub/gossipsub/behavior.nim +++ b/libp2p/protocols/pubsub/gossipsub/behavior.nim @@ -14,24 +14,55 @@ import chronos, chronicles, metrics import "."/[types, scoring] import ".."/[pubsubpeer, peertable, mcache, floodsub, pubsub] import "../rpc"/[messages] -import "../../.."/[peerid, multiaddress, utility, switch, routing_record, signed_envelope, utils/heartbeat] +import + "../../.."/[ + peerid, + multiaddress, + utility, + switch, + routing_record, + signed_envelope, + utils/heartbeat, + ] logScope: topics = "libp2p gossipsub" declareGauge(libp2p_gossipsub_cache_window_size, "the number of messages in the cache") -declareGauge(libp2p_gossipsub_peers_per_topic_mesh, "gossipsub peers per topic in mesh", labels = ["topic"]) -declareGauge(libp2p_gossipsub_peers_per_topic_fanout, "gossipsub peers per topic in fanout", labels = ["topic"]) -declareGauge(libp2p_gossipsub_peers_per_topic_gossipsub, "gossipsub peers per topic in gossipsub", labels = ["topic"]) +declareGauge( + libp2p_gossipsub_peers_per_topic_mesh, + "gossipsub peers per topic in mesh", + labels = ["topic"], +) +declareGauge( + libp2p_gossipsub_peers_per_topic_fanout, + "gossipsub peers per topic in fanout", + labels = ["topic"], +) +declareGauge( + libp2p_gossipsub_peers_per_topic_gossipsub, + "gossipsub peers per topic in gossipsub", + labels = ["topic"], +) declareGauge(libp2p_gossipsub_under_dout_topics, "number of topics below dout") declareGauge(libp2p_gossipsub_no_peers_topics, "number of topics in mesh with no peers") -declareGauge(libp2p_gossipsub_low_peers_topics, "number of topics in mesh with at least one but below dlow peers") -declareGauge(libp2p_gossipsub_healthy_peers_topics, "number of topics in mesh with at least dlow peers (but below dhigh)") -declareCounter(libp2p_gossipsub_above_dhigh_condition, "number of above dhigh pruning branches ran", labels = ["topic"]) +declareGauge( + libp2p_gossipsub_low_peers_topics, + "number of topics in mesh with at least one but below dlow peers", +) +declareGauge( + libp2p_gossipsub_healthy_peers_topics, + "number of topics in mesh with at least dlow peers (but below dhigh)", +) +declareCounter( + libp2p_gossipsub_above_dhigh_condition, + "number of above dhigh pruning branches ran", + labels = ["topic"], +) declareGauge(libp2p_gossipsub_received_iwants, "received iwants", labels = ["kind"]) proc grafted*(g: GossipSub, p: PubSubPeer, topic: string) = - g.withPeerStats(p.peerId) do (stats: var PeerStats): + g.withPeerStats(p.peerId) do(stats: var PeerStats): var info = stats.topicInfos.getOrDefault(topic) info.graftTime = Moment.now() info.meshTime = 0.seconds @@ -40,40 +71,40 @@ proc grafted*(g: GossipSub, p: PubSubPeer, topic: string) = stats.topicInfos[topic] = info - trace "grafted", peer=p, topic + trace "grafted", peer = p, topic -proc pruned*(g: GossipSub, - p: PubSubPeer, - topic: string, - setBackoff: bool = true, - backoff = none(Duration)) = +proc pruned*( + g: GossipSub, + p: PubSubPeer, + topic: string, + setBackoff: bool = true, + backoff = none(Duration), +) = if setBackoff: let backoffDuration = backoff.get(g.parameters.pruneBackoff) backoffMoment = Moment.fromNow(backoffDuration) - g.backingOff - .mgetOrPut(topic, initTable[PeerId, Moment]())[p.peerId] = backoffMoment + g.backingOff.mgetOrPut(topic, initTable[PeerId, Moment]())[p.peerId] = backoffMoment g.peerStats.withValue(p.peerId, stats): stats.topicInfos.withValue(topic, info): g.topicParams.withValue(topic, topicParams): # penalize a peer that delivered no message let threshold = topicParams[].meshMessageDeliveriesThreshold - if info[].inMesh and - info[].meshMessageDeliveriesActive and + if info[].inMesh and info[].meshMessageDeliveriesActive and info[].meshMessageDeliveries < threshold: let deficit = threshold - info.meshMessageDeliveries info[].meshFailurePenalty += deficit * deficit info.inMesh = false - trace "pruned", peer=p, topic + trace "pruned", peer = p, topic proc handleBackingOff*(t: var BackoffTable, topic: string) = let now = Moment.now() var expired = toSeq(t.getOrDefault(topic).pairs()) - expired.keepIf do (pair: tuple[peer: PeerId, expire: Moment]) -> bool: + expired.keepIf do(pair: tuple[peer: PeerId, expire: Moment]) -> bool: now >= pair.expire for (peer, _) in expired: t.withValue(topic, v): @@ -83,12 +114,12 @@ proc peerExchangeList*(g: GossipSub, topic: string): seq[PeerInfoMsg] = if not g.parameters.enablePX: return @[] var peers = g.gossipsub.getOrDefault(topic, initHashSet[PubSubPeer]()).toSeq() - peers.keepIf do (x: PubSubPeer) -> bool: - x.score >= 0.0 + peers.keepIf do(x: PubSubPeer) -> bool: + x.score >= 0.0 # by spec, larger then Dhi, but let's put some hard caps peers.setLen(min(peers.len, g.parameters.dHigh * 2)) let sprBook = g.switch.peerStore[SPRBook] - peers.map do (x: PubSubPeer) -> PeerInfoMsg: + peers.map do(x: PubSubPeer) -> PeerInfoMsg: PeerInfoMsg( peerId: x.peerId, signedPeerRecord: @@ -96,11 +127,12 @@ proc peerExchangeList*(g: GossipSub, topic: string): seq[PeerInfoMsg] = sprBook[x.peerId].encode().get(default(seq[byte])) else: default(seq[byte]) - ) + , + ) -proc handleGraft*(g: GossipSub, - peer: PubSubPeer, - grafts: seq[ControlGraft]): seq[ControlPrune] = +proc handleGraft*( + g: GossipSub, peer: PubSubPeer, grafts: seq[ControlGraft] +): seq[ControlPrune] = var prunes: seq[ControlPrune] for graft in grafts: let topic = graft.topicID @@ -113,14 +145,18 @@ proc handleGraft*(g: GossipSub, warn "a direct peer attempted to graft us, peering agreements should be reciprocal", peer, topic # and such an attempt should be logged and rejected with a PRUNE - prunes.add(ControlPrune( - topicID: topic, - peers: @[], # omitting heavy computation here as the remote did something illegal - backoff: g.parameters.pruneBackoff.seconds.uint64)) + prunes.add( + ControlPrune( + topicID: topic, + peers: @[], + # omitting heavy computation here as the remote did something illegal + backoff: g.parameters.pruneBackoff.seconds.uint64, + ) + ) let backoff = Moment.fromNow(g.parameters.pruneBackoff) - g.backingOff - .mgetOrPut(topic, initTable[PeerId, Moment]())[peer.peerId] = backoff + + g.backingOff.mgetOrPut(topic, initTable[PeerId, Moment]())[peer.peerId] = backoff peer.behaviourPenalty += 0.1 @@ -134,19 +170,22 @@ proc handleGraft*(g: GossipSub, # Ignore BackoffSlackTime here, since this only for outbound activity # and subtract a second time to avoid race conditions # (peers may wait to graft us as the exact instant they're allowed to) - if g.backingOff - .getOrDefault(topic) - .getOrDefault(peer.peerId) - (BackoffSlackTime * 2).seconds > Moment.now(): + if g.backingOff.getOrDefault(topic).getOrDefault(peer.peerId) - + (BackoffSlackTime * 2).seconds > Moment.now(): debug "a backingOff peer attempted to graft us", peer, topic # and such an attempt should be logged and rejected with a PRUNE - prunes.add(ControlPrune( - topicID: topic, - peers: @[], # omitting heavy computation here as the remote did something illegal - backoff: g.parameters.pruneBackoff.seconds.uint64)) + prunes.add( + ControlPrune( + topicID: topic, + peers: @[], + # omitting heavy computation here as the remote did something illegal + backoff: g.parameters.pruneBackoff.seconds.uint64, + ) + ) let backoff = Moment.fromNow(g.parameters.pruneBackoff) - g.backingOff - .mgetOrPut(topic, initTable[PeerId, Moment]())[peer.peerId] = backoff + + g.backingOff.mgetOrPut(topic, initTable[PeerId, Moment]())[peer.peerId] = backoff peer.behaviourPenalty += 0.1 @@ -173,21 +212,27 @@ proc handleGraft*(g: GossipSub, else: trace "pruning grafting peer, mesh full", peer, topic, score = peer.score, mesh = g.mesh.peers(topic) - prunes.add(ControlPrune( - topicID: topic, - peers: g.peerExchangeList(topic), - backoff: g.parameters.pruneBackoff.seconds.uint64)) + prunes.add( + ControlPrune( + topicID: topic, + peers: g.peerExchangeList(topic), + backoff: g.parameters.pruneBackoff.seconds.uint64, + ) + ) let backoff = Moment.fromNow(g.parameters.pruneBackoff) - g.backingOff - .mgetOrPut(topic, initTable[PeerId, Moment]())[peer.peerId] = backoff + + g.backingOff.mgetOrPut(topic, initTable[PeerId, Moment]())[peer.peerId] = + backoff else: trace "peer grafting topic we're not interested in", peer, topic # gossip 1.1, we do not send a control message prune anymore return prunes -proc getPeers(prune: ControlPrune, peer: PubSubPeer): seq[(PeerId, Option[PeerRecord])] = +proc getPeers( + prune: ControlPrune, peer: PubSubPeer +): seq[(PeerId, Option[PeerRecord])] = var routingRecords: seq[(PeerId, Option[PeerRecord])] for record in prune.peers: var peerRecord = none(PeerRecord) @@ -214,31 +259,28 @@ proc handlePrune*(g: GossipSub, peer: PubSubPeer, prunes: seq[ControlPrune]) = if prune.backoff > 0: let # avoid overflows and clamp to reasonable value - backoffSeconds = clamp( - prune.backoff + BackoffSlackTime, - 0'u64, - 1.days.seconds.uint64 - ) + backoffSeconds = + clamp(prune.backoff + BackoffSlackTime, 0'u64, 1.days.seconds.uint64) backoff = Moment.fromNow(backoffSeconds.int64.seconds) current = g.backingOff.getOrDefault(topic).getOrDefault(peer.peerId) if backoff > current: - g.backingOff - .mgetOrPut(topic, initTable[PeerId, Moment]())[peer.peerId] = backoff + g.backingOff.mgetOrPut(topic, initTable[PeerId, Moment]())[peer.peerId] = + backoff trace "pruning rpc received peer", peer, score = peer.score g.pruned(peer, topic, setBackoff = false) g.mesh.removePeer(topic, peer) if peer.score > g.parameters.gossipThreshold and prune.peers.len > 0 and - g.routingRecordsHandler.len > 0: + g.routingRecordsHandler.len > 0: let routingRecords = prune.getPeers(peer) for handler in g.routingRecordsHandler: handler(peer.peerId, topic, routingRecords) -proc handleIHave*(g: GossipSub, - peer: PubSubPeer, - ihaves: seq[ControlIHave]): ControlIWant = +proc handleIHave*( + g: GossipSub, peer: PubSubPeer, ihaves: seq[ControlIHave] +): ControlIWant = var res: ControlIWant if peer.score < g.parameters.gossipThreshold: trace "ihave: ignoring low score peer", peer, score = peer.score @@ -246,8 +288,7 @@ proc handleIHave*(g: GossipSub, trace "ihave: ignoring out of budget peer", peer, score = peer.score else: for ihave in ihaves: - trace "peer sent ihave", - peer, topicID = ihave.topicID, msgs = ihave.messageIDs + trace "peer sent ihave", peer, topicID = ihave.topicID, msgs = ihave.messageIDs if ihave.topicID in g.topics: for msgId in ihave.messageIDs: if not g.hasSeen(g.salt(msgId)): @@ -256,23 +297,22 @@ proc handleIHave*(g: GossipSub, elif msgId notin res.messageIDs: res.messageIDs.add(msgId) dec peer.iHaveBudget - trace "requested message via ihave", messageID=msgId + trace "requested message via ihave", messageID = msgId # shuffling res.messageIDs before sending it out to increase the likelihood # of getting an answer if the peer truncates the list due to internal size restrictions. g.rng.shuffle(res.messageIDs) return res -proc handleIDontWant*(g: GossipSub, - peer: PubSubPeer, - iDontWants: seq[ControlIWant]) = +proc handleIDontWant*(g: GossipSub, peer: PubSubPeer, iDontWants: seq[ControlIWant]) = for dontWant in iDontWants: for messageId in dontWant.messageIDs: - if peer.heDontWants[^1].len > 1000: break + if peer.heDontWants[^1].len > 1000: + break peer.heDontWants[^1].incl(g.salt(messageId)) -proc handleIWant*(g: GossipSub, - peer: PubSubPeer, - iwants: seq[ControlIWant]): seq[Message] = +proc handleIWant*( + g: GossipSub, peer: PubSubPeer, iwants: seq[ControlIWant] +): seq[Message] = var messages: seq[Message] invalidRequests = 0 @@ -284,17 +324,17 @@ proc handleIWant*(g: GossipSub, trace "peer sent iwant", peer, messageID = mid # canAskIWant will only return true once for a specific message if not peer.canAskIWant(mid): - libp2p_gossipsub_received_iwants.inc(1, labelValues=["notsent"]) + libp2p_gossipsub_received_iwants.inc(1, labelValues = ["notsent"]) invalidRequests.inc() if invalidRequests > 20: - libp2p_gossipsub_received_iwants.inc(1, labelValues=["skipped"]) + libp2p_gossipsub_received_iwants.inc(1, labelValues = ["skipped"]) return messages continue let msg = g.mcache.get(mid).valueOr: - libp2p_gossipsub_received_iwants.inc(1, labelValues=["unknown"]) + libp2p_gossipsub_received_iwants.inc(1, labelValues = ["unknown"]) continue - libp2p_gossipsub_received_iwants.inc(1, labelValues=["correct"]) + libp2p_gossipsub_received_iwants.inc(1, labelValues = ["correct"]) messages.add(msg) return messages @@ -303,9 +343,15 @@ proc commitMetrics(metrics: var MeshMetrics) = libp2p_gossipsub_no_peers_topics.set(metrics.noPeersTopics) libp2p_gossipsub_under_dout_topics.set(metrics.underDoutTopics) libp2p_gossipsub_healthy_peers_topics.set(metrics.healthyPeersTopics) - libp2p_gossipsub_peers_per_topic_gossipsub.set(metrics.otherPeersPerTopicGossipsub, labelValues = ["other"]) - libp2p_gossipsub_peers_per_topic_fanout.set(metrics.otherPeersPerTopicFanout, labelValues = ["other"]) - libp2p_gossipsub_peers_per_topic_mesh.set(metrics.otherPeersPerTopicMesh, labelValues = ["other"]) + libp2p_gossipsub_peers_per_topic_gossipsub.set( + metrics.otherPeersPerTopicGossipsub, labelValues = ["other"] + ) + libp2p_gossipsub_peers_per_topic_fanout.set( + metrics.otherPeersPerTopicFanout, labelValues = ["other"] + ) + libp2p_gossipsub_peers_per_topic_mesh.set( + metrics.otherPeersPerTopicMesh, labelValues = ["other"] + ) proc rebalanceMesh*(g: GossipSub, topic: string, metrics: ptr MeshMetrics = nil) = logScope: @@ -331,14 +377,13 @@ proc rebalanceMesh*(g: GossipSub, topic: string, metrics: ptr MeshMetrics = nil) var candidates: seq[PubSubPeer] currentMesh = addr defaultMesh - g.mesh.withValue(topic, v): currentMesh = v + g.mesh.withValue(topic, v): + currentMesh = v g.gossipsub.withValue(topic, peerList): for it in peerList[]: - if - it.connected and + if it.connected and # avoid negative score peers - it.score >= 0.0 and - it notin currentMesh[] and + it.score >= 0.0 and it notin currentMesh[] and # don't pick direct peers it.peerId notin g.parameters.directPeers and # and avoid peers we are backing off @@ -362,21 +407,19 @@ proc rebalanceMesh*(g: GossipSub, topic: string, metrics: ptr MeshMetrics = nil) g.grafted(peer, topic) g.fanout.removePeer(topic, peer) grafts &= peer - elif nOutPeers < g.parameters.dOut: trace "replenishing mesh outbound quota", peers = g.mesh.peers(topic) var candidates: seq[PubSubPeer] currentMesh = addr defaultMesh - g.mesh.withValue(topic, v): currentMesh = v + g.mesh.withValue(topic, v): + currentMesh = v g.gossipsub.withValue(topic, peerList): for it in peerList[]: - if - it.connected and - # get only outbound ones - it.outbound and - it notin currentMesh[] and + if it.connected and + # get only outbound ones + it.outbound and it notin currentMesh[] and # avoid negative score peers it.score >= 0.0 and # don't pick direct peers @@ -402,7 +445,6 @@ proc rebalanceMesh*(g: GossipSub, topic: string, metrics: ptr MeshMetrics = nil) g.fanout.removePeer(topic, peer) grafts &= peer - # get again npeers after possible grafts npeers = g.mesh.peers(topic) if npeers > g.parameters.dHigh: @@ -413,9 +455,15 @@ proc rebalanceMesh*(g: GossipSub, topic: string, metrics: ptr MeshMetrics = nil) libp2p_gossipsub_above_dhigh_condition.inc(labelValues = ["other"]) # prune peers if we've gone over Dhi - prunes = toSeq(try: g.mesh[topic] except KeyError: raiseAssert "have peers") + prunes = toSeq( + try: + g.mesh[topic] + except KeyError: + raiseAssert "have peers" + ) # avoid pruning peers we are currently grafting in this heartbeat - prunes.keepIf do (x: PubSubPeer) -> bool: x notin grafts + prunes.keepIf do(x: PubSubPeer) -> bool: + x notin grafts # shuffle anyway, score might be not used g.rng.shuffle(prunes) @@ -463,7 +511,12 @@ proc rebalanceMesh*(g: GossipSub, topic: string, metrics: ptr MeshMetrics = nil) # opportunistic grafting, by spec mesh should not be empty... if g.mesh.peers(topic) > 1: - var peers = toSeq(try: g.mesh[topic] except KeyError: raiseAssert "have peers") + var peers = toSeq( + try: + g.mesh[topic] + except KeyError: + raiseAssert "have peers" + ) # grafting so high score has priority peers.sort(byScore, SortOrder.Descending) let medianIdx = peers.len div 2 @@ -474,13 +527,12 @@ proc rebalanceMesh*(g: GossipSub, topic: string, metrics: ptr MeshMetrics = nil) var avail: seq[PubSubPeer] currentMesh = addr defaultMesh - g.mesh.withValue(topic, v): currentMesh = v + g.mesh.withValue(topic, v): + currentMesh = v g.gossipsub.withValue(topic, peerList): for it in peerList[]: - if - # avoid negative score peers - it.score >= median.score and - it notin currentMesh[] and + if it.score >= median.score and # avoid negative score peers + it notin currentMesh[] and # don't pick direct peers it.peerId notin g.parameters.directPeers and # and avoid peers we are backing off @@ -507,17 +559,23 @@ proc rebalanceMesh*(g: GossipSub, topic: string, metrics: ptr MeshMetrics = nil) inc metrics[].healthyPeersTopics var meshPeers = toSeq(g.mesh.getOrDefault(topic, initHashSet[PubSubPeer]())) - meshPeers.keepIf do (x: PubSubPeer) -> bool: x.outbound + meshPeers.keepIf do(x: PubSubPeer) -> bool: + x.outbound if meshPeers.len < g.parameters.dOut: inc metrics[].underDoutTopics if g.knownTopics.contains(topic): - libp2p_gossipsub_peers_per_topic_gossipsub - .set(g.gossipsub.peers(topic).int64, labelValues = [topic]) - libp2p_gossipsub_peers_per_topic_fanout - .set(g.fanout.peers(topic).int64, labelValues = [topic]) - libp2p_gossipsub_peers_per_topic_mesh - .set(g.mesh.peers(topic).int64, labelValues = [topic]) + libp2p_gossipsub_peers_per_topic_gossipsub.set( + g.gossipsub.peers(topic).int64, labelValues = [topic] + ) + + libp2p_gossipsub_peers_per_topic_fanout.set( + g.fanout.peers(topic).int64, labelValues = [topic] + ) + + libp2p_gossipsub_peers_per_topic_mesh.set( + g.mesh.peers(topic).int64, labelValues = [topic] + ) else: metrics[].otherPeersPerTopicGossipsub += g.gossipsub.peers(topic).int64 metrics[].otherPeersPerTopicFanout += g.fanout.peers(topic).int64 @@ -527,14 +585,24 @@ proc rebalanceMesh*(g: GossipSub, topic: string, metrics: ptr MeshMetrics = nil) # Send changes to peers after table updates to avoid stale state if grafts.len > 0: - let graft = RPCMsg(control: some(ControlMessage(graft: @[ControlGraft(topicID: topic)]))) + let graft = + RPCMsg(control: some(ControlMessage(graft: @[ControlGraft(topicID: topic)]))) g.broadcast(grafts, graft, isHighPriority = true) if prunes.len > 0: - let prune = RPCMsg(control: some(ControlMessage( - prune: @[ControlPrune( - topicID: topic, - peers: g.peerExchangeList(topic), - backoff: g.parameters.pruneBackoff.seconds.uint64)]))) + let prune = RPCMsg( + control: some( + ControlMessage( + prune: + @[ + ControlPrune( + topicID: topic, + peers: g.peerExchangeList(topic), + backoff: g.parameters.pruneBackoff.seconds.uint64, + ) + ] + ) + ) + ) g.broadcast(prunes, prune, isHighPriority = true) proc dropFanoutPeers*(g: GossipSub) = @@ -552,14 +620,16 @@ proc dropFanoutPeers*(g: GossipSub) = proc replenishFanout*(g: GossipSub, topic: string) = ## get fanout peers for a topic - logScope: topic + logScope: + topic trace "about to replenish fanout" if g.fanout.peers(topic) < g.parameters.dLow: let currentMesh = g.mesh.getOrDefault(topic) trace "replenishing fanout", peers = g.fanout.peers(topic) for peer in g.gossipsub.getOrDefault(topic): - if peer in currentMesh: continue + if peer in currentMesh: + continue if g.fanout.addPeer(topic, peer): if g.fanout.peers(topic) == g.parameters.d: break @@ -574,14 +644,14 @@ proc getGossipPeers*(g: GossipSub): Table[PubSubPeer, ControlMessage] = var control: Table[PubSubPeer, ControlMessage] let topics = toHashSet(toSeq(g.mesh.keys)) + toHashSet(toSeq(g.fanout.keys)) - trace "getting gossip peers (iHave)", ntopics=topics.len + trace "getting gossip peers (iHave)", ntopics = topics.len for topic in topics: if topic notin g.gossipsub: trace "topic not in gossip array, skipping", topic = topic continue let mids = g.mcache.window(topic) - if not(mids.len > 0): + if not (mids.len > 0): trace "no messages to emit" continue @@ -589,7 +659,7 @@ proc getGossipPeers*(g: GossipSub): Table[PubSubPeer, ControlMessage] = cacheWindowSize += midsSeq.len - trace "got messages to emit", size=midsSeq.len + trace "got messages to emit", size = midsSeq.len # not in spec # similar to rust: https://github.com/sigp/rust-libp2p/blob/f53d02bc873fef2bf52cd31e3d5ce366a41d8a8c/protocols/gossipsub/src/behaviour.rs#L2101 @@ -605,10 +675,9 @@ proc getGossipPeers*(g: GossipSub): Table[PubSubPeer, ControlMessage] = gossipPeers = mesh + fanout var allPeers = toSeq(g.gossipsub.getOrDefault(topic)) - allPeers.keepIf do (x: PubSubPeer) -> bool: - x.peerId notin g.parameters.directPeers and - x notin gossipPeers and - x.score >= g.parameters.gossipThreshold + allPeers.keepIf do(x: PubSubPeer) -> bool: + x.peerId notin g.parameters.directPeers and x notin gossipPeers and + x.score >= g.parameters.gossipThreshold # https://github.com/libp2p/specs/blob/98c5aa9421703fc31b0833ad8860a55db15be063/pubsub/gossipsub/gossipsub-v1.1.md#adaptive-gossip-dissemination let @@ -629,68 +698,77 @@ proc getGossipPeers*(g: GossipSub): Table[PubSubPeer, ControlMessage] = return control proc onHeartbeat(g: GossipSub) = - # reset IWANT budget - # reset IHAVE cap + # reset IWANT budget + # reset IHAVE cap + block: + for peer in g.peers.values: + peer.sentIHaves.addFirst(default(HashSet[MessageId])) + if peer.sentIHaves.len > g.parameters.historyLength: + discard peer.sentIHaves.popLast() + peer.heDontWants.addFirst(default(HashSet[SaltedId])) + if peer.heDontWants.len > g.parameters.historyLength: + discard peer.heDontWants.popLast() + peer.iHaveBudget = IHavePeerBudget + peer.pingBudget = PingsPeerBudget + + var meshMetrics = MeshMetrics() + + for t in toSeq(g.topics.keys): + # remove expired backoffs block: - for peer in g.peers.values: - peer.sentIHaves.addFirst(default(HashSet[MessageId])) - if peer.sentIHaves.len > g.parameters.historyLength: - discard peer.sentIHaves.popLast() - peer.heDontWants.addFirst(default(HashSet[SaltedId])) - if peer.heDontWants.len > g.parameters.historyLength: - discard peer.heDontWants.popLast() - peer.iHaveBudget = IHavePeerBudget - peer.pingBudget = PingsPeerBudget - - var meshMetrics = MeshMetrics() - - for t in toSeq(g.topics.keys): - # remove expired backoffs - block: - handleBackingOff(g.backingOff, t) - - # prune every negative score peer - # do this before relance - # in order to avoid grafted -> pruned in the same cycle - let meshPeers = g.mesh.getOrDefault(t) - var prunes: seq[PubSubPeer] - for peer in meshPeers: - if peer.score < 0.0: - trace "pruning negative score peer", peer, score = peer.score - g.pruned(peer, t) - g.mesh.removePeer(t, peer) - prunes &= peer - if prunes.len > 0: - let prune = RPCMsg(control: some(ControlMessage( - prune: @[ControlPrune( - topicID: t, - peers: g.peerExchangeList(t), - backoff: g.parameters.pruneBackoff.seconds.uint64)]))) - g.broadcast(prunes, prune, isHighPriority = true) - - # pass by ptr in order to both signal we want to update metrics - # and as well update the struct for each topic during this iteration - g.rebalanceMesh(t, addr meshMetrics) - - commitMetrics(meshMetrics) - - g.dropFanoutPeers() - - # replenish known topics to the fanout - for t in toSeq(g.fanout.keys): - g.replenishFanout(t) - - let peers = g.getGossipPeers() - for peer, control in peers: - # only ihave from here - for ihave in control.ihave: - if g.knownTopics.contains(ihave.topicID): - libp2p_pubsub_broadcast_ihave.inc(labelValues = [ihave.topicID]) - else: - libp2p_pubsub_broadcast_ihave.inc(labelValues = ["generic"]) - g.send(peer, RPCMsg(control: some(control)), isHighPriority = true) + handleBackingOff(g.backingOff, t) + + # prune every negative score peer + # do this before relance + # in order to avoid grafted -> pruned in the same cycle + let meshPeers = g.mesh.getOrDefault(t) + var prunes: seq[PubSubPeer] + for peer in meshPeers: + if peer.score < 0.0: + trace "pruning negative score peer", peer, score = peer.score + g.pruned(peer, t) + g.mesh.removePeer(t, peer) + prunes &= peer + if prunes.len > 0: + let prune = RPCMsg( + control: some( + ControlMessage( + prune: + @[ + ControlPrune( + topicID: t, + peers: g.peerExchangeList(t), + backoff: g.parameters.pruneBackoff.seconds.uint64, + ) + ] + ) + ) + ) + g.broadcast(prunes, prune, isHighPriority = true) + + # pass by ptr in order to both signal we want to update metrics + # and as well update the struct for each topic during this iteration + g.rebalanceMesh(t, addr meshMetrics) + + commitMetrics(meshMetrics) + + g.dropFanoutPeers() + + # replenish known topics to the fanout + for t in toSeq(g.fanout.keys): + g.replenishFanout(t) + + let peers = g.getGossipPeers() + for peer, control in peers: + # only ihave from here + for ihave in control.ihave: + if g.knownTopics.contains(ihave.topicID): + libp2p_pubsub_broadcast_ihave.inc(labelValues = [ihave.topicID]) + else: + libp2p_pubsub_broadcast_ihave.inc(labelValues = ["generic"]) + g.send(peer, RPCMsg(control: some(control)), isHighPriority = true) - g.mcache.shift() # shift the cache + g.mcache.shift() # shift the cache proc heartbeat*(g: GossipSub) {.async.} = heartbeat "GossipSub", g.parameters.heartbeatInterval: diff --git a/libp2p/protocols/pubsub/gossipsub/scoring.nim b/libp2p/protocols/pubsub/gossipsub/scoring.nim index 9239819d43..1233e22621 100644 --- a/libp2p/protocols/pubsub/gossipsub/scoring.nim +++ b/libp2p/protocols/pubsub/gossipsub/scoring.nim @@ -21,16 +21,56 @@ import ../pubsub logScope: topics = "libp2p gossipsub" -declareGauge(libp2p_gossipsub_peers_scores, "the scores of the peers in gossipsub", labels = ["agent"]) -declareCounter(libp2p_gossipsub_bad_score_disconnection, "the number of peers disconnected by gossipsub", labels = ["agent"]) -declareGauge(libp2p_gossipsub_peers_score_firstMessageDeliveries, "Detailed gossipsub scoring metric", labels = ["agent"]) -declareGauge(libp2p_gossipsub_peers_score_meshMessageDeliveries, "Detailed gossipsub scoring metric", labels = ["agent"]) -declareGauge(libp2p_gossipsub_peers_score_meshFailurePenalty, "Detailed gossipsub scoring metric", labels = ["agent"]) -declareGauge(libp2p_gossipsub_peers_score_invalidMessageDeliveries, "Detailed gossipsub scoring metric", labels = ["agent"]) -declareGauge(libp2p_gossipsub_peers_score_appScore, "Detailed gossipsub scoring metric", labels = ["agent"]) -declareGauge(libp2p_gossipsub_peers_score_behaviourPenalty, "Detailed gossipsub scoring metric", labels = ["agent"]) -declareGauge(libp2p_gossipsub_peers_score_colocationFactor, "Detailed gossipsub scoring metric", labels = ["agent"]) -declarePublicCounter(libp2p_gossipsub_peers_rate_limit_hits, "The number of times peers were above their rate limit", labels = ["agent"]) +declareGauge( + libp2p_gossipsub_peers_scores, + "the scores of the peers in gossipsub", + labels = ["agent"], +) +declareCounter( + libp2p_gossipsub_bad_score_disconnection, + "the number of peers disconnected by gossipsub", + labels = ["agent"], +) +declareGauge( + libp2p_gossipsub_peers_score_firstMessageDeliveries, + "Detailed gossipsub scoring metric", + labels = ["agent"], +) +declareGauge( + libp2p_gossipsub_peers_score_meshMessageDeliveries, + "Detailed gossipsub scoring metric", + labels = ["agent"], +) +declareGauge( + libp2p_gossipsub_peers_score_meshFailurePenalty, + "Detailed gossipsub scoring metric", + labels = ["agent"], +) +declareGauge( + libp2p_gossipsub_peers_score_invalidMessageDeliveries, + "Detailed gossipsub scoring metric", + labels = ["agent"], +) +declareGauge( + libp2p_gossipsub_peers_score_appScore, + "Detailed gossipsub scoring metric", + labels = ["agent"], +) +declareGauge( + libp2p_gossipsub_peers_score_behaviourPenalty, + "Detailed gossipsub scoring metric", + labels = ["agent"], +) +declareGauge( + libp2p_gossipsub_peers_score_colocationFactor, + "Detailed gossipsub scoring metric", + labels = ["agent"], +) +declarePublicCounter( + libp2p_gossipsub_peers_rate_limit_hits, + "The number of times peers were above their rate limit", + labels = ["agent"], +) proc init*(_: type[TopicParams]): TopicParams = TopicParams( @@ -50,21 +90,24 @@ proc init*(_: type[TopicParams]): TopicParams = meshFailurePenaltyWeight: -1.0, meshFailurePenaltyDecay: 0.5, invalidMessageDeliveriesWeight: -1.0, - invalidMessageDeliveriesDecay: 0.5 + invalidMessageDeliveriesDecay: 0.5, ) proc withPeerStats*( g: GossipSub, peerId: PeerId, - action: proc (stats: var PeerStats) {.gcsafe, raises: [].}) = + action: proc(stats: var PeerStats) {.gcsafe, raises: [].}, +) = ## Add or update peer statistics for a particular peer id - the statistics ## are retained across multiple connections until they expire - g.peerStats.withValue(peerId, stats) do: + g.peerStats.withValue(peerId, stats): action(stats[]) do: - action(g.peerStats.mgetOrPut(peerId, PeerStats( - expire: Moment.now() + g.parameters.retainScore - ))) + action( + g.peerStats.mgetOrPut( + peerId, PeerStats(expire: Moment.now() + g.parameters.retainScore) + ) + ) func `/`(a, b: Duration): float64 = let @@ -72,14 +115,15 @@ func `/`(a, b: Duration): float64 = fb = float64(b.nanoseconds) fa / fb -func byScore*(x,y: PubSubPeer): int = system.cmp(x.score, y.score) +func byScore*(x, y: PubSubPeer): int = + system.cmp(x.score, y.score) proc colocationFactor(g: GossipSub, peer: PubSubPeer): float64 = - let address = peer.address.valueOr: return 0.0 + let address = peer.address.valueOr: + return 0.0 g.peersInIP.mgetOrPut(address, initHashSet[PeerId]()).incl(peer.peerId) - let - ipPeers = g.peersInIP.getOrDefault(address).len().float64 + let ipPeers = g.peersInIP.getOrDefault(address).len().float64 if ipPeers > g.parameters.ipColocationFactorThreshold: trace "colocationFactor over threshold", peer, address, ipPeers let over = ipPeers - g.parameters.ipColocationFactorThreshold @@ -95,7 +139,7 @@ proc disconnectPeer*(g: GossipSub, peer: PubSubPeer) {.async.} = proc disconnectIfBadScorePeer*(g: GossipSub, peer: PubSubPeer, score: float64) = if g.parameters.disconnectBadPeers and score < g.parameters.graylistThreshold and - peer.peerId notin g.parameters.directPeers: + peer.peerId notin g.parameters.directPeers: debug "disconnecting bad score peer", peer, score = peer.score asyncSpawn(g.disconnectPeer(peer)) libp2p_gossipsub_bad_score_disconnection.inc(labelValues = [peer.getAgent()]) @@ -110,7 +154,7 @@ proc updateScores*(g: GossipSub) = # avoid async for peerId, stats in g.peerStats.mpairs: let peer = g.peers.getOrDefault(peerId) - if isNil(peer) or not(peer.connected): + if isNil(peer) or not (peer.connected): if now > stats.expire: evicting.add(peerId) trace "evicted peer from memory", peer = peerId @@ -147,12 +191,14 @@ proc updateScores*(g: GossipSub) = # avoid async else: info.meshMessageDeliveriesActive = false - topicScore += info.firstMessageDeliveries * topicParams.firstMessageDeliveriesWeight + topicScore += + info.firstMessageDeliveries * topicParams.firstMessageDeliveriesWeight trace "p2", peer, p2 = info.firstMessageDeliveries, topic, topicScore if info.meshMessageDeliveriesActive: if info.meshMessageDeliveries < topicParams.meshMessageDeliveriesThreshold: - let deficit = topicParams.meshMessageDeliveriesThreshold - info.meshMessageDeliveries + let deficit = + topicParams.meshMessageDeliveriesThreshold - info.meshMessageDeliveries let p3 = deficit * deficit trace "p3", peer, p3, topic, topicScore topicScore += p3 * topicParams.meshMessageDeliveriesWeight @@ -160,20 +206,34 @@ proc updateScores*(g: GossipSub) = # avoid async topicScore += info.meshFailurePenalty * topicParams.meshFailurePenaltyWeight trace "p3b", peer, p3b = info.meshFailurePenalty, topic, topicScore - topicScore += info.invalidMessageDeliveries * info.invalidMessageDeliveries * topicParams.invalidMessageDeliveriesWeight - trace "p4", peer, p4 = info.invalidMessageDeliveries * info.invalidMessageDeliveries, topic, topicScore + topicScore += + info.invalidMessageDeliveries * info.invalidMessageDeliveries * + topicParams.invalidMessageDeliveriesWeight + trace "p4", + peer, + p4 = info.invalidMessageDeliveries * info.invalidMessageDeliveries, + topic, + topicScore scoreAcc += topicScore * topicParams.topicWeight - trace "updated peer topic's scores", peer, scoreAcc, topic, info, topicScore, - topicWeight = topicParams.topicWeight + trace "updated peer topic's scores", + peer, scoreAcc, topic, info, topicScore, topicWeight = topicParams.topicWeight # Score metrics let agent = peer.getAgent() - libp2p_gossipsub_peers_score_firstMessageDeliveries.inc(info.firstMessageDeliveries, labelValues = [agent]) - libp2p_gossipsub_peers_score_meshMessageDeliveries.inc(info.meshMessageDeliveries, labelValues = [agent]) - libp2p_gossipsub_peers_score_meshFailurePenalty.inc(info.meshFailurePenalty, labelValues = [agent]) - libp2p_gossipsub_peers_score_invalidMessageDeliveries.inc(info.invalidMessageDeliveries, labelValues = [agent]) + libp2p_gossipsub_peers_score_firstMessageDeliveries.inc( + info.firstMessageDeliveries, labelValues = [agent] + ) + libp2p_gossipsub_peers_score_meshMessageDeliveries.inc( + info.meshMessageDeliveries, labelValues = [agent] + ) + libp2p_gossipsub_peers_score_meshFailurePenalty.inc( + info.meshFailurePenalty, labelValues = [agent] + ) + libp2p_gossipsub_peers_score_invalidMessageDeliveries.inc( + info.invalidMessageDeliveries, labelValues = [agent] + ) # Score decay info.firstMessageDeliveries *= topicParams.firstMessageDeliveriesDecay @@ -197,23 +257,37 @@ proc updateScores*(g: GossipSub) = # avoid async stats.topicInfos[topic] = info scoreAcc += peer.appScore * g.parameters.appSpecificWeight - trace "appScore", peer, scoreAcc, appScore = peer.appScore, - appSpecificWeight = g.parameters.appSpecificWeight + trace "appScore", + peer, + scoreAcc, + appScore = peer.appScore, + appSpecificWeight = g.parameters.appSpecificWeight # The value of the parameter is the square of the counter and is mixed with a negative weight. - scoreAcc += peer.behaviourPenalty * peer.behaviourPenalty * g.parameters.behaviourPenaltyWeight - trace "behaviourPenalty", peer, scoreAcc, behaviourPenalty = peer.behaviourPenalty, - behaviourPenaltyWeight = g.parameters.behaviourPenaltyWeight + scoreAcc += + peer.behaviourPenalty * peer.behaviourPenalty * g.parameters.behaviourPenaltyWeight + trace "behaviourPenalty", + peer, + scoreAcc, + behaviourPenalty = peer.behaviourPenalty, + behaviourPenaltyWeight = g.parameters.behaviourPenaltyWeight let colocationFactor = g.colocationFactor(peer) scoreAcc += colocationFactor * g.parameters.ipColocationFactorWeight - trace "colocationFactor", peer, scoreAcc, colocationFactor, - ipColocationFactorWeight = g.parameters.ipColocationFactorWeight + trace "colocationFactor", + peer, + scoreAcc, + colocationFactor, + ipColocationFactorWeight = g.parameters.ipColocationFactorWeight # Score metrics let agent = peer.getAgent() libp2p_gossipsub_peers_score_appScore.inc(peer.appScore, labelValues = [agent]) - libp2p_gossipsub_peers_score_behaviourPenalty.inc(peer.behaviourPenalty, labelValues = [agent]) - libp2p_gossipsub_peers_score_colocationFactor.inc(colocationFactor, labelValues = [agent]) + libp2p_gossipsub_peers_score_behaviourPenalty.inc( + peer.behaviourPenalty, labelValues = [agent] + ) + libp2p_gossipsub_peers_score_colocationFactor.inc( + colocationFactor, labelValues = [agent] + ) # decay behaviourPenalty peer.behaviourPenalty *= g.parameters.behaviourPenaltyDecay @@ -228,7 +302,8 @@ proc updateScores*(g: GossipSub) = # avoid async stats.behaviourPenalty = peer.behaviourPenalty stats.expire = now + g.parameters.retainScore # refresh expiration - trace "updated (accumulated) peer's score", peer, peerScore = peer.score, n_topics, is_grafted + trace "updated (accumulated) peer's score", + peer, peerScore = peer.score, n_topics, is_grafted g.disconnectIfBadScorePeer(peer, stats.score) libp2p_gossipsub_peers_scores.inc(peer.score, labelValues = [agent]) @@ -243,15 +318,19 @@ proc scoringHeartbeat*(g: GossipSub) {.async.} = trace "running scoring heartbeat", instance = cast[int](g) g.updateScores() -proc punishInvalidMessage*(g: GossipSub, peer: PubSubPeer, msg: Message) {.async.} = +proc punishInvalidMessage*(g: GossipSub, peer: PubSubPeer, msg: Message) {.async.} = let uselessAppBytesNum = msg.data.len peer.overheadRateLimitOpt.withValue(overheadRateLimit): if not overheadRateLimit.tryConsume(uselessAppBytesNum): - debug "Peer sent invalid message and it's above rate limit", peer, uselessAppBytesNum - libp2p_gossipsub_peers_rate_limit_hits.inc(labelValues = [peer.getAgent()]) # let's just measure at the beginning for test purposes. + debug "Peer sent invalid message and it's above rate limit", + peer, uselessAppBytesNum + libp2p_gossipsub_peers_rate_limit_hits.inc(labelValues = [peer.getAgent()]) + # let's just measure at the beginning for test purposes. if g.parameters.disconnectPeerAboveRateLimit: await g.disconnectPeer(peer) - raise newException(PeerRateLimitError, "Peer disconnected because it's above rate limit.") + raise newException( + PeerRateLimitError, "Peer disconnected because it's above rate limit." + ) let topic = msg.topic if topic notin g.topics: @@ -265,11 +344,7 @@ proc addCapped*[T](stat: var T, diff, cap: T) = stat += min(diff, cap - stat) proc rewardDelivered*( - g: GossipSub, - peer: PubSubPeer, - topic: string, - first: bool, - delay = ZeroDuration, + g: GossipSub, peer: PubSubPeer, topic: string, first: bool, delay = ZeroDuration ) = if topic notin g.topics: return @@ -281,14 +356,16 @@ proc rewardDelivered*( # Too old return - g.withPeerStats(peer.peerId) do (stats: var PeerStats): + g.withPeerStats(peer.peerId) do(stats: var PeerStats): stats.topicInfos.withValue(topic, tstats): if first: tstats[].firstMessageDeliveries.addCapped( - 1, topicParams.firstMessageDeliveriesCap) + 1, topicParams.firstMessageDeliveriesCap + ) if tstats[].inMesh: tstats[].meshMessageDeliveries.addCapped( - 1, topicParams.meshMessageDeliveriesCap) - do: # make sure we don't lose this information + 1, topicParams.meshMessageDeliveriesCap + ) + do: stats.topicInfos[topic] = TopicInfo(meshMessageDeliveries: 1) diff --git a/libp2p/protocols/pubsub/gossipsub/types.nim b/libp2p/protocols/pubsub/gossipsub/types.nim index 13dcb8809d..3fb98749ac 100644 --- a/libp2p/protocols/pubsub/gossipsub/types.nim +++ b/libp2p/protocols/pubsub/gossipsub/types.nim @@ -32,16 +32,14 @@ const GossipSubHistoryLength* = 5 GossipSubHistoryGossip* = 3 -# heartbeat interval + # heartbeat interval GossipSubHeartbeatInterval* = 1.seconds # fanout ttl -const - GossipSubFanoutTTL* = 1.minutes +const GossipSubFanoutTTL* = 1.minutes # gossip parameters -const - GossipBackoffPeriod* = 1.minutes +const GossipBackoffPeriod* = 1.minutes const BackoffSlackTime* = 2 # seconds @@ -53,8 +51,7 @@ const IHaveMaxLength* = 5000 type - TopicInfo* = object - # gossip 1.1 related + TopicInfo* = object # gossip 1.1 related graftTime*: Moment meshTime*: Duration inMesh*: bool @@ -147,7 +144,8 @@ type disconnectBadPeers*: bool enablePX*: bool - bandwidthEstimatebps*: int # This is currently used only for limting flood publishing. 0 disables flood-limiting completely + bandwidthEstimatebps*: int + # This is currently used only for limting flood publishing. 0 disables flood-limiting completely overheadRateLimit*: Opt[tuple[bytes: int, interval: Duration]] disconnectPeerAboveRateLimit*: bool @@ -159,23 +157,25 @@ type ValidationSeenTable* = Table[SaltedId, HashSet[PubSubPeer]] RoutingRecordsPair* = tuple[id: PeerId, record: Option[PeerRecord]] - RoutingRecordsHandler* = - proc(peer: PeerId, + RoutingRecordsHandler* = proc( + peer: PeerId, tag: string, # For gossipsub, the topic - peers: seq[RoutingRecordsPair]) - {.gcsafe, raises: [].} + peers: seq[RoutingRecordsPair], + ) {.gcsafe, raises: [].} GossipSub* = ref object of FloodSub - mesh*: PeerTable # peers that we send messages to when we are subscribed to the topic - fanout*: PeerTable # peers that we send messages to when we're not subscribed to the topic - gossipsub*: PeerTable # peers that are subscribed to a topic - subscribedDirectPeers*: PeerTable # directpeers that we keep alive - backingOff*: BackoffTable # peers to backoff from when replenishing the mesh - lastFanoutPubSub*: Table[string, Moment] # last publish time for fanout topics - mcache*: MCache # messages cache - validationSeen*: ValidationSeenTable # peers who sent us message in validation - heartbeatFut*: Future[void] # cancellation future for heartbeat interval - scoringHeartbeatFut*: Future[void] # cancellation future for scoring heartbeat interval + mesh*: PeerTable # peers that we send messages to when we are subscribed to the topic + fanout*: PeerTable + # peers that we send messages to when we're not subscribed to the topic + gossipsub*: PeerTable # peers that are subscribed to a topic + subscribedDirectPeers*: PeerTable # directpeers that we keep alive + backingOff*: BackoffTable # peers to backoff from when replenishing the mesh + lastFanoutPubSub*: Table[string, Moment] # last publish time for fanout topics + mcache*: MCache # messages cache + validationSeen*: ValidationSeenTable # peers who sent us message in validation + heartbeatFut*: Future[void] # cancellation future for heartbeat interval + scoringHeartbeatFut*: Future[void] + # cancellation future for scoring heartbeat interval heartbeatRunning*: bool peerStats*: Table[PeerId, PeerStats] @@ -187,8 +187,7 @@ type heartbeatEvents*: seq[AsyncEvent] - MeshMetrics* = object - # scratch buffers for metrics + MeshMetrics* = object # scratch buffers for metrics otherPeersPerTopicMesh*: int64 otherPeersPerTopicFanout*: int64 otherPeersPerTopicGossipsub*: int64 diff --git a/libp2p/protocols/pubsub/mcache.nim b/libp2p/protocols/pubsub/mcache.nim index f18ac1cb7c..994d82410a 100644 --- a/libp2p/protocols/pubsub/mcache.nim +++ b/libp2p/protocols/pubsub/mcache.nim @@ -28,8 +28,10 @@ type func get*(c: MCache, msgId: MessageId): Opt[Message] = if msgId in c.msgs: - try: Opt.some(c.msgs[msgId]) - except KeyError: raiseAssert "checked" + try: + Opt.some(c.msgs[msgId]) + except KeyError: + raiseAssert "checked" else: Opt.none(Message) @@ -42,10 +44,9 @@ func put*(c: var MCache, msgId: MessageId, msg: Message) = c.history[c.pos].add(CacheEntry(msgId: msgId, topic: msg.topic)) func window*(c: MCache, topic: string): HashSet[MessageId] = - let - len = min(c.windowSize, c.history.len) + let len = min(c.windowSize, c.history.len) - for i in 0.. 0: - peer.shortAgent - else: - "unknown" + if peer.shortAgent.len > 0: peer.shortAgent else: "unknown" else: "unknown" @@ -124,13 +145,15 @@ func `==`*(a, b: PubSubPeer): bool = a.peerId == b.peerId func shortLog*(p: PubSubPeer): string = - if p.isNil: "PubSubPeer(nil)" - else: shortLog(p.peerId) -chronicles.formatIt(PubSubPeer): shortLog(it) + if p.isNil: + "PubSubPeer(nil)" + else: + shortLog(p.peerId) +chronicles.formatIt(PubSubPeer): + shortLog(it) proc connected*(p: PubSubPeer): bool = - not p.sendConn.isNil and not - (p.sendConn.closed or p.sendConn.atEof) + not p.sendConn.isNil and not (p.sendConn.closed or p.sendConn.atEof) proc hasObservers*(p: PubSubPeer): bool = p.observers != nil and anyIt(p.observers[], it != nil) @@ -140,28 +163,24 @@ func outbound*(p: PubSubPeer): bool = # in order to give priotity to connections we make # https://github.com/libp2p/specs/blob/master/pubsub/gossipsub/gossipsub-v1.1.md#outbound-mesh-quotas # This behaviour is presrcibed to counter sybil attacks and ensures that a coordinated inbound attack can never fully take over the mesh - if not p.sendConn.isNil and p.sendConn.transportDir == Direction.Out: - true - else: - false + if not p.sendConn.isNil and p.sendConn.transportDir == Direction.Out: true else: false proc recvObservers*(p: PubSubPeer, msg: var RPCMsg) = # trigger hooks - if not(isNil(p.observers)) and p.observers[].len > 0: + if not (isNil(p.observers)) and p.observers[].len > 0: for obs in p.observers[]: - if not(isNil(obs)): # TODO: should never be nil, but... + if not (isNil(obs)): # TODO: should never be nil, but... obs.onRecv(p, msg) proc sendObservers(p: PubSubPeer, msg: var RPCMsg) = # trigger hooks - if not(isNil(p.observers)) and p.observers[].len > 0: + if not (isNil(p.observers)) and p.observers[].len > 0: for obs in p.observers[]: - if not(isNil(obs)): # TODO: should never be nil, but... + if not (isNil(obs)): # TODO: should never be nil, but... obs.onSend(p, msg) proc handle*(p: PubSubPeer, conn: Connection) {.async.} = - debug "starting pubsub read loop", - conn, peer = p, closed = conn.closed + debug "starting pubsub read loop", conn, peer = p, closed = conn.closed try: try: while not conn.atEof: @@ -169,13 +188,13 @@ proc handle*(p: PubSubPeer, conn: Connection) {.async.} = var data = await conn.readLp(p.maxMessageSize) trace "read data from peer", - conn, peer = p, closed = conn.closed, - data = data.shortLog + conn, peer = p, closed = conn.closed, data = data.shortLog await p.handler(p, data) data = newSeq[byte]() # Release memory except PeerRateLimitError as exc: - debug "Peer rate limit exceeded, exiting read while", conn, peer = p, error = exc.msg + debug "Peer rate limit exceeded, exiting read while", + conn, peer = p, error = exc.msg except CatchableError as exc: debug "Exception occurred in PubSubPeer.handle", conn, peer = p, closed = conn.closed, exc = exc.msg @@ -189,8 +208,7 @@ proc handle*(p: PubSubPeer, conn: Connection) {.async.} = trace "Exception occurred in PubSubPeer.handle", conn, peer = p, closed = conn.closed, exc = exc.msg finally: - debug "exiting pubsub read loop", - conn, peer = p, closed = conn.closed + debug "exiting pubsub read loop", conn, peer = p, closed = conn.closed proc closeSendConn(p: PubSubPeer, event: PubSubPeerEventKind) {.async.} = if p.sendConn != nil: @@ -229,7 +247,11 @@ proc connectOnce(p: PubSubPeer): Future[void] {.async.} = # to be completed, or onEvent to be called later p.connectedFut.complete() p.sendConn = newConn - p.address = if p.sendConn.observedAddr.isSome: some(p.sendConn.observedAddr.get) else: none(MultiAddress) + p.address = + if p.sendConn.observedAddr.isSome: + some(p.sendConn.observedAddr.get) + else: + none(MultiAddress) if p.onEvent != nil: p.onEvent(p, PubSubPeerEvent(kind: PubSubPeerEventKind.StreamOpened)) @@ -281,8 +303,8 @@ proc clearSendPriorityQueue(p: PubSubPeer) = when defined(pubsubpeer_queue_metrics): libp2p_gossipsub_priority_queue_size.set( - value = p.rpcmessagequeue.sendPriorityQueue.len.int64, - labelValues = [$p.peerId]) + value = p.rpcmessagequeue.sendPriorityQueue.len.int64, labelValues = [$p.peerId] + ) proc sendMsgContinue(conn: Connection, msgFut: Future[void]) {.async.} = # Continuation for a pending `sendMsg` future from below @@ -344,14 +366,17 @@ proc sendEncoded*(p: PubSubPeer, msg: seq[byte], isHighPriority: bool): Future[v # When queues are empty, skipping the non-priority queue for low priority # messages reduces latency let emptyQueues = - (p.rpcmessagequeue.sendPriorityQueue.len() + - p.rpcmessagequeue.nonPriorityQueue.len()) == 0 + ( + p.rpcmessagequeue.sendPriorityQueue.len() + + p.rpcmessagequeue.nonPriorityQueue.len() + ) == 0 if msg.len <= 0: debug "empty message, skipping", p, msg = shortLog(msg) Future[void].completed() elif msg.len > p.maxMessageSize: - info "trying to send a msg too big for pubsub", maxSize=p.maxMessageSize, msgSize=msg.len + info "trying to send a msg too big for pubsub", + maxSize = p.maxMessageSize, msgSize = msg.len Future[void].completed() elif isHighPriority or emptyQueues: let f = p.sendMsg(msg) @@ -374,7 +399,9 @@ proc sendEncoded*(p: PubSubPeer, msg: seq[byte], isHighPriority: bool): Future[v libp2p_gossipsub_non_priority_queue_size.inc(labelValues = [$p.peerId]) f -iterator splitRPCMsg(peer: PubSubPeer, rpcMsg: RPCMsg, maxSize: int, anonymize: bool): seq[byte] = +iterator splitRPCMsg( + peer: PubSubPeer, rpcMsg: RPCMsg, maxSize: int, anonymize: bool +): seq[byte] = ## This iterator takes an `RPCMsg` and sequentially repackages its Messages into new `RPCMsg` instances. ## Each new `RPCMsg` accumulates Messages until reaching the specified `maxSize`. If a single Message ## exceeds the `maxSize` when trying to fit into an empty `RPCMsg`, the latter is skipped as too large to send. @@ -389,7 +416,8 @@ iterator splitRPCMsg(peer: PubSubPeer, rpcMsg: RPCMsg, maxSize: int, anonymize: let msgSize = byteSize(msg) # Check if adding the next message will exceed maxSize - if float(currentSize + msgSize) * 1.1 > float(maxSize): # Guessing 10% protobuf overhead + if float(currentSize + msgSize) * 1.1 > float(maxSize): + # Guessing 10% protobuf overhead if currentRPCMsg.messages.len == 0: trace "message too big to sent", peer, rpcMsg = shortLog(currentRPCMsg) continue # Skip this message @@ -409,7 +437,9 @@ iterator splitRPCMsg(peer: PubSubPeer, rpcMsg: RPCMsg, maxSize: int, anonymize: else: trace "message too big to sent", peer, rpcMsg = shortLog(currentRPCMsg) -proc send*(p: PubSubPeer, msg: RPCMsg, anonymize: bool, isHighPriority: bool) {.raises: [].} = +proc send*( + p: PubSubPeer, msg: RPCMsg, anonymize: bool, isHighPriority: bool +) {.raises: [].} = ## Asynchronously sends an `RPCMsg` to a specified `PubSubPeer` with an option for anonymization. ## ## Parameters: @@ -424,17 +454,18 @@ proc send*(p: PubSubPeer, msg: RPCMsg, anonymize: bool, isHighPriority: bool) {. # or malicious data on the wire - in particular, re-encoding protects against # some forms of valid but redundantly encoded protobufs with unknown or # duplicated fields - let encoded = if p.hasObservers(): - var mm = msg - # trigger send hooks - p.sendObservers(mm) - sendMetrics(mm) - encodeRpcMsg(mm, anonymize) - else: - # If there are no send hooks, we redundantly re-encode the message to - # protobuf for every peer - this could easily be improved! - sendMetrics(msg) - encodeRpcMsg(msg, anonymize) + let encoded = + if p.hasObservers(): + var mm = msg + # trigger send hooks + p.sendObservers(mm) + sendMetrics(mm) + encodeRpcMsg(mm, anonymize) + else: + # If there are no send hooks, we redundantly re-encode the message to + # protobuf for every peer - this could easily be improved! + sendMetrics(msg) + encodeRpcMsg(msg, anonymize) if encoded.len > p.maxMessageSize and msg.messages.len > 1: for encodedSplitMsg in splitRPCMsg(p, msg, p.maxMessageSize, anonymize): @@ -453,21 +484,21 @@ proc canAskIWant*(p: PubSubPeer, msgId: MessageId): bool = proc sendNonPriorityTask(p: PubSubPeer) {.async.} = while true: - # we send non-priority messages only if there are no pending priority messages - let msg = await p.rpcmessagequeue.nonPriorityQueue.popFirst() - while p.rpcmessagequeue.sendPriorityQueue.len > 0: - p.clearSendPriorityQueue() - # waiting for the last future minimizes the number of times we have to - # wait for something (each wait = performance cost) - - # clearSendPriorityQueue ensures we're not waiting for an already-finished - # future - if p.rpcmessagequeue.sendPriorityQueue.len > 0: + # we send non-priority messages only if there are no pending priority messages + let msg = await p.rpcmessagequeue.nonPriorityQueue.popFirst() + while p.rpcmessagequeue.sendPriorityQueue.len > 0: + p.clearSendPriorityQueue() + # waiting for the last future minimizes the number of times we have to + # wait for something (each wait = performance cost) - + # clearSendPriorityQueue ensures we're not waiting for an already-finished + # future + if p.rpcmessagequeue.sendPriorityQueue.len > 0: # `race` prevents `p.rpcmessagequeue.sendPriorityQueue[^1]` from being # cancelled when this task is cancelled discard await race(p.rpcmessagequeue.sendPriorityQueue[^1]) - when defined(pubsubpeer_queue_metrics): - libp2p_gossipsub_non_priority_queue_size.dec(labelValues = [$p.peerId]) - await p.sendMsg(msg) + when defined(pubsubpeer_queue_metrics): + libp2p_gossipsub_non_priority_queue_size.dec(labelValues = [$p.peerId]) + await p.sendMsg(msg) proc startSendNonPriorityTask(p: PubSubPeer) = debug "starting sendNonPriorityTask", p @@ -489,19 +520,19 @@ proc stopSendNonPriorityTask*(p: PubSubPeer) = proc new(T: typedesc[RpcMessageQueue]): T = return T( sendPriorityQueue: initDeque[Future[void]](), - nonPriorityQueue: newAsyncQueue[seq[byte]]() + nonPriorityQueue: newAsyncQueue[seq[byte]](), ) proc new*( - T: typedesc[PubSubPeer], - peerId: PeerId, - getConn: GetConn, - onEvent: OnEvent, - codec: string, - maxMessageSize: int, - maxNumElementsInNonPriorityQueue: int = DefaultMaxNumElementsInNonPriorityQueue, - overheadRateLimitOpt: Opt[TokenBucket] = Opt.none(TokenBucket)): T = - + T: typedesc[PubSubPeer], + peerId: PeerId, + getConn: GetConn, + onEvent: OnEvent, + codec: string, + maxMessageSize: int, + maxNumElementsInNonPriorityQueue: int = DefaultMaxNumElementsInNonPriorityQueue, + overheadRateLimitOpt: Opt[TokenBucket] = Opt.none(TokenBucket), +): T = result = T( getConn: getConn, onEvent: onEvent, @@ -511,7 +542,7 @@ proc new*( maxMessageSize: maxMessageSize, overheadRateLimitOpt: overheadRateLimitOpt, rpcmessagequeue: RpcMessageQueue.new(), - maxNumElementsInNonPriorityQueue: maxNumElementsInNonPriorityQueue + maxNumElementsInNonPriorityQueue: maxNumElementsInNonPriorityQueue, ) result.sentIHaves.addFirst(default(HashSet[MessageId])) result.heDontWants.addFirst(default(HashSet[SaltedId])) diff --git a/libp2p/protocols/pubsub/rpc/message.nim b/libp2p/protocols/pubsub/rpc/message.nim index 05215d7c4f..79d1e7984b 100644 --- a/libp2p/protocols/pubsub/rpc/message.nim +++ b/libp2p/protocols/pubsub/rpc/message.nim @@ -10,13 +10,14 @@ {.push raises: [].} import chronicles, metrics, stew/[byteutils, endians2] -import ./messages, - ./protobuf, - ../../../peerid, - ../../../peerinfo, - ../../../crypto/crypto, - ../../../protobuf/minprotobuf, - ../../../protocols/pubsub/errors +import + ./messages, + ./protobuf, + ../../../peerid, + ../../../peerinfo, + ../../../crypto/crypto, + ../../../protobuf/minprotobuf, + ../../../protocols/pubsub/errors export errors, messages @@ -25,7 +26,9 @@ logScope: const PubSubPrefix = toBytes("libp2p-pubsub:") -declareCounter(libp2p_pubsub_sig_verify_success, "pubsub successfully validated messages") +declareCounter( + libp2p_pubsub_sig_verify_success, "pubsub successfully validated messages" +) declareCounter(libp2p_pubsub_sig_verify_failure, "pubsub failed validated messages") func defaultMsgIdProvider*(m: Message): Result[MessageId, ValidationResult] = @@ -36,7 +39,7 @@ func defaultMsgIdProvider*(m: Message): Result[MessageId, ValidationResult] = err ValidationResult.Reject proc sign*(msg: Message, privateKey: PrivateKey): CryptoResult[seq[byte]] = - ok((? privateKey.sign(PubSubPrefix & encodeMessage(msg, false))).getBytes()) + ok((?privateKey.sign(PubSubPrefix & encodeMessage(msg, false))).getBytes()) proc verify*(m: Message): bool = if m.signature.len > 0 and m.key.len > 0: @@ -61,8 +64,8 @@ proc init*( data: seq[byte], topic: string, seqno: Option[uint64], - sign: bool = true): Message - {.gcsafe, raises: [LPError].} = + sign: bool = true, +): Message {.gcsafe, raises: [LPError].} = var msg = Message(data: data, topic: topic) # order matters, we want to include seqno in the signature @@ -73,10 +76,14 @@ proc init*( msg.fromPeer = peer.peerId if sign: msg.signature = sign(msg, peer.privateKey).expect("Couldn't sign message!") - msg.key = peer.privateKey.getPublicKey().expect("Invalid private key!") - .getBytes().expect("Couldn't get public key bytes!") + msg.key = peer.privateKey + .getPublicKey() + .expect("Invalid private key!") + .getBytes() + .expect("Couldn't get public key bytes!") else: - if sign: raise (ref LPError)(msg: "Cannot sign message without peer info") + if sign: + raise (ref LPError)(msg: "Cannot sign message without peer info") msg @@ -85,8 +92,8 @@ proc init*( peerId: PeerId, data: seq[byte], topic: string, - seqno: Option[uint64]): Message - {.gcsafe, raises: [LPError].} = + seqno: Option[uint64], +): Message {.gcsafe, raises: [LPError].} = var msg = Message(data: data, topic: topic) msg.fromPeer = peerId diff --git a/libp2p/protocols/pubsub/rpc/messages.nim b/libp2p/protocols/pubsub/rpc/messages.nim index e1e590e98a..e19817e7e1 100644 --- a/libp2p/protocols/pubsub/rpc/messages.nim +++ b/libp2p/protocols/pubsub/rpc/messages.nim @@ -10,21 +10,27 @@ {.push raises: [].} import options, sequtils -import ../../../[ - peerid, - routing_record, - utility - ] +import ../../../[peerid, routing_record, utility] export options -proc expectedFields[T](t: typedesc[T], existingFieldNames: seq[string]) {.raises: [CatchableError].} = +proc expectedFields[T]( + t: typedesc[T], existingFieldNames: seq[string] +) {.raises: [CatchableError].} = var fieldNames: seq[string] for name, _ in fieldPairs(T()): fieldNames &= name if fieldNames != existingFieldNames: - fieldNames.keepIf(proc(it: string): bool = it notin existingFieldNames) - raise newException(CatchableError, $T & " fields changed, please search for and revise all relevant procs. New fields: " & $fieldNames) + fieldNames.keepIf( + proc(it: string): bool = + it notin existingFieldNames + ) + raise newException( + CatchableError, + $T & + " fields changed, please search for and revise all relevant procs. New fields: " & + $fieldNames, + ) type PeerInfoMsg* = object @@ -80,38 +86,27 @@ type ping*: seq[byte] pong*: seq[byte] -func withSubs*( - T: type RPCMsg, topics: openArray[string], subscribe: bool): T = - T( - subscriptions: topics.mapIt(SubOpts(subscribe: subscribe, topic: it))) +func withSubs*(T: type RPCMsg, topics: openArray[string], subscribe: bool): T = + T(subscriptions: topics.mapIt(SubOpts(subscribe: subscribe, topic: it))) func shortLog*(s: ControlIHave): auto = - ( - topic: s.topicID.shortLog, - messageIDs: mapIt(s.messageIDs, it.shortLog) - ) + (topic: s.topicID.shortLog, messageIDs: mapIt(s.messageIDs, it.shortLog)) func shortLog*(s: ControlIWant): auto = - ( - messageIDs: mapIt(s.messageIDs, it.shortLog) - ) + (messageIDs: mapIt(s.messageIDs, it.shortLog)) func shortLog*(s: ControlGraft): auto = - ( - topic: s.topicID.shortLog - ) + (topic: s.topicID.shortLog) func shortLog*(s: ControlPrune): auto = - ( - topic: s.topicID.shortLog - ) + (topic: s.topicID.shortLog) func shortLog*(c: ControlMessage): auto = ( ihave: mapIt(c.ihave, it.shortLog), iwant: mapIt(c.iwant, it.shortLog), graft: mapIt(c.graft, it.shortLog), - prune: mapIt(c.prune, it.shortLog) + prune: mapIt(c.prune, it.shortLog), ) func shortLog*(msg: Message): auto = @@ -121,25 +116,28 @@ func shortLog*(msg: Message): auto = seqno: msg.seqno.shortLog, topic: msg.topic, signature: msg.signature.shortLog, - key: msg.key.shortLog + key: msg.key.shortLog, ) func shortLog*(m: RPCMsg): auto = ( subscriptions: m.subscriptions, messages: mapIt(m.messages, it.shortLog), - control: m.control.get(ControlMessage()).shortLog + control: m.control.get(ControlMessage()).shortLog, ) -static: expectedFields(PeerInfoMsg, @["peerId", "signedPeerRecord"]) +static: + expectedFields(PeerInfoMsg, @["peerId", "signedPeerRecord"]) proc byteSize(peerInfo: PeerInfoMsg): int = peerInfo.peerId.len + peerInfo.signedPeerRecord.len -static: expectedFields(SubOpts, @["subscribe", "topic"]) +static: + expectedFields(SubOpts, @["subscribe", "topic"]) proc byteSize(subOpts: SubOpts): int = 1 + subOpts.topic.len # 1 byte for the bool -static: expectedFields(Message, @["fromPeer", "data", "seqno", "topic", "signature", "key"]) +static: + expectedFields(Message, @["fromPeer", "data", "seqno", "topic", "signature", "key"]) proc byteSize*(msg: Message): int = msg.fromPeer.len + msg.data.len + msg.seqno.len + msg.signature.len + msg.key.len + msg.topic.len @@ -147,37 +145,45 @@ proc byteSize*(msg: Message): int = proc byteSize*(msgs: seq[Message]): int = msgs.foldl(a + b.byteSize, 0) -static: expectedFields(ControlIHave, @["topicID", "messageIDs"]) +static: + expectedFields(ControlIHave, @["topicID", "messageIDs"]) proc byteSize(controlIHave: ControlIHave): int = controlIHave.topicID.len + controlIHave.messageIDs.foldl(a + b.len, 0) proc byteSize*(ihaves: seq[ControlIHave]): int = ihaves.foldl(a + b.byteSize, 0) -static: expectedFields(ControlIWant, @["messageIDs"]) +static: + expectedFields(ControlIWant, @["messageIDs"]) proc byteSize(controlIWant: ControlIWant): int = controlIWant.messageIDs.foldl(a + b.len, 0) proc byteSize*(iwants: seq[ControlIWant]): int = iwants.foldl(a + b.byteSize, 0) -static: expectedFields(ControlGraft, @["topicID"]) +static: + expectedFields(ControlGraft, @["topicID"]) proc byteSize(controlGraft: ControlGraft): int = controlGraft.topicID.len -static: expectedFields(ControlPrune, @["topicID", "peers", "backoff"]) +static: + expectedFields(ControlPrune, @["topicID", "peers", "backoff"]) proc byteSize(controlPrune: ControlPrune): int = - controlPrune.topicID.len + controlPrune.peers.foldl(a + b.byteSize, 0) + 8 # 8 bytes for uint64 + controlPrune.topicID.len + controlPrune.peers.foldl(a + b.byteSize, 0) + 8 + # 8 bytes for uint64 -static: expectedFields(ControlMessage, @["ihave", "iwant", "graft", "prune", "idontwant"]) +static: + expectedFields(ControlMessage, @["ihave", "iwant", "graft", "prune", "idontwant"]) proc byteSize(control: ControlMessage): int = control.ihave.foldl(a + b.byteSize, 0) + control.iwant.foldl(a + b.byteSize, 0) + - control.graft.foldl(a + b.byteSize, 0) + control.prune.foldl(a + b.byteSize, 0) + - control.idontwant.foldl(a + b.byteSize, 0) + control.graft.foldl(a + b.byteSize, 0) + control.prune.foldl(a + b.byteSize, 0) + + control.idontwant.foldl(a + b.byteSize, 0) -static: expectedFields(RPCMsg, @["subscriptions", "messages", "control", "ping", "pong"]) +static: + expectedFields(RPCMsg, @["subscriptions", "messages", "control", "ping", "pong"]) proc byteSize*(rpc: RPCMsg): int = - result = rpc.subscriptions.foldl(a + b.byteSize, 0) + byteSize(rpc.messages) + - rpc.ping.len + rpc.pong.len + result = + rpc.subscriptions.foldl(a + b.byteSize, 0) + byteSize(rpc.messages) + rpc.ping.len + + rpc.pong.len rpc.control.withValue(ctrl): result += ctrl.byteSize diff --git a/libp2p/protocols/pubsub/rpc/protobuf.nim b/libp2p/protocols/pubsub/rpc/protobuf.nim index 03e2d3b953..d093a0c8fc 100644 --- a/libp2p/protocols/pubsub/rpc/protobuf.nim +++ b/libp2p/protocols/pubsub/rpc/protobuf.nim @@ -12,11 +12,7 @@ import options import stew/assign2 import chronicles -import messages, - ../../../peerid, - ../../../utility, - ../../../protobuf/minprotobuf - +import messages, ../../../peerid, ../../../utility, ../../../protobuf/minprotobuf logScope: topics = "libp2p pubsubprotobuf" @@ -24,8 +20,12 @@ logScope: when defined(libp2p_protobuf_metrics): import metrics - declareCounter(libp2p_pubsub_rpc_bytes_read, "pubsub rpc bytes read", labels = ["kind"]) - declareCounter(libp2p_pubsub_rpc_bytes_write, "pubsub rpc bytes write", labels = ["kind"]) + declareCounter( + libp2p_pubsub_rpc_bytes_read, "pubsub rpc bytes read", labels = ["kind"] + ) + declareCounter( + libp2p_pubsub_rpc_bytes_write, "pubsub rpc bytes write", labels = ["kind"] + ) proc write*(pb: var ProtoBuffer, field: int, graft: ControlGraft) = var ipb = initProtoBuffer() @@ -125,64 +125,61 @@ proc encodeMessage*(msg: Message, anonymize: bool): seq[byte] = proc write*(pb: var ProtoBuffer, field: int, msg: Message, anonymize: bool) = pb.write(field, encodeMessage(msg, anonymize)) -proc decodeGraft*(pb: ProtoBuffer): ProtoResult[ControlGraft] {. - inline.} = +proc decodeGraft*(pb: ProtoBuffer): ProtoResult[ControlGraft] {.inline.} = when defined(libp2p_protobuf_metrics): libp2p_pubsub_rpc_bytes_read.inc(pb.getLen().int64, labelValues = ["graft"]) trace "decodeGraft: decoding message" var control = ControlGraft() - if ? pb.getField(1, control.topicID): + if ?pb.getField(1, control.topicID): trace "decodeGraft: read topicID", topicID = control.topicID else: trace "decodeGraft: topicID is missing" ok(control) -proc decodePeerInfoMsg*(pb: ProtoBuffer): ProtoResult[PeerInfoMsg] {. - inline.} = +proc decodePeerInfoMsg*(pb: ProtoBuffer): ProtoResult[PeerInfoMsg] {.inline.} = trace "decodePeerInfoMsg: decoding message" var pi = PeerInfoMsg() - if ? pb.getField(1, pi.peerId): + if ?pb.getField(1, pi.peerId): trace "decodePeerInfoMsg: read peerId", peerId = pi.peerId else: trace "decodePeerInfoMsg: peerId is missing" - if ? pb.getField(2, pi.signedPeerRecord): - trace "decodePeerInfoMsg: read signedPeerRecord", signedPeerRecord = pi.signedPeerRecord + if ?pb.getField(2, pi.signedPeerRecord): + trace "decodePeerInfoMsg: read signedPeerRecord", + signedPeerRecord = pi.signedPeerRecord else: trace "decodePeerInfoMsg: signedPeerRecord is missing" ok(pi) -proc decodePrune*(pb: ProtoBuffer): ProtoResult[ControlPrune] {. - inline.} = +proc decodePrune*(pb: ProtoBuffer): ProtoResult[ControlPrune] {.inline.} = when defined(libp2p_protobuf_metrics): libp2p_pubsub_rpc_bytes_read.inc(pb.getLen().int64, labelValues = ["prune"]) trace "decodePrune: decoding message" var control = ControlPrune() - if ? pb.getField(1, control.topicID): + if ?pb.getField(1, control.topicID): trace "decodePrune: read topicID", topic = control.topicID else: trace "decodePrune: topicID is missing" var bpeers: seq[seq[byte]] - if ? pb.getRepeatedField(2, bpeers): + if ?pb.getRepeatedField(2, bpeers): for bpeer in bpeers: - control.peers &= ? decodePeerInfoMsg(initProtoBuffer(bpeer)) - if ? pb.getField(3, control.backoff): + control.peers &= ?decodePeerInfoMsg(initProtoBuffer(bpeer)) + if ?pb.getField(3, control.backoff): trace "decodePrune: read backoff", backoff = control.backoff ok(control) -proc decodeIHave*(pb: ProtoBuffer): ProtoResult[ControlIHave] {. - inline.} = +proc decodeIHave*(pb: ProtoBuffer): ProtoResult[ControlIHave] {.inline.} = when defined(libp2p_protobuf_metrics): libp2p_pubsub_rpc_bytes_read.inc(pb.getLen().int64, labelValues = ["ihave"]) trace "decodeIHave: decoding message" var control = ControlIHave() - if ? pb.getField(1, control.topicID): + if ?pb.getField(1, control.topicID): trace "decodeIHave: read topicID", topic = control.topicID else: trace "decodeIHave: topicID is missing" - if ? pb.getRepeatedField(2, control.messageIDs): + if ?pb.getRepeatedField(2, control.messageIDs): trace "decodeIHave: read messageIDs", message_ids = control.messageIDs else: trace "decodeIHave: no messageIDs" @@ -194,17 +191,16 @@ proc decodeIWant*(pb: ProtoBuffer): ProtoResult[ControlIWant] {.inline.} = trace "decodeIWant: decoding message" var control = ControlIWant() - if ? pb.getRepeatedField(1, control.messageIDs): + if ?pb.getRepeatedField(1, control.messageIDs): trace "decodeIWant: read messageIDs", message_ids = control.messageIDs else: trace "decodeIWant: no messageIDs" ok(control) -proc decodeControl*(pb: ProtoBuffer): ProtoResult[Option[ControlMessage]] {. - inline.} = +proc decodeControl*(pb: ProtoBuffer): ProtoResult[Option[ControlMessage]] {.inline.} = trace "decodeControl: decoding message" var buffer: seq[byte] - if ? pb.getField(3, buffer): + if ?pb.getField(3, buffer): var control: ControlMessage var cpb = initProtoBuffer(buffer) var ihavepbs: seq[seq[byte]] @@ -212,25 +208,26 @@ proc decodeControl*(pb: ProtoBuffer): ProtoResult[Option[ControlMessage]] {. var graftpbs: seq[seq[byte]] var prunepbs: seq[seq[byte]] var idontwant: seq[seq[byte]] - if ? cpb.getRepeatedField(1, ihavepbs): + if ?cpb.getRepeatedField(1, ihavepbs): for item in ihavepbs: - control.ihave.add(? decodeIHave(initProtoBuffer(item))) - if ? cpb.getRepeatedField(2, iwantpbs): + control.ihave.add(?decodeIHave(initProtoBuffer(item))) + if ?cpb.getRepeatedField(2, iwantpbs): for item in iwantpbs: - control.iwant.add(? decodeIWant(initProtoBuffer(item))) - if ? cpb.getRepeatedField(3, graftpbs): + control.iwant.add(?decodeIWant(initProtoBuffer(item))) + if ?cpb.getRepeatedField(3, graftpbs): for item in graftpbs: - control.graft.add(? decodeGraft(initProtoBuffer(item))) - if ? cpb.getRepeatedField(4, prunepbs): + control.graft.add(?decodeGraft(initProtoBuffer(item))) + if ?cpb.getRepeatedField(4, prunepbs): for item in prunepbs: - control.prune.add(? decodePrune(initProtoBuffer(item))) - if ? cpb.getRepeatedField(5, idontwant): + control.prune.add(?decodePrune(initProtoBuffer(item))) + if ?cpb.getRepeatedField(5, idontwant): for item in idontwant: - control.idontwant.add(? decodeIWant(initProtoBuffer(item))) - trace "decodeControl: message statistics", graft_count = len(control.graft), - prune_count = len(control.prune), - ihave_count = len(control.ihave), - iwant_count = len(control.iwant) + control.idontwant.add(?decodeIWant(initProtoBuffer(item))) + trace "decodeControl: message statistics", + graft_count = len(control.graft), + prune_count = len(control.prune), + ihave_count = len(control.ihave), + iwant_count = len(control.iwant) ok(some(control)) else: ok(none[ControlMessage]()) @@ -242,27 +239,26 @@ proc decodeSubscription*(pb: ProtoBuffer): ProtoResult[SubOpts] {.inline.} = trace "decodeSubscription: decoding message" var subflag: uint64 var sub = SubOpts() - if ? pb.getField(1, subflag): + if ?pb.getField(1, subflag): sub.subscribe = bool(subflag) trace "decodeSubscription: read subscribe", subscribe = subflag else: trace "decodeSubscription: subscribe is missing" - if ? pb.getField(2, sub.topic): + if ?pb.getField(2, sub.topic): trace "decodeSubscription: read topic", topic = sub.topic else: trace "decodeSubscription: topic is missing" ok(sub) -proc decodeSubscriptions*(pb: ProtoBuffer): ProtoResult[seq[SubOpts]] {. - inline.} = +proc decodeSubscriptions*(pb: ProtoBuffer): ProtoResult[seq[SubOpts]] {.inline.} = trace "decodeSubscriptions: decoding message" var subpbs: seq[seq[byte]] var subs: seq[SubOpts] - let res = ? pb.getRepeatedField(1, subpbs) + let res = ?pb.getRepeatedField(1, subpbs) if res: trace "decodeSubscriptions: read subscriptions", count = len(subpbs) for item in subpbs: - subs.add(? decodeSubscription(initProtoBuffer(item))) + subs.add(?decodeSubscription(initProtoBuffer(item))) if len(subs) == 0: trace "decodeSubscription: no subscriptions found" ok(subs) @@ -273,15 +269,15 @@ proc decodeMessage*(pb: ProtoBuffer): ProtoResult[Message] {.inline.} = trace "decodeMessage: decoding message" var msg: Message - if ? pb.getField(1, msg.fromPeer): + if ?pb.getField(1, msg.fromPeer): trace "decodeMessage: read fromPeer", fromPeer = msg.fromPeer else: trace "decodeMessage: fromPeer is missing" - if ? pb.getField(2, msg.data): + if ?pb.getField(2, msg.data): trace "decodeMessage: read data", data = msg.data.shortLog() else: trace "decodeMessage: data is missing" - if ? pb.getField(3, msg.seqno): + if ?pb.getField(3, msg.seqno): trace "decodeMessage: read seqno", seqno = msg.seqno else: trace "decodeMessage: seqno is missing" @@ -290,11 +286,11 @@ proc decodeMessage*(pb: ProtoBuffer): ProtoResult[Message] {.inline.} = else: trace "decodeMessage: topic is required" return err(ProtoError.RequiredFieldMissing) - if ? pb.getField(5, msg.signature): + if ?pb.getField(5, msg.signature): trace "decodeMessage: read signature", signature = msg.signature.shortLog() else: trace "decodeMessage: signature is missing" - if ? pb.getField(6, msg.key): + if ?pb.getField(6, msg.key): trace "decodeMessage: read public key", key = msg.key.shortLog() else: trace "decodeMessage: public key is missing" @@ -304,11 +300,11 @@ proc decodeMessages*(pb: ProtoBuffer): ProtoResult[seq[Message]] {.inline.} = trace "decodeMessages: decoding message" var msgpbs: seq[seq[byte]] var msgs: seq[Message] - if ? pb.getRepeatedField(2, msgpbs): + if ?pb.getRepeatedField(2, msgpbs): trace "decodeMessages: read messages", count = len(msgpbs) for item in msgpbs: # size is constrained at the network level - msgs.add(? decodeMessage(initProtoBuffer(item, maxSize = uint.high))) + msgs.add(?decodeMessage(initProtoBuffer(item, maxSize = uint.high))) else: trace "decodeMessages: no messages found" ok(msgs) @@ -336,9 +332,9 @@ proc decodeRpcMsg*(msg: seq[byte]): ProtoResult[RPCMsg] {.inline.} = trace "decodeRpcMsg: decoding message", msg = msg.shortLog() var pb = initProtoBuffer(msg, maxSize = uint.high) var rpcMsg = RPCMsg() - assign(rpcMsg.messages, ? pb.decodeMessages()) - assign(rpcMsg.subscriptions, ? pb.decodeSubscriptions()) - assign(rpcMsg.control, ? pb.decodeControl()) - discard ? pb.getField(60, rpcMsg.ping) - discard ? pb.getField(61, rpcMsg.pong) + assign(rpcMsg.messages, ?pb.decodeMessages()) + assign(rpcMsg.subscriptions, ?pb.decodeSubscriptions()) + assign(rpcMsg.control, ?pb.decodeControl()) + discard ?pb.getField(60, rpcMsg.ping) + discard ?pb.getField(61, rpcMsg.pong) ok(rpcMsg) diff --git a/libp2p/protocols/pubsub/timedcache.nim b/libp2p/protocols/pubsub/timedcache.nim index ca08f0aef4..4a55a33d4e 100644 --- a/libp2p/protocols/pubsub/timedcache.nim +++ b/libp2p/protocols/pubsub/timedcache.nim @@ -47,23 +47,29 @@ func expire*(t: var TimedCache, now: Moment = Moment.now()) = t.entries.excl(t.head) t.head.prev = nil t.head = t.head.next - if t.head == nil: t.tail = nil + if t.head == nil: + t.tail = nil func del*[K](t: var TimedCache[K], key: K): Opt[TimedEntry[K]] = # Removes existing key from cache, returning the previous value if present let tmp = TimedEntry[K](key: key) if tmp in t.entries: - let item = try: - t.entries[tmp] # use the shared instance in the set - except KeyError: - raiseAssert "just checked" + let item = + try: + t.entries[tmp] # use the shared instance in the set + except KeyError: + raiseAssert "just checked" t.entries.excl(item) - if t.head == item: t.head = item.next - if t.tail == item: t.tail = item.prev + if t.head == item: + t.head = item.next + if t.tail == item: + t.tail = item.prev - if item.next != nil: item.next.prev = item.prev - if item.prev != nil: item.prev.next = item.next + if item.next != nil: + item.next.prev = item.prev + if item.prev != nil: + item.prev.next = item.next Opt.some(item) else: Opt.none(TimedEntry[K]) @@ -76,10 +82,11 @@ func put*[K](t: var TimedCache[K], k: K, now = Moment.now()): bool = let previous = t.del(k) # Refresh existing item - addedAt = if previous.isSome(): - previous[].addedAt - else: - now + addedAt = + if previous.isSome(): + previous[].addedAt + else: + now let node = TimedEntry[K](key: k, addedAt: addedAt, expiresAt: now + t.timeout) if t.head == nil: @@ -122,6 +129,4 @@ func addedAt*[K](t: var TimedCache[K], k: K): Moment = default(Moment) func init*[K](T: type TimedCache[K], timeout: Duration = Timeout): T = - T( - timeout: timeout - ) + T(timeout: timeout) diff --git a/libp2p/protocols/rendezvous.nim b/libp2p/protocols/rendezvous.nim index f638a6b395..5733579fa3 100644 --- a/libp2p/protocols/rendezvous.nim +++ b/libp2p/protocols/rendezvous.nim @@ -11,17 +11,15 @@ import tables, sequtils, sugar, sets import metrics except collect -import chronos, - chronicles, - bearssl/rand, - stew/[byteutils, objects, results] -import ./protocol, - ../switch, - ../routing_record, - ../utils/heartbeat, - ../stream/connection, - ../utils/offsettedseq, - ../utils/semaphore +import chronos, chronicles, bearssl/rand, stew/[byteutils, objects, results] +import + ./protocol, + ../switch, + ../routing_record, + ../utils/heartbeat, + ../stream/connection, + ../utils/offsettedseq, + ../utils/semaphore export chronicles @@ -62,11 +60,11 @@ type Unavailable = 400 Cookie = object - offset : uint64 - ns : string + offset: uint64 + ns: string Register = object - ns : string + ns: string signedPeerRecord: seq[byte] ttl: Opt[uint64] # in seconds @@ -166,7 +164,8 @@ proc decode(_: typedesc[Cookie], buf: seq[byte]): Opt[Cookie] = pb = initProtoBuffer(buf) r1 = pb.getRequiredField(1, c.offset) r2 = pb.getRequiredField(2, c.ns) - if r1.isErr() or r2.isErr(): return Opt.none(Cookie) + if r1.isErr() or r2.isErr(): + return Opt.none(Cookie) Opt.some(c) proc decode(_: typedesc[Register], buf: seq[byte]): Opt[Register] = @@ -178,8 +177,10 @@ proc decode(_: typedesc[Register], buf: seq[byte]): Opt[Register] = r1 = pb.getRequiredField(1, r.ns) r2 = pb.getRequiredField(2, r.signedPeerRecord) r3 = pb.getField(3, ttl) - if r1.isErr() or r2.isErr() or r3.isErr(): return Opt.none(Register) - if r3.get(false): r.ttl = Opt.some(ttl) + if r1.isErr() or r2.isErr() or r3.isErr(): + return Opt.none(Register) + if r3.get(false): + r.ttl = Opt.some(ttl) Opt.some(r) proc decode(_: typedesc[RegisterResponse], buf: seq[byte]): Opt[RegisterResponse] = @@ -194,9 +195,12 @@ proc decode(_: typedesc[RegisterResponse], buf: seq[byte]): Opt[RegisterResponse r2 = pb.getField(2, text) r3 = pb.getField(3, ttl) if r1.isErr() or r2.isErr() or r3.isErr() or - not checkedEnumAssign(rr.status, statusOrd): return Opt.none(RegisterResponse) - if r2.get(false): rr.text = Opt.some(text) - if r3.get(false): rr.ttl = Opt.some(ttl) + not checkedEnumAssign(rr.status, statusOrd): + return Opt.none(RegisterResponse) + if r2.get(false): + rr.text = Opt.some(text) + if r3.get(false): + rr.ttl = Opt.some(ttl) Opt.some(rr) proc decode(_: typedesc[Unregister], buf: seq[byte]): Opt[Unregister] = @@ -204,7 +208,8 @@ proc decode(_: typedesc[Unregister], buf: seq[byte]): Opt[Unregister] = let pb = initProtoBuffer(buf) r1 = pb.getRequiredField(1, u.ns) - if r1.isErr(): return Opt.none(Unregister) + if r1.isErr(): + return Opt.none(Unregister) Opt.some(u) proc decode(_: typedesc[Discover], buf: seq[byte]): Opt[Discover] = @@ -217,9 +222,12 @@ proc decode(_: typedesc[Discover], buf: seq[byte]): Opt[Discover] = r1 = pb.getRequiredField(1, d.ns) r2 = pb.getField(2, limit) r3 = pb.getField(3, cookie) - if r1.isErr() or r2.isErr() or r3.isErr: return Opt.none(Discover) - if r2.get(false): d.limit = Opt.some(limit) - if r3.get(false): d.cookie = Opt.some(cookie) + if r1.isErr() or r2.isErr() or r3.isErr: + return Opt.none(Discover) + if r2.get(false): + d.limit = Opt.some(limit) + if r3.get(false): + d.cookie = Opt.some(cookie) Opt.some(d) proc decode(_: typedesc[DiscoverResponse], buf: seq[byte]): Opt[DiscoverResponse] = @@ -236,14 +244,17 @@ proc decode(_: typedesc[DiscoverResponse], buf: seq[byte]): Opt[DiscoverResponse r3 = pb.getRequiredField(3, statusOrd) r4 = pb.getField(4, text) if r1.isErr() or r2.isErr() or r3.isErr or r4.isErr() or - not checkedEnumAssign(dr.status, statusOrd): return Opt.none(DiscoverResponse) + not checkedEnumAssign(dr.status, statusOrd): + return Opt.none(DiscoverResponse) for reg in registrations: var r: Register let regOpt = Register.decode(reg).valueOr: return dr.registrations.add(regOpt) - if r2.get(false): dr.cookie = Opt.some(cookie) - if r4.get(false): dr.text = Opt.some(text) + if r2.get(false): + dr.cookie = Opt.some(cookie) + if r4.get(false): + dr.text = Opt.some(text) Opt.some(dr) proc decode(_: typedesc[Message], buf: seq[byte]): Opt[Message] = @@ -253,32 +264,37 @@ proc decode(_: typedesc[Message], buf: seq[byte]): Opt[Message] = pbr, pbrr, pbu, pbd, pbdr: ProtoBuffer let pb = initProtoBuffer(buf) - ? pb.getRequiredField(1, statusOrd).toOpt - if not checkedEnumAssign(msg.msgType, statusOrd): return Opt.none(Message) + ?pb.getRequiredField(1, statusOrd).toOpt + if not checkedEnumAssign(msg.msgType, statusOrd): + return Opt.none(Message) - if ? pb.getField(2, pbr).optValue: + if ?pb.getField(2, pbr).optValue: msg.register = Register.decode(pbr.buffer) - if msg.register.isNone(): return Opt.none(Message) + if msg.register.isNone(): + return Opt.none(Message) - if ? pb.getField(3, pbrr).optValue: + if ?pb.getField(3, pbrr).optValue: msg.registerResponse = RegisterResponse.decode(pbrr.buffer) - if msg.registerResponse.isNone(): return Opt.none(Message) + if msg.registerResponse.isNone(): + return Opt.none(Message) - if ? pb.getField(4, pbu).optValue: + if ?pb.getField(4, pbu).optValue: msg.unregister = Unregister.decode(pbu.buffer) - if msg.unregister.isNone(): return Opt.none(Message) + if msg.unregister.isNone(): + return Opt.none(Message) - if ? pb.getField(5, pbd).optValue: + if ?pb.getField(5, pbd).optValue: msg.discover = Discover.decode(pbd.buffer) - if msg.discover.isNone(): return Opt.none(Message) + if msg.discover.isNone(): + return Opt.none(Message) - if ? pb.getField(6, pbdr).optValue: + if ?pb.getField(6, pbdr).optValue: msg.discoverResponse = DiscoverResponse.decode(pbdr.buffer) - if msg.discoverResponse.isNone(): return Opt.none(Message) + if msg.discoverResponse.isNone(): + return Opt.none(Message) Opt.some(msg) - type RendezVousError* = object of LPError RegisteredData = object @@ -306,46 +322,57 @@ type switch: Switch proc checkPeerRecord(spr: seq[byte], peerId: PeerId): Result[void, string] = - if spr.len == 0: return err("Empty peer record") - let signedEnv = ? SignedPeerRecord.decode(spr).mapErr(x => $x) + if spr.len == 0: + return err("Empty peer record") + let signedEnv = ?SignedPeerRecord.decode(spr).mapErr(x => $x) if signedEnv.data.peerId != peerId: return err("Bad Peer ID") return ok() -proc sendRegisterResponse(conn: Connection, - ttl: uint64) {.async.} = - let msg = encode(Message( - msgType: MessageType.RegisterResponse, - registerResponse: Opt.some(RegisterResponse(status: Ok, ttl: Opt.some(ttl))))) +proc sendRegisterResponse(conn: Connection, ttl: uint64) {.async.} = + let msg = encode( + Message( + msgType: MessageType.RegisterResponse, + registerResponse: Opt.some(RegisterResponse(status: Ok, ttl: Opt.some(ttl))), + ) + ) await conn.writeLp(msg.buffer) -proc sendRegisterResponseError(conn: Connection, - status: ResponseStatus, - text: string = "") {.async.} = - let msg = encode(Message( - msgType: MessageType.RegisterResponse, - registerResponse: Opt.some(RegisterResponse(status: status, text: Opt.some(text))))) +proc sendRegisterResponseError( + conn: Connection, status: ResponseStatus, text: string = "" +) {.async.} = + let msg = encode( + Message( + msgType: MessageType.RegisterResponse, + registerResponse: Opt.some(RegisterResponse(status: status, text: Opt.some(text))), + ) + ) await conn.writeLp(msg.buffer) -proc sendDiscoverResponse(conn: Connection, - s: seq[Register], - cookie: Cookie) {.async.} = - let msg = encode(Message( - msgType: MessageType.DiscoverResponse, - discoverResponse: Opt.some(DiscoverResponse( - status: Ok, - registrations: s, - cookie: Opt.some(cookie.encode().buffer) - )) - )) +proc sendDiscoverResponse( + conn: Connection, s: seq[Register], cookie: Cookie +) {.async.} = + let msg = encode( + Message( + msgType: MessageType.DiscoverResponse, + discoverResponse: Opt.some( + DiscoverResponse( + status: Ok, registrations: s, cookie: Opt.some(cookie.encode().buffer) + ) + ), + ) + ) await conn.writeLp(msg.buffer) -proc sendDiscoverResponseError(conn: Connection, - status: ResponseStatus, - text: string = "") {.async.} = - let msg = encode(Message( - msgType: MessageType.DiscoverResponse, - discoverResponse: Opt.some(DiscoverResponse(status: status, text: Opt.some(text))))) +proc sendDiscoverResponseError( + conn: Connection, status: ResponseStatus, text: string = "" +) {.async.} = + let msg = encode( + Message( + msgType: MessageType.DiscoverResponse, + discoverResponse: Opt.some(DiscoverResponse(status: status, text: Opt.some(text))), + ) + ) await conn.writeLp(msg.buffer) proc countRegister(rdv: RendezVous, peerId: PeerId): int = @@ -354,37 +381,36 @@ proc countRegister(rdv: RendezVous, peerId: PeerId): int = if data.peerId == peerId and data.expiration > n: result.inc() -proc save(rdv: RendezVous, - ns: string, - peerId: PeerId, - r: Register, - update: bool = true) = +proc save( + rdv: RendezVous, ns: string, peerId: PeerId, r: Register, update: bool = true +) = let nsSalted = ns & rdv.salt discard rdv.namespaces.hasKeyOrPut(nsSalted, newSeq[int]()) try: for index in rdv.namespaces[nsSalted]: if rdv.registered[index].peerId == peerId: - if update == false: return + if update == false: + return rdv.registered[index].expiration = rdv.defaultDT rdv.registered.add( RegisteredData( peerId: peerId, expiration: Moment.now() + r.ttl.get(MinimumTTL).int64.seconds, - data: r + data: r, ) ) rdv.namespaces[nsSalted].add(rdv.registered.high) -# rdv.registerEvent.fire() + # rdv.registerEvent.fire() except KeyError: doAssert false, "Should have key" proc register(rdv: RendezVous, conn: Connection, r: Register): Future[void] = trace "Received Register", peerId = conn.peerId, ns = r.ns libp2p_rendezvous_register.inc() - if r.ns.len notin 1..255: + if r.ns.len notin 1 .. 255: return conn.sendRegisterResponseError(InvalidNamespace) let ttl = r.ttl.get(MinimumTTL) - if ttl notin MinimumTTL..MaximumTTL: + if ttl notin MinimumTTL .. MaximumTTL: return conn.sendRegisterResponseError(InvalidTTL) let pr = checkPeerRecord(r.signedPeerRecord, conn.peerId) if pr.isErr(): @@ -410,21 +436,21 @@ proc unregister(rdv: RendezVous, conn: Connection, u: Unregister) = proc discover(rdv: RendezVous, conn: Connection, d: Discover) {.async.} = trace "Received Discover", peerId = conn.peerId, ns = d.ns libp2p_rendezvous_discover.inc() - if d.ns.len notin 0..255: + if d.ns.len notin 0 .. 255: await conn.sendDiscoverResponseError(InvalidNamespace) return var limit = min(DiscoverLimit, d.limit.get(DiscoverLimit)) - var - cookie = - if d.cookie.isSome(): - try: - Cookie.decode(d.cookie.tryGet()).tryGet() - except CatchableError: - await conn.sendDiscoverResponseError(InvalidCookie) - return - else: Cookie(offset: rdv.registered.low().uint64 - 1) + var cookie = + if d.cookie.isSome(): + try: + Cookie.decode(d.cookie.tryGet()).tryGet() + except CatchableError: + await conn.sendDiscoverResponseError(InvalidCookie) + return + else: + Cookie(offset: rdv.registered.low().uint64 - 1) if cookie.ns != d.ns or - cookie.offset notin rdv.registered.low().uint64..rdv.registered.high().uint64: + cookie.offset notin rdv.registered.low().uint64 .. rdv.registered.high().uint64: cookie = Cookie(offset: rdv.registered.low().uint64 - 1) let nsSalted = d.ns & rdv.salt @@ -435,32 +461,33 @@ proc discover(rdv: RendezVous, conn: Connection, d: Discover) {.async.} = except KeyError: await conn.sendDiscoverResponseError(InvalidNamespace) return - else: toSeq(cookie.offset.int..rdv.registered.high()) + else: + toSeq(cookie.offset.int .. rdv.registered.high()) if namespaces.len() == 0: await conn.sendDiscoverResponse(@[], Cookie()) return var offset = namespaces[^1] let n = Moment.now() var s = collect(newSeq()): - for index in namespaces: - var reg = rdv.registered[index] - if limit == 0: - offset = index - break - if reg.expiration < n or index.uint64 <= cookie.offset: continue - limit.dec() - reg.data.ttl = Opt.some((reg.expiration - Moment.now()).seconds.uint64) - reg.data + for index in namespaces: + var reg = rdv.registered[index] + if limit == 0: + offset = index + break + if reg.expiration < n or index.uint64 <= cookie.offset: + continue + limit.dec() + reg.data.ttl = Opt.some((reg.expiration - Moment.now()).seconds.uint64) + reg.data rdv.rng.shuffle(s) await conn.sendDiscoverResponse(s, Cookie(offset: offset.uint64, ns: d.ns)) -proc advertisePeer(rdv: RendezVous, - peer: PeerId, - msg: seq[byte]) {.async.} = +proc advertisePeer(rdv: RendezVous, peer: PeerId, msg: seq[byte]) {.async.} = proc advertiseWrap() {.async.} = try: let conn = await rdv.switch.dial(peer, RendezVousCodec) - defer: await conn.close() + defer: + await conn.close() await conn.writeLp(msg) let buf = await conn.readLp(4096) @@ -475,17 +502,18 @@ proc advertisePeer(rdv: RendezVous, trace "exception in the advertise", error = exc.msg finally: rdv.sema.release() + await rdv.sema.acquire() discard await advertiseWrap().withTimeout(5.seconds) -method advertise*(rdv: RendezVous, - ns: string, - ttl: Duration = MinimumDuration) {.async, base.} = +method advertise*( + rdv: RendezVous, ns: string, ttl: Duration = MinimumDuration +) {.async, base.} = let sprBuff = rdv.switch.peerInfo.signedPeerRecord.encode().valueOr: raise newException(RendezVousError, "Wrong Signed Peer Record") - if ns.len notin 1..255: + if ns.len notin 1 .. 255: raise newException(RendezVousError, "Invalid namespace") - if ttl notin MinimumDuration..MaximumDuration: + if ttl notin MinimumDuration .. MaximumDuration: raise newException(RendezVousError, "Invalid time to live") let r = Register(ns: ns, signedPeerRecord: sprBuff, ttl: Opt.some(ttl.seconds.uint64)) @@ -511,9 +539,9 @@ proc requestLocally*(rdv: RendezVous, ns: string): seq[PeerRecord] = except KeyError as exc: @[] -proc request*(rdv: RendezVous, - ns: string, - l: int = DiscoverLimit.int): Future[seq[PeerRecord]] {.async.} = +proc request*( + rdv: RendezVous, ns: string, l: int = DiscoverLimit.int +): Future[seq[PeerRecord]] {.async.} = let nsSalted = ns & rdv.salt var s: Table[PeerId, (PeerRecord, Register)] @@ -522,21 +550,22 @@ proc request*(rdv: RendezVous, if l <= 0 or l > DiscoverLimit.int: raise newException(RendezVousError, "Invalid limit") - if ns.len notin 0..255: + if ns.len notin 0 .. 255: raise newException(RendezVousError, "Invalid namespace") limit = l.uint64 proc requestPeer(peer: PeerId) {.async.} = let conn = await rdv.switch.dial(peer, RendezVousCodec) - defer: await conn.close() + defer: + await conn.close() d.limit = Opt.some(limit) d.cookie = try: Opt.some(rdv.cookiesSaved[peer][ns]) except KeyError as exc: Opt.none(seq[byte]) - await conn.writeLp(encode(Message( - msgType: MessageType.Discover, - discover: Opt.some(d))).buffer) + await conn.writeLp( + encode(Message(msgType: MessageType.Discover, discover: Opt.some(d))).buffer + ) let buf = await conn.readLp(65536) msgRcv = Message.decode(buf).valueOr: @@ -552,19 +581,23 @@ proc request*(rdv: RendezVous, trace "Cannot discover", ns, status = resp.status, text = resp.text return resp.cookie.withValue(cookie): - if cookie.len() < 1000 and rdv.cookiesSaved.hasKeyOrPut(peer, {ns: cookie}.toTable()): + if cookie.len() < 1000 and + rdv.cookiesSaved.hasKeyOrPut(peer, {ns: cookie}.toTable()): rdv.cookiesSaved[peer][ns] = cookie for r in resp.registrations: - if limit == 0: return + if limit == 0: + return let ttl = r.ttl.get(MaximumTTL + 1) - if ttl > MaximumTTL: continue + if ttl > MaximumTTL: + continue let - spr = SignedPeerRecord.decode(r.signedPeerRecord).valueOr: continue + spr = SignedPeerRecord.decode(r.signedPeerRecord).valueOr: + continue pr = spr.data if s.hasKey(pr.peerId): let (prSaved, rSaved) = s[pr.peerId] if (prSaved.seqNo == pr.seqNo and rSaved.ttl.get(MaximumTTL) < ttl) or - prSaved.seqNo < pr.seqNo: + prSaved.seqNo < pr.seqNo: s[pr.peerId] = (pr, r) else: s[pr.peerId] = (pr, r) @@ -575,8 +608,10 @@ proc request*(rdv: RendezVous, # copy to avoid resizes during the loop let peers = rdv.peers for peer in peers: - if limit == 0: break - if RendezVousCodec notin rdv.switch.peerStore[ProtoBook][peer]: continue + if limit == 0: + break + if RendezVousCodec notin rdv.switch.peerStore[ProtoBook][peer]: + continue try: trace "Send Request", peerId = peer, ns await peer.requestPeer() @@ -597,17 +632,18 @@ proc unsubscribeLocally*(rdv: RendezVous, ns: string) = proc unsubscribe*(rdv: RendezVous, ns: string) {.async.} = # TODO: find a way to improve this, maybe something similar to the advertise - if ns.len notin 1..255: + if ns.len notin 1 .. 255: raise newException(RendezVousError, "Invalid namespace") rdv.unsubscribeLocally(ns) - let msg = encode(Message( - msgType: MessageType.Unregister, - unregister: Opt.some(Unregister(ns: ns)))) + let msg = encode( + Message(msgType: MessageType.Unregister, unregister: Opt.some(Unregister(ns: ns))) + ) proc unsubscribePeer(rdv: RendezVous, peerId: PeerId) {.async.} = try: let conn = await rdv.switch.dial(peerId, RendezVousCodec) - defer: await conn.close() + defer: + await conn.close() await conn.writeLp(msg.buffer) except CatchableError as exc: trace "exception while unsubscribing", error = exc.msg @@ -622,33 +658,37 @@ proc setup*(rdv: RendezVous, switch: Switch) = rdv.peers.add(peerId) elif event.kind == PeerEventKind.Left: rdv.peers.keepItIf(it != peerId) + rdv.switch.addPeerEventHandler(handlePeer, Joined) rdv.switch.addPeerEventHandler(handlePeer, Left) -proc new*(T: typedesc[RendezVous], - rng: ref HmacDrbgContext = newRng()): T = +proc new*(T: typedesc[RendezVous], rng: ref HmacDrbgContext = newRng()): T = let rdv = T( rng: rng, salt: string.fromBytes(generateBytes(rng[], 8)), registered: initOffsettedSeq[RegisteredData](1), defaultDT: Moment.now() - 1.days, #registerEvent: newAsyncEvent(), - sema: newAsyncSemaphore(SemaphoreDefaultSize) + sema: newAsyncSemaphore(SemaphoreDefaultSize), ) - logScope: topics = "libp2p discovery rendezvous" + logScope: + topics = "libp2p discovery rendezvous" proc handleStream(conn: Connection, proto: string) {.async.} = try: let buf = await conn.readLp(4096) msg = Message.decode(buf).tryGet() - case msg.msgType: - of MessageType.Register: await rdv.register(conn, msg.register.tryGet()) - of MessageType.RegisterResponse: - trace "Got an unexpected Register Response", response = msg.registerResponse - of MessageType.Unregister: rdv.unregister(conn, msg.unregister.tryGet()) - of MessageType.Discover: await rdv.discover(conn, msg.discover.tryGet()) - of MessageType.DiscoverResponse: - trace "Got an unexpected Discover Response", response = msg.discoverResponse + case msg.msgType + of MessageType.Register: + await rdv.register(conn, msg.register.tryGet()) + of MessageType.RegisterResponse: + trace "Got an unexpected Register Response", response = msg.registerResponse + of MessageType.Unregister: + rdv.unregister(conn, msg.unregister.tryGet()) + of MessageType.Discover: + await rdv.discover(conn, msg.discover.tryGet()) + of MessageType.DiscoverResponse: + trace "Got an unexpected Discover Response", response = msg.discoverResponse except CancelledError as exc: raise exc except CatchableError as exc: @@ -660,9 +700,9 @@ proc new*(T: typedesc[RendezVous], rdv.codec = RendezVousCodec return rdv -proc new*(T: typedesc[RendezVous], - switch: Switch, - rng: ref HmacDrbgContext = newRng()): T = +proc new*( + T: typedesc[RendezVous], switch: Switch, rng: ref HmacDrbgContext = newRng() +): T = let rdv = T.new(rng) rdv.setup(switch) return rdv diff --git a/libp2p/protocols/secure/noise.nim b/libp2p/protocols/secure/noise.nim index 0a4032bcef..5ef02039f4 100644 --- a/libp2p/protocols/secure/noise.nim +++ b/libp2p/protocols/secure/noise.nim @@ -22,8 +22,7 @@ import ../../protobuf/minprotobuf import ../../utility import ../../errors -import secure, - ../../crypto/[crypto, chacha20poly1305, curve25519, hkdf] +import secure, ../../crypto/[crypto, chacha20poly1305, curve25519, hkdf] when defined(libp2p_dump): import ../../debugutils @@ -99,12 +98,15 @@ type func shortLog*(conn: NoiseConnection): auto = try: - if conn == nil: "NoiseConnection(nil)" - else: &"{shortLog(conn.peerId)}:{conn.oid}" + if conn == nil: + "NoiseConnection(nil)" + else: + &"{shortLog(conn.peerId)}:{conn.oid}" except ValueError as exc: raiseAssert(exc.msg) -chronicles.formatIt(NoiseConnection): shortLog(it) +chronicles.formatIt(NoiseConnection): + shortLog(it) proc genKeyPair(rng: var HmacDrbgContext): KeyPair = result.privateKey = Curve25519Key.random(rng) @@ -116,7 +118,7 @@ proc hashProtocol(name: string): MDigest[256] = # Otherwise sets h = HASH(protocol_name). if name.len <= 32: - result.data[0..name.high] = name.toBytes + result.data[0 .. name.high] = name.toBytes else: result = sha256.digest(name) @@ -130,13 +132,10 @@ proc hasKey(cs: CipherState): bool = cs.k != EmptyKey proc encrypt( - state: var CipherState, - data: var openArray[byte], - ad: openArray[byte]): ChaChaPolyTag - {.noinit, raises: [NoiseNonceMaxError].} = - + state: var CipherState, data: var openArray[byte], ad: openArray[byte] +): ChaChaPolyTag {.noinit, raises: [NoiseNonceMaxError].} = var nonce: ChaChaPolyNonce - nonce[4..<12] = toBytesLE(state.n) + nonce[4 ..< 12] = toBytesLE(state.n) ChaChaPoly.encrypt(state.k, nonce, result, data, ad) @@ -144,8 +143,9 @@ proc encrypt( if state.n > NonceMax: raise (ref NoiseNonceMaxError)(msg: "Noise max nonce value reached") -proc encryptWithAd(state: var CipherState, ad, data: openArray[byte]): seq[byte] - {.raises: [NoiseNonceMaxError].} = +proc encryptWithAd( + state: var CipherState, ad, data: openArray[byte] +): seq[byte] {.raises: [NoiseNonceMaxError].} = result = newSeqOfCap[byte](data.len + sizeof(ChaChaPolyTag)) result.add(data) @@ -156,20 +156,21 @@ proc encryptWithAd(state: var CipherState, ad, data: openArray[byte]): seq[byte] trace "encryptWithAd", tag = byteutils.toHex(tag), data = result.shortLog, nonce = state.n - 1 -proc decryptWithAd(state: var CipherState, ad, data: openArray[byte]): seq[byte] - {.raises: [NoiseDecryptTagError, NoiseNonceMaxError].} = +proc decryptWithAd( + state: var CipherState, ad, data: openArray[byte] +): seq[byte] {.raises: [NoiseDecryptTagError, NoiseNonceMaxError].} = var tagIn = data.toOpenArray(data.len - ChaChaPolyTag.len, data.high).intoChaChaPolyTag tagOut: ChaChaPolyTag nonce: ChaChaPolyNonce - nonce[4..<12] = toBytesLE(state.n) - result = data[0..(data.high - ChaChaPolyTag.len)] + nonce[4 ..< 12] = toBytesLE(state.n) + result = data[0 .. (data.high - ChaChaPolyTag.len)] ChaChaPoly.decrypt(state.k, nonce, tagOut, result, ad) - trace "decryptWithAd", tagIn = tagIn.shortLog, tagOut = tagOut.shortLog, nonce = state.n + trace "decryptWithAd", + tagIn = tagIn.shortLog, tagOut = tagOut.shortLog, nonce = state.n if tagIn != tagOut: debug "decryptWithAd failed", data = shortLog(data) - raise (ref NoiseDecryptTagError)(msg: - "decryptWithAd failed tag authentication.") + raise (ref NoiseDecryptTagError)(msg: "decryptWithAd failed tag authentication.") inc state.n if state.n > NonceMax: raise (ref NoiseNonceMaxError)(msg: "Noise max nonce value reached") @@ -204,8 +205,9 @@ proc mixKeyAndHash(ss: var SymmetricState, ikm: openArray[byte]) {.used.} = ss.mixHash(temp_keys[1]) ss.cs = CipherState(k: temp_keys[2]) -proc encryptAndHash(ss: var SymmetricState, data: openArray[byte]): seq[byte] - {.raises: [NoiseNonceMaxError].} = +proc encryptAndHash( + ss: var SymmetricState, data: openArray[byte] +): seq[byte] {.raises: [NoiseNonceMaxError].} = # according to spec if key is empty leave plaintext if ss.cs.hasKey: result = ss.cs.encryptWithAd(ss.h.data, data) @@ -213,8 +215,9 @@ proc encryptAndHash(ss: var SymmetricState, data: openArray[byte]): seq[byte] result = @data ss.mixHash(result) -proc decryptAndHash(ss: var SymmetricState, data: openArray[byte]): seq[byte] - {.raises: [NoiseDecryptTagError, NoiseNonceMaxError].} = +proc decryptAndHash( + ss: var SymmetricState, data: openArray[byte] +): seq[byte] {.raises: [NoiseDecryptTagError, NoiseNonceMaxError].} = # according to spec if key is empty leave plaintext if ss.cs.hasKey and data.len > ChaChaPolyTag.len: result = ss.cs.decryptWithAd(ss.h.data, data) @@ -223,15 +226,14 @@ proc decryptAndHash(ss: var SymmetricState, data: openArray[byte]): seq[byte] ss.mixHash(data) proc split(ss: var SymmetricState): tuple[cs1, cs2: CipherState] = - var - temp_keys: array[2, ChaChaPolyKey] + var temp_keys: array[2, ChaChaPolyKey] sha256.hkdf(ss.ck, [], [], temp_keys) return (CipherState(k: temp_keys[0]), CipherState(k: temp_keys[1])) proc init(_: type[HandshakeState]): HandshakeState = result.ss = SymmetricState.init() -template write_e: untyped = +template write_e(): untyped = trace "noise write e" # Sets e (which must be empty) to GENERATE_KEYPAIR(). # Appends e.public_key to the buffer. Calls MixHash(e.public_key). @@ -239,17 +241,17 @@ template write_e: untyped = msg.add hs.e.publicKey hs.ss.mixHash(hs.e.publicKey) -template write_s: untyped = +template write_s(): untyped = trace "noise write s" # Appends EncryptAndHash(s.public_key) to the buffer. msg.add hs.ss.encryptAndHash(hs.s.publicKey) -template dh_ee: untyped = +template dh_ee(): untyped = trace "noise dh ee" # Calls MixKey(DH(e, re)). hs.ss.mixKey(dh(hs.e.privateKey, hs.re)) -template dh_es: untyped = +template dh_es(): untyped = trace "noise dh es" # Calls MixKey(DH(e, rs)) if initiator, MixKey(DH(s, re)) if responder. when initiator: @@ -257,7 +259,7 @@ template dh_es: untyped = else: hs.ss.mixKey(dh(hs.s.privateKey, hs.re)) -template dh_se: untyped = +template dh_se(): untyped = trace "noise dh se" # Calls MixKey(DH(s, re)) if initiator, MixKey(DH(e, rs)) if responder. when initiator: @@ -266,12 +268,12 @@ template dh_se: untyped = hs.ss.mixKey(dh(hs.e.privateKey, hs.rs)) # might be used for other token/handshakes -template dh_ss: untyped {.used.} = +template dh_ss(): untyped {.used.} = trace "noise dh ss" # Calls MixKey(DH(s, rs)). hs.ss.mixKey(dh(hs.s.privateKey, hs.rs)) -template read_e: untyped = +template read_e(): untyped = trace "noise read e", size = msg.len if msg.len < Curve25519Key.len: @@ -279,27 +281,25 @@ template read_e: untyped = # Sets re (which must be empty) to the next DHLEN bytes from the message. # Calls MixHash(re.public_key). - hs.re[0..Curve25519Key.high] = msg.toOpenArray(0, Curve25519Key.high) + hs.re[0 .. Curve25519Key.high] = msg.toOpenArray(0, Curve25519Key.high) msg.consume(Curve25519Key.len) hs.ss.mixHash(hs.re) -template read_s: untyped = +template read_s(): untyped = trace "noise read s", size = msg.len # Sets temp to the next DHLEN + 16 bytes of the message if HasKey() == True, # or to the next DHLEN bytes otherwise. # Sets rs (which must be empty) to DecryptAndHash(temp). - let - rsLen = - if hs.ss.cs.hasKey: - if msg.len < Curve25519Key.len + ChaChaPolyTag.len: - raise (ref NoiseHandshakeError)(msg: "Noise S, expected more data") - Curve25519Key.len + ChaChaPolyTag.len - else: - if msg.len < Curve25519Key.len: - raise (ref NoiseHandshakeError)(msg: "Noise S, expected more data") - Curve25519Key.len - hs.rs[0..Curve25519Key.high] = - hs.ss.decryptAndHash(msg.toOpenArray(0, rsLen - 1)) + let rsLen = + if hs.ss.cs.hasKey: + if msg.len < Curve25519Key.len + ChaChaPolyTag.len: + raise (ref NoiseHandshakeError)(msg: "Noise S, expected more data") + Curve25519Key.len + ChaChaPolyTag.len + else: + if msg.len < Curve25519Key.len: + raise (ref NoiseHandshakeError)(msg: "Noise S, expected more data") + Curve25519Key.len + hs.rs[0 .. Curve25519Key.high] = hs.ss.decryptAndHash(msg.toOpenArray(0, rsLen - 1)) msg.consume(rsLen) @@ -318,10 +318,8 @@ proc readFrame( return buffer proc writeFrame( - sconn: Connection, - buf: openArray[byte] -): Future[void] {.async: (raises: [ - CancelledError, LPStreamError], raw: true).} = + sconn: Connection, buf: openArray[byte] +): Future[void] {.async: (raises: [CancelledError, LPStreamError], raw: true).} = doAssert buf.len <= uint16.high.int var lesize = buf.len.uint16 @@ -334,25 +332,19 @@ proc writeFrame( proc receiveHSMessage( sconn: Connection -): Future[seq[byte]] {.async: (raises: [ - CancelledError, LPStreamError], raw: true).} = +): Future[seq[byte]] {.async: (raises: [CancelledError, LPStreamError], raw: true).} = readFrame(sconn) proc sendHSMessage( - sconn: Connection, - buf: openArray[byte] -): Future[void] {.async: (raises: [ - CancelledError, LPStreamError], raw: true).} = + sconn: Connection, buf: openArray[byte] +): Future[void] {.async: (raises: [CancelledError, LPStreamError], raw: true).} = writeFrame(sconn, buf) proc handshakeXXOutbound( - p: Noise, conn: Connection, - p2pSecret: seq[byte] -): Future[HandshakeResult] {.async: (raises: [ - CancelledError, LPStreamError]).} = + p: Noise, conn: Connection, p2pSecret: seq[byte] +): Future[HandshakeResult] {.async: (raises: [CancelledError, LPStreamError]).} = const initiator = true - var - hs = HandshakeState.init() + var hs = HandshakeState.init() try: hs.ss.mixHash(p.commonPrologue) @@ -391,20 +383,17 @@ proc handshakeXXOutbound( await conn.sendHSMessage(msg.data) let (cs1, cs2) = hs.ss.split() - return HandshakeResult( - cs1: cs1, cs2: cs2, remoteP2psecret: remoteP2psecret, rs: hs.rs) + return + HandshakeResult(cs1: cs1, cs2: cs2, remoteP2psecret: remoteP2psecret, rs: hs.rs) finally: burnMem(hs) proc handshakeXXInbound( - p: Noise, conn: Connection, - p2pSecret: seq[byte] -): Future[HandshakeResult] {.async: (raises: [ - CancelledError, LPStreamError]).} = + p: Noise, conn: Connection, p2pSecret: seq[byte] +): Future[HandshakeResult] {.async: (raises: [CancelledError, LPStreamError]).} = const initiator = false - var - hs = HandshakeState.init() + var hs = HandshakeState.init() try: hs.ss.mixHash(p.commonPrologue) @@ -444,8 +433,8 @@ proc handshakeXXInbound( let remoteP2psecret = hs.ss.decryptAndHash(msg.data) (cs1, cs2) = hs.ss.split() - return HandshakeResult( - cs1: cs1, cs2: cs2, remoteP2psecret: remoteP2psecret, rs: hs.rs) + return + HandshakeResult(cs1: cs1, cs2: cs2, remoteP2psecret: remoteP2psecret, rs: hs.rs) finally: burnMem(hs) @@ -467,32 +456,26 @@ method readMessage*( trace "Received 0-length message", sconn proc encryptFrame( - sconn: NoiseConnection, - cipherFrame: var openArray[byte], - src: openArray[byte]) - {.raises: [NoiseNonceMaxError].} = + sconn: NoiseConnection, cipherFrame: var openArray[byte], src: openArray[byte] +) {.raises: [NoiseNonceMaxError].} = # Frame consists of length + cipher data + tag doAssert src.len <= MaxPlainSize doAssert cipherFrame.len == 2 + src.len + sizeof(ChaChaPolyTag) - cipherFrame[0..<2] = toBytesBE(uint16(src.len + sizeof(ChaChaPolyTag))) - cipherFrame[2..<2 + src.len()] = src + cipherFrame[0 ..< 2] = toBytesBE(uint16(src.len + sizeof(ChaChaPolyTag))) + cipherFrame[2 ..< 2 + src.len()] = src - let tag = encrypt( - sconn.writeCs, cipherFrame.toOpenArray(2, 2 + src.len() - 1), []) + let tag = encrypt(sconn.writeCs, cipherFrame.toOpenArray(2, 2 + src.len() - 1), []) - cipherFrame[2 + src.len().. 0: - let - chunkSize = min(MaxPlainSize, left) + let chunkSize = min(MaxPlainSize, left) try: encryptFrame( sconn, - cipherFrames.toOpenArray( - woffset, woffset + chunkSize + FramingSize - 1), - message.toOpenArray(offset, offset + chunkSize - 1)) + cipherFrames.toOpenArray(woffset, woffset + chunkSize + FramingSize - 1), + message.toOpenArray(offset, offset + chunkSize - 1), + ) except NoiseNonceMaxError as exc: debug "Noise nonce exceeded" let fut = newFuture[void]("noise.write.nonce") @@ -518,8 +500,10 @@ method write*( when defined(libp2p_dump): dumpMessage( - sconn, FlowDirection.Outgoing, - message.toOpenArray(offset, offset + chunkSize - 1)) + sconn, + FlowDirection.Outgoing, + message.toOpenArray(offset, offset + chunkSize - 1), + ) left = left - chunkSize offset += chunkSize @@ -532,10 +516,7 @@ method write*( sconn.stream.write(cipherFrames) method handshake*( - p: Noise, - conn: Connection, - initiator: bool, - peerId: Opt[PeerId] + p: Noise, conn: Connection, initiator: bool, peerId: Opt[PeerId] ): Future[SecureConn] {.async: (raises: [CancelledError, LPStreamError]).} = trace "Starting Noise handshake", conn, initiator @@ -543,14 +524,14 @@ method handshake*( conn.timeout = HandshakeTimeout # https://github.com/libp2p/specs/tree/master/noise#libp2p-data-in-handshake-messages - let signedPayload = p.localPrivateKey.sign( - PayloadString & p.noiseKeys.publicKey.getBytes) + let signedPayload = + p.localPrivateKey.sign(PayloadString & p.noiseKeys.publicKey.getBytes) if signedPayload.isErr(): - raise (ref NoiseHandshakeError)(msg: - "Failed to sign public key: " & $signedPayload.error()) + raise (ref NoiseHandshakeError)( + msg: "Failed to sign public key: " & $signedPayload.error() + ) - var - libp2pProof = initProtoBuffer() + var libp2pProof = initProtoBuffer() libp2pProof.write(1, p.localPublicKey) libp2pProof.write(2, signedPayload.get().getBytes()) # data field also there but not used! @@ -562,70 +543,76 @@ method handshake*( else: await handshakeXXInbound(p, conn, libp2pProof.buffer) - var secure = try: - var - remoteProof = initProtoBuffer(handshakeRes.remoteP2psecret) - remotePubKey: PublicKey - remotePubKeyBytes: seq[byte] - remoteSig: Signature - remoteSigBytes: seq[byte] - - if not remoteProof.getField(1, remotePubKeyBytes).valueOr(false): - raise (ref NoiseHandshakeError)(msg: - "Failed to deserialize remote public key bytes. (initiator: " & - $initiator & ")") - if not remoteProof.getField(2, remoteSigBytes).valueOr(false): - raise (ref NoiseHandshakeError)(msg: - "Failed to deserialize remote signature bytes. (initiator: " & - $initiator & ")") - - if not remotePubKey.init(remotePubKeyBytes): - raise (ref NoiseHandshakeError)(msg: - "Failed to decode remote public key. (initiator: " & $initiator & ")") - if not remoteSig.init(remoteSigBytes): - raise (ref NoiseHandshakeError)(msg: - "Failed to decode remote signature. (initiator: " & $initiator & ")") - - let verifyPayload = PayloadString & handshakeRes.rs.getBytes - if not remoteSig.verify(verifyPayload, remotePubKey): - raise (ref NoiseHandshakeError)(msg: - "Noise handshake signature verify failed.") - else: - trace "Remote signature verified", conn - - let pid = PeerId.init(remotePubKey).valueOr: - raise (ref NoiseHandshakeError)(msg: - "Invalid remote peer id: " & $error) - - trace "Remote peer id", pid = $pid - - peerId.withValue(targetPid): - if not targetPid.validate(): - raise (ref NoiseHandshakeError)(msg: - "Failed to validate expected peerId.") - - if pid != targetPid: - var - failedKey: PublicKey - discard extractPublicKey(targetPid, failedKey) - debug "Noise handshake, peer id doesn't match!", - initiator, dealt_peer = conn, - dealt_key = $failedKey, received_peer = $pid, - received_key = $remotePubKey - raise (ref NoiseHandshakeError)(msg: - "Noise handshake, peer id don't match! " & $pid & " != " & $targetPid) - conn.peerId = pid - - var tmp = NoiseConnection.new(conn, conn.peerId, conn.observedAddr) - if initiator: - tmp.readCs = handshakeRes.cs2 - tmp.writeCs = handshakeRes.cs1 - else: - tmp.readCs = handshakeRes.cs1 - tmp.writeCs = handshakeRes.cs2 - tmp - finally: - burnMem(handshakeRes) + var secure = + try: + var + remoteProof = initProtoBuffer(handshakeRes.remoteP2psecret) + remotePubKey: PublicKey + remotePubKeyBytes: seq[byte] + remoteSig: Signature + remoteSigBytes: seq[byte] + + if not remoteProof.getField(1, remotePubKeyBytes).valueOr(false): + raise (ref NoiseHandshakeError)( + msg: + "Failed to deserialize remote public key bytes. (initiator: " & $initiator & + ")" + ) + if not remoteProof.getField(2, remoteSigBytes).valueOr(false): + raise (ref NoiseHandshakeError)( + msg: + "Failed to deserialize remote signature bytes. (initiator: " & $initiator & + ")" + ) + + if not remotePubKey.init(remotePubKeyBytes): + raise (ref NoiseHandshakeError)( + msg: "Failed to decode remote public key. (initiator: " & $initiator & ")" + ) + if not remoteSig.init(remoteSigBytes): + raise (ref NoiseHandshakeError)( + msg: "Failed to decode remote signature. (initiator: " & $initiator & ")" + ) + + let verifyPayload = PayloadString & handshakeRes.rs.getBytes + if not remoteSig.verify(verifyPayload, remotePubKey): + raise (ref NoiseHandshakeError)(msg: "Noise handshake signature verify failed.") + else: + trace "Remote signature verified", conn + + let pid = PeerId.init(remotePubKey).valueOr: + raise (ref NoiseHandshakeError)(msg: "Invalid remote peer id: " & $error) + + trace "Remote peer id", pid = $pid + + peerId.withValue(targetPid): + if not targetPid.validate(): + raise (ref NoiseHandshakeError)(msg: "Failed to validate expected peerId.") + + if pid != targetPid: + var failedKey: PublicKey + discard extractPublicKey(targetPid, failedKey) + debug "Noise handshake, peer id doesn't match!", + initiator, + dealt_peer = conn, + dealt_key = $failedKey, + received_peer = $pid, + received_key = $remotePubKey + raise (ref NoiseHandshakeError)( + msg: "Noise handshake, peer id don't match! " & $pid & " != " & $targetPid + ) + conn.peerId = pid + + var tmp = NoiseConnection.new(conn, conn.peerId, conn.observedAddr) + if initiator: + tmp.readCs = handshakeRes.cs2 + tmp.writeCs = handshakeRes.cs1 + else: + tmp.readCs = handshakeRes.cs1 + tmp.writeCs = handshakeRes.cs2 + tmp + finally: + burnMem(handshakeRes) trace "Noise handshake completed!", initiator, peer = shortLog(secure.peerId) @@ -648,10 +635,13 @@ proc new*( rng: ref HmacDrbgContext, privateKey: PrivateKey, outgoing: bool = true, - commonPrologue: seq[byte] = @[]): T = - let pkBytes = privateKey.getPublicKey() + commonPrologue: seq[byte] = @[], +): T = + let pkBytes = privateKey + .getPublicKey() .expect("Expected valid Private Key") - .getBytes().expect("Couldn't get public Key bytes") + .getBytes() + .expect("Couldn't get public Key bytes") var noise = Noise( rng: rng, diff --git a/libp2p/protocols/secure/plaintext.nim b/libp2p/protocols/secure/plaintext.nim index 3e94237dde..7b5aaa1f11 100644 --- a/libp2p/protocols/secure/plaintext.nim +++ b/libp2p/protocols/secure/plaintext.nim @@ -14,13 +14,12 @@ import secure, ../../stream/connection const PlainTextCodec* = "/plaintext/1.0.0" -type - PlainText* = ref object of Secure +type PlainText* = ref object of Secure method init(p: PlainText) {.gcsafe.} = - proc handle(conn: Connection, proto: string) - {.async.} = discard + proc handle(conn: Connection, proto: string) {.async.} = ## plain text doesn't do anything + discard p.codec = PlainTextCodec p.handler = handle diff --git a/libp2p/protocols/secure/secure.nim b/libp2p/protocols/secure/secure.nim index 18f74aec63..61d8aac9a9 100644 --- a/libp2p/protocols/secure/secure.nim +++ b/libp2p/protocols/secure/secure.nim @@ -13,20 +13,20 @@ import std/[strformat] import stew/results import chronos, chronicles -import ../protocol, - ../../stream/streamseq, - ../../stream/connection, - ../../multiaddress, - ../../peerinfo, - ../../errors +import + ../protocol, + ../../stream/streamseq, + ../../stream/connection, + ../../multiaddress, + ../../peerinfo, + ../../errors export protocol, results logScope: topics = "libp2p secure" -const - SecureConnTrackerName* = "SecureConn" +const SecureConnTrackerName* = "SecureConn" type Secure* = ref object of LPProtocol # base type for secure managers @@ -37,25 +37,31 @@ type func shortLog*(conn: SecureConn): auto = try: - if conn == nil: "SecureConn(nil)" - else: &"{shortLog(conn.peerId)}:{conn.oid}" + if conn == nil: + "SecureConn(nil)" + else: + &"{shortLog(conn.peerId)}:{conn.oid}" except ValueError as exc: raiseAssert(exc.msg) -chronicles.formatIt(SecureConn): shortLog(it) +chronicles.formatIt(SecureConn): + shortLog(it) proc new*( T: type SecureConn, conn: Connection, peerId: PeerId, observedAddr: Opt[MultiAddress], - timeout: Duration = DefaultConnectionTimeout): T = - result = T(stream: conn, - peerId: peerId, - observedAddr: observedAddr, - closeEvent: conn.closeEvent, - timeout: timeout, - dir: conn.dir) + timeout: Duration = DefaultConnectionTimeout, +): T = + result = T( + stream: conn, + peerId: peerId, + observedAddr: observedAddr, + closeEvent: conn.closeEvent, + timeout: timeout, + dir: conn.dir, + ) result.initStream() method initStream*(s: SecureConn) = @@ -73,26 +79,23 @@ method closeImpl*(s: SecureConn) {.async: (raises: []).} = method readMessage*( c: SecureConn -): Future[seq[byte]] {.async: (raises: [ - CancelledError, LPStreamError], raw: true), base.} = +): Future[seq[byte]] {. + async: (raises: [CancelledError, LPStreamError], raw: true), base +.} = raiseAssert("Not implemented!") -method getWrapped*(s: SecureConn): Connection = s.stream +method getWrapped*(s: SecureConn): Connection = + s.stream method handshake*( - s: Secure, - conn: Connection, - initiator: bool, - peerId: Opt[PeerId] -): Future[SecureConn] {.async: (raises: [ - CancelledError, LPStreamError], raw: true), base.} = + s: Secure, conn: Connection, initiator: bool, peerId: Opt[PeerId] +): Future[SecureConn] {. + async: (raises: [CancelledError, LPStreamError], raw: true), base +.} = raiseAssert("Not implemented!") proc handleConn( - s: Secure, - conn: Connection, - initiator: bool, - peerId: Opt[PeerId] + s: Secure, conn: Connection, initiator: bool, peerId: Opt[PeerId] ): Future[Connection] {.async: (raises: [CancelledError, LPStreamError]).} = var sconn = await s.handshake(conn, initiator, peerId) # mark connection bottom level transport direction @@ -106,20 +109,26 @@ proc handleConn( let fut1 = conn.join() fut2 = sconn.join() - try: # https://github.com/status-im/nim-chronos/issues/516 + try: # https://github.com/status-im/nim-chronos/issues/516 discard await race(fut1, fut2) - except ValueError: raiseAssert("Futures list is not empty") + except ValueError: + raiseAssert("Futures list is not empty") # at least one join() completed, cancel pending one, if any - if not fut1.finished: await fut1.cancelAndWait() - if not fut2.finished: await fut2.cancelAndWait() + if not fut1.finished: + await fut1.cancelAndWait() + if not fut2.finished: + await fut2.cancelAndWait() block: let fut1 = sconn.close() fut2 = conn.close() await allFutures(fut1, fut2) - static: doAssert typeof(fut1).E is void # Cannot fail - static: doAssert typeof(fut2).E is void # Cannot fail - + static: + doAssert typeof(fut1).E is void + # Cannot fail + static: + doAssert typeof(fut2).E is void + # Cannot fail except CancelledError: # This is top-level procedure which will work as separate task, so it # do not need to propagate CancelledError. @@ -152,17 +161,14 @@ method init*(s: Secure) = s.handler = handle method secure*( - s: Secure, - conn: Connection, - peerId: Opt[PeerId] -): Future[Connection] {.async: (raises: [ - CancelledError, LPStreamError], raw: true), base.} = + s: Secure, conn: Connection, peerId: Opt[PeerId] +): Future[Connection] {. + async: (raises: [CancelledError, LPStreamError], raw: true), base +.} = s.handleConn(conn, conn.dir == Direction.Out, peerId) method readOnce*( - s: SecureConn, - pbytes: pointer, - nbytes: int + s: SecureConn, pbytes: pointer, nbytes: int ): Future[int] {.async: (raises: [CancelledError, LPStreamError]).} = doAssert(nbytes > 0, "nbytes must be positive integer") @@ -182,9 +188,7 @@ method readOnce*( raise exc except LPStreamError as err: debug "Error while reading message from secure connection, closing.", - error = err.name, - message = err.msg, - connection = s + error = err.name, message = err.msg, connection = s await s.close() raise err diff --git a/libp2p/routing_record.nim b/libp2p/routing_record.nim index e5a059b3a8..b5f14b4eea 100644 --- a/libp2p/routing_record.nim +++ b/libp2p/routing_record.nim @@ -13,12 +13,7 @@ import std/[sequtils, times] import pkg/stew/results -import - multiaddress, - multicodec, - peerid, - protobuf/minprotobuf, - signed_envelope +import multiaddress, multicodec, peerid, protobuf/minprotobuf, signed_envelope export peerid, multiaddress, signed_envelope @@ -32,23 +27,22 @@ type addresses*: seq[AddressInfo] proc decode*( - T: typedesc[PeerRecord], - buffer: seq[byte]): Result[PeerRecord, ProtoError] = - + T: typedesc[PeerRecord], buffer: seq[byte] +): Result[PeerRecord, ProtoError] = let pb = initProtoBuffer(buffer) var record = PeerRecord() - ? pb.getRequiredField(1, record.peerId) - ? pb.getRequiredField(2, record.seqNo) + ?pb.getRequiredField(1, record.peerId) + ?pb.getRequiredField(2, record.seqNo) var addressInfos: seq[seq[byte]] - if ? pb.getRepeatedField(3, addressInfos): + if ?pb.getRepeatedField(3, addressInfos): for address in addressInfos: var addressInfo = AddressInfo() let subProto = initProtoBuffer(address) let f = subProto.getField(1, addressInfo.address) if f.get(false): - record.addresses &= addressInfo + record.addresses &= addressInfo if record.addresses.len == 0: return err(ProtoError.RequiredFieldMissing) @@ -69,24 +63,25 @@ proc encode*(record: PeerRecord): seq[byte] = pb.finish() pb.buffer -proc init*(T: typedesc[PeerRecord], - peerId: PeerId, - addresses: seq[MultiAddress], - seqNo = getTime().toUnix().uint64 # follows the recommended implementation, using unix epoch as seq no. - ): T = - +proc init*( + T: typedesc[PeerRecord], + peerId: PeerId, + addresses: seq[MultiAddress], + seqNo = getTime().toUnix().uint64, + # follows the recommended implementation, using unix epoch as seq no. +): T = PeerRecord( - peerId: peerId, - seqNo: seqNo, - addresses: addresses.mapIt(AddressInfo(address: it)) + peerId: peerId, seqNo: seqNo, addresses: addresses.mapIt(AddressInfo(address: it)) ) - ## Functions related to signed peer records type SignedPeerRecord* = SignedPayload[PeerRecord] -proc payloadDomain*(T: typedesc[PeerRecord]): string = $multiCodec("libp2p-peer-record") -proc payloadType*(T: typedesc[PeerRecord]): seq[byte] = @[(byte) 0x03, (byte) 0x01] +proc payloadDomain*(T: typedesc[PeerRecord]): string = + $multiCodec("libp2p-peer-record") + +proc payloadType*(T: typedesc[PeerRecord]): seq[byte] = + @[(byte) 0x03, (byte) 0x01] proc checkValid*(spr: SignedPeerRecord): Result[void, EnvelopeError] = if not spr.data.peerId.match(spr.envelope.publicKey): diff --git a/libp2p/services/autorelayservice.nim b/libp2p/services/autorelayservice.nim index 259287e3f9..d2d909e9da 100644 --- a/libp2p/services/autorelayservice.nim +++ b/libp2p/services/autorelayservice.nim @@ -10,14 +10,13 @@ {.push raises: [].} import chronos, chronicles, times, tables, sequtils -import ../switch, - ../protocols/connectivity/relay/[client, utils] +import ../switch, ../protocols/connectivity/relay/[client, utils] logScope: topics = "libp2p autorelay" type - OnReservationHandler = proc (addresses: seq[MultiAddress]) {.gcsafe, raises: [].} + OnReservationHandler = proc(addresses: seq[MultiAddress]) {.gcsafe, raises: [].} AutoRelayService* = ref object of Service running: bool @@ -36,16 +35,17 @@ proc isRunning*(self: AutoRelayService): bool = return self.running proc addressMapper( - self: AutoRelayService, - listenAddrs: seq[MultiAddress]): Future[seq[MultiAddress]] {.async.} = + self: AutoRelayService, listenAddrs: seq[MultiAddress] +): Future[seq[MultiAddress]] {.async.} = return concat(toSeq(self.relayAddresses.values)) & listenAddrs -proc reserveAndUpdate(self: AutoRelayService, relayPid: PeerId, switch: Switch) {.async.} = +proc reserveAndUpdate( + self: AutoRelayService, relayPid: PeerId, switch: Switch +) {.async.} = while self.running: let rsvp = await self.client.reserve(relayPid).wait(chronos.seconds(5)) - relayedAddr = rsvp.addrs.mapIt( - MultiAddress.init($it & "/p2p-circuit").tryGet()) + relayedAddr = rsvp.addrs.mapIt(MultiAddress.init($it & "/p2p-circuit").tryGet()) ttl = rsvp.expire.int64 - times.now().utc.toTime.toUnix if ttl <= 60: # A reservation under a minute is basically useless @@ -59,7 +59,9 @@ proc reserveAndUpdate(self: AutoRelayService, relayPid: PeerId, switch: Switch) await sleepAsync chronos.seconds(ttl - 30) method setup*(self: AutoRelayService, switch: Switch): Future[bool] {.async.} = - self.addressMapper = proc (listenAddrs: seq[MultiAddress]): Future[seq[MultiAddress]] {.async.} = + self.addressMapper = proc( + listenAddrs: seq[MultiAddress] + ): Future[seq[MultiAddress]] {.async.} = return await addressMapper(self, listenAddrs) let hasBeenSetUp = await procCall Service(self).setup(switch) @@ -68,10 +70,12 @@ method setup*(self: AutoRelayService, switch: Switch): Future[bool] {.async.} = trace "Peer Joined", peerId if self.relayPeers.len < self.numRelays: self.peerAvailable.fire() + proc handlePeerLeft(peerId: PeerId, event: PeerEvent) {.async.} = trace "Peer Left", peerId self.relayPeers.withValue(peerId, future): future[].cancel() + switch.addPeerEventHandler(handlePeerJoined, Joined) switch.addPeerEventHandler(handlePeerLeft, Left) switch.peerInfo.addressMappers.add(self.addressMapper) @@ -100,9 +104,10 @@ proc innerRun(self: AutoRelayService, switch: Switch) {.async.} = # Get all connected relayPeers self.peerAvailable.clear() var connectedPeers = switch.connectedPeers(Direction.Out) - connectedPeers.keepItIf(RelayV2HopCodec in switch.peerStore[ProtoBook][it] and - it notin self.relayPeers and - it notin self.backingOff) + connectedPeers.keepItIf( + RelayV2HopCodec in switch.peerStore[ProtoBook][it] and it notin self.relayPeers and + it notin self.backingOff + ) self.rng.shuffle(connectedPeers) for relayPid in connectedPeers: @@ -135,13 +140,17 @@ method stop*(self: AutoRelayService, switch: Switch): Future[bool] {.async.} = proc getAddresses*(self: AutoRelayService): seq[MultiAddress] = result = concat(toSeq(self.relayAddresses.values)) -proc new*(T: typedesc[AutoRelayService], - numRelays: int, - client: RelayClient, - onReservation: OnReservationHandler, - rng: ref HmacDrbgContext): T = - T(numRelays: numRelays, +proc new*( + T: typedesc[AutoRelayService], + numRelays: int, + client: RelayClient, + onReservation: OnReservationHandler, + rng: ref HmacDrbgContext, +): T = + T( + numRelays: numRelays, client: client, onReservation: onReservation, peerAvailable: newAsyncEvent(), - rng: rng) + rng: rng, + ) diff --git a/libp2p/services/hpservice.nim b/libp2p/services/hpservice.nim index 78b24496d4..272c99b1af 100644 --- a/libp2p/services/hpservice.nim +++ b/libp2p/services/hpservice.nim @@ -24,17 +24,22 @@ import ../multicodec logScope: topics = "libp2p hpservice" -type - HPService* = ref object of Service - newConnectedPeerHandler: PeerEventHandler - onNewStatusHandler: StatusAndConfidenceHandler - autoRelayService: AutoRelayService - autonatService: AutonatService - -proc new*(T: typedesc[HPService], autonatService: AutonatService, autoRelayService: AutoRelayService): T = +type HPService* = ref object of Service + newConnectedPeerHandler: PeerEventHandler + onNewStatusHandler: StatusAndConfidenceHandler + autoRelayService: AutoRelayService + autonatService: AutonatService + +proc new*( + T: typedesc[HPService], + autonatService: AutonatService, + autoRelayService: AutoRelayService, +): T = return T(autonatService: autonatService, autoRelayService: autoRelayService) -proc tryStartingDirectConn(self: HPService, switch: Switch, peerId: PeerId): Future[bool] {.async.} = +proc tryStartingDirectConn( + self: HPService, switch: Switch, peerId: PeerId +): Future[bool] {.async.} = proc tryConnect(address: MultiAddress): Future[bool] {.async.} = debug "Trying to create direct connection", peerId, address await switch.connect(peerId, @[address], true, false) @@ -56,7 +61,9 @@ proc closeRelayConn(relayedConn: Connection) {.async.} = await sleepAsync(2000.milliseconds) # grace period before closing relayed connection await relayedConn.close() -proc newConnectedPeerHandler(self: HPService, switch: Switch, peerId: PeerId, event: PeerEvent) {.async.} = +proc newConnectedPeerHandler( + self: HPService, switch: Switch, peerId: PeerId, event: PeerEvent +) {.async.} = try: # Get all connections to the peer. If there is at least one non-relayed connection, return. let connections = switch.connManager.getConnections()[peerId].mapIt(it.connection) @@ -75,7 +82,8 @@ proc newConnectedPeerHandler(self: HPService, switch: Switch, peerId: PeerId, ev let dcutrClient = DcutrClient.new() var natAddrs = switch.peerStore.getMostObservedProtosAndPorts() if natAddrs.len == 0: - natAddrs = switch.peerInfo.listenAddrs.mapIt(switch.peerStore.guessDialableAddr(it)) + natAddrs = + switch.peerInfo.listenAddrs.mapIt(switch.peerStore.guessDialableAddr(it)) await dcutrClient.startSync(switch, peerId, natAddrs) await closeRelayConn(relayedConn) except CatchableError as err: @@ -89,15 +97,21 @@ method setup*(self: HPService, switch: Switch): Future[bool] {.async.} = let dcutrProto = Dcutr.new(switch) switch.mount(dcutrProto) - self.newConnectedPeerHandler = proc (peerId: PeerId, event: PeerEvent) {.async.} = + self.newConnectedPeerHandler = proc(peerId: PeerId, event: PeerEvent) {.async.} = await newConnectedPeerHandler(self, switch, peerId, event) - switch.connManager.addPeerEventHandler(self.newConnectedPeerHandler, PeerEventKind.Joined) + switch.connManager.addPeerEventHandler( + self.newConnectedPeerHandler, PeerEventKind.Joined + ) - self.onNewStatusHandler = proc (networkReachability: NetworkReachability, confidence: Opt[float]) {.async.} = - if networkReachability == NetworkReachability.NotReachable and not self.autoRelayService.isRunning(): + self.onNewStatusHandler = proc( + networkReachability: NetworkReachability, confidence: Opt[float] + ) {.async.} = + if networkReachability == NetworkReachability.NotReachable and + not self.autoRelayService.isRunning(): discard await self.autoRelayService.setup(switch) - elif networkReachability == NetworkReachability.Reachable and self.autoRelayService.isRunning(): + elif networkReachability == NetworkReachability.Reachable and + self.autoRelayService.isRunning(): discard await self.autoRelayService.stop(switch) # We do it here instead of in the AutonatService because this is useful only when hole punching. @@ -113,4 +127,6 @@ method run*(self: HPService, switch: Switch) {.async, public.} = method stop*(self: HPService, switch: Switch): Future[bool] {.async, public.} = discard await self.autonatService.stop(switch) if not isNil(self.newConnectedPeerHandler): - switch.connManager.removePeerEventHandler(self.newConnectedPeerHandler, PeerEventKind.Joined) + switch.connManager.removePeerEventHandler( + self.newConnectedPeerHandler, PeerEventKind.Joined + ) diff --git a/libp2p/signed_envelope.nim b/libp2p/signed_envelope.nim index 402d6494cd..46a7f897ca 100644 --- a/libp2p/signed_envelope.nim +++ b/libp2p/signed_envelope.nim @@ -13,18 +13,15 @@ import std/sugar import pkg/stew/[results, byteutils] -import multicodec, - crypto/crypto, - protobuf/minprotobuf, - vbuffer +import multicodec, crypto/crypto, protobuf/minprotobuf, vbuffer export crypto type EnvelopeError* = enum - EnvelopeInvalidProtobuf, - EnvelopeFieldMissing, - EnvelopeInvalidSignature, + EnvelopeInvalidProtobuf + EnvelopeFieldMissing + EnvelopeInvalidSignature EnvelopeWrongType Envelope* = object @@ -35,11 +32,9 @@ type signature*: Signature proc mapProtobufError(e: ProtoError): EnvelopeError = - case e: - of RequiredFieldMissing: - EnvelopeFieldMissing - else: - EnvelopeInvalidProtobuf + case e + of RequiredFieldMissing: EnvelopeFieldMissing + else: EnvelopeInvalidProtobuf proc getSignatureBuffer(e: Envelope): seq[byte] = var buffer = initVBuffer() @@ -48,40 +43,42 @@ proc getSignatureBuffer(e: Envelope): seq[byte] = buffer.writeSeq(domainBytes) buffer.writeSeq(e.payloadType) buffer.writeSeq(e.payload) - - buffer.buffer -proc decode*(T: typedesc[Envelope], - buf: seq[byte], - domain: string): Result[Envelope, EnvelopeError] = + buffer.buffer +proc decode*( + T: typedesc[Envelope], buf: seq[byte], domain: string +): Result[Envelope, EnvelopeError] = let pb = initProtoBuffer(buf) var envelope = Envelope() envelope.domain = domain - ? pb.getRequiredField(1, envelope.publicKey).mapErr(mapProtobufError) - discard ? pb.getField(2, envelope.payloadType).mapErr(mapProtobufError) - ? pb.getRequiredField(3, envelope.payload).mapErr(mapProtobufError) - ? pb.getRequiredField(5, envelope.signature).mapErr(mapProtobufError) + ?pb.getRequiredField(1, envelope.publicKey).mapErr(mapProtobufError) + discard ?pb.getField(2, envelope.payloadType).mapErr(mapProtobufError) + ?pb.getRequiredField(3, envelope.payload).mapErr(mapProtobufError) + ?pb.getRequiredField(5, envelope.signature).mapErr(mapProtobufError) - if envelope.signature.verify(envelope.getSignatureBuffer(), envelope.publicKey) == false: + if envelope.signature.verify(envelope.getSignatureBuffer(), envelope.publicKey) == + false: err(EnvelopeInvalidSignature) else: ok(envelope) -proc init*(T: typedesc[Envelope], +proc init*( + T: typedesc[Envelope], privateKey: PrivateKey, payloadType: seq[byte], payload: seq[byte], - domain: string): Result[Envelope, CryptoError] = + domain: string, +): Result[Envelope, CryptoError] = var envelope = Envelope( - publicKey: ? privateKey.getPublicKey(), + publicKey: ?privateKey.getPublicKey(), domain: domain, payloadType: payloadType, payload: payload, ) - envelope.signature = ? privateKey.sign(envelope.getSignatureBuffer()) + envelope.signature = ?privateKey.sign(envelope.getSignatureBuffer()) ok(envelope) @@ -103,66 +100,61 @@ proc payload*(env: Envelope): seq[byte] = # Payload is readonly env.payload -proc getField*(pb: ProtoBuffer, field: int, - value: var Envelope, - domain: string): ProtoResult[bool] {. - inline.} = +proc getField*( + pb: ProtoBuffer, field: int, value: var Envelope, domain: string +): ProtoResult[bool] {.inline.} = var buffer: seq[byte] - let res = ? pb.getField(field, buffer) - if not(res): + let res = ?pb.getField(field, buffer) + if not (res): ok(false) else: - value = Envelope.decode(buffer, domain).valueOr: return err(ProtoError.IncorrectBlob) + value = Envelope.decode(buffer, domain).valueOr: + return err(ProtoError.IncorrectBlob) ok(true) proc write*(pb: var ProtoBuffer, field: int, env: Envelope): Result[void, CryptoError] = - let e = ? env.encode() + let e = ?env.encode() pb.write(field, e) ok() -type - SignedPayload*[T] = object - # T needs to have .encode(), .decode(), .payloadType(), .domain() - envelope*: Envelope - data*: T - -proc init*[T](_: typedesc[SignedPayload[T]], - privateKey: PrivateKey, - data: T): Result[SignedPayload[T], CryptoError] = +type SignedPayload*[T] = object + # T needs to have .encode(), .decode(), .payloadType(), .domain() + envelope*: Envelope + data*: T + +proc init*[T]( + _: typedesc[SignedPayload[T]], privateKey: PrivateKey, data: T +): Result[SignedPayload[T], CryptoError] = mixin encode - let envelope = ? Envelope.init(privateKey, - T.payloadType(), - data.encode(), - T.payloadDomain) + let envelope = + ?Envelope.init(privateKey, T.payloadType(), data.encode(), T.payloadDomain) ok(SignedPayload[T](data: data, envelope: envelope)) -proc getField*[T](pb: ProtoBuffer, field: int, - value: var SignedPayload[T]): ProtoResult[bool] {. - inline.} = - if not ? getField(pb, field, value.envelope, T.payloadDomain): +proc getField*[T]( + pb: ProtoBuffer, field: int, value: var SignedPayload[T] +): ProtoResult[bool] {.inline.} = + if not ?getField(pb, field, value.envelope, T.payloadDomain): ok(false) else: mixin decode - value.data = ? T.decode(value.envelope.payload).mapErr(x => ProtoError.IncorrectBlob) + value.data = ?T.decode(value.envelope.payload).mapErr(x => ProtoError.IncorrectBlob) ok(true) proc decode*[T]( - _: typedesc[SignedPayload[T]], - buffer: seq[byte] - ): Result[SignedPayload[T], EnvelopeError] = - + _: typedesc[SignedPayload[T]], buffer: seq[byte] +): Result[SignedPayload[T], EnvelopeError] = let - envelope = ? Envelope.decode(buffer, T.payloadDomain) - data = ? T.decode(envelope.payload).mapErr(x => EnvelopeInvalidProtobuf) + envelope = ?Envelope.decode(buffer, T.payloadDomain) + data = ?T.decode(envelope.payload).mapErr(x => EnvelopeInvalidProtobuf) signedPayload = SignedPayload[T](envelope: envelope, data: data) if envelope.payloadType != T.payloadType: return err(EnvelopeWrongType) - when compiles(? signedPayload.checkValid()): - ? signedPayload.checkValid() + when compiles(?signedPayload.checkValid()): + ?signedPayload.checkValid() ok(signedPayload) diff --git a/libp2p/stream/bufferstream.nim b/libp2p/stream/bufferstream.nim index 0561ee63ec..b3be7a3722 100644 --- a/libp2p/stream/bufferstream.nim +++ b/libp2p/stream/bufferstream.nim @@ -20,29 +20,32 @@ export connection logScope: topics = "libp2p bufferstream" -const - BufferStreamTrackerName* = "BufferStream" - -type - BufferStream* = ref object of Connection - readQueue*: AsyncQueue[seq[byte]] # read queue for managing backpressure - readBuf*: StreamSeq # overflow buffer for readOnce - pushing*: bool # number of ongoing push operations - reading*: bool # is there an ongoing read? (only allow one) - pushedEof*: bool # eof marker has been put on readQueue - returnedEof*: bool # 0-byte readOnce has been completed +const BufferStreamTrackerName* = "BufferStream" + +type BufferStream* = ref object of Connection + readQueue*: AsyncQueue[seq[byte]] # read queue for managing backpressure + readBuf*: StreamSeq # overflow buffer for readOnce + pushing*: bool # number of ongoing push operations + reading*: bool # is there an ongoing read? (only allow one) + pushedEof*: bool # eof marker has been put on readQueue + returnedEof*: bool # 0-byte readOnce has been completed func shortLog*(s: BufferStream): auto = try: - if s == nil: "BufferStream(nil)" - else: &"{shortLog(s.peerId)}:{s.oid}" + if s == nil: + "BufferStream(nil)" + else: + &"{shortLog(s.peerId)}:{s.oid}" except ValueError as exc: raiseAssert(exc.msg) -chronicles.formatIt(BufferStream): shortLog(it) +chronicles.formatIt(BufferStream): + shortLog(it) proc len*(s: BufferStream): int = - s.readBuf.len + (if s.readQueue.len > 0: s.readQueue[0].len() else: 0) + s.readBuf.len + (if s.readQueue.len > 0: s.readQueue[0].len() + else: 0 + ) method initStream*(s: BufferStream) = if s.objName.len == 0: @@ -54,16 +57,13 @@ method initStream*(s: BufferStream) = trace "BufferStream created", s -proc new*( - T: typedesc[BufferStream], - timeout: Duration = DefaultConnectionTimeout): T = +proc new*(T: typedesc[BufferStream], timeout: Duration = DefaultConnectionTimeout): T = let bufferStream = T(timeout: timeout) bufferStream.initStream() bufferStream method pushData*( - s: BufferStream, - data: seq[byte] + s: BufferStream, data: seq[byte] ) {.base, async: (raises: [CancelledError, LPStreamError]).} = ## Write bytes to internal read buffer, use this to fill up the ## buffer with data. @@ -71,8 +71,7 @@ method pushData*( ## `pushTo` will block if the queue is full, thus maintaining backpressure. ## - doAssert(not s.pushing, - "Only one concurrent push allowed for stream " & s.shortLog()) + doAssert(not s.pushing, "Only one concurrent push allowed for stream " & s.shortLog()) if s.isClosed or s.pushedEof: raise newLPStreamClosedError() @@ -95,8 +94,7 @@ method pushEof*( if s.pushedEof: return - doAssert(not s.pushing, - "Only one concurrent push allowed for stream " & s.shortLog()) + doAssert(not s.pushing, "Only one concurrent push allowed for stream " & s.shortLog()) s.pushedEof = true @@ -113,19 +111,15 @@ method atEof*(s: BufferStream): bool = s.isEof and s.readBuf.len == 0 method readOnce*( - s: BufferStream, - pbytes: pointer, - nbytes: int + s: BufferStream, pbytes: pointer, nbytes: int ): Future[int] {.async: (raises: [CancelledError, LPStreamError]).} = doAssert(nbytes > 0, "nbytes must be positive integer") - doAssert(not s.reading, - "Only one concurrent read allowed for stream " & s.shortLog()) + doAssert(not s.reading, "Only one concurrent read allowed for stream " & s.shortLog()) if s.returnedEof: raise newLPStreamEOFError() - var - p = cast[ptr UncheckedArray[byte]](pbytes) + var p = cast[ptr UncheckedArray[byte]](pbytes) # First consume leftovers from previous read var rbytes = s.readBuf.consumeTo(toOpenArray(p, 0, nbytes - 1)) @@ -149,7 +143,7 @@ method readOnce*( s.isEof = true else: let remaining = min(buf.len, nbytes - rbytes) - toOpenArray(p, rbytes, nbytes - 1)[0.. 0 or t.running: startFuts.add(t.start(addrs)) @@ -358,15 +340,16 @@ proc start*(s: Switch) {.async, public.} = debug "Started libp2p node", peer = s.peerInfo -proc newSwitch*(peerInfo: PeerInfo, - transports: seq[Transport], - secureManagers: openArray[Secure] = [], - connManager: ConnManager, - ms: MultistreamSelect, - peerStore: PeerStore, - nameResolver: NameResolver = nil, - services = newSeq[Service]()): Switch - {.raises: [LPError].} = +proc newSwitch*( + peerInfo: PeerInfo, + transports: seq[Transport], + secureManagers: openArray[Secure] = [], + connManager: ConnManager, + ms: MultistreamSelect, + peerStore: PeerStore, + nameResolver: NameResolver = nil, + services = newSeq[Service](), +): Switch {.raises: [LPError].} = if secureManagers.len == 0: raise newException(LPError, "Provide at least one secure manager") @@ -376,9 +359,11 @@ proc newSwitch*(peerInfo: PeerInfo, transports: transports, connManager: connManager, peerStore: peerStore, - dialer: Dialer.new(peerInfo.peerId, connManager, peerStore, transports, nameResolver), + dialer: + Dialer.new(peerInfo.peerId, connManager, peerStore, transports, nameResolver), nameResolver: nameResolver, - services: services) + services: services, + ) switch.connManager.peerStore = peerStore return switch diff --git a/libp2p/transcoder.nim b/libp2p/transcoder.nim index 7f00fe650b..67627a7cae 100644 --- a/libp2p/transcoder.nim +++ b/libp2p/transcoder.nim @@ -10,10 +10,11 @@ ## This module implements transcoder interface. import vbuffer -type - Transcoder* = object - stringToBuffer*: proc(s: string, - vb: var VBuffer): bool {.nimcall, gcsafe, noSideEffect, raises: [].} - bufferToString*: proc(vb: var VBuffer, - s: var string): bool {.nimcall, gcsafe, noSideEffect, raises: [].} - validateBuffer*: proc(vb: var VBuffer): bool {.nimcall, gcsafe, noSideEffect, raises: [].} +type Transcoder* = object + stringToBuffer*: + proc(s: string, vb: var VBuffer): bool {.nimcall, gcsafe, noSideEffect, raises: [].} + bufferToString*: proc(vb: var VBuffer, s: var string): bool {. + nimcall, gcsafe, noSideEffect, raises: [] + .} + validateBuffer*: + proc(vb: var VBuffer): bool {.nimcall, gcsafe, noSideEffect, raises: [].} diff --git a/libp2p/transports/tcptransport.nim b/libp2p/transports/tcptransport.nim index bcf398c10f..acdd70b4b2 100644 --- a/libp2p/transports/tcptransport.nim +++ b/libp2p/transports/tcptransport.nim @@ -335,4 +335,4 @@ method dial*( method handles*(t: TcpTransport, address: MultiAddress): bool = if procCall Transport(t).handles(address): if address.protocols.isOk: - return TCP.match(address) \ No newline at end of file + return TCP.match(address) diff --git a/libp2p/transports/tortransport.nim b/libp2p/transports/tortransport.nim index d70d1873a3..f98fa83840 100644 --- a/libp2p/transports/tortransport.nim +++ b/libp2p/transports/tortransport.nim @@ -15,13 +15,14 @@ import std/strformat import chronos, chronicles, strutils import stew/[byteutils, endians2, results, objects] import ../multicodec -import transport, - tcptransport, - ../switch, - ../builders, - ../stream/[lpstream, connection, chronosstream], - ../multiaddress, - ../upgrademngrs/upgrade +import + transport, + tcptransport, + ../switch, + ../builders, + ../stream/[lpstream, connection, chronosstream], + ../multiaddress, + ../upgrademngrs/upgrade const IPTcp = mapAnd(IP, mapEq("tcp")) @@ -44,16 +45,24 @@ type NoAcceptableMethod = 0xff Socks5RequestCommand* {.pure.} = enum - Connect = 1, Bind = 2, UdpAssoc = 3 + Connect = 1 + Bind = 2 + UdpAssoc = 3 Socks5AddressType* {.pure.} = enum - IPv4 = 1, FQDN = 3, IPv6 = 4 + IPv4 = 1 + FQDN = 3 + IPv6 = 4 Socks5ReplyType* {.pure.} = enum - Succeeded = (0, "Succeeded"), ServerFailure = (1, "Server Failure"), - ConnectionNotAllowed = (2, "Connection Not Allowed"), NetworkUnreachable = (3, "Network Unreachable"), - HostUnreachable = (4, "Host Unreachable"), ConnectionRefused = (5, "Connection Refused"), - TtlExpired = (6, "Ttl Expired"), CommandNotSupported = (7, "Command Not Supported"), + Succeeded = (0, "Succeeded") + ServerFailure = (1, "Server Failure") + ConnectionNotAllowed = (2, "Connection Not Allowed") + NetworkUnreachable = (3, "Network Unreachable") + HostUnreachable = (4, "Host Unreachable") + ConnectionRefused = (5, "Connection Refused") + TtlExpired = (6, "Ttl Expired") + CommandNotSupported = (7, "Command Not Supported") AddressTypeNotSupported = (8, "Address Type Not Supported") TransportStartError* = object of transport.TransportError @@ -64,16 +73,18 @@ type Socks5ServerReplyError* = object of Socks5Error proc new*( - T: typedesc[TorTransport], - transportAddress: TransportAddress, - flags: set[ServerFlags] = {}, - upgrade: Upgrade): T {.public.} = + T: typedesc[TorTransport], + transportAddress: TransportAddress, + flags: set[ServerFlags] = {}, + upgrade: Upgrade, +): T {.public.} = ## Creates a Tor transport T( transportAddress: transportAddress, upgrader: upgrade, - tcpTransport: TcpTransport.new(flags, upgrade)) + tcpTransport: TcpTransport.new(flags, upgrade), + ) proc handlesDial(address: MultiAddress): bool {.gcsafe.} = return Onion3.match(address) or TCP.match(address) or DNSANY.match(address) @@ -82,10 +93,13 @@ proc handlesStart(address: MultiAddress): bool {.gcsafe.} = return TcpOnion3.match(address) proc connectToTorServer( - transportAddress: TransportAddress): Future[StreamTransport] {.async.} = + transportAddress: TransportAddress +): Future[StreamTransport] {.async.} = let transp = await connect(transportAddress) try: - discard await transp.write(@[Socks5ProtocolVersion, NMethods, Socks5AuthMethod.NoAuth.byte]) + discard await transp.write( + @[Socks5ProtocolVersion, NMethods, Socks5AuthMethod.NoAuth.byte] + ) let serverReply = await transp.read(2) socks5ProtocolVersion = serverReply[0] @@ -115,35 +129,40 @@ proc readServerReply(transp: StreamTransport) {.async.} = if serverReply != Socks5ReplyType.Succeeded.byte: var socks5ReplyType: Socks5ReplyType if socks5ReplyType.checkedEnumAssign(serverReply): - raise newException(Socks5ServerReplyError, fmt"Server reply error: {socks5ReplyType}") + raise + newException(Socks5ServerReplyError, fmt"Server reply error: {socks5ReplyType}") else: raise newException(LPError, fmt"Unexpected server reply: {serverReply}") let atyp = firstFourOctets[3] - case atyp: - of Socks5AddressType.IPv4.byte: - discard await transp.read(ipV4NumOctets + portNumOctets) - of Socks5AddressType.FQDN.byte: - let fqdnNumOctets = await transp.read(1) - discard await transp.read(int(uint8.fromBytes(fqdnNumOctets)) + portNumOctets) - of Socks5AddressType.IPv6.byte: - discard await transp.read(ipV6NumOctets + portNumOctets) - else: - raise newException(LPError, "Address not supported") + case atyp + of Socks5AddressType.IPv4.byte: + discard await transp.read(ipV4NumOctets + portNumOctets) + of Socks5AddressType.FQDN.byte: + let fqdnNumOctets = await transp.read(1) + discard await transp.read(int(uint8.fromBytes(fqdnNumOctets)) + portNumOctets) + of Socks5AddressType.IPv6.byte: + discard await transp.read(ipV6NumOctets + portNumOctets) + else: + raise newException(LPError, "Address not supported") -proc parseOnion3(address: MultiAddress): (byte, seq[byte], seq[byte]) {.raises: [LPError, ValueError].} = +proc parseOnion3( + address: MultiAddress +): (byte, seq[byte], seq[byte]) {.raises: [LPError, ValueError].} = var addressArray = ($address).split('/') - if addressArray.len < 2: raise newException(LPError, fmt"Onion address not supported {address}") + if addressArray.len < 2: + raise newException(LPError, fmt"Onion address not supported {address}") addressArray = addressArray[2].split(':') - if addressArray.len == 0: raise newException(LPError, fmt"Onion address not supported {address}") + if addressArray.len == 0: + raise newException(LPError, fmt"Onion address not supported {address}") let addressStr = addressArray[0] & ".onion" dstAddr = @(uint8(addressStr.len).toBytes()) & addressStr.toBytes() - dstPort = address.data.buffer[37..38] + dstPort = address.data.buffer[37 .. 38] return (Socks5AddressType.FQDN.byte, dstAddr, dstPort) -proc parseIpTcp(address: MultiAddress): - (byte, seq[byte], seq[byte]) - {.raises: [LPError, ValueError].} = +proc parseIpTcp( + address: MultiAddress +): (byte, seq[byte], seq[byte]) {.raises: [LPError, ValueError].} = let (codec, atyp) = if IPv4Tcp.match(address): (multiCodec("ip4"), Socks5AddressType.IPv4.byte) @@ -156,17 +175,16 @@ proc parseIpTcp(address: MultiAddress): dstPort = address[multiCodec("tcp")].tryGet().protoArgument().tryGet() (atyp, dstAddr, dstPort) -proc parseDnsTcp(address: MultiAddress): - (byte, seq[byte], seq[byte]) - {.raises: [LPError, ValueError].} = +proc parseDnsTcp( + address: MultiAddress +): (byte, seq[byte], seq[byte]) {.raises: [LPError, ValueError].} = let dnsAddress = address[multiCodec("dns")].tryGet().protoArgument().tryGet() dstAddr = @(uint8(dnsAddress.len).toBytes()) & dnsAddress dstPort = address[multiCodec("tcp")].tryGet().protoArgument().tryGet() (Socks5AddressType.FQDN.byte, dstAddr, dstPort) -proc dialPeer( - transp: StreamTransport, address: MultiAddress) {.async.} = +proc dialPeer(transp: StreamTransport, address: MultiAddress) {.async.} = let (atyp, dstAddr, dstPort) = if Onion3.match(address): parseOnion3(address) @@ -178,19 +196,18 @@ proc dialPeer( raise newException(LPError, fmt"Address not supported: {address}") let reserved = byte(0) - let request = @[ - Socks5ProtocolVersion, - Socks5RequestCommand.Connect.byte, - reserved, - atyp] & dstAddr & dstPort + let request = + @[Socks5ProtocolVersion, Socks5RequestCommand.Connect.byte, reserved, atyp] & dstAddr & + dstPort discard await transp.write(request) await readServerReply(transp) method dial*( - self: TorTransport, - hostname: string, - address: MultiAddress, - peerId: Opt[PeerId] = Opt.none(PeerId)): Future[Connection] {.async.} = + self: TorTransport, + hostname: string, + address: MultiAddress, + peerId: Opt[PeerId] = Opt.none(PeerId), +): Future[Connection] {.async.} = ## dial a peer ## if not handlesDial(address): @@ -205,9 +222,7 @@ method dial*( await transp.closeWait() raise err -method start*( - self: TorTransport, - addrs: seq[MultiAddress]) {.async.} = +method start*(self: TorTransport, addrs: seq[MultiAddress]) {.async.} = ## listen on the transport ## @@ -215,10 +230,10 @@ method start*( var onion3Addrs: seq[MultiAddress] for i, ma in addrs: if not handlesStart(ma): - warn "Invalid address detected, skipping!", address = ma - continue + warn "Invalid address detected, skipping!", address = ma + continue - let listenAddress = ma[0..1].tryGet() + let listenAddress = ma[0 .. 1].tryGet() listenAddrs.add(listenAddress) let onion3 = ma[multiCodec("onion3")].tryGet() onion3Addrs.add(onion3) @@ -227,7 +242,10 @@ method start*( await procCall Transport(self).start(onion3Addrs) await self.tcpTransport.start(listenAddrs) else: - raise newException(TransportStartError, "Tor Transport couldn't start, no supported addr was provided.") + raise newException( + TransportStartError, + "Tor Transport couldn't start, no supported addr was provided.", + ) method accept*(self: TorTransport): Future[Connection] {.async.} = ## accept a new Tor connection @@ -246,35 +264,37 @@ method handles*(t: TorTransport, address: MultiAddress): bool {.gcsafe.} = if procCall Transport(t).handles(address): return handlesDial(address) or handlesStart(address) -type - TorSwitch* = ref object of Switch +type TorSwitch* = ref object of Switch proc new*( - T: typedesc[TorSwitch], - torServer: TransportAddress, - rng: ref HmacDrbgContext, - addresses: seq[MultiAddress] = @[], - flags: set[ServerFlags] = {}): TorSwitch - {.raises: [LPError], public.} = - var builder = SwitchBuilder.new() - .withRng(rng) - .withTransport(proc(upgr: Upgrade): Transport = TorTransport.new(torServer, flags, upgr)) - if addresses.len != 0: - builder = builder.withAddresses(addresses) - let switch = builder.withMplex() - .withNoise() - .build() - let torSwitch = T( - peerInfo: switch.peerInfo, - ms: switch.ms, - transports: switch.transports, - connManager: switch.connManager, - peerStore: switch.peerStore, - dialer: Dialer.new(switch.peerInfo.peerId, switch.connManager, switch.peerStore, switch.transports, nil), - nameResolver: nil) - - torSwitch.connManager.peerStore = switch.peerStore - return torSwitch + T: typedesc[TorSwitch], + torServer: TransportAddress, + rng: ref HmacDrbgContext, + addresses: seq[MultiAddress] = @[], + flags: set[ServerFlags] = {}, +): TorSwitch {.raises: [LPError], public.} = + var builder = SwitchBuilder.new().withRng(rng).withTransport( + proc(upgr: Upgrade): Transport = + TorTransport.new(torServer, flags, upgr) + ) + if addresses.len != 0: + builder = builder.withAddresses(addresses) + let switch = builder.withMplex().withNoise().build() + let torSwitch = T( + peerInfo: switch.peerInfo, + ms: switch.ms, + transports: switch.transports, + connManager: switch.connManager, + peerStore: switch.peerStore, + dialer: Dialer.new( + switch.peerInfo.peerId, switch.connManager, switch.peerStore, switch.transports, + nil, + ), + nameResolver: nil, + ) + + torSwitch.connManager.peerStore = switch.peerStore + return torSwitch method addTransport*(s: TorSwitch, t: Transport) = doAssert(false, "not implemented!") diff --git a/libp2p/transports/transport.nim b/libp2p/transports/transport.nim index 7eec34cd4b..94c605eb72 100644 --- a/libp2p/transports/transport.nim +++ b/libp2p/transports/transport.nim @@ -12,12 +12,13 @@ import sequtils import chronos, chronicles -import ../stream/connection, - ../multiaddress, - ../multicodec, - ../muxers/muxer, - ../upgrademngrs/upgrade, - ../protocols/connectivity/autonat/core +import + ../stream/connection, + ../multiaddress, + ../multicodec, + ../muxers/muxer, + ../upgrademngrs/upgrade, + ../protocols/connectivity/autonat/core export core.NetworkReachability @@ -36,12 +37,9 @@ type networkReachability*: NetworkReachability proc newTransportClosedError*(parent: ref Exception = nil): ref TransportError = - newException(TransportClosedError, - "Transport closed, no more connections!", parent) + newException(TransportClosedError, "Transport closed, no more connections!", parent) -method start*( - self: Transport, - addrs: seq[MultiAddress]) {.base, async.} = +method start*(self: Transport, addrs: seq[MultiAddress]) {.base, async.} = ## start the transport ## @@ -57,49 +55,42 @@ method stop*(self: Transport) {.base, async.} = trace "stopping transport", address = $self.addrs self.running = false -method accept*(self: Transport): Future[Connection] - {.base, gcsafe.} = +method accept*(self: Transport): Future[Connection] {.base, gcsafe.} = ## accept incoming connections ## doAssert(false, "Not implemented!") method dial*( - self: Transport, - hostname: string, - address: MultiAddress, - peerId: Opt[PeerId] = Opt.none(PeerId)): Future[Connection] {.base, gcsafe.} = + self: Transport, + hostname: string, + address: MultiAddress, + peerId: Opt[PeerId] = Opt.none(PeerId), +): Future[Connection] {.base, gcsafe.} = ## dial a peer ## doAssert(false, "Not implemented!") proc dial*( - self: Transport, - address: MultiAddress, - peerId: Opt[PeerId] = Opt.none(PeerId)): Future[Connection] {.gcsafe.} = + self: Transport, address: MultiAddress, peerId: Opt[PeerId] = Opt.none(PeerId) +): Future[Connection] {.gcsafe.} = self.dial("", address) method upgrade*( - self: Transport, - conn: Connection, - peerId: Opt[PeerId] -): Future[Muxer] {.base, async: (raises: [ - CancelledError, LPError], raw: true).} = + self: Transport, conn: Connection, peerId: Opt[PeerId] +): Future[Muxer] {.base, async: (raises: [CancelledError, LPError], raw: true).} = ## base upgrade method that the transport uses to perform ## transport specific upgrades ## self.upgrader.upgrade(conn, peerId) -method handles*( - self: Transport, - address: MultiAddress): bool {.base, gcsafe.} = +method handles*(self: Transport, address: MultiAddress): bool {.base, gcsafe.} = ## check if transport supports the multiaddress ## # by default we skip circuit addresses to avoid # having to repeat the check in every transport - let protocols = address.protocols.valueOr: return false - protocols - .filterIt( - it == multiCodec("p2p-circuit") - ).len == 0 + let protocols = address.protocols.valueOr: + return false + + protocols.filterIt(it == multiCodec("p2p-circuit")).len == 0 diff --git a/libp2p/transports/wstransport.nim b/libp2p/transports/wstransport.nim index 8760dca897..893b1e4cd8 100644 --- a/libp2p/transports/wstransport.nim +++ b/libp2p/transports/wstransport.nim @@ -14,29 +14,28 @@ import std/[sequtils] import stew/results import chronos, chronicles -import transport, - ../errors, - ../wire, - ../multicodec, - ../multistream, - ../connmanager, - ../multiaddress, - ../utility, - ../stream/connection, - ../upgrademngrs/upgrade, - websock/websock +import + transport, + ../errors, + ../wire, + ../multicodec, + ../multistream, + ../connmanager, + ../multiaddress, + ../utility, + ../stream/connection, + ../upgrademngrs/upgrade, + websock/websock logScope: topics = "libp2p wstransport" export transport, websock, results -const - DefaultHeadersTimeout = 3.seconds +const DefaultHeadersTimeout = 3.seconds -type - WsStream = ref object of Connection - session: WSSession +type WsStream = ref object of Connection + session: WSSession method initStream*(s: WsStream) = if s.objName.len == 0: @@ -49,13 +48,10 @@ proc new*( session: WSSession, dir: Direction, observedAddr: Opt[MultiAddress], - timeout = 10.minutes): T = - - let stream = T( - session: session, - timeout: timeout, - dir: dir, - observedAddr: observedAddr) + timeout = 10.minutes, +): T = + let stream = + T(session: session, timeout: timeout, dir: dir, observedAddr: observedAddr) stream.initStream() return stream @@ -77,9 +73,7 @@ template mapExceptions(body: untyped) = raise newLPStreamEOFError() method readOnce*( - s: WsStream, - pbytes: pointer, - nbytes: int + s: WsStream, pbytes: pointer, nbytes: int ): Future[int] {.async: (raises: [CancelledError, LPStreamError]).} = let res = mapExceptions(await s.session.recv(pbytes, nbytes)) @@ -89,8 +83,7 @@ method readOnce*( return res method write*( - s: WsStream, - msg: seq[byte] + s: WsStream, msg: seq[byte] ): Future[void] {.async: (raises: [CancelledError, LPStreamError]).} = mapExceptions(await s.session.send(msg, Opcode.Binary)) s.activity = true # reset activity flag @@ -102,30 +95,28 @@ method closeImpl*(s: WsStream): Future[void] {.async: (raises: []).} = discard await procCall Connection(s).closeImpl() -method getWrapped*(s: WsStream): Connection = nil +method getWrapped*(s: WsStream): Connection = + nil -type - WsTransport* = ref object of Transport - httpservers: seq[HttpServer] - wsserver: WSServer - connections: array[Direction, seq[WsStream]] +type WsTransport* = ref object of Transport + httpservers: seq[HttpServer] + wsserver: WSServer + connections: array[Direction, seq[WsStream]] - acceptFuts: seq[Future[HttpRequest]] + acceptFuts: seq[Future[HttpRequest]] - tlsPrivateKey: TLSPrivateKey - tlsCertificate: TLSCertificate - tlsFlags: set[TLSFlags] - flags: set[ServerFlags] - handshakeTimeout: Duration - factories: seq[ExtFactory] - rng: ref HmacDrbgContext + tlsPrivateKey: TLSPrivateKey + tlsCertificate: TLSCertificate + tlsFlags: set[TLSFlags] + flags: set[ServerFlags] + handshakeTimeout: Duration + factories: seq[ExtFactory] + rng: ref HmacDrbgContext proc secure*(self: WsTransport): bool = not (isNil(self.tlsPrivateKey) or isNil(self.tlsCertificate)) -method start*( - self: WsTransport, - addrs: seq[MultiAddress]) {.async.} = +method start*(self: WsTransport, addrs: seq[MultiAddress]) {.async.} = ## listen on the transport ## @@ -136,19 +127,18 @@ method start*( await procCall Transport(self).start(addrs) trace "Starting WS transport" - self.wsserver = WSServer.new( - factories = self.factories, - rng = self.rng) - + self.wsserver = WSServer.new(factories = self.factories, rng = self.rng) for i, ma in addrs: let isWss = if WSS.match(ma): - if self.secure: true + if self.secure: + true else: warn "Trying to listen on a WSS address without setting certificate!" false - else: false + else: + false let httpserver = if isWss: @@ -157,11 +147,11 @@ method start*( tlsPrivateKey = self.tlsPrivateKey, tlsCertificate = self.tlsCertificate, flags = self.flags, - handshakeTimeout = self.handshakeTimeout) + handshakeTimeout = self.handshakeTimeout, + ) else: HttpServer.create( - ma.initTAddress().tryGet(), - handshakeTimeout = self.handshakeTimeout + ma.initTAddress().tryGet(), handshakeTimeout = self.handshakeTimeout ) self.httpservers &= httpserver @@ -176,8 +166,8 @@ method start*( MultiAddress.init("/ws") # always get the resolved address in case we're bound to 0.0.0.0:0 - self.addrs[i] = MultiAddress.init( - httpserver.localAddress()).tryGet() & codec.tryGet() + self.addrs[i] = + MultiAddress.init(httpserver.localAddress()).tryGet() & codec.tryGet() trace "Listening on", addresses = self.addrs @@ -196,7 +186,9 @@ method stop*(self: WsTransport) {.async.} = checkFutures( await allFinished( self.connections[Direction.In].mapIt(it.close()) & - self.connections[Direction.Out].mapIt(it.close()))) + self.connections[Direction.Out].mapIt(it.close()) + ) + ) var toWait: seq[Future[void]] for fut in self.acceptFuts: @@ -216,10 +208,9 @@ method stop*(self: WsTransport) {.async.} = except CatchableError as exc: trace "Error shutting down ws transport", exc = exc.msg -proc connHandler(self: WsTransport, - stream: WSSession, - secure: bool, - dir: Direction): Future[Connection] {.async.} = +proc connHandler( + self: WsTransport, stream: WSSession, secure: bool, dir: Direction +): Future[Connection] {.async.} = let observedAddr = try: let @@ -233,7 +224,7 @@ proc connHandler(self: WsTransport, MultiAddress.init(remoteAddr).tryGet() & codec.tryGet() except CatchableError as exc: trace "Failed to create observedAddr", exc = exc.msg - if not(isNil(stream) and stream.stream.reader.closed): + if not (isNil(stream) and stream.stream.reader.closed): await stream.close() raise exc @@ -244,6 +235,7 @@ proc connHandler(self: WsTransport, await conn.session.stream.reader.join() self.connections[dir].keepItIf(it != conn) trace "Cleaned up client" + asyncSpawn onClose() return conn @@ -302,10 +294,11 @@ method accept*(self: WsTransport): Future[Connection] {.async.} = raise exc method dial*( - self: WsTransport, - hostname: string, - address: MultiAddress, - peerId: Opt[PeerId] = Opt.none(PeerId)): Future[Connection] {.async.} = + self: WsTransport, + hostname: string, + address: MultiAddress, + peerId: Opt[PeerId] = Opt.none(PeerId), +): Future[Connection] {.async.} = ## dial a peer ## @@ -318,7 +311,8 @@ method dial*( "", secure = secure, hostName = hostname, - flags = self.tlsFlags) + flags = self.tlsFlags, + ) try: return await self.connHandler(transp, secure, Direction.Out) @@ -332,15 +326,16 @@ method handles*(t: WsTransport, address: MultiAddress): bool {.gcsafe.} = return WebSockets.match(address) proc new*( - T: typedesc[WsTransport], - upgrade: Upgrade, - tlsPrivateKey: TLSPrivateKey, - tlsCertificate: TLSCertificate, - tlsFlags: set[TLSFlags] = {}, - flags: set[ServerFlags] = {}, - factories: openArray[ExtFactory] = [], - rng: ref HmacDrbgContext = nil, - handshakeTimeout = DefaultHeadersTimeout): T {.public.} = + T: typedesc[WsTransport], + upgrade: Upgrade, + tlsPrivateKey: TLSPrivateKey, + tlsCertificate: TLSCertificate, + tlsFlags: set[TLSFlags] = {}, + flags: set[ServerFlags] = {}, + factories: openArray[ExtFactory] = [], + rng: ref HmacDrbgContext = nil, + handshakeTimeout = DefaultHeadersTimeout, +): T {.public.} = ## Creates a secure WebSocket transport T( @@ -351,15 +346,17 @@ proc new*( flags: flags, factories: @factories, rng: rng, - handshakeTimeout: handshakeTimeout) + handshakeTimeout: handshakeTimeout, + ) proc new*( - T: typedesc[WsTransport], - upgrade: Upgrade, - flags: set[ServerFlags] = {}, - factories: openArray[ExtFactory] = [], - rng: ref HmacDrbgContext = nil, - handshakeTimeout = DefaultHeadersTimeout): T {.public.} = + T: typedesc[WsTransport], + upgrade: Upgrade, + flags: set[ServerFlags] = {}, + factories: openArray[ExtFactory] = [], + rng: ref HmacDrbgContext = nil, + handshakeTimeout = DefaultHeadersTimeout, +): T {.public.} = ## Creates a clear-text WebSocket transport T.new( @@ -369,4 +366,5 @@ proc new*( flags = flags, factories = @factories, rng = rng, - handshakeTimeout = handshakeTimeout) + handshakeTimeout = handshakeTimeout, + ) diff --git a/libp2p/upgrademngrs/muxedupgrade.nim b/libp2p/upgrademngrs/muxedupgrade.nim index 1a3052ab84..a096791cc5 100644 --- a/libp2p/upgrademngrs/muxedupgrade.nim +++ b/libp2p/upgrademngrs/muxedupgrade.nim @@ -12,21 +12,18 @@ import std/sequtils import pkg/[chronos, chronicles, metrics] -import ../upgrademngrs/upgrade, - ../muxers/muxer +import ../upgrademngrs/upgrade, ../muxers/muxer export Upgrade logScope: topics = "libp2p muxedupgrade" -type - MuxedUpgrade* = ref object of Upgrade - muxers*: seq[MuxerProvider] - streamHandler*: StreamHandler +type MuxedUpgrade* = ref object of Upgrade + muxers*: seq[MuxerProvider] + streamHandler*: StreamHandler -func getMuxerByCodec( - self: MuxedUpgrade, muxerName: string): Opt[MuxerProvider] = +func getMuxerByCodec(self: MuxedUpgrade, muxerName: string): Opt[MuxerProvider] = if muxerName.len == 0 or muxerName == "na": return Opt.none(MuxerProvider) for m in self.muxers: @@ -35,10 +32,10 @@ func getMuxerByCodec( Opt.none(MuxerProvider) proc mux( - self: MuxedUpgrade, - conn: Connection -): Future[Opt[Muxer]] {.async: (raises: [ - CancelledError, LPStreamError, MultiStreamError]).} = + self: MuxedUpgrade, conn: Connection +): Future[Opt[Muxer]] {. + async: (raises: [CancelledError, LPStreamError, MultiStreamError]) +.} = ## mux connection trace "Muxing connection", conn if self.muxers.len == 0: @@ -67,28 +64,25 @@ proc mux( Opt.some(muxer) method upgrade*( - self: MuxedUpgrade, - conn: Connection, - peerId: Opt[PeerId] + self: MuxedUpgrade, conn: Connection, peerId: Opt[PeerId] ): Future[Muxer] {.async: (raises: [CancelledError, LPError]).} = trace "Upgrading connection", conn, direction = conn.dir - let sconn = await self.secure(conn, peerId) # secure the connection + let sconn = await self.secure(conn, peerId) # secure the connection if sconn == nil: - raise (ref UpgradeFailedError)(msg: - "unable to secure connection, stopping upgrade") + raise (ref UpgradeFailedError)(msg: "unable to secure connection, stopping upgrade") - let muxer = (await self.mux(sconn)).valueOr: # mux it if possible - raise (ref UpgradeFailedError)(msg: - "a muxer is required for outgoing connections") + let muxer = (await self.mux(sconn)).valueOr: + raise (ref UpgradeFailedError)(msg: "a muxer is required for outgoing connections") when defined(libp2p_agents_metrics): conn.shortAgent = muxer.connection.shortAgent if sconn.closed(): await sconn.close() - raise (ref UpgradeFailedError)(msg: - "Connection closed or missing peer info, stopping upgrade") + raise (ref UpgradeFailedError)( + msg: "Connection closed or missing peer info, stopping upgrade" + ) trace "Upgraded connection", conn, sconn, direction = conn.dir muxer @@ -97,11 +91,9 @@ proc new*( T: type MuxedUpgrade, muxers: seq[MuxerProvider], secureManagers: openArray[Secure] = [], - ms: MultistreamSelect): T = - let upgrader = T( - muxers: muxers, - secureManagers: @secureManagers, - ms: ms) + ms: MultistreamSelect, +): T = + let upgrader = T(muxers: muxers, secureManagers: @secureManagers, ms: ms) upgrader.streamHandler = proc(conn: Connection) {.async: (raises: []).} = trace "Starting stream handler", conn diff --git a/libp2p/upgrademngrs/upgrade.nim b/libp2p/upgrademngrs/upgrade.nim index cb120a4375..bc5b10de1e 100644 --- a/libp2p/upgrademngrs/upgrade.nim +++ b/libp2p/upgrademngrs/upgrade.nim @@ -13,21 +13,24 @@ import std/[sequtils, strutils] import pkg/[chronos, chronicles, metrics] -import ../stream/connection, - ../protocols/secure/secure, - ../protocols/identify, - ../muxers/muxer, - ../multistream, - ../connmanager, - ../errors, - ../utility +import + ../stream/connection, + ../protocols/secure/secure, + ../protocols/identify, + ../muxers/muxer, + ../multistream, + ../connmanager, + ../errors, + ../utility export connmanager, connection, identify, secure, multistream -declarePublicCounter(libp2p_failed_upgrades_incoming, - "incoming connections failed upgrades") -declarePublicCounter(libp2p_failed_upgrades_outgoing, - "outgoing connections failed upgrades") +declarePublicCounter( + libp2p_failed_upgrades_incoming, "incoming connections failed upgrades" +) +declarePublicCounter( + libp2p_failed_upgrades_outgoing, "outgoing connections failed upgrades" +) logScope: topics = "libp2p upgrade" @@ -40,17 +43,12 @@ type secureManagers*: seq[Secure] method upgrade*( - self: Upgrade, - conn: Connection, - peerId: Opt[PeerId] -): Future[Muxer] {.async: (raises: [ - CancelledError, LPError], raw: true), base.} = + self: Upgrade, conn: Connection, peerId: Opt[PeerId] +): Future[Muxer] {.async: (raises: [CancelledError, LPError], raw: true), base.} = raiseAssert("Not implemented!") proc secure*( - self: Upgrade, - conn: Connection, - peerId: Opt[PeerId] + self: Upgrade, conn: Connection, peerId: Opt[PeerId] ): Future[Connection] {.async: (raises: [CancelledError, LPError]).} = if self.secureManagers.len <= 0: raise (ref UpgradeFailedError)(msg: "No secure managers registered!") diff --git a/libp2p/utility.nim b/libp2p/utility.nim index 2fa236c4ca..8b0870a452 100644 --- a/libp2p/utility.nim +++ b/libp2p/utility.nim @@ -14,16 +14,12 @@ import stew/[byteutils, results] export results -template public* {.pragma.} +template public*() {.pragma.} -const - ShortDumpMax = 12 +const ShortDumpMax = 12 template compilesOr*(a, b: untyped): untyped = - when compiles(a): - a - else: - b + when compiles(a): a else: b func shortLog*(item: openArray[byte]): string = if item.len <= ShortDumpMax: @@ -45,9 +41,9 @@ func shortLog*(item: string): string = split = ShortDumpMax div 2 dumpLen = ShortDumpMax + 3 result = newStringOfCap(dumpLen) - result &= item[0.. itv: - info "Missed multiple heartbeats", heartbeat = name, - delay = delay, hinterval = itv + info "Missed multiple heartbeats", + heartbeat = name, delay = delay, hinterval = itv else: - debug "Missed heartbeat", heartbeat = name, - delay = delay, hinterval = itv + debug "Missed heartbeat", heartbeat = name, delay = delay, hinterval = itv nextHeartbeat = now + itv await sleepAsync(nextHeartbeat - now) diff --git a/libp2p/utils/offsettedseq.nim b/libp2p/utils/offsettedseq.nim index 4e28c5ff21..ec723515c8 100644 --- a/libp2p/utils/offsettedseq.nim +++ b/libp2p/utils/offsettedseq.nim @@ -9,48 +9,49 @@ import sequtils -type - OffsettedSeq*[T] = object - s*: seq[T] - offset*: int +type OffsettedSeq*[T] = object + s*: seq[T] + offset*: int proc initOffsettedSeq*[T](offset: int = 0): OffsettedSeq[T] = OffsettedSeq[T](s: newSeq[T](), offset: offset) -proc all*[T](o: OffsettedSeq[T], pred: proc (x: T): bool): bool = +proc all*[T](o: OffsettedSeq[T], pred: proc(x: T): bool): bool = o.s.all(pred) -proc any*[T](o: OffsettedSeq[T], pred: proc (x: T): bool): bool = +proc any*[T](o: OffsettedSeq[T], pred: proc(x: T): bool): bool = o.s.any(pred) -proc apply*[T](o: OffsettedSeq[T], op: proc (x: T)) = +proc apply*[T](o: OffsettedSeq[T], op: proc(x: T)) = o.s.apply(pred) -proc apply*[T](o: OffsettedSeq[T], op: proc (x: T): T) = +proc apply*[T](o: OffsettedSeq[T], op: proc(x: T): T) = o.s.apply(pred) -proc apply*[T](o: OffsettedSeq[T], op: proc (x: var T)) = +proc apply*[T](o: OffsettedSeq[T], op: proc(x: var T)) = o.s.apply(pred) func count*[T](o: OffsettedSeq[T], x: T): int = o.s.count(x) -proc flushIf*[T](o: OffsettedSeq[T], pred: proc (x: T): bool) = +proc flushIf*[T](o: OffsettedSeq[T], pred: proc(x: T): bool) = var i = 0 for e in o.s: - if not pred(e): break + if not pred(e): + break i.inc() if i > 0: - o.s.delete(0.. 0: - o.s.delete(0.. 0: @@ -91,6 +90,5 @@ proc release*(s: AsyncSemaphore) = fut.complete() break - trace "Released slot", available = s.count, - queue = s.queue.len + trace "Released slot", available = s.count, queue = s.queue.len return diff --git a/libp2p/varint.nim b/libp2p/varint.nim index 150267d578..f58985f5ab 100644 --- a/libp2p/varint.nim +++ b/libp2p/varint.nim @@ -23,18 +23,16 @@ export leb128, results type VarintError* {.pure.} = enum - Error, - Overflow, - Incomplete, - Overlong, + Error + Overflow + Incomplete + Overlong Overrun VarintResult*[T] = Result[T, VarintError] - PB* = object - ## Use this type to specify Google ProtoBuf's varint encoding - LP* = object - ## Use this type to specify LibP2P varint encoding + PB* = object ## Use this type to specify Google ProtoBuf's varint encoding + LP* = object ## Use this type to specify LibP2P varint encoding zint32* = distinct int32 zint64* = distinct int64 @@ -43,8 +41,7 @@ type hint32* = distinct int32 hint64* = distinct int64 - hint* = distinct int - ## Signed integer types which will be encoded using simple cast. + hint* = distinct int ## Signed integer types which will be encoded using simple cast. PBSomeUVarint* = uint | uint64 | uint32 PBSomeSVarint* = hint | hint64 | hint32 @@ -55,7 +52,8 @@ type SomeVarint* = PBSomeVarint | LPSomeVarint SomeUVarint* = PBSomeUVarint | LPSomeUVarint -template toUleb[T: uint64|uint32|uint16|uint8|uint](x: T): T = x +template toUleb[T: uint64 | uint32 | uint16 | uint8 | uint](x: T): T = + x func toUleb(x: zint64): uint64 = let v = cast[uint64](x) @@ -65,8 +63,11 @@ func toUleb(x: zint32): uint32 = let v = cast[uint32](x) (v shl 1) xor (0 - (v shr 31)) -template toUleb(x: hint64): uint64 = cast[uint64](x) -template toUleb(x: hint32): uint32 = cast[uint32](x) +template toUleb(x: hint64): uint64 = + cast[uint64](x) + +template toUleb(x: hint32): uint32 = + cast[uint32](x) template toUleb(x: zint): uint64 = when sizeof(x) == sizeof(zint64): @@ -80,8 +81,11 @@ template toUleb(x: hint): uint = else: uint(toUleb(hint32(x))) -template fromUleb(x: uint64, T: type uint64): T = x -template fromUleb(x: uint32, T: type uint32): T = x +template fromUleb(x: uint64, T: type uint64): T = + x + +template fromUleb(x: uint32, T: type uint32): T = + x template fromUleb(x: uint64, T: type zint64): T = cast[T]((x shr 1) xor (0 - (x and 1))) @@ -99,10 +103,12 @@ proc vsizeof*(x: SomeVarint): int {.inline.} = ## Returns number of bytes required to encode integer ``x`` as varint. Leb128.len(toUleb(x)) -proc getUVarint*[T: PB|LP](vtype: typedesc[T], - pbytes: openArray[byte], - outlen: var int, - outval: var SomeUVarint): VarintResult[void] = +proc getUVarint*[T: PB | LP]( + vtype: typedesc[T], + pbytes: openArray[byte], + outlen: var int, + outval: var SomeUVarint, +): VarintResult[void] = ## Decode `unsigned varint` from buffer ``pbytes`` and store it to ``outval``. ## On success ``outlen`` will be set to number of bytes processed while ## decoding `unsigned varint`. @@ -145,10 +151,12 @@ proc getUVarint*[T: PB|LP](vtype: typedesc[T], ok() -proc putUVarint*[T: PB|LP](vtype: typedesc[T], - pbytes: var openArray[byte], - outlen: var int, - outval: SomeUVarint): VarintResult[void] = +proc putUVarint*[T: PB | LP]( + vtype: typedesc[T], + pbytes: var openArray[byte], + outlen: var int, + outval: SomeUVarint, +): VarintResult[void] = ## Encode `unsigned varint` ``outval`` and store it to array ``pbytes``. ## ## On success ``outlen`` will hold number of bytes (octets) used to encode @@ -172,13 +180,14 @@ proc putUVarint*[T: PB|LP](vtype: typedesc[T], let bytes = toBytes(outval, Leb128) outlen = len(bytes) if len(pbytes) >= outlen: - pbytes[0.. 0: copyMem(addr vb.buffer[vb.offset], unsafeAddr value[0], len(value)) vb.offset += len(value) -proc writeArray*[T: byte|char](vb: var VBuffer, value: openArray[T]) = +proc writeArray*[T: byte | char](vb: var VBuffer, value: openArray[T]) = ## Write array ``value`` to buffer ``vb``, value will NOT be prefixed with ## varint length of the array. if len(value) > 0: @@ -111,12 +109,12 @@ proc peekVarint*(vb: var VBuffer, value: var LPSomeUVarint): int = value = cast[type(value)](0) var length = 0 if not vb.isEmpty(): - let res = LP.getUVarint( - toOpenArray(vb.buffer, vb.offset, vb.buffer.high), length, value) + let res = + LP.getUVarint(toOpenArray(vb.buffer, vb.offset, vb.buffer.high), length, value) if res.isOk(): result = length -proc peekSeq*[T: string|seq[byte]](vb: var VBuffer, value: var T): int = +proc peekSeq*[T: string | seq[byte]](vb: var VBuffer, value: var T): int = ## Peek length prefixed array from buffer ``vb`` and store result to ## ``value``. ## @@ -128,7 +126,9 @@ proc peekSeq*[T: string|seq[byte]](vb: var VBuffer, value: var T): int = var length = 0 var size = 0'u64 if not vb.isEmpty() and - LP.getUVarint(toOpenArray(vb.buffer, vb.offset, vb.buffer.high), length, size).isOk(): + LP + .getUVarint(toOpenArray(vb.buffer, vb.offset, vb.buffer.high), length, size) + .isOk(): vb.offset += length result = length if vb.isEnough(int(size)): @@ -138,8 +138,7 @@ proc peekSeq*[T: string|seq[byte]](vb: var VBuffer, value: var T): int = result += int(size) vb.offset -= length -proc peekArray*[T: char|byte](vb: var VBuffer, - value: var openArray[T]): int = +proc peekArray*[T: char | byte](vb: var VBuffer, value: var openArray[T]): int = ## Peek array from buffer ``vb`` and store result to ``value``. ## ## This procedure will not adjust internal offset. @@ -160,8 +159,7 @@ proc readVarint*(vb: var VBuffer, value: var LPSomeUVarint): int {.inline.} = if result != -1: vb.offset += result -proc readSeq*[T: string|seq[byte]](vb: var VBuffer, - value: var T): int {.inline.} = +proc readSeq*[T: string | seq[byte]](vb: var VBuffer, value: var T): int {.inline.} = ## Read length prefixed array from buffer ``vb`` and store result to ## ``value``. ## @@ -170,8 +168,9 @@ proc readSeq*[T: string|seq[byte]](vb: var VBuffer, if result != -1: vb.offset += result -proc readArray*[T: char|byte](vb: var VBuffer, - value: var openArray[T]): int {.inline.} = +proc readArray*[T: char | byte]( + vb: var VBuffer, value: var openArray[T] +): int {.inline.} = ## Read array from buffer ``vb`` and store result to ``value``. ## ## Returns number of bytes consumed from ``vb`` or ``-1`` on error. @@ -183,5 +182,5 @@ proc `$`*(vb: VBuffer): string = ## Return hexadecimal string representation of buffer ``vb``. let length = (len(vb.buffer) - vb.offset) * 2 result = newStringOfCap(length) - for i in 0.. 0: - var combinedExpr = n[0] # Start with the first expression - for i in 1.. (start + `timeout`): - checkpoint("[TIMEOUT] Timeout was reached and the conditions were not true. Check if the code is working as " & - "expected or consider increasing the timeout param.") + checkpoint( + "[TIMEOUT] Timeout was reached and the conditions were not true. Check if the code is working as " & + "expected or consider increasing the timeout param." + ) check `code` return else: - if `combinedBoolExpr`: - return - else: - await sleepAsync(1.millis) + if `combinedBoolExpr`: + return + else: + await sleepAsync(1.millis) + await checkExpiringInternal() macro checkUntilTimeout*(code: untyped): untyped = @@ -191,7 +187,7 @@ macro checkUntilTimeout*(code: untyped): untyped = ## a == 2 ## b == 1 ## ``` - result = quote do: + result = quote: checkUntilCustomTimeout(10.seconds, `code`) proc unorderedCompare*[T](a, b: seq[T]): bool = diff --git a/tests/hole-punching-interop/hole_punching.nim b/tests/hole-punching-interop/hole_punching.nim index 184bfcbf13..4e995dac35 100644 --- a/tests/hole-punching-interop/hole_punching.nim +++ b/tests/hole-punching-interop/hole_punching.nim @@ -1,25 +1,29 @@ import std/[os, options, strformat, sequtils] import redis import chronos, chronicles -import ../../libp2p/[builders, - switch, - multicodec, - observedaddrmanager, - services/hpservice, - services/autorelayservice, - protocols/connectivity/autonat/client as aclient, - protocols/connectivity/relay/client as rclient, - protocols/connectivity/relay/relay, - protocols/connectivity/autonat/service, - protocols/ping] +import + ../../libp2p/[ + builders, + switch, + multicodec, + observedaddrmanager, + services/hpservice, + services/autorelayservice, + protocols/connectivity/autonat/client as aclient, + protocols/connectivity/relay/client as rclient, + protocols/connectivity/relay/relay, + protocols/connectivity/autonat/service, + protocols/ping, + ] import ../stubs/autonatclientstub import ../errorhelpers proc createSwitch(r: Relay = nil, hpService: Service = nil): Switch = let rng = newRng() - var builder = SwitchBuilder.new() + var builder = SwitchBuilder + .new() .withRng(rng) - .withAddresses(@[ MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet() ]) + .withAddresses(@[MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet()]) .withObservedAddrManager(ObservedAddrManager.new(maxSize = 1, minCount = 1)) .withTcpTransport({ServerFlags.TcpNoDelay}) .withYamux() @@ -32,8 +36,8 @@ proc createSwitch(r: Relay = nil, hpService: Service = nil): Switch = if r != nil: builder = builder.withCircuitRelay(r) - let s = builder.build() - s.mount(Ping.new(rng=rng)) + let s = builder.build() + s.mount(Ping.new(rng = rng)) return s proc main() {.async.} = @@ -42,7 +46,8 @@ proc main() {.async.} = let autoRelayService = AutoRelayService.new(1, relayClient, nil, newRng()) let autonatClientStub = AutonatClientStub.new(expectedDials = 1) autonatClientStub.answer = NotReachable - let autonatService = AutonatService.new(autonatClientStub, newRng(), maxQueueSize = 1) + let autonatService = + AutonatService.new(autonatClientStub, newRng(), maxQueueSize = 1) let hpservice = HPService.new(autonatService, autoRelayService) let @@ -77,7 +82,10 @@ proc main() {.async.} = debug "Connected to relay", relayId # Wait for our relay address to be published - while not switch.peerInfo.addrs.anyIt(it.contains(multiCodec("p2p-circuit")).tryGet()): + while not switch.peerInfo.addrs.anyIt( + it.contains(multiCodec("p2p-circuit")).tryGet() + ) + : await sleepAsync(100.milliseconds) if isListener: @@ -106,7 +114,9 @@ proc main() {.async.} = let conn = switch.connManager.selectMuxer(listenerId).connection let channel = await switch.dial(listenerId, @[listenerRelayAddr], PingCodec) let delay = await Ping.new().ping(channel) - await allFuturesThrowing(channel.close(), conn.close(), switch.stop(), auxSwitch.stop()) + await allFuturesThrowing( + channel.close(), conn.close(), switch.stop(), auxSwitch.stop() + ) echo &"""{{"rtt_to_holepunched_peer_millis":{delay.millis}}}""" quit(0) except CatchableError as e: diff --git a/tests/pubsub/config.nims b/tests/pubsub/config.nims index 5662c5d9dc..8531b42186 100644 --- a/tests/pubsub/config.nims +++ b/tests/pubsub/config.nims @@ -1,7 +1,7 @@ import strutils -proc hasSkipParentCfg: bool = - for param in 0.. 0, "waitSub timeout!") @@ -51,10 +53,7 @@ suite "FloodSub": nodes = generateNodes(2) # start switches - nodesFut = await allFinished( - nodes[0].switch.start(), - nodes[1].switch.start(), - ) + nodesFut = await allFinished(nodes[0].switch.start(), nodes[1].switch.start()) await subscribeNodes(nodes) @@ -72,10 +71,7 @@ suite "FloodSub": agentA == "nim-libp2p" agentB == "nim-libp2p" - await allFuturesThrowing( - nodes[0].switch.stop(), - nodes[1].switch.stop() - ) + await allFuturesThrowing(nodes[0].switch.stop(), nodes[1].switch.stop()) await allFuturesThrowing(nodesFut.concat()) @@ -89,11 +85,7 @@ suite "FloodSub": nodes = generateNodes(2) # start switches - nodesFut = await allFinished( - nodes[0].switch.start(), - nodes[1].switch.start(), - ) - + nodesFut = await allFinished(nodes[0].switch.start(), nodes[1].switch.start()) await subscribeNodes(nodes) @@ -104,10 +96,7 @@ suite "FloodSub": check (await completionFut.wait(5.seconds)) == true - await allFuturesThrowing( - nodes[0].switch.stop(), - nodes[1].switch.stop() - ) + await allFuturesThrowing(nodes[0].switch.stop(), nodes[1].switch.stop()) await allFuturesThrowing(nodesFut) @@ -121,10 +110,7 @@ suite "FloodSub": nodes = generateNodes(2) # start switches - nodesFut = await allFinished( - nodes[0].switch.start(), - nodes[1].switch.start(), - ) + nodesFut = await allFinished(nodes[0].switch.start(), nodes[1].switch.start()) await subscribeNodes(nodes) @@ -132,8 +118,9 @@ suite "FloodSub": await waitSub(nodes[0], nodes[1], "foobar") var validatorFut = newFuture[bool]() - proc validator(topic: string, - message: Message): Future[ValidationResult] {.async.} = + proc validator( + topic: string, message: Message + ): Future[ValidationResult] {.async.} = check topic == "foobar" validatorFut.complete(true) result = ValidationResult.Accept @@ -143,10 +130,7 @@ suite "FloodSub": check (await nodes[0].publish("foobar", "Hello!".toBytes())) > 0 check (await handlerFut) == true - await allFuturesThrowing( - nodes[0].switch.stop(), - nodes[1].switch.stop() - ) + await allFuturesThrowing(nodes[0].switch.stop(), nodes[1].switch.stop()) await allFuturesThrowing(nodesFut) @@ -158,18 +142,16 @@ suite "FloodSub": nodes = generateNodes(2) # start switches - nodesFut = await allFinished( - nodes[0].switch.start(), - nodes[1].switch.start(), - ) + nodesFut = await allFinished(nodes[0].switch.start(), nodes[1].switch.start()) await subscribeNodes(nodes) nodes[1].subscribe("foobar", handler) await waitSub(nodes[0], nodes[1], "foobar") var validatorFut = newFuture[bool]() - proc validator(topic: string, - message: Message): Future[ValidationResult] {.async.} = + proc validator( + topic: string, message: Message + ): Future[ValidationResult] {.async.} = validatorFut.complete(true) result = ValidationResult.Reject @@ -177,10 +159,7 @@ suite "FloodSub": discard await nodes[0].publish("foobar", "Hello!".toBytes()) - await allFuturesThrowing( - nodes[0].switch.stop(), - nodes[1].switch.stop() - ) + await allFuturesThrowing(nodes[0].switch.stop(), nodes[1].switch.stop()) await allFuturesThrowing(nodesFut) @@ -194,10 +173,7 @@ suite "FloodSub": nodes = generateNodes(2) # start switches - nodesFut = await allFinished( - nodes[0].switch.start(), - nodes[1].switch.start(), - ) + nodesFut = await allFinished(nodes[0].switch.start(), nodes[1].switch.start()) await subscribeNodes(nodes) nodes[1].subscribe("foo", handler) @@ -205,8 +181,9 @@ suite "FloodSub": nodes[1].subscribe("bar", handler) await waitSub(nodes[0], nodes[1], "bar") - proc validator(topic: string, - message: Message): Future[ValidationResult] {.async.} = + proc validator( + topic: string, message: Message + ): Future[ValidationResult] {.async.} = if topic == "foo": result = ValidationResult.Accept else: @@ -217,10 +194,7 @@ suite "FloodSub": check (await nodes[0].publish("foo", "Hello!".toBytes())) > 0 check (await nodes[0].publish("bar", "Hello!".toBytes())) > 0 - await allFuturesThrowing( - nodes[0].switch.stop(), - nodes[1].switch.stop() - ) + await allFuturesThrowing(nodes[0].switch.stop(), nodes[1].switch.stop()) await allFuturesThrowing(nodesFut) @@ -228,19 +202,21 @@ suite "FloodSub": var runs = 10 var futs = newSeq[(Future[void], TopicHandler, ref int)](runs) - for i in 0.. 0 check (await bigNode[0].publish("foo", smallMessage2)) > 0 - checkUntilTimeout: messageReceived == 2 + checkUntilTimeout: + messageReceived == 2 check (await smallNode[0].publish("foo", bigMessage)) > 0 check (await bigNode[0].publish("foo", bigMessage)) > 0 - await allFuturesThrowing( - smallNode[0].switch.stop(), - bigNode[0].switch.stop() - ) + await allFuturesThrowing(smallNode[0].switch.stop(), bigNode[0].switch.stop()) await allFuturesThrowing(nodesFut) @@ -383,10 +351,8 @@ suite "FloodSub": bigNode2 = generateNodes(1, maxMessageSize = 20000000) # start switches - nodesFut = await allFinished( - bigNode1[0].switch.start(), - bigNode2[0].switch.start(), - ) + nodesFut = + await allFinished(bigNode1[0].switch.start(), bigNode2[0].switch.start()) await subscribeNodes(bigNode1 & bigNode2) bigNode2[0].subscribe("foo", handler) @@ -396,11 +362,9 @@ suite "FloodSub": check (await bigNode1[0].publish("foo", bigMessage)) > 0 - checkUntilTimeout: messageReceived == 1 + checkUntilTimeout: + messageReceived == 1 - await allFuturesThrowing( - bigNode1[0].switch.stop(), - bigNode2[0].switch.stop() - ) + await allFuturesThrowing(bigNode1[0].switch.stop(), bigNode2[0].switch.stop()) await allFuturesThrowing(nodesFut) diff --git a/tests/pubsub/testgossipinternal.nim b/tests/pubsub/testgossipinternal.nim index 96032b5fd0..95fbc3899a 100644 --- a/tests/pubsub/testgossipinternal.nim +++ b/tests/pubsub/testgossipinternal.nim @@ -45,7 +45,7 @@ suite "GossipSub internal": var conns = newSeq[Connection]() gossipSub.gossipsub[topic] = initHashSet[PubSubPeer]() - for i in 0..<15: + for i in 0 ..< 15: let conn = TestBufferStream.new(noop) conns &= conn let peerId = randomPeerId() @@ -86,7 +86,7 @@ suite "GossipSub internal": var conns = newSeq[Connection]() gossipSub.gossipsub[topic] = initHashSet[PubSubPeer]() - for i in 0..<15: + for i in 0 ..< 15: let conn = TestBufferStream.new(noop) conns &= conn let peerId = randomPeerId() @@ -112,7 +112,7 @@ suite "GossipSub internal": var conns = newSeq[Connection]() gossipSub.gossipsub[topic] = initHashSet[PubSubPeer]() var scoreLow = -11'f64 - for i in 0..<15: + for i in 0 ..< 15: let conn = TestBufferStream.new(noop) conns &= conn let peerId = randomPeerId() @@ -142,7 +142,7 @@ suite "GossipSub internal": var conns = newSeq[Connection]() gossipSub.gossipsub[topic] = initHashSet[PubSubPeer]() - for i in 0..<15: + for i in 0 ..< 15: let conn = TestBufferStream.new(noop) conns &= conn let peerId = PeerId.init(PrivateKey.random(ECDSA, rng[]).get()).tryGet() @@ -153,7 +153,8 @@ suite "GossipSub internal": check gossipSub.mesh[topic].len == 15 gossipSub.rebalanceMesh(topic) - check gossipSub.mesh[topic].len == gossipSub.parameters.d + gossipSub.parameters.dScore + check gossipSub.mesh[topic].len == + gossipSub.parameters.d + gossipSub.parameters.dScore await allFuturesThrowing(conns.mapIt(it.close())) await gossipSub.switch.stop() @@ -169,7 +170,7 @@ suite "GossipSub internal": gossipSub.topicParams[topic] = TopicParams.init() var conns = newSeq[Connection]() - for i in 0..<15: + for i in 0 ..< 15: let conn = TestBufferStream.new(noop) conns &= conn var peerId = randomPeerId() @@ -198,7 +199,7 @@ suite "GossipSub internal": await sleepAsync(5.millis) # allow the topic to expire var conns = newSeq[Connection]() - for i in 0..<6: + for i in 0 ..< 6: let conn = TestBufferStream.new(noop) conns &= conn let peerId = PeerId.init(PrivateKey.random(ECDSA, rng[]).get()).tryGet() @@ -232,7 +233,7 @@ suite "GossipSub internal": await sleepAsync(5.millis) # allow the topic to expire var conns = newSeq[Connection]() - for i in 0..<6: + for i in 0 ..< 6: let conn = TestBufferStream.new(noop) conns &= conn let peerId = randomPeerId() @@ -266,7 +267,7 @@ suite "GossipSub internal": var conns = newSeq[Connection]() # generate mesh and fanout peers - for i in 0..<30: + for i in 0 ..< 30: let conn = TestBufferStream.new(noop) conns &= conn let peerId = randomPeerId() @@ -280,7 +281,7 @@ suite "GossipSub internal": gossipSub.mesh[topic].incl(peer) # generate gossipsub (free standing) peers - for i in 0..<15: + for i in 0 ..< 15: let conn = TestBufferStream.new(noop) conns &= conn let peerId = randomPeerId() @@ -291,7 +292,7 @@ suite "GossipSub internal": # generate messages var seqno = 0'u64 - for i in 0..5: + for i in 0 .. 5: let conn = TestBufferStream.new(noop) conns &= conn let peerId = randomPeerId() @@ -324,7 +325,7 @@ suite "GossipSub internal": gossipSub.fanout[topic] = initHashSet[PubSubPeer]() gossipSub.gossipsub[topic] = initHashSet[PubSubPeer]() var conns = newSeq[Connection]() - for i in 0..<30: + for i in 0 ..< 30: let conn = TestBufferStream.new(noop) conns &= conn let peerId = randomPeerId() @@ -338,7 +339,7 @@ suite "GossipSub internal": # generate messages var seqno = 0'u64 - for i in 0..5: + for i in 0 .. 5: let conn = TestBufferStream.new(noop) conns &= conn let peerId = randomPeerId() @@ -364,7 +365,7 @@ suite "GossipSub internal": gossipSub.mesh[topic] = initHashSet[PubSubPeer]() gossipSub.gossipsub[topic] = initHashSet[PubSubPeer]() var conns = newSeq[Connection]() - for i in 0..<30: + for i in 0 ..< 30: let conn = TestBufferStream.new(noop) conns &= conn let peerId = randomPeerId() @@ -379,7 +380,7 @@ suite "GossipSub internal": # generate messages var seqno = 0'u64 - for i in 0..5: + for i in 0 .. 5: let conn = TestBufferStream.new(noop) conns &= conn let peerId = randomPeerId() @@ -405,7 +406,7 @@ suite "GossipSub internal": gossipSub.mesh[topic] = initHashSet[PubSubPeer]() gossipSub.fanout[topic] = initHashSet[PubSubPeer]() var conns = newSeq[Connection]() - for i in 0..<30: + for i in 0 ..< 30: let conn = TestBufferStream.new(noop) conns &= conn let peerId = randomPeerId() @@ -420,7 +421,7 @@ suite "GossipSub internal": # generate messages var seqno = 0'u64 - for i in 0..5: + for i in 0 .. 5: let conn = TestBufferStream.new(noop) conns &= conn let peerId = randomPeerId() @@ -443,7 +444,7 @@ suite "GossipSub internal": let topic = "foobar" var conns = newSeq[Connection]() - for i in 0..<30: + for i in 0 ..< 30: let conn = TestBufferStream.new(noop) conns &= conn let peerId = randomPeerId() @@ -453,7 +454,7 @@ suite "GossipSub internal": # generate messages var seqno = 0'u64 - for i in 0..5: + for i in 0 .. 5: let conn = TestBufferStream.new(noop) conns &= conn let peerId = randomPeerId() @@ -477,7 +478,7 @@ suite "GossipSub internal": let topic = "foobar" var conns = newSeq[Connection]() - for i in 0..<30: + for i in 0 ..< 30: let conn = TestBufferStream.new(noop) conns &= conn let peerId = randomPeerId() @@ -507,7 +508,7 @@ suite "GossipSub internal": gossipSub.topicsHigh = 10 var tooManyTopics: seq[string] - for i in 0..gossipSub.topicsHigh + 10: + for i in 0 .. gossipSub.topicsHigh + 10: tooManyTopics &= "topic" & $i let lotOfSubs = RPCMsg.withSubs(tooManyTopics, true) @@ -544,7 +545,7 @@ suite "GossipSub internal": var conns = newSeq[Connection]() gossipSub.gossipsub[topic] = initHashSet[PubSubPeer]() - for i in 0..<15: + for i in 0 ..< 15: let conn = TestBufferStream.new(noop) conns &= conn let peerId = randomPeerId() @@ -552,9 +553,10 @@ suite "GossipSub internal": let peer = gossipSub.getPubSubPeer(peerId) peer.sendConn = conn gossipSub.gossipsub[topic].incl(peer) - gossipSub.backingOff - .mgetOrPut(topic, initTable[PeerId, Moment]()) - .add(peerId, Moment.now() + 1.hours) + + gossipSub.backingOff.mgetOrPut(topic, initTable[PeerId, Moment]()).add( + peerId, Moment.now() + 1.hours + ) let prunes = gossipSub.handleGraft(peer, @[ControlGraft(topicID: topic)]) # there must be a control prune due to violation of backoff check prunes.len != 0 @@ -575,7 +577,7 @@ suite "GossipSub internal": var conns = newSeq[Connection]() gossipSub.gossipsub[topic] = initHashSet[PubSubPeer]() - for i in 0..<15: + for i in 0 ..< 15: let conn = TestBufferStream.new(noop) conns &= conn let peerId = randomPeerId() @@ -589,14 +591,19 @@ suite "GossipSub internal": gossipSub.rebalanceMesh(topic) check gossipSub.mesh[topic].len != 0 - for i in 0..<15: + for i in 0 ..< 15: let peerId = conns[i].peerId let peer = gossipSub.getPubSubPeer(peerId) - gossipSub.handlePrune(peer, @[ControlPrune( - topicID: topic, - peers: @[], - backoff: gossipSub.parameters.pruneBackoff.seconds.uint64 - )]) + gossipSub.handlePrune( + peer, + @[ + ControlPrune( + topicID: topic, + peers: @[], + backoff: gossipSub.parameters.pruneBackoff.seconds.uint64, + ) + ], + ) # expect topic cleaned up since they are all pruned check topic notin gossipSub.mesh @@ -617,7 +624,7 @@ suite "GossipSub internal": var conns = newSeq[Connection]() gossipSub.gossipsub[topic] = initHashSet[PubSubPeer]() - for i in 0..<6: + for i in 0 ..< 6: let conn = TestBufferStream.new(noop) conn.transportDir = Direction.In conns &= conn @@ -629,7 +636,7 @@ suite "GossipSub internal": gossipSub.grafted(peer, topic) gossipSub.mesh[topic].incl(peer) - for i in 0..<7: + for i in 0 ..< 7: let conn = TestBufferStream.new(noop) conn.transportDir = Direction.Out conns &= conn @@ -660,7 +667,9 @@ suite "GossipSub internal": proc handler(peer: PubSubPeer, data: seq[byte]) {.async.} = check false - proc handler2(topic: string, data: seq[byte]) {.async.} = discard + + proc handler2(topic: string, data: seq[byte]) {.async.} = + discard let topic = "foobar" var conns = newSeq[Connection]() @@ -668,7 +677,7 @@ suite "GossipSub internal": gossipSub.mesh[topic] = initHashSet[PubSubPeer]() gossipSub.subscribe(topic, handler2) - for i in 0..<30: + for i in 0 ..< 30: let conn = TestBufferStream.new(noop) conns &= conn let peerId = randomPeerId() @@ -686,13 +695,11 @@ suite "GossipSub internal": conn.peerId = peerId let peer = gossipSub.getPubSubPeer(peerId) let id = @[0'u8, 1, 2, 3] - let msg = ControlIHave( - topicID: topic, - messageIDs: @[id, id, id] - ) + let msg = ControlIHave(topicID: topic, messageIDs: @[id, id, id]) peer.iHaveBudget = 0 let iwants = gossipSub.handleIHave(peer, @[msg]) - check: iwants.messageIDs.len == 0 + check: + iwants.messageIDs.len == 0 block: # given duplicate ihave should generate only one iwant @@ -702,12 +709,10 @@ suite "GossipSub internal": conn.peerId = peerId let peer = gossipSub.getPubSubPeer(peerId) let id = @[0'u8, 1, 2, 3] - let msg = ControlIHave( - topicID: topic, - messageIDs: @[id, id, id] - ) + let msg = ControlIHave(topicID: topic, messageIDs: @[id, id, id]) let iwants = gossipSub.handleIHave(peer, @[msg]) - check: iwants.messageIDs.len == 1 + check: + iwants.messageIDs.len == 1 block: # given duplicate iwant should generate only one message @@ -719,26 +724,27 @@ suite "GossipSub internal": let id = @[0'u8, 1, 2, 3] gossipSub.mcache.put(id, Message()) peer.sentIHaves[^1].incl(id) - let msg = ControlIWant( - messageIDs: @[id, id, id] - ) + let msg = ControlIWant(messageIDs: @[id, id, id]) let genmsg = gossipSub.handleIWant(peer, @[msg]) - check: genmsg.len == 1 + check: + genmsg.len == 1 check gossipSub.mcache.msgs.len == 1 await allFuturesThrowing(conns.mapIt(it.close())) await gossipSub.switch.stop() - proc setupTest(): Future[tuple[gossip0: GossipSub, gossip1: GossipSub, receivedMessages: ref HashSet[seq[byte]]]] {.async.} = - let - nodes = generateNodes(2, gossip = true, verifySignature = false) - discard await allFinished( - nodes[0].switch.start(), - nodes[1].switch.start() - ) + proc setupTest(): Future[ + tuple[ + gossip0: GossipSub, gossip1: GossipSub, receivedMessages: ref HashSet[seq[byte]] + ] + ] {.async.} = + let nodes = generateNodes(2, gossip = true, verifySignature = false) + discard await allFinished(nodes[0].switch.start(), nodes[1].switch.start()) - await nodes[1].switch.connect(nodes[0].switch.peerInfo.peerId, nodes[0].switch.peerInfo.addrs) + await nodes[1].switch.connect( + nodes[0].switch.peerInfo.peerId, nodes[0].switch.peerInfo.addrs + ) var receivedMessages = new(HashSet[seq[byte]]) @@ -758,12 +764,11 @@ suite "GossipSub internal": return (gossip0, gossip1, receivedMessages) proc teardownTest(gossip0: GossipSub, gossip1: GossipSub) {.async.} = - await allFuturesThrowing( - gossip0.switch.stop(), - gossip1.switch.stop() - ) + await allFuturesThrowing(gossip0.switch.stop(), gossip1.switch.stop()) - proc createMessages(gossip0: GossipSub, gossip1: GossipSub, size1: int, size2: int): tuple[iwantMessageIds: seq[MessageId], sentMessages: HashSet[seq[byte]]] = + proc createMessages( + gossip0: GossipSub, gossip1: GossipSub, size1: int, size2: int + ): tuple[iwantMessageIds: seq[MessageId], sentMessages: HashSet[seq[byte]]] = var iwantMessageIds = newSeq[MessageId]() var sentMessages = initHashSet[seq[byte]]() @@ -771,7 +776,8 @@ suite "GossipSub internal": let data = newSeqWith[byte](size, i.byte) sentMessages.incl(data) - let msg = Message.init(gossip1.peerInfo.peerId, data, "foobar", some(uint64(i + 1))) + let msg = + Message.init(gossip1.peerInfo.peerId, data, "foobar", some(uint64(i + 1))) let iwantMessageId = gossip1.msgIdProvider(msg).expect(MsgIdSuccess) iwantMessageIds.add(iwantMessageId) gossip1.mcache.put(iwantMessageId, msg) @@ -787,13 +793,23 @@ suite "GossipSub internal": let (gossip0, gossip1, receivedMessages) = await setupTest() let messageSize = gossip1.maxMessageSize div 2 + 1 - let (iwantMessageIds, sentMessages) = createMessages(gossip0, gossip1, messageSize, messageSize) - - gossip1.broadcast(gossip1.mesh["foobar"], RPCMsg(control: some(ControlMessage( - ihave: @[ControlIHave(topicID: "foobar", messageIDs: iwantMessageIds)] - ))), isHighPriority = false) + let (iwantMessageIds, sentMessages) = + createMessages(gossip0, gossip1, messageSize, messageSize) + + gossip1.broadcast( + gossip1.mesh["foobar"], + RPCMsg( + control: some( + ControlMessage( + ihave: @[ControlIHave(topicID: "foobar", messageIDs: iwantMessageIds)] + ) + ) + ), + isHighPriority = false, + ) - checkUntilTimeout: receivedMessages[] == sentMessages + checkUntilTimeout: + receivedMessages[] == sentMessages check receivedMessages[].len == 2 await teardownTest(gossip0, gossip1) @@ -804,14 +820,24 @@ suite "GossipSub internal": let (gossip0, gossip1, receivedMessages) = await setupTest() let messageSize = gossip1.maxMessageSize + 10 - let (bigIWantMessageIds, sentMessages) = createMessages(gossip0, gossip1, messageSize, messageSize) - - gossip1.broadcast(gossip1.mesh["foobar"], RPCMsg(control: some(ControlMessage( - ihave: @[ControlIHave(topicID: "foobar", messageIDs: bigIWantMessageIds)] - ))), isHighPriority = false) + let (bigIWantMessageIds, sentMessages) = + createMessages(gossip0, gossip1, messageSize, messageSize) + + gossip1.broadcast( + gossip1.mesh["foobar"], + RPCMsg( + control: some( + ControlMessage( + ihave: @[ControlIHave(topicID: "foobar", messageIDs: bigIWantMessageIds)] + ) + ) + ), + isHighPriority = false, + ) await sleepAsync(300.milliseconds) - checkUntilTimeout: receivedMessages[].len == 0 + checkUntilTimeout: + receivedMessages[].len == 0 await teardownTest(gossip0, gossip1) @@ -821,13 +847,23 @@ suite "GossipSub internal": let (gossip0, gossip1, receivedMessages) = await setupTest() let size1 = gossip1.maxMessageSize div 2 let size2 = gossip1.maxMessageSize div 3 - let (bigIWantMessageIds, sentMessages) = createMessages(gossip0, gossip1, size1, size2) - - gossip1.broadcast(gossip1.mesh["foobar"], RPCMsg(control: some(ControlMessage( - ihave: @[ControlIHave(topicID: "foobar", messageIDs: bigIWantMessageIds)] - ))), isHighPriority = false) + let (bigIWantMessageIds, sentMessages) = + createMessages(gossip0, gossip1, size1, size2) + + gossip1.broadcast( + gossip1.mesh["foobar"], + RPCMsg( + control: some( + ControlMessage( + ihave: @[ControlIHave(topicID: "foobar", messageIDs: bigIWantMessageIds)] + ) + ) + ), + isHighPriority = false, + ) - checkUntilTimeout: receivedMessages[] == sentMessages + checkUntilTimeout: + receivedMessages[] == sentMessages check receivedMessages[].len == 2 await teardownTest(gossip0, gossip1) @@ -839,11 +875,20 @@ suite "GossipSub internal": let maxSize = gossip1.maxMessageSize let size1 = maxSize div 2 let size2 = maxSize + 10 - let (bigIWantMessageIds, sentMessages) = createMessages(gossip0, gossip1, size1, size2) - - gossip1.broadcast(gossip1.mesh["foobar"], RPCMsg(control: some(ControlMessage( - ihave: @[ControlIHave(topicID: "foobar", messageIDs: bigIWantMessageIds)] - ))), isHighPriority = false) + let (bigIWantMessageIds, sentMessages) = + createMessages(gossip0, gossip1, size1, size2) + + gossip1.broadcast( + gossip1.mesh["foobar"], + RPCMsg( + control: some( + ControlMessage( + ihave: @[ControlIHave(topicID: "foobar", messageIDs: bigIWantMessageIds)] + ) + ) + ), + isHighPriority = false, + ) var smallestSet: HashSet[seq[byte]] let seqs = toSeq(sentMessages) @@ -852,7 +897,8 @@ suite "GossipSub internal": else: smallestSet.incl(seqs[1]) - checkUntilTimeout: receivedMessages[] == smallestSet + checkUntilTimeout: + receivedMessages[] == smallestSet check receivedMessages[].len == 1 await teardownTest(gossip0, gossip1) diff --git a/tests/pubsub/testgossipsub.nim b/tests/pubsub/testgossipsub.nim index fada9f81f8..ac4eb95955 100644 --- a/tests/pubsub/testgossipsub.nim +++ b/tests/pubsub/testgossipsub.nim @@ -13,25 +13,32 @@ import sequtils, options, tables, sets, sugar import chronos, stew/byteutils, chronos/ratelimit import chronicles import metrics -import utils, ../../libp2p/[errors, - peerid, - peerinfo, - stream/connection, - stream/bufferstream, - crypto/crypto, - protocols/pubsub/pubsub, - protocols/pubsub/gossipsub, - protocols/pubsub/gossipsub/scoring, - protocols/pubsub/pubsubpeer, - protocols/pubsub/peertable, - protocols/pubsub/timedcache, - protocols/pubsub/rpc/messages] +import + utils, + ../../libp2p/[ + errors, + peerid, + peerinfo, + stream/connection, + stream/bufferstream, + crypto/crypto, + protocols/pubsub/pubsub, + protocols/pubsub/gossipsub, + protocols/pubsub/gossipsub/scoring, + protocols/pubsub/pubsubpeer, + protocols/pubsub/peertable, + protocols/pubsub/timedcache, + protocols/pubsub/rpc/messages, + ] import ../../libp2p/protocols/pubsub/errors as pubsub_errors import ../helpers -proc `$`(peer: PubSubPeer): string = shortLog(peer) +proc `$`(peer: PubSubPeer): string = + shortLog(peer) -template tryPublish(call: untyped, require: int, wait = 10.milliseconds, timeout = 5.seconds): untyped = +template tryPublish( + call: untyped, require: int, wait = 10.milliseconds, timeout = 5.seconds +): untyped = var expiration = Moment.now() + timeout pubs = 0 @@ -55,10 +62,7 @@ suite "GossipSub": nodes = generateNodes(2, gossip = true) # start switches - nodesFut = await allFinished( - nodes[0].switch.start(), - nodes[1].switch.start(), - ) + nodesFut = await allFinished(nodes[0].switch.start(), nodes[1].switch.start()) await subscribeNodes(nodes) @@ -72,9 +76,9 @@ suite "GossipSub": await allFuturesThrowing(subs) var validatorFut = newFuture[bool]() - proc validator(topic: string, - message: Message): - Future[ValidationResult] {.async.} = + proc validator( + topic: string, message: Message + ): Future[ValidationResult] {.async.} = check topic == "foobar" validatorFut.complete(true) result = ValidationResult.Accept @@ -84,10 +88,7 @@ suite "GossipSub": check (await validatorFut) and (await handlerFut) - await allFuturesThrowing( - nodes[0].switch.stop(), - nodes[1].switch.stop() - ) + await allFuturesThrowing(nodes[0].switch.stop(), nodes[1].switch.stop()) await allFuturesThrowing(nodesFut.concat()) @@ -99,10 +100,7 @@ suite "GossipSub": nodes = generateNodes(2, gossip = true) # start switches - nodesFut = await allFinished( - nodes[0].switch.start(), - nodes[1].switch.start(), - ) + nodesFut = await allFinished(nodes[0].switch.start(), nodes[1].switch.start()) await subscribeNodes(nodes) @@ -119,9 +117,9 @@ suite "GossipSub": gossip2.mesh["foobar"].len == 1 and "foobar" notin gossip2.fanout var validatorFut = newFuture[bool]() - proc validator(topic: string, - message: Message): - Future[ValidationResult] {.async.} = + proc validator( + topic: string, message: Message + ): Future[ValidationResult] {.async.} = result = ValidationResult.Reject validatorFut.complete(true) @@ -130,10 +128,7 @@ suite "GossipSub": check (await validatorFut) == true - await allFuturesThrowing( - nodes[0].switch.stop(), - nodes[1].switch.stop() - ) + await allFuturesThrowing(nodes[0].switch.stop(), nodes[1].switch.stop()) await allFuturesThrowing(nodesFut.concat()) @@ -145,10 +140,7 @@ suite "GossipSub": nodes = generateNodes(2, gossip = true) # start switches - nodesFut = await allFinished( - nodes[0].switch.start(), - nodes[1].switch.start(), - ) + nodesFut = await allFinished(nodes[0].switch.start(), nodes[1].switch.start()) await subscribeNodes(nodes) @@ -165,9 +157,9 @@ suite "GossipSub": gossip2.mesh["foobar"].len == 1 and "foobar" notin gossip2.fanout var validatorFut = newFuture[bool]() - proc validator(topic: string, - message: Message): - Future[ValidationResult] {.async.} = + proc validator( + topic: string, message: Message + ): Future[ValidationResult] {.async.} = result = ValidationResult.Ignore validatorFut.complete(true) @@ -176,10 +168,7 @@ suite "GossipSub": check (await validatorFut) == true - await allFuturesThrowing( - nodes[0].switch.stop(), - nodes[1].switch.stop() - ) + await allFuturesThrowing(nodes[0].switch.stop(), nodes[1].switch.stop()) await allFuturesThrowing(nodesFut.concat()) @@ -193,10 +182,7 @@ suite "GossipSub": nodes = generateNodes(2, gossip = true) # start switches - nodesFut = await allFinished( - nodes[0].switch.start(), - nodes[1].switch.start(), - ) + nodesFut = await allFinished(nodes[0].switch.start(), nodes[1].switch.start()) await subscribeNodes(nodes) @@ -204,15 +190,16 @@ suite "GossipSub": nodes[1].subscribe("bar", handler) var passed, failed: Future[bool] = newFuture[bool]() - proc validator(topic: string, - message: Message): - Future[ValidationResult] {.async.} = - result = if topic == "foo": - passed.complete(true) - ValidationResult.Accept - else: - failed.complete(true) - ValidationResult.Reject + proc validator( + topic: string, message: Message + ): Future[ValidationResult] {.async.} = + result = + if topic == "foo": + passed.complete(true) + ValidationResult.Accept + else: + failed.complete(true) + ValidationResult.Reject nodes[1].addValidator("foo", "bar", validator) tryPublish await nodes[0].publish("foo", "Hello!".toBytes()), 1 @@ -229,10 +216,7 @@ suite "GossipSub": "bar" notin gossip1.mesh and gossip1.fanout["bar"].len == 1 "bar" notin gossip2.mesh and "bar" notin gossip2.fanout - await allFuturesThrowing( - nodes[0].switch.stop(), - nodes[1].switch.stop() - ) + await allFuturesThrowing(nodes[0].switch.stop(), nodes[1].switch.stop()) await allFuturesThrowing(nodesFut.concat()) @@ -246,10 +230,7 @@ suite "GossipSub": nodes = generateNodes(2, gossip = true) # start switches - nodesFut = await allFinished( - nodes[0].switch.start(), - nodes[1].switch.start(), - ) + nodesFut = await allFinished(nodes[0].switch.start(), nodes[1].switch.start()) await subscribeNodes(nodes) @@ -266,12 +247,14 @@ suite "GossipSub": nodes[0].subscribe("foobar", handler) # regular backoff is 60 seconds, so we must not wait that long - await (waitSub(nodes[0], nodes[1], "foobar") and waitSub(nodes[1], nodes[0], "foobar")).wait(30.seconds) + await ( + waitSub(nodes[0], nodes[1], "foobar") and waitSub(nodes[1], nodes[0], "foobar") + ).wait(30.seconds) var validatorFut = newFuture[bool]() - proc validator(topic: string, - message: Message): - Future[ValidationResult] {.async.} = + proc validator( + topic: string, message: Message + ): Future[ValidationResult] {.async.} = check topic == "foobar" validatorFut.complete(true) result = ValidationResult.Accept @@ -281,10 +264,7 @@ suite "GossipSub": check (await validatorFut) and (await handlerFut) - await allFuturesThrowing( - nodes[0].switch.stop(), - nodes[1].switch.stop() - ) + await allFuturesThrowing(nodes[0].switch.stop(), nodes[1].switch.stop()) await allFuturesThrowing(nodesFut.concat()) @@ -293,15 +273,10 @@ suite "GossipSub": discard let - nodes = generateNodes( - 2, - gossip = true) + nodes = generateNodes(2, gossip = true) # start switches - nodesFut = await allFinished( - nodes[0].switch.start(), - nodes[1].switch.start(), - ) + nodesFut = await allFinished(nodes[0].switch.start(), nodes[1].switch.start()) await subscribeNodes(nodes) @@ -315,10 +290,7 @@ suite "GossipSub": "foobar" in gossip1.gossipsub gossip1.gossipsub.hasPeerId("foobar", gossip2.peerInfo.peerId) - await allFuturesThrowing( - nodes[0].switch.stop(), - nodes[1].switch.stop() - ) + await allFuturesThrowing(nodes[0].switch.stop(), nodes[1].switch.stop()) await allFuturesThrowing(nodesFut.concat()) @@ -327,15 +299,10 @@ suite "GossipSub": discard let - nodes = generateNodes( - 2, - gossip = true) + nodes = generateNodes(2, gossip = true) # start switches - nodesFut = await allFinished( - nodes[0].switch.start(), - nodes[1].switch.start(), - ) + nodesFut = await allFinished(nodes[0].switch.start(), nodes[1].switch.start()) await subscribeNodes(nodes) @@ -360,15 +327,12 @@ suite "GossipSub": "foobar" in gossip2.gossipsub gossip1.gossipsub.hasPeerId("foobar", gossip2.peerInfo.peerId) or - gossip1.mesh.hasPeerId("foobar", gossip2.peerInfo.peerId) + gossip1.mesh.hasPeerId("foobar", gossip2.peerInfo.peerId) gossip2.gossipsub.hasPeerId("foobar", gossip1.peerInfo.peerId) or - gossip2.mesh.hasPeerId("foobar", gossip1.peerInfo.peerId) + gossip2.mesh.hasPeerId("foobar", gossip1.peerInfo.peerId) - await allFuturesThrowing( - nodes[0].switch.stop(), - nodes[1].switch.stop() - ) + await allFuturesThrowing(nodes[0].switch.stop(), nodes[1].switch.stop()) await allFuturesThrowing(nodesFut.concat()) @@ -379,15 +343,10 @@ suite "GossipSub": passed.complete() let - nodes = generateNodes( - 2, - gossip = true) + nodes = generateNodes(2, gossip = true) # start switches - nodesFut = await allFinished( - nodes[0].switch.start(), - nodes[1].switch.start(), - ) + nodesFut = await allFinished(nodes[0].switch.start(), nodes[1].switch.start()) await subscribeNodes(nodes) @@ -396,11 +355,13 @@ suite "GossipSub": var observed = 0 let - obs1 = PubSubObserver(onRecv: proc(peer: PubSubPeer; msgs: var RPCMsg) = - inc observed + obs1 = PubSubObserver( + onRecv: proc(peer: PubSubPeer, msgs: var RPCMsg) = + inc observed ) - obs2 = PubSubObserver(onSend: proc(peer: PubSubPeer; msgs: var RPCMsg) = - inc observed + obs2 = PubSubObserver( + onSend: proc(peer: PubSubPeer, msgs: var RPCMsg) = + inc observed ) nodes[1].addObserver(obs1) @@ -418,10 +379,7 @@ suite "GossipSub": await passed.wait(2.seconds) - await allFuturesThrowing( - nodes[0].switch.stop(), - nodes[1].switch.stop() - ) + await allFuturesThrowing(nodes[0].switch.stop(), nodes[1].switch.stop()) await allFuturesThrowing(nodesFut.concat()) check observed == 2 @@ -433,16 +391,10 @@ suite "GossipSub": passed.complete() let - nodes = generateNodes( - 2, - gossip = true, - unsubscribeBackoff = 10.minutes) + nodes = generateNodes(2, gossip = true, unsubscribeBackoff = 10.minutes) # start switches - nodesFut = await allFinished( - nodes[0].switch.start(), - nodes[1].switch.start(), - ) + nodesFut = await allFinished(nodes[0].switch.start(), nodes[1].switch.start()) GossipSub(nodes[1]).parameters.d = 0 GossipSub(nodes[1]).parameters.dHigh = 0 @@ -472,10 +424,7 @@ suite "GossipSub": trace "test done, stopping..." - await allFuturesThrowing( - nodes[0].switch.stop(), - nodes[1].switch.stop() - ) + await allFuturesThrowing(nodes[0].switch.stop(), nodes[1].switch.stop()) await allFuturesThrowing(nodesFut.concat()) @@ -486,15 +435,10 @@ suite "GossipSub": passed.complete(true) let - nodes = generateNodes( - 2, - gossip = true) + nodes = generateNodes(2, gossip = true) # start switches - nodesFut = await allFinished( - nodes[0].switch.start(), - nodes[1].switch.start(), - ) + nodesFut = await allFinished(nodes[0].switch.start(), nodes[1].switch.start()) await subscribeNodes(nodes) @@ -517,10 +461,7 @@ suite "GossipSub": gossip2.mesh.hasPeerId("foobar", gossip1.peerInfo.peerId) not gossip2.fanout.hasPeerId("foobar", gossip1.peerInfo.peerId) - await allFuturesThrowing( - nodes[0].switch.stop(), - nodes[1].switch.stop() - ) + await allFuturesThrowing(nodes[0].switch.stop(), nodes[1].switch.stop()) await allFuturesThrowing(nodesFut.concat()) @@ -530,15 +471,11 @@ suite "GossipSub": # so B should not send to anyone let - nodes = generateNodes( - 3, - gossip = true) + nodes = generateNodes(3, gossip = true) # start switches nodesFut = await allFinished( - nodes[0].switch.start(), - nodes[1].switch.start(), - nodes[2].switch.start(), + nodes[0].switch.start(), nodes[1].switch.start(), nodes[2].switch.start() ) await subscribeNodes(nodes) @@ -551,7 +488,10 @@ suite "GossipSub": proc handlerA(topic: string, data: seq[byte]) {.async.} = inc aReceived check aReceived < 2 - proc handlerB(topic: string, data: seq[byte]) {.async.} = discard + + proc handlerB(topic: string, data: seq[byte]) {.async.} = + discard + proc handlerC(topic: string, data: seq[byte]) {.async.} = inc cReceived check cReceived < 2 @@ -566,13 +506,20 @@ suite "GossipSub": var gossip2: GossipSub = GossipSub(nodes[1]) var gossip3: GossipSub = GossipSub(nodes[2]) - proc slowValidator(topic: string, message: Message): Future[ValidationResult] {.async.} = + proc slowValidator( + topic: string, message: Message + ): Future[ValidationResult] {.async.} = await cRelayed # Empty A & C caches to detect duplicates gossip1.seen = TimedCache[SaltedId].init() gossip3.seen = TimedCache[SaltedId].init() let msgId = toSeq(gossip2.validationSeen.keys)[0] - checkUntilTimeout(try: gossip2.validationSeen[msgId].len > 0 except: false) + checkUntilTimeout( + try: + gossip2.validationSeen[msgId].len > 0 + except: + false + ) result = ValidationResult.Accept bFinished.complete() @@ -587,9 +534,7 @@ suite "GossipSub": await bFinished await allFuturesThrowing( - nodes[0].switch.stop(), - nodes[1].switch.stop(), - nodes[2].switch.stop() + nodes[0].switch.stop(), nodes[1].switch.stop(), nodes[2].switch.stop() ) await allFuturesThrowing(nodesFut.concat()) @@ -601,15 +546,10 @@ suite "GossipSub": passed.complete(true) let - nodes = generateNodes( - 2, - gossip = true) + nodes = generateNodes(2, gossip = true) # start switches - nodesFut = await allFinished( - nodes[0].switch.start(), - nodes[1].switch.start(), - ) + nodesFut = await allFinished(nodes[0].switch.start(), nodes[1].switch.start()) var gossip1: GossipSub = GossipSub(nodes[0]) gossip1.parameters.floodPublish = true @@ -631,10 +571,7 @@ suite "GossipSub": "foobar" notin gossip2.gossipsub not gossip1.mesh.hasPeerId("foobar", gossip2.peerInfo.peerId) - await allFuturesThrowing( - nodes[0].switch.stop(), - nodes[1].switch.stop() - ) + await allFuturesThrowing(nodes[0].switch.stop(), nodes[1].switch.stop()) await allFuturesThrowing(nodesFut.concat()) @@ -643,14 +580,10 @@ suite "GossipSub": generateNodes(count, gossip = true) proc startNodes(nodes: seq[PubSub]) {.async.} = - await allFuturesThrowing( - nodes.mapIt(it.switch.start()) - ) + await allFuturesThrowing(nodes.mapIt(it.switch.start())) proc stopNodes(nodes: seq[PubSub]) {.async.} = - await allFuturesThrowing( - nodes.mapIt(it.switch.stop()) - ) + await allFuturesThrowing(nodes.mapIt(it.switch.stop())) proc connectNodes(nodes: seq[PubSub], target: PubSub) {.async.} = proc handler(topic: string, data: seq[byte]) {.async.} = @@ -660,12 +593,17 @@ suite "GossipSub": node.subscribe("foobar", handler) await node.switch.connect(target.peerInfo.peerId, target.peerInfo.addrs) - proc baseTestProcedure(nodes: seq[PubSub], gossip1: GossipSub, numPeersFirstMsg: int, numPeersSecondMsg: int) {.async.} = + proc baseTestProcedure( + nodes: seq[PubSub], + gossip1: GossipSub, + numPeersFirstMsg: int, + numPeersSecondMsg: int, + ) {.async.} = proc handler(topic: string, data: seq[byte]) {.async.} = check topic == "foobar" block setup: - for i in 0..<50: + for i in 0 ..< 50: if (await nodes[0].publish("foobar", ("Hello!" & $i).toBytes())) == 19: break setup await sleepAsync(10.milliseconds) @@ -676,14 +614,14 @@ suite "GossipSub": # Now try with a mesh gossip1.subscribe("foobar", handler) - checkUntilTimeout: gossip1.mesh.peers("foobar") > 5 + checkUntilTimeout: + gossip1.mesh.peers("foobar") > 5 # use a different length so that the message is not equal to the last check (await nodes[0].publish("foobar", newSeq[byte](500_000))) == numPeersSecondMsg # Actual tests asyncTest "e2e - GossipSub floodPublish limit": - let nodes = setupNodes(20) gossip1 = GossipSub(nodes[0]) @@ -692,12 +630,11 @@ suite "GossipSub": gossip1.parameters.heartbeatInterval = milliseconds(700) await startNodes(nodes) - await connectNodes(nodes[1..^1], nodes[0]) + await connectNodes(nodes[1 ..^ 1], nodes[0]) await baseTestProcedure(nodes, gossip1, gossip1.parameters.dLow, 17) await stopNodes(nodes) asyncTest "e2e - GossipSub floodPublish limit with bandwidthEstimatebps = 0": - let nodes = setupNodes(20) gossip1 = GossipSub(nodes[0]) @@ -707,7 +644,7 @@ suite "GossipSub": gossip1.parameters.bandwidthEstimatebps = 0 await startNodes(nodes) - await connectNodes(nodes[1..^1], nodes[0]) + await connectNodes(nodes[1 ..^ 1], nodes[0]) await baseTestProcedure(nodes, gossip1, nodes.len - 1, nodes.len - 1) await stopNodes(nodes) @@ -722,7 +659,7 @@ suite "GossipSub": var seen: Table[string, int] var seenFut = newFuture[void]() - for i in 0..= runs + check: + seen.len >= runs for k, v in seen.pairs: - check: v >= 1 + check: + v >= 1 for node in nodes: var gossip = GossipSub(node) @@ -754,10 +693,7 @@ suite "GossipSub": check: "foobar" in gossip.gossipsub - await allFuturesThrowing( - nodes.mapIt( - allFutures( - it.switch.stop()))) + await allFuturesThrowing(nodes.mapIt(allFutures(it.switch.stop()))) await allFuturesThrowing(nodesFut) @@ -773,7 +709,7 @@ suite "GossipSub": var seen: Table[string, int] var seenFut = newFuture[void]() - for i in 0..= runs + check: + seen.len >= runs for k, v in seen.pairs: - check: v >= 1 + check: + v >= 1 for node in nodes: var gossip = GossipSub(node) @@ -806,10 +744,7 @@ suite "GossipSub": gossip.fanout.len == 0 gossip.mesh["foobar"].len > 0 - await allFuturesThrowing( - nodes.mapIt( - allFutures( - it.switch.stop()))) + await allFuturesThrowing(nodes.mapIt(allFutures(it.switch.stop()))) await allFuturesThrowing(nodesFut) @@ -823,17 +758,13 @@ suite "GossipSub": discard # not used in this test let - nodes = generateNodes( - 2, - gossip = true, - enablePX = true) & + nodes = + generateNodes(2, gossip = true, enablePX = true) & generateNodes(1, gossip = true, sendSignedPeerRecord = true) # start switches nodesFut = await allFinished( - nodes[0].switch.start(), - nodes[1].switch.start(), - nodes[2].switch.start(), + nodes[0].switch.start(), nodes[1].switch.start(), nodes[2].switch.start() ) var @@ -846,27 +777,26 @@ suite "GossipSub": nodes[0].subscribe("foobar", handler) nodes[1].subscribe("foobar", handler) nodes[2].subscribe("foobar", handler) - for x in 0..<3: - for y in 0..<3: + for x in 0 ..< 3: + for y in 0 ..< 3: if x != y: await waitSub(nodes[x], nodes[y], "foobar") var passed: Future[void] = newFuture[void]() - gossip0.routingRecordsHandler.add(proc(peer: PeerId, tag: string, peers: seq[RoutingRecordsPair]) = - check: - tag == "foobar" - peers.len == 2 - peers[0].record.isSome() xor peers[1].record.isSome() - passed.complete() + gossip0.routingRecordsHandler.add( + proc(peer: PeerId, tag: string, peers: seq[RoutingRecordsPair]) = + check: + tag == "foobar" + peers.len == 2 + peers[0].record.isSome() xor peers[1].record.isSome() + passed.complete() ) nodes[1].unsubscribe("foobar", handler) await passed.wait(5.seconds) await allFuturesThrowing( - nodes[0].switch.stop(), - nodes[1].switch.stop(), - nodes[2].switch.stop() + nodes[0].switch.stop(), nodes[1].switch.stop(), nodes[2].switch.stop() ) await allFuturesThrowing(nodesFut.concat()) @@ -879,25 +809,28 @@ suite "GossipSub": func dumbMsgIdProvider(m: Message): Result[MessageId, ValidationResult] = ok(newSeq[byte](10)) let - nodes = generateNodes( - 3, - gossip = true, - msgIdProvider = dumbMsgIdProvider - ) + nodes = generateNodes(3, gossip = true, msgIdProvider = dumbMsgIdProvider) nodesFut = await allFinished( - nodes[0].switch.start(), - nodes[1].switch.start(), - nodes[2].switch.start(), + nodes[0].switch.start(), nodes[1].switch.start(), nodes[2].switch.start() ) - await nodes[0].switch.connect(nodes[1].switch.peerInfo.peerId, nodes[1].switch.peerInfo.addrs) - await nodes[1].switch.connect(nodes[2].switch.peerInfo.peerId, nodes[2].switch.peerInfo.addrs) + await nodes[0].switch.connect( + nodes[1].switch.peerInfo.peerId, nodes[1].switch.peerInfo.addrs + ) + await nodes[1].switch.connect( + nodes[2].switch.peerInfo.peerId, nodes[2].switch.peerInfo.addrs + ) let bFinished = newFuture[void]() - proc handlerA(topic: string, data: seq[byte]) {.async.} = discard - proc handlerB(topic: string, data: seq[byte]) {.async.} = bFinished.complete() - proc handlerC(topic: string, data: seq[byte]) {.async.} = doAssert false + proc handlerA(topic: string, data: seq[byte]) {.async.} = + discard + + proc handlerB(topic: string, data: seq[byte]) {.async.} = + bFinished.complete() + + proc handlerC(topic: string, data: seq[byte]) {.async.} = + doAssert false nodes[0].subscribe("foobar", handlerA) nodes[1].subscribe("foobar", handlerB) @@ -908,42 +841,46 @@ suite "GossipSub": var gossip2: GossipSub = GossipSub(nodes[1]) var gossip3: GossipSub = GossipSub(nodes[2]) - check: gossip3.mesh.peers("foobar") == 1 + check: + gossip3.mesh.peers("foobar") == 1 - gossip3.broadcast(gossip3.mesh["foobar"], RPCMsg(control: some(ControlMessage( - idontwant: @[ControlIWant(messageIDs: @[newSeq[byte](10)])] - ))), isHighPriority = true) - checkUntilTimeout: gossip2.mesh.getOrDefault("foobar").anyIt(it.heDontWants[^1].len == 1) + gossip3.broadcast( + gossip3.mesh["foobar"], + RPCMsg( + control: some( + ControlMessage(idontwant: @[ControlIWant(messageIDs: @[newSeq[byte](10)])]) + ) + ), + isHighPriority = true, + ) + checkUntilTimeout: + gossip2.mesh.getOrDefault("foobar").anyIt(it.heDontWants[^1].len == 1) tryPublish await nodes[0].publish("foobar", newSeq[byte](10000)), 1 await bFinished - checkUntilTimeout: toSeq(gossip3.mesh.getOrDefault("foobar")).anyIt(it.heDontWants[^1].len == 1) - check: toSeq(gossip1.mesh.getOrDefault("foobar")).anyIt(it.heDontWants[^1].len == 0) + checkUntilTimeout: + toSeq(gossip3.mesh.getOrDefault("foobar")).anyIt(it.heDontWants[^1].len == 1) + check: + toSeq(gossip1.mesh.getOrDefault("foobar")).anyIt(it.heDontWants[^1].len == 0) await allFuturesThrowing( - nodes[0].switch.stop(), - nodes[1].switch.stop(), - nodes[2].switch.stop() + nodes[0].switch.stop(), nodes[1].switch.stop(), nodes[2].switch.stop() ) await allFuturesThrowing(nodesFut.concat()) proc initializeGossipTest(): Future[(seq[PubSub], GossipSub, GossipSub)] {.async.} = - let nodes = generateNodes( - 2, - gossip = true, - overheadRateLimit = Opt.some((20, 1.millis))) - - discard await allFinished( - nodes[0].switch.start(), - nodes[1].switch.start(), - ) + let nodes = + generateNodes(2, gossip = true, overheadRateLimit = Opt.some((20, 1.millis))) + + discard await allFinished(nodes[0].switch.start(), nodes[1].switch.start()) await subscribeNodes(nodes) - proc handle(topic: string, data: seq[byte]) {.async.} = discard + proc handle(topic: string, data: seq[byte]) {.async.} = + discard let gossip0 = GossipSub(nodes[0]) let gossip1 = GossipSub(nodes[1]) @@ -956,11 +893,13 @@ suite "GossipSub": gossip0.verifySignature = false gossip1.verifySignature = false - return (nodes, gossip0, gossip1) + return (nodes, gossip0, gossip1) proc currentRateLimitHits(): float64 = try: - libp2p_gossipsub_peers_rate_limit_hits.valueByName("libp2p_gossipsub_peers_rate_limit_hits_total", @["nim-libp2p"]) + libp2p_gossipsub_peers_rate_limit_hits.valueByName( + "libp2p_gossipsub_peers_rate_limit_hits_total", @["nim-libp2p"] + ) except KeyError: 0 @@ -998,7 +937,9 @@ suite "GossipSub": let (nodes, gossip0, gossip1) = await initializeGossipTest() # Simulate sending an undecodable message - await gossip1.peers[gossip0.switch.peerInfo.peerId].sendEncoded(newSeqWith[byte](33, 1.byte), isHighPriority = true) + await gossip1.peers[gossip0.switch.peerInfo.peerId].sendEncoded( + newSeqWith[byte](33, 1.byte), isHighPriority = true + ) await sleepAsync(300.millis) check currentRateLimitHits() == rateLimitHits + 1 @@ -1006,7 +947,9 @@ suite "GossipSub": # Disconnect peer when rate limiting is enabled gossip1.parameters.disconnectPeerAboveRateLimit = true - await gossip0.peers[gossip1.switch.peerInfo.peerId].sendEncoded(newSeqWith[byte](35, 1.byte), isHighPriority = true) + await gossip0.peers[gossip1.switch.peerInfo.peerId].sendEncoded( + newSeqWith[byte](35, 1.byte), isHighPriority = true + ) checkUntilTimeout gossip1.switch.isConnected(gossip0.switch.peerInfo.peerId) == false check currentRateLimitHits() == rateLimitHits + 2 @@ -1017,11 +960,20 @@ suite "GossipSub": let rateLimitHits = currentRateLimitHits() let (nodes, gossip0, gossip1) = await initializeGossipTest() - let msg = RPCMsg(control: some(ControlMessage(prune: @[ - ControlPrune(topicID: "foobar", peers: @[ - PeerInfoMsg(peerId: PeerId(data: newSeq[byte](33))) - ], backoff: 123'u64) - ]))) + let msg = RPCMsg( + control: some( + ControlMessage( + prune: + @[ + ControlPrune( + topicID: "foobar", + peers: @[PeerInfoMsg(peerId: PeerId(data: newSeq[byte](33)))], + backoff: 123'u64, + ) + ] + ) + ) + ) gossip0.broadcast(gossip0.mesh["foobar"], msg, isHighPriority = true) await sleepAsync(300.millis) @@ -1030,11 +982,20 @@ suite "GossipSub": # Disconnect peer when rate limiting is enabled gossip1.parameters.disconnectPeerAboveRateLimit = true - let msg2 = RPCMsg(control: some(ControlMessage(prune: @[ - ControlPrune(topicID: "foobar", peers: @[ - PeerInfoMsg(peerId: PeerId(data: newSeq[byte](35))) - ], backoff: 123'u64) - ]))) + let msg2 = RPCMsg( + control: some( + ControlMessage( + prune: + @[ + ControlPrune( + topicID: "foobar", + peers: @[PeerInfoMsg(peerId: PeerId(data: newSeq[byte](35)))], + backoff: 123'u64, + ) + ] + ) + ) + ) gossip0.broadcast(gossip0.mesh["foobar"], msg2, isHighPriority = true) checkUntilTimeout gossip1.switch.isConnected(gossip0.switch.peerInfo.peerId) == false @@ -1047,7 +1008,9 @@ suite "GossipSub": let (nodes, gossip0, gossip1) = await initializeGossipTest() let topic = "foobar" - proc execValidator(topic: string, message: messages.Message): Future[ValidationResult] {.raises: [].} = + proc execValidator( + topic: string, message: messages.Message + ): Future[ValidationResult] {.raises: [].} = let res = newFuture[ValidationResult]() res.complete(ValidationResult.Reject) res diff --git a/tests/pubsub/testgossipsub2.nim b/tests/pubsub/testgossipsub2.nim index 2ce831ad68..ffc5c24e40 100644 --- a/tests/pubsub/testgossipsub2.nim +++ b/tests/pubsub/testgossipsub2.nim @@ -11,20 +11,26 @@ import sequtils, options, tables, sets import chronos, stew/byteutils, chronicles -import utils, ../../libp2p/[errors, - peerid, - peerinfo, - stream/connection, - stream/bufferstream, - crypto/crypto, - protocols/pubsub/pubsub, - protocols/pubsub/gossipsub, - protocols/pubsub/pubsubpeer, - protocols/pubsub/peertable, - protocols/pubsub/rpc/messages] +import + utils, + ../../libp2p/[ + errors, + peerid, + peerinfo, + stream/connection, + stream/bufferstream, + crypto/crypto, + protocols/pubsub/pubsub, + protocols/pubsub/gossipsub, + protocols/pubsub/pubsubpeer, + protocols/pubsub/peertable, + protocols/pubsub/rpc/messages, + ] import ../helpers -template tryPublish(call: untyped, require: int, wait = 10.milliseconds, timeout = 10.seconds): untyped = +template tryPublish( + call: untyped, require: int, wait = 10.milliseconds, timeout = 10.seconds +): untyped = var expiration = Moment.now() + timeout pubs = 0 @@ -49,7 +55,7 @@ suite "GossipSub": var seen: Table[string, int] var seenFut = newFuture[void]() - for i in 0..= runs: seenFut.complete() @@ -72,22 +78,21 @@ suite "GossipSub": await waitSub(nodes[0], dialer, "foobar") # we want to test ping pong deliveries via control Iwant/Ihave, so we publish just in a tap - let publishedTo = nodes[0] - .publish("foobar", toBytes("from node " & $nodes[0].peerInfo.peerId)) - .await + let publishedTo = nodes[0].publish( + "foobar", toBytes("from node " & $nodes[0].peerInfo.peerId) + ).await check: publishedTo != 0 publishedTo != runs await wait(seenFut, 5.minutes) - check: seen.len >= runs + check: + seen.len >= runs for k, v in seen.pairs: - check: v >= 1 + check: + v >= 1 - await allFuturesThrowing( - nodes.mapIt( - allFutures( - it.switch.stop()))) + await allFuturesThrowing(nodes.mapIt(allFutures(it.switch.stop()))) await allFuturesThrowing(nodesFut) @@ -101,26 +106,22 @@ suite "GossipSub": nodes = generateNodes(2, gossip = true) # start switches - nodesFut = await allFinished( - nodes[0].switch.start(), - nodes[1].switch.start(), - ) + nodesFut = await allFinished(nodes[0].switch.start(), nodes[1].switch.start()) # We must subscribe before setting the validator nodes[0].subscribe("foobar", handler) var gossip = GossipSub(nodes[0]) let invalidDetected = newFuture[void]() - gossip.subscriptionValidator = - proc(topic: string): bool = - if topic == "foobar": - try: - invalidDetected.complete() - except: - raise newException(Defect, "Exception during subscriptionValidator") - false - else: - true + gossip.subscriptionValidator = proc(topic: string): bool = + if topic == "foobar": + try: + invalidDetected.complete() + except: + raise newException(Defect, "Exception during subscriptionValidator") + false + else: + true await subscribeNodes(nodes) @@ -128,42 +129,39 @@ suite "GossipSub": await invalidDetected.wait(10.seconds) - await allFuturesThrowing( - nodes[0].switch.stop(), - nodes[1].switch.stop() - ) + await allFuturesThrowing(nodes[0].switch.stop(), nodes[1].switch.stop()) await allFuturesThrowing(nodesFut.concat()) asyncTest "GossipSub test directPeers": let nodes = generateNodes(2, gossip = true) await allFutures(nodes[0].switch.start(), nodes[1].switch.start()) - await GossipSub(nodes[0]).addDirectPeer(nodes[1].switch.peerInfo.peerId, nodes[1].switch.peerInfo.addrs) + await GossipSub(nodes[0]).addDirectPeer( + nodes[1].switch.peerInfo.peerId, nodes[1].switch.peerInfo.addrs + ) let invalidDetected = newFuture[void]() - GossipSub(nodes[0]).subscriptionValidator = - proc(topic: string): bool = - if topic == "foobar": - try: - invalidDetected.complete() - except: - raise newException(Defect, "Exception during subscriptionValidator") - false - else: - true + GossipSub(nodes[0]).subscriptionValidator = proc(topic: string): bool = + if topic == "foobar": + try: + invalidDetected.complete() + except: + raise newException(Defect, "Exception during subscriptionValidator") + false + else: + true # DO NOT SUBSCRIBE, CONNECTION SHOULD HAPPEN ### await subscribeNodes(nodes) - proc handler(topic: string, data: seq[byte]) {.async.} = discard + proc handler(topic: string, data: seq[byte]) {.async.} = + discard + nodes[1].subscribe("foobar", handler) await invalidDetected.wait(10.seconds) - await allFuturesThrowing( - nodes[0].switch.stop(), - nodes[1].switch.stop() - ) + await allFuturesThrowing(nodes[0].switch.stop(), nodes[1].switch.stop()) asyncTest "GossipSub directPeers: always forward messages": let @@ -171,20 +169,27 @@ suite "GossipSub": # start switches nodesFut = await allFinished( - nodes[0].switch.start(), - nodes[1].switch.start(), - nodes[2].switch.start(), + nodes[0].switch.start(), nodes[1].switch.start(), nodes[2].switch.start() ) - await GossipSub(nodes[0]).addDirectPeer(nodes[1].switch.peerInfo.peerId, nodes[1].switch.peerInfo.addrs) - await GossipSub(nodes[1]).addDirectPeer(nodes[0].switch.peerInfo.peerId, nodes[0].switch.peerInfo.addrs) - await GossipSub(nodes[1]).addDirectPeer(nodes[2].switch.peerInfo.peerId, nodes[2].switch.peerInfo.addrs) - await GossipSub(nodes[2]).addDirectPeer(nodes[1].switch.peerInfo.peerId, nodes[1].switch.peerInfo.addrs) + await GossipSub(nodes[0]).addDirectPeer( + nodes[1].switch.peerInfo.peerId, nodes[1].switch.peerInfo.addrs + ) + await GossipSub(nodes[1]).addDirectPeer( + nodes[0].switch.peerInfo.peerId, nodes[0].switch.peerInfo.addrs + ) + await GossipSub(nodes[1]).addDirectPeer( + nodes[2].switch.peerInfo.peerId, nodes[2].switch.peerInfo.addrs + ) + await GossipSub(nodes[2]).addDirectPeer( + nodes[1].switch.peerInfo.peerId, nodes[1].switch.peerInfo.addrs + ) var handlerFut = newFuture[void]() proc handler(topic: string, data: seq[byte]) {.async.} = check topic == "foobar" handlerFut.complete() + proc noop(topic: string, data: seq[byte]) {.async.} = check topic == "foobar" @@ -202,9 +207,7 @@ suite "GossipSub": check "foobar" notin GossipSub(nodes[2]).mesh await allFuturesThrowing( - nodes[0].switch.stop(), - nodes[1].switch.stop(), - nodes[2].switch.stop() + nodes[0].switch.stop(), nodes[1].switch.stop(), nodes[2].switch.stop() ) await allFuturesThrowing(nodesFut.concat()) @@ -214,13 +217,14 @@ suite "GossipSub": nodes = generateNodes(2, gossip = true) # start switches - nodesFut = await allFinished( - nodes[0].switch.start(), - nodes[1].switch.start(), - ) + nodesFut = await allFinished(nodes[0].switch.start(), nodes[1].switch.start()) - await GossipSub(nodes[0]).addDirectPeer(nodes[1].switch.peerInfo.peerId, nodes[1].switch.peerInfo.addrs) - await GossipSub(nodes[1]).addDirectPeer(nodes[0].switch.peerInfo.peerId, nodes[0].switch.peerInfo.addrs) + await GossipSub(nodes[0]).addDirectPeer( + nodes[1].switch.peerInfo.peerId, nodes[1].switch.peerInfo.addrs + ) + await GossipSub(nodes[1]).addDirectPeer( + nodes[0].switch.peerInfo.peerId, nodes[0].switch.peerInfo.addrs + ) GossipSub(nodes[1]).parameters.disconnectBadPeers = true GossipSub(nodes[1]).parameters.graylistThreshold = 100000 @@ -240,7 +244,8 @@ suite "GossipSub": GossipSub(nodes[1]).updateScores() # peer shouldn't be in our mesh check: - GossipSub(nodes[1]).peerStats[nodes[0].switch.peerInfo.peerId].score < GossipSub(nodes[1]).parameters.graylistThreshold + GossipSub(nodes[1]).peerStats[nodes[0].switch.peerInfo.peerId].score < + GossipSub(nodes[1]).parameters.graylistThreshold GossipSub(nodes[1]).updateScores() handlerFut = newFuture[void]() @@ -249,10 +254,7 @@ suite "GossipSub": # Without directPeers, this would fail await handlerFut.wait(1.seconds) - await allFuturesThrowing( - nodes[0].switch.stop(), - nodes[1].switch.stop() - ) + await allFuturesThrowing(nodes[0].switch.stop(), nodes[1].switch.stop()) await allFuturesThrowing(nodesFut.concat()) @@ -267,7 +269,7 @@ suite "GossipSub": var seen: Table[string, int] var seenFut = newFuture[void]() - for i in 0..= runs + check: + seen.len >= runs for k, v in seen.pairs: - check: v >= 1 + check: + v >= 1 for node in nodes: var gossip = GossipSub(node) @@ -307,13 +311,13 @@ suite "GossipSub": # Removing some subscriptions - for i in 0.. 0 and m.fromPeer.data.len > 0: - byteutils.toHex(m.seqno) & $m.fromPeer - else: - # This part is irrelevant because it's not standard, - # We use it exclusively for testing basically and users should - # implement their own logic in the case they use anonymization - $m.data.hash & $m.topic.hash + let mid = + if m.seqno.len > 0 and m.fromPeer.data.len > 0: + byteutils.toHex(m.seqno) & $m.fromPeer + else: + # This part is irrelevant because it's not standard, + # We use it exclusively for testing basically and users should + # implement their own logic in the case they use anonymization + $m.data.hash & $m.topic.hash ok mid.toBytes() proc generateNodes*( - num: Natural, - secureManagers: openArray[SecureProtocol] = [ - SecureProtocol.Noise - ], - msgIdProvider: MsgIdProvider = defaultMsgIdProvider, - gossip: bool = false, - triggerSelf: bool = false, - verifySignature: bool = libp2p_pubsub_verify, - anonymize: bool = libp2p_pubsub_anonymize, - sign: bool = libp2p_pubsub_sign, - sendSignedPeerRecord = false, - unsubscribeBackoff = 1.seconds, - maxMessageSize: int = 1024 * 1024, - enablePX: bool = false, - overheadRateLimit: Opt[tuple[bytes: int, interval: Duration]] = Opt.none(tuple[bytes: int, interval: Duration])): seq[PubSub] = - - for i in 0..= 0.3: + proc statusAndConfidenceHandler( + networkReachability: NetworkReachability, confidence: Opt[float] + ) {.async.} = + if networkReachability == NetworkReachability.Reachable and confidence.isSome() and + confidence.get() >= 0.3: if not awaiter.finished: awaiter.complete() @@ -110,19 +123,21 @@ suite "Autonat Service": check autonatService.networkReachability == NetworkReachability.Reachable check libp2p_autonat_reachability_confidence.value(["Reachable"]) == 0.3 - check switch1.peerInfo.addrs == switch1.peerInfo.addrs.mapIt(switch1.peerStore.guessDialableAddr(it)) + check switch1.peerInfo.addrs == + switch1.peerInfo.addrs.mapIt(switch1.peerStore.guessDialableAddr(it)) await allFuturesThrowing( - switch1.stop(), switch2.stop(), switch3.stop(), switch4.stop()) + switch1.stop(), switch2.stop(), switch3.stop(), switch4.stop() + ) check switch1.peerInfo.addrs == switch1.peerInfo.addrs asyncTest "Peer must be not reachable and then reachable": - let autonatClientStub = AutonatClientStub.new(expectedDials = 6) autonatClientStub.answer = NotReachable - let autonatService = AutonatService.new(autonatClientStub, newRng(), Opt.some(1.seconds)) + let autonatService = + AutonatService.new(autonatClientStub, newRng(), Opt.some(1.seconds)) let switch1 = createSwitch(autonatService) let switch2 = createSwitch() @@ -131,8 +146,11 @@ suite "Autonat Service": let awaiter = newFuture[void]() - proc statusAndConfidenceHandler(networkReachability: NetworkReachability, confidence: Opt[float]) {.async.} = - if networkReachability == NetworkReachability.NotReachable and confidence.isSome() and confidence.get() >= 0.3: + proc statusAndConfidenceHandler( + networkReachability: NetworkReachability, confidence: Opt[float] + ) {.async.} = + if networkReachability == NetworkReachability.NotReachable and confidence.isSome() and + confidence.get() >= 0.3: if not awaiter.finished: autonatClientStub.answer = Reachable awaiter.complete() @@ -160,11 +178,14 @@ suite "Autonat Service": check autonatService.networkReachability == NetworkReachability.Reachable check libp2p_autonat_reachability_confidence.value(["Reachable"]) == 0.3 - await allFuturesThrowing(switch1.stop(), switch2.stop(), switch3.stop(), switch4.stop()) + await allFuturesThrowing( + switch1.stop(), switch2.stop(), switch3.stop(), switch4.stop() + ) asyncTest "Peer must be reachable when one connected peer has autonat disabled": - - let autonatService = AutonatService.new(AutonatClient.new(), newRng(), Opt.some(1.seconds), maxQueueSize = 2) + let autonatService = AutonatService.new( + AutonatClient.new(), newRng(), Opt.some(1.seconds), maxQueueSize = 2 + ) let switch1 = createSwitch(autonatService) let switch2 = createSwitch(withAutonat = false) @@ -173,8 +194,11 @@ suite "Autonat Service": let awaiter = newFuture[void]() - proc statusAndConfidenceHandler(networkReachability: NetworkReachability, confidence: Opt[float]) {.async.} = - if networkReachability == NetworkReachability.Reachable and confidence.isSome() and confidence.get() == 1: + proc statusAndConfidenceHandler( + networkReachability: NetworkReachability, confidence: Opt[float] + ) {.async.} = + if networkReachability == NetworkReachability.Reachable and confidence.isSome() and + confidence.get() == 1: if not awaiter.finished: awaiter.complete() @@ -197,14 +221,16 @@ suite "Autonat Service": check libp2p_autonat_reachability_confidence.value(["Reachable"]) == 1 await allFuturesThrowing( - switch1.stop(), switch2.stop(), switch3.stop(), switch4.stop()) + switch1.stop(), switch2.stop(), switch3.stop(), switch4.stop() + ) asyncTest "Unknown answers must be ignored": - let autonatClientStub = AutonatClientStub.new(expectedDials = 6) autonatClientStub.answer = NotReachable - let autonatService = AutonatService.new(autonatClientStub, newRng(), Opt.some(1.seconds), maxQueueSize = 3) + let autonatService = AutonatService.new( + autonatClientStub, newRng(), Opt.some(1.seconds), maxQueueSize = 3 + ) let switch1 = createSwitch(autonatService) let switch2 = createSwitch() @@ -213,8 +239,11 @@ suite "Autonat Service": let awaiter = newFuture[void]() - proc statusAndConfidenceHandler(networkReachability: NetworkReachability, confidence: Opt[float]) {.async.} = - if networkReachability == NetworkReachability.NotReachable and confidence.isSome() and confidence.get() >= 0.3: + proc statusAndConfidenceHandler( + networkReachability: NetworkReachability, confidence: Opt[float] + ) {.async.} = + if networkReachability == NetworkReachability.NotReachable and confidence.isSome() and + confidence.get() >= 0.3: if not awaiter.finished: autonatClientStub.answer = Unknown awaiter.complete() @@ -235,19 +264,22 @@ suite "Autonat Service": await awaiter check autonatService.networkReachability == NetworkReachability.NotReachable - check libp2p_autonat_reachability_confidence.value(["NotReachable"]) == 1/3 + check libp2p_autonat_reachability_confidence.value(["NotReachable"]) == 1 / 3 await autonatClientStub.finished check autonatService.networkReachability == NetworkReachability.NotReachable - check libp2p_autonat_reachability_confidence.value(["NotReachable"]) == 1/3 + check libp2p_autonat_reachability_confidence.value(["NotReachable"]) == 1 / 3 - await allFuturesThrowing(switch1.stop(), switch2.stop(), switch3.stop(), switch4.stop()) + await allFuturesThrowing( + switch1.stop(), switch2.stop(), switch3.stop(), switch4.stop() + ) asyncTest "Calling setup and stop twice must work": - let switch = createSwitch() - let autonatService = AutonatService.new(AutonatClientStub.new(expectedDials = 0), newRng(), Opt.some(1.seconds)) + let autonatService = AutonatService.new( + AutonatClientStub.new(expectedDials = 0), newRng(), Opt.some(1.seconds) + ) check (await autonatService.setup(switch)) == true check (await autonatService.setup(switch)) == false @@ -258,16 +290,22 @@ suite "Autonat Service": await allFuturesThrowing(switch.stop()) asyncTest "Must bypass maxConnectionsPerPeer limit": - let autonatService = AutonatService.new(AutonatClient.new(), newRng(), Opt.some(1.seconds), maxQueueSize = 1) + let autonatService = AutonatService.new( + AutonatClient.new(), newRng(), Opt.some(1.seconds), maxQueueSize = 1 + ) let switch1 = createSwitch(autonatService, maxConnsPerPeer = 0) - let switch2 = createSwitch(maxConnsPerPeer = 0, nameResolver = MockResolver.default()) + let switch2 = + createSwitch(maxConnsPerPeer = 0, nameResolver = MockResolver.default()) let awaiter = newFuture[void]() - proc statusAndConfidenceHandler(networkReachability: NetworkReachability, confidence: Opt[float]) {.async.} = - if networkReachability == NetworkReachability.Reachable and confidence.isSome() and confidence.get() == 1: + proc statusAndConfidenceHandler( + networkReachability: NetworkReachability, confidence: Opt[float] + ) {.async.} = + if networkReachability == NetworkReachability.Reachable and confidence.isSome() and + confidence.get() == 1: if not awaiter.finished: awaiter.complete() @@ -276,7 +314,12 @@ suite "Autonat Service": autonatService.statusAndConfidenceHandler(statusAndConfidenceHandler) await switch1.start() - switch1.peerInfo.addrs.add([ MultiAddress.init("/dns4/localhost/").tryGet() & switch1.peerInfo.addrs[0][1].tryGet() ]) + switch1.peerInfo.addrs.add( + [ + MultiAddress.init("/dns4/localhost/").tryGet() & + switch1.peerInfo.addrs[0][1].tryGet() + ] + ) await switch2.start() @@ -287,13 +330,18 @@ suite "Autonat Service": check autonatService.networkReachability == NetworkReachability.Reachable check libp2p_autonat_reachability_confidence.value(["Reachable"]) == 1 - await allFuturesThrowing( - switch1.stop(), switch2.stop()) + await allFuturesThrowing(switch1.stop(), switch2.stop()) asyncTest "Must work when peers ask each other at the same time with max 1 conn per peer": - let autonatService1 = AutonatService.new(AutonatClient.new(), newRng(), Opt.some(500.millis), maxQueueSize = 3) - let autonatService2 = AutonatService.new(AutonatClient.new(), newRng(), Opt.some(500.millis), maxQueueSize = 3) - let autonatService3 = AutonatService.new(AutonatClient.new(), newRng(), Opt.some(500.millis), maxQueueSize = 3) + let autonatService1 = AutonatService.new( + AutonatClient.new(), newRng(), Opt.some(500.millis), maxQueueSize = 3 + ) + let autonatService2 = AutonatService.new( + AutonatClient.new(), newRng(), Opt.some(500.millis), maxQueueSize = 3 + ) + let autonatService3 = AutonatService.new( + AutonatClient.new(), newRng(), Opt.some(500.millis), maxQueueSize = 3 + ) let switch1 = createSwitch(autonatService1, maxConnsPerPeer = 0) let switch2 = createSwitch(autonatService2, maxConnsPerPeer = 0) @@ -303,13 +351,19 @@ suite "Autonat Service": let awaiter2 = newFuture[void]() let awaiter3 = newFuture[void]() - proc statusAndConfidenceHandler1(networkReachability: NetworkReachability, confidence: Opt[float]) {.async.} = - if networkReachability == NetworkReachability.Reachable and confidence.isSome() and confidence.get() == 1: + proc statusAndConfidenceHandler1( + networkReachability: NetworkReachability, confidence: Opt[float] + ) {.async.} = + if networkReachability == NetworkReachability.Reachable and confidence.isSome() and + confidence.get() == 1: if not awaiter1.finished: awaiter1.complete() - proc statusAndConfidenceHandler2(networkReachability: NetworkReachability, confidence: Opt[float]) {.async.} = - if networkReachability == NetworkReachability.Reachable and confidence.isSome() and confidence.get() == 1: + proc statusAndConfidenceHandler2( + networkReachability: NetworkReachability, confidence: Opt[float] + ) {.async.} = + if networkReachability == NetworkReachability.Reachable and confidence.isSome() and + confidence.get() == 1: if not awaiter2.finished: awaiter2.complete() @@ -334,20 +388,26 @@ suite "Autonat Service": check autonatService2.networkReachability == NetworkReachability.Reachable check libp2p_autonat_reachability_confidence.value(["Reachable"]) == 1 - await allFuturesThrowing( - switch1.stop(), switch2.stop(), switch3.stop()) + await allFuturesThrowing(switch1.stop(), switch2.stop(), switch3.stop()) asyncTest "Must work for one peer when two peers ask each other at the same time with max 1 conn per peer": - let autonatService1 = AutonatService.new(AutonatClient.new(), newRng(), Opt.some(500.millis), maxQueueSize = 3) - let autonatService2 = AutonatService.new(AutonatClient.new(), newRng(), Opt.some(500.millis), maxQueueSize = 3) + let autonatService1 = AutonatService.new( + AutonatClient.new(), newRng(), Opt.some(500.millis), maxQueueSize = 3 + ) + let autonatService2 = AutonatService.new( + AutonatClient.new(), newRng(), Opt.some(500.millis), maxQueueSize = 3 + ) let switch1 = createSwitch(autonatService1, maxConnsPerPeer = 0) let switch2 = createSwitch(autonatService2, maxConnsPerPeer = 0) let awaiter1 = newFuture[void]() - proc statusAndConfidenceHandler1(networkReachability: NetworkReachability, confidence: Opt[float]) {.async.} = - if networkReachability == NetworkReachability.Reachable and confidence.isSome() and confidence.get() == 1: + proc statusAndConfidenceHandler1( + networkReachability: NetworkReachability, confidence: Opt[float] + ) {.async.} = + if networkReachability == NetworkReachability.Reachable and confidence.isSome() and + confidence.get() == 1: if not awaiter1.finished: awaiter1.complete() @@ -363,7 +423,9 @@ suite "Autonat Service": # We allow a temp conn for the peer to dial us. It could use this conn to just connect to us and not dial. # We don't care if it fails at this point or not. But this conn must be closed eventually. # Bellow we check that there's only one connection between the peers - await switch2.connect(switch1.peerInfo.peerId, switch1.peerInfo.addrs, reuseConnection = false) + await switch2.connect( + switch1.peerInfo.peerId, switch1.peerInfo.addrs, reuseConnection = false + ) except CatchableError: discard @@ -375,11 +437,12 @@ suite "Autonat Service": # Make sure remote peer can't create a connection to us check switch1.connManager.connCount(switch2.peerInfo.peerId) == 1 - await allFuturesThrowing( - switch1.stop(), switch2.stop()) + await allFuturesThrowing(switch1.stop(), switch2.stop()) asyncTest "Must work with low maxConnections": - let autonatService = AutonatService.new(AutonatClient.new(), newRng(), Opt.some(1.seconds), maxQueueSize = 1) + let autonatService = AutonatService.new( + AutonatClient.new(), newRng(), Opt.some(1.seconds), maxQueueSize = 1 + ) let switch1 = createSwitch(autonatService, maxConns = 4) let switch2 = createSwitch() @@ -389,8 +452,11 @@ suite "Autonat Service": var awaiter = newFuture[void]() - proc statusAndConfidenceHandler(networkReachability: NetworkReachability, confidence: Opt[float]) {.async.} = - if networkReachability == NetworkReachability.Reachable and confidence.isSome() and confidence.get() == 1: + proc statusAndConfidenceHandler( + networkReachability: NetworkReachability, confidence: Opt[float] + ) {.async.} = + if networkReachability == NetworkReachability.Reachable and confidence.isSome() and + confidence.get() == 1: if not awaiter.finished: awaiter.complete() @@ -421,7 +487,8 @@ suite "Autonat Service": check libp2p_autonat_reachability_confidence.value(["Reachable"]) == 1 await allFuturesThrowing( - switch1.stop(), switch2.stop(), switch3.stop(), switch4.stop(), switch5.stop()) + switch1.stop(), switch2.stop(), switch3.stop(), switch4.stop(), switch5.stop() + ) asyncTest "Peer must not ask an incoming peer": let autonatService = AutonatService.new(AutonatClient.new(), newRng()) @@ -429,7 +496,9 @@ suite "Autonat Service": let switch1 = createSwitch(autonatService) let switch2 = createSwitch() - proc statusAndConfidenceHandler(networkReachability: NetworkReachability, confidence: Opt[float]) {.async.} = + proc statusAndConfidenceHandler( + networkReachability: NetworkReachability, confidence: Opt[float] + ) {.async.} = fail() check autonatService.networkReachability == NetworkReachability.Unknown @@ -443,5 +512,4 @@ suite "Autonat Service": await sleepAsync(500.milliseconds) - await allFuturesThrowing( - switch1.stop(), switch2.stop()) + await allFuturesThrowing(switch1.stop(), switch2.stop()) diff --git a/tests/testautorelay.nim b/tests/testautorelay.nim index 7df4d00ac7..a69a0bd1cd 100644 --- a/tests/testautorelay.nim +++ b/tests/testautorelay.nim @@ -11,16 +11,20 @@ import chronos, options import ../libp2p -import ../libp2p/[crypto/crypto, - protocols/connectivity/relay/relay, - protocols/connectivity/relay/client, - services/autorelayservice] +import + ../libp2p/[ + crypto/crypto, + protocols/connectivity/relay/relay, + protocols/connectivity/relay/client, + services/autorelayservice, + ] import ./helpers proc createSwitch(r: Relay, autorelay: Service = nil): Switch = - var builder = SwitchBuilder.new() + var builder = SwitchBuilder + .new() .withRng(newRng()) - .withAddresses(@[ MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet() ], false) + .withAddresses(@[MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet()], false) .withTcpTransport() .withMplex() .withNoise() @@ -29,10 +33,13 @@ proc createSwitch(r: Relay, autorelay: Service = nil): Switch = builder = builder.withServices(@[autorelay]) builder.build() - proc buildRelayMA(switchRelay: Switch, switchClient: Switch): MultiAddress = - MultiAddress.init($switchRelay.peerInfo.addrs[0] & "/p2p/" & - $switchRelay.peerInfo.peerId & "/p2p-circuit").get() + MultiAddress + .init( + $switchRelay.peerInfo.addrs[0] & "/p2p/" & $switchRelay.peerInfo.peerId & + "/p2p-circuit" + ) + .get() suite "Autorelay": asyncTeardown: @@ -49,9 +56,12 @@ suite "Autorelay": relayClient = RelayClient.new() let fut = newFuture[void]() proc checkMA(addresses: seq[MultiAddress]) = - check: addresses[0] == buildRelayMA(switchRelay, switchClient) - check: addresses.len() == 1 + check: + addresses[0] == buildRelayMA(switchRelay, switchClient) + check: + addresses.len() == 1 fut.complete() + autorelay = AutoRelayService.new(3, relayClient, checkMA, newRng()) switchClient = createSwitch(relayClient, autorelay) await allFutures(switchClient.start(), switchRelay.start()) @@ -68,8 +78,10 @@ suite "Autorelay": relayClient = RelayClient.new() let fut = newFuture[void]() proc checkMA(address: seq[MultiAddress]) = - check: address[0] == buildRelayMA(switchRelay, switchClient) + check: + address[0] == buildRelayMA(switchRelay, switchClient) fut.complete() + let autorelay = AutoRelayService.new(3, relayClient, checkMA, newRng()) switchClient = createSwitch(relayClient, autorelay) await allFutures(switchClient.start(), switchRelay.start()) @@ -112,6 +124,7 @@ suite "Autorelay": addresses.len() == 2 state += 1 fut.complete() + let autorelay = AutoRelayService.new(2, relayClient, checkMA, newRng()) switchClient = createSwitch(relayClient, autorelay) await allFutures(switchClient.start(), rel1.start(), rel2.start(), rel3.start()) diff --git a/tests/testbufferstream.nim b/tests/testbufferstream.nim index 8a9f8b5f20..2bc392ddb2 100644 --- a/tests/testbufferstream.nim +++ b/tests/testbufferstream.nim @@ -10,9 +10,7 @@ # those terms. import chronos, stew/byteutils -import ../libp2p/stream/bufferstream, - ../libp2p/stream/lpstream, - ../libp2p/errors +import ../libp2p/stream/bufferstream, ../libp2p/stream/lpstream, ../libp2p/errors import ./helpers @@ -37,7 +35,8 @@ suite "BufferStream": check buff.len == 4 # the second write should not be visible yet var data: array[1, byte] - check: 1 == await buff.readOnce(addr data[0], data.len) + check: + 1 == await buff.readOnce(addr data[0], data.len) check ['1'] == string.fromBytes(data) await fut0 @@ -139,7 +138,7 @@ suite "BufferStream": var str: string proc writer() {.async.} = - for i in 0..<10: + for i in 0 ..< 10: await buff.pushData("123".toBytes()) str &= "123" await buff.close() # all data should still be read after close @@ -151,11 +150,9 @@ suite "BufferStream": expect LPStreamEOFError: while true: let x = await buff.readOnce(addr data[0], data.len) - str2 &= string.fromBytes(data[0..= 4: period = 75.milliseconds + let hb = t() await sleepAsync(500.milliseconds) await hb.cancelAndWait() @@ -46,7 +47,7 @@ when not defined(macosx): # (500 ms - 120 ms) / 75ms = 5x 75ms # total 9 check: - i in 8..11 + i in 8 .. 11 asyncTest "catch up on slow heartbeat": var i = 0 @@ -63,4 +64,4 @@ when not defined(macosx): # 360ms remaining, / 30ms = 12x # total 15 check: - i in 14..17 + i in 14 .. 17 diff --git a/tests/testhelpers.nim b/tests/testhelpers.nim index 0898b394e2..1448572988 100644 --- a/tests/testhelpers.nim +++ b/tests/testhelpers.nim @@ -12,7 +12,6 @@ import ./helpers suite "Helpers": - asyncTest "checkUntilTimeout should pass if the condition is true": let a = 2 let b = 2 diff --git a/tests/testhpservice.nim b/tests/testhpservice.nim index 2d9d9f625a..27fb4711d7 100644 --- a/tests/testhpservice.nim +++ b/tests/testhpservice.nim @@ -16,20 +16,19 @@ import chronos import unittest2 import ./helpers import ./stubs/switchstub -import ../libp2p/[builders, - switch, - wire, - services/hpservice, - services/autorelayservice] +import ../libp2p/[builders, switch, wire, services/hpservice, services/autorelayservice] import ../libp2p/protocols/connectivity/relay/[relay, client] import ../libp2p/protocols/connectivity/autonat/[service] import ../libp2p/nameresolving/[nameresolver, mockresolver] import stubs/autonatclientstub -proc createSwitch(r: Relay = nil, hpService: Service = nil, nameResolver: NameResolver = nil): Switch {.raises: [LPError].} = - var builder = SwitchBuilder.new() +proc createSwitch( + r: Relay = nil, hpService: Service = nil, nameResolver: NameResolver = nil +): Switch {.raises: [LPError].} = + var builder = SwitchBuilder + .new() .withRng(newRng()) - .withAddresses(@[ MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet() ]) + .withAddresses(@[MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet()]) .withTcpTransport() .withMplex() .withAutonat() @@ -47,9 +46,12 @@ proc createSwitch(r: Relay = nil, hpService: Service = nil, nameResolver: NameRe return builder.build() proc buildRelayMA(switchRelay: Switch, switchClient: Switch): MultiAddress = - MultiAddress.init($switchRelay.peerInfo.addrs[0] & "/p2p/" & - $switchRelay.peerInfo.peerId & "/p2p-circuit/p2p/" & - $switchClient.peerInfo.peerId).get() + MultiAddress + .init( + $switchRelay.peerInfo.addrs[0] & "/p2p/" & $switchRelay.peerInfo.peerId & + "/p2p-circuit/p2p/" & $switchClient.peerInfo.peerId + ) + .get() suite "Hole Punching": teardown: @@ -58,7 +60,8 @@ suite "Hole Punching": asyncTest "Direct connection must work when peer address is public": let autonatClientStub = AutonatClientStub.new(expectedDials = 1) autonatClientStub.answer = NotReachable - let autonatService = AutonatService.new(autonatClientStub, newRng(), maxQueueSize = 1) + let autonatService = + AutonatService.new(autonatClientStub, newRng(), maxQueueSize = 1) let relayClient = RelayClient.new() let privatePeerRelayAddr = newFuture[seq[MultiAddress]]() @@ -73,30 +76,53 @@ suite "Hole Punching": let hpservice = HPService.new(autonatService, autoRelayService) - let privatePeerSwitch = createSwitch(relayClient, hpservice, nameresolver = MockResolver.default()) + let privatePeerSwitch = + createSwitch(relayClient, hpservice, nameresolver = MockResolver.default()) let peerSwitch = createSwitch() let switchRelay = createSwitch(Relay.new()) - await allFuturesThrowing(switchRelay.start(), privatePeerSwitch.start(), publicPeerSwitch.start(), peerSwitch.start()) - publicPeerSwitch.peerInfo.addrs.add([ MultiAddress.init("/dns4/localhost/").tryGet() & publicPeerSwitch.peerInfo.addrs[0][1].tryGet() ]) + await allFuturesThrowing( + switchRelay.start(), + privatePeerSwitch.start(), + publicPeerSwitch.start(), + peerSwitch.start(), + ) + publicPeerSwitch.peerInfo.addrs.add( + [ + MultiAddress.init("/dns4/localhost/").tryGet() & + publicPeerSwitch.peerInfo.addrs[0][1].tryGet() + ] + ) - await privatePeerSwitch.connect(switchRelay.peerInfo.peerId, switchRelay.peerInfo.addrs) - await privatePeerSwitch.connect(peerSwitch.peerInfo.peerId, peerSwitch.peerInfo.addrs) # for autonat + await privatePeerSwitch.connect( + switchRelay.peerInfo.peerId, switchRelay.peerInfo.addrs + ) + await privatePeerSwitch.connect( + peerSwitch.peerInfo.peerId, peerSwitch.peerInfo.addrs + ) # for autonat - await publicPeerSwitch.connect(privatePeerSwitch.peerInfo.peerId, (await privatePeerRelayAddr)) + await publicPeerSwitch.connect( + privatePeerSwitch.peerInfo.peerId, (await privatePeerRelayAddr) + ) checkUntilTimeout: privatePeerSwitch.connManager.connCount(publicPeerSwitch.peerInfo.peerId) == 1 - not isRelayed(privatePeerSwitch.connManager.selectMuxer(publicPeerSwitch.peerInfo.peerId).connection) + not isRelayed( + privatePeerSwitch.connManager.selectMuxer(publicPeerSwitch.peerInfo.peerId).connection + ) await allFuturesThrowing( - privatePeerSwitch.stop(), publicPeerSwitch.stop(), switchRelay.stop(), peerSwitch.stop()) + privatePeerSwitch.stop(), + publicPeerSwitch.stop(), + switchRelay.stop(), + peerSwitch.stop(), + ) asyncTest "Direct connection must work when peer address is public and dns is used": - let autonatClientStub = AutonatClientStub.new(expectedDials = 1) autonatClientStub.answer = NotReachable - let autonatService = AutonatService.new(autonatClientStub, newRng(), maxQueueSize = 1) + let autonatService = + AutonatService.new(autonatClientStub, newRng(), maxQueueSize = 1) let relayClient = RelayClient.new() let privatePeerRelayAddr = newFuture[seq[MultiAddress]]() @@ -111,36 +137,65 @@ suite "Hole Punching": let hpservice = HPService.new(autonatService, autoRelayService) - let privatePeerSwitch = createSwitch(relayClient, hpservice, nameResolver = MockResolver.default()) + let privatePeerSwitch = + createSwitch(relayClient, hpservice, nameResolver = MockResolver.default()) let peerSwitch = createSwitch() let switchRelay = createSwitch(Relay.new()) - await allFuturesThrowing(switchRelay.start(), privatePeerSwitch.start(), publicPeerSwitch.start(), peerSwitch.start()) - publicPeerSwitch.peerInfo.addrs.add([ MultiAddress.init("/dns4/localhost/").tryGet() & publicPeerSwitch.peerInfo.addrs[0][1].tryGet() ]) + await allFuturesThrowing( + switchRelay.start(), + privatePeerSwitch.start(), + publicPeerSwitch.start(), + peerSwitch.start(), + ) + publicPeerSwitch.peerInfo.addrs.add( + [ + MultiAddress.init("/dns4/localhost/").tryGet() & + publicPeerSwitch.peerInfo.addrs[0][1].tryGet() + ] + ) - await privatePeerSwitch.connect(switchRelay.peerInfo.peerId, switchRelay.peerInfo.addrs) - await privatePeerSwitch.connect(peerSwitch.peerInfo.peerId, peerSwitch.peerInfo.addrs) # for autonat + await privatePeerSwitch.connect( + switchRelay.peerInfo.peerId, switchRelay.peerInfo.addrs + ) + await privatePeerSwitch.connect( + peerSwitch.peerInfo.peerId, peerSwitch.peerInfo.addrs + ) # for autonat - await publicPeerSwitch.connect(privatePeerSwitch.peerInfo.peerId, (await privatePeerRelayAddr)) + await publicPeerSwitch.connect( + privatePeerSwitch.peerInfo.peerId, (await privatePeerRelayAddr) + ) checkUntilTimeout: privatePeerSwitch.connManager.connCount(publicPeerSwitch.peerInfo.peerId) == 1 - not isRelayed(privatePeerSwitch.connManager.selectMuxer(publicPeerSwitch.peerInfo.peerId).connection) + not isRelayed( + privatePeerSwitch.connManager.selectMuxer(publicPeerSwitch.peerInfo.peerId).connection + ) await allFuturesThrowing( - privatePeerSwitch.stop(), publicPeerSwitch.stop(), switchRelay.stop(), peerSwitch.stop()) + privatePeerSwitch.stop(), + publicPeerSwitch.stop(), + switchRelay.stop(), + peerSwitch.stop(), + ) - proc holePunchingTest(initiatorConnectStub: connectStubType, rcvConnectStub: connectStubType, answer: Answer) {.async.} = + proc holePunchingTest( + initiatorConnectStub: connectStubType, + rcvConnectStub: connectStubType, + answer: Answer, + ) {.async.} = # There's no check in this test cause it can't test hole punching locally. It exists just to be sure the rest of # the code works properly. let autonatClientStub1 = AutonatClientStub.new(expectedDials = 1) autonatClientStub1.answer = NotReachable - let autonatService1 = AutonatService.new(autonatClientStub1, newRng(), maxQueueSize = 1) + let autonatService1 = + AutonatService.new(autonatClientStub1, newRng(), maxQueueSize = 1) let autonatClientStub2 = AutonatClientStub.new(expectedDials = 1) autonatClientStub2.answer = answer - let autonatService2 = AutonatService.new(autonatClientStub2, newRng(), maxQueueSize = 1) + let autonatService2 = + AutonatService.new(autonatClientStub2, newRng(), maxQueueSize = 1) let relayClient1 = RelayClient.new() let relayClient2 = RelayClient.new() @@ -156,7 +211,9 @@ suite "Hole Punching": let hpservice1 = HPService.new(autonatService1, autoRelayService1) let hpservice2 = HPService.new(autonatService2, autoRelayService2) - let privatePeerSwitch1 = SwitchStub.new(createSwitch(relayClient1, hpservice1, nameresolver = MockResolver.default())) + let privatePeerSwitch1 = SwitchStub.new( + createSwitch(relayClient1, hpservice1, nameresolver = MockResolver.default()) + ) let privatePeerSwitch2 = SwitchStub.new(createSwitch(relayClient2, hpservice2)) let switchRelay = createSwitch(Relay.new()) @@ -168,45 +225,75 @@ suite "Hole Punching": var awaiter = newFuture[void]() await allFuturesThrowing( - switchRelay.start(), privatePeerSwitch1.start(), privatePeerSwitch2.start(), - switchAux.start(), switchAux2.start(), switchAux3.start(), switchAux4.start() + switchRelay.start(), + privatePeerSwitch1.start(), + privatePeerSwitch2.start(), + switchAux.start(), + switchAux2.start(), + switchAux3.start(), + switchAux4.start(), ) - await privatePeerSwitch1.connect(switchRelay.peerInfo.peerId, switchRelay.peerInfo.addrs) - await privatePeerSwitch2.connect(switchAux.peerInfo.peerId, switchAux.peerInfo.addrs) + await privatePeerSwitch1.connect( + switchRelay.peerInfo.peerId, switchRelay.peerInfo.addrs + ) + await privatePeerSwitch2.connect( + switchAux.peerInfo.peerId, switchAux.peerInfo.addrs + ) await sleepAsync(200.millis) - await privatePeerSwitch1.connect(switchAux2.peerInfo.peerId, switchAux2.peerInfo.addrs) - await privatePeerSwitch1.connect(switchAux3.peerInfo.peerId, switchAux3.peerInfo.addrs) - await privatePeerSwitch1.connect(switchAux4.peerInfo.peerId, switchAux4.peerInfo.addrs) + await privatePeerSwitch1.connect( + switchAux2.peerInfo.peerId, switchAux2.peerInfo.addrs + ) + await privatePeerSwitch1.connect( + switchAux3.peerInfo.peerId, switchAux3.peerInfo.addrs + ) + await privatePeerSwitch1.connect( + switchAux4.peerInfo.peerId, switchAux4.peerInfo.addrs + ) - await privatePeerSwitch2.connect(switchAux2.peerInfo.peerId, switchAux2.peerInfo.addrs) - await privatePeerSwitch2.connect(switchAux3.peerInfo.peerId, switchAux3.peerInfo.addrs) - await privatePeerSwitch2.connect(switchAux4.peerInfo.peerId, switchAux4.peerInfo.addrs) + await privatePeerSwitch2.connect( + switchAux2.peerInfo.peerId, switchAux2.peerInfo.addrs + ) + await privatePeerSwitch2.connect( + switchAux3.peerInfo.peerId, switchAux3.peerInfo.addrs + ) + await privatePeerSwitch2.connect( + switchAux4.peerInfo.peerId, switchAux4.peerInfo.addrs + ) privatePeerSwitch1.connectStub = initiatorConnectStub - await privatePeerSwitch2.connect(privatePeerSwitch1.peerInfo.peerId, (await privatePeerRelayAddr1)) + await privatePeerSwitch2.connect( + privatePeerSwitch1.peerInfo.peerId, (await privatePeerRelayAddr1) + ) privatePeerSwitch2.connectStub = rcvConnectStub # wait for hole punching to finish in the background await sleepAsync(600.millis) await allFuturesThrowing( - privatePeerSwitch1.stop(), privatePeerSwitch2.stop(), switchRelay.stop(), - switchAux.stop(), switchAux2.stop(), switchAux3.stop(), switchAux4.stop()) + privatePeerSwitch1.stop(), + privatePeerSwitch2.stop(), + switchRelay.stop(), + switchAux.stop(), + switchAux2.stop(), + switchAux3.stop(), + switchAux4.stop(), + ) asyncTest "Hole punching when peers addresses are private": await holePunchingTest(nil, nil, NotReachable) asyncTest "Hole punching when peers addresses are private and there is an error in the initiator side": - - proc connectStub(self: SwitchStub, - peerId: PeerId, - addrs: seq[MultiAddress], - forceDial = false, - reuseConnection = true, - dir = Direction.Out): Future[void] {.async.} = + proc connectStub( + self: SwitchStub, + peerId: PeerId, + addrs: seq[MultiAddress], + forceDial = false, + reuseConnection = true, + dir = Direction.Out, + ): Future[void] {.async.} = self.connectStub = nil # this stub should be called only once raise newException(CatchableError, "error") diff --git a/tests/testidentify.nim b/tests/testidentify.nim index e516ad3f13..2b5bce5e0c 100644 --- a/tests/testidentify.nim +++ b/tests/testidentify.nim @@ -11,18 +11,21 @@ import options import chronos -import ../libp2p/[protocols/identify, - multiaddress, - peerinfo, - peerid, - stream/connection, - multistream, - transports/transport, - switch, - builders, - transports/tcptransport, - crypto/crypto, - upgrademngrs/upgrade] +import + ../libp2p/[ + protocols/identify, + multiaddress, + peerinfo, + peerid, + stream/connection, + multistream, + transports/transport, + switch, + builders, + transports/tcptransport, + crypto/crypto, + upgrademngrs/upgrade, + ] import ./helpers suite "Identify": @@ -47,10 +50,8 @@ suite "Identify": asyncSetup: ma = @[MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet()] remoteSecKey = PrivateKey.random(ECDSA, rng[]).get() - remotePeerInfo = PeerInfo.new( - remoteSecKey, - ma, - ["/test/proto1/1.0.0", "/test/proto2/1.0.0"]) + remotePeerInfo = + PeerInfo.new(remoteSecKey, ma, ["/test/proto1/1.0.0", "/test/proto2/1.0.0"]) transport1 = TcpTransport.new(upgrade = Upgrade()) transport2 = TcpTransport.new(upgrade = Upgrade()) @@ -161,11 +162,12 @@ suite "Identify": identifyPush2 {.threadvar.}: IdentifyPush conn {.threadvar.}: Connection asyncSetup: - switch1 = newStandardSwitch(sendSignedPeerRecord=true) - switch2 = newStandardSwitch(sendSignedPeerRecord=true) + switch1 = newStandardSwitch(sendSignedPeerRecord = true) + switch2 = newStandardSwitch(sendSignedPeerRecord = true) proc updateStore1(peerId: PeerId, info: IdentifyInfo) {.async.} = switch1.peerStore.updatePeerInfo(info) + proc updateStore2(peerId: PeerId, info: IdentifyInfo) {.async.} = switch2.peerStore.updatePeerInfo(info) @@ -179,9 +181,8 @@ suite "Identify": await switch2.start() conn = await switch2.dial( - switch1.peerInfo.peerId, - switch1.peerInfo.addrs, - IdentifyPushCodec) + switch1.peerInfo.peerId, switch1.peerInfo.addrs, IdentifyPushCodec + ) check: switch1.peerStore[AddressBook][switch2.peerInfo.peerId] == switch2.peerInfo.addrs @@ -190,17 +191,25 @@ suite "Identify": switch1.peerStore[KeyBook][switch2.peerInfo.peerId] == switch2.peerInfo.publicKey switch2.peerStore[KeyBook][switch1.peerInfo.peerId] == switch1.peerInfo.publicKey - switch1.peerStore[AgentBook][switch2.peerInfo.peerId] == switch2.peerInfo.agentVersion - switch2.peerStore[AgentBook][switch1.peerInfo.peerId] == switch1.peerInfo.agentVersion + switch1.peerStore[AgentBook][switch2.peerInfo.peerId] == + switch2.peerInfo.agentVersion + switch2.peerStore[AgentBook][switch1.peerInfo.peerId] == + switch1.peerInfo.agentVersion - switch1.peerStore[ProtoVersionBook][switch2.peerInfo.peerId] == switch2.peerInfo.protoVersion - switch2.peerStore[ProtoVersionBook][switch1.peerInfo.peerId] == switch1.peerInfo.protoVersion + switch1.peerStore[ProtoVersionBook][switch2.peerInfo.peerId] == + switch2.peerInfo.protoVersion + switch2.peerStore[ProtoVersionBook][switch1.peerInfo.peerId] == + switch1.peerInfo.protoVersion - switch1.peerStore[ProtoBook][switch2.peerInfo.peerId] == switch2.peerInfo.protocols - switch2.peerStore[ProtoBook][switch1.peerInfo.peerId] == switch1.peerInfo.protocols + switch1.peerStore[ProtoBook][switch2.peerInfo.peerId] == + switch2.peerInfo.protocols + switch2.peerStore[ProtoBook][switch1.peerInfo.peerId] == + switch1.peerInfo.protocols - switch1.peerStore[SPRBook][switch2.peerInfo.peerId] == switch2.peerInfo.signedPeerRecord.envelope - switch2.peerStore[SPRBook][switch1.peerInfo.peerId] == switch1.peerInfo.signedPeerRecord.envelope + switch1.peerStore[SPRBook][switch2.peerInfo.peerId] == + switch2.peerInfo.signedPeerRecord.envelope + switch2.peerStore[SPRBook][switch1.peerInfo.peerId] == + switch1.peerInfo.signedPeerRecord.envelope proc closeAll() {.async.} = await conn.close() @@ -214,12 +223,16 @@ suite "Identify": check: switch1.peerStore[AddressBook][switch2.peerInfo.peerId] != switch2.peerInfo.addrs - switch1.peerStore[ProtoBook][switch2.peerInfo.peerId] != switch2.peerInfo.protocols + switch1.peerStore[ProtoBook][switch2.peerInfo.peerId] != + switch2.peerInfo.protocols await identifyPush2.push(switch2.peerInfo, conn) - checkUntilTimeout: switch1.peerStore[ProtoBook][switch2.peerInfo.peerId] == switch2.peerInfo.protocols - checkUntilTimeout: switch1.peerStore[AddressBook][switch2.peerInfo.peerId] == switch2.peerInfo.addrs + checkUntilTimeout: + switch1.peerStore[ProtoBook][switch2.peerInfo.peerId] == + switch2.peerInfo.protocols + checkUntilTimeout: + switch1.peerStore[AddressBook][switch2.peerInfo.peerId] == switch2.peerInfo.addrs await closeAll() @@ -229,7 +242,8 @@ suite "Identify": check: switch1.peerStore[AddressBook][switch2.peerInfo.peerId] != switch2.peerInfo.addrs - switch1.peerStore[ProtoBook][switch2.peerInfo.peerId] != switch2.peerInfo.protocols + switch1.peerStore[ProtoBook][switch2.peerInfo.peerId] != + switch2.peerInfo.protocols let oldPeerId = switch2.peerInfo.peerId switch2.peerInfo = PeerInfo.new(PrivateKey.random(newRng()[]).get()) diff --git a/tests/testinterop.nim b/tests/testinterop.nim index b2b0e2e8d1..44a71463b9 100644 --- a/tests/testinterop.nim +++ b/tests/testinterop.nim @@ -4,48 +4,51 @@ import ../libp2p/crypto/crypto, ../libp2p/protocols/connectivity/relay/[relay, c proc switchMplexCreator( ma: MultiAddress = MultiAddress.init("/ip4/127.0.0.1/tcp/0").tryGet(), - prov: TransportProvider = proc(upgr: Upgrade): Transport = TcpTransport.new({}, upgr), - relay: Relay = Relay.new(circuitRelayV1 = true)): - Switch {.raises: [LPError].} = - - SwitchBuilder.new() - .withSignedPeerRecord(false) - .withMaxConnections(MaxConnections) - .withRng(crypto.newRng()) - .withAddresses(@[ ma ]) - .withMaxIn(-1) - .withMaxOut(-1) - .withTransport(prov) - .withMplex() - .withMaxConnsPerPeer(MaxConnectionsPerPeer) - .withPeerStore(capacity=1000) - .withNoise() - .withCircuitRelay(relay) - .withNameResolver(nil) - .build() + prov: TransportProvider = proc(upgr: Upgrade): Transport = + TcpTransport.new({}, upgr) + , + relay: Relay = Relay.new(circuitRelayV1 = true), +): Switch {.raises: [LPError].} = + SwitchBuilder + .new() + .withSignedPeerRecord(false) + .withMaxConnections(MaxConnections) + .withRng(crypto.newRng()) + .withAddresses(@[ma]) + .withMaxIn(-1) + .withMaxOut(-1) + .withTransport(prov) + .withMplex() + .withMaxConnsPerPeer(MaxConnectionsPerPeer) + .withPeerStore(capacity = 1000) + .withNoise() + .withCircuitRelay(relay) + .withNameResolver(nil) + .build() proc switchYamuxCreator( ma: MultiAddress = MultiAddress.init("/ip4/127.0.0.1/tcp/0").tryGet(), - prov: TransportProvider = proc(upgr: Upgrade): Transport = TcpTransport.new({}, upgr), - relay: Relay = Relay.new(circuitRelayV1 = true)): - Switch {.raises: [LPError].} = - - SwitchBuilder.new() - .withSignedPeerRecord(false) - .withMaxConnections(MaxConnections) - .withRng(crypto.newRng()) - .withAddresses(@[ ma ]) - .withMaxIn(-1) - .withMaxOut(-1) - .withTransport(prov) - .withYamux() - .withMaxConnsPerPeer(MaxConnectionsPerPeer) - .withPeerStore(capacity=1000) - .withNoise() - .withCircuitRelay(relay) - .withNameResolver(nil) - .build() - + prov: TransportProvider = proc(upgr: Upgrade): Transport = + TcpTransport.new({}, upgr) + , + relay: Relay = Relay.new(circuitRelayV1 = true), +): Switch {.raises: [LPError].} = + SwitchBuilder + .new() + .withSignedPeerRecord(false) + .withMaxConnections(MaxConnections) + .withRng(crypto.newRng()) + .withAddresses(@[ma]) + .withMaxIn(-1) + .withMaxOut(-1) + .withTransport(prov) + .withYamux() + .withMaxConnsPerPeer(MaxConnectionsPerPeer) + .withPeerStore(capacity = 1000) + .withNoise() + .withCircuitRelay(relay) + .withNameResolver(nil) + .build() suite "Tests interop": commonInteropTests("mplex", switchMplexCreator) diff --git a/tests/testminasn1.nim b/tests/testminasn1.nim index 393e7d9016..337da180bd 100644 --- a/tests/testminasn1.nim +++ b/tests/testminasn1.nim @@ -14,72 +14,62 @@ import ../libp2p/crypto/minasn1 import nimcrypto/utils as ncrutils const Asn1EdgeValues = [ - 0'u64, (1'u64 shl 7) - 1'u64, - (1'u64 shl 7), (1'u64 shl 8) - 1'u64, - (1'u64 shl 8), (1'u64 shl 16) - 1'u64, - (1'u64 shl 16), (1'u64 shl 24) - 1'u64, - (1'u64 shl 24), (1'u64 shl 32) - 1'u64, - (1'u64 shl 32), (1'u64 shl 40) - 1'u64, - (1'u64 shl 40), (1'u64 shl 48) - 1'u64, - (1'u64 shl 48), (1'u64 shl 56) - 1'u64, - (1'u64 shl 56), 0xFFFF_FFFF_FFFF_FFFF'u64 + 0'u64, + (1'u64 shl 7) - 1'u64, + (1'u64 shl 7), + (1'u64 shl 8) - 1'u64, + (1'u64 shl 8), + (1'u64 shl 16) - 1'u64, + (1'u64 shl 16), + (1'u64 shl 24) - 1'u64, + (1'u64 shl 24), + (1'u64 shl 32) - 1'u64, + (1'u64 shl 32), + (1'u64 shl 40) - 1'u64, + (1'u64 shl 40), + (1'u64 shl 48) - 1'u64, + (1'u64 shl 48), + (1'u64 shl 56) - 1'u64, + (1'u64 shl 56), + 0xFFFF_FFFF_FFFF_FFFF'u64, ] const Asn1EdgeExpects = [ - "00", "7F", - "8180", "81FF", - "820100", "82FFFF", - "83010000", "83FFFFFF", - "8401000000", "84FFFFFFFF", - "850100000000", "85FFFFFFFFFF", - "86010000000000", "86FFFFFFFFFFFF", - "8701000000000000", "87FFFFFFFFFFFFFF", - "880100000000000000", "88FFFFFFFFFFFFFFFF", + "00", "7F", "8180", "81FF", "820100", "82FFFF", "83010000", "83FFFFFF", "8401000000", + "84FFFFFFFF", "850100000000", "85FFFFFFFFFF", "86010000000000", "86FFFFFFFFFFFF", + "8701000000000000", "87FFFFFFFFFFFFFF", "880100000000000000", "88FFFFFFFFFFFFFFFF", ] -const Asn1UIntegerValues8 = [ - 0x00'u8, 0x7F'u8, 0x80'u8, 0xFF'u8, -] +const Asn1UIntegerValues8 = [0x00'u8, 0x7F'u8, 0x80'u8, 0xFF'u8] -const Asn1UIntegerExpects8 = [ - "020100", "02017F", "02020080", "020200FF" -] +const Asn1UIntegerExpects8 = ["020100", "02017F", "02020080", "020200FF"] -const Asn1UIntegerValues16 = [ - 0x00'u16, 0x7F'u16, 0x80'u16, 0xFF'u16, - 0x7FFF'u16, 0x8000'u16, 0xFFFF'u16 -] +const Asn1UIntegerValues16 = + [0x00'u16, 0x7F'u16, 0x80'u16, 0xFF'u16, 0x7FFF'u16, 0x8000'u16, 0xFFFF'u16] -const Asn1UIntegerExpects16 = [ - "020100", "02017F", "02020080", "020200FF", "02027FFF", - "0203008000", "020300FFFF" -] +const Asn1UIntegerExpects16 = + ["020100", "02017F", "02020080", "020200FF", "02027FFF", "0203008000", "020300FFFF"] const Asn1UIntegerValues32 = [ - 0x00'u32, 0x7F'u32, 0x80'u32, 0xFF'u32, - 0x7FFF'u32, 0x8000'u32, 0xFFFF'u32, - 0x7FFF_FFFF'u32, 0x8000_0000'u32, 0xFFFF_FFFF'u32 + 0x00'u32, 0x7F'u32, 0x80'u32, 0xFF'u32, 0x7FFF'u32, 0x8000'u32, 0xFFFF'u32, + 0x7FFF_FFFF'u32, 0x8000_0000'u32, 0xFFFF_FFFF'u32, ] const Asn1UIntegerExpects32 = [ - "020100", "02017F", "02020080", "020200FF", "02027FFF", - "0203008000", "020300FFFF", "02047FFFFFFF", "02050080000000", - "020500FFFFFFFF" + "020100", "02017F", "02020080", "020200FF", "02027FFF", "0203008000", "020300FFFF", + "02047FFFFFFF", "02050080000000", "020500FFFFFFFF", ] const Asn1UIntegerValues64 = [ - 0x00'u64, 0x7F'u64, 0x80'u64, 0xFF'u64, - 0x7FFF'u64, 0x8000'u64, 0xFFFF'u64, - 0x7FFF_FFFF'u64, 0x8000_0000'u64, 0xFFFF_FFFF'u64, - 0x7FFF_FFFF_FFFF_FFFF'u64, 0x8000_0000_0000_0000'u64, - 0xFFFF_FFFF_FFFF_FFFF'u64 + 0x00'u64, 0x7F'u64, 0x80'u64, 0xFF'u64, 0x7FFF'u64, 0x8000'u64, 0xFFFF'u64, + 0x7FFF_FFFF'u64, 0x8000_0000'u64, 0xFFFF_FFFF'u64, 0x7FFF_FFFF_FFFF_FFFF'u64, + 0x8000_0000_0000_0000'u64, 0xFFFF_FFFF_FFFF_FFFF'u64, ] const Asn1UIntegerExpects64 = [ - "020100", "02017F", "02020080", "020200FF", "02027FFF", - "0203008000", "020300FFFF", "02047FFFFFFF", "02050080000000", - "020500FFFFFFFF", "02087FFFFFFFFFFFFFFF", "0209008000000000000000", - "020900FFFFFFFFFFFFFFFF" + "020100", "02017F", "02020080", "020200FF", "02027FFF", "0203008000", "020300FFFF", + "02047FFFFFFF", "02050080000000", "020500FFFFFFFF", "02087FFFFFFFFFFFFFFF", + "0209008000000000000000", "020900FFFFFFFFFFFFFFFF", ] suite "Minimal ASN.1 encode/decode suite": diff --git a/tests/testminprotobuf.nim b/tests/testminprotobuf.nim index 085ad758ad..317cadaafb 100644 --- a/tests/testminprotobuf.nim +++ b/tests/testminprotobuf.nim @@ -16,41 +16,35 @@ import stew/byteutils, strutils suite "MinProtobuf test suite": const VarintVectors = [ "0800", "0801", "08ffffffff07", "08ffffffff0f", "08ffffffffffffffff7f", - "08ffffffffffffffffff01" + "08ffffffffffffffffff01", ] const VarintValues = [ - 0x0'u64, 0x1'u64, 0x7FFF_FFFF'u64, 0xFFFF_FFFF'u64, - 0x7FFF_FFFF_FFFF_FFFF'u64, 0xFFFF_FFFF_FFFF_FFFF'u64 + 0x0'u64, 0x1'u64, 0x7FFF_FFFF'u64, 0xFFFF_FFFF'u64, 0x7FFF_FFFF_FFFF_FFFF'u64, + 0xFFFF_FFFF_FFFF_FFFF'u64, ] - const Fixed32Vectors = [ - "0d00000000", "0d01000000", "0dffffff7f", "0dddccbbaa", "0dffffffff" - ] + const Fixed32Vectors = + ["0d00000000", "0d01000000", "0dffffff7f", "0dddccbbaa", "0dffffffff"] - const Fixed32Values = [ - 0x0'u32, 0x1'u32, 0x7FFF_FFFF'u32, 0xAABB_CCDD'u32, 0xFFFF_FFFF'u32 - ] + const Fixed32Values = + [0x0'u32, 0x1'u32, 0x7FFF_FFFF'u32, 0xAABB_CCDD'u32, 0xFFFF_FFFF'u32] const Fixed64Vectors = [ "090000000000000000", "090100000000000000", "09ffffff7f00000000", "09ddccbbaa00000000", "09ffffffff00000000", "09ffffffffffffff7f", - "099988ffeeddccbbaa", "09ffffffffffffffff" + "099988ffeeddccbbaa", "09ffffffffffffffff", ] const Fixed64Values = [ 0x0'u64, 0x1'u64, 0x7FFF_FFFF'u64, 0xAABB_CCDD'u64, 0xFFFF_FFFF'u64, - 0x7FFF_FFFF_FFFF_FFFF'u64, 0xAABB_CCDD_EEFF_8899'u64, - 0xFFFF_FFFF_FFFF_FFFF'u64 + 0x7FFF_FFFF_FFFF_FFFF'u64, 0xAABB_CCDD_EEFF_8899'u64, 0xFFFF_FFFF_FFFF_FFFF'u64, ] - const LengthVectors = [ - "0a00", "0a0161", "0a026162", "0a0461626364", "0a086162636465666768" - ] + const LengthVectors = + ["0a00", "0a0161", "0a026162", "0a0461626364", "0a086162636465666768"] - const LengthValues = [ - "", "a", "ab", "abcd", "abcdefgh" - ] + const LengthValues = ["", "a", "ab", "abcd", "abcdefgh"] ## This vector values was tested with `protoc` and related proto file. @@ -138,7 +132,7 @@ suite "MinProtobuf test suite": value.setLen(valueLen) value - proc isFullZero[T: byte|char](data: openArray[T]): bool = + proc isFullZero[T: byte | char](data: openArray[T]): bool = for ch in data: if int(ch) != 0: return false diff --git a/tests/testmplex.nim b/tests/testmplex.nim index c024d1f3b5..329ae1e82f 100644 --- a/tests/testmplex.nim +++ b/tests/testmplex.nim @@ -11,18 +11,21 @@ import strformat, random, sequtils import chronos, nimcrypto/utils, chronicles, stew/byteutils -import ../libp2p/[errors, - stream/connection, - stream/bufferstream, - transports/tcptransport, - transports/transport, - multiaddress, - muxers/mplex/mplex, - muxers/mplex/coder, - muxers/mplex/lpchannel, - upgrademngrs/upgrade, - vbuffer, - varint] +import + ../libp2p/[ + errors, + stream/connection, + stream/bufferstream, + transports/tcptransport, + transports/transport, + multiaddress, + muxers/mplex/mplex, + muxers/mplex/coder, + muxers/mplex/lpchannel, + upgrademngrs/upgrade, + vbuffer, + varint, + ] import ./helpers @@ -33,7 +36,8 @@ suite "Mplex": suite "channel encoding": asyncTest "encode header with channel id 0": proc encHandler( - msg: seq[byte]) {.async: (raises: [CancelledError, LPStreamError]).} = + msg: seq[byte] + ) {.async: (raises: [CancelledError, LPStreamError]).} = check msg == fromHex("000873747265616d2031") let conn = TestBufferStream.new(encHandler) @@ -42,7 +46,8 @@ suite "Mplex": asyncTest "encode header with channel id other than 0": proc encHandler( - msg: seq[byte]) {.async: (raises: [CancelledError, LPStreamError]).} = + msg: seq[byte] + ) {.async: (raises: [CancelledError, LPStreamError]).} = check msg == fromHex("88010873747265616d2031") let conn = TestBufferStream.new(encHandler) @@ -51,7 +56,8 @@ suite "Mplex": asyncTest "encode header and body with channel id 0": proc encHandler( - msg: seq[byte]) {.async: (raises: [CancelledError, LPStreamError]).} = + msg: seq[byte] + ) {.async: (raises: [CancelledError, LPStreamError]).} = check msg == fromHex("020873747265616d2031") let conn = TestBufferStream.new(encHandler) @@ -60,7 +66,8 @@ suite "Mplex": asyncTest "encode header and body with channel id other than 0": proc encHandler( - msg: seq[byte]) {.async: (raises: [CancelledError, LPStreamError]).} = + msg: seq[byte] + ) {.async: (raises: [CancelledError, LPStreamError]).} = check msg == fromHex("8a010873747265616d2031") let conn = TestBufferStream.new(encHandler) @@ -91,7 +98,9 @@ suite "Mplex": asyncTest "decode header and body with channel id other than 0": let stream = BufferStream.new() let conn = stream - await stream.pushData(fromHex("8a011668656C6C6F2066726F6D206368616E6E656C20302121")) + await stream.pushData( + fromHex("8a011668656C6C6F2066726F6D206368616E6E656C20302121") + ) let msg = await conn.readMsg() check msg.id == 17 @@ -105,6 +114,7 @@ suite "Mplex": data: seq[byte] ) {.async: (raises: [CancelledError, LPStreamError]).} = discard + let conn = TestBufferStream.new(writeHandler) chann = LPChannel.init(1, conn, true) @@ -119,10 +129,8 @@ suite "Mplex": asyncTest "(local close) - should allow reads until remote closes": let conn = TestBufferStream.new( - proc ( - data: seq[byte] - ) {.async: (raises: [CancelledError, LPStreamError]).} = - discard, + proc(data: seq[byte]) {.async: (raises: [CancelledError, LPStreamError]).} = + discard ) chann = LPChannel.init(1, conn, true) @@ -148,10 +156,8 @@ suite "Mplex": asyncTest "(remote close) - channel should close for reading by remote": let conn = TestBufferStream.new( - proc ( - data: seq[byte] - ) {.async: (raises: [CancelledError, LPStreamError]).} = - discard, + proc(data: seq[byte]) {.async: (raises: [CancelledError, LPStreamError]).} = + discard ) chann = LPChannel.init(1, conn, true) @@ -173,9 +179,7 @@ suite "Mplex": let testData = "Hello!".toBytes conn = TestBufferStream.new( - proc ( - data: seq[byte] - ) {.async: (raises: [CancelledError, LPStreamError]).} = + proc(data: seq[byte]) {.async: (raises: [CancelledError, LPStreamError]).} = discard ) chann = LPChannel.init(1, conn, true) @@ -192,12 +196,15 @@ suite "Mplex": data: seq[byte] ) {.async: (raises: [CancelledError, LPStreamError]).} = discard + let conn = TestBufferStream.new(writeHandler) chann = LPChannel.init(1, conn, true) await chann.pushEof() var buf: array[1, byte] - check: (await chann.readOnce(addr buf[0], 1)) == 0 # EOF marker read + check: + (await chann.readOnce(addr buf[0], 1)) == 0 + # EOF marker read expect LPStreamClosedError: await chann.pushData(@[byte(1)]) @@ -206,12 +213,12 @@ suite "Mplex": await conn.close() suite "channel reset": - asyncTest "channel should fail reading": proc writeHandler( data: seq[byte] ) {.async: (raises: [CancelledError, LPStreamError]).} = discard + let conn = TestBufferStream.new(writeHandler) chann = LPChannel.init(1, conn, true) @@ -228,6 +235,7 @@ suite "Mplex": data: seq[byte] ) {.async: (raises: [CancelledError, LPStreamError]).} = discard + let conn = TestBufferStream.new(writeHandler) chann = LPChannel.init(1, conn, true) @@ -246,6 +254,7 @@ suite "Mplex": data: seq[byte] ) {.async: (raises: [CancelledError, LPStreamError]).} = discard + let conn = TestBufferStream.new(writeHandler) chann = LPChannel.init(1, conn, true) @@ -268,15 +277,13 @@ suite "Mplex": data: seq[byte] ) {.async: (raises: [CancelledError, LPStreamError]).} = discard + let conn = TestBufferStream.new(writeHandler) chann = LPChannel.init(1, conn, true) var data = newSeq[byte](1) - let futs = [ - chann.readExactly(addr data[0], 1), - chann.pushData(@[0'u8]), - ] + let futs = [chann.readExactly(addr data[0], 1), chann.pushData(@[0'u8])] await chann.reset() check await allFutures(futs).withTimeout(100.millis) await conn.close() @@ -286,6 +293,7 @@ suite "Mplex": data: seq[byte] ) {.async: (raises: [CancelledError, LPStreamError]).} = discard + let conn = TestBufferStream.new(writeHandler) chann = LPChannel.init(1, conn, true) @@ -314,6 +322,7 @@ suite "Mplex": data: seq[byte] ) {.async: (raises: [CancelledError, LPStreamError]).} = discard + let conn = TestBufferStream.new(writeHandler) chann = LPChannel.init(1, conn, true) @@ -331,6 +340,7 @@ suite "Mplex": data: seq[byte] ) {.async: (raises: [CancelledError, LPStreamError]).} = discard + let conn = TestBufferStream.new(writeHandler) chann = LPChannel.init(1, conn, true) @@ -352,6 +362,7 @@ suite "Mplex": data: seq[byte] ) {.async: (raises: [CancelledError, LPStreamError]).} = discard + let conn = TestBufferStream.new(writeHandler) chann = LPChannel.init(1, conn, true) @@ -367,6 +378,7 @@ suite "Mplex": data: seq[byte] ) {.async: (raises: [CancelledError, LPStreamError]).} = discard + let conn = TestBufferStream.new(writeHandler) chann = LPChannel.init(1, conn, true) @@ -382,6 +394,7 @@ suite "Mplex": data: seq[byte] ) {.async: (raises: [CancelledError, LPStreamError]).} = discard + let conn = TestBufferStream.new(writeHandler) chann = LPChannel.init(1, conn, true) @@ -400,12 +413,11 @@ suite "Mplex": let rw = @[writer(), reader()] await chann.close() - check await chann.reset() # this would hang + check await chann.reset() + # this would hang .withTimeout(100.millis) - check await allFuturesThrowing( - allFinished(rw)) - .withTimeout(100.millis) + check await allFuturesThrowing(allFinished(rw)).withTimeout(100.millis) await conn.close() @@ -414,6 +426,7 @@ suite "Mplex": data: seq[byte] ) {.async: (raises: [CancelledError, LPStreamError]).} = discard + let conn = TestBufferStream.new(writeHandler) chann = LPChannel.init(1, conn, true) @@ -429,10 +442,10 @@ suite "Mplex": data: seq[byte] ) {.async: (raises: [CancelledError, LPStreamError]).} = discard + let conn = TestBufferStream.new(writeHandler) - chann = LPChannel.init( - 1, conn, true, timeout = 100.millis) + chann = LPChannel.init(1, conn, true, timeout = 100.millis) check await chann.join().withTimeout(1.minutes) await conn.close() @@ -447,15 +460,14 @@ suite "Mplex": proc acceptHandler() {.async.} = let conn = await transport1.accept() let mplexListen = Mplex.new(conn) - mplexListen.streamHandler = - proc(stream: Connection) {.async: (raises: []).} = - try: - let msg = await stream.readLp(1024) - check string.fromBytes(msg) == "HELLO" - except CancelledError, LPStreamError: - return - finally: - await stream.close() + mplexListen.streamHandler = proc(stream: Connection) {.async: (raises: []).} = + try: + let msg = await stream.readLp(1024) + check string.fromBytes(msg) == "HELLO" + except CancelledError, LPStreamError: + return + finally: + await stream.close() await mplexListen.handle() await mplexListen.close() @@ -474,9 +486,7 @@ suite "Mplex": await conn.close() await acceptFut.wait(1.seconds) await mplexDialFut.wait(1.seconds) - await allFuturesThrowing( - transport1.stop(), - transport2.stop()) + await allFuturesThrowing(transport1.stop(), transport2.stop()) await listenFut asyncTest "read/write receiver lazy": @@ -488,15 +498,14 @@ suite "Mplex": proc acceptHandler() {.async.} = let conn = await transport1.accept() let mplexListen = Mplex.new(conn) - mplexListen.streamHandler = - proc(stream: Connection) {.async: (raises: []).} = - try: - let msg = await stream.readLp(1024) - check string.fromBytes(msg) == "HELLO" - except CancelledError, LPStreamError: - return - finally: - await stream.close() + mplexListen.streamHandler = proc(stream: Connection) {.async: (raises: []).} = + try: + let msg = await stream.readLp(1024) + check string.fromBytes(msg) == "HELLO" + except CancelledError, LPStreamError: + return + finally: + await stream.close() await mplexListen.handle() await mplexListen.close() @@ -516,9 +525,7 @@ suite "Mplex": await conn.close() await acceptFut.wait(1.seconds) await mplexDialFut - await allFuturesThrowing( - transport1.stop(), - transport2.stop()) + await allFuturesThrowing(transport1.stop(), transport2.stop()) await listenFut asyncTest "write fragmented": @@ -527,8 +534,8 @@ suite "Mplex": listenJob = newFuture[void]() var bigseq = newSeqOfCap[uint8](MaxMsgSize * 2) - for _ in 0.. buf.buffer.len: buf.buffer.len else: size - var send = buf.buffer[0.. 2: break #3 nameserver max on linux + if resultSeq.len > 2: + break + #3 nameserver max on linux except CatchableError as err: echo "Failed to get unix nameservers ", err.msg finally: @@ -59,7 +65,6 @@ proc guessOsNameServers(): seq[TransportAddress] = else: return fallbackDnsServers - suite "Name resolving": suite "Generic Resolving": var resolver {.threadvar.}: MockResolver @@ -88,7 +93,10 @@ suite "Name resolving": check testOne("/dns/localhost/udp/0", @["/ip4/127.0.0.1/udp/0", "/ip6/::1/udp/0"]) check testOne("/dns4/localhost/tcp/0", "/ip4/127.0.0.1/tcp/0") check testOne("/dns6/localhost/tcp/0", "/ip6/::1/tcp/0") - check testOne("/dns6/localhost/tcp/4001/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN", "/ip6/::1/tcp/4001/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN") + check testOne( + "/dns6/localhost/tcp/4001/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN", + "/ip6/::1/tcp/4001/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN", + ) asyncTest "test non dns resolve": resolver.ipResponses[("localhost", false)] = @["127.0.0.1"] @@ -97,61 +105,85 @@ suite "Name resolving": check testOne("/ip6/::1/tcp/0", "/ip6/::1/tcp/0") asyncTest "dnsaddr recursive test": - resolver.txtResponses["_dnsaddr.bootstrap.libp2p.io"] = @[ - "dnsaddr=/dnsaddr/sjc-1.bootstrap.libp2p.io/tcp/4001/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN", - "dnsaddr=/dnsaddr/ams-2.bootstrap.libp2p.io/tcp/4001/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb" - ] - - resolver.txtResponses["_dnsaddr.sjc-1.bootstrap.libp2p.io"] = @[ - "dnsaddr=/ip6/2604:1380:1000:6000::1/tcp/4001/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN", - "dnsaddr=/ip4/147.75.69.143/tcp/4001/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN" - ] - - resolver.txtResponses["_dnsaddr.ams-2.bootstrap.libp2p.io"] = @[ - "dnsaddr=/ip4/147.75.83.83/tcp/4001/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb", - "dnsaddr=/ip6/2604:1380:2000:7a00::1/tcp/4001/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb" - ] - - check testOne("/dnsaddr/bootstrap.libp2p.io/", @[ - "/ip6/2604:1380:1000:6000::1/tcp/4001/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN", - "/ip4/147.75.69.143/tcp/4001/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN", - "/ip4/147.75.83.83/tcp/4001/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb", - "/ip6/2604:1380:2000:7a00::1/tcp/4001/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb", - ]) + resolver.txtResponses["_dnsaddr.bootstrap.libp2p.io"] = + @[ + "dnsaddr=/dnsaddr/sjc-1.bootstrap.libp2p.io/tcp/4001/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN", + "dnsaddr=/dnsaddr/ams-2.bootstrap.libp2p.io/tcp/4001/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb", + ] + + resolver.txtResponses["_dnsaddr.sjc-1.bootstrap.libp2p.io"] = + @[ + "dnsaddr=/ip6/2604:1380:1000:6000::1/tcp/4001/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN", + "dnsaddr=/ip4/147.75.69.143/tcp/4001/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN", + ] + + resolver.txtResponses["_dnsaddr.ams-2.bootstrap.libp2p.io"] = + @[ + "dnsaddr=/ip4/147.75.83.83/tcp/4001/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb", + "dnsaddr=/ip6/2604:1380:2000:7a00::1/tcp/4001/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb", + ] + + check testOne( + "/dnsaddr/bootstrap.libp2p.io/", + @[ + "/ip6/2604:1380:1000:6000::1/tcp/4001/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN", + "/ip4/147.75.69.143/tcp/4001/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN", + "/ip4/147.75.83.83/tcp/4001/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb", + "/ip6/2604:1380:2000:7a00::1/tcp/4001/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb", + ], + ) asyncTest "dnsaddr suffix matching test": - resolver.txtResponses["_dnsaddr.bootstrap.libp2p.io"] = @[ - "dnsaddr=/dnsaddr/ams-2.bootstrap.libp2p.io/tcp/4001/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb", - "dnsaddr=/dnsaddr/sjc-1.bootstrap.libp2p.io/tcp/4001/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN", - "dnsaddr=/dnsaddr/nrt-1.bootstrap.libp2p.io/tcp/4001/p2p/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt", - "dnsaddr=/dnsaddr/ewr-1.bootstrap.libp2p.io/tcp/4001/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa", - ] - - resolver.txtResponses["_dnsaddr.sjc-1.bootstrap.libp2p.io"] = @[ - "dnsaddr=/ip4/147.75.69.143/tcp/4001/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN", - "dnsaddr=/ip6/2604:1380:1000:6000::1/tcp/4001/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN", - ] - - resolver.txtResponses["_dnsaddr.ams-1.bootstrap.libp2p.io"] = @[ - "dnsaddr=/ip4/147.75.69.143/tcp/4001/p2p/shouldbefiltered", - "dnsaddr=/ip6/2604:1380:1000:6000::1/tcp/4001/p2p/shouldbefiltered", - ] - - check testOne("/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN", @[ - "/ip4/147.75.69.143/tcp/4001/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN", - "/ip6/2604:1380:1000:6000::1/tcp/4001/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN", - ]) + resolver.txtResponses["_dnsaddr.bootstrap.libp2p.io"] = + @[ + "dnsaddr=/dnsaddr/ams-2.bootstrap.libp2p.io/tcp/4001/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb", + "dnsaddr=/dnsaddr/sjc-1.bootstrap.libp2p.io/tcp/4001/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN", + "dnsaddr=/dnsaddr/nrt-1.bootstrap.libp2p.io/tcp/4001/p2p/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt", + "dnsaddr=/dnsaddr/ewr-1.bootstrap.libp2p.io/tcp/4001/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa", + ] + + resolver.txtResponses["_dnsaddr.sjc-1.bootstrap.libp2p.io"] = + @[ + "dnsaddr=/ip4/147.75.69.143/tcp/4001/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN", + "dnsaddr=/ip6/2604:1380:1000:6000::1/tcp/4001/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN", + ] + + resolver.txtResponses["_dnsaddr.ams-1.bootstrap.libp2p.io"] = + @[ + "dnsaddr=/ip4/147.75.69.143/tcp/4001/p2p/shouldbefiltered", + "dnsaddr=/ip6/2604:1380:1000:6000::1/tcp/4001/p2p/shouldbefiltered", + ] + + check testOne( + "/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN", + @[ + "/ip4/147.75.69.143/tcp/4001/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN", + "/ip6/2604:1380:1000:6000::1/tcp/4001/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN", + ], + ) asyncTest "dnsaddr infinite recursion": - resolver.txtResponses["_dnsaddr.bootstrap.libp2p.io"] = @["dnsaddr=/dnsaddr/bootstrap.libp2p.io"] + resolver.txtResponses["_dnsaddr.bootstrap.libp2p.io"] = + @["dnsaddr=/dnsaddr/bootstrap.libp2p.io"] check testOne("/dnsaddr/bootstrap.libp2p.io/", newSeq[string]()) test "getHostname": check: - MultiAddress.init("/dnsaddr/bootstrap.libp2p.io/").tryGet().getHostname == "bootstrap.libp2p.io" - MultiAddress.init("/ip4/147.75.69.143/tcp/4001/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN").tryGet().getHostname == "147.75.69.143" - MultiAddress.init("/ip6/2604:1380:1000:6000::1/tcp/4001/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN").tryGet().getHostname == "2604:1380:1000:6000::1" + MultiAddress.init("/dnsaddr/bootstrap.libp2p.io/").tryGet().getHostname == + "bootstrap.libp2p.io" + + MultiAddress + .init( + "/ip4/147.75.69.143/tcp/4001/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN" + ) + .tryGet().getHostname == "147.75.69.143" + + MultiAddress + .init( + "/ip6/2604:1380:1000:6000::1/tcp/4001/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN" + ) + .tryGet().getHostname == "2604:1380:1000:6000::1" MultiAddress.init("/dns/localhost/udp/0").tryGet().getHostname == "localhost" MultiAddress.init("/dns4/hello.com/udp/0").tryGet().getHostname == "hello.com" MultiAddress.init("/dns6/hello.com/udp/0").tryGet().getHostname == "hello.com" @@ -163,18 +195,19 @@ suite "Name resolving": asyncTest "test manual dns ip resolve": ## DNS mock server - proc clientMark1(transp: DatagramTransport, - raddr: TransportAddress): Future[void] {.async.} = + proc clientMark1( + transp: DatagramTransport, raddr: TransportAddress + ): Future[void] {.async.} = var msg = transp.getMessage() - let - resp = if msg[24] == 1: #AAAA or A - "\xae\xbf\x81\x80\x00\x01\x00\x03\x00\x00\x00\x00\x06\x73\x74\x61" & + let resp = + if msg[24] == 1: #AAAA or A + "\xae\xbf\x81\x80\x00\x01\x00\x03\x00\x00\x00\x00\x06\x73\x74\x61" & "\x74\x75\x73\x02\x69\x6d\x00\x00\x01\x00\x01\xc0\x0c\x00\x01\x00" & "\x01\x00\x00\x00\x4f\x00\x04\x68\x16\x18\xb5\xc0\x0c\x00\x01\x00" & "\x01\x00\x00\x00\x4f\x00\x04\xac\x43\x0a\xa1\xc0\x0c\x00\x01\x00" & "\x01\x00\x00\x00\x4f\x00\x04\x68\x16\x19\xb5" - else: - "\xe8\xc5\x81\x80\x00\x01\x00\x03\x00\x00\x00\x00\x06\x73\x74\x61" & + else: + "\xe8\xc5\x81\x80\x00\x01\x00\x03\x00\x00\x00\x00\x06\x73\x74\x61" & "\x74\x75\x73\x02\x69\x6d\x00\x00\x1c\x00\x01\xc0\x0c\x00\x1c\x00" & "\x01\x00\x00\x00\x4f\x00\x10\x26\x06\x47\x00\x00\x10\x00\x00\x00" & "\x00\x00\x00\x68\x16\x19\xb5\xc0\x0c\x00\x1c\x00\x01\x00\x00\x00" & @@ -190,33 +223,47 @@ suite "Name resolving": check await(dnsresolver.resolveIp("status.im", 0.Port, Domain.AF_UNSPEC)) == mapIt( - @["104.22.24.181:0", "172.67.10.161:0", "104.22.25.181:0", - "[2606:4700:10::6816:19b5]:0", "[2606:4700:10::6816:18b5]:0", "[2606:4700:10::ac43:aa1]:0" - ], initTAddress(it)) + @[ + "104.22.24.181:0", "172.67.10.161:0", "104.22.25.181:0", + "[2606:4700:10::6816:19b5]:0", "[2606:4700:10::6816:18b5]:0", + "[2606:4700:10::ac43:aa1]:0", + ], + initTAddress(it), + ) check await(dnsresolver.resolveIp("status.im", 0.Port, Domain.AF_INET)) == - mapIt(@["104.22.24.181:0", "172.67.10.161:0", "104.22.25.181:0"], initTAddress(it)) + mapIt( + @["104.22.24.181:0", "172.67.10.161:0", "104.22.25.181:0"], initTAddress(it) + ) check await(dnsresolver.resolveIp("status.im", 0.Port, Domain.AF_INET6)) == - mapIt(@["[2606:4700:10::6816:19b5]:0", "[2606:4700:10::6816:18b5]:0", "[2606:4700:10::ac43:aa1]:0"], initTAddress(it)) + mapIt( + @[ + "[2606:4700:10::6816:19b5]:0", "[2606:4700:10::6816:18b5]:0", + "[2606:4700:10::ac43:aa1]:0", + ], + initTAddress(it), + ) await server.closeWait() asyncTest "test unresponsive dns server": var unresponsiveTentatives = 0 ## DNS mock server - proc clientMark1(transp: DatagramTransport, - raddr: TransportAddress): Future[void] {.async.} = + proc clientMark1( + transp: DatagramTransport, raddr: TransportAddress + ): Future[void] {.async.} = unresponsiveTentatives.inc() check unresponsiveTentatives == 1 - proc clientMark2(transp: DatagramTransport, - raddr: TransportAddress): Future[void] {.async.} = + proc clientMark2( + transp: DatagramTransport, raddr: TransportAddress + ): Future[void] {.async.} = var msg = transp.getMessage() let resp = - "\xae\xbf\x81\x80\x00\x01\x00\x03\x00\x00\x00\x00\x06\x73\x74\x61" & - "\x74\x75\x73\x02\x69\x6d\x00\x00\x01\x00\x01\xc0\x0c\x00\x01\x00" & - "\x01\x00\x00\x00\x4f\x00\x04\x68\x16\x18\xb5\xc0\x0c\x00\x01\x00" & - "\x01\x00\x00\x00\x4f\x00\x04\xac\x43\x0a\xa1\xc0\x0c\x00\x01\x00" & - "\x01\x00\x00\x00\x4f\x00\x04\x68\x16\x19\xb5" + "\xae\xbf\x81\x80\x00\x01\x00\x03\x00\x00\x00\x00\x06\x73\x74\x61" & + "\x74\x75\x73\x02\x69\x6d\x00\x00\x01\x00\x01\xc0\x0c\x00\x01\x00" & + "\x01\x00\x00\x00\x4f\x00\x04\x68\x16\x18\xb5\xc0\x0c\x00\x01\x00" & + "\x01\x00\x00\x00\x4f\x00\x04\xac\x43\x0a\xa1\xc0\x0c\x00\x01\x00" & + "\x01\x00\x00\x00\x4f\x00\x04\x68\x16\x19\xb5" await transp.sendTo(raddr, resp) let @@ -224,13 +271,18 @@ suite "Name resolving": server = newDatagramTransport(clientMark2) # The test - var dnsresolver = DnsResolver.new(@[unresponsiveServer.localAddress, server.localAddress]) + var dnsresolver = + DnsResolver.new(@[unresponsiveServer.localAddress, server.localAddress]) check await(dnsresolver.resolveIp("status.im", 0.Port, Domain.AF_INET)) == - mapIt(@["104.22.24.181:0", "172.67.10.161:0", "104.22.25.181:0"], initTAddress(it)) + mapIt( + @["104.22.24.181:0", "172.67.10.161:0", "104.22.25.181:0"], initTAddress(it) + ) check await(dnsresolver.resolveIp("status.im", 0.Port, Domain.AF_INET)) == - mapIt(@["104.22.24.181:0", "172.67.10.161:0", "104.22.25.181:0"], initTAddress(it)) + mapIt( + @["104.22.24.181:0", "172.67.10.161:0", "104.22.25.181:0"], initTAddress(it) + ) await server.closeWait() await unresponsiveServer.closeWait() diff --git a/tests/testnative.nim b/tests/testnative.nim index f6e933c4e0..f88f2ae0a6 100644 --- a/tests/testnative.nim +++ b/tests/testnative.nim @@ -9,53 +9,19 @@ # This file may not be copied, modified, or distributed except according to # those terms. -import testvarint, - testconnection, - testminprotobuf, - teststreamseq, - testsemaphore, - testheartbeat, - testfuture +import + testvarint, testconnection, testminprotobuf, teststreamseq, testsemaphore, + testheartbeat, testfuture -import testminasn1, - testrsa, - testecnist, - tested25519, - testsecp256k1, - testcrypto +import testminasn1, testrsa, testecnist, tested25519, testsecp256k1, testcrypto -import testmultibase, - testmultihash, - testmultiaddress, - testcid, - testpeerid, - testsigned_envelope, - testrouting_record +import + testmultibase, testmultihash, testmultiaddress, testcid, testpeerid, + testsigned_envelope, testrouting_record -import testtcptransport, - testtortransport, - testnameresolve, - testwstransport, - testmultistream, - testbufferstream, - testidentify, - testobservedaddrmanager, - testconnmngr, - testswitch, - testnoise, - testpeerinfo, - testpeerstore, - testping, - testmplex, - testrelayv1, - testrelayv2, - testrendezvous, - testdiscovery, - testyamux, - testautonat, - testautonatservice, - testautorelay, - testdcutr, - testhpservice, - testutility, - testhelpers +import + testtcptransport, testtortransport, testnameresolve, testwstransport, testmultistream, + testbufferstream, testidentify, testobservedaddrmanager, testconnmngr, testswitch, + testnoise, testpeerinfo, testpeerstore, testping, testmplex, testrelayv1, testrelayv2, + testrendezvous, testdiscovery, testyamux, testautonat, testautonatservice, + testautorelay, testdcutr, testhpservice, testutility, testhelpers diff --git a/tests/testnoise.nim b/tests/testnoise.nim index 57fe2000cc..2d6dff85f9 100644 --- a/tests/testnoise.nim +++ b/tests/testnoise.nim @@ -11,32 +11,33 @@ import chronos, stew/byteutils import chronicles -import ../libp2p/[switch, - errors, - multistream, - stream/bufferstream, - protocols/identify, - stream/connection, - transports/transport, - transports/tcptransport, - multiaddress, - peerinfo, - crypto/crypto, - protocols/protocol, - muxers/muxer, - muxers/mplex/mplex, - protocols/secure/noise, - protocols/secure/plaintext, - protocols/secure/secure, - upgrademngrs/muxedupgrade, - connmanager] +import + ../libp2p/[ + switch, + errors, + multistream, + stream/bufferstream, + protocols/identify, + stream/connection, + transports/transport, + transports/tcptransport, + multiaddress, + peerinfo, + crypto/crypto, + protocols/protocol, + muxers/muxer, + muxers/mplex/mplex, + protocols/secure/noise, + protocols/secure/plaintext, + protocols/secure/secure, + upgrademngrs/muxedupgrade, + connmanager, + ] import ./helpers -const - TestCodec = "/test/proto/1.0.0" +const TestCodec = "/test/proto/1.0.0" -type - TestProto = ref object of LPProtocol +type TestProto = ref object of LPProtocol {.push raises: [].} @@ -52,8 +53,9 @@ method init(p: TestProto) {.gcsafe.} = {.pop.} - -proc createSwitch(ma: MultiAddress; outgoing: bool, plaintext: bool = false): (Switch, PeerInfo) = +proc createSwitch( + ma: MultiAddress, outgoing: bool, plaintext: bool = false +): (Switch, PeerInfo) = var privateKey = PrivateKey.random(ECDSA, rng[]).get() peerInfo = PeerInfo.new(privateKey, @[ma]) @@ -66,22 +68,18 @@ proc createSwitch(ma: MultiAddress; outgoing: bool, plaintext: bool = false): (S peerStore = PeerStore.new(identify) mplexProvider = MuxerProvider.new(createMplex, MplexCodec) muxers = @[mplexProvider] - secureManagers = if plaintext: - [Secure(PlainText.new())] - else: - [Secure(Noise.new(rng, privateKey, outgoing = outgoing))] + secureManagers = + if plaintext: + [Secure(PlainText.new())] + else: + [Secure(Noise.new(rng, privateKey, outgoing = outgoing))] connManager = ConnManager.new() ms = MultistreamSelect.new() muxedUpgrade = MuxedUpgrade.new(muxers, secureManagers, ms) transports = @[Transport(TcpTransport.new(upgrade = muxedUpgrade))] - let switch = newSwitch( - peerInfo, - transports, - secureManagers, - connManager, - ms, - peerStore) + let switch = + newSwitch(peerInfo, transports, secureManagers, connManager, ms, peerStore) result = (switch, peerInfo) suite "Noise": @@ -135,8 +133,7 @@ suite "Noise": serverInfo = PeerInfo.new(serverPrivKey, server) serverNoise = Noise.new(rng, serverPrivKey, outgoing = false) - let - transport1: TcpTransport = TcpTransport.new(upgrade = Upgrade()) + let transport1: TcpTransport = TcpTransport.new(upgrade = Upgrade()) asyncSpawn transport1.start(server) @@ -155,7 +152,9 @@ suite "Noise": transport2: TcpTransport = TcpTransport.new(upgrade = Upgrade()) clientPrivKey = PrivateKey.random(ECDSA, rng[]).get() clientInfo = PeerInfo.new(clientPrivKey, transport1.addrs) - clientNoise = Noise.new(rng, clientPrivKey, outgoing = true, commonPrologue = @[1'u8, 2'u8, 3'u8]) + clientNoise = Noise.new( + rng, clientPrivKey, outgoing = true, commonPrologue = @[1'u8, 2'u8, 3'u8] + ) conn = await transport2.dial(transport1.addrs[0]) var sconn: Connection = nil @@ -226,7 +225,7 @@ suite "Noise": let sconn = await serverNoise.secure(conn, Opt.none(PeerId)) defer: await sconn.close() - let msg = await sconn.readLp(1024*1024) + let msg = await sconn.readLp(1024 * 1024) check msg == hugePayload readTask.complete() @@ -265,15 +264,14 @@ suite "Noise": (switch2, peerInfo2) = createSwitch(ma2, true) await switch1.start() await switch2.start() - let conn = await switch2.dial(switch1.peerInfo.peerId, switch1.peerInfo.addrs, TestCodec) + let conn = + await switch2.dial(switch1.peerInfo.peerId, switch1.peerInfo.addrs, TestCodec) await conn.writeLp("Hello!") let msg = string.fromBytes(await conn.readLp(1024)) check "Hello!" == msg await conn.close() - await allFuturesThrowing( - switch1.stop(), - switch2.stop()) + await allFuturesThrowing(switch1.stop(), switch2.stop()) asyncTest "e2e test wrong secure negotiation": let ma1 = MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet() @@ -292,8 +290,7 @@ suite "Noise": await switch1.start() await switch2.start() expect(DialFailedError): - let conn = await switch2.dial(switch1.peerInfo.peerId, switch1.peerInfo.addrs, TestCodec) + let conn = + await switch2.dial(switch1.peerInfo.peerId, switch1.peerInfo.addrs, TestCodec) - await allFuturesThrowing( - switch1.stop(), - switch2.stop()) + await allFuturesThrowing(switch1.stop(), switch2.stop()) diff --git a/tests/testobservedaddrmanager.nim b/tests/testobservedaddrmanager.nim index a92ec80b2a..a15e2bba71 100644 --- a/tests/testobservedaddrmanager.nim +++ b/tests/testobservedaddrmanager.nim @@ -9,17 +9,13 @@ # This file may not be copied, modified, or distributed except according to # those terms. -import unittest2, - ../libp2p/multiaddress, - ../libp2p/observedaddrmanager, - ./helpers +import unittest2, ../libp2p/multiaddress, ../libp2p/observedaddrmanager, ./helpers suite "ObservedAddrManager": teardown: checkTrackers() asyncTest "Calculate the most oberserved IP correctly": - let observedAddrManager = ObservedAddrManager.new(minCount = 3) # Calculate the most oberserved IP4 correctly @@ -35,7 +31,8 @@ suite "ObservedAddrManager": observedAddrManager.addObservation(MultiAddress.init("/ip4/1.2.3.0/tcp/2").get()) observedAddrManager.addObservation(MultiAddress.init("/ip4/1.2.3.1/tcp/1").get()) - observedAddrManager.guessDialableAddr(maIP4) == MultiAddress.init("/ip4/1.2.3.0/tcp/80").get() + observedAddrManager.guessDialableAddr(maIP4) == + MultiAddress.init("/ip4/1.2.3.0/tcp/80").get() observedAddrManager.getMostObservedProtosAndPorts().len == 0 observedAddrManager.addObservation(mostObservedIP4AndPort) @@ -55,12 +52,14 @@ suite "ObservedAddrManager": observedAddrManager.addObservation(MultiAddress.init("/ip6/::2/tcp/2").get()) observedAddrManager.addObservation(MultiAddress.init("/ip6/::3/tcp/1").get()) - observedAddrManager.guessDialableAddr(maIP6) == MultiAddress.init("/ip6/::2/tcp/80").get() + observedAddrManager.guessDialableAddr(maIP6) == + MultiAddress.init("/ip6/::2/tcp/80").get() observedAddrManager.getMostObservedProtosAndPorts().len == 1 observedAddrManager.addObservation(mostObservedIP6AndPort) - observedAddrManager.getMostObservedProtosAndPorts() == @[mostObservedIP4AndPort, mostObservedIP6AndPort] + observedAddrManager.getMostObservedProtosAndPorts() == + @[mostObservedIP4AndPort, mostObservedIP6AndPort] asyncTest "replace first proto value by most observed when there is only one protocol": let observedAddrManager = ObservedAddrManager.new(minCount = 3) @@ -71,5 +70,5 @@ suite "ObservedAddrManager": observedAddrManager.addObservation(mostObservedIP4AndPort) observedAddrManager.addObservation(mostObservedIP4AndPort) - observedAddrManager.guessDialableAddr( - MultiAddress.init("/ip4/0.0.0.0").get()) == MultiAddress.init("/ip4/1.2.3.4").get() + observedAddrManager.guessDialableAddr(MultiAddress.init("/ip4/0.0.0.0").get()) == + MultiAddress.init("/ip4/1.2.3.4").get() diff --git a/tests/testpeerid.nim b/tests/testpeerid.nim index 64b47db526..264444621a 100644 --- a/tests/testpeerid.nim +++ b/tests/testpeerid.nim @@ -162,7 +162,7 @@ const # Secp256k1 keys "0802122053DADF1D5A164D6B4ACDB15E24AA4C5B1D3461BDBD42ABEDB0A4404D56CED8FB", "08021220FD659951E2ED440CC7ECE436357D123D4C8B3CF1056E3F1607FF3641FB578A1B", - "08021220B333BE3E843339E0E2CE9E083ABC119BE05C7B65B8665ADE19E172D47BF91305" + "08021220B333BE3E843339E0E2CE9E083ABC119BE05C7B65B8665ADE19E172D47BF91305", ] PeerIds = [ @@ -177,12 +177,12 @@ const "Qmcfz2MaPjw44RfVpHKFgXwhW3uFBRBxByVEkgPhefKCJW", "16Uiu2HAmLhLvBoYaoZfaMUKuibM6ac163GwKY74c5kiSLg5KvLpY", "16Uiu2HAmRRrT319h5upVoC3E8vs1Qej4UF3vPPnLgrhbpHhUb2Av", - "16Uiu2HAmDrDaty3uYPgqSr1h5Cup32S2UdYo46rhqZfXPjJMABZL" + "16Uiu2HAmDrDaty3uYPgqSr1h5Cup32S2UdYo46rhqZfXPjJMABZL", ] suite "Peer testing suite": test "Go PeerId test vectors": - for i in 0..= 5 - checkUntilTimeout: rdv.numAdvertiseNs2 >= 5 + checkUntilTimeout: + rdv.numAdvertiseNs1 >= 5 + checkUntilTimeout: + rdv.numAdvertiseNs2 >= 5 await client.stop() asyncTest "Check timeToAdvertise interval": diff --git a/tests/testrouting_record.nim b/tests/testrouting_record.nim index 5d720ffd4f..2c0f9bd460 100644 --- a/tests/testrouting_record.nim +++ b/tests/testrouting_record.nim @@ -19,7 +19,11 @@ suite "Routing record": rng = newRng() privKey = PrivateKey.random(rng[]).tryGet() peerId = PeerId.init(privKey).tryGet() - multiAddresses = @[MultiAddress.init("/ip4/0.0.0.0/tcp/24").tryGet(), MultiAddress.init("/ip4/0.0.0.0/tcp/25").tryGet()] + multiAddresses = + @[ + MultiAddress.init("/ip4/0.0.0.0/tcp/24").tryGet(), + MultiAddress.init("/ip4/0.0.0.0/tcp/25").tryGet(), + ] routingRecord = PeerRecord.init(peerId, multiAddresses, 42) buffer = routingRecord.encode() @@ -53,8 +57,14 @@ suite "Signed Routing Record": rng = newRng() privKey = PrivateKey.random(rng[]).tryGet() peerId = PeerId.init(privKey).tryGet() - multiAddresses = @[MultiAddress.init("/ip4/0.0.0.0/tcp/24").tryGet(), MultiAddress.init("/ip4/0.0.0.0/tcp/25").tryGet()] - routingRecord = SignedPeerRecord.init(privKey, PeerRecord.init(peerId, multiAddresses, 42)).tryGet() + multiAddresses = + @[ + MultiAddress.init("/ip4/0.0.0.0/tcp/24").tryGet(), + MultiAddress.init("/ip4/0.0.0.0/tcp/25").tryGet(), + ] + routingRecord = SignedPeerRecord + .init(privKey, PeerRecord.init(peerId, multiAddresses, 42)) + .tryGet() buffer = routingRecord.envelope.encode().tryGet() parsedRR = SignedPeerRecord.decode(buffer).tryGet().data @@ -72,8 +82,14 @@ suite "Signed Routing Record": privKey = PrivateKey.random(rng[]).tryGet() privKey2 = PrivateKey.random(rng[]).tryGet() peerId = PeerId.init(privKey).tryGet() - multiAddresses = @[MultiAddress.init("/ip4/0.0.0.0/tcp/24").tryGet(), MultiAddress.init("/ip4/0.0.0.0/tcp/25").tryGet()] - routingRecord = SignedPeerRecord.init(privKey2, PeerRecord.init(peerId, multiAddresses, 42)).tryGet() + multiAddresses = + @[ + MultiAddress.init("/ip4/0.0.0.0/tcp/24").tryGet(), + MultiAddress.init("/ip4/0.0.0.0/tcp/25").tryGet(), + ] + routingRecord = SignedPeerRecord + .init(privKey2, PeerRecord.init(peerId, multiAddresses, 42)) + .tryGet() buffer = routingRecord.envelope.encode().tryGet() check SignedPeerRecord.decode(buffer).error == EnvelopeInvalidSignature @@ -83,7 +99,8 @@ suite "Signed Routing Record": rng = newRng() privKey = PrivateKey.random(rng[]).tryGet() peerId = PeerId.init(privKey).tryGet() - multiAddresses = @[MultiAddress(), MultiAddress.init("/ip4/0.0.0.0/tcp/25").tryGet()] + multiAddresses = + @[MultiAddress(), MultiAddress.init("/ip4/0.0.0.0/tcp/25").tryGet()] routingRecord = PeerRecord.init(peerId, multiAddresses, 42) buffer = routingRecord.encode() diff --git a/tests/testrsa.nim b/tests/testrsa.nim index 8f83c6ef37..0b89ad7ee2 100644 --- a/tests/testrsa.nim +++ b/tests/testrsa.nim @@ -227,7 +227,7 @@ const DAA3C8C35515BDBBCBB73C023814531D5D880017DEF1D79EA68C66774009396A 226A426A5BFCC35CB5D2C4718229AC2367BF7D4FB2379093D3607A2EBFBA778C C40489622820A790E236BBB62BEAE9961D165D0784FFBA77A2999638D5AA7313 - 2BF2E6C334598283CFFA28CD""" + 2BF2E6C334598283CFFA28CD""", ] PublicKeys = [ @@ -272,12 +272,10 @@ const 4DF9B17DFED8B01CCE57F78DD3EB1A408C900024CDB1C5C909F55DC9D8C9E1F3 8DD52A3DB9287276D556FC1E5CADB7F727A0C74108CF0F8D88948331E024FA6E BBB064302E58B4FD3EAD0135CA0BAE5BEB2DC6DD5CAC02EF7A59743F5415109D - 750203010001""" + 750203010001""", ] - Messages = [ - "sample", "test", "sample", "test", "sample", "test" - ] + Messages = ["sample", "test", "sample", "test", "sample", "test"] Signatures = [ """30768DB2A850D5F3F9044D2C7545C3E3D30B03B6F8159D305F19B1CF939C1336 @@ -351,7 +349,7 @@ const 29E98C648410A8107EEADA3FAFD32B5AEF12D4E991EAA8364FDFF5AE1C9F34ED 28B120B402C92717B0D480B5DE8D53CADC0903296614158F0DFC38505762C829 9DC529573BBF8C149D5E1E8745F9B72D4E68607C859327AF54D0013F37542236 - ACB51807206B8332127E3692269013B96F0CABD95D7431805E48176ADC5D1366""" + ACB51807206B8332127E3692269013B96F0CABD95D7431805E48176ADC5D1366""", ] let rng = newRng() @@ -482,7 +480,7 @@ suite "RSA 2048/3072/4096 test suite": var csig = RsaSignature.init(sersig).expect("key initialization") check csig.verify(message, pubkey) == true let error = csig.buffer.high - csig.buffer[error] = not(csig.buffer[error]) + csig.buffer[error] = not (csig.buffer[error]) check csig.verify(message, pubkey) == false test "[rsa3072] Generate/Sign/Serialize/Deserialize/Verify test": @@ -497,7 +495,7 @@ suite "RSA 2048/3072/4096 test suite": var csig = RsaSignature.init(sersig).expect("key initialization") check csig.verify(message, pubkey) == true let error = csig.buffer.high - csig.buffer[error] = not(csig.buffer[error]) + csig.buffer[error] = not (csig.buffer[error]) check csig.verify(message, pubkey) == false test "[rsa4096] Generate/Sign/Serialize/Deserialize/Verify test": @@ -513,7 +511,7 @@ suite "RSA 2048/3072/4096 test suite": var csig = RsaSignature.init(sersig).expect("key initialization") check csig.verify(message, pubkey) == true let error = csig.buffer.high - csig.buffer[error] = not(csig.buffer[error]) + csig.buffer[error] = not (csig.buffer[error]) check csig.verify(message, pubkey) == false else: skip() @@ -531,7 +529,7 @@ suite "RSA 2048/3072/4096 test suite": pubkey.getBytes().expect("bytes") == cpubkey.getBytes().expect("bytes") pubkey.getBytes().expect("bytes") == pubser - for i in 0..1: + for i in 0 .. 1: var sigser = fromHex(stripSpaces(Signatures[i])) var sig = RsaSignature.init(sigser).expect("key initialization") var csig = seckey.sign(Messages[i]).expect("signature") @@ -554,7 +552,7 @@ suite "RSA 2048/3072/4096 test suite": pubkey.getBytes().expect("bytes") == cpubkey.getBytes().expect("bytes") pubkey.getBytes().expect("bytes") == pubser - for i in 0..1: + for i in 0 .. 1: var sigser = fromHex(stripSpaces(Signatures[2 + i])) var sig = RsaSignature.init(sigser).expect("key initialization") var csig = seckey.sign(Messages[2 + i]).expect("signature") @@ -577,7 +575,7 @@ suite "RSA 2048/3072/4096 test suite": pubkey.getBytes().expect("bytes") == cpubkey.getBytes().expect("bytes") pubkey.getBytes().expect("bytes") == pubser - for i in 0..1: + for i in 0 .. 1: var sigser = fromHex(stripSpaces(Signatures[4 + i])) var sig = RsaSignature.init(sigser).expect("key initialization") var csig = seckey.sign(Messages[4 + i]).expect("signature") diff --git a/tests/testsecp256k1.nim b/tests/testsecp256k1.nim index 88d60dfe73..7d4f11fe04 100644 --- a/tests/testsecp256k1.nim +++ b/tests/testsecp256k1.nim @@ -18,7 +18,7 @@ suite "Secp256k1 testing suite": const TestsCount = 20 test "Private key serialize/deserialize test": - for i in 0.. 0 and resource <= 3 - let sleep = rand(0..10).millis + let sleep = rand(0 .. 10).millis # echo sleep await sleepAsync(sleep) finally: @@ -105,7 +105,7 @@ suite "AsyncSemaphore": sema.release() var tasks: seq[Future[void]] - for i in 0..<10: + for i in 0 ..< 10: tasks.add(task()) await allFutures(tasks) @@ -134,7 +134,7 @@ suite "AsyncSemaphore": asyncTest "should handle out of order cancellations": let sema = newAsyncSemaphore(1) - await sema.acquire() # 1st acquire + await sema.acquire() # 1st acquire let tmp1 = sema.acquire() # 2nd acquire check not tmp1.finished() @@ -160,7 +160,8 @@ suite "AsyncSemaphore": let sema = newAsyncSemaphore(1) await sema.acquire() - check not(await sema.acquire().withTimeout(1.millis)) # should not acquire but cancel + check not (await sema.acquire().withTimeout(1.millis)) + # should not acquire but cancel sema.release() check await sema.acquire().withTimeout(10.millis) @@ -169,7 +170,8 @@ suite "AsyncSemaphore": let sema = newAsyncSemaphore(1) await sema.acquire() - check not(await sema.acquire().withTimeout(1.millis)) # should not acquire but cancel + check not (await sema.acquire().withTimeout(1.millis)) + # should not acquire but cancel let fut1 = sema.acquire() @@ -189,7 +191,6 @@ suite "AsyncSemaphore": fut1.finished() fut2.finished() - sema.forceAcquire() sema.forceAcquire() diff --git a/tests/testsigned_envelope.nim b/tests/testsigned_envelope.nim index 48bb02e0f6..6837f047cf 100644 --- a/tests/testsigned_envelope.nim +++ b/tests/testsigned_envelope.nim @@ -18,7 +18,8 @@ suite "Signed envelope": let rng = newRng() privKey = PrivateKey.random(rng[]).tryGet() - envelope = Envelope.init(privKey, @[byte 12, 0], "payload".toBytes(), "domain").tryGet() + envelope = + Envelope.init(privKey, @[byte 12, 0], "payload".toBytes(), "domain").tryGet() buffer = envelope.encode().tryGet() decodedEnvelope = Envelope.decode(buffer, "domain").tryGet() wrongDomain = Envelope.decode(buffer, "wdomain") @@ -50,20 +51,29 @@ suite "Signed envelope": type DummyPayload* = object awesome: byte + SignedDummy = SignedPayload[DummyPayload] -proc decode*(T: typedesc[DummyPayload], buffer: seq[byte]): Result[DummyPayload, cstring] = +proc decode*( + T: typedesc[DummyPayload], buffer: seq[byte] +): Result[DummyPayload, cstring] = ok(DummyPayload(awesome: buffer[0])) proc encode*(pd: DummyPayload): seq[byte] = @[pd.awesome] proc checkValid*(pd: SignedDummy): Result[void, EnvelopeError] = - if pd.data.awesome == 12.byte: ok() - else: err(EnvelopeInvalidSignature) + if pd.data.awesome == 12.byte: + ok() + else: + err(EnvelopeInvalidSignature) + +proc payloadDomain*(T: typedesc[DummyPayload]): string = + "dummy" + +proc payloadType*(T: typedesc[DummyPayload]): seq[byte] = + @[(byte) 0x00, (byte) 0x00] -proc payloadDomain*(T: typedesc[DummyPayload]): string = "dummy" -proc payloadType*(T: typedesc[DummyPayload]): seq[byte] = @[(byte) 0x00, (byte) 0x00] suite "Signed payload": test "Simple encode -> decode": let @@ -95,6 +105,8 @@ suite "Signed payload": privKey = PrivateKey.random(rng[]).tryGet() dummyPayload = DummyPayload(awesome: 30.byte) - signed = Envelope.init(privKey, @[55.byte], dummyPayload.encode(), DummyPayload.payloadDomain).tryGet() + signed = Envelope + .init(privKey, @[55.byte], dummyPayload.encode(), DummyPayload.payloadDomain) + .tryGet() encoded = signed.encode().tryGet() check SignedDummy.decode(encoded).error == EnvelopeWrongType diff --git a/tests/teststreamseq.nim b/tests/teststreamseq.nim index f5f58b942b..dd83436d8b 100644 --- a/tests/teststreamseq.nim +++ b/tests/teststreamseq.nim @@ -25,7 +25,7 @@ suite "StreamSeq": check: @(s.data()) == [byte 0, 1, 2, 3] - s.prepare(10)[0..<3] = [byte 4, 5, 6] + s.prepare(10)[0 ..< 3] = [byte 4, 5, 6] check: @(s.data()) == [byte 0, 1, 2, 3] @@ -42,14 +42,17 @@ suite "StreamSeq": s.consume(6) - check: @(s.data()) == [] + check: + @(s.data()) == [] s.add([]) - check: @(s.data()) == [] + check: + @(s.data()) == [] var o: seq[byte] - check: 0 == s.consumeTo(o) + check: + 0 == s.consumeTo(o) s.add([byte 1, 2, 3]) diff --git a/tests/testswitch.nim b/tests/testswitch.nim index b9638cafea..d15e4f8bf1 100644 --- a/tests/testswitch.nim +++ b/tests/testswitch.nim @@ -12,33 +12,34 @@ import options, sequtils import chronos import stew/byteutils -import ../libp2p/[errors, - switch, - multistream, - builders, - stream/bufferstream, - stream/connection, - multicodec, - multiaddress, - peerinfo, - crypto/crypto, - protocols/protocol, - protocols/secure/secure, - muxers/muxer, - muxers/mplex/lpchannel, - stream/lpstream, - nameresolving/mockresolver, - stream/chronosstream, - utils/semaphore, - transports/tcptransport, - transports/wstransport] +import + ../libp2p/[ + errors, + switch, + multistream, + builders, + stream/bufferstream, + stream/connection, + multicodec, + multiaddress, + peerinfo, + crypto/crypto, + protocols/protocol, + protocols/secure/secure, + muxers/muxer, + muxers/mplex/lpchannel, + stream/lpstream, + nameresolving/mockresolver, + stream/chronosstream, + utils/semaphore, + transports/tcptransport, + transports/wstransport, + ] import ./helpers -const - TestCodec = "/test/proto/1.0.0" +const TestCodec = "/test/proto/1.0.0" -type - TestProto = ref object of LPProtocol +type TestProto = ref object of LPProtocol suite "Switch": teardown: @@ -66,7 +67,8 @@ suite "Switch": await switch1.start() await switch2.start() - let conn = await switch2.dial(switch1.peerInfo.peerId, switch1.peerInfo.addrs, TestCodec) + let conn = + await switch2.dial(switch1.peerInfo.peerId, switch1.peerInfo.addrs, TestCodec) check switch1.isConnected(switch2.peerInfo.peerId) check switch2.isConnected(switch1.peerInfo.peerId) @@ -76,10 +78,7 @@ suite "Switch": check "Hello!" == msg await conn.close() - await allFuturesThrowing( - done.wait(5.seconds), - switch1.stop(), - switch2.stop()) + await allFuturesThrowing(done.wait(5.seconds), switch1.stop(), switch2.stop()) check not switch1.isConnected(switch2.peerInfo.peerId) check not switch2.isConnected(switch1.peerInfo.peerId) @@ -111,7 +110,8 @@ suite "Switch": await switch1.start() await switch2.start() - let conn = await switch2.dial(switch1.peerInfo.peerId, switch1.peerInfo.addrs, callProto) + let conn = + await switch2.dial(switch1.peerInfo.peerId, switch1.peerInfo.addrs, callProto) check switch1.isConnected(switch2.peerInfo.peerId) check switch2.isConnected(switch1.peerInfo.peerId) @@ -121,10 +121,7 @@ suite "Switch": check "Hello!" == msg await conn.close() - await allFuturesThrowing( - done.wait(5.seconds), - switch1.stop(), - switch2.stop()) + await allFuturesThrowing(done.wait(5.seconds), switch1.stop(), switch2.stop()) check not switch1.isConnected(switch2.peerInfo.peerId) check not switch2.isConnected(switch1.peerInfo.peerId) @@ -151,7 +148,8 @@ suite "Switch": await switch1.start() await switch2.start() - let conn = await switch2.dial(switch1.peerInfo.peerId, switch1.peerInfo.addrs, TestCodec) + let conn = + await switch2.dial(switch1.peerInfo.peerId, switch1.peerInfo.addrs, TestCodec) check switch1.isConnected(switch2.peerInfo.peerId) check switch2.isConnected(switch1.peerInfo.peerId) @@ -161,11 +159,7 @@ suite "Switch": check "Hello!" == msg await conn.close() - await allFuturesThrowing( - done.wait(5.seconds), - switch1.stop(), - switch2.stop(), - ) + await allFuturesThrowing(done.wait(5.seconds), switch1.stop(), switch2.stop()) check not switch1.isConnected(switch2.peerInfo.peerId) check not switch2.isConnected(switch1.peerInfo.peerId) @@ -191,7 +185,8 @@ suite "Switch": await switch2.start() await switch2.connect(switch1.peerInfo.peerId, switch1.peerInfo.addrs) - let conn = await switch2.dial(switch1.peerInfo.peerId, switch1.peerInfo.addrs, TestCodec) + let conn = + await switch2.dial(switch1.peerInfo.peerId, switch1.peerInfo.addrs, TestCodec) check switch1.isConnected(switch2.peerInfo.peerId) check switch2.isConnected(switch1.peerInfo.peerId) @@ -200,11 +195,7 @@ suite "Switch": let msg = string.fromBytes(await conn.readLp(1024)) check "Hello!" == msg - await allFuturesThrowing( - conn.close(), - switch1.stop(), - switch2.stop() - ) + await allFuturesThrowing(conn.close(), switch1.stop(), switch2.stop()) check not switch1.isConnected(switch2.peerInfo.peerId) check not switch2.isConnected(switch1.peerInfo.peerId) @@ -212,28 +203,29 @@ suite "Switch": asyncTest "e2e connect to peer with unknown PeerId": let resolver = MockResolver.new() let switch1 = newStandardSwitch(secureManagers = [SecureProtocol.Noise]) - let switch2 = newStandardSwitch(secureManagers = [SecureProtocol.Noise], nameResolver = resolver) + let switch2 = newStandardSwitch( + secureManagers = [SecureProtocol.Noise], nameResolver = resolver + ) await switch1.start() await switch2.start() # via dnsaddr - resolver.txtResponses["_dnsaddr.test.io"] = @[ - "dnsaddr=" & $switch1.peerInfo.addrs[0] & "/p2p/" & $switch1.peerInfo.peerId, - ] + resolver.txtResponses["_dnsaddr.test.io"] = + @["dnsaddr=" & $switch1.peerInfo.addrs[0] & "/p2p/" & $switch1.peerInfo.peerId] - check: (await switch2.connect(MultiAddress.init("/dnsaddr/test.io/").tryGet(), true)) == switch1.peerInfo.peerId + check: + (await switch2.connect(MultiAddress.init("/dnsaddr/test.io/").tryGet(), true)) == + switch1.peerInfo.peerId await switch2.disconnect(switch1.peerInfo.peerId) # via direct ip check not switch2.isConnected(switch1.peerInfo.peerId) - check: (await switch2.connect(switch1.peerInfo.addrs[0], true)) == switch1.peerInfo.peerId + check: + (await switch2.connect(switch1.peerInfo.addrs[0], true)) == switch1.peerInfo.peerId await switch2.disconnect(switch1.peerInfo.peerId) - await allFuturesThrowing( - switch1.stop(), - switch2.stop() - ) + await allFuturesThrowing(switch1.stop(), switch2.stop()) asyncTest "e2e connect to peer with known PeerId": let switch1 = newStandardSwitch(secureManagers = [SecureProtocol.Noise]) @@ -249,19 +241,21 @@ suite "Switch": discard await switch2.connect(switch1.peerInfo.addrs[0]) # with invalid PeerId, will fail - let fakeMa = concat(switch1.peerInfo.addrs[0], MultiAddress.init(multiCodec("p2p"), PeerId.random.tryGet().data).tryGet()).tryGet() + let fakeMa = concat( + switch1.peerInfo.addrs[0], + MultiAddress.init(multiCodec("p2p"), PeerId.random.tryGet().data).tryGet(), + ) + .tryGet() expect(CatchableError): discard (await switch2.connect(fakeMa)) # real thing works - check (await switch2.connect(switch1.peerInfo.fullAddrs.tryGet()[0])) == switch1.peerInfo.peerId + check (await switch2.connect(switch1.peerInfo.fullAddrs.tryGet()[0])) == + switch1.peerInfo.peerId await switch2.disconnect(switch1.peerInfo.peerId) - await allFuturesThrowing( - switch1.stop(), - switch2.stop() - ) + await allFuturesThrowing(switch1.stop(), switch2.stop()) asyncTest "e2e should not leak on peer disconnect": let switch1 = newStandardSwitch() @@ -272,7 +266,7 @@ suite "Switch": let startCounts = @[ switch1.connManager.inSema.count, switch1.connManager.outSema.count, - switch2.connManager.inSema.count, switch2.connManager.outSema.count + switch2.connManager.inSema.count, switch2.connManager.outSema.count, ] await switch2.connect(switch1.peerInfo.peerId, switch1.peerInfo.addrs) @@ -283,7 +277,8 @@ suite "Switch": await switch2.disconnect(switch1.peerInfo.peerId) check not switch2.isConnected(switch1.peerInfo.peerId) - checkUntilTimeout: not switch1.isConnected(switch2.peerInfo.peerId) + checkUntilTimeout: + not switch1.isConnected(switch2.peerInfo.peerId) checkTracker(LPChannelTrackerName) checkTracker(SecureConnTrackerName) @@ -292,12 +287,10 @@ suite "Switch": startCounts == @[ switch1.connManager.inSema.count, switch1.connManager.outSema.count, - switch2.connManager.inSema.count, switch2.connManager.outSema.count + switch2.connManager.inSema.count, switch2.connManager.outSema.count, ] - await allFuturesThrowing( - switch1.stop(), - switch2.stop()) + await allFuturesThrowing(switch1.stop(), switch2.stop()) asyncTest "e2e should trigger connection events (remote)": let switch1 = newStandardSwitch() @@ -307,7 +300,7 @@ suite "Switch": var kinds: set[ConnEventKind] proc hook(peerId: PeerId, event: ConnEvent) {.async.} = kinds = kinds + {event.kind} - case step: + case step of 0: check: event.kind == ConnEventKind.Connected @@ -336,20 +329,16 @@ suite "Switch": await switch2.disconnect(switch1.peerInfo.peerId) check not switch2.isConnected(switch1.peerInfo.peerId) - checkUntilTimeout: not switch1.isConnected(switch2.peerInfo.peerId) + checkUntilTimeout: + not switch1.isConnected(switch2.peerInfo.peerId) checkTracker(LPChannelTrackerName) checkTracker(SecureConnTrackerName) check: - kinds == { - ConnEventKind.Connected, - ConnEventKind.Disconnected - } + kinds == {ConnEventKind.Connected, ConnEventKind.Disconnected} - await allFuturesThrowing( - switch1.stop(), - switch2.stop()) + await allFuturesThrowing(switch1.stop(), switch2.stop()) asyncTest "e2e should trigger connection events (local)": let switch1 = newStandardSwitch() @@ -359,7 +348,7 @@ suite "Switch": var kinds: set[ConnEventKind] proc hook(peerId: PeerId, event: ConnEvent) {.async.} = kinds = kinds + {event.kind} - case step: + case step of 0: check: event.kind == ConnEventKind.Connected @@ -388,20 +377,16 @@ suite "Switch": await switch2.disconnect(switch1.peerInfo.peerId) check not switch2.isConnected(switch1.peerInfo.peerId) - checkUntilTimeout: not switch1.isConnected(switch2.peerInfo.peerId) + checkUntilTimeout: + not switch1.isConnected(switch2.peerInfo.peerId) checkTracker(LPChannelTrackerName) checkTracker(SecureConnTrackerName) check: - kinds == { - ConnEventKind.Connected, - ConnEventKind.Disconnected - } + kinds == {ConnEventKind.Connected, ConnEventKind.Disconnected} - await allFuturesThrowing( - switch1.stop(), - switch2.stop()) + await allFuturesThrowing(switch1.stop(), switch2.stop()) asyncTest "e2e should trigger peer events (remote)": let switch1 = newStandardSwitch() @@ -411,7 +396,7 @@ suite "Switch": var kinds: set[PeerEventKind] proc handler(peerId: PeerId, event: PeerEvent) {.async.} = kinds = kinds + {event.kind} - case step: + case step of 0: check: event.kind == PeerEventKind.Joined @@ -439,20 +424,16 @@ suite "Switch": await switch2.disconnect(switch1.peerInfo.peerId) check not switch2.isConnected(switch1.peerInfo.peerId) - checkUntilTimeout: not switch1.isConnected(switch2.peerInfo.peerId) + checkUntilTimeout: + not switch1.isConnected(switch2.peerInfo.peerId) checkTracker(LPChannelTrackerName) checkTracker(SecureConnTrackerName) check: - kinds == { - PeerEventKind.Joined, - PeerEventKind.Left - } + kinds == {PeerEventKind.Joined, PeerEventKind.Left} - await allFuturesThrowing( - switch1.stop(), - switch2.stop()) + await allFuturesThrowing(switch1.stop(), switch2.stop()) asyncTest "e2e should trigger peer events (local)": let switch1 = newStandardSwitch() @@ -462,7 +443,7 @@ suite "Switch": var kinds: set[PeerEventKind] proc handler(peerId: PeerId, event: PeerEvent) {.async.} = kinds = kinds + {event.kind} - case step: + case step of 0: check: event.kind == PeerEventKind.Joined @@ -490,20 +471,16 @@ suite "Switch": await switch2.disconnect(switch1.peerInfo.peerId) check not switch2.isConnected(switch1.peerInfo.peerId) - checkUntilTimeout: not switch1.isConnected(switch2.peerInfo.peerId) + checkUntilTimeout: + not switch1.isConnected(switch2.peerInfo.peerId) checkTracker(LPChannelTrackerName) checkTracker(SecureConnTrackerName) check: - kinds == { - PeerEventKind.Joined, - PeerEventKind.Left - } + kinds == {PeerEventKind.Joined, PeerEventKind.Left} - await allFuturesThrowing( - switch1.stop(), - switch2.stop()) + await allFuturesThrowing(switch1.stop(), switch2.stop()) asyncTest "e2e should trigger peer events only once per peer": let switch1 = newStandardSwitch() @@ -511,19 +488,15 @@ suite "Switch": let rng = crypto.newRng() # use same private keys to emulate two connection from same peer let privKey = PrivateKey.random(rng[]).tryGet() - let switch2 = newStandardSwitch( - privKey = some(privKey), - rng = rng) + let switch2 = newStandardSwitch(privKey = some(privKey), rng = rng) - let switch3 = newStandardSwitch( - privKey = some(privKey), - rng = rng) + let switch3 = newStandardSwitch(privKey = some(privKey), rng = rng) var step = 0 var kinds: set[PeerEventKind] proc handler(peerId: PeerId, event: PeerEvent) {.async.} = kinds = kinds + {event.kind} - case step: + case step of 0: check: event.kind == PeerEventKind.Joined @@ -542,8 +515,10 @@ suite "Switch": await switch2.start() await switch3.start() - await switch2.connect(switch1.peerInfo.peerId, switch1.peerInfo.addrs) # should trigger 1st Join event - await switch3.connect(switch1.peerInfo.peerId, switch1.peerInfo.addrs) # should trigger 2nd Join event + await switch2.connect(switch1.peerInfo.peerId, switch1.peerInfo.addrs) + # should trigger 1st Join event + await switch3.connect(switch1.peerInfo.peerId, switch1.peerInfo.addrs) + # should trigger 2nd Join event check switch1.isConnected(switch2.peerInfo.peerId) check switch2.isConnected(switch1.peerInfo.peerId) @@ -554,22 +529,18 @@ suite "Switch": check not switch2.isConnected(switch1.peerInfo.peerId) check not switch3.isConnected(switch1.peerInfo.peerId) - checkUntilTimeout: not switch1.isConnected(switch2.peerInfo.peerId) - checkUntilTimeout: not switch1.isConnected(switch3.peerInfo.peerId) + checkUntilTimeout: + not switch1.isConnected(switch2.peerInfo.peerId) + checkUntilTimeout: + not switch1.isConnected(switch3.peerInfo.peerId) checkTracker(LPChannelTrackerName) checkTracker(SecureConnTrackerName) check: - kinds == { - PeerEventKind.Joined, - PeerEventKind.Left - } + kinds == {PeerEventKind.Joined, PeerEventKind.Left} - await allFuturesThrowing( - switch1.stop(), - switch2.stop(), - switch3.stop()) + await allFuturesThrowing(switch1.stop(), switch2.stop(), switch3.stop()) asyncTest "e2e should allow dropping peer from connection events": let rng = crypto.newRng() @@ -582,7 +553,7 @@ suite "Switch": var done = newFuture[void]() var onConnect: Future[void] proc hook(peerId: PeerId, event: ConnEvent) {.async.} = - case event.kind: + case event.kind of ConnEventKind.Connected: await onConnect await switches[0].disconnect(peerInfo.peerId) # trigger disconnect @@ -590,23 +561,20 @@ suite "Switch": check not switches[0].isConnected(peerInfo.peerId) done.complete() - switches.add(newStandardSwitch( - rng = rng)) + switches.add(newStandardSwitch(rng = rng)) switches[0].addConnEventHandler(hook, ConnEventKind.Connected) switches[0].addConnEventHandler(hook, ConnEventKind.Disconnected) await switches[0].start() - switches.add(newStandardSwitch( - privKey = some(privateKey), - rng = rng)) - onConnect = switches[1].connect(switches[0].peerInfo.peerId, switches[0].peerInfo.addrs) + switches.add(newStandardSwitch(privKey = some(privateKey), rng = rng)) + onConnect = + switches[1].connect(switches[0].peerInfo.peerId, switches[0].peerInfo.addrs) await onConnect await done - await allFuturesThrowing( - switches.mapIt( it.stop() )) + await allFuturesThrowing(switches.mapIt(it.stop())) asyncTest "e2e should allow dropping multiple connections for peer from connection events": let rng = crypto.newRng() @@ -620,7 +588,7 @@ suite "Switch": var done = newFuture[void]() var onConnect: Future[void] proc hook(peerId2: PeerId, event: ConnEvent) {.async.} = - case event.kind: + case event.kind of ConnEventKind.Connected: if conns == 5: await onConnect @@ -634,27 +602,23 @@ suite "Switch": done.complete() conns.dec - switches.add(newStandardSwitch( - maxConnsPerPeer = 10, - rng = rng)) + switches.add(newStandardSwitch(maxConnsPerPeer = 10, rng = rng)) switches[0].addConnEventHandler(hook, ConnEventKind.Connected) await switches[0].start() - for i in 1..5: - switches.add(newStandardSwitch( - privKey = some(privateKey), - rng = rng)) + for i in 1 .. 5: + switches.add(newStandardSwitch(privKey = some(privateKey), rng = rng)) switches[i].addConnEventHandler(hook, ConnEventKind.Disconnected) - onConnect = switches[i].connect(switches[0].peerInfo.peerId, switches[0].peerInfo.addrs) + onConnect = + switches[i].connect(switches[0].peerInfo.peerId, switches[0].peerInfo.addrs) await onConnect await done checkTracker(LPChannelTrackerName) checkTracker(SecureConnTrackerName) - await allFuturesThrowing( - switches.mapIt( it.stop() )) + await allFuturesThrowing(switches.mapIt(it.stop())) asyncTest "e2e closing remote conn should not leak": let ma = @[MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet()] @@ -681,9 +645,7 @@ suite "Switch": checkTracker(SecureConnTrackerName) checkTracker(ChronosStreamTrackerName) - await allFuturesThrowing( - transport.stop(), - switch.stop()) + await allFuturesThrowing(transport.stop(), switch.stop()) asyncTest "e2e calling closeWithEOF on the same stream should not assert": proc handle(conn: Connection, proto: string) {.async.} = @@ -700,18 +662,20 @@ suite "Switch": await switch1.start() - let conn = await switch2.dial(switch1.peerInfo.peerId, switch1.peerInfo.addrs, TestCodec) + let conn = + await switch2.dial(switch1.peerInfo.peerId, switch1.peerInfo.addrs, TestCodec) proc closeReader() {.async.} = await conn.closeWithEOF() var readers: seq[Future[void]] - for i in 0..10: + for i in 0 .. 10: readers.add(closeReader()) await allFuturesThrowing(readers) await switch2.stop() #Otherwise this leaks - checkUntilTimeout: not switch1.isConnected(switch2.peerInfo.peerId) + checkUntilTimeout: + not switch1.isConnected(switch2.peerInfo.peerId) checkTracker(LPChannelTrackerName) checkTracker(SecureConnTrackerName) @@ -736,27 +700,30 @@ suite "Switch": await destSwitch.start() let destPeerInfo = destSwitch.peerInfo - for i in 0..<3: + for i in 0 ..< 3: let switch = newStandardSwitch() switches.add(switch) await switch.start() - check await switch.connect(destPeerInfo.peerId, destPeerInfo.addrs) - .withTimeout(1000.millis) + check await switch.connect(destPeerInfo.peerId, destPeerInfo.addrs).withTimeout( + 1000.millis + ) let switchFail = newStandardSwitch() switches.add(switchFail) await switchFail.start() - check not(await switchFail.connect(destPeerInfo.peerId, destPeerInfo.addrs) - .withTimeout(1000.millis)) + check not ( + await switchFail.connect(destPeerInfo.peerId, destPeerInfo.addrs).withTimeout( + 1000.millis + ) + ) - await allFuturesThrowing( - allFutures(switches.mapIt( it.stop() ))) + await allFuturesThrowing(allFutures(switches.mapIt(it.stop()))) asyncTest "e2e total connection limits on incoming connections": var switches: seq[Switch] - for i in 0..<3: + for i in 0 ..< 3: switches.add(newStandardSwitch()) await switches[i].start() @@ -767,8 +734,9 @@ suite "Switch": await dstSwitch.start() for s in switches: - check await srcSwitch.connect(s.peerInfo.peerId, s.peerInfo.addrs) - .withTimeout(1000.millis) + check await srcSwitch.connect(s.peerInfo.peerId, s.peerInfo.addrs).withTimeout( + 1000.millis + ) expect TooManyConnectionsError: await srcSwitch.connect(dstSwitch.peerInfo.peerId, dstSwitch.peerInfo.addrs) @@ -776,8 +744,7 @@ suite "Switch": switches.add(srcSwitch) switches.add(dstSwitch) - await allFuturesThrowing( - allFutures(switches.mapIt( it.stop() ))) + await allFuturesThrowing(allFutures(switches.mapIt(it.stop()))) asyncTest "e2e max incoming connection limits": var switches: seq[Switch] @@ -786,28 +753,30 @@ suite "Switch": await destSwitch.start() let destPeerInfo = destSwitch.peerInfo - for i in 0..<3: + for i in 0 ..< 3: let switch = newStandardSwitch() switches.add(switch) await switch.start() - check await switch.connect(destPeerInfo.peerId, destPeerInfo.addrs) - .withTimeout(1000.millis) + check await switch.connect(destPeerInfo.peerId, destPeerInfo.addrs).withTimeout( + 1000.millis + ) let switchFail = newStandardSwitch() switches.add(switchFail) await switchFail.start() - check not(await switchFail.connect(destPeerInfo.peerId, destPeerInfo.addrs) - .withTimeout(1000.millis)) + check not ( + await switchFail.connect(destPeerInfo.peerId, destPeerInfo.addrs).withTimeout( + 1000.millis + ) + ) - await allFuturesThrowing( - allFutures(switches.mapIt( it.stop() ))) + await allFuturesThrowing(allFutures(switches.mapIt(it.stop()))) asyncTest "e2e max outgoing connection limits": - var switches: seq[Switch] - for i in 0..<3: + for i in 0 ..< 3: switches.add(newStandardSwitch()) await switches[i].start() @@ -818,8 +787,9 @@ suite "Switch": await dstSwitch.start() for s in switches: - check await srcSwitch.connect(s.peerInfo.peerId, s.peerInfo.addrs) - .withTimeout(1000.millis) + check await srcSwitch.connect(s.peerInfo.peerId, s.peerInfo.addrs).withTimeout( + 1000.millis + ) expect TooManyConnectionsError: await srcSwitch.connect(dstSwitch.peerInfo.peerId, dstSwitch.peerInfo.addrs) @@ -827,8 +797,7 @@ suite "Switch": switches.add(srcSwitch) switches.add(dstSwitch) - await allFuturesThrowing( - allFutures(switches.mapIt( it.stop() ))) + await allFuturesThrowing(allFutures(switches.mapIt(it.stop()))) asyncTest "e2e peer store": let done = newFuture[void]() @@ -852,7 +821,8 @@ suite "Switch": await switch1.start() await switch2.start() - let conn = await switch2.dial(switch1.peerInfo.peerId, switch1.peerInfo.addrs, TestCodec) + let conn = + await switch2.dial(switch1.peerInfo.peerId, switch1.peerInfo.addrs, TestCodec) check switch1.isConnected(switch2.peerInfo.peerId) check switch2.isConnected(switch1.peerInfo.peerId) @@ -862,10 +832,7 @@ suite "Switch": check "Hello!" == msg await conn.close() - await allFuturesThrowing( - done.wait(5.seconds), - switch1.stop(), - switch2.stop()) + await allFuturesThrowing(done.wait(5.seconds), switch1.stop(), switch2.stop()) check not switch1.isConnected(switch2.peerInfo.peerId) check not switch2.isConnected(switch1.peerInfo.peerId) @@ -894,32 +861,30 @@ suite "Switch": testProto.codec = TestCodec testProto.handler = handle - let addrs = @[MultiAddress.init("/ip4/127.0.0.1/tcp/0").tryGet(), - MultiAddress.init("/ip6/::1/tcp/0").tryGet()] + let addrs = + @[ + MultiAddress.init("/ip4/127.0.0.1/tcp/0").tryGet(), + MultiAddress.init("/ip6/::1/tcp/0").tryGet(), + ] let switch1 = newStandardSwitch( - addrs = addrs, - transportFlags = {ServerFlags.ReuseAddr, ServerFlags.ReusePort}) + addrs = addrs, transportFlags = {ServerFlags.ReuseAddr, ServerFlags.ReusePort} + ) switch1.mount(testProto) let switch2 = newStandardSwitch() - let switch3 = newStandardSwitch( - addrs = MultiAddress.init("/ip4/127.0.0.1/tcp/0").tryGet() - ) + let switch3 = + newStandardSwitch(addrs = MultiAddress.init("/ip4/127.0.0.1/tcp/0").tryGet()) - await allFuturesThrowing( - switch1.start(), - switch2.start(), - switch3.start()) + await allFuturesThrowing(switch1.start(), switch2.start(), switch3.start()) check IP4.matchPartial(switch1.peerInfo.addrs[0]) check IP6.matchPartial(switch1.peerInfo.addrs[1]) let conn = await switch2.dial( - switch1.peerInfo.peerId, - @[switch1.peerInfo.addrs[0]], - TestCodec) + switch1.peerInfo.peerId, @[switch1.peerInfo.addrs[0]], TestCodec + ) check switch1.isConnected(switch2.peerInfo.peerId) check switch2.isConnected(switch1.peerInfo.peerId) @@ -929,9 +894,8 @@ suite "Switch": await conn.close() let connv6 = await switch3.dial( - switch1.peerInfo.peerId, - @[switch1.peerInfo.addrs[1]], - TestCodec) + switch1.peerInfo.peerId, @[switch1.peerInfo.addrs[1]], TestCodec + ) check switch1.isConnected(switch3.peerInfo.peerId) check switch3.isConnected(switch1.peerInfo.peerId) @@ -940,10 +904,7 @@ suite "Switch": check "Hello!" == string.fromBytes(await connv6.readLp(1024)) await connv6.close() - await allFuturesThrowing( - switch1.stop(), - switch2.stop(), - switch3.stop()) + await allFuturesThrowing(switch1.stop(), switch2.stop(), switch3.stop()) check not switch1.isConnected(switch2.peerInfo.peerId) check not switch2.isConnected(switch1.peerInfo.peerId) @@ -960,8 +921,9 @@ suite "Switch": await destSwitch.start() await srcSwitch.start() - let testAddr = MultiAddress.init("/dns4/localhost/").tryGet() & - destSwitch.peerInfo.addrs[0][1].tryGet() + let testAddr = + MultiAddress.init("/dns4/localhost/").tryGet() & + destSwitch.peerInfo.addrs[0][1].tryGet() await srcSwitch.connect(destSwitch.peerInfo.peerId, @[testAddr]) check srcSwitch.isConnected(destSwitch.peerInfo.peerId) @@ -977,22 +939,28 @@ suite "Switch": tcpAddress = MultiAddress.init("/ip4/127.0.0.1/tcp/0").tryGet() srcTcpSwitch = newStandardSwitch(nameResolver = resolver) - srcWsSwitch = - SwitchBuilder.new() + srcWsSwitch = SwitchBuilder + .new() .withAddress(wsAddress) .withRng(crypto.newRng()) .withMplex() - .withTransport(proc (upgr: Upgrade): Transport = WsTransport.new(upgr)) + .withTransport( + proc(upgr: Upgrade): Transport = + WsTransport.new(upgr) + ) .withNameResolver(resolver) .withNoise() .build() - destSwitch = - SwitchBuilder.new() + destSwitch = SwitchBuilder + .new() .withAddresses(@[tcpAddress, wsAddress]) .withRng(crypto.newRng()) .withMplex() - .withTransport(proc (upgr: Upgrade): Transport = WsTransport.new(upgr)) + .withTransport( + proc(upgr: Upgrade): Transport = + WsTransport.new(upgr) + ) .withTcpTransport() .withNoise() .build() @@ -1000,10 +968,12 @@ suite "Switch": await destSwitch.start() await srcWsSwitch.start() - resolver.txtResponses["_dnsaddr.test.io"] = @[ - "dnsaddr=/dns4/localhost" & $destSwitch.peerInfo.addrs[0][1..^1].tryGet() & "/p2p/" & $destSwitch.peerInfo.peerId, - "dnsaddr=/dns4/localhost" & $destSwitch.peerInfo.addrs[1][1..^1].tryGet() - ] + resolver.txtResponses["_dnsaddr.test.io"] = + @[ + "dnsaddr=/dns4/localhost" & $destSwitch.peerInfo.addrs[0][1 ..^ 1].tryGet() & + "/p2p/" & $destSwitch.peerInfo.peerId, + "dnsaddr=/dns4/localhost" & $destSwitch.peerInfo.addrs[1][1 ..^ 1].tryGet(), + ] resolver.ipResponses[("localhost", false)] = @["127.0.0.1"] let testAddr = MultiAddress.init("/dnsaddr/test.io/").tryGet() @@ -1023,6 +993,7 @@ suite "Switch": check "test123" == string.fromBytes(await conn.readLp(1024)) await conn.writeLp("test456") await conn.close() + let src = newStandardSwitch() dst = newStandardSwitch() @@ -1046,7 +1017,11 @@ suite "Switch": asyncTest "switch failing to start stops properly": let switch = newStandardSwitch( - addrs = @[MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet(), MultiAddress.init("/ip4/1.1.1.1/tcp/0").tryGet()] + addrs = + @[ + MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet(), + MultiAddress.init("/ip4/1.1.1.1/tcp/0").tryGet(), + ] ) expect LPError: @@ -1054,9 +1029,8 @@ suite "Switch": # test is that this doesn't leak asyncTest "starting two times does not crash": - let switch = newStandardSwitch( - addrs = @[MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet()] - ) + let switch = + newStandardSwitch(addrs = @[MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet()]) await switch.start() await switch.start() diff --git a/tests/testtcptransport.nim b/tests/testtcptransport.nim index b3f974a9e4..ace133cbb8 100644 --- a/tests/testtcptransport.nim +++ b/tests/testtcptransport.nim @@ -10,14 +10,17 @@ # those terms. import chronos, stew/byteutils -import ../libp2p/[stream/connection, - transports/transport, - transports/tcptransport, - upgrademngrs/upgrade, - multiaddress, - multicodec, - errors, - wire] +import + ../libp2p/[ + stream/connection, + transports/transport, + transports/tcptransport, + upgrademngrs/upgrade, + multiaddress, + multicodec, + errors, + wire, + ] import ./helpers, ./commontransport @@ -72,8 +75,7 @@ suite "TCP transport": asyncTest "test dialer: handle write": let address = initTAddress("0.0.0.0:0") let handlerWait = newFuture[void]() - proc serveClient(server: StreamServer, - transp: StreamTransport) {.async.} = + proc serveClient(server: StreamServer, transp: StreamTransport) {.async.} = var wstream = newAsyncStreamWriter(transp) await wstream.write("Hello!") await wstream.finish() @@ -105,8 +107,7 @@ suite "TCP transport": asyncTest "test dialer: handle write": let address = initTAddress("0.0.0.0:0") let handlerWait = newFuture[void]() - proc serveClient(server: StreamServer, - transp: StreamTransport) {.async.} = + proc serveClient(server: StreamServer, transp: StreamTransport) {.async.} = var rstream = newAsyncStreamReader(transp) let msg = await rstream.read(6) check string.fromBytes(msg) == "Hello!" @@ -135,8 +136,11 @@ suite "TCP transport": await server.join() asyncTest "Starting with duplicate but zero ports addresses must NOT fail": - let ma = @[MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet(), - MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet()] + let ma = + @[ + MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet(), + MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet(), + ] let transport: TcpTransport = TcpTransport.new(upgrade = Upgrade()) @@ -170,13 +174,15 @@ suite "TCP transport": let acceptedPort2 = acceptedConn2.observedAddr.get()[multiCodec("tcp")].get() check listeningPort == acceptedPort2 - await allFutures(transport.stop(), transport2.stop(), transport3.stop()) + await allFutures(transport.stop(), transport2.stop(), transport3.stop()) - proc transProvider(): Transport = TcpTransport.new(upgrade = Upgrade()) + proc transProvider(): Transport = + TcpTransport.new(upgrade = Upgrade()) asyncTest "Custom timeout": let ma = @[MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet()] - let transport: TcpTransport = TcpTransport.new(upgrade = Upgrade(), connectionsTimeout=1.milliseconds) + let transport: TcpTransport = + TcpTransport.new(upgrade = Upgrade(), connectionsTimeout = 1.milliseconds) asyncSpawn transport.start(ma) proc acceptHandler() {.async.} = @@ -190,7 +196,4 @@ suite "TCP transport": await streamTransport.closeWait() await transport.stop() - - commonTransportTest( - transProvider, - "/ip4/0.0.0.0/tcp/0") + commonTransportTest(transProvider, "/ip4/0.0.0.0/tcp/0") diff --git a/tests/testtortransport.nim b/tests/testtortransport.nim index 7b4af7adaa..3c149543ba 100644 --- a/tests/testtortransport.nim +++ b/tests/testtortransport.nim @@ -13,12 +13,15 @@ import tables import chronos, stew/[byteutils] -import ../libp2p/[stream/connection, - transports/tcptransport, - transports/tortransport, - upgrademngrs/upgrade, - multiaddress, - builders] +import + ../libp2p/[ + stream/connection, + transports/tcptransport, + transports/tortransport, + upgrademngrs/upgrade, + multiaddress, + builders, + ] import ./helpers, ./stubs/torstub, ./commontransport @@ -31,8 +34,14 @@ suite "Tor transport": stub.registerAddr("127.0.0.1:8080", "/ip4/127.0.0.1/tcp/8080") stub.registerAddr("libp2p.nim:8080", "/ip4/127.0.0.1/tcp/8080") stub.registerAddr("::1:8080", "/ip6/::1/tcp/8080") - stub.registerAddr("a2mncbqsbullu7thgm4e6zxda2xccmcgzmaq44oayhdtm6rav5vovcad.onion:80", "/ip4/127.0.0.1/tcp/8080") - stub.registerAddr("a2mncbqsbullu7thgm4e6zxda2xccmcgzmaq44oayhdtm6rav5vovcae.onion:81", "/ip4/127.0.0.1/tcp/8081") + stub.registerAddr( + "a2mncbqsbullu7thgm4e6zxda2xccmcgzmaq44oayhdtm6rav5vovcad.onion:80", + "/ip4/127.0.0.1/tcp/8080", + ) + stub.registerAddr( + "a2mncbqsbullu7thgm4e6zxda2xccmcgzmaq44oayhdtm6rav5vovcae.onion:81", + "/ip4/127.0.0.1/tcp/8081", + ) startFut = stub.start(torServer) teardown: waitFor startFut.cancelAndWait() @@ -81,11 +90,9 @@ suite "Tor transport": asyncTest "test start and dial usion onion3 and builder": const TestCodec = "/test/proto/1.0.0" # custom protocol string identifier - type - TestProto = ref object of LPProtocol # declare a custom protocol + type TestProto = ref object of LPProtocol # declare a custom protocol proc new(T: typedesc[TestProto]): T = - # every incoming connections will be in handled in this closure proc handle(conn: Connection, proto: string) {.async.} = var resp: array[6, byte] @@ -100,7 +107,11 @@ suite "Tor transport": let rng = newRng() - let ma = MultiAddress.init("/ip4/127.0.0.1/tcp/8080/onion3/a2mncbqsbullu7thgm4e6zxda2xccmcgzmaq44oayhdtm6rav5vovcad:80").tryGet() + let ma = MultiAddress + .init( + "/ip4/127.0.0.1/tcp/8080/onion3/a2mncbqsbullu7thgm4e6zxda2xccmcgzmaq44oayhdtm6rav5vovcad:80" + ) + .tryGet() let serverSwitch = TorSwitch.new(torServer, rng, @[ma], {ReuseAddr}) @@ -114,7 +125,8 @@ suite "Tor transport": let serverAddress = serverSwitch.peerInfo.addrs proc startClient() {.async.} = - let clientSwitch = TorSwitch.new(torServer = torServer, rng= rng, flags = {ReuseAddr}) + let clientSwitch = + TorSwitch.new(torServer = torServer, rng = rng, flags = {ReuseAddr}) let conn = await clientSwitch.dial(serverPeerId, serverAddress, TestCodec) @@ -131,15 +143,16 @@ suite "Tor transport": await serverSwitch.stop() test "It's not possible to add another transport in TorSwitch": - let torSwitch = TorSwitch.new(torServer = torServer, rng= rng, flags = {ReuseAddr}) + let torSwitch = TorSwitch.new(torServer = torServer, rng = rng, flags = {ReuseAddr}) expect(AssertionDefect): torSwitch.addTransport(TcpTransport.new(upgrade = Upgrade())) waitFor torSwitch.stop() proc transProvider(): Transport = - TorTransport.new(torServer, {ReuseAddr}, Upgrade()) + TorTransport.new(torServer, {ReuseAddr}, Upgrade()) commonTransportTest( transProvider, "/ip4/127.0.0.1/tcp/8080/onion3/a2mncbqsbullu7thgm4e6zxda2xccmcgzmaq44oayhdtm6rav5vovcad:80", - "/ip4/127.0.0.1/tcp/8081/onion3/a2mncbqsbullu7thgm4e6zxda2xccmcgzmaq44oayhdtm6rav5vovcae:81") + "/ip4/127.0.0.1/tcp/8081/onion3/a2mncbqsbullu7thgm4e6zxda2xccmcgzmaq44oayhdtm6rav5vovcae:81", + ) diff --git a/tests/testutility.nim b/tests/testutility.nim index 0e7abc8e59..3f8dca999c 100644 --- a/tests/testutility.nim +++ b/tests/testutility.nim @@ -1,3 +1,4 @@ +#!fmt: off Disable formatting for this file https://github.com/arnetheduck/nph/issues/69 {.used.} # Nim-Libp2p diff --git a/tests/testvarint.nim b/tests/testvarint.nim index 4af6fb201d..04cf2e128d 100644 --- a/tests/testvarint.nim +++ b/tests/testvarint.nim @@ -13,155 +13,125 @@ import unittest2 import ../libp2p/varint const PBedgeValues = [ - 0'u64, (1'u64 shl 7) - 1'u64, - (1'u64 shl 7), (1'u64 shl 14) - 1'u64, - (1'u64 shl 14), (1'u64 shl 21) - 1'u64, - (1'u64 shl 21), (1'u64 shl 28) - 1'u64, - (1'u64 shl 28), (1'u64 shl 35) - 1'u64, - (1'u64 shl 35), (1'u64 shl 42) - 1'u64, - (1'u64 shl 42), (1'u64 shl 49) - 1'u64, - (1'u64 shl 49), (1'u64 shl 56) - 1'u64, - (1'u64 shl 56), (1'u64 shl 63) - 1'u64, - (1'u64 shl 63), 0xFFFF_FFFF_FFFF_FFFF'u64 + 0'u64, + (1'u64 shl 7) - 1'u64, + (1'u64 shl 7), + (1'u64 shl 14) - 1'u64, + (1'u64 shl 14), + (1'u64 shl 21) - 1'u64, + (1'u64 shl 21), + (1'u64 shl 28) - 1'u64, + (1'u64 shl 28), + (1'u64 shl 35) - 1'u64, + (1'u64 shl 35), + (1'u64 shl 42) - 1'u64, + (1'u64 shl 42), + (1'u64 shl 49) - 1'u64, + (1'u64 shl 49), + (1'u64 shl 56) - 1'u64, + (1'u64 shl 56), + (1'u64 shl 63) - 1'u64, + (1'u64 shl 63), + 0xFFFF_FFFF_FFFF_FFFF'u64, ] const PBPositiveSignedEdgeValues = [ - 0'u64, 0x3F'u64, - 0x40'u64, 0x1FFF'u64, - 0x2000'u64, 0xFFFFF'u64, - 0x100000'u64, 0x7FFFFFF'u64, - 0x8000000'u64, 0x3FFFFFFFF'u64, - 0x400000000'u64, 0x1FFFFFFFFFF'u64, - 0x20000000000'u64, 0xFFFFFFFFFFFF'u64, - 0x1000000000000'u64, 0x7FFFFFFFFFFFFF'u64, - 0x80000000000000'u64, 0x3FFFFFFFFFFFFFFF'u64, - 0x4000000000000000'u64, 0x7FFFFFFFFFFFFFFF'u64 + 0'u64, 0x3F'u64, 0x40'u64, 0x1FFF'u64, 0x2000'u64, 0xFFFFF'u64, 0x100000'u64, + 0x7FFFFFF'u64, 0x8000000'u64, 0x3FFFFFFFF'u64, 0x400000000'u64, 0x1FFFFFFFFFF'u64, + 0x20000000000'u64, 0xFFFFFFFFFFFF'u64, 0x1000000000000'u64, 0x7FFFFFFFFFFFFF'u64, + 0x80000000000000'u64, 0x3FFFFFFFFFFFFFFF'u64, 0x4000000000000000'u64, + 0x7FFFFFFFFFFFFFFF'u64, ] const PBNegativeSignedEdgeValues = [ - 0x0000000000000000'u64, 0xFFFFFFFFFFFFFFC0'u64, - 0xFFFFFFFFFFFFFFBF'u64, 0xFFFFFFFFFFFFE000'u64, - 0xFFFFFFFFFFFFDFFF'u64, 0xFFFFFFFFFFF00000'u64, - 0xFFFFFFFFFFEFFFFF'u64, 0xFFFFFFFFF8000000'u64, - 0xFFFFFFFFF7FFFFFF'u64, 0xFFFFFFFC00000000'u64, - 0xFFFFFFFBFFFFFFFF'u64, 0xFFFFFE0000000000'u64, - 0xFFFFFDFFFFFFFFFF'u64, 0xFFFF000000000000'u64, - 0xFFFEFFFFFFFFFFFF'u64, 0xFF80000000000000'u64, - 0xFF7FFFFFFFFFFFFF'u64, 0xC000000000000000'u64, - 0xBFFFFFFFFFFFFFFF'u64, 0x8000000000000000'u64 + 0x0000000000000000'u64, 0xFFFFFFFFFFFFFFC0'u64, 0xFFFFFFFFFFFFFFBF'u64, + 0xFFFFFFFFFFFFE000'u64, 0xFFFFFFFFFFFFDFFF'u64, 0xFFFFFFFFFFF00000'u64, + 0xFFFFFFFFFFEFFFFF'u64, 0xFFFFFFFFF8000000'u64, 0xFFFFFFFFF7FFFFFF'u64, + 0xFFFFFFFC00000000'u64, 0xFFFFFFFBFFFFFFFF'u64, 0xFFFFFE0000000000'u64, + 0xFFFFFDFFFFFFFFFF'u64, 0xFFFF000000000000'u64, 0xFFFEFFFFFFFFFFFF'u64, + 0xFF80000000000000'u64, 0xFF7FFFFFFFFFFFFF'u64, 0xC000000000000000'u64, + 0xBFFFFFFFFFFFFFFF'u64, 0x8000000000000000'u64, ] const PBPositiveSignedZigZagEdgeExpects = [ - "00", "7E", - "8001", "FE7F", - "808001", "FEFF7F", - "80808001", "FEFFFF7F", - "8080808001", "FEFFFFFF7F", - "808080808001", "FEFFFFFFFF7F", - "80808080808001", "FEFFFFFFFFFF7F", - "8080808080808001", "FEFFFFFFFFFFFF7F", - "808080808080808001", "FEFFFFFFFFFFFFFF7F", - "80808080808080808001", "FEFFFFFFFFFFFFFFFF01" + "00", "7E", "8001", "FE7F", "808001", "FEFF7F", "80808001", "FEFFFF7F", "8080808001", + "FEFFFFFF7F", "808080808001", "FEFFFFFFFF7F", "80808080808001", "FEFFFFFFFFFF7F", + "8080808080808001", "FEFFFFFFFFFFFF7F", "808080808080808001", "FEFFFFFFFFFFFFFF7F", + "80808080808080808001", "FEFFFFFFFFFFFFFFFF01", ] const PBNegativeSignedZigZagEdgeExpects = [ - "00", "7F", - "8101", "FF7F", - "818001", "FFFF7F", - "81808001", "FFFFFF7F", - "8180808001", "FFFFFFFF7F", - "818080808001", "FFFFFFFFFF7F", - "81808080808001", "FFFFFFFFFFFF7F", - "8180808080808001", "FFFFFFFFFFFFFF7F", - "818080808080808001", "FFFFFFFFFFFFFFFF7F", + "00", "7F", "8101", "FF7F", "818001", "FFFF7F", "81808001", "FFFFFF7F", "8180808001", + "FFFFFFFF7F", "818080808001", "FFFFFFFFFF7F", "81808080808001", "FFFFFFFFFFFF7F", + "8180808080808001", "FFFFFFFFFFFFFF7F", "818080808080808001", "FFFFFFFFFFFFFFFF7F", "81808080808080808001", "FFFFFFFFFFFFFFFFFF01", ] const PBPositiveSignedEdgeExpects = [ - "00", "3F", - "40", "FF3F", - "8040", "FFFF3F", - "808040", "FFFFFF3F", - "80808040", "FFFFFFFF3F", - "8080808040", "FFFFFFFFFF3F", - "808080808040", "FFFFFFFFFFFF3F", - "80808080808040", "FFFFFFFFFFFFFF3F", - "8080808080808040", "FFFFFFFFFFFFFFFF3F", - "808080808080808040", "FFFFFFFFFFFFFFFF7F" + "00", "3F", "40", "FF3F", "8040", "FFFF3F", "808040", "FFFFFF3F", "80808040", + "FFFFFFFF3F", "8080808040", "FFFFFFFFFF3F", "808080808040", "FFFFFFFFFFFF3F", + "80808080808040", "FFFFFFFFFFFFFF3F", "8080808080808040", "FFFFFFFFFFFFFFFF3F", + "808080808080808040", "FFFFFFFFFFFFFFFF7F", ] const PBNegativeSignedEdgeExpects = [ - "00", "C0FFFFFFFFFFFFFFFF01", - "BFFFFFFFFFFFFFFFFF01", "80C0FFFFFFFFFFFFFF01", - "FFBFFFFFFFFFFFFFFF01", "8080C0FFFFFFFFFFFF01", - "FFFFBFFFFFFFFFFFFF01", "808080C0FFFFFFFFFF01", - "FFFFFFBFFFFFFFFFFF01", "80808080C0FFFFFFFF01", - "FFFFFFFFBFFFFFFFFF01", "8080808080C0FFFFFF01", - "FFFFFFFFFFBFFFFFFF01", "808080808080C0FFFF01", - "FFFFFFFFFFFFBFFFFF01", "80808080808080C0FF01", - "FFFFFFFFFFFFFFBFFF01", "8080808080808080C001", - "FFFFFFFFFFFFFFFFBF01", "80808080808080808001" + "00", "C0FFFFFFFFFFFFFFFF01", "BFFFFFFFFFFFFFFFFF01", "80C0FFFFFFFFFFFFFF01", + "FFBFFFFFFFFFFFFFFF01", "8080C0FFFFFFFFFFFF01", "FFFFBFFFFFFFFFFFFF01", + "808080C0FFFFFFFFFF01", "FFFFFFBFFFFFFFFFFF01", "80808080C0FFFFFFFF01", + "FFFFFFFFBFFFFFFFFF01", "8080808080C0FFFFFF01", "FFFFFFFFFFBFFFFFFF01", + "808080808080C0FFFF01", "FFFFFFFFFFFFBFFFFF01", "80808080808080C0FF01", + "FFFFFFFFFFFFFFBFFF01", "8080808080808080C001", "FFFFFFFFFFFFFFFFBF01", + "80808080808080808001", ] const PBedgeExpects = [ - "00", "7F", - "8001", "FF7F", - "808001", "FFFF7F", - "80808001", "FFFFFF7F", - "8080808001", "FFFFFFFF7F", - "808080808001", "FFFFFFFFFF7F", - "80808080808001", "FFFFFFFFFFFF7F", - "8080808080808001", "FFFFFFFFFFFFFF7F", - "808080808080808001", "FFFFFFFFFFFFFFFF7F", - "80808080808080808001", "FFFFFFFFFFFFFFFFFF01" + "00", "7F", "8001", "FF7F", "808001", "FFFF7F", "80808001", "FFFFFF7F", "8080808001", + "FFFFFFFF7F", "808080808001", "FFFFFFFFFF7F", "80808080808001", "FFFFFFFFFFFF7F", + "8080808080808001", "FFFFFFFFFFFFFF7F", "808080808080808001", "FFFFFFFFFFFFFFFF7F", + "80808080808080808001", "FFFFFFFFFFFFFFFFFF01", ] -const PBedgeSizes = [ - 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10 -] +const PBedgeSizes = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10] -const PBEdgeSignedPositiveZigZagSizes = [ - 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10 -] +const PBEdgeSignedPositiveZigZagSizes = + [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10] -const PBEdgeSignedNegativeZigZagSizes = [ - 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10 -] +const PBEdgeSignedNegativeZigZagSizes = + [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10] -const PBEdgeSignedPositiveSizes = [ - 1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 9 -] +const PBEdgeSignedPositiveSizes = + [1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 9] -const PBEdgeSignedNegativeSizes = [ - 1, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 -] +const PBEdgeSignedNegativeSizes = + [1, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10] const LPedgeValues = [ - 0'u64, (1'u64 shl 7) - 1'u64, - (1'u64 shl 7), (1'u64 shl 14) - 1'u64, - (1'u64 shl 14), (1'u64 shl 21) - 1'u64, - (1'u64 shl 21), (1'u64 shl 28) - 1'u64, - (1'u64 shl 28), (1'u64 shl 35) - 1'u64, - (1'u64 shl 35), (1'u64 shl 42) - 1'u64, - (1'u64 shl 42), (1'u64 shl 49) - 1'u64, - (1'u64 shl 49), (1'u64 shl 56) - 1'u64, - (1'u64 shl 56), (1'u64 shl 63) - 1'u64, + 0'u64, + (1'u64 shl 7) - 1'u64, + (1'u64 shl 7), + (1'u64 shl 14) - 1'u64, + (1'u64 shl 14), + (1'u64 shl 21) - 1'u64, + (1'u64 shl 21), + (1'u64 shl 28) - 1'u64, + (1'u64 shl 28), + (1'u64 shl 35) - 1'u64, + (1'u64 shl 35), + (1'u64 shl 42) - 1'u64, + (1'u64 shl 42), + (1'u64 shl 49) - 1'u64, + (1'u64 shl 49), + (1'u64 shl 56) - 1'u64, + (1'u64 shl 56), + (1'u64 shl 63) - 1'u64, ] -const LPedgeSizes = [ - 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9 -] +const LPedgeSizes = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9] const LPedgeExpects = [ - "00", "7F", - "8001", "FF7F", - "808001", "FFFF7F", - "80808001", "FFFFFF7F", - "8080808001", "FFFFFFFF7F", - "808080808001", "FFFFFFFFFF7F", - "80808080808001", "FFFFFFFFFFFF7F", - "8080808080808001", "FFFFFFFFFFFFFF7F", - "808080808080808001", "FFFFFFFFFFFFFFFF7F", + "00", "7F", "8001", "FF7F", "808001", "FFFF7F", "80808001", "FFFFFF7F", "8080808001", + "FFFFFFFF7F", "808080808001", "FFFFFFFFFF7F", "80808080808001", "FFFFFFFFFFFF7F", + "8080808080808001", "FFFFFFFFFFFFFF7F", "808080808080808001", "FFFFFFFFFFFFFFFF7F", ] proc hexChar*(c: byte, lowercase: bool = false): string = @@ -174,11 +144,15 @@ proc hexChar*(c: byte, lowercase: bool = false): string = let t1 = ord(c) shr 4 let t0 = ord(c) and 0x0F case t1 - of 0..9: result[0] = chr(t1 + ord('0')) - else: result[0] = chr(t1 - 10 + alpha) - case t0: - of 0..9: result[1] = chr(t0 + ord('0')) - else: result[1] = chr(t0 - 10 + alpha) + of 0 .. 9: + result[0] = chr(t1 + ord('0')) + else: + result[0] = chr(t1 - 10 + alpha) + case t0 + of 0 .. 9: + result[1] = chr(t0 + ord('0')) + else: + result[1] = chr(t0 - 10 + alpha) proc toHex*(a: openArray[byte], lowercase: bool = false): string = result = "" @@ -186,15 +160,13 @@ proc toHex*(a: openArray[byte], lowercase: bool = false): string = result = result & hexChar(i, lowercase) suite "Variable integer test suite": - test "vsizeof() edge cases test": for i in 0 ..< len(PBedgeValues): check vsizeof(PBedgeValues[i]) == PBedgeSizes[i] for i in 0 ..< len(PBPositiveSignedEdgeValues): check: - vsizeof(hint64(PBPositiveSignedEdgeValues[i])) == - PBEdgeSignedPositiveSizes[i] + vsizeof(hint64(PBPositiveSignedEdgeValues[i])) == PBEdgeSignedPositiveSizes[i] vsizeof(zint64(PBPositiveSignedEdgeValues[i])) == PBEdgeSignedPositiveZigZagSizes[i] @@ -222,16 +194,14 @@ suite "Variable integer test suite": for i in 0 ..< len(PBPositiveSignedEdgeValues): buffer.setLen(PBEdgeSignedPositiveSizes[i]) check: - putSVarint(buffer, length, - hint64(PBPositiveSignedEdgeValues[i])).isOk() + putSVarint(buffer, length, hint64(PBPositiveSignedEdgeValues[i])).isOk() getSVarint(buffer, length, ivalue).isOk() int64(ivalue) == int64(PBPositiveSignedEdgeValues[i]) toHex(buffer) == PBPositiveSignedEdgeExpects[i] buffer.setLen(PBEdgeSignedPositiveZigZagSizes[i]) check: - putSVarint(buffer, length, - zint64(PBPositiveSignedEdgeValues[i])).isOk() + putSVarint(buffer, length, zint64(PBPositiveSignedEdgeValues[i])).isOk() getSVarint(buffer, length, svalue).isOk() int64(svalue) == int64(PBPositiveSignedEdgeValues[i]) toHex(buffer) == PBPositiveSignedZigZagEdgeExpects[i] @@ -239,16 +209,14 @@ suite "Variable integer test suite": for i in 0 ..< len(PBNegativeSignedEdgeValues): buffer.setLen(PBEdgeSignedNegativeSizes[i]) check: - putSVarint(buffer, length, - cast[hint64](PBNegativeSignedEdgeValues[i])).isOk() + putSVarint(buffer, length, cast[hint64](PBNegativeSignedEdgeValues[i])).isOk() getSVarint(buffer, length, ivalue).isOk() int64(ivalue) == cast[int64](PBNegativeSignedEdgeValues[i]) toHex(buffer) == PBNegativeSignedEdgeExpects[i] buffer.setLen(PBEdgeSignedNegativeZigZagSizes[i]) check: - putSVarint(buffer, length, - cast[zint64](PBNegativeSignedEdgeValues[i])).isOk() + putSVarint(buffer, length, cast[zint64](PBNegativeSignedEdgeValues[i])).isOk() getSVarint(buffer, length, svalue).isOk() int64(svalue) == cast[int64](PBNegativeSignedEdgeValues[i]) @@ -257,7 +225,7 @@ suite "Variable integer test suite": test "[ProtoBuf] Buffer Overrun edge cases test": var buffer = newSeq[byte]() var length = 0 - for i in 0.. 5: var value = 0'u32 buffer.setLen(PBedgeSizes[i]) @@ -290,7 +258,7 @@ suite "Variable integer test suite": test "[ProtoBuf] Integer Overflow 64bit test": var buffer = newSeq[byte]() var length = 0 - for i in 0.. 9: var value = 0'u64 buffer.setLen(PBedgeSizes[i] + 1) @@ -313,7 +281,7 @@ suite "Variable integer test suite": PB.encodeVarint(zint64(0)).get() == @[0x00'u8] PB.encodeVarint(zint32(0)).get() == @[0x00'u8] PB.encodeVarint(zint32(-1)).get() == PB.encodeVarint(1'u32).get() - PB.encodeVarint(zint64(150)).get( ) == PB.encodeVarint(300'u32).get() + PB.encodeVarint(zint64(150)).get() == PB.encodeVarint(300'u32).get() PB.encodeVarint(zint64(-150)).get() == PB.encodeVarint(299'u32).get() PB.encodeVarint(zint32(-2147483648)).get() == PB.encodeVarint(4294967295'u64).get() PB.encodeVarint(zint32(2147483647)).get() == PB.encodeVarint(4294967294'u64).get() @@ -322,7 +290,7 @@ suite "Variable integer test suite": var buffer = newSeq[byte]() var length = 0 var value = 0'u64 - for i in 0.. 5: var value = 0'u32 buffer.setLen(LPedgeSizes[i]) @@ -366,7 +334,7 @@ suite "Variable integer test suite": test "[LibP2P] Integer Overflow 64bit test": var buffer = newSeq[byte]() var length = 0 - for i in 0.. 8: var value = 0'u64 buffer.setLen(LPedgeSizes[i] + 1) @@ -381,12 +349,13 @@ suite "Variable integer test suite": var buffer = newSeq[byte](10) var length = 0 check: - LP.putUVarint(buffer, length, - 0x7FFF_FFFF_FFFF_FFFF'u64).isOk() - LP.putUVarint(buffer, length, - 0x8000_0000_0000_0000'u64).error() == VarintError.Overflow - LP.putUVarint(buffer, length, - 0xFFFF_FFFF_FFFF_FFFF'u64).error() == VarintError.Overflow + LP.putUVarint(buffer, length, 0x7FFF_FFFF_FFFF_FFFF'u64).isOk() + + LP.putUVarint(buffer, length, 0x8000_0000_0000_0000'u64).error() == + VarintError.Overflow + + LP.putUVarint(buffer, length, 0xFFFF_FFFF_FFFF_FFFF'u64).error() == + VarintError.Overflow test "[LibP2P] Overlong values test": const OverlongValues = [ @@ -397,26 +366,20 @@ suite "Variable integer test suite": @[0x81'u8, 0x80'u8, 0x80'u8, 0x80'u8, 0x00'u8], @[0x81'u8, 0x80'u8, 0x80'u8, 0x80'u8, 0x80'u8, 0x00'u8], @[0x81'u8, 0x80'u8, 0x80'u8, 0x80'u8, 0x80'u8, 0x80'u8, 0x00'u8], - @[0x81'u8, 0x80'u8, 0x80'u8, 0x80'u8, 0x80'u8, 0x80'u8, 0x80'u8, - 0x00'u8], - @[0x81'u8, 0x80'u8, 0x80'u8, 0x80'u8, 0x80'u8, 0x80'u8, 0x80'u8, - 0x80'u8, 0x00'u8], + @[0x81'u8, 0x80'u8, 0x80'u8, 0x80'u8, 0x80'u8, 0x80'u8, 0x80'u8, 0x00'u8], + @[0x81'u8, 0x80'u8, 0x80'u8, 0x80'u8, 0x80'u8, 0x80'u8, 0x80'u8, 0x80'u8, 0x00'u8], # Zero bytes at the middle and zero byte at the end @[0x81'u8, 0x80'u8, 0x81'u8, 0x00'u8], @[0x81'u8, 0x80'u8, 0x80'u8, 0x81'u8, 0x00'u8], @[0x81'u8, 0x80'u8, 0x80'u8, 0x80'u8, 0x81'u8, 0x00'u8], @[0x81'u8, 0x80'u8, 0x80'u8, 0x80'u8, 0x80'u8, 0x81'u8, 0x00'u8], - @[0x81'u8, 0x80'u8, 0x80'u8, 0x80'u8, 0x80'u8, 0x80'u8, 0x81'u8, - 0x00'u8], - @[0x81'u8, 0x80'u8, 0x80'u8, 0x80'u8, 0x80'u8, 0x80'u8, 0x80'u8, - 0x81'u8, 0x00'u8], + @[0x81'u8, 0x80'u8, 0x80'u8, 0x80'u8, 0x80'u8, 0x80'u8, 0x81'u8, 0x00'u8], + @[0x81'u8, 0x80'u8, 0x80'u8, 0x80'u8, 0x80'u8, 0x80'u8, 0x80'u8, 0x81'u8, 0x00'u8], # Zero bytes at the middle and zero bytes at the end @[0x81'u8, 0x80'u8, 0x80'u8, 0x81'u8, 0x80'u8, 0x00'u8], @[0x81'u8, 0x80'u8, 0x80'u8, 0x81'u8, 0x80'u8, 0x80'u8, 0x00'u8], - @[0x81'u8, 0x80'u8, 0x80'u8, 0x80'u8, 0x81'u8, 0x80'u8, 0x80'u8, - 0x00'u8], - @[0x81'u8, 0x80'u8, 0x80'u8, 0x80'u8, 0x80'u8, 0x81'u8, 0x80'u8, - 0x80'u8, 0x00'u8], + @[0x81'u8, 0x80'u8, 0x80'u8, 0x80'u8, 0x81'u8, 0x80'u8, 0x80'u8, 0x00'u8], + @[0x81'u8, 0x80'u8, 0x80'u8, 0x80'u8, 0x80'u8, 0x81'u8, 0x80'u8, 0x80'u8, 0x00'u8], ] var length = 0 var value = 0'u64 @@ -440,9 +403,10 @@ suite "Variable integer test suite": value == 0 test "getVarint/putVarint tests": - proc `==`(a, b: zint32|hint32): bool = + proc `==`(a, b: zint32 | hint32): bool = int32(a) == int32(b) - proc `==`(a, b: zint64|hint64): bool = + + proc `==`(a, b: zint64 | hint64): bool = int64(a) == int64(b) template varintTest(ttype, vtype, value, expect: untyped) = diff --git a/tests/testwildcardresolverservice.nim b/tests/testwildcardresolverservice.nim index e9c40cd4d9..268dc09a18 100644 --- a/tests/testwildcardresolverservice.nim +++ b/tests/testwildcardresolverservice.nim @@ -48,7 +48,7 @@ proc createSwitch(svc: Service): Switch = MultiAddress.init("/ip4/0.0.0.0/tcp/0/").tryGet(), MultiAddress.init("/ip6/::/tcp/0/").tryGet(), ], - false + false, ) .withTcpTransport() .withMplex() diff --git a/tests/testwire.nim b/tests/testwire.nim index 6c1f674b40..c76503c80f 100644 --- a/tests/testwire.nim +++ b/tests/testwire.nim @@ -14,18 +14,15 @@ import ../libp2p/multiaddress import ../libp2p/wire suite "Wire": - test "initTAddress returns ok and correct result for a Unix domain address": let ma = MultiAddress.init("/unix/tmp/socket").get() let result = initTAddress(ma) var address_un: array[108, uint8] let unixPath = "/tmp/socket" - for i in 0.. 3: stripped[3..^1] - else: "") + echo( + if stripped.len > 3: + stripped[3 ..^ 1] + else: + "" + ) else: code &= line if code.strip.len > 0: diff --git a/tools/markdown_runner.nim b/tools/markdown_runner.nim index 1ed2b1e895..648bf69cb8 100644 --- a/tools/markdown_runner.nim +++ b/tools/markdown_runner.nim @@ -12,7 +12,8 @@ const startDelim = "```nim\n" const endDelim = "\n```" while true: let startOfBlock = contents.find(startDelim, start = index) - if startOfBlock == -1: break + if startOfBlock == -1: + break let endOfBlock = contents.find(endDelim, start = startOfBlock + startDelim.len) if endOfBlock == -1: diff --git a/tools/pbcap_parser.nim b/tools/pbcap_parser.nim index 83e7f48c8c..5b98032024 100644 --- a/tools/pbcap_parser.nim +++ b/tools/pbcap_parser.nim @@ -13,23 +13,19 @@ import ../libp2p/debugutils const PBCapParserName* = "PBCapParser" ## project name string - PBCapParserMajor*: int = 0 ## is the major number of PBCapParser' version. - PBCapParserMinor*: int = 0 ## is the minor number of PBCapParser' version. - PBCapParserPatch*: int = 1 ## is the patch number of PBCapParser' version. - - PBCapParserVersion* = $PBCapParserMajor & "." & $PBCapParserMinor & "." & - $PBCapParserPatch + PBCapParserVersion* = + $PBCapParserMajor & "." & $PBCapParserMinor & "." & $PBCapParserPatch ## is the version of Nimbus as a string. - GitRevision = staticExec("git rev-parse --short HEAD").replace("\n") # remove CR - PBCapParserUsage = """ + PBCapParserUsage = + """ USAGE: pbcap_parser @@ -40,11 +36,13 @@ COMMANDS: """ let - PBCapParserCopyright* = "Copyright (c) 2020-" & $(now().utc.year) & - " Status Research & Development GmbH" - PBCapParserHeader* = "$# Version $# [$#: $#, $#]\p$#\p" % - [PBCapParserName, PBCapParserVersion, hostOS, hostCPU, GitRevision, - PBCapParserCopyright] + PBCapParserCopyright* = + "Copyright (c) 2020-" & $(now().utc.year) & " Status Research & Development GmbH" + PBCapParserHeader* = + "$# Version $# [$#: $#, $#]\p$#\p" % [ + PBCapParserName, PBCapParserVersion, hostOS, hostCPU, GitRevision, + PBCapParserCopyright, + ] proc parseFile*(pathName: string, dump: bool): string = ## Parse `pbcap` file ``pathName``, and return decoded output as string dump. @@ -82,7 +80,7 @@ when isMainModule: else: getHomeDir() / libp2p_dump_dir / paramStr(2) - if not(fileExists(path)): + if not (fileExists(path)): fatal "Could not find pbcap file", filename = path quit 1 diff --git a/tools/pinner.nim b/tools/pinner.nim index 3417454e3e..89b60e518a 100644 --- a/tools/pinner.nim +++ b/tools/pinner.nim @@ -14,14 +14,15 @@ discard execCmd("nimble install -dy") var allDeps: Table[string, string] let nimblePkgs = - if dirExists("nimbledeps/pkgs"): "nimbledeps/pkgs" - else: "nimbledeps/pkgs2" + if dirExists("nimbledeps/pkgs"): "nimbledeps/pkgs" else: "nimbledeps/pkgs2" for (_, dependency) in walkDir(nimblePkgs): let jsonContent = parseJson(readFile(dependency & "/nimblemeta.json")) fileContent = - if "metaData" in jsonContent: jsonContent["metaData"] - else: jsonContent + if "metaData" in jsonContent: + jsonContent["metaData"] + else: + jsonContent let url = fileContent.getOrDefault("url").getStr("") var version = fileContent.getOrDefault("vcsRevision").getStr("") var packageName = dependency.split('/')[^1].split('-')[0]