diff --git a/.assets/full-logo.svg b/.assets/full-logo.svg new file mode 100644 index 000000000..92be1a9fd --- /dev/null +++ b/.assets/full-logo.svg @@ -0,0 +1,1141 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.assets/small-logo.svg b/.assets/small-logo.svg new file mode 100644 index 000000000..621f3eaa9 --- /dev/null +++ b/.assets/small-logo.svg @@ -0,0 +1,96 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.github/workflows/doc.yml b/.github/workflows/doc.yml new file mode 100644 index 000000000..dca12d533 --- /dev/null +++ b/.github/workflows/doc.yml @@ -0,0 +1,63 @@ +name: Docgen +on: + push: + workflow_dispatch: + + +jobs: + build: + timeout-minutes: 20 + + name: 'Generate & upload documentation' + runs-on: 'ubuntu-20.04' + continue-on-error: true + steps: + - name: Checkout + uses: actions/checkout@v2 + with: + submodules: true + + - uses: jiro4989/setup-nim-action@v1 + with: + nim-version: 'stable' + + - name: Generate doc + run: | + nim --version + nimble --version + nimble install_pinned + # nim doc can "fail", but the doc is still generated + nim doc --git.url:https://github.com/status-im/nim-libp2p --git.commit:${GITHUB_REF##*/} --outdir:${GITHUB_REF##*/} --project libp2p || true + + # check that the folder exists + ls ${GITHUB_REF##*/} + + - name: Clone the gh-pages branch + uses: actions/checkout@v2 + with: + repository: status-im/nim-libp2p + ref: gh-pages + path: subdoc + submodules: true + fetch-depth: 0 + + - name: Commit & push + run: | + cd subdoc + + # Delete merged branches doc's + for branch in $(git branch -vv | grep ': gone]' | awk '{print $1}'); do rm -rf $branch; done + + # Update / create this branch doc + rm -rf ${GITHUB_REF##*/} + mv ../${GITHUB_REF##*/} . + + # Remove .idx files + # NOTE: git also uses idx files in his + # internal folder, hence the `*` instead of `.` + find * -name "*.idx" -delete + git add . + git config --global user.email "${{ github.actor }}@users.noreply.github.com" + git config --global user.name = "${{ github.actor }}" + git commit -a -m "update docs for ${GITHUB_REF##*/}" + git push origin gh-pages diff --git a/README.md b/README.md index f8dfec30c..b67b85bdf 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@

- libp2p hex logo + nim-libp2p logo

The Nim implementation of the libp2p Networking Stack.

