3 releases (breaking)

0.4.0 Nov 5, 2022
0.3.0 Sep 26, 2022
0.2.0 Jan 21, 2021

#9 in #nakamoto

Download history 1/week @ 2024-11-13 3/week @ 2024-11-20 4/week @ 2024-11-27 10/week @ 2024-12-04 18/week @ 2024-12-11 11/week @ 2024-12-18 10/week @ 2024-12-25 10/week @ 2025-01-15 10/week @ 2025-01-22 5/week @ 2025-01-29 14/week @ 2025-02-05 74/week @ 2025-02-12 33/week @ 2025-02-19 24/week @ 2025-02-26

146 downloads per month
Used in 5 crates (4 directly)

MIT license

76KB
1.5K SLoC

I/O reactor that drives the protocol state machine.

The reactor translates network events into protocol events. This has the added benefit that it's trivial to swap nakamoto's networking code with a different implementation, as the code is fully self-contained.

To illustrate the above, lets trace the behavior of the system when a ping message is received via a peer connection to the client:

  1. The Reactor reads from the socket and decodes a NetworkMessage::Ping message.
  2. The Reactor wraps this message into a protocol input Input::Received(addr, NetworkMessage::Ping), where addr is the remote address of the socket on which it received this message.
  3. The Reactor calls Protocol::step(input, time), where input is the above input, and time is the current local time.
  4. The Protocol forwards this message to the PingManager, which constructs a new output Out::Message(addr, NetworkMessage::Pong), and forwards it upstream, to the reactor.
  5. The Reactor processes the output, encodes the raw message and writes it to the socket corresponding to the addr address, effectively sending a pong message back to the original sender.

Though simplified, the above steps provide a good mental model of how the reactor and protocol interplay to handle network events.

Dependencies

~1–1.6MB
~31K SLoC