Skip to content
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,16 @@ chain stays in memory, so the next connection syncs from its tip. A chain below
the minimum work starts again from genesis.

```sh
elo <mainnet|testnet4|signet|regtest> <ip>[:port] # IPv6 as [::1]:8333
elo <mainnet|testnet4|signet|regtest> <ip|name>[:port] # IPv6 as [::1]:8333
```

The peer is an IP address or a name. Before each dial, elo looks up the name
with the resolver of the OS, within 30 s. It takes at most 256 of its
addresses, in a random order, and dials them in that order until one takes the
connect. So a name with many addresses, such as a DNS seed, gives a new peer
on each dial. A failed lookup is an `end`, and elo looks up the name again
after 5 s.

SIGTERM and SIGINT (Ctrl-C) stop elo at any step, also in the wait before a
dial. It logs the signal and exits with 0. elo exits with 1 only on a fault of
this host: its async runtime or its signal handlers do not start, a write to
Expand All @@ -67,15 +74,15 @@ container runtime, reads stdout and adds the time. elo sends nothing to a log se
```text
level=info event=version peer=127.0.0.1:18444 version=70016 services=0000000000000c09 user_agent=/Satoshi:31.1.0/ start_height=0 relay=true
level=info event=tip peer=127.0.0.1:18444 height=0 hash=0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206 work=0000000000000000000000000000000000000000000000000000000000000002
level=warn event=end peer=127.0.0.1:18444 reason="the peer closed the connection" wait_ms=5000
level=warn event=end destination=127.0.0.1:18444 reason="the peer closed the connection" wait_ms=5000
level=info event=stop signal=SIGTERM
```

| `event` | `level` | Fields after `event` |
| --- | --- | --- |
| `version` | `info` | `peer` `version` `services` `user_agent` `start_height` `relay` |
| `tip` | `info` | `peer` `height` `hash` `work` |
| `end` | `warn` | `peer` `reason` `wait_ms`: elo dials the peer again after `wait_ms` |
| `end` | `warn` | `destination` `reason` `wait_ms`: elo dials `destination` again after `wait_ms`. `destination` is the peer of the command line, also a name. `peer` is always the address of a connection |
| `exit` | `error` | `reason`: a fault of this host, and elo exits with 1 |
| `stop` | `info` | `signal`: elo exits with 0 |
| `panic` | `error` | `reason`: a bug, and elo exits with 101 |
Expand Down
16 changes: 11 additions & 5 deletions src/chainparams.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ const CHAIN_WORK_MIN_SIGNET: [u8; 32] = [
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0b, 0x46, 0x3e, 0xa0, 0xa4, 0xb8,
];

// `NonZeroU16::new` runs at compile time here, so a port of 0 does not build.
const PORT_MAINNET: std::num::NonZeroU16 = std::num::NonZeroU16::new(8333).expect("not 0");
const PORT_TESTNET4: std::num::NonZeroU16 = std::num::NonZeroU16::new(48333).expect("not 0");
const PORT_SIGNET: std::num::NonZeroU16 = std::num::NonZeroU16::new(38333).expect("not 0");
const PORT_REGTEST: std::num::NonZeroU16 = std::num::NonZeroU16::new(18444).expect("not 0");

impl Network {
/// The bytes that start every message.
pub(crate) const fn magic(self) -> [u8; MAGIC_SIZE] {
Expand All @@ -75,12 +81,12 @@ impl Network {
}

/// The port that a node of the network listens on.
pub const fn default_port(self) -> u16 {
pub const fn default_port(self) -> std::num::NonZeroU16 {
match self {
Network::Mainnet => 8333,
Network::Testnet4 => 48333,
Network::Signet => 38333,
Network::Regtest => 18444,
Network::Mainnet => PORT_MAINNET,
Network::Testnet4 => PORT_TESTNET4,
Network::Signet => PORT_SIGNET,
Network::Regtest => PORT_REGTEST,
}
}

Expand Down
27 changes: 19 additions & 8 deletions src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
use std::fmt::Write;

/// The most bytes of a value before its escapes. A longer value is cut, and ends with `...`. So
/// a peer that sends a long string cannot make a long line.
const VALUE_LENGTH_MAX: usize = 256;
/// a peer that sends a long string cannot make a long line. The longest destination of the
/// command line fits, so the `destination` of an `end` line is never cut.
const VALUE_LENGTH_MAX: usize = 260;

const _: () = assert!(crate::net::DESTINATION_LENGTH_MAX <= VALUE_LENGTH_MAX);

/// What `...` adds to a value that is cut.
const CUT_MARK: &str = "...";
Expand Down Expand Up @@ -52,9 +55,11 @@ pub enum Event<'a> {
peer: std::net::SocketAddr,
tip: &'a crate::chain::Tip,
},
/// The connection to `peer` ended for `reason`, and elo dials it again after `wait`.
/// The connection to `destination` ended for `reason`, and elo dials it again after `wait`.
/// `destination` is the peer of the command line: an IP address, or a name that elo looks up
/// again. It has its own key, so `peer` is always the address of a connection.
End {
peer: std::net::SocketAddr,
destination: &'a crate::net::Destination,
reason: &'a dyn std::fmt::Display,
wait: std::time::Duration,
},
Expand Down Expand Up @@ -112,8 +117,12 @@ impl Event<'_> {
line.field("hash", tip.hash_display());
line.field("work", tip.work());
}
Event::End { peer, reason, wait } => {
line.field("peer", peer);
Event::End {
destination,
reason,
wait,
} => {
line.field("destination", destination);
line.field("reason", reason);
line.field("wait_ms", wait.as_millis());
}
Expand Down Expand Up @@ -302,11 +311,13 @@ mod tests {
),
(
super::Event::End {
peer: PEER,
destination: &crate::net::Destination::Address(
crate::net::PeerAddress::new(PEER).unwrap(),
),
reason: &"the peer closed the connection",
wait: std::time::Duration::from_secs(5),
},
"level=warn event=end peer=127.0.0.1:18444 \
"level=warn event=end destination=127.0.0.1:18444 \
reason=\"the peer closed the connection\" wait_ms=5000",
),
(
Expand Down
Loading