77 Commits

Author SHA1 Message Date
Alex Potsides
750ed9c35f
fix: add timeout for incoming connections and build-in protocols (#1292)
Ensure that we don't wait forever for upgrading an inbound connection
to occur.

Note that transports should return an AbortableSource when passed an
AbortSignal so outbound connections to not need the same fix.

Also adds default timeouts for the ping, fetch, and identify protocols.
2022-07-14 13:35:12 +00:00
Alex Potsides
de30c2cec7
feat!: limit protocol streams per-connection (#1255)
* feat: limit protocol streams per-connection

Uses the `maxInboundStreams` and `maxOutboundStreams` of the `registrar.handle`
opts to limit the number of concurrent streams open on each connection
on a per-protocol basis.

Both values default to 1 so some tuning will be necessary to set
appropriate values for some protocols.

* chore: make error codes consistent

* chore: fix up examples
2022-06-17 15:46:31 +02:00
Alex Potsides
d4dd664071
feat!: update libp2p interfaces (#1252)
BREAKING CHANGE: uses new single-issue libp2p interface modules
2022-06-15 18:30:39 +01:00
Robert Kiel
35f9c0c793
fix: fix unintended aborts in dialer (#1185)
Fix a bug where `DialRequest` can abort wrong dial attempts.

Co-authored-by: Robert Kiel <robert.kiel@hoprnet.io>
2022-05-18 14:53:58 +01:00
Alex Potsides
a15254fdd4
fix: update to new interfaces (#1206)
Notably removes the `Dialer` interface as the `ConnectionManager` is now in charge of managing connections.
2022-05-04 10:19:04 +01:00
tuyennhv
d16817ca44
fix: emit peer:connect after all (#1171)
**Motivation**

In lodestar, when we handle "peer:connect" event, we dial the peer which gives another "peer:connect" event and it causes other issues

**Motivation**

In `onConnect` function, "peer:connect" event should be emitted after we add connection to the `connections` map so that when app dial the peer in "peer:connect" event handler, it uses the same/existing connection
2022-04-22 20:56:47 +01:00
Alex Potsides
c64a586a20
chore: update aegir to the latest version (#1186)
Removes boilerplate config that is no longer necessary
2022-04-09 09:26:25 +01:00
Alex Potsides
199395de4d
feat: convert to typescript (#1172)
Converts this module to typescript.

- Ecosystem modules renamed from (e.g.) `libp2p-tcp` to `@libp2p/tcp`
- Ecosystem module now have named exports
- Configuration has been updated, now pass instances of modules instead of classes:
- Some configuration keys have been renamed to make them more descriptive.  `transport` -> `transports`, `connEncryption` -> `connectionEncryption`.  In general where we pass multiple things, the key is now plural, e.g. `streamMuxer` -> `streamMuxers`, `contentRouting` -> `contentRouters`, etc.  Where we are configuring a singleton the config key is singular, e.g. `connProtector` -> `connectionProtector` etc.
- Properties of the `modules` config key have been moved to the root
- Properties of the `config` config key have been moved to the root
```js
// before
import Libp2p from 'libp2p'
import TCP from 'libp2p-tcp'

await Libp2p.create({
  modules: {
    transport: [
      TCP
    ],
  }
  config: {
    transport: {
      [TCP.tag]: {
        foo: 'bar'
      }
    },
    relay: {
      enabled: true,
      hop: {
        enabled: true,
        active: true
      }
    }
  }
})
```
```js
// after
import { createLibp2p } from 'libp2p'
import { TCP } from '@libp2p/tcp'

await createLibp2p({
  transports: [
    new TCP({ foo: 'bar' })
  ],
  relay: {
    enabled: true,
    hop: {
      enabled: true,
      active: true
    }
  }
})
```
- Use of `enabled` flag has been reduced - previously you could pass a module but disable it with config.  Now if you don't want a feature, just don't pass an implementation.   Eg:
```js
// before
await Libp2p.create({
  modules: {
    transport: [
      TCP
    ],
    pubsub: Gossipsub
  },
  config: {
    pubsub: {
      enabled: false
    }
  }
})
```
```js
// after
await createLibp2p({
  transports: [
    new TCP()
  ]
})
```
- `.multiaddrs` renamed to `.getMultiaddrs()` because it's not a property accessor, work is done by that method to calculate announce addresses, observed addresses, etc
- `/p2p/${peerId}` is now appended to all addresses returned by `.getMultiaddrs()` so they can be used opaquely (every consumer has to append the peer ID to the address to actually use it otherwise).  If you need low-level unadulterated addresses, call methods on the address manager.

BREAKING CHANGE: types are no longer hand crafted, this module is now ESM only
2022-03-28 14:30:27 +01:00
Alex Potsides
902f10d58d
fix: reject connections when not running (#1146)
When the node is shutting down, new connections can still be received.

If this happens we can end up writing into the datastore when it's
been closed which throws an error.

Instead, if we're not running, have the connection manager close new
incoming connections.
2022-01-26 10:52:23 +00:00
Alex Potsides
ff32eba6a0
feat: connection gater (#1142)
Port of https://github.com/libp2p/go-libp2p-core/blob/master/connmgr/gater.go

Adds a new configuration key `connectionGater` which allows denying the dialing of certain peers, individual multiaddrs and the creation of connections at certain points in the connection flow.

Fixes: https://github.com/libp2p/js-libp2p/issues/175
Refs: https://github.com/libp2p/js-libp2p/issues/744
Refs: https://github.com/libp2p/js-libp2p/issues/769

Co-authored-by: mzdws <8580712+mzdws@user.noreply.gitee.com>
2022-01-25 16:27:01 +00:00
Alex Potsides
4c3bf01f35
chore: fix flaky tests (#1137)
These tests are flaky in CI, probably due to differences in timing introduced by #1058

Fixes #1134
2022-01-21 08:44:13 +00:00
Alex Potsides
978eb3676f
feat: async peerstore backed by datastores (#1058)
We have a peerstore that keeps all data for all observed peers in memory with no eviction.

This is fine when you don't discover many peers but when using the DHT you encounter a significant number of peers so our peer storage grows and grows over time.

We have a persistent peer store, but it just periodically writes peers into the datastore to be read at startup, still keeping them in memory.

It also means a restart doesn't give you any temporary reprieve from the memory leak as the previously observed peer data is read into memory at startup.

This change refactors the peerstore to use a datastore by default, reading and writing peer info as it arrives.  It can be configured with a MemoryDatastore if desired.

It was necessary to change the peerstore and *book interfaces to be asynchronous since the datastore api is asynchronous.

BREAKING CHANGE: `libp2p.handle`, `libp2p.registrar.register` and the peerstore methods have become async
2022-01-20 12:03:35 +00:00
Alex Potsides
0a4dc54d08
fix: remove abort-controller dep (#1095)
The `AbortController` class is supported by browsers and node 14+ - we only support node 16+ (e.g. LTS+Current) so the `abort-controller` module isn't needed any more.
2022-01-20 12:02:13 +00:00
Alex Potsides
3bed7b4cb2
chore: update aegir (#1027)
Updates aegir, fixes all new linting errors.
2021-11-19 08:02:24 +00:00
Vasco Santos
3d25ff7fd0 chore: use new libp2p-noise 2021-08-20 09:13:21 +02:00
Vasco Santos
0f389a7828 chore: update uint8arrays 2021-08-20 09:13:21 +02:00
Vasco Santos
af723b355e
fix: do not allow dial to large number of multiaddrs (#954) 2021-07-09 08:46:24 +02:00
Vasco Santos
b291bc06ec
fix: dialer leaking resources after stopping (#947)
* fix: dialer leaking resources after stopping

* chore: add error code to test
2021-06-14 09:19:23 +02:00
Vasco Santos
06e8f3dd42
fix: do not add abort signals to useless addresses (#913) 2021-04-20 09:31:16 +02:00
Vasco Santos
21c9aeecb1
fix: dial protocol should throw if no protocol is provided (#914)
BREAKING CHANGE: dialProtocol does not return connection when no protocols are provided
2021-04-16 15:49:57 +02:00
Vasco Santos
8506414ea1
chore: config types and dependencies update (#904)
BREAKING CHANGES:

top level types were updated, multiaddr@9.0.0 is used, dialer and keychain internal property names changed and connectionManager minPeers is not supported anymore
2021-04-15 09:40:02 +02:00
Vasco Santos
1af8472dc6
chore: add transports example (#851) 2021-02-11 11:12:23 +01:00
Alex Potsides
0a6bc0d101
feat: add UPnP NAT manager (#810)
* feat: add uPnP nat manager

Adds a really basic nat manager that attempts to use UPnP to punch
a hole through your router for any IPV4 tcp addresses you have
configured.

Adds any configured addresses to the node's observed addresses list
and adds observed addresses to `libp2p.multiaddrs` so we exchange
them with peers when performing `identify` and people can dial you.

Adds configuration options under `config.nat`

Hole punching is async to not affect start up time.

Co-authored-by: Vasco Santos <vasco.santos@moxy.studio>
2021-01-27 14:55:26 +01:00
Vasco Santos
169bb806a7 chore: add typedefs (#802) 2020-12-16 13:56:41 +01:00
Vasco Santos
4ebcdb085c chore: update websockets (#806)
* chore: update websockets
2020-12-16 13:56:41 +01:00
Vasco Santos
585ad52b4c feat: custom dialer addr sorter (#792)
* feat: custom dialer addr sorter

* chore: use libp2p utils sorter via addressBook getMultiaddrsForPeer

* chore: use new libp2p utils

* chore: apply suggestions from code review

Co-authored-by: Jacob Heun <jacobheun@gmail.com>

Co-authored-by: Jacob Heun <jacobheun@gmail.com>
2020-12-16 13:56:41 +01:00
Vasco Santos
689c35ed1c fix: remove test/dialing/utils extra file 2020-12-16 13:56:41 +01:00
Vasco Santos
ef9d3ca2c6 feat: custom announce filter 2020-12-16 13:56:41 +01:00
Vasco Santos
bb83cacb5a chore: address review 2020-12-16 13:56:41 +01:00
Vasco Santos
43eda43f06 chore: create signed peer record on new listen addresses in transport manager 2020-12-16 13:56:41 +01:00
Vasco Santos
caf66ea143 feat: auto relay (#723)
* feat: auto relay

* fix: leverage protoBook events to ask relay peers if they support hop

* chore: refactor disconnect

* chore: do not listen on a relayed conn

* chore: tweaks

* chore: improve _listenOnAvailableHopRelays logic

* chore: default value of 1 to maxListeners on auto-relay
2020-12-16 13:56:41 +01:00
Vasco Santos
6350a187c7
fix: dial self (#826) 2020-12-09 16:13:25 +01:00
Vasco Santos
093c0ea13f
feat: resolve multiaddrs before dial (#782) 2020-11-04 13:54:50 +01:00
Jacob Heun
4c6be91588 fix: ensure streams are closed on connection close 2020-10-22 14:29:52 +02:00
Alex Potsides
1e869717ff fix: replace node buffers with uint8arrays (#730)
* fix: replace node buffers with uint8arrays

Upgrades all deps and replaces all use of node Buffers with Uint8Arrays

BREAKING CHANGES:

- All deps used by this module now use Uint8Arrays in place of node Buffers

* chore: browser fixes

* chore: remove .only

* chore: stringify uint8array before parsing

* chore: update interop suite

* chore: remove ts from build command

* chore: update deps

* fix: update records to use uint8array

* chore: fix lint

* chore: update deps

Co-authored-by: Jacob Heun <jacobheun@gmail.com>
2020-08-27 15:38:01 +02:00
Vasco Santos
f574e82a5d chore: apply suggestions from code review 2020-08-27 15:38:01 +02:00
Vasco Santos
74d414c21f chore: add certified peer records to persisted peer store 2020-08-27 15:38:01 +02:00
Vasco Santos
7da9ad44ab
fix: create dial target for peer with no known addrs (#715) 2020-07-20 14:54:03 +02:00
Vasco Santos
43630f1e0b test: use libp2p-noise (#585)
* chore: use libp2p-noise

* chore: address review
2020-05-28 12:37:48 +02:00
Vasco Santos
aaf62a40ec fix: libp2p connections getter 2020-05-28 12:37:48 +02:00
Vasco Santos
7fbd1556e8 fix: use libp2p.multiaddrs instead of listen
Co-Authored-By: Jacob Heun <jacobheun@gmail.com>
2020-05-28 12:37:48 +02:00
Vasco Santos
51474c334a chore: rename address functions and apply suggestions from code review
Co-Authored-By: Jacob Heun <jacobheun@gmail.com>
2020-05-28 12:37:48 +02:00
Vasco Santos
2a7967c1cc feat: address manager 2020-05-28 12:37:48 +02:00
Vasco Santos
d3a4bf0a3f chore: refactor connection manager and registrar 2020-05-28 12:37:48 +02:00
Vasco Santos
12e48adafa chore: remove peer-info usage
BREAKING CHANGE: all API methods with peer-info parameters or return values were changed. You can check the API.md document, in order to check the new values to use
2020-05-28 12:37:48 +02:00
Vasco Santos
e9d225c9dc feat: address and proto books (#590)
* feat: address and proto books

* chore: apply suggestions from code review

Co-Authored-By: Jacob Heun <jacobheun@gmail.com>

* chore: minor fixes and initial tests added

* chore: integrate new peer-store with code using adapters for other modules

* chore: do not use peerstore.put on get-peer-info

* chore: apply suggestions from code review

Co-Authored-By: Jacob Heun <jacobheun@gmail.com>

* chore: add new peer store tests

* chore: apply suggestions from code review

Co-Authored-By: Jacob Heun <jacobheun@gmail.com>

Co-authored-by: Jacob Heun <jacobheun@gmail.com>
2020-05-28 12:37:48 +02:00
Hugo Dias
9b13fe321a
fix: remove node global (#587)
* fix: use new libp2p-crypto

* fix: remove node globals and reduce size

- adds buffer require
- adds globalThis where needed
- streaming-iterables was remove and new utils created, this will be consolidated in `ipfs-utils` and backported here to normalize all these iterator helper functions
- latency-monitor was copied inside the repo
- iso-random-stream is now used instead of node crypto
- the test `should ignore self on discovery` was moved to node only

Size: 172.97KB
47.03KB below the 220KB limit.

`aegir build --node false` and `aegir test -t browser --node false` now work 🎉

* fix: fix require path

* fix: feedback

* fix: update deps and bundle size

* chore: bump interfaces

* chore: update size
2020-04-24 17:10:40 +02:00
robertkiel
c940f2d384
fix: add null check in libp2p.hangUp()
test: add hangup test and fix linting
2020-04-15 14:49:47 +02:00
Jacob Heun
e192eb6508
fix: make circuit relay listening addresses more forgiving (#604)
* test: dont be strict on nock mocking

* fix: be more forgiving with circuit listening addresses
2020-04-14 12:37:52 +02:00
Jacob Heun
a317a8b011
fix: dont allow multiaddr dials without a peer id (#558)
* fix: require peer ids when dialing multiaddrs

* chore: fix lint

* docs: add more info about multiaddr peer ids
2020-02-11 16:32:40 +01:00