@@ -18,28 +18,16 @@ ## Introduction -An implementation of [libp2p](https://libp2p.io/) in Nim. - -## Project Status -libp2p is used in production by a few projects at [Status](https://github.com/status-im), including [Nimbus](https://github.com/status-im/nimbus-eth2). - -While far from complete, currently available components are stable. - -Check our [examples folder](/examples) to get started! +An implementation of [libp2p](https://libp2p.io/) in [Nim](https://nim-lang.org/). # Table of Contents - [Background](#background) - [Install](#install) - - [Prerequisite](#prerequisite) -- [Usage](#usage) - - [API](#api) - - [Getting Started](#getting-started) - - [Tutorials and Examples](#tutorials-and-examples) - - [Using the Go Daemon](#using-the-go-daemon) +- [Getting Started](#getting-started) +- [Modules](#modules) +- [Users](#users) - [Development](#development) - - [Tests](#tests) - - [Packages](#packages) -- [Contribute](#contribute) + - [Contribute](#contribute) - [Core Developers](#core-developers) - [License](#license) @@ -54,33 +42,63 @@ libp2p grew out of IPFS, but it is built so that lots of people can use it, for - [Here](https://github.com/libp2p/libp2p#description) is an overview of libp2p and its implementations in other programming languages. ## Install +**Prerequisite** +- [Nim](https://nim-lang.org/install.html) ``` nimble install libp2p ``` -### Prerequisite -- [Nim](https://nim-lang.org/install.html) -## Usage +## Getting Started +You'll find some tutorials [here](examples/tutorial_1_connect.md), or some examples: +- [hello world (ping)](examples/helloworld.nim) +- [direct chat](examples/directchat.nim) -### API -The specification is available in the [docs/api](docs/api) folder. +**Go Daemon:** +Please find the installation and usage intructions in [daemonapi.md](examples/go-daemon/daemonapi.md). -### Getting Started -Please read the [GETTING_STARTED.md](docs/GETTING_STARTED.md) guide. +## Modules -### Tutorials and Examples -Example code can be found in the [examples folder](/examples). +List of packages modules implemented in nim-libp2p: -#### Direct Chat Tutorial -- [Part I](https://our.status.im/nim-libp2p-tutorial-a-peer-to-peer-chat-example-1/): Set up the main function and use multi-thread for processing IO. -- [Part II](https://our.status.im/nim-libp2p-tutorial-a-peer-to-peer-chat-example-2/): Dial remote peer and allow customized user input commands. -- [Part III](https://our.status.im/nim-libp2p-tutorial-a-peer-to-peer-chat-example-3/): Configure and establish a libp2p node. +| Name | Description | +| ---------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------- | +| **Libp2p** | | +| [libp2p](libp2p/switch.nim) | The core of the project | +| [connmanager](libp2p/connmanager.nim) | Connection manager | +| [identify / push identify](libp2p/protocols/identify.nim) | [Identify](https://docs.libp2p.io/concepts/protocols/#identify) protocol | +| [ping](libp2p/protocols/ping.nim) | [Ping](https://docs.libp2p.io/concepts/protocols/#ping) protocol | +| [libp2p-daemon-client](libp2p/daemon/daemonapi.nim) | [go-daemon](https://github.com/libp2p/go-libp2p-daemon) nim wrapper | +| [interop-libp2p](tests/testinterop.nim) | Interop tests | +| **Transports** | | +| [libp2p-tcp](libp2p/transports/tcptransport.nim) | TCP transport | +| [libp2p-ws](libp2p/transports/wstransport.nim) | WebSocket & WebSocket Secure transport | +| **Secure Channels** | | +| [libp2p-secio](libp2p/protocols/secure/secio.nim) | [Secio](https://docs.libp2p.io/concepts/protocols/#secio) secure channel | +| [libp2p-noise](libp2p/protocols/secure/noise.nim) | [Noise](https://github.com/libp2p/specs/tree/master/noise) secure channel | +| [libp2p-plaintext](libp2p/protocols/secure/plaintext.nim) | [Plain Text](https://github.com/libp2p/specs/tree/master/plaintext) for development purposes | +| **Stream Multiplexers** | | +| [libp2p-mplex](libp2p/muxers/mplex/mplex.nim) | [MPlex](https://github.com/libp2p/specs/tree/master/mplex) multiplexer | +| **Data Types** | | +| [peer-id](libp2p/peerid.nim) | [Cryptographic identifiers](https://docs.libp2p.io/concepts/peer-id/) | +| [peer-store](libp2p/peerstore.nim) | ["Phone book" of known peers](https://docs.libp2p.io/concepts/peer-id/#peerinfo) | +| [multiaddress](libp2p/multiaddress.nim) | [Composable network addresses](https://github.com/multiformats/multiaddr) | +| [signed envelope](libp2p/signed_envelope.nim) | [Signed generic data container](https://github.com/libp2p/specs/blob/master/RFC/0002-signed-envelopes.md) | +| [routing record](libp2p/routing_record.nim) | [Signed peer dialing informations](https://github.com/libp2p/specs/blob/master/RFC/0003-routing-records.md) | +| **Utilities** | | +| [libp2p-crypto](libp2p/crypto) | Cryptographic backend | +| [libp2p-crypto-secp256k1](libp2p/crypto/secp.nim) | | +| **Pubsub** | | +| [libp2p-pubsub](libp2p/protocols/pubsub/pubsub.nim) | Pub-Sub generic interface | +| [libp2p-floodsub](libp2p/protocols/pubsub/floodsub.nim) | FloodSub implementation | +| [libp2p-gossipsub](libp2p/protocols/pubsub/gossipsub.nim) | [GossipSub](https://docs.libp2p.io/concepts/publish-subscribe/) implementation | +## Users -### Using the Go Daemon -Please find the installation and usage intructions in [daemonapi.md](docs/api/libp2p/daemonapi.md). - -Examples can be found in the [examples/go-daemon folder](https://github.com/status-im/nim-libp2p/tree/readme/examples/go-daemon); +nim-libp2p is used by: +- [Nimbus](https://github.com/status-im/nimbus-eth2), an Ethereum client +- [nwaku](https://github.com/status-im/nwaku), a decentralized messaging application +- [nim-codex](https://github.com/status-im/nim-codex), a decentralized storage application +- (open a pull request if you want to be included here) ## Development **Clone and Install dependencies:** @@ -90,96 +108,47 @@ git clone https://github.com/status-im/nim-libp2p cd nim-libp2p nimble install ``` -#### Run unit tests + +**Run unit tests** ```sh # run all the unit tests nimble test ``` The code follows the [Status Nim Style Guide](https://status-im.github.io/nim-style-guide/). -### Packages -List of packages currently in existence for nim-libp2p: - -#### Libp2p -- [libp2p](https://github.com/status-im/nim-libp2p) -- [libp2p-daemon-client](https://github.com/status-im/nim-libp2p/blob/master/libp2p/daemon/daemonapi.nim) -- [interop-libp2p](https://github.com/status-im/nim-libp2p/blob/master/tests/testinterop.nim) - -#### Transports -- [libp2p-tcp](https://github.com/status-im/nim-libp2p/blob/master/libp2p/transports/tcptransport.nim) -- [libp2p-ws](https://github.com/status-im/nim-libp2p/blob/master/libp2p/transports/wstransport.nim) - -#### Secure Channels -- [libp2p-secio](https://github.com/status-im/nim-libp2p/blob/master/libp2p/protocols/secure/secio.nim) -- [libp2p-noise](https://github.com/status-im/nim-libp2p/blob/master/libp2p/protocols/secure/noise.nim) -- [libp2p-plaintext](https://github.com/status-im/nim-libp2p/blob/master/libp2p/protocols/secure/plaintext.nim) - -#### Stream Multiplexers -- [libp2p-mplex](https://github.com/status-im/nim-libp2p/blob/master/libp2p/muxers/mplex/mplex.nim) - -#### Utilities -- [libp2p-crypto](https://github.com/status-im/nim-libp2p/tree/master/libp2p/crypto) -- [libp2p-crypto-secp256k1](https://github.com/status-im/nim-libp2p/blob/master/libp2p/crypto/secp.nim) - -#### Data Types -- [peer-id](https://github.com/status-im/nim-libp2p/blob/master/libp2p/peer.nim) -- [peer-info](https://github.com/status-im/nim-libp2p/blob/master/libp2p/peerinfo.nim) - -#### Pubsub -- [libp2p-pubsub](https://github.com/status-im/nim-libp2p/blob/master/libp2p/protocols/pubsub/pubsub.nim) -- [libp2p-floodsub](https://github.com/status-im/nim-libp2p/blob/master/libp2p/protocols/pubsub/floodsub.nim) -- [libp2p-gossipsub](https://github.com/status-im/nim-libp2p/blob/master/libp2p/protocols/pubsub/gossipsub.nim) - - -Packages that exist in the original libp2p specs and are under active development: -- libp2p-daemon -- libp2p-webrtc-direct -- libp2p-webrtc-star -- libp2p-spdy -- libp2p-bootstrap -- libp2p-kad-dht -- libp2p-mdns -- libp2p-webrtc-star -- libp2p-delegated-content-routing -- libp2p-delegated-peer-routing -- libp2p-nat-mgnr -- libp2p-utils - -** Note that the current stack reflects the minimal requirements for the upcoming Eth2 implementation. - -### Tips and tricks - -#### enable expensive metrics: - -```bash -nim c -d:libp2p_expensive_metrics some_file.nim -``` - -#### use identify metrics - -```bash -nim c -d:libp2p_agents_metrics -d:KnownLibP2PAgents=nimbus,lighthouse,prysm,teku some_file.nim -``` - -### specify gossipsub specific topics to measure - -```bash -nim c -d:KnownLibP2PTopics=topic1,topic2,topic3 some_file.nim -``` - -## Contribute +### Contribute The libp2p implementation in Nim is a work in progress. We welcome contributors to help out! Specifically, you can: - Go through the modules and **check out existing issues**. This would be especially useful for modules in active development. Some knowledge of IPFS/libp2p may be required, as well as the infrastructure behind it. - **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](https://github.com/status-im/nim-libp2p/tree/master/tests). +- **Add tests**. Help nim-libp2p to be more robust by adding more tests to the [tests folder](tests/). The code follows the [Status Nim Style Guide](https://status-im.github.io/nim-style-guide/). ### Core Developers [@cheatfate](https://github.com/cheatfate), [Dmitriy Ryajov](https://github.com/dryajov), [Tanguy](https://github.com/Menduist), [Zahary Karadjov](https://github.com/zah) +### Tips and tricks + +**enable expensive metrics:** + +```bash +nim c -d:libp2p_expensive_metrics some_file.nim +``` + +**use identify metrics** + +```bash +nim c -d:libp2p_agents_metrics -d:KnownLibP2PAgents=nimbus,lighthouse,prysm,teku some_file.nim +``` + +**specify gossipsub specific topics to measure** + +```bash +nim c -d:KnownLibP2PTopics=topic1,topic2,topic3 some_file.nim +``` + ## License Licensed and distributed under either of @@ -191,4 +160,3 @@ or * Apache License, Version 2.0, ([LICENSE-APACHEv2](LICENSE-APACHEv2) or http://www.apache.org/licenses/LICENSE-2.0) at your option. These files may not be copied, modified, or distributed except according to those terms. - diff --git a/docs/API.md b/docs/API.md deleted file mode 100644 index 3af9f1c0e..000000000 --- a/docs/API.md +++ /dev/null @@ -1,3 +0,0 @@ -# API - -Coming Soon... \ No newline at end of file diff --git a/docs/GETTING_STARTED.md b/docs/GETTING_STARTED.md deleted file mode 100644 index a0cb72a1a..000000000 --- a/docs/GETTING_STARTED.md +++ /dev/null @@ -1,7 +0,0 @@ -# Getting Started -Welcome to nim-libp2p! - - -To get started, please look at the [tutorials](../examples/tutorial_1_connect.md) - -For more concrete examples, you can look at the [hello world example](../examples/helloworld.nim) or the [direct chat](../examples/directchat.nim) diff --git a/docs/README.md b/docs/README.md deleted file mode 100644 index f6a82fbd2..000000000 --- a/docs/README.md +++ /dev/null @@ -1,29 +0,0 @@ -# Introduction -This folder contains the documentation for each nim-libp2p module and the sample code for the tutorials. - -# Table of Contents -### [Getting Started](GETTING_STARTED.md) -### Tutorials -- P2P Chat Example - - [part I](tutorial/directchat/start.nim) - - [part II](tutorial/directchat/second.nim) -### API Specifications -- libp2p - - [libp2p-daemon-client](api/libp2p/daemonapi.md) - - [interop-libp2p](api/libp2p/interop.md) -- transports - - [libp2p-tcp](api/transports/tcptransport.md) -- secure channels - - [libp2p-secio](api/secure_channels/secio.md) -- stream multiplexers - - [libp2p-mplex](api/stream_multiplexers/mplex.md) -- utilities - - [libp2p-crypto](api/utilities/crypto.md) - - [libp2p-crypto-secp256k1](api/utilities/secp256k1.md) -- data types - - [peer-id](api/data_types/peer.md) - - [peer-info](api/data_types/peerinfo.md) -- pubsub - - [libp2p-pubsub](api/pubsub/pubsub.md) - - [libp2p-floodsub](api/pubsub/floodsub.md) - - [libp2p-gossipsub](api/pubsub/gossipsub.md) \ No newline at end of file diff --git a/docs/api/data_types/peer.md b/docs/api/data_types/peer.md deleted file mode 100644 index e69de29bb..000000000 diff --git a/docs/api/data_types/peerinfo.md b/docs/api/data_types/peerinfo.md deleted file mode 100644 index e69de29bb..000000000 diff --git a/docs/api/libp2p/interop.md b/docs/api/libp2p/interop.md deleted file mode 100644 index e69de29bb..000000000 diff --git a/docs/api/pubsub/floodsub.md b/docs/api/pubsub/floodsub.md deleted file mode 100644 index e69de29bb..000000000 diff --git a/docs/api/pubsub/gossipsub.md b/docs/api/pubsub/gossipsub.md deleted file mode 100644 index e69de29bb..000000000 diff --git a/docs/api/pubsub/pubsub.md b/docs/api/pubsub/pubsub.md deleted file mode 100644 index e69de29bb..000000000 diff --git a/docs/api/secure_channels/secio.md b/docs/api/secure_channels/secio.md deleted file mode 100644 index e69de29bb..000000000 diff --git a/docs/api/stream_multiplexers/mplex.md b/docs/api/stream_multiplexers/mplex.md deleted file mode 100644 index e69de29bb..000000000 diff --git a/docs/api/transports/tcptransport.md b/docs/api/transports/tcptransport.md deleted file mode 100644 index e69de29bb..000000000 diff --git a/docs/api/utilities/crypto.md b/docs/api/utilities/crypto.md deleted file mode 100644 index e69de29bb..000000000 diff --git a/docs/api/utilities/secp256k1.md b/docs/api/utilities/secp256k1.md deleted file mode 100644 index e69de29bb..000000000 diff --git a/docs/tutorial/directchat/second.nim b/docs/tutorial/directchat/second.nim deleted file mode 100644 index 2c7b1a3a2..000000000 --- a/docs/tutorial/directchat/second.nim +++ /dev/null @@ -1,149 +0,0 @@ -when not(compileOption("threads")): - {.fatal: "Please, compile this program with the --threads:on option!".} - -import tables, strformat, strutils -import chronos -import ../libp2p/[switch, - multistream, - crypto/crypto, - protocols/identify, - connection, - transports/transport, - transports/tcptransport, - multiaddress, - peerinfo, - peerid, - protocols/protocol, - protocols/secure/secure, - protocols/secure/secio, - muxers/muxer, - muxers/mplex/mplex] - -const ChatCodec = "/nim-libp2p/chat/1.0.0" -const DefaultAddr = "/ip4/127.0.0.1/tcp/55505" - -const Help = """ - Commands: /[?|hep|connect|disconnect|exit] - help: Prints this help - connect: dials a remote peer - disconnect: ends current session - exit: closes the chat -""" - -type ChatProto = ref object of LPProtocol - switch: Switch # a single entry point for dialing and listening to peer - transp: StreamTransport # transport streams between read & write file descriptor - conn: Connection # create and close read & write stream - connected: bool # if the node is connected to another peer - started: bool # if the node has started - -# copied from https://github.com/status-im/nimbus-eth2/blob/0ed657e953740a92458f23033d47483ffa17ccb0/beacon_chain/eth2_network.nim#L109-L115 -proc initAddress(T: type MultiAddress, str: string): T = - let address = MultiAddress.init(str) - if IPFS.match(address) and matchPartial(multiaddress.TCP, address): - result = address - else: - raise newException(MultiAddressError, - "Invalid bootstrap node multi-address") - -proc dialPeer(p: ChatProto, address: string) {.async.} = - let multiAddr = MultiAddress.initAddress(address); - let parts = address.split("/") - let remotePeer = PeerInfo.init(parts[^1], - [multiAddr]) - - echo &"dialing peer: {multiAddr}" - p.conn = await p.switch.dial(remotePeer, ChatCodec) - p.connected = true - -proc readAndPrint(p: ChatProto) {.async.} = - while true: - while p.connected: - echo cast[string](await p.conn.readLp(1024)) - await sleepAsync(100.millis) - -proc writeAndPrint(p: ChatProto) {.async.} = - while true: - if not p.connected: - echo "type an address or wait for a connection:" - echo "type /[help|?] for help" - - let line = await p.transp.readLine() - if line.startsWith("/help") or line.startsWith("/?") or not p.started: - echo Help - continue - - if line.startsWith("/disconnect"): - echo "Ending current session" - if p.connected and p.conn.closed.not: - await p.conn.close() - p.connected = false - elif line.startsWith("/connect"): - if p.connected: - var yesno = "N" - echo "a session is already in progress, do you want end it [y/N]?" - yesno = await p.transp.readLine() - if yesno.cmpIgnoreCase("y") == 0: - await p.conn.close() - p.connected = false - elif yesno.cmpIgnoreCase("n") == 0: - continue - else: - echo "unrecognized response" - continue - - echo "enter address of remote peer" - let address = await p.transp.readLine() - if address.len > 0: - await p.dialPeer(address) - - elif line.startsWith("/exit"): - if p.connected and p.conn.closed.not: - await p.conn.close() - p.connected = false - - await p.switch.stop() - echo "quitting..." - quit(0) - else: - if p.connected: - await p.conn.writeLp(line) - else: - try: - if line.startsWith("/") and "ipfs" in line: - await p.dialPeer(line) - except: - echo &"unable to dial remote peer {line}" - echo getCurrentExceptionMsg() - -proc readWriteLoop(p: ChatProto) {.async.} = - asyncSpawn p.writeAndPrint() # execute the async function but does not block - asyncSpawn p.readAndPrint() - -proc processInput(rfd: AsyncFD) {.async.} = - let transp = fromPipe(rfd) - while true: - let a = await transp.readLine() - echo "You just entered: " & a - -proc readInput(wfd: AsyncFD) {.thread.} = - ## This procedure performs reading from `stdin` and sends data over - ## pipe to main thread. - let transp = fromPipe(wfd) - - while true: - let line = stdin.readLine() - discard waitFor transp.write(line & "\r\n") - -proc main() {.async.} = - let (rfd, wfd) = createAsyncPipe() - if rfd == asyncInvalidPipe or wfd == asyncInvalidPipe: - raise newException(ValueError, "Could not initialize pipe!") - - var thread: Thread[AsyncFD] - thread.createThread(readInput, wfd) - - await processInput(rfd) - -when isMainModule: # isMainModule = true when the module is compiled as the main file - waitFor(main()) diff --git a/docs/tutorial/directchat/start.nim b/docs/tutorial/directchat/start.nim deleted file mode 100644 index 7e7b8326f..000000000 --- a/docs/tutorial/directchat/start.nim +++ /dev/null @@ -1,39 +0,0 @@ -when not(compileOption("threads")): - {.fatal: "Please, compile this program with the --threads:on option!".} - -import chronos # an efficient library for async - -proc processInput(rfd: AsyncFD) {.async.} = - echo "Type something below to see if the multithread IO works:\nType 'exit' to exit." - - let transp = fromPipe(rfd) - while true: - let a = await transp.readLine() - - if a == "exit": - quit(0); - - echo "You just entered: " & a - -proc readInput(wfd: AsyncFD) {.thread.} = - ## This procedure performs reading from `stdin` and sends data over - ## pipe to main thread. - let transp = fromPipe(wfd) - - while true: - let line = stdin.readLine() - discard waitFor transp.write(line & "\r\n") - -proc main() {.async.} = - let (rfd, wfd) = createAsyncPipe() - if rfd == asyncInvalidPipe or wfd == asyncInvalidPipe: - raise newException(ValueError, "Could not initialize pipe!") - - var thread: Thread[AsyncFD] - thread.createThread(readInput, wfd) - - await processInput(rfd) - -when isMainModule: # isMainModule = true when the module is compiled as the main file - waitFor(main()) - \ No newline at end of file diff --git a/docs/tutorial/second.nim b/docs/tutorial/second.nim deleted file mode 100644 index e7ad282d8..000000000 --- a/docs/tutorial/second.nim +++ /dev/null @@ -1,205 +0,0 @@ -when not(compileOption("threads")): - {.fatal: "Please, compile this program with the --threads:on option!".} - -import tables, strformat, strutils, bearssl -import chronos # an efficient library for async -import ../libp2p/[switch, # manage transports, a single entry point for dialing and listening - builders, # helper to build the switch object - multistream, # tag stream with short header to identify it - multicodec, # multicodec utilities - crypto/crypto, # cryptographic functions - errors, # error handling utilities - protocols/identify, # identify the peer info of a peer - stream/connection, # create and close stream read / write connections - transports/transport, # listen and dial to other peers using p2p protocol - transports/tcptransport, # listen and dial to other peers using client-server protocol - multiaddress, # encode different addressing schemes. For example, /ip4/7.7.7.7/tcp/6543 means it is using IPv4 protocol and TCP - peerinfo, # manage the information of a peer, such as peer ID and public / private key - peerid, # Implement how peers interact - protocols/protocol, # define the protocol base type - protocols/secure/secure, # define the protocol of secure connection - protocols/secure/secio, # define the protocol of secure input / output, allows encrypted communication that uses public keys to validate signed messages instead of a certificate authority like in TLS - muxers/muxer, # define an interface for stream multiplexing, allowing peers to offer many protocols over a single connection - muxers/mplex/mplex] # define some contants and message types for stream multiplexing - -const ChatCodec = "/nim-libp2p/chat/1.0.0" -const DefaultAddr = "/ip4/127.0.0.1/tcp/55505" - -const Help = """ - Commands: /[?|hep|connect|disconnect|exit] - help: Prints this help - connect: dials a remote peer - disconnect: ends current session - exit: closes the chat -""" - -type ChatProto = ref object of LPProtocol - switch: Switch # a single entry point for dialing and listening to peer - transp: StreamTransport # transport streams between read & write file descriptor - conn: Connection # create and close read & write stream - connected: bool # if the node is connected to another peer - started: bool # if the node has started - -proc readAndPrint(p: ChatProto) {.async.} = - while true: - var strData = await p.conn.readLp(1024) - strData &= '\0'.uint8 - var str = cast[cstring](addr strdata[0]) - echo $p.switch.peerInfo.peerId & ": " & $str - await sleepAsync(100.millis) - -proc dialPeer(p: ChatProto, address: string) {.async.} = - let - multiAddr = MultiAddress.init(address).tryGet() - # split the peerId part /p2p/... - peerIdBytes = multiAddr[multiCodec("p2p")] - .tryGet() - .protoAddress() - .tryGet() - remotePeer = PeerId.init(peerIdBytes).tryGet() - # split the wire address - ip4Addr = multiAddr[multiCodec("ip4")].tryGet() - tcpAddr = multiAddr[multiCodec("tcp")].tryGet() - wireAddr = ip4Addr & tcpAddr - - echo &"dialing peer: {multiAddr}" - p.conn = await p.switch.dial(remotePeer, @[wireAddr], ChatCodec) - p.connected = true - asyncSpawn p.readAndPrint() - -proc writeAndPrint(p: ChatProto) {.async.} = - while true: - if not p.connected: - echo "type an address or wait for a connection:" - echo "type /[help|?] for help" - - let line = await p.transp.readLine() - if line.startsWith("/help") or line.startsWith("/?") or not p.started: - echo Help - continue - - if line.startsWith("/disconnect"): - echo "Ending current session" - if p.connected and p.conn.closed.not: - await p.conn.close() - p.connected = false - elif line.startsWith("/connect"): - if p.connected: - var yesno = "N" - echo "a session is already in progress, do you want end it [y/N]?" - yesno = await p.transp.readLine() - if yesno.cmpIgnoreCase("y") == 0: - await p.conn.close() - p.connected = false - elif yesno.cmpIgnoreCase("n") == 0: - continue - else: - echo "unrecognized response" - continue - - echo "enter address of remote peer" - let address = await p.transp.readLine() - if address.len > 0: - await p.dialPeer(address) - - elif line.startsWith("/exit"): - if p.connected and p.conn.closed.not: - await p.conn.close() - p.connected = false - - await p.switch.stop() - echo "quitting..." - quit(0) - else: - if p.connected: - await p.conn.writeLp(line) - else: - try: - if line.startsWith("/") and "p2p" in line: - await p.dialPeer(line) - except: - echo &"unable to dial remote peer {line}" - echo getCurrentExceptionMsg() - -proc readWriteLoop(p: ChatProto) {.async.} = - await p.writeAndPrint() - -proc newChatProto(switch: Switch, transp: StreamTransport): ChatProto = - var chatproto = ChatProto(switch: switch, transp: transp, codecs: @[ChatCodec]) - - # create handler for incoming connection - proc handle(stream: Connection, proto: string) {.async.} = - if chatproto.connected and not chatproto.conn.closed: - echo "a chat session is already in progress - disconnecting!" - await stream.close() - else: - chatproto.conn = stream - chatproto.connected = true - await chatproto.readAndPrint() - - # assign the new handler - chatproto.handler = handle - return chatproto - -proc readInput(wfd: AsyncFD) {.thread.} = - ## This procedure performs reading from `stdin` and sends data over - ## pipe to main thread. - let transp = fromPipe(wfd) - - while true: - let line = stdin.readLine() - discard waitFor transp.write(line & "\r\n") - -proc processInput(rfd: AsyncFD, rng: ref HmacDrbgContext) {.async.} = - let transp = fromPipe(rfd) - - let seckey = PrivateKey.random(RSA, rng[]).get() - var localAddress = DefaultAddr - while true: - echo &"Type an address to bind to or Enter to use the default {DefaultAddr}" - let a = await transp.readLine() - try: - if a.len > 0: - localAddress = a - break - # uise default - break - except: - echo "invalid address" - localAddress = DefaultAddr - continue - - var switch = SwitchBuilder - .init() - .withRng(rng) - .withPrivateKey(seckey) - .withAddress(MultiAddress.init(localAddress).tryGet()) - .build() - - let chatProto = newChatProto(switch, transp) - switch.mount(chatProto) - let libp2pFuts = await switch.start() - chatProto.started = true - - let id = $switch.peerInfo.peerId - echo "PeerId: " & id - echo "listening on: " - for a in switch.peerInfo.addrs: - echo &"{a}/p2p/{id}" - - await chatProto.readWriteLoop() - await allFuturesThrowing(libp2pFuts) - -proc main() {.async.} = - let rng = newRng() # Singe random number source for the whole application - let (rfd, wfd) = createAsyncPipe() - if rfd == asyncInvalidPipe or wfd == asyncInvalidPipe: - raise newException(ValueError, "Could not initialize pipe!") - - var thread: Thread[AsyncFD] - thread.createThread(readInput, wfd) - - await processInput(rfd, rng) - -when isMainModule: # isMainModule = true when the module is compiled as the main file - waitFor(main()) diff --git a/docs/tutorial/start.nim b/docs/tutorial/start.nim deleted file mode 100644 index 7e7b8326f..000000000 --- a/docs/tutorial/start.nim +++ /dev/null @@ -1,39 +0,0 @@ -when not(compileOption("threads")): - {.fatal: "Please, compile this program with the --threads:on option!".} - -import chronos # an efficient library for async - -proc processInput(rfd: AsyncFD) {.async.} = - echo "Type something below to see if the multithread IO works:\nType 'exit' to exit." - - let transp = fromPipe(rfd) - while true: - let a = await transp.readLine() - - if a == "exit": - quit(0); - - echo "You just entered: " & a - -proc readInput(wfd: AsyncFD) {.thread.} = - ## This procedure performs reading from `stdin` and sends data over - ## pipe to main thread. - let transp = fromPipe(wfd) - - while true: - let line = stdin.readLine() - discard waitFor transp.write(line & "\r\n") - -proc main() {.async.} = - let (rfd, wfd) = createAsyncPipe() - if rfd == asyncInvalidPipe or wfd == asyncInvalidPipe: - raise newException(ValueError, "Could not initialize pipe!") - - var thread: Thread[AsyncFD] - thread.createThread(readInput, wfd) - - await processInput(rfd) - -when isMainModule: # isMainModule = true when the module is compiled as the main file - waitFor(main()) - \ No newline at end of file diff --git a/docs/api/libp2p/daemonapi.md b/examples/go-daemon/daemonapi.md similarity index 100% rename from docs/api/libp2p/daemonapi.md rename to examples/go-daemon/daemonapi.md diff --git a/libp2p.nim b/libp2p.nim index 298c6f6d9..3df1b24a2 100644 --- a/libp2p.nim +++ b/libp2p.nim @@ -1,38 +1,72 @@ -## Nim-LibP2P -## Copyright (c) 2018 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-LibP2P +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. -import - libp2p/[protobuf/minprotobuf, - muxers/muxer, - muxers/mplex/mplex, - stream/lpstream, - stream/bufferstream, - stream/connection, - transports/transport, - transports/tcptransport, - protocols/secure/noise, - cid, - multihash, - multibase, - multicodec, - errors, - switch, - peerid, - peerinfo, - multiaddress, - builders, - crypto/crypto, - protocols/pubsub] +when defined(nimdoc): + ## Welcome to the nim-libp2p documentation! + ## + ## On the left, you'll find a switch that allows you to see private + ## procedures. By default, you'll only see the public one (marked with `{.public.}`) + ## + ## The difference between public and private procedures is that public procedure + ## stay backward compatible during the Major version, whereas private ones can + ## change at each new Minor version. + ## + ## If you're new to nim-libp2p, you can find a tutorial `here`_ + ## that can help you get started. -export - minprotobuf, switch, peerid, peerinfo, - connection, multiaddress, crypto, lpstream, - bufferstream, muxer, mplex, transport, - tcptransport, noise, errors, cid, multihash, - multicodec, builders, pubsub + # Import stuff for doc + import libp2p/[ + protobuf/minprotobuf, + switch, + stream/lpstream, + builders, + transports/tcptransport, + transports/wstransport, + protocols/ping, + protocols/pubsub, + peerid, + peerinfo, + peerstore, + 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, + transports/wstransport, + protocols/secure/noise, + protocols/ping, + cid, + multihash, + multibase, + 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, + multicodec, builders, pubsub diff --git a/libp2p.nimble b/libp2p.nimble index 513d83491..514f15ce0 100644 --- a/libp2p.nimble +++ b/libp2p.nimble @@ -102,6 +102,23 @@ task examples_build, "Build the samples": buildTutorial("examples/tutorial_1_connect.md") buildTutorial("examples/tutorial_2_customproto.md") +proc tutorialToHtml(source, output: string) = + var html = gorge("./nimbledeps/bin/markdown < " & source) + html &= """ + + + + + + """ + writeFile(output, html) + + +task markdown_to_html, "Build the tutorials HTML": + exec "nimble install -y markdown" + tutorialToHtml("examples/tutorial_1_connect.md", "tuto1.html") + tutorialToHtml("examples/tutorial_2_customproto.md", "tuto2.html") + # pin system # while nimble lockfile # isn't available diff --git a/libp2p/builders.nim b/libp2p/builders.nim index 699975e61..873bc4326 100644 --- a/libp2p/builders.nim +++ b/libp2p/builders.nim @@ -1,11 +1,20 @@ -## Nim-Libp2p -## Copyright (c) 2020 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-Libp2p +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. + +## This module contains a Switch Building helper. +runnableExamples: + let switch = + SwitchBuilder.new() + .withRng(rng) + .withAddresses(multiaddress) + # etc + .build() {.push raises: [Defect].} @@ -17,13 +26,13 @@ import protocols/[identify, secure/secure, secure/noise, relay], connmanager, upgrademngrs/muxedupgrade, nameresolving/nameresolver, - errors + errors, utility export switch, peerid, peerinfo, connection, multiaddress, crypto, errors type - TransportProvider* = proc(upgr: Upgrade): Transport {.gcsafe, raises: [Defect].} + TransportProvider* {.public.} = proc(upgr: Upgrade): Transport {.gcsafe, raises: [Defect].} SecureProtocol* {.pure.} = enum Noise, @@ -52,7 +61,8 @@ type isCircuitRelay: bool circuitRelayCanHop: bool -proc new*(T: type[SwitchBuilder]): T = +proc new*(T: type[SwitchBuilder]): T {.public.} = + ## Creates a SwitchBuilder let address = MultiAddress .init("/ip4/127.0.0.1/tcp/0") @@ -70,19 +80,28 @@ proc new*(T: type[SwitchBuilder]): T = agentVersion: AgentVersion, isCircuitRelay: false) -proc withPrivateKey*(b: SwitchBuilder, privateKey: PrivateKey): SwitchBuilder = +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 withAddress*(b: SwitchBuilder, address: MultiAddress): SwitchBuilder = +proc withAddress*(b: SwitchBuilder, address: MultiAddress): SwitchBuilder {.public.} = + ## | Set the listening address of the switch + ## | Calling it multiple time will override the value + b.addresses = @[address] b -proc withAddresses*(b: SwitchBuilder, addresses: seq[MultiAddress]): SwitchBuilder = +proc withAddresses*(b: SwitchBuilder, addresses: seq[MultiAddress]): SwitchBuilder {.public.} = + ## | Set the listening addresses of the switch + ## | Calling it multiple time will override the value + b.addresses = addresses b -proc withSignedPeerRecord*(b: SwitchBuilder, sendIt = true): SwitchBuilder = +proc withSignedPeerRecord*(b: SwitchBuilder, sendIt = true): SwitchBuilder {.public.} = b.sendSignedPeerRecord = sendIt b @@ -90,7 +109,9 @@ proc withMplex*( b: SwitchBuilder, inTimeout = 5.minutes, outTimeout = 5.minutes, - maxChannCount = 200): SwitchBuilder = + 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, @@ -105,50 +126,60 @@ proc withMplex*( b -proc withNoise*(b: SwitchBuilder): SwitchBuilder = +proc withNoise*(b: SwitchBuilder): SwitchBuilder {.public.} = b.secureManagers.add(SecureProtocol.Noise) b -proc withTransport*(b: SwitchBuilder, prov: TransportProvider): SwitchBuilder = +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)) + .build() b.transports.add(prov) b -proc withTcpTransport*(b: SwitchBuilder, flags: set[ServerFlags] = {}): SwitchBuilder = +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 = +proc withRng*(b: SwitchBuilder, rng: ref HmacDrbgContext): SwitchBuilder {.public.} = b.rng = rng b -proc withMaxConnections*(b: SwitchBuilder, maxConnections: int): SwitchBuilder = +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 b -proc withMaxIn*(b: SwitchBuilder, maxIn: int): SwitchBuilder = +proc withMaxIn*(b: SwitchBuilder, maxIn: int): SwitchBuilder {.public.} = + ## Maximum concurrent incoming connections. Should be used with `withMaxOut<#withMaxOut,SwitchBuilder,int>`_ b.maxIn = maxIn b -proc withMaxOut*(b: SwitchBuilder, maxOut: int): SwitchBuilder = +proc withMaxOut*(b: SwitchBuilder, maxOut: int): SwitchBuilder {.public.} = + ## Maximum concurrent outgoing connections. Should be used with `withMaxIn<#withMaxIn,SwitchBuilder,int>`_ b.maxOut = maxOut b -proc withMaxConnsPerPeer*(b: SwitchBuilder, maxConnsPerPeer: int): SwitchBuilder = +proc withMaxConnsPerPeer*(b: SwitchBuilder, maxConnsPerPeer: int): SwitchBuilder {.public.} = b.maxConnsPerPeer = maxConnsPerPeer b -proc withPeerStore*(b: SwitchBuilder, capacity: int): SwitchBuilder = +proc withPeerStore*(b: SwitchBuilder, capacity: int): SwitchBuilder {.public.} = b.peerStoreCapacity = some(capacity) b -proc withProtoVersion*(b: SwitchBuilder, protoVersion: string): SwitchBuilder = +proc withProtoVersion*(b: SwitchBuilder, protoVersion: string): SwitchBuilder {.public.} = b.protoVersion = protoVersion b -proc withAgentVersion*(b: SwitchBuilder, agentVersion: string): SwitchBuilder = +proc withAgentVersion*(b: SwitchBuilder, agentVersion: string): SwitchBuilder {.public.} = b.agentVersion = agentVersion b -proc withNameResolver*(b: SwitchBuilder, nameResolver: NameResolver): SwitchBuilder = +proc withNameResolver*(b: SwitchBuilder, nameResolver: NameResolver): SwitchBuilder {.public.} = b.nameResolver = nameResolver b @@ -158,7 +189,7 @@ proc withRelayTransport*(b: SwitchBuilder, canHop: bool): SwitchBuilder = b proc build*(b: SwitchBuilder): Switch - {.raises: [Defect, LPError].} = + {.raises: [Defect, LPError], public.} = if b.rng == nil: # newRng could fail raise newException(Defect, "Cannot initialize RNG") @@ -246,7 +277,9 @@ proc newStandardSwitch*( nameResolver: NameResolver = nil, sendSignedPeerRecord = false, peerStoreCapacity = 1000): Switch - {.raises: [Defect, LPError].} = + {.raises: [Defect, LPError], public.} = + ## Helper for common switch configurations. + if SecureProtocol.Secio in secureManagers: quit("Secio is deprecated!") # use of secio is unsafe diff --git a/libp2p/cid.nim b/libp2p/cid.nim index 7af403567..f12c4d814 100644 --- a/libp2p/cid.nim +++ b/libp2p/cid.nim @@ -1,11 +1,11 @@ -## Nim-LibP2P -## Copyright (c) 2018 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-LibP2P +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. ## This module implementes CID (Content IDentifier). diff --git a/libp2p/connmanager.nim b/libp2p/connmanager.nim index 76de2da07..e3cb2e8e9 100644 --- a/libp2p/connmanager.nim +++ b/libp2p/connmanager.nim @@ -1,11 +1,11 @@ -## Nim-LibP2P -## Copyright (c) 2020 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-LibP2P +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. {.push raises: [Defect].} diff --git a/libp2p/crypto/chacha20poly1305.nim b/libp2p/crypto/chacha20poly1305.nim index 4e763d5ac..c3eeca776 100644 --- a/libp2p/crypto/chacha20poly1305.nim +++ b/libp2p/crypto/chacha20poly1305.nim @@ -1,11 +1,11 @@ -## Nim-Libp2p -## Copyright (c) 2020-2022 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-Libp2p +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. ## This module integrates BearSSL ChaCha20+Poly1305 ## diff --git a/libp2p/crypto/crypto.nim b/libp2p/crypto/crypto.nim index fa5a9fa3d..99bf5849b 100644 --- a/libp2p/crypto/crypto.nim +++ b/libp2p/crypto/crypto.nim @@ -1,11 +1,11 @@ -## Nim-Libp2p -## Copyright (c) 2018 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-Libp2p +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. ## This module implements Public Key and Private Key interface for libp2p. {.push raises: [Defect].} @@ -76,7 +76,7 @@ import nimcrypto/[rijndael, twofish, sha2, hash, hmac] import nimcrypto/utils as ncrutils import ../utility import stew/results -export results +export results, utility # This is workaround for Nim's `import` bug export rijndael, twofish, sha2, hash, hmac, ncrutils, rand diff --git a/libp2p/crypto/curve25519.nim b/libp2p/crypto/curve25519.nim index 7198a6073..ad14ec6f8 100644 --- a/libp2p/crypto/curve25519.nim +++ b/libp2p/crypto/curve25519.nim @@ -1,11 +1,11 @@ -## Nim-Libp2p -## Copyright (c) 2020-2022 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-Libp2p +# Copyright (c) 2022-2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. ## This module integrates BearSSL Cyrve25519 mul and mulgen ## diff --git a/libp2p/crypto/ecnist.nim b/libp2p/crypto/ecnist.nim index 74df31ae6..5acd0644b 100644 --- a/libp2p/crypto/ecnist.nim +++ b/libp2p/crypto/ecnist.nim @@ -1,11 +1,11 @@ -## Nim-Libp2p -## Copyright (c) 2018 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-Libp2p +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. ## This module implements constant-time ECDSA and ECDHE for NIST elliptic ## curves secp256r1, secp384r1 and secp521r1. diff --git a/libp2p/crypto/ed25519/constants.nim b/libp2p/crypto/ed25519/constants.nim index bc2248c54..bc77ea770 100644 --- a/libp2p/crypto/ed25519/constants.nim +++ b/libp2p/crypto/ed25519/constants.nim @@ -1,11 +1,11 @@ -## Nim-Libp2p -## Copyright (c) 2018 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-Libp2p +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. ## This module implements ED25519. ## This is pure nim implementation of ED25519 ref10. diff --git a/libp2p/crypto/ed25519/ed25519.nim b/libp2p/crypto/ed25519/ed25519.nim index 414bd594c..e6e199694 100644 --- a/libp2p/crypto/ed25519/ed25519.nim +++ b/libp2p/crypto/ed25519/ed25519.nim @@ -1,11 +1,11 @@ -## Nim-Libp2p -## Copyright (c) 2018 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-Libp2p +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. ## This module implements ED25519. ## This code is a port of the public domain, "ref10" implementation of ed25519 diff --git a/libp2p/crypto/hkdf.nim b/libp2p/crypto/hkdf.nim index bc5c65b2e..4159867c5 100644 --- a/libp2p/crypto/hkdf.nim +++ b/libp2p/crypto/hkdf.nim @@ -1,11 +1,11 @@ -## Nim-LibP2P -## Copyright (c) 2020 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-LibP2P +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. # https://tools.ietf.org/html/rfc5869 diff --git a/libp2p/crypto/minasn1.nim b/libp2p/crypto/minasn1.nim index 3bea03739..c23b0e35d 100644 --- a/libp2p/crypto/minasn1.nim +++ b/libp2p/crypto/minasn1.nim @@ -1,11 +1,11 @@ -## Nim-Libp2p -## Copyright (c) 2018 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-Libp2p +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. ## This module implements minimal ASN.1 encoding/decoding primitives. diff --git a/libp2p/crypto/rsa.nim b/libp2p/crypto/rsa.nim index 23ebcc7d9..2b95a4452 100644 --- a/libp2p/crypto/rsa.nim +++ b/libp2p/crypto/rsa.nim @@ -1,11 +1,11 @@ -## Nim-Libp2p -## Copyright (c) 2018 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-Libp2p +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. ## This module implements constant-time RSA PKCS#1.5 DSA. ## diff --git a/libp2p/crypto/secp.nim b/libp2p/crypto/secp.nim index 8f31fe0ac..bff6b518a 100644 --- a/libp2p/crypto/secp.nim +++ b/libp2p/crypto/secp.nim @@ -1,11 +1,11 @@ -## Nim-Libp2p -## Copyright (c) 2018 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-Libp2p +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. {.push raises: [Defect].} diff --git a/libp2p/daemon/daemonapi.nim b/libp2p/daemon/daemonapi.nim index c082a6070..b5b044716 100644 --- a/libp2p/daemon/daemonapi.nim +++ b/libp2p/daemon/daemonapi.nim @@ -1,11 +1,11 @@ -## Nim-LibP2P -## Copyright (c) 2018 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-LibP2P +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. {.push raises: [Defect].} diff --git a/libp2p/daemon/transpool.nim b/libp2p/daemon/transpool.nim index ae7ddab61..381f82744 100644 --- a/libp2p/daemon/transpool.nim +++ b/libp2p/daemon/transpool.nim @@ -1,11 +1,11 @@ -## Nim-Libp2p -## Copyright (c) 2018 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-Libp2p +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. {.push raises: [Defect].} diff --git a/libp2p/debugutils.nim b/libp2p/debugutils.nim index 20daacfbd..a384f9de6 100644 --- a/libp2p/debugutils.nim +++ b/libp2p/debugutils.nim @@ -1,11 +1,11 @@ -## Nim-LibP2P -## Copyright (c) 2018 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-LibP2P +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. ## To enable dump of all incoming and outgoing unencrypted messages you need ## to compile project with ``-d:libp2p_dump`` compile-time option. When this diff --git a/libp2p/dial.nim b/libp2p/dial.nim index d850a9da2..9f16c89ce 100644 --- a/libp2p/dial.nim +++ b/libp2p/dial.nim @@ -1,11 +1,11 @@ -## Nim-LibP2P -## Copyright (c) 2021 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-LibP2P +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. {.push raises: [Defect].} diff --git a/libp2p/dialer.nim b/libp2p/dialer.nim index eb89164da..9d50cb547 100644 --- a/libp2p/dialer.nim +++ b/libp2p/dialer.nim @@ -1,11 +1,11 @@ -## Nim-LibP2P -## Copyright (c) 2021 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-LibP2P +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. import std/[sugar, tables] diff --git a/libp2p/multiaddress.nim b/libp2p/multiaddress.nim index 7d7feedb3..4405973fe 100644 --- a/libp2p/multiaddress.nim +++ b/libp2p/multiaddress.nim @@ -1,23 +1,24 @@ -## Nim-Libp2p -## Copyright (c) 2018 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-Libp2p +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. ## This module implements MultiAddress. {.push raises: [Defect].} +{.push public.} import pkg/chronos import std/[nativesockets, hashes] import tables, strutils, sets, stew/shims/net import multicodec, multihash, multibase, transcoder, vbuffer, peerid, - protobuf/minprotobuf, errors + protobuf/minprotobuf, errors, utility import stew/[base58, base32, endians2, results] -export results, minprotobuf, vbuffer +export results, minprotobuf, vbuffer, utility type MAKind* = enum diff --git a/libp2p/multibase.nim b/libp2p/multibase.nim index e2b875d31..19ca75f87 100644 --- a/libp2p/multibase.nim +++ b/libp2p/multibase.nim @@ -1,11 +1,11 @@ -## Nim-Libp2p -## Copyright (c) 2018 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-Libp2p +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. ## This module implements MultiBase. ## diff --git a/libp2p/multicodec.nim b/libp2p/multicodec.nim index e3fbe9d1f..6abfbcae0 100644 --- a/libp2p/multicodec.nim +++ b/libp2p/multicodec.nim @@ -1,11 +1,11 @@ -## Nim-Libp2p -## Copyright (c) 2018 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not BE copied, modified, or distributed except according to -## those terms. +# Nim-Libp2p +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not BE copied, modified, or distributed except according to +# those terms. ## This module implements MultiCodec. diff --git a/libp2p/multihash.nim b/libp2p/multihash.nim index bba81d9ce..f449bbd3a 100644 --- a/libp2p/multihash.nim +++ b/libp2p/multihash.nim @@ -1,11 +1,11 @@ -## Nim-Libp2p -## Copyright (c) 2018 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-Libp2p +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. ## This module implements MultiHash. ## Supported hashes are: diff --git a/libp2p/multistream.nim b/libp2p/multistream.nim index 15cdad9b2..42aef1530 100644 --- a/libp2p/multistream.nim +++ b/libp2p/multistream.nim @@ -1,11 +1,11 @@ -## Nim-LibP2P -## Copyright (c) 2019 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-LibP2P +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. {.push raises: [Defect].} diff --git a/libp2p/muxers/mplex/coder.nim b/libp2p/muxers/mplex/coder.nim index 3a8df9f5c..e1ba5bf77 100644 --- a/libp2p/muxers/mplex/coder.nim +++ b/libp2p/muxers/mplex/coder.nim @@ -1,11 +1,11 @@ -## Nim-LibP2P -## Copyright (c) 2019 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-LibP2P +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. {.push raises: [Defect].} diff --git a/libp2p/muxers/mplex/lpchannel.nim b/libp2p/muxers/mplex/lpchannel.nim index eb7d6ca14..758e1cb89 100644 --- a/libp2p/muxers/mplex/lpchannel.nim +++ b/libp2p/muxers/mplex/lpchannel.nim @@ -1,11 +1,11 @@ -## Nim-LibP2P -## Copyright (c) 2019 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-LibP2P +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. {.push raises: [Defect].} diff --git a/libp2p/muxers/mplex/mplex.nim b/libp2p/muxers/mplex/mplex.nim index fc3a69e7d..977a64e8f 100644 --- a/libp2p/muxers/mplex/mplex.nim +++ b/libp2p/muxers/mplex/mplex.nim @@ -1,11 +1,11 @@ -## Nim-LibP2P -## Copyright (c) 2019 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-LibP2P +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. {.push raises: [Defect].} diff --git a/libp2p/muxers/muxer.nim b/libp2p/muxers/muxer.nim index 670a5c777..9dc520347 100644 --- a/libp2p/muxers/muxer.nim +++ b/libp2p/muxers/muxer.nim @@ -1,11 +1,11 @@ -## Nim-LibP2P -## Copyright (c) 2019 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-LibP2P +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. {.push raises: [Defect].} diff --git a/libp2p/nameresolving/dnsresolver.nim b/libp2p/nameresolving/dnsresolver.nim index d224afc40..2bda23c72 100644 --- a/libp2p/nameresolving/dnsresolver.nim +++ b/libp2p/nameresolving/dnsresolver.nim @@ -1,11 +1,11 @@ -## Nim-LibP2P -## Copyright (c) 2021 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-LibP2P +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. {.push raises: [Defect].} diff --git a/libp2p/nameresolving/mockresolver.nim b/libp2p/nameresolving/mockresolver.nim index 9a5368f32..e1d84488e 100644 --- a/libp2p/nameresolving/mockresolver.nim +++ b/libp2p/nameresolving/mockresolver.nim @@ -1,11 +1,11 @@ -## Nim-LibP2P -## Copyright (c) 2021 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-LibP2P +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. {.push raises: [Defect].} diff --git a/libp2p/nameresolving/nameresolver.nim b/libp2p/nameresolving/nameresolver.nim index b8da3352a..ad87af13e 100644 --- a/libp2p/nameresolving/nameresolver.nim +++ b/libp2p/nameresolving/nameresolver.nim @@ -1,11 +1,11 @@ -## Nim-LibP2P -## Copyright (c) 2021 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-LibP2P +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. {.push raises: [Defect].} diff --git a/libp2p/peerid.nim b/libp2p/peerid.nim index d7e578929..efc43cfc4 100644 --- a/libp2p/peerid.nim +++ b/libp2p/peerid.nim @@ -1,25 +1,27 @@ -## Nim-LibP2P -## Copyright (c) 2018 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-LibP2P +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. ## This module implementes API for libp2p peer. {.push raises: [Defect].} +{.push public.} import std/[hashes, strutils], stew/[base58, results], chronicles, nimcrypto/utils, + utility, ./crypto/crypto, ./multicodec, ./multihash, ./vbuffer, ./protobuf/minprotobuf -export results +export results, utility const maxInlineKeyLength* = 42 diff --git a/libp2p/peerinfo.nim b/libp2p/peerinfo.nim index ce953cc3b..b6de563ab 100644 --- a/libp2p/peerinfo.nim +++ b/libp2p/peerinfo.nim @@ -1,17 +1,18 @@ -## Nim-LibP2P -## Copyright (c) 2019 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-LibP2P +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. {.push raises: [Defect].} +{.push public.} import std/[options, sequtils] import pkg/[chronos, chronicles, stew/results] -import peerid, multiaddress, crypto/crypto, routing_record, errors +import peerid, multiaddress, crypto/crypto, routing_record, errors, utility export peerid, multiaddress, crypto, routing_record, errors, results @@ -20,7 +21,7 @@ export peerid, multiaddress, crypto, routing_record, errors, results type PeerInfoError* = LPError - PeerInfo* = ref object + PeerInfo* {.public.} = ref object peerId*: PeerId addrs*: seq[MultiAddress] protocols*: seq[string] diff --git a/libp2p/peerstore.nim b/libp2p/peerstore.nim index b86145c95..efe38a6a9 100644 --- a/libp2p/peerstore.nim +++ b/libp2p/peerstore.nim @@ -1,11 +1,26 @@ -## Nim-LibP2P -## Copyright (c) 2021 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-LibP2P +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. + +## Stores generic informations about peers. +runnableExamples: + # Will keep info of all connected peers + + # last 50 disconnected peers + let peerStore = PeerStore.new(capacity = 50) + + # Create a custom book type + type MoodBook = ref object of PeerBook[string] + + var somePeerId: PeerId + discard somePeerId.init("") + + peerStore[MoodBook][somePeerId] = "Happy" + doAssert peerStore[MoodBook][somePeerId] == "Happy" {.push raises: [Defect].} @@ -15,7 +30,8 @@ import ./protocols/identify, ./peerid, ./peerinfo, ./routing_record, - ./multiaddress + ./multiaddress, + utility type ################# @@ -23,7 +39,7 @@ type ################# PeerBookChangeHandler* = proc(peerId: PeerId) {.gcsafe, raises: [Defect].} - + ######### # Books # ######### @@ -33,29 +49,29 @@ type changeHandlers: seq[PeerBookChangeHandler] deletor: PeerBookChangeHandler - PeerBook*[T] = ref object of BasePeerBook + PeerBook*[T] {.public.} = ref object of BasePeerBook book*: Table[PeerId, T] SeqPeerBook*[T] = ref object of PeerBook[seq[T]] - - AddressBook* = ref object of SeqPeerBook[MultiAddress] - ProtoBook* = ref object of SeqPeerBook[string] - KeyBook* = ref object of PeerBook[PublicKey] - AgentBook* = ref object of PeerBook[string] - ProtoVersionBook* = ref object of PeerBook[string] - SPRBook* = ref object of PeerBook[Envelope] - + AddressBook* {.public.} = ref object of SeqPeerBook[MultiAddress] + ProtoBook* {.public.} = ref object of SeqPeerBook[string] + KeyBook* {.public.} = ref object of PeerBook[PublicKey] + + AgentBook* {.public.} = ref object of PeerBook[string] + ProtoVersionBook* {.public.} = ref object of PeerBook[string] + SPRBook* {.public.} = ref object of PeerBook[Envelope] + #################### # Peer store types # #################### - PeerStore* = ref object + PeerStore* {.public.} = ref object books: Table[string, BasePeerBook] capacity*: int toClean*: seq[PeerId] - -proc new*(T: type PeerStore, capacity = 1000): PeerStore = + +proc new*(T: type PeerStore, capacity = 1000): PeerStore {.public.} = T(capacity: capacity) ######################### @@ -63,16 +79,15 @@ proc new*(T: type PeerStore, capacity = 1000): PeerStore = ######################### proc `[]`*[T](peerBook: PeerBook[T], - peerId: PeerId): T = - ## Get all the known metadata of a provided peer. + 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) = - ## Set metadata for a given peerId. This will replace any - ## previously stored metadata. - + entry: T) {.public.} = + ## Set metadata for a given peerId. + peerBook.book[peerId] = entry # Notify clients @@ -80,9 +95,9 @@ proc `[]=`*[T](peerBook: PeerBook[T], handler(peerId) proc del*[T](peerBook: PeerBook[T], - peerId: PeerId): bool = - ## Delete the provided peer from the book. - + peerId: PeerId): bool {.public.} = + ## Delete the provided peer from the book. Returns whether the peer was in the book + if peerId notin peerBook.book: return false else: @@ -92,15 +107,16 @@ proc del*[T](peerBook: PeerBook[T], handler(peerId) return true -proc contains*[T](peerBook: PeerBook[T], peerId: PeerId): bool = +proc contains*[T](peerBook: PeerBook[T], peerId: PeerId): bool {.public.} = peerId in peerBook.book -proc addHandler*[T](peerBook: PeerBook[T], handler: PeerBookChangeHandler) = +proc addHandler*[T](peerBook: PeerBook[T], handler: PeerBookChangeHandler) {.public.} = + ## Adds a callback that will be called everytime the book changes peerBook.changeHandlers.add(handler) -proc len*[T](peerBook: PeerBook[T]): int = peerBook.book.len +proc len*[T](peerBook: PeerBook[T]): int {.public.} = peerBook.book.len -################## +################## # Peer Store API # ################## macro getTypeName(t: type): untyped = @@ -108,7 +124,8 @@ macro getTypeName(t: type): untyped = let typ = getTypeImpl(t)[1] newLit(repr(typ.owner()) & "." & repr(typ)) -proc `[]`*[T](p: PeerStore, typ: type[T]): T = +proc `[]`*[T](p: PeerStore, typ: type[T]): T {.public.} = + ## Get a book from the PeerStore (ex: peerStore[AddressBook]) let name = getTypeName(T) result = T(p.books.getOrDefault(name)) if result.isNil: @@ -121,7 +138,7 @@ proc `[]`*[T](p: PeerStore, typ: type[T]): T = return result proc del*(peerStore: PeerStore, - peerId: PeerId) = + peerId: PeerId) {.public.} = ## Delete the provided peer from every book. for _, book in peerStore.books: book.deletor(peerId) diff --git a/libp2p/protobuf/minprotobuf.nim b/libp2p/protobuf/minprotobuf.nim index 919c0839b..a7144e21d 100644 --- a/libp2p/protobuf/minprotobuf.nim +++ b/libp2p/protobuf/minprotobuf.nim @@ -1,18 +1,20 @@ -## Nim-Libp2p -## Copyright (c) 2018 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-Libp2p +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. ## This module implements minimal Google's ProtoBuf primitives. {.push raises: [Defect].} -import ../varint, stew/[endians2, results] -export results +import ../varint, ../utility, stew/[endians2, results] +export results, utility + +{.push public.} const MaxMessageSize* = 1'u shl 22 diff --git a/libp2p/protocols/identify.nim b/libp2p/protocols/identify.nim index 8e5cfcd3f..9fce0e150 100644 --- a/libp2p/protocols/identify.nim +++ b/libp2p/protocols/identify.nim @@ -1,11 +1,14 @@ -## Nim-LibP2P -## Copyright (c) 2019 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-LibP2P +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. + +## `Identify `_ and +## `Push Identify `_ implementation {.push raises: [Defect].} @@ -36,7 +39,7 @@ type IdentityInvalidMsgError* = object of IdentifyError IdentifyNoPubKeyError* = object of IdentifyError - IdentifyInfo* = object + IdentifyInfo* {.public.} = object pubkey*: Option[PublicKey] peerId*: PeerId addrs*: seq[MultiAddress] @@ -54,7 +57,7 @@ type peer: PeerId, newInfo: IdentifyInfo): Future[void] - {.gcsafe, raises: [Defect].} + {.gcsafe, raises: [Defect], public.} IdentifyPush* = ref object of LPProtocol identifyHandler: IdentifyPushHandler @@ -203,7 +206,9 @@ proc identify*(p: Identify, else: raise newException(IdentityInvalidMsgError, "No pubkey in identify") -proc new*(T: typedesc[IdentifyPush], handler: IdentifyPushHandler = nil): T = +proc new*(T: typedesc[IdentifyPush], handler: IdentifyPushHandler = nil): T {.public.} = + ## Create a IdentifyPush protocol. `handler` will be called every time + ## a peer sends us new `PeerInfo` let identifypush = T(identifyHandler: handler) identifypush.init() identifypush @@ -240,6 +245,7 @@ proc init*(p: IdentifyPush) = p.handler = handle p.codec = IdentifyPushCodec -proc push*(p: IdentifyPush, peerInfo: PeerInfo, conn: Connection) {.async.} = +proc push*(p: IdentifyPush, peerInfo: PeerInfo, conn: Connection) {.async, public.} = + ## Send new `peerInfo`s to a connection var pb = encodeMsg(peerInfo, conn.observedAddr, true) await conn.writeLp(pb.buffer) diff --git a/libp2p/protocols/ping.nim b/libp2p/protocols/ping.nim index a1e8b3af5..648a60d7c 100644 --- a/libp2p/protocols/ping.nim +++ b/libp2p/protocols/ping.nim @@ -1,11 +1,13 @@ -## Nim-LibP2P -## Copyright (c) 2021 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-LibP2P +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. + +## `Ping `_ protocol implementation {.push raises: [Defect].} @@ -18,6 +20,7 @@ import ../protobuf/minprotobuf, ../crypto/crypto, ../multiaddress, ../protocols/protocol, + ../utility, ../errors export chronicles, rand, connection @@ -31,9 +34,9 @@ const type PingError* = object of LPError - WrongPingAckError* = object of LPError + WrongPingAckError* = object of PingError - PingHandler* = proc ( + PingHandler* {.public.} = proc ( peer: PeerId): Future[void] {.gcsafe, raises: [Defect].} @@ -42,7 +45,7 @@ type pingHandler*: PingHandler rng: ref HmacDrbgContext -proc new*(T: typedesc[Ping], handler: PingHandler = nil, rng: ref HmacDrbgContext = newRng()): T = +proc new*(T: typedesc[Ping], handler: PingHandler = nil, rng: ref HmacDrbgContext = newRng()): T {.public.} = let ping = Ping(pinghandler: handler, rng: rng) ping.init() ping @@ -68,10 +71,8 @@ method init*(p: Ping) = proc ping*( p: Ping, conn: Connection, - ): Future[Duration] {.async, gcsafe.} = - ## Sends ping to `conn` - ## Returns the delay - ## + ): Future[Duration] {.async, gcsafe, public.} = + ## Sends ping to `conn`, returns the delay trace "initiating ping", conn diff --git a/libp2p/protocols/protocol.nim b/libp2p/protocols/protocol.nim index edba47647..adafe89d4 100644 --- a/libp2p/protocols/protocol.nim +++ b/libp2p/protocols/protocol.nim @@ -1,11 +1,11 @@ -## Nim-LibP2P -## Copyright (c) 2019 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-LibP2P +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. {.push raises: [Defect].} diff --git a/libp2p/protocols/pubsub.nim b/libp2p/protocols/pubsub.nim index c094c682a..ade55b248 100644 --- a/libp2p/protocols/pubsub.nim +++ b/libp2p/protocols/pubsub.nim @@ -1,3 +1,8 @@ import ./pubsub/[pubsub, floodsub, gossipsub] +## Home of PubSub & it's implementations: +## | **pubsub**: base interface for pubsub implementations +## | **floodsub**: simple flood-based publishing +## | **gossipsub**: more sophisticated gossip based publishing + export pubsub, floodsub, gossipsub diff --git a/libp2p/protocols/pubsub/errors.nim b/libp2p/protocols/pubsub/errors.nim index cfb2ccc6d..5c4880455 100644 --- a/libp2p/protocols/pubsub/errors.nim +++ b/libp2p/protocols/pubsub/errors.nim @@ -1,6 +1,8 @@ # this module will be further extended in PR # https://github.com/status-im/nim-libp2p/pull/107/ +import ../../utility + type - ValidationResult* {.pure.} = enum + ValidationResult* {.pure, public.} = enum Accept, Reject, Ignore diff --git a/libp2p/protocols/pubsub/floodsub.nim b/libp2p/protocols/pubsub/floodsub.nim index b1b462ce8..6b0b2bd51 100644 --- a/libp2p/protocols/pubsub/floodsub.nim +++ b/libp2p/protocols/pubsub/floodsub.nim @@ -1,11 +1,11 @@ -## Nim-LibP2P -## Copyright (c) 2019 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-LibP2P +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. {.push raises: [Defect].} @@ -22,13 +22,15 @@ import ./pubsub, ../../peerinfo, ../../utility +## Simple flood-based publishing. + logScope: topics = "libp2p floodsub" const FloodSubCodec* = "/floodsub/1.0.0" type - FloodSub* = ref object of PubSub + FloodSub* {.public.} = ref object of PubSub floodsub*: PeerTable # topic to remote peer map seen*: TimedCache[MessageID] # message id:s already seen on the network seenSalt*: seq[byte] diff --git a/libp2p/protocols/pubsub/gossipsub.nim b/libp2p/protocols/pubsub/gossipsub.nim index 931cd57ba..6eaf5edc3 100644 --- a/libp2p/protocols/pubsub/gossipsub.nim +++ b/libp2p/protocols/pubsub/gossipsub.nim @@ -1,11 +1,13 @@ -## Nim-LibP2P -## Copyright (c) 2019 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-LibP2P +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. + +## Gossip based publishing {.push raises: [Defect].} diff --git a/libp2p/protocols/pubsub/gossipsub/behavior.nim b/libp2p/protocols/pubsub/gossipsub/behavior.nim index eda2e8cf9..701d2d93a 100644 --- a/libp2p/protocols/pubsub/gossipsub/behavior.nim +++ b/libp2p/protocols/pubsub/gossipsub/behavior.nim @@ -1,11 +1,11 @@ -## Nim-LibP2P -## Copyright (c) 2021 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-LibP2P +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. {.push raises: [Defect].} diff --git a/libp2p/protocols/pubsub/gossipsub/scoring.nim b/libp2p/protocols/pubsub/gossipsub/scoring.nim index 872ad0b6b..28ae245a5 100644 --- a/libp2p/protocols/pubsub/gossipsub/scoring.nim +++ b/libp2p/protocols/pubsub/gossipsub/scoring.nim @@ -1,11 +1,11 @@ -## Nim-LibP2P -## Copyright (c) 2021 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-LibP2P +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. {.push raises: [Defect].} diff --git a/libp2p/protocols/pubsub/gossipsub/types.nim b/libp2p/protocols/pubsub/gossipsub/types.nim index 11279eacf..7ce8f29cf 100644 --- a/libp2p/protocols/pubsub/gossipsub/types.nim +++ b/libp2p/protocols/pubsub/gossipsub/types.nim @@ -1,11 +1,11 @@ -## Nim-LibP2P -## Copyright (c) 2021 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-LibP2P +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. {.push raises: [Defect].} diff --git a/libp2p/protocols/pubsub/mcache.nim b/libp2p/protocols/pubsub/mcache.nim index 821fcea85..d35742fa0 100644 --- a/libp2p/protocols/pubsub/mcache.nim +++ b/libp2p/protocols/pubsub/mcache.nim @@ -1,11 +1,11 @@ -## Nim-LibP2P -## Copyright (c) 2019 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-LibP2P +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. {.push raises: [Defect].} diff --git a/libp2p/protocols/pubsub/peertable.nim b/libp2p/protocols/pubsub/peertable.nim index bff272d5f..c8eb86c2a 100644 --- a/libp2p/protocols/pubsub/peertable.nim +++ b/libp2p/protocols/pubsub/peertable.nim @@ -1,11 +1,11 @@ -## Nim-LibP2P -## Copyright (c) 2019 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-LibP2P +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. {.push raises: [Defect].} diff --git a/libp2p/protocols/pubsub/pubsub.nim b/libp2p/protocols/pubsub/pubsub.nim index 8ddea7389..97bd3a617 100644 --- a/libp2p/protocols/pubsub/pubsub.nim +++ b/libp2p/protocols/pubsub/pubsub.nim @@ -1,11 +1,17 @@ -## Nim-LibP2P -## Copyright (c) 2019 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-LibP2P +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. + +## Base interface for pubsub protocols +## +## You can `subscribe<#subscribe%2CPubSub%2Cstring%2CTopicHandler>`_ to a topic, +## `publish<#publish.e%2CPubSub%2Cstring%2Cseq%5Bbyte%5D>`_ something on it, +## and eventually `unsubscribe<#unsubscribe%2CPubSub%2Cstring%2CTopicHandler>`_ from it. {.push raises: [Defect].} @@ -75,37 +81,40 @@ declarePublicCounter(libp2p_pubsub_received_prune, "pubsub broadcast prune", lab type InitializationError* = object of LPError - TopicHandler* = proc(topic: string, + TopicHandler* {.public.} = proc(topic: string, data: seq[byte]): Future[void] {.gcsafe, raises: [Defect].} - ValidatorHandler* = proc(topic: string, + ValidatorHandler* {.public.} = proc(topic: string, message: Message): Future[ValidationResult] {.gcsafe, raises: [Defect].} TopicPair* = tuple[topic: string, handler: TopicHandler] - MsgIdProvider* = + MsgIdProvider* {.public.} = proc(m: Message): Result[MessageID, ValidationResult] {.noSideEffect, raises: [Defect], gcsafe.} - SubscriptionValidator* = + SubscriptionValidator* {.public.} = proc(topic: string): bool {.raises: [Defect], gcsafe.} + ## Every time a peer send us a subscription (even to an unknown topic), + ## we have to store it, which may be an attack vector. + ## This callback can be used to reject topic we're not interested in - PubSub* = ref object of LPProtocol + PubSub* {.public.} = ref object of LPProtocol switch*: Switch # the switch used to dial/connect to peers peerInfo*: PeerInfo # this peer's info topics*: Table[string, seq[TopicHandler]] # the topics that _we_ are interested in - peers*: Table[PeerId, PubSubPeer] ##\ - ## Peers that we are interested to gossip with (but not necessarily - ## yet connected to) - triggerSelf*: bool # trigger own local handler on publish - verifySignature*: bool # enable signature verification - sign*: bool # enable message signing + peers*: Table[PeerId, PubSubPeer] #\ + # Peers that we are interested to gossip with (but not necessarily + # yet connected to) + triggerSelf*: bool ## trigger own local handler on publish + verifySignature*: bool ## enable signature verification + sign*: bool ## enable message signing validators*: Table[string, HashSet[ValidatorHandler]] observers: ref seq[PubSubObserver] # ref as in smart_ptr - msgIdProvider*: MsgIdProvider # Turn message into message id (not nil) + msgIdProvider*: MsgIdProvider ## Turn message into message id (not nil) msgSeqno*: uint64 - anonymize*: bool # if we omit fromPeer and seqno from RPC messages we send + anonymize*: bool ## if we omit fromPeer and seqno from RPC messages we send subscriptionValidator*: SubscriptionValidator # callback used to validate subscriptions - topicsHigh*: int # the maximum number of topics a peer is allowed to subscribe to + topicsHigh*: int ## the maximum number of topics a peer is allowed to subscribe to maxMessageSize*: int ##\ ## the maximum raw message size we'll globally allow ## for finer tuning, check message size on topic validator @@ -411,7 +420,7 @@ method onTopicSubscription*(p: PubSub, topic: string, subscribed: bool) {.base.} proc unsubscribe*(p: PubSub, topic: string, - handler: TopicHandler) = + handler: TopicHandler) {.public.} = ## unsubscribe from a ``topic`` string ## p.topics.withValue(topic, handlers): @@ -424,12 +433,13 @@ proc unsubscribe*(p: PubSub, p.updateTopicMetrics(topic) -proc unsubscribe*(p: PubSub, topics: openArray[TopicPair]) = +proc unsubscribe*(p: PubSub, topics: openArray[TopicPair]) {.public.} = ## unsubscribe from a list of ``topic`` handlers for t in topics: p.unsubscribe(t.topic, t.handler) -proc unsubscribeAll*(p: PubSub, topic: string) = +proc unsubscribeAll*(p: PubSub, topic: string) {.public.} = + ## unsubscribe every `handler` from `topic` if topic notin p.topics: debug "unsubscribeAll called for an unknown topic", topic else: @@ -441,15 +451,14 @@ proc unsubscribeAll*(p: PubSub, topic: string) = proc subscribe*(p: PubSub, topic: string, - handler: TopicHandler) = + handler: TopicHandler) {.public.} = ## subscribe to a topic ## ## ``topic`` - a string topic to subscribe to ## - ## ``handler`` - is a user provided proc - ## that will be triggered - ## on every received message - ## + ## ``handler`` - user provided proc that + ## will be triggered on every + ## received message # Check that this is an allowed topic if p.subscriptionValidator != nil and p.subscriptionValidator(topic) == false: @@ -470,8 +479,9 @@ proc subscribe*(p: PubSub, method publish*(p: PubSub, topic: string, - data: seq[byte]): Future[int] {.base, async.} = + data: seq[byte]): Future[int] {.base, async, public.} = ## publish to a ``topic`` + ## ## The return value is the number of neighbours that we attempted to send the ## message to, excluding self. Note that this is an optimistic number of ## attempts - the number of peers that actually receive the message might @@ -490,14 +500,17 @@ method initPubSub*(p: PubSub) method addValidator*(p: PubSub, topic: varargs[string], - hook: ValidatorHandler) {.base.} = + hook: ValidatorHandler) {.base, public.} = + ## Add a validator to a `topic`. Each new message received in this + ## will be sent to `hook`. `hook` can return either `Accept`, + ## `Ignore` or `Reject` (which can descore the peer) for t in topic: trace "adding validator for topic", topicId = t p.validators.mgetOrPut(t, HashSet[ValidatorHandler]()).incl(hook) method removeValidator*(p: PubSub, topic: varargs[string], - hook: ValidatorHandler) {.base.} = + hook: ValidatorHandler) {.base, public.} = for t in topic: p.validators.withValue(t, validators): validators[].excl(hook) @@ -547,7 +560,7 @@ proc init*[PubParams: object | bool]( maxMessageSize: int = 1024 * 1024, rng: ref HmacDrbgContext = newRng(), parameters: PubParams = false): P - {.raises: [Defect, InitializationError].} = + {.raises: [Defect, InitializationError], public.} = let pubsub = when PubParams is bool: P(switch: switch, @@ -590,9 +603,9 @@ proc init*[PubParams: object | bool]( return pubsub -proc addObserver*(p: PubSub; observer: PubSubObserver) = p.observers[] &= observer +proc addObserver*(p: PubSub; observer: PubSubObserver) {.public.} = p.observers[] &= observer -proc removeObserver*(p: PubSub; observer: PubSubObserver) = +proc removeObserver*(p: PubSub; observer: PubSubObserver) {.public.} = let idx = p.observers[].find(observer) if idx != -1: p.observers[].del(idx) diff --git a/libp2p/protocols/pubsub/pubsubpeer.nim b/libp2p/protocols/pubsub/pubsubpeer.nim index 1b5348d63..55331e768 100644 --- a/libp2p/protocols/pubsub/pubsubpeer.nim +++ b/libp2p/protocols/pubsub/pubsubpeer.nim @@ -1,11 +1,11 @@ -## Nim-LibP2P -## Copyright (c) 2019 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-LibP2P +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. {.push raises: [Defect].} diff --git a/libp2p/protocols/pubsub/rpc/message.nim b/libp2p/protocols/pubsub/rpc/message.nim index 2750c88bf..ea978c3bb 100644 --- a/libp2p/protocols/pubsub/rpc/message.nim +++ b/libp2p/protocols/pubsub/rpc/message.nim @@ -1,11 +1,11 @@ -## Nim-LibP2P -## Copyright (c) 2019 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-LibP2P +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. {.push raises: [Defect].} diff --git a/libp2p/protocols/pubsub/rpc/messages.nim b/libp2p/protocols/pubsub/rpc/messages.nim index 1ebbf959b..c31d2b710 100644 --- a/libp2p/protocols/pubsub/rpc/messages.nim +++ b/libp2p/protocols/pubsub/rpc/messages.nim @@ -1,11 +1,11 @@ -## Nim-LibP2P -## Copyright (c) 2019 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-LibP2P +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. {.push raises: [Defect].} diff --git a/libp2p/protocols/pubsub/rpc/protobuf.nim b/libp2p/protocols/pubsub/rpc/protobuf.nim index 28b71a919..dda09f364 100644 --- a/libp2p/protocols/pubsub/rpc/protobuf.nim +++ b/libp2p/protocols/pubsub/rpc/protobuf.nim @@ -1,11 +1,11 @@ -## Nim-LibP2P -## Copyright (c) 2019 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-LibP2P +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. {.push raises: [Defect].} diff --git a/libp2p/protocols/pubsub/timedcache.nim b/libp2p/protocols/pubsub/timedcache.nim index ad47846fa..7cdf26b70 100644 --- a/libp2p/protocols/pubsub/timedcache.nim +++ b/libp2p/protocols/pubsub/timedcache.nim @@ -1,11 +1,11 @@ -## Nim-LibP2P -## Copyright (c) 2019 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-LibP2P +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. {.push raises: [Defect].} diff --git a/libp2p/protocols/relay.nim b/libp2p/protocols/relay.nim index acc042b13..0a08f98e4 100644 --- a/libp2p/protocols/relay.nim +++ b/libp2p/protocols/relay.nim @@ -1,11 +1,11 @@ -## Nim-LibP2P -## Copyright (c) 2022 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-LibP2P +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. {.push raises: [Defect].} diff --git a/libp2p/protocols/secure/noise.nim b/libp2p/protocols/secure/noise.nim index fac99a4f3..4051f09c8 100644 --- a/libp2p/protocols/secure/noise.nim +++ b/libp2p/protocols/secure/noise.nim @@ -1,11 +1,11 @@ -## Nim-LibP2P -## Copyright (c) 2020 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-LibP2P +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. {.push raises: [Defect].} diff --git a/libp2p/protocols/secure/plaintext.nim b/libp2p/protocols/secure/plaintext.nim index 7d712f8b7..c7e44a5ee 100644 --- a/libp2p/protocols/secure/plaintext.nim +++ b/libp2p/protocols/secure/plaintext.nim @@ -1,11 +1,11 @@ -## Nim-LibP2P -## Copyright (c) 2019 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-LibP2P +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. {.push raises: [Defect].} diff --git a/libp2p/protocols/secure/secio.nim b/libp2p/protocols/secure/secio.nim index 255600c48..f963a795d 100644 --- a/libp2p/protocols/secure/secio.nim +++ b/libp2p/protocols/secure/secio.nim @@ -1,11 +1,11 @@ -## Nim-LibP2P -## Copyright (c) 2019 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-LibP2P +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. {.push raises: [Defect].} diff --git a/libp2p/protocols/secure/secure.nim b/libp2p/protocols/secure/secure.nim index b68b5b14c..d3e22b57c 100644 --- a/libp2p/protocols/secure/secure.nim +++ b/libp2p/protocols/secure/secure.nim @@ -1,11 +1,11 @@ -## Nim-LibP2P -## Copyright (c) 2019 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-LibP2P +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. {.push raises: [Defect].} diff --git a/libp2p/routing_record.nim b/libp2p/routing_record.nim index b3410581c..10b584864 100644 --- a/libp2p/routing_record.nim +++ b/libp2p/routing_record.nim @@ -1,11 +1,11 @@ -## Nim-Libp2p -## Copyright (c) 2021 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-Libp2p +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. ## This module implements Routing Records. diff --git a/libp2p/signed_envelope.nim b/libp2p/signed_envelope.nim index 6568b6029..7806bfcf8 100644 --- a/libp2p/signed_envelope.nim +++ b/libp2p/signed_envelope.nim @@ -1,11 +1,11 @@ -## Nim-Libp2p -## Copyright (c) 2021 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-Libp2p +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. ## This module implements Signed Envelope. diff --git a/libp2p/stream/bufferstream.nim b/libp2p/stream/bufferstream.nim index 56f3151fa..cc3471a49 100644 --- a/libp2p/stream/bufferstream.nim +++ b/libp2p/stream/bufferstream.nim @@ -1,11 +1,11 @@ -## Nim-LibP2P -## Copyright (c) 2019 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-LibP2P +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. {.push raises: [Defect].} diff --git a/libp2p/stream/chronosstream.nim b/libp2p/stream/chronosstream.nim index 90677ea05..d7b5f1d1e 100644 --- a/libp2p/stream/chronosstream.nim +++ b/libp2p/stream/chronosstream.nim @@ -1,11 +1,11 @@ -## Nim-LibP2P -## Copyright (c) 2019 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-LibP2P +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. {.push raises: [Defect].} diff --git a/libp2p/stream/connection.nim b/libp2p/stream/connection.nim index 0e7b7202b..125bac5ad 100644 --- a/libp2p/stream/connection.nim +++ b/libp2p/stream/connection.nim @@ -1,11 +1,11 @@ -## Nim-LibP2P -## Copyright (c) 2020 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-LibP2P +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. {.push raises: [Defect].} diff --git a/libp2p/stream/lpstream.nim b/libp2p/stream/lpstream.nim index 168ef9102..ec3a7b28d 100644 --- a/libp2p/stream/lpstream.nim +++ b/libp2p/stream/lpstream.nim @@ -1,11 +1,13 @@ -## Nim-LibP2P -## Copyright (c) 2019 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-LibP2P +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. + +## Length Prefixed stream implementation {.push raises: [Defect].} @@ -15,6 +17,7 @@ import chronicles, chronos, metrics import ../varint, ../peerinfo, ../multiaddress, + ../utility, ../errors export errors @@ -132,26 +135,32 @@ method initStream*(s: LPStream) {.base.} = inc getStreamTracker(s.objName).opened trace "Stream created", s, objName = s.objName, dir = $s.dir -proc join*(s: LPStream): Future[void] = +proc join*(s: LPStream): Future[void] {.public.} = + ## Wait for the stream to be closed s.closeEvent.wait() -method closed*(s: LPStream): bool {.base.} = +method closed*(s: LPStream): bool {.base, public.} = s.isClosed -method atEof*(s: LPStream): bool {.base.} = +method atEof*(s: LPStream): bool {.base, public.} = s.isEof method readOnce*( s: LPStream, pbytes: pointer, nbytes: int): - Future[int] {.base, async.} = + Future[int] {.base, async, public.} = + ## Reads whatever is available in the stream, + ## up to `nbytes`. Will block if nothing is + ## available doAssert(false, "not implemented!") proc readExactly*(s: LPStream, pbytes: pointer, nbytes: int): - Future[void] {.async.} = + Future[void] {.async, public.} = + ## Waits for `nbytes` to be available, then read + ## them and return them if s.atEof: raise newLPStreamEOFError() @@ -180,7 +189,8 @@ proc readExactly*(s: LPStream, proc readLine*(s: LPStream, limit = 0, sep = "\r\n"): Future[string] - {.async.} = + {.async, public.} = + ## Reads up to `limit` bytes are read, or a `sep` is found # TODO replace with something that exploits buffering better var lim = if limit <= 0: -1 else: limit var state = 0 @@ -206,7 +216,7 @@ proc readLine*(s: LPStream, if len(result) == lim: break -proc readVarint*(conn: LPStream): Future[uint64] {.async, gcsafe.} = +proc readVarint*(conn: LPStream): Future[uint64] {.async, gcsafe, public.} = var buffer: array[10, byte] @@ -225,7 +235,7 @@ proc readVarint*(conn: LPStream): Future[uint64] {.async, gcsafe.} = if true: # can't end with a raise apparently raise (ref InvalidVarintError)(msg: "Cannot parse varint") -proc readLp*(s: LPStream, maxSize: int): Future[seq[byte]] {.async, gcsafe.} = +proc readLp*(s: LPStream, maxSize: int): Future[seq[byte]] {.async, gcsafe, public.} = ## read length prefixed msg, with the length encoded as a varint let length = await s.readVarint() @@ -241,10 +251,11 @@ proc readLp*(s: LPStream, maxSize: int): Future[seq[byte]] {.async, gcsafe.} = await s.readExactly(addr res[0], res.len) return res -method write*(s: LPStream, msg: seq[byte]): Future[void] {.base.} = +method write*(s: LPStream, msg: seq[byte]): Future[void] {.base, public.} = + # Write `msg` to stream, waiting for the write to be finished doAssert(false, "not implemented!") -proc writeLp*(s: LPStream, msg: openArray[byte]): Future[void] = +proc writeLp*(s: LPStream, msg: openArray[byte]): Future[void] {.public.} = ## Write `msg` with a varint-encoded length prefix let vbytes = PB.toBytes(msg.len().uint64) var buf = newSeqUninitialized[byte](msg.len() + vbytes.len) @@ -252,10 +263,10 @@ proc writeLp*(s: LPStream, msg: openArray[byte]): Future[void] = buf[vbytes.len.. +
$header
+
+$deprecationMsg +$desc +$seeSrc +
+ +""" + +doc.toc = """ + +
    +$content +
