Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions crates/randprotocol-node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -763,8 +763,16 @@ enum Cmd {
/// subscribing to and relaying on the legacy signed topic `rand/{chain}/tx` as well.
/// Since audit v12 (PRIV-601) a node never publishes its own transactions there unless
/// `--legacy-tx-publish`; this flag also stops it hearing and relaying older nodes' copies.
/// The default from v0.9.0 on (`--legacy-tx-relay` keeps the topic there).
#[arg(long)]
anonymous_tx_only: bool,
/// Keep subscribing to and relaying on the legacy signed topic `rand/{chain}/tx` on a
/// release that leaves it by default (v0.9.0 on), for a chain that still runs nodes from
/// before v0.8.3. No effect before v0.9.0, where the topic is kept anyway, nor under
/// `--anonymous-tx-only`. This node's own transactions still go on the anonymous lane only
/// unless `--legacy-tx-publish`.
#[arg(long)]
legacy_tx_relay: bool,
/// Also publish this node's transactions, signed, on the legacy topic `rand/{chain}/tx`
/// (audit v12, PRIV-601). Each copy there names this node as its publisher, which is what
/// the anonymous lane and the stem exist to hide, so it is off by default. Only for a
Expand Down Expand Up @@ -3058,6 +3066,7 @@ async fn main() -> Result<()> {
reserved_peer,
strict_gossip,
anonymous_tx_only,
legacy_tx_relay,
legacy_tx_publish,
dandelion,
dandelion_fluff_prob,
Expand Down Expand Up @@ -3164,7 +3173,7 @@ async fn main() -> Result<()> {
}, rpc_options, node::NetOptions {
reserved_peers: reserved_peer,
strict_gossip,
anonymous_tx_only,
anonymous_tx_only: randprotocol_node::network::anonymous_tx_only(anonymous_tx_only, legacy_tx_relay || legacy_tx_publish, env!("CARGO_PKG_VERSION")),
legacy_tx_publish,
dandelion: randprotocol_node::dandelion::DandelionConfig {
enabled: dandelion == "on",
Expand Down Expand Up @@ -4627,13 +4636,15 @@ mod tests {
let run = |extra: &[&str]| {
let args = ["rand-node", "run", "--datadir", "d", "--key", "k.json"].into_iter().chain(extra.iter().copied());
match Cli::try_parse_from(args).map(|c| c.cmd) {
Ok(Cmd::Run { legacy_tx_publish, anonymous_tx_only, .. }) => (legacy_tx_publish, anonymous_tx_only),
Ok(Cmd::Run { legacy_tx_publish, anonymous_tx_only, legacy_tx_relay, .. }) => (legacy_tx_publish, anonymous_tx_only, legacy_tx_relay),
_ => unreachable!("a run"),
}
};
assert_eq!(run(&[]), (false, false), "no signed legacy copy by default");
assert_eq!(run(&["--legacy-tx-publish"]), (true, false));
assert_eq!(run(&["--anonymous-tx-only"]), (false, true));
assert_eq!(run(&[]), (false, false, false), "no signed legacy copy by default");
assert_eq!(run(&["--legacy-tx-publish"]), (true, false, false));
assert_eq!(run(&["--anonymous-tx-only"]), (false, true, false));
// monorepo#27: the flag that keeps the legacy topic on a release that leaves it by default.
assert_eq!(run(&["--legacy-tx-relay"]), (false, false, true));
}

/// Issue #145: Dandelion++ is on by default at q = 0.1 and a 3 s embargo; each flag parses,
Expand Down
46 changes: 46 additions & 0 deletions crates/randprotocol-node/src/network/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1028,6 +1028,29 @@ fn dial_bootstrap(swarm: &mut Swarm<RandBehaviour>, cfg: &NetworkConfig, peers:
}
}

/// Whether a node gossips transactions on the anonymous lane only
/// ([`EdgeConfig::anonymous_tx_only`]), from its flags and the release it is (`version`, the
/// crate's `major.minor.patch[-pre]`). `--anonymous-tx-only` leaves the legacy signed topic on any
/// release; `--legacy-tx-relay` or `--legacy-tx-publish` keeps it on any release. With none of
/// them a release before v0.9.0 still subscribes and relays there (the v0.8.3 rolling upgrade,
/// issue #146) and v0.9.0 or later does not: the last phase of that roll, taken by the version so
/// it needs no flag on any unit and no edit at the bump (monorepo#27, PRIV-601). Every node since
/// v0.8.3 speaks the anonymous lane, so the two defaults interoperate. A version that does not
/// parse takes the private default.
pub fn anonymous_tx_only(anonymous_only: bool, keep_legacy: bool, version: &str) -> bool {
if anonymous_only {
return true;
}
if keep_legacy {
return false;
}
let mut parts = version.split(['.', '-']).map(|p| p.parse::<u64>().ok());
match (parts.next().flatten(), parts.next().flatten()) {
(Some(major), Some(minor)) => (major, minor) >= (0, 9),
_ => true,
}
}

/// How transactions leave this node besides the anonymous lane (issues #145, #146).
struct TxPaths {
/// Subscribe and relay on the legacy signed topic too ([`EdgeConfig::anonymous_tx_only`] off).
Expand Down Expand Up @@ -2635,6 +2658,29 @@ mod tests {
assert!(none.admit(p, 0, t), "but an empty frame costs nothing");
}

/// monorepo#27 (PRIV-601): which lanes a node gossips transactions on follows its release. A
/// v0.8.x node keeps the legacy signed topic unless `--anonymous-tx-only`; from v0.9.0 a node
/// leaves it unless `--legacy-tx-relay` or `--legacy-tx-publish` keeps it. The explicit
/// anonymous-only flag wins on every release, and an unreadable version is anonymous-only.
#[test]
fn the_legacy_transaction_topic_is_left_by_default_from_v0_9_0() {
for v in ["0.8.6", "0.8.99", "0.8.7-rc1", "0.1.0"] {
assert!(!anonymous_tx_only(false, false, v), "{v}: both lanes by default");
assert!(anonymous_tx_only(true, false, v), "{v}: --anonymous-tx-only");
assert!(anonymous_tx_only(true, true, v), "{v}: --anonymous-tx-only wins");
}
for v in ["0.9.0", "0.9.0-rc1", "0.10.2", "1.0.0"] {
assert!(anonymous_tx_only(false, false, v), "{v}: the anonymous lane only by default");
assert!(!anonymous_tx_only(false, true, v), "{v}: kept on request");
assert!(anonymous_tx_only(true, true, v), "{v}: --anonymous-tx-only wins");
}
assert!(anonymous_tx_only(false, false, "not a version"));
assert!(anonymous_tx_only(false, false, "1"));
// This build.
let v = env!("CARGO_PKG_VERSION");
assert_eq!(anonymous_tx_only(false, false, v), !v.starts_with("0.8."));
}

/// `EdgeConfig::default()` is what `start` passes: no reserved or bound peers, permissive
/// gossip, both transaction lanes. The `--strict-gossip`, `--reserved-peer` and
/// `--anonymous-tx-only` paths are opt-in.
Expand Down
149 changes: 147 additions & 2 deletions crates/randprotocol-node/tests/dandelion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ struct Net {
}

async fn start(gen: &Genesis, seed: u8, bootstrap: Vec<libp2p::Multiaddr>, dandelion: DandelionConfig) -> TestNode {
start_on_lanes(gen, seed, bootstrap, dandelion, false).await
}

/// [`start`], choosing the node's transaction lanes: `anonymous_tx_only` is what a node of v0.9.0
/// or later runs by default, off is a v0.8.x node's default (both lanes subscribed and relayed).
async fn start_on_lanes(gen: &Genesis, seed: u8, bootstrap: Vec<libp2p::Multiaddr>, dandelion: DandelionConfig, anonymous_tx_only: bool) -> TestNode {
let dir = tempfile::tempdir().unwrap();
std::fs::write(dir.path().join("genesis.json"), gen.to_json()).unwrap();
let key = Keypair::from_seed([seed; 32]).unwrap();
Expand Down Expand Up @@ -62,7 +68,7 @@ async fn start(gen: &Genesis, seed: u8, bootstrap: Vec<libp2p::Multiaddr>, dande
msauth: Default::default(),
},
RpcOptions::default(),
NetOptions { dandelion, ..NetOptions::default() },
NetOptions { dandelion, anonymous_tx_only, ..NetOptions::default() },
)
.await
.expect("node starts");
Expand All @@ -75,13 +81,24 @@ impl Net {
/// is connected to all the others — and, where its config speaks the stem, until it knows
/// every other stem-capable node as a stem peer — plus two gossip heartbeats for the mesh.
async fn start(configs: Vec<DandelionConfig>) -> Net {
Net::start_on_lanes(configs, &[]).await
}

/// [`Net::start`] with the nodes whose index is in `anonymous_only` on the anonymous
/// transaction lane only, the rest on both lanes.
async fn start_on_lanes(configs: Vec<DandelionConfig>, anonymous_only: &[usize]) -> Net {
common::cluster::init_tracing();
let gen = common::genesis(&minter());
let executor = node::executor_for_profile(&gen.fri_profile).unwrap();
let mut nodes: Vec<TestNode> = Vec::new();
for (i, cfg) in configs.iter().enumerate() {
let boot = nodes.iter().map(common::cluster::bootstrap_addr).collect();
nodes.push(start(&gen, 1 + i as u8, boot, cfg.clone()).await);
let node = if anonymous_only.contains(&i) {
start_on_lanes(&gen, 1 + i as u8, boot, cfg.clone(), true).await
} else {
start(&gen, 1 + i as u8, boot, cfg.clone()).await
};
nodes.push(node);
}
let n = nodes.len();
let capable = configs.iter().filter(|c| c.enabled).count();
Expand Down Expand Up @@ -371,3 +388,131 @@ async fn dandelion_latency_against_publishing_at_once() {
}
eprintln!("dandelion added {:.1} ms to the mean time to every mempool on loopback", report[1] - report[0]);
}

/// A first spy: a bare network node connected to every node of `net`, on both transaction lanes,
/// speaking no stem (so no node ever stems to it), that relays nothing and records, for each
/// transaction, the peer that first forwarded it — the first-spy estimator's guess at its origin —
/// and the lane each copy arrived on.
struct Spy {
handle: network::NetworkHandle,
seen: Arc<std::sync::Mutex<Vec<(Hash, libp2p::PeerId, network::GossipLane)>>>,
}

impl Spy {
async fn join(net: &Net) -> Spy {
let (handle, mut rx) = network::start_with(
NetworkConfig {
chain_id: net.gen.chain_id,
listen: vec!["/ip4/127.0.0.1/tcp/0".parse().unwrap()],
bootstrap: net.nodes.iter().map(common::cluster::bootstrap_addr).collect(),
enable_mdns: false,
limits: WireLimits::default(),
},
[98; 32],
EdgeConfig::default(),
)
.await
.unwrap();
let seen = Arc::new(std::sync::Mutex::new(Vec::new()));
let connected = Arc::new(std::sync::atomic::AtomicUsize::new(0));
let (record, count, reporter) = (seen.clone(), connected.clone(), handle.clone());
tokio::spawn(async move {
while let Some(ev) = rx.recv().await {
match ev {
NetworkEvent::PeerConnected(_) => {
count.fetch_add(1, Ordering::Relaxed);
}
NetworkEvent::Gossip { from, msg, id } => {
if let network::GossipMessage::Transaction(tx) = &msg {
record.lock().unwrap().push((tx.hash(), from, id.lane));
}
// Every delivery is answered; `Ignore`: the spy relays nothing.
reporter.report_validation(id, libp2p::gossipsub::MessageAcceptance::Ignore).await;
}
_ => {}
}
}
});
let n = net.nodes.len();
common::cluster::wait_for("the spy is connected to every node", Duration::from_secs(30), || connected.load(Ordering::Relaxed) >= n).await;
// Three gossip heartbeats, so every node has grafted the spy into its transaction mesh.
tokio::time::sleep(Duration::from_millis(3000)).await;
Spy { handle, seen }
}

/// The node (by index in `net`) that first forwarded `tx` to the spy.
fn first_forwarder(&self, net: &Net, tx: &Hash) -> Option<usize> {
let from = self.seen.lock().unwrap().iter().find(|(h, ..)| h == tx).map(|(_, from, _)| *from)?;
net.nodes.iter().position(|n| n.handle.network.local_peer_id == from)
}

/// How many transaction copies reached the spy on the legacy signed topic.
fn signed_copies(&self) -> usize {
self.seen.lock().unwrap().iter().filter(|(.., lane)| *lane == network::GossipLane::Signed).count()
}
}

/// The first-spy test (monorepo#27, PRIV-601), in process: the measurement that found the
/// submitter first in 27 of 27 traces on chain 21, re-run against this build's defaults with an
/// observer that is connected to every node and guesses each transaction's origin as the peer
/// that first forwarded it.
///
/// * Control, Dandelion off (how a node published before v0.8.3): the spy's guess is the
/// submitter for at least 8 of 10 transactions — the estimator works, so the next result means
/// something.
/// * Dandelion on at the defaults (q = 0.1, 3 s embargo), 20 transactions from rotating
/// submitters over a mixed fleet — two nodes on the anonymous lane only (a v0.9.0 node's
/// default), three on both lanes (a v0.8.x node's): the spy's guess is the submitter for at most
/// half of them, every mempool still gets every transaction, and both counts are printed.
/// * In both runs no copy reaches the spy on the legacy signed topic: nothing names a publisher.
///
/// Five nodes on loopback bound what this shows: with five nodes a stem ends back at the
/// submitter about one time in five. The fleet measurement is `docs/deploy.md`'s to record.
#[tokio::test(flavor = "multi_thread", worker_threads = 8)]
async fn a_first_spy_names_the_submitter_without_dandelion_and_rarely_with_it() {
// (label, configs, nodes on the anonymous lane only, transactions)
let runs: [(&str, Vec<DandelionConfig>, &[usize], usize); 2] = [
("off", (0..5).map(|_| DandelionConfig::off()).collect(), &[], 10),
("on (q = 0.1, b = 3 s)", (0..5).map(|i| DandelionConfig { seed: Some(60 + i), ..DandelionConfig::default() }).collect(), &[1, 3], 20),
];
let mut named = Vec::new();
for (label, configs, anonymous_only, txs) in runs {
let mut net = Net::start_on_lanes(configs, anonymous_only).await;
let spy = Spy::join(&net).await;
let (mut spy_named_submitter, mut submitter_published, mut unheard) = (0, 0, 0);
for k in 1..=txs {
let tx = net.mint();
let at_node = (k - 1) % 5;
let before = net.all_fluffs();
net.submit_and_watch(at_node, &tx, k, Duration::from_secs(30)).await;
submitter_published += usize::from(published_by(&before, &net.all_fluffs()).contains(&at_node));
let hash = tx.hash();
let heard = tokio::time::timeout(Duration::from_secs(10), async {
loop {
if let Some(first) = spy.first_forwarder(&net, &hash) {
return first;
}
tokio::time::sleep(Duration::from_millis(5)).await;
}
})
.await;
match heard {
Ok(first) => spy_named_submitter += usize::from(first == at_node),
Err(_) => unheard += 1,
}
tokio::time::sleep(Duration::from_millis(250)).await;
}
eprintln!(
"first spy, dandelion {label}: the spy named the submitter for {spy_named_submitter}/{txs}; the submitter itself published {submitter_published}/{txs}; unheard by the spy {unheard}; copies on the signed legacy topic {}",
spy.signed_copies()
);
assert_eq!(unheard, 0, "dandelion {label}: the spy missed a transaction, so its count means nothing");
assert_eq!(spy.signed_copies(), 0, "dandelion {label}: a transaction reached the spy on the signed legacy topic");
named.push((spy_named_submitter, txs));
spy.handle.shutdown().await;
net.stop().await;
}
let ((off, off_txs), (on, on_txs)) = (named[0], named[1]);
assert!(off * 10 >= off_txs * 8, "without Dandelion the first spy should name the submitter nearly always: {off}/{off_txs}");
assert!(on * 2 <= on_txs, "with Dandelion the first spy named the submitter for more than half: {on}/{on_txs}");
}
2 changes: 1 addition & 1 deletion docs/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ libp2p 0.54 over TCP with Noise and Yamux. Behaviours: **gossipsub**, twice —
legacy `/tx`) and anonymous (`MessageAuthenticity::Anonymous` under `ValidationMode::Anonymous`, on
its own protocol `/rand/<chain-id>/txsub`, topic `rand/<chain-id>/tx/2`: a transaction names no
author, issue #146; a node publishes its own transactions on the anonymous lane only and merely
relays the legacy `/tx`, unless `--legacy-tx-publish`, audit v12 PRIV-601) — **Kademlia** (protocol `/rand/<chain-id>/kad/1`)
relays the legacy `/tx`, unless `--legacy-tx-publish`, audit v12 PRIV-601; from v0.9.0 a node leaves the legacy `/tx` by default, `--legacy-tx-relay` keeps it) — **Kademlia** (protocol `/rand/<chain-id>/kad/1`)
seeded from `--bootstrap`, **identify**, optional **mDNS** for LAN discovery, **request-response**
(`/rand/sync/1`, CBOR) for block sync, and **ping** every 15 s. A node remembers every address it
dialled or learnt through identify and redials disconnected known peers every 30 s, in addition to
Expand Down
Loading
Loading