1345 Commits

Author SHA1 Message Date
Roman Borschel
dfbf5b65c5
Rename RawSwarm* to Network*. (#1194)
* Rename RawSwarm* to Network*.

To complete the cut performed in [1].

The only remaining mention of a "swarm" in libp2p-core is in some tests
which actually depend on libp2p-swarm.

[1]: https://github.com/libp2p/rust-libp2p/pull/1188

* Post-merge corrections.
2019-07-10 10:27:21 +02:00
Roman Borschel
ede114a13c
Tweak ipfs-kad example. (#1198)
The example typically runs into a lot of connection timeouts,
which may cause the query to time out. A query timeout currently
results in the example to be considered failed, but the example
should only be considered failed if no closest peers are found,
whether the query timed out or not.
2019-07-10 10:07:18 +02:00
Toralf Wittner
6aba7961d1 Replace unbounded channels with bounded ones. (#1191)
* Replace unbounded channels with bounded ones.

To remove the unbounded channels used for communicating with node tasks
an API similar to `futures::Sink` is used, i.e. sending is split into a
start and complete phase. The start phase returns `StartSend` and first
attempts to complete any pending send operations. Completing the send
means polling until `Poll::Ready(())` is returned.

In addition this PR has split the `handled_node_tasks` module into
several smaller ones (cf. `nodes::tasks`) and renamed some types:

- `nodes::handled_node_tasks::NodeTask` -> `nodes::tasks::task::Task`
- `nodes::handled_node_tasks::NodeTaskInner` -> `nodes::tasks::task::State`
- `nodes::handled_node_tasks::NodeTasks` -> `nodes::tasks::Manager`
- `nodes::handled_node_tasks::TaskClosedEvent` -> `nodes::tasks::Error`
- `nodes::handled_node_tasks::HandledNodesEvent` -> `nodes::tasks::Event`
- `nodes::handled_node_tasks::Task` -> `nodes::tasks::TaskEntry`
- `nodes::handled_node_tasks::ExtToInMessage` -> `nodes::tasks::task::ToTaskMessage`
- `nodes::handled_node_tasks::InToExtMessage` -> `nodes::tasks::task::FromTaskMessage`

* `take_over_to_complete` can be an `Option`.

Since it is always holding just a single pending message.

* `send_event_to_complete` can be an `Option`.

* Update core/src/nodes/tasks/manager.rs

Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>

* Update core/src/nodes/tasks/manager.rs

Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>

* Add comments to explain the need to flush sends ...

of take-over and event messages delivered over Sinks.
2019-07-09 16:47:24 +02:00
Shotaro Yamada
a0d278a479 Remove unused dependencies (#1195) 2019-07-08 19:17:51 +02:00
Fedor Sakharov
dc4d1ea990 Adds a store_mut method to kademlia. (#1192)
* Adds a retain method to kademlia.

* Add a store_mut getter to Kademlia

* Removes a blank line

* Changes store_mut comment appropriately

* Fixes build

* Return a type, not a trait
2019-07-08 10:57:49 +02:00
Jens Krause
c11cb4d1a1 FloodSub: Add publish_any (#1193)
* FloodSub: Add `publish_any`

to emit messages even if we are not subscribed to the topic.

* Review suggestion: Add `publish_many_inner`
2019-07-05 18:28:52 +02:00
Toralf Wittner
68c36d87d3
Move swarm and protocols handler into swarm crate. (#1188)
Move swarm and protocols handler into swarm crate.
2019-07-04 14:47:59 +02:00
Roman Borschel
ef9cb056b2
Kademlia: Address some TODOs - Refactoring - API updates. (#1174)
* Address some TODOs, refactor queries and public API.

The following left-over issues are addressed:

  * The key for FIND_NODE requests is generalised to any Multihash,
    instead of just peer IDs.
  * All queries get a (configurable) timeout.
  * Finishing queries as soon as enough results have been received is simplified
    to avoid code duplication.
  * No more panics in provider-API-related code paths. The provider API is
    however still untested and (I think) still incomplete (e.g. expiration
    of provider records).
  * Numerous smaller TODOs encountered in the code.

The following public API changes / additions are made:

  * Introduce a `KademliaConfig` with new configuration options for
    the replication factor and query timeouts.
  * Rename `find_node` to `get_closest_peers`.
  * Rename `get_value` to `get_record` and `put_value` to `put_record`,
    introducing a `Quorum` parameter for both functions, replacing the
    existing `num_results` parameter with clearer semantics.
  * Rename `add_providing` to `start_providing` and `remove_providing`
    to `stop_providing`.
  * Add a `bootstrap` function that implements a (almost) standard
    Kademlia bootstrapping procedure.
  * Rename `KademliaOut` to `KademliaEvent` with an updated list of
    constructors (some renaming). All events that report query results
    now report a `Result` to uniformly permit reporting of errors.

The following refactorings are made:

  * Introduce some constants.
  * Consolidate `query.rs` and `write.rs` behind a common query interface
    to reduce duplication and facilitate better code reuse, introducing
    the notion of a query peer iterator. `query/peers/closest.rs`
    contains the code that was formerly in `query.rs`. `query/peers/fixed.rs` contains
    a modified variant of `write.rs` (which is removed). The new `query.rs`
    provides an interface for working with a collection of queries, taking
    over some code from `behaviour.rs`.
  * Reduce code duplication in tests and use the current_thread runtime for
    polling swarms to avoid spurious errors in the test output due to aborted
    connections when a test finishes prematurely (e.g. because a quorum of
    results has been collected).
  * Some additions / improvements to the existing tests.

* Fix test.

* Fix rebase.

* Tweak kad-ipfs example.

* Incorporate some feedback.

* Provide easy access and conversion to keys in error results.
2019-07-03 16:16:25 +02:00
Toralf Wittner
8af4a28152
multihash: Use Bytes instead of Vec<u8> internally. (#1187)
multihash: Use `Bytes` instead of `Vec<u8>` internally.

To improve the efficiency of cloning multi-hashes (e.g. as the
representation of `PeerId`s), this PR replaces the `Vec<u8>`
representation with `Bytes`. The API is kept backwards
compatible and does not leak the representation type.
2019-07-03 14:27:26 +02:00
Pierre Krieger
404f1bdf4d
Publish v0.10.0 (#1185)
* Publish v0.10.0

* Add line for #1178
v0.10.0
2019-06-25 13:56:59 +02:00
Pierre Krieger
aca0d46b37
Fix #1135 (#1178)
* Fix #1135

* Address review

* Update core/src/swarm/registry.rs
2019-06-25 13:32:27 +02:00
Toralf Wittner
6999c36d78 libp2p_tcp: Ignore sockets without local/remote addr. (#1183)
The transport should be able to continue processing other connections.
An error to determine a socket's address is not a fatal condition but
may happen when a connection is immediately reset after being
established. By the time the programme asks for the remote address, the
socket may already be gone.

Fixes #1182.
2019-06-24 13:54:22 +02:00
Pierre Krieger
6d5aa86cef
Cleanup in spawning tasks (#1181) 2019-06-24 12:18:07 +02:00
Shotaro Yamada
e4d43030eb Make <SubstreamRef as AsyncWrite>::shutdown imply flush (#1180)
* Make `<SubstreamRef as AsyncWrite>::shutdown` imply flush

* Use try_ready

* Apply suggestions from code review

Co-Authored-By: Toralf Wittner <tw@dtex.org>
2019-06-24 10:42:33 +02:00
Roman Borschel
69bd0dfffb
Refactor iterative queries. (#1154)
Refactoring of iterative queries (`query.rs`) to improve both
correctness and performance (for larger DHTs):

Correctness:

  1. Queries no longer terminate prematurely due to counting results
     from peers farther from the target while results from closer
     peers are still pending. (#1105).

  2. Queries no longer ignore reported closer peers that are not duplicates
     just because they are currently not among the `num_results` closest.
     The currently `max_results` closest may contain peers marked as failed
     or pending / waiting. Hence all reported closer peers that are not
     duplicates must be considered candidates that may still end up
     among the `num_results` closest that successfully responded.

  3. Bounded parallelism based on the `active_counter` was not working
     correctly, as new (not yet contacted) peers closer to the target
     may be discovered at any time and thus appear in `closer_peers`
     before the already active / pending peers.

  4. The `Frozen` query mechanism allowed all remaining not-yet contacted
     peers to be contacted, but their results were discarded, because
     `inject_rpc_result` would only incorporate results while the
     query is `Iterating`. The `Frozen` state has been reworked into
     a `Stalled` state that implements a slightly more permissive
     variant of the following from the paper / specs: "If a round of
     FIND_NODEs fails to return a node any closer than the closest
     already seen, the initiator resends the FIND_NODE to all of the
     k closest nodes it has not already queried.". Importantly, though
     not explicitly mentioned, the query can move back to `Iterating`
     if it makes further progress again as a result of these requests.
     The `Stalled` state thus allows (temporarily) higher parallelism
     in an effort to make progress and bring the query to an end.

Performance:

  1. Repeated distance calculations between the same peers and the
     target is avoided.

  2. Enabled by #1108, use of a more appropriate data structure (`BTreeMap`) for
     the incrementally updated list of closer peers. The data structure needs
     efficient lookups (to avoid duplicates) and insertions at any position,
     both of which large(r) vectors are not that good at. Unscientific benchmarks
     showed a ~40-60% improvement in somewhat pathological scenarios with at least
     20 healthy nodes, each possibly returning a distinct list of closer 20 peers
     to the requestor. A previous assumption may have been that the vector always
     stays very small, but that is not the case in larger clusters: Even if the
     lists of closer peers reported by the 20 contacted peers are heavily overlapping,
     typically a lot more than 20 peers have to be (at least temporarily) considered
     as closest peers until the query completes. See also issue (2) above.

New tests are added for:

  * Query termination conditions.
  * Bounded parallelism.
  * Absence of duplicates.
2019-06-20 13:26:09 +02:00
Toralf Wittner
acebab07f3 Update soketto and enable deflate extension. (#1169)
* Update soketto and enable deflate extension.

* libp2p-deflate and libp2p-websocket share flate2.

Due to the way feature resolution works in cargo today, the `deflate`
feature of `soketto` will include `flate2` with feature `zlib` which is
then also active for the `flate2` that `libp2p-deflate` depends on. This
leads to compilation failures for WASM targets. This PR therefore moves
libp2p-deflate to the crates which are not available on WASM.
2019-06-20 13:03:34 +02:00
Tony Arcieri
3b4f8d7094 zeroize: Upgrade to v0.9 (#1179) 2019-06-19 10:52:49 +02:00
Age Manning
f6ce1e6b3a Remove redundant loop (#1176) 2019-06-18 10:42:40 +02:00
Pierre Krieger
b6378ac526
PollParameters is now a trait (#1177)
* PollParameters is now a trait

* Fix unused variable
2019-06-18 10:23:26 +02:00
Pierre Krieger
78d6f44e46
Allow customizing the Swarm with TConnInfo (#1172) 2019-06-12 16:21:39 +02:00
Fedor Sakharov
58015d1fb4
Report which key exactly was not found. (#1171) 2019-06-07 17:50:06 +03:00
Pierre Krieger
60228f7dcb
Update the IPFS bootnodes (#1170) 2019-06-07 14:38:36 +02:00
Pierre Krieger
9d0dfa56c0
Publish v0.9.1 (#1168) v0.9.1 2019-06-05 18:04:59 +02:00
Toralf Wittner
f7577c0f1f Update soketto dependency to 0.1.0. (#1167)
Also ensure error type in libp2p-websocket are `Sync`.
2019-06-05 17:43:33 +02:00
Pierre Krieger
134f472070
Implement Stream and Sink for EitherOutput (#1166) 2019-06-05 16:07:13 +02:00
Pierre Krieger
012c5a948b
Add comment to wasm_ext::Connection (#1165) 2019-06-05 15:49:26 +02:00
Roman Borschel
a2550215a0 Add AsRef<u8> for Multihash. (#1161)
* Add AsRef<u8> for Multihash.

* Bump patch version of multihash.
2019-06-05 10:27:06 +02:00
Pierre Krieger
f51573b628
Publish v0.9.0 (#1160)
* Publish v0.9.0

* Update CHANGELOG.md

Co-Authored-By: Roman Borschel <romanb@users.noreply.github.com>

* Published soketto
v0.9.0
2019-06-04 17:17:03 +02:00
Fedor Sakharov
22527e7eb6 Kademlia Records (#1144)
* initial implementation of the records

* move to multihash keys

* correctly process query results

* comments and formatting

* correctly return closer_peers in query

* checking wrong peer id in test

* Apply suggestions from code review

Co-Authored-By: Roman Borschel <romanb@users.noreply.github.com>

* Fix changes from suggestions

* Send responses to PUT_VALUE requests

* Shortcut in get_value

* Update protocols/kad/src/behaviour.rs

Co-Authored-By: Roman Borschel <romanb@users.noreply.github.com>

* Revert "Update protocols/kad/src/behaviour.rs"

This reverts commit 579ce742a7f4c94587f1e1f0866d2a3a37418efb.

* Remove duplicate insertion

* Adds a record to a PUT_VALUE response

* Fix a racy put_value test

* Store value ourselves only if we are in K closest

* Abstract over storage

* Revert "Abstract over storage": bad take

This reverts commit eaebf5b6d915712eaf3b05929577fdf697f204d8.

* Abstract over records storage using hashmap as default

* Constructor for custom records

* New Record type and its traits

* Fix outdated storage name

* Fixes returning an event

* Change FindNodeReq key type to Multihash

* WriteState for a second stage of a PUT_VALUE request

* GET_VALUE should not have a record

* Refactor a match arm

* Add successes and failures counters to PutValueRes

* If value is found no need to return closer peers

* Remove a custo storage from tests

* Rename a test to get_value_not_found

* Adds a TODO to change FindNode request key to Multihash

Co-Authored-By: Roman Borschel <romanb@users.noreply.github.com>

* Move MemoryRecordStorage to record.rs

* Return a Cow-ed Record from get

* Fix incorrect GET_VALUE parsing

* Various fixes with review

* Fixes get_value_not_found

* Fix peerids names in test

* another fix

* PutValue correctly distributes values

* Simplify the test

* Check that results are actually the closest

* Reverts changes to tests

* Fix the test topology and checking the results

* Run put_value test ten times

* Adds a get_value test

* Apply suggestions from code review

Co-Authored-By: Roman Borschel <romanb@users.noreply.github.com>

* Make Record fields public

* Moves WriteState to write.rs

* A couple of minor fixes

* Another few fixes of review

* Simplify the put_value test

* Dont synchronously return an error from put_value

* Formatting fixes and comments

* Collect a bunch of results

* Take exactly as much elements as neede

* Check if the peer is still connected

* Adds a multiple GetValueResults results number test

* Unnecessary mut iterators in put_value

* Ask for num_results in get_value

* Dont allocate twice in get_value

* Dont count same errored peer multiple times

* Apply suggestions from code review

Co-Authored-By: Roman Borschel <romanb@users.noreply.github.com>

* Fix another review

* Apply suggestions from code review

Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>

* Bring back FromIterator and improve a panic message

* Update protocols/kad/src/behaviour.rs

Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
2019-06-04 13:44:24 +02:00
Pierre Krieger
603fd5744f
No longer require fmt::Debug when not necessary (#1158) 2019-06-04 13:08:37 +02:00
Toralf Wittner
e56c4c10ed Reimplement the websocket transport. (#1150)
* Begin reimplementing the websocket transport.

* Add TLS support.

* Add support for redirects during handshake.

* Cosmetics.

* Remove unused error cases in tls module.

Left-overs from a previous implementation.

* No libp2p-websocket for wasm targets.

* Change tls::Config to make the server optional.

* Update transports/websocket/src/lib.rs

Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>

* Duplicate config methods.

As per PR review feedback.
2019-06-04 11:47:20 +02:00
Age Manning
34e7e35310 Add sign/verify raw_hash to secp256k1 (#1149) 2019-06-03 19:36:49 +02:00
Pierre Krieger
a27ce807ee
Some improvements to the docs of NetworkBehaviour (#1152)
* Some improvements to the docs of NetworkBehaviour

* Apply suggestions from code review

Co-Authored-By: Roman Borschel <romanb@users.noreply.github.com>
2019-06-03 18:16:02 +02:00
Toralf Wittner
7394d608a3 TCP transport: change listen address selection. (#1132)
* Map all 127.0.0.0/8 addresses to 127.0.0.1.

Since every local socket address in the 127.0.0.0/8 space is looped back
to 127.0.0.1/32 we should only have to report the later as the listen
address. For other addresses we still attempt to discover host addresses
when we encounter an unknown local address. We now also check that after
the host addresses have been reset that the address is now found,
otherwise we produce an error.

* Change listen address lookup.

Perform multiple steps:

1. Check for exact address match.
2. Else consider netmask and check for containment.
3. Else re-check host addresses and try 1 & 2 again.
4. Else report an error.

* Small fixes.

* Test and improve prefix_len.

* Simplify and inline the prefix_len logic.
2019-06-03 17:55:15 +02:00
Pierre Krieger
fbc6ea5c5e
Allow injecting an existing connection (#1157) 2019-06-03 17:27:43 +02:00
Roman Borschel
710ce90c6e
Kademlia: Fix premature query termination. (#1155)
* Fix premature query termination.

* Don't skip over 'NotContacted' closest peers.

* Code style tweak.
2019-06-03 12:47:01 +02:00
Roman Borschel
3440d1896e
Send on pending RPCs on established connection. (#1156) 2019-06-03 12:08:01 +02:00
Arve Knudsen
6a2a401458 Add deflate protocol (#1146) 2019-05-29 11:03:50 +02:00
Pierre Krieger
e7ab8eb1c3
Allow a path for WS multiaddresses (#1093)
* Allow a path for WS multiaddresses

* Fix tests

* Finish

* Tests

* Don't accept any path other than /
2019-05-24 14:12:44 +02:00
Pierre Krieger
39c476edeb
Bump secio and wasm-ext crates (#1145) 2019-05-24 11:37:04 +02:00
Thomas Eizinger
5866e6aa93 Add comit-rs to README (#1141) 2019-05-23 20:29:34 +02:00
Pierre Krieger
7c108b66f2
Fix small todo in handled tasks (#1114)
* Fix small todo in handled tasks

* Address review
2019-05-23 15:16:46 +02:00
Pierre Krieger
18bb40bbb9
Switch to parity-send-wrapper (#1143) 2019-05-23 14:04:09 +02:00
Roman Borschel
0bdd637b01
Kademlia: Fix status updates in KBuckets. (#1140)
* Fix status updates in KBuckets.

`KBucket::update` does currently not correctly update the `first_connected_pos`
before reinserting the node. This may result in connected nodes being considered
disconnected and thus eligible for replacement by a pending node if the bucket is
full and a new connected node is added.

Tests have been added for checking that `KBucket::update` preserves the status
and ordering of all other nodes in the bucket.

* Small test improvement.

Set an expectation for the new position, instead of taking the assigned position.
2019-05-23 12:18:13 +02:00
Pierre Krieger
6b78fa0cf7
Update dependencies (#1142)
* parking-lot to 0.8

* zeroize to 0.8
2019-05-23 11:14:34 +02:00
Roman Borschel
b6ab8e64d4
Update Kademlia protobuf. (#1138) 2019-05-22 15:47:46 +02:00
Roman Borschel
09f54df44d
Kademlia: Optimise iteration over closest keys / entries. (#1117)
* Kademlia: Optimise iteration over closest entries.

The current implementation for finding the entries whose keys are closest
to some target key in the Kademlia routing table involves copying the
keys of all buckets into a new `Vec` which is then sorted based on the
distances to the target and turned into an iterator from which only a
small number of elements (by default 20) are drawn.

This commit introduces an iterator over buckets for finding the closest
keys to a target that visits the buckets in the optimal order, based on
the information contained in the distance bit-string representing the
distance between the local key and the target.

Correctness is tested against full-table scans.

Also included:

  * Updated documentation.
  * The `Entry` API was moved to the `kbucket::entry` sub-module for
    ease of maintenance.
  * The pending node handling has been slightly refactored in order to
    bring code and documentation in agreement and clarify the semantics
    a little.

* Rewrite pending node handling and add tests.
2019-05-22 14:49:38 +02:00
Pierre Krieger
8adc5fa069
Add golem to the list of users (#1137) 2019-05-22 11:04:49 +02:00
TriplEight
5b97babe1c new dockerfile (#1122)
* new dockerfile

* caching
2019-05-17 19:55:50 +02:00
Roman Borschel
c80205454a
Improve XOR metric. (#1108)
There are two issues with the current definition and use of Kademlia's
XOR metric:

  1. The distance is currently equated with the bucket index, i.e.
     `distance(a,b) - 1` is the index of the bucket into which either
     peer is put by the other. The result is a metric that is not
     unidirectional, as defined in the Kademlia paper and as implemented
     in e.g. libp2p-go and libp2p-js, which is to interpret the result
     of the XOR as an integer in its entirety.

  2. The current `KBucketsPeerId` trait and its instances allow computing
     distances between types with differing bit lengths as well as between
     types that hash all inputs again (i.e. `KadHash`) and "plain" `PeerId`s
     or `Multihash`es. This can result in computed distances that are either
     incorrect as per the requirement of the libp2p specs that all distances
     are to be computed from the XOR of the SHA256 of the input keys, or
     even fall outside of the image of the metric used for the `KBucketsTable`.
     In the latter case, such distances are not currently used as a bucket index
     - they can only occur in the context of comparing distances for the purpose
     of sorting peers - but that still seems undesirable.

These issues are addressed here as follows:

  * Unidirectionality of the XOR metric is restored by keeping the "full"
    integer representation of the bitwise XOR. The result is an XOR metric
    as defined in the paper. This also opens the door to avoiding the
    "full table scan" when searching for the keys closest to a given key -
    the ideal order in which to visit the buckets can be computed with the
    help of the distance bit string.

  * As a simplification and to make it easy to "do the right thing", the
    XOR metric is only defined on an opaque `kbucket::Key` type, partially
    derived from the current `KadHash`. `KadHash` and `KBucketsPeerId`
    are removed.
2019-05-17 17:27:57 +02:00