+""" + diff --git a/tests/pubsub/testfloodsub.nim b/tests/pubsub/testfloodsub.nim index 78513281c..dad332190 100644 --- a/tests/pubsub/testfloodsub.nim +++ b/tests/pubsub/testfloodsub.nim @@ -1,11 +1,11 @@ -## Nim-Libp2p -## Copyright (c) 2018 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-Libp2p +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. {.used.} diff --git a/tests/pubsub/testgossipsub.nim b/tests/pubsub/testgossipsub.nim index 9f15b4baf..86b4c3604 100644 --- a/tests/pubsub/testgossipsub.nim +++ b/tests/pubsub/testgossipsub.nim @@ -1,11 +1,11 @@ -## Nim-Libp2p -## Copyright (c) 2018 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-Libp2p +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. {.used.} diff --git a/tests/pubsub/testgossipsub2.nim b/tests/pubsub/testgossipsub2.nim index fa19447f9..fdb663cfc 100644 --- a/tests/pubsub/testgossipsub2.nim +++ b/tests/pubsub/testgossipsub2.nim @@ -1,11 +1,11 @@ -## Nim-Libp2p -## Copyright (c) 2018 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-Libp2p +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. {.used.} diff --git a/tests/testcrypto.nim b/tests/testcrypto.nim index 6bff6bb4b..b9a6ecfa4 100644 --- a/tests/testcrypto.nim +++ b/tests/testcrypto.nim @@ -1,11 +1,11 @@ -## Nim-Libp2p -## Copyright (c) 2018 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-Libp2p +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. ## Test vectors was made using Go implementation ## https://github.com/libp2p/go-libp2p-crypto/blob/master/key.go diff --git a/tests/testecnist.nim b/tests/testecnist.nim index 76999a4f3..339de4c43 100644 --- a/tests/testecnist.nim +++ b/tests/testecnist.nim @@ -1,11 +1,11 @@ -## Nim-Libp2p -## Copyright (c) 2018 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-Libp2p +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. import unittest2 import nimcrypto/utils import ../libp2p/crypto/[crypto, ecnist] diff --git a/tests/tested25519.nim b/tests/tested25519.nim index 1dde4fc3d..8b269f916 100644 --- a/tests/tested25519.nim +++ b/tests/tested25519.nim @@ -1,11 +1,11 @@ -## Nim-Libp2p -## Copyright (c) 2018 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-Libp2p +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. ## Test vectors are from RFC 8032 (https://tools.ietf.org/html/rfc8032) import unittest2 diff --git a/tests/testminasn1.nim b/tests/testminasn1.nim index 1dd5817ff..6bda13204 100644 --- a/tests/testminasn1.nim +++ b/tests/testminasn1.nim @@ -1,11 +1,11 @@ -## Nim-Libp2p -## Copyright (c) 2018 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-Libp2p +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. import unittest2 import ../libp2p/crypto/minasn1 import nimcrypto/utils as ncrutils diff --git a/tests/testminprotobuf.nim b/tests/testminprotobuf.nim index acbe9c927..fae8fd742 100644 --- a/tests/testminprotobuf.nim +++ b/tests/testminprotobuf.nim @@ -1,11 +1,11 @@ -## Nim-Libp2p -## Copyright (c) 2018 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-Libp2p +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. import unittest2 import ../libp2p/protobuf/minprotobuf diff --git a/tests/testnoise.nim b/tests/testnoise.nim index b1705dba1..7e503e4b3 100644 --- a/tests/testnoise.nim +++ b/tests/testnoise.nim @@ -1,11 +1,11 @@ -## Nim-LibP2P -## Copyright (c) 2020 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-LibP2P +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. {.used.} diff --git a/tests/testpeerid.nim b/tests/testpeerid.nim index 551bf12ed..bc1f0882b 100644 --- a/tests/testpeerid.nim +++ b/tests/testpeerid.nim @@ -1,11 +1,11 @@ -## Nim-Libp2p -## Copyright (c) 2018 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-Libp2p +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. ## Test vectors was made using Go implementation ## https://github.com/libp2p/go-libp2p-peer diff --git a/tests/testpkifilter.nim b/tests/testpkifilter.nim index b1ea8a839..4f5562ef6 100644 --- a/tests/testpkifilter.nim +++ b/tests/testpkifilter.nim @@ -1,11 +1,11 @@ -## Nim-Libp2p -## Copyright (c) 2018 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-Libp2p +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. when defined(nimHasUsed): {.used.} import unittest2 diff --git a/tests/testrsa.nim b/tests/testrsa.nim index a209f4446..83d6a8dd0 100644 --- a/tests/testrsa.nim +++ b/tests/testrsa.nim @@ -1,11 +1,11 @@ -## Nim-Libp2p -## Copyright (c) 2018 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-Libp2p +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. import unittest2 import nimcrypto/utils import ../libp2p/crypto/[crypto, rsa] diff --git a/tests/testsecp256k1.nim b/tests/testsecp256k1.nim index 9ac2bf9e2..03c2920c7 100644 --- a/tests/testsecp256k1.nim +++ b/tests/testsecp256k1.nim @@ -1,11 +1,11 @@ -## Nim-Libp2p -## Copyright (c) 2018 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-Libp2p +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. import unittest2 import ../libp2p/crypto/[crypto, secp] import nimcrypto/utils diff --git a/tools/pbcap_parser.nim b/tools/pbcap_parser.nim index af55e700e..a13ab794d 100644 --- a/tools/pbcap_parser.nim +++ b/tools/pbcap_parser.nim @@ -1,11 +1,11 @@ -## Nim-LibP2P -## Copyright (c) 2018 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. +# Nim-LibP2P +# Copyright (c) 2022 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. import std/[os, strutils, times] import chronicles import ../libp2p/debugutils