From 4d1e69ba5c8d9e9a57ed47b4cd2363a1c80d16b5 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Thu, 5 Nov 2026 20:03:46 -0800 Subject: [PATCH 001/264] snap: keep the rate limiter's map bounded Dropping only full buckets bounds nothing while peers arrive faster than their buckets refill. In a release build 5,000 new peers asking once each left about 150 buckets against a bound of 64, and tracking_is_bounded_however_many_peers_appear failed. A newcomer that finds no room now takes the place of the bucket nearest full, which forgives the smallest debt there is; the whole-server bucket still holds everyone to the total. --- crates/sync/src/snap/rate.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/crates/sync/src/snap/rate.rs b/crates/sync/src/snap/rate.rs index 23d2fea..ca69897 100644 --- a/crates/sync/src/snap/rate.rs +++ b/crates/sync/src/snap/rate.rs @@ -116,7 +116,9 @@ pub struct RateLimiter { max_wait: Duration, /// Beyond this many tracked peers, idle buckets are dropped. A full bucket /// carries no information -- it is indistinguishable from a peer that has - /// never asked -- so dropping it costs nothing and bounds the map. + /// never asked -- so dropping it costs nothing. When that frees no room, + /// the bucket nearest full makes way for the newcomer, so the map never + /// holds more than this. capacity: usize, state: Mutex, } @@ -163,6 +165,25 @@ impl RateLimiter { b.refill(self.per_peer, cap, now); !b.is_full(cap) }); + // Dropping full buckets alone bounds nothing while peers arrive + // faster than buckets refill: in a release build, 5,000 new peers + // asking once each left about 150 behind against a bound of 64. + // A newcomer that still finds no room takes the place of the peer + // nearest to full, which forgives the smallest debt there is; the + // whole-server bucket still holds everyone to the total. + if !state.peers.contains_key(&peer) { + while state.peers.len() >= self.capacity { + let Some(nearest) = state + .peers + .iter() + .max_by(|a, b| a.1.tokens.total_cmp(&b.1.tokens)) + .map(|(p, _)| *p) + else { + break; + }; + state.peers.remove(&nearest); + } + } } state.global.refill(self.global, global_capacity, now); From cc51b30394e0bc7098a9a77e457b7cb6281bb7ff Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Thu, 5 Nov 2026 20:03:46 -0800 Subject: [PATCH 002/264] snap: walk the header chain down from the checkpoint, and quickly On mainnet the descending walk sat at 0.0% for hours. Five things held it there, each fixed here with a test that fails without it. - Header and skeleton requests went only to the few peers serving snapshots, and nothing was asked when none was connected. They are messages every peer serves, so they now go to any peer, and headers with uncles to rsk/63 peers. Only the snapshot messages, which rskj answers by disconnecting, stay with snap peers. - The skeleton queue was reversed, as the ascent's is, so the walk asked from genesis up and its frontier could not move until all nine million headers had arrived. It now asks from the checkpoint down. - The frontier is asked for from the hash the linked chain names for it, so a run there that does not link, a short answer or an empty one no longer strands the walk, and a stretch that has linked is never asked for again. Off-grid skeleton points, like the best block rskj ends every skeleton with, are not asked for. - A peer that leaves a request unanswered sits out for a minute, doubling up to sixteen, unless nobody else can be asked. About half of mainnet's peers never answered a header request, and each one's turn cost the walk thirty seconds. - The walk may have two requests outstanding per peer that can take them, up to 64, instead of the eight a state download is held to. Against mainnet the walk now descends at about 3,900 blocks a second, on course for about forty minutes for the whole chain. --- crates/networking/src/peers.rs | 33 ++++ crates/sync/src/service.rs | 38 +++-- crates/sync/src/snap/driver.rs | 226 +++++++++++++++++++++----- crates/sync/src/snap/header_tests.rs | 190 ++++++++++++++++++++++ crates/sync/src/snap/headers.rs | 90 +++++----- crates/sync/src/snap/session.rs | 48 +++++- crates/sync/src/snap/session_tests.rs | 194 +++++++++++++++++++++- 7 files changed, 723 insertions(+), 96 deletions(-) diff --git a/crates/networking/src/peers.rs b/crates/networking/src/peers.rs index c17954e..560ba4e 100644 --- a/crates/networking/src/peers.rs +++ b/crates/networking/src/peers.rs @@ -127,6 +127,20 @@ impl PeerStore { .collect() } + /// Peers that can be asked for headers with their uncles. + /// + /// The snapshot ascent asks only these: it needs each header's uncles, and + /// only a peer at `rsk/63` sends them. The descending walk asks anyone. + pub async fn peers_sending_header_uncles(&self) -> Vec { + self.connected_peers + .lock() + .await + .iter() + .filter(|(_, p)| p.caps.understands_header_uncles()) + .map(|(id, _)| *id) + .collect() + } + /// Whether this peer can serve a block at `number`. /// /// A peer that never stated a range serves everything: that is what every @@ -544,4 +558,23 @@ mod snap_capability_tests { "rsk/63 does not imply snap" ); } + + /// Headers with their uncles come only from `rsk/63` peers, and `snap` + /// says nothing about it either way. + #[tokio::test] + async fn header_uncles_are_asked_only_of_rsk63_peers() { + let store = PeerStore::new(); + let (tx, _rx) = mpsc::channel(4); + let rsk63 = B512::repeat_byte(1); + let rsk62_snap = B512::repeat_byte(2); + store + .add_peer_with(rsk63, tx.clone(), PeerCapabilities { rsk_version: 63, snap: false }) + .await; + store + .add_peer_with(rsk62_snap, tx, PeerCapabilities { rsk_version: 62, snap: true }) + .await; + + assert_eq!(store.peers_sending_header_uncles().await, vec![rsk63]); + assert_eq!(store.peers_serving_snapshots().await, vec![rsk62_snap]); + } } diff --git a/crates/sync/src/service.rs b/crates/sync/src/service.rs index 8d1583b..8855af8 100644 --- a/crates/sync/src/service.rs +++ b/crates/sync/src/service.rs @@ -1215,28 +1215,32 @@ impl SyncService { where F: FnOnce( &mut crate::snap::driver::SnapDriver, - &[B512], + crate::snap::driver::Peers<'_>, ) -> Vec, { - // Only peers that announced `snap`. Sending one of the six snapshot - // messages to a peer that did not is not a wasted request -- rskj - // throws out of `MessageType.valueOfType` on an unknown id and closes - // the connection, so an unfiltered round robin sheds every - // non-snap-capable peer, which on mainnet is most of them. - let peers: Vec = self.peer_store.peers_serving_snapshots().await; - if peers.is_empty() { - // Say so rather than falling back to every peer: a snapshot sync - // with nobody to ask should stall visibly and let the ordinary - // sync take over, not quietly disconnect the peer set. - debug!( - target: "rustock::snap", - "no peer has announced the snap capability; nothing to drive" - ); - return; + // Who can be sent what; `Peers` says why. The six snapshot messages go + // only to peers that announced `snap`: rskj throws out of + // `MessageType.valueOfType` on an unknown id and closes the connection, + // so an unfiltered round robin would shed every non-snap-capable peer, + // which on mainnet is most of them. The header walk's requests are + // ordinary messages that every peer serves, so they go to anyone, and + // the walk no longer waits on the few peers that serve snapshots. + let snap: Vec = self.peer_store.peers_serving_snapshots().await; + let uncles: Vec = self.peer_store.peers_sending_header_uncles().await; + let any: Vec = self.peer_store.peers().await; + if any.is_empty() { + // Say so rather than stall silently. The step still runs, so an + // answer that arrived just before its peer left is not thrown away; + // whatever the session asks next is handed back until a peer can + // take it. + debug!(target: "rustock::snap", "no peer connected; nothing can be asked"); } let Some(driver) = self.snap.as_mut() else { return }; - let outbound = step(driver, &peers); + let outbound = step( + driver, + crate::snap::driver::Peers { snap: &snap, uncles: &uncles, any: &any }, + ); // Charge whatever the step turned up before anything else: a peer that // misbehaved should pay for it even if the session went on to finish diff --git a/crates/sync/src/snap/driver.rs b/crates/sync/src/snap/driver.rs index f74a47f..f067a3a 100644 --- a/crates/sync/src/snap/driver.rs +++ b/crates/sync/src/snap/driver.rs @@ -25,11 +25,19 @@ use rustock_networking::protocol::{ }; use std::collections::HashMap; use std::time::{Duration, Instant}; -use tracing::{debug, warn}; +use tracing::{debug, trace, warn}; /// How long to wait on a snap request before offering the work elsewhere. const REQUEST_TIMEOUT: Duration = Duration::from_secs(30); +/// How long a peer that left a request unanswered sits out, doubling for each +/// further one it leaves in a row, up to [`BENCH_MAX`]. +const BENCH: Duration = Duration::from_secs(60); +const BENCH_MAX: Duration = Duration::from_secs(16 * 60); +/// Benched peers held before those whose time is up are swept out, so the +/// record stays bounded however many peers come and go. +const BENCH_SWEEP: usize = 1024; + /// rskj reads request ids through Java's signed long, so an id above 2^63 /// comes back sign-mangled. fn fresh_id() -> u64 { @@ -62,6 +70,58 @@ pub struct Outbound { pub message: P2pMessage, } +/// Who can be sent what. +/// +/// The six snapshot messages go only to peers that announced `snap`: rskj +/// resolves an incoming id through `MessageType.valueOfType`, which throws on an +/// unknown one, and closes the connection, so sending one to anyone else loses +/// the peer. The header walk's requests are the ordinary header and skeleton +/// messages, which every peer serves, so they go to anyone -- except during the +/// ascent, which asks for headers with their uncles and so needs `rsk/63`. +/// +/// Every request used to go to the `snap` peers alone. On mainnet those are +/// few, and when the one that offered the checkpoint stopped answering, the +/// walk stalled for good forty headers in, nine million short of genesis, +/// with fifteen ordinary peers connected that could have served it. +#[derive(Debug, Clone, Copy)] +pub struct Peers<'a> { + /// Peers that announced `snap`. + pub snap: &'a [B512], + /// Peers that can be asked for headers with their uncles (`rsk/63`). + pub uncles: &'a [B512], + /// Every connected peer. + pub any: &'a [B512], +} + +impl<'a> Peers<'a> { + /// One set of peers for every kind of request. + pub fn all(peers: &'a [B512]) -> Self { + Self { snap: peers, uncles: peers, any: peers } + } + + fn is_empty(&self) -> bool { + self.snap.is_empty() && self.uncles.is_empty() && self.any.is_empty() + } +} + +impl<'a> From<&'a [B512]> for Peers<'a> { + fn from(peers: &'a [B512]) -> Self { + Self::all(peers) + } +} + +impl<'a> From<&'a Vec> for Peers<'a> { + fn from(peers: &'a Vec) -> Self { + Self::all(peers) + } +} + +impl<'a, const N: usize> From<&'a [B512; N]> for Peers<'a> { + fn from(peers: &'a [B512; N]) -> Self { + Self::all(peers) + } +} + /// Something a peer did that it should be charged for. /// /// The driver names the peer and the offence; applying it is the caller's, @@ -112,6 +172,15 @@ pub struct SnapDriver { /// keep declining. Not an accusation -- just a note to stop wasting /// requests on them. unhelpful: std::collections::HashSet, + /// Peers that left a request unanswered: how many in a row, and until + /// when they sit out. + /// + /// On mainnet about half the peers connected never answered a header + /// request at all. The rotation gave each its turn regardless, and every + /// turn cost thirty seconds of a request the walk was waiting on. A + /// peer's time out ends, so one that was only slow for a while is asked + /// again; and any answer clears it. + benched: HashMap, } impl SnapDriver { @@ -122,6 +191,7 @@ impl SnapDriver { next_peer: 0, blame: Vec::new(), unhelpful: std::collections::HashSet::new(), + benched: HashMap::new(), } } @@ -238,11 +308,38 @@ impl SnapDriver { let peer = request.peer; self.release(request.what); self.charge(peer, EventType::TimeoutMessage, "snap request timed out"); + self.bench(peer, now); } } stale.len() } + /// Sit `peer` out after a request it left unanswered. + fn bench(&mut self, peer: B512, now: Instant) { + if self.benched.len() >= BENCH_SWEEP { + self.benched.retain(|_, (_, until)| *until > now); + } + let (strikes, until) = self.benched.entry(peer).or_insert((0, now)); + *strikes = strikes.saturating_add(1); + *until = now + BENCH.saturating_mul(1 << (*strikes - 1).min(4)).min(BENCH_MAX); + } + + /// Of `peers`, those not sitting out -- or all of them, if every one is. + /// Asking a peer that was slow beats asking nobody, and a sync left with + /// only such peers must still finish. + fn available(&self, peers: &[B512], now: Instant) -> Vec { + let ready: Vec = peers + .iter() + .copied() + .filter(|p| self.benched.get(p).is_none_or(|(_, until)| now >= *until)) + .collect(); + if ready.is_empty() { + peers.to_vec() + } else { + ready + } + } + fn release(&mut self, what: Pending) { match what { Pending::Status => {} @@ -271,29 +368,40 @@ impl SnapDriver { } /// Ask for whatever the session wants next, spreading the requests over - /// the peers given. - pub fn poll(&mut self, peers: &[B512]) -> Vec { + /// the peers that can take each kind. + pub fn poll<'p>(&mut self, peers: impl Into>) -> Vec { + let peers = peers.into(); if peers.is_empty() || self.is_finished() { return Vec::new(); } - let actions = self.ask(); + let actions = self.ask(peers); self.dispatch(actions, peers) } /// Ask the session for work, having first told it how much is already /// outstanding. /// - /// Every path that can produce requests goes through here. Capping only - /// `poll` left the five response handlers issuing a fresh budget's worth - /// each, which is how ~1,100 requests ended up outstanding against a - /// budget of 8. See #185. - fn ask(&mut self) -> Vec { - self.session.note_in_flight(self.in_flight.len()); + /// Every path that can produce requests goes through here or through + /// [`Self::note`]. Capping only `poll` left the five response handlers + /// issuing a fresh budget's worth each, which is how ~1,100 requests + /// ended up outstanding against a budget of 8. See #185. + fn ask(&mut self, peers: Peers<'_>) -> Vec { + self.note(peers); self.session.poll() } - /// The peers worth asking, which is all of them minus the ones that have - /// already said they cannot help. + /// Tell the session, before it decides what to ask for, what is already + /// outstanding and how many peers its header requests can go to. + fn note(&mut self, peers: Peers<'_>) { + self.session.note_in_flight(self.in_flight.len()); + let now = Instant::now(); + let headers = self.available(peers.any, now).len(); + let uncles = self.available(peers.uncles, now).len(); + self.session.note_header_peers(headers, uncles); + } + + /// The snapshot peers worth asking for state, which is all of them minus + /// the ones that have already said they cannot serve it. fn worth_asking(&self, peers: &[B512]) -> Vec { let willing: Vec = peers.iter().copied().filter(|p| !self.unhelpful.contains(p)).collect(); @@ -307,19 +415,21 @@ impl SnapDriver { } } - fn dispatch(&mut self, actions: Vec, peers: &[B512]) -> Vec { + fn dispatch(&mut self, actions: Vec, peers: Peers<'_>) -> Vec { let mut out = Vec::new(); if actions.is_empty() { return out; } - let peers = self.worth_asking(peers); - if peers.is_empty() { - return out; - } + // The ascent asks for headers with their uncles, which only `rsk/63` + // peers understand. The descending walk and the sampling gate ask for + // plain headers, which every peer serves. + let now = Instant::now(); + let header_peers = self.available( + if self.session.phase() == Phase::AscendingHeaders { peers.uncles } else { peers.any }, + now, + ); + let snap_peers = self.available(&self.worth_asking(peers.snap), now); for action in actions { - let peer = peers[self.next_peer % peers.len()]; - self.next_peer = self.next_peer.wrapping_add(1); - let id = fresh_id(); let (what, sub) = match action { Action::RequestStatus => ( @@ -355,6 +465,20 @@ impl SnapDriver { ), }; + let candidates: &[B512] = match what { + Pending::Headers { .. } | Pending::Skeleton { .. } => &header_peers, + Pending::Status | Pending::Chunk { .. } | Pending::Blocks => &snap_peers, + }; + if candidates.is_empty() { + // Nobody connected can be sent this one. Hand it back, so it is + // asked again once somebody can take it, rather than lose it. + self.release(what); + continue; + } + let peer = candidates[self.next_peer % candidates.len()]; + self.next_peer = self.next_peer.wrapping_add(1); + + trace!(target: "rustock::snap", "asking {:?} for {what:?} (request {id})", &peer.0[..4]); self.in_flight.insert(id, InFlight { what, peer, sent: Instant::now() }); out.push(Outbound { peer, @@ -370,7 +494,7 @@ impl SnapDriver { self.session.set_peer_serves_uncles(yes); } - pub fn on_status( + pub fn on_status<'p>( &mut self, id: u64, sender: B512, @@ -378,23 +502,24 @@ impl SnapDriver { difficulties: &[U256], trie_size: u64, grid: u64, - peers: &[B512], + peers: impl Into>, ) -> Vec { if !self.answers(id, sender, |p| matches!(p, Pending::Status)) { return Vec::new(); } - self.session.note_in_flight(self.in_flight.len()); + let peers = peers.into(); + self.note(peers); let actions = self.session.on_status(blocks, difficulties, trie_size, grid); self.charge_for_failure(sender); self.dispatch(actions, peers) } - pub fn on_headers( + pub fn on_headers<'p>( &mut self, id: u64, sender: B512, headers: &[Header], - peers: &[B512], + peers: impl Into>, ) -> Vec { let Some(point) = self.in_flight.get(&id).and_then(|r| match r.what { Pending::Headers { point, .. } if r.peer == sender => Some(point), @@ -403,8 +528,16 @@ impl SnapDriver { return Vec::new(); }; self.in_flight.remove(&id); - - self.session.note_in_flight(self.in_flight.len()); + self.benched.remove(&sender); + trace!( + target: "rustock::snap", + "{} header(s) from {:?} for #{point} (request {id})", + headers.len(), + &sender.0[..4] + ); + + let peers = peers.into(); + self.note(peers); let actions = self.session.on_headers(point, headers); self.charge_for_failure(sender); self.dispatch(actions, peers) @@ -416,12 +549,12 @@ impl SnapDriver { /// checkpoint, and in RSK that work includes every uncle the chain /// absorbed. Fed bare headers the walk can only compute a lower bound; fed /// these it computes the figure rskj computes. - pub fn on_headers_with_uncles( + pub fn on_headers_with_uncles<'p>( &mut self, id: u64, sender: B512, entries: &[rustock_networking::protocol::HeaderWithUncles], - peers: &[B512], + peers: impl Into>, ) -> Vec { let Some(point) = self.in_flight.get(&id).and_then(|r| match r.what { Pending::Headers { point, .. } if r.peer == sender => Some(point), @@ -430,25 +563,36 @@ impl SnapDriver { return Vec::new(); }; self.in_flight.remove(&id); + self.benched.remove(&sender); - self.session.note_in_flight(self.in_flight.len()); + let peers = peers.into(); + self.note(peers); let actions = self.session.on_headers_with_uncles(point, entries); self.charge_for_failure(sender); self.dispatch(actions, peers) } /// Block identifiers: where to ask, nothing more. - pub fn on_skeleton( + pub fn on_skeleton<'p>( &mut self, id: u64, sender: B512, identifiers: &[BlockIdentifier], - peers: &[B512], + peers: impl Into>, ) -> Vec { if !self.answers(id, sender, |p| matches!(p, Pending::Skeleton { .. })) { return Vec::new(); } - self.session.note_in_flight(self.in_flight.len()); + trace!( + target: "rustock::snap", + "skeleton of {} point(s) from {:?}, #{:?}..#{:?} (request {id})", + identifiers.len(), + &sender.0[..4], + identifiers.first().map(|i| i.number), + identifiers.last().map(|i| i.number) + ); + let peers = peers.into(); + self.note(peers); let actions = self.session.on_skeleton(identifiers); self.charge_for_failure(sender); self.dispatch(actions, peers) @@ -458,13 +602,13 @@ impl SnapDriver { /// /// The offset comes from this node's record of the request, never from /// the response. - pub fn on_chunk( + pub fn on_chunk<'p>( &mut self, id: u64, sender: B512, payload: &ChunkPayload, refusal: Refusal, - peers: &[B512], + peers: impl Into>, ) -> Vec { if self.in_flight.get(&id).is_some_and(|r| r.peer != sender) { debug!( @@ -477,6 +621,7 @@ impl SnapDriver { debug!(target: "rustock::snap", "chunk answer to unknown request {id}"); return Vec::new(); }; + self.benched.remove(&sender); let Pending::Chunk { from } = request.what else { warn!(target: "rustock::snap", "request {id} was not a chunk request"); self.release(request.what); @@ -487,7 +632,8 @@ impl SnapDriver { // before it is used, so a stray one cannot corrupt the download -- // but accepting it would still let an unasked peer consume the // request and leave the genuine answer to be dropped as unknown. - self.session.note_in_flight(self.in_flight.len()); + let peers = peers.into(); + self.note(peers); // The chunk that completes the download also verifies it, and that // walk takes about ten minutes on a mainnet state: single-threaded, @@ -508,18 +654,19 @@ impl SnapDriver { self.dispatch(actions, peers) } - pub fn on_blocks( + pub fn on_blocks<'p>( &mut self, id: u64, sender: B512, blocks: &[Block], difficulties: &[U256], - peers: &[B512], + peers: impl Into>, ) -> Vec { if !self.answers(id, sender, |p| matches!(p, Pending::Blocks)) { return Vec::new(); } - self.session.note_in_flight(self.in_flight.len()); + let peers = peers.into(); + self.note(peers); let actions = self.session.on_blocks(blocks, difficulties); self.charge_for_failure(sender); self.dispatch(actions, peers) @@ -546,6 +693,7 @@ impl SnapDriver { } Some(request) if want(request.what) => { self.in_flight.remove(&id); + self.benched.remove(&sender); true } Some(_) => { diff --git a/crates/sync/src/snap/header_tests.rs b/crates/sync/src/snap/header_tests.rs index 4c02798..e15852a 100644 --- a/crates/sync/src/snap/header_tests.rs +++ b/crates/sync/src/snap/header_tests.rs @@ -134,6 +134,44 @@ impl Fixture { } (requests, widest) } + + /// The same honest drive, recording every request and where the walk's + /// frontier stood when it was made. + fn run_logged(&self, walk: &mut HeaderWalk, budget: usize) -> Vec<(u64, Want)> { + let mut log = Vec::new(); + for _ in 0..10_000 { + if walk.is_done() { + break; + } + let wants = walk.wants(budget); + if wants.is_empty() { + break; + } + let frontier = walk.frontier(); + for want in wants { + log.push((frontier, want)); + match want { + Want::Skeleton { start } => walk.on_skeleton(&self.skeleton(start)), + Want::Headers { point, count, .. } => { + walk.on_headers(point, &self.headers(point, count), &Default::default()).expect("honest") + } + } + } + walk.advance().expect("honest answers link"); + } + log + } +} + +/// A chain of the same shape as `chain(n)` from a different genesis, so that +/// every hash differs while each run of it is internally perfect. +fn another_chain(n: u64) -> Vec
{ + let mut out = vec![header(0, B256::repeat_byte(0x99))]; + for i in 1..=n { + let parent = out[(i - 1) as usize].hash(); + out.push(header(i, parent)); + } + out } #[test] @@ -371,3 +409,155 @@ fn the_established_difficulty_counts_from_genesis_not_from_the_anchor() { ({established}); using it alone would refuse an honest peer" ); } + +/// **Top down.** The walk descends from the checkpoint, so it asks for the +/// skeletons from the top down, and its frontier comes a good part of the way +/// before it asks for the bottom of the chain. +/// +/// Asked for from genesis up, the frontier could not move until everything +/// had arrived: on mainnet, 0.0% for the two hours of the walk. +#[test] +fn the_walk_asks_from_the_top_down() { + let f = fixture(20_000); + let mut walk = f.walk(20_000, HeaderVerifier::new()); + let log = f.run_logged(&mut walk, 4); + assert!(walk.is_done()); + + let starts: Vec = log + .iter() + .filter_map(|(_, w)| match w { + Want::Skeleton { start } => Some(*start), + Want::Headers { .. } => None, + }) + .collect(); + assert_eq!(starts.first(), Some(&19_200), "the first skeleton is the one under the checkpoint"); + assert!(starts.windows(2).all(|w| w[0] > w[1]), "not asked from the top down: {starts:?}"); + + let (frontier, _) = log + .iter() + .find(|(_, w)| *w == Want::Skeleton { start: 0 }) + .expect("the bottom is asked for in the end"); + assert!( + *frontier < 20_000 * 2 / 3, + "the frontier was still at #{frontier} when the bottom of the chain was asked for" + ); +} + +/// **Nothing is fetched twice.** A stretch that has linked is behind the +/// frontier, and the walk never asks for it again. +#[test] +fn a_linked_stretch_is_never_asked_for_again() { + let f = fixture(20_000); + let mut walk = f.walk(20_000, HeaderVerifier::new()); + let log = f.run_logged(&mut walk, 8); + assert!(walk.is_done()); + + let mut asked = std::collections::HashSet::new(); + for (_, want) in &log { + if let Want::Headers { point, .. } = want { + assert!(asked.insert(*point), "#{point} was asked for twice"); + } + } + // Every grid point above genesis, and the checkpoint's own stretch. + assert_eq!(asked.len() as u64, 20_000 / HEADER_CHUNK + 1); +} + +/// **The frontier is never lost.** A peer answers the walk's frontier from +/// another chain. The run does not link and is dropped, and the walk asks for +/// that height again, from the hash its own chain names for it, which no +/// peer chose. Once nothing asked for it again, and the walk stopped there. +#[test] +fn a_frontier_answered_from_another_chain_is_asked_for_again() { + let f = fixture(1000); + let forked = another_chain(1000); + let mut walk = f.walk(1000, HeaderVerifier::new()); + walk.on_headers(1000, &f.headers(1000, (1000 - 960) as u32), &Default::default()).expect("honest"); + walk.advance().expect("links"); + assert_eq!(walk.frontier(), 960); + + let frontier = Want::Headers { from: f.chain[960].hash(), count: HEADER_CHUNK as u32, point: 960 }; + assert!(walk.wants(8).contains(&frontier), "the walk asks for its frontier from the hash it holds"); + + // Right height, wrong chain. + let run: Vec
= (769..=960).rev().map(|i| forked[i as usize].clone()).collect(); + walk.on_headers(960, &run, &Default::default()).expect("internally valid"); + walk.advance().expect("no error, just no progress"); + assert_eq!(walk.frontier(), 960, "a foreign run moved the walk"); + + assert!(walk.wants(8).contains(&frontier), "the frontier was not asked for again"); + walk.on_headers(960, &f.headers(960, HEADER_CHUNK as u32), &Default::default()).expect("honest"); + walk.advance().expect("links"); + assert_eq!(walk.frontier(), 768); +} + +/// **A short answer costs a request, not the walk.** A peer may send fewer +/// headers than were asked for -- rskj stops at the first parent it lacks -- +/// which leaves the frontier between grid points, where no skeleton point +/// will ever sit. The walk asks from there to the next grid point down, and +/// is back on the grid. +#[test] +fn a_short_answer_does_not_strand_the_walk() { + let f = fixture(1000); + let mut walk = f.walk(1000, HeaderVerifier::new()); + walk.on_headers(1000, &f.headers(1000, (1000 - 960) as u32), &Default::default()).expect("honest"); + walk.advance().expect("links"); + + // 100 of the 192 asked for. + walk.on_headers(960, &f.headers(960, 100), &Default::default()).expect("honest, if short"); + walk.advance().expect("links"); + assert_eq!(walk.frontier(), 860); + + let rest = Want::Headers { from: f.chain[860].hash(), count: 860 - 768, point: 860 }; + assert!(walk.wants(8).contains(&rest), "the walk did not ask for the rest of the stretch"); + walk.on_headers(860, &f.headers(860, 860 - 768), &Default::default()).expect("honest"); + walk.advance().expect("links"); + assert_eq!(walk.frontier(), 768, "not back on the grid"); + + f.run(&mut walk, 8); + assert!(walk.is_done()); + assert_eq!(walk.frontier(), 0); +} + +/// **The anchor is the floor.** A node that already holds most of the chain +/// walks only the part above it. Asked for from genesis up, the walk began +/// with exactly the part the node had. +#[test] +fn a_walk_that_anchors_above_genesis_leaves_the_chain_below_alone() { + let f = fixture(20_000); + let anchor = 15_360u64; + for h in &f.chain[1..=anchor as usize] { + f.store.put_header_with_hash(h.hash(), h).expect("header"); + f.store.put_canonical_hash(h.number, h.hash()).expect("index"); + } + + let mut walk = f.walk(20_000, HeaderVerifier::new()); + let log = f.run_logged(&mut walk, 4); + assert!(walk.is_done()); + assert_eq!(walk.frontier(), anchor); + + for (_, want) in &log { + if let Want::Headers { point, .. } = want { + assert!(*point > anchor, "asked for #{point}, which this node holds"); + } + } + assert!( + !log.iter().any(|(_, w)| *w == Want::Skeleton { start: 0 }), + "asked for the bottom of a chain this node holds" + ); +} + +/// An rskj skeleton ends with the peer's best block, wherever that sits. Off +/// the grid no run from it can link, so it is never asked for. +#[test] +fn an_identifier_off_the_grid_is_not_asked_for() { + let f = fixture(1000); + let mut walk = f.walk(1000, HeaderVerifier::new()); + walk.on_skeleton(&[ + BlockIdentifier { number: 768, hash: f.chain[768].hash() }, + BlockIdentifier { number: 900, hash: f.chain[900].hash() }, + ]); + + let wants = walk.wants(8); + assert!(wants.iter().any(|w| matches!(w, Want::Headers { point: 768, .. })), "{wants:?}"); + assert!(!wants.iter().any(|w| matches!(w, Want::Headers { point: 900, .. })), "{wants:?}"); +} diff --git a/crates/sync/src/snap/headers.rs b/crates/sync/src/snap/headers.rs index 040856a..b17aec0 100644 --- a/crates/sync/src/snap/headers.rs +++ b/crates/sync/src/snap/headers.rs @@ -182,9 +182,14 @@ impl HeaderWalk { skeleton_todo.push(start); start += span; } - // Asked for from the top down: the walk finishes at the bottom, but it - // is the top that unblocks it. - skeleton_todo.reverse(); + // Popped from the back, so the highest start is asked for first: the + // walk finishes at the bottom, but it is the top that unblocks it. + // + // Left ascending for that reason. The ascent reverses its queue + // because it wants the low end first; reversed here as well, the walk + // asked from genesis up, and its frontier sat at the checkpoint until + // all nine million mainnet headers had arrived -- 0.0% for the whole + // two hours, with nothing to tell that from a hang. // A walk feeds the freezer only when there is nothing to lose by // doing so: an empty freezer, on a node syncing from scratch. On a @@ -277,51 +282,57 @@ impl HeaderWalk { /// Up to `budget` things to ask for, none of them already outstanding. pub fn wants(&mut self, budget: usize) -> Vec { let mut out = Vec::new(); - if self.done { + if self.done || budget == 0 || self.need_number == 0 { return out; } + let need = self.need_number; + + // The frontier first, asked for from the hash the linked chain names + // for it. That hash is this node's own finding, not a peer's claim, + // so the request needs no skeleton: it is the serial walk, running + // beneath the parallel one. It is also the only way back to the + // frontier once its point is gone -- a run there that does not link + // takes the point with it, and a short answer leaves the need between + // grid points, where no point sits -- and either used to stop the + // walk for good. + // + // It reaches down to the grid point below, so that the run under it + // can link: all 192 from a point on the grid, the remainder from the + // checkpoint or from wherever a short answer stopped. + if !self.headers_sent.contains(&need) && !self.runs.contains_key(&need) { + let floor = ((need - 1) / HEADER_CHUNK) * HEADER_CHUNK; + self.headers_sent.insert(need); + out.push(Want::Headers { from: self.need_hash, count: (need - floor) as u32, point: need }); + if out.len() >= budget { + return out; + } + } - // Headers first: a skeleton is only useful once its points are being - // turned into headers, and the queue below is what finishes the walk. - let points: Vec = self + // Then the points below the frontier, highest first: a skeleton is + // only useful once its points are being turned into headers. Only + // below, because a point at or above the frontier is linked already, + // and asking for it again would fetch that stretch twice. + let points: Vec<(u64, B256)> = self .points - .keys() + .range(..need) .rev() - .copied() - .filter(|p| !self.headers_sent.contains(p) && !self.runs.contains_key(p)) - .take(budget) + .filter(|(p, _)| !self.headers_sent.contains(*p) && !self.runs.contains_key(*p)) + .take(budget - out.len()) + .map(|(p, hash)| (*p, *hash)) .collect(); - for point in points { - let Some(hash) = self.points.get(&point).copied() else { continue }; + for (point, hash) in points { self.headers_sent.insert(point); out.push(Want::Headers { from: hash, count: HEADER_CHUNK as u32, point }); - if out.len() >= budget { - return out; - } - } - - // The stretch above the grid, which no skeleton point covers. It stops - // one *above* the grid top rather than at it: the grid's own run ends - // at its point, so a bespoke run reaching that far would overlap it and - // the two would not link. - let grid_top = (self.top.number / HEADER_CHUNK) * HEADER_CHUNK; - if self.top.number > grid_top - && !self.headers_sent.contains(&self.top.number) - && !self.runs.contains_key(&self.top.number) - { - self.headers_sent.insert(self.top.number); - out.push(Want::Headers { - from: self.top.hash(), - count: (self.top.number - grid_top) as u32, - point: self.top.number, - }); - if out.len() >= budget { - return out; - } } + // Skeletons with whatever is left, the highest first. One that starts + // at or above the frontier could name only linked points, and the + // frontier never rises, so it is dropped rather than asked. while out.len() < budget { let Some(start) = self.skeleton_todo.pop() else { break }; + if start >= need { + continue; + } if self.skeleton_sent.insert(start) { out.push(Want::Skeleton { start }); } @@ -346,7 +357,11 @@ impl HeaderWalk { /// Skeleton identifiers: where to ask, and nothing more. pub fn on_skeleton(&mut self, identifiers: &[BlockIdentifier]) { for id in identifiers { - if id.number == 0 || id.number > self.top.number { + // Only a grid point below the frontier can lead anywhere. Above + // it the chain is linked already. Off the grid no run can link, + // and one identifier usually is: rskj ends every skeleton with + // its best block, wherever that sits. + if id.number == 0 || id.number >= self.need_number || id.number % HEADER_CHUNK != 0 { continue; } // A point already answered for is not replaced: the run that @@ -547,6 +562,7 @@ impl HeaderWalk { } self.runs.remove(&self.need_number); + self.points.remove(&self.need_number); // The run covered down to its oldest header, so what is needed // next is that header's parent: one lower, and named by it. self.need_number = run.oldest_number.saturating_sub(1); diff --git a/crates/sync/src/snap/session.rs b/crates/sync/src/snap/session.rs index 38cecab..c9f27c6 100644 --- a/crates/sync/src/snap/session.rs +++ b/crates/sync/src/snap/session.rs @@ -45,6 +45,19 @@ use tracing::{debug, info, warn}; /// concludes the chain does not connect to ours. const EMPTY_HEADER_REPLIES_ALLOWED: u32 = 5; +/// Header requests the walk may have outstanding for each peer that can take +/// them. +/// +/// Header and skeleton requests are messages every peer serves, so their +/// budget grows with the peers there are, where a state download is held to +/// `SnapConfig::max_in_flight` by the few peers that serve state. On mainnet +/// that budget of eight left forty peers mostly idle through a walk of +/// hours. Two keeps a peer busy while its last answer is on the way, without +/// queueing work behind a slow one. +const HEADER_REQUESTS_PER_PEER: usize = 2; +/// And never more than this in all, however many peers there are. +const MAX_HEADER_REQUESTS: usize = 64; + /// Chunk answers in a row that add nothing before the session gives up. /// /// A peer declining to serve is ordinary -- it may be pruned, or on another @@ -168,6 +181,11 @@ pub struct SnapSession { /// Requests this session has outstanding, as last reported by the driver. /// See [`Self::note_in_flight`]. in_flight: usize, + /// Peers that can take header requests, and those that can take them + /// with uncles, as last counted by the driver. See + /// [`Self::note_header_peers`]. + header_peers: usize, + uncle_peers: usize, /// The block whose state is being downloaded, once one has been offered. checkpoint: Option
, /// Its cumulative difficulty, as offered and then vouched for by the @@ -230,6 +248,8 @@ impl SnapSession { phase: Phase::AwaitingStatus, failure: None, in_flight: 0, + header_peers: 0, + uncle_peers: 0, checkpoint: None, checkpoint_td: U256::ZERO, offered: Vec::new(), @@ -307,6 +327,27 @@ impl SnapSession { self.in_flight = n; } + /// Tell this session how many peers can take its header requests, and how + /// many of those can send uncles with them, which sets the header phases' + /// budget. See [`HEADER_REQUESTS_PER_PEER`]. + pub fn note_header_peers(&mut self, headers: usize, uncles: usize) { + self.header_peers = headers; + self.uncle_peers = uncles; + } + + /// What may be outstanding at once in the current phase. + fn budget(&self) -> usize { + let per_peer = |peers: usize| { + peers.saturating_mul(HEADER_REQUESTS_PER_PEER).min(MAX_HEADER_REQUESTS) + }; + let cap = match self.phase { + Phase::VerifyingHeaders => per_peer(self.header_peers), + Phase::AscendingHeaders => per_peer(self.uncle_peers), + _ => 0, + }; + cap.max(self.config.max_in_flight).max(1) + } + /// Moves to a new phase, restarting the clock the progress lines report /// against. fn set_phase(&mut self, phase: Phase) { @@ -418,7 +459,7 @@ impl SnapSession { // that let the budget be exceeded by one on *every* call, which is // unbounded growth wearing a smaller number. When the queue is full // the right answer is to ask for nothing and wait for an answer. - let budget = self.config.max_in_flight.max(1).saturating_sub(self.in_flight); + let budget = self.budget().saturating_sub(self.in_flight); if budget == 0 { return Vec::new(); } @@ -920,6 +961,11 @@ impl SnapSession { if headers.is_empty() { // That peer cannot fill this range; another might. Give up only // once several in a row have come back empty. + // + // The range is handed back first, which is all the walk does with + // an empty run. Left marked as asked, it was never asked again, + // and when it was the frontier nothing could move the walk past it. + let _ = walk.on_headers(point, headers, proven); self.empty_header_replies += 1; if self.empty_header_replies >= EMPTY_HEADER_REPLIES_ALLOWED { return self.fail(SnapFailure::NoCommonAncestor); diff --git a/crates/sync/src/snap/session_tests.rs b/crates/sync/src/snap/session_tests.rs index 0faf400..fa14c96 100644 --- a/crates/sync/src/snap/session_tests.rs +++ b/crates/sync/src/snap/session_tests.rs @@ -638,7 +638,7 @@ fn a_body_that_does_not_match_its_header_is_refused() { // ------------------------------------------------------- blaming the peer use alloy_primitives::B512; -use crate::snap::driver::{Blame, SnapDriver}; +use crate::snap::driver::{Blame, Peers, SnapDriver}; use rustock_networking::protocol::{P2pMessage, RskSubMessage}; use rustock_networking::protocol::snap::Refusal; use rustock_networking::scoring::EventType; @@ -897,6 +897,152 @@ fn a_timed_out_request_is_charged() { assert_eq!(blame[0].event, EventType::TimeoutMessage); } +// ------------------------------------------------ who is asked for what + +/// The header walk asks ordinary peers. Its header and skeleton requests are +/// messages every peer serves, and with only the `snap` peers to ask, the walk +/// stalled for good on mainnet when the one peer that had offered the +/// checkpoint stopped answering, with fifteen other peers connected. +#[test] +fn the_header_walk_asks_peers_without_snap() { + let f = fixture(20); + let genesis = f.with_genesis(); + let (blocks, tds) = chain(1, 3, genesis, 100, f.state_root); + let server = B512::repeat_byte(1); + let plain = B512::repeat_byte(2); + let mut driver = SnapDriver::new(f.session(HeaderVerifier::new())); + + let out = driver.poll(Peers { snap: &[server], uncles: &[], any: &[server, plain] }); + let (status_id, kind) = asked(&out[0]); + assert_eq!(kind, "status"); + assert_eq!(out[0].peer, server, "a status request goes only to a snap peer"); + + // Only an ordinary peer is left to ask, as when the server stops answering. + let only_plain = Peers { snap: &[], uncles: &[], any: &[plain] }; + let out = driver.on_status(status_id, server, &blocks, &tds, 5_000, 0, only_plain); + assert!(!out.is_empty(), "the walk stalled with an ordinary peer to ask"); + for request in &out { + let (_, kind) = asked(request); + assert!(matches!(kind, "headers" | "skeleton"), "asked an ordinary peer for a {kind}"); + assert_eq!(request.peer, plain); + } +} + +/// A snapshot message never goes to a peer that did not announce `snap`: rskj +/// closes the connection on a message id it does not know, so asking the wrong +/// peer would lose it. +#[test] +fn snapshot_requests_go_only_to_snap_peers() { + let f = fixture(20); + let server = B512::repeat_byte(1); + let plain = B512::repeat_byte(2); + let mut driver = SnapDriver::new(f.session(HeaderVerifier::new())); + + let out = driver.poll(Peers { snap: &[], uncles: &[plain], any: &[plain] }); + assert!(out.is_empty(), "asked a peer without snap for a snapshot message"); + assert!(driver.outstanding_ids().is_empty()); + + let out = driver.poll(Peers { snap: &[server], uncles: &[], any: &[server, plain] }); + let (_, kind) = asked(&out[0]); + assert_eq!(kind, "status"); + assert_eq!(out[0].peer, server); +} + +/// A request that nobody connected can take is handed back, not lost: once a +/// peer that can take it is there, it is asked. +#[test] +fn a_request_nobody_can_take_is_asked_once_somebody_can() { + let f = fixture(20); + let genesis = f.with_genesis(); + let (blocks, tds) = chain(1, 3, genesis, 100, f.state_root); + let server = B512::repeat_byte(1); + let mut driver = SnapDriver::new(f.session(HeaderVerifier::new())); + + let out = driver.poll(&[server]); + let (status_id, _) = asked(&out[0]); + let nobody_for_headers = Peers { snap: &[server], uncles: &[], any: &[] }; + let out = driver.on_status(status_id, server, &blocks, &tds, 5_000, 0, nobody_for_headers); + assert!(out.is_empty()); + assert!( + driver.outstanding_ids().is_empty(), + "a request that was never sent still counts against the budget" + ); + + let plain = B512::repeat_byte(2); + let out = driver.poll(Peers { snap: &[], uncles: &[], any: &[plain] }); + assert!(!out.is_empty(), "the walk's requests were lost"); + assert!(out.iter().all(|request| request.peer == plain)); +} + +/// An empty answer hands its range back, to be asked of somebody else. +/// +/// Left marked as asked, it never was again. When the range was the walk's +/// frontier, as here, nothing could move the walk past it. +#[test] +fn an_empty_header_answer_is_asked_again() { + let f = fixture(20); + let genesis = f.with_genesis(); + let (blocks, tds) = chain(1, 4, genesis, 100, f.state_root); + let mut session = f.session(HeaderVerifier::new()); + + let first = session.on_status(&blocks, &tds, 8_000, 0); + assert_eq!(session.phase(), Phase::VerifyingHeaders); + let asked = first + .iter() + .find(|a| matches!(a, Action::RequestHeaders { .. })) + .cloned() + .expect("the walk asks for headers"); + let Action::RequestHeaders { point, .. } = asked else { unreachable!() }; + + let again = session.on_headers(point, &[]); + assert!(again.contains(&asked), "the range was not asked for again: {again:?}"); +} + +/// A peer that left a request unanswered sits out while somebody else can be +/// asked. On mainnet about half the peers never answered a header request, +/// and each one's turn in the rotation cost the walk thirty seconds. +#[test] +fn a_peer_that_left_a_request_unanswered_sits_out() { + let f = fixture(20); + let genesis = f.with_genesis(); + let (blocks, tds) = chain(1, 3, genesis, 100, f.state_root); + let server = B512::repeat_byte(1); + let silent = B512::repeat_byte(2); + let mut driver = SnapDriver::new(f.session(HeaderVerifier::new())); + + let out = driver.poll(&[server]); + let (status_id, _) = asked(&out[0]); + // At first only `silent` can be asked for headers, and it answers nothing. + let only_silent = Peers { snap: &[server], uncles: &[], any: &[silent] }; + let out = driver.on_status(status_id, server, &blocks, &tds, 5_000, 0, only_silent); + assert!(!out.is_empty() && out.iter().all(|request| request.peer == silent)); + let later = std::time::Instant::now() + std::time::Duration::from_secs(31); + assert_eq!(driver.expire(later), out.len()); + + let out = driver.poll(Peers { snap: &[server], uncles: &[], any: &[server, silent] }); + assert!(!out.is_empty(), "the walk's requests were not asked again"); + assert!( + out.iter().all(|request| request.peer == server), + "a peer that answered nothing was asked again with another peer to ask" + ); +} + +/// But a peer sitting out is still asked when nobody else can be: a slow peer +/// beats none, and a sync left with only such peers must still finish. +#[test] +fn a_peer_sitting_out_is_asked_when_nobody_else_can_be() { + let f = fixture(20); + let peers = vec![B512::repeat_byte(1)]; + let mut driver = SnapDriver::new(f.session(HeaderVerifier::new())); + driver.poll(&peers); + let later = std::time::Instant::now() + std::time::Duration::from_secs(31); + assert_eq!(driver.expire(later), 1); + + let out = driver.poll(&peers); + assert_eq!(out.len(), 1, "the request was not asked again"); + assert_eq!(out[0].peer, peers[0]); +} + /// Serves a real chunk for `from` out of the fixture's trie. fn served_chunk(f: &Fixture, from: u64) -> ChunkPayload { let root_message = f.trie.get(f.state_root.as_slice()).expect("root"); @@ -1092,7 +1238,9 @@ fn outstanding_snap_requests_never_exceed_the_budget() { assert_eq!(kind, "status"); driver.on_status(status_id, peers[0], &blocks, &tds, 8_000, 0, &peers); - let budget = 2usize; // `Fixture::session` sets max_in_flight: 2 + // The walk's budget: two for each peer it can ask, never less than + // `max_in_flight` (which `Fixture::session` sets to 2). + let budget = 2usize.max(2 * peers.len()); // Poll *and* feed responses back, so the response handlers are exercised // too -- capping only `poll` left those five paths issuing a fresh // budget's worth each. @@ -1111,6 +1259,48 @@ fn outstanding_snap_requests_never_exceed_the_budget() { } } +/// The header walk's budget is per peer, since every peer serves headers. +/// Held to the eight a state download is allowed, forty peers sat mostly +/// idle on mainnet through a walk of hours. +#[test] +fn the_header_walk_asks_more_of_more_peers() { + let f = fixture(64); + let genesis = f.with_genesis(); + let (blocks, tds) = chain(1, 4_000, genesis, 100, f.state_root); + let peers: Vec = (1..=10).map(B512::repeat_byte).collect(); + let mut driver = SnapDriver::new(f.session(HeaderVerifier::new())); + + let out = driver.poll(&peers); + let (status_id, _) = asked(&out[0]); + let mut out = driver.on_status(status_id, peers[0], &blocks, &tds, 8_000, 0, &peers); + + // Answer the skeletons honestly, so the walk knows more points than any + // budget allows, and leave the header requests outstanding. + for _ in 0..10 { + let mut next = Vec::new(); + for request in &out { + let P2pMessage::RskMessage(m) = &request.message else { continue }; + let RskSubMessage::SkeletonRequest(r) = &m.sub_message else { continue }; + let ids: Vec = blocks + .iter() + .map(|b| &b.header) + .filter(|h| h.number % 192 == 0) + .filter(|h| h.number >= r.start_number && h.number <= r.start_number + 3_840) + .map(|h| rustock_networking::protocol::BlockIdentifier { number: h.number, hash: h.hash() }) + .collect(); + next.extend(driver.on_skeleton(r.id, request.peer, &ids, &peers)); + } + if next.is_empty() { + break; + } + out = next; + } + + let outstanding = driver.outstanding_ids().len(); + assert!(outstanding > 2, "held to a state download's budget: {outstanding} outstanding"); + assert!(outstanding <= 2 * peers.len(), "{outstanding} outstanding, more than two per peer"); +} + /// With a checkpoint configured, a session bounds the peer's claim before it /// commits to the header walk. /// From 83c43f4bb48eb93c43c0b82b7ca7fca71c467512 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Thu, 5 Nov 2026 20:17:32 -0800 Subject: [PATCH 003/264] snap: keep a refused claim and unasked-for headers out of the chain Two ways for a session to be handed what it exists to establish, each fixed here with a test that fails without it. - start_state_download wrote the checkpoint's claimed total difficulty, the canonical index under it and the head, and only then checked the claim against the work the walk had established. A refused claim stayed written, and the next session's walk found the checkpoint already canonical, stopped there at once and took the claim: on mainnet a second attempt "verified" the chain in four seconds. The check now comes first. - While a session held the peer set, header answers it did not claim, most likely late ones to its own timed-out requests, went to the ordinary sync, which stored them with total difficulties and moved the head, writing canonical pointers a thousand blocks deep into the middle of the walk. The walk took them for ground the node already held and stopped after two of mainnet's nine million blocks. Chain data the session did not ask for is now kept from the ordinary sync until it resumes. --- crates/sync/src/service.rs | 36 +++++++++ crates/sync/src/snap/session.rs | 70 ++++++++++-------- crates/sync/src/snap/session_tests.rs | 101 ++++++++++++++++++++++++++ 3 files changed, 176 insertions(+), 31 deletions(-) diff --git a/crates/sync/src/service.rs b/crates/sync/src/service.rs index 8855af8..7596d55 100644 --- a/crates/sync/src/service.rs +++ b/crates/sync/src/service.rs @@ -3401,6 +3401,42 @@ impl SyncService { } } + // While a snapshot session holds the peer set, chain data it did not + // ask for is kept from the ordinary sync, which is standing down for + // it. `suspended_for_snap` already stops that sync's rounds; this stops + // what arrives without one. + // + // The ordinary handlers write what they are given -- headers with their + // total difficulties, the canonical index, the head -- and the likeliest + // such answer is a late one to the session's own request, arriving after + // it timed out: a stretch of the very chain the session is walking. + // Taken by the ordinary path, one wrote canonical pointers a thousand + // blocks deep into the middle of a mainnet walk, which the walk then + // took for ground this node already held, and stopped there. + if self.suspended_for_snap { + let ours = |driver: Option<&crate::snap::driver::SnapDriver>, id: u64, headers: bool| { + driver.is_some_and(|d| if headers { d.awaits_headers(id) } else { d.awaits_skeleton(id) }) + }; + let claimed = match &event { + SyncEvent::SkeletonResponse { id, .. } => ours(self.snap.as_ref(), *id, false), + SyncEvent::HeadersResponse { id, .. } + | SyncEvent::HeadersWithUnclesResponse { id, .. } => ours(self.snap.as_ref(), *id, true), + SyncEvent::SnapStatusResponse { .. } + | SyncEvent::SnapChunkResponse { .. } + | SyncEvent::SnapBlocksResponse { .. } => true, + SyncEvent::BodyResponse { .. } + | SyncEvent::BlockHashResponse { .. } + | SyncEvent::NewBlockHashes { .. } => false, + }; + if !claimed { + trace!( + target: "rustock::snap", + "chain data the snapshot session did not ask for; kept from the ordinary sync" + ); + return; + } + } + match event { SyncEvent::BlockHashResponse { hash, .. } => { self.on_block_hash_response(hash).await; diff --git a/crates/sync/src/snap/session.rs b/crates/sync/src/snap/session.rs index c9f27c6..7a7c07c 100644 --- a/crates/sync/src/snap/session.rs +++ b/crates/sync/src/snap/session.rs @@ -1075,6 +1075,45 @@ impl SnapSession { fn start_state_download(&mut self) -> Vec { let checkpoint = self.checkpoint.clone(); + // The claim is judged before anything is written. Everything after this + // makes the checkpoint this node's own -- its total difficulty, the + // canonical index under it, the head -- and the next session's walk + // stops at the first block it finds there. Written first and refused + // after, a claim turned down here was taken on the next attempt without + // a walk: on mainnet the second session "verified" the chain in four + // seconds, on the strength of the first one's refused total. + // + // The walk has read every header from the checkpoint down to a block + // this node already trusted, so the work of that chain is known. The + // claimed cumulative difficulty may exceed it only by the checkpoint + // block's own contribution -- the one header the walk never visits as + // a candidate. + // + // That contribution includes the checkpoint's uncles, whose headers + // are not in hand here: only `uncle_count` is. An uncle is a sibling, + // computed from the same parent, so its difficulty is close to the + // block's own; `difficulty * (1 + uncle_count)` is therefore a sound + // upper bound without pretending to a precision the data does not + // support. Bounding it low instead would reject every honest peer + // whose totals are right, which is what the header-only bound did. + // + // The claim is written as this node's own total difficulty at the + // checkpoint, so it is worth establishing rather than accepting. + if let Some(established) = self.walk.as_ref().and_then(|w| w.established_difficulty()) { + let ceiling = established.saturating_add( + checkpoint.as_ref().map_or(U256::ZERO, |h| { + h.difficulty + .saturating_mul(U256::from(1u64).saturating_add(U256::from(h.uncle_count))) + }), + ); + if self.checkpoint_td > ceiling { + return self.fail(SnapFailure::OverstatedDifficulty { + claimed: self.checkpoint_td, + ceiling, + }); + } + } + // The walk reached a block this node already had, so everything above // it is anchored to work this node had already accepted. Only now do // the headers become part of this chain. @@ -1112,37 +1151,6 @@ impl SnapSession { let _ = self.store.set_head(hash); } - // The walk has read every header from the checkpoint down to a block - // this node already trusted, so the work of that chain is known. The - // claimed cumulative difficulty may exceed it only by the checkpoint - // block's own contribution -- the one header the walk never visits as - // a candidate. - // - // That contribution includes the checkpoint's uncles, whose headers - // are not in hand here: only `uncle_count` is. An uncle is a sibling, - // computed from the same parent, so its difficulty is close to the - // block's own; `difficulty * (1 + uncle_count)` is therefore a sound - // upper bound without pretending to a precision the data does not - // support. Bounding it low instead would reject every honest peer - // whose totals are right, which is what the header-only bound did. - // - // The claim is written as this node's own total difficulty at the - // checkpoint, so it is worth establishing rather than accepting. - if let Some(established) = self.walk.as_ref().and_then(|w| w.established_difficulty()) { - let ceiling = established.saturating_add( - checkpoint.as_ref().map_or(U256::ZERO, |h| { - h.difficulty - .saturating_mul(U256::from(1u64).saturating_add(U256::from(h.uncle_count))) - }), - ); - if self.checkpoint_td > ceiling { - return self.fail(SnapFailure::OverstatedDifficulty { - claimed: self.checkpoint_td, - ceiling, - }); - } - } - info!( target: "rustock::snap", "header chain verified back to #{}; downloading state at #{}", diff --git a/crates/sync/src/snap/session_tests.rs b/crates/sync/src/snap/session_tests.rs index fa14c96..54a20c4 100644 --- a/crates/sync/src/snap/session_tests.rs +++ b/crates/sync/src/snap/session_tests.rs @@ -1301,6 +1301,107 @@ fn the_header_walk_asks_more_of_more_peers() { assert!(outstanding <= 2 * peers.len(), "{outstanding} outstanding, more than two per peer"); } +/// **A refused claim leaves nothing behind.** Making the checkpoint this +/// node's own -- its total difficulty, the canonical index under it, the head +/// -- waits until the work the walk established bounds the claim. Written +/// first and refused after, the next session's walk found the checkpoint +/// already canonical, stopped there at once, and took the claim that had just +/// been refused: on mainnet a second attempt "verified" the chain in four +/// seconds. +#[test] +fn a_refused_claim_leaves_nothing_behind() { + let f = fixture(20); + let genesis = f.with_genesis(); + // A chain that links, with totals that agree with each other, all raised + // by the same million: more work than the chain carries. + let (blocks, tds) = chain(1, 4, genesis, 1_000_000, f.state_root); + let checkpoint = blocks.last().unwrap().header.clone(); + + let mut session = f.session(HeaderVerifier::new()); + let actions = session.on_status(&blocks, &tds, 8_000, 0); + assert_eq!(session.phase(), Phase::VerifyingHeaders); + let Some(Action::RequestHeaders { point, count, .. }) = + actions.into_iter().find(|a| matches!(a, Action::RequestHeaders { .. })) + else { + panic!("the walk asks for headers"); + }; + session.on_headers(point, &headers_for(&blocks, point, count)); + assert!( + matches!(session.failure(), Some(SnapFailure::OverstatedDifficulty { .. })), + "{:?}", + session.failure() + ); + + assert_eq!( + f.store.canonical_hash(checkpoint.number).unwrap(), + None, + "the refused checkpoint was indexed as canonical" + ); + assert_ne!(f.store.head().unwrap(), Some(checkpoint.hash()), "it became the head"); + assert_eq!( + f.store.total_difficulty(checkpoint.hash()).unwrap(), + None, + "the refused claim was recorded as this node's own" + ); +} + +/// **While a session holds the peer set, the ordinary sync takes no chain +/// data.** A late answer to one of the session's own header requests, after +/// it timed out, is a stretch of the very chain being walked. Handed to the +/// ordinary sync, which writes what it is given, one put canonical pointers a +/// thousand blocks deep into a mainnet walk, and the walk took them for +/// ground this node already held. +#[tokio::test] +async fn the_ordinary_sync_takes_no_headers_during_a_snapshot_session() { + use crate::events::SyncEvent; + use crate::manager::SyncManager; + use crate::SyncService; + use rustock_networking::peers::PeerStore; + + let f = fixture(20); + let genesis = f.with_genesis(); + f.store.put_total_difficulty(genesis, U256::from(100u64)).expect("td"); + f.store.set_head(genesis).expect("head"); + let (blocks, _) = chain(1, 3, genesis, 100, f.state_root); + + let peers = Arc::new(PeerStore::new()); + let peer = B512::repeat_byte(9); + let (tx, _rx) = tokio::sync::mpsc::channel(8); + peers + .add_peer_with( + peer, + tx, + rustock_networking::handshake::PeerCapabilities { rsk_version: 62, snap: true }, + ) + .await; + let manager = Arc::new(SyncManager::new( + f.store.clone(), + Arc::new(HeaderVerifier::new()), + peers.clone(), + )); + let (_event_tx, event_rx) = tokio::sync::mpsc::unbounded_channel(); + let mut service = SyncService::new(manager, peers, event_rx) + .with_trie_store_for_test(f.trie.clone() as Arc); + service.start_snap_sync(SnapConfig::default(), Arc::new(HeaderVerifier::new())); + service.on_tick().await; + + // Headers that link to this node's genesis, under an id nobody awaits. + service + .handle_event(SyncEvent::HeadersResponse { + peer, + id: 0x5eed, + headers: blocks.iter().map(|b| b.header.clone()).collect(), + }) + .await; + + assert_eq!(f.store.head().unwrap(), Some(genesis), "the ordinary sync moved the head"); + assert_eq!( + f.store.canonical_hash(1).unwrap(), + None, + "the ordinary sync wrote the canonical index" + ); +} + /// With a checkpoint configured, a session bounds the peer's claim before it /// commits to the header walk. /// From 30edacf3fe1a5196ef0ad7e4e16d934642d479fb Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Thu, 5 Nov 2026 20:30:37 -0800 Subject: [PATCH 004/264] snap: ask the sampling gate's lost skeletons again A skeleton request that timed out was handed back to the walk and the ascent, but never to the sampling gate before them, which kept counting it as outstanding. The gate judges only once every skeleton is in, so it never reached a verdict and the session never left it: on mainnet a fresh node stopped there as soon as its first skeleton went to one of the peers that never answer. The gate now asks a released skeleton again before any new one, and does not count the window covered while one is waiting. --- crates/sync/src/sampler.rs | 89 ++++++++++++++++++++++++++++++++- crates/sync/src/snap/session.rs | 7 ++- 2 files changed, 93 insertions(+), 3 deletions(-) diff --git a/crates/sync/src/sampler.rs b/crates/sync/src/sampler.rs index a49b1ac..af76c8d 100644 --- a/crates/sync/src/sampler.rs +++ b/crates/sync/src/sampler.rs @@ -677,6 +677,8 @@ pub struct SamplingGate { /// Next skeleton start not yet asked for. next_skeleton: u64, skeletons_outstanding: usize, + /// Skeleton starts handed back unanswered, asked again before new ones. + retry_skeletons: Vec, /// Whether the claimed work is bounded, which is what the sampling is /// for. With it off the gate asks only for the checkpoint block: there is /// a chain to establish but no ceiling to compute. @@ -713,6 +715,7 @@ impl SamplingGate { skeleton_span: skeleton_span.max(1), next_skeleton: checkpoint_number, skeletons_outstanding: 0, + retry_skeletons: Vec::new(), learned: BTreeMap::new(), started: false, bound_work: defence.bound_work, @@ -750,7 +753,14 @@ impl SamplingGate { out.push(GateAction::RequestHeader { number: req.number, hash: req.hash }); } - // And keep the skeleton walk moving until the window is covered. + // And keep the skeleton walk moving until the window is covered, + // starting with any skeleton nobody answered: until it is in, the + // window is not covered, and nothing else will ask for it again. + while self.skeletons_outstanding < 4 { + let Some(start) = self.retry_skeletons.pop() else { break }; + out.push(GateAction::RequestSkeleton { start }); + self.skeletons_outstanding += 1; + } while self.next_skeleton < self.head && self.skeletons_outstanding < 4 { out.push(GateAction::RequestSkeleton { start: self.next_skeleton }); self.next_skeleton = self.next_skeleton.saturating_add(self.skeleton_span); @@ -776,6 +786,20 @@ impl SamplingGate { self.sampler.give_up_on(number); } + /// A skeleton request nobody answered, handed back to be asked again. + /// + /// The window counts as covered only once every skeleton is in, and one + /// lost to a timeout used to stay counted as outstanding for ever: the gate + /// never reached a verdict, and the session never left it. On mainnet that + /// happened as soon as a fresh node's first skeleton went to one of the + /// peers that never answer. + pub fn release_skeleton(&mut self, start: u64) { + if start < self.next_skeleton && self.skeletons_outstanding > 0 { + self.skeletons_outstanding -= 1; + self.retry_skeletons.push(start); + } + } + /// The verdict, if one can be reached yet. pub fn outcome(&self) -> GateOutcome { if self.sampler.too_many_missing() { @@ -784,7 +808,9 @@ impl SamplingGate { }); } // Judge as soon as the window is covered and every sample answered. - let covered = self.next_skeleton >= self.head && self.skeletons_outstanding == 0; + let covered = self.next_skeleton >= self.head + && self.skeletons_outstanding == 0 + && self.retry_skeletons.is_empty(); if !self.started || !covered || !self.sampler.is_complete() || !self.learned.is_empty() { return GateOutcome::Pending; } @@ -925,6 +951,65 @@ mod gate_tests { } } + /// Answers one request as an honest server would. + fn answer(gate: &mut SamplingGate, action: GateAction, head: u64, verifier: &HeaderVerifier) { + let cp = MAINNET_CHECKPOINT; + match action { + GateAction::RequestSkeleton { start } => { + let ids: Vec<(u64, B256)> = (0..20) + .map(|i| start + (i + 1) * 192) + .filter(|n| *n <= head) + .map(|n| (n, header_at(n, cp.difficulty).hash())) + .collect(); + gate.on_skeleton(&ids); + } + GateAction::RequestHeader { number, .. } => { + gate.on_header(&header_at(number, cp.difficulty), verifier); + } + } + } + + /// A skeleton request nobody answered is asked again, and does not hold + /// the verdict for ever. One lost to a timeout used to stay counted as + /// outstanding, so the window was never covered and the session never + /// left the gate. + #[test] + fn a_skeleton_nobody_answered_is_asked_again() { + let cp = MAINNET_CHECKPOINT; + let blocks = 20_000u64; + let head = cp.number + blocks; + let honest = cp.cumulative_difficulty + cp.difficulty * U256::from(blocks); + let mut gate = SamplingGate::new(cp, honest, head, DIV, MIN, SPAN, CheckpointDefence { verify_hash: false, bound_work: true }); + let verifier = HeaderVerifier::new(); + + // The first skeleton goes unanswered and is handed back; the rest of + // the first round is answered. + let mut lost = None; + for action in gate.poll() { + match action { + GateAction::RequestSkeleton { start } if lost.is_none() => lost = Some(start), + other => answer(&mut gate, other, head, &verifier), + } + } + let lost = lost.expect("a skeleton is asked for"); + gate.release_skeleton(lost); + assert_eq!(gate.outcome(), GateOutcome::Pending, "judged without the lost skeleton"); + + let mut asked_again = false; + for _ in 0..64 { + let actions = gate.poll(); + if actions.is_empty() && gate.outcome() != GateOutcome::Pending { + break; + } + for action in actions { + asked_again |= action == GateAction::RequestSkeleton { start: lost }; + answer(&mut gate, action, head, &verifier); + } + } + assert!(asked_again, "the lost skeleton was never asked again"); + assert!(matches!(gate.outcome(), GateOutcome::Passed { .. }), "{:?}", gate.outcome()); + } + /// The gate does not reach a verdict before the window is covered — /// otherwise a peer could pass by answering only the first few samples. #[test] diff --git a/crates/sync/src/snap/session.rs b/crates/sync/src/snap/session.rs index 7a7c07c..8754139 100644 --- a/crates/sync/src/snap/session.rs +++ b/crates/sync/src/snap/session.rs @@ -1053,8 +1053,13 @@ impl SnapSession { } } - /// Ask again for a skeleton nobody answered. See above: both phases. + /// Ask again for a skeleton nobody answered: in both header phases, and in + /// the sampling gate before them, which asks for skeletons too and waits + /// for every one of them before it judges. pub fn release_skeleton_request(&mut self, start: u64) { + if let Some(gate) = self.gate.as_mut() { + gate.release_skeleton(start); + } if let Some(walk) = self.walk.as_mut() { walk.release(&super::headers::Want::Skeleton { start }); } From dc2b43432284149b125101f1604d2d8169a17a46 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Thu, 5 Nov 2026 20:48:34 -0800 Subject: [PATCH 005/264] rpc: rsk_getStorageNodes, which proves a slot absent as well as present eth_getProof answers a key the trie does not hold with an empty array, on purpose, so that a path is never read as inclusion. That leaves no way to prove a slot empty, and on Rootstock that is also the only way to prove a slot holds zero, since a slot set to zero is deleted from the trie (rskj MutableRepository.addStorageBytes). rsk_getStorageNodes(address, [slots], block) answers each slot with the nodes from its key up to the state root either way, says which case it is, and names the block whose state root they hang from. For an absent slot the deepest node is where the key leaves the trie. A node the store cannot produce is an error, never an absence. The walk underneath is TrieNode::try_nodes_toward; eth_getProof is unchanged. --- crates/rpc/src/proof.rs | 105 ++++++++++++++++++++++++++++++++++++ crates/rpc/src/server.rs | 1 + crates/rpc/src/tests.rs | 66 +++++++++++++++++++++++ crates/trie/src/node.rs | 114 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 286 insertions(+) diff --git a/crates/rpc/src/proof.rs b/crates/rpc/src/proof.rs index 7be8a97..1180d22 100644 --- a/crates/rpc/src/proof.rs +++ b/crates/rpc/src/proof.rs @@ -201,3 +201,108 @@ pub fn eth_get_proof(id: Value, params: &Value, state: &RpcState) -> JsonRpcResp }), ) } + +/// `rsk_getStorageNodes(address, [slots], block)`: for each slot, whether the +/// state holds it, its value, and the unitrie nodes from the slot's key up to +/// the state root -- for a slot that is absent as much as for one that is not. +/// +/// `eth_getProof` answers an absent key with an empty array, on purpose (see +/// the module note), which leaves a caller no way to prove a slot empty. On +/// Rootstock that is also the only way to prove a slot holds zero, because a +/// slot set to zero is deleted from the trie (rskj +/// `MutableRepository.addStorageBytes`). So this answers both cases with the +/// path toward the key, and says which case it is, so that a path is never +/// read as inclusion when it is not: +/// +/// - `present: true`: the deepest node is the slot's own, and holds its value. +/// - `present: false`: the deepest node is where the key leaves the trie: its +/// shared path parts from the key's, or the key ends inside it, or the child +/// the key would take is empty, or the key ends at it and it holds nothing. +/// +/// A caller checks the nodes as `eth_getProof`'s are checked, walking the key +/// down from the root, and must reach the same answer as `present`. The block +/// is named in the answer, so the caller knows which header's state root the +/// nodes hang from. A node the store cannot produce is an error, never an +/// absence. +pub fn rsk_get_storage_nodes(id: Value, params: &Value, state: &RpcState) -> JsonRpcResponse { + let Some(addr) = params + .get(0) + .and_then(|v| v.as_str()) + .and_then(|s| s.parse::
().ok()) + else { + return JsonRpcResponse::error(id, INVALID_PARAMS, "Missing or invalid address"); + }; + let Some(slot_values) = params.get(1).and_then(|v| v.as_array()) else { + return JsonRpcResponse::error(id, INVALID_PARAMS, "Missing storage key array"); + }; + let mut slots: Vec<(String, B256)> = Vec::with_capacity(slot_values.len()); + for v in slot_values { + let Some(raw) = v.as_str() else { + return JsonRpcResponse::error(id, INVALID_PARAMS, "Storage key must be a string"); + }; + let Some(slot) = crate::helpers::parse_storage_slot(raw) else { + return JsonRpcResponse::error( + id, + INVALID_PARAMS, + format!("Invalid storage key: {raw}"), + ); + }; + slots.push((raw.to_string(), slot)); + } + + let block_param = params.get(2).and_then(|v| v.as_str()).unwrap_or("latest"); + let Some((root, header)) = resolve_state_root(block_param, state) else { + return JsonRpcResponse::error(id, INTERNAL_ERROR, "Cannot resolve block"); + }; + let Some(trie_store) = state.trie_store.clone() else { + return JsonRpcResponse::error(id, INTERNAL_ERROR, "State not available"); + }; + let store = &*trie_store; + + let mut storage: Vec = Vec::with_capacity(slots.len()); + for (raw, slot) in &slots { + let key = TrieKeySlice::from_key(&storage_key(&addr, slot)); + let incomplete = |missing: B256| { + JsonRpcResponse::error( + id.clone(), + INTERNAL_ERROR, + format!("The state is incomplete here: node {missing:#x} is missing"), + ) + }; + let nodes = match root.try_nodes_toward(&key, store) { + Ok(nodes) => nodes, + Err(missing) => return incomplete(missing), + }; + let value = match root.try_get(&key, store) { + Ok(value) => value.filter(|v| !v.is_empty()), + Err(missing) => return incomplete(missing), + }; + // Stored minimally, as `eth_getStorageAt` also finds. + let number = value.as_ref().map_or(U256::ZERO, |bytes| { + let mut padded = [0u8; 32]; + let start = 32usize.saturating_sub(bytes.len()); + padded[start..].copy_from_slice(&bytes[bytes.len().saturating_sub(32)..]); + U256::from_be_bytes(padded) + }); + storage.push(json!({ + "key": raw, + "present": value.is_some(), + "value": format!("{number:#x}"), + "nodes": nodes + .iter() + .map(|n| format!("0x{}", hex::encode(n.to_message(store)))) + .collect::>(), + })); + } + + JsonRpcResponse::success( + id, + json!({ + "address": format!("{addr:#x}"), + "blockNumber": format!("{:#x}", header.number), + "blockHash": format!("{:#x}", header.hash()), + "stateRoot": format!("{:#x}", header.state_root), + "storage": storage, + }), + ) +} diff --git a/crates/rpc/src/server.rs b/crates/rpc/src/server.rs index fe01622..32c8f37 100644 --- a/crates/rpc/src/server.rs +++ b/crates/rpc/src/server.rs @@ -330,6 +330,7 @@ async fn dispatch(state: &RpcState, req: JsonRpcRequest) -> JsonRpcResponse { ), "rsk_storageStatus" => admin::rsk_storage_status(id, &state.store, &state.epoch_store), "rsk_getStorageBytesAt" => rsk::rsk_get_storage_bytes_at(id, params, state), + "rsk_getStorageNodes" => proof::rsk_get_storage_nodes(id, params, state), "rsk_getRawTransactionReceiptByHash" => { rsk::rsk_get_raw_transaction_receipt_by_hash(id, params, &state.store) } diff --git a/crates/rpc/src/tests.rs b/crates/rpc/src/tests.rs index fcc3d3c..eadfc3e 100644 --- a/crates/rpc/src/tests.rs +++ b/crates/rpc/src/tests.rs @@ -6028,3 +6028,69 @@ async fn get_proof_rejects_a_missing_slot_array() { .await; assert!(resp.error.is_some(), "the slot array is required, even when empty"); } + +// ===== rsk_getStorageNodes ===== + +/// The nodes a slot's answer carries hang from the block's state root, each +/// a child of the one above it. +fn assert_hangs_from(nodes: &Value, state_root: B256, trie_store: &dyn rustock_trie::TrieStore) { + use rustock_trie::TrieNode; + + let parsed: Vec = nodes + .as_array() + .expect("an array of nodes") + .iter() + .map(|n| hex::decode(n.as_str().unwrap().trim_start_matches("0x")).unwrap()) + .map(|bytes| TrieNode::from_message(&bytes, trie_store)) + .collect(); + assert!(parsed.len() > 1, "the path should span more than one level"); + assert_eq!(parsed.last().unwrap().compute_hash(trie_store), state_root, "not the block's state root"); + for pair in parsed.windows(2) { + let (child, parent) = (&pair[0], &pair[1]); + let message = child.to_message(trie_store); + let linked = [&parent.left, &parent.right] + .iter() + .any(|r| r.resolve(trie_store).is_some_and(|c| c.to_message(trie_store) == message)); + assert!(linked, "a node on the path is not a child of the one above it"); + } +} + +/// A slot the state holds: present, its value, and its path. +#[tokio::test] +async fn storage_nodes_prove_a_slot_that_is_there() { + let (state, _tmp, addr, state_root) = setup_proof_state(); + let trie_store = state.trie_store.clone().unwrap(); + let req = make_request("rsk_getStorageNodes", json!([format!("{addr:#x}"), ["0x1"], "latest"])); + let r = dispatch_for_test(&state, req).await.result.unwrap(); + + assert_eq!(r["blockNumber"], json!("0x2a"), "the block the nodes hang from is named"); + assert_eq!(r["stateRoot"], json!(format!("{state_root:#x}"))); + let slot = &r["storage"][0]; + assert_eq!(slot["present"], json!(true)); + assert_eq!(slot["value"], json!("0x2a"), "42"); + assert_hangs_from(&slot["nodes"], state_root, &*trie_store); +} + +/// **A slot the state does not hold is proved absent**, not answered with +/// nothing: on Rootstock that is also how a slot holding zero is proved, since +/// a slot set to zero is deleted. `eth_getProof` keeps its empty array. +#[tokio::test] +async fn storage_nodes_prove_a_slot_that_is_not() { + let (state, _tmp, addr, state_root) = setup_proof_state(); + let trie_store = state.trie_store.clone().unwrap(); + let req = make_request("rsk_getStorageNodes", json!([format!("{addr:#x}"), ["0x3"], "latest"])); + let r = dispatch_for_test(&state, req).await.result.unwrap(); + + let slot = &r["storage"][0]; + assert_eq!(slot["present"], json!(false)); + assert_eq!(slot["value"], json!("0x0")); + assert_hangs_from(&slot["nodes"], state_root, &*trie_store); + + let req = make_request("eth_getProof", json!([format!("{addr:#x}"), ["0x3"], "latest"])); + let r = dispatch_for_test(&state, req).await.result.unwrap(); + assert_eq!( + r["storageProof"][0]["proof"].as_array().unwrap().len(), + 0, + "eth_getProof still answers an absent slot with an empty array" + ); +} diff --git a/crates/trie/src/node.rs b/crates/trie/src/node.rs index bed5c0c..7536460 100644 --- a/crates/trie/src/node.rs +++ b/crates/trie/src/node.rs @@ -694,6 +694,43 @@ impl TrieNode { Some(subnodes) } + /// Every node from this one toward `key`, **leaf first, root last**, + /// whether or not the trie holds the key. + /// + /// Where it does, this is [`nodes_on_path`](Self::nodes_on_path). Where it + /// does not, the path runs down to the node at which the key leaves the + /// trie, and that node shows it on its own terms: its shared path parts + /// from the key's, or the key ends inside it, or the child the key would + /// take is empty, or the key ends at it and it holds no value. The nodes + /// above it tie it to the root, which is what makes the path a proof of + /// absence. + /// + /// `Err` with the hash the store could not produce when a reference on the + /// way cannot be followed: an incomplete store proves nothing either way, + /// and answering as though the key were absent would be a false proof. + pub fn try_nodes_toward( + &self, + key: &TrieKeySlice, + store: &dyn TrieStore, + ) -> Result, B256> { + let common = key.common_path(&self.shared_path); + if common.length() < self.shared_path.length() || common.length() == key.length() { + return Ok(vec![self.clone()]); + } + + let bit = key.get(common.length()); + let child_ref = if bit == 0 { &self.left } else { &self.right }; + let child = match child_ref.try_resolve(store) { + Resolution::Found(c) => c, + Resolution::Absent => return Ok(vec![self.clone()]), + Resolution::Missing(h) => return Err(h), + }; + let sub_key = key.slice(common.length() + 1, key.length()); + let mut nodes = child.try_nodes_toward(&sub_key, store)?; + nodes.push(self.clone()); + Ok(nodes) + } + /// Like [`get`](Self::get), but says which node was missing instead of /// answering "no value" when the trie is incomplete. /// @@ -1239,6 +1276,83 @@ mod tests { assert_eq!(root.get(&key, &store), None); } + /// A trie that branches, so that paths are more than a node or two deep. + fn branching(store: &MemoryTrieStore) -> TrieNode { + let mut root = TrieNode::empty(); + for i in 0u8..50 { + root = root.put(&TrieKeySlice::from_key(&[i, 7]), &[i], store); + } + root + } + + /// Each node is a child of the one after it, and the last is `root`. + fn assert_chained(nodes: &[TrieNode], root: &TrieNode, store: &MemoryTrieStore) { + assert_eq!(nodes.last().expect("a path").compute_hash(store), root.compute_hash(store)); + for pair in nodes.windows(2) { + let (child, parent) = (&pair[0], &pair[1]); + let message = child.to_message(store); + let linked = [&parent.left, &parent.right] + .iter() + .any(|r| r.resolve(store).is_some_and(|c| c.to_message(store) == message)); + assert!(linked, "a node on the path is not a child of the one above it"); + } + } + + /// A key the trie holds gets the path `nodes_on_path` gives it. + #[test] + fn nodes_toward_a_present_key_are_its_path() { + let store = MemoryTrieStore::new(); + let root = branching(&store); + let key = TrieKeySlice::from_key(&[21, 7]); + + let toward = root.try_nodes_toward(&key, &store).expect("a complete store"); + let on_path = root.nodes_on_path(&key, &store).expect("present"); + let messages = |nodes: &[TrieNode]| nodes.iter().map(|n| n.to_message(&store)).collect::>(); + assert_eq!(messages(&toward), messages(&on_path)); + } + + /// A key the trie does not hold still gets a path, tied to the root, down + /// to the node where it leaves the trie -- whichever way it leaves. + #[test] + fn nodes_toward_an_absent_key_end_where_it_leaves_the_trie() { + let store = MemoryTrieStore::new(); + let root = branching(&store); + let absent: [&[u8]; 4] = [ + &[200, 7], // parts from every key early + &[21, 8], // shares the first byte of one, then parts + &[21], // ends inside the path to one + &[21, 7, 1], // runs on past one + ]; + for key in absent { + let slice = TrieKeySlice::from_key(key); + assert_eq!(root.try_get(&slice, &store), Ok(None), "{key:?} is in the trie"); + let nodes = root.try_nodes_toward(&slice, &store).expect("a complete store"); + assert_chained(&nodes, &root, &store); + } + + // Running on past a key ends at that key's own node, which has no + // child for the rest. + let past = root + .try_nodes_toward(&TrieKeySlice::from_key(&[21, 7, 1]), &store) + .expect("a complete store"); + assert_eq!(past[0].value, Some(vec![21])); + } + + /// A store that cannot follow the path proves nothing, and says so rather + /// than answering that the key is absent. + #[test] + fn nodes_toward_through_an_incomplete_store_are_an_error() { + let store = MemoryTrieStore::new(); + let mut root = branching(&store); + root.save(&store, true); + + // The same root, read back against a store holding none of its + // descendants. + let empty = MemoryTrieStore::new(); + let detached = TrieNode::from_message(&root.to_message(&store), &empty); + assert!(detached.try_nodes_toward(&TrieKeySlice::from_key(&[21, 8]), &empty).is_err()); + } + #[test] fn test_hash_changes_on_put() { let store = MemoryTrieStore::new(); From bc1c22f18dd4bcd706e7d794d6b24fa99df1d348 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Thu, 5 Nov 2026 21:45:53 -0800 Subject: [PATCH 006/264] snap: bound the uncles a header walk cannot see, and count work once The walk's work stood in for a ceiling on the peer's claimed total difficulty, but rskj peers (rsk/62) send no uncles with headers, so the walk counted only each block's own difficulty: on mainnet a little over half the work. With the checkpoint's own uncles added it still refused every honest peer. RSKj's public node gives #9381160 a total difficulty of 62447305256930453742879258772, exactly what the peer claimed; the walk established 35445300132102231728936625804, and charged the peer InvalidBlock. It stayed hidden until the walk could finish on mainnet. HeaderWalk::work_ceiling adds a bound on the uncles it did not see. A header's uncle count is inside what its proof of work commits to, an uncle hangs from the trunk two to seven blocks down (rskj BlockUnclesValidationRule), and its difficulty is at most its parent's and a fiftieth (DifficultyCalculator, divisor 50 before RSKIP156). So each unseen uncle is bounded by the heaviest trunk header in that stretch, which the walk has checked. A stretch reaching below a run is finished from the run below, or from the store under the anchor. With every uncle sent the ceiling is the exact work. Work now counts when a run joins the chain, in advance, rather than as answers arrive: a run that never linked, from another chain or answered twice, raised the ceiling a claim is checked against. The checkpoint is the first run's newest header, so it is no longer added a second time. Test fixtures record genesis's own total, as setup_genesis does; one honest test passed only on that double count. --- crates/sync/src/snap/header_tests.rs | 149 ++++++++++++++++- crates/sync/src/snap/headers.rs | 230 +++++++++++++++++++++++--- crates/sync/src/snap/session.rs | 60 ++++--- crates/sync/src/snap/session_tests.rs | 66 +++++++- docs/difficulty-gate.md | 5 + docs/header-first-sync.md | 3 +- 6 files changed, 460 insertions(+), 53 deletions(-) diff --git a/crates/sync/src/snap/header_tests.rs b/crates/sync/src/snap/header_tests.rs index e15852a..6fe7e1b 100644 --- a/crates/sync/src/snap/header_tests.rs +++ b/crates/sync/src/snap/header_tests.rs @@ -59,12 +59,18 @@ struct Fixture { /// A node that knows only its genesis, and a chain it has never seen. fn fixture(n: u64) -> Fixture { + fixture_of(chain(n)) +} + +/// The same, with a chain of any shape. +fn fixture_of(chain: Vec
) -> Fixture { let dir = tempfile::tempdir().expect("tempdir"); let store = Arc::new(BlockStore::open(dir.path()).expect("open")); - let chain = chain(n); + // As `setup_genesis` records it: genesis's total difficulty is its own. let genesis = &chain[0]; store.put_header_with_hash(genesis.hash(), genesis).expect("genesis"); + store.put_total_difficulty(genesis.hash(), genesis.difficulty).expect("td"); store.put_canonical_hash(0, genesis.hash()).expect("index"); Fixture { store, chain, _dir: dir } @@ -174,6 +180,37 @@ fn another_chain(n: u64) -> Vec
{ out } +/// A chain of `n+1` headers, 0..=n, whose block `i` has the difficulty and +/// uncle count that `shape(i)` gives. +fn shaped_chain(n: u64, shape: impl Fn(u64) -> (u64, u64)) -> Vec
{ + let mut out = vec![header(0, B256::ZERO)]; + for i in 1..=n { + let mut next = header(i, out[(i - 1) as usize].hash()); + let (difficulty, uncles) = shape(i); + next.difficulty = U256::from(difficulty); + next.uncle_count = uncles; + out.push(next); + } + out +} + +/// The most the uncles of `blocks` can weigh under rskj's rules: each hangs +/// from the trunk two to seven blocks below the block that includes it, with at +/// most its parent's difficulty and a fiftieth more. +fn heaviest_uncles(chain: &[Header], blocks: std::ops::RangeInclusive) -> U256 { + let mut total = U256::ZERO; + for n in blocks { + let parents = n.saturating_sub(7)..=n.saturating_sub(2); + let heaviest = chain[parents].iter().map(|h| h.difficulty).max().expect("a parent"); + total += U256::from(chain[n].uncle_count) * (heaviest + heaviest / U256::from(50u64)); + } + total +} + +fn sum(headers: &[Header]) -> U256 { + headers.iter().fold(U256::ZERO, |sum, h| sum + h.difficulty) +} + #[test] fn an_honest_walk_reaches_our_genesis() { for top in [1u64, 191, 192, 193, 500, 2000] { @@ -490,6 +527,116 @@ fn a_frontier_answered_from_another_chain_is_asked_for_again() { assert_eq!(walk.frontier(), 768); } +/// **Work counts once, and only for the chain.** The walk adds up what each +/// run is worth, to bound the peer's claim. Counted as answers arrived, a +/// frontier answered twice from another chain before the right answer came +/// added two runs of foreign work to what the walk said it had established -- +/// raising the very ceiling a claim is checked against. +#[test] +fn work_counts_once_and_only_for_the_chain() { + let f = fixture(1000); + let forked = another_chain(1000); + let mut walk = f.walk(1000, HeaderVerifier::new()); + walk.on_headers(1000, &f.headers(1000, 40), &Default::default()).expect("honest"); + walk.advance().expect("links"); + let foreign: Vec
= (769..=960).rev().map(|i| forked[i as usize].clone()).collect(); + for _ in 0..2 { + walk.on_headers(960, &foreign, &Default::default()).expect("internally valid"); + walk.advance().expect("no progress, and no error"); + } + f.run(&mut walk, 8); + assert!(walk.is_done()); + + // Genesis's own 100, and #1 to #1000 at 100 each, the checkpoint among them. + let work = U256::from(100_100u64); + assert_eq!(walk.established_difficulty(), Some(work)); + assert_eq!(walk.work_ceiling(U256::ZERO), Some(work), "without uncles the work is exact"); +} + +/// **The ceiling covers the heaviest uncles the rules allow.** rskj peers send +/// no uncles with headers, so the walk counts each block's own difficulty and +/// bounds what its uncles add: an uncle hangs from the trunk two to seven +/// blocks down, at most a fiftieth heavier than its parent. Here every block +/// from #8 has uncles, on a sawtooth trunk, so that the heaviest possible +/// parent sits anywhere in the stretch; and the trunk just below #769, the +/// bottom of the run under the checkpoint's, is ten times heavier than the +/// rest, so a bound taken from each run alone comes out under what the chain +/// can carry. +#[test] +fn the_ceiling_covers_the_heaviest_uncles_the_rules_allow() { + let chain = shaped_chain(1000, |n| { + let difficulty = if (762..=767).contains(&n) { 10_000_000 } else { 1_000_000 + (n % 10) * 50_000 }; + (difficulty, if n >= 8 { n % 3 } else { 0 }) + }); + let f = fixture_of(chain); + let mut walk = f.walk(1000, HeaderVerifier::new()); + f.run(&mut walk, 8); + assert!(walk.is_done()); + + let seen = sum(&f.chain); + let carried = seen + heaviest_uncles(&f.chain, 1..=1000); + assert_eq!(walk.established_difficulty(), Some(seen), "the walk counts the trunk it sees"); + let ceiling = walk.work_ceiling(U256::ZERO).expect("anchored"); + assert!(ceiling >= carried, "the ceiling {ceiling} is under the {carried} the chain can carry"); + assert!( + ceiling * U256::from(100u64) <= carried * U256::from(101u64), + "the ceiling {ceiling} is loose against {carried}" + ); +} + +/// The same where the walk anchors above genesis: the lowest uncles it walks +/// hang partly from this node's own chain under the anchor, whose heavy +/// stretch only the store can show. +#[test] +fn the_ceiling_reaches_under_the_anchor() { + let chain = shaped_chain(1000, |n| { + let difficulty = if (762..=767).contains(&n) { 10_000_000 } else { 1_000_000 }; + (difficulty, u64::from(n >= 8)) + }); + let f = fixture_of(chain); + // This node already holds the chain to #768, with its totals. + let anchor = 768usize; + let mut td = f.chain[0].difficulty; + for header in &f.chain[1..=anchor] { + td += header.difficulty * U256::from(1 + header.uncle_count); + f.store.put_header_with_hash(header.hash(), header).expect("header"); + f.store.put_canonical_hash(header.number, header.hash()).expect("index"); + f.store.put_total_difficulty(header.hash(), td).expect("td"); + } + let mut walk = f.walk(1000, HeaderVerifier::new()); + f.run(&mut walk, 8); + assert!(walk.is_done()); + + let carried = td + sum(&f.chain[anchor + 1..]) + heaviest_uncles(&f.chain, anchor + 1..=1000); + assert_eq!(walk.established_difficulty(), Some(td + sum(&f.chain[anchor + 1..]))); + let ceiling = walk.work_ceiling(U256::ZERO).expect("anchored"); + assert!(ceiling >= carried, "the ceiling {ceiling} is under the {carried} the chain can carry"); + assert!( + ceiling * U256::from(100u64) <= carried * U256::from(101u64), + "the ceiling {ceiling} is loose against {carried}" + ); +} + +/// Where a header under the anchor is missing from the store, the uncles that +/// may hang from it cannot be bounded, and the walk says so rather than +/// guessing: it puts no ceiling on the claim. +#[test] +fn a_missing_header_under_the_anchor_leaves_the_uncles_unbounded() { + let chain = shaped_chain(1000, |n| (1_000_000, u64::from(n >= 8))); + let f = fixture_of(chain); + // This node holds #768 as canonical, but none of the headers under it. + let anchor = &f.chain[768]; + f.store.put_header_with_hash(anchor.hash(), anchor).expect("header"); + f.store.put_canonical_hash(768, anchor.hash()).expect("index"); + f.store.put_total_difficulty(anchor.hash(), U256::from(1_000_000_000u64)).expect("td"); + let mut walk = f.walk(1000, HeaderVerifier::new()); + // One request at a time, so that the walk asks only for its frontier and + // never fetches, and stores, the run under the anchor itself. + f.run(&mut walk, 1); + assert!(walk.is_done()); + assert_eq!(walk.work_ceiling(U256::ZERO), Some(U256::MAX)); +} + /// **A short answer costs a request, not the walk.** A peer may send fewer /// headers than were asked for -- rskj stops at the first parent it lacks -- /// which leaves the frontier between grid points, where no skeleton point diff --git a/crates/sync/src/snap/headers.rs b/crates/sync/src/snap/headers.rs index b17aec0..ed5369b 100644 --- a/crates/sync/src/snap/headers.rs +++ b/crates/sync/src/snap/headers.rs @@ -78,16 +78,54 @@ pub enum WalkError { ForeignGenesis, } -/// A verified run of headers, remembered by its ends. +/// The steepest step the retarget rule has ever allowed, as a divisor: a +/// block's difficulty is its parent's, plus or minus at most this fraction of +/// it, or the floor (rskj `DifficultyCalculator`, whose `difficultyBoundDivisor` +/// was 50 until RSKIP156 raised it to 400). Bounding an uncle by its parent +/// takes the largest step any height could. +const STEEPEST_RETARGET: u64 = 50; + +/// How far below the block that includes it an uncle's parent may sit (rskj +/// `uncleGenerationLimit`): an uncle of the block at height `h` hangs from the +/// trunk somewhere in `h-7 ..= h-2` (`BlockUnclesValidationRule +/// .validateParentNumber`). +const UNCLE_GENERATIONS: usize = 7; + +/// Uncles whose possible parents have not all been walked yet: how many, the +/// highest trunk difficulty seen among those parents so far, and how many +/// trunk headers below are still to be seen. +#[derive(Debug, Clone)] +struct Reaching { + count: u64, + highest: U256, + below: usize, +} + +/// A verified run of headers, remembered by its ends and by what it is worth. /// /// The headers themselves go to the store as they are checked; only the two /// ends are kept, which is what makes walking nine million of them affordable. +/// What the run is worth counts only once the run joins the chain: see +/// [`HeaderWalk::advance`]. #[derive(Debug, Clone)] struct Run { newest: B256, oldest_number: u64, /// The parent of the oldest header: what the run below must produce. oldest_parent: B256, + /// Each header's difficulty, or its exact contribution where the peer + /// sent its uncles. + work: U256, + /// Uncles the work leaves out, because the peer sent none. + uncles_omitted: u64, + /// Of those, the ones whose possible parents are all in this run: for each + /// block, its uncle count times the highest difficulty among them. + uncle_weight: U256, + /// The ones whose possible parents reach below the run. + reaching: Vec, + /// The difficulties of the run's newest headers, newest first: as many as + /// the uncles of the run above can hang from. + newest_difficulties: Vec, } pub struct HeaderWalk { @@ -119,18 +157,26 @@ pub struct HeaderWalk { need_hash: B256, done: bool, - /// Difficulty summed over every header the walk has verified. + /// Difficulty summed over the headers of every run that has joined the + /// chain, each counted once. /// /// The checkpoint's total difficulty arrives as a claim and is written to /// the store as this node's own, so it is worth establishing against the /// chain actually walked. The walk reads every header anyway; adding them - /// up costs nothing and makes the claim checkable. + /// up costs nothing and makes the claim checkable. Where a peer sent no + /// uncles it is a floor, which [`Self::work_ceiling`] bounds from above. walked_difficulty: U256, - /// Uncles seen during the walk whose difficulty could not be added, - /// because a header walk never carries bodies. Non-zero means - /// `walked_difficulty` understates the chain and the totals derived from - /// it need `--repair total-difficulty` once bodies are present. + /// Uncles in those runs whose difficulty could not be added, because a + /// header walk never carries bodies. Non-zero means `walked_difficulty` + /// understates the chain and the totals derived from it need `--repair + /// total-difficulty` once bodies are present. uncles_omitted: u64, + /// For each block with such uncles whose possible parents have all been + /// walked, its uncle count times the highest difficulty among them, + /// summed. See [`Self::work_ceiling`]. + uncle_weight: U256, + /// The uncles whose possible parents have not all been walked yet. + reaching: Vec, /// Total difficulty this node had already established at the block the /// walk anchored on. @@ -216,17 +262,21 @@ impl HeaderWalk { done: false, walked_difficulty: U256::ZERO, uncles_omitted: 0, + uncle_weight: U256::ZERO, + reaching: Vec::new(), anchor_difficulty: None, freeze_horizon: number.saturating_sub(rustock_storage::freezer::FREEZE_DEPTH), staging, } } - /// Difficulty summed across every header verified so far. + /// Difficulty summed across the headers of every run that has joined the + /// chain. /// - /// Once the walk is done this is the work of the whole chain below the - /// checkpoint, and a peer's claimed cumulative difficulty may not exceed - /// it by more than the checkpoint block's own difficulty. + /// Once the walk is done this is the work of the whole chain from the + /// checkpoint down to the anchor, the checkpoint included -- but of the + /// headers only, unless the peers sent the uncles: see + /// [`Self::work_ceiling`]. pub fn walked_difficulty(&self) -> U256 { self.walked_difficulty } @@ -247,6 +297,32 @@ impl HeaderWalk { .map(|anchor| anchor.saturating_add(self.walked_difficulty)) } + /// The most cumulative difficulty the walked chain can carry at the + /// checkpoint, once the walk has anchored: the work established, plus the + /// most that the uncles it did not see can add. Exact when every uncle + /// came with its header. + /// + /// Uncles travel in bodies, which a header walk never fetches, and an rskj + /// peer (`rsk/62`) sends none alongside, so on mainnet the headers carry a + /// little over half of the work. What the walk does see bounds the rest. + /// A header's uncle count is inside what its proof of work commits to, and + /// an uncle hangs from the trunk two to seven blocks down, with a + /// difficulty the retarget rule ties to its parent's: at most a fiftieth + /// more, or the floor. So each uncle unseen is worth at most the highest + /// trunk difficulty in that stretch and a fiftieth, or `min_difficulty`; + /// this adds both, which is never less. + pub fn work_ceiling(&self, min_difficulty: U256) -> Option { + let established = self.established_difficulty()?; + let rise = self.uncle_weight / U256::from(STEEPEST_RETARGET); + let floor = min_difficulty.saturating_mul(U256::from(self.uncles_omitted)); + Some( + established + .saturating_add(self.uncle_weight) + .saturating_add(rise) + .saturating_add(floor), + ) + } + /// The height the walk started from. /// /// With `frontier`, this is how far it has descended -- the walk runs for @@ -411,23 +487,43 @@ impl HeaderWalk { reason: e.to_string(), })?; - for header in headers { - // Uncle difficulty counts toward the chain's work and uncle headers - // travel only in the body, which a backward header walk never - // fetches. A peer at `rsk/63` sends them alongside and the figure - // is exact; from anyone else `walked_difficulty` is a lower bound, - // widened by every block that included an uncle. See - // `uncles_omitted`. + // What the run is worth, which counts once it joins the chain. + // + // Uncle difficulty counts toward the chain's work, and uncle headers + // travel only in the body, which a backward header walk never fetches. + // A peer at `rsk/63` sends them alongside and the figure is exact. + // From anyone else -- every rskj node -- they are left out, and what + // the run does hold bounds them: see `Self::work_ceiling`. + let mut work = U256::ZERO; + let mut uncles_omitted = 0u64; + let mut uncle_weight = U256::ZERO; + let mut reaching = Vec::new(); + for (i, header) in headers.iter().enumerate() { let hash = header.hash(); match proven.get(&hash) { - Some(exact) => { - self.walked_difficulty = self.walked_difficulty.saturating_add(*exact); - } + Some(exact) => work = work.saturating_add(*exact), None => { - self.walked_difficulty = - self.walked_difficulty.saturating_add(header.difficulty); + work = work.saturating_add(header.difficulty); if header.uncle_count > 0 { - self.uncles_omitted += header.uncle_count; + uncles_omitted += header.uncle_count; + // Its uncles hang from the headers two to seven below + // it: those this run holds, and for the rest, the + // newest of the runs below. + let end = (i + UNCLE_GENERATIONS + 1).min(headers.len()); + let highest = headers + .get(i + 2..end) + .unwrap_or_default() + .iter() + .map(|parent| parent.difficulty) + .max() + .unwrap_or(U256::ZERO); + let below = (i + UNCLE_GENERATIONS + 1).saturating_sub(headers.len()); + if below == 0 { + uncle_weight = uncle_weight + .saturating_add(U256::from(header.uncle_count).saturating_mul(highest)); + } else { + reaching.push(Reaching { count: header.uncle_count, highest, below }); + } } } } @@ -452,6 +548,15 @@ impl HeaderWalk { newest: newest.hash(), oldest_number: oldest.number, oldest_parent: oldest.parent_hash, + work, + uncles_omitted, + uncle_weight, + reaching, + newest_difficulties: headers + .iter() + .take(UNCLE_GENERATIONS) + .map(|header| header.difficulty) + .collect(), }, ); Ok(()) @@ -536,6 +641,7 @@ impl HeaderWalk { .flatten() .unwrap_or_default(), ); + self.settle_on_own_chain(); self.done = true; self.commit_freezer(); break; @@ -567,8 +673,84 @@ impl HeaderWalk { // next is that header's parent: one lower, and named by it. self.need_number = run.oldest_number.saturating_sub(1); self.need_hash = run.oldest_parent; + // Only now is the run part of the chain, so only now does it + // count. Counted as answers arrived, a run that never linked -- an + // answer from another chain, a range answered twice, a late answer + // to a request asked again -- raised the work established, and + // with it the ceiling a claim is checked against. + self.count(run); } Ok(started_at.saturating_sub(self.need_number)) } + + /// Add a run that has just joined the chain to the work established. + fn count(&mut self, run: Run) { + // Its newest headers are the trunk that the lowest uncles of the run + // above may hang from. + self.absorb(&run.newest_difficulties); + self.walked_difficulty = self.walked_difficulty.saturating_add(run.work); + self.uncles_omitted += run.uncles_omitted; + self.uncle_weight = self.uncle_weight.saturating_add(run.uncle_weight); + self.reaching.extend(run.reaching); + } + + /// Show the uncles still waiting for their possible parents the next trunk + /// headers down, newest first, and settle those that have seen them all. + fn absorb(&mut self, difficulties: &[U256]) { + let mut settled = U256::ZERO; + self.reaching.retain_mut(|uncles| { + let seen = uncles.below.min(difficulties.len()); + for difficulty in &difficulties[..seen] { + uncles.highest = uncles.highest.max(*difficulty); + } + uncles.below -= seen; + if uncles.below > 0 { + return true; + } + settled = settled.saturating_add(U256::from(uncles.count).saturating_mul(uncles.highest)); + false + }); + self.uncle_weight = self.uncle_weight.saturating_add(settled); + } + + /// Settle the uncles whose possible parents reach below the walk, on this + /// node's own chain under the anchor, which the store holds. + fn settle_on_own_chain(&mut self) { + let wanted = self.reaching.iter().map(|uncles| uncles.below).max().unwrap_or(0); + let mut difficulties = Vec::with_capacity(wanted); + let mut hash = self.need_hash; + let mut whole = true; + while difficulties.len() < wanted { + let Ok(Some(header)) = self.store.header(hash) else { + whole = false; + break; + }; + difficulties.push(header.difficulty); + // Nothing hangs from below genesis. + if header.number == 0 { + break; + } + hash = header.parent_hash; + } + self.absorb(&difficulties); + if self.reaching.is_empty() { + return; + } + if whole { + // The chain ended at genesis before their stretches did: what they + // have seen is all there is. + let settled = self.reaching.drain(..).fold(U256::ZERO, |sum, uncles| { + sum.saturating_add(U256::from(uncles.count).saturating_mul(uncles.highest)) + }); + self.uncle_weight = self.uncle_weight.saturating_add(settled); + } else { + warn!( + target: "rustock::snap", + "a header under the anchor is missing, so the uncles above it cannot be bounded" + ); + self.reaching.clear(); + self.uncle_weight = U256::MAX; + } + } } diff --git a/crates/sync/src/snap/session.rs b/crates/sync/src/snap/session.rs index 8754139..30d27c4 100644 --- a/crates/sync/src/snap/session.rs +++ b/crates/sync/src/snap/session.rs @@ -1088,29 +1088,28 @@ impl SnapSession { // a walk: on mainnet the second session "verified" the chain in four // seconds, on the strength of the first one's refused total. // - // The walk has read every header from the checkpoint down to a block - // this node already trusted, so the work of that chain is known. The - // claimed cumulative difficulty may exceed it only by the checkpoint - // block's own contribution -- the one header the walk never visits as - // a candidate. + // The walk has read every header from the checkpoint, its first, down + // to a block this node already trusted, and counted each run as it + // joined the chain. Uncles it could not count: they travel in bodies, + // which a header walk never fetches, and rskj peers send none + // alongside. Taken for a ceiling, with only the checkpoint's own + // uncles allowed for, that floor refused every honest peer on mainnet: + // #9381160 carries 6.24e28, and its headers alone 3.54e28. // - // That contribution includes the checkpoint's uncles, whose headers - // are not in hand here: only `uncle_count` is. An uncle is a sibling, - // computed from the same parent, so its difficulty is close to the - // block's own; `difficulty * (1 + uncle_count)` is therefore a sound - // upper bound without pretending to a precision the data does not - // support. Bounding it low instead would reject every honest peer - // whose totals are right, which is what the header-only bound did. + // What the walk did see bounds the uncles it did not, and + // `HeaderWalk::work_ceiling` adds that bound to the floor. With every + // uncle sent, the ceiling is the exact work. // // The claim is written as this node's own total difficulty at the - // checkpoint, so it is worth establishing rather than accepting. - if let Some(established) = self.walk.as_ref().and_then(|w| w.established_difficulty()) { - let ceiling = established.saturating_add( - checkpoint.as_ref().map_or(U256::ZERO, |h| { - h.difficulty - .saturating_mul(U256::from(1u64).saturating_add(U256::from(h.uncle_count))) - }), - ); + // checkpoint, so it is worth bounding rather than accepting. + let judged = self.walk.as_ref().and_then(|walk| { + Some(( + walk.established_difficulty()?, + walk.work_ceiling(self.config.min_difficulty)?, + walk.uncles_omitted(), + )) + }); + if let Some((_, ceiling, _)) = judged { if self.checkpoint_td > ceiling { return self.fail(SnapFailure::OverstatedDifficulty { claimed: self.checkpoint_td, @@ -1156,12 +1155,21 @@ impl SnapSession { let _ = self.store.set_head(hash); } - info!( - target: "rustock::snap", - "header chain verified back to #{}; downloading state at #{}", - self.walk.as_ref().map_or(0, |w| w.frontier()), - checkpoint.as_ref().map_or(0, |h| h.number) - ); + let frontier = self.walk.as_ref().map_or(0, |w| w.frontier()); + let at = checkpoint.as_ref().map_or(0, |h| h.number); + match judged { + Some((established, ceiling, uncles)) => info!( + target: "rustock::snap", + "header chain verified back to #{frontier}: work {established} counted, at most \ + {ceiling} with the {uncles} uncles a header walk cannot see, against {} claimed; \ + downloading state at #{at}", + self.checkpoint_td + ), + None => info!( + target: "rustock::snap", + "header chain verified back to #{frontier}; downloading state at #{at}" + ), + } self.set_phase(Phase::DownloadingState); self.poll() } diff --git a/crates/sync/src/snap/session_tests.rs b/crates/sync/src/snap/session_tests.rs index 54a20c4..828f3df 100644 --- a/crates/sync/src/snap/session_tests.rs +++ b/crates/sync/src/snap/session_tests.rs @@ -104,6 +104,32 @@ fn chain( (blocks, tds) } +/// The same, each block with one uncle as heavy as itself, and totals that +/// count it as rskj's do. +fn chain_with_uncles( + first_number: u64, + n: usize, + from_parent: B256, + base_td: u64, + state_root: B256, +) -> (Vec, Vec) { + let difficulty = 100u64; + let (mut blocks, mut tds) = (Vec::new(), Vec::new()); + let (mut parent, mut td) = (from_parent, base_td); + for i in 0..n { + let number = first_number + i as u64; + let uncle = header(number - 1, B256::repeat_byte(0x0c), B256::ZERO, difficulty); + let mut h = header(number, parent, state_root, difficulty); + h.uncle_count = 1; + h.ommers_hash = rustock_execution::processor::compute_ommers_hash(std::slice::from_ref(&uncle)); + parent = h.hash(); + td += 2 * difficulty; + tds.push(U256::from(td)); + blocks.push(Block { header: h, transactions: Vec::new(), ommers: vec![uncle] }); + } + (blocks, tds) +} + struct Fixture { store: Arc, trie: Arc, @@ -137,6 +163,8 @@ impl Fixture { let genesis = header(0, B256::ZERO, B256::ZERO, 100); let hash = genesis.hash(); self.store.put_header_with_hash(hash, &genesis).expect("genesis"); + // As `setup_genesis` records it: genesis's total difficulty is its own. + self.store.put_total_difficulty(hash, genesis.difficulty).expect("td"); self.store.put_canonical_hash(0, hash).expect("index"); hash } @@ -167,7 +195,6 @@ fn headers_for(blocks: &[Block], point: u64, count: u32) -> Vec
{ /// Answers every header and skeleton request the session makes, until it /// leaves the header phase. Returns the actions it was left with. -#[allow(dead_code)] // no caller; see the dead-code issue before deleting fn answer_header_walk( session: &mut SnapSession, blocks: &[Block], @@ -1715,6 +1742,43 @@ fn offered_totals_may_grow_by_more_than_the_header_difficulty() { ); } +/// **An honest claim counts the uncles a header walk never sees.** rskj peers +/// send no uncles with headers, so the walk counts each block's own +/// difficulty: on mainnet, a little over half the work. Checked against that +/// and the checkpoint's own uncles, every honest claim was refused -- mainnet's +/// #9381160 carries 6.24e28, its headers alone 3.54e28 -- and the peer making +/// it was charged for a lie it had not told. +#[test] +fn an_honest_claim_counts_the_uncles_a_header_walk_never_sees() { + let f = fixture(20); + let genesis = f.with_genesis(); + // Each block has an uncle as heavy as itself: half the work is in uncles. + let (blocks, tds) = chain_with_uncles(1, 4, genesis, 100, f.state_root); + let mut session = f.session(HeaderVerifier::new()); + let actions = session.on_status(&blocks, &tds, 8_000, 0); + answer_header_walk(&mut session, &blocks, actions); + assert_eq!(session.failure(), None); + assert_eq!(session.phase(), Phase::DownloadingState); +} + +/// And what the walk allows for the uncles it did not see is a bound, not a +/// pass: a claim beyond what they could add is refused. +#[test] +fn a_claim_beyond_what_the_unseen_uncles_could_add_is_refused() { + let f = fixture(20); + let genesis = f.with_genesis(); + // Totals far past what four uncles could weigh, even each at the floor. + let (blocks, tds) = chain_with_uncles(1, 4, genesis, 1_000_000_000_000_000_000, f.state_root); + let mut session = f.session(HeaderVerifier::new()); + let actions = session.on_status(&blocks, &tds, 8_000, 0); + answer_header_walk(&mut session, &blocks, actions); + assert!( + matches!(session.failure(), Some(SnapFailure::OverstatedDifficulty { .. })), + "{:?}", + session.failure() + ); +} + /// The check still has teeth: a total that grows by the wrong amount is /// refused, so a peer cannot inflate its claimed work. #[test] diff --git a/docs/difficulty-gate.md b/docs/difficulty-gate.md index 3843ef7..ecafc9e 100644 --- a/docs/difficulty-gate.md +++ b/docs/difficulty-gate.md @@ -397,6 +397,11 @@ The walk only produces the exact number if the peer sends the uncles. From an `rsk/62` peer — every rskj node today — the walk sums header difficulty alone, which is a **lower** bound on the chain's work. That is worse than the gate's upper bound, not better: see `headers.rs` `uncles_omitted`, and issue #258. +The walk does bound what it cannot count (`HeaderWalk::work_ceiling`: each +unseen uncle at most a fiftieth over the heaviest trunk block it could hang +from), so its check of the claim is sound against such a peer too; but that +check comes after the walk, so the gate still decides whether the walk is +worth its cost. Capabilities are negotiated per peer and known before the status response is handled, so this is decidable at run time rather than something to wait for a diff --git a/docs/header-first-sync.md b/docs/header-first-sync.md index a8bd3e3..e733d2a 100644 --- a/docs/header-first-sync.md +++ b/docs/header-first-sync.md @@ -128,7 +128,8 @@ up: This is the same 10.5 GB the current walk would have to pay for an exact total. It does not pay it today, which is #258, and the consequence is that -`walked_difficulty` understates the chain. +`walked_difficulty` understates the chain, and the walk bounds the rest from +above (`HeaderWalk::work_ceiling`) rather than counting it. ### RSKIP-699 does not remove this From 20a128be2f76f687266da45b7e99060df7159c01 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Fri, 6 Nov 2026 05:59:15 -0800 Subject: [PATCH 007/264] snap: place each rskj chunk in the trie and ask where the last one ended The light node's state download on mainnet left 45.7% of the state at #9,381,160 unfetched, found only by the completeness check after an hour's download, five attempts running. An rskj server serves `from` to `from + 50 * 1024` whatever the request asks (getSnapshotChunkSize() in RskSystemProperties is fixed at 50, SnapshotProcessor.CHUNK_ITEM_SIZE is 1024), and the client advanced by the 100,000-byte cell it had asked for, so the last 48,800 bytes of every cell were marked done and never fetched. snap_holes, a new read-only example that walks a stored trie and lists what it lacks, put every missing byte past the first 51,200 of its cell. The rebuild now places a chunk. The rebuilt tree is the top of the real trie, and a parent's children_size commits to both children together, so beside a carried child the one left out is the difference; descending from the root gives each carried node its offset. Rebuilt::span is where the chunk's own nodes begin and end, and their sizes must fill that stretch exactly or the chunk is refused (Unplaced). Rebuilt::total is the whole trie's size, from the root, so a peer's hint no longer decides where the download ends. The client keeps a chunk only if it begins at or before the offset asked, moves the cursor to where the chunk ended, and asks for exactly that offset next. A chunk that begins after the offset is charged. One with no nodes of its own, or a refusal of the offset as off the grid or past the end, sends the next request back to the grid, uncharged. Asking at the cursor also steps around a fault in rskj's own client, which asks for k * cell and trusts each chunk to begin where the one before ended. Seeking an offset inside a node's embedded right child, TrieDTOInOrderIterator counts from the child rather than the node, so the chunk stops short of its cell, and the nodes in the shortfall belong to no chunk. rskj_vectors shows both: stepping by the cell loses nodes, asking at the cursor loses none. Two faults in the rebuild itself are fixed. The long values of embedded children, which is where mainnet keeps contract code, were dropped; they are kept now. And a child hung where its parent's flags have none, so that it would never enter the parent's message, is refused (Dangling). docs/rskj-to-rustock-map.md no longer says the client half is to come. --- crates/cli/examples/snap_holes.rs | 218 +++++++++++++ crates/sync/src/snap/client.rs | 103 ++++-- crates/sync/src/snap/session.rs | 36 ++- crates/sync/src/snap/session_tests.rs | 134 ++++++++ crates/sync/src/snap/tests.rs | 157 +++++++++ crates/trie/src/snapshot_legacy.rs | 171 +++++++++- crates/trie/tests/rskj_vectors.rs | 440 ++++++++++++++++++++++++++ docs/rskj-to-rustock-map.md | 5 +- docs/snapshot-sync.md | 59 +++- 9 files changed, 1283 insertions(+), 40 deletions(-) create mode 100644 crates/cli/examples/snap_holes.rs diff --git a/crates/cli/examples/snap_holes.rs b/crates/cli/examples/snap_holes.rs new file mode 100644 index 0000000..9e4dd32 --- /dev/null +++ b/crates/cli/examples/snap_holes.rs @@ -0,0 +1,218 @@ +//! Where are the holes in a downloaded state? +//! +//! Walks the stored trie under a state root in traversal order -- the order +//! snapshot chunks tile -- and reports every subtree that a stored node +//! references but the store does not hold: where it sits in offset space and +//! how large it is. A download that skipped the same part of every cell shows +//! up as holes at the same place in each cell, which is what `--grid` and +//! `--served` test: how much of the missing space falls past the first +//! `served` bytes of a `grid`-sized cell. +//! +//! A missing child's size is not read from the child, which is the thing that +//! is missing, but from its parent: `children_size` commits to both children +//! together, so with one child present the other's size is the difference. +//! With both missing only the pair's span is known, and the hole is reported +//! as one. +//! +//! Opens the trie read-only, so it is safe beside a running node -- with the +//! read-only caveat that writes still in the node's memtables are not seen. +//! +//! Usage: snap_holes [limit-bytes] [grid] [served] + +use alloy_primitives::B256; +use rustock_storage::epoch_store::{EpochConfig, EpochTrieStore}; +use rustock_storage::BlockStore; +use rustock_trie::snapshot::{stream_size, total_size}; +use rustock_trie::{NodeRef, TrieNode, TrieStore}; +use std::sync::Arc; +use std::time::Instant; + +struct Holes { + limit: u64, + /// Start, end, and whether the end is exact (one child missing) or the + /// span of a pair (both missing). + found: Vec<(u64, u64, bool)>, + missing_bytes: u64, + present_nodes: u64, + present_bytes: u64, +} + +impl Holes { + fn hole(&mut self, start: u64, end: u64, exact: bool) { + self.missing_bytes += end - start; + self.found.push((start, end, exact)); + } +} + +/// A child stored as its own entry: present, missing, or not a separate entry. +enum Child { + None, + Embedded(u64), + Present(Arc, u64), + Missing, +} + +fn child(r: &NodeRef, store: &dyn TrieStore) -> Child { + match r { + NodeRef::Empty => Child::None, + // Read from a store, an embedded child is parsed in place and a + // separate one is a hash; nothing else reaches here. + NodeRef::Node(n) => Child::Embedded(total_size(n, store)), + NodeRef::Hash(_) => match r.resolve(store) { + Some(n) => { + let size = total_size(&n, store); + Child::Present(n, size) + } + None => Child::Missing, + }, + } +} + +fn walk(node: &TrieNode, start: u64, store: &dyn TrieStore, h: &mut Holes) { + if start >= h.limit { + return; + } + let here = stream_size(node, store); + let left = child(&node.left, store); + let right = child(&node.right, store); + + let embedded: u64 = [&left, &right] + .iter() + .map(|c| if let Child::Embedded(s) = c { *s } else { 0 }) + .sum(); + let separate = |c: &Child| match c { + Child::Present(_, s) => Some(*s), + Child::Missing => None, + _ => Some(0), + }; + // children_size = separate left + separate right + embedded. + let pair = node.children_size.saturating_sub(embedded); + let (lsize, rsize) = match (separate(&left), separate(&right)) { + (Some(l), Some(r)) => (l, r), + (None, Some(r)) => (pair.saturating_sub(r), r), + (Some(l), None) => (l, pair.saturating_sub(l)), + (None, None) => { + h.present_nodes += 1; + h.present_bytes += here; + // Only the pair's span is known; this node's own entry sits + // somewhere inside it. + h.hole(start, start + pair + here, false); + return; + } + }; + + match left { + Child::Present(n, _) => walk(&n, start, store, h), + Child::Missing => h.hole(start, start + lsize, true), + _ => {} + } + let at = start + lsize; + if at < h.limit { + h.present_nodes += 1; + h.present_bytes += here; + } + let rstart = at + here; + match right { + Child::Present(n, _) => walk(&n, rstart, store, h), + Child::Missing => { + if rstart < h.limit { + h.hole(rstart, rstart + rsize, true) + } + } + _ => {} + } +} + +fn main() -> anyhow::Result<()> { + let a: Vec = std::env::args().collect(); + if a.len() < 3 { + eprintln!("usage: snap_holes [limit-bytes] [grid] [served]"); + std::process::exit(2); + } + let dir = &a[1]; + let limit: u64 = a.get(3).and_then(|s| s.parse().ok()).unwrap_or(20_000_000); + let grid: u64 = a.get(4).and_then(|s| s.parse().ok()).unwrap_or(100_000); + let served: u64 = a.get(5).and_then(|s| s.parse().ok()).unwrap_or(50 * 1024); + + let root_hash: B256 = if let Some(hex) = a[2].strip_prefix("0x") { + hex.parse()? + } else { + let number: u64 = a[2].parse()?; + let blocks = BlockStore::open_read_only(dir)?; + let hash = blocks + .canonical_hash(number)? + .ok_or_else(|| anyhow::anyhow!("no canonical block at #{number}"))?; + let header = blocks + .header(hash)? + .ok_or_else(|| anyhow::anyhow!("no header for #{number}"))?; + header.state_root + }; + + let trie_dir = std::env::var("RUSTOCK_TRIE_DIR").unwrap_or_else(|_| format!("{dir}/trie-epochs")); + let store = EpochTrieStore::open_read_only(&trie_dir, EpochConfig::default())?; + let message = store + .get(root_hash.as_slice()) + .ok_or_else(|| anyhow::anyhow!("state root {root_hash:?} is not in the store"))?; + let root = TrieNode::try_from_message(&message, &store) + .ok_or_else(|| anyhow::anyhow!("the state root does not parse"))?; + let total = total_size(&root, &store); + + let started = Instant::now(); + let mut h = Holes { + limit: limit.min(total), + found: Vec::new(), + missing_bytes: 0, + present_nodes: 0, + present_bytes: 0, + }; + // A trie is hundreds of levels deep at most, but give the recursion room. + let h = std::thread::scope(|s| { + std::thread::Builder::new() + .stack_size(256 << 20) + .spawn_scoped(s, || { + walk(&root, 0, &store, &mut h); + h + }) + .expect("spawn") + .join() + .expect("walk") + }); + + println!("state root {root_hash:?}, {total} bytes; walked offsets 0..{} in {:?}", h.limit, started.elapsed()); + println!( + "present: {} nodes, {} bytes of entries; missing: {} subtree(s), {} bytes ({:.1}% of the span walked)", + h.present_nodes, + h.present_bytes, + h.found.len(), + h.missing_bytes, + h.missing_bytes as f64 * 100.0 / h.limit.max(1) as f64 + ); + + // How much of the missing space lies past the first `served` bytes of a + // cell, the part a server serving `served` per request never sends. + let mut unserved = 0u64; + for &(s, e, _) in &h.found { + let mut x = s; + while x < e { + let cell = x / grid * grid; + let next = (cell + grid).min(e); + let gap_from = (cell + served).max(x); + if gap_from < next { + unserved += next - gap_from; + } + x = next; + } + } + println!( + "of the missing bytes, {:.1}% lie past the first {served} bytes of a {grid}-byte cell \ + (a random hole would put {:.1}% there)", + unserved as f64 * 100.0 / h.missing_bytes.max(1) as f64, + (grid.saturating_sub(served)) as f64 * 100.0 / grid as f64 + ); + println!(); + println!("{:>14} {:>14} {:>9} {:>8} {:>8} exact", "start", "end", "bytes", "start%g", "end%g"); + for &(s, e, exact) in h.found.iter().take(40) { + println!("{s:>14} {e:>14} {:>9} {:>8} {:>8} {exact}", e - s, s % grid, e % grid); + } + Ok(()) +} diff --git a/crates/sync/src/snap/client.rs b/crates/sync/src/snap/client.rs index e761202..2dcf126 100644 --- a/crates/sync/src/snap/client.rs +++ b/crates/sync/src/snap/client.rs @@ -74,6 +74,10 @@ struct Slice { /// its responses below the grid size will do that -- because asking for /// the same cell again would produce the same answer forever. Costs a /// cache miss at that server, which is strictly better than a stall. + /// + /// Set after every rskj chunk too. An rskj server answers any offset, + /// starting with the node that ends there, so asking for the cursor picks + /// up exactly where the last chunk stopped, whatever size it serves. exact: bool, } @@ -122,6 +126,11 @@ const OVERSIZE_FACTOR: u64 = 4; pub struct RebuiltChunk { pub nodes: Vec<(B256, Vec)>, pub long_values: Vec>, + /// The size of the whole trie, which the rebuilt root commits to. + pub total: u64, + /// Where the chunk's own nodes begin and end in the traversal, as the + /// rebuild placed them. `None` when it carried none. + pub span: Option<(u64, u64)>, } #[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] @@ -134,6 +143,10 @@ pub enum ChunkError { LegacyRebuild(String), #[error("the answer is {got} bytes against a {asked} byte request")] Oversized { asked: u64, got: u64 }, + #[error("the chunk begins at offset {starts}, after the offset {asked} it answers")] + Misplaced { asked: u64, starts: u64 }, + #[error("the chunk carried no nodes of its own")] + Empty, } /// Downloading one state trie. @@ -242,11 +255,22 @@ impl StateDownload { /// undo. Called on a timeout, a disconnect, or a chunk that failed to /// verify. pub fn release(&mut self, from: u64) { - let grid = self.grid; if let Some(i) = self.slice_for(from) { self.slices[i].in_flight = false; } - let _ = grid; + } + + /// Give back a request whose offset did not suit the server -- not on its + /// grid, or past an end the hint had wrong -- and ask on the grid next. + /// + /// An exact offset is what an rskj server needs and what a rustock server + /// refuses, and either may be asked next; the grid is the request every + /// server takes. + pub fn realign(&mut self, from: u64) { + if let Some(i) = self.slice_for(from) { + self.slices[i].in_flight = false; + self.slices[i].exact = false; + } } /// The slice an answer for `from` belongs to. @@ -349,14 +373,53 @@ impl StateDownload { /// Keep a chunk that arrived in rskj's format. /// /// It has already proved itself: the only way to read one is to rebuild - /// it, and the rebuild is checked against the state root. So there is - /// nothing left to verify here, only nodes to store and a cursor to move. + /// it, and the rebuild is checked against the state root. The rebuild + /// also placed it -- from the sizes the rebuilt tree commits to, where its + /// first node begins, where its last ends, and that nothing between them + /// was left out -- so the slice advances to where the chunk really + /// reached. + /// + /// It used to advance by the cell it asked for, on the reasoning that + /// rskj's format states no offsets. But an rskj server does not serve the + /// cell asked for: it serves `from` to `from + 50 * 1024`, a constant on + /// its side, whatever the request says. On rustock's 100,000-byte grid + /// the last 48,800 bytes of every cell were marked done and never + /// fetched. On mainnet that was 45.7% of the state, found only by the + /// check after an hour's download, five attempts running. /// - /// The offsets it covers are not stated anywhere -- rskj's format carries - /// no per-node offsets and the rebuild derives structure, not position -- - /// so the slice advances by the cell it asked for rather than by where - /// the nodes turned out to sit. - pub fn accept_rebuilt(&mut self, from: u64, chunk: &RebuiltChunk) -> Progress { + /// The next request then names the cursor exactly: an rskj server + /// answers any offset, beginning with the node that ends there, so the + /// next chunk picks up where this one stopped. + pub fn accept_rebuilt( + &mut self, + from: u64, + chunk: &RebuiltChunk, + ) -> Result { + if self.slice_for(from).is_none() { + return Err(ChunkError::Unsolicited(from)); + } + + // The rebuilt root commits to the size of the whole trie, as a proved + // chunk's does. Taken before anything else, so that a range a peer's + // overstated hint put past the end is trimmed away. + if self.total.is_none() { + self.adopt_total(chunk.total); + } + + // Nothing of its own is what rskj sends for an offset past the end. + // Whatever was asked, the grid is the next thing to try. + let Some((starts, reached)) = chunk.span else { + self.realign(from); + return Err(ChunkError::Empty); + }; + // An answer to `from` begins with the node that holds it. One that + // begins after it is not an answer to the question, and taking it + // would leave a gap behind the cursor. + if starts > from { + self.release(from); + return Err(ChunkError::Misplaced { asked: from, starts }); + } + for (hash, message) in &chunk.nodes { self.store.put(hash.as_slice(), message); self.stored_bytes += message.len() as u64; @@ -367,24 +430,24 @@ impl StateDownload { } self.stored_nodes += chunk.nodes.len() as u64; - let reached = from.saturating_add(self.grid); let mut advanced = 0; if let Some(i) = self.slice_for(from) { let slice = &mut self.slices[i]; - advanced = reached.saturating_sub(slice.cursor).min(slice.end - slice.cursor); - slice.exact = reached <= slice.cursor; + advanced = reached + .saturating_sub(slice.cursor) + .min(slice.end.saturating_sub(slice.cursor)); slice.cursor = reached.max(slice.cursor); + slice.exact = true; slice.in_flight = false; } - // The size of the trie comes from the root, which a rebuilt chunk - // always reconstructs; without a parsed root here it stays whatever - // the peer advertised until a proved chunk settles it. - if self.total.is_none() { - self.adopt_total(self.hinted_total); - } + trace!( + target: "rustock::snap", + "accepted {} rskj-format nodes from offset {from}, covering {starts}..{reached}", + chunk.nodes.len() + ); - Progress { nodes: chunk.nodes.len(), advanced, complete: self.is_complete() } + Ok(Progress { nodes: chunk.nodes.len(), advanced, complete: self.is_complete() }) } /// Replace the hinted size with the one the root proves, and reshape the @@ -580,6 +643,8 @@ pub fn chunk_from_payload( Ok(Arrived::Rebuilt(RebuiltChunk { nodes: rebuilt.nodes, long_values: rebuilt.long_values, + total: rebuilt.total, + span: rebuilt.span, })) } } diff --git a/crates/sync/src/snap/session.rs b/crates/sync/src/snap/session.rs index 30d27c4..08aa355 100644 --- a/crates/sync/src/snap/session.rs +++ b/crates/sync/src/snap/session.rs @@ -1192,12 +1192,18 @@ impl SnapSession { // much: it costs it nothing to lie, but the lie only wastes our // requests, and the alternative is guessing from an empty payload. if refusal != Refusal::None { - self.chunk_fault = Some(if refusal.worth_asking_again() { + let realign = refusal.worth_asking_again(); + self.chunk_fault = Some(if realign { ChunkFault::Realign } else { ChunkFault::Declined }); debug!(target: "rustock::snap", "peer refused offset {from}: {refusal:?}"); + if realign { + if let Some(download) = self.download.as_mut() { + download.realign(from); + } + } return self.fruitless(from); } @@ -1219,16 +1225,30 @@ impl SnapSession { // An rskj chunk proves itself by rebuilding to the root, which has // already happened -- there is no way to inspect one without - // reconstructing it. What is left is to keep the nodes. + // reconstructing it. The rebuild placed it too, so what is left is to + // check it answers what was asked, and keep the nodes. let proof = match arrived { super::client::Arrived::Rebuilt(rebuilt) => { let Some(download) = self.download.as_mut() else { return Vec::new() }; - let progress = download.accept_rebuilt(from, &rebuilt); - self.fruitless_chunks = 0; - return if progress.complete { - self.finish_state_download() - } else { - self.poll() + return match download.accept_rebuilt(from, &rebuilt) { + Ok(progress) => { + self.fruitless_chunks = 0; + if progress.complete { + self.finish_state_download() + } else { + self.poll() + } + } + Err(e) => { + debug!(target: "rustock::snap", "rskj chunk from offset {from} refused: {e}"); + self.chunk_fault = Some(match e { + // Nothing of its own: past the end, as rskj answers + // an offset there. About the offset, not the peer. + ChunkError::Empty => ChunkFault::Realign, + other => ChunkFault::Misbehaved(other), + }); + self.fruitless(from) + } }; } super::client::Arrived::Proved(proof) => proof, diff --git a/crates/sync/src/snap/session_tests.rs b/crates/sync/src/snap/session_tests.rs index 828f3df..69597e3 100644 --- a/crates/sync/src/snap/session_tests.rs +++ b/crates/sync/src/snap/session_tests.rs @@ -856,6 +856,140 @@ fn an_rskj_chunk_that_does_not_rebuild_is_charged() { assert_eq!(blame[0].event, EventType::InvalidMessage); } +/// An rskj chunk of the fixture's state, as an rskj server answers `from`. +fn rskj_chunk(f: &Fixture, from: u64, cell: u64) -> ChunkPayload { + use rustock_trie::snapshot_legacy::{encode_blob, legacy_chunk}; + let root: [u8; 32] = f.state_root.into(); + let chunk = legacy_chunk(&root, from, from + cell, f.trie.as_ref()).expect("a chunk"); + ChunkPayload::Legacy(encode_blob(&chunk).into()) +} + +/// An honest rskj chunk is kept, moves the download as far as it reached, +/// and costs its sender nothing. +#[test] +fn an_honest_rskj_chunk_is_kept_and_not_charged() { + let f = fixture(250); + let genesis = f.with_genesis(); + let (blocks, tds) = chain(1, 3, genesis, 100, f.state_root); + let peers = vec![B512::repeat_byte(1), B512::repeat_byte(2)]; + + let (mut driver, id, from, server) = driver_awaiting_a_chunk(&f, &blocks, &tds, &peers); + driver.take_blame(); + + driver.on_chunk(id, server, &rskj_chunk(&f, from, 4_000), Refusal::None, &peers); + + assert!(driver.take_blame().is_empty(), "an honest rskj peer was charged"); + let (covered, total) = driver.state_progress(); + assert!(covered > 0, "the chunk moved nothing"); + assert_eq!( + total, + Some(rustock_trie::snapshot::total_size(&f.trie_root, f.trie.as_ref())), + "the size of the trie comes from the rebuilt root, not the peer's hint" + ); +} + +/// An rskj chunk that answers another offset is charged: its nodes are +/// genuine, but it is not an answer to the question, any more than a proved +/// chunk for the wrong offset is. +#[test] +fn an_rskj_chunk_for_another_offset_is_charged() { + let f = fixture(250); + let genesis = f.with_genesis(); + let (blocks, tds) = chain(1, 3, genesis, 100, f.state_root); + let peers = vec![B512::repeat_byte(1), B512::repeat_byte(2)]; + + let (mut driver, id, from, server) = driver_awaiting_a_chunk(&f, &blocks, &tds, &peers); + driver.take_blame(); + + let total = rustock_trie::snapshot::total_size(&f.trie_root, f.trie.as_ref()); + let elsewhere = rskj_chunk(&f, from + total / 2, 1_000); + driver.on_chunk(id, server, &elsewhere, Refusal::None, &peers); + + let blame = driver.take_blame(); + assert_eq!(blame.len(), 1, "got {blame:?}"); + assert_eq!(blame[0].peer, server); + assert_eq!(blame[0].event, EventType::InvalidMessage); + assert_eq!(driver.state_progress().0, 0, "a chunk for another offset moved the download"); +} + +/// An rskj chunk with nothing of its own -- what rskj answers when the node at +/// the offset outruns its cell, or the offset is past the end -- is not held +/// against the peer. It is about the offset, and the range is asked again. +#[test] +fn an_rskj_chunk_with_nothing_of_its_own_is_not_charged() { + let f = fixture(250); + let genesis = f.with_genesis(); + let (blocks, tds) = chain(1, 3, genesis, 100, f.state_root); + let peers = vec![B512::repeat_byte(1), B512::repeat_byte(2)]; + + let (mut driver, id, from, server) = driver_awaiting_a_chunk(&f, &blocks, &tds, &peers); + driver.take_blame(); + + // A one-byte cell: no node fits, so the chunk carries only the path to it. + let nothing = rskj_chunk(&f, from, 1); + let ChunkPayload::Legacy(blob) = ¬hing else { unreachable!() }; + let chunk = rustock_trie::snapshot_legacy::decode_blob(blob).expect("decodes"); + assert!(chunk.nodes.is_empty() && !chunk.post_root.is_empty(), "not the chunk this test is about"); + + driver.on_chunk(id, server, ¬hing, Refusal::None, &peers); + assert!(driver.take_blame().is_empty(), "charged a peer for the offset it was asked"); + assert!(!driver.unhelpful().contains(&server), "stopped asking a peer that can serve"); + assert_eq!(driver.state_progress().0, 0); +} + +/// The chunk requests among a batch of outbound messages: id, offset, peer. +fn chunk_requests(out: &[super::driver::Outbound]) -> Vec<(u64, u64, B512)> { + out.iter() + .filter_map(|o| { + let P2pMessage::RskMessage(m) = &o.message else { return None }; + let RskSubMessage::SnapChunkRequest(req) = &m.sub_message else { return None }; + Some((req.id, req.from, o.peer)) + }) + .collect() +} + +/// After an rskj chunk the session asks for exactly where it stopped, which +/// only an rskj server takes; a server that refuses the offset as off its +/// grid gets the cell next, not the same offset again. +#[test] +fn an_offset_refused_as_off_the_grid_is_asked_on_the_grid_next() { + let f = fixture(250); + let genesis = f.with_genesis(); + let (blocks, tds) = chain(1, 3, genesis, 100, f.state_root); + let peers = vec![B512::repeat_byte(1), B512::repeat_byte(2)]; + + let (mut driver, id, from, server) = driver_awaiting_a_chunk(&f, &blocks, &tds, &peers); + driver.on_chunk(id, server, &rskj_chunk(&f, from, 2_000), Refusal::None, &peers); + let (cursor, _) = driver.state_progress(); + assert!(cursor > 0, "the chunk moved nothing"); + // Let whatever else the setup left outstanding time out, so the budget + // has room for the request this is about. Timing out is charged; that is + // not what this test is about. + driver.expire(std::time::Instant::now() + std::time::Duration::from_secs(60)); + driver.take_blame(); + let out = driver.poll(&peers); + + let exact = chunk_requests(&out); + let &(id, asked, peer) = exact + .iter() + .find(|(_, asked, _)| *asked == cursor) + .unwrap_or_else(|| panic!("nothing asked at the cursor {cursor}: {exact:?}")); + assert_ne!(asked % 100_000, 0, "the exact offset happens to be on the grid; the test proves nothing"); + + let refused = ChunkPayload::Proved { entries: Vec::new(), witness: Vec::new() }; + let out = driver.on_chunk(id, peer, &refused, Refusal::OffsetNotOnGrid, &peers); + assert!(driver.take_blame().is_empty(), "a refusal about the offset was charged"); + let next = chunk_requests(&out); + assert!( + next.iter().any(|(_, asked, _)| *asked == cursor / 100_000 * 100_000), + "the refused offset was not replaced by its cell: {next:?}" + ); + assert!( + next.iter().all(|(_, asked, _)| *asked != cursor), + "the refused offset was asked again: {next:?}" + ); +} + /// A peer that offers a chain which does not link is charged for the status,/// A peer that offers a chain which does not link is charged for the status, /// not merely ignored. #[test] diff --git a/crates/sync/src/snap/tests.rs b/crates/sync/src/snap/tests.rs index 51d692a..69784ee 100644 --- a/crates/sync/src/snap/tests.rs +++ b/crates/sync/src/snap/tests.rs @@ -1550,3 +1550,160 @@ fn a_missing_root_is_caught() { assert!(err.contains("root"), "unhelpful complaint: {err}"); } +/// An rskj server, serving as rskj does: whatever the request says, the nodes +/// from the one holding `from` until `from + cell`, in rskj's format, through +/// the wire. rskj's `cell` is `50 * 1024`, a constant on its side. +fn rskj_serves(peer: &Peer, from: u64, cell: u64) -> SnapChunkResponse { + use rustock_trie::snapshot_legacy::{encode_blob, legacy_chunk}; + let root: [u8; 32] = peer.root_hash.into(); + let chunk = legacy_chunk(&root, from, from + cell, peer.store.as_ref()).expect("a chunk"); + let response = SnapChunkResponse { + id: 1, + payload: ChunkPayload::Legacy(encode_blob(&chunk).into()), + block_number: 100, + from, + to: from + cell, + complete: false, + refusal: rustock_networking::protocol::snap::Refusal::None, + }; + let encoded = response.encode_body(); + SnapChunkResponse::decode_body(&mut encoded.as_slice()).expect("survives the wire") +} + +/// The rebuilt chunk an rskj server's answer comes to. +fn rebuilt(peer: &Peer, response: &SnapChunkResponse) -> super::client::RebuiltChunk { + use super::client::{chunk_from_payload, Arrived}; + match chunk_from_payload(&response.payload, peer.root_hash) { + Ok(Arrived::Rebuilt(chunk)) => chunk, + other => panic!("an rskj chunk must rebuild: {other:?}"), + } +} + +/// Downloads a state from rskj servers alone, as mainnet's are, with this +/// node's own grid. Returns the client, its store, and how many requests it +/// took. +fn download_from_rskj(peer: &Peer, grid: u64, cell: u64) -> (StateDownload, Arc, usize) { + let local = Arc::new(MemoryTrieStore::new()); + let config = SnapConfig { chunk_grid: grid, ..config(4, grid) }; + let mut client = StateDownload::new(peer.root_hash, peer.total(), &config, local.clone()); + // Several times what a download that moves needs, so that one that has + // stopped moving fails at once rather than after a hundred thousand + // rebuilds. + let enough = 4 * (peer.total() / cell + 1) as usize + 64; + let mut asked = 0; + while !client.is_complete() { + asked += 1; + assert!(asked < enough, "{asked} requests and not done: the download has stopped moving"); + let request = client.next_request().expect("work left and none in flight"); + let chunk = rebuilt(peer, &rskj_serves(peer, request.from, cell)); + client.accept_rebuilt(request.from, &chunk).expect("an honest rskj chunk"); + } + (client, local, asked) +} + +/// Every node of the peer's trie, and every long value, is in `local`. +fn holds_everything(peer: &Peer, local: &MemoryTrieStore) { + use rustock_trie::snapshot::chunk_from; + for node in chunk_from(&peer.root, 0, u64::MAX, peer.store.as_ref()) { + assert!( + local.get(keccak256(&node.message).as_slice()).is_some(), + "the node at offset {} was never fetched", + node.offset + ); + for value in &node.long_values { + assert!(local.get(keccak256(value).as_slice()).is_some(), "a long value was never fetched"); + } + } +} + +/// **An rskj server fills every cell of a wider grid.** +/// +/// rskj serves `from` to `from + 50 * 1024` whatever is asked, and this client +/// used to mark the whole 100,000-byte cell it asked for as done. On mainnet +/// that left the last 48,800 bytes of every cell unfetched -- 45.7% of the +/// state, which nothing noticed until the check after an hour's download, +/// five attempts running. The cursor now moves as far as the chunk reached, +/// and the next request picks up exactly there. +#[test] +fn an_rskj_server_fills_every_cell_of_a_wider_grid() { + use crate::snap::RSKJ_CHUNK_GRID; + + let peer = Peer::new(12_000); + let total = peer.total(); + assert!(total > 6 * 100_000, "the trie must span several cells, not {total} bytes"); + + let (client, local, asked) = download_from_rskj(&peer, 100_000, RSKJ_CHUNK_GRID); + client.verify_stored().expect("the downloaded state is whole"); + holds_everything(&peer, &local); + + // And at one request per rskj cell, give or take one per slice: an + // answer that stops mid-cell is followed by a request for exactly where + // it stopped, not by the same cell again. + let cells = total.div_ceil(RSKJ_CHUNK_GRID) as usize; + assert!(asked <= cells + cells / 10 + 2 * 4, "{asked} requests for {cells} rskj cells"); +} + +/// **A short answer is not taken for the cell.** A server serving less than +/// anything this client expects still leaves no hole: each answer advances +/// the download by what it carried. +#[test] +fn a_short_rskj_chunk_is_not_taken_for_the_cell() { + let peer = Peer::new(3_000); + let (client, local, _) = download_from_rskj(&peer, 100_000, 7_000); + client.verify_stored().expect("the downloaded state is whole"); + holds_everything(&peer, &local); +} + +/// **An rskj chunk that begins after the offset asked for is refused**, and +/// leaves the store and the range as they were. Its nodes are genuine -- it +/// rebuilds to the root -- but it is not an answer to the question, and +/// taking it would leave a gap behind the cursor. +#[test] +fn an_rskj_chunk_that_begins_after_the_offset_asked_is_refused() { + use crate::snap::RSKJ_CHUNK_GRID; + + let peer = Peer::new(3_000); + assert!(peer.total() > 4 * RSKJ_CHUNK_GRID, "the trie must span several rskj cells"); + let local = Arc::new(MemoryTrieStore::new()); + let mut client = StateDownload::new(peer.root_hash, peer.total(), &config(1, 100_000), local.clone()); + + let request = client.next_request().expect("a request"); + let elsewhere = rebuilt(&peer, &rskj_serves(&peer, request.from + 2 * RSKJ_CHUNK_GRID, RSKJ_CHUNK_GRID)); + match client.accept_rebuilt(request.from, &elsewhere) { + Err(ChunkError::Misplaced { asked, starts }) => { + assert_eq!(asked, request.from); + assert!(starts > request.from); + } + other => panic!("a chunk from elsewhere was taken: {other:?}"), + } + assert_eq!(local.len(), 0, "a refused chunk wrote to the store"); + assert_eq!(client.progress().0, 0, "a refused chunk moved the cursor"); + assert_eq!(client.next_request().map(|r| r.from), Some(request.from), "the range was not asked again"); +} + +/// **After an rskj chunk the next request names the cursor; refused, it goes +/// back to the grid.** An rskj server answers any offset and a rustock server +/// only its grid's, and the next request may go to either. +#[test] +fn a_refused_exact_offset_is_asked_on_the_grid_next() { + use crate::snap::RSKJ_CHUNK_GRID; + + let peer = Peer::new(3_000); + let local = Arc::new(MemoryTrieStore::new()); + let mut client = StateDownload::new(peer.root_hash, peer.total(), &config(1, 100_000), local); + + let first = client.next_request().expect("a request"); + assert_eq!(first.from, 0); + let chunk = rebuilt(&peer, &rskj_serves(&peer, first.from, RSKJ_CHUNK_GRID)); + client.accept_rebuilt(first.from, &chunk).expect("an honest chunk"); + let (cursor, _) = client.progress(); + assert!(cursor > 0 && cursor < 100_000, "an rskj chunk stops mid-cell, at {cursor}"); + + let exact = client.next_request().expect("more to fetch"); + assert_eq!(exact.from, cursor, "the next request does not pick up where the chunk stopped"); + + client.realign(exact.from); + let aligned = client.next_request().expect("more to fetch"); + assert_eq!(aligned.from, 0, "a refused offset was asked again instead of the cell"); +} + diff --git a/crates/trie/src/snapshot_legacy.rs b/crates/trie/src/snapshot_legacy.rs index a965468..897f68f 100644 --- a/crates/trie/src/snapshot_legacy.rs +++ b/crates/trie/src/snapshot_legacy.rs @@ -475,6 +475,15 @@ pub struct Rebuilt { pub long_values: Vec>, /// The state root the reconstruction produced. pub root: B256, + /// The size of the whole trie, which the root commits to. + pub total: u64, + /// The stretch of the traversal the chunk's own nodes fill, from where + /// the first begins to where the last ends. `None` when it carried none. + /// + /// rskj's format states no offsets, but it does not need to: see + /// `span_of`. This is what a download advances by, rather than by the + /// range it asked for, which an rskj server does not honour. + pub span: Option<(u64, u64)>, } #[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] @@ -489,6 +498,10 @@ pub enum RebuildError { WrongRoot { expected: B256, got: B256 }, #[error("the chunk is empty")] Empty, + #[error("node {index} hangs under a parent that has no such child")] + Dangling { index: usize }, + #[error("the chunk's nodes are not one unbroken stretch of the trie")] + Unplaced, } /// One node as the blob carries it. @@ -504,6 +517,16 @@ struct Stub { after_path: usize, /// The embedded children, already converted to consensus form. embedded: Vec>, + /// Their whole sizes, added up. `children_size` counts them, and in the + /// traversal they sit inside this node's own entry. + embedded_total: u64, + /// Their long values, which rskj's format inlines in the child. + /// + /// The ones that matter: a contract's code is a long value, and its leaf + /// -- a one-byte path, the value's hash and length -- is small enough to + /// embed in the account node. Dropped, every contract's code would be a + /// hash with nothing behind it. + embedded_values: Vec>, /// The inlined value, if the node carries one. value: Vec, has_long_value: bool, @@ -534,6 +557,12 @@ struct Stub { /// would have to collide with the root hash. It is sound, but it establishes /// the property at the end rather than by construction -- which is why /// rustock asks for its own format when it can. +/// +/// # Where the chunk sits +/// +/// Not stated by the chunk, and not needed: the rebuilt tree places its own +/// nodes, from the sizes it commits to. See `span_of`, which also refuses +/// a chunk that leaves out a stretch between its first node and its last. pub fn rebuild(chunk: &LegacyChunk, expected_root: B256) -> Result { let mut stubs = assemble(chunk)?; if stubs.is_empty() { @@ -544,6 +573,8 @@ pub fn rebuild(chunk: &LegacyChunk, expected_root: B256) -> Result> = vec![None; stubs.len()]; + // Each node's own bytes: its message, and the value it keeps outside it. + let mut own: Vec = vec![0; stubs.len()]; let mut nodes: Vec<(B256, Vec)> = Vec::with_capacity(stubs.len()); let mut long_values: Vec> = Vec::new(); @@ -560,6 +591,17 @@ pub fn rebuild(chunk: &LegacyChunk, expected_root: B256) -> Result hashes[child], None => stubs[index].given_left.map(B256::from), @@ -574,6 +616,9 @@ pub fn rebuild(chunk: &LegacyChunk, expected_root: B256) -> Result Result None, + n => { + let first = chunk.pre_root.len(); + Some(span_of(&stubs, &own, &left_of, &right_of, root_index, first, first + n - 1)?) + } + }; + Ok(Rebuilt { nodes, long_values, root, total, span }) +} + +/// Whether a node's left child is a node of its own: present, not embedded. +fn separate_left(flags: u8) -> bool { + flags & 0b0000_1000 != 0 && flags & 0b0000_0010 == 0 +} + +/// Whether a node's right child is a node of its own: present, not embedded. +fn separate_right(flags: u8) -> bool { + flags & 0b0000_0100 != 0 && flags & 0b0000_0001 == 0 +} + +/// Where a chunk's own nodes sit in the traversal, and that nothing between +/// them was left out. +/// +/// rskj's format carries no offsets, and does not need to. The tree a chunk +/// rebuilds to is the top of the real trie -- every node it carries, and the +/// hash of every subtree it leaves out -- and a subtree left out keeps its +/// size: a parent's `children_size` commits to its two children together, so +/// beside a carried child the other is the difference. Descending from the +/// root places every carried node exactly, by the arithmetic of +/// [`crate::snapshot::seek`]: a node's entry begins after its left branch, and +/// its right branch after its entry. +/// +/// The nodes from `first` to `last` must then fill the stretch between them. +/// A subtree left out in the middle, its hash handed in as the last node's +/// left, still rebuilds to the right root; without this it is a stretch the +/// chunk appears to cover and does not carry, a hole a download finds only at +/// the end. +/// +/// Returns where the first node's entry begins and the last's ends. +fn span_of( + stubs: &[Stub], + own: &[u64], + left_of: &[Option], + right_of: &[Option], + root: usize, + first: usize, + last: usize, +) -> Result<(u64, u64), RebuildError> { + let unplaced = || RebuildError::Unplaced; + // `total_size` and `stream_size`, from what the chunk carried. + let total = |i: usize| stubs[i].children_size.checked_add(own[i]).ok_or_else(unplaced); + let entry = |i: usize| own[i].checked_add(stubs[i].embedded_total).ok_or_else(unplaced); + + let mut starts: Vec> = vec![None; stubs.len()]; + // Each node, with the offset its subtree begins at. + let mut stack = vec![(root, 0u64)]; + while let Some((i, base)) = stack.pop() { + let flags = stubs[i].flags; + // The two separate children together. + let pair = stubs[i] + .children_size + .checked_sub(stubs[i].embedded_total) + .ok_or_else(unplaced)?; + let left = if !separate_left(flags) { + 0 + } else if let Some(l) = left_of[i] { + total(l)? + } else if !separate_right(flags) { + pair + } else if let Some(r) = right_of[i] { + pair.checked_sub(total(r)?).ok_or_else(unplaced)? + } else { + // Both children left out: where one ends and the other begins is + // not known, and nothing below is carried to place. + continue; + }; + let at = base.checked_add(left).ok_or_else(unplaced)?; + starts[i] = Some(at); + if let Some(l) = left_of[i] { + stack.push((l, base)); + } + if let Some(r) = right_of[i] { + stack.push((r, at.checked_add(entry(i)?).ok_or_else(unplaced)?)); + } + } + + let start = starts[first].ok_or_else(unplaced)?; + let end = starts[last] + .ok_or_else(unplaced)? + .checked_add(entry(last)?) + .ok_or_else(unplaced)?; + let mut carried = 0u64; + for i in first..=last { + carried = carried.checked_add(entry(i)?).ok_or_else(unplaced)?; + } + if end.checked_sub(start) != Some(carried) { + return Err(unplaced()); + } + Ok((start, end)) } /// pre-root nodes, the chunk's own, then post-root, with the boundary hashes @@ -654,14 +802,29 @@ fn parse_stub( // prefix differs between the two encodings and an embedded child that // inlines a long value is a different length in each. let mut embedded = Vec::new(); + let mut embedded_total = 0u64; + let mut embedded_values = Vec::new(); for (present, is_embedded) in [(left_present, left_embedded), (right_present, right_embedded)] { if present && is_embedded { let len = read_uint24(&mut r).ok_or_else(bad)?; let child = r.take(len).ok_or_else(bad)?; - let stub = parse_stub(child, None, None, index)?; - embedded.push(consensus_message(&stub, None, None).ok_or_else(bad)?); + let mut stub = parse_stub(child, None, None, index)?; + let message = consensus_message(&stub, None, None).ok_or_else(bad)?; + // `total_size` of the child: its message, the value it keeps + // outside it, and whatever hangs below it. + let outside = if stub.has_long_value { stub.value.len() as u64 } else { 0 }; + embedded_total = (message.len() as u64) + .checked_add(outside) + .and_then(|size| size.checked_add(stub.children_size)) + .and_then(|size| size.checked_add(embedded_total)) + .ok_or_else(bad)?; + embedded.push(message); + if stub.has_long_value { + embedded_values.push(std::mem::take(&mut stub.value)); + } + embedded_values.append(&mut stub.embedded_values); } } @@ -682,6 +845,8 @@ fn parse_stub( children_size, after_path, embedded, + embedded_total, + embedded_values, value, has_long_value, }) diff --git a/crates/trie/tests/rskj_vectors.rs b/crates/trie/tests/rskj_vectors.rs index 3393e01..a6cbcd0 100644 --- a/crates/trie/tests/rskj_vectors.rs +++ b/crates/trie/tests/rskj_vectors.rs @@ -346,3 +346,443 @@ fn the_documented_size_formulas_hold() { } let _ = store.get(&[0u8; 32]); } + +/// Where a chunk's own nodes sit in rustock's traversal of the same trie: the +/// index of the first. +/// +/// rskj begins a chunk with the node holding the offset asked for -- the one +/// it falls inside, or ends at -- and the chunk's stripped nodes must follow +/// from there in order. Not found by searching for the run: stripped forms +/// leave out every hash, so the same shapes repeat across a trie. +fn locate( + chunk: &rustock_trie::snapshot_legacy::LegacyChunk, + stream: &[rustock_trie::snapshot::StreamNode], + store: &rustock_trie::MemoryTrieStore, + asked: u64, +) -> usize { + use rustock_trie::snapshot_legacy::strip; + let k = if asked == 0 { + 0 + } else { + stream + .iter() + .position(|n| n.offset < asked && asked <= n.end()) + .expect("the offset asked for is inside the trie") + }; + for (j, body) in chunk.nodes.iter().enumerate() { + assert_eq!( + strip(&stream[k + j].message, store).expect("strips"), + *body, + "node {j} of the chunk asked at {asked} is not the trie's" + ); + } + k +} + +/// **rskj's chunks say where they sit, without saying so.** +/// +/// rskj's format carries no offsets, so a client that wants to know how far +/// a chunk reached works it out from the sizes the rebuilt tree commits to. +/// Checked against rskj's own blobs: the span must be exactly where rustock's +/// traversal puts those nodes, begin at or before the offset asked for, and +/// stop short of the end of the range asked for -- where the next chunk, +/// asked for there, begins. +#[test] +fn rskj_chunks_place_themselves_where_the_trie_has_them() { + use rustock_trie::snapshot::chunk_from; + use rustock_trie::snapshot_legacy::{decode_blob, rebuild}; + + let text = include_str!("rskj-vectors/chunks.txt"); + let (mut keys, mut from, mut to) = (0usize, 0u64, 0u64); + // The previous chunk of the same trie: where its range ended, and where + // its nodes did. + let mut previous: Option<(usize, u64, u64)> = None; + let mut placed = 0; + + for line in text.lines() { + if let Some(rest) = line.strip_prefix("KEYS ") { + let f: Vec<&str> = rest.split_whitespace().collect(); + keys = f[0].parse().unwrap(); + from = f[2].parse().unwrap(); + to = f[4].parse().unwrap(); + } else if let Some(rest) = line.strip_prefix("BLOB ") { + let (root, store) = trie(keys); + let root_hash = root.compute_hash(&store); + let total = total_size(&root, &store); + + let chunk = decode_blob(&unhex(rest.trim())).expect("rskj's blob decodes"); + let rebuilt = rebuild(&chunk, root_hash) + .unwrap_or_else(|e| panic!("{keys} keys {from}..{to}: {e}")); + assert_eq!(rebuilt.total, total, "{keys} keys: the root's size"); + + let (start, end) = rebuilt + .span + .unwrap_or_else(|| panic!("{keys} keys {from}..{to}: no span")); + let stream = chunk_from(&root, 0, u64::MAX, &store); + let k = locate(&chunk, &stream, &store, from); + let last = &stream[k + chunk.nodes.len() - 1]; + assert_eq!(start, stream[k].offset, "{keys} keys {from}..{to}: where it begins"); + assert_eq!(end, last.end(), "{keys} keys {from}..{to}: where it ends"); + + assert!(start <= from, "{keys} keys {from}..{to}: begins at {start}, after {from}"); + assert!( + end < to || end == total, + "{keys} keys {from}..{to}: runs to {end}, past the range" + ); + if let Some((was, until, reached)) = previous { + if was == keys && until == from { + assert_eq!(start, reached, "{keys} keys: {from}.. does not pick up at {reached}"); + } + } + previous = Some((keys, to, end)); + placed += 1; + } + } + assert!(placed >= 6, "only {placed} chunks placed"); +} + +/// **Asked for where the last one stopped, rskj's chunks tile the trie** -- +/// each beginning at or before that point, the last ending at the trie's +/// end -- which is how rustock's client asks for them. +/// +/// Not how rskj's own client asks. It steps by its cell, trusting the chunk +/// for `k * cell` to begin where the one before ended, and rskj's iterator +/// breaks that. An offset landing in a node's embedded right child puts the +/// walk's position at that child rather than at the node, so everything +/// after it in the chunk is counted too far along and the chunk stops short +/// of its cell. The nodes in the shortfall belong to no chunk at all. It +/// happens at mainnet's 51,200-byte cells as well, so it is rskj's own snap +/// sync that loses them; asking at the cursor, nothing is lost: the next +/// answer begins with the node that ends there. +#[test] +fn asked_where_they_stopped_rskj_chunks_tile_the_trie() { + use rustock_trie::snapshot::chunk_from; + use rustock_trie::snapshot_legacy::{legacy_chunk, rebuild}; + + let (root, store) = trie(400); + let root_hash = root.compute_hash(&store); + let root_bytes: [u8; 32] = root_hash.into(); + let total = total_size(&root, &store); + let stream = chunk_from(&root, 0, u64::MAX, &store); + + let mut stepping_by_the_cell_loses = false; + for cell in [300u64, 777, 1024, 5000, total] { + let mut reached = 0u64; + let mut asked = 0u64; + let mut requests = 0; + while reached < total { + requests += 1; + assert!(requests < 10_000, "cell {cell}: no progress from {reached}"); + let chunk = legacy_chunk(&root_bytes, asked, asked + cell, &store).expect("builds"); + let rebuilt = rebuild(&chunk, root_hash) + .unwrap_or_else(|e| panic!("cell {cell} at {asked}: {e}")); + let (start, end) = rebuilt + .span + .unwrap_or_else(|| panic!("cell {cell} at {asked}: nothing of its own")); + assert!(start <= asked, "cell {cell} at {asked}: begins at {start}, after the offset"); + let k = locate(&chunk, &stream, &store, asked); + assert_eq!(start, stream[k].offset, "cell {cell} at {asked}"); + assert_eq!(end, stream[k + chunk.nodes.len() - 1].end(), "cell {cell} at {asked}"); + assert!(end > reached, "cell {cell} at {asked}: no progress"); + reached = end; + asked = reached; + } + assert_eq!(reached, total, "cell {cell}: ran past the end"); + + // rskj's own stepping, for contrast: does every node land in a chunk? + let mut covered = 0u64; + let mut from = 0u64; + while from < total { + let chunk = legacy_chunk(&root_bytes, from, from + cell, &store).expect("builds"); + if let Some((start, end)) = rebuild(&chunk, root_hash).expect("rebuilds").span { + stepping_by_the_cell_loses |= start > covered; + covered = covered.max(end); + } + from += cell; + } + } + assert!( + stepping_by_the_cell_loses, + "rskj's stepping lost nothing here, so this trie no longer shows why the client asks at its cursor" + ); +} + +/// **A chunk that leaves out a stretch is refused**, even though it rebuilds +/// to the right root. +/// +/// rskj sends both hashes of a chunk's last node, so a server can drop the +/// whole left subtree of that node and the rebuild still reaches the root: the +/// hash stands in for the subtree. What it cannot keep is the arithmetic -- +/// the nodes it did send no longer fill the stretch from the first to the +/// last -- and a client advancing by that stretch would mark the missing +/// subtree done. +#[test] +fn a_chunk_that_leaves_out_a_stretch_is_refused() { + use rustock_trie::snapshot::chunk_from; + use rustock_trie::snapshot_legacy::{legacy_chunk, rebuild, RebuildError}; + use rustock_trie::NodeRef; + + let (root, store) = trie(400); + let root_hash = root.compute_hash(&store); + let root_bytes: [u8; 32] = root_hash.into(); + let total = total_size(&root, &store); + let stream = chunk_from(&root, 0, u64::MAX, &store); + + let (mut tampered_with, mut placement_caught) = (0, 0); + for cell in [300u64, 700, 1500, 4000] { + let mut from = 0u64; + while from < total { + let asked = from; + let chunk = legacy_chunk(&root_bytes, asked, asked + cell, &store).expect("builds"); + from += cell; + let n = chunk.nodes.len(); + if n < 2 { + continue; + } + let k = locate(&chunk, &stream, &store, asked); + let last = &stream[k + n - 1]; + let node = rustock_trie::TrieNode::from_message(&last.message, &store); + let NodeRef::Hash(left) = node.left.clone() else { continue }; + let left_size = total_size(&NodeRef::Hash(left).resolve(&store).expect("held"), &store); + // The last node's left subtree, wholly inside the chunk and not + // the whole of what precedes the last node. + let begins = last.offset - left_size; + if stream[k].offset >= begins { + continue; + } + assert_eq!( + chunk.last.and_then(|(l, _)| l), + Some(left.0), + "rskj names the last node's left hash" + ); + + let mut tampered = chunk.clone(); + tampered.nodes = chunk + .nodes + .iter() + .zip(&stream[k..k + n]) + .filter(|(_, s)| s.offset < begins || s.offset >= last.offset) + .map(|(body, _)| body.clone()) + .collect(); + tampered_with += 1; + match rebuild(&tampered, root_hash) { + Err(RebuildError::Unplaced) => placement_caught += 1, + Err(_) => {} + Ok(r) => panic!( + "a chunk without the {left_size} bytes at {begins} was taken, spanning {:?}", + r.span + ), + } + } + } + assert!(tampered_with > 0, "no chunk had a subtree to leave out"); + assert!( + placement_caught > 0, + "the root caught every tampering, so the placement check was never reached" + ); +} + +/// **A node hung where its parent has no child is refused.** The rebuild +/// derives the tree from `children_size`, so a node appended after the +/// chunk's last becomes its right child when it is no larger -- and when the +/// last node's right child is embedded or absent, its message has no slot for +/// one, so nothing ties the new node to the root, which still matches. Kept, +/// it would be stored, and placed: it would stretch the chunk past where it +/// ends. +#[test] +fn a_node_hung_where_its_parent_has_none_is_refused() { + use rustock_trie::snapshot::chunk_from; + use rustock_trie::snapshot_legacy::{legacy_chunk, rebuild, strip, RebuildError}; + use rustock_trie::{NodeRef, TrieNode}; + + let (root, store) = trie(400); + let root_hash = root.compute_hash(&store); + let root_bytes: [u8; 32] = root_hash.into(); + let total = total_size(&root, &store); + let stream = chunk_from(&root, 0, u64::MAX, &store); + // The smallest node there is, so that it hangs under whatever precedes it. + let (hung, hung_size) = stream + .iter() + .map(|s| (s, TrieNode::from_message(&s.message, &store).children_size)) + .min_by_key(|(_, size)| *size) + .map(|(s, size)| (strip(&s.message, &store).expect("strips"), size)) + .expect("a trie has nodes"); + + let (mut tampered_with, mut dangling) = (0, 0); + for cell in [500u64, 1300] { + let mut from = 0u64; + while from < total { + let asked = from; + let chunk = legacy_chunk(&root_bytes, asked, asked + cell, &store).expect("builds"); + from += cell; + let n = chunk.nodes.len(); + if n == 0 || chunk.post_root.is_empty() { + continue; + } + let k = locate(&chunk, &stream, &store, asked); + let last = TrieNode::from_message(&stream[k + n - 1].message, &store); + if matches!(last.right, NodeRef::Hash(_)) || last.children_size < hung_size { + continue; + } + let mut tampered = chunk.clone(); + tampered.nodes.push(hung.clone()); + tampered_with += 1; + match rebuild(&tampered, root_hash) { + Err(RebuildError::Dangling { .. }) => dangling += 1, + Err(_) => {} + Ok(r) => panic!("a node hung where there is no child was taken, spanning {:?}", r.span), + } + } + } + assert!(tampered_with > 0, "no chunk ended in a node without a separate right child"); + assert!(dangling > 0, "the root caught every tampering, so the link check was never reached"); +} + +/// A trie in the shape Rootstock's state has: accounts, and under a contract's +/// account its storage to the left (`0x00`) and its code to the right (`0x80`). +/// +/// The shape matters because of what embeds. A contract's code is a long +/// value whose leaf -- a one-byte path, the value's hash and length -- is small +/// enough to sit inside the account node, while two or more storage slots make +/// a subtree that cannot. So a contract's account node has a separate left and +/// an embedded right, the shape a trie of same-length keys never makes. +fn unitrie(accounts: usize) -> (TrieNode, MemoryTrieStore) { + use alloy_primitives::{Address, B256}; + use rustock_trie::{account_key, code_key, storage_key}; + + let store = MemoryTrieStore::new(); + let mut root = TrieNode::empty(); + let put = |root: TrieNode, key: Vec, value: Vec| -> TrieNode { + root.put(&TrieKeySlice::from_key(&key), &value, &store) + }; + for i in 0..accounts { + let address = Address::from_word(B256::from(alloy_primitives::U256::from(i as u64 + 1))); + // An account state: nonce and balance, well under 32 bytes. + root = put(root, account_key(&address), vec![0xc4, (i as u8).wrapping_add(1), 0x82, 0x03, 0xe8]); + if i % 3 == 0 { + continue; + } + let code: Vec = (0..(40 + i % 90) as u8).map(|b| b.wrapping_mul(i as u8 | 1)).collect(); + root = put(root, code_key(&address), code); + for slot in 0..(i % 6) as u64 { + let value = vec![(slot as u8).wrapping_add(i as u8) | 1; 1 + (slot as usize * 7) % 32]; + root = put(root, storage_key(&address, &B256::from(alloy_primitives::U256::from(slot))), value); + } + } + root.save(&store, true); + (root, store) +} + +/// **Any node can begin a chunk, and the chunk is placed where that node +/// sits.** A chunk's first node never brings its left subtree -- the chunk +/// names only its hash -- so where the node begins is arithmetic on +/// `children_size`, and each shape of node takes its own branch of it: a +/// separate left beside a carried right, beside an embedded right, beside +/// none. Every node of a trie is made a first node in turn. +#[test] +fn every_node_can_begin_a_chunk_where_it_sits() { + use rustock_trie::snapshot::chunk_from; + use rustock_trie::snapshot_legacy::{legacy_chunk, rebuild}; + use rustock_trie::{NodeRef, TrieNode}; + + let kind = |child: &NodeRef| match child { + NodeRef::Empty => "none", + NodeRef::Hash(_) => "separate", + NodeRef::Node(_) => "embedded", + }; + let (root, store) = unitrie(300); + let root_hash = root.compute_hash(&store); + let root_bytes: [u8; 32] = root_hash.into(); + let stream = chunk_from(&root, 0, u64::MAX, &store); + + let mut shapes = std::collections::BTreeSet::new(); + for (i, entry) in stream.iter().enumerate() { + // rskj begins a chunk with the node that ends at the offset asked. + let asked = entry.end(); + let chunk = legacy_chunk(&root_bytes, asked, asked + 2_000, &store).expect("builds"); + let rebuilt = rebuild(&chunk, root_hash).unwrap_or_else(|e| panic!("node {i}: {e}")); + let (start, end) = rebuilt.span.unwrap_or_else(|| panic!("node {i}: nothing of its own")); + let k = locate(&chunk, &stream, &store, asked); + assert_eq!(k, i, "the chunk asked at the end of node {i} begins elsewhere"); + assert_eq!(start, entry.offset, "node {i} is placed at {start}, not {}", entry.offset); + assert_eq!(end, stream[k + chunk.nodes.len() - 1].end(), "node {i}: where the chunk ends"); + let node = TrieNode::from_message(&entry.message, &store); + shapes.insert((kind(&node.left), kind(&node.right))); + } + for shape in [("separate", "separate"), ("separate", "embedded"), ("embedded", "embedded")] { + assert!(shapes.contains(&shape), "no first node shaped {shape:?} among {shapes:?}"); + } +} + +/// The long values a rebuilt chunk's nodes name -- their own, and their +/// embedded children's -- and how many of those were embedded, if each is +/// among the values the rebuild kept. Panics on one that is not. +fn long_values_named(rebuilt: &rustock_trie::snapshot_legacy::Rebuilt, what: &str) -> (usize, usize) { + use rustock_trie::{MemoryTrieStore, NodeRef, TrieNode}; + let kept: std::collections::HashSet<[u8; 32]> = + rebuilt.long_values.iter().map(|v| alloy_primitives::keccak256(v).0).collect(); + let nothing = MemoryTrieStore::new(); + let (mut named, mut embedded) = (0, 0); + for (_, message) in &rebuilt.nodes { + let node = TrieNode::try_from_message(message, ¬hing).expect("a rebuilt node parses"); + let children = [&node.left, &node.right].into_iter().filter_map(|c| match c { + NodeRef::Node(n) => Some(n.as_ref()), + _ => None, + }); + for (n, inside) in std::iter::once((&node, false)).chain(children.map(|n| (n, true))) { + if n.has_long_value() { + let hash = n.value_hash.expect("a long value is named by its hash"); + assert!(kept.contains(&hash.0), "{what}: a long value named and not kept"); + named += 1; + embedded += usize::from(inside); + } + } + } + (named, embedded) +} + +/// **Every long value a chunk names comes with it**, an embedded child's +/// included. +/// +/// rskj's format inlines a long value in the stripped node that names it, +/// and an embedded child is stripped inside its parent. A rebuild that kept +/// only each node's own value left an embedded child's value behind -- and +/// on Rootstock that is a contract's code: a long value whose leaf, a one-byte +/// path with the value's hash and length, embeds in the account node. +#[test] +fn every_long_value_a_chunk_names_comes_with_it() { + use rustock_trie::snapshot_legacy::{decode_blob, legacy_chunk, rebuild}; + + // rskj's own blobs. + let text = include_str!("rskj-vectors/chunks.txt"); + let mut keys = 0usize; + for line in text.lines() { + if let Some(rest) = line.strip_prefix("KEYS ") { + keys = rest.split_whitespace().next().unwrap().parse().unwrap(); + } else if let Some(rest) = line.strip_prefix("BLOB ") { + let (root, store) = trie(keys); + let chunk = decode_blob(&unhex(rest.trim())).expect("decodes"); + let rebuilt = rebuild(&chunk, root.compute_hash(&store)).expect("rebuilds"); + long_values_named(&rebuilt, &format!("rskj's blob of {keys} keys")); + } + } + + // And a trie wide enough to embed plenty, cut every way. + let (root, store) = trie(400); + let root_hash = root.compute_hash(&store); + let root_bytes: [u8; 32] = root_hash.into(); + let total = total_size(&root, &store); + let (mut named, mut embedded) = (0, 0); + for cell in [300u64, 1024, total] { + let mut from = 0u64; + while from < total { + let chunk = legacy_chunk(&root_bytes, from, from + cell, &store).expect("builds"); + let rebuilt = rebuild(&chunk, root_hash).expect("rebuilds"); + let (n, e) = long_values_named(&rebuilt, &format!("cell {cell} at {from}")); + named += n; + embedded += e; + from += cell; + } + } + assert!(embedded > 0, "no embedded long value was exercised ({named} named in all)"); +} diff --git a/docs/rskj-to-rustock-map.md b/docs/rskj-to-rustock-map.md index e8c3f53..942113e 100644 --- a/docs/rskj-to-rustock-map.md +++ b/docs/rskj-to-rustock-map.md @@ -136,8 +136,9 @@ rustock's sync has no rskj counterpart for three of its files — see rustock serves rskj clients in rskj's own chunk format, verified byte-for-byte against vectors generated by running rskj's code (`crates/trie/tests/rskj-vectors/`). -The reverse -- a rustock client reading rskj's chunks -- needs a bottom-up -rebuild and is tracked as **issue #136**. +The reverse works too: a rustock client rebuilds each rskj chunk bottom-up, +checks it at the state root, and places it in the trie from the sizes the +rebuilt nodes commit to (`docs/snapshot-sync.md`, "Reading rskj's chunks"). Snapshot sync is the same six p2p commands and the same trust model, with three differences documented in `docs/snapshot-sync.md`: chunks are verified diff --git a/docs/snapshot-sync.md b/docs/snapshot-sync.md index 24fb9d5..fe433a7 100644 --- a/docs/snapshot-sync.md +++ b/docs/snapshot-sync.md @@ -588,10 +588,21 @@ Given the state root `R` the client trusts: in from the child the tree gave it, or from the stub hash the chunk supplied for a subtree it did not carry; embedded children re-prefixed with a one-byte length; a long value replaced by its keccak and a three-byte - length, the bytes kept aside to store separately. Its hash is the keccak of - that message. - -5. **Compare the tree root's hash with `R`.** Everything stands or falls here. + length, the bytes kept aside to store separately -- an embedded child's + as well as the node's own. Its hash is the keccak of that message. A child + the tree hangs where the node's flags have none is refused: it would not + enter the message, so nothing would tie it to the root. + +5. **Compare the tree root's hash with `R`.** The nodes and their shape stand + or fall here. + +6. **Place the chunk.** The rebuilt tree is the top of the real trie, and a + subtree it left out keeps its size: a parent's `children_size` commits to + both children together, so beside a carried child the other is the + difference. Descending from the root places every carried node at its + offset, by the arithmetic of the seek. The chunk's own nodes must then fill + the stretch from the first's start to the last's end -- their sizes, added + up, must equal it -- or the chunk is refused. ### Why the Cartesian tree is the right tree @@ -612,15 +623,47 @@ what makes the Cartesian tree well defined. Had it been merely non-strict, ties would leave the shape ambiguous, and an ambiguous rebuild is a *wrong* answer rather than a slow one. +### Where a chunk stops + +rskj's format states no offsets, so for a while the client advanced by the +cell it had asked for. An rskj server does not serve that cell: it serves +`from` to `from + 50 * 1024`, a constant on its side, whatever the request +says. On rustock's 100,000-byte grid the last 48,800 bytes of every cell were +marked done and never fetched. On mainnet at #9,381,160 that was 45.7% of the +state, found only by the completeness check after an hour's download, on five +attempts running; `crates/cli/examples/snap_holes.rs` maps such holes, and put +every missing byte past the first 51,200 of its cell. + +So the client advances by step 6's span, and asks next for exactly where the +chunk stopped. rskj answers any offset, starting with the node that ends +there, so the next chunk picks up from that point whatever size the server +serves. A chunk that begins after the offset asked for is not an answer to it +and is charged; one with no nodes of its own is about the offset and is not. + +Asking where the last chunk stopped also avoids a fault in rskj's own client, +which steps by its cell and trusts the chunk for `k * cell` to begin where the +one before ended. rskj's iterator breaks that: an offset that lands in a +node's embedded right child puts the walk's position at the child rather than +the node, everything after it in that chunk is counted too far along, and the +chunk stops short of its cell. The nodes in the shortfall belong to no chunk at +all. `asked_where_they_stopped_rskj_chunks_tile_the_trie` in +`crates/trie/tests/rskj_vectors.rs` shows both: rskj's stepping losing nodes, +and asking at the cursor losing none. + +The root also commits to the trie's size, so a rebuilt chunk settles the total +as a proved one does, and a peer's hint is never the last word on where the +download ends. + ### What this gives up Completeness is established *at the end* rather than by construction. If the rebuilt root matches, the nodes and their arrangement are the ones the trie commits to, since any other set or shape would have to collide with the root -hash. Sound — but the property arrives as a conclusion rather than falling out -of a comparison, and a conclusion is easier to get subtly wrong. It is why -rustock asks for its own format whenever it can, and why this path exists only -for peers that cannot speak it. +hash; that the chunk left nothing out between its first node and its last +comes from step 6. Sound -- but the property arrives as a conclusion rather +than falling out of a comparison, and a conclusion is easier to get subtly +wrong. It is why rustock asks for its own format whenever it can, and why this +path exists only for peers that cannot speak it. ## Differences from rskj From 9e625069f940e6432ccad21f6141c3ec6120e2b2 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Fri, 6 Nov 2026 06:23:11 -0800 Subject: [PATCH 008/264] sync: never record execution that did not happen A light node on mainnet whose snapshot sync had given up after five attempts held genesis, headers from #9,372,260 up, and no executed head. Three minutes later the watchdog's first escalation retreated the head from #9,388,800 to #9,388,799, and the retreat recorded #9,388,799 as executed: a block nothing had executed, with no state under it. From then on I7 fired every half minute, and a restart could not help, because snapshot sync starts only on a node that has executed nothing; it declined, saying ordinary sync was the shorter path. Only a wiped data folder got the node going again. With no executed head recorded, cursor() reads execution as the head, with a zero state root. stage_execution_after_reorg took that stand-in at its word, as above any retreat, and "rolled it back" to the retreat's target. It now does nothing when nothing has executed, and never moves a recorded executed head to a height at or above its own, which can only mean the executed block was off the chain before the transition began; that repair is the sync layer's (I5), which can see what state is held. The sync service rolled back from the same stand-in in two places: after the watchdog's retreat, and on I5, which with nothing executed is the head off the chain. Rolling back from it walks down to the first block whose state root the trie store holds and records it as executed; after a snapshot sync that failed its check, that is the checkpoint, whose state is the one the check refused. The retreat now rolls back only recorded execution, and I5 with nothing executed gets I8's repair, which re-seats the head. A test for each, and each guard taken out fails one. --- crates/storage/src/position.rs | 121 +++++++++++++++++++++++++++++ crates/sync/src/service.rs | 32 +++++++- crates/sync/src/tests.rs | 135 +++++++++++++++++++++++++++++++++ 3 files changed, 284 insertions(+), 4 deletions(-) diff --git a/crates/storage/src/position.rs b/crates/storage/src/position.rs index 794da8b..3934f70 100644 --- a/crates/storage/src/position.rs +++ b/crates/storage/src/position.rs @@ -252,6 +252,12 @@ impl BlockStore { /// /// `None` when the node has no head yet -- an empty database, before /// genesis. There is nothing to be incoherent about. + /// + /// With no executed head recorded -- a node that has executed nothing, as + /// one has until its snapshot sync finishes -- `executed` reads as the + /// head and `state_root` as zero. That keeps the relations between the + /// three keys checkable, but it is a stand-in, not a block whose state was + /// computed: code that moves execution reads [`BlockStore::exec_head`]. pub fn cursor(&self) -> Result> { let Some(head_hash) = self.head()? else { return Ok(None); @@ -368,6 +374,10 @@ impl BlockStore { /// conservative: a block common to both branches. Re-executing a few /// blocks costs seconds; executing from the wrong branch costs a fork. /// + /// It only ever goes down, and only from execution that happened. Moving + /// the executed head anywhere else records as computed a state that was + /// not, and the node cannot resume from it. + /// /// The state root comes from the target header, which post-RSKIP126 is the /// committed unitrie root -- the same source the reorg path has always /// used. @@ -378,6 +388,18 @@ impl BlockStore { new_head: &Validated, ) -> Result<()> { let Some(before) = before else { return Ok(()) }; + // Nothing executed is nothing to bring back. Without a recorded + // executed head, `cursor` reads execution as the head, and taken at + // its word that stand-in is above any retreat -- so the rollback below + // recorded the retreat's target as executed. Mainnet, 6 November 2026: + // a light node whose snapshot sync had given up retreated from + // #9,388,800 to #9,388,799 on the watchdog's first escalation and was + // left "executed" at #9,388,799, with no state under it. A restart + // could not clear that either, because snapshot sync starts only on a + // node that has executed nothing. + if self.exec_head()?.is_none() { + return Ok(()); + } let executed = before.executed; // What will the canonical block at the executed height be once this @@ -404,6 +426,15 @@ impl BlockStore { None => new_head.number(), }; + // A target at or above the executed height means this transition + // rewrites nothing at or below it: the executed block was off the + // chain before it began. Moving execution to the canonical block up + // there would claim a state never computed. That repair belongs to the + // sync layer, which can see what state is held (I5). + if target_number >= executed.number { + return Ok(()); + } + // Resolve the target on the chain as it will be after this batch. let target_hash = new_head .lineage @@ -786,4 +817,94 @@ mod tests { assert_eq!(store.canonical_hash(c.validated_head.number + 1).unwrap(), None); } } + + // -- Execution that never happened ------------------------------------ + + /// A coherent chain 0..=len and no executed head: what a node holds before + /// it has executed anything, as a light node does until its snapshot sync + /// finishes. + fn chain_never_executed(len: u64) -> (BlockStore, tempfile::TempDir, Vec
) { + let dir = tempdir().unwrap(); + let store = BlockStore::open(dir.path()).unwrap(); + let mut hs = Vec::new(); + let mut parent = B256::ZERO; + for n in 0..=len { + let h = header(n, parent, 0); + store.update_head(&h, U256::from(n + 1)).unwrap(); + parent = h.hash(); + hs.push(h); + } + assert_eq!(store.exec_head().unwrap(), None, "precondition: nothing executed"); + (store, dir, hs) + } + + /// A retreat on a node that has executed nothing leaves it having executed + /// nothing. It used to record the retreat's target as executed: on + /// mainnet, a block with no state under it, which even a restart could not + /// undo. + #[test] + fn retreating_a_node_that_executed_nothing_records_no_execution() { + let (store, _d, hs) = chain_never_executed(10); + let v = Validated::prove(&store, hs[9].hash()).unwrap(); + let cursor = store.apply(&Transition::Retreat { to: v }).unwrap(); + + assert_eq!(cursor.validated_head.number, 9, "the retreat itself happens"); + assert_eq!( + store.exec_head().unwrap(), + None, + "a retreat recorded as executed a block nothing executed" + ); + } + + /// The same for a reorg: adopting a fork records no execution, at the fork + /// point or anywhere else. + #[test] + fn adopting_a_fork_on_a_node_that_executed_nothing_records_no_execution() { + let (store, _d, hs) = chain_never_executed(10); + // A fork from #4: 5', 6', 7'. + let a = header(5, hs[4].hash(), 99); + let b = header(6, a.hash(), 99); + let c = header(7, b.hash(), 99); + for h in [&a, &b, &c] { + stash(&store, h); + } + let v = Validated::prove(&store, c.hash()).unwrap(); + assert_eq!(v.lineage()[0].0, 5, "precondition: the fork rewrites from #5"); + + let cursor = store.apply(&Transition::Adopt { head: v }).unwrap(); + + assert_eq!(cursor.validated_head.hash, c.hash(), "the fork is adopted"); + assert_eq!( + store.exec_head().unwrap(), + None, + "adopting a fork recorded execution the node never did" + ); + } + + /// Execution only comes down. When the executed block was off the chain + /// before a transition that rewrites only above it, moving execution to the + /// canonical block up there would claim a state never computed. It stays + /// where it is, for the sync layer's repair (I5), which can see what state + /// is held. + #[test] + fn execution_already_off_the_chain_is_not_moved_up() { + let (store, _d, hs) = chain(10); + // Executed at a sibling of #6, which is not canonical. + let sibling = header(6, hs[5].hash(), 99); + stash(&store, &sibling); + let root = B256::repeat_byte(0x66); + store.set_exec_head(sibling.hash(), root).unwrap(); + + // One block on top of the chain: the transition rewrites #11 alone. + let next = header(11, hs[10].hash(), 0); + stash(&store, &next); + let v = Validated::prove(&store, next.hash()).unwrap(); + store.apply(&Transition::Adopt { head: v }).unwrap(); + + assert_eq!( + store.exec_head().unwrap(), + Some((sibling.hash(), root)), + "execution was moved off the block it ran at, to one it never ran" + ); + } } diff --git a/crates/sync/src/service.rs b/crates/sync/src/service.rs index 7596d55..fc4a504 100644 --- a/crates/sync/src/service.rs +++ b/crates/sync/src/service.rs @@ -2259,6 +2259,15 @@ impl SyncService { ); self.repair_canonical_gap(cursor.validated_head.number, at.saturating_sub(1)); } + // With nothing executed, the executed head the check read + // is the head standing in for it, so what is off the chain + // is the head: I8, and I8's repair. Rolling back from the + // stand-in would walk down to whatever state the store + // holds -- after a snapshot sync that failed its check, a + // state that was never verified. + Violation::ExecutedOffChain { .. } if self.recorded_execution().is_none() => { + self.reverify_head_lineage(0); + } Violation::ExecutedOffChain { at, hash, canonical } => { if self.violation_held_for(&v, EXECUTION_OFF_CHAIN_GRACE) { warn!( @@ -2574,6 +2583,20 @@ impl SyncService { } } + /// The executed head, if this node has executed anything. + /// + /// Not `cursor().executed`, which reads as the head when nothing is + /// recorded. Rolling back from that stand-in walks the ancestry of a block + /// nobody executed, and on a node with no state but genesis -- a light + /// node whose snapshot sync gave up -- it reports on every escalation that + /// the node needs a resync, about execution that never happened. + fn recorded_execution(&self) -> Option { + let store = &self.manager.store; + let (hash, _) = store.exec_head().ok().flatten()?; + let header = store.header(hash).ok().flatten()?; + Some(rustock_storage::BlockRef::new(header.number, hash)) + } + /// Move execution to the highest block on the executed head's own ancestry /// that is canonical and whose state we still hold. /// @@ -2956,13 +2979,14 @@ impl SyncService { ); self.last_body_height = self.last_body_height.min(dest.number); // Execution follows the head, exactly as it does in - // orphan recovery. Reading the cursor again rather + // orphan recovery. Reading the store again rather // than reusing `cursor.executed` matters: the retreat // may already have moved execution, and rolling back // from a stale reference would walk the ancestry of a - // block that is no longer the executed head. - if let Ok(Some(after)) = self.manager.store.cursor() { - self.roll_execution_back(after.executed); + // block that is no longer the executed head. And only + // execution that happened is rolled back. + if let Some(executed) = self.recorded_execution() { + self.roll_execution_back(executed); } } Err(e) => warn!(target: "rustock::sync", "Head re-verification: retreat failed: {:?}", e), diff --git a/crates/sync/src/tests.rs b/crates/sync/src/tests.rs index b23f2c0..f89ac66 100644 --- a/crates/sync/src/tests.rs +++ b/crates/sync/src/tests.rs @@ -5357,6 +5357,141 @@ async fn the_watchdog_retreat_stops_at_genesis() { assert_eq!(crate::invariant::check(&store, None, crate::Scope::Full), Ok(())); } +/// A failed snapshot sync stays recoverable through the watchdog's retreat. +/// +/// Mainnet, 6 November 2026. A light node's snapshot sync gave up after five +/// attempts, leaving genesis, headers from #9,372,260 up that link to nothing +/// below them, and no executed head. The watchdog's first escalation retreated +/// the head one block, and the retreat recorded #9,388,799 as executed: a +/// block nothing had executed, with no state under it. From then on every +/// check found execution stranded, and a restart could not help, because +/// snapshot sync starts only on a node that has executed nothing; it declined, +/// saying ordinary sync was the shorter path. Only a wiped data folder got the +/// node going again. +#[tokio::test] +async fn a_retreat_after_a_failed_snapshot_sync_leaves_it_free_to_try_again() { + let dir = tempdir().unwrap(); + let store = Arc::new(BlockStore::open(dir.path()).unwrap()); + let genesis = dummy_header(0, B256::ZERO, U256::from(1)); + store.update_head(&genesis, U256::from(1)).unwrap(); + // The checkpoint's window: linked to itself and to nothing below, its + // state roots nowhere in the trie store. + let mut parent = B256::repeat_byte(0x9a); + for n in 1_000..=1_010u64 { + let mut h = dummy_header(n, parent, U256::from(1)); + h.state_root = B256::repeat_byte(0x5a); + store.update_head(&h, U256::from(n)).unwrap(); + parent = h.hash(); + } + assert_eq!(store.exec_head().unwrap(), None, "precondition: nothing executed"); + + let trie = Arc::new(rustock_trie::MemoryTrieStore::new()) as Arc; + let mut service = service_over(store.clone()).with_trie_store_for_test(trie); + + // The watchdog's first escalation. + service.reverify_head_lineage(1); + + let head = store.header(store.head().unwrap().unwrap()).unwrap().unwrap(); + assert_eq!(head.number, 1_009, "precondition: the retreat happened"); + assert_eq!( + store.exec_head().unwrap(), + None, + "the retreat recorded as executed a block nothing executed" + ); + + // And the node can still do the one thing that gets it going: try the + // snapshot again. + service.start_snap_sync(crate::snap::SnapConfig::default(), Arc::new(HeaderVerifier::new())); + assert!( + service.snap_phase().is_some(), + "snapshot sync declined to start on a node that has executed nothing" + ); +} + +/// What a node holds when its snapshot sync failed the final check: a linked +/// chain, nothing executed, and in the trie store the root of the checkpoint's +/// state -- downloaded, never verified, perhaps full of holes. No other block's +/// state is there. +fn chain_with_an_unverified_state( + len: u64, + checkpoint: u64, +) -> (Arc, tempfile::TempDir, Vec
, Arc) { + let dir = tempdir().unwrap(); + let store = Arc::new(BlockStore::open(dir.path()).unwrap()); + let unverified = B256::repeat_byte(0x77); + let mut headers = Vec::new(); + let mut parent = B256::ZERO; + for n in 0..=len { + let mut h = dummy_header(n, parent, U256::from(1)); + h.state_root = if n == checkpoint { unverified } else { B256::repeat_byte(0x5a) }; + store.update_head(&h, U256::from(n + 1)).unwrap(); + parent = h.hash(); + headers.push(h); + } + let trie = Arc::new(rustock_trie::MemoryTrieStore::new()) as Arc; + let root = rustock_trie::TrieNode::empty(); + trie.put(unverified.as_slice(), &root.to_message(trie.as_ref())); + assert_eq!(store.exec_head().unwrap(), None, "precondition: nothing executed"); + (store, dir, headers, trie) +} + +/// A retreat on a node that has executed nothing rolls nothing back. +/// +/// Rolling back from the head that stands in for execution walks down to the +/// first block whose state root the trie store holds, and records it as +/// executed. After a snapshot sync that failed its check, that block is the +/// checkpoint, and its state is the one the check refused. +#[tokio::test] +async fn a_retreat_does_not_take_up_a_state_that_failed_its_check() { + let (store, _dir, headers, trie) = chain_with_an_unverified_state(10, 5); + let mut service = service_over(store.clone()).with_trie_store_for_test(trie); + + service.reverify_head_lineage(1); + + assert_eq!(store.head().unwrap(), Some(headers[9].hash()), "precondition: the retreat happened"); + assert_eq!( + store.exec_head().unwrap(), + None, + "execution was recorded at a state nothing verified" + ); +} + +/// With nothing executed, I5 is the head off the chain, and gets the head's +/// repair rather than execution's. +/// +/// The check reads execution as the head when nothing is recorded, so a head +/// that is not canonical reads as execution off the chain. Rolling execution +/// back from there walks down to the unverified state, as above; re-seating +/// the head is what the state calls for. +#[tokio::test] +async fn with_nothing_executed_a_head_off_the_chain_is_reseated_not_rolled_back() { + let (store, _dir, headers, trie) = chain_with_an_unverified_state(10, 5); + // The head names a sibling of #10, held but not canonical. + let mut sibling = dummy_header(10, headers[9].hash(), U256::from(1)); + sibling.extra_data = vec![0xEE].into(); + store.put_header(&sibling).unwrap(); + store.set_head(sibling.hash()).unwrap(); + let violation = crate::invariant::check(&store, None, crate::Scope::Full).unwrap_err(); + assert_eq!(violation.relation(), "I5", "precondition: {violation}"); + + let mut service = service_over(store.clone()).with_trie_store_for_test(trie); + service.verify_coherence(); + age_violation(&mut service, EXECUTION_OFF_CHAIN_GRACE + Duration::from_secs(1)); + service.verify_coherence(); + + assert_eq!( + store.exec_head().unwrap(), + None, + "execution was recorded at a state nothing verified" + ); + assert_eq!( + store.head().unwrap(), + Some(headers[10].hash()), + "the head should be back on the canonical chain" + ); + assert_eq!(crate::invariant::check(&store, None, crate::Scope::Full), Ok(())); +} + // ── snapshot sync owns the peer set while it runs (#185) ───────────────────── /// **Header sync first, then state — never the reverse.** From 1900090138848f8d9170167c28aaa37b43abde1e Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Fri, 6 Nov 2026 06:24:02 -0800 Subject: [PATCH 009/264] execution: read the bridge dispatch test's own source as LF bridge_method_table_is_fully_dispatched searches include_str!("mod.rs") for text with \n line breaks, so on a checkout with CRLF endings, as Git for Windows writes by default, it found nothing and failed on a source that was fine. It was the one failure in the workspace's 2,567 tests. The source is now read as LF before the search. --- crates/execution/src/bridge/mod.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/crates/execution/src/bridge/mod.rs b/crates/execution/src/bridge/mod.rs index 93ac13a..0297ae4 100644 --- a/crates/execution/src/bridge/mod.rs +++ b/crates/execution/src/bridge/mod.rs @@ -1108,15 +1108,18 @@ mod tests { /// the failure it exists to catch. #[test] fn bridge_method_table_is_fully_dispatched() { - const SOURCE: &str = include_str!("mod.rs"); - let start = SOURCE + // Read as LF whatever the checkout wrote: on a Windows checkout with + // CRLF endings the search below found nothing, and the test failed + // on a source that was fine. + let source = include_str!("mod.rs").replace("\r\n", "\n"); + let start = source .find(" match method_name {\n // Phase 2: BTC header chain") .expect("dispatch match not found -- did its shape change?"); - let end = SOURCE[start..] + let end = source[start..] .find(" _ => unimplemented_method(method_name),") .expect("dispatch default arm not found") + start; - let dispatch = &SOURCE[start..end]; + let dispatch = &source[start..end]; /// Every method in the table is implemented as of issue #97. The list /// stays as the place to name one that is not, so that "unimplemented" From 3dd5f4a620df8cfe65c01160c0bac66d4da5db24 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Fri, 6 Nov 2026 06:30:41 -0800 Subject: [PATCH 010/264] snap: a node that has executed nothing tries again, not ordinary sync After five failed snapshot attempts a light node on mainnet handed over to ordinary sync, which had nothing to execute from: execution stood at genesis and the canonical chain began at #9,372,260. It tried three times, said "this node holds headers it has no way to execute", and stopped until someone restarted it. A node that has executed nothing past genesis now stays out of ordinary sync when its attempts are spent, and tries snapshot sync again after a wait: 30 minutes, then twice as long each time, up to a day. Each later round is one attempt, so a failure that keeps repeating costs one state download a day, and every peer is asked again: one set aside may have been set aside through no fault of its own (those five attempts failed on a bug in this client), and may be the only one serving snapshots. Pruning stays held while it waits. A node that has executed something hands over as before. The tick's guard against a node suspended with no session to wait for now counts a waiting try as one. --- crates/sync/src/service.rs | 105 +++++++++++++++++++++- crates/sync/src/snap/driver.rs | 6 ++ crates/sync/src/snap/session.rs | 6 ++ crates/sync/src/tests.rs | 149 ++++++++++++++++++++++++++++++++ docs/snapshot-sync.md | 17 ++++ 5 files changed, 280 insertions(+), 3 deletions(-) diff --git a/crates/sync/src/service.rs b/crates/sync/src/service.rs index fc4a504..37939b9 100644 --- a/crates/sync/src/service.rs +++ b/crates/sync/src/service.rs @@ -60,7 +60,20 @@ const REQUEST_TIMEOUT: Duration = Duration::from_secs(30); /// reason to abandon the feature -- but trying forever against a hostile /// network is not better. Each attempt costs a status round trip and whatever /// of the header walk it got through. -const MAX_SNAP_ATTEMPTS: u32 = 5; +/// +/// Concluding it will not work *now*, for a node that has executed nothing: +/// such a node has no other way forward, so it waits [`SNAP_RETRY_FIRST`] and +/// tries again, one attempt per wait (see `finish_snap_sync`). +pub(crate) const MAX_SNAP_ATTEMPTS: u32 = 5; + +/// The first wait before a node that has executed nothing tries snapshot sync +/// again, after its attempts are spent. Each wait doubles, to +/// [`SNAP_RETRY_MAX`]. +pub(crate) const SNAP_RETRY_FIRST: Duration = Duration::from_secs(30 * 60); + +/// The longest wait between attempts, so a failure that keeps repeating costs +/// a day one state download, not five an hour. +pub(crate) const SNAP_RETRY_MAX: Duration = Duration::from_secs(24 * 60 * 60); /// How long a coherence violation must persist before it is worth reporting. /// @@ -491,6 +504,12 @@ pub struct SyncService { snap_attempts: u32, /// Peers not worth asking, carried between sessions. snap_avoid: std::collections::HashSet, + /// When a node that has executed nothing tries snapshot sync again, its + /// attempts spent. Ordinary sync stays suspended until then: it has + /// nothing to execute from. + snap_retry_at: Option, + /// How long the next such wait will be. + snap_retry_wait: Duration, block_processor: Option>, trie_store: Option>, pub(crate) current_state_root: Option, @@ -689,6 +708,8 @@ impl SyncService { snap_restart: None, snap_attempts: 0, snap_avoid: std::collections::HashSet::new(), + snap_retry_at: None, + snap_retry_wait: SNAP_RETRY_FIRST, trie_store: None, current_state_root: None, executing: None, @@ -1202,6 +1223,32 @@ impl SyncService { self.snap_restart = None; } + /// End the running session as a failure, through the handover a real + /// failure takes. + #[cfg(test)] + pub(crate) async fn fail_snap_session_for_test( + &mut self, + why: crate::snap::session::SnapFailure, + ) { + if let Some(driver) = self.snap.as_mut() { + driver.fail_for_test(why); + } + self.finish_snap_sync().await; + } + + /// When snapshot sync is next tried, if a node is waiting to try it again, + /// and how long the wait after that will be. + #[cfg(test)] + pub(crate) fn snap_retry_for_test(&mut self) -> (&mut Option, Duration) { + (&mut self.snap_retry_at, self.snap_retry_wait) + } + + /// Peers snapshot sessions are not asking, carried between them. + #[cfg(test)] + pub(crate) fn snap_avoid_for_test(&mut self) -> &mut std::collections::HashSet { + &mut self.snap_avoid + } + /// Sessions started so far. #[cfg(test)] pub(crate) fn snap_attempts_for_test(&self) -> u32 { @@ -1346,6 +1393,32 @@ impl SyncService { } } + /// Whether this node has executed nothing past genesis: the only node + /// `start_snap_sync` starts a session for, and one ordinary sync can do + /// nothing for once a session has written its window of headers. + fn executed_nothing(&self) -> bool { + self.recorded_execution().is_none_or(|executed| executed.number == 0) + } + + /// Try snapshot sync again, after the wait `finish_snap_sync` set. + /// + /// Every peer gets another chance: one set aside may have been set aside + /// through no fault of its own, and it may be the only one that serves + /// snapshots. The round is a single attempt, so a failure that repeats + /// costs one attempt per wait rather than a round of them. + fn retry_snap_sync(&mut self) { + let (Some((config, verifier)), Some(trie_store)) = + (self.snap_restart.clone(), self.trie_store.clone()) + else { + self.resume_ordinary_sync(); + return; + }; + info!(target: "rustock::snap", "trying snapshot sync again"); + self.snap_avoid.clear(); + self.snap_attempts = MAX_SNAP_ATTEMPTS - 1; + self.open_snap_session(config, verifier, trie_store); + } + /// Hand a completed snapshot over to ordinary sync, or report the failure. async fn finish_snap_sync(&mut self) { let Some(driver) = self.snap.take() else { return }; @@ -1395,6 +1468,26 @@ impl SyncService { return; }; if self.snap_attempts >= MAX_SNAP_ATTEMPTS { + // A node that has executed nothing has no other way + // forward: ordinary sync has no state to execute from, + // only the headers the sessions wrote. Handing over left + // a mainnet light node halted for good on 6 November + // 2026 ("this node holds headers it has no way to + // execute"). So it waits, and tries again. + if self.executed_nothing() && self.trie_store.is_some() { + let wait = self.snap_retry_wait; + self.snap_retry_wait = (wait * 2).min(SNAP_RETRY_MAX); + self.snap_retry_at = Some(Instant::now() + wait); + warn!( + target: "rustock::snap", + "snapshot sync gave up after {} attempt(s): {why}. This node has \ + executed nothing, so ordinary sync has nothing to start from; \ + trying again in {} min", + self.snap_attempts, + wait.as_secs() / 60 + ); + return; + } warn!( target: "rustock::snap", "snapshot sync gave up after {} attempts: {why}", self.snap_attempts @@ -1448,6 +1541,11 @@ impl SyncService { self.refresh_status_to_peers().await; } + if self.snap.is_none() && self.snap_retry_at.is_some_and(|at| Instant::now() >= at) { + self.snap_retry_at = None; + self.retry_snap_sync(); + } + if self.snap.is_some() { let now = Instant::now(); if let Some(driver) = self.snap.as_mut() { @@ -1459,8 +1557,9 @@ impl SyncService { // Suspended with no session to wait for is a bug, and the shape of the // bug is a node that never syncs again -- the worst failure available // here, and a silent one. `finish_snap_sync` resumes on every path it - // owns; this catches a path it does not, complains, and recovers. - if self.suspended_for_snap && self.snap.is_none() { + // owns; this catches a path it does not, complains, and recovers. A + // node waiting to try snapshot sync again has a session to wait for. + if self.suspended_for_snap && self.snap.is_none() && self.snap_retry_at.is_none() { warn!( target: "rustock::snap", "ordinary sync was suspended with no snapshot session running; resuming" diff --git a/crates/sync/src/snap/driver.rs b/crates/sync/src/snap/driver.rs index f067a3a..b6997df 100644 --- a/crates/sync/src/snap/driver.rs +++ b/crates/sync/src/snap/driver.rs @@ -275,6 +275,12 @@ impl SnapDriver { &self.session } + /// End the session as a failure, for tests of what follows one. + #[cfg(test)] + pub(crate) fn fail_for_test(&mut self, why: super::session::SnapFailure) { + self.session.fail_for_test(why); + } + pub fn is_finished(&self) -> bool { matches!(self.session.phase(), Phase::Done | Phase::Failed) } diff --git a/crates/sync/src/snap/session.rs b/crates/sync/src/snap/session.rs index 08aa355..8797a44 100644 --- a/crates/sync/src/snap/session.rs +++ b/crates/sync/src/snap/session.rs @@ -311,6 +311,12 @@ impl SnapSession { Vec::new() } + /// End the session as a failure, for tests of what follows one. + #[cfg(test)] + pub(crate) fn fail_for_test(&mut self, why: SnapFailure) { + self.fail(why); + } + /// What to do now. Safe to call as often as the caller likes: it returns /// only work that is not already outstanding. /// Tell this session how many of its requests are still outstanding. diff --git a/crates/sync/src/tests.rs b/crates/sync/src/tests.rs index f89ac66..b0b0849 100644 --- a/crates/sync/src/tests.rs +++ b/crates/sync/src/tests.rs @@ -5570,6 +5570,155 @@ async fn the_ordinary_sync_resumes_when_the_snapshot_session_gives_up() { ); } +/// A node with genesis and nothing executed, a snapshot sync started on it. +fn snapshot_syncing_node() -> (SyncService, Arc, tempfile::TempDir) { + let dir = tempdir().unwrap(); + let store = Arc::new(BlockStore::open(dir.path()).unwrap()); + let genesis = dummy_header(0, B256::ZERO, U256::from(1)); + store.update_head(&genesis, U256::from(1)).unwrap(); + let mut service = make_service(store.clone()).with_trie_store_for_test(Arc::new( + rustock_trie::MemoryTrieStore::new(), + )); + service.start_snap_sync( + crate::snap::SnapConfig { client_enabled: true, ..Default::default() }, + Arc::new(HeaderVerifier::new()), + ); + assert!(service.snap_phase().is_some(), "precondition: a session is running"); + (service, store, dir) +} + +/// What failed every attempt on mainnet on 6 November 2026. +fn state_not_whole() -> crate::snap::session::SnapFailure { + crate::snap::session::SnapFailure::IncompleteState("trie node 0x751b is not in the store".into()) +} + +/// Fail sessions until no further one opens, and say how many that took. +async fn fail_until_spent(service: &mut SyncService) -> u32 { + let mut failed = 0; + while service.snap_phase().is_some() { + assert!(failed <= crate::service::MAX_SNAP_ATTEMPTS, "sessions kept opening"); + service.fail_snap_session_for_test(state_not_whole()).await; + failed += 1; + } + failed +} + +/// Make the waiting try due now. +fn make_the_next_try_due(service: &mut SyncService) { + let (at, _) = service.snap_retry_for_test(); + assert!(at.is_some(), "no try is waiting"); + *at = Some(Instant::now()); +} + +/// A node that has executed nothing waits and tries snapshot sync again when +/// its attempts are spent, rather than handing over to an ordinary sync that +/// has nothing to execute from. +/// +/// Mainnet, 6 November 2026: after five failed attempts a light node handed +/// over, ordinary sync tried three times to execute #9,372,260 on genesis, +/// said "this node holds headers it has no way to execute", and stopped for +/// good: nothing more would happen until someone restarted it. +#[tokio::test] +async fn a_node_that_executed_nothing_tries_snapshot_sync_again_rather_than_halting() { + use crate::service::{MAX_SNAP_ATTEMPTS, SNAP_RETRY_FIRST}; + use crate::snap::session::Phase; + + let (mut service, _store, _dir) = snapshot_syncing_node(); + let gate = service.prune_gate(); + + assert_eq!(fail_until_spent(&mut service).await, MAX_SNAP_ATTEMPTS); + + // Spent: no session, still standing aside, and another try set. + assert!( + service.is_suspended_for_snap_for_test(), + "handed over to an ordinary sync with nothing to execute from" + ); + assert!( + !gate.load(std::sync::atomic::Ordering::Relaxed), + "pruning was released while the node still waits on a snapshot" + ); + let (at, next_wait) = service.snap_retry_for_test(); + assert!(at.is_some(), "no further try was set"); + assert_eq!(next_wait, SNAP_RETRY_FIRST * 2, "the wait after this one doubles"); + + // A tick before it is due changes nothing. In particular the guard for a + // node suspended with no session must not take the wait for one. + service.on_tick().await; + assert_eq!(service.snap_phase(), None); + assert!(service.is_suspended_for_snap_for_test(), "the wait was taken for a lost session"); + + // Once due, a session opens, and it is a single attempt. + make_the_next_try_due(&mut service); + service.on_tick().await; + assert_eq!(service.snap_phase(), Some(Phase::AwaitingStatus), "the try that was set did not happen"); + assert_eq!(fail_until_spent(&mut service).await, 1, "a later round is one attempt"); + let (at, next_wait) = service.snap_retry_for_test(); + assert!(at.is_some(), "no try was set after the second round"); + assert_eq!(next_wait, SNAP_RETRY_FIRST * 4); + assert!(service.is_suspended_for_snap_for_test()); +} + +/// The wait between tries stops growing at a day, so a node keeps trying. +#[tokio::test] +async fn the_wait_between_snapshot_tries_stops_growing_at_a_day() { + let (mut service, _store, _dir) = snapshot_syncing_node(); + fail_until_spent(&mut service).await; + for _ in 0..12 { + make_the_next_try_due(&mut service); + service.on_tick().await; + fail_until_spent(&mut service).await; + } + let (at, next_wait) = service.snap_retry_for_test(); + assert!(at.is_some(), "the node stopped trying"); + assert_eq!(next_wait, crate::service::SNAP_RETRY_MAX); +} + +/// A new try asks every peer again: one set aside may have been set aside +/// through no fault of its own -- the attempts on mainnet failed on a bug in +/// this client -- and it may be the only one serving snapshots. +#[tokio::test] +async fn a_new_snapshot_try_asks_every_peer_again() { + let (mut service, _store, _dir) = snapshot_syncing_node(); + fail_until_spent(&mut service).await; + service.snap_avoid_for_test().insert(B512::repeat_byte(7)); + + make_the_next_try_due(&mut service); + service.on_tick().await; + + assert!(service.snap_phase().is_some(), "precondition: the try happened"); + assert!(service.snap_avoid_for_test().is_empty(), "a peer is still set aside"); +} + +/// A node that has executed something hands over to ordinary sync as before: +/// executing the blocks it is missing is its way forward. +#[tokio::test] +async fn a_node_that_has_executed_still_hands_over_to_ordinary_sync() { + let (mut service, store, _dir) = snapshot_syncing_node(); + // Execution recorded beyond genesis, as though it had happened meanwhile. + let one = dummy_header(1, store.head().unwrap().unwrap(), U256::from(1)); + store.update_head(&one, U256::from(2)).unwrap(); + store.set_exec_head(one.hash(), one.state_root).unwrap(); + + assert_eq!(fail_until_spent(&mut service).await, crate::service::MAX_SNAP_ATTEMPTS); + + assert!(!service.is_suspended_for_snap_for_test(), "the peer set was not handed back"); + assert!(service.snap_retry_for_test().0.is_none(), "a try was set for a node with a way forward"); +} + +/// Execution recorded at genesis is execution of nothing, as `start_snap_sync` +/// already reads it: such a node waits and tries again too. +#[tokio::test] +async fn a_node_executed_only_to_genesis_tries_snapshot_sync_again_too() { + let (mut service, store, _dir) = snapshot_syncing_node(); + let genesis = store.head().unwrap().unwrap(); + store.set_exec_head(genesis, B256::ZERO).unwrap(); + + fail_until_spent(&mut service).await; + + assert!(service.is_suspended_for_snap_for_test(), "handed over with nothing to execute from"); + assert!(service.snap_retry_for_test().0.is_some(), "no further try was set"); +} + // ── serving headers: the canonical fast path (#185) ────────────────────────── /// **The fast path must return exactly what the walk returns.** It is an diff --git a/docs/snapshot-sync.md b/docs/snapshot-sync.md index fe433a7..1a1e521 100644 --- a/docs/snapshot-sync.md +++ b/docs/snapshot-sync.md @@ -73,6 +73,23 @@ a free denial of service. Now the peer is charged, the session is abandoned, and another starts against a different peer — up to `MAX_SNAP_ATTEMPTS` (5), carrying forward the list of peers already found unhelpful. +### Nor can five failures + +When the attempts are spent, a node that has executed something hands over to +ordinary sync, which can execute the blocks it is missing. A node that has +executed nothing cannot: all it holds are the headers the sessions wrote, and +nothing to execute them from. On mainnet on 6 November 2026 a light node handed +over anyway, and ordinary sync tried three times to execute #9,372,260 on top +of genesis, said "this node holds headers it has no way to execute", and +stopped until someone restarted it. + +So such a node stays out of ordinary sync and tries snapshot sync again, after +30 minutes, then after twice as long each time up to a day, one attempt per +wait, and with every peer asked again: one set aside may have been set aside +through no fault of its own (those five attempts failed on a bug in this +client), and it may be the only one serving snapshots. A failure that keeps +repeating costs one state download a day. + ### The header walk runs in parallel For a node that starts with nothing, step 2 covers the whole mainnet chain: From f6fa173f90f11c64fd6fd3c9f3b4b11665cf372f Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Fri, 6 Nov 2026 08:49:26 -0800 Subject: [PATCH 011/264] sync: settle a block's total difficulty with its uncles once its body is held Header-first sync gives a header from an rsk/62 peer, which is every rskj node, only a lower bound for its total difficulty: its parent's total and its own difficulty, since the uncles travel in the body. Nothing raised the bound once the body came. In follow mode, a fork announcement brings the last few hundred headers round again with their bodies held, which hid this; a node catching up ingests each header once, so every uncle it skipped stayed out of every total above it. On mainnet the light node ran 6.9 x 10^25 short of rskj's total difficulty after the 8,179 blocks from its snapshot to the tip. total_difficulty::settle gives a held block its parent's total, its own difficulty and its uncles', oldest first: for a caught-up batch once its bodies are all in (finish_batch), and in follow mode as each block is drained. An uncle counts only once it passes rskj's whole uncle rule, proof of work included, against the ancestry the node holds, which must include the uncle lists of every ancestor the rule walks: the body check on receipt shows only that the block's miner chose those uncles. Re-ingestion now credits a held body's uncles through the same gate, where it trusted any body that matched its commitment; and a held header whose parent isn't held takes off what it adds rather than its own difficulty alone, so a settled total keeps its uncles once, not twice. BlockStore::uncles reads a body's uncle list without decoding its transactions. Seven tests, each failing without the part it covers, and ten deliberate bugs, each caught by one of them. --- crates/storage/src/lib.rs | 43 ++++ crates/sync/src/lib.rs | 1 + crates/sync/src/manager.rs | 95 ++++++--- crates/sync/src/service.rs | 12 ++ crates/sync/src/tests.rs | 293 ++++++++++++++++++++++++++++ crates/sync/src/total_difficulty.rs | 156 +++++++++++++++ 6 files changed, 578 insertions(+), 22 deletions(-) create mode 100644 crates/sync/src/total_difficulty.rs diff --git a/crates/storage/src/lib.rs b/crates/storage/src/lib.rs index 3ec847b..5402ec8 100644 --- a/crates/storage/src/lib.rs +++ b/crates/storage/src/lib.rs @@ -1361,6 +1361,31 @@ impl BlockStore { } } + /// The uncle headers of a stored block body, read without decoding its + /// transactions: all that the uncle rule and total difficulty need, for a + /// fraction of [`Self::body`]'s work on a busy block. + pub fn uncles(&self, hash: B256) -> Result>> { + let Some(bytes) = self + .db + .get_cf(self.cf(CF_BODIES)?, hash.as_slice()) + .context("Failed to read block body")? + else { + return Ok(None); + }; + let mut buf = bytes.as_slice(); + let outer = RlpHeader::decode(&mut buf)?; + let mut body = &buf[..outer.payload_length]; + let transactions = RlpHeader::decode(&mut body)?; + body = &body[transactions.payload_length..]; + let ommers_h = RlpHeader::decode(&mut body)?; + let mut ommers_buf = &body[..ommers_h.payload_length]; + let mut ommers = Vec::new(); + while !ommers_buf.is_empty() { + ommers.push(Header::decode(&mut ommers_buf)?); + } + Ok(Some(ommers)) + } + /// Returns a full block (header + body) by hash. /// If only the header is stored (no body), returns a header-only block @@ -2841,6 +2866,24 @@ mod tests { } } + #[test] + fn uncles_reads_the_uncles_of_a_body_past_its_transactions() { + let dir = tempdir().unwrap(); + let store = BlockStore::open(dir.path()).unwrap(); + let hash = B256::repeat_byte(0xCC); + assert_eq!(store.uncles(hash).unwrap(), None, "no body, no answer"); + + let txs: Vec = (0..10).map(dummy_tx).collect(); + let ommers: Vec
= (100..103).map(dummy_header).collect(); + store.put_body(hash, &txs, &ommers).unwrap(); + let (_, from_body) = store.body(hash).unwrap().unwrap(); + assert_eq!(store.uncles(hash).unwrap(), Some(from_body)); + + // A body that names none answers none, which is not the same as no body. + store.put_body(hash, &txs, &[]).unwrap(); + assert_eq!(store.uncles(hash).unwrap(), Some(Vec::new())); + } + #[test] fn test_put_body_overwrite() { let dir = tempdir().unwrap(); diff --git a/crates/sync/src/lib.rs b/crates/sync/src/lib.rs index afe7ee1..a2dcced 100644 --- a/crates/sync/src/lib.rs +++ b/crates/sync/src/lib.rs @@ -10,6 +10,7 @@ mod manager; mod progress; mod service; mod state; +mod total_difficulty; mod tracker; pub mod chain_activity; pub mod gas_price; diff --git a/crates/sync/src/manager.rs b/crates/sync/src/manager.rs index 384278f..293908d 100644 --- a/crates/sync/src/manager.rs +++ b/crates/sync/src/manager.rs @@ -167,6 +167,62 @@ impl SyncManager { true } + /// What a block adds to the chain's work from what this node holds: with + /// its uncles in hand, its own difficulty and theirs once they pass the + /// whole uncle rule; without, its own difficulty alone, the lower bound a + /// bare header gives. + fn held_contribution(&self, hash: B256, header: &Header) -> U256 { + let Some(uncles) = crate::total_difficulty::held_uncles(&self.store, hash, header) else { + return header.difficulty; + }; + match crate::total_difficulty::credited_uncles(&self.store, &self.verifier, header, &uncles) { + Ok(credited) => header.difficulty.saturating_add(credited), + Err(why) => { + debug!( + target: "rustock::sync", + "Block #{}: its uncles are not credited with their difficulty: {why}", + header.number + ); + header.difficulty + } + } + } + + /// Settle the total difficulty of each of `blocks`, oldest first, now that + /// their bodies are held. See [`crate::total_difficulty`]. + /// + /// Stops at the first it cannot settle, a block without its body or whose + /// parent has no total, because every total above it would start from a + /// bound. Returns how many it settled. + pub(crate) fn settle_total_difficulties(&self, blocks: &[(B256, Header)]) -> usize { + for (settled, (hash, header)) in blocks.iter().enumerate() { + let Ok(Some(uncles)) = self.store.uncles(*hash) else { + return settled; + }; + if !self.settle_total_difficulty(*hash, header, &uncles) { + return settled; + } + } + blocks.len() + } + + /// Settle one block's total difficulty from the uncles its body names. + /// `false` when it cannot be settled: its parent has no total, or the + /// store failed. + pub(crate) fn settle_total_difficulty(&self, hash: B256, header: &Header, uncles: &[Header]) -> bool { + match crate::total_difficulty::settle(&self.store, &self.verifier, hash, header, uncles) { + Ok(settled) => settled.is_some(), + Err(e) => { + warn!( + target: "rustock::sync", + "Could not settle the total difficulty of #{} ({:?}): {e}", + header.number, hash + ); + false + } + } + } + /// The body of [`Self::handle_headers_response`], with the peer optional so /// that paths without an attributable sender can still store headers. A /// header with no parent from such a path is dropped and charged to nobody, @@ -233,6 +289,17 @@ impl SyncManager { let already_stored = self.store.header(hash)?.is_some(); + // What this block adds to the chain's work: its own difficulty and + // its uncles', and uncle headers travel only in the body. In order + // of what can be proved: the uncles this peer sent with the header; + // then the uncles of a body this node holds, once they pass the + // whole uncle rule; otherwise -- plain header-first sync from an + // `rsk/62` peer, which is every rskj node -- the header difficulty + // alone. That is a lower bound, and `crate::total_difficulty` + // settles it once the body is in. + let proven = self.proven.lock().unwrap().get(&hash).copied(); + let contribution = proven.unwrap_or_else(|| self.held_contribution(hash, header)); + // The parent is the block named by `parent_hash`, and nothing else. // Take it from the chunk when the previous entry *is* that block, // otherwise from the store. @@ -258,13 +325,17 @@ impl SyncManager { // Held already, so it was verified when it was first // stored. Carry its committed total difficulty rather // than recomputing one from a parent of zero, which - // would rewrite a real chain's weight downward. + // would rewrite a real chain's weight downward. What + // the block adds is taken off here and added back + // below, so the total stays as stored; taking off its + // own difficulty alone would count its uncles twice + // once its total holds them. parent_ref = None; parent_td = self .store .total_difficulty(hash)? .unwrap_or_default() - .saturating_sub(header.difficulty); + .saturating_sub(contribution); } else { // The named parent is not held, so there is nothing to // verify this header against. Storing it anyway is how @@ -294,26 +365,6 @@ impl SyncManager { } } - // Total difficulty counts the uncle difficulties too, and those - // live only in the body -- a header records `uncle_count` and - // nothing more. During header-first sync the body has usually not - // arrived, so this is the header difficulty alone: a lower bound, - // corrected once the body lands. A block that declares no uncles - // needs no body, and `cumulative_difficulty` says so without a read. - // Total difficulty counts uncle difficulty too, and uncle headers - // live only in the body. In order of what can actually be proved: - // the uncles this peer sent with the header, then a body this node - // already holds, then -- during plain header-first sync, where - // neither exists -- the header difficulty alone, which is a lower - // bound and the reason `rsk/63` carries the uncles at all. - let contribution = self - .proven - .lock() - .unwrap() - .get(&hash) - .copied() - .or_else(|| self.store.cumulative_difficulty(hash, header).ok().flatten()) - .unwrap_or(header.difficulty); let new_td = parent_td + contribution; // For NEW headers with a known parent, run full verification. diff --git a/crates/sync/src/service.rs b/crates/sync/src/service.rs index 37939b9..ba5f0b2 100644 --- a/crates/sync/src/service.rs +++ b/crates/sync/src/service.rs @@ -4271,6 +4271,13 @@ impl SyncService { /// blocking thread (freeing the event loop to download N+1) or — if a /// previous batch is still executing — park this batch until it completes. pub(crate) async fn finish_batch(&mut self, pending: &[(B256, Header)], peer_best: u64) { + // The batch's bodies are all held, so the totals that header-first + // sync could only bound are settled now, oldest first, each with its + // block's uncles once they pass the uncle rule. Without this a node + // that caught up this way carries every uncle it skipped in every + // total above it. See `crate::total_difficulty`. + self.manager.settle_total_difficulties(pending); + // The download succeeded: advance the download cursor so the next // skeleton round queues the blocks that follow this batch. Execution // (the exec_head cursor) advances independently when the spawned job @@ -5165,6 +5172,11 @@ impl SyncService { } self.follow_gap_since = None; let (hash, header, txs, uncles) = self.follow_buffer.remove(&next_num).unwrap(); + // The body is in hand and the parent is the executed head, whose + // total is settled already, so this block's total can be settled + // too, rather than wait for a fork announcement to bring its + // header round again. See `crate::total_difficulty`. + self.manager.settle_total_difficulty(hash, &header, &uncles); if !self.process_single_block(hash, &header, txs, uncles).await { // Real divergence: head didn't advance, so looping would spin. return; diff --git a/crates/sync/src/tests.rs b/crates/sync/src/tests.rs index b0b0849..2a5327a 100644 --- a/crates/sync/src/tests.rs +++ b/crates/sync/src/tests.rs @@ -6886,6 +6886,299 @@ async fn a_committed_uncle_without_proof_of_work_proves_nothing() { ); } +// --------------------------------------------------------------------------- +// Total difficulty once the body is held. Header-first sync can only bound a +// block's total, because its uncles travel in the body; `total_difficulty` +// settles it once the body is in, crediting the uncles only once they pass +// the whole uncle rule. +// --------------------------------------------------------------------------- + +/// A header naming `uncles`: its uncle count and its uncles hash commit to +/// them, as the body check on receipt requires. +fn naming_uncles(mut header: Header, uncles: &[Header]) -> Header { + header.uncle_count = uncles.len() as u64; + header.ommers_hash = rustock_execution::processor::compute_ommers_hash(uncles); + header.cached_hash = None; + header +} + +/// Fails the header whose difficulty is 999, standing in for an uncle whose +/// merged-mining proof of work does not hold. +struct NoWorkAt999; + +impl rustock_core::validation::HeaderValidator for NoWorkAt999 { + fn validate(&self, h: &Header) -> Result<(), rustock_core::validation::ValidationError> { + if h.difficulty == U256::from(999u64) { + return Err(rustock_core::validation::ValidationError::BitcoinPowInvalid { + hash: h.hash(), + target: U256::ZERO, + }); + } + Ok(()) + } +} + +/// Genesis held whole with a total of 1; #1 on it, worth 10; and two siblings +/// of #1 for a later block to name as uncles, worth `first` and `second`. +fn genesis_and_siblings(store: &BlockStore, first: u64, second: u64) -> (Header, Vec
) { + let genesis = dummy_header(0, B256::ZERO, U256::from(1)); + store.update_head(&genesis, U256::from(1)).unwrap(); + store.put_body(genesis.hash(), &[], &[]).unwrap(); + let b1 = dummy_header(1, genesis.hash(), U256::from(10)); + let uncles = vec![ + dummy_header(1, genesis.hash(), U256::from(first)), + dummy_header(1, genesis.hash(), U256::from(second)), + ]; + (b1, uncles) +} + +/// Answer each body request the service makes until it makes no more, with no +/// transactions and the uncles `uncles_of` gives the block asked for. +async fn answer_body_requests(service: &mut SyncService, uncles_of: impl Fn(B256) -> Vec
) { + for _ in 0..16 { + let asked: Vec<(u64, B256, B512)> = match &service.state { + SyncState::DownloadingBodies { pending_headers, id_index, .. } => id_index + .iter() + .map(|(id, (i, peer))| (*id, pending_headers[*i].0, *peer)) + .collect(), + _ => return, + }; + for (id, hash, peer) in asked { + service.on_body_response(peer, id, vec![], uncles_of(hash)).await; + } + } + panic!("the service kept asking for bodies: {:?}", service.state); +} + +/// Catch up #1 to #3 header-first, from a peer that cannot send uncles, then +/// download their bodies, #2's naming `uncles`. Returns the three headers. +async fn caught_up( + store: &Arc, + verifier: HeaderVerifier, + b1: Header, + uncles: &[Header], +) -> [Header; 3] { + let b2 = naming_uncles(dummy_header(2, b1.hash(), U256::from(10)), uncles); + let b3 = dummy_header(3, b2.hash(), U256::from(10)); + let peer_store = Arc::new(rustock_networking::peers::PeerStore::new()); + let (tx, _rx) = mpsc::channel(rustock_networking::peers::PEER_CHANNEL_CAPACITY); + peer_store.add_peer(B512::repeat_byte(0x01), tx).await; + let manager = Arc::new(SyncManager::new(store.clone(), Arc::new(verifier), peer_store.clone())); + manager + .handle_headers_response(vec![b3.clone(), b2.clone(), b1.clone()], test_peer()) + .unwrap(); + assert_eq!( + store.total_difficulty(b3.hash()).unwrap(), + Some(U256::from(31)), + "each total is a bound until the bodies come" + ); + + let (_event_tx, event_rx) = mpsc::unbounded_channel(); + let mut service = SyncService::new(manager, peer_store, event_rx); + service.start_body_downloads(3).await; + let named = b2.hash(); + answer_body_requests(&mut service, |hash| { + if hash == named { uncles.to_vec() } else { Vec::new() } + }) + .await; + assert!(matches!(service.state, SyncState::Following), "{:?}", service.state); + [b1, b2, b3] +} + +/// Once a caught-up batch's bodies are in, each total is made exact, oldest +/// first, and every total above an uncle carries it, as rskj's do. A node that +/// never did this ran 6.9 x 10^25 short of rskj after 8,000 mainnet blocks. +#[tokio::test] +async fn a_caught_up_batch_counts_the_uncles_its_bodies_bring() { + let dir = tempdir().unwrap(); + let store = Arc::new(BlockStore::open(dir.path()).unwrap()); + let (b1, uncles) = genesis_and_siblings(&store, 7, 5); + let [b1, b2, b3] = caught_up(&store, HeaderVerifier::new(), b1, &uncles).await; + + // 1 + 10; then + 10 + 7 + 5; then + 10. + assert_eq!(store.total_difficulty(b1.hash()).unwrap(), Some(U256::from(11))); + assert_eq!(store.total_difficulty(b2.hash()).unwrap(), Some(U256::from(33))); + assert_eq!(store.total_difficulty(b3.hash()).unwrap(), Some(U256::from(43))); +} + +/// A body's uncles count only once they pass the whole uncle rule, proof of +/// work included. The body check on receipt shows only that the block's miner +/// chose them, and an uncle that never did its work would otherwise add what +/// it claims to every total above it. +#[tokio::test] +async fn a_caught_up_body_whose_uncle_did_no_work_adds_no_uncle() { + let dir = tempdir().unwrap(); + let store = Arc::new(BlockStore::open(dir.path()).unwrap()); + let (b1, uncles) = genesis_and_siblings(&store, 7, 999); + let verifier = HeaderVerifier::new().with_static_rule(NoWorkAt999); + let [_, b2, b3] = caught_up(&store, verifier, b1, &uncles).await; + + // Neither uncle: rskj refuses the whole block over one. + assert_eq!(store.total_difficulty(b2.hash()).unwrap(), Some(U256::from(21))); + assert_eq!(store.total_difficulty(b3.hash()).unwrap(), Some(U256::from(31))); +} + +/// In follow mode a block's total is settled from the executed head's when its +/// body comes, rather than when a fork announcement next brings its header +/// round again. +#[tokio::test] +async fn a_followed_block_counts_its_uncles_when_its_body_comes() { + let dir = tempdir().unwrap(); + let store = Arc::new(BlockStore::open(dir.path()).unwrap()); + let (b1, uncles) = genesis_and_siblings(&store, 7, 5); + let b2 = naming_uncles(dummy_header(2, b1.hash(), U256::from(10)), &uncles); + + let peer_store = Arc::new(rustock_networking::peers::PeerStore::new()); + let verifier = Arc::new(HeaderVerifier::new()); + let manager = Arc::new(SyncManager::new(store.clone(), verifier, peer_store.clone())); + manager.handle_headers_response(vec![b1.clone()], test_peer()).unwrap(); + store.put_body(b1.hash(), &[], &[]).unwrap(); + store.set_exec_head(b1.hash(), B256::ZERO).unwrap(); + manager.handle_headers_response(vec![b2.clone()], test_peer()).unwrap(); + assert_eq!( + store.total_difficulty(b2.hash()).unwrap(), + Some(U256::from(21)), + "a bound until the body comes" + ); + + let (_event_tx, event_rx) = mpsc::unbounded_channel(); + let mut service = SyncService::new(manager, peer_store, event_rx); + service.state = SyncState::Following; + let peer = B512::repeat_byte(9); + service + .pending_follow_bodies + .insert(7, (b2.hash(), b2.clone(), std::time::Instant::now(), peer)); + service.on_body_response(peer, 7, vec![], uncles).await; + + assert_eq!(store.total_difficulty(b2.hash()).unwrap(), Some(U256::from(33))); +} + +/// A header ingested again with its body held, as a fork announcement does to +/// the last few hundred, is credited the body's uncles only once they pass the +/// whole uncle rule, as settling does. +#[tokio::test] +async fn a_header_ingested_again_credits_only_uncles_that_did_their_work() { + for (second, total) in [(5u64, 33u64), (999, 21)] { + let dir = tempdir().unwrap(); + let store = Arc::new(BlockStore::open(dir.path()).unwrap()); + let (b1, uncles) = genesis_and_siblings(&store, 7, second); + let b2 = naming_uncles(dummy_header(2, b1.hash(), U256::from(10)), &uncles); + let verifier = Arc::new(HeaderVerifier::new().with_static_rule(NoWorkAt999)); + let peer_store = Arc::new(rustock_networking::peers::PeerStore::new()); + let manager = SyncManager::new(store.clone(), verifier, peer_store); + manager.handle_headers_response(vec![b2.clone(), b1.clone()], test_peer()).unwrap(); + store.put_body(b1.hash(), &[], &[]).unwrap(); + store.put_body(b2.hash(), &[], &uncles).unwrap(); + + manager.handle_headers_response(vec![b2.clone(), b1.clone()], test_peer()).unwrap(); + assert_eq!( + store.total_difficulty(b2.hash()).unwrap(), + Some(U256::from(total)), + "uncles worth 7 and {second}" + ); + } +} + +/// A held header whose parent is not held keeps the total it has. What the +/// block adds is taken off and added back, so taking off its own difficulty +/// alone counted its uncles twice once its total held them. +#[tokio::test] +async fn a_held_header_without_its_parent_keeps_its_total() { + use rustock_networking::protocol::HeaderWithUncles; + let dir = tempdir().unwrap(); + let store = Arc::new(BlockStore::open(dir.path()).unwrap()); + let (b1, uncles) = genesis_and_siblings(&store, 7, 5); + let b2 = naming_uncles(dummy_header(2, b1.hash(), U256::from(10)), &uncles); + // #2 held with the total that counts its uncles; #1 not held at all. + store.put_header(&b2).unwrap(); + store.put_total_difficulty(b2.hash(), U256::from(33)).unwrap(); + + let peer_store = Arc::new(rustock_networking::peers::PeerStore::new()); + let manager = SyncManager::new(store.clone(), Arc::new(HeaderVerifier::new()), peer_store); + // A peer at `rsk/63` proves what #2 adds, its uncles with it. + let entries = vec![ + HeaderWithUncles { header: b1, uncles: Vec::new() }, + HeaderWithUncles { header: b2.clone(), uncles }, + ]; + assert!(entries.iter().all(HeaderWithUncles::commitment_matches)); + manager.note_proven_difficulties(entries); + + manager.handle_headers_response(vec![b2.clone()], test_peer()).unwrap(); + assert_eq!(store.total_difficulty(b2.hash()).unwrap(), Some(U256::from(33))); +} + +/// An uncle is judged against the uncles its block's ancestors named, so it is +/// not credited while an ancestor's are not held: the rule would read that +/// ancestor as naming none, and pass an uncle it had named already. +#[tokio::test] +async fn an_uncle_is_not_credited_while_an_ancestors_uncles_are_not_held() { + let dir = tempdir().unwrap(); + let store = Arc::new(BlockStore::open(dir.path()).unwrap()); + let (b1, uncles) = genesis_and_siblings(&store, 7, 5); + let reused = &uncles[..1]; + // #2 names the uncle and #3 names it again, which the rule refuses -- but + // only a node holding #2's uncles can see it. + let b2 = naming_uncles(dummy_header(2, b1.hash(), U256::from(10)), reused); + let b3 = naming_uncles(dummy_header(3, b2.hash(), U256::from(10)), reused); + for (header, total) in [(&b1, 11u64), (&b2, 28)] { + store.put_header(header).unwrap(); + store.put_total_difficulty(header.hash(), U256::from(total)).unwrap(); + } + store.put_body(b1.hash(), &[], &[]).unwrap(); + store.put_header(&b3).unwrap(); + + let peer_store = Arc::new(rustock_networking::peers::PeerStore::new()); + let manager = SyncManager::new(store.clone(), Arc::new(HeaderVerifier::new()), peer_store); + assert!(manager.settle_total_difficulty(b3.hash(), &b3, reused)); + assert_eq!( + store.total_difficulty(b3.hash()).unwrap(), + Some(U256::from(38)), + "#2's uncles are not held, so #3's are not credited" + ); + + // With #2's uncles held, the rule sees the reuse and refuses it. + store.put_body(b2.hash(), &[], reused).unwrap(); + assert!(manager.settle_total_difficulty(b3.hash(), &b3, reused)); + assert_eq!(store.total_difficulty(b3.hash()).unwrap(), Some(U256::from(38))); + + // And an uncle that no ancestor named is credited: 28 + 10 + 5. + let fresh = &uncles[1..]; + let b3 = naming_uncles(dummy_header(3, b2.hash(), U256::from(10)), fresh); + store.put_header(&b3).unwrap(); + assert!(manager.settle_total_difficulty(b3.hash(), &b3, fresh)); + assert_eq!(store.total_difficulty(b3.hash()).unwrap(), Some(U256::from(43))); +} + +/// The oldest ancestor the rule walks, seven generations back or genesis, must +/// have its uncles held as much as the nearest. +#[tokio::test] +async fn the_oldest_ancestor_the_uncle_rule_walks_must_be_held_too() { + let dir = tempdir().unwrap(); + let store = Arc::new(BlockStore::open(dir.path()).unwrap()); + let genesis = dummy_header(0, B256::ZERO, U256::from(1)); + store.update_head(&genesis, U256::from(1)).unwrap(); + let b1 = dummy_header(1, genesis.hash(), U256::from(10)); + store.put_header(&b1).unwrap(); + store.put_total_difficulty(b1.hash(), U256::from(11)).unwrap(); + store.put_body(b1.hash(), &[], &[]).unwrap(); + let uncles = vec![dummy_header(1, genesis.hash(), U256::from(7))]; + let b2 = naming_uncles(dummy_header(2, b1.hash(), U256::from(10)), &uncles); + store.put_header(&b2).unwrap(); + + let peer_store = Arc::new(rustock_networking::peers::PeerStore::new()); + let manager = SyncManager::new(store.clone(), Arc::new(HeaderVerifier::new()), peer_store); + assert!(manager.settle_total_difficulty(b2.hash(), &b2, &uncles)); + assert_eq!( + store.total_difficulty(b2.hash()).unwrap(), + Some(U256::from(21)), + "genesis is within the rule's reach and its uncles are not held" + ); + + store.put_body(genesis.hash(), &[], &[]).unwrap(); + assert!(manager.settle_total_difficulty(b2.hash(), &b2, &uncles)); + assert_eq!(store.total_difficulty(b2.hash()).unwrap(), Some(U256::from(28))); +} + /// One real uncle may not be counted twice. /// /// After #268 a peer cannot fabricate uncles: the list is bound to the trunk diff --git a/crates/sync/src/total_difficulty.rs b/crates/sync/src/total_difficulty.rs new file mode 100644 index 0000000..abb41ee --- /dev/null +++ b/crates/sync/src/total_difficulty.rs @@ -0,0 +1,156 @@ +//! A block's total difficulty, once its body is held. +//! +//! rskj gives a block its parent's total difficulty plus its own difficulty and +//! its uncles' (`Block.getCumulativeDifficulty`). The uncles travel only in the +//! body, so a header from an `rsk/62` peer -- every rskj node -- can only be +//! given a lower bound when it arrives: its parent's total and its own +//! difficulty. Nothing raised that bound once the body came, so a node that +//! caught up by header-first sync carried every uncle it skipped in every total +//! above it: on mainnet, 6.9 x 10^25 short of rskj after 8,000 blocks. [`settle`] +//! makes a block's total exact once its body is held, in the order the chain +//! runs, so that each block starts from a parent whose total is exact already. +//! +//! An uncle is credited only once it passes rskj's whole uncle rule against the +//! ancestry this node holds, with every rule a trunk header faces, proof of work +//! included: the standard [`crate::uncle_guard`] holds uncles from the wire to. +//! The body check on receipt proves only that the list is the one the block's +//! header commits to, that its miner chose those uncles, not that each did the +//! work it claims. An uncle that fails is not credited, which leaves the total +//! understated, the safe direction, rather than overstated. + +use alloy_primitives::{B256, U256}; +use rustock_core::types::block::Block; +use rustock_core::validation::uncles::{ + AncestorSource, UnclesValidationRule, UNCLE_GENERATION_LIMIT, UNCLE_LIST_LIMIT, +}; +use rustock_core::validation::HeaderVerifier; +use rustock_core::Header; +use rustock_storage::BlockStore; +use tracing::warn; + +/// The ancestry the uncle rule walks, from what this node holds. +struct Held<'a> { + store: &'a BlockStore, +} + +impl Held<'_> { + /// Whether every ancestor the rule walks is held, with the uncles it named. + /// + /// Asked first, because the rule reads an ancestor it cannot find as one + /// that named no uncles, and could then pass an uncle that one had used. + fn has_ancestry(&self, number: u64, parent: B256) -> bool { + let floor = number.saturating_sub(UNCLE_GENERATION_LIMIT); + let mut cursor = parent; + loop { + let Some(header) = self.store.header(cursor).ok().flatten() else { + return false; + }; + if header.number < floor { + return true; + } + if held_uncles(self.store, cursor, &header).is_none() { + return false; + } + if header.number == 0 { + return true; + } + cursor = header.parent_hash; + } + } +} + +impl AncestorSource for Held<'_> { + fn header(&self, hash: B256) -> Option
{ + self.store.header(hash).ok().flatten() + } + + fn uncles_of(&self, hash: B256) -> Vec
{ + // Asked only of ancestors that `has_ancestry` found held, so an empty + // answer means the ancestor named none. + self.header(hash) + .and_then(|header| held_uncles(self.store, hash, &header)) + .unwrap_or_default() + } +} + +/// The uncles a held block named: from its body, or once the body is pruned, +/// from the freezer, which keeps them by height for the canonical chain. +/// `None` when neither holds them. +pub(crate) fn held_uncles(store: &BlockStore, hash: B256, header: &Header) -> Option> { + if let Ok(Some(uncles)) = store.uncles(hash) { + return Some(uncles); + } + if store.canonical_hash(header.number).ok().flatten() != Some(hash) { + return None; + } + store.freezer()?.uncles(header.number).ok().flatten() +} + +/// The difficulty of `uncles`, the uncles a block names, if they pass rskj's +/// whole uncle rule against the ancestry this node holds; otherwise why not. +pub(crate) fn credited_uncles( + store: &BlockStore, + verifier: &HeaderVerifier, + header: &Header, + uncles: &[Header], +) -> Result { + if uncles.is_empty() { + return Ok(U256::ZERO); + } + let held = Held { store }; + if !held.has_ancestry(header.number, header.parent_hash) { + return Err("the ancestry needed to judge them is not all held".to_string()); + } + let block = Block { + header: header.clone(), + transactions: Vec::new(), + ommers: uncles.to_vec(), + }; + UnclesValidationRule { + store: &held, + uncle_list_limit: UNCLE_LIST_LIMIT, + uncle_generation_limit: UNCLE_GENERATION_LIMIT, + header_rules: verifier.static_rules(), + parent_rules: verifier.parent_rules(), + } + .validate(&block) + .map_err(|e| e.to_string())?; + Ok(uncles.iter().fold(U256::ZERO, |sum, uncle| { + sum.saturating_add(uncle.difficulty) + })) +} + +/// Give a block whose uncles are `uncles` the total difficulty rskj gives it: +/// its parent's total, its own difficulty, and its uncles' once they pass the +/// rule. Written only where it differs from the stored total. +/// +/// The parent's total must be exact already, which holds when blocks are +/// settled in the order the chain runs, from one that is. `None` when the +/// parent has no total to start from. +pub(crate) fn settle( + store: &BlockStore, + verifier: &HeaderVerifier, + hash: B256, + header: &Header, + uncles: &[Header], +) -> anyhow::Result> { + let Some(parent) = store.total_difficulty(header.parent_hash)? else { + return Ok(None); + }; + let credited = credited_uncles(store, verifier, header, uncles).unwrap_or_else(|why| { + warn!( + target: "rustock::sync", + "Block #{} ({:?}): its uncles are not credited with their difficulty: {why}", + header.number, + hash + ); + U256::ZERO + }); + let total = parent + .saturating_add(header.difficulty) + .saturating_add(credited); + if store.total_difficulty(hash)? != Some(total) { + store.put_total_difficulty(hash, total)?; + } + Ok(Some(total)) +} From 92aa8860cacb29fc027ab9c7b93ca295318efb67 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Fri, 6 Nov 2026 08:59:28 -0800 Subject: [PATCH 012/264] sync: a batch stopped by a block that fails validation is not clean process_downloaded_blocks stopped the batch when a block failed consensus validation, but left all_ok set, unlike every other failure branch. poll_execution then treated the batch as clean and resumed, and the halt came only from the next batch's lineage check, a batch later; rskj treats such a block as invalid and stops there. The test runs a real processor on a block without its REMASC transaction. --- crates/sync/src/service.rs | 8 ++++-- crates/sync/src/tests.rs | 51 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/crates/sync/src/service.rs b/crates/sync/src/service.rs index ba5f0b2..8b4a67f 100644 --- a/crates/sync/src/service.rs +++ b/crates/sync/src/service.rs @@ -5426,7 +5426,7 @@ pub(crate) fn identify_chunk_by_content( /// missing); the sync must not advance past it (rskj treats such a block as /// invalid and stops there). The returned state root and flush counter are /// carried back into the service for the next batch. -fn process_downloaded_blocks( +pub(crate) fn process_downloaded_blocks( processor: &BlockProcessor, trie_store: Arc, state_root: TrieNode, @@ -5507,13 +5507,17 @@ fn process_downloaded_blocks( ommers, }; - // Same consensus gate as the single-block path above. + // Same consensus gate as the single-block path above, and a failure + // here is a failure of the batch like any other below: reported clean, + // it let the sync resume past an invalid block until the next batch's + // lineage check tripped over the gap it left. if let Err(e) = processor.validate_block(&block) { warn!( target: "rustock::sync", "Block #{} ({:?}) failed consensus validation: {e}", block.header.number, hash ); + all_ok = false; break; } diff --git a/crates/sync/src/tests.rs b/crates/sync/src/tests.rs index 2a5327a..3e0e05d 100644 --- a/crates/sync/src/tests.rs +++ b/crates/sync/src/tests.rs @@ -7179,6 +7179,57 @@ async fn the_oldest_ancestor_the_uncle_rule_walks_must_be_held_too() { assert_eq!(store.total_difficulty(b2.hash()).unwrap(), Some(U256::from(28))); } +/// A batch stopped by a block that fails consensus validation is not clean, +/// any more than one stopped by a block that fails to execute: rskj treats the +/// block as invalid and stops there. +#[test] +fn a_batch_stopped_by_a_block_that_fails_validation_is_not_clean() { + let dir = tempdir().unwrap(); + let store = Arc::new(BlockStore::open(dir.path()).unwrap()); + let trie = Arc::new(rustock_trie::MemoryTrieStore::new()) as Arc; + let empty = rustock_trie::TrieNode::empty(); + let root = empty.compute_hash(trie.as_ref()); + trie.put(root.as_slice(), &empty.to_message(trie.as_ref())); + + let genesis = dummy_header(0, B256::ZERO, U256::from(1)); + store.update_head(&genesis, U256::from(1)).unwrap(); + store.set_exec_head(genesis.hash(), root).unwrap(); + // A block with no REMASC transaction, which every block but genesis needs. + let b1 = dummy_header(1, genesis.hash(), U256::from(1)); + store.put_header(&b1).unwrap(); + store.put_body(b1.hash(), &[], &[]).unwrap(); + + let processor = rustock_execution::BlockProcessor::new( + rustock_execution::RskHardforkConfig::mainnet(), + store.clone(), + ); + let block = rustock_core::types::block::Block { + header: b1.clone(), + transactions: Vec::new(), + ommers: Vec::new(), + }; + assert!(processor.validate_block(&block).is_err(), "the block must fail validation"); + + let outcome = crate::service::process_downloaded_blocks( + &processor, + trie, + empty, + &store, + None, + 0, + 1, + &[(b1.hash(), b1)], + &crate::chain_activity::ChainActivity::new(), + &crate::gas_price::GasPriceTracker::default(), + ); + assert!(!outcome.all_ok, "a batch stopped by an invalid block reported itself clean"); + assert_eq!( + store.exec_head().unwrap().map(|(hash, _)| hash), + Some(genesis.hash()), + "and nothing past the invalid block's parent executed" + ); +} + /// One real uncle may not be counted twice. /// /// After #268 a peer cannot fabricate uncles: the list is bound to the trunk From c0a5de4047431a3d1671ffb089e9cf683440e9be Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Sun, 8 Nov 2026 08:23:55 -0800 Subject: [PATCH 013/264] Hold each uncle to rskj's uncle rules in block validation BlockProcessor::validate_block_rules ran rskj's BlockUnclesValidationRule with empty per-uncle rule lists: it checked an uncle's place in the chain and nothing about the uncle itself. A block could name an uncle that did no work, and the node would execute it and count the uncle's difficulty. Each uncle now passes the rules rskj's RskContext gives the uncle rule: on its own, proof of work, the timestamp rule and gas used; against its own parent, the minimum gas price, number, timestamp, difficulty and gas limit; and, under the processor's fork-detection switch, its fork-detection data, read from the 449 headers below its own parent. Not a trunk block's minimum gas limit or 32-byte extra-data cap, which rskj does not apply to uncles. The lists are uncles::rsk_header_rules and rsk_parent_rules, and the processor takes the node's ChainConfig for them. rskj's timestamp rule is two rules here, TimestampRule and BitcoinTimestampRule (RSKIP179). To be rskj's rules exactly, four rules change for every header: - BitcoinTimestampRule is off where the chain's max_future_block_time is zero, as RSKIP179 is in rskj: BlockTimeStampValidationRule.isValid returns before isBitcoinTimestampValid when validPeriodLength is zero, which is regtest's setting. The rule's regtest tests now switch the period on. - BlockParentGasLimitRule bounds by parent * 1023 / 1024 as rskj does. Its lower bound was a gas too high whenever 1024 does not divide the parent's limit. - MergedMiningRule wants exactly an 80-byte Bitcoin header from orchid (rskj's BtcHeaderSizeRule, RSKIP98). A longer one passed. - fork_detection::validate starts above block 449, as rskj's does. Below it rskj reads no fork-detection data, and the twelve coinbase bytes after the hash prefix would have refused every early testnet and regtest block. UncleGuard and total_difficulty in the sync still judge uncles by the trunk rules. Tests: core tests of each rule against rskj's numbers and of the uncle rule lists; processor tests that an uncle without proof of work, with work done for another header, or no later than its parent fails import, and that with fork detection on an uncle must carry its own parent's data. On a light node's store, every uncle of the uncle-bearing mainnet blocks it holds passes with fork detection on; the real-data test now skips blocks whose ancestry the pruned store no longer holds. --- crates/cli/src/main.rs | 5 +- crates/core/src/config.rs | 8 +- crates/core/src/validation/fork_detection.rs | 17 +- crates/core/src/validation/header_rules.rs | 26 +- crates/core/src/validation/merged_mining.rs | 19 +- crates/core/src/validation/mod.rs | 2 +- crates/core/src/validation/tests.rs | 297 +++++++++++++++++- crates/core/src/validation/uncles.rs | 65 +++- crates/execution/src/processor.rs | 205 +++++++++++- crates/execution/tests/mainnet_block_rules.rs | 47 ++- crates/sync/src/total_difficulty.rs | 1 + crates/sync/src/uncle_guard.rs | 1 + 12 files changed, 656 insertions(+), 37 deletions(-) diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index 78e84ae..bfb80ee 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -2581,10 +2581,13 @@ async fn run(local_offset: Option) -> Result<()> { None }; + // The same chain configuration the header verifier uses: an uncle is + // held to the rules a header is, on this chain's constants. let mut block_processor = rustock_execution::BlockProcessor::new( hardfork_cfg.clone(), store.clone(), - ); + ) + .with_chain_config(config.clone()); if let Some(cache) = &btc_block_cache { block_processor = block_processor.with_btc_block_cache(cache.clone()); } diff --git a/crates/core/src/config.rs b/crates/core/src/config.rs index aabb0af..2547cb5 100644 --- a/crates/core/src/config.rs +++ b/crates/core/src/config.rs @@ -29,7 +29,8 @@ pub struct ActivationHeights { /// Papyrus200 hardfork – enables RSKIP156 (difficulty divisor 50 -> 400). pub papyrus200: u64, /// Iris300 hardfork – enables RSKIP180 (960-byte merged-mining merkle - /// proof cap). + /// proof cap) and RSKIP179 (a header's timestamp must sit near that of + /// the Bitcoin header that merge-mined it). pub iris300: u64, /// Fingerroot500 hardfork – enables RSKIP252 (transaction gas price cap). pub fingerroot500: u64, @@ -155,7 +156,8 @@ pub struct ChainConfig { /// rskj `Constants.newBlockMaxSecondsInTheFuture`: how far past the /// node's clock a header may be timestamped. 60 on mainnet, 540 on /// testnet, 0 on regtest, where zero turns the timestamp rule off - /// altogether (parent comparison included), as it does in rskj. + /// altogether (parent comparison and RSKIP179 included), as it does in + /// rskj. pub max_future_block_time: u64, pub gas_limit_bound_divisor: u64, /// rskj `Constants.getMinGasLimit()`: 3,000,000 on every network @@ -586,6 +588,7 @@ mod tests { assert_eq!(heights.wasabi100, 1_591_000); assert_eq!(heights.papyrus200, 2_392_700); assert_eq!(heights.iris300, 3_614_800); + assert_eq!(heights.hop400, 4_598_500); assert_eq!(heights.fingerroot500, 5_468_000); } @@ -602,6 +605,7 @@ mod tests { assert_eq!(heights.wasabi100, 0); assert_eq!(heights.papyrus200, 863_000); assert_eq!(heights.iris300, 2_060_500); + assert_eq!(heights.hop400, 3_103_000); assert_eq!(heights.fingerroot500, 4_015_800); } diff --git a/crates/core/src/validation/fork_detection.rs b/crates/core/src/validation/fork_detection.rs index a8e6978..c9df6c7 100644 --- a/crates/core/src/validation/fork_detection.rs +++ b/crates/core/src/validation/fork_detection.rs @@ -103,14 +103,15 @@ pub fn validate( return Ok(()); } - // hasForkDetectionDataWhereItShouldNotHave. Unreachable on mainnet and - // testnet, where RSKIP110 activates far above block 449, but kept so the - // rule reads as rskj's does. - if header.number < REQUIRED_BLOCKS { - return match actual { - Some(_) => Err(ValidationError::ForkDetectionDataUnreadable), - None => Ok(()), - }; + // rskj reads no fork-detection data from a header below 449 at all + // (`BlockFactory` builds it with `includeForkDetectionData` false), so + // its `hasForkDetectionDataWhereItShouldNotHave` branch never fires, and + // it compares only above 449, not at 449 itself. Reading the coinbase + // here instead would find twelve bytes after the tag and the hash prefix + // -- the rest of the hash -- and refuse every early testnet and regtest + // block. + if header.number <= REQUIRED_BLOCKS { + return Ok(()); } let got = actual.ok_or(ValidationError::ForkDetectionDataUnreadable)?; diff --git a/crates/core/src/validation/header_rules.rs b/crates/core/src/validation/header_rules.rs index 6aa4652..81a6d69 100644 --- a/crates/core/src/validation/header_rules.rs +++ b/crates/core/src/validation/header_rules.rs @@ -142,21 +142,33 @@ impl HeaderValidator for GasLimitBoundsRule { } } +/// rskj `BlockParentGasLimitRule`: a header's gas limit may move from its +/// parent's by at most a `gas_limit_bound_divisor`th, either way. pub struct BlockParentGasLimitRule { pub config: std::sync::Arc, } impl ParentHeaderValidator for BlockParentGasLimitRule { fn validate_with_parent(&self, header: &Header, parent: &Header) -> Result<(), ValidationError> { - let limit = gas_limit_as_u64(header.gas_limit)?; - let parent_limit = gas_limit_as_u64(parent.gas_limit)?; - let divisor = self.config.gas_limit_bound_divisor; - let delta = parent_limit / divisor; - - if limit < parent_limit - delta || limit > parent_limit + delta { + // rskj compares in BigInteger against `parent * (d - 1) / d` and + // `parent * (d + 1) / d`. As integers those are + // `parent - ceil(parent / d)` and `parent + floor(parent / d)`, so the + // lower bound sits one below `parent - parent / d` whenever `d` does + // not divide the parent's limit. + let divisor = U256::from(self.config.gas_limit_bound_divisor); + let parent_limit = parent.gas_limit; + let step = parent_limit / divisor; + let lower = if (parent_limit % divisor).is_zero() { + parent_limit - step + } else { + parent_limit - step - U256::from(1u8) + }; + let upper = parent_limit.saturating_add(step); + + if header.gas_limit < lower || header.gas_limit > upper { return Err(ValidationError::GasLimitInvalid { parent: parent_limit, - got: limit, + got: header.gas_limit, }); } Ok(()) diff --git a/crates/core/src/validation/merged_mining.rs b/crates/core/src/validation/merged_mining.rs index 0cceeb4..de9a12d 100644 --- a/crates/core/src/validation/merged_mining.rs +++ b/crates/core/src/validation/merged_mining.rs @@ -25,6 +25,11 @@ pub const MAX_RSK_TAG_POSITION_IN_TAIL: usize = 64; /// RSKIP180 (iris300). pub const MAX_MERGED_MINING_MERKLE_PROOF_LENGTH: usize = 960; +/// The length of a Bitcoin block header, and from RSKIP98 (orchid) the only +/// length rskj accepts for the field that carries one: rskj +/// `RskMiningConstants.BTC_HEADER_SIZE`, enforced by `BtcHeaderSizeRule`. +pub const BTC_HEADER_SIZE: usize = 80; + pub struct MergedMiningRule { pub config: std::sync::Arc, } @@ -39,6 +44,10 @@ pub struct MergedMiningRule { /// is invalid outright, as it is in rskj. The rule does not depend on /// `skip_pow_validation`: rskj's `miner.server.skipPowValidation` reaches /// `ProofOfWorkRule` only. +/// +/// It is part of rskj's timestamp rule, and goes with it where the chain's +/// `max_future_block_time` is zero (regtest): rskj's `isValid` returns +/// before `isBitcoinTimestampValid` when `validPeriodLength` is zero. pub struct BitcoinTimestampRule { pub config: std::sync::Arc, } @@ -68,7 +77,9 @@ impl HeaderValidator for BitcoinTimestampRule { use bitcoin::block::Header as BtcHeader; use bitcoin::consensus::Decodable; - if header.number < self.config.activation_heights.iris300 { + if self.config.max_future_block_time == 0 + || header.number < self.config.activation_heights.iris300 + { return Ok(()); } let btc_header_bytes = header @@ -107,6 +118,12 @@ impl HeaderValidator for MergedMiningRule { let btc_header_bytes = header.bitcoin_merged_mining_header.as_ref() .ok_or(ValidationError::BitcoinHeaderDecodeError)?; + // rskj `BtcHeaderSizeRule`, from RSKIP98, which is orchid like the + // rest of this rule: exactly a Bitcoin header. The decoder below + // reads the first 80 bytes and would let anything after them ride. + if btc_header_bytes.len() != BTC_HEADER_SIZE { + return Err(ValidationError::BitcoinHeaderDecodeError); + } let mut reader = &btc_header_bytes[..]; let btc_header: BtcHeader = Decodable::consensus_decode(&mut reader) .map_err(|_| ValidationError::BitcoinHeaderDecodeError)?; diff --git a/crates/core/src/validation/mod.rs b/crates/core/src/validation/mod.rs index afa78bc..432d739 100644 --- a/crates/core/src/validation/mod.rs +++ b/crates/core/src/validation/mod.rs @@ -29,7 +29,7 @@ pub enum ValidationError { GasLimitOutOfBounds { min: u64, max: u64, got: u64 }, #[error("Gas limit {got} is invalid compared to parent {parent}")] - GasLimitInvalid { parent: u64, got: u64 }, + GasLimitInvalid { parent: U256, got: U256 }, #[error("Difficulty mismatch: expected {expected}, got {got}")] DifficultyMismatch { expected: U256, got: U256 }, diff --git a/crates/core/src/validation/tests.rs b/crates/core/src/validation/tests.rs index 9882592..da996d6 100644 --- a/crates/core/src/validation/tests.rs +++ b/crates/core/src/validation/tests.rs @@ -429,6 +429,15 @@ fn rskip179_rule(config: crate::config::ChainConfig) -> BitcoinTimestampRule { BitcoinTimestampRule { config: Arc::new(config) } } +/// Regtest's heights, iris300 at genesis, with the timestamp rule's period +/// on: where it is zero, regtest's own setting, rskj runs none of the rule, +/// RSKIP179 included. +fn regtest_with_a_period() -> crate::config::ChainConfig { + let mut config = crate::config::ChainConfig::regtest(); + config.max_future_block_time = 540; + config +} + /// rskj `BlockTimeStampValidationRuleTest.timestampsAreCloseEnough`: equal /// times pass; exactly five minutes apart fails (strict); one second inside /// the window passes, in either direction. @@ -436,7 +445,7 @@ fn rskip179_rule(config: crate::config::ChainConfig) -> BitcoinTimestampRule { fn timestamps_are_close_enough() { let max = BitcoinTimestampRule::DEFAULT_MAX_TIMESTAMPS_DIFF_IN_SECS; // regtest activates iris300 at genesis. - let rule = rskip179_rule(crate::config::ChainConfig::regtest()); + let rule = rskip179_rule(regtest_with_a_period()); let check = |rsk: u64, btc: u64| { let mut header = create_dummy_header(10, rsk, B256::ZERO); header.bitcoin_merged_mining_header = Some(btc_header_at(btc as u32)); @@ -455,7 +464,7 @@ fn timestamps_are_close_enough() { /// Before RSKIP179 the rule is off: no header, any distance. #[test] fn bitcoin_timestamp_is_not_checked_before_rskip179() { - let mut config = crate::config::ChainConfig::regtest(); + let mut config = regtest_with_a_period(); config.activation_heights.iris300 = 100; let rule = rskip179_rule(config); @@ -472,13 +481,25 @@ fn bitcoin_timestamp_is_not_checked_before_rskip179() { /// fails the rule by itself. #[test] fn a_header_without_a_decodable_bitcoin_header_fails_rskip179() { - let rule = rskip179_rule(crate::config::ChainConfig::regtest()); + let rule = rskip179_rule(regtest_with_a_period()); let mut header = create_dummy_header(10, 1_000, B256::ZERO); assert!(matches!(rule.validate(&header), Err(ValidationError::BitcoinHeaderDecodeError))); header.bitcoin_merged_mining_header = Some(Bytes::from_static(&[0x00])); assert!(matches!(rule.validate(&header), Err(ValidationError::BitcoinHeaderDecodeError))); } +/// rskj `BlockTimeStampValidationRule.isValid` returns before +/// `isBitcoinTimestampValid` when `validPeriodLength` is zero, regtest's +/// setting: RSKIP179 goes with the rest of the rule. +#[test] +fn a_zero_period_turns_rskip179_off_too() { + let rule = rskip179_rule(crate::config::ChainConfig::regtest()); + let mut header = create_dummy_header(10, 1_000, B256::ZERO); + assert!(rule.validate(&header).is_ok(), "no merged-mining header"); + header.bitcoin_merged_mining_header = Some(btc_header_at(1_000_000)); + assert!(rule.validate(&header).is_ok(), "a million seconds away"); +} + /// rskj `Constants.getMaxTimestampsDiffInSecs`: testnet widens the window to /// two hours from RSKIP297 (hop400); mainnet keeps five minutes. #[test] @@ -605,3 +626,273 @@ fn gas_limit_less_than_minimum_gas_limit() { Err(ValidationError::GasLimitOutOfBounds { min: 3_000_000, got: 2_999_999, .. }) )); } + +// --- rskj's exact parent gas-limit bound, and the rules each uncle must pass + +/// rskj bounds a gas limit by `parent * 1023 / 1024` and +/// `parent * 1025 / 1024` in BigInteger. Where 1024 does not divide the +/// parent's limit, the lower bound is one below `parent - parent / 1024`. +#[test] +fn the_parent_gas_limit_bounds_are_rskjs_when_the_divisor_leaves_a_remainder() { + let rule = BlockParentGasLimitRule { config: Arc::new(crate::config::ChainConfig::mainnet()) }; + let mut parent = create_dummy_header(10, 1000, B256::ZERO); + parent.gas_limit = U256::from(6_800_000); + let with = |limit: u64| { + let mut header = create_dummy_header(11, 1010, parent.hash()); + header.gas_limit = U256::from(limit); + header + }; + + // 6,800,000 * 1023 / 1024 = 6,793,359.375 and * 1025 / 1024 = 6,806,640.625. + assert_eq!(rule.validate_with_parent(&with(6_793_359), &parent), Ok(())); + assert!(matches!( + rule.validate_with_parent(&with(6_793_358), &parent), + Err(ValidationError::GasLimitInvalid { .. }) + )); + assert_eq!(rule.validate_with_parent(&with(6_806_640), &parent), Ok(())); + assert!(rule.validate_with_parent(&with(6_806_641), &parent).is_err()); +} + +/// rskj `BtcHeaderSizeRule`: from orchid the field holds a Bitcoin header and +/// nothing else. A byte appended changes neither the proof of work nor the +/// merkle proof, which is why it needs a check of its own. +#[test] +fn merged_mining_wants_exactly_a_bitcoin_header() { + let rule = MergedMiningRule { config: Arc::new(crate::config::ChainConfig::regtest()) }; + let mut header = create_dummy_header(10, 1000, B256::ZERO); + header.difficulty = U256::from(1); + let (btc_h, btc_cb, btc_m) = build_mm_proof(&header, 0x207fffff, None); + header.bitcoin_merged_mining_coinbase_transaction = Some(btc_cb); + header.bitcoin_merged_mining_merkle_proof = Some(btc_m); + + let mut longer = btc_h.to_vec(); + longer.push(0); + header.bitcoin_merged_mining_header = Some(Bytes::from(longer)); + assert_eq!(rule.validate(&header), Err(ValidationError::BitcoinHeaderDecodeError)); + + header.bitcoin_merged_mining_header = Some(btc_h); + assert_eq!(rule.validate(&header), Ok(())); +} + +/// A view of the chain that holds nothing. +struct NoChain; + +impl fork_detection::MainchainView for NoChain { + fn headers_from(&self, _from: B256, _count: u64) -> Vec
{ + Vec::new() + } +} + +/// rskj reads no fork-detection data below block 449 and compares none at +/// 449, so twelve bytes after the tag -- which every coinbase has, the rest +/// of the hash -- are no reason to refuse a block there. Above 449, a chain +/// too short to compute from is. +#[test] +fn fork_detection_starts_above_block_449() { + for number in [100, 449] { + let header = create_dummy_header(number, 1000, B256::ZERO); + assert_eq!(fork_detection::validate(&header, Some([7; 12]), &NoChain, 0), Ok(())); + } + let header = create_dummy_header(450, 1000, B256::ZERO); + assert_eq!( + fork_detection::validate(&header, Some([7; 12]), &NoChain, 0), + Err(ValidationError::ForkDetectionDataUnreadable) + ); +} + +/// Merge-mine `header` the way a miner does: commit to it in a coinbase, and +/// roll the Bitcoin header's nonce until its hash meets the RSK difficulty. +fn seal(header: &mut Header, btc_time: u64) { + let (btc, coinbase, proof) = build_mm_proof(header, 0x207fffff, None); + let mut btc: BtcHeader = bitcoin::consensus::deserialize(&btc).unwrap(); + btc.time = btc_time as u32; + let target = U256::MAX / header.difficulty; + while U256::from_le_slice(btc.block_hash().as_byte_array()) > target { + btc.nonce += 1; + } + header.bitcoin_merged_mining_header = Some(Bytes::from(bitcoin::consensus::serialize(&btc))); + header.bitcoin_merged_mining_coinbase_transaction = Some(coinbase); + header.bitcoin_merged_mining_merkle_proof = Some(proof); +} + +/// A chain held in memory, for the uncle rule. +struct Ancestry(std::collections::HashMap); + +impl uncles::AncestorSource for Ancestry { + fn header(&self, hash: B256) -> Option
{ + self.0.get(&hash).cloned() + } + fn uncles_of(&self, _hash: B256) -> Vec
{ + Vec::new() + } +} + +/// Regtest's constants with rskj's timestamp rule switched on, so that every +/// rule an uncle faces runs. +fn uncle_config() -> Arc { + let mut config = crate::config::ChainConfig::regtest(); + config.max_future_block_time = 60; + Arc::new(config) +} + +/// Three trunk blocks from `base` and the block above them, with the middle +/// one's parent: (ancestry, parent, block). +fn uncle_chain(base: u64) -> (Ancestry, Header, Header) { + let mut root = create_dummy_header(base, 980, B256::ZERO); + root.difficulty = U256::from(1); + let mut parent = create_dummy_header(base + 1, 1000, root.hash()); + parent.difficulty = U256::from(1); + let mut tip = create_dummy_header(base + 2, 1020, parent.hash()); + tip.difficulty = U256::from(1); + let block = create_dummy_header(base + 3, 1030, tip.hash()); + let ancestry = [root, parent.clone(), tip] + .into_iter() + .map(|header| (header.hash(), header)) + .collect(); + (Ancestry(ancestry), parent, block) +} + +/// The tip's sibling: a child of `parent` at `timestamp`, with the difficulty +/// its parent calls for. +fn uncle_of(parent: &Header, timestamp: u64) -> Header { + let mut uncle = create_dummy_header(parent.number + 1, timestamp, parent.hash()); + uncle.beneficiary = Address::repeat_byte(0x0b); + uncle.difficulty = DifficultyRule { config: uncle_config() } + .difficulty_for_child(parent, uncle.number, timestamp, 0); + uncle +} + +fn judge( + ancestry: &Ancestry, + block: &Header, + uncle: Header, + fork_detection: Option>, +) -> Result<(), ValidationError> { + let header_rules = uncles::rsk_header_rules(uncle_config()); + let parent_rules = uncles::rsk_parent_rules(uncle_config()); + let block = crate::types::block::Block { + header: block.clone(), + transactions: Vec::new(), + ommers: vec![uncle], + }; + uncles::UnclesValidationRule { + store: ancestry, + uncle_list_limit: uncles::UNCLE_LIST_LIMIT, + uncle_generation_limit: uncles::UNCLE_GENERATION_LIMIT, + header_rules: &header_rules, + parent_rules: &parent_rules, + fork_detection, + } + .validate(&block) +} + +/// An uncle's difficulty counts toward the chain's work, so rskj holds it to +/// what a block is held to: its own proof of work, and its difficulty and +/// timestamps against its own parent. +#[test] +fn an_uncle_must_pass_rskjs_rules() { + let (ancestry, parent, block) = uncle_chain(0); + + let mut uncle = uncle_of(&parent, 1010); + seal(&mut uncle, 1010); + assert_eq!(judge(&ancestry, &block, uncle, None), Ok(())); + + // Without its proof of work it is work nobody did. + let bare = uncle_of(&parent, 1010); + assert_eq!( + judge(&ancestry, &block, bare, None), + Err(ValidationError::BitcoinHeaderDecodeError) + ); + // Nor is work done for another header: this proof commits to the uncle + // as it was before its beneficiary changed. Its Bitcoin header is there + // and in time, so only proof of work can catch it. + let mut stolen = uncle_of(&parent, 1010); + seal(&mut stolen, 1010); + stolen.beneficiary = Address::repeat_byte(0x0c); + assert_eq!( + judge(&ancestry, &block, stolen, None), + Err(ValidationError::BitcoinCoinbaseTagInvalid) + ); + + // Its difficulty is the one its parent calls for, proof or no proof. + let mut harder = uncle_of(&parent, 1010); + harder.difficulty += U256::from(1); + seal(&mut harder, 1010); + assert!(matches!( + judge(&ancestry, &block, harder, None), + Err(ValidationError::DifficultyMismatch { .. }) + )); + + // Its gas limit, minimum gas price and number follow its parent's. + let mut grasping = uncle_of(&parent, 1010); + grasping.gas_limit = U256::from(9_990_233); + seal(&mut grasping, 1010); + assert!(matches!( + judge(&ancestry, &block, grasping, None), + Err(ValidationError::GasLimitInvalid { .. }) + )); + let mut pricey = uncle_of(&parent, 1010); + pricey.minimum_gas_price = U256::from(2); + seal(&mut pricey, 1010); + assert!(matches!( + judge(&ancestry, &block, pricey, None), + Err(ValidationError::MinGasPriceOutOfRange { .. }) + )); + let mut misnumbered = uncle_of(&parent, 1010); + misnumbered.number = parent.number; + seal(&mut misnumbered, 1010); + assert!(matches!( + judge(&ancestry, &block, misnumbered, None), + Err(ValidationError::InvalidBlockNumber { .. }) + )); + + // It comes after its parent. + let mut early = uncle_of(&parent, parent.timestamp); + seal(&mut early, parent.timestamp); + assert!(matches!( + judge(&ancestry, &block, early, None), + Err(ValidationError::TimestampOlderThanParent { .. }) + )); + + // And it sits within five minutes of the Bitcoin header that mined it. + let mut adrift = uncle_of(&parent, 1010); + seal(&mut adrift, 1010 + 300); + assert!(matches!( + judge(&ancestry, &block, adrift, None), + Err(ValidationError::BitcoinTimestampTooFar { .. }) + )); +} + +/// What rskj does not hold an uncle to: a trunk block's 32-byte extra-data +/// cap, or `parent - parent / 1024` where its gas-limit bound is a gas lower. +#[test] +fn an_uncle_is_not_held_to_more_than_rskj_holds_it_to() { + let (ancestry, parent, block) = uncle_chain(0); + + let mut chatty = uncle_of(&parent, 1010); + chatty.extra_data = Bytes::from(vec![0xab; 40]); + seal(&mut chatty, 1010); + assert_eq!(judge(&ancestry, &block, chatty, None), Ok(())); + + // 10,000,000 * 1023 / 1024 = 9,990,234.375. + let mut lean = uncle_of(&parent, 1010); + lean.gas_limit = U256::from(9_990_234); + seal(&mut lean, 1010); + assert_eq!(judge(&ancestry, &block, lean, None), Ok(())); +} + +/// Asked to, the rule runs fork detection on each uncle, and an uncle above +/// block 449 whose chain cannot be read is refused; not asked, it is not. +#[test] +fn the_uncle_rule_runs_fork_detection_when_asked() { + let (ancestry, parent, block) = uncle_chain(1_000); + let mut uncle = uncle_of(&parent, 1010); + seal(&mut uncle, 1010); + + let asked = uncles::UncleForkDetection { view: &NoChain, rskip110_height: 0 }; + assert_eq!( + judge(&ancestry, &block, uncle.clone(), Some(asked)), + Err(ValidationError::ForkDetectionDataUnreadable) + ); + assert_eq!(judge(&ancestry, &block, uncle, None), Ok(())); +} diff --git a/crates/core/src/validation/uncles.rs b/crates/core/src/validation/uncles.rs index 599ddc1..4027570 100644 --- a/crates/core/src/validation/uncles.rs +++ b/crates/core/src/validation/uncles.rs @@ -5,17 +5,66 @@ //! header field. That says nothing about whether the uncles themselves are //! admissible, which is what this rule decides. -use super::{ValidationError, HeaderValidator, ParentHeaderValidator}; +use super::fork_detection::MainchainView; +use super::{ + BitcoinTimestampRule, BlockNumberRule, BlockParentGasLimitRule, DifficultyRule, GasUsedRule, + HeaderValidator, MergedMiningRule, ParentHeaderValidator, PrevMinGasPriceRule, TimestampRule, + ValidationError, +}; +use crate::config::ChainConfig; use crate::types::block::Block; use crate::types::header::Header; use alloy_primitives::B256; use std::collections::HashSet; +use std::sync::Arc; /// rskj `Constants.getUncleListLimit()`. pub const UNCLE_LIST_LIMIT: usize = 10; /// rskj `Constants.getUncleGenerationLimit()`. pub const UNCLE_GENERATION_LIMIT: u64 = 7; +/// The rules rskj puts each uncle through on its own, in +/// `RskContext.getBlockValidationRule`'s order: proof of work, the timestamp +/// rule and gas used, its `BlockHeaderCompositeRule`. rskj's timestamp rule +/// is two here: [`TimestampRule`], the clock, and [`BitcoinTimestampRule`], +/// RSKIP179. The fourth, fork detection, reads the chain rather than the +/// header and is [`UnclesValidationRule::fork_detection`]. +/// +/// Not the trunk header rules: rskj holds no uncle to the minimum gas limit +/// or the 32-byte extra-data cap, so an uncle may carry either and still be +/// one. `GasUsedRule` reads the gas limit as a `u64` where rskj's +/// `ValidGasUsedRule` reads Java's `longValue`; the two part only above +/// 2^63, which the parent rule's bound keeps out of an uncle's reach. +pub fn rsk_header_rules(config: Arc) -> Vec> { + vec![ + Box::new(MergedMiningRule { config: config.clone() }), + Box::new(TimestampRule::new(config.max_future_block_time)), + Box::new(BitcoinTimestampRule { config }), + Box::new(GasUsedRule), + ] +} + +/// The rules rskj puts each uncle through against its own parent, in its +/// `BlockHeaderParentCompositeRule`'s order. +pub fn rsk_parent_rules(config: Arc) -> Vec> { + vec![ + Box::new(PrevMinGasPriceRule), + Box::new(BlockNumberRule), + Box::new(TimestampRule::new(config.max_future_block_time)), + Box::new(DifficultyRule { config: config.clone() }), + Box::new(BlockParentGasLimitRule { config }), + ] +} + +/// rskj's `ForkDetectionDataRule` run on an uncle: the per-uncle check that +/// reads the chain, the 449 headers from the uncle's parent down, rather than +/// the uncle alone. +pub struct UncleForkDetection<'a> { + pub view: &'a dyn MainchainView, + /// RSKIP110 (wasabi100). + pub rskip110_height: u64, +} + /// The ancestry lookups `FamilyUtils` performs against rskj's block store. /// Implemented by the caller that owns the store. pub trait AncestorSource { @@ -79,16 +128,20 @@ pub fn used_uncles( /// rskj `BlockUnclesValidationRule.validateUncleList`. /// -/// `header_rules` and `parent_rules` are the same per-uncle checks rskj passes -/// in: `BlockHeaderCompositeRule(PoW, forkDetection, timestamp, gasUsed)` and +/// `header_rules` and `parent_rules` are the per-uncle checks rskj passes +/// in, [`rsk_header_rules`] and [`rsk_parent_rules`]: +/// `BlockHeaderCompositeRule(PoW, forkDetection, timestamp, gasUsed)` and /// `BlockHeaderParentCompositeRule(prevMinGasPrice, parentNumber, timestamp, -/// difficulty, parentGasLimit)`. +/// difficulty, parentGasLimit)`, with fork detection in `fork_detection`. pub struct UnclesValidationRule<'a, S: AncestorSource + ?Sized> { pub store: &'a S, pub uncle_list_limit: usize, pub uncle_generation_limit: u64, pub header_rules: &'a [Box], pub parent_rules: &'a [Box], + /// rskj's fork-detection check on each uncle. `None` skips it, as the + /// block processor does while its own fork-detection check is off. + pub fork_detection: Option>, } impl UnclesValidationRule<'_, S> { @@ -124,6 +177,10 @@ impl UnclesValidationRule<'_, S> { for rule in self.header_rules { rule.validate(uncle)?; } + if let Some(check) = &self.fork_detection { + let actual = super::merged_mining::extract_fork_detection_data(uncle); + super::fork_detection::validate(uncle, actual, check.view, check.rskip110_height)?; + } // validateParentNumber: a sibling or descendant is not an uncle, // and the generation gap is bounded. diff --git a/crates/execution/src/processor.rs b/crates/execution/src/processor.rs index ecfddd5..2589e05 100644 --- a/crates/execution/src/processor.rs +++ b/crates/execution/src/processor.rs @@ -138,6 +138,10 @@ pub struct BlockProcessor { /// recovered most of this block's transactions when it admitted them. /// `None` disables the cache entirely and every sender is recomputed. sender_cache: Option>, + /// The chain's consensus constants, for the rules each uncle must pass: + /// proof of work, timestamps, difficulty and gas limit against its own + /// parent. + chain_config: Arc, } /// Ancestry lookups backed by the block store, for the uncle and @@ -183,15 +187,29 @@ impl BlockProcessor { block_store: Arc, ) -> Self { let executor = RskExecutor::new(hardfork_cfg.clone(), block_store.clone()); + let chain_config = Arc::new(match hardfork_cfg.chain_id { + crate::hardfork::RSK_MAINNET_CHAIN_ID => rustock_core::config::ChainConfig::mainnet(), + crate::hardfork::RSK_TESTNET_CHAIN_ID => rustock_core::config::ChainConfig::testnet(), + _ => rustock_core::config::ChainConfig::regtest(), + }); Self { executor, hardfork_cfg, block_store, validate_fork_detection_data: false, sender_cache: None, + chain_config, } } + /// The chain's consensus constants, for a node whose chain is not exactly + /// the one its chain id names: the configuration the rest of the node + /// validates headers with. + pub fn with_chain_config(mut self, config: Arc) -> Self { + self.chain_config = config; + self + } + /// Share a sender cache with whoever else recovers senders -- the pool, /// above all. Handing the same `Arc` to both is the point: a transaction /// the pool admitted should not be recovered a second time when the @@ -683,7 +701,10 @@ impl BlockProcessor { block: &Block, parent: Option<&rustock_core::Header>, ) -> Result<(), ValidationError> { - use rustock_core::validation::{HeaderValidator, ParentHeaderValidator}; + use rustock_core::validation::uncles::{ + rsk_header_rules, rsk_parent_rules, UncleForkDetection, UnclesValidationRule, + UNCLE_GENERATION_LIMIT, UNCLE_LIST_LIMIT, + }; let number = block.header.number; @@ -698,16 +719,27 @@ impl BlockProcessor { self.validate_block_body_rules(block, parent)?; // rskj BlockUnclesValidationRule. The ommers HASH is checked in - // process_block; this decides whether the uncles are admissible. + // process_block; this decides whether the uncles are admissible, each + // put through the rules rskj gives the rule: its own proof of work, + // timestamps and gas used, and against its own parent the minimum gas + // price, number, timestamp, difficulty and gas limit. An uncle's + // difficulty counts toward the chain's work, so one checked less hard + // than a trunk block is work credited that nobody did. let ancestry = self.ancestry(); - let header_rules: Vec> = Vec::new(); - let parent_rules: Vec> = Vec::new(); - rustock_core::validation::uncles::UnclesValidationRule { + let header_rules = rsk_header_rules(self.chain_config.clone()); + let parent_rules = rsk_parent_rules(self.chain_config.clone()); + UnclesValidationRule { store: &ancestry, - uncle_list_limit: rustock_core::validation::uncles::UNCLE_LIST_LIMIT, - uncle_generation_limit: rustock_core::validation::uncles::UNCLE_GENERATION_LIMIT, + uncle_list_limit: UNCLE_LIST_LIMIT, + uncle_generation_limit: UNCLE_GENERATION_LIMIT, header_rules: &header_rules, parent_rules: &parent_rules, + // Under the same switch as the block's own fork-detection check + // below, and for the same cost: 449 headers per uncle. + fork_detection: self.validate_fork_detection_data.then_some(UncleForkDetection { + view: &ancestry, + rskip110_height: self.rskip110_height(), + }), } .validate(block)?; @@ -2839,6 +2871,165 @@ mod tests { let validated = processor.process_block(&block, &root, store).unwrap(); assert_eq!(validated.gas_used, 0); } + + /// Merge-mine `header` as a miner would, committing to `fork_data` after + /// its merged-mining hash, beside a Bitcoin header of its own time. Its + /// difficulty must be 1, which any Bitcoin hash meets. + fn seal(header: &mut Header, fork_data: &[u8]) { + use crate::mining::{coinbase, fork_detection, merkle}; + use rustock_core::validation::merged_mining::compress_coinbase; + + let payload = fork_detection::apply_to_hash(header.hash_for_merged_mining(), fork_data); + let cb = coinbase::build_coinbase(&payload, 0); + let mut btc = coinbase::build_bitcoin_block(cb.clone(), 0); + btc.header.time = header.timestamp as u32; + let compressed = compress_coinbase(&coinbase::encode_transaction(&cb), true).unwrap(); + let proof = merkle::proof_from_txids(&coinbase::txids_display_order(&btc)).unwrap(); + header.bitcoin_merged_mining_header = Some(coinbase::encode_header(&btc.header).into()); + header.bitcoin_merged_mining_coinbase_transaction = Some(compressed.into()); + header.bitcoin_merged_mining_merkle_proof = Some(proof.into()); + header.cached_hash = None; + } + + /// Regtest's constants with rskj's timestamp rule on, so every rule an + /// uncle faces runs. + fn uncle_config() -> Arc { + let mut config = rustock_core::config::ChainConfig::regtest(); + config.max_future_block_time = 60; + Arc::new(config) + } + + /// A header at `number` on `parent`, at `timestamp` and difficulty 1. + fn child_of(number: u64, parent: &Header, timestamp: u64) -> Header { + let mut header = dummy_header(number); + header.parent_hash = parent.hash(); + header.timestamp = timestamp; + header.difficulty = U256::from(1); + header + } + + /// `parent`'s child at `number` naming `uncle`, its REMASC transaction + /// in place, sealed with `fork_data`. + fn naming(number: u64, parent: &Header, uncle: &Header, fork_data: &[u8]) -> Block { + let mut header = child_of(number, parent, parent.timestamp + 10); + header.uncle_count = 1; + header.ommers_hash = compute_ommers_hash(std::slice::from_ref(uncle)); + seal(&mut header, fork_data); + Block { + header, + transactions: vec![crate::mining::remasc_transaction(number)], + ommers: vec![uncle.clone()], + } + } + + /// The import path's uncle rule holds each uncle to rskj's rules. It used + /// to check only an uncle's place in the chain, so a block could name one + /// that did no work and have its difficulty counted all the same. + #[test] + fn an_uncle_without_proof_of_work_fails_the_import_rules() { + let dir = tempfile::tempdir().unwrap(); + let block_store = Arc::new(BlockStore::open(dir.path()).unwrap()); + let processor = BlockProcessor::new(test_hardfork_cfg(), block_store.clone()) + .with_chain_config(uncle_config()); + + let mut genesis = dummy_header(0); + genesis.difficulty = U256::from(1); + let b1 = child_of(1, &genesis, 1_700_000_010); + let b2 = child_of(2, &b1, 1_700_000_030); + for header in [&genesis, &b1, &b2] { + block_store.put_header(header).unwrap(); + } + + // b2's sibling: a child of b1, ten seconds on, at the difficulty b1 + // calls for on regtest. + let mut uncle = child_of(2, &b1, 1_700_000_020); + uncle.beneficiary = Address::repeat_byte(0x0b); + + assert_eq!( + processor.validate_block_rules(&naming(3, &b2, &uncle, &[]), Some(&b2)), + Err(ValidationError::BitcoinHeaderDecodeError), + "an uncle with no proof of work must not pass" + ); + + // Work done for another header is not its work either: this proof + // commits to the uncle as it was before its beneficiary changed. + let mut stolen = uncle.clone(); + seal(&mut stolen, &[]); + stolen.beneficiary = Address::repeat_byte(0x0c); + assert_eq!( + processor.validate_block_rules(&naming(3, &b2, &stolen, &[]), Some(&b2)), + Err(ValidationError::BitcoinCoinbaseTagInvalid) + ); + + seal(&mut uncle, &[]); + assert_eq!(processor.validate_block_rules(&naming(3, &b2, &uncle, &[]), Some(&b2)), Ok(())); + + // Proof of work is not all: against its parent it must come later. + let mut early = child_of(2, &b1, b1.timestamp); + early.beneficiary = Address::repeat_byte(0x0b); + seal(&mut early, &[]); + assert!(matches!( + processor.validate_block_rules(&naming(3, &b2, &early, &[]), Some(&b2)), + Err(ValidationError::TimestampOlderThanParent { .. }) + )); + } + + /// With fork detection on, an uncle must carry the fork-detection data of + /// its own parent, read from the 449 headers below that parent as rskj's + /// `ForkDetectionDataRule` reads them. With it off, as for a bulk replay, + /// the data is not looked at. + #[test] + fn with_fork_detection_on_an_uncle_carries_its_own_parents_data() { + use rustock_core::validation::fork_detection::{calculate, REQUIRED_BLOCKS}; + + let dir = tempfile::tempdir().unwrap(); + let block_store = Arc::new(BlockStore::open(dir.path()).unwrap()); + let processor = |fork_detection: bool| { + BlockProcessor::new(test_hardfork_cfg(), block_store.clone()) + .with_chain_config(uncle_config()) + .with_fork_detection_validation(fork_detection) + }; + + // A trunk long enough to read fork-detection data from, each header + // merge-mined so the commit-to-parents bytes have hashes to come from. + let mut genesis = dummy_header(0); + genesis.difficulty = U256::from(1); + let mut chain = vec![genesis]; + for number in 1..=REQUIRED_BLOCKS + 2 { + let parent = chain.last().unwrap(); + let mut header = child_of(number, parent, parent.timestamp + 10); + seal(&mut header, &[]); + chain.push(header); + } + for header in &chain { + block_store.put_header(header).unwrap(); + } + // Newest first, from `tip` down, as the rule reads them. + let data_from = |tip: usize| { + let view: Vec
= chain[..=tip].iter().rev().cloned().collect(); + calculate(&view).expect("449 headers to read") + }; + + let tip = chain.len() - 1; + let parent = &chain[tip]; + let uncle_parent = &chain[tip - 2]; + let mut uncle = child_of(uncle_parent.number + 1, uncle_parent, uncle_parent.timestamp + 5); + uncle.beneficiary = Address::repeat_byte(0x0b); + let mut right = uncle.clone(); + seal(&mut right, &data_from(tip - 2)); + // The data its sibling carries: read from one block too high. + let mut wrong = uncle; + seal(&mut wrong, &data_from(tip - 1)); + + let number = parent.number + 1; + let block = |uncle: &Header| naming(number, parent, uncle, &data_from(tip)); + assert_eq!(processor(true).validate_block_rules(&block(&right), Some(parent)), Ok(())); + assert!(matches!( + processor(true).validate_block_rules(&block(&wrong), Some(parent)), + Err(ValidationError::ForkDetectionDataMismatch { .. }) + )); + assert_eq!(processor(false).validate_block_rules(&block(&wrong), Some(parent)), Ok(())); + } } #[cfg(test)] diff --git a/crates/execution/tests/mainnet_block_rules.rs b/crates/execution/tests/mainnet_block_rules.rs index 427fddc..3da59e5 100644 --- a/crates/execution/tests/mainnet_block_rules.rs +++ b/crates/execution/tests/mainnet_block_rules.rs @@ -91,6 +91,32 @@ fn rskip179_accepts_every_sampled_mainnet_block() { assert!(failures.is_empty(), "RSKIP179 rejects real mainnet blocks: {failures:?}"); } +/// Whether the store holds the `depth` headers below a block at `number` on +/// `parent_hash`: what the uncle rule walks for it. +/// +/// A pruned store, a light node's, lets go of the oldest headers, and a block +/// just above that edge names uncles whose parents, or whose chain, are gone. +/// rskj holds the whole chain and would judge them; here they can only be +/// skipped. +fn ancestry_held( + store: &BlockStore, + number: u64, + parent_hash: alloy_primitives::B256, + depth: u64, +) -> bool { + let floor = number.saturating_sub(depth); + let mut cursor = parent_hash; + loop { + let Some(header) = store.header(cursor).ok().flatten() else { + return false; + }; + if header.number <= floor || header.number == 0 { + return true; + } + cursor = header.parent_hash; + } +} + #[test] #[ignore = "requires a synced block store (RUSTOCK_BLOCKS_DB)"] fn block_rules_accept_every_sampled_mainnet_block() { @@ -157,10 +183,13 @@ fn block_rules_accept_every_sampled_mainnet_block() { /// The uncle rules only mean something on blocks that actually carry uncles, /// which the uniform sample above mostly misses. Walk a dense range and assert -/// every uncle-bearing block still passes. +/// every uncle-bearing block still passes, each uncle held to rskj's rules, +/// fork detection included: its 449 headers start at its own parent. #[test] #[ignore = "requires a synced block store (RUSTOCK_BLOCKS_DB)"] fn uncle_rules_accept_real_uncle_bearing_blocks() { + use rustock_core::validation::{fork_detection::REQUIRED_BLOCKS, uncles::UNCLE_GENERATION_LIMIT}; + let Some(path) = db_path() else { eprintln!("RUSTOCK_BLOCKS_DB not set; skipping"); return; @@ -171,10 +200,12 @@ fn uncle_rules_accept_real_uncle_bearing_blocks() { .expect("head") .and_then(|h| store.header(h).ok().flatten()) .expect("head header"); - let processor = BlockProcessor::new(RskHardforkConfig::mainnet(), store.clone()); + let processor = BlockProcessor::new(RskHardforkConfig::mainnet(), store.clone()) + .with_fork_detection_validation(true); let start = head.number.saturating_sub(20_000); let mut with_uncles = 0usize; + let mut pruned = 0usize; let mut failures: Vec<(u64, String)> = Vec::new(); for number in start..head.number { @@ -183,6 +214,13 @@ fn uncle_rules_accept_real_uncle_bearing_blocks() { if block.ommers.is_empty() { continue; } + // An uncle's parent sits up to seven generations down, and its fork + // detection reads 449 headers from there. + let depth = UNCLE_GENERATION_LIMIT + REQUIRED_BLOCKS; + if !ancestry_held(&store, number, block.header.parent_hash, depth) { + pruned += 1; + continue; + } with_uncles += 1; let parent = store.header(block.header.parent_hash).ok().flatten(); if let Err(e) = processor.validate_block_rules(&block, parent.as_ref()) { @@ -190,7 +228,10 @@ fn uncle_rules_accept_real_uncle_bearing_blocks() { } } - println!("uncle-bearing blocks checked: {with_uncles}, failures {}", failures.len()); + println!( + "uncle-bearing blocks checked: {with_uncles}, failures {}, skipped with their ancestry pruned {pruned}", + failures.len() + ); for (number, err) in failures.iter().take(25) { println!(" #{number}: {err}"); } diff --git a/crates/sync/src/total_difficulty.rs b/crates/sync/src/total_difficulty.rs index abb41ee..c3db87d 100644 --- a/crates/sync/src/total_difficulty.rs +++ b/crates/sync/src/total_difficulty.rs @@ -112,6 +112,7 @@ pub(crate) fn credited_uncles( uncle_generation_limit: UNCLE_GENERATION_LIMIT, header_rules: verifier.static_rules(), parent_rules: verifier.parent_rules(), + fork_detection: None, } .validate(&block) .map_err(|e| e.to_string())?; diff --git a/crates/sync/src/uncle_guard.rs b/crates/sync/src/uncle_guard.rs index c10e254..f7c87b7 100644 --- a/crates/sync/src/uncle_guard.rs +++ b/crates/sync/src/uncle_guard.rs @@ -169,6 +169,7 @@ impl UncleGuard { uncle_generation_limit: UNCLE_GENERATION_LIMIT, header_rules: verifier.static_rules(), parent_rules: verifier.parent_rules(), + fork_detection: None, }; let block = rustock_core::types::block::Block { header: header.clone(), From d7f20f32e534afa890c49dcf32a97af63216e572 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Fri, 6 Nov 2026 09:43:48 -0800 Subject: [PATCH 014/264] sync: judge uncles by rskj's uncle rules, not a trunk header's The sync's three uncle checks put each uncle through the trunk HeaderVerifier's rules: crediting a held body's uncles to its total difficulty, UncleGuard on uncles an rsk/63 peer sends with its headers, and the snapshot ascent. Those are not the rules rskj gives its uncle rule. They hold an uncle to a trunk block's gas-limit bounds and 32-byte extra-data cap, which rskj does not, so an uncle rskj counts could go uncredited. HeaderVerifier now carries rskj's uncle rules beside the trunk's: default_rsk fills them from uncles::rsk_header_rules and rsk_parent_rules, the lists block validation uses. total_difficulty and UncleGuard run them, and the ascent, which does not hold an uncle's parent, runs the rules an uncle faces on its own (verify_uncle). Fork detection, which reads 449 headers per uncle, is not run in these paths. Tests: default_rsk passes an uncle with 40 bytes of extra data and a gas limit above regtest's trunk cap, which its trunk rules refuse, and refuses one five minutes from its Bitcoin header both as an uncle and as a trunk header, since RSKIP179 holds for both. Crediting and UncleGuard are each shown to refuse an uncle that fails either an uncle rule or an uncle parent rule, and UncleGuard to credit the same uncle when nothing refuses it. Tests that stood a trunk rule in for an uncle's proof of work now give it as an uncle rule. 7 mutants, all caught. --- crates/core/src/validation/mod.rs | 58 ++++++++-- crates/core/src/validation/tests.rs | 24 ++++ crates/sync/src/snap/forward.rs | 4 +- crates/sync/src/snap/forward_tests.rs | 2 +- crates/sync/src/tests.rs | 151 +++++++++++++++----------- crates/sync/src/total_difficulty.rs | 17 +-- crates/sync/src/uncle_guard.rs | 13 ++- 7 files changed, 182 insertions(+), 87 deletions(-) diff --git a/crates/core/src/validation/mod.rs b/crates/core/src/validation/mod.rs index 432d739..2057c38 100644 --- a/crates/core/src/validation/mod.rs +++ b/crates/core/src/validation/mod.rs @@ -168,6 +168,10 @@ impl BlockVerifier { pub struct HeaderVerifier { static_rules: Vec>, parent_rules: Vec>, + /// The rules an uncle passes, which are not a trunk header's: rskj gives + /// its uncle rule lists of its own. + uncle_rules: Vec>, + uncle_parent_rules: Vec>, } impl Default for HeaderVerifier { @@ -181,10 +185,14 @@ impl HeaderVerifier { Self { static_rules: Vec::new(), parent_rules: Vec::new(), + uncle_rules: Vec::new(), + uncle_parent_rules: Vec::new(), } } - /// Creates a standard RSK verifier with all consensus rules. + /// Creates a standard RSK verifier with all consensus rules, and rskj's + /// rules for uncles, [`uncles::rsk_header_rules`] and + /// [`uncles::rsk_parent_rules`]. /// /// Note: `ParentHashRule` is intentionally **not** included here because /// the sync pipeline looks up the parent by `header.parent_hash`, so @@ -193,7 +201,7 @@ impl HeaderVerifier { /// from Java's non-canonical RLP encoding (leading zeros in difficulty) /// and cannot be reproduced by our standard `Header::hash()`. pub fn default_rsk(config: std::sync::Arc) -> Self { - Self::new() + let mut verifier = Self::new() .with_static_rule(GasUsedRule) .with_static_rule(GasLimitBoundsRule { min_gas_limit: config.min_gas_limit, @@ -207,28 +215,60 @@ impl HeaderVerifier { .with_parent_rule(TimestampRule::new(config.max_future_block_time)) .with_parent_rule(BlockParentGasLimitRule { config: config.clone() }) .with_parent_rule(block_rules::PrevMinGasPriceRule) - .with_parent_rule(DifficultyRule { config }) + .with_parent_rule(DifficultyRule { config: config.clone() }); + verifier.uncle_rules = uncles::rsk_header_rules(config.clone()); + verifier.uncle_parent_rules = uncles::rsk_parent_rules(config); + verifier } - /// The rules that judge a header on its own, proof of work among them. - /// - /// Exposed so an uncle can be put through exactly the same rules as a - /// trunk header. An uncle that is not checked as hard as a trunk block is - /// a way to credit work nobody did. + /// The rules that judge a trunk header on its own, proof of work among + /// them. pub fn static_rules(&self) -> &[Box] { &self.static_rules } - /// The rules that judge a header against its parent. + /// The rules that judge a trunk header against its parent. pub fn parent_rules(&self) -> &[Box] { &self.parent_rules } + /// The rules that judge an uncle on its own: rskj's, which are not a + /// trunk header's. An uncle checked less hard than rskj checks it is work + /// credited that nobody did; one checked harder is work rskj counts and + /// this node does not. + pub fn uncle_rules(&self) -> &[Box] { + &self.uncle_rules + } + + /// The rules that judge an uncle against its own parent. + pub fn uncle_parent_rules(&self) -> &[Box] { + &self.uncle_parent_rules + } + + /// Runs the rules an uncle faces on its own, for a caller that does not + /// hold its parent. + pub fn verify_uncle(&self, uncle: &Header) -> Result<(), ValidationError> { + for rule in &self.uncle_rules { + rule.validate(uncle)?; + } + Ok(()) + } + pub fn with_static_rule(mut self, rule: impl HeaderValidator + 'static) -> Self { self.static_rules.push(Box::new(rule)); self } + pub fn with_uncle_rule(mut self, rule: impl HeaderValidator + 'static) -> Self { + self.uncle_rules.push(Box::new(rule)); + self + } + + pub fn with_uncle_parent_rule(mut self, rule: impl ParentHeaderValidator + 'static) -> Self { + self.uncle_parent_rules.push(Box::new(rule)); + self + } + pub fn with_parent_rule(mut self, rule: impl ParentHeaderValidator + 'static) -> Self { self.parent_rules.push(Box::new(rule)); self diff --git a/crates/core/src/validation/tests.rs b/crates/core/src/validation/tests.rs index da996d6..7a5f257 100644 --- a/crates/core/src/validation/tests.rs +++ b/crates/core/src/validation/tests.rs @@ -896,3 +896,27 @@ fn the_uncle_rule_runs_fork_detection_when_asked() { ); assert_eq!(judge(&ancestry, &block, uncle, None), Ok(())); } + +/// The verifier every sync path holds judges uncles by rskj's uncle rules, +/// not by a trunk header's: no extra-data cap or gas-limit bounds. The +/// timestamp rule, RSKIP179 included, holds for both. +#[test] +fn default_rsk_holds_uncles_to_rskjs_uncle_rules() { + let verifier = HeaderVerifier::default_rsk(uncle_config()); + let (_, parent, _) = uncle_chain(0); + + let mut chatty = uncle_of(&parent, 1010); + chatty.extra_data = Bytes::from(vec![0xab; 40]); + chatty.gas_limit = U256::from(20_000_000); + seal(&mut chatty, 1010); + assert_eq!(verifier.verify_uncle(&chatty), Ok(())); + assert!(verifier.verify(&chatty, None).is_err(), "a trunk header may not carry either"); + + let mut adrift = uncle_of(&parent, 1010); + seal(&mut adrift, 1010 + 300); + for verdict in [verifier.verify_uncle(&adrift), verifier.verify(&adrift, None)] { + assert!(matches!(verdict, Err(ValidationError::BitcoinTimestampTooFar { .. }))); + } + + assert_eq!(verifier.uncle_parent_rules().len(), 5, "rskj's five, in its order"); +} diff --git a/crates/sync/src/snap/forward.rs b/crates/sync/src/snap/forward.rs index b107099..d483a34 100644 --- a/crates/sync/src/snap/forward.rs +++ b/crates/sync/src/snap/forward.rs @@ -482,7 +482,9 @@ impl ForwardSync { } for uncle in &entry.uncles { let hash = uncle.hash(); - self.verifier.verify(uncle, None).map_err(|e| { + // An uncle's own rules, which are not a trunk header's; its + // parent is not held here, so only the rules that need none. + self.verifier.verify_uncle(uncle).map_err(|e| { AscentError::UncleProofOfWork { number: entry.header.number, hash, diff --git a/crates/sync/src/snap/forward_tests.rs b/crates/sync/src/snap/forward_tests.rs index 3854f3b..fa58d86 100644 --- a/crates/sync/src/snap/forward_tests.rs +++ b/crates/sync/src/snap/forward_tests.rs @@ -157,7 +157,7 @@ impl HeaderValidator for RejectMarked { #[test] fn an_uncle_without_proof_of_work_is_refused() { let anchor = header(0, B256::ZERO, 100); - let (mut s, _d) = sync(&anchor, 2, HeaderVerifier::new().with_static_rule(RejectMarked)); + let (mut s, _d) = sync(&anchor, 2, HeaderVerifier::new().with_uncle_rule(RejectMarked)); let entries = run_of(1, 2, anchor.hash(), 100, |n| { if n == 1 { vec![header(0, B256::repeat_byte(5), 666)] } else { Vec::new() } diff --git a/crates/sync/src/tests.rs b/crates/sync/src/tests.rs index 3e0e05d..e27d91c 100644 --- a/crates/sync/src/tests.rs +++ b/crates/sync/src/tests.rs @@ -6824,7 +6824,8 @@ async fn forged_uncles_do_not_prove_a_difficulty() { ); } -/// An uncle the header *does* commit to still has to have done the work. +/// An uncle the header *does* commit to still has to have done the work, and +/// to follow its parent as rskj's uncle rule requires. /// /// A matching commitment only proves the block's miner chose this list. The /// uncle's own merged-mining proof of work is what makes the difficulty it @@ -6833,57 +6834,57 @@ async fn forged_uncles_do_not_prove_a_difficulty() { #[tokio::test] async fn a_committed_uncle_without_proof_of_work_proves_nothing() { use rustock_networking::protocol::HeaderWithUncles; - let dir = tempfile::tempdir().unwrap(); - let store = Arc::new(BlockStore::open(dir.path()).unwrap()); - let genesis = dummy_header(0, B256::ZERO, U256::from(1)); - let genesis_hash = genesis.hash(); - store.update_head(&genesis, U256::from(1)).unwrap(); - - /// Rejects the marked header, standing in for a failed merged-mining check. - struct RejectMarked; - impl rustock_core::validation::HeaderValidator for RejectMarked { - fn validate( - &self, - h: &rustock_core::Header, - ) -> Result<(), rustock_core::validation::ValidationError> { - if h.difficulty == U256::from(999u64) { - return Err(rustock_core::validation::ValidationError::BitcoinPowInvalid { - hash: h.hash(), - target: U256::ZERO, - }); - } - Ok(()) - } - } - let verifier = Arc::new( - HeaderVerifier::new() - .with_static_rule(RejectMarked) + let verifiers = [ + (HeaderVerifier::new(), 1_020u64), + (HeaderVerifier::new().with_uncle_rule(NoWorkAt999), 21), + (HeaderVerifier::new().with_uncle_parent_rule(OffItsParentAt999), 21), + ]; + for (verifier, total) in verifiers { + let dir = tempfile::tempdir().unwrap(); + let store = Arc::new(BlockStore::open(dir.path()).unwrap()); + let genesis = dummy_header(0, B256::ZERO, U256::from(1)); + let genesis_hash = genesis.hash(); + store.update_head(&genesis, U256::from(1)).unwrap(); + let verifier = verifier .with_parent_rule(rustock_core::validation::BlockNumberRule) - .with_parent_rule(rustock_core::validation::ParentHashRule), - ); - let peer_store = Arc::new(rustock_networking::peers::PeerStore::new()); - let manager = SyncManager::new(store.clone(), verifier, peer_store); - - let uncle = dummy_header(0, B256::ZERO, U256::from(999)); - let mut b1 = dummy_header(1, genesis_hash, U256::from(10)); - b1.uncle_count = 1; - let entry = HeaderWithUncles { header: b1, uncles: vec![uncle] }; - let mut header = entry.header.clone(); - header.ommers_hash = alloy_primitives::keccak256(entry.encoded_uncles()); - header.cached_hash = None; - let entry = HeaderWithUncles { header, uncles: entry.uncles }; - assert!(entry.commitment_matches(), "the commitment is honest; the work is not"); - - let hash = entry.header.hash(); - let headers = manager.note_proven_difficulties(vec![entry]); - manager.handle_headers_response(headers, test_peer()).unwrap(); + .with_parent_rule(rustock_core::validation::ParentHashRule); + let peer_store = Arc::new(rustock_networking::peers::PeerStore::new()); + let manager = SyncManager::new(store.clone(), Arc::new(verifier), peer_store); + + // #1 on the trunk, then #2 naming a genuine sibling of #1 worth 999: + // its parent is an ancestor, so with no rule against it, it counts. + let commit = |h: &Header, us: &[Header]| -> Header { + let mut h = h.clone(); + h.uncle_count = us.len() as u64; + h.ommers_hash = alloy_primitives::keccak256( + HeaderWithUncles { header: h.clone(), uncles: us.to_vec() }.encoded_uncles(), + ); + h.cached_hash = None; + h + }; + let b1 = commit(&dummy_header(1, genesis_hash, U256::from(10)), &[]); + let mut uncle = dummy_header(1, genesis_hash, U256::from(999)); + uncle.gas_used = 1; + uncle.cached_hash = None; + let b2 = + commit(&dummy_header(2, b1.hash(), U256::from(10)), std::slice::from_ref(&uncle)); + let hash = b2.hash(); + let entry = HeaderWithUncles { header: b2, uncles: vec![uncle] }; + assert!(entry.commitment_matches(), "the commitment is honest; the work is not"); + + let headers = manager.note_proven_difficulties(vec![ + HeaderWithUncles { header: b1, uncles: Vec::new() }, + entry, + ]); + manager.handle_headers_response(headers, test_peer()).unwrap(); - assert_eq!( - store.total_difficulty(hash).unwrap(), - Some(U256::from(11)), - "a committed uncle with no proof of work must not be credited" - ); + assert_eq!( + store.total_difficulty(hash).unwrap(), + Some(U256::from(total)), + "an uncle is credited exactly when it passes the uncle rules" + ); + } } // --------------------------------------------------------------------------- @@ -6918,6 +6919,26 @@ impl rustock_core::validation::HeaderValidator for NoWorkAt999 { } } +/// Fails the header whose difficulty is 999 against its parent, standing in +/// for an uncle whose difficulty is not the one its parent calls for. +struct OffItsParentAt999; + +impl rustock_core::validation::ParentHeaderValidator for OffItsParentAt999 { + fn validate_with_parent( + &self, + h: &Header, + _parent: &Header, + ) -> Result<(), rustock_core::validation::ValidationError> { + if h.difficulty == U256::from(999u64) { + return Err(rustock_core::validation::ValidationError::DifficultyMismatch { + expected: U256::from(10u64), + got: h.difficulty, + }); + } + Ok(()) + } +} + /// Genesis held whole with a total of 1; #1 on it, worth 10; and two siblings /// of #1 for a later block to name as uncles, worth `first` and `second`. fn genesis_and_siblings(store: &BlockStore, first: u64, second: u64) -> (Header, Vec
) { @@ -7001,21 +7022,27 @@ async fn a_caught_up_batch_counts_the_uncles_its_bodies_bring() { assert_eq!(store.total_difficulty(b3.hash()).unwrap(), Some(U256::from(43))); } -/// A body's uncles count only once they pass the whole uncle rule, proof of -/// work included. The body check on receipt shows only that the block's miner -/// chose them, and an uncle that never did its work would otherwise add what -/// it claims to every total above it. +/// A body's uncles count only once they pass the whole uncle rule: the rules +/// an uncle faces on its own, proof of work among them, and those it faces +/// against its parent. The body check on receipt shows only that the block's +/// miner chose them, and an uncle that never did its work would otherwise add +/// what it claims to every total above it. #[tokio::test] async fn a_caught_up_body_whose_uncle_did_no_work_adds_no_uncle() { - let dir = tempdir().unwrap(); - let store = Arc::new(BlockStore::open(dir.path()).unwrap()); - let (b1, uncles) = genesis_and_siblings(&store, 7, 999); - let verifier = HeaderVerifier::new().with_static_rule(NoWorkAt999); - let [_, b2, b3] = caught_up(&store, verifier, b1, &uncles).await; + let verifiers = [ + HeaderVerifier::new().with_uncle_rule(NoWorkAt999), + HeaderVerifier::new().with_uncle_parent_rule(OffItsParentAt999), + ]; + for verifier in verifiers { + let dir = tempdir().unwrap(); + let store = Arc::new(BlockStore::open(dir.path()).unwrap()); + let (b1, uncles) = genesis_and_siblings(&store, 7, 999); + let [_, b2, b3] = caught_up(&store, verifier, b1, &uncles).await; - // Neither uncle: rskj refuses the whole block over one. - assert_eq!(store.total_difficulty(b2.hash()).unwrap(), Some(U256::from(21))); - assert_eq!(store.total_difficulty(b3.hash()).unwrap(), Some(U256::from(31))); + // Neither uncle: rskj refuses the whole block over one. + assert_eq!(store.total_difficulty(b2.hash()).unwrap(), Some(U256::from(21))); + assert_eq!(store.total_difficulty(b3.hash()).unwrap(), Some(U256::from(31))); + } } /// In follow mode a block's total is settled from the executed head's when its @@ -7063,7 +7090,7 @@ async fn a_header_ingested_again_credits_only_uncles_that_did_their_work() { let store = Arc::new(BlockStore::open(dir.path()).unwrap()); let (b1, uncles) = genesis_and_siblings(&store, 7, second); let b2 = naming_uncles(dummy_header(2, b1.hash(), U256::from(10)), &uncles); - let verifier = Arc::new(HeaderVerifier::new().with_static_rule(NoWorkAt999)); + let verifier = Arc::new(HeaderVerifier::new().with_uncle_rule(NoWorkAt999)); let peer_store = Arc::new(rustock_networking::peers::PeerStore::new()); let manager = SyncManager::new(store.clone(), verifier, peer_store); manager.handle_headers_response(vec![b2.clone(), b1.clone()], test_peer()).unwrap(); diff --git a/crates/sync/src/total_difficulty.rs b/crates/sync/src/total_difficulty.rs index c3db87d..cbfadf3 100644 --- a/crates/sync/src/total_difficulty.rs +++ b/crates/sync/src/total_difficulty.rs @@ -10,12 +10,13 @@ //! makes a block's total exact once its body is held, in the order the chain //! runs, so that each block starts from a parent whose total is exact already. //! -//! An uncle is credited only once it passes rskj's whole uncle rule against the -//! ancestry this node holds, with every rule a trunk header faces, proof of work -//! included: the standard [`crate::uncle_guard`] holds uncles from the wire to. -//! The body check on receipt proves only that the list is the one the block's -//! header commits to, that its miner chose those uncles, not that each did the -//! work it claims. An uncle that fails is not credited, which leaves the total +//! An uncle is credited only once it passes rskj's uncle rule against the +//! ancestry this node holds, with the rules rskj gives that rule for each +//! uncle: proof of work, timestamps, difficulty and gas limit against its own +//! parent. Not fork detection, which reads 449 headers per uncle. The body +//! check on receipt proves only that the list is the one the block's header +//! commits to, that its miner chose those uncles, not that each did the work +//! it claims. An uncle that fails is not credited, which leaves the total //! understated, the safe direction, rather than overstated. use alloy_primitives::{B256, U256}; @@ -110,8 +111,8 @@ pub(crate) fn credited_uncles( store: &held, uncle_list_limit: UNCLE_LIST_LIMIT, uncle_generation_limit: UNCLE_GENERATION_LIMIT, - header_rules: verifier.static_rules(), - parent_rules: verifier.parent_rules(), + header_rules: verifier.uncle_rules(), + parent_rules: verifier.uncle_parent_rules(), fork_detection: None, } .validate(&block) diff --git a/crates/sync/src/uncle_guard.rs b/crates/sync/src/uncle_guard.rs index f7c87b7..6b48710 100644 --- a/crates/sync/src/uncle_guard.rs +++ b/crates/sync/src/uncle_guard.rs @@ -133,10 +133,11 @@ impl UncleGuard { /// Whether this block's uncles may be credited with their difficulty. /// - /// Runs rskj's whole `BlockUnclesValidationRule`: the list limit, the - /// generation limit, no repeats, no ancestor, nothing already used by an - /// ancestor, a parent that is itself an ancestor, and every per-header and - /// parent-relative rule a trunk header faces -- proof of work included. + /// Runs rskj's `BlockUnclesValidationRule`: the list limit, the generation + /// limit, no repeats, no ancestor, nothing already used by an ancestor, a + /// parent that is itself an ancestor, and the rules rskj gives it for each + /// uncle -- proof of work, timestamps, difficulty and gas limit against its + /// own parent. Not fork detection, which reads 449 headers per uncle. /// /// Returns the reason when it refuses, for the caller to log. pub fn uncles_are_admissible( @@ -167,8 +168,8 @@ impl UncleGuard { store: &source, uncle_list_limit: UNCLE_LIST_LIMIT, uncle_generation_limit: UNCLE_GENERATION_LIMIT, - header_rules: verifier.static_rules(), - parent_rules: verifier.parent_rules(), + header_rules: verifier.uncle_rules(), + parent_rules: verifier.uncle_parent_rules(), fork_detection: None, }; let block = rustock_core::types::block::Block { From 85c22630bb9a2874c9f6fb64faa38ee5e68d2db8 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Sun, 8 Nov 2026 08:28:54 -0800 Subject: [PATCH 015/264] Hold trunk headers to rskj's rules: timestamp, proof of work Trunk headers take rskj's timestamp rule on the header alone, as BlockValidatorImpl and the header sync run it: default_rsk holds TimestampRule as a static rule, not a parent rule. Its parent form is for uncles and for rskj's relay; a block no later than its parent is one rskj imports, so refusing it parted this node from the chain the network follows. MergedMiningRule, as rskj's ProofOfWorkRule: - the merkle root compares in rskj's byte order only. Either order used to pass, so a proof folding to the root's byte reversal did too; - the coinbase must run past 64 bytes: the midstate's count, read as a Java long, plus the tail; - every fold of the proof runs rskj's checkNotAValid64ByteTransaction; - the midstate resumes as BouncyCastle's does, padding from the tail's first byte and the length wrapping. A count of other than whole blocks dropped the length block, and one past i64 overflowed; - a UMM root is empty or 20 bytes; - below block 449 a coinbase commits to the whole hash, RSKIP110 or not, which decides testnet's and regtest's first blocks. The node's block processor checks fork-detection data, as rskj's ForkDetectionDataRule does on every block and uncle; the startup log says whether it is on. The tests' Bitcoin header held its merkle root reversed, which passed only because either order did. examples/verify_stored_headers walks a store's headers through default_rsk and reports failures rule by rule. --- crates/cli/examples/loadtest.rs | 7 +- crates/cli/examples/verify_stored_headers.rs | 169 ++++++++ crates/cli/src/main.rs | 15 +- crates/core/Cargo.toml | 3 + crates/core/src/validation/header_rules.rs | 7 + crates/core/src/validation/merged_mining.rs | 131 +++++- crates/core/src/validation/mod.rs | 15 +- crates/core/src/validation/tests.rs | 396 ++++++++++++++++++- crates/execution/src/mining/merkle.rs | 5 +- crates/execution/src/mining/tests.rs | 4 +- crates/execution/src/processor.rs | 5 + 11 files changed, 725 insertions(+), 32 deletions(-) create mode 100644 crates/cli/examples/verify_stored_headers.rs diff --git a/crates/cli/examples/loadtest.rs b/crates/cli/examples/loadtest.rs index 05d6827..eb4b2cf 100644 --- a/crates/cli/examples/loadtest.rs +++ b/crates/cli/examples/loadtest.rs @@ -779,6 +779,7 @@ fn remasc_tx() -> Transaction { /// is a handful of hashes rather than a search. Verification does the /// same work it would against a real mainnet target. fn seal_merged_mining(header: &mut Header) -> Result<()> { + use rustock_core::validation::fork_detection::REQUIRED_BLOCKS; use rustock_core::validation::merged_mining::compress_coinbase; use rustock_execution::mining::{coinbase, fork_detection, merkle}; @@ -788,7 +789,11 @@ fn seal_merged_mining(header: &mut Header) -> Result<()> { // hash is the one from before the transaction root was set. header.cached_hash = None; header.cached_hash_for_merged_mining = None; - let payload = fork_detection::apply_to_hash(header.hash_for_merged_mining(), &[0u8; 12]); + // From block 449 the hash's last twelve bytes are fork-detection data, + // and any twelve do here: this measures proof of work, not fork + // detection. Below 449 rskj wants the whole hash. + let fork_data: &[u8] = if header.number >= REQUIRED_BLOCKS { &[0u8; 12] } else { &[] }; + let payload = fork_detection::apply_to_hash(header.hash_for_merged_mining(), fork_data); let cb = coinbase::build_coinbase(&payload, 0); let btc = coinbase::build_bitcoin_block(cb.clone(), 0); diff --git a/crates/cli/examples/verify_stored_headers.rs b/crates/cli/examples/verify_stored_headers.rs new file mode 100644 index 0000000..631b34c --- /dev/null +++ b/crates/cli/examples/verify_stored_headers.rs @@ -0,0 +1,169 @@ +//! Verify every header in a store by `HeaderVerifier::default_rsk`, the +//! trunk rules the node syncs by, and report what fails, rule by rule. +//! +//! Each header is checked on its own and against its parent, ranges of the +//! chain split across threads, walking back by parent hash within each. The +//! store is opened read-only, so this can run against a copy of a node's +//! data or the store of a node that is stopped. +//! +//! It also counts blocks no later than their parent, which rskj's trunk +//! takes and its relay would not: zero is what an honest chain shows. +//! +//! Usage: verify_stored_headers [lowest-block] [highest-block] + +use anyhow::Context; +use rustock_core::config::ChainConfig; +use rustock_core::validation::HeaderVerifier; +use rustock_storage::BlockStore; +use std::collections::BTreeMap; +use std::sync::atomic::{AtomicU64, Ordering}; +use std::sync::{Arc, Mutex}; +use std::time::Instant; + +/// How many failing block numbers to keep per rule. +const EXAMPLES: usize = 8; + +#[derive(Default)] +struct Failures { + count: u64, + blocks: Vec, + first: String, +} + +fn main() -> anyhow::Result<()> { + let mut args = std::env::args().skip(1); + let dir = args + .next() + .context("usage: verify_stored_headers [lowest-block] [highest-block]")?; + let store = BlockStore::open_read_only(&dir)?; + let head_hash = store.head()?.context("the store has no head")?; + let head = store.header(head_hash)?.context("the head header is missing")?; + let lowest: u64 = args.next().map(|s| s.parse()).transpose()?.unwrap_or(1).max(1); + let highest: u64 = args.next().map(|s| s.parse()).transpose()?.unwrap_or(head.number); + anyhow::ensure!(lowest <= highest, "#{lowest} is above #{highest}"); + + let verifier = HeaderVerifier::default_rsk(Arc::new(ChainConfig::mainnet())); + let threads = std::thread::available_parallelism().map(|n| n.get() as u64).unwrap_or(4); + let span = (highest - lowest + 1).div_ceil(threads); + println!( + "store {dir}\n head #{}\n verifying #{highest} down to #{lowest} on {threads} threads", + head.number + ); + + let failures: Mutex> = Mutex::new(BTreeMap::new()); + let checked = AtomicU64::new(0); + let not_later = AtomicU64::new(0); + // Headers whose parent the store lacks, checked on their own, and + // canonical numbers the store has no header for at all. + let orphaned = AtomicU64::new(0); + let missing = AtomicU64::new(0); + let started = Instant::now(); + + std::thread::scope(|scope| -> anyhow::Result<()> { + let mut workers = Vec::new(); + for t in 0..threads { + let Some(top) = highest.checked_sub(t * span) else { break }; + if top < lowest { + break; + } + let bottom = top.saturating_sub(span - 1).max(lowest); + let (store, verifier, failures, checked, not_later, orphaned, missing) = + (&store, &verifier, &failures, &checked, ¬_later, &orphaned, &missing); + workers.push(scope.spawn(move || -> anyhow::Result<()> { + // The highest block from `number` down to this range's bottom + // that the store holds, counting the numbers it does not. + let held_from = |mut number: u64| -> anyhow::Result> { + loop { + let found = match store.canonical_hash(number)? { + Some(hash) => store.header(hash)?, + None => None, + }; + if found.is_some() { + return Ok(found); + } + missing.fetch_add(1, Ordering::Relaxed); + if number <= bottom { + return Ok(None); + } + number -= 1; + } + }; + let Some(mut header) = held_from(top)? else { return Ok(()) }; + loop { + let parent = store.header(header.parent_hash)?; + if let Err(e) = verifier.verify(&header, parent.as_ref()) { + let debug = format!("{e:?}"); + let rule = debug + .split(|c: char| !c.is_alphanumeric()) + .next() + .unwrap_or_default() + .to_string(); + let mut failures = failures.lock().unwrap(); + let entry = failures.entry(rule).or_default(); + entry.count += 1; + if entry.blocks.len() < EXAMPLES { + entry.blocks.push(header.number); + } + if entry.first.is_empty() { + entry.first = debug; + } + } + match &parent { + Some(parent) if header.timestamp <= parent.timestamp => { + not_later.fetch_add(1, Ordering::Relaxed); + } + Some(_) => {} + None => { + orphaned.fetch_add(1, Ordering::Relaxed); + } + } + let done = checked.fetch_add(1, Ordering::Relaxed) + 1; + if done % 500_000 == 0 { + println!(" {done} checked, {:.0}s", started.elapsed().as_secs_f64()); + } + if header.number <= bottom { + break; + } + header = match parent { + Some(parent) => parent, + // A hole in the store: go on from the next block below + // it that the store holds. + None => match held_from(header.number - 1)? { + Some(found) => found, + None => return Ok(()), + }, + }; + } + Ok(()) + })); + } + for worker in workers { + worker.join().expect("a worker panicked")?; + } + Ok(()) + })?; + + let checked = checked.into_inner(); + let failures = failures.into_inner().unwrap(); + let failed: u64 = failures.values().map(|f| f.count).sum(); + println!( + "\nverified {checked} headers in {:.0}s: {failed} failed", + started.elapsed().as_secs_f64() + ); + for (rule, f) in &failures { + println!(" {rule}: {} -- e.g. #{:?}\n first: {}", f.count, f.blocks, f.first); + } + println!( + " blocks no later than their parent: {}", + not_later.into_inner() + ); + let (orphaned, missing) = (orphaned.into_inner(), missing.into_inner()); + println!(" checked without their parent, which the store lacks: {orphaned}"); + println!(" canonical numbers with no header in the store: {missing}"); + let expected = highest - lowest + 1; + anyhow::ensure!( + checked + missing == expected, + "checked {checked} and {missing} missing, expected {expected} in all" + ); + Ok(()) +} diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index bfb80ee..d345730 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -2583,11 +2583,19 @@ async fn run(local_offset: Option) -> Result<()> { // The same chain configuration the header verifier uses: an uncle is // held to the rules a header is, on this chain's constants. + // + // Fork detection on, as rskj runs `ForkDetectionDataRule` on every block + // it imports and on every uncle. It reads the 449 headers below each + // one's parent: a few milliseconds a block, which a node following the + // chain can spare and a bulk replay -- the reason it is a switch at all -- + // turns off for itself. The headers are here: the walk writes those under + // the checkpoint, and pruning keeps thousands more than 449. let mut block_processor = rustock_execution::BlockProcessor::new( hardfork_cfg.clone(), store.clone(), ) - .with_chain_config(config.clone()); + .with_chain_config(config.clone()) + .with_fork_detection_validation(true); if let Some(cache) = &btc_block_cache { block_processor = block_processor.with_btc_block_cache(cache.clone()); } @@ -2603,9 +2611,10 @@ async fn run(local_offset: Option) -> Result<()> { ); } info!( - "Block processor wired (hardfork config: {:?}, execution threads: {})", + "Block processor wired (hardfork config: {:?}, execution threads: {}, fork detection: {})", hardfork_cfg, - block_processor.execution_threads() + block_processor.execution_threads(), + block_processor.validates_fork_detection() ); if let Some(count) = args.measure_block_reads { diff --git a/crates/core/Cargo.toml b/crates/core/Cargo.toml index ebe5a81..fd7a19c 100644 --- a/crates/core/Cargo.toml +++ b/crates/core/Cargo.toml @@ -32,3 +32,6 @@ thiserror = { workspace = true } # a cross-check between two independent implementations rather than a # round trip through one. Production code no longer uses k256 at all. k256 = { workspace = true, features = ["ecdsa"] } +# The merged-mining tests check this crate's own SHA-256 compression, which +# resumes from a Bitcoin coinbase's midstate, against `sha2`'s. +sha2 = { workspace = true, features = ["compress"] } diff --git a/crates/core/src/validation/header_rules.rs b/crates/core/src/validation/header_rules.rs index 81a6d69..bbc4e06 100644 --- a/crates/core/src/validation/header_rules.rs +++ b/crates/core/src/validation/header_rules.rs @@ -44,6 +44,13 @@ impl ParentHeaderValidator for ParentHashRule { /// parent comparison goes with it (`validPeriodLength == 0` returns true /// from both `isValid` overloads), which is what lets a regtest replay /// blocks whose clock has nothing to do with the node's. +/// +/// rskj runs the parent form on uncles, and in its relay and snapshot +/// validators. `BlockValidatorImpl`, which decides whether a block joins the +/// chain, runs the rule on the header alone, as its header sync does: a +/// block no later than its parent is one rskj imports. So +/// [`HeaderVerifier::default_rsk`](super::HeaderVerifier::default_rsk) holds +/// a trunk header to the clock alone, and an uncle to its parent too. pub struct TimestampRule { max_future_offset: u64, } diff --git a/crates/core/src/validation/merged_mining.rs b/crates/core/src/validation/merged_mining.rs index de9a12d..d7bfcc9 100644 --- a/crates/core/src/validation/merged_mining.rs +++ b/crates/core/src/validation/merged_mining.rs @@ -1,3 +1,4 @@ +use super::fork_detection::REQUIRED_BLOCKS; use super::{HeaderValidator, ValidationError}; use crate::types::header::Header; use crate::config::ChainConfig; @@ -30,6 +31,12 @@ pub const MAX_MERGED_MINING_MERKLE_PROOF_LENGTH: usize = 960; /// `RskMiningConstants.BTC_HEADER_SIZE`, enforced by `BtcHeaderSizeRule`. pub const BTC_HEADER_SIZE: usize = 80; +/// The length of a UMM root when a header has one: rskj +/// `BlockHeader.UMM_LEAVES_LENGTH`. Any other length but none makes rskj's +/// `getHashRootForMergedMining` throw, so the header has no merged-mining +/// hash and its proof of work cannot be checked. +pub const UMM_ROOT_LENGTH: usize = 20; + pub struct MergedMiningRule { pub config: std::sync::Arc, } @@ -157,9 +164,23 @@ impl HeaderValidator for MergedMiningRule { let tail = &compressed[MIDSTATE_SIZE_TRIMMED..]; + // rskj `BlockHeader.getHashRootForMergedMining`: a UMM root is empty or + // exactly 20 bytes, and any other length throws before there is a hash + // to look for. + if let Some(umm_root) = &header.umm_root { + if !umm_root.is_empty() && umm_root.len() != UMM_ROOT_LENGTH { + return Err(ValidationError::UmmRootInvalid { len: umm_root.len() }); + } + } let rsk_hash = header.hash_for_merged_mining(); - let include_fork_detection = header.number >= self.config.activation_heights.wasabi100; + // rskj `BlockFactory`: the merged-mining hash carries fork-detection + // data from RSKIP110 (wasabi100) on, but never below block 449, where + // there is not yet the chain to compute it from. Testnet and regtest + // have RSKIP110 from genesis, so there it is the second bound that + // decides: their first 449 blocks commit to the whole hash. + let include_fork_detection = header.number >= self.config.activation_heights.wasabi100 + && header.number >= REQUIRED_BLOCKS; let expected_tag: Vec = if include_fork_detection { [RSK_TAG, &rsk_hash.as_slice()[..HASH_FOR_MERGED_MINING_PREFIX_LENGTH]].concat() } else { @@ -187,6 +208,16 @@ impl HeaderValidator for MergedMiningRule { return Err(ValidationError::BitcoinCoinbaseTagInvalid); } + // rskj: the whole coinbase -- the bytes the midstate absorbed, a Java + // `long` read from its first eight, and the tail -- must run past 64 + // bytes. 64 bytes is the shape of an inner node of the Bitcoin merkle + // tree, which must not pass for a transaction. + let absorbed = i64::from_be_bytes(compressed[..8].try_into().expect("eight bytes")); + let length = (tail.len() as i64).wrapping_add(absorbed); + if length <= 64 { + return Err(ValidationError::CoinbaseTooShort { length }); + } + // 3. Compute coinbase hash from SHA-256 midstate + tail, verify Merkle proof let coinbase_hash = compute_coinbase_hash(compressed); @@ -210,16 +241,15 @@ impl HeaderValidator for MergedMiningRule { return Err(ValidationError::BitcoinMerkleProofDecodeError); } - let computed_root = rskip92_merkle_root(&coinbase_hash, merkle_proof_bytes); - - let btc_merkle_root_bytes = btc_header.merkle_root.to_byte_array(); - - if computed_root != btc_merkle_root_bytes { - let mut reversed = computed_root; - reversed.reverse(); - if reversed != btc_merkle_root_bytes { - return Err(ValidationError::BitcoinMerkleProofInvalid); - } + // rskj compares in bitcoinj's display order, reversed from the wire, + // which is the order the computed root is in; the header holds its + // root in wire order. Either order matching used to pass, so a proof + // folding to the root's byte reversal did too. + let mut computed_root = rskip92_merkle_root(&coinbase_hash, merkle_proof_bytes) + .ok_or(ValidationError::MerkleNodesFormTransaction)?; + computed_root.reverse(); + if computed_root != btc_header.merkle_root.to_byte_array() { + return Err(ValidationError::BitcoinMerkleProofInvalid); } Ok(()) @@ -409,15 +439,24 @@ pub fn find_first_subsequence(haystack: &[u8], needle: &[u8]) -> Option { haystack.windows(needle.len()).position(|w| w == needle) } -/// Completes a SHA-256 hash from a midstate. +/// Completes a SHA-256 hash from a midstate, as BouncyCastle's `SHA256Digest` +/// does when rskj restores one. +/// +/// Restoring a state starts BouncyCastle's word and block offsets at zero, +/// so the tail is absorbed from a block boundary whatever the byte count +/// says; the count only feeds the length in the padding, which is the count +/// plus the tail's length, times eight, in a Java `long`. For a coinbase +/// compressed honestly the count is whole blocks and none of this shows. A +/// count that is not is the miner's to write, and rskj hashes it this way, +/// so it is hashed this way here too: laying the padding out from the count +/// instead left the length block short, and `chunks_exact` dropped it. pub fn sha256_from_midstate(state: [u32; 8], byte_count: u64, tail: &[u8]) -> [u8; 32] { let mut h = state; - let total_len = byte_count + tail.len() as u64; - let total_bits = total_len * 8; + let total_bits = byte_count.wrapping_add(tail.len() as u64).wrapping_mul(8); let mut padded = tail.to_vec(); padded.push(0x80); - while (byte_count as usize + padded.len()) % 64 != 56 { + while padded.len() % 64 != 56 { padded.push(0); } padded.extend_from_slice(&total_bits.to_be_bytes()); @@ -438,15 +477,73 @@ pub fn sha256_from_midstate(state: [u32; 8], byte_count: u64, tail: &[u8]) -> [u /// hashes. Starting from the coinbase hash, combine left-to-right with each /// sibling using Bitcoin's double-SHA256 Merkle tree construction, matching /// rskj's `combineLeftRight`. -pub fn rskip92_merkle_root(coinbase_hash: &[u8; 32], proof_bytes: &[u8]) -> [u8; 32] { +/// +/// `None` when two nodes it would combine parse as a 64-byte Bitcoin +/// transaction, where rskj's `combineLeftRight` throws instead. +pub fn rskip92_merkle_root(coinbase_hash: &[u8; 32], proof_bytes: &[u8]) -> Option<[u8; 32]> { let mut current = *coinbase_hash; for chunk in proof_bytes.chunks_exact(32) { let sibling: [u8; 32] = chunk.try_into().unwrap(); + if parses_as_a_64_byte_transaction(¤t, &sibling) { + return None; + } current = combine_left_right(¤t, &sibling); } - current + Some(current) +} + +/// The most a single output can carry, in satoshis: 21 million bitcoin. +const MAX_SATOSHIS: i64 = 21_000_000 * 100_000_000; + +/// rskj `MerkleTreeUtils.checkNotAValid64ByteTransaction`, its guard against +/// the leaf-node weakness of Bitcoin's merkle tree: an inner node is the hash +/// of 64 bytes, and 64 bytes that also parse as a transaction would let a +/// proof stop at a node and present it as a leaf. +/// +/// True when `left || right` passes every test rskj applies -- one input, an +/// output index of at most a million or the coinbase's `0xffffffff`, one +/// output worth between one satoshi and 21 million bitcoin, and scripts of +/// four bytes between them -- which is when rskj throws. It reads the nodes +/// in the order it holds them, display order, the reverse of the bytes the +/// tree hashes. That is rskj's rule, so it is this one. +pub fn parses_as_a_64_byte_transaction(left: &[u8; 32], right: &[u8; 32]) -> bool { + let mut tx = [0u8; 64]; + tx[..32].copy_from_slice(left); + tx[32..].copy_from_slice(right); + + // Four bytes of version, then the input count. + if tx[4] != 1 { + return false; + } + // The outpoint's 32-byte hash, then its index: an unsigned little-endian + // 32-bit integer. + let output_index = u32::from_le_bytes(tx[37..41].try_into().unwrap()); + if output_index > 1_000_000 && output_index != u32::MAX { + return false; + } + // A Java byte: from 0x80 it is negative, refused with the rest over four. + let input_script_length = tx[41]; + if input_script_length > 4 { + return false; + } + // The script, four bytes of sequence, then the output count. + let mut at = 42 + usize::from(input_script_length) + 4; + if tx[at] != 1 { + return false; + } + at += 1; + let value = i64::from_le_bytes(tx[at..at + 8].try_into().unwrap()); + if !(1..=MAX_SATOSHIS).contains(&value) { + return false; + } + at += 8; + let output_script_length = tx[at]; + if output_script_length > 4 { + return false; + } + input_script_length + output_script_length == 4 } /// Matches rskj's MerkleTreeUtils.combineLeftRight: diff --git a/crates/core/src/validation/mod.rs b/crates/core/src/validation/mod.rs index 2057c38..d1a9f5e 100644 --- a/crates/core/src/validation/mod.rs +++ b/crates/core/src/validation/mod.rs @@ -61,6 +61,15 @@ pub enum ValidationError { #[error("Merged-mining merkle proof is {got} bytes, over the {max}-byte limit")] MerkleProofTooLarge { max: usize, got: usize }, + #[error("Merged-mining coinbase is {length} bytes long, must be over 64")] + CoinbaseTooShort { length: i64 }, + + #[error("Two nodes of the merged-mining merkle proof parse as a 64-byte Bitcoin transaction")] + MerkleNodesFormTransaction, + + #[error("UMM root is {len} bytes, must be empty or 20")] + UmmRootInvalid { len: usize }, + #[error("Tx gas price {tx_gas_price} is below the block minimum {block_minimum}")] TxGasPriceBelowMinimum { tx_gas_price: U256, block_minimum: U256 }, @@ -209,10 +218,12 @@ impl HeaderVerifier { }) .with_static_rule(MergedMiningRule { config: config.clone() }) .with_static_rule(BitcoinTimestampRule { config: config.clone() }) + // rskj `newBlockMaxSecondsInTheFuture`; zero turns the rule off. On + // the header alone, as rskj's `BlockValidatorImpl` and its header + // sync run it: a block no later than its parent is one rskj imports. + .with_static_rule(TimestampRule::new(config.max_future_block_time)) .with_static_rule(block_rules::ExtraDataRule::default()) .with_parent_rule(BlockNumberRule) - // rskj `newBlockMaxSecondsInTheFuture`; zero turns the rule off. - .with_parent_rule(TimestampRule::new(config.max_future_block_time)) .with_parent_rule(BlockParentGasLimitRule { config: config.clone() }) .with_parent_rule(block_rules::PrevMinGasPriceRule) .with_parent_rule(DifficultyRule { config: config.clone() }); diff --git a/crates/core/src/validation/tests.rs b/crates/core/src/validation/tests.rs index 7a5f257..e9967de 100644 --- a/crates/core/src/validation/tests.rs +++ b/crates/core/src/validation/tests.rs @@ -81,12 +81,13 @@ fn build_mm_proof(header: &Header, btc_bits: u32, rsk_tag_hash: Option) -> } compressed.extend_from_slice(&coinbase_bytes); - // Coinbase txid = double-SHA256 of raw tx, byte-reversed. + // A one-transaction block's merkle root is its coinbase's txid, in the + // internal byte order a Bitcoin header holds: double SHA-256 as it + // comes, not reversed for display. let first_hash = Sha256::digest(&coinbase_bytes); let txid_bytes = Sha256::digest(first_hash); let mut merkle_root_bytes = [0u8; 32]; merkle_root_bytes.copy_from_slice(&txid_bytes); - merkle_root_bytes.reverse(); let merkle_root = bitcoin::TxMerkleNode::from_byte_array(merkle_root_bytes); let btc_header = BtcHeader { @@ -153,6 +154,9 @@ fn test_gas_used_rule() { header.gas_used = 90; assert!(rule.validate(&header).is_ok()); + header.gas_used = 100; + assert!(rule.validate(&header).is_ok()); + header.gas_used = 110; assert!(matches!(rule.validate(&header), Err(ValidationError::GasUsedExceedsLimit { .. }))); } @@ -898,7 +902,7 @@ fn the_uncle_rule_runs_fork_detection_when_asked() { } /// The verifier every sync path holds judges uncles by rskj's uncle rules, -/// not by a trunk header's: no extra-data cap or gas-limit bounds. The +/// not by a trunk header's: no extra-data cap and no gas-limit bounds. The /// timestamp rule, RSKIP179 included, holds for both. #[test] fn default_rsk_holds_uncles_to_rskjs_uncle_rules() { @@ -907,10 +911,21 @@ fn default_rsk_holds_uncles_to_rskjs_uncle_rules() { let mut chatty = uncle_of(&parent, 1010); chatty.extra_data = Bytes::from(vec![0xab; 40]); - chatty.gas_limit = U256::from(20_000_000); seal(&mut chatty, 1010); assert_eq!(verifier.verify_uncle(&chatty), Ok(())); - assert!(verifier.verify(&chatty, None).is_err(), "a trunk header may not carry either"); + assert!(matches!( + verifier.verify(&chatty, None), + Err(ValidationError::ExtraDataTooLarge { .. }) + )); + + let mut thin = uncle_of(&parent, 1010); + thin.gas_limit = U256::from(2_999_999); + seal(&mut thin, 1010); + assert_eq!(verifier.verify_uncle(&thin), Ok(())); + assert_eq!( + verifier.verify(&thin, None), + Err(ValidationError::GasLimitOutOfBounds { min: 3_000_000, max: 10_000_000, got: 2_999_999 }) + ); let mut adrift = uncle_of(&parent, 1010); seal(&mut adrift, 1010 + 300); @@ -920,3 +935,374 @@ fn default_rsk_holds_uncles_to_rskjs_uncle_rules() { assert_eq!(verifier.uncle_parent_rules().len(), 5, "rskj's five, in its order"); } + +/// rskj's trunk takes the timestamp rule on the header alone. A block no +/// later than its parent is one `BlockValidatorImpl` imports -- the parent +/// form is for uncles, and for rskj's relay -- so this node must take it +/// too, or part from the chain the network follows. +#[test] +fn a_block_no_later_than_its_parent_joins_the_trunk_but_is_no_uncle() { + let verifier = HeaderVerifier::default_rsk(uncle_config()); + let (_, parent, _) = uncle_chain(0); + + let mut same = uncle_of(&parent, parent.timestamp); + seal(&mut same, parent.timestamp); + assert_eq!(verifier.verify(&same, Some(&parent)), Ok(())); + + let older = Err(ValidationError::TimestampOlderThanParent { + parent: parent.timestamp, + got: parent.timestamp, + }); + assert!(verifier + .uncle_parent_rules() + .iter() + .any(|rule| rule.validate_with_parent(&same, &parent) == older)); +} + +// --- the merged-mining proof, byte for byte as rskj's `ProofOfWorkRule` + +/// A regtest header at `number`, at difficulty 1, where any Bitcoin hash +/// meets the target and only the coinbase and the merkle proof are in +/// question. +fn header_at(number: u64) -> Header { + let mut header = create_dummy_header(number, 1000, B256::ZERO); + header.difficulty = U256::from(1); + header +} + +/// A coinbase in the compressed form a header carries: the SHA-256 initial +/// state as its midstate, claiming `absorbed` bytes, then `tail`. +fn compressed_coinbase(absorbed: u64, tail: &[u8]) -> Vec { + let mut out = absorbed.to_be_bytes().to_vec(); + for word in merged_mining::SHA256_INITIAL_STATE { + out.extend_from_slice(&word.to_be_bytes()); + } + out.extend_from_slice(tail); + out +} + +/// A coinbase tail committing to `committed`: `before` bytes, the tag, the +/// 32 committed bytes, then `after` bytes. +fn tail_committing_to(committed: &[u8], before: usize, after: usize) -> Vec { + let mut tail = vec![0x11; before]; + tail.extend_from_slice(merged_mining::RSK_TAG); + tail.extend_from_slice(committed); + tail.resize(tail.len() + after, 0x22); + tail +} + +/// `header` merge-mined through `coinbase` and `proof`, by a Bitcoin header +/// holding `wire_root` as its merkle root. +fn mined_through(mut header: Header, coinbase: Vec, proof: Vec, wire_root: [u8; 32]) -> Header { + let btc = BtcHeader { + version: bitcoin::block::Version::from_consensus(1), + prev_blockhash: bitcoin::BlockHash::all_zeros(), + merkle_root: bitcoin::TxMerkleNode::from_byte_array(wire_root), + time: 1000, + bits: bitcoin::CompactTarget::from_consensus(0x207fffff), + nonce: 0, + }; + header.bitcoin_merged_mining_header = Some(Bytes::from(bitcoin::consensus::serialize(&btc))); + header.bitcoin_merged_mining_coinbase_transaction = Some(Bytes::from(coinbase)); + header.bitcoin_merged_mining_merkle_proof = Some(Bytes::from(proof)); + header +} + +/// A root as a Bitcoin header holds it, for one folded in display order. +fn wire(display: [u8; 32]) -> [u8; 32] { + let mut out = display; + out.reverse(); + out +} + +fn regtest_merged_mining() -> MergedMiningRule { + MergedMiningRule { config: Arc::new(crate::config::ChainConfig::regtest()) } +} + +/// rskj compares the root its proof folds to in bitcoinj's display order, +/// against the root the Bitcoin header holds in wire order. A proof folding +/// to the root's byte reversal proves nothing, and passed while either order +/// did. +#[test] +fn the_merkle_root_is_compared_in_rskjs_byte_order_only() { + let header = header_at(10); + let tail = tail_committing_to(header.hash_for_merged_mining().as_slice(), 10, 20); + let coinbase = compressed_coinbase(0, &tail); + // No siblings: the root is the coinbase's own hash. + let root = merged_mining::compute_coinbase_hash(&coinbase); + assert_ne!(root, wire(root)); + + let right = mined_through(header.clone(), coinbase.clone(), Vec::new(), wire(root)); + assert_eq!(regtest_merged_mining().validate(&right), Ok(())); + let reversed = mined_through(header, coinbase, Vec::new(), root); + assert_eq!( + regtest_merged_mining().validate(&reversed), + Err(ValidationError::BitcoinMerkleProofInvalid) + ); +} + +/// rskj: a coinbase runs past 64 bytes -- the count its midstate claims, read +/// as a Java `long`, plus its tail -- so it cannot pass for an inner node of +/// the merkle tree. +#[test] +fn the_coinbase_must_run_past_64_bytes() { + let header = header_at(10); + let hash = header.hash_for_merged_mining(); + let judge = |absorbed: u64, after: usize| { + let coinbase = compressed_coinbase(absorbed, &tail_committing_to(hash.as_slice(), 0, after)); + let root = merged_mining::compute_coinbase_hash(&coinbase); + regtest_merged_mining().validate(&mined_through(header.clone(), coinbase, Vec::new(), wire(root))) + }; + + // The tag and the hash are 41 bytes of tail. + assert_eq!(judge(0, 23), Err(ValidationError::CoinbaseTooShort { length: 64 })); + assert_eq!(judge(0, 24), Ok(())); + assert_eq!(judge(23, 0), Err(ValidationError::CoinbaseTooShort { length: 64 })); + assert_eq!(judge(24, 0), Ok(())); + // `u64::MAX` is -1 to a Java `long`, and takes a byte off. + assert_eq!(judge(u64::MAX, 24), Err(ValidationError::CoinbaseTooShort { length: 64 })); + assert_eq!(judge(u64::MAX, 25), Ok(())); + // A count that carries the sum past `i64::MAX` wraps it negative. + assert_eq!( + judge(i64::MAX as u64, 25), + Err(ValidationError::CoinbaseTooShort { length: i64::MAX.wrapping_add(66) }) + ); +} + +/// The coinbase hash as BouncyCastle computes it from a restored midstate, +/// worked out with `sha2`'s compression function rather than this crate's: +/// the padding laid out from the tail's first byte, and the length the count +/// plus the tail, in bits, wrapping as a Java `long` does. Display order. +// `sha2` 0.10 takes its blocks as generic-array 0.14 arrays, which that +// crate now marks deprecated in favour of a 1.x `sha2` has yet to adopt. +#[allow(deprecated)] +fn bouncycastle_coinbase_hash(absorbed: u64, tail: &[u8]) -> [u8; 32] { + use sha2::digest::generic_array::GenericArray; + + let mut padded = tail.to_vec(); + padded.push(0x80); + while padded.len() % 64 != 56 { + padded.push(0); + } + let bits = absorbed.wrapping_add(tail.len() as u64).wrapping_mul(8); + padded.extend_from_slice(&bits.to_be_bytes()); + let mut state = merged_mining::SHA256_INITIAL_STATE; + for block in padded.chunks_exact(64) { + sha2::compress256(&mut state, &[*GenericArray::from_slice(block)]); + } + let mut once = [0u8; 32]; + for (bytes, word) in once.chunks_exact_mut(4).zip(state) { + bytes.copy_from_slice(&word.to_be_bytes()); + } + let mut hash: [u8; 32] = Sha256::digest(once).into(); + hash.reverse(); + hash +} + +/// rskj resumes a coinbase's hash with BouncyCastle, which restores the +/// midstate's byte count but starts its blocks afresh at the tail. A count +/// that is not whole blocks is the miner's to write, and the hash must be +/// the one rskj computes from it, or this node refuses a block rskj takes. +#[test] +fn a_coinbase_hashes_from_its_midstate_as_bouncycastle_does() { + // With nothing absorbed the midstate is SHA-256's start, and the hash is + // plain double SHA-256 of the tail, at every length the padding turns on. + for len in 0..=130usize { + let tail: Vec = (0..len).map(|i| i as u8).collect(); + let mut expected: [u8; 32] = Sha256::digest(Sha256::digest(&tail)).into(); + expected.reverse(); + assert_eq!( + merged_mining::compute_coinbase_hash(&compressed_coinbase(0, &tail)), + expected, + "{len} bytes of tail" + ); + } + // Counts of whole blocks and not, and ones that wrap the length. + let tail: Vec = (0..100u8).collect(); + for absorbed in [64, 1, 63, 100, u64::MAX, u64::MAX - 99, 1 << 61] { + assert_eq!( + merged_mining::compute_coinbase_hash(&compressed_coinbase(absorbed, &tail)), + bouncycastle_coinbase_hash(absorbed, &tail), + "{absorbed} bytes absorbed" + ); + } + + // So a block whose coinbase claims one byte absorbed is one the rule + // takes, when its Bitcoin header commits to the hash rskj computes. + let header = header_at(10); + let tail = tail_committing_to(header.hash_for_merged_mining().as_slice(), 0, 24); + let root = bouncycastle_coinbase_hash(1, &tail); + let mined = mined_through(header, compressed_coinbase(1, &tail), Vec::new(), wire(root)); + assert_eq!(regtest_merged_mining().validate(&mined), Ok(())); +} + +/// Two merkle nodes, as rskj's `checkNotAValid64ByteTransaction` reads them +/// -- left then right, display order -- that parse as a transaction with one +/// input, one output of a satoshi, and scripts of `input` and `output` bytes. +/// The left node only has to supply the input count, its fifth byte. +fn transaction_shaped(input: u8, output: u8) -> ([u8; 32], [u8; 32]) { + let mut tx = [0x55u8; 64]; + tx[4] = 1; + tx[37..41].copy_from_slice(&0u32.to_le_bytes()); + tx[41] = input; + let at = 46 + usize::from(input); + tx[at] = 1; + tx[at + 1..at + 9].copy_from_slice(&1i64.to_le_bytes()); + tx[at + 9] = output; + (tx[..32].try_into().unwrap(), tx[32..].try_into().unwrap()) +} + +/// rskj `MerkleTreeUtils.checkNotAValid64ByteTransaction`: each test it +/// makes, at its bounds and past them. +#[test] +fn rskj_refuses_merkle_nodes_that_parse_as_a_transaction() { + use merged_mining::parses_as_a_64_byte_transaction as parses; + + for input in 0..=4 { + let (left, right) = transaction_shaped(input, 4 - input); + assert!(parses(&left, &right), "{input} + {} bytes of script", 4 - input); + } + for (input, output) in [(1, 2), (2, 3)] { + let (left, right) = transaction_shaped(input, output); + assert!(!parses(&left, &right), "{input} + {output} bytes of script"); + } + + // The shape with two bytes of script each way, with `bytes` written at + // `at`. + let edited = |at: usize, bytes: &[u8]| { + let (left, right) = transaction_shaped(2, 2); + let mut tx = [0u8; 64]; + tx[..32].copy_from_slice(&left); + tx[32..].copy_from_slice(&right); + tx[at..at + bytes.len()].copy_from_slice(bytes); + parses(&tx[..32].try_into().unwrap(), &tx[32..].try_into().unwrap()) + }; + // One input. + assert!(!edited(4, &[0])); + assert!(!edited(4, &[2])); + // Spending an output at most a million in, or the coinbase's. + assert!(edited(37, &1_000_000u32.to_le_bytes())); + assert!(!edited(37, &1_000_001u32.to_le_bytes())); + assert!(edited(37, &u32::MAX.to_le_bytes())); + assert!(!edited(37, &(u32::MAX - 1).to_le_bytes())); + // Script lengths over four, or negative as a Java byte. + for length in [5, 0x80, 0xff] { + assert!(!edited(41, &[length]), "input script of {length:#x}"); + assert!(!edited(57, &[length]), "output script of {length:#x}"); + } + // One output. + assert!(!edited(48, &[2])); + // Worth a satoshi to 21 million bitcoin. + for (value, parses_as_one) in [ + (0i64, false), + (-1, false), + (2_100_000_000_000_000, true), + (2_100_000_000_000_001, false), + ] { + assert_eq!(edited(49, &value.to_le_bytes()), parses_as_one, "{value} satoshis"); + } +} + +/// rskj runs the guard at every fold of the proof, before any root is +/// compared, so a block whose coinbase hash and first sibling parse as a +/// transaction is no block at all. +#[test] +fn a_merkle_proof_through_a_transaction_shaped_pair_is_refused() { + let header = header_at(10); + let hash = header.hash_for_merged_mining(); + // A coinbase whose hash supplies the input count, found by varying what + // follows the commitment. + let (coinbase, coinbase_hash) = (0..=u16::MAX) + .find_map(|salt| { + let mut tail = tail_committing_to(hash.as_slice(), 0, 24); + let end = tail.len(); + tail[end - 2..].copy_from_slice(&salt.to_be_bytes()); + let coinbase = compressed_coinbase(0, &tail); + let coinbase_hash = merged_mining::compute_coinbase_hash(&coinbase); + (coinbase_hash[4] == 1).then_some((coinbase, coinbase_hash)) + }) + .expect("a coinbase hash with 1 as its fifth byte"); + let (_, sibling) = transaction_shaped(2, 2); + + assert!(merged_mining::parses_as_a_64_byte_transaction(&coinbase_hash, &sibling)); + assert_eq!(merged_mining::rskip92_merkle_root(&coinbase_hash, &sibling), None); + let root = merged_mining::combine_left_right(&coinbase_hash, &sibling); + let mined = mined_through(header.clone(), coinbase.clone(), sibling.to_vec(), wire(root)); + assert_eq!( + regtest_merged_mining().validate(&mined), + Err(ValidationError::MerkleNodesFormTransaction) + ); + + // A byte more of output script and the pair is no transaction: the same + // block, with the root that sibling folds to, is merge-mined. + let mut other = sibling; + other[57 - 32] = 3; + assert!(!merged_mining::parses_as_a_64_byte_transaction(&coinbase_hash, &other)); + let root = merged_mining::combine_left_right(&coinbase_hash, &other); + let mined = mined_through(header, coinbase, other.to_vec(), wire(root)); + assert_eq!(regtest_merged_mining().validate(&mined), Ok(())); +} + +/// rskj `BlockHeader.getHashRootForMergedMining`: a UMM root is 20 bytes or +/// none, and any other length throws before there is a hash to look for. +#[test] +fn a_umm_root_is_twenty_bytes_or_none() { + for (len, valid) in [(0, true), (20, true), (19, false), (21, false), (32, false)] { + let mut header = header_at(10); + header.umm_root = Some(Bytes::from(vec![0x0c; len])); + seal(&mut header, 1000); + let verdict = regtest_merged_mining().validate(&header); + if valid { + assert_eq!(verdict, Ok(()), "{len} bytes"); + } else { + assert_eq!(verdict, Err(ValidationError::UmmRootInvalid { len }), "{len} bytes"); + } + } +} + +/// rskj puts fork-detection data in the merged-mining hash from RSKIP110 +/// (wasabi100) on, and never below block 449. Where it does not, a coinbase +/// commits to the whole hash, and twelve bytes of anything else after its +/// first twenty commit to nothing. +#[test] +fn fork_detection_data_joins_the_hash_from_wasabi100_and_block_449() { + use crate::config::{ActivationHeights, ChainConfig}; + + // Regtest has RSKIP110 from genesis, so there the height alone decides; + // with wasabi100 at 1,000, it is wasabi100 that does. + let late = ChainConfig { + activation_heights: ActivationHeights { wasabi100: 1_000, ..ActivationHeights::regtest() }, + ..ChainConfig::regtest() + }; + for (config, number, altered_valid) in [ + (ChainConfig::regtest(), 448, false), + (ChainConfig::regtest(), 449, true), + (late.clone(), 999, false), + (late, 1_000, true), + ] { + let rule = MergedMiningRule { config: Arc::new(config) }; + let header = header_at(number); + let hash = header.hash_for_merged_mining(); + let mut altered = hash; + altered[20..].copy_from_slice(&[0xfd; 12]); + let judge = |committed: B256| { + let mut mined = header.clone(); + let (btc, coinbase, proof) = build_mm_proof(&mined, 0x207fffff, Some(committed)); + mined.bitcoin_merged_mining_header = Some(btc); + mined.bitcoin_merged_mining_coinbase_transaction = Some(coinbase); + mined.bitcoin_merged_mining_merkle_proof = Some(proof); + rule.validate(&mined) + }; + + assert_eq!(judge(hash), Ok(()), "block {number}, the whole hash"); + if altered_valid { + assert_eq!(judge(altered), Ok(()), "block {number}, fork-detection data"); + } else { + assert_eq!( + judge(altered), + Err(ValidationError::BitcoinCoinbaseTagInvalid), + "block {number}, fork-detection data" + ); + } + } +} diff --git a/crates/execution/src/mining/merkle.rs b/crates/execution/src/mining/merkle.rs index 5da73c0..d253f64 100644 --- a/crates/execution/src/mining/merkle.rs +++ b/crates/execution/src/mining/merkle.rs @@ -100,7 +100,8 @@ pub fn parse_wire_hashes(joined: &str) -> Result, MerkleProofError } /// The merkle root a proof folds to, for checking a proof before it is put in -/// a header. Mirrors the verifier exactly. -pub fn root_from_proof(coinbase_hash: &[u8; 32], proof: &[u8]) -> [u8; 32] { +/// a header. Mirrors the verifier exactly, `None` included: two nodes on the +/// path that parse as a 64-byte Bitcoin transaction, which rskj refuses. +pub fn root_from_proof(coinbase_hash: &[u8; 32], proof: &[u8]) -> Option<[u8; 32]> { rustock_core::validation::merged_mining::rskip92_merkle_root(coinbase_hash, proof) } diff --git a/crates/execution/src/mining/tests.rs b/crates/execution/src/mining/tests.rs index b3aa093..ec43755 100644 --- a/crates/execution/src/mining/tests.rs +++ b/crates/execution/src/mining/tests.rs @@ -332,7 +332,7 @@ fn merkle_proofs_fold_back_to_the_root() { assert_eq!(proof.len() % 32, 0); let root = merkle::root_from_proof(&txids[0], &proof); - assert_eq!(root, reference_merkle_root(&txids), "{count} transactions"); + assert_eq!(root, Some(reference_merkle_root(&txids)), "{count} transactions"); } } @@ -355,7 +355,7 @@ fn reference_merkle_root(txids: &[[u8; 32]]) -> [u8; 32] { fn a_single_transaction_block_needs_no_proof() { let proof = merkle::proof_from_txids(&[[7u8; 32]]).unwrap(); assert!(proof.is_empty()); - assert_eq!(merkle::root_from_proof(&[7u8; 32], &proof), [7u8; 32]); + assert_eq!(merkle::root_from_proof(&[7u8; 32], &proof), Some([7u8; 32])); } #[test] diff --git a/crates/execution/src/processor.rs b/crates/execution/src/processor.rs index 2589e05..057b2ad 100644 --- a/crates/execution/src/processor.rs +++ b/crates/execution/src/processor.rs @@ -291,6 +291,11 @@ impl BlockProcessor { self } + /// Whether the RSKIP110 fork-detection check runs. + pub fn validates_fork_detection(&self) -> bool { + self.validate_fork_detection_data + } + /// Execute all transactions in a block without validating results against the header. /// /// Returns the processed block with receipts and new state root. From 93ef7030b3d819098f2cce18132f25e5bbe87844 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Fri, 6 Nov 2026 10:53:12 -0800 Subject: [PATCH 016/264] Work out the expected block time in Java int arithmetic, as rskj does rskj's DifficultyCalculator computes `int calcDur = (1 + uncleCount) * duration` in Java int arithmetic, which wraps, and compares it with the gap to the parent as a long. Nothing ties a header's uncle count to the uncles it carries, so the count is the miner's to choose: one that wraps the product to zero or below makes a block late however soon it came, and its difficulty falls by a 400th. rskj takes that block. The u64 product rustock used made the same block early, expected a rise, and refused it. The test pins the wrap to zero (uncle count 2^31 - 1), below zero (2^30), and the ordinary case. Mutants with the u64 product, an i64 product, an unsigned 32-bit wrap and a saturating product are all caught. --- crates/core/src/validation/difficulty.rs | 33 +++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/crates/core/src/validation/difficulty.rs b/crates/core/src/validation/difficulty.rs index f592bdf..f9b0938 100644 --- a/crates/core/src/validation/difficulty.rs +++ b/crates/core/src/validation/difficulty.rs @@ -80,7 +80,17 @@ impl DifficultyRule { return parent.difficulty; } - let calc_dur = (1 + header.uncle_count) * self.config.duration_limit; + // rskj: `int calcDur = (1 + uncleCount) * duration`, in Java `int` + // arithmetic, which wraps. Nothing ties a header's uncle count to the + // uncles it carries, so the count is the miner's to choose, and one + // that wraps the product to zero or below makes any block late: a + // lower difficulty, which rskj takes, and so must this. Both + // timestamps are Java `long`s, so the gap fits an `i64`. + let calc_dur = i64::from( + 1i32.wrapping_add(header.uncle_count as i32) + .wrapping_mul(self.config.duration_limit as i32), + ); + let delta = delta as i64; let sign = if calc_dur > delta { 1i8 @@ -210,4 +220,25 @@ mod rskip97_tests { let got = rule.difficulty_for_child(&parent, 101, 1_000_000 + 599, 0); assert_ne!(got, U256::from(131_072u64), "599s is under the ten-minute threshold"); } + + /// rskj works out the expected time in Java `int` arithmetic, and nothing + /// ties a header's uncle count to its uncles. A count that wraps the + /// product to zero, or below it, makes a block late however soon it came, + /// and its difficulty falls. + #[test] + fn the_expected_time_wraps_as_a_java_int() { + let rule = DifficultyRule { config: Arc::new(ChainConfig::mainnet()) }; + let parent = header_at(5_000_000, 1_000_000, U256::from(10u128.pow(21))); + let step = parent.difficulty / U256::from(400); + let child = |uncles: u64| { + rule.difficulty_for_child(&parent, 5_000_001, 1_000_000 + 10, uncles) + }; + + // (1 + 1) * 14: 28 seconds expected, and 10 is sooner. + assert_eq!(child(1), parent.difficulty + step); + // (1 + 2^31 - 1) * 14 wraps to 0, and 10 is later. + assert_eq!(child(i32::MAX as u64), parent.difficulty - step); + // (1 + 2^30) * 14 wraps below zero. + assert_eq!(child(1 << 30), parent.difficulty - step); + } } From f6cf30b92ec4b1be5ab3e13908eb729f144ec1ca Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Fri, 6 Nov 2026 11:07:23 -0800 Subject: [PATCH 017/264] Sum the fork-detection uncle counts as a Java int, as rskj does rskj's ForkDetectionDataCalculator adds the uncle counts of the 32 headers below a block with IntStream.sum, in Java int arithmetic, which wraps, and caps a sum over 255 at 255. A sum that wraps below zero makes `new Uint8(sum)` throw, and BlockChainImpl.tryToConnect turns the throw into INVALID_BLOCK. Nothing ties a header's uncle count to its uncles, so a miner can choose counts that wrap: one header counting 2^31 - 1 leaves its chain no valid child, and two such counts and a nine make the sum 7, where rustock's u64 sum saturated at 255. The validator and the miner each kept a u64 sum. Both now use core's fork_detection::uncle_sum: the validator refuses a block above such a chain with its own error, and the template builder refuses to build on it, before executing anything, as rskj's BlockToMineBuilder throws. Eight mutants are all caught, among them a saturating sum, a clamp to 0 or to 255, a 33-block window, a validator that takes any byte, a miner keeping its own sum, and a template builder that goes on regardless. --- crates/core/src/validation/fork_detection.rs | 51 ++++++++++----- crates/core/src/validation/mod.rs | 3 + crates/core/src/validation/tests.rs | 61 ++++++++++++++++++ crates/execution/src/mining/fork_detection.rs | 30 +++++---- crates/execution/src/mining/template.rs | 10 ++- crates/execution/src/mining/tests.rs | 63 +++++++++++++++++-- 6 files changed, 182 insertions(+), 36 deletions(-) diff --git a/crates/core/src/validation/fork_detection.rs b/crates/core/src/validation/fork_detection.rs index c9df6c7..536f7d2 100644 --- a/crates/core/src/validation/fork_detection.rs +++ b/crates/core/src/validation/fork_detection.rs @@ -37,10 +37,13 @@ pub trait MainchainView { /// rskj `ForkDetectionDataCalculator.calculateWithBlockHeaders`. /// /// `headers[0]` is the best block (the parent of the block being mined). -/// Returns `None` when the view is too short, matching the Java's empty array. -pub fn calculate(headers: &[Header]) -> Option<[u8; 12]> { +/// [`ValidationError::ForkDetectionDataUnreadable`] when the view is too short, +/// which rskj's rule refuses before it calculates, or when a Bitcoin header it +/// reads is missing or doesn't decode, where the Java throws; and +/// [`uncle_sum`]'s error where rskj's throws. +pub fn calculate(headers: &[Header]) -> Result<[u8; 12], ValidationError> { if (headers.len() as u64) < REQUIRED_BLOCKS { - return None; + return Err(ValidationError::ForkDetectionDataUnreadable); } let mut data = [0u8; 12]; @@ -50,25 +53,43 @@ pub fn calculate(headers: &[Header]) -> Option<[u8; 12]> { let cpv_start = (best_height / CPV_JUMP_FACTOR) * CPV_JUMP_FACTOR; for (i, slot) in data.iter_mut().enumerate().take(CPV_SIZE) { let index = best_height - cpv_start + i as u64 * CPV_JUMP_FACTOR; - let header = headers.get(index as usize)?; - *slot = btc_hash_least_significant_byte(header)?; + *slot = headers + .get(index as usize) + .and_then(btc_hash_least_significant_byte) + .ok_or(ValidationError::ForkDetectionDataUnreadable)?; } - // getNumberOfUncles: the uncle count over the 32 newest blocks, saturating - // at 255 (`Uint8.MAX_VALUE`). - let uncle_sum: u64 = headers - .iter() - .take(NUMBER_OF_UNCLES) - .map(|h| h.uncle_count) - .sum(); - data[7] = uncle_sum.min(u8::MAX as u64) as u8; + data[7] = uncle_sum(headers)?; // getBlockBeingMinedHeight: the parent's height plus one, as a big-endian // 32-bit integer (Java `(int)` truncation). let height_being_mined = best_height.wrapping_add(1) as u32; data[8..12].copy_from_slice(&height_being_mined.to_be_bytes()); - Some(data) + Ok(data) +} + +/// rskj `getNumberOfUncles`: the uncle counts of the 32 newest headers, +/// summed in Java `int` arithmetic and capped at 255 (`Uint8.MAX_VALUE`). +/// +/// Nothing ties a header's uncle count to the uncles it carries, so the +/// counts are the miners' to choose, and `IntStream.sum` wraps. A sum that +/// wraps below zero makes `new Uint8(sum)` throw, which rskj's +/// `BlockChainImpl.tryToConnect` turns into `INVALID_BLOCK`, so no child of +/// such a chain is valid, and its `BlockToMineBuilder` can't build one. A sum +/// that wraps on past zero counts what it came to. rskj decodes each count +/// with `intValueExact`, so none is over `i32::MAX`. +pub fn uncle_sum(headers: &[Header]) -> Result { + let sum = headers + .iter() + .take(NUMBER_OF_UNCLES) + .fold(0i32, |sum, header| { + sum.wrapping_add(header.uncle_count as i32) + }); + if sum < 0 { + return Err(ValidationError::UncleCountsSumBelowZero { sum }); + } + Ok(u8::try_from(sum).unwrap_or(u8::MAX)) } /// The last byte of the BTC block hash in bitcoinj's `getBytes()` order. @@ -119,7 +140,7 @@ pub fn validate( // hasEnoughBlocksToCalculateForkDetectionData: a short view is a rejection, // not a skip. - let expected = calculate(&headers).ok_or(ValidationError::ForkDetectionDataUnreadable)?; + let expected = calculate(&headers)?; if expected != got { return Err(ValidationError::ForkDetectionDataMismatch { expected, got }); diff --git a/crates/core/src/validation/mod.rs b/crates/core/src/validation/mod.rs index d1a9f5e..a46b13b 100644 --- a/crates/core/src/validation/mod.rs +++ b/crates/core/src/validation/mod.rs @@ -112,6 +112,9 @@ pub enum ValidationError { #[error("Fork detection data could not be read from the coinbase")] ForkDetectionDataUnreadable, + #[error("The uncle counts of the 32 blocks below sum to {sum} as a Java int, which rskj refuses")] + UncleCountsSumBelowZero { sum: i32 }, + #[error("Invalid transaction execution sublist edges: {reason}")] InvalidSublistEdges { reason: String }, } diff --git a/crates/core/src/validation/tests.rs b/crates/core/src/validation/tests.rs index e9967de..3e3a895 100644 --- a/crates/core/src/validation/tests.rs +++ b/crates/core/src/validation/tests.rs @@ -704,6 +704,67 @@ fn fork_detection_starts_above_block_449() { ); } +/// A view of the chain that holds these headers, newest first. +struct Held(Vec
); + +impl fork_detection::MainchainView for Held { + fn headers_from(&self, _from: B256, count: u64) -> Vec
{ + self.0.iter().take(count as usize).cloned().collect() + } +} + +/// rskj sums the uncle counts of the 32 headers below a block in Java `int` +/// arithmetic (`IntStream.sum`) and caps the sum at 255. A sum that wraps +/// below zero makes `new Uint8` throw, which `tryToConnect` turns into +/// `INVALID_BLOCK`, so the block is refused whatever data it carries; one +/// that wraps on past zero counts what it came to. +#[test] +fn the_uncle_counts_sum_as_a_java_int() { + let chain = |counts: &[u64]| -> Vec
{ + (0..fork_detection::REQUIRED_BLOCKS) + .map(|i| { + let mut header = create_dummy_header(10_000 - i, 1000, B256::ZERO); + header.uncle_count = counts.get(i as usize).copied().unwrap_or(0); + header.bitcoin_merged_mining_header = Some(Bytes::from(vec![0u8; 80])); + header + }) + .collect() + }; + let sum = |counts: &[u64]| fork_detection::uncle_sum(&chain(counts)); + let max = i32::MAX as u64; + let below_zero = || ValidationError::UncleCountsSumBelowZero { sum: i32::MIN }; + + assert_eq!(sum(&[1, 2, 3]), Ok(6)); + assert_eq!(sum(&[200, 56]), Ok(255)); + let mut beyond = vec![0; 33]; + beyond[32] = 5; + assert_eq!(sum(&beyond), Ok(0), "only the 32 newest count"); + assert_eq!(sum(&[max, 1]), Err(below_zero())); + // 2 * (2^31 - 1) + 9 = 2^32 + 7. + assert_eq!(sum(&[max, max, 9]), Ok(7)); + + let block = create_dummy_header(10_001, 1000, B256::ZERO); + let wrapped = Held(chain(&[max, max, 9])); + let data = fork_detection::calculate(&wrapped.0).unwrap(); + assert_eq!(data[7], 7); + assert_eq!( + fork_detection::validate(&block, Some(data), &wrapped, 0), + Ok(()) + ); + // Below zero, no data passes: not the 255 a saturating sum gives, nor the + // 0 a clamp gives, nor any other count with the right vector and height. + let refused = Held(chain(&[max, 1])); + for count in 0..=u8::MAX { + let mut carried = data; + carried[7] = count; + assert_eq!( + fork_detection::validate(&block, Some(carried), &refused, 0), + Err(below_zero()), + "carrying {count}" + ); + } +} + /// Merge-mine `header` the way a miner does: commit to it in a coinbase, and /// roll the Bitcoin header's nonce until its hash meets the RSK difficulty. fn seal(header: &mut Header, btc_time: u64) { diff --git a/crates/execution/src/mining/fork_detection.rs b/crates/execution/src/mining/fork_detection.rs index 7dbbf2e..3e94c95 100644 --- a/crates/execution/src/mining/fork_detection.rs +++ b/crates/execution/src/mining/fork_detection.rs @@ -8,14 +8,15 @@ //! //! Ported from rskj `co.rsk.mine.ForkDetectionDataCalculator`. +use rustock_core::validation::fork_detection::uncle_sum; +use rustock_core::validation::ValidationError; use rustock_core::Header; /// Bytes of commit-to-parents vector in the fork-detection data. const CPV_SIZE: usize = 7; /// Spacing, in blocks, between the ancestors the CPV commits to. const CPV_JUMP_FACTOR: u64 = 64; -/// How many recent blocks' uncle counts are summed. -const NUMBER_OF_UNCLES: usize = 32; + /// Total length of the fork-detection data. pub const FORK_DETECTION_DATA_LENGTH: usize = 12; @@ -32,17 +33,21 @@ pub const REQUIRED_MAINCHAIN_BLOCKS: usize = CPV_SIZE * CPV_JUMP_FACTOR as usize /// the block being built, index 1 its parent, and so on. Returns an empty /// vector when the chain is too short, which is what rskj does and what the /// header then carries -- early blocks legitimately have none. -pub fn calculate(mainchain: &[Header]) -> Vec { +/// +/// The uncle count is the validator's own sum, so the two can't drift, and +/// its error is rskj's `BlockToMineBuilder` throwing: no block on top of +/// `mainchain[0]` would be valid. +pub fn calculate(mainchain: &[Header]) -> Result, ValidationError> { if mainchain.len() < REQUIRED_MAINCHAIN_BLOCKS { - return Vec::new(); + return Ok(Vec::new()); } let mut data = Vec::with_capacity(FORK_DETECTION_DATA_LENGTH); data.extend_from_slice(&commit_to_parents_vector(mainchain)); - data.push(uncle_sum(mainchain)); + data.push(uncle_sum(mainchain)?); data.extend_from_slice(&((mainchain[0].number + 1) as u32).to_be_bytes()); debug_assert_eq!(data.len(), FORK_DETECTION_DATA_LENGTH); - data + Ok(data) } /// One byte per committed ancestor, each the least significant byte of that @@ -64,7 +69,9 @@ fn commit_to_parents_vector(mainchain: &[Header]) -> [u8; CPV_SIZE] { /// The last byte of the Bitcoin block hash in its conventional (reversed, /// as-displayed) order -- which is the *first* byte of the consensus /// little-endian encoding. A header without a decodable Bitcoin block -/// contributes zero, matching what rskj gets from a hash it cannot compute. +/// contributes zero, so chains built without merged mining, as the tests and +/// the load generator build them, still get data. rskj's miner throws there +/// instead, and the validator refuses a block above such a header. fn bitcoin_hash_least_significant_byte(header: &Header) -> u8 { use bitcoin::block::Header as BtcHeader; use bitcoin::consensus::Decodable; @@ -80,15 +87,6 @@ fn bitcoin_hash_least_significant_byte(header: &Header) -> u8 { } } -/// Uncles included by the last 32 mainchain blocks, saturating at a byte. -fn uncle_sum(mainchain: &[Header]) -> u8 { - let sum: u64 = mainchain[..NUMBER_OF_UNCLES] - .iter() - .map(|h| h.uncle_count) - .sum(); - sum.min(u8::MAX as u64) as u8 -} - /// Overlay fork-detection data onto a merged-mining hash, producing the 32 /// bytes that go in the coinbase after the tag. /// diff --git a/crates/execution/src/mining/template.rs b/crates/execution/src/mining/template.rs index b42f8d0..e572995 100644 --- a/crates/execution/src/mining/template.rs +++ b/crates/execution/src/mining/template.rs @@ -103,6 +103,10 @@ pub enum TemplateError { /// alternative is a chain of blocks nobody else accepts. #[error("the assembled block fails a block rule: {0}")] SelfCheck(ValidationError), + /// rskj's `BlockToMineBuilder` throws here too: no block on this parent + /// can carry fork-detection data the validator takes. + #[error("no block on this parent can be valid: {0}")] + ForkDetection(ValidationError), } /// A transaction waiting to be mined, with its sender already recovered. @@ -190,6 +194,11 @@ impl BlockTemplateBuilder { pending: &dyn PendingTransactionSource, ) -> Result { let parent = &mainchain[0]; + // First, as rskj's `BlockToMineBuilder` works it out before executing + // anything: on a chain whose uncle counts sum below zero, no block is + // valid, and there is no template to build. + let fork_data = + fork_detection::calculate(mainchain).map_err(TemplateError::ForkDetection)?; let parent_state_root = parent.state_root; let state_root = self.load_state_root(parent_state_root)?; @@ -334,7 +343,6 @@ impl BlockTemplateBuilder { header.gas_used = executed.gas_used; header.paid_fees = executed.paid_fees; - let fork_data = fork_detection::calculate(mainchain); let hash_for_merged_mining = fork_detection::apply_to_hash(header.hash_for_merged_mining(), &fork_data); diff --git a/crates/execution/src/mining/tests.rs b/crates/execution/src/mining/tests.rs index ec43755..a2e63eb 100644 --- a/crates/execution/src/mining/tests.rs +++ b/crates/execution/src/mining/tests.rs @@ -10,7 +10,7 @@ use alloy_primitives::{Address, B256, Bytes, U256, address, b256}; use bitcoin::hashes::Hash as _; use rustock_core::config::{ActivationHeights, ChainConfig}; use rustock_core::validation::merged_mining::{compress_coinbase, compute_coinbase_hash}; -use rustock_core::validation::{HeaderValidator, MergedMiningRule}; +use rustock_core::validation::{HeaderValidator, MergedMiningRule, ValidationError}; use rustock_core::Header; use rustock_storage::BlockStore; use rustock_trie::{MemoryTrieStore, TrieNode, TrieStore}; @@ -404,7 +404,7 @@ fn fork_detection_data_is_empty_on_a_short_chain() { let chain: Vec
= (0..448) .map(|i| header_at(448 - i, B256::ZERO, B256::ZERO)) .collect(); - assert!(fork_detection::calculate(&chain).is_empty()); + assert!(fork_detection::calculate(&chain).unwrap().is_empty()); } #[test] @@ -418,7 +418,7 @@ fn fork_detection_data_commits_to_height_and_uncles() { } chain[32].uncle_count = 99; - let data = fork_detection::calculate(&chain); + let data = fork_detection::calculate(&chain).unwrap(); assert_eq!(data.len(), fork_detection::FORK_DETECTION_DATA_LENGTH); assert_eq!(data[7], 32, "uncles outside the 32-block window must not count"); assert_eq!(&data[8..12], &5_001u32.to_be_bytes(), "the height being mined"); @@ -436,7 +436,62 @@ fn fork_detection_uncle_count_saturates() { h }) .collect(); - assert_eq!(fork_detection::calculate(&chain)[7], 255); + assert_eq!(fork_detection::calculate(&chain).unwrap()[7], 255); +} + +/// The miner sums the uncle counts as the validator does, in rskj's Java +/// `int`: counts that wrap the sum below zero leave no block to build, as +/// rskj's `BlockToMineBuilder` throws, and counts that wrap it on past zero +/// give what it came to, not a saturated 255. +#[test] +fn fork_detection_uncle_count_wraps_as_a_java_int() { + let chain = |counts: &[u64]| -> Vec
{ + (0..fork_detection::REQUIRED_MAINCHAIN_BLOCKS) + .map(|i| { + let mut h = header_at(5_000 - i as u64, B256::ZERO, B256::ZERO); + h.uncle_count = counts.get(i).copied().unwrap_or(0); + h + }) + .collect() + }; + let max = i32::MAX as u64; + assert_eq!( + fork_detection::calculate(&chain(&[max, 1])), + Err(ValidationError::UncleCountsSumBelowZero { sum: i32::MIN }) + ); + // 2 * (2^31 - 1) + 9 = 2^32 + 7. + assert_eq!( + fork_detection::calculate(&chain(&[max, max, 9])).unwrap()[7], + 7 + ); +} + +/// And no template either, as rskj's `BlockToMineBuilder` throws before it +/// executes anything; the same chain without those counts builds. +#[test] +fn no_template_on_a_chain_whose_uncle_counts_sum_below_zero() { + let fx = fixture(20); + let chain = |counts: [u64; 2]| -> Vec
{ + let mut chain = vec![fx.parent.clone()]; + chain.extend((1..fork_detection::REQUIRED_MAINCHAIN_BLOCKS).map(|i| { + let mut h = header_at(5_000 - i as u64, B256::ZERO, B256::ZERO); + h.uncle_count = counts.get(i - 1).copied().unwrap_or(0); + h + })); + chain + }; + let build = |mainchain: &[Header]| { + fx.server + .builder_for_test() + .build(mainchain, Vec::new(), &NoPendingTransactions) + }; + assert!(build(&chain([0, 0])).is_ok()); + assert!(matches!( + build(&chain([i32::MAX as u64, 1])), + Err(TemplateError::ForkDetection( + ValidationError::UncleCountsSumBelowZero { sum: i32::MIN } + )) + )); } /// The overlay replaces the last 12 bytes and nothing else: the first 20 are From d4fb9b275aef7c2227df3ec8a6e4a030a31b2d7d Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Sun, 8 Nov 2026 08:36:41 -0800 Subject: [PATCH 018/264] Read a header as rskj's BlockFactory does: its ranges and its layout rskj reads a header's number, gas used and timestamp with longValueExact and its uncle count with intValueExact, so it cannot read one past a Java long, or an int; it reads the three merged-mining fields together or not at all; and canBeDecoded counts a header's fields exactly, by the RSKIPs active at its height. rustock's decoder read u64s, took one or two merged-mining fields, and ignored whatever came after them. The uncle count matters most: nothing ties it to the uncles, so a miner picks it, and a count past 2^31 - 1, which rskj cannot read, was taken here, truncated into the difficulty. The decoder reads by shape, not by height, so HeaderLayoutRule, first in default_rsk and in the uncle rules, holds what it read to what rskj reads at the header's height: a UMM root from RSKIPUMM (papyrus200) and only then, and no RSKIP351 fields, versioned or compressed, where RSKIP351 is inactive, which is all of mainnet. ActivationHeights gains rskip351 beside rskip144: none on mainnet, reed810 on testnet, 1 on regtest, as rskj's configs set it, and --consensus-rule rskip351 moves it as well as the execution ladder's. A chain with no papyrus200, rustock's regtest, is not held to either UMM layout: rskj's regtest has rskipUMM = 1, and the rskj regtest chain --import-blocks takes carries a UMM root from block 1, which the rule would otherwise refuse. --- crates/cli/src/main.rs | 8 ++ crates/core/src/config.rs | 12 ++ crates/core/src/types/header.rs | 138 +++++++++++++++++++-- crates/core/src/validation/header_rules.rs | 42 +++++++ crates/core/src/validation/mod.rs | 10 +- crates/core/src/validation/tests.rs | 55 ++++++++ crates/core/src/validation/uncles.rs | 8 +- crates/execution/src/mining/tests.rs | 3 + 8 files changed, 259 insertions(+), 17 deletions(-) diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index d345730..5b9a81a 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -3891,6 +3891,12 @@ fn apply_chain_options(mut config: ChainConfig, args: &Args) -> Result config.activation_heights.rskip97 = height, + // The ladder reads it from the overrides, and the header layout + // rule from the activation heights: both must move. + "rskip351" => { + config.activation_heights.rskip351 = height; + config.consensus_rule_overrides.push((name, height)); + } _ => config.consensus_rule_overrides.push((name, height)), } } @@ -5414,6 +5420,8 @@ mod genesis_tests { let config = apply_chain_options(ChainConfig::mainnet(), &mainnet_args(SEED_FLAGS)).unwrap(); assert_eq!(config.chain_id, 30); assert_eq!(config.activation_heights.rskip144, Some(9_002_504)); + // The header layout rule reads RSKIP351 here, the ladder below. + assert_eq!(config.activation_heights.rskip351, Some(9_002_504)); assert_eq!(config.activation_heights.orchid, 729_000); assert_eq!(config.activation_heights.rskip97, Some(729_000)); assert_eq!(config.activation_heights.papyrus200, 2_392_700); diff --git a/crates/core/src/config.rs b/crates/core/src/config.rs index 2547cb5..088d0ac 100644 --- a/crates/core/src/config.rs +++ b/crates/core/src/config.rs @@ -60,6 +60,11 @@ pub struct ActivationHeights { /// activation). rskj's own regtest is the second case: `rskip144 = -1` /// with `rskip535 = 1`. pub rskip144: Option, + /// Height at which RSKIP351 -- versioned headers, the version and its + /// extension where a header had only a logs bloom -- activates, or + /// `None` where it never does. rskj's `reference.conf` sets it at + /// reed810, which mainnet has at -1, so no mainnet header carries it. + pub rskip351: Option, } /// Process-wide activation heights, consulted by block-hash computation @@ -109,6 +114,8 @@ impl ActivationHeights { rskip97: Some(729_000), // main.conf: reed810 = -1. rskip144: None, + // main.conf: `reed810 = -1`. + rskip351: None, } } @@ -124,8 +131,11 @@ impl ActivationHeights { hop400: 3_103_000, // testnet.conf: `rskip97 = -1 # disable orchid difficulty drop`. rskip97: None, + // testnet.conf: reed810. rskip144: Some(7_139_600), + // testnet.conf: `reed810 = 7139600`. + rskip351: Some(7_139_600), } } @@ -142,6 +152,8 @@ impl ActivationHeights { // regtest.conf: `rskip144 = -1`; rustock's regtest ladder does // not schedule reed810 either. rskip144: None, + // regtest.conf: `rskip351 = 1`. + rskip351: Some(1), } } } diff --git a/crates/core/src/types/header.rs b/crates/core/src/types/header.rs index 9e0ee1f..4557b05 100644 --- a/crates/core/src/types/header.rs +++ b/crates/core/src/types/header.rs @@ -93,6 +93,26 @@ fn count_rlp_items(buf: &[u8]) -> usize { count } +/// rskj reads a header's number, gas used and timestamp with +/// `longValueExact`, which throws past a Java `long`: it cannot read a header +/// with one over `i64::MAX`. +fn decode_java_long(buf: &mut &[u8]) -> alloy_rlp::Result { + let value = decode_u64_lenient(buf)?; + if value > i64::MAX as u64 { + return Err(alloy_rlp::Error::Custom("a header field past a Java long")); + } + Ok(value) +} + +/// And its uncle count with `intValueExact`, which throws past a Java `int`. +fn decode_java_int(buf: &mut &[u8]) -> alloy_rlp::Result { + let value = decode_u64_lenient(buf)?; + if value > i32::MAX as u64 { + return Err(alloy_rlp::Error::Custom("an uncle count past a Java int")); + } + Ok(value) +} + /// rskj `ByteUtil.rlpToShorts`: little-endian pairs. fn rlp_to_shorts(bytes: &[u8]) -> Vec { bytes.chunks_exact(2).map(|c| i16::from_le_bytes([c[0], c[1]])).collect() @@ -358,14 +378,14 @@ impl Header { logs_bloom, extension_data, difficulty: decode_u256_lenient(&mut body)?, - number: decode_u64_lenient(&mut body)?, + number: decode_java_long(&mut body)?, gas_limit: decode_u256_lenient(&mut body)?, - gas_used: decode_u64_lenient(&mut body)?, - timestamp: decode_u64_lenient(&mut body)?, + gas_used: decode_java_long(&mut body)?, + timestamp: decode_java_long(&mut body)?, extra_data: Bytes::decode(&mut body)?, paid_fees: decode_u256_lenient(&mut body)?, minimum_gas_price: decode_u256_lenient(&mut body)?, - uncle_count: decode_u64_lenient(&mut body)?, + uncle_count: decode_java_int(&mut body)?, umm_root: None, version_fields: None, bitcoin_merged_mining_header: None, @@ -375,10 +395,11 @@ impl Header { cached_hash_for_merged_mining: None, }; - // Count remaining RLP items to distinguish header format: - // 3 items → V0: btc_header, btc_merkle_proof, btc_coinbase_tx - // 4 items → V1 (RSKIP-153/Hop): umm_root, btc_header, btc_merkle_proof, btc_coinbase_tx - // 0-2 → partial (pre-orchid blocks without full mining data) + // What is left is told apart by count, since nothing here knows the + // network: none is a header without merged-mining fields, three are + // those fields, and four or more begin with a UMM root (RSKIPUMM, + // papyrus200). `HeaderLayoutRule` holds what was read to the fields + // rskj reads at the header's height. let remaining_items = count_rlp_items(body); if remaining_items >= 4 { @@ -441,16 +462,20 @@ impl Header { header.version_fields = Some(HeaderVersionFields { version, edges, base_event }); } + // rskj reads the three merged-mining fields together or not at all, + // and `canBeDecoded` counts a header's fields exactly: with one or two + // of them, or anything after them, it cannot read the header. if !body.is_empty() { header.bitcoin_merged_mining_header = Some(Bytes::decode(&mut body)?); - } - if !body.is_empty() { header.bitcoin_merged_mining_merkle_proof = Some(Bytes::decode(&mut body)?); - } - if !body.is_empty() { header.bitcoin_merged_mining_coinbase_transaction = Some(Bytes::decode(&mut body)?); + if !body.is_empty() { + return Err(alloy_rlp::Error::Custom( + "a header with fields after its merged-mining fields", + )); + } } - + Ok(header) } } @@ -684,6 +709,93 @@ mod tests { assert_ne!(hash, B256::ZERO); } + /// The items of an RLP list, each with its own header, as written. + fn items_of(encoded: &[u8]) -> Vec> { + let mut buf = encoded; + let list = alloy_rlp::Header::decode(&mut buf).unwrap(); + assert!(list.list); + let mut body = &buf[..list.payload_length]; + let mut items = Vec::new(); + while !body.is_empty() { + let start = body; + let item = alloy_rlp::Header::decode(&mut body).unwrap(); + let length = start.len() - body.len() + item.payload_length; + items.push(start[..length].to_vec()); + body = &body[item.payload_length..]; + } + items + } + + /// An RLP list of items already written. + fn list_of(items: &[Vec]) -> Vec { + let payload = items.concat(); + let mut list = Vec::new(); + alloy_rlp::Header { + list: true, + payload_length: payload.len(), + } + .encode(&mut list); + list.extend_from_slice(&payload); + list + } + + /// rskj reads a header's number, gas used and timestamp with + /// `longValueExact`, and its uncle count with `intValueExact`: one past a + /// Java `long`, or an `int`, and it cannot read the header at all. + #[test] + fn rskj_reads_no_header_past_a_java_long_or_int() { + let decodes = |change: &dyn Fn(&mut Header)| { + let mut header = standard_test_header(); + change(&mut header); + let mut bytes = Vec::new(); + header.encode(&mut bytes); + Header::decode(&mut bytes.as_slice()).is_ok() + }; + let long = i64::MAX as u64; + let int = i32::MAX as u64; + assert!(decodes(&|h| h.number = long)); + assert!(!decodes(&|h| h.number = long + 1)); + assert!(decodes(&|h| h.gas_used = long)); + assert!(!decodes(&|h| h.gas_used = long + 1)); + assert!(decodes(&|h| h.timestamp = long)); + assert!(!decodes(&|h| h.timestamp = long + 1)); + assert!(decodes(&|h| h.uncle_count = int)); + assert!(!decodes(&|h| h.uncle_count = int + 1)); + } + + /// rskj reads the merged-mining fields all three or none, and + /// `canBeDecoded` counts a header's fields exactly: with one or two of + /// them, or a field after them, it cannot read the header. + #[test] + fn rskj_reads_the_merged_mining_fields_all_three_or_none() { + let mut bytes = Vec::new(); + Header { + umm_root: None, + ..standard_test_header() + } + .encode(&mut bytes); + let items = items_of(&bytes); + assert_eq!( + items.len(), + 19, + "sixteen fields and three for merged mining" + ); + // The sixteen fields, then `extra` items from the three. + let read = |extra: usize| { + let mut fields = items[..16].to_vec(); + fields.extend(items[16..].iter().cycle().take(extra).cloned()); + Header::decode(&mut list_of(&fields).as_slice()) + }; + assert!(read(0).is_ok(), "no merged-mining fields"); + assert!(read(1).is_err()); + assert!(read(2).is_err()); + assert!(read(3).is_ok(), "the three merged-mining fields"); + // Four are a UMM root and the three: which a header may carry is its + // height's to say, in `HeaderLayoutRule`. + assert!(read(4).is_ok_and(|header| header.umm_root.is_some())); + assert!(read(5).is_err(), "a field after the merged-mining fields"); + } + #[test] fn test_decode_with_hash_caches_hash() { let header = standard_test_header(); diff --git a/crates/core/src/validation/header_rules.rs b/crates/core/src/validation/header_rules.rs index bbc4e06..a948493 100644 --- a/crates/core/src/validation/header_rules.rs +++ b/crates/core/src/validation/header_rules.rs @@ -149,6 +149,48 @@ impl HeaderValidator for GasLimitBoundsRule { } } +/// The fields rskj's `BlockFactory.decodeHeader` reads a header with, which +/// depend on its height: `canBeDecoded` counts them exactly, so a header with +/// one too many or one too few is one rskj cannot read, and so cannot take. +/// The decoder here reads by shape rather than by height, so this holds what +/// it read to what rskj would have. +/// +/// - A UMM root from RSKIPUMM on, and none before it. rskj's `reference.conf` +/// puts RSKIPUMM at papyrus200, which mainnet and testnet keep. A chain +/// with no papyrus200, rustock's regtest, is not held to either: rskj's +/// regtest has `rskipUMM = 1`, and both regtests are read here, rustock's +/// own and one imported from rskj. +/// - No RSKIP351 fields where RSKIP351 is inactive, as it is on mainnet: no +/// version, edges or base event after the UMM root, and a 256-byte logs +/// bloom rather than the extension's commitment in its place. +/// +/// An uncle is read the same way, at its own height. +pub struct HeaderLayoutRule { + pub config: std::sync::Arc, +} + +impl HeaderValidator for HeaderLayoutRule { + fn validate(&self, header: &Header) -> Result<(), ValidationError> { + let heights = &self.config.activation_heights; + let umm = header.number >= heights.papyrus200; + if heights.papyrus200 != u64::MAX && header.umm_root.is_some() != umm { + return Err(ValidationError::UmmRootMisplaced { + number: header.number, + present: header.umm_root.is_some(), + }); + } + let rskip351 = heights + .rskip351 + .is_some_and(|height| header.number >= height); + if !rskip351 && (header.version_fields.is_some() || header.extension_data.is_some()) { + return Err(ValidationError::VersionedBeforeRskip351 { + number: header.number, + }); + } + Ok(()) + } +} + /// rskj `BlockParentGasLimitRule`: a header's gas limit may move from its /// parent's by at most a `gas_limit_bound_divisor`th, either way. pub struct BlockParentGasLimitRule { diff --git a/crates/core/src/validation/mod.rs b/crates/core/src/validation/mod.rs index a46b13b..a5bfe50 100644 --- a/crates/core/src/validation/mod.rs +++ b/crates/core/src/validation/mod.rs @@ -70,6 +70,12 @@ pub enum ValidationError { #[error("UMM root is {len} bytes, must be empty or 20")] UmmRootInvalid { len: usize }, + #[error("Header #{number} has a UMM root: {present}, which rskj reads from RSKIPUMM on and only then")] + UmmRootMisplaced { number: u64, present: bool }, + + #[error("Header #{number} carries RSKIP351 fields where RSKIP351 is inactive")] + VersionedBeforeRskip351 { number: u64 }, + #[error("Tx gas price {tx_gas_price} is below the block minimum {block_minimum}")] TxGasPriceBelowMinimum { tx_gas_price: U256, block_minimum: U256 }, @@ -214,6 +220,8 @@ impl HeaderVerifier { /// and cannot be reproduced by our standard `Header::hash()`. pub fn default_rsk(config: std::sync::Arc) -> Self { let mut verifier = Self::new() + // First, as rskj reads a header with these fields or not at all. + .with_static_rule(HeaderLayoutRule { config: config.clone() }) .with_static_rule(GasUsedRule) .with_static_rule(GasLimitBoundsRule { min_gas_limit: config.min_gas_limit, @@ -327,7 +335,7 @@ pub mod header_rules; pub mod difficulty; pub mod merged_mining; -pub use header_rules::{BlockNumberRule, ParentHashRule, TimestampRule, GasUsedRule, GasLimitBoundsRule, BlockParentGasLimitRule}; +pub use header_rules::{BlockNumberRule, ParentHashRule, TimestampRule, GasUsedRule, GasLimitBoundsRule, BlockParentGasLimitRule, HeaderLayoutRule}; pub use difficulty::DifficultyRule; pub use merged_mining::{BitcoinTimestampRule, MergedMiningRule}; pub use block_rules::{ diff --git a/crates/core/src/validation/tests.rs b/crates/core/src/validation/tests.rs index 3e3a895..dd6adb0 100644 --- a/crates/core/src/validation/tests.rs +++ b/crates/core/src/validation/tests.rs @@ -997,6 +997,61 @@ fn default_rsk_holds_uncles_to_rskjs_uncle_rules() { assert_eq!(verifier.uncle_parent_rules().len(), 5, "rskj's five, in its order"); } +/// rskj reads a header's fields by its height (`BlockFactory.canBeDecoded`): +/// a UMM root from RSKIPUMM, papyrus200, on and only then, and no RSKIP351 +/// fields where RSKIP351 is inactive, which is all of mainnet. The decoder +/// reads by shape, so the verifier holds what it read to that, for a block +/// and for an uncle alike, before anything else. +#[test] +fn a_header_carries_the_fields_rskj_reads_at_its_height() { + use crate::config::ChainConfig; + use crate::types::header::HeaderVersionFields; + + let mainnet = Arc::new(ChainConfig::mainnet()); + let papyrus = mainnet.activation_heights.papyrus200; + let rule = HeaderLayoutRule { config: mainnet.clone() }; + let at = |number: u64, umm: bool| { + let mut header = create_dummy_header(number, 1000, B256::ZERO); + header.umm_root = umm.then(Bytes::new); + header + }; + let misplaced = |number, present| Err(ValidationError::UmmRootMisplaced { number, present }); + + assert_eq!(rule.validate(&at(papyrus - 1, false)), Ok(())); + assert_eq!(rule.validate(&at(papyrus - 1, true)), misplaced(papyrus - 1, true)); + assert_eq!(rule.validate(&at(papyrus, true)), Ok(())); + assert_eq!(rule.validate(&at(papyrus, false)), misplaced(papyrus, false)); + + let versioned = |mut header: Header| { + header.version_fields = + Some(HeaderVersionFields { version: 1, edges: Some(Vec::new()), base_event: None }); + header + }; + let compressed = |mut header: Header| { + header.extension_data = Some(Bytes::from(vec![0xc0])); + header + }; + let tip = 9_400_000; + for header in [versioned(at(tip, true)), compressed(at(tip, true))] { + assert_eq!( + rule.validate(&header), + Err(ValidationError::VersionedBeforeRskip351 { number: tip }) + ); + } + // Testnet has RSKIP351 from reed810, #7,139,600. + let testnet = HeaderLayoutRule { config: Arc::new(ChainConfig::testnet()) }; + let reed810 = 7_139_600; + assert_eq!( + testnet.validate(&versioned(at(reed810 - 1, true))), + Err(ValidationError::VersionedBeforeRskip351 { number: reed810 - 1 }) + ); + assert_eq!(testnet.validate(&versioned(at(reed810, true))), Ok(())); + + let verifier = HeaderVerifier::default_rsk(mainnet); + assert_eq!(verifier.verify(&at(papyrus, false), None), misplaced(papyrus, false)); + assert_eq!(verifier.verify_uncle(&at(papyrus, false)), misplaced(papyrus, false)); +} + /// rskj's trunk takes the timestamp rule on the header alone. A block no /// later than its parent is one `BlockValidatorImpl` imports -- the parent /// form is for uncles, and for rskj's relay -- so this node must take it diff --git a/crates/core/src/validation/uncles.rs b/crates/core/src/validation/uncles.rs index 4027570..1a9dbc9 100644 --- a/crates/core/src/validation/uncles.rs +++ b/crates/core/src/validation/uncles.rs @@ -8,8 +8,8 @@ use super::fork_detection::MainchainView; use super::{ BitcoinTimestampRule, BlockNumberRule, BlockParentGasLimitRule, DifficultyRule, GasUsedRule, - HeaderValidator, MergedMiningRule, ParentHeaderValidator, PrevMinGasPriceRule, TimestampRule, - ValidationError, + HeaderLayoutRule, HeaderValidator, MergedMiningRule, ParentHeaderValidator, + PrevMinGasPriceRule, TimestampRule, ValidationError, }; use crate::config::ChainConfig; use crate::types::block::Block; @@ -28,7 +28,8 @@ pub const UNCLE_GENERATION_LIMIT: u64 = 7; /// rule and gas used, its `BlockHeaderCompositeRule`. rskj's timestamp rule /// is two here: [`TimestampRule`], the clock, and [`BitcoinTimestampRule`], /// RSKIP179. The fourth, fork detection, reads the chain rather than the -/// header and is [`UnclesValidationRule::fork_detection`]. +/// header and is [`UnclesValidationRule::fork_detection`]. Before them, the +/// layout `BlockFactory` reads every header in, an uncle's at its own height. /// /// Not the trunk header rules: rskj holds no uncle to the minimum gas limit /// or the 32-byte extra-data cap, so an uncle may carry either and still be @@ -37,6 +38,7 @@ pub const UNCLE_GENERATION_LIMIT: u64 = 7; /// 2^63, which the parent rule's bound keeps out of an uncle's reach. pub fn rsk_header_rules(config: Arc) -> Vec> { vec![ + Box::new(HeaderLayoutRule { config: config.clone() }), Box::new(MergedMiningRule { config: config.clone() }), Box::new(TimestampRule::new(config.max_future_block_time)), Box::new(BitcoinTimestampRule { config }), diff --git a/crates/execution/src/mining/tests.rs b/crates/execution/src/mining/tests.rs index a2e63eb..62252f9 100644 --- a/crates/execution/src/mining/tests.rs +++ b/crates/execution/src/mining/tests.rs @@ -49,6 +49,9 @@ fn test_chain_config() -> Arc { // so the ten-minute drop would only add noise. rskip97: Some(0), rskip144: None, + // From genesis too: a partitioned template carries the version + // fields RSKIP351 brings. + rskip351: Some(0), }, genesis_spec: None, skip_pow_validation: false, From f3adce4ea0a20283f97b1b3f8d419846e799ac52 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Fri, 6 Nov 2026 11:57:06 -0800 Subject: [PATCH 019/264] Hold a Bitcoin hash to rskj's target: 2^256 over max(difficulty, 3) rskj's DifficultyUtils.difficultyToTarget divides 2^256 by the difficulty, in BigInteger, and by 3 where the difficulty is 2 or less. rustock divided 2^256 - 1 and refused a difficulty of zero, which rskj judges as 3. At a difficulty of 1 every hash passed, and at 2 half of them, where rskj passes one in three; and where the difficulty is a power of two the target came out one below rskj's. On mainnet, whose difficulty is never under 7 x 10^15, only that last difference remains. core's merged_mining::difficulty_to_target is now rskj's in 256 bits: (2^256 - 1) / d, one more where d divides 2^256, never over less than 3. MergedMiningRule judges by it, and the miner re-exports it rather than keeping its own, so the target mnr_getWork hands out is the one a submission is judged by. DifficultyZero is gone. Test helpers that sealed at difficulty 1 with any nonce now mine for one, as does the load test. The miner's target test names the validator's function, not its own re-export, and compares the two at rskj's corners. docs/merged-mining.md no longer says the target is U256::MAX / d. Nine mutants are all caught: the old U256::MAX / d, a floor of 2 or 4, no extra one for powers of two or always one, a rule with a target of its own or refusing zero, and a miner keeping the old function or one without the power-of-two step. --- crates/cli/examples/loadtest.rs | 27 +++-- crates/core/src/validation/merged_mining.rs | 29 +++-- crates/core/src/validation/mod.rs | 3 - crates/core/src/validation/tests.rs | 116 +++++++++++++++++--- crates/execution/src/mining/template.rs | 13 +-- crates/execution/src/mining/tests.rs | 31 ++++-- crates/execution/src/processor.rs | 10 +- docs/merged-mining.md | 9 +- 8 files changed, 178 insertions(+), 60 deletions(-) diff --git a/crates/cli/examples/loadtest.rs b/crates/cli/examples/loadtest.rs index eb4b2cf..faf3b15 100644 --- a/crates/cli/examples/loadtest.rs +++ b/crates/cli/examples/loadtest.rs @@ -47,12 +47,9 @@ //! **Proof of work costs the same at any difficulty**, which is what makes //! this affordable: verification decodes the Bitcoin header, hashes it and //! compares against the target, and none of those depend on how hard the -//! target is. The blocks are sealed at difficulty 1, where the target is -//! `U256::MAX` and the first nonce tried always clears it, so sealing -//! costs nothing and verifying costs exactly what it costs on mainnet. -//! Difficulty 0 would be cheaper still and is not allowed -- the rule -//! rejects it with `DifficultyZero` before doing any work, which would -//! measure nothing. +//! target is. The blocks are sealed at difficulty 1, where rskj's target is +//! 2^256 / 3: one hash in three meets it, so sealing takes a few nonces and +//! verifying costs exactly what it costs on mainnet. //! //! Sealing is deliberately **outside** the timed region. A node receives //! blocks already mined; making one is not a cost it pays. @@ -774,13 +771,14 @@ fn remasc_tx() -> Transaction { /// /// Not a cost a node pays -- it receives blocks already mined -- so this /// runs outside the timed region. It is here because verification has -/// nothing to verify without it, and because at `difficulty: 1` the -/// target is `U256::MAX`, so the first nonce tried clears it and sealing -/// is a handful of hashes rather than a search. Verification does the -/// same work it would against a real mainnet target. +/// nothing to verify without it, and because at `difficulty: 1` rskj's +/// target is 2^256 / 3, which one hash in three meets, so sealing is a few +/// nonces rather than a search. Verification does the same work it would +/// against a real mainnet target. fn seal_merged_mining(header: &mut Header) -> Result<()> { + use bitcoin::hashes::Hash as _; use rustock_core::validation::fork_detection::REQUIRED_BLOCKS; - use rustock_core::validation::merged_mining::compress_coinbase; + use rustock_core::validation::merged_mining::{compress_coinbase, difficulty_to_target}; use rustock_execution::mining::{coinbase, fork_detection, merkle}; // The coinbase commits to the header's merged-mining hash, which is @@ -796,7 +794,12 @@ fn seal_merged_mining(header: &mut Header) -> Result<()> { let payload = fork_detection::apply_to_hash(header.hash_for_merged_mining(), fork_data); let cb = coinbase::build_coinbase(&payload, 0); - let btc = coinbase::build_bitcoin_block(cb.clone(), 0); + // One hash in three meets rskj's target at difficulty 1, 2^256 / 3. + let target = difficulty_to_target(header.difficulty); + let btc = (0..=u32::MAX) + .map(|nonce| coinbase::build_bitcoin_block(cb.clone(), nonce)) + .find(|block| U256::from_le_slice(block.header.block_hash().as_byte_array()) <= target) + .context("no nonce meets the target")?; let compressed = compress_coinbase(&coinbase::encode_transaction(&cb), true) .map_err(|e| anyhow::anyhow!("compressing the coinbase: {e}"))?; let proof = merkle::proof_from_txids(&coinbase::txids_display_order(&btc)) diff --git a/crates/core/src/validation/merged_mining.rs b/crates/core/src/validation/merged_mining.rs index d7bfcc9..685a9cb 100644 --- a/crates/core/src/validation/merged_mining.rs +++ b/crates/core/src/validation/merged_mining.rs @@ -107,6 +107,25 @@ impl HeaderValidator for BitcoinTimestampRule { } } +/// rskj `DifficultyUtils.difficultyToTarget`: 2^256 over the difficulty, or +/// over 3 where the difficulty is 2 or less, so that the target never needs +/// bit 255. A merge-mining Bitcoin header's hash, read as a number, must not +/// exceed it. +/// +/// 2^256 is past a `U256`. floor(2^256 / d) is floor((2^256 - 1) / d) except +/// where d divides 2^256, a power of two, where it is one more. +pub fn difficulty_to_target(difficulty: alloy_primitives::U256) -> alloy_primitives::U256 { + use alloy_primitives::U256; + let divisor = difficulty.max(U256::from(3)); + let below = U256::MAX / divisor; + if divisor.is_power_of_two() { + below + U256::from(1) + } else { + below + + } +} + impl HeaderValidator for MergedMiningRule { fn validate(&self, header: &Header) -> Result<(), ValidationError> { use bitcoin::consensus::Decodable; @@ -136,15 +155,7 @@ impl HeaderValidator for MergedMiningRule { .map_err(|_| ValidationError::BitcoinHeaderDecodeError)?; // 1. Bitcoin PoW vs RSK difficulty - let difficulty = header.difficulty; - if difficulty.is_zero() { - return Err(ValidationError::DifficultyZero); - } - let target = if difficulty > U256::MAX { - U256::ZERO - } else { - U256::MAX / difficulty - }; + let target = difficulty_to_target(header.difficulty); let btc_hash = btc_header.block_hash(); let btc_hash_u256 = U256::from_le_slice(btc_hash.as_byte_array()); if btc_hash_u256 > target { diff --git a/crates/core/src/validation/mod.rs b/crates/core/src/validation/mod.rs index a5bfe50..2113bd2 100644 --- a/crates/core/src/validation/mod.rs +++ b/crates/core/src/validation/mod.rs @@ -34,9 +34,6 @@ pub enum ValidationError { #[error("Difficulty mismatch: expected {expected}, got {got}")] DifficultyMismatch { expected: U256, got: U256 }, - #[error("Difficulty is zero")] - DifficultyZero, - #[error("Failed to decode Bitcoin block header")] BitcoinHeaderDecodeError, diff --git a/crates/core/src/validation/tests.rs b/crates/core/src/validation/tests.rs index dd6adb0..be344fd 100644 --- a/crates/core/src/validation/tests.rs +++ b/crates/core/src/validation/tests.rs @@ -90,7 +90,7 @@ fn build_mm_proof(header: &Header, btc_bits: u32, rsk_tag_hash: Option) -> merkle_root_bytes.copy_from_slice(&txid_bytes); let merkle_root = bitcoin::TxMerkleNode::from_byte_array(merkle_root_bytes); - let btc_header = BtcHeader { + let mut btc_header = BtcHeader { version: bitcoin::block::Version::from_consensus(1), prev_blockhash: bitcoin::BlockHash::all_zeros(), merkle_root, @@ -98,6 +98,7 @@ fn build_mm_proof(header: &Header, btc_bits: u32, rsk_tag_hash: Option) -> bits: bitcoin::CompactTarget::from_consensus(btc_bits), nonce: 0, }; + mine(&mut btc_header, header.difficulty); let mut btc_header_bytes = Vec::new(); btc_header.consensus_encode(&mut btc_header_bytes).unwrap(); @@ -108,6 +109,19 @@ fn build_mm_proof(header: &Header, btc_bits: u32, rsk_tag_hash: Option) -> (Bytes::from(btc_header_bytes), Bytes::from(compressed), merkle_proof) } +/// Roll `btc`'s nonce until its hash meets rskj's target for `difficulty`, as +/// a miner does. At the difficulties tests use, a few nonces do it. +fn mine(btc: &mut BtcHeader, difficulty: U256) { + let target = merged_mining::difficulty_to_target(difficulty); + for _ in 0..1_000_000 { + if U256::from_le_slice(btc.block_hash().as_byte_array()) <= target { + return; + } + btc.nonce += 1; + } + panic!("no nonce meets the target for difficulty {difficulty}"); +} + #[test] fn test_block_number_rule() { let parent = create_dummy_header(10, 100, B256::ZERO); @@ -243,13 +257,17 @@ fn test_merged_mining_rule_invalid_pow() { let config = Arc::new(crate::config::ChainConfig::regtest()); let rule = MergedMiningRule { config }; let mut header = create_dummy_header(10, 1000, B256::ZERO); - header.difficulty = U256::MAX; - + header.difficulty = U256::from(1); + let (btc_h, btc_cb, btc_m) = build_mm_proof(&header, 0x207fffff, None); header.bitcoin_merged_mining_header = Some(btc_h); header.bitcoin_merged_mining_coinbase_transaction = Some(btc_cb); header.bitcoin_merged_mining_merkle_proof = Some(btc_m); - + assert_eq!(rule.validate(&header), Ok(())); + + // The same Bitcoin header, held to the hardest target: proof of work is + // judged before the coinbase, whose tag the new difficulty also moves. + header.difficulty = U256::MAX; let res = rule.validate(&header); assert!(matches!(res, Err(ValidationError::BitcoinPowInvalid { .. }))); } @@ -300,6 +318,75 @@ fn test_merged_mining_rule_wrong_tag() { assert!(matches!(res, Err(ValidationError::BitcoinCoinbaseTagInvalid))); } +/// rskj's target is 2^256 over the difficulty, in `BigInteger`, and over 3 +/// where the difficulty is 2 or less, so it never needs bit 255. In 256 +/// bits that is (2^256 - 1) / d, but one more where d divides 2^256. +#[test] +fn the_target_is_two_to_the_256_over_the_difficulty_and_never_over_less_than_3() { + use merged_mining::difficulty_to_target as target; + let one = U256::from(1); + // 2^256 = 3q + 1, so (2^256 - 1) / 3 is q itself. + let third = U256::MAX / U256::from(3); + for difficulty in [0u64, 1, 2, 3] { + assert_eq!( + target(U256::from(difficulty)), + third, + "difficulty {difficulty}" + ); + } + assert_eq!(target(U256::from(4)), one << 254); + assert_eq!(target(U256::from(5)), U256::MAX / U256::from(5)); + assert_eq!(target(one << 64), one << 192); + assert_eq!(target(one << 255), U256::from(2)); + assert_eq!(target(U256::MAX), one); + let floor = U256::from(7_000_000_000_000_000u64); + assert_eq!( + target(floor), + U256::MAX / floor, + "mainnet's least difficulty" + ); +} + +/// The rule holds a Bitcoin hash to that target. At a difficulty of 2 or +/// less it is 2^256 / 3, so two hashes in three fall short, where every one +/// used to pass; and a difficulty of 0 is judged the same, not refused. +#[test] +fn proof_of_work_meets_rskjs_target_at_the_least_difficulties() { + let rule = regtest_merged_mining(); + let third = U256::MAX / U256::from(3); + let hash_of = |btc: &BtcHeader| U256::from_le_slice(btc.block_hash().as_byte_array()); + for difficulty in [0u64, 1, 2] { + let mut header = header_at(100); + header.difficulty = U256::from(difficulty); + let (btc, coinbase, proof) = build_mm_proof(&header, 0x207fffff, None); + header.bitcoin_merged_mining_coinbase_transaction = Some(coinbase); + header.bitcoin_merged_mining_merkle_proof = Some(proof); + let mut btc: BtcHeader = bitcoin::consensus::deserialize(&btc).unwrap(); + + assert!(hash_of(&btc) <= third); + header.bitcoin_merged_mining_header = + Some(Bytes::from(bitcoin::consensus::serialize(&btc))); + assert_eq!( + rule.validate(&header), + Ok(()), + "difficulty {difficulty}, under the target" + ); + + while hash_of(&btc) <= third { + btc.nonce += 1; + } + header.bitcoin_merged_mining_header = + Some(Bytes::from(bitcoin::consensus::serialize(&btc))); + assert!( + matches!( + rule.validate(&header), + Err(ValidationError::BitcoinPowInvalid { target, .. }) if target == third + ), + "difficulty {difficulty}, over the target" + ); + } +} + // --- Difficulty rule tests (mainnet) --- #[test] @@ -771,10 +858,7 @@ fn seal(header: &mut Header, btc_time: u64) { let (btc, coinbase, proof) = build_mm_proof(header, 0x207fffff, None); let mut btc: BtcHeader = bitcoin::consensus::deserialize(&btc).unwrap(); btc.time = btc_time as u32; - let target = U256::MAX / header.difficulty; - while U256::from_le_slice(btc.block_hash().as_byte_array()) > target { - btc.nonce += 1; - } + mine(&mut btc, header.difficulty); header.bitcoin_merged_mining_header = Some(Bytes::from(bitcoin::consensus::serialize(&btc))); header.bitcoin_merged_mining_coinbase_transaction = Some(coinbase); header.bitcoin_merged_mining_merkle_proof = Some(proof); @@ -1077,9 +1161,9 @@ fn a_block_no_later_than_its_parent_joins_the_trunk_but_is_no_uncle() { // --- the merged-mining proof, byte for byte as rskj's `ProofOfWorkRule` -/// A regtest header at `number`, at difficulty 1, where any Bitcoin hash -/// meets the target and only the coinbase and the merkle proof are in -/// question. +/// A regtest header at `number`, at difficulty 1, where a Bitcoin hash meets +/// rskj's target, 2^256 / 3, one time in three: the helpers mine for one +/// that does, so only the coinbase and the merkle proof are in question. fn header_at(number: u64) -> Header { let mut header = create_dummy_header(number, 1000, B256::ZERO); header.difficulty = U256::from(1); @@ -1109,8 +1193,13 @@ fn tail_committing_to(committed: &[u8], before: usize, after: usize) -> Vec /// `header` merge-mined through `coinbase` and `proof`, by a Bitcoin header /// holding `wire_root` as its merkle root. -fn mined_through(mut header: Header, coinbase: Vec, proof: Vec, wire_root: [u8; 32]) -> Header { - let btc = BtcHeader { +fn mined_through( + mut header: Header, + coinbase: Vec, + proof: Vec, + wire_root: [u8; 32], +) -> Header { + let mut btc = BtcHeader { version: bitcoin::block::Version::from_consensus(1), prev_blockhash: bitcoin::BlockHash::all_zeros(), merkle_root: bitcoin::TxMerkleNode::from_byte_array(wire_root), @@ -1118,6 +1207,7 @@ fn mined_through(mut header: Header, coinbase: Vec, proof: Vec, wire_roo bits: bitcoin::CompactTarget::from_consensus(0x207fffff), nonce: 0, }; + mine(&mut btc, header.difficulty); header.bitcoin_merged_mining_header = Some(Bytes::from(bitcoin::consensus::serialize(&btc))); header.bitcoin_merged_mining_coinbase_transaction = Some(Bytes::from(coinbase)); header.bitcoin_merged_mining_merkle_proof = Some(Bytes::from(proof)); diff --git a/crates/execution/src/mining/template.rs b/crates/execution/src/mining/template.rs index e572995..20bfaff 100644 --- a/crates/execution/src/mining/template.rs +++ b/crates/execution/src/mining/template.rs @@ -68,7 +68,7 @@ pub struct BlockTemplate { /// the header's merged-mining hash, then RSKIP110 fork-detection data. /// This, not the block hash, is what a submission is looked up by. pub hash_for_merged_mining: B256, - /// `U256::MAX / difficulty`: the Bitcoin block hash, read little-endian, + /// [`difficulty_to_target`]: the Bitcoin block hash, read little-endian, /// must not exceed this. pub target: U256, /// Fees this block would pay its miner, for a pool deciding whether the @@ -785,11 +785,6 @@ pub fn next_minimum_gas_price(parent_price: U256, target: Option) -> U256 target.clamp(lower, upper) } -/// rskj `DifficultyUtils.difficultyToTarget`. -pub fn difficulty_to_target(difficulty: U256) -> U256 { - if difficulty.is_zero() { - U256::MAX - } else { - U256::MAX / difficulty - } -} +/// rskj `DifficultyUtils.difficultyToTarget`, the validator's own, so that the +/// target handed out is the one a submission is judged by. +pub use rustock_core::validation::merged_mining::difficulty_to_target; diff --git a/crates/execution/src/mining/tests.rs b/crates/execution/src/mining/tests.rs index 62252f9..07dee8f 100644 --- a/crates/execution/src/mining/tests.rs +++ b/crates/execution/src/mining/tests.rs @@ -70,8 +70,8 @@ fn header_at(number: u64, parent_hash: B256, state_root: B256) -> Header { receipts_root: B256::ZERO, logs_bloom: Default::default(), extension_data: None, - // Small enough that every Bitcoin hash clears the target, so the tests - // never have to search for a nonce. + // Small: rskj takes a difficulty of 2 or less as 3, a target of + // 2^256 / 3, so a nonce search clears it in a few tries. difficulty: U256::from(2), number, gas_limit: U256::from(6_800_000), @@ -255,9 +255,10 @@ fn fixture_on( /// which is to say, actually mined. /// /// The search is the point: RSK compares the Bitcoin block hash, read -/// little-endian, against `U256::MAX / difficulty`, so a block built with a -/// fixed nonce clears the target only by luck. At these difficulties a handful -/// of nonces suffice, which is what makes the round trip a unit test. +/// little-endian, against the target rskj derives from the difficulty -- at +/// best 2^256 / 3 -- so a block built with a fixed nonce clears the target +/// only by luck. At these difficulties a handful of nonces suffice, which is +/// what makes the round trip a unit test. fn solve(work: &MinerWork) -> bitcoin::Block { let coinbase = coinbase::build_coinbase(&work.block_hash_for_merged_mining, 0xDEAD_BEEF); for nonce in 0..100_000u32 { @@ -1185,13 +1186,27 @@ fn a_solution_that_misses_the_target_is_refused() { } /// `mnr_getWork` hands out the target the Bitcoin hash is compared against, -/// and it has to be the same number the verifier derives from the difficulty. +/// and it has to be the same number the verifier derives from the difficulty: +/// the validator's own function, not one the miner keeps beside it. #[test] fn the_advertised_target_matches_the_difficulty() { + use rustock_core::validation::merged_mining::difficulty_to_target as validators; let fx = fixture(20); let template = fx.build().unwrap(); - assert_eq!(template.target, U256::MAX / template.block.header.difficulty); - assert_eq!(difficulty_to_target(U256::from(1)), U256::MAX); + assert_eq!( + template.target, + validators(template.block.header.difficulty) + ); + // At rskj's corners too: 2^256 over 3 for a difficulty of 2 or less, never + // all of 2^256, and a power of two dividing 2^256 exactly. + for d in [0u64, 1, 2, 3, 4, 5, 1 << 20] { + let d = U256::from(d); + assert_eq!(difficulty_to_target(d), validators(d), "difficulty {d}"); + } + assert_eq!( + difficulty_to_target(U256::from(1)), + U256::MAX / U256::from(3) + ); } /// A mined block that loses the total-difficulty comparison is still stored -- diff --git a/crates/execution/src/processor.rs b/crates/execution/src/processor.rs index 057b2ad..c2393dd 100644 --- a/crates/execution/src/processor.rs +++ b/crates/execution/src/processor.rs @@ -2879,15 +2879,21 @@ mod tests { /// Merge-mine `header` as a miner would, committing to `fork_data` after /// its merged-mining hash, beside a Bitcoin header of its own time. Its - /// difficulty must be 1, which any Bitcoin hash meets. + /// difficulty must be small: at 1, rskj's target is 2^256 / 3, which a + /// few nonces meet. fn seal(header: &mut Header, fork_data: &[u8]) { use crate::mining::{coinbase, fork_detection, merkle}; - use rustock_core::validation::merged_mining::compress_coinbase; + use bitcoin::hashes::Hash as _; + use rustock_core::validation::merged_mining::{compress_coinbase, difficulty_to_target}; let payload = fork_detection::apply_to_hash(header.hash_for_merged_mining(), fork_data); let cb = coinbase::build_coinbase(&payload, 0); let mut btc = coinbase::build_bitcoin_block(cb.clone(), 0); btc.header.time = header.timestamp as u32; + let target = difficulty_to_target(header.difficulty); + while U256::from_le_slice(btc.header.block_hash().as_byte_array()) > target { + btc.header.nonce += 1; + } let compressed = compress_coinbase(&coinbase::encode_transaction(&cb), true).unwrap(); let proof = merkle::proof_from_txids(&coinbase::txids_display_order(&btc)).unwrap(); header.bitcoin_merged_mining_header = Some(coinbase::encode_header(&btc.header).into()); diff --git a/docs/merged-mining.md b/docs/merged-mining.md index 02d28e3..76f4641 100644 --- a/docs/merged-mining.md +++ b/docs/merged-mining.md @@ -62,8 +62,9 @@ breaks them: `feesPaidToMiner` is a **decimal** string, and last 12 are replaced with RSKIP110 fork-detection data (§4). 3. A miner puts `RSKBLOCK:` followed by those 32 bytes in a Bitcoin coinbase and searches for a Bitcoin block whose hash, read little-endian, is at most - `U256::MAX / difficulty`. That is **RSK's** difficulty, not Bitcoin's, so a - solution is usually not a valid Bitcoin block. + rskj's target: 2^256 / difficulty, or 2^256 / 3 where the difficulty is 2 or + less (`difficulty_to_target`). That is **RSK's** difficulty, not Bitcoin's, + so a solution is usually not a valid Bitcoin block. 4. The solution comes back and fills in the three `bitcoin_merged_mining_*` header fields: the 80-byte Bitcoin header, the compressed coinbase, and the merkle proof linking the coinbase to the Bitcoin merkle root. @@ -433,8 +434,8 @@ runs on blocks arriving from peers. The search is not decorative: before it existed the tests used a fixed nonce, cleared the target only by luck, and that is how the `get_work` notify bug surfaced. -The difficulty is 2, so the target is `2^255 - 1` and a nonce is found in a few -tries. Two heights are used deliberately: +The difficulty is 2, which rskj takes as 3, so the target is 2^256 / 3 and a +nonce is found in a few tries. Two heights are used deliberately: - Most tests mine #21, which is fast but **does not exercise REMASC**: `RskExecutor::new` hardcodes `RemascConfig::mainnet()` whatever the chain id, From e34d889fa20203f69375efc3501d8897457fb676 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Fri, 6 Nov 2026 12:19:35 -0800 Subject: [PATCH 020/264] Check every stored header beside the live node, fork detection included Freezer::open_read_only opens a freezer to read beside the process that writes it. It creates nothing and repairs nothing: open truncates a data file past what the index accounts for, which in another process's freezer is that writer's append in progress. It refuses every write before a file is touched, since rolling to a new data file opens one to create and truncate, and read-only handles alone would not stop that. verify_stored_headers attaches it, so a snapshot-synced node, which indexes by number only the window it fetched bodies for, answers every height. It now also checks fork-detection data as BlockProcessor does, against the 449 blocks below each block: each thread holds the headers of its walk until those are in hand, and reads on past its range. On a mainnet store synced from genesis to #9,392,253, every header passes the trunk rules, with none out of order, orphaned or missing, and fork-detection data matches on all 7,801,254 blocks from wasabi100. As controls, on the same store at #9,381,160, a halved target failed 4,254,513 headers, a UMM root required one block late failed #2,392,700 alone, and a zeroed uncle-count byte failed all 7,790,161. --- crates/cli/examples/verify_stored_headers.rs | 203 +++++++++++++++---- crates/storage/src/freezer.rs | 138 ++++++++++++- 2 files changed, 295 insertions(+), 46 deletions(-) diff --git a/crates/cli/examples/verify_stored_headers.rs b/crates/cli/examples/verify_stored_headers.rs index 631b34c..94bd516 100644 --- a/crates/cli/examples/verify_stored_headers.rs +++ b/crates/cli/examples/verify_stored_headers.rs @@ -3,19 +3,26 @@ //! //! Each header is checked on its own and against its parent, ranges of the //! chain split across threads, walking back by parent hash within each. The -//! store is opened read-only, so this can run against a copy of a node's -//! data or the store of a node that is stopped. +//! store and its header freezer are both opened read-only, so this can run +//! beside the node that writes them. +//! +//! Fork-detection data (rskj `ForkDetectionDataRule`, from wasabi100) is +//! checked as the node checks it, against the 449 blocks below each block: +//! a thread holds each header until its walk has reached them, and reads on +//! past its range's bottom to finish the range's lowest blocks. //! //! It also counts blocks no later than their parent, which rskj's trunk //! takes and its relay would not: zero is what an honest chain shows. //! //! Usage: verify_stored_headers [lowest-block] [highest-block] +use alloy_primitives::B256; use anyhow::Context; use rustock_core::config::ChainConfig; -use rustock_core::validation::HeaderVerifier; +use rustock_core::validation::{fork_detection, merged_mining, HeaderVerifier}; +use rustock_core::Header; use rustock_storage::BlockStore; -use std::collections::BTreeMap; +use std::collections::{BTreeMap, VecDeque}; use std::sync::atomic::{AtomicU64, Ordering}; use std::sync::{Arc, Mutex}; use std::time::Instant; @@ -30,20 +37,111 @@ struct Failures { first: String, } +/// Counts a failure under its rule, the error's variant name, keeping the +/// first in full and the first few block numbers. +fn record(failures: &Mutex>, number: u64, error: &impl std::fmt::Debug) { + let debug = format!("{error:?}"); + let rule = debug + .split(|c: char| !c.is_alphanumeric()) + .next() + .unwrap_or_default() + .to_string(); + let mut failures = failures.lock().unwrap(); + let entry = failures.entry(rule).or_default(); + entry.count += 1; + if entry.blocks.len() < EXAMPLES { + entry.blocks.push(number); + } + if entry.first.is_empty() { + entry.first = debug; + } +} + +/// The blocks below the newest a walk holds, newest first. +struct Below<'a>(&'a VecDeque
); + +impl fork_detection::MainchainView for Below<'_> { + fn headers_from(&self, from: B256, count: u64) -> Vec
{ + // Only if they are the chain below `from`: a walk holds no window + // across a hole. + if self.0.get(1).map(|h| h.hash()) != Some(from) { + return Vec::new(); + } + self.0 + .iter() + .skip(1) + .take(count as usize) + .cloned() + .collect() + } +} + +/// Takes the next header of a descending walk. Once the 449 blocks below the +/// newest held are in hand, that one's fork-detection data is checked as +/// `BlockProcessor::validate_block_rules` checks it, and it is let go. +fn carry( + window: &mut VecDeque
, + header: Header, + wasabi100: u64, + failures: &Mutex>, + fork_checked: &AtomicU64, +) { + window.push_back(header); + if window.len() as u64 <= fork_detection::REQUIRED_BLOCKS { + return; + } + let block = &window[0]; + if block.number >= wasabi100 && block.number > fork_detection::REQUIRED_BLOCKS { + let actual = merged_mining::extract_fork_detection_data(block); + if let Err(e) = fork_detection::validate(block, actual, &Below(window), wasabi100) { + record(failures, block.number, &e); + } + fork_checked.fetch_add(1, Ordering::Relaxed); + } + window.pop_front(); +} + fn main() -> anyhow::Result<()> { let mut args = std::env::args().skip(1); let dir = args .next() .context("usage: verify_stored_headers [lowest-block] [highest-block]")?; let store = BlockStore::open_read_only(&dir)?; + // A snapshot-synced node indexes by number only the window it fetched + // bodies for, and answers every height below from the freezer. + let freezer_dir = std::path::Path::new(&dir).join("freezer"); + if freezer_dir.exists() { + let freezer = rustock_storage::freezer::Freezer::open_read_only(&freezer_dir)?; + println!( + "freezer {}: {} header(s)", + freezer_dir.display(), + freezer.end_number() + ); + store.set_freezer(Arc::new(freezer)); + } let head_hash = store.head()?.context("the store has no head")?; - let head = store.header(head_hash)?.context("the head header is missing")?; - let lowest: u64 = args.next().map(|s| s.parse()).transpose()?.unwrap_or(1).max(1); - let highest: u64 = args.next().map(|s| s.parse()).transpose()?.unwrap_or(head.number); + let head = store + .header(head_hash)? + .context("the head header is missing")?; + let lowest: u64 = args + .next() + .map(|s| s.parse()) + .transpose()? + .unwrap_or(1) + .max(1); + let highest: u64 = args + .next() + .map(|s| s.parse()) + .transpose()? + .unwrap_or(head.number); anyhow::ensure!(lowest <= highest, "#{lowest} is above #{highest}"); - let verifier = HeaderVerifier::default_rsk(Arc::new(ChainConfig::mainnet())); - let threads = std::thread::available_parallelism().map(|n| n.get() as u64).unwrap_or(4); + let config = Arc::new(ChainConfig::mainnet()); + let wasabi100 = config.activation_heights.wasabi100; + let verifier = HeaderVerifier::default_rsk(config); + let threads = std::thread::available_parallelism() + .map(|n| n.get() as u64) + .unwrap_or(4); let span = (highest - lowest + 1).div_ceil(threads); println!( "store {dir}\n head #{}\n verifying #{highest} down to #{lowest} on {threads} threads", @@ -57,18 +155,29 @@ fn main() -> anyhow::Result<()> { // canonical numbers the store has no header for at all. let orphaned = AtomicU64::new(0); let missing = AtomicU64::new(0); + let fork_checked = AtomicU64::new(0); let started = Instant::now(); std::thread::scope(|scope| -> anyhow::Result<()> { let mut workers = Vec::new(); for t in 0..threads { - let Some(top) = highest.checked_sub(t * span) else { break }; + let Some(top) = highest.checked_sub(t * span) else { + break; + }; if top < lowest { break; } let bottom = top.saturating_sub(span - 1).max(lowest); - let (store, verifier, failures, checked, not_later, orphaned, missing) = - (&store, &verifier, &failures, &checked, ¬_later, &orphaned, &missing); + let (store, verifier, failures, checked, not_later, orphaned, missing, fork_checked) = ( + &store, + &verifier, + &failures, + &checked, + ¬_later, + &orphaned, + &missing, + &fork_checked, + ); workers.push(scope.spawn(move || -> anyhow::Result<()> { // The highest block from `number` down to this range's bottom // that the store holds, counting the numbers it does not. @@ -88,25 +197,16 @@ fn main() -> anyhow::Result<()> { number -= 1; } }; - let Some(mut header) = held_from(top)? else { return Ok(()) }; - loop { + let Some(mut header) = held_from(top)? else { + return Ok(()); + }; + // Fork detection reads the 449 blocks below a block, which + // this walk reaches after it: each header is held till then. + let mut window: VecDeque
= VecDeque::new(); + let below = loop { let parent = store.header(header.parent_hash)?; if let Err(e) = verifier.verify(&header, parent.as_ref()) { - let debug = format!("{e:?}"); - let rule = debug - .split(|c: char| !c.is_alphanumeric()) - .next() - .unwrap_or_default() - .to_string(); - let mut failures = failures.lock().unwrap(); - let entry = failures.entry(rule).or_default(); - entry.count += 1; - if entry.blocks.len() < EXAMPLES { - entry.blocks.push(header.number); - } - if entry.first.is_empty() { - entry.first = debug; - } + record(failures, header.number, &e); } match &parent { Some(parent) if header.timestamp <= parent.timestamp => { @@ -121,18 +221,36 @@ fn main() -> anyhow::Result<()> { if done % 500_000 == 0 { println!(" {done} checked, {:.0}s", started.elapsed().as_secs_f64()); } + carry( + &mut window, + header.clone(), + wasabi100, + failures, + fork_checked, + ); if header.number <= bottom { - break; + break parent; } header = match parent { Some(parent) => parent, // A hole in the store: go on from the next block below - // it that the store holds. - None => match held_from(header.number - 1)? { - Some(found) => found, - None => return Ok(()), - }, + // it that the store holds, with no window across it. + None => { + window.clear(); + match held_from(header.number - 1)? { + Some(found) => found, + None => return Ok(()), + } + } }; + }; + // The range's lowest blocks are checked against blocks below + // it, which the next range verifies: read on to them. + let mut next = below; + while window.front().is_some_and(|h| h.number >= bottom) { + let Some(header) = next else { break }; + next = store.header(header.parent_hash)?; + carry(&mut window, header, wasabi100, failures, fork_checked); } Ok(()) })); @@ -151,7 +269,10 @@ fn main() -> anyhow::Result<()> { started.elapsed().as_secs_f64() ); for (rule, f) in &failures { - println!(" {rule}: {} -- e.g. #{:?}\n first: {}", f.count, f.blocks, f.first); + println!( + " {rule}: {} -- e.g. #{:?}\n first: {}", + f.count, f.blocks, f.first + ); } println!( " blocks no later than their parent: {}", @@ -160,10 +281,20 @@ fn main() -> anyhow::Result<()> { let (orphaned, missing) = (orphaned.into_inner(), missing.into_inner()); println!(" checked without their parent, which the store lacks: {orphaned}"); println!(" canonical numbers with no header in the store: {missing}"); + let fork_checked = fork_checked.into_inner(); + let fork_from = lowest + .max(wasabi100) + .max(fork_detection::REQUIRED_BLOCKS + 1); + let fork_expected = (highest + 1).saturating_sub(fork_from); + println!(" fork-detection data checked on {fork_checked} blocks, from #{fork_from}"); let expected = highest - lowest + 1; anyhow::ensure!( checked + missing == expected, "checked {checked} and {missing} missing, expected {expected} in all" ); + anyhow::ensure!( + missing > 0 || fork_checked == fork_expected, + "fork-detection data checked on {fork_checked} blocks, expected {fork_expected}" + ); Ok(()) } diff --git a/crates/storage/src/freezer.rs b/crates/storage/src/freezer.rs index a592441..848d84a 100644 --- a/crates/storage/src/freezer.rs +++ b/crates/storage/src/freezer.rs @@ -145,16 +145,7 @@ fn open_store(dir: &Path, prefix: &str, kind: &str) -> Result { } let end_number = index_len / INDEX_ENTRY_BYTES; - // Find the newest data file on disk; the index does not record which one - // is being appended to, only where each block landed. - let mut file_number = 0u16; - for n in 0..=u16::MAX { - if dir.join(format!("{prefix}{kind}-{n:04}.cdat")).exists() { - file_number = n; - } else if n > 0 { - break; - } - } + let file_number = newest_data_file(dir, prefix, kind); let data_path = dir.join(format!("{prefix}{kind}-{file_number:04}.cdat")); let data = OpenOptions::new() @@ -182,6 +173,49 @@ fn open_store(dir: &Path, prefix: &str, kind: &str) -> Result { }) } +/// The newest data file on disk: the index does not record which one is +/// being appended to, only where each block landed. +fn newest_data_file(dir: &Path, prefix: &str, kind: &str) -> u16 { + let mut file_number = 0u16; + for n in 0..=u16::MAX { + if dir.join(format!("{prefix}{kind}-{n:04}.cdat")).exists() { + file_number = n; + } else if n > 0 { + break; + } + } + file_number +} + +/// Opens one of the parallel stores to read only. +/// +/// Nothing is created and nothing is repaired. In a freezer another process +/// has open, data past the index's last entry is that writer's append in +/// progress, and a ragged index tail its entry half-written: `open_store` +/// would cut both away, so this leaves both alone and counts whole entries. +fn open_store_read_only(dir: &Path, prefix: &str, kind: &str) -> Result { + let index_path = dir.join(format!("{prefix}{kind}.cidx")); + let index = + File::open(&index_path).with_context(|| format!("opening {}", index_path.display()))?; + let end_number = index.metadata()?.len() / INDEX_ENTRY_BYTES; + + let file_number = newest_data_file(dir, prefix, kind); + let data_path = dir.join(format!("{prefix}{kind}-{file_number:04}.cdat")); + let data = + File::open(&data_path).with_context(|| format!("opening {}", data_path.display()))?; + let data_len = u32::try_from(data.metadata()?.len()).unwrap_or(u32::MAX); + + Ok(Writable { + index, + data, + file_number, + data_len, + end_number, + pending_data: Vec::new(), + pending_index: Vec::new(), + }) +} + /// Where one block's data is: which file, and the byte range within it. #[derive(Clone, Copy, Debug, PartialEq, Eq)] struct IndexEntry { @@ -250,6 +284,8 @@ pub struct Freezer { /// can neither recompute total difficulty nor prove the uncles its chain /// absorbed ever existed. uncles: Mutex, + /// Opened by [`Freezer::open_read_only`]: every write is refused. + read_only: bool, } /// File-name stems for the two parallel stores. @@ -269,6 +305,36 @@ impl Freezer { Self::open_with_prefix(dir, "") } + /// Opens the freezer in `dir` to read beside the node that writes it, as + /// a diagnostic does. + /// + /// Nothing is created, repaired or written: see `open_store_read_only`. + /// What it holds is the freezer as it stood when opened, and every write + /// is refused before it touches a file. + pub fn open_read_only(dir: impl AsRef) -> Result { + let dir = dir.as_ref().to_path_buf(); + let headers = open_store_read_only(&dir, "", HEADERS)?; + let uncles = open_store_read_only(&dir, "", UNCLES)?; + Ok(Self { + dir, + prefix: String::new(), + write: Mutex::new(headers), + uncles: Mutex::new(uncles), + read_only: true, + }) + } + + /// Refuses a write to a freezer opened read-only. Rolling to a new data + /// file opens it to create and truncate, which in another process's + /// freezer is that writer's next file, so read-only handles alone would + /// not be enough. + fn writable(&self) -> Result<()> { + if self.read_only { + bail!("the freezer at {} is open read-only", self.dir.display()); + } + Ok(()) + } + /// Opens a staging freezer for a chain that is not yet trusted. /// /// A snapshot sync walks down from a checkpoint a *peer* offered. Each @@ -313,6 +379,7 @@ impl Freezer { prefix, write: Mutex::new(headers), uncles: Mutex::new(uncles), + read_only: false, }) } @@ -461,6 +528,7 @@ impl Freezer { number: u64, encoded: Vec, ) -> Result<()> { + self.writable()?; let mut w = store.lock().unwrap(); // Roll before writing, never across: a header must be contiguous @@ -681,6 +749,7 @@ impl Freezer { /// order there is no single offset above which everything belongs to the /// dropped blocks, and unreferenced bytes cost only space. pub fn truncate_head(&self, number: u64) -> Result<()> { + self.writable()?; // Both stores are addressed by block number, so a head that is rolled // back must drop the same range from each. Leaving uncles behind for a // header that no longer exists would make the uncle store answer for @@ -964,6 +1033,55 @@ pub(crate) mod tests { assert_eq!(f.header(19).unwrap().unwrap().number, 19); } + /// A read-only open, beside a writer mid-append, reads what is frozen, + /// cuts away neither the writer's data nor its half-written index entry, + /// and refuses every write. + #[test] + fn a_read_only_open_reads_and_leaves_a_writers_append_alone() { + let dir = tempfile::tempdir().unwrap(); + { + let f = Freezer::open(dir.path()).unwrap(); + for n in 0..20 { + f.put(n, &header(n)).unwrap(); + } + f.sync().unwrap(); + } + let data_path = dir.path().join("headers-0000.cdat"); + let index_path = dir.path().join("headers.cidx"); + let append = |path: &Path, bytes: &[u8]| { + OpenOptions::new() + .append(true) + .open(path) + .unwrap() + .write_all(bytes) + .unwrap() + }; + append(&data_path, &[0xAA; 512]); + append(&index_path, &[0x01; 4]); + let len = |path: &Path| std::fs::metadata(path).unwrap().len(); + let (data_len, index_len) = (len(&data_path), len(&index_path)); + + let f = Freezer::open_read_only(dir.path()).unwrap(); + assert_eq!(f.end_number(), 20, "whole index entries only"); + assert_eq!(f.run(0, 20).unwrap().iter().flatten().count(), 20); + assert!(f.put(20, &header(20)).is_err()); + assert!(f.put_uncles(20, &[]).is_err()); + assert!(f.truncate_head(10).is_err()); + assert_eq!(len(&data_path), data_len, "the writer's data is not cut"); + assert_eq!(len(&index_path), index_len, "nor its index entry"); + assert_eq!(f.header(15).unwrap().unwrap().number, 15); + } + + /// Where there is no freezer, a read-only open fails and makes none. + #[test] + fn a_read_only_open_creates_nothing() { + let dir = tempfile::tempdir().unwrap(); + assert!(Freezer::open_read_only(dir.path()).is_err()); + assert_eq!(std::fs::read_dir(dir.path()).unwrap().count(), 0); + assert!(Freezer::open_read_only(dir.path().join("absent")).is_err()); + assert!(!dir.path().join("absent").exists()); + } + #[test] fn truncate_head_drops_from_a_point_upwards() { let (f, _d) = freezer(); From 391abb3a6b937bd75b00dd01ae231ad182b9fca7 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Fri, 6 Nov 2026 14:13:41 -0800 Subject: [PATCH 021/264] Hold fallback-mined blocks to rskj's signature check Below RSKIP98 (orchid), rskj's ProofOfWorkRule takes a block with no coinbase and no merkle proof, at no more than the network's fallback-mining difficulty, as fallback-mined: its Bitcoin-header field holds an RLP list [v, r, s], a signature of the header's merged-mining hash by one of two fixed keys, the first at even heights and the second at odd ones. rustock skipped proof of work below orchid altogether, so such a block passed with any signature or none. MergedMiningRule now asks rskj's question first and checks the signature as validFallbackBlockSignature does: by rskj's lenient RLP (rskj_rlp, a port of RLP.decode2), exactly one list of exactly three, a one-byte v of 27 to 30 read as a Java byte, r and s exactly as BigInteger.toByteArray writes them, s below half the curve order, and the key recovered by libsecp256k1, rskj's default native library. ChainConfig carries the fallback-mining difficulty per network, as rskj's Constants do: 14 x 10^15 on mainnet and testnet, zero on regtest. Merged mining below orchid is still unchecked; its partial merkle tree comes next. Every mainnet header to #9,392,253 passes, 165,328 of them fallback-mined, #1 to #365,411; verify_stored_headers now counts them. As controls, swapping the two keys fails all 165,328, and so does checking the signature against the block hash instead of the merged-mining hash. The tests take mainnet #1 and #2 as stored, and signatures made with k256 for each way rskj refuses one, and every one of 32 mutants of the check, its RLP and the per-network difficulty is caught. --- crates/cli/examples/verify_stored_headers.rs | 24 +- crates/core/src/config.rs | 8 + crates/core/src/lib.rs | 1 + crates/core/src/rskj_rlp.rs | 198 +++++++++ crates/core/src/types/transaction.rs | 2 +- crates/core/src/validation/merged_mining.rs | 122 ++++++ crates/core/src/validation/mod.rs | 3 + crates/core/src/validation/tests.rs | 432 ++++++++++++++++++- crates/execution/src/mining/tests.rs | 1 + 9 files changed, 776 insertions(+), 15 deletions(-) create mode 100644 crates/core/src/rskj_rlp.rs diff --git a/crates/cli/examples/verify_stored_headers.rs b/crates/cli/examples/verify_stored_headers.rs index 94bd516..0b53074 100644 --- a/crates/cli/examples/verify_stored_headers.rs +++ b/crates/cli/examples/verify_stored_headers.rs @@ -138,7 +138,7 @@ fn main() -> anyhow::Result<()> { let config = Arc::new(ChainConfig::mainnet()); let wasabi100 = config.activation_heights.wasabi100; - let verifier = HeaderVerifier::default_rsk(config); + let verifier = HeaderVerifier::default_rsk(config.clone()); let threads = std::thread::available_parallelism() .map(|n| n.get() as u64) .unwrap_or(4); @@ -156,6 +156,9 @@ fn main() -> anyhow::Result<()> { let orphaned = AtomicU64::new(0); let missing = AtomicU64::new(0); let fork_checked = AtomicU64::new(0); + // Headers below orchid that answer for their work with a fallback-mining + // signature rather than a Bitcoin header. + let fallback = AtomicU64::new(0); let started = Instant::now(); std::thread::scope(|scope| -> anyhow::Result<()> { @@ -168,16 +171,10 @@ fn main() -> anyhow::Result<()> { break; } let bottom = top.saturating_sub(span - 1).max(lowest); - let (store, verifier, failures, checked, not_later, orphaned, missing, fork_checked) = ( - &store, - &verifier, - &failures, - &checked, - ¬_later, - &orphaned, - &missing, - &fork_checked, + let (store, verifier, config, failures, checked, not_later, orphaned, missing) = ( + &store, &verifier, &config, &failures, &checked, ¬_later, &orphaned, &missing, ); + let (fork_checked, fallback) = (&fork_checked, &fallback); workers.push(scope.spawn(move || -> anyhow::Result<()> { // The highest block from `number` down to this range's bottom // that the store holds, counting the numbers it does not. @@ -205,6 +202,9 @@ fn main() -> anyhow::Result<()> { let mut window: VecDeque
= VecDeque::new(); let below = loop { let parent = store.header(header.parent_hash)?; + if merged_mining::is_fallback_mined(&header, config) { + fallback.fetch_add(1, Ordering::Relaxed); + } if let Err(e) = verifier.verify(&header, parent.as_ref()) { record(failures, header.number, &e); } @@ -287,6 +287,10 @@ fn main() -> anyhow::Result<()> { .max(fork_detection::REQUIRED_BLOCKS + 1); let fork_expected = (highest + 1).saturating_sub(fork_from); println!(" fork-detection data checked on {fork_checked} blocks, from #{fork_from}"); + println!( + " fallback-mined, their signatures checked: {}", + fallback.into_inner() + ); let expected = highest - lowest + 1; anyhow::ensure!( checked + missing == expected, diff --git a/crates/core/src/config.rs b/crates/core/src/config.rs index 088d0ac..558b817 100644 --- a/crates/core/src/config.rs +++ b/crates/core/src/config.rs @@ -165,6 +165,11 @@ pub struct ChainConfig { pub duration_limit: u64, pub difficulty_divisor: U256, pub min_difficulty: U256, + /// The greatest difficulty at which a block below RSKIP98 (orchid) may + /// be signed by a fallback-mining key rather than merge-mined: rskj + /// `Constants.getFallbackMiningDifficulty()`. 14 x 10^15 on mainnet and + /// testnet, zero on regtest. + pub fallback_mining_difficulty: U256, /// rskj `Constants.newBlockMaxSecondsInTheFuture`: how far past the /// node's clock a header may be timestamped. 60 on mainnet, 540 on /// testnet, 0 on regtest, where zero turns the timestamp rule off @@ -211,6 +216,7 @@ impl ChainConfig { duration_limit: 14, difficulty_divisor: U256::from(50), min_difficulty: U256::from(7_000_000_000_000_000u64), // 7e15, FALLBACK_MINING_DIFFICULTY / 2 + fallback_mining_difficulty: U256::from(14_000_000_000_000_000u64), max_future_block_time: 60, gas_limit_bound_divisor: 1024, min_gas_limit: 3_000_000, @@ -231,6 +237,7 @@ impl ChainConfig { duration_limit: 14, difficulty_divisor: U256::from(50), min_difficulty: U256::from(131072), + fallback_mining_difficulty: U256::from(14_000_000_000_000_000u64), max_future_block_time: 540, gas_limit_bound_divisor: 1024, min_gas_limit: 3_000_000, @@ -251,6 +258,7 @@ impl ChainConfig { duration_limit: 10, difficulty_divisor: U256::from(2048), min_difficulty: U256::from(1), + fallback_mining_difficulty: U256::ZERO, max_future_block_time: 0, gas_limit_bound_divisor: 1024, min_gas_limit: 3_000_000, diff --git a/crates/core/src/lib.rs b/crates/core/src/lib.rs index bda3ff7..d61cda0 100644 --- a/crates/core/src/lib.rs +++ b/crates/core/src/lib.rs @@ -7,6 +7,7 @@ pub mod validation; pub mod config; pub mod rlp_compat; pub mod rskj_genesis; +pub mod rskj_rlp; pub use types::block::Block; pub use types::header::Header; diff --git a/crates/core/src/rskj_rlp.rs b/crates/core/src/rskj_rlp.rs new file mode 100644 index 0000000..fe4124e --- /dev/null +++ b/crates/core/src/rskj_rlp.rs @@ -0,0 +1,198 @@ +//! RLP as rskj's `org.ethereum.util.RLP` reads it. +//! +//! rskj's decoder is more lenient than canonical RLP. A long-form length +//! prefix may carry a short length, or leading zeros; a single byte below +//! 0x80 may come as a one-byte string; and a long-form length is built in a +//! Java `int`, so one of more than four bytes wraps instead of failing. A +//! consensus rule that reads bytes as rskj does has to accept what it accepts +//! and refuse what it refuses, so this ports `RLP.decode2`, `decodeElement` +//! and `bytesToLength`, with `RLPItem` and `RLPList`, rather than decoding +//! canonically. + +/// Where rskj's decoder throws. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct RlpError; + +/// One element, as `RLP.decodeElement` cuts it. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Element<'a> { + /// An `RLPItem`: its payload. + Item(&'a [u8]), + /// An `RLPList`: its whole encoding, prefix included, and where its + /// payload starts in it. + List { encoding: &'a [u8], offset: usize }, +} + +impl<'a> Element<'a> { + /// `getRLPData`: none for an empty item, and a list's whole encoding, + /// which is never empty. + pub fn data(&self) -> Option<&'a [u8]> { + let bytes = match *self { + Element::Item(payload) => payload, + Element::List { encoding, .. } => encoding, + }; + (!bytes.is_empty()).then_some(bytes) + } + + /// A list's elements, decoded as `RLPList.checkElements` decodes them. + /// An item is refused, where rskj's cast to `RLPList` throws. + pub fn elements(&self) -> Result>, RlpError> { + match *self { + Element::List { encoding, offset } => decode2(&encoding[offset..]), + Element::Item(_) => Err(RlpError), + } + } +} + +/// `RLP.decode2`: every element from the start of `data` to its end. +pub fn decode2(data: &[u8]) -> Result>, RlpError> { + let mut elements = Vec::new(); + let mut position = 0; + while position < data.len() { + let (element, next) = decode_element(data, position)?; + elements.push(element); + position = next; + } + Ok(elements) +} + +/// `RLP.decodeElement`, with its lengths in Java `int`s. +fn decode_element(data: &[u8], position: usize) -> Result<(Element<'_>, usize), RlpError> { + let b0 = data[position]; + if b0 >= 0xc0 { + // A list's length counts its prefix too. + let (length, offset) = if b0 <= 0xf7 { + (i32::from(b0) - 0xc0 + 1, 1) + } else { + let nbytes = usize::from(b0 - 0xf7); + let offset = 1 + nbytes as i32; + let payload = bytes_to_length(data, position + 1, nbytes)?; + (offset.checked_add(payload).ok_or(RlpError)?, offset) + }; + let ending = (position as i32).checked_add(length).ok_or(RlpError)? as usize; + if ending > data.len() { + return Err(RlpError); + } + let encoding = &data[position..ending]; + return Ok(( + Element::List { + encoding, + offset: offset as usize, + }, + ending, + )); + } + if b0 == 0x80 { + return Ok((Element::Item(&[]), position + 1)); + } + if b0 < 0x80 { + return Ok((Element::Item(&data[position..=position]), position + 1)); + } + let (length, offset) = if b0 > 0xb7 { + let offset = usize::from(b0 - 0xb7) + 1; + (bytes_to_length(data, position + 1, offset - 1)?, offset) + } else { + (i32::from(b0 & 0x7f), 1) + }; + // The length is never negative here, and rskj's `int` sum overflows only + // to a negative it refuses: in either case the item runs past the end. + let from = position + offset; + let to = from + length as usize; + if to > data.len() { + return Err(RlpError); + } + Ok((Element::Item(&data[from..to]), to)) +} + +/// `RLP.bytesToLength`: big-endian in a Java `int`, so a length of more than +/// four bytes keeps only its low 32 bits, and a negative one is refused. +fn bytes_to_length(data: &[u8], position: usize, size: usize) -> Result { + if position + size > data.len() { + return Err(RlpError); + } + let mut length = 0i32; + for byte in &data[position..position + size] { + length = length.wrapping_shl(8).wrapping_add(i32::from(*byte)); + } + if length < 0 { + return Err(RlpError); + } + Ok(length) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn items(data: &[u8]) -> Result>>, RlpError> { + Ok(decode2(data)? + .iter() + .map(|e| e.data().map(<[u8]>::to_vec)) + .collect()) + } + + /// What canonical RLP writes, rskj reads. + #[test] + fn canonical_items_and_lists_read_as_written() { + assert_eq!(items(&[]), Ok(vec![])); + assert_eq!(items(&[0x05]), Ok(vec![Some(vec![0x05])])); + assert_eq!(items(&[0x80]), Ok(vec![None]), "an empty item has no data"); + assert_eq!(items(&[0x82, 0xaa, 0xbb]), Ok(vec![Some(vec![0xaa, 0xbb])])); + assert_eq!(items(&[0x01, 0x02]), Ok(vec![Some(vec![1]), Some(vec![2])])); + + let list = [0xc3, 0x1b, 0x81, 0x80]; + let decoded = decode2(&list).unwrap(); + assert_eq!(decoded.len(), 1); + assert_eq!( + decoded[0].data(), + Some(&list[..]), + "a list's data is its encoding" + ); + let inner = decoded[0].elements().unwrap(); + assert_eq!(inner, vec![Element::Item(&[0x1b]), Element::Item(&[0x80])]); + assert_eq!(inner[0].elements(), Err(RlpError), "an item is no list"); + } + + /// rskj takes what canonical RLP refuses: a byte below 0x80 as a string, + /// a long-form prefix on a short payload, and a length with leading zeros. + #[test] + fn rskj_reads_non_canonical_lengths() { + assert_eq!(items(&[0x81, 0x05]), Ok(vec![Some(vec![0x05])])); + assert_eq!( + items(&[0xb8, 0x02, 0xaa, 0xbb]), + Ok(vec![Some(vec![0xaa, 0xbb])]) + ); + assert_eq!( + items(&[0xb9, 0x00, 0x02, 0xaa, 0xbb]), + Ok(vec![Some(vec![0xaa, 0xbb])]) + ); + let list = [0xf8, 0x02, 0x01, 0x02]; + let decoded = decode2(&list).unwrap(); + assert_eq!(decoded[0].elements().unwrap().len(), 2); + } + + /// A length is built in a Java `int`: five bytes keep their low four, and + /// one with the sign bit set is refused. + #[test] + fn a_long_length_wraps_and_a_negative_one_is_refused() { + let wrapped = [0xbc, 0x01, 0x00, 0x00, 0x00, 0x02, 0xaa, 0xbb]; + assert_eq!(items(&wrapped), Ok(vec![Some(vec![0xaa, 0xbb])])); + assert_eq!(items(&[0xbb, 0x80, 0x00, 0x00, 0x00]), Err(RlpError)); + assert_eq!(items(&[0xfb, 0x80, 0x00, 0x00, 0x00]), Err(RlpError)); + // A list's length counts its prefix, so a length of -1 would net the + // five-byte prefix a list of four bytes, and reading would go on to + // the well-formed list after it. rskj refuses it before that. + let minus_one = [0xfb, 0xff, 0xff, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0]; + assert_eq!(items(&minus_one), Err(RlpError)); + } + + /// Anything that runs past the end is refused, an item's length bytes too. + #[test] + fn an_overrun_is_refused() { + assert_eq!(items(&[0x83, 0x01]), Err(RlpError)); + assert_eq!(items(&[0xb9, 0x01]), Err(RlpError)); + assert_eq!(items(&[0xc3, 0x01]), Err(RlpError)); + assert_eq!(items(&[0xf9, 0x00]), Err(RlpError)); + assert_eq!(items(&[0x01, 0x82, 0xaa]), Err(RlpError)); + } +} diff --git a/crates/core/src/types/transaction.rs b/crates/core/src/types/transaction.rs index 111cce2..d4c0cc2 100644 --- a/crates/core/src/types/transaction.rs +++ b/crates/core/src/types/transaction.rs @@ -199,7 +199,7 @@ fn take_rlp_item<'a>(buf: &mut &'a [u8]) -> Option<&'a [u8]> { /// more than a recovery does -- so a per-call context would spend the whole /// advantage of using this library and then some. Verification-only because /// nothing here signs: the signing half of the tables is not built. -fn secp_context() -> &'static secp256k1::Secp256k1 { +pub(crate) fn secp_context() -> &'static secp256k1::Secp256k1 { static CTX: std::sync::OnceLock> = std::sync::OnceLock::new(); CTX.get_or_init(secp256k1::Secp256k1::verification_only) diff --git a/crates/core/src/validation/merged_mining.rs b/crates/core/src/validation/merged_mining.rs index 685a9cb..c9dbae5 100644 --- a/crates/core/src/validation/merged_mining.rs +++ b/crates/core/src/validation/merged_mining.rs @@ -126,6 +126,116 @@ pub fn difficulty_to_target(difficulty: alloy_primitives::U256) -> alloy_primiti } } +/// rskj `Constants.FALLBACKMINING_PUBKEY_0` and `_1`, uncompressed: the key +/// that signs a fallback-mined block at an even height, and the one at an +/// odd height. +pub const FALLBACK_MINING_PUBKEYS: [[u8; 65]; 2] = [ + hex_literal::hex!( + "041e2b148c024770e19c4f31db2233cac791583df95b4d14a5e9fd4b38dc8254b3" + "048f937f169446b19d2eca40db1dd93fab34c0cd8a310afd6e6211f9a89e4bca" + ), + hex_literal::hex!( + "04b55031870df5de88bdb84f65bd1c6f8331c633e759caa5ac7cad3fa4f8a36791" + "e995804bba1558ddcf330a67ff5bfa253fa1d8789735f97a97e849686527976e" + ), +]; + +/// rskj `ProofOfWorkRule.isFallbackMiningPossibleAndBlockSigned`: below +/// RSKIP98 (orchid), a block with no coinbase and no Merkle proof, of no +/// greater difficulty than the network's fallback-mining difficulty, +/// answers for its proof of work with a signature in the Bitcoin-header +/// field instead. rskj reads an empty coinbase as none, and an empty proof +/// counts as none. +pub fn is_fallback_mined(header: &Header, config: &ChainConfig) -> bool { + use alloy_primitives::Bytes; + let none = |field: &Option| field.as_ref().is_none_or(|f| f.is_empty()); + header.number < config.activation_heights.orchid + && none(&header.bitcoin_merged_mining_coinbase_transaction) + && none(&header.bitcoin_merged_mining_merkle_proof) + && header.difficulty <= config.fallback_mining_difficulty +} + +/// rskj `ProofOfWorkRule.validFallbackBlockSignature`: the Bitcoin-header +/// field holds the RLP list `[v, r, s]`, a signature of the header's +/// merged-mining hash by `keys[0]` at an even height or `keys[1]` at an odd +/// one. +/// +/// Read as rskj reads it: by its lenient RLP, with `r` and `s` exactly as +/// Java's `BigInteger.toByteArray` writes them, `v` from 27 to 30, `s` below +/// half the curve order, and the key recovered by libsecp256k1, which is +/// rskj's default `native` crypto library. Where rskj throws, as where it +/// answers false, the signature does not stand. +pub fn fallback_signature_is_valid(header: &Header, keys: &[[u8; 65]; 2]) -> bool { + use alloy_primitives::U256; + let key = &keys[(header.number % 2) as usize]; + let field = header + .bitcoin_merged_mining_header + .as_ref() + .map_or(&[][..], |f| f.as_ref()); + let Ok(elements) = crate::rskj_rlp::decode2(field) else { + return false; + }; + let [signature] = elements.as_slice() else { + return false; + }; + let Ok(parts) = signature.elements() else { + return false; + }; + let [v, r, s] = parts.as_slice() else { + return false; + }; + let (Some(v), Some(r), Some(s)) = (v.data(), r.data(), s.data()) else { + return false; + }; + let &[v] = v else { return false }; + if r != java_unsigned(r) || s != java_unsigned(s) { + return false; + } + // `getV` is a Java byte, negative from 0x80. From 31 the recovery id + // passes 3, where rskj's `check` throws. + let v = v as i8; + if !(27..=30).contains(&v) { + return false; + } + let (Some(r), Some(s)) = (magnitude(r), magnitude(s)) else { + return false; + }; + if s >= U256::from_be_bytes(secp256k1::constants::CURVE_ORDER) >> 1 { + return false; + } + let mut compact = [0u8; 64]; + compact[..32].copy_from_slice(&r.to_be_bytes::<32>()); + compact[32..].copy_from_slice(&s.to_be_bytes::<32>()); + // `validateComponentsWithoutV` holds r and s to 1 through n - 1, as + // libsecp256k1 does in parsing and recovering. The id is 0 to 3 here. + let id = secp256k1::ecdsa::RecoveryId::from_u8_masked((v - 27) as u8); + let Ok(signature) = secp256k1::ecdsa::RecoverableSignature::from_compact(&compact, id) else { + return false; + }; + let message = secp256k1::Message::from_digest(header.hash_for_merged_mining().0); + crate::types::transaction::secp_context() + .recover_ecdsa(message, &signature) + .is_ok_and(|found| found.serialize_uncompressed() == *key) +} + +/// `new BigInteger(1, bytes).toByteArray()`: the magnitude without leading +/// zeros, with one zero byte back in front where its high bit is set, which +/// also writes zero as a single zero byte. +fn java_unsigned(bytes: &[u8]) -> Vec { + let start = bytes.iter().position(|b| *b != 0).unwrap_or(bytes.len()); + let magnitude = &bytes[start..]; + match magnitude.first() { + Some(b) if b & 0x80 == 0 => magnitude.to_vec(), + _ => [&[0u8][..], magnitude].concat(), + } +} + +/// The unsigned number in `bytes`, if it fits 256 bits. +fn magnitude(bytes: &[u8]) -> Option { + let start = bytes.iter().position(|b| *b != 0).unwrap_or(bytes.len()); + (bytes.len() - start <= 32).then(|| alloy_primitives::U256::from_be_slice(&bytes[start..])) +} + impl HeaderValidator for MergedMiningRule { fn validate(&self, header: &Header) -> Result<(), ValidationError> { use bitcoin::consensus::Decodable; @@ -133,6 +243,18 @@ impl HeaderValidator for MergedMiningRule { use bitcoin::hashes::Hash; use alloy_primitives::{B256, U256}; + // rskj `ProofOfWorkRule.isValid` asks first whether the block was + // mined without Bitcoin, which RSKIP98 (orchid) ended. + if is_fallback_mined(header, &self.config) { + if fallback_signature_is_valid(header, &FALLBACK_MINING_PUBKEYS) { + return Ok(()); + } + return Err(ValidationError::FallbackMiningSignatureInvalid { + number: header.number, + }); + } + // Merged mining below RSKIP92, orchid too, proves its coinbase by a + // bitcoinj partial Merkle tree, which this does not check yet. if header.number < self.config.activation_heights.orchid { return Ok(()); } diff --git a/crates/core/src/validation/mod.rs b/crates/core/src/validation/mod.rs index 2113bd2..92deac0 100644 --- a/crates/core/src/validation/mod.rs +++ b/crates/core/src/validation/mod.rs @@ -49,6 +49,9 @@ pub enum ValidationError { #[error("Bitcoin Proof of Work invalid: hash {hash} exceeds target {target}")] BitcoinPowInvalid { hash: B256, target: U256 }, + #[error("Fallback-mined block #{number} is not signed by its fallback-mining key")] + FallbackMiningSignatureInvalid { number: u64 }, + #[error("Bitcoin Merkle proof invalid")] BitcoinMerkleProofInvalid, diff --git a/crates/core/src/validation/tests.rs b/crates/core/src/validation/tests.rs index be344fd..49255ab 100644 --- a/crates/core/src/validation/tests.rs +++ b/crates/core/src/validation/tests.rs @@ -473,17 +473,441 @@ fn test_difficulty_equal_timestamps_increases() { assert!(rule.validate_with_parent(&header, &parent).is_ok()); } +/// Below orchid, a header with no coinbase and no proof, at no more than the +/// fallback-mining difficulty, answers for its work with a fallback-mining +/// signature. Without one it has no proof of work, and rskj refuses it. #[test] -fn test_merged_mining_skipped_before_orchid() { +fn below_orchid_a_header_without_bitcoin_needs_a_fallback_signature() { let config = Arc::new(crate::config::ChainConfig::mainnet()); let rule = MergedMiningRule { config }; - // Header at block 100 (before orchid=729,000) with NO bitcoin merged mining fields let mut header = create_dummy_header(100, 1000, B256::ZERO); header.difficulty = U256::from(7_000_000_000_000_000u64); - // bitcoin_merged_mining_* are None by default in create_dummy_header - assert!(rule.validate(&header).is_ok()); + assert_eq!( + rule.validate(&header), + Err(ValidationError::FallbackMiningSignatureInvalid { number: 100 }) + ); +} + +/// Mainnet #1 or #2 as rskj stores it. Both were fallback-mined at the +/// least difficulty: no coinbase, no proof, and in the Bitcoin-header field +/// the list `[v, r, s]`, signed by the key for the block's parity. The logs +/// bloom between the two halves is 256 zero bytes. +fn mainnet_rlp(number: u64) -> Vec { + let (before_bloom, after_bloom) = match number { + 1 => ( + "f90221\ + a0f88529d4ab262c0f4d042e9d8d3f2472848eaafe1a9b7213f57617eb40a9f9e0\ + a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347\ + 9414d3065c8eb89895f4df12450ec6b130049f8034\ + a021e18f252c8579cee053a215b1f56b90f0b0d008716f5bfbf40009bd6c7d8c04\ + a01b4008e6fba070d33fb937e369ddbea5c2b66e5bcb12cfc829f15e1f25e568d0\ + a0a70fc9a7d692ac80a46b23dcf81d5928dbdebc8ac097cfdbce2c03301d9e9406\ + b90100", + "8718de76816d8000018367a89080845a4ddaa32a80840acc6f5080\ + b845f8431b\ + a07c85e0cdfd3e8ae89755a8c82de0cc0b22539635b5f5aab084b390726d67c2cf\ + a05cc475b9130d27193f405de48cdeca961f071e97ffb727299f8cf471a7906017\ + 8080", + ), + 2 => ( + "f90221\ + a02422043c4210153a91b974edcd8091b7f40f1cc8ec082ce2a47bc9c613811256\ + a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347\ + 9414d3065c8eb89895f4df12450ec6b130049f8034\ + a08a9f2c7c75ba8e2dc20d94d94dc5b80a6099adb345eef3af04aa2e08a095490e\ + a0c10441f2e687fd7032dbdef56cb65afafb0e9a6b54bbe5f081807988cdb7d92c\ + a0a70fc9a7d692ac80a46b23dcf81d5928dbdebc8ac097cfdbce2c03301d9e9406\ + b90100", + "8718de76816d80000283678ea680845a4ddab22a80840ab0ca5c80\ + b845f8431c\ + a02207f67db2d064b6348b7e67041f183274609f85dff748b95fadccbff9640ce2\ + a0568cd574b87979cfedeaf7dc9c9f5079d0b5df28d90dcaa1b7656828c404b5dd\ + 8080", + ), + _ => unreachable!("only #1 and #2 are here"), + }; + let mut rlp = hex::decode(before_bloom).unwrap(); + rlp.extend([0u8; 256]); + rlp.extend(hex::decode(after_bloom).unwrap()); + rlp +} + +/// A header decoded as the node decodes one, the whole of `rlp`. +fn decoded(rlp: &[u8]) -> Header { + let mut rest = rlp; + let header = Header::decode_with_hash(&mut rest).unwrap(); + assert!(rest.is_empty(), "bytes after the header"); + header +} + +/// rskj takes mainnet's first fallback-mined blocks, and so must this: each +/// carries the signature of the key for its parity, and of no other. +#[test] +fn mainnets_fallback_mined_blocks_carry_their_keys_signatures() { + use merged_mining::{fallback_signature_is_valid, is_fallback_mined, FALLBACK_MINING_PUBKEYS}; + + let config = Arc::new(crate::config::ChainConfig::mainnet()); + let rule = MergedMiningRule { + config: config.clone(), + }; + let first = decoded(&mainnet_rlp(1)); + let second = decoded(&mainnet_rlp(2)); + // Mainnet's: #1 follows genesis and #2 follows #1. + assert_eq!(Some(first.parent_hash), config.known_genesis_hash()); + assert_eq!(second.parent_hash, first.hash()); + + let swapped = [FALLBACK_MINING_PUBKEYS[1], FALLBACK_MINING_PUBKEYS[0]]; + for header in [&first, &second] { + assert!(is_fallback_mined(header, &config), "#{}", header.number); + assert_eq!(rule.validate(header), Ok(()), "#{}", header.number); + assert!( + !fallback_signature_is_valid(header, &swapped), + "#{}", + header.number + ); + } +} + +/// The signature covers the header: one byte of #1's timestamp changed and +/// the key it recovers is no fallback-mining key. +#[test] +fn a_fallback_signature_covers_its_header() { + let rule = MergedMiningRule { + config: Arc::new(crate::config::ChainConfig::mainnet()), + }; + let mut rlp = mainnet_rlp(1); + let timestamp = [0x84, 0x5a, 0x4d, 0xda, 0xa3]; + let at = rlp.windows(5).position(|w| w == timestamp).unwrap() + 4; + rlp[at] ^= 1; + + assert_eq!( + rule.validate(&decoded(&rlp)), + Err(ValidationError::FallbackMiningSignatureInvalid { number: 1 }) + ); +} + +/// rskj's `isFallbackMiningPossibleAndBlockSigned`: below orchid, with no +/// coinbase and no proof, an empty one counting as none, at no more than the +/// network's fallback-mining difficulty: 14 x 10^15 on mainnet and testnet, +/// zero on regtest. +#[test] +fn which_headers_are_fallback_mined() { + use crate::config::{ActivationHeights, ChainConfig}; + use merged_mining::is_fallback_mined; + + let at = |number: u64, difficulty: u64| { + let mut header = create_dummy_header(number, 1000, B256::ZERO); + header.difficulty = U256::from(difficulty); + header + }; + let ceiling = 14_000_000_000_000_000u64; + let mainnet = ChainConfig::mainnet(); + assert!(is_fallback_mined(&at(728_999, ceiling), &mainnet)); + assert!(!is_fallback_mined(&at(728_999, ceiling + 1), &mainnet)); + assert!(!is_fallback_mined(&at(729_000, ceiling / 2), &mainnet)); + + let mut empty = at(1, ceiling); + empty.bitcoin_merged_mining_coinbase_transaction = Some(Bytes::new()); + empty.bitcoin_merged_mining_merkle_proof = Some(Bytes::new()); + assert!(is_fallback_mined(&empty, &mainnet)); + let mut coinbase = empty.clone(); + coinbase.bitcoin_merged_mining_coinbase_transaction = Some(Bytes::from_static(&[0])); + assert!(!is_fallback_mined(&coinbase, &mainnet)); + let mut proof = empty; + proof.bitcoin_merged_mining_merkle_proof = Some(Bytes::from_static(&[0])); + assert!(!is_fallback_mined(&proof, &mainnet)); + + // Testnet and regtest put orchid at genesis, so neither has any. With + // orchid later, each has its own ceiling. + assert!(!is_fallback_mined(&at(0, 0), &ChainConfig::testnet())); + assert!(!is_fallback_mined(&at(0, 0), &ChainConfig::regtest())); + let late = |config: ChainConfig| ChainConfig { + activation_heights: ActivationHeights { + orchid: 1_000, + ..config.activation_heights.clone() + }, + ..config + }; + let testnet = late(ChainConfig::testnet()); + assert!(is_fallback_mined(&at(999, ceiling), &testnet)); + assert!(!is_fallback_mined(&at(999, ceiling + 1), &testnet)); + let regtest = late(ChainConfig::regtest()); + assert!(is_fallback_mined(&at(999, 0), ®test)); + assert!(!is_fallback_mined(&at(999, 1), ®test)); +} + +/// A key's public half as rskj's constants hold one: uncompressed. +fn uncompressed(key: &k256::ecdsa::VerifyingKey) -> [u8; 65] { + key.to_encoded_point(false).as_bytes().try_into().unwrap() +} + +/// A header below orchid that only a fallback signature can answer for. +fn unsigned_fallback_header(number: u64) -> Header { + let mut header = create_dummy_header(number, 1000 + number, B256::ZERO); + header.difficulty = U256::from(7_000_000_000_000_000u64); + header +} + +/// `key`'s signature of `header`'s merged-mining hash: `v` as rskj reads +/// it, 27 plus the recovery id, then `r` and `s`, low-s as k256 makes it. +fn fallback_sign(header: &Header, key: &k256::ecdsa::SigningKey) -> (u8, [u8; 32], [u8; 32]) { + let hash = header.hash_for_merged_mining(); + let (signature, id) = key.sign_prehash_recoverable(hash.as_slice()).unwrap(); + assert!(signature.normalize_s().is_none(), "k256 signs low-s"); + let bytes = signature.to_bytes(); + ( + 27 + id.to_byte(), + bytes[..32].try_into().unwrap(), + bytes[32..].try_into().unwrap(), + ) +} + +/// What Java's `BigInteger.toByteArray` writes for the unsigned `bytes`: no +/// leading zeros, then a zero byte in front where the high bit is set. +fn java_bytes(bytes: &[u8; 32]) -> Vec { + let trimmed = U256::from_be_bytes(*bytes).to_be_bytes_trimmed_vec(); + match trimmed.first() { + None => vec![0], + Some(&b) if b >= 0x80 => [vec![0], trimmed].concat(), + Some(_) => trimmed, + } +} + +/// An RLP list of `items`, each already encoded. +fn rlp_list(items: &[Vec]) -> Vec { + let payload = items.concat(); + let mut out = Vec::new(); + alloy_rlp::Header { + list: true, + payload_length: payload.len(), + } + .encode(&mut out); + out.extend_from_slice(&payload); + out +} + +/// `bytes` as a canonical RLP string. +fn rlp_string(bytes: &[u8]) -> Vec { + let mut out = Vec::new(); + alloy_rlp::Encodable::encode(bytes, &mut out); + out +} + +/// Whether `header` stands with `field` in its Bitcoin-header field. +fn signed_by(header: &Header, field: Vec, keys: &[[u8; 65]; 2]) -> bool { + let mut header = header.clone(); + header.bitcoin_merged_mining_header = Some(Bytes::from(field)); + merged_mining::fallback_signature_is_valid(&header, keys) +} + +/// rskj's `validFallbackBlockSignature`, on signatures made here by keys +/// that stand in for mainnet's two: what it takes and what it refuses. +#[test] +fn a_fallback_signature_is_read_as_rskj_reads_it() { + use k256::ecdsa::SigningKey; + + let even = SigningKey::from_slice(&[0x11; 32]).unwrap(); + let odd = SigningKey::from_slice(&[0x22; 32]).unwrap(); + let keys = [ + uncompressed(even.verifying_key()), + uncompressed(odd.verifying_key()), + ]; + let n = U256::from_be_bytes(secp256k1::constants::CURVE_ORDER); + // Whether an `r`, or an `s`, came that Java writes with a zero in front. + // An `r` has to, for the test below; a low `s` never can, its high bit + // being clear. + let mut zero_in_front = [false; 2]; + + for number in 1..=16u64 { + let header = unsigned_fallback_header(number); + let (signer, other) = if number % 2 == 0 { + (&even, &odd) + } else { + (&odd, &even) + }; + let (v, r, s) = fallback_sign(&header, signer); + let (r_java, s_java) = (java_bytes(&r), java_bytes(&s)); + let field = + |v: &[u8], r: &[u8], s: &[u8]| rlp_list(&[rlp_string(v), rlp_string(r), rlp_string(s)]); + let stands = |field: Vec| signed_by(&header, field, &keys); + + assert!(stands(field(&[v], &r_java, &s_java)), "#{number}"); + + // Another key's signature, and this one with the wrong `v`. + let (v_other, r_other, s_other) = fallback_sign(&header, other); + assert!(!stands(field( + &[v_other], + &java_bytes(&r_other), + &java_bytes(&s_other) + ))); + let flipped = if v == 27 { 28 } else { 27 }; + assert!(!stands(field(&[flipped], &r_java, &s_java)), "#{number}"); + // Four either way, or 128 more, keeps the recovery id's two bits, so + // only the range refuses these. + for v in [v - 4, v + 4, v | 0x80] { + assert!(!stands(field(&[v], &r_java, &s_java)), "#{number}, v {v}"); + } + assert!( + !stands(field(&[0, v], &r_java, &s_java)), + "#{number}, v in two bytes" + ); + assert!(!stands(field(&[], &r_java, &s_java)), "#{number}, no v"); + + // `r` and `s` exactly as Java writes them, and no other way. + for (i, java) in [&r_java, &s_java].into_iter().enumerate() { + let mut written = [r_java.clone(), s_java.clone()]; + written[i] = [&[0u8][..], &java[..]].concat(); + assert!( + !stands(field(&[v], &written[0], &written[1])), + "#{number}, extra zero" + ); + if java[0] == 0 { + zero_in_front[i] = true; + written[i] = java[1..].to_vec(); + assert!( + !stands(field(&[v], &written[0], &written[1])), + "#{number}, no zero" + ); + } + written[i] = vec![]; + assert!( + !stands(field(&[v], &written[0], &written[1])), + "#{number}, empty" + ); + } + + // The same signature with `s` high is good ECDSA, the key recovers + // from it, but rskj takes only the low `s`. + let high: [u8; 32] = (n - U256::from_be_bytes(s)).to_be_bytes(); + let compact = [r, high].concat(); + let id = secp256k1::ecdsa::RecoveryId::from_u8_masked(flipped - 27); + let recovered = secp256k1::Secp256k1::verification_only() + .recover_ecdsa( + secp256k1::Message::from_digest(header.hash_for_merged_mining().0), + &secp256k1::ecdsa::RecoverableSignature::from_compact(&compact, id).unwrap(), + ) + .unwrap(); + assert_eq!( + recovered.serialize_uncompressed(), + keys[(number % 2) as usize] + ); + assert!( + !stands(field(&[flipped], &r_java, &java_bytes(&high))), + "#{number}, high s" + ); + + // Exactly one list of exactly three. + let good = field(&[v], &r_java, &s_java); + let parts = [rlp_string(&[v]), rlp_string(&r_java), rlp_string(&s_java)]; + assert!(!stands(rlp_list(&parts[..2])), "#{number}, two"); + let four = [parts.to_vec(), vec![vec![0x80]]].concat(); + assert!(!stands(rlp_list(&four)), "#{number}, four"); + assert!( + !stands([good.clone(), vec![0x80]].concat()), + "#{number}, after the list" + ); + assert!(!stands(rlp_string(&parts.concat())), "#{number}, an item"); + assert!( + !stands(good[..good.len() - 1].to_vec()), + "#{number}, cut short" + ); + assert!(!stands(vec![]), "#{number}, empty"); + let mut absent = header.clone(); + absent.bitcoin_merged_mining_header = None; + assert!(!merged_mining::fallback_signature_is_valid(&absent, &keys)); + + // What rskj's lenient RLP takes, this takes: `v` as a one-byte + // string, `r` in a long-form prefix, and the list's length with a + // zero in front. + let long = |bytes: &[u8]| [vec![0xb8, bytes.len() as u8], bytes.to_vec()].concat(); + let payload = [vec![0x81, v], long(&r_java), rlp_string(&s_java)].concat(); + let lenient = [vec![0xf9, 0x00, payload.len() as u8], payload].concat(); + assert!(stands(lenient), "#{number}, lenient"); + } + assert_eq!( + zero_in_front, + [true, false], + "an r with a zero in front, and no s" + ); +} + +/// rskj refuses an `s` of half the curve order, rounded down, or more, and +/// takes one less. The key is whatever k256 recovers from each signature, +/// so only the bound decides. +#[test] +fn a_fallback_signatures_s_stops_below_half_the_curve_order() { + use k256::ecdsa::{RecoveryId, Signature, VerifyingKey}; + + let header = unsigned_fallback_header(7); + let hash = header.hash_for_merged_mining(); + let half: U256 = U256::from_be_bytes(secp256k1::constants::CURVE_ORDER) >> 1; + // The generator's x, which is on the curve. + let r: [u8; 32] = + hex_literal::hex!("79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"); + for (s, taken) in [(half, false), (half - U256::from(1), true)] { + let s: [u8; 32] = s.to_be_bytes(); + let signature = Signature::from_scalars(r, s).unwrap(); + let id = RecoveryId::from_byte(0).unwrap(); + let key = VerifyingKey::recover_from_prehash(hash.as_slice(), &signature, id).unwrap(); + let key = uncompressed(&key); + let field = rlp_list(&[ + rlp_string(&[27]), + rlp_string(&java_bytes(&r)), + rlp_string(&java_bytes(&s)), + ]); + let s = U256::from_be_bytes(s); + assert_eq!(signed_by(&header, field, &[key, key]), taken, "s = {s}"); + } +} + +/// rskj takes `v` up to 30: recovery ids 2 and 3 say `r` is a point's x less +/// the curve order, and libsecp256k1, rskj's library, recovers from them. +/// Four less keeps the id's two bits, and rskj refuses it all the same. +#[test] +fn a_fallback_signature_takes_each_recovery_id_rskj_takes() { + let header = unsigned_fallback_header(9); + let message = secp256k1::Message::from_digest(header.hash_for_merged_mining().0); + let n = U256::from_be_bytes(secp256k1::constants::CURVE_ORDER); + // The least `r` whose `r + n` is a point's x, as ids 2 and 3 need. + let r = (1u64..) + .map(U256::from) + .find(|r| { + let x: [u8; 32] = (*r + n).to_be_bytes(); + secp256k1::PublicKey::from_slice(&[&[2u8][..], &x].concat()).is_ok() + }) + .unwrap(); + let s = U256::from(0x5eed); + let (r, s): ([u8; 32], [u8; 32]) = (r.to_be_bytes(), s.to_be_bytes()); + let field = |v: u8| { + rlp_list(&[ + rlp_string(&[v]), + rlp_string(&java_bytes(&r)), + rlp_string(&java_bytes(&s)), + ]) + }; + for id in [2u8, 3] { + let signature = secp256k1::ecdsa::RecoverableSignature::from_compact( + &[r, s].concat(), + secp256k1::ecdsa::RecoveryId::from_u8_masked(id), + ) + .unwrap(); + let key = secp256k1::Secp256k1::verification_only() + .recover_ecdsa(message, &signature) + .unwrap() + .serialize_uncompressed(); + assert!( + signed_by(&header, field(27 + id), &[key, key]), + "v {}", + 27 + id + ); + assert!( + !signed_by(&header, field(23 + id), &[key, key]), + "v {}", + 23 + id + ); + } } #[test] diff --git a/crates/execution/src/mining/tests.rs b/crates/execution/src/mining/tests.rs index 07dee8f..a677e19 100644 --- a/crates/execution/src/mining/tests.rs +++ b/crates/execution/src/mining/tests.rs @@ -32,6 +32,7 @@ fn test_chain_config() -> Arc { duration_limit: 10, difficulty_divisor: U256::from(2048), min_difficulty: U256::from(1), + fallback_mining_difficulty: U256::ZERO, max_future_block_time: 0, gas_limit_bound_divisor: 1024, min_gas_limit: 1, From 750cdfd3c00e7773a554e24076bfd63215dcfb49 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Fri, 6 Nov 2026 14:59:42 -0800 Subject: [PATCH 022/264] Read partial merkle trees as bitcoinj-thin does, in one place rskj reads a Bitcoin partial merkle tree with bitcoinj-thin's PartialMerkleTree (v0.14.4-rsk-20) wherever it has to show that a transaction is in a Bitcoin block: below RSKIP92, a merge-mined block's coinbase, and in the Bridge, peg-ins, flyover peg-ins and registered coinbases. rustock's Bridge had a tree of its own that lacked bitcoinj's refusals: more than 16,666 transactions, fewer flag bits than hashes, two nodes that form a 64-byte transaction, and counts read as Java ints. rustock_core::partial_merkle_tree ports bitcoinj's parse and getTxnHashAndMerkleRoot, in display order as bitcoinj holds hashes, and rskj's hasExpectedSize in Java's exact int arithmetic. The Bridge's pmt module is now an adapter over it that gives its callers wire order, as before. The proof of work below RSKIP92 will use the same tree. MerkleBranch is now rskj's: display order, at most 32 hashes, no more significant path bits than hashes (none for a negative path), the path as BigInteger.intValue reads it, and reduceFrom refusing two nodes that form a 64-byte transaction, as MerkleTreeUtils.combineLeftRight does. That fixes getBtcTransactionConfirmations. It climbed the branch in wire order and compared the root with the registered witness merkle root, which is stored in display order, so a transaction proved by the witness tree got -5 where rskj counts its confirmations. Now all of it is in display order, and a branch through a 64-byte transaction fails the call as rskj's throw does. The comments that called the ABI's witness root internal order now say display order. The tests port rskj's MerkleBranchTest and MerkleTreeUtilsTest vectors, walk mainnet's first peg-in (#6,916) to its block's root, take each of bitcoinj's refusals, and on regtest register a coinbase's witness root and ask for confirmations through both trees, in both byte orders, and through a 64-byte transaction. 34 of 36 mutants are caught; the other two make checked additions in hasExpectedSize unchecked where they cannot overflow, and change no result. --- crates/core/src/lib.rs | 1 + crates/core/src/partial_merkle_tree.rs | 663 +++++++++++++++++++ crates/execution/src/bridge/pmt.rs | 861 ++++++++----------------- crates/execution/src/bridge/tx.rs | 39 +- crates/execution/src/executor.rs | 226 +++++++ 5 files changed, 1183 insertions(+), 607 deletions(-) create mode 100644 crates/core/src/partial_merkle_tree.rs diff --git a/crates/core/src/lib.rs b/crates/core/src/lib.rs index d61cda0..9b15d6d 100644 --- a/crates/core/src/lib.rs +++ b/crates/core/src/lib.rs @@ -8,6 +8,7 @@ pub mod config; pub mod rlp_compat; pub mod rskj_genesis; pub mod rskj_rlp; +pub mod partial_merkle_tree; pub use types::block::Block; pub use types::header::Header; diff --git a/crates/core/src/partial_merkle_tree.rs b/crates/core/src/partial_merkle_tree.rs new file mode 100644 index 0000000..e78558a --- /dev/null +++ b/crates/core/src/partial_merkle_tree.rs @@ -0,0 +1,663 @@ +//! A Bitcoin partial Merkle tree, as bitcoinj-thin's `PartialMerkleTree` +//! (`v0.14.4-rsk-20`) reads and walks it, and as rskj's +//! `PartialMerkleTreeFormatUtils.hasExpectedSize` sizes it. +//! +//! rskj reads one wherever it has to be shown that a Bitcoin transaction is +//! in a Bitcoin block: below RSKIP92, a merge-mined block's coinbase +//! (`GenesisMerkleProofValidator`), and in the Bridge, a peg-in or a coinbase +//! registered for its witness root. Each goes through the one bitcoinj +//! class, so each goes through this. +//! +//! bitcoinj holds every hash in display order, the reverse of the bytes a +//! Bitcoin block hashes, and so does this. The hashes a tree matches and its +//! root come back that way, and two nodes are tested for a 64-byte +//! transaction that way, as bitcoinj tests them. +//! +//! A tree is the block's transaction count, four bytes little-endian, read +//! into a Java `int`; a varint count of hashes, then the hashes, 32 bytes +//! each; and a varint count of flag bytes, then the flag bytes, whose bits +//! are read least significant first. + +use crate::validation::merged_mining::{combine_left_right, parses_as_a_64_byte_transaction}; + +/// bitcoinj `BtcBlock.MAX_BLOCK_SIZE / 60`: no more transactions than a +/// block of a million bytes holds at 60 bytes each, the least one takes. +pub const MAX_TRANSACTIONS: i32 = 1_000_000 / 60; + +/// bitcoinj `Message.MAX_SIZE`: the longest array it reads, 32 MiB. +const MAX_ARRAY: i32 = 0x0200_0000; + +/// Where bitcoinj throws: a `ProtocolException` reading a tree, or a +/// `VerificationException` walking it. rskj takes them alike. +#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)] +pub enum PmtError { + #[error("the partial merkle tree runs past its bytes, or counts below zero")] + Unreadable, + #[error("the partial merkle tree has no transactions")] + NoTransactions, + #[error("the partial merkle tree has more transactions than a block holds")] + TooManyTransactions, + #[error("the partial merkle tree has more hashes than transactions")] + MoreHashesThanTransactions, + #[error("the partial merkle tree has fewer flag bits than hashes")] + FewerBitsThanHashes, + #[error("the partial merkle tree runs out of flag bits")] + OutOfBits, + #[error("the partial merkle tree runs out of hashes")] + OutOfHashes, + #[error("the partial merkle tree repeats a branch")] + DuplicatedBranches, + #[error("two nodes of the partial merkle tree form a Bitcoin transaction")] + NodesFormTransaction, + #[error("the partial merkle tree has flag bits or hashes it does not use")] + UnusedData, +} + +/// A partial Merkle tree as bitcoinj reads one. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct PartialMerkleTree { + transaction_count: i32, + /// In display order: bitcoinj's `readHash` reverses each. + hashes: Vec<[u8; 32]>, + matched_child_bits: Vec, +} + +impl PartialMerkleTree { + /// `new PartialMerkleTree(params, payload, 0)`, which reads a tree from the + /// start of `payload` and leaves whatever follows it unread. + pub fn parse(payload: &[u8]) -> Result { + let count = payload.get(..4).ok_or(PmtError::Unreadable)?; + // `(int) readUint32()`: from 2^31 on, negative. + let transaction_count = u32::from_le_bytes(count.try_into().expect("four bytes")) as i32; + let mut cursor = 4; + + // `(int) readVarInt()` keeps a varint's low 32 bits, and a count + // below zero sizes the list bitcoinj makes for the hashes below zero, + // which throws. + let (hash_count, size) = var_int(payload, cursor).ok_or(PmtError::Unreadable)?; + cursor += size; + let hash_count = hash_count as i32; + if hash_count < 0 { + return Err(PmtError::Unreadable); + } + let mut hashes = Vec::new(); + for _ in 0..hash_count { + let wire = payload + .get(cursor..cursor + 32) + .ok_or(PmtError::Unreadable)?; + let mut hash: [u8; 32] = wire.try_into().expect("32 bytes"); + hash.reverse(); + hashes.push(hash); + cursor += 32; + } + + // `readBytes((int) readVarInt())`, which refuses more than 32 MiB or + // more than is left, and below zero cannot make the array. + let (flag_bytes, size) = var_int(payload, cursor).ok_or(PmtError::Unreadable)?; + cursor += size; + let flag_bytes = flag_bytes as i32; + if !(0..=MAX_ARRAY).contains(&flag_bytes) { + return Err(PmtError::Unreadable); + } + let bits = payload + .get(cursor..cursor + flag_bytes as usize) + .ok_or(PmtError::Unreadable)?; + + Ok(Self { + transaction_count, + hashes, + matched_child_bits: bits.to_vec(), + }) + } + + /// The block's transaction count, as the Java `int` bitcoinj holds it in. + pub fn transaction_count(&self) -> i32 { + self.transaction_count + } + + /// The tree's hashes, in display order. + pub fn hashes(&self) -> &[[u8; 32]] { + &self.hashes + } + + /// The flag bytes. + pub fn matched_child_bits(&self) -> &[u8] { + &self.matched_child_bits + } + + /// `getTxnHashAndMerkleRoot`: the transactions the tree matches and the + /// root it walks up to, both in display order. + pub fn txn_hashes_and_merkle_root(&self) -> Result<(Vec<[u8; 32]>, [u8; 32]), PmtError> { + let count = self.transaction_count; + if count == 0 { + return Err(PmtError::NoTransactions); + } + if count > MAX_TRANSACTIONS { + return Err(PmtError::TooManyTransactions); + } + // Java `int`s, so a count below zero fails here. + if self.hashes.len() as i64 > i64::from(count) { + return Err(PmtError::MoreHashesThanTransactions); + } + // At least a bit for every node, and a node for every hash. + if self.matched_child_bits.len() * 8 < self.hashes.len() { + return Err(PmtError::FewerBitsThanHashes); + } + + let mut height = 0; + while tree_width(count, height) > 1 { + height += 1; + } + let mut walk = Walk { + tree: self, + bits_used: 0, + hashes_used: 0, + matched: Vec::new(), + }; + let root = walk.extract(height, 0)?; + + // Every bit, but for the last byte's padding, and every hash. + if walk.bits_used.div_ceil(8) != self.matched_child_bits.len() + || walk.hashes_used != self.hashes.len() + { + return Err(PmtError::UnusedData); + } + Ok((walk.matched, root)) + } +} + +/// bitcoinj `getTreeWidth`: how many nodes the tree of `count` transactions +/// has `height` levels up. +fn tree_width(count: i32, height: u32) -> i32 { + (count + (1 << height) - 1) >> height +} + +/// bitcoinj's `recursiveExtractHashes`, with its `ValuesUsed` and the list it +/// gathers the matched hashes in. +struct Walk<'a> { + tree: &'a PartialMerkleTree, + bits_used: usize, + hashes_used: usize, + matched: Vec<[u8; 32]>, +} + +impl Walk<'_> { + /// The hash of the node `height` levels up at `pos`, depth first. + fn extract(&mut self, height: u32, pos: i32) -> Result<[u8; 32], PmtError> { + let bits = &self.tree.matched_child_bits; + if self.bits_used >= bits.len() * 8 { + return Err(PmtError::OutOfBits); + } + let bit = self.bits_used; + self.bits_used += 1; + let parent_of_match = bits[bit / 8] & (1 << (bit % 8)) != 0; + + if height == 0 || !parent_of_match { + // A leaf, or nothing matched below: the node's hash is given. + let Some(&hash) = self.tree.hashes.get(self.hashes_used) else { + return Err(PmtError::OutOfHashes); + }; + self.hashes_used += 1; + if height == 0 && parent_of_match { + self.matched.push(hash); + } + return Ok(hash); + } + + let left = self.extract(height - 1, pos * 2)?; + let right = if pos * 2 + 1 < tree_width(self.tree.transaction_count, height - 1) { + let right = self.extract(height - 1, pos * 2 + 1)?; + if right == left { + return Err(PmtError::DuplicatedBranches); + } + right + } else { + left + }; + if parses_as_a_64_byte_transaction(&left, &right) { + return Err(PmtError::NodesFormTransaction); + } + Ok(combine_left_right(&left, &right)) + } +} + +/// rskj `PartialMerkleTreeFormatUtils.hasExpectedSize`: whether `pmt` is +/// exactly the tree its two counts declare, nothing short and nothing over. +/// rskj works the sizes out in Java's exact `int` arithmetic, where an +/// overflow throws, and takes any throw for a no; so does this. +pub fn has_expected_size(pmt: &[u8]) -> bool { + declared_size(pmt).is_some_and(|size| i64::from(size) == pmt.len() as i64) +} + +/// The size `pmt`'s counts declare, where rskj can work it out. +fn declared_size(pmt: &[u8]) -> Option { + // `getHashesCount`, the varint after the transaction count, and + // `Math.multiplyExact(Math.toIntExact(count), 32)`. + let (hashes, hashes_size) = var_int(pmt, 4)?; + let hash_bytes = i32::try_from(hashes).ok()?.checked_mul(32)?; + // `getFlagBitsCount`, the varint after the hashes, at an offset + // `Math.addExact` works out; one below zero is outside the array. + let flags_at = (4 + hashes_size as i32).checked_add(hash_bytes)?; + let (flags, flags_size) = var_int(pmt, usize::try_from(flags_at).ok()?)?; + let flags = i32::try_from(flags).ok()?; + (4 + hashes_size as i32 + flags_size as i32) + .checked_add(flags)? + .checked_add(hash_bytes) +} + +/// bitcoinj `new VarInt(buf, offset)`: the value in the Java `long` it is +/// read into, where a nine-byte one can be negative, and how many bytes it +/// took. `None` where it runs past the end, where bitcoinj throws. +fn var_int(buf: &[u8], offset: usize) -> Option<(i64, usize)> { + let first = *buf.get(offset)?; + let rest = |n: usize| buf.get(offset + 1..offset + 1 + n); + Some(match first { + 0..=252 => (i64::from(first), 1), + 253 => (i64::from(u16::from_le_bytes(rest(2)?.try_into().ok()?)), 3), + 254 => (i64::from(u32::from_le_bytes(rest(4)?.try_into().ok()?)), 5), + 255 => (i64::from_le_bytes(rest(8)?.try_into().ok()?), 9), + }) +} + +#[cfg(test)] +mod tests { + use super::*; + + /// A leaf for the tests, in display order. + fn leaf(i: usize) -> [u8; 32] { + use sha2::{Digest, Sha256}; + Sha256::digest((i as u64).to_le_bytes()).into() + } + + fn var_int_bytes(n: usize) -> Vec { + match n { + 0..=252 => vec![n as u8], + 253..=0xffff => [vec![0xfd], (n as u16).to_le_bytes().to_vec()].concat(), + _ => [vec![0xfe], (n as u32).to_le_bytes().to_vec()].concat(), + } + } + + /// bitcoinj `buildFromLeaves` and `bitcoinSerializeToStream`: the tree over + /// `leaves`, in display order, that matches those `include` marks. + fn build(leaves: &[[u8; 32]], include: &[bool]) -> Vec { + fn width(n: usize, height: u32) -> usize { + (n + (1 << height) - 1) >> height + } + fn hash(height: u32, pos: usize, leaves: &[[u8; 32]]) -> [u8; 32] { + if height == 0 { + return leaves[pos]; + } + let left = hash(height - 1, pos * 2, leaves); + let right = if pos * 2 + 1 < width(leaves.len(), height - 1) { + hash(height - 1, pos * 2 + 1, leaves) + } else { + left + }; + combine_left_right(&left, &right) + } + struct Built { + bits: Vec, + hashes: Vec<[u8; 32]>, + } + fn traverse( + height: u32, + pos: usize, + leaves: &[[u8; 32]], + include: &[bool], + out: &mut Built, + ) { + let below = pos << height..((pos + 1) << height).min(leaves.len()); + let parent_of_match = below.into_iter().any(|p| include[p]); + out.bits.push(parent_of_match); + if height == 0 || !parent_of_match { + out.hashes.push(hash(height, pos, leaves)); + } else { + traverse(height - 1, pos * 2, leaves, include, out); + if pos * 2 + 1 < width(leaves.len(), height - 1) { + traverse(height - 1, pos * 2 + 1, leaves, include, out); + } + } + } + + let mut height = 0; + while width(leaves.len(), height) > 1 { + height += 1; + } + let mut built = Built { + bits: Vec::new(), + hashes: Vec::new(), + }; + traverse(height, 0, leaves, include, &mut built); + let mut flags = vec![0u8; built.bits.len().div_ceil(8)]; + for (i, bit) in built.bits.iter().enumerate() { + if *bit { + flags[i / 8] |= 1 << (i % 8); + } + } + let mut out = (leaves.len() as u32).to_le_bytes().to_vec(); + out.extend(var_int_bytes(built.hashes.len())); + for hash in &built.hashes { + out.extend(hash.iter().rev()); + } + out.extend(var_int_bytes(flags.len())); + out.extend(flags); + out + } + + /// The root of the whole tree over `leaves`, level by level, the last + /// node of an odd level paired with itself. + fn merkle_root(leaves: &[[u8; 32]]) -> [u8; 32] { + let mut level = leaves.to_vec(); + while level.len() > 1 { + level = level + .chunks(2) + .map(|pair| combine_left_right(&pair[0], pair.get(1).unwrap_or(&pair[0]))) + .collect(); + } + level[0] + } + + fn walked(tree: &[u8]) -> Result<(Vec<[u8; 32]>, [u8; 32]), PmtError> { + PartialMerkleTree::parse(tree)?.txn_hashes_and_merkle_root() + } + + /// A tree bitcoinj builds walks back to the root of the whole tree, and + /// matches the leaves it was built to, whatever their number and wherever + /// they sit. + #[test] + fn a_tree_walks_back_to_the_whole_trees_root() { + for n in (1..=40).chain([63, 64, 65, 255, 1000]) { + let leaves: Vec<_> = (0..n).map(leaf).collect(); + let root = merkle_root(&leaves); + let mut patterns: Vec> = vec![ + (0..n).map(|i| i == 0).collect(), + (0..n).map(|i| i == n - 1).collect(), + (0..n).map(|i| i % 3 == 1).collect(), + vec![true; n], + ]; + if n <= 40 { + patterns.extend((0..n).map(|k| (0..n).map(|i| i == k).collect())); + } + for include in patterns { + let tree = build(&leaves, &include); + assert!(has_expected_size(&tree), "{n} leaves"); + let want: Vec<_> = (0..n).filter(|i| include[*i]).map(|i| leaves[i]).collect(); + assert_eq!(walked(&tree), Ok((want, root)), "{n} leaves, {include:?}"); + } + } + } + + /// rskj's `PartialMerkleTreeFormatUtilsTest`: a tree of three transactions + /// and two hashes has the size its counts declare; with a byte lost from + /// the middle it has not, and with a hash count that overflows no size + /// can be worked out. + #[test] + fn rskjs_format_vectors() { + let tree = hex::decode( + "030000000279e7c0da739df8a00f12c0bff55e5438f530aa5859ff9874258cd7bad3fe70\ + 9746aff897e6a851faa80120d6ae99db30883699ac0428fc7192d6c3fec0ca6409010d", + ) + .unwrap(); + assert!(has_expected_size(&tree)); + let parsed = PartialMerkleTree::parse(&tree).unwrap(); + assert_eq!(parsed.transaction_count(), 3); + assert_eq!(parsed.hashes().len(), 2); + assert_eq!(parsed.matched_child_bits(), [0x0d]); + + let short = [&tree[..tree.len() - 3], &tree[tree.len() - 2..]].concat(); + assert!(!has_expected_size(&short)); + // rskj's `overflowSize`: "0300ffffff79e7c0...", a nine-byte hash count. + let overflow = [&[0x03, 0x00, 0xff, 0xff, 0xff][..], &tree[5..]].concat(); + assert!(!has_expected_size(&overflow)); + } + + /// The first peg-in on mainnet, at RSK #6916, proves Bitcoin transaction + /// b4b31c7c...94b5 in Bitcoin block #502,572, whose Merkle root is + /// 794bc781...835d. + #[test] + fn mainnets_first_peg_in_walks_to_its_blocks_root() { + let tree = hex::decode( + "630800000d14e6e59463afbfd4155db84d7592b5af577ef5dc0e9d906676e9ec81f37401\ + d472706346992d8220d9138dc144077bdbf5522de323024c2cf3d74e239fc11fb2afc654\ + 3956e6fb6f0cc43c5dd4c66fd271eff5f9309768dd7c267d767725f4538a093bb1fa025a\ + 85c0cd4967119bc286d9a951a828a7c413e4a8994ded5b9077b5944346e0ee8d09ed2e86\ + 22f3ad1e7b3a022d9696fbf894251e6d487c1cb3b4522650fec9201f035d965ce42cfcc1\ + 57b388470b6f6ff15dd72fd5651a57586cab8bac3b5531b3f490a97b2363a2ef18121a43\ + 873753a08e35991732fb789265cd9dd43584dbe701bb953f21af68029a30d8b6327cd57d\ + 2dd242327c1b135d4c8f5e36b8bf09f6e52420834e8e0e0b1a6df563aba550cf7f99e972\ + 4264187c45dcf3d73e8585a0e351963062f66fa39895750a0f0c651f2fc736d69b746ed7\ + 7319d5c0ecab6539d3655f163f3eccf325846b1c1244e17b6464d5b85c3cd06587cf9920\ + 67f37c086d17ab6dd35ee7aef6de22ac59ed37468ee5eb15948db42c438fdfb4c5b08d63\ + 16a203bcecd9713b9a2b150614ee116dcff986c29c2246745704bbdb0100", + ) + .unwrap(); + assert!(has_expected_size(&tree)); + let (matched, root) = walked(&tree).unwrap(); + let display = |s: &str| -> [u8; 32] { hex::decode(s).unwrap().try_into().unwrap() }; + assert_eq!( + root, + display("794bc781118b3bbe2c6a3a6ae21128bb50756e2dabe36e8225aacc133375835d") + ); + assert_eq!( + matched, + [display( + "b4b31c7c486d1e2594f8fb96962d023a7b1eadf322862eed098deee0464394b5" + )] + ); + } + + /// A tree as bitcoinj writes it, with its count, its hashes and its flags. + fn tree(count: u32, hashes: &[[u8; 32]], flags: &[u8]) -> Vec { + let mut out = count.to_le_bytes().to_vec(); + out.extend(var_int_bytes(hashes.len())); + for hash in hashes { + out.extend(hash.iter().rev()); + } + out.extend(var_int_bytes(flags.len())); + out.extend(flags); + out + } + + /// Each of bitcoinj's refusals, at the first thing it checks that fails. + #[test] + fn bitcoinj_refuses_what_it_refuses() { + let (a, b) = (leaf(1), leaf(2)); + let cases: [(&str, Vec, PmtError); 9] = [ + ( + "no transactions", + tree(0, &[a], &[1]), + PmtError::NoTransactions, + ), + ( + "a count past 2^31", + tree(u32::MAX, &[], &[]), + PmtError::MoreHashesThanTransactions, + ), + ( + "two hashes, one transaction", + tree(1, &[a, b], &[1]), + PmtError::MoreHashesThanTransactions, + ), + ( + "nine hashes, eight bits", + tree(9, &[a; 9], &[0xff]), + PmtError::FewerBitsThanHashes, + ), + ("no bits", tree(4, &[], &[]), PmtError::OutOfBits), + ("no hashes", tree(2, &[], &[0]), PmtError::OutOfHashes), + ( + "a byte of flags unused", + tree(1, &[a], &[1, 0]), + PmtError::UnusedData, + ), + ( + "a hash unused", + tree(2, &[a, b], &[0]), + PmtError::UnusedData, + ), + ( + "a branch repeated", + build(&[a, a], &[true, false]), + PmtError::DuplicatedBranches, + ), + ]; + for (what, tree, error) in cases { + assert_eq!(walked(&tree), Err(error), "{what}"); + } + } + + /// bitcoinj walks a tree of 16,666 transactions and no more. + #[test] + fn a_tree_holds_at_most_16666_transactions() { + for (n, walks) in [(16_666, true), (16_667, false)] { + let leaves: Vec<_> = (0..n).map(leaf).collect(); + let include: Vec<_> = (0..n).map(|i| i == n / 2).collect(); + let result = walked(&build(&leaves, &include)); + if walks { + assert_eq!(result.map(|r| r.1), Ok(merkle_root(&leaves))); + } else { + assert_eq!(result, Err(PmtError::TooManyTransactions)); + } + } + } + + /// Two nodes that read, in display order, as a 64-byte Bitcoin + /// transaction stop the walk where they would be combined. + #[test] + fn two_nodes_that_form_a_transaction_stop_the_walk() { + let transaction = [ + &[1u8, 0, 0, 0][..], + &[1], + &[0x11; 32], + &[0, 0, 0, 0], + &[2, 0xaa, 0xbb], + &[0xff; 4], + &[1], + &1i64.to_le_bytes(), + &[2, 0xcc, 0xdd], + &[0; 4], + ] + .concat(); + let left: [u8; 32] = transaction[..32].try_into().unwrap(); + let right: [u8; 32] = transaction[32..].try_into().unwrap(); + assert!(parses_as_a_64_byte_transaction(&left, &right)); + assert_eq!( + walked(&build(&[left, right], &[true, false])), + Err(PmtError::NodesFormTransaction) + ); + // The same two the other way round are no transaction. + assert!(walked(&build(&[right, left], &[true, false])).is_ok()); + } + + /// bitcoinj reads a tree from the start and leaves the rest. It casts each + /// varint to an `int`, so a nine-byte count keeps its low 32 bits, and + /// it refuses a count that is negative there, or a flag count over + /// 32 MiB, or bytes that are not there. + #[test] + fn bitcoinj_reads_a_tree_as_java_does() { + let one = tree(1, &[leaf(1)], &[1]); + assert_eq!(walked(&[one.clone(), vec![0xee; 3]].concat()), walked(&one)); + for cut in 0..one.len() { + assert_eq!( + PartialMerkleTree::parse(&one[..cut]), + Err(PmtError::Unreadable), + "{cut} bytes" + ); + } + + let count = &one[..4]; + let hash = &one[5..37]; + let nine_bytes_for_one = [0xff, 1, 0, 0, 0, 1, 0, 0, 0]; + let wrapped = [count, &nine_bytes_for_one, hash, &[1, 1]].concat(); + assert_eq!( + PartialMerkleTree::parse(&wrapped).unwrap().hashes().len(), + 1 + ); + assert!(!has_expected_size(&wrapped), "toIntExact refuses it"); + + let minus_one = [0xfe, 0xff, 0xff, 0xff, 0xff]; + let negative_hashes = [count, &minus_one[..], hash, &[1, 1]].concat(); + assert_eq!( + PartialMerkleTree::parse(&negative_hashes), + Err(PmtError::Unreadable) + ); + // Below zero, where what follows would read as flags, and at 2^31. + for negative in [&minus_one[..], &[0xfe, 0, 0, 0, 0x80]] { + let then_flags = [count, negative, &[1, 1]].concat(); + assert_eq!( + PartialMerkleTree::parse(&then_flags), + Err(PmtError::Unreadable) + ); + } + let negative_flags = [count, &[1], hash, &minus_one[..], &[1]].concat(); + assert_eq!( + PartialMerkleTree::parse(&negative_flags), + Err(PmtError::Unreadable) + ); + let over_32_mib = [count, &[1], hash, &[0xfe, 1, 0, 0, 2], &[1]].concat(); + assert_eq!( + PartialMerkleTree::parse(&over_32_mib), + Err(PmtError::Unreadable) + ); + // 32 MiB of flags are bitcoinj's most, there or not. + let flags = |n: u32| { + [ + count, + &[0], + &[0xfe], + &n.to_le_bytes(), + &vec![0u8; n as usize][..], + ] + .concat() + }; + assert!(PartialMerkleTree::parse(&flags(0x0200_0000)).is_ok()); + assert_eq!( + PartialMerkleTree::parse(&flags(0x0200_0001)), + Err(PmtError::Unreadable) + ); + } + + /// rskj takes a varint in any of its widths and counts the bytes it + /// took, refuses one past what an `int` holds, and takes no tree whose + /// sizes cannot be worked out without overflowing one. + #[test] + fn the_size_is_worked_out_as_rskj_does() { + let count = 1u32.to_le_bytes(); + let hash = leaf(1); + let wide = [&count[..], &[0xfd, 1, 0], &hash, &[0xfe, 1, 0, 0, 0], &[1]].concat(); + assert!(has_expected_size(&wide)); + assert!(walked(&wide).is_ok()); + let after = [wide.clone(), vec![0]].concat(); + assert!(!has_expected_size(&after)); + assert!(!has_expected_size(&wide[..wide.len() - 1])); + + // A nine-byte flag count of -1 declares a byte fewer than its own. + let minus_one = [0xff; 9]; + let negative = [&count[..], &[1], &hash, &minus_one[..]].concat(); + assert!(!has_expected_size(&negative)); + // A flag count past an `int`, its low 32 bits the one byte there. + let past_an_int = [ + &count[..], + &[1], + &hash, + &[0xff, 1, 0, 0, 0, 1, 0, 0, 0], + &[1], + ] + .concat(); + assert!( + PartialMerkleTree::parse(&past_an_int).is_ok(), + "bitcoinj keeps the low 32 bits" + ); + assert!(!has_expected_size(&past_an_int), "toIntExact refuses it"); + // 2^26 hashes are 2^31 bytes, past an `int`; one fewer are not, but + // the flag count would sit past the end. + assert!(!has_expected_size( + &[&count[..], &[0xfe, 0, 0, 0, 4]].concat() + )); + assert!(!has_expected_size( + &[&count[..], &[0xfe, 0xff, 0xff, 0xff, 3]].concat() + )); + assert!(!has_expected_size(&[])); + assert!(!has_expected_size(&count)); + } +} diff --git a/crates/execution/src/bridge/pmt.rs b/crates/execution/src/bridge/pmt.rs index e9f6ec5..ffaeb72 100644 --- a/crates/execution/src/bridge/pmt.rs +++ b/crates/execution/src/bridge/pmt.rs @@ -1,31 +1,27 @@ -//! Partial Merkle Tree (PMT) implementation for BTC transaction inclusion proofs. +//! The Bridge's proofs that a Bitcoin transaction is in a Bitcoin block. //! -//! Implements BIP37 merkleblock encoding, matching bitcoinj-thin's -//! `PartialMerkleTree` used by rskj. The PMT proves that a set of -//! transactions are included in a BTC block's merkle tree. +//! A partial Merkle tree is read and walked by `rustock_core`'s port of +//! bitcoinj-thin's `PartialMerkleTree`, the one rskj's Bridge shares with its +//! check of merged mining below RSKIP92. The Bridge compares in wire order, +//! the bytes a Bitcoin block hashes, so the tree's results, which bitcoinj +//! gives in display order, are turned round here. //! -//! ## Wire format -//! -//! ```text -//! [tx_count: u32 LE][nHashes: varint][hashes: 32*nHashes bytes] -//! [nFlagBytes: varint][flag_bits: nFlagBytes bytes] -//! ``` -//! -//! Hashes are in depth-first traversal order. Flag bits (LSB first per byte) -//! indicate whether each node is a "parent of match" (recurse deeper) or a -//! leaf/hash boundary. +//! A Merkle branch is rskj's `MerkleBranch`, for +//! `getBtcTransactionConfirmations`. It works in display order, as rskj's +//! does, since that is the order two of its nodes are tested in for a +//! 64-byte transaction. -use bitcoin::hashes::{sha256d, Hash}; +use rustock_core::partial_merkle_tree as bitcoinj; +use rustock_core::validation::merged_mining::{ + combine_left_right, parses_as_a_64_byte_transaction, +}; -/// A parsed Partial Merkle Tree. +/// A partial Merkle tree, as bitcoinj reads one. #[derive(Debug)] -pub struct PartialMerkleTree { - pub tx_count: u32, - hashes: Vec<[u8; 32]>, - flag_bits: Vec, -} +pub struct PartialMerkleTree(bitcoinj::PartialMerkleTree); -/// Result of PMT verification: merkle root + list of matched tx hashes. +/// What a tree walks to: the root and the transactions it matches, in wire +/// order. #[derive(Debug)] pub struct PmtResult { pub merkle_root: [u8; 32], @@ -33,348 +29,110 @@ pub struct PmtResult { } impl PartialMerkleTree { - /// Parse a PMT from its serialized form. + /// `new PartialMerkleTree(params, data, 0)`; `None` where bitcoinj throws. pub fn parse(data: &[u8]) -> Option { - if data.len() < 4 { - return None; - } - - // tx_count: u32 LE - let tx_count = u32::from_le_bytes([data[0], data[1], data[2], data[3]]); - let mut offset = 4; - - // nHashes: varint - let (n_hashes, varint_len) = read_varint(&data[offset..])?; - offset += varint_len; - - // hashes: 32 * nHashes bytes - let n_hashes = n_hashes as usize; - let mut hashes = Vec::with_capacity(n_hashes); - for _ in 0..n_hashes { - if offset + 32 > data.len() { - return None; - } - let mut hash = [0u8; 32]; - hash.copy_from_slice(&data[offset..offset + 32]); - hashes.push(hash); - offset += 32; - } - - // nFlagBytes: varint - let (n_flag_bytes, varint_len) = read_varint(&data[offset..])?; - offset += varint_len; - - // flag_bits: nFlagBytes bytes - let n_flag_bytes = n_flag_bytes as usize; - if offset + n_flag_bytes > data.len() { - return None; - } - - let mut flag_bits = Vec::with_capacity(n_flag_bytes * 8); - for i in 0..n_flag_bytes { - let byte = data[offset + i]; - for bit in 0..8 { - flag_bits.push((byte >> bit) & 1 == 1); - } - } - - Some(Self { - tx_count, - hashes, - flag_bits, - }) + bitcoinj::PartialMerkleTree::parse(data).ok().map(Self) } - /// Validate the expected size of PMT serialized data. + /// rskj `PartialMerkleTreeFormatUtils.hasExpectedSize`. pub fn has_expected_size(data: &[u8]) -> bool { - if data.len() < 4 { - return false; - } - let mut offset = 4; - - let (n_hashes, varint_len) = match read_varint(&data[offset..]) { - Some(v) => v, - None => return false, - }; - offset += varint_len; - let hash_bytes = (n_hashes as usize).checked_mul(32); - offset = match hash_bytes.and_then(|hb| offset.checked_add(hb)) { - Some(o) => o, - None => return false, - }; - - if offset > data.len() { - return false; - } - - let (n_flag_bytes, varint_len) = match read_varint(&data[offset..]) { - Some(v) => v, - None => return false, - }; - offset = match offset.checked_add(varint_len).and_then(|o| o.checked_add(n_flag_bytes as usize)) { - Some(o) => o, - None => return false, - }; + bitcoinj::has_expected_size(data) + } - offset == data.len() + /// The block's transaction count, as the Java `int` bitcoinj reads it into. + pub fn transaction_count(&self) -> i32 { + self.0.transaction_count() } - /// Extract matched transaction hashes and compute the merkle root. - /// - /// Returns `None` if the tree is invalid (malformed bits/hashes, or - /// the structure doesn't match tx_count). + /// `getTxnHashAndMerkleRoot`, in wire order; `None` where bitcoinj throws + /// a `VerificationException`. pub fn extract_matches(&self) -> Option { - if self.tx_count == 0 { - return None; - } - if self.hashes.len() > self.tx_count as usize { - return None; - } - - let height = tree_height(self.tx_count); - - let mut bits_used = 0usize; - let mut hashes_used = 0usize; - let mut matched = Vec::new(); - - let root = self.recursive_extract( - height, - 0, - &mut bits_used, - &mut hashes_used, - &mut matched, - )?; - - // Verify all hashes and bits were consumed - if hashes_used != self.hashes.len() { - return None; - } - // Allow unused padding bits in the last byte - let used_bytes = bits_used.div_ceil(8); - let total_bytes = self.flag_bits.len().div_ceil(8); - if used_bytes != total_bytes { - return None; - } - + let (matched, root) = self.0.txn_hashes_and_merkle_root().ok()?; Some(PmtResult { - merkle_root: root, - matched_hashes: matched, + merkle_root: reversed(root), + matched_hashes: matched.into_iter().map(reversed).collect(), }) } - - fn recursive_extract( - &self, - height: u32, - pos: u32, - bits_used: &mut usize, - hashes_used: &mut usize, - matched: &mut Vec<[u8; 32]>, - ) -> Option<[u8; 32]> { - if *bits_used >= self.flag_bits.len() { - return None; - } - let parent_of_match = self.flag_bits[*bits_used]; - *bits_used += 1; - - if height == 0 || !parent_of_match { - // Leaf or non-matching subtree: consume one hash - if *hashes_used >= self.hashes.len() { - return None; - } - let hash = self.hashes[*hashes_used]; - *hashes_used += 1; - - if height == 0 && parent_of_match { - matched.push(hash); - } - - return Some(hash); - } - - // Internal node that is parent of a match: recurse - let left = self.recursive_extract(height - 1, pos * 2, bits_used, hashes_used, matched)?; - - let right = if pos * 2 + 1 < tree_width(self.tx_count, height - 1) { - let r = self.recursive_extract( - height - 1, - pos * 2 + 1, - bits_used, - hashes_used, - matched, - )?; - // Left and right must differ (protection against 64-byte tx attack) - if left == r { - return None; - } - r - } else { - left - }; - - Some(combine_hashes(&left, &right)) - } -} - -/// Combine two 32-byte hashes for the Bitcoin merkle tree. -/// -/// Our hashes stay in wire ("internal", little-endian) order from parse -/// through extraction, so the combination is a plain double-SHA256 of the -/// concatenation — equivalent to rskj's `MerkleTreeUtils.combineLeftRight`, -/// which operates on display-order `Sha256Hash` values and therefore -/// reverses on the way in and out. -fn combine_hashes(left: &[u8; 32], right: &[u8; 32]) -> [u8; 32] { - let mut combined = [0u8; 64]; - combined[..32].copy_from_slice(left); - combined[32..].copy_from_slice(right); - *sha256d::Hash::hash(&combined).as_byte_array() -} - -/// Compute tree height from transaction count. -fn tree_height(tx_count: u32) -> u32 { - let mut height = 0u32; - let mut size = tx_count; - while size > 1 { - height += 1; - size = size.div_ceil(2); - } - height } -/// Compute tree width at a given height. -fn tree_width(tx_count: u32, height: u32) -> u32 { - (tx_count + (1 << height) - 1) >> height -} - -/// Read a Bitcoin varint from a byte slice. -/// Returns (value, bytes_consumed). -fn read_varint(data: &[u8]) -> Option<(u64, usize)> { - if data.is_empty() { - return None; - } - match data[0] { - 0..=0xFC => Some((data[0] as u64, 1)), - 0xFD => { - if data.len() < 3 { - return None; - } - let val = u16::from_le_bytes([data[1], data[2]]); - Some((val as u64, 3)) - } - 0xFE => { - if data.len() < 5 { - return None; - } - let val = u32::from_le_bytes([data[1], data[2], data[3], data[4]]); - Some((val as u64, 5)) - } - 0xFF => { - if data.len() < 9 { - return None; - } - let val = u64::from_le_bytes([ - data[1], data[2], data[3], data[4], data[5], data[6], data[7], data[8], - ]); - Some((val, 9)) - } - } +fn reversed(mut hash: [u8; 32]) -> [u8; 32] { + hash.reverse(); + hash } // --------------------------------------------------------------------------- // Merkle Branch (for getBtcTransactionConfirmations) // --------------------------------------------------------------------------- -/// A Merkle branch proof: list of sibling hashes + path bits. -/// -/// Matches rskj's `MerkleBranch`. The `path` integer encodes which side -/// the target hash is on at each level (bit i: 0 = left, 1 = right). +/// rskj `MerkleBranch`: the hashes beside a transaction's on its way up the +/// tree, and its path, whose bit `i` is set where, `i` levels up, the +/// transaction's side is the right one. The hashes are in display order, as +/// rskj wraps the ones it is given. #[derive(Debug)] pub struct MerkleBranch { - pub hashes: Vec<[u8; 32]>, - pub path: u32, + hashes: Vec<[u8; 32]>, + path: i32, } impl MerkleBranch { + /// rskj's constructor: no more than 32 hashes, and no more significant + /// bits in the path than there are hashes. rskj's `significantBitCount` + /// counts none in a path below zero. Where rskj throws, `None`. + pub fn new(hashes: Vec<[u8; 32]>, path: i32) -> Option { + let significant = if path > 0 { + 32 - path.leading_zeros() as usize + } else { + 0 + }; + (hashes.len() <= 32 && significant <= hashes.len()).then_some(Self { hashes, path }) + } + /// Parse from ABI-encoded arguments: - /// `(bytes32 btcTxHash, bytes32 btcBlockHash, uint256 merkleBranchPath, bytes32[] merkleBranchHashes)` - /// - /// The path and hashes are at offsets 64..96 and 96+, respectively. + /// `(bytes32 btcTxHash, bytes32 btcBlockHash, uint256 merkleBranchPath, bytes32[] merkleBranchHashes)`. + /// The transaction and block hashes come back as given, in display order, + /// and the path as rskj's `BigInteger.intValue` reads it, its low 32 bits. pub fn from_abi_args(args: &[u8]) -> Option<(Self, [u8; 32], [u8; 32])> { if args.len() < 128 { return None; } - - let mut tx_hash = [0u8; 32]; - tx_hash.copy_from_slice(&args[0..32]); - - let mut block_hash = [0u8; 32]; - block_hash.copy_from_slice(&args[32..64]); - - let path = { - let mut buf = [0u8; 32]; - buf.copy_from_slice(&args[64..96]); - u32::from_be_bytes([buf[28], buf[29], buf[30], buf[31]]) - }; - - // Dynamic array offset - let array_offset = { - let mut buf = [0u8; 32]; - buf.copy_from_slice(&args[96..128]); - u32::from_be_bytes([buf[28], buf[29], buf[30], buf[31]]) as usize - }; - - if array_offset + 32 > args.len() { - return None; - } - - let count = { - let mut buf = [0u8; 32]; - buf.copy_from_slice(&args[array_offset..array_offset + 32]); - u32::from_be_bytes([buf[28], buf[29], buf[30], buf[31]]) as usize + let tx_hash: [u8; 32] = args[0..32].try_into().ok()?; + let block_hash: [u8; 32] = args[32..64].try_into().ok()?; + let path = i32::from_be_bytes(args[92..96].try_into().ok()?); + + // The array's offset and, there, its length, each in its word's low + // four bytes. + let word = |at: usize| -> Option { + Some(u32::from_be_bytes(args.get(at + 28..at + 32)?.try_into().ok()?) as usize) }; - - let mut hashes = Vec::with_capacity(count); + let array_offset = word(96)?; + let count = word(array_offset)?; let data_start = array_offset + 32; + let mut hashes = Vec::new(); for i in 0..count { - let offset = data_start + i * 32; - if offset + 32 > args.len() { - return None; - } - let mut h = [0u8; 32]; - h.copy_from_slice(&args[offset..offset + 32]); - hashes.push(h); + let at = data_start + i * 32; + hashes.push(args.get(at..at + 32)?.try_into().ok()?); } - - // ABI callers supply display-order hashes (rskj Sha256Hash.wrap of - // the bytes32 args); our merkle arithmetic uses wire (internal) - // order throughout. - #[allow(clippy::redundant_locals)] // rebind to mutate the caller's copy - let mut tx_hash = tx_hash; - tx_hash.reverse(); - let hashes = hashes - .into_iter() - .map(|mut h| { - h.reverse(); - h - }) - .collect(); - let branch = MerkleBranch { hashes, path }; - Some((branch, tx_hash, block_hash)) + Some((MerkleBranch::new(hashes, path)?, tx_hash, block_hash)) } - /// Reduce the branch from a given tx hash to compute the merkle root. - pub fn reduce_from(&self, tx_hash: &[u8; 32]) -> [u8; 32] { + /// `reduceFrom`: the root the branch climbs to from `tx_hash`, in display + /// order. `None` where two of the nodes form a 64-byte transaction, where + /// rskj's `MerkleTreeUtils.combineLeftRight` throws. + pub fn reduce_from(&self, tx_hash: &[u8; 32]) -> Option<[u8; 32]> { let mut current = *tx_hash; - - for (i, sibling) in self.hashes.iter().enumerate() { - let is_right = (self.path >> i) & 1 == 1; - if is_right { - current = combine_hashes(sibling, ¤t); + for (index, sibling) in self.hashes.iter().enumerate() { + // `(path >> index) & 1`, the constructor holding `index` below 32. + let (left, right) = if (self.path >> index) & 1 == 1 { + (*sibling, current) } else { - current = combine_hashes(¤t, sibling); + (current, *sibling) + }; + if parses_as_a_64_byte_transaction(&left, &right) { + return None; } + current = combine_left_right(&left, &right); } - - current + Some(current) } } @@ -386,55 +144,12 @@ impl MerkleBranch { mod tests { use super::*; - #[test] - fn tree_height_one_tx() { - assert_eq!(tree_height(1), 0); - } - - #[test] - fn tree_height_two_tx() { - assert_eq!(tree_height(2), 1); - } - - #[test] - fn tree_height_three_tx() { - assert_eq!(tree_height(3), 2); - } - - #[test] - fn tree_height_four_tx() { - assert_eq!(tree_height(4), 2); - } - - #[test] - fn tree_width_single() { - assert_eq!(tree_width(1, 0), 1); - } - - #[test] - fn tree_width_two_at_level_zero() { - assert_eq!(tree_width(2, 0), 2); - assert_eq!(tree_width(2, 1), 1); - } - - #[test] - fn tree_width_three() { - assert_eq!(tree_width(3, 0), 3); - assert_eq!(tree_width(3, 1), 2); - assert_eq!(tree_width(3, 2), 1); - } - - #[test] - fn varint_single_byte() { - assert_eq!(read_varint(&[0x00]), Some((0, 1))); - assert_eq!(read_varint(&[0x01]), Some((1, 1))); - assert_eq!(read_varint(&[0xFC]), Some((252, 1))); + fn hex_to_bytes(hex: &str) -> Vec { + alloy_primitives::hex::decode(hex.replace(char::is_whitespace, "")).unwrap() } - #[test] - fn varint_two_bytes() { - assert_eq!(read_varint(&[0xFD, 0xFD, 0x00]), Some((253, 3))); - assert_eq!(read_varint(&[0xFD, 0xFF, 0xFF]), Some((65535, 3))); + fn hex_to_32(hex: &str) -> [u8; 32] { + hex_to_bytes(hex).try_into().unwrap() } #[test] @@ -443,248 +158,208 @@ mod tests { assert!(!PartialMerkleTree::has_expected_size(&[0, 0, 0])); } + /// A block of one transaction, matched: the tree is its hash alone, and + /// so is the root. #[test] fn simple_pmt_single_tx() { - // PMT for a block with 1 transaction (the tx itself is matched) - // tx_count=1, nHashes=1, hash=all-zeros, nFlagBytes=1, flags=0x01 - let mut data = Vec::new(); - data.extend_from_slice(&1u32.to_le_bytes()); // tx_count - data.push(1); // nHashes (varint) - data.extend_from_slice(&[0xAA; 32]); // hash - data.push(1); // nFlagBytes (varint) - data.push(0x01); // flag bits: bit 0 = 1 (matched) + let mut data = 1u32.to_le_bytes().to_vec(); + data.push(1); + data.extend_from_slice(&[0xAA; 32]); + data.push(1); + data.push(0x01); assert!(PartialMerkleTree::has_expected_size(&data)); - let pmt = PartialMerkleTree::parse(&data).unwrap(); - assert_eq!(pmt.tx_count, 1); - + assert_eq!(pmt.transaction_count(), 1); let result = pmt.extract_matches().unwrap(); - assert_eq!(result.matched_hashes.len(), 1); - assert_eq!(result.matched_hashes[0], [0xAA; 32]); + assert_eq!(result.matched_hashes, [[0xAA; 32]]); assert_eq!(result.merkle_root, [0xAA; 32]); } + /// Ported from rskj PartialMerkleTreeFormatUtilsTest: the canonical blob + /// has its expected size, with a byte gone it has not, and with a hash + /// count that overflows no size can be worked out. #[test] - fn combine_hashes_deterministic() { - let a = [0x01; 32]; - let b = [0x02; 32]; - let result = combine_hashes(&a, &b); - // Same inputs should always produce same output - assert_eq!(result, combine_hashes(&a, &b)); - // Different order should produce different result - assert_ne!(result, combine_hashes(&b, &a)); - } - - #[test] - fn merkle_branch_reduce_single() { - let tx_hash = [0x42; 32]; - let sibling = [0x43; 32]; - let branch = MerkleBranch { - hashes: vec![rev(sibling)], - path: 0, // tx is on the left - }; - let root = branch.reduce_from(&rev(tx_hash)); - assert_eq!(root, combine_hashes(&tx_hash, &sibling)); - } - - #[test] - fn merkle_branch_reduce_right_side() { - let tx_hash = [0x42; 32]; - let sibling = [0x43; 32]; - let branch = MerkleBranch { - hashes: vec![rev(sibling)], - path: 1, // tx is on the right - }; - let root = branch.reduce_from(&rev(tx_hash)); - assert_eq!(root, combine_hashes(&sibling, &tx_hash)); - } - - // ----------------------------------------------------------------------- - // Tests ported from rskj - // ----------------------------------------------------------------------- - - /// Ported from rskj PartialMerkleTreeFormatUtilsTest.getHashesCount - /// and getFlagBitsCount — canonical PMT blob from rskj test suite. - #[test] - fn rskj_pmt_canonical_blob_parse() { - let data = hex_to_bytes( - "030000000279e7c0da739df8a00f12c0bff55e5438f530aa5859ff98\ - 74258cd7bad3fe709746aff897e6a851faa80120d6ae99db30883699\ - ac0428fc7192d6c3fec0ca6409010d" - ); - - // rskj expects: 2 hashes, 1 flag byte, valid size + fn rskj_pmt_format_vectors() { + let canonical = "030000000279e7c0da739df8a00f12c0bff55e5438f530aa5859ff98\ + 74258cd7bad3fe709746aff897e6a851faa80120d6ae99db30883699\ + ac0428fc7192d6c3fec0ca6409010d"; + let data = hex_to_bytes(canonical); assert!(PartialMerkleTree::has_expected_size(&data)); - - let pmt = PartialMerkleTree::parse(&data).unwrap(); - assert_eq!(pmt.tx_count, 3); // LE 03000000 = 3 - assert_eq!(pmt.hashes.len(), 2); - assert_eq!(pmt.flag_bits.len(), 8); // 1 flag byte = 8 bits - } - - /// Ported from rskj PartialMerkleTreeFormatUtilsTest.doesntHaveExpectedSize - #[test] - fn rskj_pmt_invalid_size_rejected() { - // Truncated/wrong tail (from rskj: ending in "64010d") - let data = hex_to_bytes( - "030000000279e7c0da739df8a00f12c0bff55e5438f530aa5859ff98\ - 74258cd7bad3fe709746aff897e6a851faa80120d6ae99db30883699\ - ac0428fc7192d6c3fec0ca64010d" + assert_eq!( + PartialMerkleTree::parse(&data).unwrap().transaction_count(), + 3 ); - assert!(!PartialMerkleTree::has_expected_size(&data)); - } - - /// rskj branch vectors are display-order hashes; reverse for our wire - /// order convention. - fn rev(mut h: [u8; 32]) -> [u8; 32] { - h.reverse(); - h - } - /// Ported from rskj MerkleBranchTest.oneHashBranch - /// assertBranchCorrectlyProves(hashes, path, txHash, expectedMerkleRoot) - #[test] - fn rskj_merkle_branch_one_hash() { - let h1 = hex_to_32("3709934297f8bfd8a1cfcf82514bbfdcc910cf4d934e0eabd58b6eb955954b45"); - let tx_hash = hex_to_32("444f6714a6010b452d705531fb9ed850f2578ef28c364676d7e9f3f436c25554"); - let expected_root = hex_to_32("71306e0f38f1f606d7a4eadf22814432321b2a6d6f40c94fdc8e7dd2bbcef47f"); - - let branch = MerkleBranch { - hashes: vec![rev(h1)], - path: 1, - }; - let root = branch.reduce_from(&rev(tx_hash)); - assert_eq!(rev(root), expected_root, "rskj oneHashBranch root mismatch"); + let short = canonical.replace("ca6409010d", "ca64010d"); + assert!(!PartialMerkleTree::has_expected_size(&hex_to_bytes(&short))); + let overflow = canonical.replacen("0300000002", "0300ffffff", 1); + assert!(!PartialMerkleTree::has_expected_size(&hex_to_bytes( + &overflow + ))); } - /// Ported from rskj MerkleBranchTest.twoHashesBranch + /// rskj's MerkleBranchTest vectors, display order as rskj gives them: one, + /// two and three hashes climb to their roots, and a wrong path does not. #[test] - fn rskj_merkle_branch_two_hashes() { - let h1 = hex_to_32("bfc0770be0c8bc9d06714b00c89cc769286968c28632aa7768f9525a0287d5e6"); - let h2 = hex_to_32("ad13c3242a67436e8958c0b1a76ff017d1e0c36ab7b38b98d7fb3df9ebea2bff"); - let tx_hash = hex_to_32("9f4c1131e3dfab1b0413f97d53cab183f48f4ed0003387e7cbadea0c1f4ce365"); - let expected_root = hex_to_32("14fd0a443ebba0fe62e323b5250fb13042e7840701058fb025032f1527f8f6f5"); - - let branch = MerkleBranch { - hashes: vec![rev(h1), rev(h2)], - path: 3, - }; - let root = branch.reduce_from(&rev(tx_hash)); - assert_eq!(rev(root), expected_root, "rskj twoHashesBranch root mismatch"); + fn rskj_merkle_branch_vectors() { + let cases: [(&[&str], i32, &str, &str); 4] = [ + ( + &["3709934297f8bfd8a1cfcf82514bbfdcc910cf4d934e0eabd58b6eb955954b45"], + 1, + "444f6714a6010b452d705531fb9ed850f2578ef28c364676d7e9f3f436c25554", + "71306e0f38f1f606d7a4eadf22814432321b2a6d6f40c94fdc8e7dd2bbcef47f", + ), + ( + &[ + "bfc0770be0c8bc9d06714b00c89cc769286968c28632aa7768f9525a0287d5e6", + "ad13c3242a67436e8958c0b1a76ff017d1e0c36ab7b38b98d7fb3df9ebea2bff", + ], + 3, + "9f4c1131e3dfab1b0413f97d53cab183f48f4ed0003387e7cbadea0c1f4ce365", + "14fd0a443ebba0fe62e323b5250fb13042e7840701058fb025032f1527f8f6f5", + ), + ( + &[ + "81aa2c77c201daab3868da9a4c2e29bc1e42bdc804c7c8a4d84c5e7f3866fb3f", + "bed5ecce0c0ffa58236271d4ca49767c968fb6aba6aa3c10abe31170ca826404", + "f733ca23def34db37b79af4c65d5929be741b79a1d74006153892b87487d1137", + ], + 5, + "807d74e37d9c39a315eb74955889c9be83ba33eaaf9735a9617211870cde22b2", + "f7f0eb7ba33dd6f37ad11153d1bc803cd6f932051d72fa73343bff59d270e9ef", + ), + ( + &[ + "807d74e37d9c39a315eb74955889c9be83ba33eaaf9735a9617211870cde22b2", + "bed5ecce0c0ffa58236271d4ca49767c968fb6aba6aa3c10abe31170ca826404", + "f733ca23def34db37b79af4c65d5929be741b79a1d74006153892b87487d1137", + ], + 4, + "81aa2c77c201daab3868da9a4c2e29bc1e42bdc804c7c8a4d84c5e7f3866fb3f", + "f7f0eb7ba33dd6f37ad11153d1bc803cd6f932051d72fa73343bff59d270e9ef", + ), + ]; + for (hashes, path, tx, root) in cases { + let hashes: Vec<_> = hashes.iter().map(|h| hex_to_32(h)).collect(); + let branch = MerkleBranch::new(hashes.clone(), path).unwrap(); + assert_eq!( + branch.reduce_from(&hex_to_32(tx)), + Some(hex_to_32(root)), + "path {path}" + ); + if path == 4 { + let wrong = MerkleBranch::new(hashes, 3).unwrap(); + assert_ne!(wrong.reduce_from(&hex_to_32(tx)), Some(hex_to_32(root))); + } + } } - /// Ported from rskj MerkleBranchTest.threeHashesBranch + /// rskj's MerkleBranch takes at most 32 hashes and a path with no more + /// significant bits than hashes, counting none in a negative path. #[test] - fn rskj_merkle_branch_three_hashes() { - let h1 = hex_to_32("81aa2c77c201daab3868da9a4c2e29bc1e42bdc804c7c8a4d84c5e7f3866fb3f"); - let h2 = hex_to_32("bed5ecce0c0ffa58236271d4ca49767c968fb6aba6aa3c10abe31170ca826404"); - let h3 = hex_to_32("f733ca23def34db37b79af4c65d5929be741b79a1d74006153892b87487d1137"); - let tx_hash = hex_to_32("807d74e37d9c39a315eb74955889c9be83ba33eaaf9735a9617211870cde22b2"); - let expected_root = hex_to_32("f7f0eb7ba33dd6f37ad11153d1bc803cd6f932051d72fa73343bff59d270e9ef"); - - let branch = MerkleBranch { - hashes: vec![rev(h1), rev(h2), rev(h3)], - path: 5, - }; - let root = branch.reduce_from(&rev(tx_hash)); - assert_eq!(rev(root), expected_root, "rskj threeHashesBranch root mismatch"); - } - - /// Ported from rskj MerkleBranchTest.threeHashesBranchBis (alternate leaf) + fn a_merkle_branch_is_held_to_rskjs_constructor() { + let hashes = |n: usize| vec![[0x5a; 32]; n]; + assert!(MerkleBranch::new(hashes(32), 0).is_some()); + assert!(MerkleBranch::new(hashes(33), 0).is_none()); + assert!(MerkleBranch::new(hashes(3), 0b111).is_some()); + assert!(MerkleBranch::new(hashes(3), 0b1000).is_none()); + assert!(MerkleBranch::new(hashes(0), 0).is_some()); + assert!(MerkleBranch::new(hashes(0), 1).is_none()); + assert!(MerkleBranch::new(hashes(1), i32::MIN).is_some()); + assert!(MerkleBranch::new(hashes(31), i32::MAX).is_some()); + assert!(MerkleBranch::new(hashes(30), i32::MAX).is_none()); + } + + /// Two nodes that read, in display order, as a 64-byte Bitcoin + /// transaction stop the climb, on whichever side the transaction sits. #[test] - fn rskj_merkle_branch_three_hashes_bis() { - let h1 = hex_to_32("807d74e37d9c39a315eb74955889c9be83ba33eaaf9735a9617211870cde22b2"); - let h2 = hex_to_32("bed5ecce0c0ffa58236271d4ca49767c968fb6aba6aa3c10abe31170ca826404"); - let h3 = hex_to_32("f733ca23def34db37b79af4c65d5929be741b79a1d74006153892b87487d1137"); - let tx_hash = hex_to_32("81aa2c77c201daab3868da9a4c2e29bc1e42bdc804c7c8a4d84c5e7f3866fb3f"); - let expected_root = hex_to_32("f7f0eb7ba33dd6f37ad11153d1bc803cd6f932051d72fa73343bff59d270e9ef"); - - let branch = MerkleBranch { - hashes: vec![rev(h1), rev(h2), rev(h3)], - path: 4, - }; - let root = branch.reduce_from(&rev(tx_hash)); - assert_eq!(rev(root), expected_root, "rskj threeHashesBranchBis root mismatch"); - } - - /// Ported from rskj MerkleBranchTest.threeHashesBranchFailsIfPathIsWrong + fn a_merkle_branch_through_a_transaction_shaped_pair_fails() { + let transaction = [ + &[1u8, 0, 0, 0][..], + &[1], + &[0x11; 32], + &[0, 0, 0, 0], + &[2, 0xaa, 0xbb], + &[0xff; 4], + &[1], + &1i64.to_le_bytes(), + &[2, 0xcc, 0xdd], + &[0; 4], + ] + .concat(); + let left: [u8; 32] = transaction[..32].try_into().unwrap(); + let right: [u8; 32] = transaction[32..].try_into().unwrap(); + let on_the_left = MerkleBranch::new(vec![right], 0).unwrap(); + assert_eq!(on_the_left.reduce_from(&left), None); + let on_the_right = MerkleBranch::new(vec![left], 1).unwrap(); + assert_eq!(on_the_right.reduce_from(&right), None); + // The other way round they are no transaction. + assert!(MerkleBranch::new(vec![left], 0) + .unwrap() + .reduce_from(&right) + .is_some()); + } + + /// The ABI's path is read as rskj's `BigInteger.intValue` reads it, and the + /// branch is held to rskj's constructor. #[test] - fn rskj_merkle_branch_wrong_path() { - let h1 = hex_to_32("807d74e37d9c39a315eb74955889c9be83ba33eaaf9735a9617211870cde22b2"); - let h2 = hex_to_32("bed5ecce0c0ffa58236271d4ca49767c968fb6aba6aa3c10abe31170ca826404"); - let h3 = hex_to_32("f733ca23def34db37b79af4c65d5929be741b79a1d74006153892b87487d1137"); - let tx_hash = hex_to_32("81aa2c77c201daab3868da9a4c2e29bc1e42bdc804c7c8a4d84c5e7f3866fb3f"); - let expected_root = hex_to_32("f7f0eb7ba33dd6f37ad11153d1bc803cd6f932051d72fa73343bff59d270e9ef"); - - let branch = MerkleBranch { - hashes: vec![rev(h1), rev(h2), rev(h3)], - path: 3, // wrong path + fn merkle_branch_abi_args() { + let args = |path: [u8; 32], count: usize| { + let mut args = [[0x01; 32], [0x02; 32], path].concat(); + let mut offset = [0u8; 32]; + offset[31] = 128; + args.extend(offset); + let mut length = [0u8; 32]; + length[31] = count as u8; + args.extend(length); + for i in 0..count { + args.extend([i as u8; 32]); + } + args }; - let root = branch.reduce_from(&rev(tx_hash)); - assert_ne!(root, expected_root, "Wrong path should produce different root"); - } - - /// rskj vectors are display-order `Sha256Hash` values; our convention is - /// wire (internal) order, so they are reversed on the way in and out. - fn combine_display(left: &str, right: &str) -> [u8; 32] { - let mut l = hex_to_32(left); - l.reverse(); - let mut r = hex_to_32(right); - r.reverse(); - let mut out = combine_hashes(&l, &r); - out.reverse(); - out - } - - /// Ported from rskj MerkleTreeUtilsTest.combineLeftRight — 3 vectors from - /// bitcoind regtest (two-transaction blocks). - #[test] - fn rskj_combine_left_right_vector_1() { - let expected = hex_to_32("ceea4835dd23fae1978a3f6f3f0aa0171e018360272dd5b98d37550fbc978d01"); - assert_eq!(combine_display("b945b008fbc3f357db745909958b570773fc14575a36af8bbc195b484e21f366", "9880f57b6735152a8c6d4c7e1b3bc6434ee75e459511a642bbb8cb71d3a6b6d8"), expected, "rskj combineLeftRight vector 1"); - } - - #[test] - fn rskj_combine_left_right_vector_2() { - let expected = hex_to_32("107857d7233c41d4c37ecaa9ad9d9ab15371f643074866cd23d657e6e99676be"); - assert_eq!(combine_display("083eafdf670bb1bbc83b63262887e3cf519c3e252fac29adfb92c1e857b37f91", "5740915e973a211c71655d10e4c672301c27c287843dcfa97b7aafc04992ec5e"), expected, "rskj combineLeftRight vector 2"); - } - - #[test] - fn rskj_combine_left_right_vector_3() { - let expected = hex_to_32("71a12c9bd54735864dd6e12640e6d00d60a42a2e92e4cd0bde3f9f268b7d4345"); - assert_eq!(combine_display("c960ed36a67318cd562d384bfbf41499db1312835e2bfe86805d9465afe9736f", "120196be3b0ca6ba07d3cfee53f8dc883781e82afdfba11181184f41a67b9898"), expected, "rskj combineLeftRight vector 3"); - } - - /// Ported from rskj PartialMerkleTreeFormatUtilsTest.overflowSize - #[test] - fn rskj_pmt_overflow_size() { - let data = hex_to_bytes( - "0300ffffff79e7c0da739df8a00f12c0bff55e5438f530aa5859ff98\ - 74258cd7bad3fe709746aff897e6a851faa80120d6ae99db30883699\ - ac0428fc7192d6c3fec0ca6409010d" + let mut path = [0u8; 32]; + path[31] = 0b10; + let (branch, tx, block) = MerkleBranch::from_abi_args(&args(path, 2)).unwrap(); + assert_eq!( + (branch.path, branch.hashes.len(), tx, block), + (2, 2, [0x01; 32], [0x02; 32]) ); - // In rskj this throws ArithmeticException due to overflow in hash count. - // Our parser should either reject it or fail to parse. - assert!(!PartialMerkleTree::has_expected_size(&data) || PartialMerkleTree::parse(&data).is_none()); - } - - // Test helpers - fn hex_to_bytes(hex: &str) -> Vec { - let hex = hex.replace(|c: char| c.is_whitespace(), ""); - (0..hex.len()) - .step_by(2) - .map(|i| u8::from_str_radix(&hex[i..i + 2], 16).unwrap()) - .collect() + // Only the low 32 bits count: this path is -1, of no significant bits. + let high = [0xff; 32]; + assert_eq!( + MerkleBranch::from_abi_args(&args(high, 1)).unwrap().0.path, + -1 + ); + path[31] = 0b100; + assert!(MerkleBranch::from_abi_args(&args(path, 2)).is_none()); + assert!(MerkleBranch::from_abi_args(&args([0; 32], 33)).is_none()); } - fn hex_to_32(hex: &str) -> [u8; 32] { - let bytes = hex_to_bytes(hex); - assert_eq!(bytes.len(), 32); - let mut arr = [0u8; 32]; - arr.copy_from_slice(&bytes); - arr + /// Ported from rskj MerkleTreeUtilsTest.combineLeftRight: three vectors + /// from bitcoind regtest, display order. + #[test] + fn rskj_combine_left_right_vectors() { + for (left, right, expected) in [ + ( + "b945b008fbc3f357db745909958b570773fc14575a36af8bbc195b484e21f366", + "9880f57b6735152a8c6d4c7e1b3bc6434ee75e459511a642bbb8cb71d3a6b6d8", + "ceea4835dd23fae1978a3f6f3f0aa0171e018360272dd5b98d37550fbc978d01", + ), + ( + "083eafdf670bb1bbc83b63262887e3cf519c3e252fac29adfb92c1e857b37f91", + "5740915e973a211c71655d10e4c672301c27c287843dcfa97b7aafc04992ec5e", + "107857d7233c41d4c37ecaa9ad9d9ab15371f643074866cd23d657e6e99676be", + ), + ( + "c960ed36a67318cd562d384bfbf41499db1312835e2bfe86805d9465afe9736f", + "120196be3b0ca6ba07d3cfee53f8dc883781e82afdfba11181184f41a67b9898", + "71a12c9bd54735864dd6e12640e6d00d60a42a2e92e4cd0bde3f9f268b7d4345", + ), + ] { + let combined = combine_left_right(&hex_to_32(left), &hex_to_32(right)); + assert_eq!(combined, hex_to_32(expected)); + } } /// Groundtruth: the PMT from the first mainnet peg-in (RSK #6916, @@ -702,12 +377,16 @@ mod tests { // Internal (little-endian) byte order, as stored in BTC headers. let mut expected_root = alloy_primitives::hex::decode( - "794bc781118b3bbe2c6a3a6ae21128bb50756e2dabe36e8225aacc133375835d").unwrap(); + "794bc781118b3bbe2c6a3a6ae21128bb50756e2dabe36e8225aacc133375835d", + ) + .unwrap(); expected_root.reverse(); assert_eq!(result.merkle_root.to_vec(), expected_root, "merkle root"); let mut expected_txid = alloy_primitives::hex::decode( - "b4b31c7c486d1e2594f8fb96962d023a7b1eadf322862eed098deee0464394b5").unwrap(); + "b4b31c7c486d1e2594f8fb96962d023a7b1eadf322862eed098deee0464394b5", + ) + .unwrap(); expected_txid.reverse(); let contains = result .matched_hashes diff --git a/crates/execution/src/bridge/tx.rs b/crates/execution/src/bridge/tx.rs index 19c8919..f832514 100644 --- a/crates/execution/src/bridge/tx.rs +++ b/crates/execution/src/bridge/tx.rs @@ -254,8 +254,9 @@ pub fn register_btc_coinbase_transaction( // Validate the witness commitment (rskj `validateWitnessInformation`, // BridgeSupport l.2568-2587). The commitment embedded in the coinbase tx // must equal SHA256d(witnessMerkleRoot.getReversedBytes() || witnessReservedValue). - // The ABI arg is the witness merkle root in INTERNAL byte order (bitcoinj - // `Sha256Hash.wrap`), so it must be reversed to display order before hashing. + // The ABI arg is the witness merkle root in display order (bitcoinj + // `Sha256Hash.wrap` keeps the bytes as given), and the commitment hashes it + // in wire order, so it is reversed here. It is stored as given. let mut commitment_data = [0u8; 64]; commitment_data[..32].copy_from_slice(&witness_merkle_root); commitment_data[..32].reverse(); @@ -401,21 +402,27 @@ pub fn get_btc_transaction_confirmations( } } - // 4. Merkle branch must reduce to the block's merkle root (or, from - // RSKIP143, to the registered witness merkle root). - let computed_root = branch.reduce_from(&tx_hash); + // 4. The branch must climb to the block's merkle root or, from RSKIP143, + // to the witness merkle root registered for the block (rskj + // `isBlockMerkleRootValid`). rskj holds all of them in display order: + // the branch and the transaction as the ABI gives them, the header's + // root as bitcoinj reads it, and the witness root as + // `registerBtcCoinbaseTransaction` stored it. Two branch nodes that + // form a 64-byte transaction make rskj's `combineLeftRight` throw, + // which fails the call. + let computed_root = branch.reduce_from(&tx_hash).ok_or_else(|| { + PrecompileError::other( + "getBtcTransactionConfirmations: two branch nodes form a transaction", + ) + })?; let block_merkle_root = { - let raw = block.header.merkle_root.to_raw_hash(); - *raw.as_byte_array() - }; - let root_valid = if computed_root == block_merkle_root { - true - } else { - match get_coinbase_information(ctx, &block_hash_bytes) { - Some(witness_root) => computed_root == witness_root, - None => false, - } + let mut root = *block.header.merkle_root.to_raw_hash().as_byte_array(); + root.reverse(); + root }; + let root_valid = computed_root == block_merkle_root + || get_coinbase_information(ctx, &block_hash_bytes) + .is_some_and(|witness_root| witness_root == computed_root); if !root_valid { return Ok(encode_int_output( gas_cost, @@ -639,7 +646,7 @@ mod tests { "feb3b9170ae765cc6586edd67229eaa8bc19f9674d64cb10ee8a205f4ccf0bc6" ); - // witnessMerkleRoot ABI arg (INTERNAL byte order, bitcoinj Sha256Hash.wrap): + // witnessMerkleRoot ABI arg, display order (bitcoinj Sha256Hash.wrap): // = MerkleTreeUtils.combineLeftRight(ZERO_HASH, secondHashTx) from the rskj test. let witness_merkle_root: [u8; 32] = hex::decode("613cb22535df8d9443fe94b66d807cd60312f982e305e25e825b00e6f429799f") diff --git a/crates/execution/src/executor.rs b/crates/execution/src/executor.rs index 548dee1..1d2d063 100644 --- a/crates/execution/src/executor.rs +++ b/crates/execution/src/executor.rs @@ -5687,6 +5687,232 @@ mod tests { let _ = b256_to_bitcoin_hash; } + /// rskj compares a `getBtcTransactionConfirmations` branch in display + /// order: with the header's merkle root as bitcoinj reads it, and with the + /// witness merkle root as `registerBtcCoinbaseTransaction` stored it, its + /// ABI argument untouched. rustock climbed the branch in wire order, so a + /// transaction proved through the witness tree got -5 where rskj counts + /// its confirmations. And two branch nodes that read as a 64-byte + /// transaction make rskj's `combineLeftRight` throw, so the call answers + /// nothing: a top-level Bridge call that throws is rskj's invisible + /// exception, a successful receipt with no return data. + /// + /// On regtest: one block of two transactions, a coinbase and one more, + /// whose roots are no palindromes, so the order shows. + #[test] + fn test_btc_transaction_confirmations_compare_in_display_order() { + use bitcoin::absolute::LockTime; + use bitcoin::block::Version; + use bitcoin::consensus::serialize as btc_serialize; + use bitcoin::hashes::{sha256d, Hash as _}; + use bitcoin::{ + Amount, CompactTarget, OutPoint, ScriptBuf, Sequence, TxIn, TxMerkleNode, TxOut, + Witness, + }; + use rustock_core::validation::merged_mining::combine_left_right; + + const BLOCK: u64 = 7_000_000; // every relevant RSKIP active + let reversed = |mut hash: [u8; 32]| { + hash.reverse(); + hash + }; + + // The witness tree: the coinbase's wtxid, zero by definition, then one + // more. Its root, in display order, is the ABI argument, and the + // coinbase commits to it in wire order. + let other_wtxid: [u8; 32] = std::array::from_fn(|i| 0x40 + i as u8); + let witness_root = combine_left_right(&[0; 32], &other_wtxid); + let reserved = [0u8; 32]; + let commitment = + sha256d::Hash::hash(&[reversed(witness_root), reserved].concat()).to_byte_array(); + let mut commitment_script = vec![0x6a, 0x24, 0xaa, 0x21, 0xa9, 0xed]; + commitment_script.extend_from_slice(&commitment); + let coinbase = bitcoin::Transaction { + version: bitcoin::transaction::Version::TWO, + lock_time: LockTime::ZERO, + input: vec![TxIn { + previous_output: OutPoint::null(), + script_sig: ScriptBuf::from_bytes(vec![0x01, 0x01]), + sequence: Sequence::MAX, + witness: Witness::new(), + }], + output: vec![ + TxOut { + value: Amount::from_sat(5_000_000_000), + script_pubkey: ScriptBuf::from_bytes(vec![0x51]), + }, + TxOut { + value: Amount::ZERO, + script_pubkey: ScriptBuf::from_bytes(commitment_script), + }, + ], + }; + // Without its witness, so that its hash is its txid. + let coinbase_bytes = btc_serialize(&coinbase); + + // The block's tree, in wire order. + let coinbase_txid = sha256d::Hash::hash(&coinbase_bytes).to_byte_array(); + let other_txid: [u8; 32] = std::array::from_fn(|i| 0x80 + i as u8); + let block_root = sha256d::Hash::hash(&[coinbase_txid, other_txid].concat()).to_byte_array(); + let genesis = bitcoin::constants::genesis_block(bitcoin::Network::Regtest) + .header + .block_hash(); + let header = (0u32..10_000) + .map(|nonce| bitcoin::block::Header { + version: Version::from_consensus(1), + prev_blockhash: genesis, + merkle_root: TxMerkleNode::from_byte_array(block_root), + time: 1_700_000_000, + bits: CompactTarget::from_consensus(0x207f_ffff), + nonce, + }) + .find(crate::bridge::btc_store::check_proof_of_work) + .expect("regtest PoW is trivial"); + let block_hash = crate::bridge::btc_chain::bitcoin_hash_to_b256(&header.block_hash()).0; + + // The tree that proves the coinbase: two transactions, the first + // matched, flags 1 (the root), 1 (the coinbase), 0 (the other). + let mut pmt = 2u32.to_le_bytes().to_vec(); + pmt.push(2); + pmt.extend_from_slice(&coinbase_txid); + pmt.extend_from_slice(&other_txid); + pmt.extend_from_slice(&[1, 0b011]); + + let word = |n: usize| U256::from(n).to_be_bytes::<32>(); + let padded = |bytes: &[u8]| { + let mut out = word(bytes.len()).to_vec(); + out.extend_from_slice(bytes); + out.resize(32 + bytes.len().div_ceil(32) * 32, 0); + out + }; + let mut receive_header = crate::bridge::compute_selector("receiveHeader(bytes)").to_vec(); + receive_header.extend_from_slice(&word(32)); + receive_header.extend_from_slice(&padded(&btc_serialize(&header))); + let mut register = crate::bridge::compute_selector( + "registerBtcCoinbaseTransaction(bytes,bytes32,bytes,bytes32,bytes32)", + ) + .to_vec(); + let tx_part = padded(&coinbase_bytes); + register.extend_from_slice(&word(160)); + register.extend_from_slice(&block_hash); + register.extend_from_slice(&word(160 + tx_part.len())); + register.extend_from_slice(&witness_root); + register.extend_from_slice(&reserved); + register.extend_from_slice(&tx_part); + register.extend_from_slice(&padded(&pmt)); + + let bridge_tx = |nonce: u64, input: Vec| rustock_core::Transaction { + nonce, + gas_price: U256::from(0), + gas_limit: U256::from(2_000_000), + to: Bytes::copy_from_slice(crate::precompiles::BRIDGE_ADDR.as_slice()), + value: U256::ZERO, + input: Bytes::from(input), + v: 0, + r: U256::ZERO, + s: U256::ZERO, + cached_rlp: None, + }; + let store = Arc::new(MemoryTrieStore::new()); + let sender = Address::repeat_byte(0xC1); + let one_rbtc = U256::from(10u64).pow(U256::from(18)); + let root = put_account(&TrieNode::empty(), store.as_ref(), &sender, 0, one_rbtc); + let block_store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store) + .with_bridge_constants(crate::bridge::constants::BridgeConstants::regtest()); + let txs = [ + (bridge_tx(0, receive_header), sender), + (bridge_tx(1, register), sender), + ]; + let built = executor + .execute_block(&dummy_header(BLOCK), &txs, &root, store.clone()) + .expect("header and coinbase block"); + assert!(built.tx_results.iter().all(|r| r.success)); + let root = crate::state::apply_state_changes( + &root, + store.as_ref(), + &built.state_changes, + &built.markers, + ); + + // The confirmations, or None where the call throws. + let confirmations = |tx_hash: [u8; 32], path: usize, branch: &[[u8; 32]]| { + let mut input = crate::bridge::compute_selector( + "getBtcTransactionConfirmations(bytes32,bytes32,uint256,bytes32[])", + ) + .to_vec(); + input.extend_from_slice(&tx_hash); + input.extend_from_slice(&block_hash); + input.extend_from_slice(&word(path)); + input.extend_from_slice(&word(128)); + input.extend_from_slice(&word(branch.len())); + branch.iter().for_each(|hash| input.extend_from_slice(hash)); + let built = executor + .execute_block( + &dummy_header(BLOCK), + &[(bridge_tx(2, input), sender)], + &root, + store.clone(), + ) + .expect("query block"); + let result = &built.tx_results[0]; + assert!( + result.success, + "a top-level Bridge call's receipt succeeds even when it throws" + ); + let answer: Option<[u8; 32]> = result.output.as_slice().try_into().ok(); + assert!(answer.is_some() || result.output.is_empty()); + answer.map(alloy_primitives::I256::from_be_bytes::<32>) + }; + let code = |n: i64| Some(alloy_primitives::I256::try_from(n).unwrap()); + + // The second transaction, by the block's tree. + assert_eq!( + confirmations(reversed(other_txid), 1, &[reversed(coinbase_txid)]), + code(1) + ); + assert_eq!( + confirmations(other_txid, 1, &[coinbase_txid]), + code(-5), + "wire order proves nothing" + ); + // By the witness tree, where rustock got -5. + assert_eq!(confirmations(other_wtxid, 1, &[[0; 32]]), code(1)); + assert_eq!( + confirmations(reversed(other_wtxid), 1, &[[0; 32]]), + code(-5) + ); + + // Nodes that read, left then right, as a 64-byte transaction: one + // input, an output index of 0, scripts of two bytes each, one output + // of one satoshi. + let transaction = [ + &[1u8, 0, 0, 0][..], + &[1], + &[0x11; 32], + &[0; 4], + &[2, 0xaa, 0xbb], + &[0xff; 4], + &[1], + &1i64.to_le_bytes(), + &[2, 0xcc, 0xdd], + &[0; 4], + ] + .concat(); + let left: [u8; 32] = transaction[..32].try_into().unwrap(); + let right: [u8; 32] = transaction[32..].try_into().unwrap(); + assert_eq!( + confirmations(left, 0, &[right]), + None, + "rskj's combineLeftRight throws" + ); + assert_eq!( + confirmations(right, 0, &[left]), + code(-5), + "the other way round they are no transaction" + ); + } + /// Audit findings FRCR-78 and FRCR-678. /// /// `getEstimatedFeesForNextPegOutEvent` is **transaction-callable** — rskj From 0ca5e2ee368c3d32683d70aa55e414f56c9f5913 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Fri, 6 Nov 2026 15:38:46 -0800 Subject: [PATCH 023/264] Hold merged mining below orchid to rskj's GenesisMerkleProofValidator Below orchid, rskj's ProofOfWorkRule checks a merge-mined block as it does above it but for two things, and rustock skipped the check there. The Bitcoin-header field. From RSKIP98 it must be exactly a Bitcoin header. Below it, and at block 0 always, BtcHeaderSizeRule passes any size and bitcoinj-thin's makeBlock reads the field: a header, then, if more bytes follow, the block's transactions, throwing where they do not parse. rustock_core::btc_block reads it as bitcoinj does, with Java's int and long counts, a negative list size refused as ArrayList refuses it, and the 32 MiB cap on a read. Only whether it throws matters, since the proof of work is the first 80 bytes' either way. The merkle proof. Below RSKIP92 it is a bitcoinj partial merkle tree, which GenesisMerkleProofValidator requires to be exactly the size its counts declare, to walk to the Bitcoin header's merkle root, and to match the coinbase, both compared in display order. It is the bitcoinj-thin tree in core, the one the Bridge reads. Every mainnet header to #9,392,253 passes. Of the 728,999 below orchid, 165,328 are fallback-mined, their signatures already checked, and 563,671 merge-mined, their trees checked now; verify_stored_headers counts them. As controls, comparing the header's root in wire order, matching the coinbase in wire order, and refusing a field that holds the header alone each fail exactly those 563,671, so every one of them carries a bare header. The tests take mainnet #728,999, the last block below orchid, as stored, and bitcoinj's reading of the Bitcoin genesis block and of each count it reads as Java does; all 25 mutants of the new code are caught. The docs said the rule skipped blocks below orchid. They now say what it checks there, and the map names the files. --- crates/cli/examples/verify_stored_headers.rs | 11 +- crates/core/src/btc_block.rs | 320 +++++++++++++++++++ crates/core/src/lib.rs | 1 + crates/core/src/partial_merkle_tree.rs | 2 +- crates/core/src/validation/merged_mining.rs | 53 ++- crates/core/src/validation/mod.rs | 3 + crates/core/src/validation/tests.rs | 207 ++++++++++++ docs/design-divergences.md | 41 ++- docs/rskj-to-rustock-map.md | 4 +- 9 files changed, 616 insertions(+), 26 deletions(-) create mode 100644 crates/core/src/btc_block.rs diff --git a/crates/cli/examples/verify_stored_headers.rs b/crates/cli/examples/verify_stored_headers.rs index 0b53074..1f8ed3f 100644 --- a/crates/cli/examples/verify_stored_headers.rs +++ b/crates/cli/examples/verify_stored_headers.rs @@ -159,6 +159,9 @@ fn main() -> anyhow::Result<()> { // Headers below orchid that answer for their work with a fallback-mining // signature rather than a Bitcoin header. let fallback = AtomicU64::new(0); + // And those below orchid that answer for it with a Bitcoin header, whose + // coinbase a bitcoinj partial merkle tree proves. + let trees = AtomicU64::new(0); let started = Instant::now(); std::thread::scope(|scope| -> anyhow::Result<()> { @@ -174,7 +177,7 @@ fn main() -> anyhow::Result<()> { let (store, verifier, config, failures, checked, not_later, orphaned, missing) = ( &store, &verifier, &config, &failures, &checked, ¬_later, &orphaned, &missing, ); - let (fork_checked, fallback) = (&fork_checked, &fallback); + let (fork_checked, fallback, trees) = (&fork_checked, &fallback, &trees); workers.push(scope.spawn(move || -> anyhow::Result<()> { // The highest block from `number` down to this range's bottom // that the store holds, counting the numbers it does not. @@ -204,6 +207,8 @@ fn main() -> anyhow::Result<()> { let parent = store.header(header.parent_hash)?; if merged_mining::is_fallback_mined(&header, config) { fallback.fetch_add(1, Ordering::Relaxed); + } else if header.number < config.activation_heights.orchid { + trees.fetch_add(1, Ordering::Relaxed); } if let Err(e) = verifier.verify(&header, parent.as_ref()) { record(failures, header.number, &e); @@ -291,6 +296,10 @@ fn main() -> anyhow::Result<()> { " fallback-mined, their signatures checked: {}", fallback.into_inner() ); + println!( + " merge-mined below orchid, their partial merkle trees checked: {}", + trees.into_inner() + ); let expected = highest - lowest + 1; anyhow::ensure!( checked + missing == expected, diff --git a/crates/core/src/btc_block.rs b/crates/core/src/btc_block.rs new file mode 100644 index 0000000..bd08f34 --- /dev/null +++ b/crates/core/src/btc_block.rs @@ -0,0 +1,320 @@ +//! Whether bitcoinj-thin's `BtcBlock` (`v0.14.4-rsk-20`) makes a Bitcoin +//! block of some bytes, or throws. +//! +//! Below RSKIP98 (orchid), rskj's `ProofOfWorkRule` takes a merge-mined +//! block's Bitcoin-header field at any length and makes a bitcoinj block of +//! it: the 80 bytes of a header and, if anything follows, the block's +//! transactions. Nothing after the header is used, but where bitcoinj cannot +//! read it, it throws, and the block is refused. Every merge-mined header on +//! mainnet below orchid is the 80 bytes alone; this decides the rest as +//! bitcoinj does. +//! +//! bitcoinj reads each input and output twice: once into its own message, and +//! once to step the transaction past it, in Java `long`s narrowed into an +//! `int` cursor. Wherever the first reading returns, the second lands where it +//! stopped, since each length it narrows was read whole the first time. So +//! only the first is followed here. + +use crate::partial_merkle_tree::var_int; + +/// bitcoinj `BtcBlock.HEADER_SIZE`. +pub const HEADER_SIZE: usize = 80; +/// bitcoinj `Message.MAX_SIZE`: the longest array `readBytes` makes, 32 MiB. +const MAX_SIZE: i32 = 0x0200_0000; +/// bitcoinj `TransactionOutPoint.MESSAGE_LENGTH`: a hash and an index. +const OUTPOINT_LENGTH: usize = 36; +/// bitcoinj `Utils.MAX_INITIAL_ARRAY_LENGTH`. +const MAX_INITIAL_ARRAY_LENGTH: i32 = 20; + +/// Whether `new BtcBlock(params, payload, 0, serializer, payload.length)` +/// returns rather than throws. +pub fn bitcoinj_reads(payload: &[u8]) -> bool { + Reader { payload, cursor: 0 }.block().is_some() +} + +struct Reader<'a> { + payload: &'a [u8], + cursor: usize, +} + +impl Reader<'_> { + /// `BtcBlock.parse` and `parseTransactions`, which reads none where the + /// header is all there is, and leaves unread whatever follows the last. + fn block(&mut self) -> Option<()> { + // Version, the previous block's hash, the merkle root, time, target + // and nonce. + self.skip(HEADER_SIZE)?; + if self.cursor == self.payload.len() { + return Some(()); + } + let count = self.var_int()? as i32; + list(count)?; + for _ in 0..count { + self.transaction()?; + } + Some(()) + } + + /// `BtcTransaction.parse`: with no inputs, a byte of flags follows, and + /// its lowest bit, the only one known, says there are witnesses. + fn transaction(&mut self) -> Option<()> { + self.skip(4)?; + let mut inputs = self.inputs()?; + let mut flags = 0; + if inputs == 0 { + flags = self.bytes(1)?[0]; + if flags != 0 { + inputs = self.inputs()?; + self.outputs()?; + } + } else { + self.outputs()?; + } + if flags & 1 != 0 { + flags ^= 1; + self.witnesses(inputs)?; + } + if flags != 0 { + return None; + } + // The lock time. + self.skip(4) + } + + /// `readInputs`, each input read by `TransactionInput.parse`: its outpoint, + /// its script and its sequence. How many it read. + fn inputs(&mut self) -> Option { + let count = self.var_int()?; + list(count as i32)?; + let mut read = 0; + // A `long` loop, so a count below zero reads none. + while (read as i64) < count { + self.skip(OUTPOINT_LENGTH)?; + let script = self.var_int()? as i32; + self.bytes(script)?; + self.skip(4)?; + read += 1; + } + Some(read) + } + + /// `readOutputs`, each output read by `TransactionOutput.parse`: its value + /// and its script. + fn outputs(&mut self) -> Option<()> { + let count = self.var_int()?; + list(count as i32)?; + let mut read = 0; + while read < count { + self.skip(8)?; + let script = self.var_int()? as i32; + self.bytes(script)?; + read += 1; + } + Some(()) + } + + /// `readWitness`: for each input, a count of pushes and the pushes, the + /// count first sizing the list `TransactionWitness` keeps them in. + fn witnesses(&mut self, inputs: usize) -> Option<()> { + for _ in 0..inputs { + let pushes = self.var_int()?; + list(pushes as i32)?; + let mut read = 0; + while read < pushes { + let push = self.var_int()? as i32; + self.bytes(push)?; + read += 1; + } + } + Some(()) + } + + /// `readUint32`, `readInt64` and `readHash`: that many bytes, or a throw. + fn skip(&mut self, n: usize) -> Option<()> { + let end = self + .cursor + .checked_add(n) + .filter(|&end| end <= self.payload.len())?; + self.cursor = end; + Some(()) + } + + /// `readBytes`, which refuses more than 32 MiB or more than is left, and + /// cannot make an array below zero. + fn bytes(&mut self, n: i32) -> Option<&[u8]> { + if !(0..=MAX_SIZE).contains(&n) { + return None; + } + let start = self.cursor; + self.skip(n as usize)?; + Some(&self.payload[start..self.cursor]) + } + + /// `readVarInt`, the value in a Java `long`. + fn var_int(&mut self) -> Option { + let (value, size) = var_int(self.payload, self.cursor)?; + self.cursor += size; + Some(value) + } +} + +/// `new ArrayList<>(Math.min(n, Utils.MAX_INITIAL_ARRAY_LENGTH))`, which +/// throws for a capacity below zero. +fn list(n: i32) -> Option<()> { + (n.min(MAX_INITIAL_ARRAY_LENGTH) >= 0).then_some(()) +} + +#[cfg(test)] +mod tests { + use super::*; + use bitcoin::consensus::serialize; + + /// Bitcoin's genesis block: its header, one transaction, the coinbase. + fn genesis() -> Vec { + serialize(&bitcoin::constants::genesis_block( + bitcoin::Network::Bitcoin, + )) + } + + /// A header, then `count` and the transactions given. + fn block(count: &[u8], transactions: &[&[u8]]) -> Vec { + [&genesis()[..HEADER_SIZE], count, &transactions.concat()].concat() + } + + /// A transaction of one input, its script two bytes, and one output, its + /// script three, the two lengths written as given. + fn transaction(input_length: &[u8], output_length: &[u8]) -> Vec { + [ + &1u32.to_le_bytes()[..], + &[1], + &[0x11; 32], + &[0; 4], + input_length, + &[0x51; 2], + &[0xff; 4], + &[1], + &5_000u64.to_le_bytes(), + output_length, + &[0x52; 3], + &[0; 4], + ] + .concat() + } + + /// A header, its transactions, both whole, and a byte past them. + #[test] + fn bitcoinj_reads_a_block_and_ignores_what_follows() { + let genesis = genesis(); + assert_eq!(genesis.len(), 285); + assert!(bitcoinj_reads(&genesis)); + assert!(bitcoinj_reads(&genesis[..HEADER_SIZE])); + assert!(bitcoinj_reads(&[genesis.clone(), vec![0xee]].concat())); + for cut in (0..HEADER_SIZE).chain(HEADER_SIZE + 1..genesis.len()) { + assert!(!bitcoinj_reads(&genesis[..cut]), "{cut} bytes"); + } + // No transactions after all. + assert!(bitcoinj_reads(&block(&[0], &[]))); + let two = transaction(&[2], &[3]); + assert!(bitcoinj_reads(&block(&[2], &[&two, &two]))); + assert!(!bitcoinj_reads(&block(&[3], &[&two, &two]))); + } + + /// With no inputs, bitcoinj reads a byte of flags: zero for none, one for + /// witnesses after the outputs, anything else refused. + #[test] + fn bitcoinj_reads_witnesses_after_a_flag() { + let version = 2u32.to_le_bytes(); + let lock_time = [0; 4]; + let input = [&[0x22; 36][..], &[0], &[0xff; 4]].concat(); + let output = [&7u64.to_le_bytes()[..], &[1, 0x51]].concat(); + // Two pushes: three bytes, then none. + let witness = [2, 3, 1, 2, 3, 0]; + let segwit = |flags: u8, witness: &[u8]| { + [ + &version[..], + &[0, flags, 1], + &input, + &[1], + &output, + witness, + &lock_time, + ] + .concat() + }; + assert!(bitcoinj_reads(&block(&[1], &[&segwit(1, &witness)]))); + assert!(!bitcoinj_reads(&block(&[1], &[&segwit(3, &witness)]))); + assert!(!bitcoinj_reads(&block(&[1], &[&segwit(2, &witness)]))); + // Flags of zero: no inputs, no outputs. + let bare = [&version[..], &[0, 0], &lock_time].concat(); + assert!(bitcoinj_reads(&block(&[1], &[&bare]))); + // A push of nine bytes, with seven left in all. + assert!(!bitcoinj_reads(&block( + &[1], + &[&segwit(1, &[1, 9, 1, 2, 3])] + ))); + } + + /// bitcoinj casts each count to an `int` to size its list, which throws + /// below zero, and loops on the `long` for inputs and outputs. + #[test] + fn bitcoinj_counts_as_java_does() { + let two = transaction(&[2], &[3]); + // Transactions: 2^31 is below zero as an `int`. + assert!(!bitcoinj_reads(&block(&[0xfe, 0, 0, 0, 0x80], &[&two]))); + // 2^32 + 1 is one, as an `int`. + let wide_one = [0xff, 1, 0, 0, 0, 1, 0, 0, 0]; + assert!(bitcoinj_reads(&block(&wide_one, &[&two]))); + // Inputs: -1 sizes a list below zero; -2^32, a `long` below zero whose + // `int` is zero, reads none, so the flags byte comes next. + let tail = |count: &[u8]| [&1u32.to_le_bytes()[..], count, &[0], &[0; 4]].concat(); + assert!(!bitcoinj_reads(&block(&[1], &[&tail(&[0xff; 9])]))); + let below_zero = [0xff, 0, 0, 0, 0, 0xff, 0xff, 0xff, 0xff]; + assert!(bitcoinj_reads(&block(&[1], &[&tail(&below_zero)]))); + // A script length whose low 32 bits are 2 reads two bytes, and the + // next transaction starts after them. + let wide_two = [0xff, 2, 0, 0, 0, 1, 0, 0, 0]; + let wide = transaction(&wide_two, &[3]); + assert!(bitcoinj_reads(&block(&[2], &[&wide, &two]))); + // And one below zero cannot be read. + let minus_two = [0xfe, 0xfe, 0xff, 0xff, 0xff]; + assert!(!bitcoinj_reads(&block( + &[1], + &[&transaction(&minus_two, &[3])] + ))); + assert!(!bitcoinj_reads(&block( + &[1], + &[&transaction(&[2], &minus_two)] + ))); + // Outputs: -1 sizes a list below zero, where a `long` loop would read + // none. + let input = [&[0x22; 36][..], &[0], &[0xff; 4]].concat(); + let version = 1u32.to_le_bytes(); + let no_outputs = |count: &[u8]| [&version[..], &[1], &input, count, &[0; 4]].concat(); + assert!(bitcoinj_reads(&block(&[1], &[&no_outputs(&[0])]))); + assert!(!bitcoinj_reads(&block(&[1], &[&no_outputs(&[0xff; 9])]))); + // A witness's push count below zero, as an `int`: 2^31, and -1, where + // a `long` loop would read none. + let witnessed = + |pushes: &[u8]| [&version[..], &[0, 1, 1], &input, &[0], pushes, &[0; 4]].concat(); + assert!(bitcoinj_reads(&block(&[1], &[&witnessed(&[0])]))); + assert!(!bitcoinj_reads(&block( + &[1], + &[&witnessed(&[0xfe, 0, 0, 0, 0x80])] + ))); + assert!(!bitcoinj_reads(&block(&[1], &[&witnessed(&[0xff; 9])]))); + } + + /// `readBytes` makes no array over 32 MiB, whatever is there. + #[test] + fn bitcoinj_reads_no_script_over_32_mib() { + let script = |n: u32| { + let length = [&[0xfe][..], &n.to_le_bytes()].concat(); + let output = [&5u64.to_le_bytes()[..], &length, &vec![0u8; n as usize]].concat(); + let input = [&[0x22; 36][..], &[0], &[0xff; 4]].concat(); + let version = 1u32.to_le_bytes(); + [&version[..], &[1], &input, &[1], &output, &[0; 4]].concat() + }; + assert!(bitcoinj_reads(&block(&[1], &[&script(0x0200_0000)]))); + assert!(!bitcoinj_reads(&block(&[1], &[&script(0x0200_0001)]))); + } +} diff --git a/crates/core/src/lib.rs b/crates/core/src/lib.rs index 9b15d6d..4de7e7e 100644 --- a/crates/core/src/lib.rs +++ b/crates/core/src/lib.rs @@ -9,6 +9,7 @@ pub mod rlp_compat; pub mod rskj_genesis; pub mod rskj_rlp; pub mod partial_merkle_tree; +pub mod btc_block; pub use types::block::Block; pub use types::header::Header; diff --git a/crates/core/src/partial_merkle_tree.rs b/crates/core/src/partial_merkle_tree.rs index e78558a..1af0efb 100644 --- a/crates/core/src/partial_merkle_tree.rs +++ b/crates/core/src/partial_merkle_tree.rs @@ -248,7 +248,7 @@ fn declared_size(pmt: &[u8]) -> Option { /// bitcoinj `new VarInt(buf, offset)`: the value in the Java `long` it is /// read into, where a nine-byte one can be negative, and how many bytes it /// took. `None` where it runs past the end, where bitcoinj throws. -fn var_int(buf: &[u8], offset: usize) -> Option<(i64, usize)> { +pub(crate) fn var_int(buf: &[u8], offset: usize) -> Option<(i64, usize)> { let first = *buf.get(offset)?; let rest = |n: usize| buf.get(offset + 1..offset + 1 + n); Some(match first { diff --git a/crates/core/src/validation/merged_mining.rs b/crates/core/src/validation/merged_mining.rs index c9dbae5..7232178 100644 --- a/crates/core/src/validation/merged_mining.rs +++ b/crates/core/src/validation/merged_mining.rs @@ -1,5 +1,7 @@ use super::fork_detection::REQUIRED_BLOCKS; use super::{HeaderValidator, ValidationError}; +use crate::btc_block; +use crate::partial_merkle_tree::{self, PartialMerkleTree}; use crate::types::header::Header; use crate::config::ChainConfig; @@ -27,7 +29,7 @@ pub const MAX_RSK_TAG_POSITION_IN_TAIL: usize = 64; pub const MAX_MERGED_MINING_MERKLE_PROOF_LENGTH: usize = 960; /// The length of a Bitcoin block header, and from RSKIP98 (orchid) the only -/// length rskj accepts for the field that carries one: rskj +/// length rskj accepts for the field that carries one, block 0 excepted: rskj /// `RskMiningConstants.BTC_HEADER_SIZE`, enforced by `BtcHeaderSizeRule`. pub const BTC_HEADER_SIZE: usize = 80; @@ -253,23 +255,27 @@ impl HeaderValidator for MergedMiningRule { number: header.number, }); } - // Merged mining below RSKIP92, orchid too, proves its coinbase by a - // bitcoinj partial Merkle tree, which this does not check yet. - if header.number < self.config.activation_heights.orchid { - return Ok(()); - } // rskj `ProofOfWorkRule` with `miner.server.skipPowValidation`: the // whole rule passes, merkle proof and coinbase included. if self.config.skip_pow_validation { return Ok(()); } + // RSKIP92 and RSKIP98 arrive together, at orchid. Below them the + // Bitcoin-header field may carry its block's transactions after the + // header, and the merkle proof is a bitcoinj partial merkle tree. + let orchid = header.number >= self.config.activation_heights.orchid; let btc_header_bytes = header.bitcoin_merged_mining_header.as_ref() .ok_or(ValidationError::BitcoinHeaderDecodeError)?; - // rskj `BtcHeaderSizeRule`, from RSKIP98, which is orchid like the - // rest of this rule: exactly a Bitcoin header. The decoder below - // reads the first 80 bytes and would let anything after them ride. - if btc_header_bytes.len() != BTC_HEADER_SIZE { + // rskj `BtcHeaderSizeRule`: from RSKIP98, exactly a Bitcoin header, + // block 0 excepted. Otherwise any bytes bitcoinj makes a block of, + // whose header is their first 80. The decoder below reads those 80 alone. + let readable = if orchid && header.number != 0 { + btc_header_bytes.len() == BTC_HEADER_SIZE + } else { + btc_block::bitcoinj_reads(btc_header_bytes) + }; + if !readable { return Err(ValidationError::BitcoinHeaderDecodeError); } let mut reader = &btc_header_bytes[..]; @@ -356,6 +362,9 @@ impl HeaderValidator for MergedMiningRule { let merkle_proof_bytes = header.bitcoin_merged_mining_merkle_proof.as_ref() .ok_or(ValidationError::BitcoinMerkleProofDecodeError)?; + if !orchid { + return genesis_merkle_proof(merkle_proof_bytes, &btc_header, &coinbase_hash); + } // RSKIP180 (iris300): rskj `Rskip92MerkleProofValidator` rejects a // merged-mining merkle proof longer than @@ -389,6 +398,30 @@ impl HeaderValidator for MergedMiningRule { } } +/// rskj `GenesisMerkleProofValidator`, merged mining's proof below RSKIP92: a +/// bitcoinj partial merkle tree of exactly the size its counts declare, which +/// walks to the Bitcoin header's merkle root and matches the coinbase, both +/// compared in bitcoinj's display order. +fn genesis_merkle_proof( + proof: &[u8], + btc_header: &bitcoin::block::Header, + coinbase_hash: &[u8; 32], +) -> Result<(), ValidationError> { + use bitcoin::hashes::Hash; + if !partial_merkle_tree::has_expected_size(proof) { + return Err(ValidationError::BitcoinMerkleProofDecodeError); + } + let (matched, root) = PartialMerkleTree::parse(proof) + .and_then(|tree| tree.txn_hashes_and_merkle_root()) + .map_err(ValidationError::PartialMerkleTreeInvalid)?; + let mut header_root = btc_header.merkle_root.to_byte_array(); + header_root.reverse(); + if root != header_root || !matched.contains(coinbase_hash) { + return Err(ValidationError::BitcoinMerkleProofInvalid); + } + Ok(()) +} + /// The 12 fork-detection bytes a header commits to, read out of its /// merged-mining coinbase. /// diff --git a/crates/core/src/validation/mod.rs b/crates/core/src/validation/mod.rs index 92deac0..eed6edb 100644 --- a/crates/core/src/validation/mod.rs +++ b/crates/core/src/validation/mod.rs @@ -67,6 +67,9 @@ pub enum ValidationError { #[error("Two nodes of the merged-mining merkle proof parse as a 64-byte Bitcoin transaction")] MerkleNodesFormTransaction, + #[error("Merged-mining partial merkle tree refused: {0}")] + PartialMerkleTreeInvalid(crate::partial_merkle_tree::PmtError), + #[error("UMM root is {len} bytes, must be empty or 20")] UmmRootInvalid { len: usize }, diff --git a/crates/core/src/validation/tests.rs b/crates/core/src/validation/tests.rs index 49255ab..930b865 100644 --- a/crates/core/src/validation/tests.rs +++ b/crates/core/src/validation/tests.rs @@ -588,6 +588,178 @@ fn a_fallback_signature_covers_its_header() { ); } +/// Mainnet #728,999, the last block below orchid, as rskj stores it. Below +/// RSKIP92 a block's hash is Keccak-256 of the whole of it, so RSKj's hash of +/// the block vouches for every byte. +const MAINNET_728999: &str = + "f90484a092beb2d8b8dce8fa2ad47cdb50966185c78b31176cc9305b18e562e569b673fda010b55649b9283184efeed4\ + ba62df75cc9cb0e7829c79ac4f4c36d76b91dcb09f9432dfc7a84f24b10a5dded1d8b24f48b96ab77373a0622343f407\ + 291f8eafc1e19bebeeb0c97e70a93ba6d96fe00dfe90fea8bc2055a0bb597f3ba349abf127973d075cfd1a8b70088da2\ + 3633c69c521ddada4f4d9f61a09d84e10a4187f8b1ff7d2f5bc3287b037056d70f7e032d7c822bee2835f1d766b90100\ + 000000000000000000000000000004080000000000000040000200000000000000100000000000000000000000000000\ + 0000000400000000200000000000800100000800000000100000200000080000a0000000000000000000020000000000\ + 40000000000000000000000000800000200020000800000000000000000000020000000000000001008a004000402000\ + 000010000000200008000020005000000000000100001000200a00002000000000002000000000000010000000000000\ + 000000010000000000008000000000000000200011000004002000001008000000040000000000000080040400000000\ + 008200000000000000000084000000008909e5a16d6b4451573d830b1fa78367c28080845b932beb8080840387ee4002\ + b85000000020ec507847794a1088d577e8f664e3a566f42c6aea8f4723000000000000000000a0c9c1038cf9f5d980cc\ + 32408a6ff544638eacfac5dd68d352d7438364bb3878f12b935ba119281713ddc006b901aad20800000d737de9538e4c\ + 6d9668fea42548309ac66597dc7c727e0fe3fe4cf03c500b69415a62de517b3b39ae2b8ecbadb65f03c7898d17bdb29a\ + 75fcac327489509e4377c6e359c5f55d76b9040ee953afa09fbaa9f211501e01af40ea5064413faec66bb2d1f2e541a9\ + 55760b378aefe63a761151259b5bff356228e0300a3e72b70da8bf48b2ae696dae70321f7ccaf84e3c66882302c9f291\ + 7b8446ced622059c5a294842f441e78b33370cb385c6cbdb2862533f9a6b0dfe5d37282316bf5fce5650b1ef0cab4b16\ + 4f7b14a031b84ba25f57e74e924c070414f33276fa17226b110858a54ebb9c2bd8ad61c5fbd65f7be61d8392dcc2a24a\ + 48775f53ce6ab765ff2017bb571e1f5e840e2ac3be1a9df2c69c71b6dfd701670ea605b4de30209e39b39682bf26c376\ + 66e89853be6aa1dfd10b67f228a6598ddc23d4765d1f70f3d32f69a81560269a1d49883e6871d7a9b11f1f01c53503e6\ + e1c00b20a07c5e9d819c00b1c9ad08297129669bae66881911c75c964e10d7119f9ba78b675d4c1a0f7403237186beab\ + 82f01c05454172e10af3448d9ce9213baa9c58c93634ab598ff704ff1f0000b8a600000000000000805998b42c25f5e9\ + d5948bfdb8806ad419d3ae2203f69ab4c0c643ecd39f6161d97c154ed1dc59609e3d26abb2df2ea3d587cd8c4188ac00\ + 000000000000002c6a4c2952534b424c4f434b3ad3e5bd1cd73aece37e3be2c9ea36bcfd663f30599838fdeaff75e4bf\ + 54bf9d110000000000000000266a24aa21a9ed5c478fd38b405c680d17c3b281a98baa67e9191c16aac595bb7765aef4\ + 82513400000000"; + +/// Mainnet #728,999, its hash checked against RSKj's. +fn mainnet_728999() -> Header { + let rlp = hex::decode(MAINNET_728999).unwrap(); + let rskj: B256 = "0x450f29ed02e088c1f7a37ed1d53ff1246857ab04e6f7032d0a299005a0c5e021" + .parse() + .unwrap(); + assert_eq!(alloy_primitives::keccak256(&rlp), rskj); + let header = decoded(&rlp); + assert_eq!((header.number, header.hash()), (728_999, rskj)); + header +} + +/// rskj `GenesisMerkleProofValidator`: below orchid a merge-mined block's +/// coinbase is proved by a bitcoinj partial merkle tree, of the size its +/// counts declare, that walks to the Bitcoin header's root and matches the +/// coinbase. Mainnet's last such block passes, and fails with its tree or +/// its coinbase changed. +#[test] +fn below_orchid_a_partial_merkle_tree_proves_the_coinbase() { + use crate::config::{ActivationHeights, ChainConfig}; + use crate::partial_merkle_tree::{PartialMerkleTree, PmtError}; + + let rule = MergedMiningRule { + config: Arc::new(ChainConfig::mainnet()), + }; + let header = mainnet_728999(); + assert_eq!(rule.validate(&header), Ok(())); + + let proof = header + .bitcoin_merged_mining_merkle_proof + .clone() + .unwrap() + .to_vec(); + // The tree's first hash is the coinbase's, and its only match. + let coinbase = header + .bitcoin_merged_mining_coinbase_transaction + .clone() + .unwrap(); + let coinbase_hash = merged_mining::compute_coinbase_hash(&coinbase); + let tree = PartialMerkleTree::parse(&proof).unwrap(); + assert_eq!(tree.hashes()[0], coinbase_hash); + assert_eq!( + tree.txn_hashes_and_merkle_root().unwrap().0, + [coinbase_hash] + ); + let with_proof = |proof: &[u8]| { + let mut changed = header.clone(); + changed.bitcoin_merged_mining_merkle_proof = Some(Bytes::copy_from_slice(proof)); + rule.validate(&changed) + }; + // A bit of the hash beside the coinbase's: the coinbase still matches, + // but the tree walks to another root. + let mut other = proof.clone(); + other[5 + 32] ^= 1; + assert_eq!( + with_proof(&other), + Err(ValidationError::BitcoinMerkleProofInvalid) + ); + // A byte more or fewer than its counts declare. + let longer = [&proof[..], &[0]].concat(); + assert_eq!( + with_proof(&longer), + Err(ValidationError::BitcoinMerkleProofDecodeError) + ); + let shorter = &proof[..proof.len() - 1]; + assert_eq!( + with_proof(shorter), + Err(ValidationError::BitcoinMerkleProofDecodeError) + ); + // No flag set: the first hash is the root, and the rest go unused. + let flags = tree.matched_child_bits().len(); + let mut unflagged = proof.clone(); + let at = unflagged.len() - flags; + unflagged[at..].fill(0); + assert_eq!( + with_proof(&unflagged), + Err(ValidationError::PartialMerkleTreeInvalid( + PmtError::UnusedData + )) + ); + + // A bit of the coinbase's midstate: it is no longer what the tree matches. + let mut coinbase = header + .bitcoin_merged_mining_coinbase_transaction + .clone() + .unwrap() + .to_vec(); + coinbase[20] ^= 1; + let mut changed = header.clone(); + changed.bitcoin_merged_mining_coinbase_transaction = Some(Bytes::from(coinbase)); + assert_eq!( + rule.validate(&changed), + Err(ValidationError::BitcoinMerkleProofInvalid) + ); + + // From orchid the proof is RSKIP92's, a run of 32-byte hashes, which this + // tree is not. + let at_orchid = MergedMiningRule { + config: Arc::new(ChainConfig { + activation_heights: ActivationHeights { + orchid: 728_999, + ..ActivationHeights::mainnet() + }, + ..ChainConfig::mainnet() + }), + }; + assert_eq!( + at_orchid.validate(&header), + Err(ValidationError::BitcoinMerkleProofDecodeError) + ); +} + +/// Below RSKIP98 rskj makes a bitcoinj block of the Bitcoin-header field, so +/// the block's transactions may follow the header; what bitcoinj cannot read +/// refuses the block. +#[test] +fn below_orchid_the_bitcoin_header_may_carry_its_blocks_transactions() { + let rule = MergedMiningRule { + config: Arc::new(crate::config::ChainConfig::mainnet()), + }; + let header = mainnet_728999(); + let field = header + .bitcoin_merged_mining_header + .clone() + .unwrap() + .to_vec(); + assert_eq!(field.len(), 80); + let with_field = |field: Vec| { + let mut changed = header.clone(); + changed.bitcoin_merged_mining_header = Some(Bytes::from(field)); + rule.validate(&changed) + }; + let genesis = bitcoin::constants::genesis_block(bitcoin::Network::Bitcoin); + let coinbase = bitcoin::consensus::serialize(&genesis.txdata[0]); + assert_eq!(with_field([&field[..], &[1], &coinbase].concat()), Ok(())); + assert_eq!(with_field([&field[..], &[0]].concat()), Ok(())); + // One transaction promised, none there; and a header cut short. + let refused = Err(ValidationError::BitcoinHeaderDecodeError); + assert_eq!(with_field([&field[..], &[1]].concat()), refused); + assert_eq!(with_field(field[..79].to_vec()), refused); +} + /// rskj's `isFallbackMiningPossibleAndBlockSigned`: below orchid, with no /// coinbase and no proof, an empty one counting as none, at no more than the /// network's fallback-mining difficulty: 14 x 10^15 on mainnet and testnet, @@ -1189,6 +1361,41 @@ fn merged_mining_wants_exactly_a_bitcoin_header() { assert_eq!(rule.validate(&header), Ok(())); } +/// rskj `BtcHeaderSizeRule` passes block 0 at any size, RSKIP98 or not, and +/// leaves its field to bitcoinj's `makeBlock`, as below orchid. On regtest, +/// where orchid is at genesis, block 0's field may carry its block's +/// transactions after the header and block 1's may not. +#[test] +fn merged_mining_reads_block_zero_as_below_orchid() { + let rule = MergedMiningRule { + config: Arc::new(crate::config::ChainConfig::regtest()), + }; + for number in [0, 1] { + let mut header = create_dummy_header(number, 1000, B256::ZERO); + header.difficulty = U256::from(1); + let (btc_h, btc_cb, btc_m) = build_mm_proof(&header, 0x207fffff, None); + header.bitcoin_merged_mining_coinbase_transaction = Some(btc_cb); + header.bitcoin_merged_mining_merkle_proof = Some(btc_m); + + // A count of no transactions: a block bitcoinj reads. + header.bitcoin_merged_mining_header = Some(Bytes::from([&btc_h[..], &[0]].concat())); + let size_rule = if number == 0 { + Ok(()) + } else { + Err(ValidationError::BitcoinHeaderDecodeError) + }; + assert_eq!(rule.validate(&header), size_rule, "block {number}"); + + // One transaction declared and none there: bitcoinj throws. + header.bitcoin_merged_mining_header = Some(Bytes::from([&btc_h[..], &[1]].concat())); + assert_eq!( + rule.validate(&header), + Err(ValidationError::BitcoinHeaderDecodeError), + "block {number}" + ); + } +} + /// A view of the chain that holds nothing. struct NoChain; diff --git a/docs/design-divergences.md b/docs/design-divergences.md index e920cd6..e63eade 100644 --- a/docs/design-divergences.md +++ b/docs/design-divergences.md @@ -24,19 +24,36 @@ during sync. | Hardfork | Block | Relevant RSKIPs | |----------|-------|-----------------| -| Orchid | 729,000 | RSKIP92 (merged mining PoW), RSKIP97 (no 10-min reset), RSKIP98 (no fallback mining) | +| Orchid | 729,000 | RSKIP92 (merged-mining merkle proof), RSKIP97 (no 10-min reset), RSKIP98 (no fallback mining; a Bitcoin header of exactly 80 bytes) | | Papyrus200 | 2,392,700 | RSKIP156 (difficulty divisor 50 → 400) | ### Merged mining (RSKIP92/98) -Before Orchid, RSK allowed "fallback mining" without proper Bitcoin merged -mining fields. The `MergedMiningRule` in `crates/core/src/validation/merged_mining.rs` -skips validation for blocks below `activation_heights.orchid`. - -After Orchid, the rule validates: -1. Bitcoin header PoW against RSK difficulty target -2. Merkle proof linking coinbase to Bitcoin header -3. RSK tag (`RSKBLOCK:` + hash) in coinbase outputs +`MergedMiningRule` in `crates/core/src/validation/merged_mining.rs` follows +rskj's `ProofOfWorkRule`, and checks every block, below orchid too. + +Below orchid a block may be fallback-mined: no coinbase, no merkle proof, a +difficulty no greater than the network's fallback-mining difficulty, and in +the Bitcoin-header field a signature of the block's merged-mining hash by +rskj's fallback-mining key for an even or an odd height, checked as rskj's +`validFallbackBlockSignature` checks it. RSKIP98 ended fallback mining. + +Every other block is merge-mined, and the rule checks: +1. The Bitcoin header: from orchid exactly 80 bytes (`BtcHeaderSizeRule`). + Below orchid, and at block 0, any bytes bitcoinj-thin reads as a block, + whose header is their first 80 (`crates/core/src/btc_block.rs`). +2. Its proof of work against the RSK target, 2^256 / max(difficulty, 3). +3. The `RSKBLOCK:` tag and the block's merged-mining hash in the coinbase, + where rskj requires them, and a coinbase longer than 64 bytes. +4. The merkle proof that puts the coinbase in the Bitcoin block. From orchid + (RSKIP92) it is the coinbase's branch of sibling hashes, at most 960 bytes + from iris300 (RSKIP180). Below orchid it is a bitcoinj-thin partial merkle + tree, of exactly the size its counts declare, that walks to the header's + merkle root and matches the coinbase (rskj's `GenesisMerkleProofValidator`; + `crates/core/src/partial_merkle_tree.rs`, shared with the Bridge). + +On mainnet all 728,999 blocks below orchid pass: 165,328 fallback-mined and +563,671 merge-mined. ### Difficulty calculation @@ -60,9 +77,9 @@ three hardfork-gated behaviors matching `rskj/.../core/DifficultyCalculator.java ### Rust implementation `ChainConfig` in `crates/core/src/config.rs` includes an `ActivationHeights` -struct with `orchid` and `papyrus200` fields. Both `MergedMiningRule` and -`DifficultyRule` check `header.number` against these heights before applying -hardfork-specific logic. +struct with the heights these rules read, `orchid`, `wasabi100`, `papyrus200` +and `iris300` among them. Both `MergedMiningRule` and `DifficultyRule` check +`header.number` against these heights before applying hardfork-specific logic. --- diff --git a/docs/rskj-to-rustock-map.md b/docs/rskj-to-rustock-map.md index 942113e..00f0fb1 100644 --- a/docs/rskj-to-rustock-map.md +++ b/docs/rskj-to-rustock-map.md @@ -62,7 +62,7 @@ rustock `crates/execution/src/bridge/` (~22,700 lines). | Peg-out queue and building | `ReleaseRequestQueue`, `PegoutsWaitingForConfirmations`, `ReleaseTransactionBuilder` | `bridge/release_tx.rs` | | Peg-in classification | `peg/pegin/*`, `PegUtils`, `btcLockSender/*` | `bridge/peg.rs`, `rskj_sender_compat.rs` | | Peg-in instructions (flyover) | `peg/pegininstructions/*` | `bridge/pegin_instructions.rs` | -| Merkle proofs | `PartialMerkleTreeFormatUtils`, plus `co.rsk.bitcoinj.core.PartialMerkleTree` from the **bitcoinj-thin** dependency | `bridge/pmt.rs` | +| Merkle proofs | `PartialMerkleTreeFormatUtils`, `MerkleBranch`, plus `co.rsk.bitcoinj.core.PartialMerkleTree` from the **bitcoinj-thin** dependency | `bridge/pmt.rs`, over `crates/core/src/partial_merkle_tree.rs` | | Whitelist / locking cap / fees | `peg/whitelist/*`, `peg/lockingcap/*`, `peg/feeperkb/*` | `bridge/governance.rs` | | Voting | `peg/vote/*` (`ABICallSpec`, `ABICallElection`) | `bridge/vote.rs` | | Local-call getters | `BridgeSupport` getters, `BridgeState` | `bridge/getters.rs` | @@ -93,7 +93,7 @@ as functions. | Body rules | `BlockTxsValidationRule`, `BlockTxsMaxGasPriceRule`, `BlockRootValidationRule` | `crates/core/src/validation/block_rules.rs` | | Uncles | `BlockUnclesValidationRule`, `BlockUnclesHashValidationRule`, `FamilyUtils` | `crates/core/src/validation/uncles.rs` | | Difficulty | `co.rsk.core.DifficultyCalculator` | `crates/core/src/validation/difficulty.rs` | -| Merged mining | `ProofOfWorkRule`, `BtcHeaderSizeRule` | `crates/core/src/validation/merged_mining.rs` | +| Merged mining | `ProofOfWorkRule`, `BtcHeaderSizeRule`, `Rskip92MerkleProofValidator`, `GenesisMerkleProofValidator`, plus bitcoinj-thin's `BtcBlock` and `PartialMerkleTree` | `crates/core/src/validation/merged_mining.rs`, `crates/core/src/btc_block.rs`, `crates/core/src/partial_merkle_tree.rs` | | Fork detection | `ForkDetectionDataRule`, `ConsensusValidationMainchainView` | `crates/core/src/validation/fork_detection.rs` | ### Networking From 6545eabce130c1d25502ac463fb735c6d10b26ec Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Sun, 8 Nov 2026 08:44:31 -0800 Subject: [PATCH 024/264] Hash a versioned header from the network as rskj does decode_with_hash cached the keccak of a header's bytes as sent. rskj hashes a compressed form instead, and for a versioned (RSKIP-351) header that form is not the wire form: the wire carries the literal bloom in slot 6 and the version, edges and base event after ummRoot, while the hashed form leaves those three out and puts a commitment to all four in slot 6. Every testnet header from reed810 (#7,139,600) on got a block hash and a merged-mining hash no other node agrees with, so no header named its parent, and the testnet snapshot sync refused every chain it was offered as not forming one. Both hashes are now built from the items as the peer spelled them, less the version fields and, from RSKIP-92 on, the merged-mining proof and coinbase, with the commitment in slot 6. A version-0 header hashes exactly as before. decode_with_hash_and_activations does the same under explicit activation heights, as decode_with_activations does: RSKIP-144's height decides what a lone item after a version-2 byte is. decode_with_hash keeps reading under the process-wide heights. The tests check the network's decoder on three captured testnet headers (version 1, version 2, and version 2's short form), read under testnet's heights, and on #8,160,760, against rskj's block hash and the merged-mining hash each miner committed to in its coinbase, and on every layout the decoder reads, a version-2 base event without RSKIP-144 included, against the hashes this node derives by encoding. 18 of 19 mutants of the new code are caught; the survivor, rlp_list_items' list-length check, cannot fail at its one call site. --- crates/core/src/types/header.rs | 398 ++++++++++++++++++++++---------- docs/quirks-java-artifacts.md | 42 ++-- 2 files changed, 311 insertions(+), 129 deletions(-) diff --git a/crates/core/src/types/header.rs b/crates/core/src/types/header.rs index 4557b05..c418378 100644 --- a/crates/core/src/types/header.rs +++ b/crates/core/src/types/header.rs @@ -42,16 +42,18 @@ pub struct Header { pub bitcoin_merged_mining_merkle_proof: Option, pub bitcoin_merged_mining_coinbase_transaction: Option, - /// Hash computed from the original RLP bytes received from the peer. - /// Java's RLP encoding may differ from Rust's canonical encoding (e.g. - /// leading zeros in BigInteger values), so we cache the hash at decode - /// time instead of recomputing it from re-encoded bytes. + /// The block hash, cached by `decode_with_hash` from the peer's own + /// encoding of each field. Java's RLP encoding may differ from Rust's + /// canonical encoding (e.g. leading zeros in BigInteger values), so the + /// hash is taken at decode time instead of from re-encoded bytes. It is + /// the hash of the form rskj hashes, which is not the form a versioned + /// header is sent in. #[serde(skip)] pub cached_hash: Option, - /// Merged mining hash computed from the original RLP bytes (base fields - /// only, excluding bitcoin mining fields). Cached at decode time for the - /// same Java RLP compatibility reasons as `cached_hash`. + /// The merged-mining hash, cached the same way and for the same Java RLP + /// reasons as `cached_hash`: the base fields and `ummRoot` only, without + /// the bitcoin mining fields. #[serde(skip)] pub cached_hash_for_merged_mining: Option, } @@ -93,6 +95,26 @@ fn count_rlp_items(buf: &[u8]) -> usize { count } +/// The items of the RLP list `encoded`, each in its whole encoding, header +/// and payload, as the peer spelled it. `None` unless `encoded` is exactly +/// one list of complete items. +fn rlp_list_items(encoded: &[u8]) -> Option> { + let mut rest = encoded; + let list = alloy_rlp::Header::decode(&mut rest).ok()?; + if !list.list || rest.len() != list.payload_length { + return None; + } + let mut items = Vec::new(); + while !rest.is_empty() { + let mut temp = rest; + let item = alloy_rlp::Header::decode(&mut temp).ok()?; + let length = (rest.len() - temp.len()).checked_add(item.payload_length)?; + items.push(rest.get(..length)?); + rest = &rest[length..]; + } + Some(items) +} + /// rskj reads a header's number, gas used and timestamp with /// `longValueExact`, which throws past a Java `long`: it cannot read a header /// with one over `i64::MAX`. @@ -499,120 +521,101 @@ impl Header { alloy_primitives::keccak256(&buffer) } - /// Compute the RSKIP92 block hash from original encoded bytes: re-wrap the - /// list payload with the last two items (merkle proof, coinbase) dropped. - fn rskip92_hash_from_encoded(encoded: &[u8]) -> Option { - let mut parse = encoded; - let list_h = alloy_rlp::Header::decode(&mut parse).ok()?; - let body = parse.get(..list_h.payload_length)?; - let mut ends = Vec::new(); - let mut cursor = body; - while !cursor.is_empty() { - let mut temp = cursor; - let item_h = alloy_rlp::Header::decode(&mut temp).ok()?; - if item_h.payload_length > temp.len() { - return None; - } - cursor = &temp[item_h.payload_length..]; - ends.push(body.len() - cursor.len()); - } - if ends.len() < 3 { - return None; - } - let payload = &body[..ends[ends.len() - 3]]; - let mut buf = Vec::with_capacity(payload.len() + 4); - alloy_rlp::Header { list: true, payload_length: payload.len() }.encode(&mut buf); - buf.extend_from_slice(payload); - Some(alloy_primitives::keccak256(&buf)) + /// Decode a header from RLP bytes and cache both of its hashes from those + /// bytes, since re-encoding need not reproduce a peer's Java spelling of + /// every field. + /// + /// rskj does not hash the form it sends. It hashes the compressed form, + /// which leaves out the merged-mining proof and coinbase (RSKIP-92) and, + /// for a versioned (RSKIP-351) header, the version, edges and base event + /// that follow `ummRoot` on the wire, whose commitment takes the bloom's + /// place in slot 6. So both hashes are built from the items as sent, + /// less those, with slot 6 swapped. Hashing a versioned header as sent + /// gave every testnet header from reed810 on a hash no other node agrees + /// with: no header named its parent, and the snapshot sync refused every + /// chain it was offered. + pub fn decode_with_hash(buf: &mut &[u8]) -> alloy_rlp::Result { + Self::decode_with_hash_and_activations(buf, crate::config::ActivationHeights::current()) } - /// Decode a header from RLP bytes and compute the hash from those original - /// bytes (before our re-encoding potentially changes them). - pub fn decode_with_hash(buf: &mut &[u8]) -> alloy_rlp::Result { - use alloy_rlp::Decodable; + /// [`Self::decode_with_hash`] under explicit activation heights, which + /// decide what a lone item after a V2 version byte is, as they do for + /// [`Self::decode_with_activations`]. + pub fn decode_with_hash_and_activations( + buf: &mut &[u8], + activations: &crate::config::ActivationHeights, + ) -> alloy_rlp::Result { let original = *buf; - let mut header = ::decode(buf)?; - let consumed = original.len() - buf.len(); - let has_proof_and_coinbase = header.bitcoin_merged_mining_merkle_proof.is_some() - || header.bitcoin_merged_mining_coinbase_transaction.is_some(); - header.cached_hash = if header.rskip92_active() && has_proof_and_coinbase { - Self::rskip92_hash_from_encoded(&original[..consumed]) + let mut header = Self::decode_with_activations(buf, activations)?; + let encoded = &original[..original.len() - buf.len()]; + + // `decode` has just read this list, so its items split; were one too + // lenient for that, both hashes would be left to come from this + // node's own encoding instead. + let Some(items) = rlp_list_items(encoded) else { + return Ok(header); + }; + // The sixteen base fields and `ummRoot`, then on the wire a versioned + // header's own fields, then the merged-mining fields. + let base = 16 + usize::from(header.umm_root.is_some()); + let versioned = header.version_fields.as_ref().map_or(0, |v| { + 1 + usize::from(v.edges.is_some()) + usize::from(v.base_event.is_some()) + }); + let (Some(base_items), Some(mining)) = (items.get(..base), items.get(base + versioned..)) + else { + return Ok(header); + }; + // A versioned header that sent its literal bloom is hashed with its + // commitment in slot 6, as `encode_base_fields` does. + let commitment = if header.extension_data.is_none() { + header.compressed_extension_field() } else { None - } - .or_else(|| Some(alloy_primitives::keccak256(&original[..consumed]))); - - // Compute merged mining hash from original RLP bytes. - // Three cases depending on ummRoot presence: - // - ummRoot absent (peer omitted it): hash fields 0-15 only - // - ummRoot present but empty: hash fields 0-16 (single keccak256) - // - ummRoot present and non-empty (UMM): double-hash - // keccak256( keccak256(RLP_LIST(fields_0_16))[0:20] ++ ummRoot ) - let mut parse = &original[..consumed]; - if let Ok(list_h) = alloy_rlp::Header::decode(&mut parse) { - let body = &parse[..list_h.payload_length]; - let mut cursor = body; - for _ in 0..16 { - if cursor.is_empty() { break; } - let mut temp = cursor; - if let Ok(item_h) = alloy_rlp::Header::decode(&mut temp) { - if item_h.payload_length > temp.len() { break; } - cursor = &temp[item_h.payload_length..]; - } else { - break; + }; + let hash_with = |mining: &[&[u8]]| { + let mut payload = Vec::with_capacity(encoded.len()); + for (slot, item) in base_items.iter().enumerate() { + match &commitment { + Some(field) if slot == 6 => payload.extend_from_slice(field), + _ => payload.extend_from_slice(item), } } - let mm_end = body.len() - cursor.len(); - - let has_umm_root = header.umm_root.is_some(); - let is_umm_block = header.umm_root.as_ref().is_some_and(|u| !u.is_empty()); - - let mm_hash = if has_umm_root { - // ummRoot is present in the RLP — include it in the hash payload. - // The miner included ummRoot when encoding for the merged mining - // hash, so we must do the same regardless of whether ummRoot is - // empty or non-empty. - let mut mm_end_with_umm = mm_end; - if !cursor.is_empty() { - let mut temp = cursor; - if let Ok(item_h) = alloy_rlp::Header::decode(&mut temp) { - if item_h.payload_length <= temp.len() { - let header_len = cursor.len() - temp.len(); - mm_end_with_umm += header_len + item_h.payload_length; - } - } - } - let mm_payload = &body[..mm_end_with_umm]; - let mm_list_h = alloy_rlp::Header { list: true, payload_length: mm_payload.len() }; - let mut mm_buf = Vec::with_capacity(mm_list_h.length() + mm_payload.len()); - mm_list_h.encode(&mut mm_buf); - mm_buf.extend_from_slice(mm_payload); - - if is_umm_block { - // Non-empty ummRoot: apply UMM double-hash - let umm_bytes = header.umm_root.as_ref().unwrap(); - let base = alloy_primitives::keccak256(&mm_buf); - let mut input = Vec::with_capacity(20 + umm_bytes.len()); - input.extend_from_slice(&base.as_slice()[..20]); - input.extend_from_slice(umm_bytes.as_ref()); + for item in mining { + payload.extend_from_slice(item); + } + let mut list = Vec::with_capacity(payload.len() + 9); + alloy_rlp::Header { + list: true, + payload_length: payload.len(), + } + .encode(&mut list); + list.extend_from_slice(&payload); + alloy_primitives::keccak256(&list) + }; + + // RSKIP-92: the block hash keeps the merged-mining header and leaves + // out the proof and the coinbase. + let hashed_mining = if header.rskip92_active() { + &mining[..mining.len().min(1)] + } else { + mining + }; + header.cached_hash = Some(hash_with(hashed_mining)); + + // The merged-mining hash leaves out all three, and a UMM block then + // folds its root in: keccak(the first 20 bytes ++ ummRoot). + let for_merged_mining = hash_with(&[]); + header.cached_hash_for_merged_mining = Some( + match header.umm_root.as_ref().filter(|umm| !umm.is_empty()) { + Some(umm) => { + let mut input = Vec::with_capacity(20 + umm.len()); + input.extend_from_slice(&for_merged_mining.as_slice()[..20]); + input.extend_from_slice(umm.as_ref()); alloy_primitives::keccak256(&input) - } else { - // Empty ummRoot present: single hash of fields 0-16 - alloy_primitives::keccak256(&mm_buf) } - } else { - // No ummRoot in the RLP at all: hash of fields 0-15 only - let mm_payload = &body[..mm_end]; - let mm_list_h = alloy_rlp::Header { list: true, payload_length: mm_payload.len() }; - let mut mm_buf = Vec::with_capacity(mm_list_h.length() + mm_payload.len()); - mm_list_h.encode(&mut mm_buf); - mm_buf.extend_from_slice(mm_payload); - alloy_primitives::keccak256(&mm_buf) - }; - - header.cached_hash_for_merged_mining = Some(mm_hash); - } - + None => for_merged_mining, + }, + ); Ok(header) } @@ -1172,6 +1175,21 @@ mod tests { b256!("a3403ff963cb87a3fb52371c690170f292e516d3ca367b73402f5c5900f140b1"), "must reproduce the hash testnet reached consensus on" ); + + // The network's decoder reaches it from the bytes as sent, edges and + // all, and the merged-mining hash the coinbase commits to. + let decoded = Header::decode_with_hash(&mut raw.as_slice()).unwrap(); + assert_eq!(decoded.hash(), alloy_primitives::keccak256(&buf)); + let coinbase = decoded + .bitcoin_merged_mining_coinbase_transaction + .clone() + .unwrap(); + let tag = b"RSKBLOCK:"; + let at = coinbase.windows(tag.len()).rposition(|w| w == tag).unwrap() + tag.len(); + assert_eq!( + &decoded.hash_for_merged_mining()[..20], + &coinbase[at..at + 20] + ); } /// Splits an RLP list into the payloads of its items. @@ -1240,15 +1258,16 @@ mod versioned_wire_header_tests { "2a6a52534b424c4f434b3aa299d0e6dcc89894d1897433dcfc94cb400657c925509283bf09a213007407e800000000", ); + /// A header decoded without caching either hash, so `hash()` and + /// `hash_for_merged_mining()` derive them by encoding: the path a header + /// built here takes. fn decode(hex: &str) -> Header { let bytes = alloy_primitives::hex::decode(hex).unwrap(); let mut slice = bytes.as_slice(); // Testnet headers, so testnet's RSKIP-144 height. let testnet = crate::config::ActivationHeights::testnet(); - let mut h = Header::decode_with_activations(&mut slice, &testnet).unwrap(); - // The decoder caches the hash of the bytes it was handed; clearing it - // forces `hash()` to re-derive, which is the thing under test. - h.cached_hash = None; + let h = Header::decode_with_activations(&mut slice, &testnet).unwrap(); + assert_eq!((h.cached_hash, h.cached_hash_for_merged_mining), (None, None)); h } @@ -1468,4 +1487,151 @@ mod versioned_wire_header_tests { let again = Header::decode(&mut slice).unwrap(); assert_eq!(again.version_fields, None); } + + /// A header decoded as the network decodes one: `decode_with_hash`, over + /// the whole of `hex`, under testnet's heights, whose headers these are. + fn from_the_network(hex: &str) -> Header { + let bytes = alloy_primitives::hex::decode(hex).unwrap(); + let mut rest = bytes.as_slice(); + let testnet = crate::config::ActivationHeights::testnet(); + let h = Header::decode_with_hash_and_activations(&mut rest, &testnet).unwrap(); + assert!(rest.is_empty(), "bytes after the header"); + h + } + + /// What the miner committed to in the coinbase after `RSKBLOCK:`: the + /// first 20 bytes of the merged-mining hash. + fn committed(h: &Header) -> Vec { + let coinbase = h + .bitcoin_merged_mining_coinbase_transaction + .as_ref() + .unwrap(); + let tag = b"RSKBLOCK:"; + let at = coinbase.windows(tag.len()).rposition(|w| w == tag).unwrap() + tag.len(); + coinbase[at..at + 20].to_vec() + } + + /// The network decodes headers with `decode_with_hash`, which caches + /// both hashes from the bytes sent. It used to hash those bytes whole, + /// and for a versioned header they are not what rskj hashes: every + /// testnet header from reed810 on got a hash no other node agrees with, + /// so none named its parent, and the snapshot sync refused every chain it + /// was offered as not forming one. + #[test] + fn the_network_decoder_hashes_a_versioned_header_as_rskj_does() { + for (wire, hash) in [ + ( + V1_WIRE, + "0xa44a88ce39fb3851d7f9b5ca9879b2c93b2544b12ac8239c034c6e589219c411", + ), + ( + V2_WIRE, + "0x27138ca46912bd08e1a9a942a5e81d0ec96fc91820f539f7e872239b3a1626bd", + ), + ( + V2_SHORT_WIRE, + "0x119296ea9d8d7efa82550bfb447ae9441ea75f9537f47917f1ca38e0b8982c05", + ), + ] { + let hash: B256 = hash.parse().unwrap(); + let h = from_the_network(wire); + assert_eq!(h.cached_hash, Some(hash), "#{}", h.number); + // What testnet's miners committed to, so its hash rate attests to it. + let merged = h.cached_hash_for_merged_mining.unwrap(); + assert_eq!(&merged[..20], committed(&h).as_slice(), "#{}", h.number); + // And a header built here derives both the same way. + let built = decode(wire); + assert_eq!(built.hash(), hash, "#{}", h.number); + assert_eq!(built.hash_for_merged_mining(), merged, "#{}", h.number); + } + } + + /// Every layout the decoder reads, built here and read back as the + /// network reads it: what `decode_with_hash` caches from the items sent + /// is what this node derives by encoding. The captured headers above tie + /// both paths to rskj; this ties down where `ummRoot`, a versioned + /// header's own fields and the mining fields sit, across layouts no + /// captured header has: a UMM root, edges, a base event, no mining + /// fields, a header from before RSKIP-92. + #[test] + fn every_layout_caches_the_hashes_it_would_encode_to() { + let real = decode(V1_WIRE); + let umm = [None, Some(Bytes::new()), Some(Bytes::from(vec![0xab; 20]))]; + let fields = |version, edges: Option>, base_event: Option| { + Some(HeaderVersionFields { + version, + edges, + base_event, + }) + }; + // RSKIP-144 decides what a lone item after a V2 version byte is: the + // edges where it is active, the base event where it is not. Each + // layout is read under heights that make it one rskj reads. + let with_144 = crate::config::ActivationHeights { + rskip144: Some(0), + ..crate::config::ActivationHeights::testnet() + }; + let without_144 = crate::config::ActivationHeights { + rskip144: None, + ..crate::config::ActivationHeights::testnet() + }; + let versions = [ + (None, &with_144), + (fields(1, None, None), &with_144), + (fields(1, Some(vec![1, 3]), None), &with_144), + (fields(2, Some(vec![]), Some(Bytes::new())), &with_144), + (fields(2, Some(vec![2]), None), &with_144), + (fields(2, Some(vec![2]), Some(Bytes::from_static(b"base event"))), &with_144), + (fields(2, None, Some(Bytes::from_static(b"base event"))), &without_144), + ]; + let mut layouts = 0; + for number in [100, real.number] { + for umm_root in &umm { + for (version_fields, activations) in &versions { + for mining in [true, false] { + // The decoder tells `ummRoot` from the mining fields + // by count, so it reads one only beside them, and + // the version fields only after one. + if (umm_root.is_some() || version_fields.is_some()) && !mining { + continue; + } + if version_fields.is_some() && umm_root.is_none() { + continue; + } + let mut header = Header { + number, + umm_root: umm_root.clone(), + version_fields: version_fields.clone(), + ..real.clone() + }; + if !mining { + header.bitcoin_merged_mining_header = None; + header.bitcoin_merged_mining_merkle_proof = None; + header.bitcoin_merged_mining_coinbase_transaction = None; + } + let mut wire = Vec::new(); + header.encode(&mut wire); + assert_eq!( + Header::decode_with_activations(&mut wire.as_slice(), activations).unwrap(), + header, + "the layout reads back" + ); + let cached = + Header::decode_with_hash_and_activations(&mut wire.as_slice(), activations) + .unwrap(); + assert_eq!(cached.cached_hash, Some(header.hash()), "{header:?}"); + assert_eq!( + cached.cached_hash_for_merged_mining, + Some(header.hash_for_merged_mining()), + "{header:?}" + ); + layouts += 1; + } + } + } + } + // Per height: four version-0 layouts, and six versioned ones on + // each of two UMM roots. + assert_eq!(layouts, 2 * (4 + 6 * 2)); + } } diff --git a/docs/quirks-java-artifacts.md b/docs/quirks-java-artifacts.md index 84669f7..74045fa 100644 --- a/docs/quirks-java-artifacts.md +++ b/docs/quirks-java-artifacts.md @@ -68,19 +68,29 @@ leading zeros. The rskj node inherited this behavior. **Cached hash from original bytes** (`crates/core/src/types/header.rs`): -- `Header` carries an optional `cached_hash: Option` field. -- `Header::decode_with_hash()` computes `keccak256` over the **original RLP - bytes** received from the peer (before decoding and re-encoding), and stores - the result in `cached_hash`. +- `Header` carries an optional `cached_hash: Option` field, and + `cached_hash_for_merged_mining` beside it. +- `Header::decode_with_hash()` builds both hashes from the **peer's own + encoding of each field**, as received, and stores them. +- It hashes the form rskj hashes, which is not the form rskj sends. rskj + hashes a compressed encoding: from RSKIP-92 on the block hash leaves out + the merged-mining proof and coinbase, and a versioned (RSKIP-351) header, + sent with its literal bloom in slot 6 and its version, edges and base + event after `ummRoot`, is hashed without those three and with a commitment + to all four in slot 6. So `decode_with_hash` takes the items as sent, + leaves those out and swaps slot 6. Hashing the bytes whole gave every + testnet header from reed810 (#7,139,600) on a hash no other node agrees + with, and the snapshot sync found no chain in what it was offered. - `Header::hash()` returns `cached_hash` when present, falling back to - `keccak256(self.encode())` for locally-constructed headers. -- The network message decoder (`crates/networking/src/protocol/rsk.rs`) uses - `decode_with_hash` for all `BlockHeadersResponse` payloads. + encoding the header for locally-constructed headers. +- The network message decoder (`crates/networking/src/protocol/rsk.rs`), the + snapshot block decoder (`snap.rs`) and the freezer's uncles use + `decode_with_hash` for every header and uncle they read. This is the same approach used by production Ethereum clients (Geth, Reth): -cache the hash from wire bytes rather than recomputing from re-encoded data. -It decouples hash identity from encoding, making the client resilient to any -encoding differences across implementations. +cache the hash from the peer's bytes rather than recomputing from re-encoded +data. It decouples hash identity from how this node would spell a field, +making the client resilient to encoding differences across implementations. **ParentHashRule omission** (`crates/core/src/validation/mod.rs`): @@ -106,9 +116,15 @@ it: - **Fragile over time**: if rskj changes how it encodes a field, our mimicry would silently break. -The cached-hash approach is more robust: it works for all headers, all values, -and all future protocol versions, without requiring byte-level encoding -compatibility. +The cached-hash approach is more robust: it works for all values, without +requiring byte-level encoding compatibility. It does not spare the decoder +from knowing which form rskj hashes: a header version that changes that +form, as RSKIP-351 did, has to be taught to `decode_with_hash`. + +The encoder does now spell `difficulty`, `gasLimit` and `minimumGasPrice` as +rskj does (`java_signed_bytes`), because a header this node builds has no +peer's bytes to hash. That leaves the cache for headers from peers, whose +fields need not be spelled as this node would spell them. --- From 4e4c0e1760954253d6b9f9d0312a12bda0bfe498 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Fri, 6 Nov 2026 18:40:09 -0800 Subject: [PATCH 025/264] Hold testnet to RSKIP290's minimum difficulty From RSKIP290 on, rskj holds testnet's difficulty to at least 550,000,000 (Constants.getMinimumDifficulty), and testnet.conf activates it at #2,581,800 rather than at hop400. rustock held testnet to 131,072 at every height, so at #6,360,768, a late block whose parent sat at the floor, it expected 548,625,000 where the chain has 550,000,000, and the testnet snapshot sync refused the header after walking 1.9 million others. The floor in force at a height is ChainConfig::min_difficulty_at, which both of the difficulty rule's floors now use: the clamp on a falling difficulty, and the ten-minute drop that testnet never retires. rskj applies RSKIP290 on testnet alone; mainnet's floor stays 7e15, which taking it there would lower. The tests take #6,360,768 and its parent from the live chain, the activation height, the ten-minute drop on either side of it, and mainnet and regtest, which keep their floors. Each of 8 deliberate bugs fails a test. --- crates/core/src/config.rs | 32 ++++++++++ crates/core/src/validation/difficulty.rs | 79 ++++++++++++++++++++++-- crates/execution/src/mining/tests.rs | 2 + 3 files changed, 108 insertions(+), 5 deletions(-) diff --git a/crates/core/src/config.rs b/crates/core/src/config.rs index 558b817..c19cc4b 100644 --- a/crates/core/src/config.rs +++ b/crates/core/src/config.rs @@ -65,6 +65,13 @@ pub struct ActivationHeights { /// `None` where it never does. rskj's `reference.conf` sets it at /// reed810, which mainnet has at -1, so no mainnet header carries it. pub rskip351: Option, + /// Height at which RSKIP290 -- testnet's minimum difficulty raised to + /// 550,000,000 -- activates, or `None` where it never does. + /// `reference.conf` sets it at hop400, and `testnet.conf` overrides it to + /// #2,581,800, half a million blocks sooner. rskj applies it on testnet + /// alone (`Constants.getMinimumDifficulty`), so on the other chains it + /// is recorded but changes nothing: see [`ChainConfig::min_difficulty_at`]. + pub rskip290: Option, } /// Process-wide activation heights, consulted by block-hash computation @@ -116,6 +123,8 @@ impl ActivationHeights { rskip144: None, // main.conf: `reed810 = -1`. rskip351: None, + // reference.conf: `rskip290 = hop400`. Mainnet's floor stays 7e15. + rskip290: Some(4_598_500), } } @@ -136,6 +145,8 @@ impl ActivationHeights { rskip144: Some(7_139_600), // testnet.conf: `reed810 = 7139600`. rskip351: Some(7_139_600), + // testnet.conf: `rskip290 = 2581800`, not hop400. + rskip290: Some(2_581_800), } } @@ -154,6 +165,8 @@ impl ActivationHeights { rskip144: None, // regtest.conf: `rskip351 = 1`. rskip351: Some(1), + // reference.conf's hop400, which regtest.conf puts at genesis. + rskip290: Some(0), } } } @@ -164,6 +177,8 @@ pub struct ChainConfig { pub network_id: u64, pub duration_limit: u64, pub difficulty_divisor: U256, + /// rskj `Constants.minimumDifficulty`. The floor in force at a height, + /// which testnet raises from RSKIP290, is [`ChainConfig::min_difficulty_at`]. pub min_difficulty: U256, /// The greatest difficulty at which a block below RSKIP98 (orchid) may /// be signed by a fallback-mining key rather than merge-mined: rskj @@ -272,6 +287,23 @@ impl ChainConfig { } } + /// The least difficulty a block at `number` may have: rskj + /// `Constants.getMinimumDifficulty`. `min_difficulty`, except on testnet + /// from RSKIP290, where it is 550,000,000. A falling difficulty is raised + /// to it, and before RSKIP97 a block ten minutes late drops to it. + /// + /// Holding testnet to 131,072 there made rustock expect 548,625,000 of + /// #6,360,768, a late block whose parent sat at the 550,000,000 floor, + /// and its snapshot sync refused the header. + pub fn min_difficulty_at(&self, number: u64) -> U256 { + // rskj's TESTNET_CHAIN_ID and TESTNET2_CHAIN_ID. + let testnet = matches!(self.chain_id, 31 | 34); + match self.activation_heights.rskip290 { + Some(height) if testnet && number >= height => U256::from(550_000_000u64), + _ => self.min_difficulty, + } + } + /// Returns the well-known genesis hash for this network, if one exists. /// Mainnet and testnet have hardcoded hashes because Java's RLP encoding /// of the genesis difficulty includes a leading zero byte that differs diff --git a/crates/core/src/validation/difficulty.rs b/crates/core/src/validation/difficulty.rs index f9b0938..c7f994b 100644 --- a/crates/core/src/validation/difficulty.rs +++ b/crates/core/src/validation/difficulty.rs @@ -66,7 +66,7 @@ impl DifficultyRule { if !rskip97_active { let delta_ts = header.timestamp.saturating_sub(parent.timestamp); if delta_ts >= TEN_MINUTES { - return self.config.min_difficulty; + return self.config.min_difficulty_at(header.number); } } @@ -121,9 +121,11 @@ impl DifficultyRule { U256::ZERO }; - // Clamp to minimum difficulty — matches rskj's max(minDifficulty, fromParent). - if from_parent < self.config.min_difficulty { - self.config.min_difficulty + // Clamp to the floor in force at this height: rskj's + // max(getMinimumDifficulty(number), fromParent). + let floor = self.config.min_difficulty_at(header.number); + if from_parent < floor { + floor } else { from_parent } @@ -137,7 +139,7 @@ mod rskip97_tests { use alloy_primitives::{Bytes, B256}; use std::sync::Arc; - fn header_at(number: u64, timestamp: u64, difficulty: U256) -> Header { + pub(super) fn header_at(number: u64, timestamp: u64, difficulty: U256) -> Header { let mut h = Header { parent_hash: B256::ZERO, ommers_hash: B256::ZERO, @@ -242,3 +244,70 @@ mod rskip97_tests { assert_eq!(child(1 << 30), parent.difficulty - step); } } + +#[cfg(test)] +mod rskip290_tests { + use super::rskip97_tests::header_at; + use super::*; + use crate::config::ChainConfig; + use std::sync::Arc; + + fn testnet() -> DifficultyRule { + DifficultyRule { + config: Arc::new(ChainConfig::testnet()), + } + } + + /// Testnet #6,360,768 and its parent, from the live chain: 120 seconds + /// apart, so the block is late and its difficulty falls, but not below + /// the 550,000,000 that RSKIP290 holds testnet to, where the parent + /// already sat. Held to 131,072, rustock expected 548,625,000, and its + /// snapshot sync refused the header after walking 1.9 million others. + #[test] + fn testnet_block_6360768_stays_on_rskip290s_floor() { + let parent = header_at(6_360_767, 1_746_607_067, U256::from(550_000_000u64)); + let mut block = header_at(6_360_768, 1_746_607_187, U256::from(550_000_000u64)); + assert_eq!(testnet().validate_with_parent(&block, &parent), Ok(())); + block.difficulty = U256::from(548_625_000u64); + assert!(testnet().validate_with_parent(&block, &parent).is_err()); + } + + /// testnet.conf activates RSKIP290 at #2,581,800, not at hop400. + #[test] + fn testnets_floor_rises_at_2581800() { + let late = |number: u64| { + let parent = header_at(number - 1, 1_000_000, U256::from(550_000_000u64)); + testnet().difficulty_for_child(&parent, number, 1_000_000 + 60, 0) + }; + assert_eq!(late(2_581_799), U256::from(548_625_000u64)); + assert_eq!(late(2_581_800), U256::from(550_000_000u64)); + } + + /// The ten-minute drop, which testnet never retires, lands on the floor + /// in force: RSKIP290's from its height on. + #[test] + fn testnets_ten_minute_drop_lands_on_the_floor_in_force() { + let drop = |number: u64| { + let parent = header_at(number - 1, 1_000_000, U256::from(10_000_000_000u64)); + testnet().difficulty_for_child(&parent, number, 1_000_000 + 600, 0) + }; + assert_eq!(drop(2_581_799), U256::from(131_072u64)); + assert_eq!(drop(2_581_800), U256::from(550_000_000u64)); + } + + /// rskj holds testnet alone to it. Mainnet's floor is 7e15, far above + /// 550,000,000, so taking RSKIP290 there would lower it; regtest's is 1, + /// and taking it would raise it. + #[test] + fn only_testnet_has_rskip290s_floor() { + for config in [ChainConfig::mainnet(), ChainConfig::regtest()] { + let floor = config.min_difficulty; + let rule = DifficultyRule { + config: Arc::new(config), + }; + let parent = header_at(5_000_000, 1_000_000, floor); + let late = rule.difficulty_for_child(&parent, 5_000_001, 1_000_000 + 60, 0); + assert_eq!(late, floor); + } + } +} diff --git a/crates/execution/src/mining/tests.rs b/crates/execution/src/mining/tests.rs index a677e19..35bec47 100644 --- a/crates/execution/src/mining/tests.rs +++ b/crates/execution/src/mining/tests.rs @@ -53,6 +53,8 @@ fn test_chain_config() -> Arc { // From genesis too: a partitioned template carries the version // fields RSKIP351 brings. rskip351: Some(0), + // Testnet's floor alone, and this chain is not testnet. + rskip290: None, }, genesis_spec: None, skip_pow_validation: false, From 950a25d7ab38c17c3a7c19a92df71b9c8f471184 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Fri, 6 Nov 2026 21:37:39 -0800 Subject: [PATCH 026/264] Serve a relay what a light client checks: raw blocks, numbers, state walks A relay answers a light client's Rootstock requests from its own node, and the client checks every byte, so the node gives the bytes as rskj writes them: - rsk_getBlockNumberByHash(hash): the block's number on the chain the node follows, or null, as for a block held only on a fork. - rsk_getRawBlocks(from, to): one run of that chain, at most 1,000 blocks, oldest first, each its header compressed, as rskj sends a chain's headers to its peers (BlockHeadersResponseMessage, getEncodedCompressed), and its uncles in full, the form its uncles hash covers. It stops before the first block it doesn't hold whole and before one that doesn't follow the one before it. Uncles come from the block's body or, once that's gone, from the freezer; a block whose uncles hash is the empty list's is whole without either; an uncle held only compressed isn't held whole. - rsk_getStateNodes(blockHash, [keys]): for each raw unitrie key, the nodes of the walk along it from the block's own state root, deepest first, as try_nodes_toward gives them; null if the node doesn't hold that state, and an error if the store can't produce a node on the way. At most 64 keys, of at most 128 bytes each. Header::encode_compressed writes a header compressed. Testnet #8,260,761, which the testnet node took in full, compresses to what a converter written apart from this crate makes of it, with rskj's hash; #8,260,720, which rskj's peers sent compressed, writes back as sent. The README lists these methods with rsk_getStorageNodes. Each of 24 deliberate bugs fails a test. --- README.md | 10 + crates/core/src/types/header.rs | 112 +++++++ crates/rpc/src/proof.rs | 98 +++++++ crates/rpc/src/rsk.rs | 149 +++++++++- crates/rpc/src/server.rs | 3 + crates/rpc/src/tests.rs | 500 ++++++++++++++++++++++++++++++++ 6 files changed, 871 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9004b6d..147ad86 100644 --- a/README.md +++ b/README.md @@ -272,6 +272,16 @@ The RPC server is compatible with rskj's JSON-RPC 2.0 interface. Supported metho - `eth_getBalance`, `eth_getTransactionCount`, `eth_getCode`, `eth_getStorageAt` +**For light clients** (rustock's own, not rskj's: what a relay answers a light client from, which checks every byte itself): + +- `rsk_getStorageNodes(address, [slots], block)`: for each slot, whether it's held, its value, and the unitrie nodes from + the slot's key, or from where the key leaves the trie, up to the block's state root +- `rsk_getStateNodes(blockHash, [keys])`: for each raw unitrie key, the nodes from where its walk ends up to the state root + of that block, or null if the node doesn't hold that state +- `rsk_getBlockNumberByHash(hash)`: the block's number on the chain the node follows, or null +- `rsk_getRawBlocks(from, to)`: one run of that chain, at most 1,000 blocks, each its header compressed, as rskj sends a + chain's headers to its peers, and its uncles in full + **Execution:** - `eth_call`, `eth_estimateGas` (run against a forked state with full precompile and Bridge support) diff --git a/crates/core/src/types/header.rs b/crates/core/src/types/header.rs index c418378..ae1ed5f 100644 --- a/crates/core/src/types/header.rs +++ b/crates/core/src/types/header.rs @@ -521,6 +521,25 @@ impl Header { alloy_primitives::keccak256(&buffer) } + /// The header as rskj sends a chain's headers to its peers + /// (`BlockHeader.getEncodedCompressed`): every field and all three + /// merged-mining fields, with a versioned header's commitment to its + /// extension in slot 6 and none of its own fields after `ummRoot`. rskj + /// reads a `BlockHeadersResponseMessage`'s headers so + /// (`decodeHeader(.., true)`), and a header taken from one is held so here. + /// A version-0 header has this one form. + pub fn encode_compressed(&self) -> Vec { + let payload = self.encode_payload(true, true); + let mut out = Vec::with_capacity(payload.len() + 4); + alloy_rlp::Header { + list: true, + payload_length: payload.len(), + } + .encode(&mut out); + out.extend_from_slice(&payload); + out + } + /// Decode a header from RLP bytes and cache both of its hashes from those /// bytes, since re-encoding need not reproduce a peer's Java spelling of /// every field. @@ -1258,6 +1277,99 @@ mod versioned_wire_header_tests { "2a6a52534b424c4f434b3aa299d0e6dcc89894d1897433dcfc94cb400657c925509283bf09a213007407e800000000", ); + /// Testnet #8,260,761 in full, as rustock's testnet node took it with its + /// block, which rskj hashes to 0x9a1cfe09...0d47. + const V2_FULL: &str = concat!( + "f9029ba0285f7c3513c466f54fde5f1b73b2d93b35374515472192250bd3a56396dc649ea01dcc4de8dec75d7aab85b5", + "67b6ccd41ad312451b948a7413f0a142fd40d4934794b774aa2876145b2f6f3de27e5e6ac970aa12d771a04aa4b67c3e", + "30508692f13e9e10758d907e7d851234cd6cd00d65aa49e5a1ac44a0f40c99e8151d350e30309fa0a74234c365961aa0", + "81ad18e51a4281c513ec76d0a066cfdb731f620cd96e2c2cb0f7d3c3a2879c29b40014aa27efbbf3cf9cd3b0f6b90100", + "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "00000000000000000000000000000000844edfab4b837e0c998400b71b0080846aec1a5c92d1018f564554495645522d", + "3733316239643880835a74cf8080028080b8507111010000000000000000000000000000000000000000000000000000", + "00000000000000135534881310aada94b2b5867af37e4e2fc48be028c35beeaa7f1dc4cf501e825d1aec6affff0f1d0d", + "16caea80b85800000000000003c09e1ed89b4ab8f092a3dc3a4b852db4552dfd95136f298514ba947d076032a8f20bbe", + "a052534b424c4f434b3a6e7fa68f56e3bc6085e7e02e325250f1c72ad9eef1a0d7693e516e10007e0c9900000000", + ); + + /// The same header compressed, as a converter written apart from this + /// crate compressed it, following rskj's `getEncodedCompressed`. + const V2_COMPRESSED: &str = concat!( + "f901b9a0285f7c3513c466f54fde5f1b73b2d93b35374515472192250bd3a56396dc649ea01dcc4de8dec75d7aab85b5", + "67b6ccd41ad312451b948a7413f0a142fd40d4934794b774aa2876145b2f6f3de27e5e6ac970aa12d771a04aa4b67c3e", + "30508692f13e9e10758d907e7d851234cd6cd00d65aa49e5a1ac44a0f40c99e8151d350e30309fa0a74234c365961aa0", + "81ad18e51a4281c513ec76d0a066cfdb731f620cd96e2c2cb0f7d3c3a2879c29b40014aa27efbbf3cf9cd3b0f6a3e202", + "a09aca8469839f117b1a26abbdea244a32cb0833e387cf6af9dc13eb336094d3c2844edfab4b837e0c998400b71b0080", + "846aec1a5c92d1018f564554495645522d3733316239643880835a74cf8080b850711101000000000000000000000000", + "000000000000000000000000000000000000000000135534881310aada94b2b5867af37e4e2fc48be028c35beeaa7f1d", + "c4cf501e825d1aec6affff0f1d0d16caea80b85800000000000003c09e1ed89b4ab8f092a3dc3a4b852db4552dfd9513", + "6f298514ba947d076032a8f20bbea052534b424c4f434b3a6e7fa68f56e3bc6085e7e02e325250f1c72ad9eef1a0d769", + "3e516e10007e0c9900000000", + ); + + /// Testnet #8,260,720 as rskj's peers sent it to rustock's testnet node, + /// compressed, which rskj hashes to 0xf343f40f...1bef. + const V2_FROM_PEERS: &str = concat!( + "f901f9a00bdcd28d517b22bc7b65bf669b05924ac8ba8a862ccb4b17d619fdbd8b9789f8a01dcc4de8dec75d7aab85b5", + "67b6ccd41ad312451b948a7413f0a142fd40d4934794ad418c1d48780005f6d847ef0a5e3bd93ea09090a0c545101cc3", + "df462f9c369b22f722ec66ade0f031b528512f12fceca574d0ecc8a01db2709b879ae45385439bc35bc1ce8e821de8ae", + "b4a4e8ec9c94cf9f3dc55f7ea01ddfe2c6ff70e83e21bc8a905eb71278aab2fc921dd08543c20451a2ccbf286da3e202", + "a03c015999cbb7ab1fdb80e23b658a251e424af79a99262579f2fd59bd50fddc78844f476ad3837e0c708400b71b0082", + "e8f8846aec16be92d1018f564554495645522d37333162396438855a76a29740835a5a088080b85000000020785e2a8b", + "51231a7478b788a2427a2a4c190c0f4e418e25c4ca0c000000000000ba37f569d2f87d917d83e79d50af85497b7d35f0", + "ebe4e7c57ddd76e80fb58dfca130ec6af0ff0f1a007dc5fa80b8910000000000000080f880b614920083ca51e27b69b7", + "d6dba5262da24a347487e62e8406463eb94d6a6088ac0000000000000000266a24aa21a9ede2f61c3f71d1defd3fa999", + "dfa36953755c690689799962b48bebd836974e8cf900000000000000002a6a52534b424c4f434b3a3d8461c1058c9db8", + "352ad0c0b7e56a1ba4fcac4da0d7693e516ef40a007e0c7000000000", + ); + + /// rskj sends a chain's headers to its peers compressed + /// (`getEncodedCompressed`), and reads them so: a versioned header's + /// commitment in slot 6, none of its own fields after `ummRoot`, and all + /// three merged-mining fields. A header taken in full compresses to what a + /// converter written apart from this crate makes of it; one that rskj's + /// peers sent compressed is held so, and written back the same in either + /// form, with rskj's hash. + #[test] + fn a_versioned_header_compresses_as_rskj_sends_it_to_its_peers() { + let hex = |text: &str| alloy_primitives::hex::decode(text).unwrap(); + let wire = |header: &Header| { + let mut out = Vec::new(); + header.encode(&mut out); + out + }; + let hash: B256 = "0x9a1cfe099ff781afac37ad659654fd4c0e6fe400ba764be2f80bd3dec2600d47" + .parse() + .unwrap(); + let full = from_the_network(V2_FULL); + assert_eq!((full.number, full.hash()), (8_260_761, hash)); + assert_eq!(full.encode_compressed(), hex(V2_COMPRESSED)); + assert_eq!(wire(&full), hex(V2_FULL)); + // Read back as rskj's peers send it: the same hashes, and held + // compressed, no other form. + let read = from_the_network(V2_COMPRESSED); + assert_eq!(read.hash(), hash); + assert_eq!(read.hash_for_merged_mining(), full.hash_for_merged_mining()); + assert_eq!(read.encode_compressed(), hex(V2_COMPRESSED)); + assert_eq!(wire(&read), hex(V2_COMPRESSED)); + // So for one that rskj's peers sent. + let peers = from_the_network(V2_FROM_PEERS); + let peers_hash: B256 = "0xf343f40fdd2152318c13fda9abdc80bb5e8a57a9166e831dfbdfb448f7821bef" + .parse() + .unwrap(); + assert_eq!((peers.number, peers.hash()), (8_260_720, peers_hash)); + assert_eq!(peers.encode_compressed(), hex(V2_FROM_PEERS)); + assert_eq!(wire(&peers), hex(V2_FROM_PEERS)); + // A version-0 header has one form. + let mut v0 = decode(V1_WIRE); + v0.version_fields = None; + assert_eq!(v0.encode_compressed(), wire(&v0)); + } + /// A header decoded without caching either hash, so `hash()` and /// `hash_for_merged_mining()` derive them by encoding: the path a header /// built here takes. diff --git a/crates/rpc/src/proof.rs b/crates/rpc/src/proof.rs index 1180d22..c3b80e8 100644 --- a/crates/rpc/src/proof.rs +++ b/crates/rpc/src/proof.rs @@ -306,3 +306,101 @@ pub fn rsk_get_storage_nodes(id: Value, params: &Value, state: &RpcState) -> Jso }), ) } + +/// The most keys that one `rsk_getStateNodes` call walks along. +pub const MAX_STATE_KEYS: usize = 64; + +/// The longest key that `rsk_getStateNodes` walks along, in bytes: more than +/// any key the unitrie holds, the longest being a storage key's 74. +pub const MAX_STATE_KEY: usize = 128; + +/// `rsk_getStateNodes(blockHash, [keys])`: for each key, the unitrie nodes of +/// the walk along it from the state root of the block with hash `blockHash`, +/// from where the walk ends up to the root, as [`TrieNode::try_nodes_toward`] +/// gives them; or null if this node doesn't hold the block's header, or the +/// state at its state root. +/// +/// A key is the trie's own, raw (`code_key`, `storage_key`, ...), so one call +/// proves a contract's code and its slots together. The walk hangs from the +/// header's own state root and no other, as that is what a caller checks it +/// against, so a header from before wasabi, whose state root is no unitrie's, +/// has no state here. Whether a key is held is the caller's to read from the +/// nodes, as `rsk_getStorageNodes` describes. A node the store cannot produce +/// is an error, never an absence. +pub fn rsk_get_state_nodes(id: Value, params: &Value, state: &RpcState) -> JsonRpcResponse { + let Some(hash) = params + .get(0) + .and_then(|v| v.as_str()) + .and_then(crate::helpers::parse_b256) + else { + return JsonRpcResponse::error(id, INVALID_PARAMS, "Missing or invalid block hash"); + }; + let Some(listed) = params.get(1).and_then(|v| v.as_array()) else { + return JsonRpcResponse::error(id, INVALID_PARAMS, "Missing key array"); + }; + if listed.len() > MAX_STATE_KEYS { + return JsonRpcResponse::error( + id, + INVALID_PARAMS, + format!("At most {MAX_STATE_KEYS} keys at a time"), + ); + } + let mut keys = Vec::with_capacity(listed.len()); + for key in listed { + match key.as_str().and_then(parse_state_key) { + Some(key) => keys.push(key), + None => { + return JsonRpcResponse::error(id, INVALID_PARAMS, format!("Invalid key: {key}")) + } + } + } + + let header = match state.store.header(hash) { + Ok(Some(header)) => header, + Ok(None) => return JsonRpcResponse::success(id, Value::Null), + Err(e) => { + return JsonRpcResponse::error( + id, + INTERNAL_ERROR, + format!("Failed to read the header: {e}"), + ) + } + }; + let Some(trie_store) = state.trie_store.clone() else { + return JsonRpcResponse::success(id, Value::Null); + }; + let store = &*trie_store; + let Some(message) = store.get(header.state_root.as_slice()) else { + return JsonRpcResponse::success(id, Value::Null); + }; + let root = TrieNode::from_message(&message, store); + + let mut walks: Vec> = Vec::with_capacity(keys.len()); + for key in &keys { + match root.try_nodes_toward(&TrieKeySlice::from_key(key), store) { + Ok(nodes) => walks.push( + nodes + .iter() + .map(|n| format!("0x{}", hex::encode(n.to_message(store)))) + .collect(), + ), + Err(missing) => { + return JsonRpcResponse::error( + id, + INTERNAL_ERROR, + format!("The state is incomplete here: node {missing:#x} is missing"), + ) + } + } + } + JsonRpcResponse::success(id, json!(walks)) +} + +/// A trie key as `0x`-prefixed hex, at most [`MAX_STATE_KEY`] bytes. +fn parse_state_key(text: &str) -> Option> { + let digits = text.strip_prefix("0x")?; + if digits.len() > 2 * MAX_STATE_KEY { + return None; + } + hex::decode(digits).ok() +} diff --git a/crates/rpc/src/rsk.rs b/crates/rpc/src/rsk.rs index 84664e1..a684e1f 100644 --- a/crates/rpc/src/rsk.rs +++ b/crates/rpc/src/rsk.rs @@ -1,6 +1,8 @@ -use crate::helpers::{parse_b256, parse_block_number, to_hex_bytes}; +use crate::helpers::{parse_b256, parse_block_number, to_hex_bytes, to_hex_u64}; use crate::types::*; +use alloy_primitives::{b256, B256}; use alloy_rlp::Encodable; +use rustock_core::Header; use rustock_storage::BlockStore; use serde_json::{json, Value}; @@ -70,6 +72,151 @@ pub fn rsk_get_raw_block_header_by_number( } } +/// The most blocks that one `rsk_getRawBlocks` call gives. +pub const MAX_RAW_BLOCKS: u64 = 1_000; + +/// Keccak-256 of the empty RLP list: the uncles hash of a block that names no +/// uncles. +const EMPTY_UNCLES_HASH: B256 = + b256!("1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347"); + +/// `rsk_getBlockNumberByHash(hash)`: the number of the block with hash `hash` +/// on the chain this node follows, or null if it holds no such block there. A +/// block it holds only on a fork has none. +pub fn rsk_get_block_number_by_hash( + id: Value, + params: &Value, + store: &BlockStore, +) -> JsonRpcResponse { + let Some(hash) = params.get(0).and_then(|v| v.as_str()).and_then(parse_b256) else { + return JsonRpcResponse::error(id, INVALID_PARAMS, "Missing or invalid block hash"); + }; + let header = match store.header(hash) { + Ok(Some(header)) => header, + Ok(None) => return JsonRpcResponse::success(id, Value::Null), + Err(e) => { + return JsonRpcResponse::error( + id, + INTERNAL_ERROR, + format!("Failed to read the header: {e}"), + ) + } + }; + match store.canonical_hash(header.number) { + Ok(Some(canonical)) if canonical == hash => { + JsonRpcResponse::success(id, json!(to_hex_u64(header.number))) + } + Ok(_) => JsonRpcResponse::success(id, Value::Null), + Err(e) => { + JsonRpcResponse::error(id, INTERNAL_ERROR, format!("Failed to read the chain: {e}")) + } + } +} + +/// `rsk_getRawBlocks(from, to)`: the blocks numbered `from` to `to` on the +/// chain this node follows, both included, oldest first, at most +/// [`MAX_RAW_BLOCKS`] of them. Each is `{header, uncles}`: its header +/// compressed, as rskj sends a chain's headers to its peers +/// ([`Header::encode_compressed`]), and its uncles in full, as a block carries +/// them and its uncles hash covers them, in the order it names them. +/// +/// What it gives is one run of the chain: it stops before the first block it +/// doesn't hold whole, header and uncles, and before one that doesn't follow +/// the block before it, as when the chain moved while it read. A block whose +/// uncles hash is the empty list's names none, so it is whole without its +/// body; an uncle held only compressed isn't held whole, as its block's uncles +/// hash covers it in full. +pub fn rsk_get_raw_blocks(id: Value, params: &Value, store: &BlockStore) -> JsonRpcResponse { + let number = |at: usize| { + params + .get(at) + .and_then(|v| v.as_str()) + .and_then(parse_quantity) + }; + let (Some(from), Some(to)) = (number(0), number(1)) else { + return JsonRpcResponse::error(id, INVALID_PARAMS, "Missing or invalid block numbers"); + }; + if to >= from && to - from >= MAX_RAW_BLOCKS { + return JsonRpcResponse::error( + id, + INVALID_PARAMS, + format!("At most {MAX_RAW_BLOCKS} blocks at a time"), + ); + } + let mut blocks = Vec::new(); + let mut parent: Option = None; + for number in from..=to { + let (hash, header, uncles) = match whole_block(store, number) { + Ok(Some(block)) => block, + Ok(None) => break, + Err(e) => { + return JsonRpcResponse::error( + id, + INTERNAL_ERROR, + format!("Failed to read block #{number}: {e}"), + ) + } + }; + if parent.is_some_and(|parent| header.parent_hash != parent) { + break; + } + parent = Some(hash); + let uncles: Vec = uncles + .iter() + .map(|uncle| { + let mut out = Vec::new(); + uncle.encode(&mut out); + to_hex_bytes(&out) + }) + .collect(); + blocks.push(json!({ + "header": to_hex_bytes(&header.encode_compressed()), + "uncles": uncles, + })); + } + JsonRpcResponse::success(id, json!(blocks)) +} + +/// The block at `number` on the chain this node follows, its hash and its +/// uncles, if the node holds the header and every uncle in full. +fn whole_block( + store: &BlockStore, + number: u64, +) -> anyhow::Result)>> { + let Some(hash) = store.canonical_hash(number)? else { + return Ok(None); + }; + let Some(header) = store.header(hash)? else { + return Ok(None); + }; + // The body's, or the freezer's once the body is gone. + let held = match store.uncles(hash)? { + Some(uncles) => Some(uncles), + None => match store.freezer() { + Some(freezer) => freezer.uncles(number)?, + None => None, + }, + }; + let uncles = match held { + Some(uncles) => uncles, + None if header.ommers_hash == EMPTY_UNCLES_HASH => Vec::new(), + None => return Ok(None), + }; + if uncles.iter().any(|uncle| uncle.extension_data.is_some()) { + return Ok(None); + } + Ok(Some((hash, header, uncles))) +} + +/// A block number as a `0x`-prefixed hex quantity, and nothing else. +fn parse_quantity(text: &str) -> Option { + let digits = text.strip_prefix("0x")?; + if digits.is_empty() || !digits.bytes().all(|b| b.is_ascii_hexdigit()) { + return None; + } + u64::from_str_radix(digits, 16).ok() +} + /// `rsk_getStorageBytesAt` — the variable-length value at a storage key. /// /// `eth_getStorageAt` can only return one 32-byte word. rskj stores some diff --git a/crates/rpc/src/server.rs b/crates/rpc/src/server.rs index 32c8f37..410ce34 100644 --- a/crates/rpc/src/server.rs +++ b/crates/rpc/src/server.rs @@ -331,6 +331,9 @@ async fn dispatch(state: &RpcState, req: JsonRpcRequest) -> JsonRpcResponse { "rsk_storageStatus" => admin::rsk_storage_status(id, &state.store, &state.epoch_store), "rsk_getStorageBytesAt" => rsk::rsk_get_storage_bytes_at(id, params, state), "rsk_getStorageNodes" => proof::rsk_get_storage_nodes(id, params, state), + "rsk_getStateNodes" => proof::rsk_get_state_nodes(id, params, state), + "rsk_getBlockNumberByHash" => rsk::rsk_get_block_number_by_hash(id, params, &state.store), + "rsk_getRawBlocks" => rsk::rsk_get_raw_blocks(id, params, &state.store), "rsk_getRawTransactionReceiptByHash" => { rsk::rsk_get_raw_transaction_receipt_by_hash(id, params, &state.store) } diff --git a/crates/rpc/src/tests.rs b/crates/rpc/src/tests.rs index eadfc3e..eea7f8f 100644 --- a/crates/rpc/src/tests.rs +++ b/crates/rpc/src/tests.rs @@ -6094,3 +6094,503 @@ async fn storage_nodes_prove_a_slot_that_is_not() { "eth_getProof still answers an absent slot with an empty array" ); } + +// ===== rsk_getBlockNumberByHash, rsk_getRawBlocks ===== + +/// Testnet #8,260,761 in full, as rustock's testnet node took it with its +/// block, which rskj hashes to 0x9a1cfe09...0d47 (core's `V2_FULL`). +const TESTNET_FULL: &str = concat!( + "f9029ba0285f7c3513c466f54fde5f1b73b2d93b35374515472192250bd3a56396dc649ea01dcc4de8dec75d7aab85b5", + "67b6ccd41ad312451b948a7413f0a142fd40d4934794b774aa2876145b2f6f3de27e5e6ac970aa12d771a04aa4b67c3e", + "30508692f13e9e10758d907e7d851234cd6cd00d65aa49e5a1ac44a0f40c99e8151d350e30309fa0a74234c365961aa0", + "81ad18e51a4281c513ec76d0a066cfdb731f620cd96e2c2cb0f7d3c3a2879c29b40014aa27efbbf3cf9cd3b0f6b90100", + "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "00000000000000000000000000000000844edfab4b837e0c998400b71b0080846aec1a5c92d1018f564554495645522d", + "3733316239643880835a74cf8080028080b8507111010000000000000000000000000000000000000000000000000000", + "00000000000000135534881310aada94b2b5867af37e4e2fc48be028c35beeaa7f1dc4cf501e825d1aec6affff0f1d0d", + "16caea80b85800000000000003c09e1ed89b4ab8f092a3dc3a4b852db4552dfd95136f298514ba947d076032a8f20bbe", + "a052534b424c4f434b3a6e7fa68f56e3bc6085e7e02e325250f1c72ad9eef1a0d7693e516e10007e0c9900000000", +); + +/// The same header compressed, as rskj sends a chain's headers to its peers +/// (core's `V2_COMPRESSED`). +const TESTNET_COMPRESSED: &str = concat!( + "f901b9a0285f7c3513c466f54fde5f1b73b2d93b35374515472192250bd3a56396dc649ea01dcc4de8dec75d7aab85b5", + "67b6ccd41ad312451b948a7413f0a142fd40d4934794b774aa2876145b2f6f3de27e5e6ac970aa12d771a04aa4b67c3e", + "30508692f13e9e10758d907e7d851234cd6cd00d65aa49e5a1ac44a0f40c99e8151d350e30309fa0a74234c365961aa0", + "81ad18e51a4281c513ec76d0a066cfdb731f620cd96e2c2cb0f7d3c3a2879c29b40014aa27efbbf3cf9cd3b0f6a3e202", + "a09aca8469839f117b1a26abbdea244a32cb0833e387cf6af9dc13eb336094d3c2844edfab4b837e0c998400b71b0080", + "846aec1a5c92d1018f564554495645522d3733316239643880835a74cf8080b850711101000000000000000000000000", + "000000000000000000000000000000000000000000135534881310aada94b2b5867af37e4e2fc48be028c35beeaa7f1d", + "c4cf501e825d1aec6affff0f1d0d16caea80b85800000000000003c09e1ed89b4ab8f092a3dc3a4b852db4552dfd9513", + "6f298514ba947d076032a8f20bbea052534b424c4f434b3a6e7fa68f56e3bc6085e7e02e325250f1c72ad9eef1a0d769", + "3e516e10007e0c9900000000", +); + +/// Testnet #8,260,720 as rskj's peers sent it to rustock's testnet node, +/// compressed (core's `V2_FROM_PEERS`). +const TESTNET_FROM_PEERS: &str = concat!( + "f901f9a00bdcd28d517b22bc7b65bf669b05924ac8ba8a862ccb4b17d619fdbd8b9789f8a01dcc4de8dec75d7aab85b5", + "67b6ccd41ad312451b948a7413f0a142fd40d4934794ad418c1d48780005f6d847ef0a5e3bd93ea09090a0c545101cc3", + "df462f9c369b22f722ec66ade0f031b528512f12fceca574d0ecc8a01db2709b879ae45385439bc35bc1ce8e821de8ae", + "b4a4e8ec9c94cf9f3dc55f7ea01ddfe2c6ff70e83e21bc8a905eb71278aab2fc921dd08543c20451a2ccbf286da3e202", + "a03c015999cbb7ab1fdb80e23b658a251e424af79a99262579f2fd59bd50fddc78844f476ad3837e0c708400b71b0082", + "e8f8846aec16be92d1018f564554495645522d37333162396438855a76a29740835a5a088080b85000000020785e2a8b", + "51231a7478b788a2427a2a4c190c0f4e418e25c4ca0c000000000000ba37f569d2f87d917d83e79d50af85497b7d35f0", + "ebe4e7c57ddd76e80fb58dfca130ec6af0ff0f1a007dc5fa80b8910000000000000080f880b614920083ca51e27b69b7", + "d6dba5262da24a347487e62e8406463eb94d6a6088ac0000000000000000266a24aa21a9ede2f61c3f71d1defd3fa999", + "dfa36953755c690689799962b48bebd836974e8cf900000000000000002a6a52534b424c4f434b3a3d8461c1058c9db8", + "352ad0c0b7e56a1ba4fcac4da0d7693e516ef40a007e0c7000000000", +); + +/// A header as the network reads one. +fn testnet_header(hex: &str) -> Header { + let bytes = hex::decode(hex).unwrap(); + Header::decode_with_hash(&mut bytes.as_slice()).unwrap() +} + +/// A header in full, as hex. +fn in_full(header: &Header) -> String { + use alloy_rlp::Encodable; + let mut out = Vec::new(); + header.encode(&mut out); + hex::encode(out) +} + +/// Puts `header` on the chain this node follows, and gives its hash. +fn follow(store: &BlockStore, header: &Header) -> B256 { + let hash = header.hash(); + store.put_header(header).unwrap(); + store.put_canonical_hash(header.number, hash).unwrap(); + hash +} + +/// Puts `build(number)` for each of `numbers` on the chain this node follows, +/// each naming the one before, and gives their hashes. +fn follow_run( + store: &BlockStore, + numbers: std::ops::RangeInclusive, + build: impl Fn(u64) -> Header, +) -> Vec { + let mut parent_hash = B256::repeat_byte(0x11); + numbers + .map(|number| { + parent_hash = follow( + store, + &Header { + parent_hash, + ..build(number) + }, + ); + parent_hash + }) + .collect() +} + +/// `rsk_getRawBlocks(from, to)`'s answer. +async fn raw_blocks(state: &RpcState, from: &str, to: &str) -> JsonRpcResponse { + dispatch_for_test(state, make_request("rsk_getRawBlocks", json!([from, to]))).await +} + +/// The blocks an answer of `rsk_getRawBlocks` gives: each header and its +/// uncles, as hex. +fn blocks_of(response: JsonRpcResponse) -> Vec<(String, Vec)> { + let hex = |v: &Value| v.as_str().unwrap().strip_prefix("0x").unwrap().to_string(); + response + .result + .expect("an answer") + .as_array() + .expect("an array of blocks") + .iter() + .map(|block| { + ( + hex(&block["header"]), + block["uncles"] + .as_array() + .unwrap() + .iter() + .map(hex) + .collect(), + ) + }) + .collect() +} + +/// The code of an error, if the answer is one. +fn refused(response: JsonRpcResponse) -> Option { + response.error.map(|e| e.code) +} + +/// A block's number on the chain this node follows, and on that chain alone. +#[tokio::test] +async fn block_number_by_hash_answers_for_the_chain_followed_alone() { + let (state, _tmp) = setup_state(); + let number = |hash: String| { + dispatch_for_test( + &state, + make_request("rsk_getBlockNumberByHash", json!([hash])), + ) + }; + let head = state.store.head().unwrap().unwrap(); + assert_eq!( + number(format!("{head:#x}")).await.result, + Some(json!("0x2a")) + ); + + // A block held only on a fork has none, nor has one not held. + let fork = Header { + extra_data: Bytes::from_static(b"fork"), + ..test_header(42) + }; + state.store.put_header(&fork).unwrap(); + assert_eq!( + number(format!("{:#x}", fork.hash())).await.result, + Some(Value::Null) + ); + let unheld = B256::repeat_byte(7); + assert_eq!( + number(format!("{unheld:#x}")).await.result, + Some(Value::Null) + ); + + // A hash is 32 bytes. + assert_eq!(refused(number("0x2a".into()).await), Some(INVALID_PARAMS)); +} + +/// Blocks `from` to `to` oldest first, or as many from `from` as the chain +/// holds; a version-0 header has one form. +#[tokio::test] +async fn raw_blocks_give_a_run_of_the_chain_oldest_first() { + let (state, _tmp) = setup_state(); + follow_run(&state.store, 100..=104, test_header); + let run: Vec<(String, Vec)> = (100..=104) + .map(|number| { + let hash = state.store.canonical_hash(number).unwrap().unwrap(); + let header = state.store.header(hash).unwrap().unwrap(); + assert_eq!( + header.encode_compressed(), + hex::decode(in_full(&header)).unwrap() + ); + (in_full(&header), Vec::new()) + }) + .collect(); + assert_eq!(blocks_of(raw_blocks(&state, "0x64", "0x68").await), run); + assert_eq!( + blocks_of(raw_blocks(&state, "0x65", "0x66").await), + run[1..3] + ); + assert_eq!( + blocks_of(raw_blocks(&state, "0x68", "0x68").await), + run[4..] + ); + assert_eq!( + blocks_of(raw_blocks(&state, "0x66", "0x70").await), + run[2..] + ); + assert!(blocks_of(raw_blocks(&state, "0x69", "0x70").await).is_empty()); + assert!(blocks_of(raw_blocks(&state, "0x68", "0x67").await).is_empty()); +} + +/// **A testnet header comes compressed**, as rskj sends a chain's headers to +/// its peers: one held in full is compressed, one held compressed comes as +/// held. +#[tokio::test] +async fn a_testnet_block_comes_with_its_header_compressed() { + let (state, _tmp) = setup_state(); + follow(&state.store, &testnet_header(TESTNET_FULL)); + assert_eq!( + blocks_of(raw_blocks(&state, "0x7e0c99", "0x7e0c99").await), + vec![(TESTNET_COMPRESSED.to_string(), Vec::new())] + ); + follow(&state.store, &testnet_header(TESTNET_FROM_PEERS)); + assert_eq!( + blocks_of(raw_blocks(&state, "0x7e0c70", "0x7e0c70").await), + vec![(TESTNET_FROM_PEERS.to_string(), Vec::new())] + ); +} + +/// **Uncles come in full**, the form their block's uncles hash covers, from +/// the block's body or, once that is gone, from the freezer. +#[tokio::test] +async fn raw_blocks_carry_uncles_in_full_from_the_body_or_the_freezer() { + use rustock_execution::processor::compute_ommers_hash; + + let (state, _tmp) = setup_state(); + let testnet = testnet_header(TESTNET_FULL); + let other = Header { + extra_data: Bytes::from_static(b"an uncle"), + ..test_header(99) + }; + let uncles = |number: u64| match number { + 101 => vec![testnet.clone()], + 102 => vec![other.clone()], + _ => Vec::new(), + }; + let hashes = follow_run(&state.store, 100..=102, |number| Header { + ommers_hash: compute_ommers_hash(&uncles(number)), + ..test_header(number) + }); + state.store.put_body(hashes[1], &[], &uncles(101)).unwrap(); + let dir = tempfile::tempdir().unwrap(); + let freezer = Arc::new(rustock_storage::freezer::Freezer::open(dir.path()).unwrap()); + freezer.put_uncles(102, &uncles(102)).unwrap(); + state.store.set_freezer(freezer); + + let blocks = blocks_of(raw_blocks(&state, "0x64", "0x66").await); + let carried: Vec> = blocks.into_iter().map(|(_, uncles)| uncles).collect(); + assert_eq!( + carried, + vec![ + Vec::new(), + vec![TESTNET_FULL.to_string()], + vec![in_full(&other)] + ] + ); +} + +/// **What comes is one run of the chain held whole**: the answer stops before +/// a block whose uncles the node doesn't hold, or holds only compressed, before +/// a gap, and before a block that doesn't follow the one before it. +#[tokio::test] +async fn raw_blocks_stop_before_a_block_not_held_whole_or_not_following() { + use rustock_execution::processor::compute_ommers_hash; + + let compressed = testnet_header(TESTNET_FROM_PEERS); + let naming = |at: u64, uncles: Vec
| { + move |number: u64| Header { + ommers_hash: if number == at { + compute_ommers_hash(&uncles) + } else { + test_header(number).ommers_hash + }, + ..test_header(number) + } + }; + + // #102 names uncles that the node doesn't hold. + let (state, _tmp) = setup_state(); + follow_run( + &state.store, + 100..=104, + naming(102, vec![compressed.clone()]), + ); + assert_eq!(blocks_of(raw_blocks(&state, "0x64", "0x68").await).len(), 2); + assert!(blocks_of(raw_blocks(&state, "0x66", "0x68").await).is_empty()); + + // It holds them only compressed, as no block carries an uncle. + let (state, _tmp) = setup_state(); + let hashes = follow_run( + &state.store, + 100..=104, + naming(102, vec![compressed.clone()]), + ); + state + .store + .put_body(hashes[2], &[], std::slice::from_ref(&compressed)) + .unwrap(); + assert_eq!(blocks_of(raw_blocks(&state, "0x64", "0x68").await).len(), 2); + + // A gap: the node doesn't follow any block at #102. + let (state, _tmp) = setup_state(); + let hashes = follow_run(&state.store, 100..=101, test_header); + let gap = Header { + parent_hash: hashes[1], + ..test_header(102) + }; + state.store.put_header(&gap).unwrap(); + let after = Header { + parent_hash: gap.hash(), + ..test_header(103) + }; + follow(&state.store, &after); + assert_eq!(blocks_of(raw_blocks(&state, "0x64", "0x67").await).len(), 2); + + // The chain moved at #102 while the answer was read. + let (state, _tmp) = setup_state(); + follow_run(&state.store, 100..=103, test_header); + follow( + &state.store, + &Header { + parent_hash: B256::repeat_byte(0x33), + ..test_header(102) + }, + ); + assert_eq!(blocks_of(raw_blocks(&state, "0x64", "0x67").await).len(), 2); + assert_eq!(blocks_of(raw_blocks(&state, "0x66", "0x67").await).len(), 1); +} + +/// At most a thousand blocks at a time, named as hex quantities. +#[tokio::test] +async fn raw_blocks_take_at_most_a_thousand_blocks_named_in_hex() { + let (state, _tmp) = setup_state(); + follow_run(&state.store, 0..=1000, test_header); + assert_eq!( + blocks_of(raw_blocks(&state, "0x0", "0x3e7").await).len(), + 1000 + ); + assert_eq!( + blocks_of(raw_blocks(&state, "0x1", "0x3e8").await).len(), + 1000 + ); + assert_eq!( + refused(raw_blocks(&state, "0x0", "0x3e8").await), + Some(INVALID_PARAMS) + ); + for (from, to) in [ + ("100", "0x1"), + ("0x", "0x1"), + ("0xg", "0x1"), + ("0x+1", "0x2"), + ("0x1", "2"), + ] { + assert_eq!( + refused(raw_blocks(&state, from, to).await), + Some(INVALID_PARAMS), + "{from} {to}" + ); + } + let alone = make_request("rsk_getRawBlocks", json!(["0x1"])); + assert_eq!( + refused(dispatch_for_test(&state, alone).await), + Some(INVALID_PARAMS) + ); +} + +// ===== rsk_getStateNodes ===== + +/// `rsk_getStateNodes(hash, keys)`'s answer. +async fn state_nodes(state: &RpcState, hash: B256, keys: &[Vec]) -> JsonRpcResponse { + let keys: Vec = keys + .iter() + .map(|key| format!("0x{}", hex::encode(key))) + .collect(); + dispatch_for_test( + state, + make_request("rsk_getStateNodes", json!([format!("{hash:#x}"), keys])), + ) + .await +} + +/// For each key, the walk along it from the block's state root: the code's +/// ends at the code, and a slot's, held or not, is the walk +/// `rsk_getStorageNodes` gives. +#[tokio::test] +async fn state_nodes_walk_each_key_from_the_blocks_state_root() { + use rustock_trie::{code_key, storage_key, TrieNode}; + + let (state, _tmp, addr, state_root) = setup_proof_state(); + let trie_store = state.trie_store.clone().unwrap(); + let head = state.store.head().unwrap().unwrap(); + let keys = [ + code_key(&addr), + storage_key(&addr, &B256::with_last_byte(1)), + storage_key(&addr, &B256::with_last_byte(3)), + ]; + let walks = state_nodes(&state, head, &keys).await.result.unwrap(); + let walks = walks.as_array().unwrap(); + assert_eq!(walks.len(), 3); + for walk in walks { + assert_hangs_from(walk, state_root, &*trie_store); + } + let deepest = hex::decode(walks[0][0].as_str().unwrap().trim_start_matches("0x")).unwrap(); + assert_eq!( + TrieNode::from_message(&deepest, &*trie_store).value, + Some(vec![0x60, 0x00, 0x60, 0x00]) + ); + for (walk, slot) in walks[1..].iter().zip(["0x1", "0x3"]) { + let req = make_request( + "rsk_getStorageNodes", + json!([format!("{addr:#x}"), [slot], "latest"]), + ); + let r = dispatch_for_test(&state, req).await.result.unwrap(); + assert_eq!(walk, &r["storage"][0]["nodes"], "slot {slot}"); + } +} + +/// Null where the node doesn't hold the block, or the state at its state +/// root, or any state at all. +#[tokio::test] +async fn state_nodes_are_null_where_the_state_isnt_held() { + let (mut state, _tmp, addr, _) = setup_proof_state(); + let keys = [rustock_trie::code_key(&addr)]; + assert_eq!( + state_nodes(&state, B256::repeat_byte(7), &keys) + .await + .result, + Some(Value::Null) + ); + let elsewhere = Header { + state_root: B256::repeat_byte(9), + ..test_header(43) + }; + state.store.put_header(&elsewhere).unwrap(); + assert_eq!( + state_nodes(&state, elsewhere.hash(), &keys).await.result, + Some(Value::Null) + ); + let head = state.store.head().unwrap().unwrap(); + state.trie_store = None; + assert_eq!( + state_nodes(&state, head, &keys).await.result, + Some(Value::Null) + ); +} + +/// **An incomplete state is an error, never an absence**: a walk it can't +/// finish proves nothing either way. +#[tokio::test] +async fn state_nodes_are_an_error_where_the_state_is_incomplete() { + let (mut state, _tmp, addr, state_root) = setup_proof_state(); + let whole = state.trie_store.clone().unwrap(); + let partial = Arc::new(rustock_trie::MemoryTrieStore::new()); + let root = whole.get(state_root.as_slice()).unwrap(); + rustock_trie::TrieStore::put(&*partial, state_root.as_slice(), &root); + state.trie_store = Some(partial); + let head = state.store.head().unwrap().unwrap(); + let error = state_nodes(&state, head, &[rustock_trie::code_key(&addr)]) + .await + .error + .unwrap(); + assert_eq!(error.code, INTERNAL_ERROR); + assert!(error.message.contains("incomplete"), "{}", error.message); +} + +/// At most 64 keys at a time, each of at most 128 bytes, as `0x` hex. +#[tokio::test] +async fn state_nodes_take_at_most_64_keys_of_at_most_128_bytes() { + let (state, _tmp, _, _) = setup_proof_state(); + let head = state.store.head().unwrap().unwrap(); + let most = state_nodes(&state, head, &vec![vec![0xab; 128]; 64]).await; + assert_eq!(most.result.unwrap().as_array().unwrap().len(), 64); + assert_eq!( + refused(state_nodes(&state, head, &vec![vec![0xab]; 65]).await), + Some(INVALID_PARAMS) + ); + assert_eq!( + refused(state_nodes(&state, head, &[vec![0xab; 129]]).await), + Some(INVALID_PARAMS) + ); + for keys in [ + json!(["0x123"]), + json!(["12"]), + json!(["0xzz"]), + json!([12]), + json!("0x12"), + ] { + let req = make_request("rsk_getStateNodes", json!([format!("{head:#x}"), keys])); + assert_eq!( + refused(dispatch_for_test(&state, req).await), + Some(INVALID_PARAMS), + "{keys}" + ); + } + let unnamed = make_request("rsk_getStateNodes", json!(["0x12", ["0x12"]])); + assert_eq!( + refused(dispatch_for_test(&state, unnamed).await), + Some(INVALID_PARAMS) + ); +} From 4c88f8645939cdaf9bbacf0adfbc6ba8adc7b434 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Fri, 6 Nov 2026 23:11:19 -0800 Subject: [PATCH 027/264] Take the node rskj's walk stops at, and ask just past where it stopped A state node longer than rskj's 51,200-byte chunk window is never one of a chunk's own nodes, wherever it is asked for: rskj keeps a node only if it ends inside the window, and leaves the first that doesn't on its stack, which it hands back top-first, the node whole. The client counted a chunk by its own nodes alone, so it never counted such a node. Asked at the cursor, rskj's walk began with the node that ends there; when that node has an embedded right child and no separate left one, the walk counts it from inside the child, never keeps a long one, and stops at it at once. Every answer was taken, the cursor didn't move, and each answer taken reset the count of fruitless ones: on testnet at #8,261,160 the download stood at 94% for hours. - The trie's rebuild places the node the walk stopped at (Rebuilt::stopped_at), and with none of the chunk's own, gives that node the first node's left hash: without it an account over its storage, with its code embedded in it, doesn't rebuild. - A chunk covers its own nodes and the node it stopped at, and the next request asks just past the cursor. What a slice asks next is one value, Ask: the cell, the cursor, or just past it. - Each slice keeps the offset it asked. An answer used to be matched by the cursor or the cell holding it, so a slice asking exactly took the answer of a slice that had asked for that cell, and the first answer's reshaping of the slices could leave a request no slice would match. - An answer taken that moves nothing counts as fruitless; 64 in a row end the session, as 64 refusals do, blaming no peer. Tests download from rskj servers through nodes longer than the window, tile three shapes of trie asking just past where each chunk stopped, place long nodes over a separate left child, give up on answers that move nothing, and take an answer to the slice that asked for it. docs/snapshot-sync.md says where a chunk stops. Each of 20 deliberate bugs fails a test. --- crates/sync/src/snap/client.rs | 150 ++++++++++------ crates/sync/src/snap/session.rs | 49 ++++-- crates/sync/src/snap/session_tests.rs | 62 ++++++- crates/sync/src/snap/tests.rs | 235 +++++++++++++++++++++++++- crates/trie/src/snapshot_legacy.rs | 118 ++++++++++--- crates/trie/tests/rskj_vectors.rs | 220 +++++++++++++++++++++++- docs/snapshot-sync.md | 42 ++++- 7 files changed, 760 insertions(+), 116 deletions(-) diff --git a/crates/sync/src/snap/client.rs b/crates/sync/src/snap/client.rs index 2dcf126..de7a26f 100644 --- a/crates/sync/src/snap/client.rs +++ b/crates/sync/src/snap/client.rs @@ -56,6 +56,31 @@ use std::sync::Mutex; use std::time::{Duration, Instant}; use tracing::{debug, info, trace}; +/// What a slice asks for next. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum Ask { + /// The cell holding the cursor. A cell is the unit a server can cache and + /// share; asking for the cursor exactly would give every client a + /// different range and every server a cache miss. + Cell, + /// The cursor itself, after a cell answer that did not move it -- a server + /// that caps its responses below the grid size will do that -- because + /// asking for the same cell again would produce the same answer forever. + /// Costs a cache miss at that server, which is strictly better than a + /// stall. + Cursor, + /// The offset just past the cursor, after every rskj chunk. An rskj server + /// answers any offset, starting with the node that holds it, so asking + /// just past the cursor picks up with the node that begins there, whatever + /// size the server serves. Asked at the cursor itself, it starts with the + /// node that ends there, which the last chunk brought; and when that node + /// has an embedded right child and no separate left one, rskj's walk sets + /// out from inside the child and counts the whole node from there, so a + /// node longer than the window is never kept, and the walk stops at it at + /// once: the same answer every time. + JustPast, +} + /// A contiguous stretch of the traversal that one worker walks front to back. #[derive(Debug, Clone, PartialEq, Eq)] struct Slice { @@ -66,24 +91,24 @@ struct Slice { cursor: u64, /// One past the last offset this slice is responsible for. end: u64, - /// Whether a request for `cursor` is outstanding. - in_flight: bool, - /// Ask for exactly `cursor` rather than the cell containing it. - /// - /// Set when a cell answer did not move the cursor -- a server that caps - /// its responses below the grid size will do that -- because asking for - /// the same cell again would produce the same answer forever. Costs a - /// cache miss at that server, which is strictly better than a stall. - /// - /// Set after every rskj chunk too. An rskj server answers any offset, - /// starting with the node that ends there, so asking for the cursor picks - /// up exactly where the last chunk stopped, whatever size it serves. - exact: bool, + /// What it asks for next. + next: Ask, + /// The offset of the request outstanding for this slice, if one is. + asked: Option, } impl Slice { fn wants_work(&self) -> bool { - !self.in_flight && self.cursor < self.end + self.asked.is_none() && self.cursor < self.end + } + + /// The offset this slice asks for next. + fn asks(&self, grid: u64) -> u64 { + match self.next { + Ask::Cell => (self.cursor / grid) * grid, + Ask::Cursor => self.cursor, + Ask::JustPast => self.cursor + 1, + } } } @@ -131,6 +156,10 @@ pub struct RebuiltChunk { /// Where the chunk's own nodes begin and end in the traversal, as the /// rebuild placed them. `None` when it carried none. pub span: Option<(u64, u64)>, + /// Where the node rskj's walk stopped at sits, once the rebuild has placed + /// it: the first that reached past the window, which rskj leaves out of + /// the chunk's own nodes and sends whole all the same. + pub stopped_at: Option<(u64, u64)>, } #[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] @@ -145,7 +174,7 @@ pub enum ChunkError { Oversized { asked: u64, got: u64 }, #[error("the chunk begins at offset {starts}, after the offset {asked} it answers")] Misplaced { asked: u64, starts: u64 }, - #[error("the chunk carried no nodes of its own")] + #[error("the chunk placed no node, of its own or where the walk stopped")] Empty, } @@ -221,7 +250,7 @@ impl StateDownload { .map(|i| { let start = from + step * i as u64; let end = if i + 1 == count { to } else { from + step * (i as u64 + 1) }; - Slice { start, cursor: start, end, in_flight: false, exact: false } + Slice { start, cursor: start, end, next: Ask::Cell, asked: None } }) .collect() } @@ -235,17 +264,13 @@ impl StateDownload { // The cap is on requests, not on slices. Those were the same number // until the trie turned out bigger than advertised and more slices // were cut; keeping the two separate means the limit stays the limit. - if self.slices.iter().filter(|s| s.in_flight).count() >= self.max_in_flight { + if self.slices.iter().filter(|s| s.asked.is_some()).count() >= self.max_in_flight { return None; } let grid = self.grid; let slice = self.slices.iter_mut().find(|s| s.wants_work())?; - slice.in_flight = true; - // Asked for on the grid, whatever the cursor. A cell is the unit a - // server can cache and share; asking for `cursor` exactly would give - // every client a different range and every server a cache miss. - let from = - if slice.exact { slice.cursor } else { (slice.cursor / grid) * grid }; + let from = slice.asks(grid); + slice.asked = Some(from); Some(ChunkRequest { from, budget: self.budget }) } @@ -256,7 +281,7 @@ impl StateDownload { /// verify. pub fn release(&mut self, from: u64) { if let Some(i) = self.slice_for(from) { - self.slices[i].in_flight = false; + self.slices[i].asked = None; } } @@ -268,26 +293,24 @@ impl StateDownload { /// server takes. pub fn realign(&mut self, from: u64) { if let Some(i) = self.slice_for(from) { - self.slices[i].in_flight = false; - self.slices[i].exact = false; + self.slices[i].asked = None; + self.slices[i].next = Ask::Cell; } } - /// The slice an answer for `from` belongs to. + /// The slice waiting on an answer for `from`. /// - /// A cell begins at or before the cursor that asked for it, because the - /// request was aligned down to the grid. An exact match wins over an - /// aligned one, so two slices inside the same cell cannot be confused. + /// Each slice keeps the offset it asked. It used to be worked out again + /// from the slice, as its cursor or the cell holding it, preferring a + /// slice whose cursor was `from`. But a slice asking exactly is inside + /// its cell too, and took the answer of a slice that had asked for that + /// cell: that one waited for an answer already spent, and the first + /// slice's own came back unsolicited, to be charged to the honest peer + /// that sent it. And the first answer can reshape the slices, moving a + /// cursor whose request is out. Two slices that asked the same offset get + /// the same answer, so either may take it. fn slice_for(&self, from: u64) -> Option { - let grid = self.grid; - self.slices - .iter() - .position(|s| s.in_flight && s.cursor == from) - .or_else(|| { - self.slices - .iter() - .position(|s| s.in_flight && (s.cursor / grid) * grid == from) - }) + self.slices.iter().position(|s| s.asked == Some(from)) } /// Check a chunk and, if it holds up, keep it. @@ -356,9 +379,13 @@ impl StateDownload { // An answer that moved nothing means this server serves less than // a whole cell, so the next request names the cursor exactly // rather than asking the same cell again and again. - slice.exact = reached <= slice.cursor; + slice.next = if reached <= slice.cursor { + Ask::Cursor + } else { + Ask::Cell + }; slice.cursor = reached.max(slice.cursor); - slice.in_flight = false; + slice.asked = None; } trace!( @@ -387,9 +414,21 @@ impl StateDownload { /// fetched. On mainnet that was 45.7% of the state, found only by the /// check after an hour's download, five attempts running. /// - /// The next request then names the cursor exactly: an rskj server - /// answers any offset, beginning with the node that ends there, so the - /// next chunk picks up where this one stopped. + /// The next request then names the offset just past the cursor: an rskj + /// server answers any offset, beginning with the node that holds it, so + /// the next chunk picks up with the node that begins where this one + /// stopped. + /// + /// What a chunk brings whole is its own nodes, and the node rskj's walk + /// stopped at when it follows them. rskj keeps a node among a chunk's own + /// only if it ends before `from + 50 * 1024`, and the first that does not + /// it sends whole all the same, which the rebuild has checked against the + /// root and placed. A node longer than that window is never among a + /// chunk's own, however it is asked for, and comes only this way: asked + /// where it begins, the chunk brings only the node before it, and asked + /// inside it, nothing of its own. Counted by its own nodes alone, + /// every answer was taken with the cursor where it was, and testnet's + /// download stood at 94% for hours, asking again. pub fn accept_rebuilt( &mut self, from: u64, @@ -406,9 +445,21 @@ impl StateDownload { self.adopt_total(chunk.total); } - // Nothing of its own is what rskj sends for an offset past the end. - // Whatever was asked, the grid is the next thing to try. - let Some((starts, reached)) = chunk.span else { + // Its own nodes and the node the walk stopped at, when that follows + // them; with none of its own, the node it stopped at is the one + // holding the offset asked. + let covered = match (chunk.span, chunk.stopped_at) { + (Some((starts, reached)), Some((begins, ends))) if begins == reached => { + Some((starts, ends)) + } + (None, stopped) => stopped, + (span, _) => span, + }; + // Nothing to count it for: none of its own, and the node it stopped + // at not placed, as a node beside two children both left out cannot + // be. Not a shape Rootstock's state has. Asked on the grid, the walk + // may begin elsewhere. + let Some((starts, reached)) = covered else { self.realign(from); return Err(ChunkError::Empty); }; @@ -437,8 +488,8 @@ impl StateDownload { .saturating_sub(slice.cursor) .min(slice.end.saturating_sub(slice.cursor)); slice.cursor = reached.max(slice.cursor); - slice.exact = true; - slice.in_flight = false; + slice.next = Ask::JustPast; + slice.asked = None; } trace!( @@ -645,6 +696,7 @@ pub fn chunk_from_payload( long_values: rebuilt.long_values, total: rebuilt.total, span: rebuilt.span, + stopped_at: rebuilt.stopped_at, })) } } diff --git a/crates/sync/src/snap/session.rs b/crates/sync/src/snap/session.rs index 8797a44..1d2efb4 100644 --- a/crates/sync/src/snap/session.rs +++ b/crates/sync/src/snap/session.rs @@ -1237,19 +1237,12 @@ impl SnapSession { super::client::Arrived::Rebuilt(rebuilt) => { let Some(download) = self.download.as_mut() else { return Vec::new() }; return match download.accept_rebuilt(from, &rebuilt) { - Ok(progress) => { - self.fruitless_chunks = 0; - if progress.complete { - self.finish_state_download() - } else { - self.poll() - } - } + Ok(progress) => self.taken(progress), Err(e) => { debug!(target: "rustock::snap", "rskj chunk from offset {from} refused: {e}"); self.chunk_fault = Some(match e { - // Nothing of its own: past the end, as rskj answers - // an offset there. About the offset, not the peer. + // Nothing it could be counted for: about the + // offset and the shape of the trie, not the peer. ChunkError::Empty => ChunkFault::Realign, other => ChunkFault::Misbehaved(other), }); @@ -1271,19 +1264,13 @@ impl SnapSession { let Some(download) = self.download.as_mut() else { return Vec::new() }; match download.accept(from, &proof) { - Ok(progress) => { - self.fruitless_chunks = 0; - if progress.complete { - return self.finish_state_download(); - } - } + Ok(progress) => self.taken(progress), Err(e) => { debug!(target: "rustock::snap", "chunk from offset {from} refused: {e}"); self.chunk_fault = Some(ChunkFault::Misbehaved(e)); - return self.fruitless(from); + self.fruitless(from) } } - self.poll() } /// An answer that added nothing: put the range back, and count it. @@ -1291,6 +1278,32 @@ impl SnapSession { if let Some(download) = self.download.as_mut() { download.release(from); } + self.no_progress() + } + + /// A chunk the download took: the last one, one that moved it on, or one + /// that moved it nothing. + /// + /// A chunk taken that moves nothing is asked for again and comes back the + /// same, so it counts as an answer that added nothing. Counted only by + /// whether it was taken, each one started the count again: a node longer + /// than rskj's window held testnet's download at 94% for hours that way, + /// without a word. Nothing is given back to the download, which ended the + /// request when it took the chunk. + fn taken(&mut self, progress: super::client::Progress) -> Vec { + if progress.complete { + return self.finish_state_download(); + } + if progress.advanced == 0 { + return self.no_progress(); + } + self.fruitless_chunks = 0; + self.poll() + } + + /// One more answer in a row that added nothing; at the limit, the session + /// stops and lets the caller try a different set of peers. + fn no_progress(&mut self) -> Vec { self.fruitless_chunks += 1; if self.fruitless_chunks >= FRUITLESS_CHUNKS_ALLOWED { return self.fail(SnapFailure::NoProgress(self.fruitless_chunks)); diff --git a/crates/sync/src/snap/session_tests.rs b/crates/sync/src/snap/session_tests.rs index 69597e3..eed322f 100644 --- a/crates/sync/src/snap/session_tests.rs +++ b/crates/sync/src/snap/session_tests.rs @@ -913,8 +913,8 @@ fn an_rskj_chunk_for_another_offset_is_charged() { } /// An rskj chunk with nothing of its own -- what rskj answers when the node at -/// the offset outruns its cell, or the offset is past the end -- is not held -/// against the peer. It is about the offset, and the range is asked again. +/// the offset outruns its cell -- is not held against the peer, and the node, +/// which rskj sends whole on the stack it hands back, is kept. #[test] fn an_rskj_chunk_with_nothing_of_its_own_is_not_charged() { let f = fixture(250); @@ -934,7 +934,51 @@ fn an_rskj_chunk_with_nothing_of_its_own_is_not_charged() { driver.on_chunk(id, server, ¬hing, Refusal::None, &peers); assert!(driver.take_blame().is_empty(), "charged a peer for the offset it was asked"); assert!(!driver.unhelpful().contains(&server), "stopped asking a peer that can serve"); - assert_eq!(driver.state_progress().0, 0); + assert!(driver.state_progress().0 > 0, "the node at the offset, sent whole, was not kept"); +} + +/// **Answers that move nothing end the session**, however well they rebuild. +/// Each was taken as progress and reset the count of answers in a row that +/// added nothing, so a session whose answers no longer moved it asked again +/// forever: testnet's stood at 94% for hours. They count now, and the session +/// gives up, as it does when every answer declines. +#[test] +fn answers_that_move_nothing_end_the_session() { + let f = fixture(250); + let genesis = f.with_genesis(); + let (blocks, tds) = chain(1, 3, genesis, 100, f.state_root); + let mut session = f.session(HeaderVerifier::new()); + session.on_status(&blocks, &tds, 5_000, 0); + let top = blocks.last().unwrap().header.number; + let mut actions = session.on_headers(top, &headers_for(&blocks, top, top as u32)); + assert_eq!( + session.phase(), + Phase::DownloadingState, + "{:?}", + session.failure() + ); + + // Every request gets the chunk for the trie's first 2,000 bytes: genuine, + // and taken, but after the first time it holds nothing not already held. + let first = rskj_chunk(&f, 0, 2_000); + let mut answers = 0; + while session.phase() == Phase::DownloadingState { + if actions.is_empty() { + actions = session.poll(); + } + let Some(Action::RequestChunk { from, .. }) = actions.pop() else { + panic!("nothing asked, and the session still downloading"); + }; + answers += 1; + assert!(answers < 1_000, "the session never gave up"); + actions.extend(session.on_chunk(from, &first, Refusal::None)); + } + assert!( + matches!(session.failure(), Some(SnapFailure::NoProgress(_))), + "{:?}", + session.failure() + ); + assert!(answers > 64, "gave up after {answers} answers"); } /// The chunk requests among a batch of outbound messages: id, offset, peer. @@ -948,9 +992,9 @@ fn chunk_requests(out: &[super::driver::Outbound]) -> Vec<(u64, u64, B512)> { .collect() } -/// After an rskj chunk the session asks for exactly where it stopped, which -/// only an rskj server takes; a server that refuses the offset as off its -/// grid gets the cell next, not the same offset again. +/// After an rskj chunk the session asks for the offset just past where it +/// stopped, which only an rskj server takes; a server that refuses the offset +/// as off its grid gets the cell next, not the same offset again. #[test] fn an_offset_refused_as_off_the_grid_is_asked_on_the_grid_next() { let f = fixture(250); @@ -972,8 +1016,8 @@ fn an_offset_refused_as_off_the_grid_is_asked_on_the_grid_next() { let exact = chunk_requests(&out); let &(id, asked, peer) = exact .iter() - .find(|(_, asked, _)| *asked == cursor) - .unwrap_or_else(|| panic!("nothing asked at the cursor {cursor}: {exact:?}")); + .find(|(_, asked, _)| *asked == cursor + 1) + .unwrap_or_else(|| panic!("nothing asked just past the cursor {cursor}: {exact:?}")); assert_ne!(asked % 100_000, 0, "the exact offset happens to be on the grid; the test proves nothing"); let refused = ChunkPayload::Proved { entries: Vec::new(), witness: Vec::new() }; @@ -985,7 +1029,7 @@ fn an_offset_refused_as_off_the_grid_is_asked_on_the_grid_next() { "the refused offset was not replaced by its cell: {next:?}" ); assert!( - next.iter().all(|(_, asked, _)| *asked != cursor), + next.iter().all(|(_, asked, _)| *asked != cursor + 1), "the refused offset was asked again: {next:?}" ); } diff --git a/crates/sync/src/snap/tests.rs b/crates/sync/src/snap/tests.rs index 69784ee..47beede 100644 --- a/crates/sync/src/snap/tests.rs +++ b/crates/sync/src/snap/tests.rs @@ -48,6 +48,32 @@ impl Peer { Self { root, store, root_hash } } + /// The trie [`toy_state`] makes, with the value of each account `long` + /// names made that many bytes long instead: the length a contract's code + /// or a value of the Bridge's storage can reach. + fn with_long_values(n: usize, long: &[(usize, usize)]) -> Self { + let store = Arc::new(MemoryTrieStore::new()); + let mut root = TrieNode::empty(); + for i in 0..n { + let key = [(i >> 8) as u8, i as u8, (i * 7) as u8, (i * 13) as u8]; + let value: Vec = match long.iter().find(|(at, _)| *at == i) { + Some(&(_, len)) => (0..len) + .map(|b| (b as u8).wrapping_mul(31).wrapping_add(i as u8)) + .collect(), + None if i % 5 == 0 => (0..96u8).map(|b| b.wrapping_add(i as u8)).collect(), + None => vec![i as u8, (i >> 8) as u8, 7], + }; + root = root.put(&TrieKeySlice::from_key(&key), &value, store.as_ref()); + } + root.save(store.as_ref(), true); + let root_hash = root.compute_hash(store.as_ref()); + Self { + root, + store, + root_hash, + } + } + /// Serves a grid cell and puts it through RLP, so the test sees what a /// real client would. `budget` is the grid size, as a server's is. fn serve(&self, from: u64, budget: u64) -> SnapChunkResponse { @@ -1643,6 +1669,113 @@ fn an_rskj_server_fills_every_cell_of_a_wider_grid() { assert!(asked <= cells + cells / 10 + 2 * 4, "{asked} requests for {cells} rskj cells"); } +/// **A node longer than rskj's cell is fetched.** +/// +/// rskj keeps a node among a chunk's own only if it ends before `from + 50 * +/// 1024`, so a node longer than that -- a contract's code, or a long value of +/// the Bridge's storage -- is never one of them. It is the node the walk stops +/// at, which rskj sends whole all the same, on top of the stack it hands back +/// after the chunk's own. Asked at the cursor before it, every answer was the +/// one node that ends there, taken and stored again with the cursor where it +/// was: testnet's download stood at 94% for hours, writing all the while. The +/// node the walk stopped at now counts as fetched when it follows the chunk's +/// own, and when the chunk carried nothing of its own, the node it stopped at +/// is the one holding the offset asked. +#[test] +fn a_node_longer_than_an_rskj_cell_is_fetched() { + use crate::snap::RSKJ_CHUNK_GRID; + use rustock_trie::snapshot::chunk_from; + + let long = [ + (2_000, 60_000), + (6_000, 150_000), + (6_001, 52_000), + (9_000, 400_000), + ]; + let peer = Peer::with_long_values(12_000, &long); + let stream = chunk_from(&peer.root, 0, u64::MAX, peer.store.as_ref()); + let longer: Vec<_> = stream.iter().filter(|n| n.span > RSKJ_CHUNK_GRID).collect(); + // Two of them side by side: a leaf that holds a long value names it by + // hash and length, small enough to embed, so both sit in one node's entry. + assert_eq!( + longer.len(), + 3, + "four long values make three nodes longer than rskj's cell" + ); + + // What rskj sends: asked where a long node begins, only the node that ends + // there; asked inside one, a cell or more from its end, nothing of its own. + for node in &longer { + let at_start = rebuilt(&peer, &rskj_serves(&peer, node.offset, RSKJ_CHUNK_GRID)); + assert_eq!( + at_start.span.map(|(_, end)| end), + Some(node.offset), + "asked at {}", + node.offset + ); + assert_eq!( + at_start.stopped_at, + Some((node.offset, node.end())), + "stopped at, asked at {}", + node.offset + ); + let inside = node.offset + node.span - RSKJ_CHUNK_GRID; + let within = rebuilt(&peer, &rskj_serves(&peer, inside, RSKJ_CHUNK_GRID)); + assert_eq!( + within.span, None, + "asked at {inside}, inside the node at {}", + node.offset + ); + assert_eq!( + within.stopped_at, + Some((node.offset, node.end())), + "stopped at, asked at {inside}" + ); + } + + // From the start, each long node is met at the cursor just before it. + let (client, local, _) = download_from_rskj(&peer, 100_000, RSKJ_CHUNK_GRID); + client + .verify_stored() + .expect("the downloaded state is whole"); + holds_everything(&peer, &local); + + // And a slice that begins inside the longest node asks for an offset the + // node holds, which rskj answers with nothing of its own. + let node = longer.iter().max_by_key(|n| n.span).expect("a long node"); + let inside = node.offset + node.span / 2; + let local = Arc::new(MemoryTrieStore::new()); + let cfg = SnapConfig { + chunk_grid: 1, + ..config(2, 100_000) + }; + let mut client = StateDownload::new(peer.root_hash, 2 * inside, &cfg, local.clone()); + let mut offsets = Vec::new(); + while !client.is_complete() { + let enough = 4 * (peer.total() / RSKJ_CHUNK_GRID) as usize + 64; + assert!( + offsets.len() < enough, + "{} requests and not done: stopped moving", + offsets.len() + ); + let request = client.next_request().expect("work left and none in flight"); + offsets.push(request.from); + let chunk = rebuilt(&peer, &rskj_serves(&peer, request.from, RSKJ_CHUNK_GRID)); + client + .accept_rebuilt(request.from, &chunk) + .expect("an honest rskj chunk"); + } + assert!( + offsets.contains(&inside), + "no slice began inside the node at {}", + node.offset + ); + client + .verify_stored() + .expect("the downloaded state is whole"); + holds_everything(&peer, &local); +} + /// **A short answer is not taken for the cell.** A server serving less than /// anything this client expects still leaves no hole: each answer advances /// the download by what it carried. @@ -1681,9 +1814,10 @@ fn an_rskj_chunk_that_begins_after_the_offset_asked_is_refused() { assert_eq!(client.next_request().map(|r| r.from), Some(request.from), "the range was not asked again"); } -/// **After an rskj chunk the next request names the cursor; refused, it goes -/// back to the grid.** An rskj server answers any offset and a rustock server -/// only its grid's, and the next request may go to either. +/// **After an rskj chunk the next request names the offset just past the +/// cursor; refused, it goes back to the grid.** An rskj server answers any +/// offset and a rustock server only its grid's, and the next request may go +/// to either. #[test] fn a_refused_exact_offset_is_asked_on_the_grid_next() { use crate::snap::RSKJ_CHUNK_GRID; @@ -1700,10 +1834,103 @@ fn a_refused_exact_offset_is_asked_on_the_grid_next() { assert!(cursor > 0 && cursor < 100_000, "an rskj chunk stops mid-cell, at {cursor}"); let exact = client.next_request().expect("more to fetch"); - assert_eq!(exact.from, cursor, "the next request does not pick up where the chunk stopped"); + assert_eq!( + exact.from, + cursor + 1, + "the next request does not ask just past where the chunk stopped" + ); client.realign(exact.from); let aligned = client.next_request().expect("more to fetch"); assert_eq!(aligned.from, 0, "a refused offset was asked again instead of the cell"); } +/// **An rskj chunk counts the node its walk stopped at**, which rskj sends +/// whole after the chunk's own nodes: the download moves to where that node +/// ends, and does not ask for it again. +#[test] +fn an_rskj_chunk_counts_the_node_its_walk_stopped_at() { + use crate::snap::RSKJ_CHUNK_GRID; + + let peer = Peer::new(3_000); + let local = Arc::new(MemoryTrieStore::new()); + let mut client = StateDownload::new(peer.root_hash, peer.total(), &config(1, 100_000), local); + + let first = client.next_request().expect("a request"); + let chunk = rebuilt(&peer, &rskj_serves(&peer, first.from, RSKJ_CHUNK_GRID)); + let (Some((_, own_end)), Some((begins, ends))) = (chunk.span, chunk.stopped_at) else { + panic!( + "not the chunk this test is about: {:?} {:?}", + chunk.span, chunk.stopped_at + ); + }; + assert_eq!( + begins, own_end, + "the walk stopped somewhere other than after the chunk's own" + ); + client + .accept_rebuilt(first.from, &chunk) + .expect("an honest chunk"); + assert_eq!( + client.progress().0, + ends, + "the node the walk stopped at was not counted" + ); + assert_eq!(client.next_request().map(|r| r.from), Some(ends + 1)); +} + +/// **An answer goes to the slice that asked for it**, when two slices ask +/// inside one cell: one for the cell, the other just past where its last +/// chunk stopped. The cell's offset used to match either, and the slice asking +/// exactly took the other's answer; the other waited on an answer already +/// spent, and the first's own came back unsolicited. +#[test] +fn an_answer_goes_to_the_slice_that_asked_for_it() { + let peer = Peer::new(1_000); + let total = peer.total(); + let local = Arc::new(MemoryTrieStore::new()); + let mut client = StateDownload::new(peer.root_hash, total, &config(2, 100_000), local); + + let (first, second) = ( + client.next_request().expect("a request"), + client.next_request().expect("another"), + ); + assert_eq!( + (first.from, second.from), + (0, 0), + "both slices begin in the first cell, of {total}" + ); + let chunk = rebuilt(&peer, &rskj_serves(&peer, 0, 2_000)); + client + .accept_rebuilt(first.from, &chunk) + .expect("the first slice's answer"); + let (reached, _) = client.progress(); + assert!( + reached > 0 && reached < total / 2, + "the first chunk reached {reached}, into the second slice" + ); + let exact = client.next_request().expect("the first slice asks again"); + assert_eq!(exact.from, reached + 1); + + // The second slice's answer, for the cell it asked; then the first's own. + let taken = client + .accept_rebuilt(second.from, &chunk) + .expect("the second slice's answer"); + assert_eq!( + taken.advanced, 0, + "the cell's first chunk ends before the second slice begins" + ); + let next = rebuilt(&peer, &rskj_serves(&peer, exact.from, 2_000)); + let taken = client + .accept_rebuilt(exact.from, &next) + .expect("the first slice's answer was taken by another"); + assert!(taken.advanced > 0); + assert_eq!(client.progress().0, reached + taken.advanced); + + // Both slices are free again: each asks just past its own cursor. + let asks = [client.next_request(), client.next_request()].map(|r| r.map(|r| r.from)); + assert_eq!( + asks, + [Some(reached + taken.advanced + 1), Some(total / 2 + 1)] + ); +} diff --git a/crates/trie/src/snapshot_legacy.rs b/crates/trie/src/snapshot_legacy.rs index 897f68f..e4184d7 100644 --- a/crates/trie/src/snapshot_legacy.rs +++ b/crates/trie/src/snapshot_legacy.rs @@ -484,6 +484,19 @@ pub struct Rebuilt { /// `span_of`. This is what a download advances by, rather than by the /// range it asked for, which an rskj server does not honour. pub span: Option<(u64, u64)>, + /// Where the node rskj's walk stopped at sits, from where it begins to + /// where it ends. `None` when the walk ran to the trie's end, or the node + /// cannot be placed. + /// + /// rskj keeps a node among a chunk's own only if it ends before the + /// window does, and leaves the first one that does not on its stack. It + /// sends that node whole all the same, first among the nodes still owed + /// their right subtree, and the rebuild has checked it against the root + /// with the rest. A node longer than the window -- a contract's code sits + /// inside its account's entry, and a value of the Bridge's storage can + /// pass 50 KiB -- is never among a chunk's own nodes, wherever it is + /// asked for. It only ever arrives this way. + pub stopped_at: Option<(u64, u64)>, } #[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] @@ -634,14 +647,41 @@ pub fn rebuild(chunk: &LegacyChunk, expected_root: B256) -> Result None, - n => { - let first = chunk.pre_root.len(); - Some(span_of(&stubs, &own, &left_of, &right_of, root_index, first, first + n - 1)?) + n => Some(span_of(&stubs, &own, &starts, first, first + n - 1)?), + }; + // rskj hands its stack back top-first, so the node it stopped at leads + // the nodes still owed their right subtree. + let stopped_at = if chunk.post_root.is_empty() { + None + } else { + let i = first + chunk.nodes.len(); + match starts[i] { + Some(at) => Some(( + at, + at.checked_add(entry(&stubs, &own, i)?) + .ok_or(RebuildError::Unplaced)?, + )), + None => None, } }; - Ok(Rebuilt { nodes, long_values, root, total, span }) + Ok(Rebuilt { + nodes, + long_values, + root, + total, + span, + stopped_at, + }) } /// Whether a node's left child is a node of its own: present, not embedded. @@ -654,8 +694,8 @@ fn separate_right(flags: u8) -> bool { flags & 0b0000_0100 != 0 && flags & 0b0000_0001 == 0 } -/// Where a chunk's own nodes sit in the traversal, and that nothing between -/// them was left out. +/// Where each node of the rebuilt tree begins in the traversal; `None` for a +/// node whose two children were both left out, which cannot be placed. /// /// rskj's format carries no offsets, and does not need to. The tree a chunk /// rebuilds to is the top of the real trie -- every node it carries, and the @@ -665,27 +705,15 @@ fn separate_right(flags: u8) -> bool { /// root places every carried node exactly, by the arithmetic of /// [`crate::snapshot::seek`]: a node's entry begins after its left branch, and /// its right branch after its entry. -/// -/// The nodes from `first` to `last` must then fill the stretch between them. -/// A subtree left out in the middle, its hash handed in as the last node's -/// left, still rebuilds to the right root; without this it is a stretch the -/// chunk appears to cover and does not carry, a hole a download finds only at -/// the end. -/// -/// Returns where the first node's entry begins and the last's ends. -fn span_of( +fn placements( stubs: &[Stub], own: &[u64], left_of: &[Option], right_of: &[Option], root: usize, - first: usize, - last: usize, -) -> Result<(u64, u64), RebuildError> { +) -> Result>, RebuildError> { let unplaced = || RebuildError::Unplaced; - // `total_size` and `stream_size`, from what the chunk carried. let total = |i: usize| stubs[i].children_size.checked_add(own[i]).ok_or_else(unplaced); - let entry = |i: usize| own[i].checked_add(stubs[i].embedded_total).ok_or_else(unplaced); let mut starts: Vec> = vec![None; stubs.len()]; // Each node, with the offset its subtree begins at. @@ -716,18 +744,51 @@ fn span_of( stack.push((l, base)); } if let Some(r) = right_of[i] { - stack.push((r, at.checked_add(entry(i)?).ok_or_else(unplaced)?)); + stack.push(( + r, + at.checked_add(entry(stubs, own, i)?).ok_or_else(unplaced)?, + )); } } + Ok(starts) +} +/// A node's entry in the traversal: its own bytes, and its embedded children, +/// which sit inside it. +fn entry(stubs: &[Stub], own: &[u64], i: usize) -> Result { + own[i] + .checked_add(stubs[i].embedded_total) + .ok_or(RebuildError::Unplaced) +} + +/// Where a chunk's own nodes sit in the traversal, as [`placements`] has them, +/// and that nothing between them was left out. +/// +/// The nodes from `first` to `last` must fill the stretch between them. A +/// subtree left out in the middle, its hash handed in as the last node's +/// left, still rebuilds to the right root; without this it is a stretch the +/// chunk appears to cover and does not carry, a hole a download finds only at +/// the end. +/// +/// Returns where the first node's entry begins and the last's ends. +fn span_of( + stubs: &[Stub], + own: &[u64], + starts: &[Option], + first: usize, + last: usize, +) -> Result<(u64, u64), RebuildError> { + let unplaced = || RebuildError::Unplaced; let start = starts[first].ok_or_else(unplaced)?; let end = starts[last] .ok_or_else(unplaced)? - .checked_add(entry(last)?) + .checked_add(entry(stubs, own, last)?) .ok_or_else(unplaced)?; let mut carried = 0u64; for i in first..=last { - carried = carried.checked_add(entry(i)?).ok_or_else(unplaced)?; + carried = carried + .checked_add(entry(stubs, own, i)?) + .ok_or_else(unplaced)?; } if end.checked_sub(start) != Some(carried) { return Err(unplaced()); @@ -763,6 +824,17 @@ fn assemble(chunk: &LegacyChunk) -> Result, RebuildError> { for (body, hash) in &chunk.post_root { out.push(parse_stub(body, None, *hash, out.len())?); } + + // A chunk that carried none of its own began its walk at the node holding + // the offset asked, and stopped there at once: that node leads the ones + // still owed their right subtree, and the first node's left hash is its. + // Left without it, a node with a separate left child -- a contract's + // account over its storage, with its code inside it -- cannot be rebuilt. + if chunk.nodes.is_empty() { + if let Some(stub) = out.get_mut(first) { + stub.given_left = chunk.first_left; + } + } Ok(out) } diff --git a/crates/trie/tests/rskj_vectors.rs b/crates/trie/tests/rskj_vectors.rs index a6cbcd0..1eb4334 100644 --- a/crates/trie/tests/rskj_vectors.rs +++ b/crates/trie/tests/rskj_vectors.rs @@ -441,9 +441,9 @@ fn rskj_chunks_place_themselves_where_the_trie_has_them() { assert!(placed >= 6, "only {placed} chunks placed"); } -/// **Asked for where the last one stopped, rskj's chunks tile the trie** -- -/// each beginning at or before that point, the last ending at the trie's -/// end -- which is how rustock's client asks for them. +/// **Asked for just past where the last one stopped, rskj's chunks tile the +/// trie** -- each beginning at or before that point, the last ending at the +/// trie's end -- which is how rustock's client asks for them. /// /// Not how rskj's own client asks. It steps by its cell, trusting the chunk /// for `k * cell` to begin where the one before ended, and rskj's iterator @@ -452,8 +452,8 @@ fn rskj_chunks_place_themselves_where_the_trie_has_them() { /// after it in the chunk is counted too far along and the chunk stops short /// of its cell. The nodes in the shortfall belong to no chunk at all. It /// happens at mainnet's 51,200-byte cells as well, so it is rskj's own snap -/// sync that loses them; asking at the cursor, nothing is lost: the next -/// answer begins with the node that ends there. +/// sync that loses them; asking just past the cursor, nothing is lost: the +/// next answer begins with the node that begins there. #[test] fn asked_where_they_stopped_rskj_chunks_tile_the_trie() { use rustock_trie::snapshot::chunk_from; @@ -485,7 +485,7 @@ fn asked_where_they_stopped_rskj_chunks_tile_the_trie() { assert_eq!(end, stream[k + chunk.nodes.len() - 1].end(), "cell {cell} at {asked}"); assert!(end > reached, "cell {cell} at {asked}: no progress"); reached = end; - asked = reached; + asked = reached + 1; } assert_eq!(reached, total, "cell {cell}: ran past the end"); @@ -503,7 +503,7 @@ fn asked_where_they_stopped_rskj_chunks_tile_the_trie() { } assert!( stepping_by_the_cell_loses, - "rskj's stepping lost nothing here, so this trie no longer shows why the client asks at its cursor" + "rskj's stepping lost nothing here, so this trie no longer shows why the client asks where it stopped" ); } @@ -786,3 +786,209 @@ fn every_long_value_a_chunk_names_comes_with_it() { } assert!(embedded > 0, "no embedded long value was exercised ({named} named in all)"); } + +/// The Rootstock-shaped trie of [`unitrie`], with long code under some +/// contracts and long values in some storage slots, longer than the windows +/// the tests below ask with: as long as a contract's code, or a value of the +/// Bridge's storage, is against rskj's 50 KiB. +fn unitrie_with_long_values(accounts: usize) -> (TrieNode, MemoryTrieStore) { + use alloy_primitives::{Address, B256}; + use rustock_trie::{account_key, code_key, storage_key}; + + let store = MemoryTrieStore::new(); + let mut root = TrieNode::empty(); + let put = |root: TrieNode, key: Vec, value: Vec| -> TrieNode { + root.put(&TrieKeySlice::from_key(&key), &value, &store) + }; + for i in 0..accounts { + let address = Address::from_word(B256::from(alloy_primitives::U256::from(i as u64 + 1))); + root = put( + root, + account_key(&address), + vec![0xc4, (i as u8).wrapping_add(1), 0x82, 0x03, 0xe8], + ); + if i % 3 == 0 { + continue; + } + let code_len = if i % 7 == 1 { + 9_000 + i * 13 + } else { + 40 + i % 90 + }; + let code: Vec = (0..code_len) + .map(|b| (b as u8).wrapping_mul(i as u8 | 1)) + .collect(); + root = put(root, code_key(&address), code); + for slot in 0..(i % 6) as u64 { + let len = if slot == 0 && i % 11 == 2 { + 12_000 + i * 7 + } else { + 1 + (slot as usize * 7) % 32 + }; + let value = vec![(slot as u8).wrapping_add(i as u8) | 1; len]; + root = put( + root, + storage_key(&address, &B256::from(alloy_primitives::U256::from(slot))), + value, + ); + } + } + root.save(&store, true); + (root, store) +} + +/// What an rskj chunk covers, as rustock's client counts it: its own nodes, +/// and the node its walk stopped at when that follows them; with none of its +/// own, the node it stopped at, which holds the offset asked. +fn covered(rebuilt: &rustock_trie::snapshot_legacy::Rebuilt) -> Option<(u64, u64)> { + match (rebuilt.span, rebuilt.stopped_at) { + (Some((start, end)), Some((begins, ends))) if begins == end => Some((start, ends)), + (None, stopped) => stopped, + (span, _) => span, + } +} + +/// **Asked just past where the last one stopped, rskj's chunks tile any +/// trie**, nodes longer than the window included, the way rustock's client +/// asks for them: just past the cursor, the walk begins with the node that +/// begins there, and each chunk covers what [`covered`] says. The node a walk +/// stopped at is always the one after the chunk's own, where rustock's +/// traversal has it. +#[test] +fn asked_just_past_where_they_stopped_rskj_chunks_tile_any_trie() { + use rustock_trie::snapshot::chunk_from; + use rustock_trie::snapshot_legacy::{legacy_chunk, rebuild}; + + let tries = [ + ( + "rskj's generator's", + trie(400), + vec![300u64, 777, 1024, 5000], + ), + ("Rootstock-shaped", unitrie(300), vec![300, 777, 1024, 5000]), + ( + "with long values", + unitrie_with_long_values(200), + vec![2_000, 5_000], + ), + ]; + for (name, (root, store), cells) in tries { + let root_hash = root.compute_hash(&store); + let root_bytes: [u8; 32] = root_hash.into(); + let total = total_size(&root, &store); + let stream = chunk_from(&root, 0, u64::MAX, &store); + for cell in cells.into_iter().chain([total]) { + let (mut reached, mut requests) = (0u64, 0); + while reached < total { + requests += 1; + assert!( + requests < 10_000, + "{name}, cell {cell}: no progress from {reached}" + ); + let asked = if reached == 0 { 0 } else { reached + 1 }; + let chunk = legacy_chunk(&root_bytes, asked, asked + cell, &store).expect("builds"); + let rebuilt = rebuild(&chunk, root_hash) + .unwrap_or_else(|e| panic!("{name}, cell {cell} at {asked}: {e}")); + if let Some((begins, ends)) = rebuilt.stopped_at { + let at = stream.iter().position(|n| n.offset == begins).unwrap_or_else(|| { + panic!("{name}, cell {cell} at {asked}: stopped at {begins}, where no node begins") + }); + assert_eq!( + stream[at].end(), + ends, + "{name}, cell {cell} at {asked}: where it ends" + ); + if let Some((_, end)) = rebuilt.span { + assert_eq!( + begins, end, + "{name}, cell {cell} at {asked}: not the node after the chunk's own" + ); + } + } + let (start, end) = covered(&rebuilt) + .unwrap_or_else(|| panic!("{name}, cell {cell} at {asked}: nothing at all")); + assert!( + start <= reached, + "{name}, cell {cell} at {asked}: a gap from {reached} to {start}" + ); + assert!(end > reached, "{name}, cell {cell} at {asked}: no progress"); + reached = end; + } + assert_eq!(reached, total, "{name}, cell {cell}: ran past the end"); + } + } +} + +/// **A node longer than the window arrives whole where the walk stops.** Asked +/// where it begins, rskj's chunk is only the node that ends there, when that +/// one is short; asked inside it, a window or more from its end, nothing of +/// its own. Either way it stopped at the long node, which leads the nodes it +/// hands back, and the rebuild places it. With nothing of its own, the long +/// node takes the first node's left hash, without which a contract's account +/// over its storage, with its long code inside it, would not rebuild. +#[test] +fn a_node_longer_than_the_window_arrives_whole_where_the_walk_stops() { + use rustock_trie::snapshot::chunk_from; + use rustock_trie::snapshot_legacy::{legacy_chunk, rebuild}; + use rustock_trie::NodeRef; + + let (root, store) = unitrie_with_long_values(200); + let root_hash = root.compute_hash(&store); + let root_bytes: [u8; 32] = root_hash.into(); + let stream = chunk_from(&root, 0, u64::MAX, &store); + let cell = 2_000u64; + + let (mut long, mut after_short, mut over_storage) = (0, 0, 0); + for (i, node) in stream + .iter() + .enumerate() + .filter(|(i, n)| *i > 0 && n.span > cell) + { + long += 1; + let parsed = TrieNode::from_message(&node.message, &store); + over_storage += usize::from(matches!(parsed.left, NodeRef::Hash(_))); + + if stream[i - 1].span < cell { + after_short += 1; + let at_start = + legacy_chunk(&root_bytes, node.offset, node.offset + cell, &store).expect("builds"); + let rebuilt = + rebuild(&at_start, root_hash).unwrap_or_else(|e| panic!("at {}: {e}", node.offset)); + assert_eq!( + rebuilt.span.map(|(_, end)| end), + Some(node.offset), + "asked at {}", + node.offset + ); + assert_eq!( + rebuilt.stopped_at, + Some((node.offset, node.end())), + "asked at {}", + node.offset + ); + } + + let inside = node.offset + node.span - cell; + let within = legacy_chunk(&root_bytes, inside, inside + cell, &store).expect("builds"); + assert!( + within.nodes.is_empty(), + "asked at {inside}, inside the node at {}", + node.offset + ); + let rebuilt = rebuild(&within, root_hash).unwrap_or_else(|e| panic!("at {inside}: {e}")); + assert_eq!(rebuilt.span, None); + assert_eq!( + rebuilt.stopped_at, + Some((node.offset, node.end())), + "asked at {inside}" + ); + } + assert!( + long >= 10 && after_short >= 5, + "only {long} nodes longer than the window, {after_short} after a short one" + ); + assert!( + over_storage > 0, + "no long node with a separate left child: the left hash went untested" + ); +} diff --git a/docs/snapshot-sync.md b/docs/snapshot-sync.md index 1a1e521..200c00b 100644 --- a/docs/snapshot-sync.md +++ b/docs/snapshot-sync.md @@ -651,11 +651,37 @@ state, found only by the completeness check after an hour's download, on five attempts running; `crates/cli/examples/snap_holes.rs` maps such holes, and put every missing byte past the first 51,200 of its cell. -So the client advances by step 6's span, and asks next for exactly where the -chunk stopped. rskj answers any offset, starting with the node that ends -there, so the next chunk picks up from that point whatever size the server -serves. A chunk that begins after the offset asked for is not an answer to it -and is charged; one with no nodes of its own is about the offset and is not. +So the client advances by what the chunk covers, and asks next for the offset +just past where it stopped. rskj answers any offset, starting with the node +that holds it, so the next chunk begins with the node that begins there, +whatever size the server serves. A chunk that begins after the offset asked +for is not an answer to it and is charged. + +What a chunk covers is its own nodes, placed by step 6, and the node rskj's +walk stopped at when that one follows them. rskj keeps a node among a chunk's +own only if it ends before `from + 50 * 1024`, and leaves the first that does +not on its stack, which it hands back top-first: that node arrives whole, +first among the nodes still owed their right subtree, and is rebuilt and +checked with the rest. A node longer than that window -- an account with its +contract's code embedded in it, or a long value of the Bridge's storage -- is +never among a chunk's own, wherever it is asked for, and arrives only this +way. Counted by its own nodes alone, a chunk asked where such a node began +brought only the node before it, and one asked inside it nothing of its own; +every answer was taken and none moved the cursor. On testnet at #8,261,160 the +download stood at 94% for hours. + +Asking at the cursor itself, as the client did once it stopped advancing by +the cell, stalls there too. rskj begins with the node that ends at the offset, +and when that node has an embedded right child and no separate left one, its +walk sets out from inside the child and counts the whole node from there. A +long node is then never kept, and the walk stops at it at once, with the same +answer every time. Just past the cursor, the walk begins with the node after +it. + +An answer the client takes that moves nothing counts as one that added +nothing. It did not before: each was taken, and taking one started the count +again, so the stall never ended the session. Sixty-four such answers in a row +end it, as sixty-four refusals do, and nobody is charged. Asking where the last chunk stopped also avoids a fault in rskj's own client, which steps by its cell and trusts the chunk for `k * cell` to begin where the @@ -665,7 +691,11 @@ the node, everything after it in that chunk is counted too far along, and the chunk stops short of its cell. The nodes in the shortfall belong to no chunk at all. `asked_where_they_stopped_rskj_chunks_tile_the_trie` in `crates/trie/tests/rskj_vectors.rs` shows both: rskj's stepping losing nodes, -and asking at the cursor losing none. +and asking just past where the last chunk stopped losing none. +`asked_just_past_where_they_stopped_rskj_chunks_tile_any_trie` shows it for +tries with nodes longer than the window, and +`a_node_longer_than_the_window_arrives_whole_where_the_walk_stops` where such +a node comes. The root also commits to the trie's size, so a rebuilt chunk settles the total as a proved one does, and a peer's hint is never the last word on where the From cffd6a4e73335ddd1507db79104e0dd0865bb90d Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Sat, 7 Nov 2026 02:26:39 -0800 Subject: [PATCH 028/264] Take the extension rskj sends beside a body, so testnet's blocks run From RSKIP-351 rskj sends a chain's headers to its peers compressed: a version-1 or version-2 header holds, in its bloom's slot, only RLP([version, keccak(extension)]), and the extension -- the block's logs bloom, execution edges and base event -- comes beside the block's body, as the third element of BodyResponseMessage. DownloadingBodiesSyncState.newBody sets it on the header before the block is built and run. rustock's decoder dropped it, so a block whose header came compressed ran against an empty bloom: after its snapshot the testnet node executed #8,261,161 and stopped at #8,261,162 on "logs bloom mismatch", its receipts root matching. Not even an empty block's bloom is empty, since REMASC logs each fee it pays. - A BodyResponse carries the extension, framed as rskj frames it: a body of exactly three elements has one, read whole as BlockHeaderExtension.fromEncoded reads it, and a third that isn't one is refused; a body of any other length has none. - Header::with_extension completes a header that holds only the commitment with the extension it names, once accepts_extension holds and the bloom is 256 bytes long. The hash stays the same, and the header keeps its own merged-mining fields. Header::extension gives the extension a header holds, and none for one that holds only the commitment. - The sync takes a body for such a header only with that extension (body_check::completed_header). The completed header is stored before the body, and the batch, or follow mode's buffer, runs it. A body without the extension, or with another, is refused as a body that doesn't match its header is, and asked of another peer. - Such a header's body is neither derived from the header nor filled from a local block source, which has no extension to give. One already stored beside it -- derived before this -- is dropped when its batch starts or follow mode reaches it, and asked for again. - A served body carries its header's extension, as NodeBlockProcessor.processBodyRequest sends it. A header holding only the commitment has none to send, so its body isn't served. - BlockStore::delete_body. rskj sets whatever extension comes and lets execution find out; this takes only the one the block's hash covers. The blocks a snapshot fetches below its checkpoint still keep the walk's compressed headers. Tests complete testnet #8,260,761 and versioned headers of both versions from their compressed form, byte for byte and hash for hash, and refuse another bloom, another version and a short bloom; frame bodies with and without an extension against rskj's framing; and take, refuse and ask again for bodies in a batch and in follow mode, drop stale bodies, and serve the extension. Each of 29 deliberate bugs fails a test. --- crates/core/src/types/header.rs | 189 +++++++++++- crates/networking/src/protocol/rsk.rs | 196 +++++++++++-- crates/storage/src/lib.rs | 7 + crates/sync/src/body_check.rs | 91 ++++++ crates/sync/src/events.rs | 4 + crates/sync/src/handler.rs | 14 +- crates/sync/src/service.rs | 157 ++++++++-- crates/sync/src/tests.rs | 403 +++++++++++++++++++++++++- 8 files changed, 978 insertions(+), 83 deletions(-) diff --git a/crates/core/src/types/header.rs b/crates/core/src/types/header.rs index ae1ed5f..33ff31f 100644 --- a/crates/core/src/types/header.rs +++ b/crates/core/src/types/header.rs @@ -198,12 +198,10 @@ impl Header { /// literal bloom commits to no extension, and accepting one would let a /// peer attach edges or a base event to a block that never had them. /// - /// Nothing calls this today, deliberately. An extension that arrives - /// separately from its header -- in a body response, say -- is discarded - /// rather than checked, because the fields are bound to the block only - /// while they travel inside the header. This is the gate any future code - /// that does accept one must pass through; see the body decoder in - /// `rustock-networking` for the other end of the rule. + /// This is the gate an extension that arrives apart from its header must + /// pass: the fields are bound to the block by the commitment its hash + /// covers, so one that doesn't match it is not the block's. + /// [`Header::with_extension`] passes through it. pub fn accepts_extension( &self, extension: &crate::types::header_extension::BlockHeaderExtension, @@ -218,24 +216,80 @@ impl Header { /// is derived from the bloom, edges and base event the header already /// holds -- keeping a second copy would let the two disagree. fn compressed_extension_field(&self) -> Option> { + let extension = self.literal_extension()?; + let mut out = Vec::new(); + extension + .create_extension_data() + .as_slice() + .encode(&mut out); + Some(out) + } + + /// The extension the fields of a versioned header make up: its bloom, + /// edges and base event, whether or not the header also holds the + /// commitment to them. + fn literal_extension(&self) -> Option { use crate::types::header_extension::BlockHeaderExtension; let v = self.version_fields.as_ref()?; let bloom = self.logs_bloom.as_slice().to_vec(); - let extension = match v.version { - 1 => BlockHeaderExtension::V1 { logs_bloom: bloom, edges: v.edges.clone() }, + match v.version { + 1 => Some(BlockHeaderExtension::V1 { + logs_bloom: bloom, + edges: v.edges.clone(), + }), // A V2 extension always has a base event; a header that omitted // the field still commits to an empty one, which is what rskj // substitutes when it accepts the short form. - 2 => BlockHeaderExtension::V2 { + 2 => Some(BlockHeaderExtension::V2 { logs_bloom: bloom, edges: v.edges.clone(), base_event: Some(v.base_event.as_ref().map(|b| b.to_vec()).unwrap_or_default()), - }, - _ => return None, - }; - let mut out = Vec::new(); - extension.create_extension_data().as_slice().encode(&mut out); - Some(out) + }), + _ => None, + } + } + + /// The RSKIP-351 extension this header holds, as rskj's `getExtension` + /// gives it to send beside the block's body: its bloom, edges and base + /// event. `None` for a version-0 header, which has none, and for one + /// that holds only the commitment to its extension, as headers sent + /// compressed do until a body brings it. + pub fn extension(&self) -> Option { + if self.extension_data.is_some() { + return None; + } + self.literal_extension() + } + + /// This header with the extension its commitment names, as rskj takes the + /// one a peer sends beside a block's body (`DownloadingBodiesSyncState` + /// `.newBody`, `header.setExtension`) before the block is checked and run: + /// its bloom, edges and base event in place of the commitment. They hash + /// back to the commitment, so the header's hash is the same. + /// + /// `None` unless this header holds only the commitment, as a compressed + /// header does, and `extension` is the one it names, with a bloom of 256 + /// bytes. rskj sets whatever it is sent and lets execution find out; this + /// takes only the extension the block's hash covers, which for every + /// block a miner built is the one an honest peer sends. + pub fn with_extension( + &self, + extension: &crate::types::header_extension::BlockHeaderExtension, + ) -> Option
{ + if self.extension_data.is_none() || !self.accepts_extension(extension) { + return None; + } + let logs_bloom = <[u8; 256]>::try_from(extension.logs_bloom()).ok()?; + Some(Header { + logs_bloom: Bloom::from(logs_bloom), + extension_data: None, + version_fields: Some(HeaderVersionFields { + version: extension.version(), + edges: extension.edges().map(<[i16]>::to_vec), + base_event: extension.base_event().map(Bytes::copy_from_slice), + }), + ..self.clone() + }) } fn encode_base_fields(&self, list: &mut Vec, compressed: bool) { @@ -1370,6 +1424,111 @@ mod versioned_wire_header_tests { assert_eq!(v0.encode_compressed(), wire(&v0)); } + /// **The extension beside a body.** A header read compressed, as rskj's + /// peers send a chain's headers, holds only the commitment to its + /// extension. Given the extension it names, as rskj sets the one sent + /// beside the block's body, it is the header the network sends whole: the + /// same wire form byte for byte, the same hashes, the same extension. It + /// takes no other -- another bloom, another version, a bloom not 256 bytes + /// long -- and a header that holds its own fields, or none, takes none. + #[test] + fn a_compressed_header_takes_the_extension_its_commitment_names() { + use crate::types::header_extension::BlockHeaderExtension; + let wire = |header: &Header| { + let mut out = Vec::new(); + header.encode(&mut out); + out + }; + for full_form in [V1_WIRE, V2_WIRE, V2_FULL] { + let full = from_the_network(full_form); + let extension = full + .extension() + .expect("a versioned header's own extension"); + let compressed = + from_the_network(&alloy_primitives::hex::encode(full.encode_compressed())); + assert!(compressed.extension_data.is_some()); + assert_eq!(compressed.extension(), None, "only the commitment"); + let mut both = compressed.clone(); + both.version_fields = full.version_fields.clone(); + assert_eq!( + both.extension(), + None, + "its hash commits through the commitment alone" + ); + assert_eq!(compressed.hash(), full.hash()); + + let completed = compressed + .with_extension(&extension) + .expect("the one it names"); + assert_eq!(wire(&completed), wire(&full), "#{}", full.number); + assert_eq!(completed.extension(), Some(extension.clone())); + let derived = Header { + cached_hash: None, + cached_hash_for_merged_mining: None, + ..completed.clone() + }; + assert_eq!( + (derived.hash(), derived.hash_for_merged_mining()), + (full.hash(), full.hash_for_merged_mining()) + ); + + let mut bloom = extension.logs_bloom().to_vec(); + bloom[0] ^= 1; + let edges = extension.edges().map(<[i16]>::to_vec); + let others = [ + match &extension { + BlockHeaderExtension::V1 { .. } => BlockHeaderExtension::V1 { + logs_bloom: bloom, + edges: edges.clone(), + }, + BlockHeaderExtension::V2 { base_event, .. } => BlockHeaderExtension::V2 { + logs_bloom: bloom, + edges: edges.clone(), + base_event: base_event.clone(), + }, + }, + match &extension { + BlockHeaderExtension::V1 { logs_bloom, .. } => BlockHeaderExtension::V2 { + logs_bloom: logs_bloom.clone(), + edges: edges.clone(), + base_event: Some(Vec::new()), + }, + BlockHeaderExtension::V2 { logs_bloom, .. } => BlockHeaderExtension::V1 { + logs_bloom: logs_bloom.clone(), + edges, + }, + }, + ]; + for other in &others { + assert_eq!(compressed.with_extension(other), None, "#{}", full.number); + } + assert_eq!( + full.with_extension(&extension), + None, + "it holds its own fields" + ); + } + + // A bloom that isn't 256 bytes long, though the commitment names it. + let short = BlockHeaderExtension::V2 { + logs_bloom: vec![0; 255], + edges: None, + base_event: Some(Vec::new()), + }; + let mut committed = from_the_network(V2_COMPRESSED); + let mut slot = Vec::new(); + short.create_extension_data().as_slice().encode(&mut slot); + committed.extension_data = Some(Bytes::from(slot)); + assert!(committed.accepts_extension(&short)); + assert_eq!(committed.with_extension(&short), None); + + // A version-0 header has none, and takes none. + let mut v0 = decode(V1_WIRE); + v0.version_fields = None; + assert_eq!(v0.extension(), None); + assert_eq!(v0.with_extension(&short), None); + } + /// A header decoded without caching either hash, so `hash()` and /// `hash_for_merged_mining()` derive them by encoding: the path a header /// built here takes. diff --git a/crates/networking/src/protocol/rsk.rs b/crates/networking/src/protocol/rsk.rs index 4338176..a5fa3a8 100644 --- a/crates/networking/src/protocol/rsk.rs +++ b/crates/networking/src/protocol/rsk.rs @@ -1,6 +1,7 @@ use alloy_rlp::{Decodable, Encodable, RlpDecodable, RlpEncodable, Header as RlpHeader}; use alloy_primitives::{B256, Bytes, U256}; use rustock_core::{Header, Transaction}; +use rustock_core::types::header_extension::BlockHeaderExtension; use rustock_core::rlp_compat::{decode_u8_lenient, decode_u64_lenient, decode_u256_lenient, decode_u32_lenient}; use super::snap; @@ -241,6 +242,12 @@ pub struct BodyResponse { pub id: u64, pub transactions: Vec, pub uncles: Vec
, + /// The RSKIP-351 extension of the block's header -- its logs bloom, + /// execution edges and base event -- which rskj sends beside the body of + /// a version-1 or version-2 block (`BodyResponseMessage`), whose header + /// goes to peers holding only a commitment to it. `None` for a version-0 + /// block. + pub extension: Option, } /// Request the hash of the block at a given height (type 8). @@ -601,7 +608,8 @@ impl RskSubMessage { out.extend_from_slice(¶ms); } RskSubMessage::BodyResponse(r) => { - // RLP([id, RLP([txs_list, uncles_list])]) + // RLP([id, RLP([txs_list, uncles_list, extension?])]), the + // extension as rskj's `BlockHeaderExtension.toEncoded` writes it. let mut txs_payload = Vec::new(); for tx in &r.transactions { // As in `encode_block`: serve the bytes the header commits @@ -620,11 +628,17 @@ impl RskSubMessage { RlpHeader { list: true, payload_length: uncles_payload.len() }.encode(&mut uncles_list); uncles_list.extend_from_slice(&uncles_payload); - let body_len = txs_list.len() + uncles_list.len(); + let extension = r + .extension + .as_ref() + .map(BlockHeaderExtension::to_encoded) + .unwrap_or_default(); + let body_len = txs_list.len() + uncles_list.len() + extension.len(); let mut body = Vec::new(); RlpHeader { list: true, payload_length: body_len }.encode(&mut body); body.extend_from_slice(&txs_list); body.extend_from_slice(&uncles_list); + body.extend_from_slice(&extension); let mut params = Vec::new(); r.id.encode(&mut params); @@ -869,35 +883,51 @@ impl Decodable for RskMessage { // Decode uncles list let uncles_h = RlpHeader::decode(&mut body_data)?; let mut uncles_body = &body_data[..uncles_h.payload_length]; + body_data = &body_data[uncles_h.payload_length..]; let mut uncles = Vec::new(); while !uncles_body.is_empty() { uncles.push(Header::decode_with_hash(&mut uncles_body)?); } - // The optional BlockHeaderExtension is skipped, and that is a - // rule rather than a convenience. + // rskj `MessageType.BODY_RESPONSE`: a third element is the + // block header's RSKIP-351 extension, which + // `BlockHeaderExtension.fromEncoded` reads whole, and a body of + // any other length has none. // - // From RSKIP-351 a header can carry its bloom, execution - // edges and base event as a commitment -- `RLP[version, - // keccak(extension)]` -- instead of the literal fields. The - // block hash covers that commitment, so those fields are - // bound to the block only while they travel *inside* the - // header, which is the form this decoder receives and the - // form `Header::encode_base_fields` hashes. - // - // An extension delivered here arrives beside the header with - // nothing tying it to that commitment. Attaching it would - // leave the block holding fields no miner committed to while - // its hash still matched, and a block stored that way would - // re-decode to a different hash than it was filed under. - // - // So: this is discarded because it is unbound, not because it - // is unneeded. If a later change does need it -- serving - // light clients, or filling a bloom without executing -- it - // must first pass `Header::accepts_extension`, which exists - // for this and is otherwise unused. + // From RSKIP-351 a header goes to peers with its bloom, execution + // edges and base event folded into a commitment -- + // `RLP[version, keccak(extension)]` -- in the bloom's slot, and the + // extension comes here, beside the block's body. It travels apart + // from the header, so nothing in this message binds it to the + // block: the sync takes it only once it matches the commitment + // that the header's hash covers (`Header::with_extension`), and a + // header that holds its own fields takes none. + let mut rest = Vec::new(); + while !body_data.is_empty() { + let mut item = body_data; + let item_h = RlpHeader::decode(&mut item)?; + let length = body_data.len() - item.len() + item_h.payload_length; + let element = body_data + .get(..length) + .ok_or(alloy_rlp::Error::InputTooShort)?; + rest.push(element); + body_data = &body_data[length..]; + } + let extension = match rest.as_slice() { + [extension] => { + Some(BlockHeaderExtension::from_encoded(extension).map_err(|_| { + alloy_rlp::Error::Custom("malformed block header extension") + })?) + } + _ => None, + }; - RskSubMessage::BodyResponse(BodyResponse { id, transactions, uncles }) + RskSubMessage::BodyResponse(BodyResponse { + id, + transactions, + uncles, + extension, + }) } 6 => { // NewBlockHashes: RLP list of [hash, number] pairs @@ -1070,6 +1100,7 @@ mod tests { id: 7, transactions: vec![tx], uncles: Vec::new(), + extension: None, })); msg.encode(&mut buf); let decoded = RskMessage::decode(&mut buf.as_slice()).expect("decodes"); @@ -1348,6 +1379,7 @@ mod tests { id: 55, transactions: vec![], uncles: vec![], + extension: None, }; let msg = RskMessage::new(RskSubMessage::BodyResponse(resp)); @@ -1387,6 +1419,7 @@ mod tests { id: 77, transactions: vec![tx], uncles: vec![], + extension: None, }; let msg = RskMessage::new(RskSubMessage::BodyResponse(resp)); @@ -1462,6 +1495,7 @@ mod tests { id: 100, transactions: transactions.clone(), uncles: uncles.clone(), + extension: None, }; let msg = RskMessage::new(RskSubMessage::BodyResponse(resp)); @@ -1527,6 +1561,7 @@ mod tests { id: 88, transactions: vec![], uncles: vec![uncle], + extension: None, }; let msg = RskMessage::new(RskSubMessage::BodyResponse(resp)); @@ -1547,6 +1582,119 @@ mod tests { } } + /// A body response with these elements after its id, framed as rskj frames + /// a message: `RLP([RLP([15, RLP_String(RLP([id, RLP(elements)]))])])`. + fn body_frame(id: u64, elements: &[Vec]) -> Vec { + let wrap = |list: bool, payload: &[u8]| { + let mut out = Vec::new(); + RlpHeader { + list, + payload_length: payload.len(), + } + .encode(&mut out); + out.extend_from_slice(payload); + out + }; + let mut params = Vec::new(); + id.encode(&mut params); + params.extend_from_slice(&wrap(true, &elements.concat())); + let mut message = Vec::new(); + 15u8.encode(&mut message); + message.extend_from_slice(&wrap(false, &wrap(true, ¶ms))); + wrap(true, &wrap(true, &message)) + } + + /// **The extension beside a body.** rskj sends a version-1 or version-2 + /// block's header extension as the body's third element + /// (`BodyResponseMessage`), as `BlockHeaderExtension.toEncoded` writes it, + /// and reads one only from a body of exactly three + /// (`MessageType.BODY_RESPONSE`), refusing a third that isn't one. + #[test] + fn a_body_carries_its_header_extension_as_rskj_sends_it() { + let empty = vec![0xc0]; + let mut bloom = vec![0u8; 256]; + bloom[3] = 0x40; + let extensions = [ + BlockHeaderExtension::V1 { + logs_bloom: bloom.clone(), + edges: Some(vec![2, 5]), + }, + BlockHeaderExtension::V2 { + logs_bloom: bloom.clone(), + edges: None, + base_event: Some(vec![0xbe; 9]), + }, + BlockHeaderExtension::V2 { + logs_bloom: bloom, + edges: Some(vec![]), + base_event: Some(Vec::new()), + }, + ]; + for extension in extensions { + let msg = RskMessage::new(RskSubMessage::BodyResponse(BodyResponse { + id: 9, + transactions: vec![], + uncles: vec![], + extension: Some(extension.clone()), + })); + let mut buf = Vec::new(); + msg.encode(&mut buf); + assert_eq!( + buf, + body_frame(9, &[empty.clone(), empty.clone(), extension.to_encoded()]) + ); + match RskMessage::decode(&mut buf.as_slice()) + .expect("decodes") + .sub_message + { + RskSubMessage::BodyResponse(r) => assert_eq!(r.extension, Some(extension)), + other => panic!("decoded as {other:?}"), + } + } + + // Without one, as for a version-0 block, the body has two elements. + let msg = RskMessage::new(RskSubMessage::BodyResponse(BodyResponse { + id: 9, + transactions: vec![], + uncles: vec![], + extension: None, + })); + let mut buf = Vec::new(); + msg.encode(&mut buf); + assert_eq!(buf, body_frame(9, &[empty.clone(), empty.clone()])); + + // With a fourth element, rskj reads no extension at all. + let extension = BlockHeaderExtension::V1 { + logs_bloom: vec![0; 256], + edges: None, + } + .to_encoded(); + let four = body_frame( + 9, + &[empty.clone(), empty.clone(), extension.clone(), extension], + ); + match RskMessage::decode(&mut four.as_slice()) + .expect("decodes") + .sub_message + { + RskSubMessage::BodyResponse(r) => assert_eq!(r.extension, None), + other => panic!("decoded as {other:?}"), + } + + // A string, an unknown version, a list of one: none is an extension. + for bad in [ + vec![0x83, 1, 2, 3], + vec![0xc2, 0x03, 0x80], + vec![0xc1, 0x01], + ] { + let frame = body_frame(9, &[empty.clone(), empty.clone(), bad.clone()]); + assert!( + RskMessage::decode(&mut frame.as_slice()).is_err(), + "{bad:02x?}" + ); + } + } + #[test] fn test_rsk_message_rlp_transactions_empty() { let msg = RskMessage::new(RskSubMessage::Transactions(vec![])); diff --git a/crates/storage/src/lib.rs b/crates/storage/src/lib.rs index 5402ec8..2e43ee2 100644 --- a/crates/storage/src/lib.rs +++ b/crates/storage/src/lib.rs @@ -1126,6 +1126,13 @@ impl BlockStore { .context("Failed to write block body") } + /// Removes a block's body, so that the sync asks for it again. + pub fn delete_body(&self, hash: B256) -> Result<()> { + self.db + .delete_cf(self.cf(CF_BODIES)?, hash.as_slice()) + .context("Failed to delete block body") + } + /// Attaches a freezer, so header runs can be served from flat files. pub fn set_freezer(&self, freezer: Arc) { *self.freezer.write().unwrap() = Some(freezer); diff --git a/crates/sync/src/body_check.rs b/crates/sync/src/body_check.rs index be8c61b..4c4be8f 100644 --- a/crates/sync/src/body_check.rs +++ b/crates/sync/src/body_check.rs @@ -10,6 +10,7 @@ //! Cheap, too: two hashes against a body already in memory, versus discovering //! the same thing after execution has built a state from it. +use rustock_core::types::header_extension::BlockHeaderExtension; use rustock_core::{Header, Transaction}; /// What about this body fails to match the header, if anything. @@ -44,6 +45,32 @@ pub(crate) fn body_mismatch( None } +/// The header to keep with a body that fits it, and the RSKIP-351 extension +/// the peer sent beside the body. +/// +/// A version-1 or version-2 header that came compressed, as rskj sends a +/// chain's headers to its peers, holds only a commitment to its extension: +/// its logs bloom, execution edges and base event. rskj sends the extension +/// beside the block's body (`BodyResponseMessage`), and +/// `DownloadingBodiesSyncState.newBody` sets it on the header before the +/// block is checked and run. Such a header is completed here, with the +/// extension its commitment names (`Header::with_extension`); a body without +/// that extension is refused, since without its bloom a block that logs +/// anything fails execution. `Ok(None)` means the header holds its own fields +/// and keeps them, whatever was sent beside the body. +pub(crate) fn completed_header( + header: &Header, + extension: Option<&BlockHeaderExtension>, +) -> Result, &'static str> { + if header.extension_data.is_none() { + return Ok(None); + } + extension + .and_then(|extension| header.with_extension(extension)) + .map(Some) + .ok_or("RSKIP-351 extension fields") +} + /// The body this header determines, when it determines one. /// /// A block is `[header, transactions, uncles]`. The uncle list cannot be @@ -61,7 +88,16 @@ pub(crate) fn body_mismatch( /// network, no state lookup -- which matters, because this runs against every /// header in a sync, and a test more expensive than a request would eat the /// saving it exists to create. +/// +/// Not for a header that holds only the commitment to its RSKIP-351 +/// extension: the extension comes only beside a peer's body +/// ([`completed_header`]), and nothing in it follows from the height. Not +/// even an empty block's bloom does, since REMASC logs each fee it pays +/// (`RemascFeesPayer.logPayment`). pub(crate) fn derivable_body(header: &Header) -> Option> { + if header.extension_data.is_some() { + return None; + } let transactions = vec![Transaction::remasc(header.number)]; body_mismatch(header, &transactions, &[]).is_none().then_some(transactions) } @@ -120,4 +156,59 @@ mod derivation_tests { let wrong = vec![Transaction::remasc(number + 1)]; assert!(body_mismatch(&header, &wrong, &[]).is_some()); } + + /// A version-2 extension whose bloom starts with `first`. + fn extension(first: u8) -> BlockHeaderExtension { + let mut logs_bloom = vec![0; 256]; + logs_bloom[0] = first; + BlockHeaderExtension::V2 { + logs_bloom, + edges: Some(vec![1]), + base_event: Some(Vec::new()), + } + } + + /// The bloom's slot of a header that commits to `extension`, as rskj + /// writes it. + fn commitment(extension: &BlockHeaderExtension) -> alloy_primitives::Bytes { + let mut slot = Vec::new(); + alloy_rlp::Encodable::encode(&extension.create_extension_data().as_slice(), &mut slot); + slot.into() + } + + /// A header that holds only the commitment to its RSKIP-351 extension is + /// not derivable, though its transactions are REMASC's alone: the + /// extension comes only beside a peer's body. + #[test] + fn a_header_holding_only_its_extension_commitment_is_not_derivable() { + let number = 4_000_000; + let txs = vec![Transaction::remasc(number)]; + let mut header = header_committing_to(&txs, true, number); + assert_eq!(derivable_body(&header), Some(txs)); + header.extension_data = Some(commitment(&extension(1))); + assert_eq!(derivable_body(&header), None); + } + + /// A header that came compressed is completed only by the extension its + /// commitment names, and a body sent without it, or with another, is + /// refused. A header that holds its own fields keeps them, whatever comes. + #[test] + fn a_compressed_header_is_completed_only_by_the_extension_it_names() { + let mut header = crate::tests::dummy_header(7, Default::default(), Default::default()); + header.extension_data = Some(commitment(&extension(1))); + assert_eq!( + completed_header(&header, None), + Err("RSKIP-351 extension fields") + ); + assert_eq!( + completed_header(&header, Some(&extension(2))), + Err("RSKIP-351 extension fields") + ); + let completed = completed_header(&header, Some(&extension(1))) + .expect("the extension it names") + .expect("a completed header"); + assert_eq!(completed.extension(), Some(extension(1))); + assert_eq!(completed_header(&completed, Some(&extension(2))), Ok(None)); + assert_eq!(completed_header(&completed, None), Ok(None)); + } } diff --git a/crates/sync/src/events.rs b/crates/sync/src/events.rs index 585ed21..f34f2fa 100644 --- a/crates/sync/src/events.rs +++ b/crates/sync/src/events.rs @@ -1,5 +1,6 @@ use alloy_primitives::{B256, B512}; use rustock_core::types::header::Header; +use rustock_core::types::header_extension::BlockHeaderExtension; use rustock_core::types::transaction::Transaction; use alloy_primitives::U256; use rustock_core::types::block::Block; @@ -42,6 +43,9 @@ pub enum SyncEvent { id: u64, transactions: Vec, uncles: Vec
, + /// The RSKIP-351 extension the peer sent beside the body, which a + /// header that came compressed needs (`body_check::completed_header`). + extension: Option, }, NewBlockHashes { peer: B512, diff --git a/crates/sync/src/handler.rs b/crates/sync/src/handler.rs index 51103c6..0af146c 100644 --- a/crates/sync/src/handler.rs +++ b/crates/sync/src/handler.rs @@ -267,11 +267,21 @@ impl SyncHandler { let store = &self.manager.store; // Above the simulated head this node has nothing to say, even though // the block is on disk. - if let Some(h) = store.header(hash).ok().flatten() { + let header = store.header(hash).ok().flatten(); + if let Some(h) = &header { if !self.may_serve(h.number) { return None; } } + // rskj `NodeBlockProcessor.processBodyRequest` sends the header's + // RSKIP-351 extension beside the body, which a peer needs for a + // header it got compressed. A header that holds only its commitment + // has none to send, and a body without it is no use to the peer. + let extension = match &header { + Some(h) if h.extension_data.is_some() => return None, + Some(h) => h.extension(), + None => None, + }; let (transactions, uncles) = store.body(hash).ok()??; trace!( @@ -284,6 +294,7 @@ impl SyncHandler { id: request_id, transactions, uncles, + extension, }; Some(P2pMessage::RskMessage(RskMessage::new( RskSubMessage::BodyResponse(resp), @@ -610,6 +621,7 @@ impl P2pHandler for SyncHandler { id: r.id, transactions: r.transactions.clone(), uncles: r.uncles.clone(), + extension: r.extension.clone(), }); } diff --git a/crates/sync/src/service.rs b/crates/sync/src/service.rs index 8b4a67f..7aaa77e 100644 --- a/crates/sync/src/service.rs +++ b/crates/sync/src/service.rs @@ -757,16 +757,48 @@ impl SyncService { /// Fill a missing body from the local block source, if available. Returns /// true when the body was found and stored (so no peer request is needed). + /// + /// Not for a header that holds only its RSKIP-351 commitment: the source + /// gives transactions and uncles, never the extension such a header needs. fn try_fill_body_locally(&self, hash: B256) -> bool { let Some(source) = &self.block_source else { return false; }; + if matches!(self.manager.store.header(hash), Ok(Some(h)) if h.extension_data.is_some()) { + return false; + } match source.body_by_hash(hash) { Some((txs, ommers)) => self.manager.store.put_body(hash, &txs, &ommers).is_ok(), None => false, } } + /// Drop the body stored for the block `hash` if its header, `header`, + /// holds only the commitment to its RSKIP-351 extension. + /// + /// A body is stored beside such a header only once the extension that + /// came with it has completed the header, so one found beside it came + /// without: derived from the header, before that was refused. Its block + /// can't be run, its bloom missing, so it is dropped and asked for again. + fn drop_body_without_extension(&self, hash: B256, header: &Header) { + if header.extension_data.is_none() || !matches!(self.manager.store.body(hash), Ok(Some(_))) + { + return; + } + match self.manager.store.delete_body(hash) { + Ok(()) => debug!( + target: "rustock::sync", + "Dropped the body of #{}, stored without its header's extension; asking for it again", + header.number + ), + Err(e) => warn!( + target: "rustock::sync", + "Could not drop the body of #{}, stored without its header's extension: {e:?}", + header.number + ), + } + } + /// Attach the chain-event channel that feeds `eth_subscribe`. pub fn with_events(mut self, events: Option) -> Self { self.events = events; @@ -2010,6 +2042,7 @@ impl SyncService { let Ok(Some(header)) = self.manager.store.header(hash) else { break; }; + self.drop_body_without_extension(hash, &header); if self.manager.store.body(hash).ok().flatten().is_none() { missing.push(header); } @@ -2259,6 +2292,7 @@ impl SyncService { let Ok(Some(hash)) = self.manager.store.canonical_hash(number) else { break }; let Ok(Some(header)) = self.manager.store.header(hash) else { break }; // No body yet: `fill_follow_body_gap` is the path for that one. + self.drop_body_without_extension(hash, &header); let Ok(Some((txs, uncles))) = self.manager.store.body(hash) else { break }; self.follow_buffer.insert(number, (hash, header, txs, uncles)); } @@ -3597,8 +3631,15 @@ impl SyncService { } self.on_headers_with_uncles_response(peer, entries).await; } - SyncEvent::BodyResponse { peer, id, transactions, uncles } => { - self.on_body_response(peer, id, transactions, uncles).await; + SyncEvent::BodyResponse { + peer, + id, + transactions, + uncles, + extension, + } => { + self.on_body_response(peer, id, transactions, uncles, extension) + .await; } SyncEvent::SnapStatusResponse { peer, @@ -4217,6 +4258,7 @@ impl SyncService { for num in start..=our_height { if let Ok(Some(hash)) = self.manager.store.canonical_hash(num) { if let Ok(Some(header)) = self.manager.store.header(hash) { + self.drop_body_without_extension(hash, &header); if matches!(self.manager.store.body(hash), Ok(None)) && !self.try_fill_body_locally(hash) { @@ -4873,13 +4915,14 @@ impl SyncService { request_id: u64, transactions: Vec, uncles: Vec
, + extension: Option, ) { rustock_core::metrics::BODIES_RECEIVED.inc(); let old = std::mem::take(&mut self.state); match old { SyncState::DownloadingBodies { peer_best, - pending_headers, + mut pending_headers, next_request, mut in_flight, mut id_index, @@ -4937,29 +4980,60 @@ impl SyncService { // strikes it has not earned. self.body_peer_strikes.remove(&peer); let (hash, header) = &pending_headers[idx]; + let (hash, number) = (*hash, header.number); // Does this body belong to that header? Receipt is the one // moment the sender is unambiguous, and two hashes settle - // it. A mismatch is not stored and the request goes back - // in flight, so the stall path asks somebody else. - if let Some(what) = - crate::body_check::body_mismatch(header, &transactions, &uncles) - { - self.charge_bad_body(peer, header.number, what); - in_flight.insert(idx, req); - id_index.insert(request_id, (idx, peer)); - self.state = SyncState::DownloadingBodies { - peer_best, - pending_headers, - next_request, - in_flight, - id_index, + // it. A header that came compressed takes the extension sent + // beside the body, and a body without the one it commits to + // fails the same way. A mismatch is not stored and the + // request goes back in flight, so the stall path asks + // somebody else. + let checked = + match crate::body_check::body_mismatch(header, &transactions, &uncles) { + Some(what) => Err(what), + None => crate::body_check::completed_header(header, extension.as_ref()), }; - return; + let completed = match checked { + Ok(completed) => completed, + Err(what) => { + self.charge_bad_body(peer, number, what); + in_flight.insert(idx, req); + id_index.insert(request_id, (idx, peer)); + self.state = SyncState::DownloadingBodies { + peer_best, + pending_headers, + next_request, + in_flight, + id_index, + }; + return; + } + }; + // The header with its extension is stored before the body, + // so that a body never sits beside a header still missing it. + if let Some(completed) = completed { + if let Err(e) = self.manager.store.put_header_with_hash(hash, &completed) { + error!( + target: "rustock::sync", + "Failed to store the header of #{number} with its extension: {e:?}" + ); + in_flight.insert(idx, req); + id_index.insert(request_id, (idx, peer)); + self.state = SyncState::DownloadingBodies { + peer_best, + pending_headers, + next_request, + in_flight, + id_index, + }; + return; + } + pending_headers[idx].1 = completed; } - self.body_providers.insert(header.number, peer); + self.body_providers.insert(number, peer); - if let Err(e) = self.manager.store.put_body(*hash, &transactions, &uncles) { + if let Err(e) = self.manager.store.put_body(hash, &transactions, &uncles) { error!( target: "rustock::sync", "Failed to store body for {:?}: {:?}", hash, e @@ -5024,16 +5098,39 @@ impl SyncService { self.pending_follow_bodies.remove(&request_id) { // Same check as the batch path, same reason. - if let Some(what) = - crate::body_check::body_mismatch(&header, &transactions, &uncles) - { - self.charge_bad_body(peer, header.number, what); - // Put it back so the follow-mode retry re-requests it; - // dropping the entry would strand the block instead. - self.pending_follow_bodies - .insert(request_id, (hash, header, sent, peer)); - return; - } + let checked = + match crate::body_check::body_mismatch(&header, &transactions, &uncles) { + Some(what) => Err(what), + None => { + crate::body_check::completed_header(&header, extension.as_ref()) + } + }; + let header = match checked { + Ok(None) => header, + Ok(Some(completed)) => { + if let Err(e) = + self.manager.store.put_header_with_hash(hash, &completed) + { + error!( + target: "rustock::sync", + "Failed to store the header of #{} with its extension: {e:?}", + completed.number + ); + self.pending_follow_bodies + .insert(request_id, (hash, header, sent, peer)); + return; + } + completed + } + Err(what) => { + self.charge_bad_body(peer, header.number, what); + // Put it back so the follow-mode retry re-requests it; + // dropping the entry would strand the block instead. + self.pending_follow_bodies + .insert(request_id, (hash, header, sent, peer)); + return; + } + }; self.body_providers.insert(header.number, peer); if let Err(e) = self.manager.store.put_body(hash, &transactions, &uncles) { diff --git a/crates/sync/src/tests.rs b/crates/sync/src/tests.rs index e27d91c..7fc3f17 100644 --- a/crates/sync/src/tests.rs +++ b/crates/sync/src/tests.rs @@ -1102,7 +1102,9 @@ async fn test_skeleton_round_transitions_to_next_skeleton() { Vec::new() }; for (req_id, asked) in pending { - service.on_body_response(asked, req_id, vec![], vec![]).await; + service + .on_body_response(asked, req_id, vec![], vec![], None) + .await; } // After all bodies downloaded, should continue to DownloadingSkeleton @@ -1183,7 +1185,7 @@ async fn a_body_that_does_not_match_its_header_is_charged_and_refetched() { // b1's header commits to an empty body. Send an uncle it never had. let bogus_uncle = dummy_header(0, B256::repeat_byte(0xEE), U256::from(1)); service - .on_body_response(peer, 7, vec![], vec![bogus_uncle]) + .on_body_response(peer, 7, vec![], vec![bogus_uncle], None) .await; assert_eq!( @@ -1238,7 +1240,9 @@ async fn a_matching_body_is_stored_and_its_supplier_remembered() { id_index, }; - service.on_body_response(peer, 7, vec![], vec![]).await; + service + .on_body_response(peer, 7, vec![], vec![], None) + .await; assert_eq!( scored(&scoring, peer, EventType::InvalidBlock), @@ -1326,7 +1330,9 @@ async fn test_failed_body_send_stays_tracked_for_retry() { // send fails (dead peer). The request must stay tracked so the stalled // retry re-attempts it — dropping it would orphan b2 forever and the // batch could never complete. - service.on_body_response(B512::repeat_byte(9), 7, vec![], vec![]).await; + service + .on_body_response(B512::repeat_byte(9), 7, vec![], vec![], None) + .await; match &service.state { SyncState::DownloadingBodies { in_flight, .. } => { @@ -1537,7 +1543,9 @@ async fn test_late_body_response_on_superseded_id_still_applies() { // Late response on the SUPERSEDED id 7 must still store b1. It comes from // the peer id 7 was sent to -- the retry went to a fresh id, and the old // id keeps its own peer, which is what lets a late answer still land. - service.on_body_response(peer, 7, vec![], vec![]).await; + service + .on_body_response(peer, 7, vec![], vec![], None) + .await; assert!( store.body(b1.hash()).unwrap().is_some(), "late response on a superseded id must still apply its body" @@ -1552,7 +1560,9 @@ async fn test_late_body_response_on_superseded_id_still_applies() { // Duplicate: the retried id now arrives too — must be ignored, no panic, // batch state unchanged (b2 still the only thing outstanding). - service.on_body_response(B512::repeat_byte(9), new_id, vec![], vec![]).await; + service + .on_body_response(B512::repeat_byte(9), new_id, vec![], vec![], None) + .await; match &service.state { SyncState::DownloadingBodies { in_flight, .. } => { assert!(!in_flight.contains_key(&0)); @@ -2241,7 +2251,9 @@ async fn test_follow_mode_buffers_out_of_order_body() { req_id, (h102_hash, h102, std::time::Instant::now(), B512::repeat_byte(9)), ); - service.on_body_response(B512::repeat_byte(9), req_id, vec![], vec![]).await; + service + .on_body_response(B512::repeat_byte(9), req_id, vec![], vec![], None) + .await; // #102 must remain buffered (parent #101 is not our head), not executed. assert!(service.follow_buffer.contains_key(&102), @@ -3584,6 +3596,7 @@ async fn test_sync_handler_forwards_body_response() { id: 42, transactions: vec![], uncles: vec![], + extension: Some(an_extension(1)), }; let msg = P2pMessage::RskMessage(RskMessage::new(RskSubMessage::BodyResponse(resp))); @@ -3591,10 +3604,21 @@ async fn test_sync_handler_forwards_body_response() { assert!(handler_resp.is_none()); match event_rx.try_recv().unwrap() { - SyncEvent::BodyResponse { id, transactions, uncles, .. } => { + SyncEvent::BodyResponse { + id, + transactions, + uncles, + extension, + .. + } => { assert_eq!(id, 42); assert!(transactions.is_empty()); assert!(uncles.is_empty()); + assert_eq!( + extension, + Some(an_extension(1)), + "the extension goes on with the body" + ); } other => panic!("Expected BodyResponse event, got {:?}", other), } @@ -3645,7 +3669,9 @@ async fn test_body_download_state_machine() { None }; if let Some((req_id, asked)) = pending { - service.on_body_response(asked, req_id, vec![], vec![]).await; + service + .on_body_response(asked, req_id, vec![], vec![], None) + .await; } assert!(matches!(service.state, SyncState::Following), @@ -6965,7 +6991,9 @@ async fn answer_body_requests(service: &mut SyncService, uncles_of: impl Fn(B256 _ => return, }; for (id, hash, peer) in asked { - service.on_body_response(peer, id, vec![], uncles_of(hash)).await; + service + .on_body_response(peer, id, vec![], uncles_of(hash), None) + .await; } } panic!("the service kept asking for bodies: {:?}", service.state); @@ -7075,7 +7103,9 @@ async fn a_followed_block_counts_its_uncles_when_its_body_comes() { service .pending_follow_bodies .insert(7, (b2.hash(), b2.clone(), std::time::Instant::now(), peer)); - service.on_body_response(peer, 7, vec![], uncles).await; + service + .on_body_response(peer, 7, vec![], uncles, None) + .await; assert_eq!(store.total_difficulty(b2.hash()).unwrap(), Some(U256::from(33))); } @@ -7519,7 +7549,9 @@ async fn a_body_from_a_peer_that_was_not_asked_is_ignored() { // b1's header commits to an empty body, so this payload is the correct // one. It is still refused, because of who sent it. let stranger = B512::repeat_byte(0x09); - service.on_body_response(stranger, 7, vec![], vec![]).await; + service + .on_body_response(stranger, 7, vec![], vec![], None) + .await; assert!( store.body(b1.hash()).unwrap().is_none(), @@ -7534,13 +7566,358 @@ async fn a_body_from_a_peer_that_was_not_asked_is_ignored() { } // The peer that was asked is still able to answer it. - service.on_body_response(asked, 7, vec![], vec![]).await; + service + .on_body_response(asked, 7, vec![], vec![], None) + .await; assert!( store.body(b1.hash()).unwrap().is_some(), "the genuine answer must still be accepted" ); } +// --------------------------------------------------------------------------- +// A header that came compressed takes the extension sent beside its body. +// +// From RSKIP-351 rskj sends a chain's headers compressed: a version-1 or +// version-2 header holds only a commitment to its extension -- the block's +// logs bloom, execution edges and base event -- and the extension comes beside +// the block's body (`BodyResponseMessage`), which rskj sets on the header +// before the block runs (`DownloadingBodiesSyncState.newBody`). While it was +// dropped, testnet's first block after a snapshot whose bloom wasn't empty +// failed `logs bloom mismatch`, its receipts all rskj's. +// --------------------------------------------------------------------------- + +/// A version-2 extension whose bloom has one bit set: bit `bit`. Its edges +/// are empty, as rskj emits them on every versioned header while RSKIP144 is +/// active and the block has no sublists; the store's decoder counts on it. +fn an_extension(bit: usize) -> rustock_core::types::header_extension::BlockHeaderExtension { + let mut logs_bloom = vec![0u8; 256]; + logs_bloom[bit / 8] |= 0x80 >> (bit % 8); + rustock_core::types::header_extension::BlockHeaderExtension::V2 { + logs_bloom, + edges: Some(Vec::new()), + base_event: Some(Vec::new()), + } +} + +/// `header` as rskj sends it to its peers, holding only the commitment to +/// `extension` in its bloom's slot, with the UMM root and merged-mining fields +/// that every versioned header has, and that the store's decoder counts on. +fn compressed( + header: Header, + extension: &rustock_core::types::header_extension::BlockHeaderExtension, +) -> Header { + let mut slot = Vec::new(); + alloy_rlp::Encodable::encode(&extension.create_extension_data().as_slice(), &mut slot); + Header { + logs_bloom: Default::default(), + extension_data: Some(Bytes::from(slot)), + version_fields: None, + umm_root: Some(Bytes::new()), + bitcoin_merged_mining_header: Some(Bytes::from(vec![0u8; 80])), + bitcoin_merged_mining_merkle_proof: Some(Bytes::new()), + bitcoin_merged_mining_coinbase_transaction: Some(Bytes::new()), + cached_hash: None, + cached_hash_for_merged_mining: None, + ..header + } +} + +/// A service over `store` with one peer, `asked`, connected. +async fn service_with_a_peer(store: Arc, asked: B512) -> SyncService { + let verifier = Arc::new(HeaderVerifier::new()); + let peer_store = Arc::new(rustock_networking::peers::PeerStore::new()); + let manager = Arc::new(SyncManager::new(store, verifier, peer_store.clone())); + let (_event_tx, event_rx) = mpsc::unbounded_channel(); + let service = SyncService::new(manager, peer_store.clone(), event_rx); + let (tx, rx) = mpsc::channel(rustock_networking::peers::PEER_CHANNEL_CAPACITY); + // Kept open, so that what is sent to the peer has somewhere to go. + std::mem::forget(rx); + peer_store.add_peer(asked, tx).await; + service +} + +/// The batch path. A body without the extension its header commits to, or +/// with another, is refused, and the request stays outstanding. With it, the +/// header is stored with its bloom, edges and base event, under the same hash, +/// and the batch carries the completed header to execution. +#[tokio::test] +async fn a_compressed_header_is_stored_with_the_extension_that_came_beside_its_body() { + let store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let genesis = dummy_header(0, B256::ZERO, U256::from(1)); + store.update_head(&genesis, U256::from(1)).unwrap(); + let extension = an_extension(77); + let b1 = compressed(dummy_header(1, genesis.hash(), U256::from(1)), &extension); + let b2 = dummy_header(2, b1.hash(), U256::from(1)); + let hash = b1.hash(); + for h in [&b1, &b2] { + store.put_header(h).unwrap(); + } + assert_eq!( + store.header(hash).unwrap(), + Some(b1.clone()), + "stored as it came" + ); + + let asked = B512::repeat_byte(0x01); + let mut service = service_with_a_peer(store.clone(), asked).await; + let mut in_flight = std::collections::HashMap::new(); + in_flight.insert( + 0usize, + InFlightBody { + req_id: 7, + sent: Instant::now(), + peer: asked, + }, + ); + let mut id_index = std::collections::HashMap::new(); + id_index.insert(7u64, (0usize, asked)); + service.state = SyncState::DownloadingBodies { + peer_best: 2, + pending_headers: vec![(hash, b1.clone()), (b2.hash(), b2.clone())], + next_request: 1, + in_flight, + id_index, + }; + + for (sent, why) in [ + (None, "no extension"), + (Some(an_extension(78)), "another extension"), + ] { + service + .on_body_response(asked, 7, vec![], vec![], sent) + .await; + assert!( + store.body(hash).unwrap().is_none(), + "{why}: no body is stored" + ); + assert_eq!( + store.header(hash).unwrap(), + Some(b1.clone()), + "{why}: nor the header changed" + ); + match &service.state { + SyncState::DownloadingBodies { + in_flight, + id_index, + .. + } => { + assert!( + in_flight.contains_key(&0), + "{why}: the request stays outstanding" + ); + assert!(id_index.contains_key(&7), "{why}: and keeps its id"); + } + other => panic!("expected DownloadingBodies, got {other:?}"), + } + } + + service + .on_body_response(asked, 7, vec![], vec![], Some(extension.clone())) + .await; + assert!(store.body(hash).unwrap().is_some(), "the body is stored"); + let stored = store.header(hash).unwrap().expect("the header"); + assert_eq!(stored.extension(), Some(extension.clone())); + assert_eq!(stored.logs_bloom.as_slice(), extension.logs_bloom()); + assert_eq!( + stored.hash(), + hash, + "the same hash, derived from the fields" + ); + match &service.state { + SyncState::DownloadingBodies { + pending_headers, .. + } => { + assert_eq!( + pending_headers[0].1.extension(), + Some(extension), + "the batch runs it" + ); + } + other => panic!("expected DownloadingBodies, got {other:?}"), + } +} + +/// Follow mode takes the extension the same way, and buffers the completed +/// header for execution. +#[tokio::test] +async fn a_followed_compressed_header_takes_the_extension_that_came_beside_its_body() { + let store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let genesis = dummy_header(0, B256::ZERO, U256::from(1)); + store.update_head(&genesis, U256::from(1)).unwrap(); + let extension = an_extension(3); + let b1 = compressed(dummy_header(1, genesis.hash(), U256::from(1)), &extension); + let hash = b1.hash(); + store.put_header(&b1).unwrap(); + + let asked = B512::repeat_byte(0x01); + let mut service = service_with_a_peer(store.clone(), asked).await; + service.state = SyncState::Following; + service + .pending_follow_bodies + .insert(7, (hash, b1.clone(), Instant::now(), asked)); + + service + .on_body_response(asked, 7, vec![], vec![], None) + .await; + assert!(store.body(hash).unwrap().is_none(), "no body is stored"); + assert!( + service.pending_follow_bodies.contains_key(&7), + "it is asked for again" + ); + assert!(service.follow_buffer.is_empty()); + + service + .on_body_response(asked, 7, vec![], vec![], Some(extension.clone())) + .await; + assert!(store.body(hash).unwrap().is_some(), "the body is stored"); + assert_eq!( + store.header(hash).unwrap().and_then(|h| h.extension()), + Some(extension.clone()) + ); + assert!(!service.pending_follow_bodies.contains_key(&7)); + // No block has run, so nothing drains the buffer. + let (buffered, header, ..) = service.follow_buffer.get(&1).expect("buffered"); + assert_eq!((*buffered, header.extension()), (hash, Some(extension))); +} + +/// A local block source that has every body, empty. +struct EveryBody; + +impl ExternalBlockSource for EveryBody { + fn body_by_hash(&self, _: B256) -> Option<(Vec, Vec
)> { + Some((Vec::new(), Vec::new())) + } +} + +/// A body stored beside a header that holds only its commitment -- derived +/// from the header, before that was refused -- is dropped when its batch +/// starts, and asked of a peer, neither derived again nor filled from a local +/// source, which has no extension to give. A header that holds its own fields +/// keeps its body. +#[tokio::test] +async fn a_body_stored_without_its_header_extension_is_asked_for_again() { + let store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let genesis = dummy_header(0, B256::ZERO, U256::from(1)); + store.update_head(&genesis, U256::from(1)).unwrap(); + let b1 = compressed(derivable_header(1, genesis.hash()), &an_extension(5)); + let b2 = dummy_header(2, b1.hash(), U256::from(1)); + let hash = b1.hash(); + for (number, h) in [(1, &b1), (2, &b2)] { + store.put_header(h).unwrap(); + store.put_canonical_hash(number, h.hash()).unwrap(); + store + .put_total_difficulty(h.hash(), U256::from(number + 1)) + .unwrap(); + } + store.update_head(&b2, U256::from(3)).unwrap(); + store + .put_body(hash, &[rustock_core::Transaction::remasc(1)], &[]) + .unwrap(); + store.put_body(b2.hash(), &[], &[]).unwrap(); + + let service = service_with_a_peer(store.clone(), B512::repeat_byte(0x01)).await; + let mut service = service.with_block_source(Arc::new(EveryBody)); + service.start_body_downloads(2).await; + + assert!(store.body(hash).unwrap().is_none(), "the body is dropped"); + assert!( + store.body(b2.hash()).unwrap().is_some(), + "a whole header keeps its body" + ); + assert_eq!(service.derived_bodies, 0, "and not derived again"); + match &service.state { + SyncState::DownloadingBodies { + pending_headers, + in_flight, + .. + } => { + assert_eq!(pending_headers.len(), 2); + assert_eq!(in_flight.len(), 1, "it is asked of the peer"); + } + other => panic!("expected DownloadingBodies, got {other:?}"), + } +} + +/// Follow mode drops such a body too, rather than run the block without its +/// bloom, and asks for it again. +#[tokio::test] +async fn follow_mode_asks_again_for_a_body_stored_without_its_header_extension() { + let store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let genesis = dummy_header(0, B256::ZERO, U256::from(1)); + store.update_head(&genesis, U256::from(1)).unwrap(); + store.put_body(genesis.hash(), &[], &[]).unwrap(); + store.set_exec_head(genesis.hash(), B256::ZERO).unwrap(); + let b1 = compressed(derivable_header(1, genesis.hash()), &an_extension(6)); + let hash = b1.hash(); + store.put_header(&b1).unwrap(); + store.put_canonical_hash(1, hash).unwrap(); + let stale = [rustock_core::Transaction::remasc(1)]; + store.put_body(hash, &stale, &[]).unwrap(); + + let mut service = service_with_a_peer(store.clone(), B512::repeat_byte(0x01)).await; + service.state = SyncState::Following; + service.refill_follow_buffer_from_store(); + assert!( + service.follow_buffer.is_empty(), + "not run without its bloom" + ); + assert!(store.body(hash).unwrap().is_none(), "the body is dropped"); + + store.put_body(hash, &stale, &[]).unwrap(); + service.fill_follow_body_gap().await; + assert!(store.body(hash).unwrap().is_none(), "dropped again"); + assert_eq!( + service.pending_follow_bodies.len(), + 1, + "and asked of the peer" + ); +} + +/// A body is served with its header's extension, as rskj's +/// `NodeBlockProcessor.processBodyRequest` sends it; for a header that holds +/// only the commitment, which has none to send, no body is served. +#[tokio::test] +async fn a_served_body_carries_its_header_extension() { + use rustock_networking::protocol::rsk::BodyRequest; + use rustock_networking::protocol::{P2pHandler, P2pMessage, RskMessage, RskSubMessage}; + + let store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let extension = an_extension(9); + let whole = compressed(dummy_header(1, B256::ZERO, U256::from(1)), &extension) + .with_extension(&extension) + .expect("its own extension"); + let only_committed = compressed( + dummy_header(2, whole.hash(), U256::from(1)), + &an_extension(10), + ); + for h in [&whole, &only_committed] { + store.put_header(h).unwrap(); + store.put_body(h.hash(), &[], &[]).unwrap(); + } + + let verifier = Arc::new(HeaderVerifier::new()); + let peer_store = Arc::new(rustock_networking::peers::PeerStore::new()); + let manager = Arc::new(SyncManager::new(store, verifier, peer_store)); + let (event_tx, _event_rx) = mpsc::unbounded_channel(); + let handler = SyncHandler::new(manager, event_tx); + let ask = |hash| { + let req = BodyRequest { id: 33, hash }; + let msg = P2pMessage::RskMessage(RskMessage::new(RskSubMessage::BodyRequest(req))); + handler.handle_message(B512::repeat_byte(0x01), &msg) + }; + match ask(whole.hash()) { + Some(P2pMessage::RskMessage(RskMessage { + sub_message: RskSubMessage::BodyResponse(r), + })) => assert_eq!(r.extension, Some(extension)), + other => panic!("expected a body, got {other:?}"), + } + assert!( + ask(only_committed.hash()).is_none(), + "no extension to send beside it" + ); +} + // --------------------------------------------------------------------------- // A body batch that needs no network must still end. // From 15d2b8bcebbb86257f0d9eff18573df2a79b6b97 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Sat, 7 Nov 2026 02:38:20 -0800 Subject: [PATCH 029/264] docs: rskj sends a chain's headers to its peers compressed rskip-spec-vs-rskj-code.md said the compressed form of a versioned header exists only to be hashed and is never sent. It is sent: BlockHeadersResponseMessage gives a chain's headers to peers with getEncodedCompressed, and their bloom, edges and base event come beside each block's body, as BodyResponseMessage's third element. Whole blocks, in BlockMessage and in a snapshot's blocks, carry the full form. Both tables now say which form goes where, and the second says how rustock takes the extension. --- docs/rskip-spec-vs-rskj-code.md | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/docs/rskip-spec-vs-rskj-code.md b/docs/rskip-spec-vs-rskj-code.md index 6d32d89..cb81444 100644 --- a/docs/rskip-spec-vs-rskj-code.md +++ b/docs/rskip-spec-vs-rskj-code.md @@ -222,11 +222,13 @@ if (!usingCompressedEncoding) { | | field 6 | after `ummRoot` | |---|---|---| -| **uncompressed** (wire, storage) | the real 256-byte `logsBloom` | `version`, then `edges` if any | -| **compressed** (block hash only) | `RLP.encodeElement(extensionData)` | nothing | +| **uncompressed** (blocks on the wire, storage) | the real 256-byte `logsBloom` | `version`, then `edges` if any | +| **compressed** (block hash; headers sent to peers) | `RLP.encodeElement(extensionData)` | nothing | `getEncodedForHash() = getEncoded(true, !rskip92, true)` — the hash uses the -compressed form; the network never sends it. +compressed form. So does `BlockHeadersResponseMessage`, which sends a chain's +headers to peers with `getEncodedCompressed`; the bloom and edges then come +beside each block's body, as the third element of `BodyResponseMessage`. **What rustock does.** `Header::decode` dispatches on how many RLP items remain after the base fields: 3 means the three merged-mining fields, 4 means @@ -359,13 +361,19 @@ So a versioned header has **two serialisations**, and only one is documented: | | field 6 | after `ummRoot` | |---|---|---| -| **wire / storage** (uncompressed) | the real 256-byte bloom | `version`, `edges?`, `baseEvent` | -| **block hash** (compressed) | `encodeElement(RLP([version, hash]))` | nothing | - -`getEncodedForHash() = getEncoded(true, !rskip92, true)` — the compressed form -exists only to be hashed and is never transmitted. RSKIP-351 describes only the -compressed shape and calls it "the extended header"; nothing says the wire form -keeps the bloom and appends the fields separately. +| **blocks on the wire, storage** (uncompressed) | the real 256-byte bloom | `version`, `edges?`, `baseEvent` | +| **block hash, headers sent to peers** (compressed) | `encodeElement(RLP([version, hash]))` | nothing | + +`getEncodedForHash() = getEncoded(true, !rskip92, true)` — the block hash +covers the compressed form, and `BlockHeadersResponseMessage` sends a chain's +headers in it (`getEncodedCompressed`). Their bloom, edges and base event come +beside each block's body instead, as the third element of `BodyResponseMessage`, +which `DownloadingBodiesSyncState.newBody` sets on the header before the block +runs; rustock takes it only if it matches the commitment +(`Header::with_extension`). A whole block (`BlockMessage`, a snapshot's blocks) +carries the full form. RSKIP-351 describes only the compressed shape and calls +it "the extended header"; nothing says the full form keeps the bloom and +appends the fields separately. An implementer reading the RSKIPs would decode every testnet header wrongly: the version byte lands where the merged-mining header is expected and every From f70176325bec35fc1aa136a6942001e84728a794 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Sat, 7 Nov 2026 02:39:50 -0800 Subject: [PATCH 030/264] docs: the node catches no signal; rsk_shutdown is its ordered stop rsk_shutdown's doc gave the operating procedure as "SIGTERM and wait, never SIGKILL", as if SIGTERM stopped the node cleanly. Only an rskj import installs signal handlers; the node installs none, so SIGTERM ends it as abruptly as SIGKILL. That is safe, since the executed head never advances past what a crash keeps -- the testnet node, stopped with SIGTERM on 7 November, resumed from its executed head -- but it is rsk_shutdown that flushes the trie store first and leaves nothing to execute again. --- crates/rpc/src/admin.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/crates/rpc/src/admin.rs b/crates/rpc/src/admin.rs index 43eccc7..0996057 100644 --- a/crates/rpc/src/admin.rs +++ b/crates/rpc/src/admin.rs @@ -327,12 +327,14 @@ pub fn rsk_flush(id: Value, state: &RpcState) -> JsonRpcResponse { /// `System::exit`. The same is done here, with one addition that matters more /// on this node than on rskj: the trie store is flushed **first**. /// -/// That ordering is the whole point of offering this at all. Stopping this -/// node by killing it discards unflushed memtables, so the operating -/// procedure is "SIGTERM and wait, never SIGKILL". This gives automation the -/// same ordered stop without needing signal access to the process — useful for -/// orchestration, and for anything that can reach the RPC port but not the -/// process table. +/// That ordering is the whole point of offering this at all. The node catches +/// no signal -- only an rskj import does -- so SIGTERM stops it as abruptly as +/// SIGKILL, and what the trie store holds unflushed is lost. Nothing is +/// corrupted by that: the executed head never advances past what a crash +/// keeps, so the node executes again from there. This gives automation an +/// ordered stop, with nothing to execute again, without needing signal access +/// to the process -- useful for orchestration, and for anything that can +/// reach the RPC port but not the process table. /// /// The response is sent before the process exits, because a caller that gets /// no reply cannot tell "shut down" from "crashed". From 7f81a59dd76c198e7d887fdb709f9674b885143e Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Sun, 8 Nov 2026 06:19:07 -0800 Subject: [PATCH 031/264] Take a signature whose key is the point at infinity, as rskj does rskj recovers a transaction's sender with libsecp256k1 by default, and when libsecp256k1 refuses to recover a key, asks whether the key would have been the point at infinity: if so, it answers the key whose encoding is the one byte 00 (Secp256k1ServiceNative.recoverFromSignature, "to be compatible with BC implementation": Bouncy Castle encodes the point at infinity so). ECKey.getAddress hashes that encoding past its first byte, which is nothing, so the sender is keccak256("")[12..], 0xdcc703c0e500b653ca82273b7bfad8045d85a470. rustock refused such a signature, so the testnet node stopped at #8,269,753: the first transaction in #8,269,754, a transfer from that account at nonce 10, is signed so that sR = eG. - recover_sender and btc_sender_hash160 take the key from one place, recover_key, which answers the point at infinity where libsecp256k1 refuses a signature whose R exists, whose r and s aren't zero, and for which sR = eG, e being the signing hash modulo the order. Any other signature libsecp256k1 refuses is refused as before. - Its sender is INFINITY_SENDER. A peg-out from it pays the hash160 of the one byte 00, as rskj's BridgeUtils.recoverBtcAddressFromEthTransaction does from that same encoding; recover_compressed_pubkey has no 33-byte key for it and answers none. Execution, the pool, mining, alerts and the RPC all recover senders through recover_sender. The ECRecover precompile is revm's, which answers nothing for such a signature where rskj's answers this sender; that is left for a change of its own. Tests recover testnet's transaction, byte for byte and hash for hash, as rskj does, with its peg-out address; check the sender is the hash of nothing; recover the point at infinity from a signature made so that sR = eG, and an ordinary key from the same r with y's other parity; and still refuse an R off the curve and a zero s. --- crates/core/src/types/transaction.rs | 263 ++++++++++++++++++++++----- 1 file changed, 222 insertions(+), 41 deletions(-) diff --git a/crates/core/src/types/transaction.rs b/crates/core/src/types/transaction.rs index d4c0cc2..928ac89 100644 --- a/crates/core/src/types/transaction.rs +++ b/crates/core/src/types/transaction.rs @@ -205,6 +205,78 @@ pub(crate) fn secp_context() -> &'static secp256k1::Secp256k1 bool { + let (r, s) = signature.split_at(32); + if r.iter().all(|byte| *byte == 0) || s.iter().all(|byte| *byte == 0) { + return false; + } + let mut point = [0_u8; 33]; + point[0] = 0x02 | recovery_id; + point[1..].copy_from_slice(r); + let Ok(point) = secp256k1::PublicKey::from_byte_array_compressed(point) else { + return false; + }; + let order = U256::from_be_bytes(secp256k1::constants::CURVE_ORDER); + let e = U256::from_be_bytes(*hash); + let e = if e >= order { e - order } else { e }; + // eG at e = 0 is no point, and sR always is one. + if e.is_zero() { + return false; + } + // The generator, whose y is even. + let mut generator = [0x02_u8; 33]; + generator[1..].copy_from_slice(&secp256k1::constants::GENERATOR_X); + let mut s_bytes = [0_u8; 32]; + s_bytes.copy_from_slice(s); + let (Ok(generator), Ok(s), Ok(e)) = ( + secp256k1::PublicKey::from_byte_array_compressed(generator), + secp256k1::Scalar::from_be_bytes(s_bytes), + secp256k1::Scalar::from_be_bytes(e.to_be_bytes::<32>()), + ) else { + return false; + }; + match ( + point.mul_tweak(secp_context(), &s), + generator.mul_tweak(secp_context(), &e), + ) { + (Ok(sr), Ok(eg)) => sr == eg, + _ => false, + } +} + impl Transaction { /// Recover the sender address from the transaction signature. @@ -218,7 +290,25 @@ impl Transaction { /// so at a full block it was the single largest cost outside execution /// -- 50 ms of a 205 ms block. See `cargo run --release --example /// validation_cost` and `docs/execution-performance.md`. + /// + /// A signature that recovers the point at infinity has the sender rskj + /// gives it, [`INFINITY_SENDER`]. pub fn recover_sender(&self, chain_id: u64) -> anyhow::Result
{ + Ok(match self.recover_key(chain_id)? { + // The leading byte is the uncompressed-form marker and is not part + // of the key the address is taken from. + Recovered::Key(key) => { + let pubkey_hash = Keccak256::digest(&key.serialize_uncompressed()[1..]); + Address::from_slice(&pubkey_hash[12..]) + } + Recovered::Infinity => INFINITY_SENDER, + }) + } + + /// The key this transaction's signature recovers, as rskj recovers it. + /// `recover_sender` and `btc_sender_hash160` both take it from here, so + /// they cannot drift apart on what a signature means. + fn recover_key(&self, chain_id: u64) -> anyhow::Result { let hash = self.signing_hash(chain_id); let recovery_id = if self.is_eip155(chain_id) { @@ -246,14 +336,13 @@ impl Transaction { .map_err(|e| anyhow::anyhow!("invalid signature: {e}"))?; let message = secp256k1::Message::from_digest(hash.0); - let key = secp_context() - .recover_ecdsa(message, &signature) - .map_err(|e| anyhow::anyhow!("ECDSA recovery failed: {e}"))?; - - // The leading byte is the uncompressed-form marker and is not part - // of the key the address is taken from. - let pubkey_hash = Keccak256::digest(&key.serialize_uncompressed()[1..]); - Ok(Address::from_slice(&pubkey_hash[12..])) + match secp_context().recover_ecdsa(message, &signature) { + Ok(key) => Ok(Recovered::Key(key)), + Err(_) if recovers_infinity(&sig_bytes, recovery_id, &hash.0) => { + Ok(Recovered::Infinity) + } + Err(e) => Err(anyhow::anyhow!("ECDSA recovery failed: {e}")), + } } pub fn is_eip155(&self, chain_id: u64) -> bool { @@ -262,37 +351,14 @@ impl Transaction { /// Recover the compressed (33-byte) secp256k1 public key from the transaction signature. /// - /// Used to derive the BTC P2PKH address for peg-out destination: - /// `RIPEMD160(SHA256(compressed_pubkey))` = BTC hash160. - /// - /// Returns `None` if the signature is invalid or missing. + /// Returns `None` if the signature is invalid or missing, or recovers the + /// point at infinity, which has no such encoding: `btc_sender_hash160` + /// hashes rskj's one byte for it instead. pub fn recover_compressed_pubkey(&self, chain_id: u64) -> Option<[u8; 33]> { - let hash = self.signing_hash(chain_id); - - let recovery_id = if self.is_eip155(chain_id) { - let rid = self.v as i64 - chain_id as i64 * 2 - 35; - if !(0..=1).contains(&rid) { return None; } - rid as u8 - } else { - if self.v != 27 && self.v != 28 { return None; } - (self.v - 27) as u8 - }; - - let mut sig_bytes = [0u8; 64]; - sig_bytes[..32].copy_from_slice(&self.r.to_be_bytes::<32>()); - sig_bytes[32..].copy_from_slice(&self.s.to_be_bytes::<32>()); - - // Confined to 0 or 1 above, so masking cannot turn a rejected id - // into an accepted one. - let recid = secp256k1::ecdsa::RecoveryId::from_u8_masked(recovery_id); - let signature = - secp256k1::ecdsa::RecoverableSignature::from_compact(&sig_bytes, recid).ok()?; - let message = secp256k1::Message::from_digest(hash.0); - - // Same backend as `recover_sender`, for the same reason and so the - // two cannot drift apart on what a signature means. - let key = secp_context().recover_ecdsa(message, &signature).ok()?; - Some(key.serialize()) + match self.recover_key(chain_id).ok()? { + Recovered::Key(key) => Some(key.serialize()), + Recovered::Infinity => None, + } } /// Compute the Keccak256 hash of this transaction's RLP bytes. @@ -306,11 +372,15 @@ impl Transaction { /// /// Matches rskj's `BridgeUtils.recoverBtcAddressFromEthTransaction`: /// recovers the compressed public key from the ECDSA signature, then computes - /// `RIPEMD160(SHA256(compressed_pubkey))`. + /// `RIPEMD160(SHA256(compressed_pubkey))`. To rskj the point at infinity's + /// compressed encoding is the one byte `00` too, so a peg-out from + /// [`INFINITY_SENDER`] pays that byte's hash160. pub fn btc_sender_hash160(&self, chain_id: u64) -> Option<[u8; 20]> { use sha2::Digest as Sha2Digest; - let compressed = self.recover_compressed_pubkey(chain_id)?; - let sha256_hash = sha2::Sha256::digest(compressed); + let sha256_hash = match self.recover_key(chain_id).ok()? { + Recovered::Key(key) => sha2::Sha256::digest(key.serialize()), + Recovered::Infinity => sha2::Sha256::digest([0u8]), + }; let hash160 = ripemd::Ripemd160::digest(sha256_hash); let mut arr = [0u8; 20]; arr.copy_from_slice(&hash160); @@ -530,6 +600,117 @@ mod tests { let expected: Address = "0x7e5f4552091a69125d5dfcb7b8c2659029395bdf".parse().unwrap(); assert_eq!(sender, expected, "sender must recover from the original encoding"); } + + /// Testnet block #8,269,754's first transaction, where the testnet node + /// stopped on 8 November: a transfer for chain 31 (v = 97) whose signature + /// recovers the point at infinity. rskj takes it, from 0xdcc703c0...a470 + /// as the public node reports, and would pay a peg-out from it to the + /// hash160 of the one byte 00. + #[test] + fn a_testnet_transfer_whose_key_is_the_point_at_infinity_has_rskjs_sender() { + let hex = |text: &str| -> Vec { + (0..text.len()) + .step_by(2) + .map(|at| u8::from_str_radix(&text[at..at + 2], 16).unwrap()) + .collect() + }; + let mut inner = vec![0x0a]; + inner.extend(hex("8405f5e100")); + inner.extend(hex("825208")); + inner.extend(hex("947e5f4552091a69125d5dfcb7b8c2659029395bdf")); + inner.extend(hex("871c02a6fbc95200")); + inner.extend([0x80, 0x61]); + inner.extend(hex( + "a0cbe5079c44265dba4b8e6bb935f6ae95b7ba00df33ee4e27cdd709929de43037", + )); + inner.extend(hex( + "a05196f13a0727e24f3312944740d1a7c13ff62a61eae43840efb0869ce8f4bc93", + )); + let mut raw = Vec::new(); + alloy_rlp::Header { + list: true, + payload_length: inner.len(), + } + .encode(&mut raw); + raw.extend_from_slice(&inner); + + let tx = Transaction::decode(&mut raw.as_slice()).unwrap(); + assert_eq!( + tx.tx_hash(), + B256::from_slice(&hex( + "574c3ba99e8f4cee089e984d18ff4ef82fd737c08857836a053d187fb095d5d9" + )), + "the bytes are the transaction on chain" + ); + let expected: Address = "0xdcc703c0e500b653ca82273b7bfad8045d85a470" + .parse() + .unwrap(); + assert_eq!(tx.recover_sender(31).unwrap(), expected); + assert_eq!(tx.recover_compressed_pubkey(31), None); + let hash160: [u8; 20] = hex("9f7fd096d37ed2c0e3f7f0cfc924beef4ffceb68") + .try_into() + .unwrap(); + assert_eq!(tx.btc_sender_hash160(31), Some(hash160)); + } + + #[test] + fn the_infinity_sender_is_the_address_of_an_empty_key() { + assert_eq!(INFINITY_SENDER.as_slice(), &Keccak256::digest(b"")[12..]); + } + + /// A signature made so that sR = eG, with R the generator and s the + /// signing hash, recovers the point at infinity whatever the transaction; + /// the same r with y's other parity recovers an ordinary key. + #[test] + fn a_signature_made_so_that_sr_is_eg_recovers_the_infinity_sender() { + let mut tx = Transaction { + nonce: 3, + gas_price: U256::from(60_000_000u64), + gas_limit: U256::from(21_000), + to: Bytes::from(vec![0x22; 20]), + value: U256::from(1), + input: Bytes::new(), + v: 33 * 2 + 35, + r: U256::from_be_bytes(secp256k1::constants::GENERATOR_X), + s: U256::ZERO, + cached_rlp: None, + }; + let order = U256::from_be_bytes(secp256k1::constants::CURVE_ORDER); + tx.s = U256::from_be_bytes(tx.signing_hash_eip155(33).0) % order; + assert_eq!(tx.recover_sender(33).unwrap(), INFINITY_SENDER); + + tx.v += 1; + let other = tx.recover_sender(33).unwrap(); + assert_ne!(other, INFINITY_SENDER); + assert!(tx.recover_compressed_pubkey(33).is_some()); + } + + /// A signature whose R isn't on the curve, or whose s is zero, recovers no + /// key: rskj's native service, its default, recovers none from either, and + /// this refuses both, as it did before it took the point at infinity. + #[test] + fn a_signature_that_recovers_no_key_is_refused() { + let mut tx = Transaction { + nonce: 3, + gas_price: U256::from(60_000_000u64), + gas_limit: U256::from(21_000), + to: Bytes::from(vec![0x22; 20]), + value: U256::from(1), + input: Bytes::new(), + v: 33 * 2 + 35, + // 5³ + 7 has no square root modulo p. + r: U256::from(5), + s: U256::from(1), + cached_rlp: None, + }; + let error = tx.recover_sender(33).unwrap_err().to_string(); + assert!(error.contains("ECDSA recovery failed"), "{error}"); + assert_eq!(tx.btc_sender_hash160(33), None); + + tx.r = U256::from_be_bytes(secp256k1::constants::GENERATOR_X); + tx.s = U256::ZERO; + assert!(tx.recover_sender(33).is_err()); + } } impl Transaction { From cb042060974da16713ce07d0e45a4757ff508c2e Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Sun, 8 Nov 2026 13:31:23 -0800 Subject: [PATCH 032/264] rpc: check a contract's storage as a light client does The tests of rsk_getStorageNodes checked that its nodes chain up to the block's state root, through this node's own trie store. A light client has neither that store nor any reason to trust the node: it has a block hash it trusts, and the bytes it is sent. read_slot_as_a_light_client does what such a client does: it files each node under its own hash in an empty store, requires the top node to hash to the header's state root, and walks the slot's key with try_get, so a node left out of the answer fails the walk instead of reading as an absent slot. The test reads a contract's storage that way at two blocks, the second rewriting one slot, clearing one and writing a third, each from a raw header checked against the hash the client trusts. An answer with an interior node removed is refused as incomplete, and the earlier block's answer is refused against the later block's state root. --- crates/rpc/src/tests.rs | 115 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) diff --git a/crates/rpc/src/tests.rs b/crates/rpc/src/tests.rs index eea7f8f..1f97f92 100644 --- a/crates/rpc/src/tests.rs +++ b/crates/rpc/src/tests.rs @@ -6055,6 +6055,121 @@ fn assert_hangs_from(nodes: &Value, state_root: B256, trie_store: &dyn rustock_t } } +/// What a light client makes of one slot's nodes, with nothing else in hand +/// but the state root of a header it trusts: the nodes, each filed under its +/// own hash, must start at that root and walk along the slot's key to its +/// value, or to where the key leaves the trie. A node the answer leaves out +/// fails the walk rather than reading as "not there". +fn read_slot_as_a_light_client( + nodes: &Value, + state_root: B256, + key: &[u8], +) -> Result>, String> { + use rustock_trie::{MemoryTrieStore, TrieKeySlice, TrieNode, TrieStore}; + + let nodes: Vec> = nodes + .as_array() + .ok_or("no nodes")? + .iter() + .map(|n| { + hex::decode(n.as_str().unwrap_or_default().trim_start_matches("0x")) + .map_err(|e| e.to_string()) + }) + .collect::>()?; + let root = nodes.last().ok_or("no nodes")?; + if alloy_primitives::keccak256(root) != state_root { + return Err("the top node is not the header's state root".into()); + } + let proof = MemoryTrieStore::new(); + for node in &nodes { + TrieStore::put(&proof, alloy_primitives::keccak256(node).as_slice(), node); + } + let root = TrieNode::try_from_message(root, &proof).ok_or("the root does not parse")?; + root.try_get(&TrieKeySlice::from_key(key), &proof) + .map_err(|missing| format!("the answer leaves out node {missing:#x}")) +} + +/// **A contract's current storage, read as a light client reads it.** The +/// client trusts a block's hash and nothing the node says: it decodes the raw +/// header the node sends, checks that it hashes to that block, takes the +/// state root from it, and reads each slot from `rsk_getStorageNodes`'s nodes +/// alone. The storage changes in the next block -- a slot rewritten, one +/// cleared, one written for the first time -- and each block's answer proves +/// that block's values. An answer missing a node proves nothing, and the +/// earlier block's answer does not pass against the later block's root. +#[tokio::test] +async fn a_light_client_reads_a_contracts_storage_from_the_answer_alone() { + use rustock_trie::{storage_key, TrieKeySlice, TrieNode, TrieStore}; + + let (state, _tmp, addr, root_42) = setup_proof_state(); + let trie_store = state.trie_store.clone().unwrap(); + let key = |slot: u8| storage_key(&addr, &B256::with_last_byte(slot)); + let hash_42 = state.store.head().unwrap().unwrap(); + + // Block 43: slot 1 rewritten (42 -> 43), slot 2 cleared, slot 3 written. + let at_42 = trie_store.get(root_42.as_slice()).unwrap(); + let mut root = TrieNode::from_message(&at_42, &*trie_store) + .put(&TrieKeySlice::from_key(&key(1)), &[43], &*trie_store) + .delete(&TrieKeySlice::from_key(&key(2)), &*trie_store) + .put(&TrieKeySlice::from_key(&key(3)), &[7], &*trie_store); + root.save(&*trie_store, true); + let root_43 = root.compute_hash(&*trie_store); + TrieStore::put(&*trie_store, root_43.as_slice(), &root.to_message(&*trie_store)); + let header = Header { parent_hash: hash_42, state_root: root_43, ..test_header(43) }; + let hash_43 = header.hash(); + state.store.put_header(&header).unwrap(); + state.store.put_canonical_hash(43, hash_43).unwrap(); + state.store.set_head(hash_43).unwrap(); + + let mut answers = Vec::new(); + for (block, trusted, want) in [ + ("0x2a", hash_42, [Some(42u8), Some(99), None]), + ("0x2b", hash_43, [Some(43), None, Some(7)]), + ] { + let raw = dispatch_for_test(&state, make_request("rsk_getRawBlockHeaderByNumber", json!([block]))) + .await + .result + .unwrap(); + let raw = hex::decode(raw.as_str().unwrap().trim_start_matches("0x")).unwrap(); + let header = Header::decode_with_hash(&mut raw.as_slice()).unwrap(); + assert_eq!(header.hash(), trusted, "block {block}: the header is the trusted block's"); + + let req = make_request( + "rsk_getStorageNodes", + json!([format!("{addr:#x}"), ["0x1", "0x2", "0x3"], block]), + ); + let r = dispatch_for_test(&state, req).await.result.unwrap(); + for (i, want) in want.iter().enumerate() { + let slot = i as u8 + 1; + assert_eq!( + read_slot_as_a_light_client(&r["storage"][i]["nodes"], header.state_root, &key(slot)), + Ok(want.map(|v| vec![v])), + "block {block}, slot {slot}" + ); + } + answers.push((header.state_root, r)); + } + + let (root_43, now) = &answers[1]; + // The node under the root: an interior node, so the root holds its hash + // rather than the node itself, as a small leaf's parent may. + let mut short = now["storage"][0]["nodes"].clone(); + let nodes = short.as_array_mut().unwrap(); + nodes.remove(nodes.len() - 2); + let missing = read_slot_as_a_light_client(&short, *root_43, &key(1)); + assert!( + matches!(&missing, Err(why) if why.starts_with("the answer leaves out node")), + "an answer missing a node on the slot's path proves nothing, not absence: {missing:?}" + ); + let before = &answers[0].1; + let stale = read_slot_as_a_light_client(&before["storage"][0]["nodes"], *root_43, &key(1)); + assert_eq!( + stale, + Err("the top node is not the header's state root".to_string()), + "block 42's answer is not block 43's state" + ); +} + /// A slot the state holds: present, its value, and its path. #[tokio::test] async fn storage_nodes_prove_a_slot_that_is_there() { From 1c6282785cd569e89d3990e7c255a28fd31e0075 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Sun, 8 Nov 2026 13:41:07 -0800 Subject: [PATCH 033/264] Measure a thread's CPU time on macOS and Windows too thread_cpu_time read CLOCK_THREAD_CPUTIME_ID on Linux and returned zero everywhere else, so on macOS and Windows the chain-activity report's CPU figures, cost per gas among them, were always zero, and thread_cpu_time_advances_while_working failed there. macOS has the same clock from 10.12. Windows has none: GetThreadTimes gives the thread's kernel and user time in 100 ns units, advancing by scheduler ticks of about 15.6 ms, which the report's averages over many blocks absorb. Other platforms still report zero. --- crates/sync/src/chain_activity.rs | 52 ++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/crates/sync/src/chain_activity.rs b/crates/sync/src/chain_activity.rs index 9bc6b27..03915f9 100644 --- a/crates/sync/src/chain_activity.rs +++ b/crates/sync/src/chain_activity.rs @@ -17,19 +17,63 @@ use std::time::Duration; /// Distinct from wall time on purpose: wall time includes waiting on the trie /// database, CPU time does not. Tracking both means the two can be compared, /// and the gap between them is where the node is waiting rather than working. -#[cfg(target_os = "linux")] +#[cfg(any(target_os = "linux", target_os = "macos"))] pub fn thread_cpu_time() -> Duration { let mut ts = libc::timespec { tv_sec: 0, tv_nsec: 0 }; // SAFETY: `ts` is a valid, initialised timespec and CLOCK_THREAD_CPUTIME_ID - // is always available on Linux. A failure leaves `ts` zeroed, which reports - // no CPU time rather than a wrong figure. + // is available on Linux and on macOS from 10.12. A failure leaves `ts` + // zeroed, which reports no CPU time rather than a wrong figure. unsafe { libc::clock_gettime(libc::CLOCK_THREAD_CPUTIME_ID, &mut ts) }; Duration::new(ts.tv_sec as u64, ts.tv_nsec as u32) } +/// Windows has no thread CPU clock to read; `GetThreadTimes` gives the +/// thread's kernel and user time instead, in the 100 ns units a FILETIME +/// counts. It advances by scheduler ticks of about 15.6 ms, so a stretch of +/// work shorter than a tick can read as none; the figures it feeds are +/// averages over many blocks. +#[cfg(windows)] +pub fn thread_cpu_time() -> Duration { + #[repr(C)] + #[derive(Default, Clone, Copy)] + struct FileTime { + low: u32, + high: u32, + } + #[link(name = "kernel32")] + unsafe extern "system" { + fn GetCurrentThread() -> *mut std::ffi::c_void; + fn GetThreadTimes( + thread: *mut std::ffi::c_void, + creation: *mut FileTime, + exit: *mut FileTime, + kernel: *mut FileTime, + user: *mut FileTime, + ) -> i32; + } + let (mut creation, mut exit, mut kernel, mut user) = Default::default(); + // SAFETY: GetCurrentThread's pseudo-handle stands for the calling thread + // and needs no closing, and the four out-pointers are to valid FILETIMEs. + // A failure, zero, reports no CPU time rather than a wrong figure. + let ok = unsafe { + GetThreadTimes( + GetCurrentThread(), + &mut creation, + &mut exit, + &mut kernel, + &mut user, + ) + }; + if ok == 0 { + return Duration::ZERO; + } + let ticks = |t: FileTime| (u64::from(t.high) << 32) | u64::from(t.low); + Duration::from_nanos((ticks(kernel) + ticks(user)).saturating_mul(100)) +} + /// Every other platform reports zero, so the ratios read as unavailable rather /// than wrong. -#[cfg(not(target_os = "linux"))] +#[cfg(not(any(target_os = "linux", target_os = "macos", windows)))] pub fn thread_cpu_time() -> Duration { Duration::ZERO } From 804695851cff840d7ef757f8a7cb104e71d5e90f Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Sun, 8 Nov 2026 13:45:50 -0800 Subject: [PATCH 034/264] flatdb: lock and reserve its files on Windows too rustock-flatdb did not compile on Windows: it locked the database directory with rustix's flock and reserved each segment with rustix's fallocate, and rustix has neither there. The lock is std's File::try_lock: flock on Unix, as before, and LockFileEx on Windows, and a lock held elsewhere still reads as WouldBlock. reserve keeps fallocate on Unix; on Windows it is set_len, the fallback Unix already takes where a filesystem refuses fallocate, so rustix becomes a Unix-only dependency. --- crates/flatdb/Cargo.toml | 5 ++++- crates/flatdb/src/heap.rs | 7 +++++++ crates/flatdb/src/lock.rs | 9 +++++---- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/crates/flatdb/Cargo.toml b/crates/flatdb/Cargo.toml index acdd919..b0fd747 100644 --- a/crates/flatdb/Cargo.toml +++ b/crates/flatdb/Cargo.toml @@ -12,11 +12,14 @@ workspace = true [dependencies] memmap2 = "0.9" crc32fast = "1" -rustix = { version = "1", features = ["fs"] } sha3 = { workspace = true } thiserror = { workspace = true } tracing = { workspace = true } +# `fallocate`, which only Unix has; Windows reserves a segment with `set_len`. +[target.'cfg(unix)'.dependencies] +rustix = { version = "1", features = ["fs"] } + [dev-dependencies] tempfile = { workspace = true } proptest = "1" diff --git a/crates/flatdb/src/heap.rs b/crates/flatdb/src/heap.rs index 4c25a01..52613c7 100644 --- a/crates/flatdb/src/heap.rs +++ b/crates/flatdb/src/heap.rs @@ -539,6 +539,7 @@ impl Heap { /// `fallocate` is Linux-specific and some filesystems refuse it; falling /// back to `set_len` leaves a sparse file, which is the behaviour the Java /// version has unconditionally. +#[cfg(unix)] fn reserve(file: &std::fs::File, len: u64) -> io::Result<()> { match rustix::fs::fallocate(file, rustix::fs::FallocateFlags::empty(), 0, len) { Ok(()) => Ok(()), @@ -547,6 +548,12 @@ fn reserve(file: &std::fs::File, len: u64) -> io::Result<()> { } } +/// Windows has no `fallocate`: `set_len`, the fallback above, is what it gets. +#[cfg(not(unix))] +fn reserve(file: &std::fs::File, len: u64) -> io::Result<()> { + file.set_len(len) +} + #[cfg(test)] mod tests { use super::*; diff --git a/crates/flatdb/src/lock.rs b/crates/flatdb/src/lock.rs index d454d24..c2616d4 100644 --- a/crates/flatdb/src/lock.rs +++ b/crates/flatdb/src/lock.rs @@ -3,7 +3,8 @@ //! Two processes mapping the same heap would each believe they owned the //! write frontier. It is also the soundness argument for every `unsafe` //! mapping in this crate: no cooperating process truncates or rewrites a -//! file while it is mapped. `flock` is released by the kernel when the +//! file while it is mapped. The lock is `File::try_lock`: `flock` on Unix, +//! `LockFileEx` on Windows. Either is released by the system when the //! process dies, so a crash does not leave the directory unopenable. use std::fs::File; @@ -22,13 +23,13 @@ impl DbLock { .create(true) .truncate(false) .open(path)?; - match rustix::fs::flock(&file, rustix::fs::FlockOperation::NonBlockingLockExclusive) { + match file.try_lock() { Ok(()) => Ok(Self { _file: file }), - Err(rustix::io::Errno::WOULDBLOCK) => Err(io::Error::new( + Err(std::fs::TryLockError::WouldBlock) => Err(io::Error::new( io::ErrorKind::WouldBlock, format!("database is already open: {}", path.display()), )), - Err(e) => Err(io::Error::from(e)), + Err(std::fs::TryLockError::Error(e)) => Err(e), } } } From 8f2273e448472f08014852ffd9a35b00eee74228 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Sun, 8 Nov 2026 13:57:29 -0800 Subject: [PATCH 035/264] alerts: build on Windows, where an env file's mode is not checked read_env_file refuses an env file that group or others can read, by its Unix mode bits, and on Windows the metadata it read for them went unused, which -D warnings made an error, as was the test helper's unused mode. The metadata is read inside the Unix check, so Windows reads only the file. Windows keeps access in ACLs, which the check does not read, so there the file is not checked, as before, and the test of the refusal runs on Unix only. --- crates/alerts/src/config.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/crates/alerts/src/config.rs b/crates/alerts/src/config.rs index 2614d80..74efac0 100644 --- a/crates/alerts/src/config.rs +++ b/crates/alerts/src/config.rs @@ -576,11 +576,13 @@ impl Config { /// may be quoted, and a `export ` prefix is tolerated so the same file can be /// sourced by a shell. fn read_env_file(path: &Path) -> anyhow::Result> { - let meta = std::fs::metadata(path) - .map_err(|e| anyhow::anyhow!("reading {}: {e}", path.display()))?; + // Group and other permissions are Unix mode bits. Windows keeps access in + // ACLs, which this does not read, so there the file is not checked. #[cfg(unix)] { use std::os::unix::fs::PermissionsExt; + let meta = std::fs::metadata(path) + .map_err(|e| anyhow::anyhow!("reading {}: {e}", path.display()))?; let mode = meta.permissions().mode() & 0o077; if mode != 0 { // Refuse rather than warn: a credential readable by other users on @@ -801,6 +803,8 @@ mod secret_tests { use std::os::unix::fs::PermissionsExt; std::fs::set_permissions(f.path(), std::fs::Permissions::from_mode(mode)).unwrap(); } + #[cfg(not(unix))] + let _ = mode; f } @@ -825,6 +829,8 @@ mod secret_tests { assert_eq!(c.email.password.as_deref(), Some("s3cr3t")); } + /// Unix only: the refusal reads mode bits, which Windows does not have. + #[cfg(unix)] #[test] fn a_world_readable_env_file_is_refused() { let f = env_file("SMTP_PASSWORD=s3cr3t\n", 0o644); From 4390d730b9513a9dddcbd7599d51302761c5b428 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Sun, 8 Nov 2026 13:57:30 -0800 Subject: [PATCH 036/264] cli: run the node on a thread with an 8 MiB stack on every OS main ran the node on the process's main thread, whose stack is the OS's: 8 MiB on Linux and macOS, 1 MiB on Windows. A debug build overflows 1 MiB while clap builds the parser for the CLI's 136 options, so on Windows even rustock-cli --version crashed, and so did every test that runs the binary (the four in tests/import_blocks.rs). The node now runs on a thread with an 8 MiB stack, the stack Linux and macOS already gave it, set by the node rather than by the OS. The local offset is still read before any thread is spawned, a panic on the thread still ends the process with it, and the thread is named main, so logs and panic messages read as before. --- crates/cli/src/main.rs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index 5b9a81a..d1002ce 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -1711,10 +1711,21 @@ fn resolve_local_offset() -> Option { fn main() -> Result<()> { // Before anything spawns a thread. let local_offset = resolve_local_offset(); - tokio::runtime::Builder::new_multi_thread() - .enable_all() - .build()? - .block_on(run(local_offset)) + // The node runs on a thread with a stack it sets, the same on every OS. + // The process's own main thread has the OS's: 8 MiB on Linux and macOS but + // 1 MiB on Windows, which a debug build overflows while clap builds the + // parser for this many options. + std::thread::Builder::new() + .name("main".into()) + .stack_size(8 << 20) + .spawn(move || { + tokio::runtime::Builder::new_multi_thread() + .enable_all() + .build()? + .block_on(run(local_offset)) + })? + .join() + .unwrap_or_else(|panic| std::panic::resume_unwind(panic)) } async fn run(local_offset: Option) -> Result<()> { From 4f268cf509989e68426508f6fe328110c2e74f21 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Sun, 8 Nov 2026 15:35:07 -0800 Subject: [PATCH 037/264] hardfork: rules activate where rskj's config files put them An upgrade ladder says when a rule is on only on a network that runs every rung of it. Two networks skip one: mainnet never schedules reed810 and testnet never schedules arrowhead631, and on each the next upgrade outranks the skipped one, so `active_upgrade() >= X` reads as true there for good. And rskj's network files move some rules off their upgrade altogether. One Bridge gate and three predicates were wrong: - The union bridge's seven methods (RSKIP502, reed810) were gated `Since(Reed810)`, so on mainnet they became methods at #8,804,200, when vetiver900 arrived. rskj never enables them there: its parseData finds no method, so a call to one fails (a direct transaction paying its whole gas limit) while rustock executed it. One such call on mainnet would have split rustock off the chain. They now follow has_rskip502: reed810 scheduled, which is testnet from #7,139,600. - RSKIP284 is at #2,581,800 on testnet (testnet.conf), not at hop400 (#3,103,000). Besides the BTC address checks, it moves testnet's federation UTXOs into the pre-hop cell `newFederationBtcUTXOsForTestnet` for the 521,200 blocks before RSKIP293. rustock had the cell but, with RSKIP284 at hop400, never reached it. RSKj's public testnet node shows the cell empty through #2,581,800 and written at #2,581,801. This matters for a testnet sync from genesis. - RSKIP434 asked `>= Arrowhead631`, true on testnet from lovell700, where rskj never activates it. Inert, since the guard it lifts applies only to Bitcoin mainnet. `all_active` now names arrowhead631, so tests keep it on. - RSKIP535 was on from vetiver900 on mainnet. reference.conf has it at -1 and main.conf never sets it; testnet.conf sets 7,604,200 and regtest.conf 1. Nothing in consensus reads it yet. A new test holds 68 predicates to rskj 9.1's schedule on both networks: each must turn on exactly at rskj's height or, where rskj says -1, at none of the heights either network schedules anything at. has_rskip502 and has_rskip529 (RSKIP502's schedule) are added for the Bridge. rustock's regtest does not schedule reed810, as rskj's regtest.conf does, so the union bridge's methods are now off there, as RSKIP144 and RSKIP351 already were. --- crates/execution/src/bridge/mod.rs | 47 +++++- crates/execution/src/bridge/storage.rs | 31 ++-- crates/execution/src/hardfork.rs | 216 ++++++++++++++++++++++++- 3 files changed, 276 insertions(+), 18 deletions(-) diff --git a/crates/execution/src/bridge/mod.rs b/crates/execution/src/bridge/mod.rs index 0297ae4..0000343 100644 --- a/crates/execution/src/bridge/mod.rs +++ b/crates/execution/src/bridge/mod.rs @@ -248,6 +248,10 @@ pub enum BridgeMethodEnabled { Since(crate::hardfork::RskNetworkUpgrade), /// Enabled only BEFORE the given network upgrade (legacy methods). Until(crate::hardfork::RskNetworkUpgrade), + /// Enabled where the rule's own predicate says so. For a rule whose + /// upgrade a network skips, which `Since` would read as implied by any + /// later upgrade there (see `RskHardforkConfig::has_rskip144`). + Rule(fn(&RskHardforkConfig, u64) -> bool), } impl BridgeMethodEnabled { @@ -256,6 +260,7 @@ impl BridgeMethodEnabled { Self::Always => true, Self::Since(u) => hardfork_cfg.active_upgrade(block_number) >= *u, Self::Until(u) => hardfork_cfg.active_upgrade(block_number) < *u, + Self::Rule(active) => active(hardfork_cfg, block_number), } } } @@ -268,14 +273,16 @@ fn method_enabled(name: &str) -> BridgeMethodEnabled { use crate::hardfork::RskNetworkUpgrade::*; use BridgeMethodEnabled::*; match name { - // RSKIP502 (reed810): the union bridge. + // RSKIP502 (reed810): the union bridge. By reed810's schedule, which + // mainnet never sets: `Since(Reed810)` read its vetiver900 as + // implying it. "setUnionBridgeContractAddressForTestnet" | "getUnionBridgeContractAddress" | "getUnionBridgeLockingCap" | "increaseUnionBridgeLockingCap" | "requestUnionBridgeRbtc" | "releaseUnionBridgeRbtc" - | "setUnionBridgeTransferPermissions" => Since(Reed810), + | "setUnionBridgeTransferPermissions" => Rule(RskHardforkConfig::has_rskip502), // !RSKIP87 / !RSKIP89 "addLockWhitelistAddress" | "getBtcBlockchainBlockLocator" => Until(Orchid), // RSKIP87 / RSKIP89 @@ -1291,6 +1298,42 @@ mod tests { assert!(!method_only_allows_local_calls(dynm.permission, &cfg, post_iris)); } + /// The union bridge's selectors follow reed810's schedule, which mainnet + /// never sets. Its vetiver900 outranks reed810, so the upgrade-order gate + /// these had made them methods on mainnet from #8,804,200, where rskj's + /// `parseData` finds no method: a call to one would have forked the node. + #[test] + fn the_union_bridge_is_never_a_method_on_mainnet() { + use crate::hardfork::RskHardforkConfig; + let mainnet = RskHardforkConfig::mainnet(); + let testnet = RskHardforkConfig::testnet(); + for signature in [ + "setUnionBridgeContractAddressForTestnet(address)", + "getUnionBridgeContractAddress()", + "getUnionBridgeLockingCap()", + "increaseUnionBridgeLockingCap(uint256)", + "requestUnionBridgeRbtc(uint256)", + "releaseUnionBridgeRbtc()", + "setUnionBridgeTransferPermissions(bool,bool)", + ] { + let m = find_bridge_method(&compute_selector(signature)).unwrap(); + for n in [8_804_199, 8_804_200, 9_500_000, u64::MAX] { + assert!( + !m.enabled.is_enabled(&mainnet, n), + "{signature} on mainnet at #{n}" + ); + } + assert!( + !m.enabled.is_enabled(&testnet, 7_139_599), + "{signature} before reed810" + ); + assert!( + m.enabled.is_enabled(&testnet, 7_139_600), + "{signature} at testnet's reed810" + ); + } + } + /// A well-formed `receiveHeaders(bytes[])` with one empty header element /// must decode successfully (no false parse failure). #[test] diff --git a/crates/execution/src/bridge/storage.rs b/crates/execution/src/bridge/storage.rs index eb6491e..89a3d41 100644 --- a/crates/execution/src/bridge/storage.rs +++ b/crates/execution/src/bridge/storage.rs @@ -549,9 +549,9 @@ pub fn deserialize_utxo_list(data: &[u8]) -> Vec { /// ``` /// /// and for testnet, the general key before RSKIP284, the pre-hop key between -/// RSKIP284 and RSKIP293, the post-hop key after. Both activate at hop400 -/// (testnet #3,103,000), so the middle case is unreachable there -- it is -/// modelled anyway because the rule is rskj's, not the schedule's. +/// RSKIP284 and RSKIP293, the post-hop key after. `testnet.conf` moves +/// RSKIP284 to #2,581,800 while RSKIP293 comes with hop400 at #3,103,000, so +/// the pre-hop key held testnet's UTXOs for the 521,200 blocks between. /// /// Mainnet never moved: its UTXOs stayed under the general key throughout. /// @@ -621,20 +621,33 @@ mod utxo_key_tests { /// Testnet moved the active federation's UTXOs twice; mainnet never did. /// - /// Ground truth: testnet #7,139,903 is a pegout batch where rskj spends - /// from `newFedBtcUTXOsForTestnetPostHop` and leaves + /// `testnet.conf` puts RSKIP284 at #2,581,800 and RSKIP293 comes with + /// hop400 at #3,103,000, so the pre-hop cell holds them in between. RSKj's + /// public testnet node shows it: `newFederationBtcUTXOsForTestnet` is + /// empty through #2,581,800 and an RLP list (`0xc0`) from #2,581,801. With + /// RSKIP284 read at hop400, the two moves coincided and that cell was + /// never reached. + /// + /// Ground truth for the second move: testnet #7,139,903 is a pegout batch + /// where rskj spends from `newFedBtcUTXOsForTestnetPostHop` and leaves /// `newFederationBtcUTXOs` untouched. Reading the general cell there /// selects different inputs, so the pegout transaction, its hash and its /// outpoint values all come out different. #[test] - fn testnet_moves_to_the_post_hop_cell_at_hop400() { + fn testnet_moves_through_the_pre_hop_cell_to_the_post_hop_cell() { let cfg = RskHardforkConfig::testnet(); - // hop400 on testnet is #3,103,000; RSKIP284 and RSKIP293 both land there. assert_eq!( - new_federation_utxos_key(&cfg, 3_102_999), + new_federation_utxos_key(&cfg, 2_581_799), NEW_FEDERATION_BTC_UTXOS_KEY, - "before hop400 testnet used the general cell" + "before RSKIP284 testnet used the general cell" ); + for h in [2_581_800, 3_000_000, 3_102_999] { + assert_eq!( + new_federation_utxos_key(&cfg, h), + NEW_FEDERATION_BTC_UTXOS_TESTNET_PRE_HOP_KEY, + "between RSKIP284 and hop400, at #{h}" + ); + } assert_eq!( new_federation_utxos_key(&cfg, 3_103_000), NEW_FEDERATION_BTC_UTXOS_TESTNET_POST_HOP_KEY diff --git a/crates/execution/src/hardfork.rs b/crates/execution/src/hardfork.rs index b310e87..05bfa6d 100644 --- a/crates/execution/src/hardfork.rs +++ b/crates/execution/src/hardfork.rs @@ -177,9 +177,16 @@ impl RskHardforkConfig { /// gated on `reed810` being *scheduled*, and naming Vetiver900 alone /// does not schedule it -- see `has_rskip144`. A test that wants /// parallel sublists wants [`RskHardforkConfig::all_active_parallel`]. + /// + /// Arrowhead631 is named for the same reason: testnet never schedules + /// it, so RSKIP-434 asks whether it is scheduled, not whether a later + /// upgrade is active. pub fn all_active(chain_id: u64) -> Self { Self { - activations: vec![(0, RskNetworkUpgrade::Vetiver900)], + activations: vec![ + (0, RskNetworkUpgrade::Arrowhead631), + (0, RskNetworkUpgrade::Vetiver900), + ], chain_id, rule_overrides: Vec::new(), } @@ -523,7 +530,15 @@ impl RskHardforkConfig { /// RSKIP284: BTC address-with-version serialization uses a single version /// byte instead of `BigInteger.toByteArray()` (Hop400, mainnet 4_598_500). + /// + /// Testnet moves it: `testnet.conf` sets `rskip284 = 2581800`, 521,200 + /// blocks before testnet's hop400. Among other things it switches + /// testnet's ERP redeem script off the hard-coded one + /// (`NonStandardErpRedeemScriptBuilderFactory`). pub fn has_rskip284(&self, block_number: u64) -> bool { + if self.chain_id == RSK_TESTNET_CHAIN_ID { + return block_number >= 2_581_800; + } self.active_upgrade(block_number) >= RskNetworkUpgrade::Hop400 } @@ -605,8 +620,16 @@ impl RskHardforkConfig { } /// RSKIP434: lifts the BTC chainwork-overflow header block (Arrowhead631). + /// + /// Whether arrowhead631 is scheduled, not an ordinal comparison: testnet + /// never schedules it (`arrowhead631 = -1` in `testnet.conf`, and in + /// `regtest.conf`), and its lovell700 outranks it, so `>= Arrowhead631` + /// turned this on at testnet #6,110,487. Inert there so far, since the + /// guard it lifts only applies to Bitcoin mainnet, but it is the trap + /// `has_rskip144` describes. pub fn has_rskip434(&self, block_number: u64) -> bool { - self.active_upgrade(block_number) >= RskNetworkUpgrade::Arrowhead631 + self.activation_height(RskNetworkUpgrade::Arrowhead631) + .is_some_and(|h| block_number >= h) } /// RSKIP415: the `add_signature` event's `federatorRskAddress` topic is @@ -807,16 +830,42 @@ impl RskHardforkConfig { .is_some_and(|h| block_number >= h) } + /// RSKIP502: the union bridge's seven Bridge methods (reed810). + /// + /// Not an ordinal comparison, for `has_rskip144`'s reason: mainnet + /// never schedules reed810, and nothing in `main.conf` moves this rule, + /// so rskj keeps the methods off there for good. `>= Reed810` had them + /// on from mainnet #8,804,200, where vetiver900 outranks reed810: a + /// call that rskj fails as an unknown method, rustock executed. + pub fn has_rskip502(&self, block_number: u64) -> bool { + self.activation_height(RskNetworkUpgrade::Reed810) + .is_some_and(|h| block_number >= h) + } + + /// RSKIP529: the union bridge's super and base events, six more Bridge + /// methods on RSKIP502's schedule (reed810, never on mainnet). + pub fn has_rskip529(&self, block_number: u64) -> bool { + self.activation_height(RskNetworkUpgrade::Reed810) + .is_some_and(|h| block_number >= h) + } + /// RSKIP535: the `baseEvent` header-extension field, which makes the /// header version 2. /// - /// `testnet.conf` sets `rskip535 = 7604200`, the vetiver900 height, with - /// the comment "To be activated in testnet only". Mainnet does not set it. + /// Each network schedules it on its own, apart from any upgrade: + /// `reference.conf` has `rskip535 = -1`, `testnet.conf` sets `7604200` + /// (the vetiver900 height, "To be activated in testnet only") and + /// `regtest.conf` sets `1`. Mainnet does not set it, so it never + /// activates there -- vetiver900 does not imply it. pub fn has_rskip535(&self, block_number: u64) -> bool { if let Some(height) = self.rule_override(ConsensusRule::Rskip535) { return height.is_some_and(|h| block_number >= h); } - self.active_upgrade(block_number) >= RskNetworkUpgrade::Vetiver900 + match self.chain_id { + RSK_MAINNET_CHAIN_ID => false, + RSK_TESTNET_CHAIN_ID => block_number >= 7_604_200, + _ => block_number >= 1, + } } /// RSKIP536: additional BlockHeader precompile methods. Maps to reed810 in @@ -1311,8 +1360,9 @@ mod override_tests { assert_eq!(cfg.activation_height(RskNetworkUpgrade::Reed810), None); assert!(!cfg.has_rskip144(9_003_580)); assert!(!cfg.has_rskip351(9_003_580)); - assert!(cfg.has_rskip535(8_804_200)); - assert!(!cfg.has_rskip535(8_804_199)); + // main.conf never sets rskip535 and reference.conf has it at -1. + assert!(!cfg.has_rskip535(8_804_200)); + assert!(!cfg.has_rskip535(9_003_580)); assert!(cfg.has_rskip536(8_804_200)); assert_eq!(cfg.active_upgrade(8_804_200), RskNetworkUpgrade::Vetiver900); assert_eq!(cfg.active_upgrade(8_804_199), RskNetworkUpgrade::Reed800); @@ -1352,3 +1402,155 @@ mod override_tests { assert_eq!(cfg.active_upgrade(7_604_200), RskNetworkUpgrade::Vetiver900); } } + +/// Every rule this config answers for by predicate, against the height rskj +/// activates it at on each network: `reference.conf` maps a rule to an +/// upgrade (or a number), and `config/main.conf` and `config/testnet.conf` +/// give the upgrades' heights and the `consensusRules` that move a rule off +/// its upgrade. +/// +/// `activation_heights_match_rskj_config` pins the upgrades. This pins the +/// rules, which is where the two part: a network can move a rule off its +/// upgrade (testnet's rskip132, rskip284 and rskip535, mainnet's rskip536), +/// and an upgrade a network never schedules (mainnet's reed810, testnet's +/// arrowhead631) is outranked there by the ones after it, so an ordinal +/// `active_upgrade() >= X` turns its rules on where rskj never does. +#[cfg(test)] +mod rskj_schedule_tests { + use super::*; + + type Predicate = fn(&RskHardforkConfig, u64) -> bool; + + /// (rule, predicate, mainnet, testnet), from rskj 9.1's config files. + /// `None` is rskj's `-1`: never. + #[rustfmt::skip] + const SCHEDULE: &[(&str, Predicate, Option, Option)] = &[ + ("areBridgeTxsPaid", RskHardforkConfig::has_are_bridge_txs_paid, Some(370_000), Some(114_000)), + ("rskip87", RskHardforkConfig::has_rskip87, Some(729_000), Some(0)), + ("rskip88", RskHardforkConfig::has_rskip88, Some(729_000), Some(0)), + ("rskip90", RskHardforkConfig::has_rskip90, Some(729_000), Some(0)), + ("rskip91", RskHardforkConfig::has_rskip91, Some(729_000), Some(0)), + ("rskip123", RskHardforkConfig::has_rskip123, Some(1_591_000), Some(0)), + ("rskip124", RskHardforkConfig::has_rskip124, Some(1_591_000), Some(0)), + ("rskip125", RskHardforkConfig::has_rskip125, Some(1_591_000), Some(0)), + ("rskip126", RskHardforkConfig::has_unitrie_state_root, Some(1_591_000), Some(0)), + ("rskip132", RskHardforkConfig::has_rskip132, Some(1_591_000), Some(43_550)), + ("rskip134", RskHardforkConfig::has_rskip134, Some(2_392_700), Some(863_000)), + ("rskip136", RskHardforkConfig::has_rskip136, Some(3_397), Some(0)), + ("rskip140", RskHardforkConfig::has_rskip140, Some(2_392_700), Some(863_000)), + ("rskip143", RskHardforkConfig::has_rskip143, Some(2_392_700), Some(863_000)), + ("rskip144", RskHardforkConfig::has_rskip144, None, Some(7_139_600)), + ("rskip146", RskHardforkConfig::has_rskip146, Some(2_392_700), Some(863_000)), + ("rskip150", RskHardforkConfig::has_rskip150, Some(2_018_000), Some(504_000)), + ("rskip152", RskHardforkConfig::has_chainid, Some(2_392_700), Some(863_000)), + ("rskip170", RskHardforkConfig::has_rskip170, Some(3_614_800), Some(2_060_500)), + ("rskip171", RskHardforkConfig::has_rskip171, Some(3_614_800), Some(2_060_500)), + ("rskip174", RskHardforkConfig::has_rskip174, Some(3_614_800), Some(2_060_500)), + ("rskip176", RskHardforkConfig::has_rskip176, Some(3_614_800), Some(2_060_500)), + ("rskip181", RskHardforkConfig::has_rskip181, Some(3_614_800), Some(2_060_500)), + ("rskip185", RskHardforkConfig::has_rskip185, Some(3_614_800), Some(2_060_500)), + ("rskip186", RskHardforkConfig::has_rskip186, Some(3_614_800), Some(2_060_500)), + ("rskip197", RskHardforkConfig::has_rskip197, Some(3_614_800), Some(2_060_500)), + ("rskip199", RskHardforkConfig::has_rskip199, Some(3_614_800), Some(2_060_500)), + ("rskip200", RskHardforkConfig::has_rskip200, Some(3_614_800), Some(2_060_500)), + ("rskip201", RskHardforkConfig::has_rskip201, Some(3_614_800), Some(2_060_500)), + ("rskip219", RskHardforkConfig::has_rskip219, Some(3_614_800), Some(2_060_500)), + ("rskip271", RskHardforkConfig::has_rskip271, Some(4_598_500), Some(3_103_000)), + ("rskip284", RskHardforkConfig::has_rskip284, Some(4_598_500), Some(2_581_800)), + ("rskip293", RskHardforkConfig::has_rskip293, Some(4_598_500), Some(3_103_000)), + ("rskip294", RskHardforkConfig::has_rskip294, Some(4_598_500), Some(3_103_000)), + ("rskip305", RskHardforkConfig::has_rskip305, Some(8_052_200), Some(6_835_700)), + ("rskip326", RskHardforkConfig::has_rskip326, Some(5_468_000), Some(4_015_800)), + ("rskip351", RskHardforkConfig::has_rskip351, None, Some(7_139_600)), + ("rskip353", RskHardforkConfig::has_rskip353, Some(4_976_300), Some(3_362_200)), + ("rskip357", RskHardforkConfig::has_rskip357, Some(4_976_300), Some(3_362_200)), + ("rskip374", RskHardforkConfig::has_rskip374, Some(5_468_000), Some(4_015_800)), + ("rskip375", RskHardforkConfig::has_rskip375, Some(5_468_000), Some(4_015_800)), + ("rskip376", RskHardforkConfig::has_rskip376, Some(6_223_700), Some(4_927_100)), + ("rskip377", RskHardforkConfig::has_rskip377, Some(5_468_000), Some(4_015_800)), + ("rskip379", RskHardforkConfig::has_rskip379, Some(6_223_700), Some(4_927_100)), + ("rskip383", RskHardforkConfig::has_rskip383, Some(5_468_000), Some(4_015_800)), + ("rskip385", RskHardforkConfig::has_rskip385, Some(5_468_000), Some(4_015_800)), + ("rskip398", RskHardforkConfig::has_push0, Some(6_223_700), Some(4_927_100)), + ("rskip400", RskHardforkConfig::has_reduced_calldata_cost, Some(6_223_700), Some(4_927_100)), + ("rskip412", RskHardforkConfig::has_basefee, Some(6_223_700), Some(4_927_100)), + ("rskip415", RskHardforkConfig::has_rskip415, Some(6_223_700), Some(4_927_100)), + ("rskip419", RskHardforkConfig::has_rskip419, Some(7_338_024), Some(6_110_487)), + ("rskip427", RskHardforkConfig::has_rskip427, Some(7_338_024), Some(6_110_487)), + ("rskip428", RskHardforkConfig::has_rskip428, Some(7_338_024), Some(6_110_487)), + ("rskip434", RskHardforkConfig::has_rskip434, Some(6_549_300), None), + ("rskip438", RskHardforkConfig::has_initcode_metering, Some(7_338_024), Some(6_110_487)), + ("rskip445", RskHardforkConfig::has_rskip445, Some(7_338_024), Some(6_110_487)), + ("rskip446", RskHardforkConfig::has_rskip446, Some(7_338_024), Some(6_110_487)), + ("rskip453", RskHardforkConfig::has_rskip453, Some(7_338_024), Some(6_110_487)), + ("rskip454", RskHardforkConfig::has_stored_block_v2, Some(7_338_024), Some(6_110_487)), + ("rskip459", RskHardforkConfig::has_rskip459, Some(7_338_024), Some(6_110_487)), + ("rskip502", RskHardforkConfig::has_rskip502, None, Some(7_139_600)), + ("rskip529", RskHardforkConfig::has_rskip529, None, Some(7_139_600)), + ("rskip535", RskHardforkConfig::has_rskip535, None, Some(7_604_200)), + ("rskip536", RskHardforkConfig::has_rskip536, Some(8_804_200), Some(7_139_600)), + ("rskip540", RskHardforkConfig::has_rskip540, Some(8_804_200), Some(7_604_200)), + ("rskip544", RskHardforkConfig::has_rskip544, Some(8_804_200), Some(7_604_200)), + ("rskip551", RskHardforkConfig::has_rskip551, Some(8_804_200), Some(7_604_200)), + ("rskip552", RskHardforkConfig::has_rskip552, Some(8_804_200), Some(7_604_200)), + ]; + + /// Far past every height either network schedules anything at. + const LATER: u64 = 50_000_000; + + /// Where a rule rskj never turns on could wrongly be on: every height + /// either network schedules an upgrade or moves a rule to, and later. + fn probes() -> Vec { + let mut at: Vec = MAINNET_ACTIVATIONS + .iter() + .chain(TESTNET_ACTIVATIONS) + .map(|(h, _)| *h) + .collect(); + at.extend([43_550, 2_581_800, 7_604_200, 8_804_200, LATER]); + at + } + + fn check(cfg: &RskHardforkConfig, active: Predicate, rskj: Option) -> Result<(), String> { + match rskj { + Some(h) => { + if h > 0 && active(cfg, h - 1) { + return Err(format!("already on at #{}", h - 1)); + } + if !active(cfg, h) { + return Err(format!("off at #{h}")); + } + if !active(cfg, LATER) { + return Err(format!("off again by #{LATER}")); + } + } + None => { + if let Some(at) = probes().into_iter().find(|&n| active(cfg, n)) { + return Err(format!("on at #{at}")); + } + } + } + Ok(()) + } + + #[test] + fn every_rule_activates_where_rskjs_config_puts_it() { + let mainnet = RskHardforkConfig::mainnet(); + let testnet = RskHardforkConfig::testnet(); + let mut wrong = Vec::new(); + for &(rule, active, on_mainnet, on_testnet) in SCHEDULE { + for (network, cfg, rskj) in [ + ("mainnet", &mainnet, on_mainnet), + ("testnet", &testnet, on_testnet), + ] { + if let Err(e) = check(cfg, active, rskj) { + wrong.push(format!("{network} {rule}: {e}; rskj: {rskj:?}")); + } + } + } + assert!( + wrong.is_empty(), + "rules off rskj's schedule:\n{}", + wrong.join("\n") + ); + } +} From 15367e77959c326a9ba14403c1397370da8eceea Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Sun, 8 Nov 2026 15:35:55 -0800 Subject: [PATCH 038/264] bridge: the union bridge's getters take a STATICCALL, as rskj's do rskj's BridgeMethods marks getUnionBridgeContractAddress and getUnionBridgeLockingCap ALLOW_STATIC_CALL, like every other read-only getter. rustock's list of static-callable methods left them out, so a contract that STATICCALLed either one had the call refused here (validateCallMessageType's check, which spends the method's cost and returns nothing) while rskj answered it. The contract then went on from a failed call where the chain went on from a value. Testnet only, from reed810. A test now pins the count: of this table's methods, rskj marks 52 ALLOW_STATIC_CALL, counted from its enum. --- crates/execution/src/bridge/mod.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/crates/execution/src/bridge/mod.rs b/crates/execution/src/bridge/mod.rs index 0000343..15b313f 100644 --- a/crates/execution/src/bridge/mod.rs +++ b/crates/execution/src/bridge/mod.rs @@ -392,6 +392,9 @@ fn method_allows_static_call(name: &str) -> bool { | "getQueuedPegoutsCount" | "getEstimatedFeesForNextPegOutEvent" | "getEstimatedFeesForPegOutAmount" + // RSKIP502: the union bridge's two getters. + | "getUnionBridgeContractAddress" + | "getUnionBridgeLockingCap" ) } @@ -1268,6 +1271,20 @@ mod tests { assert!(method_allows_static_call("getFederationAddress")); assert!(method_allows_static_call("getBtcTransactionConfirmations")); assert!(method_allows_static_call("isBtcTxHashAlreadyProcessed")); + // The union bridge's getters are rskj's ALLOW_STATIC_CALL too, and + // its four mutators are not. + assert!(method_allows_static_call("getUnionBridgeContractAddress")); + assert!(method_allows_static_call("getUnionBridgeLockingCap")); + assert!(!method_allows_static_call("increaseUnionBridgeLockingCap")); + assert!(!method_allows_static_call("requestUnionBridgeRbtc")); + + // Of the methods in this table, rskj's BridgeMethods marks 52 + // ALLOW_STATIC_CALL (counted from its enum, rskj 9.1). + let static_callable = BRIDGE_METHODS + .iter() + .filter(|m| method_allows_static_call(m.name)) + .count(); + assert_eq!(static_callable, 52); } /// rskj `validateLocalCall`: a local-only getter reached by an on-chain call From 41f9bd35059e841da322fd4f6306c6d77a6007f8 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Sun, 8 Nov 2026 15:43:16 -0800 Subject: [PATCH 039/264] bridge: RSKIP529's super and base events rskj 9.1's Bridge has 83 methods and rustock had 77. The six missing are RSKIP529's, on reed810's schedule (testnet from #7,139,600, never on mainnet): getSuperEvent, setSuperEvent, clearSuperEvent, getBaseEvent, setBaseEvent and clearBaseEvent. A Bridge method rustock does not know fails as a malformed call, so the first testnet transaction to use one would have parted rustock's state from RSKj's and stopped the node. As rskj's UnionBridgeSupportImpl and UnionBridgeStorageProviderImpl have it: - Each event is a byte string in the keccak-keyed cell "superEvent" or "baseEvent", stored as it came, with no RLP around it. An absent cell reads as the empty array, which the getter returns ABI-encoded in 96 bytes (BytesType pads empty data to one word). - Setting and clearing are for the union bridge contract only, checked against the immediate caller as the RSKIP502 methods are. Any other caller, or an event over 128 bytes, is an IllegalArgumentException rather than a response code, so the call fails as any Bridge method that throws does: its cost is spent and nothing is written. - Clearing, or setting the empty array, deletes the cell. - The getters cost 3,000 and take a STATICCALL; the rest cost 8,000. The bytes argument is read the way rskj reads it, each offset and length word as a Java int, so high bits set in either are ignored rather than overflowing a conversion to usize. Under RSKIP535, rskj also copies baseEvent into the header of a block it mines (BlockExecutor.setBaseEvent). rustock's miner does not build RSKIP351 headers yet, so that is left to the miner's own change. --- crates/execution/src/bridge/getters.rs | 2 +- crates/execution/src/bridge/mod.rs | 81 +++++++-- crates/execution/src/bridge/union.rs | 242 ++++++++++++++++++++++++- 3 files changed, 311 insertions(+), 14 deletions(-) diff --git a/crates/execution/src/bridge/getters.rs b/crates/execution/src/bridge/getters.rs index 0b82f08..ea4440c 100644 --- a/crates/execution/src/bridge/getters.rs +++ b/crates/execution/src/bridge/getters.rs @@ -1012,7 +1012,7 @@ fn abi_encode_string(s: &str) -> Vec { abi_encode_bytes(s.as_bytes()) } -fn abi_encode_bytes(data: &[u8]) -> Vec { +pub(crate) fn abi_encode_bytes(data: &[u8]) -> Vec { // rskj's `SolidityType.BytesType.encode` sizes the data section as // `((len - 1) / 32 + 1) * 32` with Java's truncating division, so an EMPTY // `bytes` still emits one 32-byte zero word (-1/32 == 0 → 1 word): empty diff --git a/crates/execution/src/bridge/mod.rs b/crates/execution/src/bridge/mod.rs index 15b313f..bba2710 100644 --- a/crates/execution/src/bridge/mod.rs +++ b/crates/execution/src/bridge/mod.rs @@ -214,7 +214,7 @@ fn abi_args_decode_ok(args: &[u8], signature: &str) -> bool { } // --------------------------------------------------------------------------- -// BridgeMethod — all 69 methods (32 tx-callable + 37 local-only) +// BridgeMethod — all 83 methods (45 tx-callable, 37 local-only, 1 that turns) // --------------------------------------------------------------------------- /// Permission model for a Bridge method. @@ -283,6 +283,13 @@ fn method_enabled(name: &str) -> BridgeMethodEnabled { | "requestUnionBridgeRbtc" | "releaseUnionBridgeRbtc" | "setUnionBridgeTransferPermissions" => Rule(RskHardforkConfig::has_rskip502), + // RSKIP529 (reed810): the union bridge's super and base events. + "getSuperEvent" + | "setSuperEvent" + | "clearSuperEvent" + | "getBaseEvent" + | "setBaseEvent" + | "clearBaseEvent" => Rule(RskHardforkConfig::has_rskip529), // !RSKIP87 / !RSKIP89 "addLockWhitelistAddress" | "getBtcBlockchainBlockLocator" => Until(Orchid), // RSKIP87 / RSKIP89 @@ -395,6 +402,9 @@ fn method_allows_static_call(name: &str) -> bool { // RSKIP502: the union bridge's two getters. | "getUnionBridgeContractAddress" | "getUnionBridgeLockingCap" + // RSKIP529: the two event getters. + | "getSuperEvent" + | "getBaseEvent" ) } @@ -484,6 +494,13 @@ fn build_method_table() -> Vec { ("requestUnionBridgeRbtc", "requestUnionBridgeRbtc(uint256)", Fixed(8000), TransactionCallable), ("releaseUnionBridgeRbtc", "releaseUnionBridgeRbtc()", Fixed(8000), TransactionCallable), ("setUnionBridgeTransferPermissions","setUnionBridgeTransferPermissions(bool,bool)", Fixed(8000), TransactionCallable), + // -- RSKIP529 union bridge events (reed810) -- + ("getSuperEvent", "getSuperEvent()", Fixed(3000), TransactionCallable), + ("setSuperEvent", "setSuperEvent(bytes)", Fixed(8000), TransactionCallable), + ("clearSuperEvent", "clearSuperEvent()", Fixed(8000), TransactionCallable), + ("getBaseEvent", "getBaseEvent()", Fixed(3000), TransactionCallable), + ("setBaseEvent", "setBaseEvent(bytes)", Fixed(8000), TransactionCallable), + ("clearBaseEvent", "clearBaseEvent()", Fixed(8000), TransactionCallable), // -- Dynamic permission -- ("getBtcBlockchainBestChainHeight","getBtcBlockchainBestChainHeight()", Fixed(19000), DynamicRskip220), // -- Local-only getters (needed for completeness; not consensus-critical) -- @@ -986,6 +1003,32 @@ fn execute_method( "setUnionBridgeTransferPermissions" => { union::set_transfer_permissions(ctx, args, gas_cost, hardfork_cfg, caller) } + // RSKIP529: the super and base events, written only by the union + // bridge contract, against the same immediate caller. + "getSuperEvent" => union::get_event(ctx, union::SUPER_EVENT_KEY, gas_cost), + "setSuperEvent" => union::set_event( + ctx, + union::SUPER_EVENT_KEY, + args, + gas_cost, + hardfork_cfg, + caller, + ), + "clearSuperEvent" => { + union::clear_event(ctx, union::SUPER_EVENT_KEY, gas_cost, hardfork_cfg, caller) + } + "getBaseEvent" => union::get_event(ctx, union::BASE_EVENT_KEY, gas_cost), + "setBaseEvent" => union::set_event( + ctx, + union::BASE_EVENT_KEY, + args, + gas_cost, + hardfork_cfg, + caller, + ), + "clearBaseEvent" => { + union::clear_event(ctx, union::BASE_EVENT_KEY, gas_cost, hardfork_cfg, caller) + } // Phase 3: BTC transaction verification "registerBtcCoinbaseTransaction" => tx::register_btc_coinbase_transaction(ctx, args, gas_cost), @@ -1158,15 +1201,16 @@ mod tests { } #[test] - fn method_table_has_77_entries() { + fn method_table_has_83_entries() { // 69 pre-vetiver methods + getEstimatedFeesForPegOutAmount (RSKIP540) - // + the seven union bridge methods (RSKIP502, reed810). - assert_eq!(BRIDGE_METHODS.len(), 77); + // + the seven union bridge methods (RSKIP502, reed810) + its six + // event methods (RSKIP529, reed810): rskj 9.1's BridgeMethods, whole. + assert_eq!(BRIDGE_METHODS.len(), 83); } #[test] - fn selector_map_has_77_entries() { - assert_eq!(BRIDGE_SELECTOR_MAP.len(), 77); + fn selector_map_has_83_entries() { + assert_eq!(BRIDGE_SELECTOR_MAP.len(), 83); } #[test] @@ -1277,14 +1321,20 @@ mod tests { assert!(method_allows_static_call("getUnionBridgeLockingCap")); assert!(!method_allows_static_call("increaseUnionBridgeLockingCap")); assert!(!method_allows_static_call("requestUnionBridgeRbtc")); - - // Of the methods in this table, rskj's BridgeMethods marks 52 - // ALLOW_STATIC_CALL (counted from its enum, rskj 9.1). + // RSKIP529: the event getters may be static-called, setting and + // clearing may not. + assert!(method_allows_static_call("getSuperEvent")); + assert!(method_allows_static_call("getBaseEvent")); + assert!(!method_allows_static_call("setSuperEvent")); + assert!(!method_allows_static_call("clearBaseEvent")); + + // rskj's BridgeMethods marks 54 methods ALLOW_STATIC_CALL (counted + // from its enum, rskj 9.1), and this table names every one. let static_callable = BRIDGE_METHODS .iter() .filter(|m| method_allows_static_call(m.name)) .count(); - assert_eq!(static_callable, 52); + assert_eq!(static_callable, 54); } /// rskj `validateLocalCall`: a local-only getter reached by an on-chain call @@ -1332,6 +1382,12 @@ mod tests { "requestUnionBridgeRbtc(uint256)", "releaseUnionBridgeRbtc()", "setUnionBridgeTransferPermissions(bool,bool)", + "getSuperEvent()", + "setSuperEvent(bytes)", + "clearSuperEvent()", + "getBaseEvent()", + "setBaseEvent(bytes)", + "clearBaseEvent()", ] { let m = find_bridge_method(&compute_selector(signature)).unwrap(); for n in [8_804_199, 8_804_200, 9_500_000, u64::MAX] { @@ -1373,9 +1429,10 @@ mod tests { .filter(|m| m.permission == BridgePermission::TransactionCallable) .count(); assert_eq!( - count, 39, + count, 45, "31 pre-vetiver methods with fixedPermission(false), \ - getEstimatedFeesForPegOutAmount, and the seven union bridge methods" + getEstimatedFeesForPegOutAmount, the seven union bridge methods \ + and its six event methods" ); } diff --git a/crates/execution/src/bridge/union.rs b/crates/execution/src/bridge/union.rs index 88e35d2..4b8bdd2 100644 --- a/crates/execution/src/bridge/union.rs +++ b/crates/execution/src/bridge/union.rs @@ -11,11 +11,15 @@ //! single missing selector shows up as a failed transaction or a doubled gas //! bill rather than as anything naming the Bridge. //! +//! RSKIP529 adds six more on the same schedule: a "super event" and a +//! "base event", each a string of at most 128 bytes that only the union +//! bridge contract may set or clear and anyone may read. +//! //! Storage keys are `DataWord.fromLongString` -- keccak of the name -- not the //! right-aligned ASCII form the older Bridge cells use //! (`UnionBridgeStorageIndexKey`). -use alloy_primitives::{Address, U256}; +use alloy_primitives::{Address, Bytes, U256}; use revm::precompile::{PrecompileError, PrecompileOutput}; use super::storage::{bridge_load_bytes, bridge_storage_key_long, bridge_store_bytes}; @@ -49,6 +53,13 @@ pub const UNION_LOCKING_CAP_KEY: &str = "unionBridgeLockingCap"; pub const WEIS_TRANSFERRED_KEY: &str = "weisTransferredToUnionBridge"; pub const UNION_REQUEST_ENABLED_KEY: &str = "unionBridgeRequestEnabled"; pub const UNION_RELEASE_ENABLED_KEY: &str = "unionBridgeReleaseEnabled"; +/// RSKIP529. The event's bytes as they came, with no RLP around them. +pub const SUPER_EVENT_KEY: &str = "superEvent"; +/// RSKIP529. Read into a mined header's `baseEvent` field under RSKIP535. +pub const BASE_EVENT_KEY: &str = "baseEvent"; + +/// rskj `UnionBridgeSupportImpl.MAX_EVENT_DATA_LENGTH`. +pub const MAX_EVENT_DATA_LENGTH: usize = 128; /// rskj `UnionBridgeConstants`, per network. /// @@ -415,6 +426,80 @@ pub fn set_transfer_permissions( int_result(gas_cost, UnionResponse::Success.code()) } +/// `getSuperEvent()` / `getBaseEvent()` -> bytes: the event `key` holds, +/// empty when there is none. +/// +/// rskj `UnionBridgeStorageProviderImpl.getSuperEvent` takes the cell's bytes +/// as they are (`data -> data`), with no RLP to strip, and an absent cell +/// reads as the empty array. +pub fn get_event( + ctx: &mut CTX, + key: &str, + gas_cost: u64, +) -> Result { + let event = load_long(ctx, key); + let encoded = super::getters::abi_encode_bytes(&event); + Ok(PrecompileOutput::new(gas_cost, encoded.into())) +} + +/// `setSuperEvent(bytes)` / `setBaseEvent(bytes)`, void. +/// +/// rskj `UnionBridgeSupportImpl.setSuperEvent`: the caller must be the union +/// bridge contract and the event at most 128 bytes. Unlike the RSKIP502 +/// methods, which answer a refusal with a response code, both checks throw +/// `IllegalArgumentException`, so the call fails as any Bridge method that +/// throws: its cost is spent and nothing is written. +pub fn set_event( + ctx: &mut CTX, + key: &str, + args: &[u8], + gas_cost: u64, + hardfork_cfg: &RskHardforkConfig, + sender: Address, +) -> Result { + let event = abi_bytes(args).ok_or_else(|| { + PrecompileError::other("union bridge event: the bytes argument does not decode") + })?; + require_union_bridge_caller(ctx, hardfork_cfg, sender)?; + if event.len() > MAX_EVENT_DATA_LENGTH { + return Err(PrecompileError::other(format!( + "union bridge event: {} bytes is above the maximum of {MAX_EVENT_DATA_LENGTH}", + event.len() + ))); + } + // An empty event deletes the cell, as rskj's addStorageBytes does. + store_long(ctx, key, &event); + Ok(PrecompileOutput::new(gas_cost, Bytes::new())) +} + +/// `clearSuperEvent()` / `clearBaseEvent()`, void: the union bridge contract +/// sets the event to the empty array, which deletes the cell. +pub fn clear_event( + ctx: &mut CTX, + key: &str, + gas_cost: u64, + hardfork_cfg: &RskHardforkConfig, + sender: Address, +) -> Result { + require_union_bridge_caller(ctx, hardfork_cfg, sender)?; + store_long(ctx, key, &[]); + Ok(PrecompileOutput::new(gas_cost, Bytes::new())) +} + +/// rskj `UnionBridgeSupportImpl.validateCallerIsUnionBridge`, which throws. +fn require_union_bridge_caller( + ctx: &mut CTX, + hardfork_cfg: &RskHardforkConfig, + sender: Address, +) -> Result<(), PrecompileError> { + if sender != contract_address(ctx, hardfork_cfg) { + return Err(PrecompileError::other( + "union bridge event: the caller is not the union bridge contract", + )); + } + Ok(()) +} + // --------------------------------------------------------------------------- // Helpers // --------------------------------------------------------------------------- @@ -435,6 +520,22 @@ fn abi_bool(args: &[u8], index: usize) -> Option { abi_word(args, index).map(|w| w.iter().any(|b| *b != 0)) } +/// The single `bytes` argument, read as rskj's `BytesType.decode` reads it: +/// the offset and the length are each the low 32 bits of their word as a +/// Java int (`IntType.decodeInt(..).intValue()`), so high bits are ignored +/// where `U256::to::` would panic. The dispatcher has already checked +/// that the arguments decode (`abi_args_decode_ok`); `None` is a backstop. +fn abi_bytes(args: &[u8]) -> Option> { + let int_at = |at: usize| -> Option { + let word = args.get(at..at.checked_add(32)?)?; + usize::try_from(i32::from_be_bytes(word[28..32].try_into().ok()?)).ok() + }; + let offset = int_at(0)?; + let len = int_at(offset)?; + let start = offset.checked_add(32)?; + args.get(start..start.checked_add(len)?).map(<[u8]>::to_vec) +} + /// Moves RBTC out of the Bridge's own balance. fn transfer_from_bridge( ctx: &mut CTX, @@ -564,4 +665,143 @@ mod tests { assert_eq!(code, UnionResponse::UnauthorizedCaller.code()); assert_eq!(locking_cap(&mut c, &cfg), one_rbtc(), "unchanged"); } + + // -- RSKIP529: the super and base events ------------------------------ + + /// The union bridge contract testnet's tests call from. + fn union_bridge() -> Address { + addr("2c1d1ba63ca24d08a92b309db6729332c4f76834") + } + + /// A testnet context whose union bridge contract is `union_bridge()`. + fn testnet_ctx() -> impl crate::RskContextTr { + let mut c = ctx(); + let address = rlp_encode_bytes(union_bridge().as_slice()); + store_long(&mut c, UNION_CONTRACT_ADDRESS_KEY, &address); + c + } + + /// `bytes` as a lone ABI argument: offset, length, data padded to a word. + fn bytes_arg(data: &[u8]) -> Vec { + let mut a = U256::from(32u64).to_be_bytes::<32>().to_vec(); + a.extend_from_slice(&U256::from(data.len()).to_be_bytes::<32>()); + a.extend_from_slice(data); + a.resize(64 + data.len().div_ceil(32) * 32, 0); + a + } + + /// `setSuperEvent` / `setBaseEvent` on testnet, called by `from`. + fn set_as( + c: &mut impl crate::RskContextTr, + key: &str, + event: &[u8], + from: Address, + ) -> Result { + let cfg = RskHardforkConfig::testnet(); + set_event(c, key, &bytes_arg(event), 8_000, &cfg, from) + } + + /// `clearSuperEvent` / `clearBaseEvent` on testnet, called by `from`. + fn clear_as( + c: &mut impl crate::RskContextTr, + key: &str, + from: Address, + ) -> Result { + clear_event(c, key, 8_000, &RskHardforkConfig::testnet(), from) + } + + /// rskj stores the event as it came, with no RLP around it, and answers + /// it ABI-encoded as `bytes`. + #[test] + fn an_event_is_stored_as_it_came_and_read_back_as_bytes() { + let mut c = testnet_ctx(); + let event = b"a super event".to_vec(); + set_as(&mut c, SUPER_EVENT_KEY, &event, union_bridge()).unwrap(); + assert_eq!(load_long(&mut c, SUPER_EVENT_KEY), event, "no RLP"); + + let out = get_event(&mut c, SUPER_EVENT_KEY, 3_000).unwrap(); + assert_eq!(out.bytes.as_ref(), bytes_arg(&event).as_slice()); + assert_eq!(out.gas_used, 3_000); + } + + /// An absent event reads as the empty array, which rskj's `BytesType` + /// encodes as 96 bytes: offset 32, length 0, and one zero word of data. + #[test] + fn an_absent_event_reads_as_empty_bytes() { + let mut c = testnet_ctx(); + let out = get_event(&mut c, BASE_EVENT_KEY, 3_000).unwrap(); + let mut empty = U256::from(32u64).to_be_bytes::<32>().to_vec(); + empty.resize(96, 0); + assert_eq!(out.bytes.as_ref(), empty.as_slice()); + } + + /// rskj `setSuperEvent_afterChangingUnionBridgeAddress_newAddressShouldSet_ + /// oldAddressShouldNot` and its `clear` twin: only the contract the Bridge + /// currently names may write, and a refusal is a throw, not a code. + #[test] + fn only_the_union_bridge_contract_may_set_or_clear_an_event() { + let mut c = testnet_ctx(); + let first = b"first".to_vec(); + set_as(&mut c, BASE_EVENT_KEY, &first, union_bridge()).unwrap(); + + let stranger = Address::repeat_byte(0xAA); + assert!(set_as(&mut c, BASE_EVENT_KEY, b"x", stranger).is_err()); + assert!(clear_as(&mut c, BASE_EVENT_KEY, stranger).is_err()); + assert_eq!(load_long(&mut c, BASE_EVENT_KEY), first, "untouched"); + + // The address moves: the old contract is refused, the new one is not. + let moved = Address::repeat_byte(0x11); + let address = rlp_encode_bytes(moved.as_slice()); + store_long(&mut c, UNION_CONTRACT_ADDRESS_KEY, &address); + assert!(clear_as(&mut c, BASE_EVENT_KEY, union_bridge()).is_err()); + clear_as(&mut c, BASE_EVENT_KEY, moved).unwrap(); + assert_eq!(load_long(&mut c, BASE_EVENT_KEY), Vec::::new()); + } + + /// `MAX_EVENT_DATA_LENGTH`: 128 bytes is an event, 129 is a throw. + #[test] + fn an_event_may_be_128_bytes_and_no_more() { + let mut c = testnet_ctx(); + let longest = vec![0x5A; MAX_EVENT_DATA_LENGTH]; + set_as(&mut c, SUPER_EVENT_KEY, &longest, union_bridge()).unwrap(); + assert_eq!(load_long(&mut c, SUPER_EVENT_KEY), longest); + + let too_long = vec![0x5A; MAX_EVENT_DATA_LENGTH + 1]; + let refused = set_as(&mut c, SUPER_EVENT_KEY, &too_long, union_bridge()); + assert!(refused.is_err()); + assert_eq!(load_long(&mut c, SUPER_EVENT_KEY), longest, "unchanged"); + } + + /// Clearing, or setting the empty array, deletes the cell (rskj's + /// `addStorageBytes` stores an empty value as no value), and the two + /// events are separate cells. + #[test] + fn clearing_an_event_deletes_only_its_own_cell() { + let mut c = testnet_ctx(); + let base = b"base".to_vec(); + set_as(&mut c, SUPER_EVENT_KEY, b"super", union_bridge()).unwrap(); + set_as(&mut c, BASE_EVENT_KEY, &base, union_bridge()).unwrap(); + + clear_as(&mut c, SUPER_EVENT_KEY, union_bridge()).unwrap(); + let cell = super::super::storage::bridge_load_raw(&mut c, union_key(SUPER_EVENT_KEY)); + assert_eq!(cell, None, "no value at all, not an empty one"); + assert_eq!(load_long(&mut c, BASE_EVENT_KEY), base, "its own cell"); + + set_as(&mut c, BASE_EVENT_KEY, &[], union_bridge()).unwrap(); + assert_eq!(load_long(&mut c, BASE_EVENT_KEY), Vec::::new()); + } + + /// rskj reads the offset and length words as Java ints, so a word whose + /// high bytes are set still says 32. Converting the whole word to a + /// `usize` panics on it instead. + #[test] + fn the_bytes_argument_is_read_as_rskj_reads_it() { + let event = b"event".to_vec(); + let mut args = bytes_arg(&event); + args[..28].fill(0xFF); + args[32..60].fill(0x01); + assert_eq!(abi_bytes(&args), Some(event)); + assert_eq!(abi_bytes(&[]), None); + assert_eq!(abi_bytes(&args[..40]), None, "the length word is cut short"); + } } From c93ddb34f7acaf2bbb4c8b7856b438f3a18f2ab2 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Sun, 8 Nov 2026 17:04:44 -0800 Subject: [PATCH 040/264] bridge: read arguments as rskj's decoder does, and validate peg-ins in its order A new bridge::abi decoder follows rskj's CallTransaction.Function.decode and SolidityType exactly, and every Bridge method now reads its arguments through it: - an offset, length or count is its word's low 32 bits as a Java int (intValue), so high bits are ignored and a negative one throws, where converting the whole word with U256::to panicked; - integers are signed, an empty payload reads as zero for the integer types (int, uint, bool, address) and fails for the others; - an address goes through DataWord, which drops a negative word's leading 0xFF bytes; a bool is intValue() != 0; a string is lossy UTF-8; - getGasForData parses the call as execute does, so arguments that do not decode cost the flat 23,000 there too. What changes with it, each as rskj answers: - getBtcBlockchainBlockHeaderByHash answers 96 bytes for an unknown block, and ...ByHeight uses the full main-chain lookup; - receiveHeaders refuses the whole call when any header has the wrong size; - lock whitelist: the address is checked against the network, in rskj's order of checks; addOneOff's value and the disable delay are read with longValueExact / intValueExact, which throw; the delay sum is Java int arithmetic; voteFeePerKbChange answers GENERIC_ERROR for a fee that is not a long; - isBtcTxHashAlreadyProcessed and getBtcTxHashProcessedHeight throw for a hash string that is not 64 lower-case hex digits; - negative indexes find nothing, and an empty index is index 0; - addSignature refuses a key bitcoinj cannot parse, an empty signature list, a non-DER signature and a hash that is not 32 bytes: a success with no output before RSKIP88, a throw from it; the point at infinity parses; - the union bridge's amounts and caps are signed, so a negative one is INVALID_VALUE instead of wrapping the running total; - releaseBtc and getEstimatedFeesForPegOutAmount take the satoshis' longValue, as Coin.toBitcoin does. registerBtcTransaction and registerFlyoverBtcTransaction now share one validationsForRegisterBtcTransaction, in rskj's order: height and confirmations, the tree's size, the merkle root, validateInputsCount (new: a transaction without inputs throws), the block lookup with RSKIP199's search limit (it throws below it, and a block the store does not have throws, where both were swallowed), then the merkle root against the block. The flyover answers a false with the validations code and a throw, or a transaction that does not parse, with GENERIC_ERROR, as Bridge.registerFlyoverBtcTransaction's catch does. core's bitcoinj VarInt reader is public for validateInputsCount. Tests: the decoder's rules; offsets, counts and heights with 2^64 added through the executor for receiveHeader, receiveHeaders, getBtcTransactionConfirmations and registerFastBridgeBtcTransaction; the validations' order and answers; a crafted input-free peg-in failing the transaction; the flyover's codes; addSignature before and after RSKIP88; the union, whitelist, hash-string, index and header changes; and every method-table signature decoding. --- crates/core/src/partial_merkle_tree.rs | 2 +- crates/execution/src/bridge/abi.rs | 492 +++++++++++++ crates/execution/src/bridge/btc_chain.rs | 232 +++--- crates/execution/src/bridge/getters.rs | 181 ++--- crates/execution/src/bridge/governance.rs | 210 +++--- crates/execution/src/bridge/mod.rs | 216 ++---- crates/execution/src/bridge/peg.rs | 835 +++++++++++++--------- crates/execution/src/bridge/pmt.rs | 28 +- crates/execution/src/bridge/tx.rs | 64 +- crates/execution/src/bridge/union.rs | 159 ++-- crates/execution/src/executor.rs | 102 +++ 11 files changed, 1599 insertions(+), 922 deletions(-) create mode 100644 crates/execution/src/bridge/abi.rs diff --git a/crates/core/src/partial_merkle_tree.rs b/crates/core/src/partial_merkle_tree.rs index 1af0efb..512c308 100644 --- a/crates/core/src/partial_merkle_tree.rs +++ b/crates/core/src/partial_merkle_tree.rs @@ -248,7 +248,7 @@ fn declared_size(pmt: &[u8]) -> Option { /// bitcoinj `new VarInt(buf, offset)`: the value in the Java `long` it is /// read into, where a nine-byte one can be negative, and how many bytes it /// took. `None` where it runs past the end, where bitcoinj throws. -pub(crate) fn var_int(buf: &[u8], offset: usize) -> Option<(i64, usize)> { +pub fn var_int(buf: &[u8], offset: usize) -> Option<(i64, usize)> { let first = *buf.get(offset)?; let rest = |n: usize| buf.get(offset + 1..offset + 1 + n); Some(match first { diff --git a/crates/execution/src/bridge/abi.rs b/crates/execution/src/bridge/abi.rs new file mode 100644 index 0000000..4c9a4e1 --- /dev/null +++ b/crates/execution/src/bridge/abi.rs @@ -0,0 +1,492 @@ +//! A Bridge call's arguments, read as rskj reads them. +//! +//! rskj decodes a call's arguments once, in `Bridge.parseData`, through +//! `CallTransaction.Function.decode` and the decoders of +//! `org.ethereum.solidity.SolidityType`; a payload they cannot decode is a +//! parse failure. Each method then converts the `BigInteger`s it gets as Java +//! does. Those rules are not the ABI's, and a transaction can reach every +//! place they differ: +//! +//! - an offset or a length is a word's `intValue()`: its low 32 bits as a +//! signed int, whatever the other 28 bytes hold; +//! - an integer is a signed `BigInteger`, a `uint` parameter's too; +//! - an empty payload reads as zero wherever an integer is read, so a method +//! whose parameters are all integers, `bool`s or `address`es can be called +//! with no arguments at all, and an empty payload is an empty array; +//! - an array element's offset may be negative, if it still lands inside the +//! payload; +//! - a `bool` is true when the word's low 32 bits are not all zero; +//! - an `address` is `DataWord.valueOf(BigInteger.toByteArray())`, which +//! drops a negative word's leading 0xFF bytes and pads with zeros; +//! - a `string` is UTF-8 with bad sequences replaced, never refused. +//! +//! Every reader returns `None` exactly where rskj's decoder throws, and none +//! of them can panic, whatever the payload. + +use alloy_primitives::{Address, I256, U256}; + +/// An integer parameter as `IntType.decodeInt` leaves it: the word, read as +/// a signed `BigInteger`. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) struct Int(pub(crate) [u8; 32]); + +impl Int { + pub(crate) const ZERO: Int = Int([0; 32]); + + /// `BigInteger.intValue()`: the low 32 bits, as a signed int. + pub(crate) fn int_value(&self) -> i32 { + let w = &self.0; + i32::from_be_bytes([w[28], w[29], w[30], w[31]]) + } + + /// `BigInteger.longValue()`: the low 64 bits, as a signed long. + pub(crate) fn long_value(&self) -> i64 { + let mut low = [0u8; 8]; + low.copy_from_slice(&self.0[24..]); + i64::from_be_bytes(low) + } + + /// `BigInteger.intValueExact()`: the number, if an int holds it. + pub(crate) fn int_value_exact(&self) -> Option { + self.fits(4).then(|| self.int_value()) + } + + /// `BigInteger.longValueExact()`: the number, if a long holds it. + pub(crate) fn long_value_exact(&self) -> Option { + self.fits(8).then(|| self.long_value()) + } + + /// The number itself. + pub(crate) fn signed(&self) -> I256 { + I256::from_raw(U256::from_be_bytes(self.0)) + } + + /// Whether the word is its low `bytes` bytes, sign-extended. + fn fits(&self, bytes: usize) -> bool { + let fill = if self.0[32 - bytes] & 0x80 != 0 { + 0xFF + } else { + 0 + }; + self.0[..32 - bytes].iter().all(|b| *b == fill) + } +} + +/// A call's payload after its selector, read as `Function.decode` reads it: +/// parameter `i`'s head is the word at `32 * i`, and a dynamic parameter's +/// head holds the offset of its data. +#[derive(Clone, Copy)] +pub(crate) struct Args<'a>(&'a [u8]); + +impl<'a> Args<'a> { + pub(crate) fn new(payload: &'a [u8]) -> Self { + Args(payload) + } + + /// An integer parameter: `IntType.decode`. + pub(crate) fn int(&self, i: usize) -> Option { + self.int_at(head(i)) + } + + /// A `bool` parameter: `BoolType.decode`, true when `intValue() != 0`. + pub(crate) fn bool(&self, i: usize) -> Option { + Some(self.int(i)?.int_value() != 0) + } + + /// An `address` parameter: `AddressType.decode`, then + /// `new RskAddress(DataWord)`, the word's last 20 bytes. + pub(crate) fn address(&self, i: usize) -> Option
{ + let word = data_word(&self.int(i)?); + Some(Address::from_slice(&word[12..])) + } + + /// A `bytes32` parameter: `Bytes32Type.decode`, which, unlike an + /// integer, has no empty-payload case. + pub(crate) fn bytes32(&self, i: usize) -> Option<[u8; 32]> { + self.word_at(head(i)) + } + + /// A `bytes` parameter: `BytesType.decode` at the offset its head holds. + pub(crate) fn bytes(&self, i: usize) -> Option<&'a [u8]> { + self.bytes_at(self.offset(i)?) + } + + /// A `string` parameter: `StringType.decode`, the bytes read as UTF-8 + /// with bad sequences replaced. + pub(crate) fn string(&self, i: usize) -> Option { + self.bytes(i) + .map(|b| String::from_utf8_lossy(b).into_owned()) + } + + /// A `bytes[]` parameter: `DynamicArrayType.decode` of `bytes`. Each + /// element's head holds its offset from the first head, and + /// `Math.addExact` lets that offset be negative. + pub(crate) fn bytes_array(&self, i: usize) -> Option> { + self.array_at(self.offset(i)?, |args, heads, at| { + args.bytes_at(heads + i64::from(args.int_at(at)?.int_value())) + }) + } + + /// A `bytes32[]` parameter: `DynamicArrayType.decode` of `bytes32`. + pub(crate) fn bytes32_array(&self, i: usize) -> Option> { + self.array_at(self.offset(i)?, |args, _, at| args.word_at(at)) + } + + /// Whether every parameter of `signature` decodes: `Function.decode` + /// returning rather than throwing. `None` for a parameter type the + /// Bridge does not use. + pub(crate) fn decode(&self, signature: &str) -> Option { + let mut decodes = true; + for (i, param) in params(signature).enumerate() { + decodes &= match param { + "bool" => self.bool(i).is_some(), + "address" => self.address(i).is_some(), + "bytes" | "string" => self.bytes(i).is_some(), + "bytes32" => self.bytes32(i).is_some(), + "bytes[]" => self.bytes_array(i).is_some(), + "bytes32[]" => self.bytes32_array(i).is_some(), + "int256" | "uint256" => self.int(i).is_some(), + _ => return None, + }; + } + Some(decodes) + } + + /// Where a dynamic parameter's data starts: its head's `intValue()`. + fn offset(&self, i: usize) -> Option { + Some(i64::from(self.int(i)?.int_value())) + } + + /// `Utils.safeCopyOfRange(encoded, from, size)`: throws unless both are + /// non-negative and the range ends inside the payload. + fn range(&self, from: i64, size: i64) -> Option<&'a [u8]> { + let from = usize::try_from(from).ok()?; + let size = usize::try_from(size).ok()?; + self.0.get(from..from.checked_add(size)?) + } + + fn word_at(&self, at: i64) -> Option<[u8; 32]> { + self.range(at, 32)?.try_into().ok() + } + + /// `IntType.decodeInt(encoded, at)`: an empty payload is zero wherever + /// it is read. + fn int_at(&self, at: i64) -> Option { + if self.0.is_empty() { + return Some(Int::ZERO); + } + self.word_at(at).map(Int) + } + + /// `BytesType.decode(encoded, at)`: a length word, then that many bytes. + fn bytes_at(&self, at: i64) -> Option<&'a [u8]> { + let len = self.int_at(at)?.int_value(); + self.range(at + 32, i64::from(len)) + } + + /// `DynamicArrayType.decode(encoded, at)`: a count, then that many + /// element heads, each read by `element(args, heads, head)`. + fn array_at( + &self, + at: i64, + element: impl Fn(&Self, i64, i64) -> Option, + ) -> Option> { + if self.0.is_empty() { + return Some(Vec::new()); + } + let len = i64::from(self.int_at(at)?.int_value()); + let heads = at + 32; + // `validateArrayAllegedSize`, a bound that only allows each element + // a byte, and `new Object[len]`, which throws when len is negative. + if len < 0 || heads + len > self.0.len() as i64 { + return None; + } + (0..len) + .map(|n| element(self, heads, heads + 32 * n)) + .collect() + } +} + +/// The head of parameter `i`. Every parameter's head is one word. +fn head(i: usize) -> i64 { + 32 * i as i64 +} + +/// The parameter types of a signature such as `name(bytes,int256)`. +fn params(signature: &str) -> impl Iterator { + let inner = signature + .split_once('(') + .and_then(|(_, rest)| rest.strip_suffix(')')) + .unwrap_or(""); + inner.split(',').filter(|p| !p.is_empty()) +} + +/// `DataWord.valueOf(value.toByteArray())`. `toByteArray` gives a number's +/// shortest two's-complement form, so a negative word loses its leading 0xFF +/// bytes while the byte after each still has its top bit set, and +/// `DataWord.valueOf` pads what is left with zeros. +fn data_word(value: &Int) -> [u8; 32] { + let word = value.0; + if word[0] & 0x80 == 0 { + return word; + } + let mut start = 0; + while start < 31 && word[start] == 0xFF && word[start + 1] & 0x80 != 0 { + start += 1; + } + let mut padded = [0u8; 32]; + padded[start..].copy_from_slice(&word[start..]); + padded +} + +/// `Sha256Hash.wrap(String)`: bitcoinj's `Utils.HEX`, Guava's lower-case +/// base16, decodes the string, and the hash must be 32 bytes. Upper-case +/// digits are refused. +pub(crate) fn sha256_hash_from_hex(hex: &str) -> Option<[u8; 32]> { + let digit = |c: u8| match c { + b'0'..=b'9' => Some(c - b'0'), + b'a'..=b'f' => Some(c - b'a' + 10), + _ => None, + }; + let digits = hex.as_bytes(); + if digits.len() != 64 { + return None; + } + let mut hash = [0u8; 32]; + for (byte, pair) in hash.iter_mut().zip(digits.chunks_exact(2)) { + *byte = (digit(pair[0])? << 4) | digit(pair[1])?; + } + Some(hash) +} + +/// Arguments encoded as rskj's `CallTransaction` encodes them, for the +/// Bridge's tests. +#[cfg(test)] +pub(crate) mod encode { + use alloy_primitives::{I256, U256}; + + /// One argument. + pub(crate) enum Arg<'a> { + Word([u8; 32]), + Bytes(&'a [u8]), + BytesArray(&'a [&'a [u8]]), + /// `bytes32[]`. + Words(&'a [[u8; 32]]), + } + + /// A signed integer's word. + pub(crate) fn int(n: i64) -> [u8; 32] { + I256::try_from(n).unwrap().into_raw().to_be_bytes() + } + + /// The arguments, heads then tails, each dynamic one's head its offset. + pub(crate) fn args(args: &[Arg]) -> Vec { + fn word(n: usize) -> [u8; 32] { + U256::from(n).to_be_bytes() + } + fn bytes(data: &[u8]) -> Vec { + let mut out = word(data.len()).to_vec(); + out.extend_from_slice(data); + out.resize(32 + data.len().div_ceil(32) * 32, 0); + out + } + let (mut heads, mut tails) = (Vec::new(), Vec::new()); + for arg in args { + let tail = match arg { + Arg::Word(w) => { + heads.extend_from_slice(w); + continue; + } + Arg::Bytes(data) => bytes(data), + Arg::BytesArray(items) => { + let mut out = word(items.len()).to_vec(); + let mut body = Vec::new(); + for item in items.iter() { + out.extend_from_slice(&word(32 * items.len() + body.len())); + body.extend(bytes(item)); + } + out.extend(body); + out + } + Arg::Words(words) => { + let mut out = word(words.len()).to_vec(); + words.iter().for_each(|w| out.extend_from_slice(w)); + out + } + }; + heads.extend_from_slice(&word(32 * args.len() + tails.len())); + tails.extend(tail); + } + heads.extend(tails); + heads + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn word(n: u64) -> [u8; 32] { + U256::from(n).to_be_bytes() + } + + fn payload(words: &[[u8; 32]], tail: &[u8]) -> Vec { + let mut out: Vec = words.iter().flatten().copied().collect(); + out.extend_from_slice(tail); + out + } + + /// A negative number's word, as rskj's encoder writes it. + fn negative(n: i64) -> [u8; 32] { + I256::try_from(n).unwrap().into_raw().to_be_bytes() + } + + #[test] + fn an_offset_is_read_from_its_low_32_bits() { + let mut offset = word(32); + offset[0] = 0xFF; + offset[23] = 1; + let p = payload(&[offset, word(3)], b"abc"); + assert_eq!(Args::new(&p).bytes(0), Some(&b"abc"[..])); + } + + #[test] + fn a_length_is_read_from_its_low_32_bits() { + let mut len = word(3); + len[4] = 0x7F; + let p = payload(&[word(32), len], b"abc"); + assert_eq!(Args::new(&p).bytes(0), Some(&b"abc"[..])); + } + + #[test] + fn a_negative_offset_or_length_throws() { + assert_eq!(Int(negative(-32)).int_value(), -32); + let p = payload(&[negative(-32), word(0)], &[]); + assert_eq!(Args::new(&p).bytes(0), None); + let p = payload(&[word(32), [0xFF; 32]], &[]); + assert_eq!(Args::new(&p).bytes(0), None); + } + + #[test] + fn bytes_must_end_inside_the_payload() { + let p = payload(&[word(32), word(4)], b"abc"); + assert_eq!(Args::new(&p).bytes(0), None); + let p = payload(&[word(32)], &[]); + assert_eq!(Args::new(&p).bytes(0), None, "no length word"); + } + + #[test] + fn an_empty_payload_reads_as_zero_for_integers_and_nothing_else() { + let args = Args::new(&[]); + assert_eq!(args.int(0), Some(Int::ZERO)); + assert_eq!(args.int(3), Some(Int::ZERO)); + assert_eq!(args.bool(1), Some(false)); + assert_eq!(args.address(0), Some(Address::ZERO)); + assert_eq!(args.bytes32(0), None); + assert_eq!(args.bytes(0), None); + assert_eq!(args.bytes_array(0), Some(Vec::new())); + assert_eq!(args.bytes32_array(0), Some(Vec::new())); + } + + #[test] + fn a_short_payload_is_not_an_empty_one() { + let args = Args::new(&[0u8; 31]); + assert_eq!(args.int(0), None); + assert_eq!(args.bool(0), None); + } + + #[test] + fn an_array_elements_offset_may_be_negative() { + // The heads start at 64. Element 0's offset, 64, puts its length at + // 128; element 1's, -32, puts it at 32, the array's count word, so + // element 1 is the two bytes after it. + let p = payload( + &[word(32), word(2), word(64), negative(-32), word(1)], + &[0xAB; 32], + ); + let elements = Args::new(&p).bytes_array(0).unwrap(); + assert_eq!(elements[0], &[0xAB][..]); + assert_eq!(elements[1], &p[64..66]); + } + + #[test] + fn an_array_count_must_be_non_negative_and_fit() { + let p = payload(&[word(32), [0xFF; 32]], &[]); + assert_eq!(Args::new(&p).bytes32_array(0), None); + let p = payload(&[word(32), word(2)], &[0u8; 32]); + assert_eq!(Args::new(&p).bytes32_array(0), None, "one element of two"); + let p = payload(&[word(32), word(2), word(7), word(9)], &[]); + assert_eq!(Args::new(&p).bytes32_array(0), Some(vec![word(7), word(9)])); + } + + #[test] + fn a_bool_looks_only_at_the_low_32_bits() { + let mut high = [0u8; 32]; + high[27] = 1; + assert_eq!(Args::new(&high).bool(0), Some(false)); + assert_eq!(Args::new(&word(1 << 31)).bool(0), Some(true)); + } + + #[test] + fn a_negative_address_word_loses_its_leading_ff_bytes() { + let mut w = [0xFF; 32]; + w[13] = 0xAB; + let address = Args::new(&w).address(0).unwrap(); + assert_eq!(address.as_slice()[0], 0, "the 0xFF before 0xAB is dropped"); + assert_eq!(&address.as_slice()[1..], &w[13..]); + let mut w = [0xFF; 32]; + w[12] = 0x12; + assert_eq!(Args::new(&w).address(0).unwrap().as_slice(), &w[12..]); + assert_eq!( + Args::new(&[0xFF; 32]).address(0).unwrap(), + Address::with_last_byte(0xFF) + ); + } + + #[test] + fn a_string_with_bad_utf8_still_decodes() { + let p = payload(&[word(32), word(2)], &[0xFF, b'a']); + assert_eq!(Args::new(&p).string(0).as_deref(), Some("\u{FFFD}a")); + } + + #[test] + fn conversions_are_javas() { + let minus_one = Int([0xFF; 32]); + assert_eq!(minus_one.int_value(), -1); + assert_eq!(minus_one.long_value_exact(), Some(-1)); + assert_eq!(minus_one.signed(), I256::MINUS_ONE); + let mut big = word(5); + big[0] = 1; + assert_eq!(Int(big).int_value(), 5); + assert_eq!(Int(big).long_value(), 5); + assert_eq!(Int(big).long_value_exact(), None); + assert_eq!(Int(word(1 << 31)).int_value_exact(), None); + assert_eq!(Int(word(1 << 31)).int_value(), i32::MIN); + assert_eq!(Int(word((1 << 31) - 1)).int_value_exact(), Some(i32::MAX)); + assert_eq!(Int(word(1 << 63)).long_value_exact(), None); + } + + #[test] + fn a_hash_string_is_64_lower_case_hex_digits() { + let lower = "00".repeat(31) + "ab"; + assert_eq!(sha256_hash_from_hex(&lower).map(|h| h[31]), Some(0xAB)); + assert_eq!(sha256_hash_from_hex(&lower.to_uppercase()), None); + assert_eq!(sha256_hash_from_hex(&lower[2..]), None); + assert_eq!(sha256_hash_from_hex(&("0".repeat(61) + "\u{FFFD}")), None); + } + + #[test] + fn decode_walks_every_parameter() { + let p = payload(&[word(64), word(9), word(1)], &[0x11]); + assert_eq!(Args::new(&p).decode("f(bytes,int256)"), Some(true)); + assert_eq!(Args::new(&p).decode("f(bytes,int256,bytes32)"), Some(true)); + assert_eq!( + Args::new(&p).decode("f(bytes,int256,bytes32,bytes32)"), + Some(false) + ); + assert_eq!(Args::new(&[]).decode("f()"), Some(true)); + assert_eq!(Args::new(&[]).decode("f(int256,bool,address)"), Some(true)); + assert_eq!(Args::new(&[]).decode("f(bytes32)"), Some(false)); + assert_eq!(Args::new(&[]).decode("f(int256[])"), None); + } +} diff --git a/crates/execution/src/bridge/btc_chain.rs b/crates/execution/src/bridge/btc_chain.rs index b4f0364..c1c644d 100644 --- a/crates/execution/src/bridge/btc_chain.rs +++ b/crates/execution/src/bridge/btc_chain.rs @@ -75,10 +75,9 @@ pub fn receive_header( let rskip199 = hardfork_cfg.has_rskip199(block_number); ensure_btc_chain_seeded(ctx, config, use_v2, rskip199); - // Decode ABI-encoded `bytes` argument - let header_bytes = decode_abi_bytes(args).ok_or_else(|| { - PrecompileError::other("receiveHeader: invalid ABI encoding") - })?; + let header_bytes = super::abi::Args::new(args) + .bytes(0) + .ok_or_else(|| PrecompileError::other("receiveHeader: invalid ABI encoding"))?; // Deserialize BTC header (80 bytes). rskj has already parsed the header // (Bridge.receiveHeader takes a BtcBlock) by this point, so the order of @@ -466,15 +465,28 @@ pub fn receive_headers( use_v2: bool, hardfork_cfg: &RskHardforkConfig, ) -> Result { - let headers = decode_abi_bytes_array(args).ok_or_else(|| { - PrecompileError::other("receiveHeaders: invalid ABI encoding") - })?; + let headers = super::abi::Args::new(args) + .bytes_array(0) + .ok_or_else(|| PrecompileError::other("receiveHeaders: invalid ABI encoding"))?; // NOTE: rskj's receiveHeaders has NO rate limiting — the // minSecondsBetweenCallsToReceiveHeader window only applies to the // singular receiveHeader (RSKIP200). The federation calls this every // few blocks. let block_number = revm::context_interface::Block::number(ctx.block()).to::(); + + // rskj `Bridge.receiveHeaders` refuses the whole call when any header is + // not a header's size, before it parses one (Bridge.java:537-542). + let rskip124 = hardfork_cfg.has_rskip124(block_number); + if headers + .iter() + .any(|header| !is_block_header_size(header.len(), rskip124)) + { + return Err(PrecompileError::other( + "receiveHeaders: a header is not a header's size", + )); + } + let rskip434 = hardfork_cfg.has_rskip434(block_number); let rskip199 = hardfork_cfg.has_rskip199(block_number); @@ -485,10 +497,6 @@ pub fn receive_headers( let count = headers.len().min(max_headers); for header_bytes in headers.iter().take(count) { - if header_bytes.len() < 80 { - continue; - } - let btc_header: BtcHeader = match deserialize(&header_bytes[..80]) { Ok(h) => h, Err(_) => continue, @@ -656,16 +664,15 @@ pub fn get_block_hash_at_depth( config: &BridgeConstants, hardfork_cfg: &crate::hardfork::RskHardforkConfig, ) -> Result { - if args.len() < 32 { - return Err(PrecompileError::other( - "getBtcBlockchainBlockHashAtDepth: args too short", - )); - } - // rskj reads the argument as `((BigInteger) args[0]).intValue()`, so a - // value that does not fit an int wraps rather than failing -- but any - // wrapped value then fails the `depth < 0` check below. - let depth_word = alloy_primitives::U256::from_be_slice(&args[..32]); - let depth: i64 = depth_word.try_into().unwrap_or(i64::MAX); + // rskj reads the argument as `((BigInteger) args[0]).intValue()`: its low + // 32 bits, so 2^32 + 5 is depth 5, and a negative int fails the bound + // check below. + let depth = i64::from( + super::abi::Args::new(args) + .int(0) + .ok_or_else(|| PrecompileError::other("getBtcBlockchainBlockHashAtDepth: no depth"))? + .int_value(), + ); let head = load_chain_head(ctx).ok_or_else(|| { PrecompileError::other("getBtcBlockchainBlockHashAtDepth: no BTC chain head") @@ -733,49 +740,60 @@ pub fn get_block_header_by_hash( args: &[u8], gas_cost: u64, ) -> Result { - if args.len() < 32 { - return Err(PrecompileError::other("getBlockHeaderByHash: args too short")); - } - - let hash = b256_to_bitcoin_hash(&alloy_primitives::B256::from_slice(&args[..32])); - - match get_stored_block(ctx, &hash) { - Some(block) => { - let header_bytes = serialize(&block.header); - Ok(PrecompileOutput::new(gas_cost, encode_abi_bytes(&header_bytes).into())) - } - None => Ok(PrecompileOutput::new(gas_cost, Bytes::new())), - } + let hash = super::abi::Args::new(args) + .bytes32(0) + .ok_or_else(|| PrecompileError::other("getBlockHeaderByHash: no hash"))?; + let hash = b256_to_bitcoin_hash(&alloy_primitives::B256::from(hash)); + + // rskj's `serializeBlockHeader` answers a block it does not hold with + // `EMPTY_BYTE_ARRAY`, which `Bridge.execute` ABI-encodes like any other + // `byte[]`: an encoded empty `bytes`, not zero-length return data. + let header_bytes = get_stored_block(ctx, &hash) + .map(|block| serialize(&block.header)) + .unwrap_or_default(); + Ok(PrecompileOutput::new( + gas_cost, + encode_abi_bytes(&header_bytes).into(), + )) } /// `getBtcBlockchainBlockHeaderByHeight(uint256)` → bytes +/// +/// rskj reads the height with `intValue()` and looks it up with +/// `getStoredBlockAtMainChainHeight`, catching whatever that throws: a +/// height above the head, one below RSKIP199's search limit, or a walk that +/// finds the wrong block all answer `EMPTY_BYTE_ARRAY`. `Bridge.execute` +/// always ABI-encodes the `byte[]` result via `encodeOutputs` +/// (Bridge.java:418), so an empty result is returned as the ABI encoding of +/// an empty `bytes`, NOT as zero-length return data. pub fn get_block_header_by_height( ctx: &mut CTX, args: &[u8], gas_cost: u64, + config: &BridgeConstants, + hardfork_cfg: &RskHardforkConfig, ) -> Result { - if args.len() < 32 { - return Err(PrecompileError::other("getBlockHeaderByHeight: args too short")); - } - - let height_u256 = U256::from_be_slice(&args[..32]); - let height = height_u256.to::(); - - // Look up hash by height, then get the block. - // - // rskj returns `EMPTY_BYTE_ARRAY` when the block is absent (or - // `getStoredBlockAtMainChainHeight` throws and `Bridge` catches it), but - // `Bridge.execute` always ABI-encodes the `byte[]` result via - // `encodeOutputs` (Bridge.java:418), so an empty result is returned as the - // ABI encoding of an empty `bytes` (offset `0x20` + length `0x00` = 64 - // bytes), NOT as zero-length return data. - let hash_b256 = super::storage::bridge_load_btc_block_hash_by_height(ctx, height); - let header_bytes = hash_b256 - .map(|h| b256_to_bitcoin_hash(&h)) - .and_then(|hash| get_stored_block(ctx, &hash)) - .map(|block| serialize(&block.header)) + let height = super::abi::Args::new(args) + .int(0) + .ok_or_else(|| PrecompileError::other("getBlockHeaderByHeight: no height"))? + .int_value(); + let block_number = revm::context_interface::Block::number(ctx.block()).to::(); + let rskip199 = hardfork_cfg.has_rskip199(block_number); + // A negative height is below every search limit, or walks off the stored + // chain where there is none: not found either way. + let header_bytes = u32::try_from(height) + .ok() + .and_then( + |height| match main_chain_block_at_height(ctx, height, rskip199, Some(config)) { + MainChainLookup::Found(block) => Some(serialize(&block.header)), + MainChainLookup::Absent | MainChainLookup::StoreError => None, + }, + ) .unwrap_or_default(); - Ok(PrecompileOutput::new(gas_cost, encode_abi_bytes(&header_bytes).into())) + Ok(PrecompileOutput::new( + gas_cost, + encode_abi_bytes(&header_bytes).into(), + )) } /// `getBtcBlockchainParentBlockHeaderByHash(bytes32)` → bytes @@ -784,15 +802,14 @@ pub fn get_parent_block_header_by_hash( args: &[u8], gas_cost: u64, ) -> Result { - if args.len() < 32 { - return Err(PrecompileError::other("getParentBlockHeaderByHash: args too short")); - } - - let hash = b256_to_bitcoin_hash(&alloy_primitives::B256::from_slice(&args[..32])); + let hash = super::abi::Args::new(args) + .bytes32(0) + .ok_or_else(|| PrecompileError::other("getParentBlockHeaderByHash: no hash"))?; + let hash = b256_to_bitcoin_hash(&alloy_primitives::B256::from(hash)); // As in `get_block_header_by_height`, an absent block yields rskj's // `EMPTY_BYTE_ARRAY`, which `Bridge.execute` still ABI-encodes as an empty - // `bytes` (64 bytes), not zero-length return data. + // `bytes` (96 bytes, see `encode_abi_bytes`), not zero-length return data. let header_bytes = get_stored_block(ctx, &hash) .and_then(|block| get_stored_block(ctx, &block.header.prev_blockhash)) .map(|parent| serialize(&parent.header)) @@ -801,68 +818,9 @@ pub fn get_parent_block_header_by_hash( } // --------------------------------------------------------------------------- -// ABI encoding/decoding helpers +// ABI encoding helpers (arguments are read through `super::abi`) // --------------------------------------------------------------------------- -/// Decode a single ABI-encoded `bytes` argument. -fn decode_abi_bytes(data: &[u8]) -> Option> { - if data.len() < 64 { - return None; - } - // First 32 bytes: offset (should be 0x20 = 32) - let offset = U256::from_be_slice(&data[..32]).to::(); - if offset + 32 > data.len() { - return None; - } - // At the offset: length, then the data - let len = U256::from_be_slice(&data[offset..offset + 32]).to::(); - let data_start = offset + 32; - if data_start + len > data.len() { - return None; - } - Some(data[data_start..data_start + len].to_vec()) -} - -/// Decode an ABI-encoded `bytes[]` argument (dynamic array of dynamic bytes). -fn decode_abi_bytes_array(data: &[u8]) -> Option>> { - if data.len() < 64 { - return None; - } - // First 32 bytes: offset to the array - let array_offset = U256::from_be_slice(&data[..32]).to::(); - if array_offset + 32 > data.len() { - return None; - } - // At array_offset: number of elements - let count = U256::from_be_slice(&data[array_offset..array_offset + 32]).to::(); - if count == 0 { - return Some(Vec::new()); - } - - let offsets_start = array_offset + 32; - let mut result = Vec::with_capacity(count); - - for i in 0..count { - let offset_pos = offsets_start + i * 32; - if offset_pos + 32 > data.len() { - return None; - } - let elem_offset = U256::from_be_slice(&data[offset_pos..offset_pos + 32]).to::(); - let abs_offset = array_offset + 32 + elem_offset; - if abs_offset + 32 > data.len() { - return None; - } - let elem_len = U256::from_be_slice(&data[abs_offset..abs_offset + 32]).to::(); - let elem_start = abs_offset + 32; - if elem_start + elem_len > data.len() { - return None; - } - result.push(data[elem_start..elem_start + elem_len].to_vec()); - } - - Some(result) -} - /// ABI-encode a `bytes` return value. fn encode_abi_bytes(data: &[u8]) -> Vec { // rskj's `SolidityType.BytesType.encode` sizes the data section as @@ -1077,7 +1035,7 @@ mod tests { fn abi_bytes_roundtrip() { let original = vec![1u8, 2, 3, 4, 5]; let encoded = encode_abi_bytes(&original); - let decoded = decode_abi_bytes(&encoded).unwrap(); + let decoded = crate::bridge::abi::Args::new(&encoded).bytes(0).unwrap(); assert_eq!(decoded, original); } @@ -1085,7 +1043,7 @@ mod tests { fn abi_bytes_80_byte_header() { let data = vec![0xABu8; 80]; let encoded = encode_abi_bytes(&data); - let decoded = decode_abi_bytes(&encoded).unwrap(); + let decoded = crate::bridge::abi::Args::new(&encoded).bytes(0).unwrap(); assert_eq!(decoded, data); // 80 bytes → offset + length + ceil(80/32)*32 = 32 + 32 + 96 = 160. assert_eq!(encoded.len(), 160); @@ -1106,7 +1064,10 @@ mod tests { // offset 0x20, length 0, one zero word. assert_eq!(encoded[31], 0x20); assert!(encoded[32..].iter().all(|&b| b == 0)); - assert_eq!(decode_abi_bytes(&encoded).unwrap(), Vec::::new()); + assert_eq!( + crate::bridge::abi::Args::new(&encoded).bytes(0), + Some(&[][..]) + ); } #[test] @@ -1155,4 +1116,27 @@ mod tests { assert_eq!(ERR_SIZE_MISMATCH, -20); // Bridge.RECEIVE_HEADER_ERROR_SIZE_MISTMATCH } + + fn empty_ctx() -> impl crate::RskContextTr { + use revm::MainContext; + revm::Context::mainnet().with_chain(crate::raw_storage::RskChainExt::default()) + } + + /// rskj's `getBtcBlockchainBlockHeaderByHash` serializes a block it does + /// not have as the empty byte array, which encodes to 96 bytes. + #[test] + fn an_unknown_block_hash_answers_the_empty_bytes() { + let out = get_block_header_by_hash(&mut empty_ctx(), &[0x11; 32], 0).unwrap(); + assert_eq!(out.bytes.to_vec(), encode_abi_bytes(&[])); + } + + /// rskj's `receiveHeaders` checks every header's size before it takes any, + /// and refuses the whole call for one of the wrong size. + #[test] + fn receive_headers_refuses_them_all_for_one_of_the_wrong_size() { + use crate::bridge::abi::encode::{self, Arg}; + let args = encode::args(&[Arg::BytesArray(&[&[0u8; 80], &[0u8; 79]])]); + let (config, cfg) = (BridgeConstants::mainnet(), RskHardforkConfig::mainnet()); + assert!(receive_headers(&mut empty_ctx(), &args, 0, &config, false, &cfg).is_err()); + } } diff --git a/crates/execution/src/bridge/getters.rs b/crates/execution/src/bridge/getters.rs index ea4440c..a30d2cc 100644 --- a/crates/execution/src/bridge/getters.rs +++ b/crates/execution/src/bridge/getters.rs @@ -139,12 +139,7 @@ pub fn get_federator_public_key( config: &BridgeConstants, hardfork_cfg: &crate::hardfork::RskHardforkConfig, ) -> Result { - if args.len() < 32 { - return Err(PrecompileError::other("getFederatorPublicKey: args too short")); - } - let index: usize = U256::from_be_slice(&args[..32]) - .try_into() - .map_err(|_| PrecompileError::other("getFederatorPublicKey: index out of range"))?; + let index = index_arg(args, "getFederatorPublicKey")?; let block_number = revm::context_interface::Block::number(ctx.block()).to::(); let fed = super::peg::active_federation(ctx, config, hardfork_cfg, block_number); let key = member_key_at(&fed.members, index, KeyType::Btc, "Federator")?; @@ -257,35 +252,13 @@ pub fn is_btc_tx_hash_already_processed( args: &[u8], gas_cost: u64, ) -> Result { - if args.len() < 64 { - return Err(PrecompileError::other("isBtcTxHashAlreadyProcessed: args too short")); - } - let offset = U256::from_be_slice(&args[0..32]).to::(); - if offset + 32 > args.len() { - return Ok(PrecompileOutput::new(gas_cost, abi_encode_bool(false))); - } - let len = U256::from_be_slice(&args[offset..offset + 32]).to::(); - if offset + 32 + len > args.len() { - return Ok(PrecompileOutput::new(gas_cost, abi_encode_bool(false))); - } - let hash_str = &args[offset + 32..offset + 32 + len]; - let hash_hex = String::from_utf8_lossy(hash_str); + // rskj: `Sha256Hash.wrap((String) args[0])` inside a catch-all that throws a + // VMException, so a string that is not 64 lower-case hex digits reverts + // the call rather than answering false. + let hash_display = hash_arg(args, "isBtcTxHashAlreadyProcessed")?; // rskj goes through getHeightIfBtcTxhashIsAlreadyProcessed, which checks // the legacy btcTxHashesAP map first at every era (and loads it, so the // post-call save persists the possibly-empty map entry). - let mut hash_display = [0u8; 32]; - let ok = hash_hex.len() == 64 - && (0..32).all(|i| { - u8::from_str_radix(&hash_hex[2 * i..2 * i + 2], 16) - .map(|b| { - hash_display[i] = b; - true - }) - .unwrap_or(false) - }); - if !ok { - return Ok(PrecompileOutput::new(gas_cost, abi_encode_bool(false))); - } let mut hash_internal = hash_display; hash_internal.reverse(); let processed = super::tx::is_btc_tx_processed(ctx, &hash_internal); @@ -339,13 +312,11 @@ pub fn get_lock_whitelist_address( gas_cost: u64, config: &BridgeConstants, ) -> Result { - if args.len() < 32 { - return Err(PrecompileError::other("getLockWhitelistAddress: args too short")); - } - let index = U256::from_be_slice(&args[..32]); + let index = index_arg(args, "getLockWhitelistAddress")?; let entries = merged_whitelist(ctx); - let address = match index.try_into().ok().and_then(|i: usize| entries.get(i)) { + // rskj `getLockWhitelistEntryByIndex`: a negative index is not found either. + let address = match usize::try_from(index).ok().and_then(|i| entries.get(i)) { // rskj: "Empty string is returned when address is not found". None => String::new(), Some((hash160, _)) => p2pkh_base58_address(hash160, config), @@ -374,7 +345,7 @@ pub fn get_lock_whitelist_entry_by_address( gas_cost: u64, config: &BridgeConstants, ) -> Result { - let Some(addr_str) = abi_decode_string(args, 0) else { + let Some(addr_str) = super::abi::Args::new(args).string(0) else { return Err(PrecompileError::other( "getLockWhitelistEntryByAddress: malformed string argument", )); @@ -441,7 +412,10 @@ fn p2sh_version(config: &BridgeConstants) -> u8 { /// /// The version is checked but then discarded, because the whitelist's TreeMap /// comparator looks at the hash160 alone. -fn parse_network_address_hash160(addr: &str, config: &BridgeConstants) -> Option<[u8; 20]> { +pub(super) fn parse_network_address_hash160( + addr: &str, + config: &BridgeConstants, +) -> Option<[u8; 20]> { let decoded = bitcoin::base58::decode_check(addr).ok()?; if decoded.len() != 21 { return None; @@ -491,15 +465,13 @@ fn member_key(member: &super::federation::StoredMember, key_type: KeyType) -> [u } } -/// The `(index, keyType)` pair the `*PublicKeyOfType` methods take. -fn decode_index_and_key_type(args: &[u8]) -> Result<(usize, KeyType), PrecompileError> { - if args.len() < 64 { - return Err(PrecompileError::other("PublicKeyOfType: args too short")); - } - let index = U256::from_be_slice(&args[..32]) - .try_into() - .map_err(|_| PrecompileError::other("PublicKeyOfType: index out of range"))?; - let key_type = abi_decode_string(args, 1) +/// The `(index, keyType)` pair the `*PublicKeyOfType` methods take: the index +/// as rskj's `intValue()`, the key type through `KeyType.byValue`, which +/// throws for anything but "btc", "rsk" and "mst". +fn decode_index_and_key_type(args: &[u8]) -> Result<(i32, KeyType), PrecompileError> { + let index = index_arg(args, "PublicKeyOfType")?; + let key_type = super::abi::Args::new(args) + .string(1) .as_deref() .and_then(parse_key_type) .ok_or_else(|| PrecompileError::other("PublicKeyOfType: unknown key type"))?; @@ -507,17 +479,20 @@ fn decode_index_and_key_type(args: &[u8]) -> Result<(usize, KeyType), Precompile } /// rskj `FederationSupportImpl.getFederationMemberPublicKeyOfType`: an index -/// outside the member list throws `IndexOutOfBoundsException`, which the -/// Bridge turns into a `VMException` -- the call **reverts**, it does not -/// return empty bytes. +/// outside the member list, a negative one included, throws +/// `IndexOutOfBoundsException`, which the Bridge turns into a `VMException` +/// -- the call **reverts**, it does not return empty bytes. The index is +/// checked only here, after the caller has found the federation: with no +/// federation to look in, rskj answers empty whatever the index. fn member_key_at( members: &[super::federation::StoredMember], - index: usize, + index: i32, key_type: KeyType, who: &'static str, ) -> Result<[u8; 33], PrecompileError> { - members - .get(index) + usize::try_from(index) + .ok() + .and_then(|index| members.get(index)) .map(|m| member_key(m, key_type)) .ok_or_else(|| { PrecompileError::Other( @@ -559,12 +534,7 @@ pub fn get_pending_federator_public_key( args: &[u8], gas_cost: u64, ) -> Result { - if args.len() < 32 { - return Err(PrecompileError::other("getPendingFederatorPublicKey: args too short")); - } - let index: usize = U256::from_be_slice(&args[..32]) - .try_into() - .map_err(|_| PrecompileError::other("getPendingFederatorPublicKey: index out of range"))?; + let index = index_arg(args, "getPendingFederatorPublicKey")?; if bridge_load_bytes_named(ctx, PENDING_FEDERATION_KEY).is_empty() { return Ok(PrecompileOutput::new(gas_cost, abi_encode_bytes(&[]).into())); @@ -646,12 +616,7 @@ pub fn get_retiring_federator_public_key( config: &BridgeConstants, hardfork_cfg: &crate::hardfork::RskHardforkConfig, ) -> Result { - if args.len() < 32 { - return Err(PrecompileError::other("getRetiringFederatorPublicKey: args too short")); - } - let index: usize = U256::from_be_slice(&args[..32]) - .try_into() - .map_err(|_| PrecompileError::other("getRetiringFederatorPublicKey: index out of range"))?; + let index = index_arg(args, "getRetiringFederatorPublicKey")?; let block_number = revm::context_interface::Block::number(ctx.block()).to::(); let Some(fed) = super::peg::retiring_federation(ctx, config, hardfork_cfg, block_number) else { return Ok(PrecompileOutput::new(gas_cost, abi_encode_bytes(&[]).into())); @@ -763,31 +728,30 @@ pub fn get_btc_tx_hash_processed_height( args: &[u8], gas_cost: u64, ) -> Result { - let Some(hash_hex) = abi_decode_string(args, 0) else { - return Err(PrecompileError::other( - "getBtcTxHashProcessedHeight: malformed string argument", - )); - }; - let Some(mut hash) = parse_btc_hash_display(&hash_hex) else { - // rskj: `Sha256Hash.wrap` throws, the Bridge wraps it in a VMException. - return Err(PrecompileError::other( - "getBtcTxHashProcessedHeight: not a 32-byte hash", - )); - }; + // rskj: `Sha256Hash.wrap` throws, the Bridge wraps it in a VMException. + let mut hash = hash_arg(args, "getBtcTxHashProcessedHeight")?; hash.reverse(); let height = super::tx::get_btc_tx_processed_height(ctx, &hash).map_or(-1i64, |h| h as i64); Ok(PrecompileOutput::new(gas_cost, abi_encode_int(height))) } -fn parse_btc_hash_display(hex_str: &str) -> Option<[u8; 32]> { - if hex_str.len() != 64 { - return None; - } - let mut out = [0u8; 32]; - for (i, b) in out.iter_mut().enumerate() { - *b = u8::from_str_radix(hex_str.get(2 * i..2 * i + 2)?, 16).ok()?; - } - Some(out) +/// A method's first argument as an index: `((BigInteger) args[0]).intValue()`. +fn index_arg(args: &[u8], method: &str) -> Result { + super::abi::Args::new(args) + .int(0) + .map(|index| index.int_value()) + .ok_or_else(|| PrecompileError::Other(format!("{method}: no index").into())) +} + +/// A method's first argument as a BTC transaction hash in display order: +/// `Sha256Hash.wrap((String) args[0])`, whose lower-case-only hex decoding +/// and 32-byte check both throw. +fn hash_arg(args: &[u8], method: &str) -> Result<[u8; 32], PrecompileError> { + super::abi::Args::new(args) + .string(0) + .as_deref() + .and_then(super::abi::sha256_hash_from_hex) + .ok_or_else(|| PrecompileError::Other(format!("{method}: not a 32-byte hash").into())) } /// The federation creation time as the era reports it: milliseconds before @@ -804,23 +768,6 @@ fn creation_time_for_era( } } -/// ABI-decode the `string` at parameter slot `slot`. -fn abi_decode_string(args: &[u8], slot: usize) -> Option { - let head = slot * 32; - if args.len() < head + 32 { - return None; - } - let offset: usize = U256::from_be_slice(&args[head..head + 32]).try_into().ok()?; - if args.len() < offset + 32 { - return None; - } - let len: usize = U256::from_be_slice(&args[offset..offset + 32]).try_into().ok()?; - if args.len() < offset + 32 + len { - return None; - } - String::from_utf8(args[offset + 32..offset + 32 + len].to_vec()).ok() -} - // --------------------------------------------------------------------------- // Federator-client state // --------------------------------------------------------------------------- @@ -1607,4 +1554,32 @@ mod tests { assert_eq!(out[63], 33); // length assert_eq!(out[64], 0xAA); } + + /// rskj `Sha256Hash.wrap(String)` decodes with Guava's lower-case base16 + /// and wants 32 bytes, so an upper-case or short hash throws: it is not + /// read as a hash that was never processed. + #[test] + fn a_tx_hash_string_must_be_64_lower_case_hex_digits() { + let mut ctx = ctx_at(1); + let lower = "ab".repeat(32); + let args = |s: &str| abi_encode_bytes(s.as_bytes()); + let out = is_btc_tx_hash_already_processed(&mut ctx, &args(&lower), 0).unwrap(); + assert_eq!(as_int(&out), 0); + assert!( + is_btc_tx_hash_already_processed(&mut ctx, &args(&lower.to_uppercase()), 0).is_err() + ); + assert!(get_btc_tx_hash_processed_height(&mut ctx, &args("ab"), 0).is_err()); + } + + /// rskj's `IntType` reads an empty payload as zero, so an index-taking + /// getter called with no arguments at all answers for index 0. + #[test] + fn no_arguments_is_index_zero() { + let mut ctx = ctx_at(1); + let empty = get_federator_public_key(&mut ctx, &[], 0, &mainnet(), &hardforks()).unwrap(); + let zero = + get_federator_public_key(&mut ctx, &[0u8; 32], 0, &mainnet(), &hardforks()).unwrap(); + assert_eq!(empty.bytes, zero.bytes); + assert_eq!(as_bytes(&empty).len(), 33); + } } diff --git a/crates/execution/src/bridge/governance.rs b/crates/execution/src/bridge/governance.rs index 8a78d1b..a05d334 100644 --- a/crates/execution/src/bridge/governance.rs +++ b/crates/execution/src/bridge/governance.rs @@ -39,53 +39,49 @@ use super::storage::*; // BTC address ABI decoding helpers // --------------------------------------------------------------------------- -/// ABI-decode a `string` argument at the given parameter slot. -/// Returns the decoded UTF-8 string or an error. +/// The `string` argument at parameter slot `slot`, read as rskj's decoder +/// reads it: UTF-8 with bad sequences replaced, never refused for its +/// content. A payload that reaches a method has already decoded, so this +/// only fails for a caller that skipped the Bridge's argument check. fn abi_decode_string_arg(args: &[u8], slot: usize) -> Result { - if args.len() < (slot + 1) * 32 { - return Err(PrecompileError::other("governance: args too short for string param")); - } - let offset = U256::from_be_slice(&args[slot * 32..(slot + 1) * 32]).to::(); - if offset + 32 > args.len() { - return Err(PrecompileError::other("governance: string offset out of bounds")); - } - let len = U256::from_be_slice(&args[offset..offset + 32]).to::(); - let data_start = offset + 32; - if data_start + len > args.len() { - return Err(PrecompileError::other("governance: string data out of bounds")); - } - String::from_utf8(args[data_start..data_start + len].to_vec()) - .map_err(|_| PrecompileError::other("governance: invalid UTF-8 in string")) + super::abi::Args::new(args) + .string(slot) + .ok_or_else(|| PrecompileError::other("governance: malformed string argument")) } -/// Decode a BTC address string (Base58Check) to its 20-byte hash160. -/// -/// Matches rskj's `BridgeUtils.parseBtcAddressFromHex` / Address.fromBase58: -/// Base58Check decode → version byte + 20-byte hash160 + 4-byte checksum. -fn btc_address_to_hash160(addr_str: &str) -> Result<[u8; 20], PrecompileError> { - let decoded = bitcoin::base58::decode_check(addr_str) - .map_err(|_| PrecompileError::other("governance: invalid BTC address (bad Base58Check)"))?; - if decoded.len() != 21 { - return Err(PrecompileError::other("governance: BTC address decoded to wrong length")); - } - let mut hash160 = [0u8; 20]; - hash160.copy_from_slice(&decoded[1..21]); - Ok(hash160) +/// Decode a BTC address string (Base58Check) to its 20-byte hash160, as +/// rskj's `Address.fromBase58(networkParameters, s)`: this network's P2PKH +/// or P2SH version byte, and nothing else. +fn btc_address_to_hash160( + addr_str: &str, + config: &super::constants::BridgeConstants, +) -> Result<[u8; 20], PrecompileError> { + super::getters::parse_network_address_hash160(addr_str, config) + .ok_or_else(|| PrecompileError::other("governance: not a BTC address of this network")) } -/// ABI-decode an `int256` argument at the given parameter slot as a u64. -fn abi_decode_int256_as_u64(args: &[u8], slot: usize) -> Option { - if args.len() < (slot + 1) * 32 { - return None; +/// The satoshis a one-off whitelist entry stores for `maxTransferValue`. +/// +/// rskj takes `Coin.valueOf(maxTransferValue.longValueExact())`, which +/// throws for a value no long holds and keeps a negative one, and stores it +/// with `RLP.encodeBigInteger`, whose `asUnsignedByteArray` writes a negative +/// number's shortest two's-complement bytes. What the entry reads back as, +/// in rskj and here, is those bytes taken unsigned: -5 becomes 251. +fn one_off_max_transfer_value(max: &super::abi::Int) -> Option { + let value = max.long_value_exact()?; + if value >= 0 { + return Some(value as u64); } - let word = &args[slot * 32..(slot + 1) * 32]; - // Only accept non-negative values that fit in u64 (leading bytes must be 0x00) - if word[..24].iter().any(|&b| b != 0) { - return None; + let bytes = value.to_be_bytes(); + let mut start = 0; + while start < 7 && bytes[start] == 0xFF && bytes[start + 1] & 0x80 != 0 { + start += 1; } - let mut buf = [0u8; 8]; - buf.copy_from_slice(&word[24..32]); - Some(u64::from_be_bytes(buf)) + Some( + bytes[start..] + .iter() + .fold(0u64, |n, b| n << 8 | u64::from(*b)), + ) } // --------------------------------------------------------------------------- @@ -785,9 +781,15 @@ pub fn vote_fee_per_kb_change( config: &super::constants::BridgeConstants, tx_ctx: &crate::precompiles::BridgeTxContext, ) -> Result { - if args.len() < 32 { - return Err(PrecompileError::other("voteFeePerKbChange: args too short")); - } + // rskj `Bridge.voteFeePerKbChange` takes `Coin.valueOf(longValueExact())` + // before anything else, and a value no long holds is GENERIC_ERROR: -10, + // the same code an unauthorized caller gets. + let Some(fee) = super::abi::Args::new(args) + .int(0) + .and_then(|fee| fee.long_value_exact()) + else { + return Ok(encode_int_result(gas_cost, -10)); + }; let authorizers: Vec = config .fee_per_kb_authorizer_keys @@ -803,18 +805,17 @@ pub fn vote_fee_per_kb_change( return Ok(encode_int_result(gas_cost, -10)); } - // Negative (int256 sign bit) or zero is not a positive coin. - if args[0] & 0x80 != 0 { + // FeePerKbSupportImpl: `!feePerKb.isPositive()` is NEGATIVE_FEE_VOTED (-1), + // and a fee above the maximum EXCESSIVE_FEE_VOTED (-2). + let Ok(fee_satoshis) = u64::try_from(fee) else { return Ok(encode_int_result(gas_cost, -1)); - } - let fee_value = U256::from_be_slice(&args[..32]); - if fee_value.is_zero() { + }; + if fee_satoshis == 0 { return Ok(encode_int_result(gas_cost, -1)); } - if fee_value > U256::from(config.max_fee_per_kb) { + if fee_satoshis > config.max_fee_per_kb { return Ok(encode_int_result(gas_cost, -2)); } - let fee_satoshis = fee_value.to::(); // MAJORITY of the authorizer set must vote the same value. Entries are // keyed by the RLP-ENCODED coin (serializeCoin) bytes, like rskj's @@ -949,17 +950,15 @@ pub fn increase_locking_cap( config: &super::constants::BridgeConstants, tx_ctx: &crate::precompiles::BridgeTxContext, ) -> Result { - if args.len() < 32 { - return Err(PrecompileError::other("increaseLockingCap: args too short")); - } - // int256 satoshis; rskj getCoinFromBigInteger (longValueExact) and the - // <= 0 check both end in BridgeIllegalArgumentException. - let raw = U256::from_be_slice(&args[..32]); - if raw.is_zero() || raw > U256::from(i64::MAX as u64) { - return Err(PrecompileError::other("increaseLockingCap: invalid value")); - } - let new_cap = raw.to::(); + // <= 0 check both end in BridgeIllegalArgumentException, before the + // authorizer is looked at. + let new_cap = super::abi::Args::new(args) + .int(0) + .and_then(|cap| cap.long_value_exact()) + .and_then(|cap| u64::try_from(cap).ok()) + .filter(|cap| *cap > 0) + .ok_or_else(|| PrecompileError::other("increaseLockingCap: invalid value"))?; let authorized = config.increase_locking_cap_authorizer_keys.iter().any(|hex| { alloy_primitives::hex::decode(hex) @@ -1044,15 +1043,21 @@ pub fn add_one_off_lock_whitelist_address( Err(_) => return Ok(encode_int_result(gas_cost, -2)), }; - let hash160 = match btc_address_to_hash160(&addr_str) { + // WhitelistSupportImpl.addOneOffLockWhitelistAddress parses the address + // (INVALID_ADDRESS_FORMAT, -2), then the value, whose `longValueExact` + // throws, and only then asks whether the caller may change the list. + let hash160 = match btc_address_to_hash160(&addr_str, config) { Ok(h) => h, Err(_) => return Ok(encode_int_result(gas_cost, -2)), }; - let max_val = match abi_decode_int256_as_u64(args, 1) { - Some(v) => v, - None => return Ok(encode_int_result(gas_cost, -2)), - }; + let max_val = super::abi::Args::new(args) + .int(1) + .as_ref() + .and_then(one_off_max_transfer_value) + .ok_or_else(|| { + PrecompileError::other("addOneOffLockWhitelistAddress: the value is not a long") + })?; if !is_whitelist_change_authorized(config, tx_ctx.rsk_sender) { return Ok(encode_int_result(gas_cost, -10)); @@ -1094,7 +1099,7 @@ pub fn add_unlimited_lock_whitelist_address( Err(_) => return Ok(encode_int_result(gas_cost, -2)), }; - let hash160 = match btc_address_to_hash160(&addr_str) { + let hash160 = match btc_address_to_hash160(&addr_str, config) { Ok(h) => h, Err(_) => return Ok(encode_int_result(gas_cost, -2)), }; @@ -1142,7 +1147,7 @@ pub fn remove_lock_whitelist_address( Err(_) => return Ok(encode_int_result(gas_cost, -2)), }; - let hash160 = match btc_address_to_hash160(&addr_str) { + let hash160 = match btc_address_to_hash160(&addr_str, config) { Ok(h) => h, Err(_) => return Ok(encode_int_result(gas_cost, -2)), }; @@ -1181,12 +1186,6 @@ pub fn set_lock_whitelist_disable_block_delay( config: &super::constants::BridgeConstants, tx_ctx: &crate::precompiles::BridgeTxContext, ) -> Result { - if args.len() < 32 { - return Err(PrecompileError::other( - "setLockWhitelistDisableBlockDelay: args too short", - )); - } - if !is_whitelist_change_authorized(config, tx_ctx.rsk_sender) { return Ok(encode_int_result(gas_cost, -10)); } @@ -1196,15 +1195,23 @@ pub fn set_lock_whitelist_disable_block_delay( return Ok(encode_int_result(gas_cost, -1)); // delay already set } - let delay = U256::from_be_slice(&args[..32]).to::() as i64; + // rskj `disableBlockDelayBI.intValueExact()`: a delay no int holds + // throws. The sum is Java int arithmetic, so one that wraps is not above + // the height either. + let delay = super::abi::Args::new(args) + .int(0) + .and_then(|delay| delay.int_value_exact()) + .ok_or_else(|| { + PrecompileError::other("setLockWhitelistDisableBlockDelay: the delay is not an int") + })?; let best_height = super::btc_store::load_chain_head(ctx) - .map(|h| h.height as i64) + .map(|h| h.height as i32) .unwrap_or(0); - if delay + best_height <= best_height { + if delay.wrapping_add(best_height) <= best_height { return Ok(encode_int_result(gas_cost, -2)); // invalid delay } - store_one_off_whitelist(ctx, &entries, (best_height + delay) as i32); + store_one_off_whitelist(ctx, &entries, best_height.wrapping_add(delay)); Ok(encode_int_result(gas_cost, 1)) } @@ -1312,23 +1319,12 @@ fn decode_bytes_arg(args: &[u8]) -> Result, PrecompileError> { decode_bytes_arg_at(args, 0) } -/// Decode the `index`-th `bytes` ABI argument (head word `index` holds the -/// payload offset). +/// Decode the `index`-th `bytes` ABI argument, as rskj's decoder does. fn decode_bytes_arg_at(args: &[u8], index: usize) -> Result, PrecompileError> { - let head = index * 32; - if args.len() < head + 32 { - return Err(PrecompileError::other("bytes arg: too short")); - } - let offset = U256::from_be_slice(&args[head..head + 32]).to::(); - if offset + 32 > args.len() { - return Err(PrecompileError::other("bytes arg: offset out of bounds")); - } - let len = U256::from_be_slice(&args[offset..offset + 32]).to::(); - let start = offset + 32; - if start + len > args.len() { - return Err(PrecompileError::other("bytes arg: data out of bounds")); - } - Ok(args[start..start + len].to_vec()) + super::abi::Args::new(args) + .bytes(index) + .map(<[u8]>::to_vec) + .ok_or_else(|| PrecompileError::other("bytes arg: malformed")) } pub(crate) fn encode_int_result(gas_cost: u64, value: i64) -> PrecompileOutput { @@ -1487,4 +1483,32 @@ mod tests { // The promoted active-height cell encodes as serializeLong = RLP bigint. assert_eq!(serialization::rlp_encode_u64(next_creation), vec![0x83, 0x46, 0xfe, 0xed]); } + + /// rskj `addOneOffLockWhitelistAddress` parses the address first, -2 where + /// it does not parse, then reads the value with `longValueExact`, which + /// throws for one beyond a Java long -- all before the sender is checked. + #[test] + fn a_one_off_value_beyond_a_long_throws_before_the_sender_check() { + use crate::bridge::abi::encode::{self, Arg}; + use revm::MainContext; + let config = super::super::constants::BridgeConstants::mainnet(); + let tx_ctx = crate::precompiles::BridgeTxContext::default(); + let mut ctx = + revm::Context::mainnet().with_chain(crate::raw_storage::RskChainExt::default()); + let mut call = |address: &str, value: [u8; 32]| { + let args = encode::args(&[Arg::Bytes(address.as_bytes()), Arg::Word(value)]); + add_one_off_lock_whitelist_address(&mut ctx, &args, 0, &config, &tx_ctx) + .map(|out| i64::from_be_bytes(out.bytes[24..32].try_into().unwrap())) + }; + let address = "1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2"; + let mut beyond = [0u8; 32]; + beyond[23] = 1; // 2^64 + assert!(call(address, beyond).is_err()); + assert_eq!(call("not an address", beyond).unwrap(), -2); + assert_eq!( + call(address, encode::int(1_000)).unwrap(), + -10, + "then the sender" + ); + } } diff --git a/crates/execution/src/bridge/mod.rs b/crates/execution/src/bridge/mod.rs index bba2710..7147443 100644 --- a/crates/execution/src/bridge/mod.rs +++ b/crates/execution/src/bridge/mod.rs @@ -10,6 +10,7 @@ //! This module implements the consensus-critical subset (32 transaction-callable //! methods). Local-call-only getters are deferred to RPC support. +pub(crate) mod abi; pub mod btc_block_cache; #[cfg(test)] mod btc_block_cache_readpath_tests; @@ -67,150 +68,16 @@ pub(crate) fn compute_selector(sig: &str) -> [u8; 4] { // mainnet #6,223,797 tx[3] forwards `receiveHeaders(bytes[])` with a `bytes[]` // offset of 0xa0 into a 60-byte argument payload, so rskj's decode throws. // -// `abi_args_decode_ok` mirrors the bound checks rskj's `SolidityType` decoders -// perform (`Utils.safeCopyOfRange`/`validateArrayAllegedSize` throw when -// `data.length < offset + size`; `IntType.decodeInt` special-cases an empty -// payload as 0; offsets/lengths are read via `BigInteger.intValue()`). - -#[derive(Clone)] -enum AbiArgType { - /// 32-byte head word read with `IntType.decodeInt` (empty payload → 0): - /// int/uint/address/bool. - Word, - /// 32-byte head word read with `safeCopyOfRange` (no empty special case): - /// bytesN. - FixedBytes, - /// Dynamic `bytes`/`string`. - Bytes, - /// Dynamic `T[]`. - Array(Box), -} - -impl AbiArgType { - fn is_dynamic(&self) -> bool { - matches!(self, AbiArgType::Bytes | AbiArgType::Array(_)) - } -} - -/// Parse a single Solidity type name (as it appears in a bridge signature). -fn parse_abi_type(t: &str) -> AbiArgType { - let t = t.trim(); - if let Some(elem) = t.strip_suffix("[]") { - return AbiArgType::Array(Box::new(parse_abi_type(elem))); - } - match t { - "bytes" | "string" => AbiArgType::Bytes, - _ if t.starts_with("bytes") => AbiArgType::FixedBytes, // bytes1..bytes32 - _ => AbiArgType::Word, // int*, uint*, address, bool - } -} - -/// Extract the argument types from a method signature like `name(a,b,c)`. -fn parse_signature_args(signature: &str) -> Vec { - let inner = signature - .split_once('(') - .and_then(|(_, rest)| rest.strip_suffix(')')) - .unwrap_or(""); - if inner.is_empty() { - return Vec::new(); - } - inner.split(',').map(parse_abi_type).collect() -} - -/// rskj `IntType.decodeInt(encoded, offset).intValue()`: an empty payload -/// decodes to 0 without bounds-checking; otherwise the 32-byte word must fit -/// (`safeCopyOfRange`) and the result is its low-32-bit signed `intValue()`. -fn abi_decode_int(args: &[u8], off: usize) -> Option { - if args.is_empty() { - return Some(0); - } - let end = off.checked_add(32)?; - if end > args.len() { - return None; - } - let low = u32::from_be_bytes([args[off + 28], args[off + 29], args[off + 30], args[off + 31]]); - Some(low as i32 as i64) -} - -/// Validate that `value` (an `int`-decoded offset/length) is usable as a -/// non-negative index. rskj throws (`Math.addExact`, array bounds) on negatives. -fn abi_index(value: i64) -> Option { - if value < 0 { - None - } else { - Some(value as usize) - } -} - -/// Mirror `SolidityType.decode(encoded, offset)` for one value, returning -/// `None` exactly where rskj would throw. -fn abi_decode_value_ok(args: &[u8], off: usize, ty: &AbiArgType) -> Option<()> { - match ty { - // IntType.decode -> decodeInt (empty payload tolerated). - AbiArgType::Word => abi_decode_int(args, off).map(|_| ()), - // Bytes32Type.decode -> safeCopyOfRange(encoded, off, 32) (strict). - AbiArgType::FixedBytes => { - let end = off.checked_add(32)?; - (end <= args.len()).then_some(()) - } - // BytesType.decode: len = decodeInt(off); safeCopyOfRange(off+32, len). - AbiArgType::Bytes => { - let len = abi_index(abi_decode_int(args, off)?)?; - let data_off = off.checked_add(32)?; - (data_off.checked_add(len)? <= args.len()).then_some(()) - } - // DynamicArrayType.decode. - AbiArgType::Array(elem) => { - if args.is_empty() { - return Some(()); // empty payload → empty array (no throw) - } - let len = abi_index(abi_decode_int(args, off)?)?; - let base = off.checked_add(32)?; - // validateArrayAllegedSize(encoded, base, len) — lower bound. - if base.checked_add(len)? > args.len() { - return None; - } - let mut elem_off = base; - for _ in 0..len { - if elem.is_dynamic() { - let dyn_off = abi_index(abi_decode_int(args, elem_off)?)?; - abi_decode_value_ok(args, base.checked_add(dyn_off)?, elem)?; - } else { - abi_decode_value_ok(args, elem_off, elem)?; - } - elem_off = elem_off.checked_add(32)?; // every element head slot is 32 bytes - } - Some(()) - } - } -} - -/// rskj `CallTransaction.Function.decode(encoded, inputs)` (CallTransaction.java -/// :409): walk the head, following each dynamic argument's offset, and confirm -/// every value decodes without an out-of-bounds access. `args` is the calldata -/// AFTER the 4-byte selector. Returns false where rskj's decode would throw -/// (→ `parseData` null → flat 23_000 parse-failure cost). +// The decoding is `abi::Args`, which every method handler reads its +// arguments through as well: a payload that passes here is read by each +// handler as rskj's `Object[] args` holds it. + +/// rskj `CallTransaction.Function.decode(encoded, inputs)`: whether every +/// argument decodes. `args` is the calldata AFTER the 4-byte selector. +/// Returns false where rskj's decode would throw (→ `parseData` null → flat +/// 23_000 parse-failure cost). fn abi_args_decode_ok(args: &[u8], signature: &str) -> bool { - let types = parse_signature_args(signature); - let mut off = 0usize; - for ty in &types { - let ok = if ty.is_dynamic() { - match abi_decode_int(args, off).and_then(abi_index) { - Some(dyn_off) => abi_decode_value_ok(args, dyn_off, ty).is_some(), - None => false, - } - } else { - abi_decode_value_ok(args, off, ty).is_some() - }; - if !ok { - return false; - } - match off.checked_add(32) { - Some(n) => off = n, - None => return false, - } - } - true + abi::Args::new(args).decode(signature).unwrap_or(false) } // --------------------------------------------------------------------------- @@ -846,6 +713,9 @@ pub fn bridge_call_gas_cost( let selector = [input[0], input[1], input[2], input[3]]; let Some(method) = find_bridge_method(&selector) .filter(|m| m.enabled.is_enabled(hardfork_cfg, block_number)) + // getGasForData parses the call as execute() does, so arguments that + // do not decode cost the flat releaseBtc amount here too. + .filter(|m| abi_args_decode_ok(&input[4..], m.signature)) else { return Ok(23_000); }; @@ -871,7 +741,10 @@ fn receive_headers_cost(args: &[u8], hardfork_cfg: &RskHardforkConfig, block_num let base: u64 = if rskip132 { 25_000 } else { 66_000 }; let per_additional_header: u64 = if rskip132 { 3_500 } else { 1_650 }; - match abi_array_len(args, 0) { + match abi::Args::new(args) + .bytes_array(0) + .map(|headers| headers.len() as u64) + { Some(n) if n > 0 => base.saturating_add((n - 1).saturating_mul(per_additional_header)), _ => base, } @@ -890,8 +763,9 @@ fn btc_tx_confirmations_cost( const DOUBLE_HASH_COST: u64 = 144; // args: bytes32 btcTxHash, bytes32 btcBlockHash, uint256 path, bytes32[] hashes - let (block_hash, branch_hashes) = match (args.get(32..64), abi_array_len(args, 96)) { - (Some(h), Some(n)) => (alloy_primitives::B256::from_slice(h), n), + let args = abi::Args::new(args); + let (block_hash, branch_hashes) = match (args.bytes32(1), args.bytes32_array(3)) { + (Some(h), Some(hashes)) => (alloy_primitives::B256::from(h), hashes.len() as u64), _ => return BASIC_COST, }; @@ -911,14 +785,6 @@ fn btc_tx_confirmations_cost( BASIC_COST + depth * STEP_COST + branch_hashes * DOUBLE_HASH_COST } -/// Length of an ABI dynamic array whose offset word sits at `slot` in `args`. -fn abi_array_len(args: &[u8], slot: usize) -> Option { - let offset = U256::from_be_slice(args.get(slot..slot + 32)?).try_into().ok()?; - let offset: usize = offset; - let len = args.get(offset..offset + 32)?; - Some(U256::from_be_slice(len).to::()) -} - /// Dispatch to individual method handlers. fn execute_method( ctx: &mut CTX, @@ -967,12 +833,20 @@ fn execute_method( match method_name { // Phase 2: BTC header chain - "receiveHeader" => btc_chain::receive_header(ctx, args, gas_cost, config, use_v2, hardfork_cfg), - "receiveHeaders" => btc_chain::receive_headers(ctx, args, gas_cost, config, use_v2, hardfork_cfg), + "receiveHeader" => { + btc_chain::receive_header(ctx, args, gas_cost, config, use_v2, hardfork_cfg) + } + "receiveHeaders" => { + btc_chain::receive_headers(ctx, args, gas_cost, config, use_v2, hardfork_cfg) + } "getBtcBlockchainBestChainHeight" => btc_chain::get_best_chain_height(ctx, gas_cost), "getBtcBlockchainBestBlockHeader" => btc_chain::get_best_block_header(ctx, gas_cost), - "getBtcBlockchainBlockHeaderByHash" => btc_chain::get_block_header_by_hash(ctx, args, gas_cost), - "getBtcBlockchainBlockHeaderByHeight" => btc_chain::get_block_header_by_height(ctx, args, gas_cost), + "getBtcBlockchainBlockHeaderByHash" => { + btc_chain::get_block_header_by_hash(ctx, args, gas_cost) + } + "getBtcBlockchainBlockHeaderByHeight" => { + btc_chain::get_block_header_by_height(ctx, args, gas_cost, config, hardfork_cfg) + } "getBtcBlockchainParentBlockHeaderByHash" => btc_chain::get_parent_block_header_by_hash(ctx, args, gas_cost), // RSKIP502: the union bridge, authorized against the *immediate* @@ -1145,6 +1019,19 @@ fn unimplemented_method(method: &str) -> Result } } +/// How rskj's `validationsForRegisterBtcTransaction` turns a transaction +/// down. `registerBtcTransaction` swallows a false and fails the transaction +/// on a throw; `registerFlyoverBtcTransaction` answers a false with its +/// validations code and a throw with GENERIC_ERROR. +enum Invalid { + /// `return false`. + Swallowed(&'static str), + /// A throw: `BridgeIllegalArgumentException`, `VerificationException`, + /// `BlockStoreException`, or the `NullPointerException` of a block the + /// store does not have. + Thrown(&'static str), +} + +/// rskj `BridgeSupport.validationsForRegisterBtcTransaction`. Its order is +/// load-bearing: a transaction that fails more than one check gets the answer +/// of the first, so one with too few confirmations and a malformed tree is +/// turned down quietly and never reaches the throw. +#[allow(clippy::too_many_arguments)] +fn validations_for_register_btc_transaction( + ctx: &mut CTX, + config: &BridgeConstants, + hardfork_cfg: &RskHardforkConfig, + btc_tx_hash: &[u8; 32], + height: i32, + pmt_data: &[u8], + btc_tx_data: &[u8], +) -> Result<(), Invalid> { + let block_number = revm::context_interface::Block::number(ctx.block()).to::(); + let rskip143 = hardfork_cfg.has_rskip143(block_number); + + // validateHeightAndConfirmations: a negative height throws, which this + // catches and answers false. The block itself counts as one confirmation, + // and the caller's height is the one checked. + if height < 0 { + return Err(Invalid::Swallowed("negative height")); + } + let best_height = load_chain_head(ctx).map(|h| h.height).unwrap_or(0); + let confirmations = best_height as i64 - i64::from(height) + 1; + if confirmations < config.btc2rsk_minimum_acceptable_confirmations as i64 { + return Err(Invalid::Swallowed("insufficient confirmations")); + } + + // A tree of the wrong size throws. + if !PartialMerkleTree::has_expected_size(pmt_data) { + return Err(Invalid::Thrown( + "PartialMerkleTree doesn't have expected size", + )); + } + // `BridgeUtils.calculateMerkleRoot` builds the tree and walks it, and + // rethrows a VerificationException from either. A transaction that is not + // among its matches gets null back, which is false. + let pmt = PartialMerkleTree::parse(pmt_data).ok_or(Invalid::Thrown("failed to parse PMT"))?; + let pmt_result = pmt + .extract_matches() + .ok_or(Invalid::Thrown("PMT verification failed"))?; + if !pmt_result.matched_hashes.contains(btc_tx_hash) { + return Err(Invalid::Swallowed("tx not in PMT")); + } + + // `BridgeUtils.validateInputsCount` throws for a transaction with no + // inputs. It runs before any block is looked at, and a one-leaf tree + // proves any bytes at all, so a crafted transaction reaches it. + if !has_inputs(btc_tx_data, rskip143) { + return Err(Invalid::Thrown("the transaction has no inputs")); + } + + // `getStoredBlockAtMainChainHeight(height).getHeader()`: the store throws + // below RSKIP199's search limit, and where its walk runs out it answers + // null, on which `getHeader()` throws. + let rskip199 = hardfork_cfg.has_rskip199(block_number); + let lookup = + super::btc_chain::main_chain_block_at_height(ctx, height as u32, rskip199, Some(config)); + let stored_block = match lookup { + super::btc_chain::MainChainLookup::Found(block) => block, + super::btc_chain::MainChainLookup::Absent => { + return Err(Invalid::Thrown("no stored block at the height")) + } + super::btc_chain::MainChainLookup::StoreError => { + return Err(Invalid::Thrown("the block store refused the height")) + } + }; + + // isBlockMerkleRootValid: the block's merkle root, or from RSKIP143 the + // witness merkle root registered with its coinbase. The coinbase + // information is keyed by the block hash in DISPLAY byte order (rskj + // `Sha256Hash.toString()`; see set_coinbase_information), and holds the + // witness root in display order too (rskj registerBtcCoinbaseTransaction + // wraps the ABI argument, BridgeSupport l.2574), while the tree's root is + // in internal order -- so both are reversed. + let block_merkle_root = *stored_block + .header + .merkle_root + .to_raw_hash() + .as_byte_array(); + let root_valid = pmt_result.merkle_root == block_merkle_root + || (rskip143 && { + let mut block_hash = *stored_block + .header + .block_hash() + .to_raw_hash() + .as_byte_array(); + block_hash.reverse(); + get_coinbase_information(ctx, &block_hash).is_some_and(|mut witness_root| { + witness_root.reverse(); + pmt_result.merkle_root == witness_root + }) + }); + if !root_valid { + return Err(Invalid::Swallowed("merkle root mismatch")); + } + Ok(()) +} + +/// rskj `BridgeUtils.validateInputsCount`, false where it throws: for a +/// transaction whose input count reads zero and, from RSKIP143, is not a +/// segwit transaction with inputs, and for one too short for bitcoinj's +/// `VarInt` to read, which throws `ArrayIndexOutOfBoundsException` instead. +fn has_inputs(tx: &[u8], rskip143: bool) -> bool { + // `getInputsCount` reads the count after the version. A segwit + // transaction has its zero marker there, then its flag, then its count, + // which `getInputsCountForSegwit` reads when the flag is 1. + let count = + |offset| rustock_core::partial_merkle_tree::var_int(tx, offset).map(|(value, _)| value); + match count(4) { + None => false, + Some(0) if rskip143 => match count(5) { + None => false, + Some(1) => count(6).is_some_and(|inputs| inputs != 0), + Some(_) => true, + }, + Some(0) => false, + Some(_) => true, + } +} + pub fn register_btc_transaction( ctx: &mut CTX, args: &[u8], @@ -103,26 +238,18 @@ pub fn register_btc_transaction( hardfork_cfg: &RskHardforkConfig, tx_ctx: &BridgeTxContext, ) -> Result { - if args.len() < 96 { + // rskj `Bridge.registerBtcTransaction`: the transaction, the height as + // `intValue()`, the partial merkle tree, all as its decoder reads them. + let decoded = super::abi::Args::new(args); + let (Some(btc_tx_data), Some(height), Some(pmt_data)) = + (decoded.bytes(0), decoded.int(1), decoded.bytes(2)) + else { return Err(PrecompileError::other( - "registerBtcTransaction: args too short", + "registerBtcTransaction: malformed arguments", )); - } - - // Parse ABI arguments: - // arg0: bytes btcTxSerialized (dynamic) - // arg1: int256 height - // arg2: bytes pmtSerialized (dynamic) - - let btc_tx_offset = U256::from_be_slice(&args[0..32]).to::(); - let btc_block_height = { - let raw = U256::from_be_slice(&args[32..64]); - raw.to::() }; - let pmt_offset = U256::from_be_slice(&args[64..96]).to::(); - - let btc_tx_data = read_dynamic_bytes(args, btc_tx_offset)?; - let pmt_data = read_dynamic_bytes(args, pmt_offset)?; + let (btc_tx_data, pmt_data) = (btc_tx_data.to_vec(), pmt_data.to_vec()); + let btc_block_height = height.int_value(); // Compute BTC tx hash let btc_tx_hash = calculate_btc_tx_hash(&btc_tx_data); @@ -136,111 +263,30 @@ pub fn register_btc_transaction( // rskj wraps the registerBtcTransaction body in // `try { ... } catch (RegisterBtcTransactionException e) { log }`: an - // already-processed tx or any failed validation - // (validationsForRegisterBtcTransaction → "Could not validate - // transaction") is logged and the call returns SUCCESSFULLY without - // registering anything. Mirror that — these are a no-op success, not a - // failed tx. Only a malformed BTC tx (parsed below, a separate - // exception) propagates and fails the tx. - // rskj's validation failures are NOT all equivalent, and treating them as - // if they were diverged mainnet #9,227,292. - // - // `BridgeSupport.registerBtcTransaction` wraps its body in - // `catch (RegisterBtcTransactionException e) { log }`, so a validation that - // *returns false* is a silent no-op success. But - // `validationsForRegisterBtcTransaction` also **throws** - // `BridgeIllegalArgumentException` for a malformed partial merkle tree, and - // that is not a `RegisterBtcTransactionException` -- it escapes the catch, - // reaches `Bridge.execute`'s outer handler, becomes a `VMException`, and - // charges the sender the full gas limit (quirks-frozen-bugs.md §9d). + // already-processed transaction, or validations that answer false, are + // logged and the call succeeds without registering anything. A throw from + // the validations is not that exception: it escapes the catch, reaches + // `Bridge.execute`'s outer handler, becomes a `VMException`, and charges + // the sender the full gas limit (quirks-frozen-bugs.md §9d). Treating the + // two alike diverged mainnet #9,227,292. // - // Order is load-bearing too: rskj validates height and confirmations FIRST - // and returns false there, so a transaction with bad confirmations *and* a - // malformed PMT is swallowed, never reaching the throw. - enum Invalid { - /// `return false` in rskj: logged, call succeeds, nothing registered. - Swallowed(&'static str), - /// `throw BridgeIllegalArgumentException`: escapes the catch and fails - /// the transaction with the full gas limit charged. - Thrown(&'static str), - } - - let validation = (|| -> Result<(), Invalid> { - // Check if already processed (rskj: RegisterBtcTransactionException). - if is_btc_tx_processed(ctx, &btc_tx_hash) { - return Err(Invalid::Swallowed("transaction already processed")); - } - - // Height and confirmations come first, and are swallowed - // (BridgeUtils.validateHeightAndConfirmations -> return false). The - // block itself counts as one confirmation, and the CALLER-supplied - // height is what gets validated. - let best_height = load_chain_head(ctx).map(|h| h.height).unwrap_or(0); - let confirmations = best_height as i64 - btc_block_height as i64 + 1; - if confirmations < config.btc2rsk_minimum_acceptable_confirmations as i64 { - return Err(Invalid::Swallowed("insufficient confirmations")); - } - - // PMT size: rskj THROWS BridgeIllegalArgumentException here. - if !PartialMerkleTree::has_expected_size(&pmt_data) { - return Err(Invalid::Thrown("PartialMerkleTree doesn't have expected size")); - } - // `BridgeUtils.calculateMerkleRoot` constructs the PMT and walks it; - // a VerificationException from either is rethrown as - // BridgeIllegalArgumentException, so both of these throw. - let pmt = PartialMerkleTree::parse(&pmt_data) - .ok_or(Invalid::Thrown("failed to parse PMT"))?; - let pmt_result = pmt - .extract_matches() - .ok_or(Invalid::Thrown("PMT verification failed"))?; - - // Tx not among the matched hashes: `calculateMerkleRoot` returns null, - // so the caller returns false -- swallowed. - if !pmt_result.matched_hashes.contains(&btc_tx_hash) { - return Err(Invalid::Swallowed("tx not in PMT")); - } - - // Look up the BTC block on the main chain at the caller-supplied - // height (walks the chain pre-RSKIP199, indexed lookup afterwards). - let stored_block = - super::btc_chain::stored_block_at_main_chain_height(ctx, btc_block_height as u32, rskip199) - .ok_or(Invalid::Swallowed("BTC block not found at height"))?; - let block_hash = stored_block.header.block_hash(); - - // Verify merkle root matches - let block_merkle_root = { - let raw = stored_block.header.merkle_root.to_raw_hash(); - *raw.as_byte_array() - }; - let root_valid = if pmt_result.merkle_root == block_merkle_root { - true - } else { - // Post-RSKIP143: also accept witness merkle root. The coinbase - // information is keyed by the block hash in DISPLAY byte order - // (rskj `Sha256Hash.toString()`; see set_coinbase_information), - // the reverse of rust-bitcoin's internal order. - let block_hash_bytes = { - let mut raw = *block_hash.to_raw_hash().as_byte_array(); - raw.reverse(); - raw - }; - match get_coinbase_information(ctx, &block_hash_bytes) { - // The stored witness merkle root is in DISPLAY byte order - // (rskj registerBtcCoinbaseTransaction wraps the ABI arg and - // reverses it for the commitment, BridgeSupport l.2574), while - // the PMT merkle root is internal order — compare reversed. - Some(mut witness_root) => { - witness_root.reverse(); - pmt_result.merkle_root == witness_root - } - None => false, - } - }; - if !root_valid { - return Err(Invalid::Swallowed("merkle root mismatch")); - } - Ok(()) - })(); + // rskj then parses the transaction and runs bitcoinj's `verify()` on it, + // whose throw fails the transaction too. Only a transaction committed in a + // stored block's merkle tree gets that far, and a block's transactions + // pass `verify()`, so that check is not repeated here. + let validation = if is_btc_tx_processed(ctx, &btc_tx_hash) { + Err(Invalid::Swallowed("transaction already processed")) + } else { + validations_for_register_btc_transaction( + ctx, + config, + hardfork_cfg, + &btc_tx_hash, + btc_block_height, + &pmt_data, + &btc_tx_data, + ) + }; match validation { Ok(()) => {} Err(Invalid::Swallowed(m)) => { @@ -1415,14 +1461,14 @@ fn whitelist_allows_and_consume( ctx: &mut CTX, sender_hash160: &[u8; 20], amount: u64, - height: u64, + height: i32, rskip87: bool, ) -> bool { // rskj `getLockWhitelist()` deserializes BOTH cells together, so reaching // this function reads both even where the decision needs only one. let (mut one_off, disable_h) = load_one_off_whitelist(ctx); let unlimited = load_unlimited_whitelist(ctx); - let disabled = (height as i64) > (disable_h as i64); + let disabled = i64::from(height) > (disable_h as i64); let allowed = if let Some(pos) = one_off.iter().position(|(h, _)| h == sender_hash160) { let allowed = disabled || amount <= one_off[pos].1; @@ -1561,9 +1607,9 @@ fn flyover_redeem_script(flyover_derivation_hash: &[u8; 32], federation_redeem: /// Flyover (RSKIP176, iris300) peg-in. Full port of rskj /// `BridgeSupport.registerFlyoverBtcTransaction`. Returns an int256: /// the locked amount in wei on success, or a negative `FlyoverTxResponseCodes` -/// value on every rejection / unprocessable path. The method only ever fails -/// the tx for a malformed BTC transaction or PMT (mirrors rskj's outer -/// try/catch returning GENERIC_ERROR for those). +/// value on every rejection / unprocessable path. It never fails the +/// transaction: rskj's `Bridge.registerFlyoverBtcTransaction` catches every +/// exception and answers GENERIC_ERROR. #[allow(clippy::too_many_arguments)] pub fn register_fast_bridge_btc_transaction( ctx: &mut CTX, @@ -1585,29 +1631,41 @@ pub fn register_fast_bridge_btc_transaction( // rskj wraps the whole body in try/catch → GENERIC_ERROR. Any malformed // ABI / arg shape returns GENERIC_ERROR (NOT a failed tx). - let parse = (|| -> Option<(usize, u64, usize, [u8; 32], Vec, RskAddress, Vec, bool)> { - if args.len() < 256 { - return None; - } - let btc_tx_offset = U256::from_be_slice(args.get(0..32)?).to::(); - let btc_block_height = U256::from_be_slice(args.get(32..64)?).to::(); - let pmt_offset = U256::from_be_slice(args.get(64..96)?).to::(); - let derivation_args_hash: [u8; 32] = args.get(96..128)?.try_into().ok()?; - let user_refund_offset = U256::from_be_slice(args.get(128..160)?).to::(); - // arg5: address (right-aligned in 32 bytes → low 20 bytes). - let lbc_address = RskAddress::from_slice(args.get(172..192)?); - let lp_offset = U256::from_be_slice(args.get(192..224)?).to::(); - let should_transfer_to_contract = U256::from_be_slice(args.get(224..256)?) != U256::ZERO; - let user_refund_addr = read_dynamic_bytes(args, user_refund_offset).ok()?; - let lp_btc_addr = read_dynamic_bytes(args, lp_offset).ok()?; + // The arguments as rskj's decoder reads them: the height is `intValue()`, + // the LBC address `new RskAddress((DataWord) args[5])` and the flag + // `BoolType`'s `intValue() != 0`. + type Parsed = ( + Vec, + i32, + Vec, + [u8; 32], + Vec, + RskAddress, + Vec, + bool, + ); + let parse = (|| -> Option { + let args = super::abi::Args::new(args); Some(( - btc_tx_offset, btc_block_height, pmt_offset, derivation_args_hash, - user_refund_addr, lbc_address, lp_btc_addr, should_transfer_to_contract, + args.bytes(0)?.to_vec(), + args.int(1)?.int_value(), + args.bytes(2)?.to_vec(), + args.bytes32(3)?, + args.bytes(4)?.to_vec(), + args.address(5)?, + args.bytes(6)?.to_vec(), + args.bool(7)?, )) })(); let Some(( - btc_tx_offset, btc_block_height, pmt_offset, derivation_args_hash, - user_refund_addr, lbc_address, lp_btc_addr, should_transfer_to_contract, + btc_tx_data, + btc_block_height, + pmt_data, + derivation_args_hash, + user_refund_addr, + lbc_address, + lp_btc_addr, + should_transfer_to_contract, )) = parse else { return ok(flyover_code_output(flyover_codes::GENERIC_ERROR)); @@ -1642,9 +1700,6 @@ pub fn register_fast_bridge_btc_transaction( return ok(flyover_code_output(flyover_codes::UNPROCESSABLE_INVALID_SENDER)); } - let btc_tx_data = read_dynamic_bytes(args, btc_tx_offset)?; - let pmt_data = read_dynamic_bytes(args, pmt_offset)?; - // wtxid (calculateBtcTxHash over the raw bytes) — the first already-used key. let btc_tx_hash = calculate_btc_tx_hash(&btc_tx_data); @@ -1666,51 +1721,32 @@ pub fn register_fast_bridge_btc_transaction( return ok(flyover_code_output(flyover_codes::UNPROCESSABLE_ALREADY_PROCESSED)); } - // validationsForRegisterBtcTransaction: PMT + merkle root + confirmations. - let validations = (|| -> bool { - if !PartialMerkleTree::has_expected_size(&pmt_data) { - return false; - } - let Some(pmt) = PartialMerkleTree::parse(&pmt_data) else { return false }; - let Some(pmt_result) = pmt.extract_matches() else { return false }; - if !pmt_result.matched_hashes.contains(&btc_tx_hash) { - return false; - } - let Some(stored_block) = - super::btc_chain::stored_block_at_main_chain_height(ctx, btc_block_height as u32, rskip199) - else { - return false; - }; - let block_hash = stored_block.header.block_hash(); - let block_merkle_root = *stored_block.header.merkle_root.to_raw_hash().as_byte_array(); - let root_valid = pmt_result.merkle_root == block_merkle_root || { - let block_hash_bytes = { - let mut raw = *block_hash.to_raw_hash().as_byte_array(); - raw.reverse(); - raw - }; - match get_coinbase_information(ctx, &block_hash_bytes) { - Some(mut wr) => { - wr.reverse(); - pmt_result.merkle_root == wr - } - None => false, - } - }; - if !root_valid { - return false; + // validationsForRegisterBtcTransaction: a false is the validations code, + // and a throw reaches `Bridge.registerFlyoverBtcTransaction`'s catch, which + // answers GENERIC_ERROR. + match validations_for_register_btc_transaction( + ctx, + config, + hardfork_cfg, + &btc_tx_hash, + btc_block_height, + &pmt_data, + &btc_tx_data, + ) { + Ok(()) => {} + Err(Invalid::Swallowed(_)) => { + return ok(flyover_code_output( + flyover_codes::UNPROCESSABLE_VALIDATIONS, + )) } - let best_height = load_chain_head(ctx).map(|h| h.height).unwrap_or(0); - let confirmations = best_height as i64 - btc_block_height as i64 + 1; - confirmations >= config.btc2rsk_minimum_acceptable_confirmations as i64 - })(); - if !validations { - return ok(flyover_code_output(flyover_codes::UNPROCESSABLE_VALIDATIONS)); + Err(Invalid::Thrown(_)) => return ok(flyover_code_output(flyover_codes::GENERIC_ERROR)), } // Parse the BTC tx and compute the witness-stripped txid (getHash(false)). - let btc_tx: BtcTransaction = deserialize(&btc_tx_data) - .map_err(|_| PrecompileError::other("registerFastBridgeBtcTransaction: invalid BTC transaction"))?; + // A transaction bitcoinj cannot parse throws, which the same catch answers. + let Ok(btc_tx) = deserialize::(&btc_tx_data) else { + return ok(flyover_code_output(flyover_codes::GENERIC_ERROR)); + }; let btc_tx_hash_no_witness = legacy_btc_txid(&btc_tx); // Second already-used check, keyed by the legacy txid (only when it differs @@ -2397,7 +2433,14 @@ pub fn release_btc( // Convert Wei to satoshis: 1 satoshi = 10^10 wei let satoshis_per_wei = U256::from(10_000_000_000u64); let amount_satoshis_u256 = call_value_wei / satoshis_per_wei; - let amount_satoshis = amount_satoshis_u256.to::(); + // rskj `co.rsk.core.Coin.toBitcoin()` takes the satoshis' `longValue()`, + // their low 64 bits, and the minimum, the event and the refund before + // RSKIP427 all use that long. No mainnet or testnet balance comes near + // 2^63 satoshis, but regtest's genesis funds accounts with 10^30 wei, + // which wrap. A long that wraps negative is below every minimum; rskj + // would then log it negative and refund a negative amount, which this + // reads as zero (a regtest-only difference). + let amount_satoshis = u64::try_from(amount_satoshis_u256.as_limbs()[0] as i64).unwrap_or(0); // rskj `BridgeSupport.releaseBtc` (BridgeSupport.java:891) rejects a peg-out // requested by a CONTRACT before any min-value check, because a BTC address @@ -2457,7 +2500,7 @@ pub fn release_btc( } else { None } - } else if amount_satoshis_u256 <= U256::from(config.legacy_minimum_pegout_tx_value) { + } else if amount_satoshis <= config.legacy_minimum_pegout_tx_value { Some(1) // legacy: always LOW_AMOUNT } else { None @@ -2477,7 +2520,7 @@ pub fn release_btc( let refund_wei = if hardfork_cfg.has_rskip427(block_number) { call_value_wei } else { - amount_satoshis_u256 * U256::from(10_000_000_000u64) + U256::from(amount_satoshis) * satoshis_per_wei }; let _ = ctx .journal_mut() @@ -4283,6 +4326,14 @@ fn is_der_signature(sig: &[u8]) -> bool { true } +/// Does `key` parse the way rskj's `BtcECKey.fromPublicOnly` requires? +/// That is BouncyCastle's `decodePoint`: a compressed, uncompressed or hybrid +/// encoding of a point on the curve, as libsecp256k1 parses them, and also +/// the single byte 0x00, the point at infinity, which libsecp256k1 refuses. +fn is_btc_public_key(key: &[u8]) -> bool { + key == [0x00] || bitcoin::secp256k1::PublicKey::from_slice(key).is_ok() +} + pub fn add_signature( ctx: &mut CTX, args: &[u8], @@ -4290,51 +4341,14 @@ pub fn add_signature( config: &BridgeConstants, hardfork_cfg: &RskHardforkConfig, ) -> Result { - if args.len() < 96 { - return Ok(PrecompileOutput::new(gas_cost, Bytes::new())); - } - - // ABI layout: bytes federatorPublicKey, bytes[] signatures, bytes rskTxHash. - // An out-of-range (oversized) ABI offset cannot index this calldata; treat - // it as a parse failure (empty output) rather than panicking — rskj's - // Solidity decoder likewise rejects such inputs. Mainnet #6,223,783 tx[3] - // forwards malformed calldata whose `signatures` offset points into the - // middle of the federator key, yielding an absurd array count. - let (Ok(fed_key_offset), Ok(sigs_offset), Ok(rsk_hash_offset)) = ( - usize::try_from(U256::from_be_slice(&args[0..32])), - usize::try_from(U256::from_be_slice(&args[32..64])), - usize::try_from(U256::from_be_slice(&args[64..96])), - ) else { - return Ok(PrecompileOutput::new(gas_cost, Bytes::new())); - }; - - // Parse federator public key - let fed_key = match read_dynamic_bytes(args, fed_key_offset) { - Ok(k) => k, - Err(_) => return Ok(PrecompileOutput::new(gas_cost, Bytes::new())), - }; - if fed_key.len() != 33 && fed_key.len() != 65 { - return Ok(PrecompileOutput::new(gas_cost, Bytes::new())); - } - - // Parse RSK tx hash (32 bytes) - let rsk_tx_hash_bytes = match read_dynamic_bytes(args, rsk_hash_offset) { - Ok(h) if h.len() == 32 => h, - _ => return Ok(PrecompileOutput::new(gas_cost, Bytes::new())), - }; - let mut rsk_tx_hash = [0u8; 32]; - rsk_tx_hash.copy_from_slice(&rsk_tx_hash_bytes); - - // Parse signatures array - let sigs = parse_bytes_array(args, sigs_offset); - if sigs.is_empty() { - return Ok(PrecompileOutput::new(gas_cost, Bytes::new())); - } - - // rskj `Bridge.addSignature` (Bridge.java:632) runs every signature through - // `BtcECKey.ECDSASignature.decodeFromDER` before `bridgeSupport` sees it, - // and a parse failure throws `BridgeIllegalArgumentException`. `Bridge.execute` - // wraps that in a `VMException`, which `TransactionExecutor` records as + // rskj `Bridge.addSignature` (Bridge.java:615) parses the federator key + // (`BtcECKey.fromPublicOnly`), refuses an empty signature list, runs every + // signature through `BtcECKey.ECDSASignature.decodeFromDER` and wraps the + // RSK transaction hash in a `Keccak256`, all before `bridgeSupport` sees + // them, and each failure is a `BridgeIllegalArgumentException`. Before + // RSKIP88 `Bridge.execute` answers that with null, a success with no + // output. From RSKIP88 it throws, and `Bridge.execute` wraps it in a + // `VMException`, which `TransactionExecutor` records as // `result.setException` -- marking the execution summary failed, so the // sender is charged the FULL gas limit with no leftover refund, while the // receipt still reports SUCCESS because its status comes from @@ -4343,16 +4357,38 @@ pub fn add_signature( // it into exactly those semantics. // // Mainnet #9,217,796 tx[1] submits a 71-byte signature beginning 0x9f - // rather than a DER SEQUENCE. Without this check rustock reached the + // rather than a DER SEQUENCE. Without the DER check rustock reached the // pegouts-waiting-for-signatures lookup, missed, returned quietly and // refunded 104,512 gas the chain had charged -- diverging the state root. - for sig in &sigs { - if !is_der_signature(sig) { - return Err(PrecompileError::other( - "addSignature: signature could not be parsed as DER", - )); + let rskip88 = + hardfork_cfg.has_rskip88(revm::context_interface::Block::number(ctx.block()).to::()); + let refuse = |why: &'static str| -> Result { + if rskip88 { + Err(PrecompileError::other(why)) + } else { + Ok(PrecompileOutput::new(gas_cost, Bytes::new())) } + }; + let decoded = super::abi::Args::new(args); + let (Some(fed_key), Some(sigs), Some(rsk_tx_hash)) = + (decoded.bytes(0), decoded.bytes_array(1), decoded.bytes(2)) + else { + return refuse("addSignature: malformed arguments"); + }; + if !is_btc_public_key(fed_key) { + return refuse("addSignature: public key could not be parsed"); + } + if sigs.is_empty() { + return refuse("addSignature: signatures array is empty"); + } + if !sigs.iter().all(|sig| is_der_signature(sig)) { + return refuse("addSignature: signature could not be parsed as DER"); } + let Ok(rsk_tx_hash) = <[u8; 32]>::try_from(rsk_tx_hash) else { + return refuse("addSignature: invalid rsk tx hash"); + }; + let fed_key = fed_key.to_vec(); + let sigs: Vec> = sigs.into_iter().map(<[u8]>::to_vec).collect(); // RSKIP419: if the SVP is ongoing and this RSK tx hash is the one that // created the SVP spend transaction, sign that (with the PROPOSED @@ -5350,54 +5386,6 @@ fn apply_signatures_to_tx( signed } -/// Parse an array of dynamic bytes from ABI-encoded `bytes[]` argument. -fn parse_bytes_array(args: &[u8], array_offset: usize) -> Vec> { - if array_offset + 32 > args.len() { - return Vec::new(); - } - // Any ABI length/offset that does not fit in a usize is necessarily out of - // range for this calldata, so treat it as unparseable (an empty/short - // result) rather than panicking — rskj's Solidity decoder likewise rejects - // such inputs (the count guard runs after the read, so the conversion must - // never overflow). Mainnet #6,223,783 tx[3] feeds a count of ~uint256::MAX. - let Ok(count) = usize::try_from(U256::from_be_slice(&args[array_offset..array_offset + 32])) - else { - return Vec::new(); - }; - if count == 0 || count > 1000 { - return Vec::new(); - } - - let offsets_start = array_offset + 32; - let mut result = Vec::with_capacity(count); - - for i in 0..count { - let off_pos = offsets_start + i * 32; - if off_pos + 32 > args.len() { - break; - } - let Ok(elem_rel_offset) = - usize::try_from(U256::from_be_slice(&args[off_pos..off_pos + 32])) - else { - break; - }; - let elem_abs = offsets_start + elem_rel_offset; - if elem_abs + 32 > args.len() { - break; - } - let Ok(elem_len) = usize::try_from(U256::from_be_slice(&args[elem_abs..elem_abs + 32])) - else { - break; - }; - let data_start = elem_abs + 32; - if data_start + elem_len > args.len() { - break; - } - result.push(args[data_start..data_start + elem_len].to_vec()); - } - result -} - // --------------------------------------------------------------------------- // Helpers // --------------------------------------------------------------------------- @@ -5408,22 +5396,6 @@ fn btc_satoshi_to_rbtc_wei(satoshis: u64) -> U256 { U256::from(satoshis) * U256::from(10_000_000_000u64) // 10^10 } -fn read_dynamic_bytes(args: &[u8], offset: usize) -> Result, PrecompileError> { - if offset + 32 > args.len() { - return Err(PrecompileError::other("ABI: offset out of bounds")); - } - // An oversized length can never fit this calldata: reject it (rskj's - // decoder does the same) instead of panicking on the usize conversion. - let Ok(len) = usize::try_from(U256::from_be_slice(&args[offset..offset + 32])) else { - return Err(PrecompileError::other("ABI: length out of bounds")); - }; - let data_start = offset + 32; - if data_start + len > args.len() { - return Err(PrecompileError::other("ABI: data out of bounds")); - } - Ok(args[data_start..data_start + len].to_vec()) -} - // --------------------------------------------------------------------------- // Read-only query methods // --------------------------------------------------------------------------- @@ -5716,16 +5688,22 @@ pub fn get_estimated_fees_for_pegout_amount( "getEstimatedFeesForPegOutAmount: method not enabled", )); } - if args.len() < 32 { - return Err(PrecompileError::other( - "getEstimatedFeesForPegOutAmount: args too short", - )); - } - - // co.rsk.core.Coin (wei) -> bitcoin Coin (satoshi): / 10^10 - let wei = U256::from_be_slice(&args[0..32]); - let satoshis = wei / U256::from(10_000_000_000u64); - if satoshis < U256::from(config.minimum_pegout_tx_value) { + // rskj `new co.rsk.core.Coin((BigInteger) args[0]).toBitcoin()`: the wei + // as a signed number, divided by 10^10 toward zero, then `longValue()`, + // its low 64 bits. A negative amount is below the minimum like any other. + let wei = super::abi::Args::new(args) + .int(0) + .ok_or_else(|| PrecompileError::other("getEstimatedFeesForPegOutAmount: no amount"))?; + let negative = wei.0[0] & 0x80 != 0; + let raw = U256::from_be_bytes(wei.0); + let magnitude = if negative { + (!raw).wrapping_add(U256::from(1)) + } else { + raw + }; + let low = (magnitude / U256::from(10_000_000_000u64)).as_limbs()[0] as i64; + let satoshis = if negative { low.wrapping_neg() } else { low }; + if satoshis < config.minimum_pegout_tx_value as i64 { return Err(PrecompileError::other( "getEstimatedFeesForPegOutAmount: peg-out amount is below the minimum peg-out value", )); @@ -7849,28 +7827,215 @@ mod tests { ); } - /// Regression: an ABI `bytes[]` whose declared count does not fit in a - /// usize must be rejected (empty result), never panic on the U256->usize - /// conversion. Mainnet #6,223,783 tx[3] forwarded malformed addSignature - /// calldata whose `signatures` offset landed mid-key, yielding an absurd - /// (~uint256::MAX) array count. + use crate::bridge::abi::encode::{self, Arg}; + + /// A tree of one transaction, the one given, matched. Its root is the + /// transaction's hash, so it proves any bytes at all. + fn one_leaf_tree(tx: &[u8]) -> Vec { + let mut pmt = 1u32.to_le_bytes().to_vec(); + pmt.push(1); + pmt.extend_from_slice(&calculate_btc_tx_hash(tx)); + pmt.extend_from_slice(&[1, 0x01]); + pmt + } + + /// What a crafted transaction needs for `validateInputsCount`: the version, + /// then an input count of one, or from RSKIP143 a segwit marker and flag + /// with a count of zero. + const ONE_INPUT: [u8; 5] = [1, 0, 0, 0, 1]; + const NO_INPUTS: [u8; 7] = [1, 0, 0, 0, 0, 1, 0]; + + /// rskj `validateInputsCount`: what counts as no inputs, before and after + /// RSKIP143, and the transactions too short for bitcoinj to read. + #[test] + fn a_transaction_without_inputs_is_what_rskj_counts() { + assert!(has_inputs(&ONE_INPUT, false)); + // Zero, also when bitcoinj reads it from a longer encoding. + assert!(!has_inputs(&[1, 0, 0, 0, 0], false)); + assert!(!has_inputs(&[1, 0, 0, 0, 0xFD, 0, 0], false)); + // From RSKIP143 a zero may be a segwit marker: with flag 1 the count + // after it decides, and any other flag is not refused. + assert!(!has_inputs(&NO_INPUTS, true)); + assert!(has_inputs(&[1, 0, 0, 0, 0, 1, 2], true)); + assert!(has_inputs(&[1, 0, 0, 0, 0, 2], true)); + // Too short to read throws too. + assert!(!has_inputs(&[1, 0, 0, 0], false)); + assert!(!has_inputs(&[1, 0, 0, 0, 0], true)); + assert!(!has_inputs(&[1, 0, 0, 0, 0, 1], true)); + assert!(!has_inputs(&[1, 0, 0, 0, 0xFD, 0], false)); + } + + /// rskj's `validationsForRegisterBtcTransaction`, check by check, in its + /// order. Regtest needs one confirmation, so height 0 passes with no chain + /// head stored, and with none stored the block store refuses the lookup. + #[test] + fn pegin_validations_answer_as_rskj_in_its_order() { + let config = BridgeConstants::regtest(); + let cfg = RskHardforkConfig::regtest(); + let run = |height: i32, pmt: &[u8], tx: &[u8]| { + let mut ctx = queue_ctx(); + let hash = calculate_btc_tx_hash(tx); + match validations_for_register_btc_transaction( + &mut ctx, &config, &cfg, &hash, height, pmt, tx, + ) { + Ok(()) => ("valid", ""), + Err(Invalid::Swallowed(why)) => ("false", why), + Err(Invalid::Thrown(why)) => ("throws", why), + } + }; + let garbage = [0xFF; 3]; + // Too few confirmations comes first, even with a tree that would throw. + assert_eq!( + run(5, &garbage, &ONE_INPUT), + ("false", "insufficient confirmations") + ); + assert_eq!(run(-1, &garbage, &ONE_INPUT), ("false", "negative height")); + // Then the tree: the wrong size throws, and a tree without the + // transaction is false. + assert_eq!(run(0, &garbage, &ONE_INPUT).0, "throws"); + assert_eq!( + run(0, &one_leaf_tree(&NO_INPUTS), &ONE_INPUT), + ("false", "tx not in PMT") + ); + // A transaction without inputs throws before any block is looked at. + assert_eq!( + run(0, &one_leaf_tree(&NO_INPUTS), &NO_INPUTS), + ("throws", "the transaction has no inputs") + ); + // With inputs it reaches the store, which throws. + assert_eq!( + run(0, &one_leaf_tree(&ONE_INPUT), &ONE_INPUT), + ("throws", "the block store refused the height") + ); + } + + /// The divergence the validations fix closes: since RSKIP199 anyone may + /// call registerBtcTransaction, and a crafted transaction without inputs, + /// proved by a one-leaf tree, makes rskj throw and fail the transaction. + /// Answered with a quiet success instead, the sender would pay a + /// different fee and the block's state root would differ. #[test] - fn parse_bytes_array_rejects_oversized_count_without_panic() { - // 32-byte count word = uint256::MAX (left of any plausible calldata). - let args = vec![0xFFu8; 32]; - assert!(parse_bytes_array(&args, 0).is_empty()); + fn a_crafted_pegin_without_inputs_fails_the_transaction() { + let tree = one_leaf_tree(&NO_INPUTS); + let args = encode::args(&[ + Arg::Bytes(&NO_INPUTS), + Arg::Word(encode::int(0)), + Arg::Bytes(&tree), + ]); + let mut ctx = queue_ctx(); + let result = register_btc_transaction( + &mut ctx, + &args, + 0, + &BridgeConstants::regtest(), + &RskHardforkConfig::regtest(), + &BridgeTxContext::default(), + ); + assert!(result.is_err()); + } - // A within-range but too-large count (> 1000 guard) is also rejected. - let mut over = [0u8; 32]; - over[30] = 0x10; // 0x1000 = 4096 > 1000 - assert!(parse_bytes_array(&over, 0).is_empty()); + /// rskj's `Bridge.registerFlyoverBtcTransaction` catches whatever its + /// validations throw and answers GENERIC_ERROR, and answers their false + /// with the validations code, the checks running in rskj's order. + #[test] + fn a_flyover_throw_is_a_generic_error_and_a_false_the_validations_code() { + let config = BridgeConstants::regtest(); + let cfg = RskHardforkConfig::regtest(); + let lbc = RskAddress::repeat_byte(0x42); + let mut lbc_word = [0u8; 32]; + lbc_word[12..].copy_from_slice(lbc.as_slice()); + let code = |tx: &[u8], height: i64, pmt: &[u8]| { + let args = encode::args(&[ + Arg::Bytes(tx), + Arg::Word(encode::int(height)), + Arg::Bytes(pmt), + Arg::Word([0x11; 32]), + Arg::Bytes(&[0x6F; 21]), + Arg::Word(lbc_word), + Arg::Bytes(&[0x6F; 21]), + Arg::Word([0; 32]), + ]); + let mut ctx = queue_ctx(); + let ctx_tx = BridgeTxContext::default(); + register_fast_bridge_btc_transaction(&mut ctx, &args, 0, &config, &cfg, &ctx_tx, lbc, 2) + .unwrap() + .bytes + }; + let generic = flyover_code_output(flyover_codes::GENERIC_ERROR); + let validations = flyover_code_output(flyover_codes::UNPROCESSABLE_VALIDATIONS); + assert_eq!( + code(&NO_INPUTS, 0, &one_leaf_tree(&NO_INPUTS)), + generic, + "no inputs" + ); + assert_eq!( + code(&ONE_INPUT, 0, &[0xFF; 3]), + generic, + "a tree of the wrong size" + ); + assert_eq!( + code(&ONE_INPUT, i64::from(i32::MAX), &[0xFF; 3]), + validations, + "confirmations first" + ); + assert_eq!( + code(&ONE_INPUT, -1, &[0xFF; 3]), + validations, + "a negative height" + ); } - /// Regression: `read_dynamic_bytes` must reject an oversized ABI length - /// rather than panic converting it to usize (same malformed-calldata path). + /// rskj's `Bridge.addSignature` turns down a key bitcoinj cannot parse, an + /// empty list of signatures, a signature that is not DER and a hash that is + /// not 32 bytes, each with a `BridgeIllegalArgumentException`: before + /// RSKIP88 a success with no output, from it a throw. The single byte 0, + /// the point at infinity, is a key bitcoinj parses. #[test] - fn read_dynamic_bytes_rejects_oversized_length() { - let args = vec![0xFFu8; 64]; // offset 0 -> length word = uint256::MAX - assert!(read_dynamic_bytes(&args, 0).is_err()); + fn add_signature_refuses_what_rskj_cannot_parse() { + let der: &[u8] = &[0x30, 0x06, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01]; + let not_der: &[u8] = &[0x9f, 0x06, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01]; + let key = real_pubkey(); + let run = |cfg: &RskHardforkConfig, key: &[u8], sigs: &[&[u8]], hash: &[u8]| { + let args = encode::args(&[Arg::Bytes(key), Arg::BytesArray(sigs), Arg::Bytes(hash)]); + let mut ctx = queue_ctx(); + add_signature(&mut ctx, &args, 0, &BridgeConstants::regtest(), cfg).map(|out| out.bytes) + }; + let rskip88 = RskHardforkConfig::regtest(); + assert!( + run(&rskip88, &[0x05; 33], &[der], &[0x11; 32]).is_err(), + "not a key" + ); + assert!( + run(&rskip88, &key, &[], &[0x11; 32]).is_err(), + "no signatures" + ); + assert!( + run(&rskip88, &key, &[der, not_der], &[0x11; 32]).is_err(), + "not DER" + ); + assert!( + run(&rskip88, &key, &[der], &[0x11; 31]).is_err(), + "not a hash" + ); + assert_eq!( + run(&rskip88, &key, &[der], &[0x11; 32]).unwrap(), + Bytes::new(), + "not a member" + ); + assert_eq!( + run(&rskip88, &[0x00], &[der], &[0x11; 32]).unwrap(), + Bytes::new(), + "infinity" + ); + // Mainnet's block 0 is before RSKIP88. + let before = RskHardforkConfig::mainnet(); + assert_eq!( + run(&before, &[0x05; 33], &[der], &[0x11; 32]).unwrap(), + Bytes::new() + ); + assert_eq!( + run(&before, &key, &[not_der], &[0x11; 32]).unwrap(), + Bytes::new() + ); } } diff --git a/crates/execution/src/bridge/pmt.rs b/crates/execution/src/bridge/pmt.rs index ffaeb72..5809d4b 100644 --- a/crates/execution/src/bridge/pmt.rs +++ b/crates/execution/src/bridge/pmt.rs @@ -90,28 +90,14 @@ impl MerkleBranch { /// Parse from ABI-encoded arguments: /// `(bytes32 btcTxHash, bytes32 btcBlockHash, uint256 merkleBranchPath, bytes32[] merkleBranchHashes)`. /// The transaction and block hashes come back as given, in display order, - /// and the path as rskj's `BigInteger.intValue` reads it, its low 32 bits. + /// and the path as rskj's `BigInteger.intValue` reads it, its low 32 bits, + /// all through the decoder the Bridge's argument check uses. pub fn from_abi_args(args: &[u8]) -> Option<(Self, [u8; 32], [u8; 32])> { - if args.len() < 128 { - return None; - } - let tx_hash: [u8; 32] = args[0..32].try_into().ok()?; - let block_hash: [u8; 32] = args[32..64].try_into().ok()?; - let path = i32::from_be_bytes(args[92..96].try_into().ok()?); - - // The array's offset and, there, its length, each in its word's low - // four bytes. - let word = |at: usize| -> Option { - Some(u32::from_be_bytes(args.get(at + 28..at + 32)?.try_into().ok()?) as usize) - }; - let array_offset = word(96)?; - let count = word(array_offset)?; - let data_start = array_offset + 32; - let mut hashes = Vec::new(); - for i in 0..count { - let at = data_start + i * 32; - hashes.push(args.get(at..at + 32)?.try_into().ok()?); - } + let args = super::abi::Args::new(args); + let tx_hash = args.bytes32(0)?; + let block_hash = args.bytes32(1)?; + let path = args.int(2)?.int_value(); + let hashes = args.bytes32_array(3)?; Some((MerkleBranch::new(hashes, path)?, tx_hash, block_hash)) } diff --git a/crates/execution/src/bridge/tx.rs b/crates/execution/src/bridge/tx.rs index f832514..cf08a39 100644 --- a/crates/execution/src/bridge/tx.rs +++ b/crates/execution/src/bridge/tx.rs @@ -174,29 +174,29 @@ pub fn register_btc_coinbase_transaction( args: &[u8], gas_cost: u64, ) -> Result { - // Decode 5 ABI arguments - if args.len() < 160 { + // The five arguments, as rskj's decoder reads them: the transaction, the + // block hash, the partial merkle tree, the witness merkle root and the + // witness reserved value. + let decoded = super::abi::Args::new(args); + let ( + Some(btc_tx_data), + Some(block_hash), + Some(pmt_data), + Some(witness_merkle_root), + Some(witness_reserved_value), + ) = ( + decoded.bytes(0), + decoded.bytes32(1), + decoded.bytes(2), + decoded.bytes32(3), + decoded.bytes32(4), + ) + else { return Err(PrecompileError::other( - "registerBtcCoinbaseTransaction: args too short", + "registerBtcCoinbaseTransaction: malformed arguments", )); - } - - // Parse the 5 ABI-encoded arguments: - // arg0: bytes btcTx (dynamic) - // arg1: bytes32 blockHash - // arg2: bytes pmtSerialized (dynamic) - // arg3: bytes32 witnessMerkleRoot - // arg4: bytes32 witnessReservedValue - - let btc_tx_offset = U256::from_be_slice(&args[0..32]).to::(); - let block_hash: [u8; 32] = args[32..64].try_into().unwrap(); - let pmt_offset = U256::from_be_slice(&args[64..96]).to::(); - let witness_merkle_root: [u8; 32] = args[96..128].try_into().unwrap(); - let witness_reserved_value: [u8; 32] = args[128..160].try_into().unwrap(); - - // Read dynamic bytes - let btc_tx_data = read_abi_dynamic_bytes(args, btc_tx_offset)?; - let pmt_data = read_abi_dynamic_bytes(args, pmt_offset)?; + }; + let (btc_tx_data, pmt_data) = (btc_tx_data.to_vec(), pmt_data.to_vec()); // Compute BTC tx hash (double-SHA256) let btc_tx_hash = calculate_btc_tx_hash(&btc_tx_data); @@ -293,13 +293,9 @@ pub fn has_btc_block_coinbase_info( args: &[u8], gas_cost: u64, ) -> Result { - if args.len() < 32 { - return Err(PrecompileError::other( - "hasBtcBlockCoinbaseTransactionInformation: args too short", - )); - } - - let block_hash: [u8; 32] = args[..32].try_into().unwrap(); + let block_hash = super::abi::Args::new(args).bytes32(0).ok_or_else(|| { + PrecompileError::other("hasBtcBlockCoinbaseTransactionInformation: no block hash") + })?; let has_info = has_coinbase_information(ctx, &block_hash); let mut output = [0u8; 32]; @@ -497,18 +493,6 @@ fn find_witness_commitment(tx: &bitcoin::Transaction) -> Option<[u8; 32]> { }) } -fn read_abi_dynamic_bytes(args: &[u8], offset: usize) -> Result, PrecompileError> { - if offset + 32 > args.len() { - return Err(PrecompileError::other("ABI: offset out of bounds")); - } - let len = U256::from_be_slice(&args[offset..offset + 32]).to::(); - let data_start = offset + 32; - if data_start + len > args.len() { - return Err(PrecompileError::other("ABI: data out of bounds")); - } - Ok(args[data_start..data_start + len].to_vec()) -} - fn encode_int_output(gas_cost: u64, value: i64) -> PrecompileOutput { let mut output = [0u8; 32]; if value >= 0 { diff --git a/crates/execution/src/bridge/union.rs b/crates/execution/src/bridge/union.rs index 4b8bdd2..fe29e50 100644 --- a/crates/execution/src/bridge/union.rs +++ b/crates/execution/src/bridge/union.rs @@ -245,7 +245,9 @@ pub fn set_contract_address_for_testnet( if sender != constants.change_address_authorizer { return int_result(gas_cost, UnionResponse::UnauthorizedCaller.code()); } - let Some(new_address) = abi_address(args, 0) else { + // rskj `new RskAddress((DataWord) args[0])`: the address as its decoder + // reads it, an empty argument list being the zero address. + let Some(new_address) = super::abi::Args::new(args).address(0) else { return int_result(gas_cost, UnionResponse::GenericError.code()); }; store_long(ctx, UNION_CONTRACT_ADDRESS_KEY, &rlp_encode_bytes(new_address.as_slice())); @@ -289,11 +291,15 @@ pub fn increase_locking_cap( if sender != constants.change_locking_cap_authorizer { return int_result(gas_cost, UnionResponse::UnauthorizedCaller.code()); } - let Some(new_cap) = abi_uint(args, 0) else { + let Some(new_cap) = super::abi::Args::new(args).int(0) else { return int_result(gas_cost, UnionResponse::GenericError.code()); }; let current = locking_cap(ctx, hardfork_cfg); let max_allowed = current * U256::from(constants.locking_cap_increments_multiplier); + // A negative cap never increases the current one. + let Some(new_cap) = coin(&new_cap) else { + return int_result(gas_cost, UnionResponse::InvalidValue.code()); + }; if new_cap <= current || new_cap > max_allowed { return int_result(gas_cost, UnionResponse::InvalidValue.code()); } @@ -321,12 +327,15 @@ pub fn request_rbtc( if !request_enabled(ctx) { return int_result(gas_cost, UnionResponse::TransferDisabled.code()); } - let Some(amount) = abi_uint(args, 0) else { + let Some(amount) = super::abi::Args::new(args).int(0) else { return int_result(gas_cost, UnionResponse::GenericError.code()); }; - if amount.is_zero() { + // rskj `isAmountRequestedValid` refuses an amount that is not positive, + // and the amount is signed: a negative one would otherwise wrap the total + // below the cap. + let Some(amount) = coin(&amount).filter(|amount| !amount.is_zero()) else { return int_result(gas_cost, UnionResponse::InvalidValue.code()); - } + }; let previous = weis_transferred(ctx); let new_total = previous + amount; if new_total > locking_cap(ctx, hardfork_cfg) { @@ -411,7 +420,10 @@ pub fn set_transfer_permissions( if sender != constants.change_transfer_permissions_authorizer { return int_result(gas_cost, UnionResponse::UnauthorizedCaller.code()); } - let (Some(request), Some(release)) = (abi_bool(args, 0), abi_bool(args, 1)) else { + // rskj's `BoolType` looks at its word's low 32 bits only, and an empty + // argument list reads as two falses. + let args = super::abi::Args::new(args); + let (Some(request), Some(release)) = (args.bool(0), args.bool(1)) else { return int_result(gas_cost, UnionResponse::GenericError.code()); }; @@ -457,9 +469,12 @@ pub fn set_event( hardfork_cfg: &RskHardforkConfig, sender: Address, ) -> Result { - let event = abi_bytes(args).ok_or_else(|| { - PrecompileError::other("union bridge event: the bytes argument does not decode") - })?; + let event = super::abi::Args::new(args) + .bytes(0) + .map(<[u8]>::to_vec) + .ok_or_else(|| { + PrecompileError::other("union bridge event: the bytes argument does not decode") + })?; require_union_bridge_caller(ctx, hardfork_cfg, sender)?; if event.len() > MAX_EVENT_DATA_LENGTH { return Err(PrecompileError::other(format!( @@ -504,36 +519,11 @@ fn require_union_bridge_caller( // Helpers // --------------------------------------------------------------------------- -fn abi_word(args: &[u8], index: usize) -> Option<&[u8]> { - args.get(index * 32..index * 32 + 32) -} - -fn abi_address(args: &[u8], index: usize) -> Option
{ - abi_word(args, index).map(|w| Address::from_slice(&w[12..])) -} - -fn abi_uint(args: &[u8], index: usize) -> Option { - abi_word(args, index).map(U256::from_be_slice) -} - -fn abi_bool(args: &[u8], index: usize) -> Option { - abi_word(args, index).map(|w| w.iter().any(|b| *b != 0)) -} - -/// The single `bytes` argument, read as rskj's `BytesType.decode` reads it: -/// the offset and the length are each the low 32 bits of their word as a -/// Java int (`IntType.decodeInt(..).intValue()`), so high bits are ignored -/// where `U256::to::` would panic. The dispatcher has already checked -/// that the arguments decode (`abi_args_decode_ok`); `None` is a backstop. -fn abi_bytes(args: &[u8]) -> Option> { - let int_at = |at: usize| -> Option { - let word = args.get(at..at.checked_add(32)?)?; - usize::try_from(i32::from_be_bytes(word[28..32].try_into().ok()?)).ok() - }; - let offset = int_at(0)?; - let len = int_at(offset)?; - let start = offset.checked_add(32)?; - args.get(start..start.checked_add(len)?).map(<[u8]>::to_vec) +/// rskj `new co.rsk.core.Coin((BigInteger) arg)`: the word as a signed +/// number, `None` where it is negative. +fn coin(value: &super::abi::Int) -> Option { + let value = value.signed(); + (!value.is_negative()).then(|| value.into_raw()) } /// Moves RBTC out of the Bridge's own balance. @@ -800,8 +790,93 @@ mod tests { let mut args = bytes_arg(&event); args[..28].fill(0xFF); args[32..60].fill(0x01); - assert_eq!(abi_bytes(&args), Some(event)); - assert_eq!(abi_bytes(&[]), None); - assert_eq!(abi_bytes(&args[..40]), None, "the length word is cut short"); + let cfg = RskHardforkConfig::testnet(); + let mut c = testnet_ctx(); + set_event(&mut c, SUPER_EVENT_KEY, &args, 8_000, &cfg, union_bridge()).unwrap(); + assert_eq!(load_long(&mut c, SUPER_EVENT_KEY), event); + assert!(set_event(&mut c, SUPER_EVENT_KEY, &[], 8_000, &cfg, union_bridge()).is_err()); + assert!( + set_event( + &mut c, + SUPER_EVENT_KEY, + &args[..40], + 8_000, + &cfg, + union_bridge() + ) + .is_err(), + "the length word is cut short" + ); + } + + fn code(out: PrecompileOutput) -> i64 { + i64::from_be_bytes(out.bytes[24..32].try_into().unwrap()) + } + + /// rskj's amounts are signed: a word with its top bit set is a negative + /// cap or request, refused as INVALID_VALUE, where read unsigned it was a + /// huge one that wrapped the running total back under the cap. + #[test] + fn a_negative_cap_or_amount_is_an_invalid_value() { + let cfg = RskHardforkConfig::testnet(); + let minus_one = [0xFF; 32]; + let authorizer = union_constants(&cfg).change_locking_cap_authorizer; + let mut c = ctx(); + let out = increase_locking_cap(&mut c, &minus_one, 0, &cfg, authorizer).unwrap(); + assert_eq!(code(out), UnionResponse::InvalidValue.code()); + + let mut c = testnet_ctx(); + store_coin(&mut c, WEIS_TRANSFERRED_KEY, U256::from(5u64)); + let out = request_rbtc(&mut c, &minus_one, 0, &cfg, union_bridge()).unwrap(); + assert_eq!(code(out), UnionResponse::InvalidValue.code()); + assert_eq!( + weis_transferred(&mut c), + U256::from(5u64), + "the total is untouched" + ); + } + + /// An empty argument list reads as zeros: two falses for the permissions, + /// and a zero cap, which is no increase. + #[test] + fn an_empty_argument_list_reads_as_zeros() { + let cfg = RskHardforkConfig::testnet(); + let constants = union_constants(&cfg); + let mut c = ctx(); + let out = set_transfer_permissions( + &mut c, + &[], + 0, + &cfg, + constants.change_transfer_permissions_authorizer, + ) + .unwrap(); + assert_eq!(code(out), UnionResponse::Success.code()); + assert!(!request_enabled(&mut c) && !release_enabled(&mut c)); + + let out = increase_locking_cap( + &mut c, + &[], + 0, + &cfg, + constants.change_locking_cap_authorizer, + ) + .unwrap(); + assert_eq!(code(out), UnionResponse::InvalidValue.code()); + } + + /// rskj's `BoolType` is `intValue() != 0`: a word whose only set bits are + /// above its low 32 is false. + #[test] + fn a_flag_looks_only_at_its_low_32_bits() { + let cfg = RskHardforkConfig::testnet(); + let authorizer = union_constants(&cfg).change_transfer_permissions_authorizer; + let mut args = [0u8; 64]; + args[0] = 1; + args[63] = 1; + let mut c = ctx(); + set_transfer_permissions(&mut c, &args, 0, &cfg, authorizer).unwrap(); + assert!(!request_enabled(&mut c), "only a high bit set"); + assert!(release_enabled(&mut c)); } } diff --git a/crates/execution/src/executor.rs b/crates/execution/src/executor.rs index 1d2d063..9270c4b 100644 --- a/crates/execution/src/executor.rs +++ b/crates/execution/src/executor.rs @@ -7898,6 +7898,108 @@ mod tests { assert_eq!(result.tx_results[0].gas_used, 0, "all of the zero gas limit consumed"); } + /// rskj reads every ABI offset, length and count with `intValue()`: the + /// word's low 32 bits, the rest ignored. A call with 2^64 added to one of + /// those words is therefore the same call. Converting the whole word + /// instead panicked the node, for every one of these: the call's gas is + /// worked out, or its arguments read, before anything checks the sender. + #[test] + fn bridge_offsets_are_read_from_their_low_32_bits() { + use crate::bridge::abi::encode::{self, Arg}; + let run = |input: &[u8]| { + let store = Arc::new(MemoryTrieStore::new()); + let sender = Address::repeat_byte(0xAA); + let one_rbtc = U256::from(10u64).pow(U256::from(18)); + let root = put_account(&TrieNode::empty(), store.as_ref(), &sender, 0, one_rbtc); + let block_store = + Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let header = dummy_header(7_000_000); + let tx = rustock_core::Transaction { + nonce: 0, + gas_price: U256::ZERO, + gas_limit: U256::from(200_000), + to: Bytes::copy_from_slice(crate::precompiles::BRIDGE_ADDR.as_slice()), + value: U256::ZERO, + input: Bytes::copy_from_slice(input), + v: 28, + r: U256::from(1), + s: U256::from(1), + cached_rlp: None, + }; + let executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); + let result = executor + .execute_tx(&header, &tx, sender, &root, store) + .unwrap(); + (result.success, result.gas_used) + }; + // Each input ends in a word nothing reads, so that the same call can + // carry one more nonzero byte without reading differently: 2^64 added + // to its `word`th word, or a 1 in that last word. Both pay the same + // calldata gas. + let call = |signature: &str, args: &[Arg]| { + [ + block_header_selector(signature).to_vec(), + encode::args(args), + vec![0; 32], + ] + .concat() + }; + let high = |input: &[u8], word: usize| { + let mut input = input.to_vec(); + input[4 + 32 * word + 23] = 1; + input + }; + let unread = |input: &[u8]| { + let mut input = input.to_vec(); + *input.last_mut().unwrap() = 1; + input + }; + let header = [0u8; 80]; + let one_header = call("receiveHeader(bytes)", &[Arg::Bytes(&header)]); + let headers = call("receiveHeaders(bytes[])", &[Arg::BytesArray(&[&header])]); + let confirmations = call( + "getBtcTransactionConfirmations(bytes32,bytes32,uint256,bytes32[])", + &[ + Arg::Word([1; 32]), + Arg::Word([2; 32]), + Arg::Word([0; 32]), + Arg::Words(&[[3; 32]]), + ], + ); + let flyover = call( + "registerFastBridgeBtcTransaction(bytes,uint256,bytes,bytes32,bytes,address,bytes,bool)", + &[ + Arg::Bytes(&[1, 0, 0, 0, 1]), + Arg::Word([0; 32]), + Arg::Bytes(&[0; 3]), + Arg::Word([0x11; 32]), + Arg::Bytes(&[0; 21]), + Arg::Word([0; 32]), + Arg::Bytes(&[0; 21]), + Arg::Word([0; 32]), + ], + ); + // Each call's offsets, then: the header array's count and its element's + // offset; the merkle path and the hashes' count; the flyover height. + for (name, input, word) in [ + ("receiveHeader", &one_header, 0), + ("receiveHeaders", &headers, 0), + ("receiveHeaders", &headers, 1), + ("receiveHeaders", &headers, 2), + ("getBtcTransactionConfirmations", &confirmations, 2), + ("getBtcTransactionConfirmations", &confirmations, 3), + ("getBtcTransactionConfirmations", &confirmations, 4), + ("registerFastBridgeBtcTransaction", &flyover, 0), + ("registerFastBridgeBtcTransaction", &flyover, 1), + ] { + assert_eq!( + run(&high(input, word)), + run(&unread(input)), + "{name}, word {word}" + ); + } + } + /// Regression for the sync halt at mainnet #6916: the block's first tx is /// a registerBtcTransaction whose referenced BTC block (height 0x07ab2c) /// is absent from the bridge's header chain. rskj wraps the method body in From dfe5b026343267d6c65c315d33eac18f8eb36901 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Sun, 8 Nov 2026 21:18:52 -0800 Subject: [PATCH 041/264] native contracts run as rskj's NativeContract; a throw in a transaction's own call is the invisible exception HDWalletUtils, BlockHeader and Environment are rskj's native contracts (co.rsk.pcc.NativeContract) and now run as one: a new native module reads a call's selector and arguments with the shared crate::abi decoder (moved from bridge::abi), charges the method's getGasForData before it runs, and settles a throw as rskj does. Nothing they read from a call can make them panic: offsets, counts and lengths are rskj's Java ints. A precompile that throws in a transaction's own call is rskj's invisible exception (TransactionExecutor.call), now for every precompile, not only the Bridge and REMASC: the receipt is a success with no output that used the intrinsic gas and getGasForData, the sender pays for the whole gas limit, REMASC receives it, the value stays with the sender, and the precompile's account is not created. The precompile's frame reverts, so revm undoes the value and the touch, and RskHandler's last_frame_result turns the result back into a success. In a contract's call a throw costs getGasForData and the call fails from RSKIP197; before it, it spends the call's gas (#295). A call short of getGasForData is out of gas before the precompile runs. What changes with it, each as rskj answers: - the natives are precompiles only from their activation; Environment and BlockHeader used to answer at their addresses before it; - Environment's getCallStackDepth is the caller's depth plus one, the contract a transaction calls being depth 0 (it answered one too many); - BlockHeader walks up from the executing block's parent by hash until it meets the main chain, as getBlockAtDepthStartingAt does, so a block on a fork reads its own ancestors, and past depth 0 the genesis block is out of reach; 1 to 31 bytes after the selector do not parse; getMergedMiningTags throws for a block without a coinbase (an empty RLP field is rskj's null) and for a tag whose hash runs past the end; getBitcoinHeader answers no bytes at all for a block without one; an uncle index is read before the uncles, which come from the body, the freezer or the header's uncle count; - a block, body or total difficulty the store lacks, which rskj always holds, stops the node instead of answering empty. The header sync records a total only for each run's newest block, so for getCumulativeWork that gap is still open; - HDWalletUtils follows bitcoinj: the xpub and tpub prefixes and headers, a depth that wraps past 255, CKDpub through libsecp256k1, a path read as Java's Integer.parseInt reads it (signs, leading zeros, decimal digits of any script), hybrid keys in a multisig; - BN128 refusing a point answers no bytes before RSKIP197 (BN128PrecompiledContract.safeExecute) and throws from it; a modexp length no int holds is out of gas, as rskj prices it above 10^16. Tests: rskj's vectors for every HDWalletUtils method and a payload sweep that never panics; BlockHeader's depths, forks, genesis, uncles, RSKIP536 and a store with gaps; Environment three calls deep (nested_environment_calls); the invisible exception's receipt, fees, value and account; out of gas before a native runs; BN128 either side of RSKIP197; and a contract's throwing call either side of RSKIP197. The old implementations' tests in precompiles.rs go with them; every case they checked is in the new tests, with rskj's own test constants where those differ. --- crates/execution/src/{bridge => }/abi.rs | 106 +- crates/execution/src/bridge/btc_chain.rs | 25 +- crates/execution/src/bridge/getters.rs | 39 +- crates/execution/src/bridge/governance.rs | 16 +- crates/execution/src/bridge/mod.rs | 17 +- crates/execution/src/bridge/peg.rs | 10 +- crates/execution/src/bridge/pmt.rs | 2 +- crates/execution/src/bridge/tx.rs | 4 +- crates/execution/src/bridge/union.rs | 12 +- crates/execution/src/executor.rs | 461 +++- crates/execution/src/lib.rs | 2 + crates/execution/src/native/block_header.rs | 693 ++++++ crates/execution/src/native/environment.rs | 90 + crates/execution/src/native/hd_wallet.rs | 705 ++++++ crates/execution/src/native/mod.rs | 193 ++ crates/execution/src/precompiles.rs | 2273 +++---------------- crates/execution/src/rsk_handler.rs | 33 +- 17 files changed, 2663 insertions(+), 2018 deletions(-) rename crates/execution/src/{bridge => }/abi.rs (83%) create mode 100644 crates/execution/src/native/block_header.rs create mode 100644 crates/execution/src/native/environment.rs create mode 100644 crates/execution/src/native/hd_wallet.rs create mode 100644 crates/execution/src/native/mod.rs diff --git a/crates/execution/src/bridge/abi.rs b/crates/execution/src/abi.rs similarity index 83% rename from crates/execution/src/bridge/abi.rs rename to crates/execution/src/abi.rs index 4c9a4e1..d8127ca 100644 --- a/crates/execution/src/bridge/abi.rs +++ b/crates/execution/src/abi.rs @@ -1,11 +1,12 @@ -//! A Bridge call's arguments, read as rskj reads them. +//! A call's arguments, read as rskj reads them for its own contracts. //! -//! rskj decodes a call's arguments once, in `Bridge.parseData`, through -//! `CallTransaction.Function.decode` and the decoders of -//! `org.ethereum.solidity.SolidityType`; a payload they cannot decode is a -//! parse failure. Each method then converts the `BigInteger`s it gets as Java -//! does. Those rules are not the ABI's, and a transaction can reach every -//! place they differ: +//! rskj decodes the arguments of a call to the Bridge or to a native contract +//! (HDWalletUtils, BlockHeader, Environment) once, in `Bridge.parseData` or +//! `NativeContract.parseData`, through `CallTransaction.Function.decode` and +//! the decoders of `org.ethereum.solidity.SolidityType`; a payload they cannot +//! decode is a parse failure. Each method then converts the `BigInteger`s it +//! gets as Java does. Those rules are not the ABI's, and a transaction can +//! reach every place they differ: //! //! - an offset or a length is a word's `intValue()`: its low 32 bits as a //! signed int, whatever the other 28 bytes hold; @@ -56,6 +57,12 @@ impl Int { self.fits(8).then(|| self.long_value()) } + /// `BigInteger.shortValueExact()`: the number, if a short holds it. + pub(crate) fn short_value_exact(&self) -> Option { + self.fits(2) + .then(|| i16::from_be_bytes([self.0[30], self.0[31]])) + } + /// The number itself. pub(crate) fn signed(&self) -> I256 { I256::from_raw(U256::from_be_bytes(self.0)) @@ -133,8 +140,8 @@ impl<'a> Args<'a> { } /// Whether every parameter of `signature` decodes: `Function.decode` - /// returning rather than throwing. `None` for a parameter type the - /// Bridge does not use. + /// returning rather than throwing. `None` for a parameter type none of + /// rskj's contracts use. pub(crate) fn decode(&self, signature: &str) -> Option { let mut decodes = true; for (i, param) in params(signature).enumerate() { @@ -239,28 +246,32 @@ fn data_word(value: &Int) -> [u8; 32] { padded } -/// `Sha256Hash.wrap(String)`: bitcoinj's `Utils.HEX`, Guava's lower-case -/// base16, decodes the string, and the hash must be 32 bytes. Upper-case -/// digits are refused. -pub(crate) fn sha256_hash_from_hex(hex: &str) -> Option<[u8; 32]> { - let digit = |c: u8| match c { - b'0'..=b'9' => Some(c - b'0'), - b'a'..=b'f' => Some(c - b'a' + 10), - _ => None, - }; - let digits = hex.as_bytes(); - if digits.len() != 64 { - return None; - } - let mut hash = [0u8; 32]; - for (byte, pair) in hash.iter_mut().zip(digits.chunks_exact(2)) { - *byte = (digit(pair[0])? << 4) | digit(pair[1])?; - } - Some(hash) +/// `Function.encodeSignature()`: the first four bytes of the signature's +/// Keccak-256, as a call's payload starts. +pub(crate) fn selector(signature: &str) -> [u8; 4] { + use sha3::{Digest, Keccak256}; + let hash = Keccak256::digest(signature.as_bytes()); + [hash[0], hash[1], hash[2], hash[3]] +} + +/// `Function.encodeOutputs` of a single `bytes` or `string` result: the +/// offset of its data, its length, then the data padded to whole words. +/// +/// `BytesType.encode` pads to `((len - 1) / 32 + 1) * 32` bytes, and Java's +/// `-1 / 32 == 0` gives an empty value a zero word of its own: 96 bytes, not +/// the ABI's 64. The word is consensus, since a caller's `RETURNDATACOPY` +/// pays for it (mainnet #1,669,062). +pub(crate) fn encode_bytes(data: &[u8]) -> Vec { + let padded = data.len().div_ceil(32).max(1) * 32; + let mut out = Vec::with_capacity(64 + padded); + out.extend_from_slice(&U256::from(32u8).to_be_bytes::<32>()); + out.extend_from_slice(&U256::from(data.len()).to_be_bytes::<32>()); + out.extend_from_slice(data); + out.resize(64 + padded, 0); + out } -/// Arguments encoded as rskj's `CallTransaction` encodes them, for the -/// Bridge's tests. +/// Arguments encoded as rskj's `CallTransaction` encodes them, for tests. #[cfg(test)] pub(crate) mod encode { use alloy_primitives::{I256, U256}; @@ -464,15 +475,11 @@ mod tests { assert_eq!(Int(word(1 << 31)).int_value(), i32::MIN); assert_eq!(Int(word((1 << 31) - 1)).int_value_exact(), Some(i32::MAX)); assert_eq!(Int(word(1 << 63)).long_value_exact(), None); - } - - #[test] - fn a_hash_string_is_64_lower_case_hex_digits() { - let lower = "00".repeat(31) + "ab"; - assert_eq!(sha256_hash_from_hex(&lower).map(|h| h[31]), Some(0xAB)); - assert_eq!(sha256_hash_from_hex(&lower.to_uppercase()), None); - assert_eq!(sha256_hash_from_hex(&lower[2..]), None); - assert_eq!(sha256_hash_from_hex(&("0".repeat(61) + "\u{FFFD}")), None); + assert_eq!(Int(word(32_767)).short_value_exact(), Some(i16::MAX)); + assert_eq!(Int(word(32_768)).short_value_exact(), None); + assert_eq!(Int(negative(-32_768)).short_value_exact(), Some(i16::MIN)); + assert_eq!(Int(negative(-32_769)).short_value_exact(), None); + assert_eq!(minus_one.short_value_exact(), Some(-1)); } #[test] @@ -489,4 +496,27 @@ mod tests { assert_eq!(Args::new(&[]).decode("f(bytes32)"), Some(false)); assert_eq!(Args::new(&[]).decode("f(int256[])"), None); } + + /// Both selectors as solc compiled them into rskj's own test contracts + /// (`dsl/nested_environment_calls.txt`). + #[test] + fn a_selector_is_keccak_of_the_signature() { + assert_eq!(selector("getCallStackDepth()"), [0xe8, 0xce, 0x22, 0x74]); + assert_eq!( + selector("callGetCallStackDepth()"), + [0x6e, 0xad, 0x0b, 0x33] + ); + } + + #[test] + fn an_empty_bytes_result_takes_a_zero_word_of_its_own() { + let empty = encode_bytes(&[]); + assert_eq!(empty, payload(&[word(32), word(0), word(0)], &[])); + let mut one = payload(&[word(32), word(1)], &[0xAB]); + one.resize(96, 0); + assert_eq!(encode_bytes(&[0xAB]), one); + assert_eq!(encode_bytes(&[1; 32]).len(), 96); + assert_eq!(encode_bytes(&[1; 33]).len(), 128); + assert_eq!(&encode_bytes(&[1; 33])[32..64], &word(33)); + } } diff --git a/crates/execution/src/bridge/btc_chain.rs b/crates/execution/src/bridge/btc_chain.rs index c1c644d..eaeb080 100644 --- a/crates/execution/src/bridge/btc_chain.rs +++ b/crates/execution/src/bridge/btc_chain.rs @@ -75,7 +75,7 @@ pub fn receive_header( let rskip199 = hardfork_cfg.has_rskip199(block_number); ensure_btc_chain_seeded(ctx, config, use_v2, rskip199); - let header_bytes = super::abi::Args::new(args) + let header_bytes = crate::abi::Args::new(args) .bytes(0) .ok_or_else(|| PrecompileError::other("receiveHeader: invalid ABI encoding"))?; @@ -465,7 +465,7 @@ pub fn receive_headers( use_v2: bool, hardfork_cfg: &RskHardforkConfig, ) -> Result { - let headers = super::abi::Args::new(args) + let headers = crate::abi::Args::new(args) .bytes_array(0) .ok_or_else(|| PrecompileError::other("receiveHeaders: invalid ABI encoding"))?; @@ -668,7 +668,7 @@ pub fn get_block_hash_at_depth( // 32 bits, so 2^32 + 5 is depth 5, and a negative int fails the bound // check below. let depth = i64::from( - super::abi::Args::new(args) + crate::abi::Args::new(args) .int(0) .ok_or_else(|| PrecompileError::other("getBtcBlockchainBlockHashAtDepth: no depth"))? .int_value(), @@ -740,7 +740,7 @@ pub fn get_block_header_by_hash( args: &[u8], gas_cost: u64, ) -> Result { - let hash = super::abi::Args::new(args) + let hash = crate::abi::Args::new(args) .bytes32(0) .ok_or_else(|| PrecompileError::other("getBlockHeaderByHash: no hash"))?; let hash = b256_to_bitcoin_hash(&alloy_primitives::B256::from(hash)); @@ -773,7 +773,7 @@ pub fn get_block_header_by_height( config: &BridgeConstants, hardfork_cfg: &RskHardforkConfig, ) -> Result { - let height = super::abi::Args::new(args) + let height = crate::abi::Args::new(args) .int(0) .ok_or_else(|| PrecompileError::other("getBlockHeaderByHeight: no height"))? .int_value(); @@ -802,7 +802,7 @@ pub fn get_parent_block_header_by_hash( args: &[u8], gas_cost: u64, ) -> Result { - let hash = super::abi::Args::new(args) + let hash = crate::abi::Args::new(args) .bytes32(0) .ok_or_else(|| PrecompileError::other("getParentBlockHeaderByHash: no hash"))?; let hash = b256_to_bitcoin_hash(&alloy_primitives::B256::from(hash)); @@ -818,7 +818,7 @@ pub fn get_parent_block_header_by_hash( } // --------------------------------------------------------------------------- -// ABI encoding helpers (arguments are read through `super::abi`) +// ABI encoding helpers (arguments are read through `crate::abi`) // --------------------------------------------------------------------------- /// ABI-encode a `bytes` return value. @@ -1035,7 +1035,7 @@ mod tests { fn abi_bytes_roundtrip() { let original = vec![1u8, 2, 3, 4, 5]; let encoded = encode_abi_bytes(&original); - let decoded = crate::bridge::abi::Args::new(&encoded).bytes(0).unwrap(); + let decoded = crate::abi::Args::new(&encoded).bytes(0).unwrap(); assert_eq!(decoded, original); } @@ -1043,7 +1043,7 @@ mod tests { fn abi_bytes_80_byte_header() { let data = vec![0xABu8; 80]; let encoded = encode_abi_bytes(&data); - let decoded = crate::bridge::abi::Args::new(&encoded).bytes(0).unwrap(); + let decoded = crate::abi::Args::new(&encoded).bytes(0).unwrap(); assert_eq!(decoded, data); // 80 bytes → offset + length + ceil(80/32)*32 = 32 + 32 + 96 = 160. assert_eq!(encoded.len(), 160); @@ -1064,10 +1064,7 @@ mod tests { // offset 0x20, length 0, one zero word. assert_eq!(encoded[31], 0x20); assert!(encoded[32..].iter().all(|&b| b == 0)); - assert_eq!( - crate::bridge::abi::Args::new(&encoded).bytes(0), - Some(&[][..]) - ); + assert_eq!(crate::abi::Args::new(&encoded).bytes(0), Some(&[][..])); } #[test] @@ -1134,7 +1131,7 @@ mod tests { /// and refuses the whole call for one of the wrong size. #[test] fn receive_headers_refuses_them_all_for_one_of_the_wrong_size() { - use crate::bridge::abi::encode::{self, Arg}; + use crate::abi::encode::{self, Arg}; let args = encode::args(&[Arg::BytesArray(&[&[0u8; 80], &[0u8; 79]])]); let (config, cfg) = (BridgeConstants::mainnet(), RskHardforkConfig::mainnet()); assert!(receive_headers(&mut empty_ctx(), &args, 0, &config, false, &cfg).is_err()); diff --git a/crates/execution/src/bridge/getters.rs b/crates/execution/src/bridge/getters.rs index a30d2cc..00978eb 100644 --- a/crates/execution/src/bridge/getters.rs +++ b/crates/execution/src/bridge/getters.rs @@ -345,7 +345,7 @@ pub fn get_lock_whitelist_entry_by_address( gas_cost: u64, config: &BridgeConstants, ) -> Result { - let Some(addr_str) = super::abi::Args::new(args).string(0) else { + let Some(addr_str) = crate::abi::Args::new(args).string(0) else { return Err(PrecompileError::other( "getLockWhitelistEntryByAddress: malformed string argument", )); @@ -470,7 +470,7 @@ fn member_key(member: &super::federation::StoredMember, key_type: KeyType) -> [u /// throws for anything but "btc", "rsk" and "mst". fn decode_index_and_key_type(args: &[u8]) -> Result<(i32, KeyType), PrecompileError> { let index = index_arg(args, "PublicKeyOfType")?; - let key_type = super::abi::Args::new(args) + let key_type = crate::abi::Args::new(args) .string(1) .as_deref() .and_then(parse_key_type) @@ -737,7 +737,7 @@ pub fn get_btc_tx_hash_processed_height( /// A method's first argument as an index: `((BigInteger) args[0]).intValue()`. fn index_arg(args: &[u8], method: &str) -> Result { - super::abi::Args::new(args) + crate::abi::Args::new(args) .int(0) .map(|index| index.int_value()) .ok_or_else(|| PrecompileError::Other(format!("{method}: no index").into())) @@ -747,13 +747,33 @@ fn index_arg(args: &[u8], method: &str) -> Result { /// `Sha256Hash.wrap((String) args[0])`, whose lower-case-only hex decoding /// and 32-byte check both throw. fn hash_arg(args: &[u8], method: &str) -> Result<[u8; 32], PrecompileError> { - super::abi::Args::new(args) + crate::abi::Args::new(args) .string(0) .as_deref() - .and_then(super::abi::sha256_hash_from_hex) + .and_then(sha256_hash_from_hex) .ok_or_else(|| PrecompileError::Other(format!("{method}: not a 32-byte hash").into())) } +/// `Sha256Hash.wrap(String)`: bitcoinj's `Utils.HEX`, Guava's lower-case +/// base16, decodes the string, and the hash must be 32 bytes. Upper-case +/// digits are refused. +fn sha256_hash_from_hex(hex: &str) -> Option<[u8; 32]> { + let digit = |c: u8| match c { + b'0'..=b'9' => Some(c - b'0'), + b'a'..=b'f' => Some(c - b'a' + 10), + _ => None, + }; + let digits = hex.as_bytes(); + if digits.len() != 64 { + return None; + } + let mut hash = [0u8; 32]; + for (byte, pair) in hash.iter_mut().zip(digits.chunks_exact(2)) { + *byte = (digit(pair[0])? << 4) | digit(pair[1])?; + } + Some(hash) +} + /// The federation creation time as the era reports it: milliseconds before /// RSKIP419, seconds from RSKIP419 on. Storage always holds milliseconds. fn creation_time_for_era( @@ -1582,4 +1602,13 @@ mod tests { assert_eq!(empty.bytes, zero.bytes); assert_eq!(as_bytes(&empty).len(), 33); } + + #[test] + fn a_hash_string_is_64_lower_case_hex_digits() { + let lower = "00".repeat(31) + "ab"; + assert_eq!(sha256_hash_from_hex(&lower).map(|h| h[31]), Some(0xAB)); + assert_eq!(sha256_hash_from_hex(&lower.to_uppercase()), None); + assert_eq!(sha256_hash_from_hex(&lower[2..]), None); + assert_eq!(sha256_hash_from_hex(&("0".repeat(61) + "\u{FFFD}")), None); + } } diff --git a/crates/execution/src/bridge/governance.rs b/crates/execution/src/bridge/governance.rs index a05d334..902f375 100644 --- a/crates/execution/src/bridge/governance.rs +++ b/crates/execution/src/bridge/governance.rs @@ -44,7 +44,7 @@ use super::storage::*; /// content. A payload that reaches a method has already decoded, so this /// only fails for a caller that skipped the Bridge's argument check. fn abi_decode_string_arg(args: &[u8], slot: usize) -> Result { - super::abi::Args::new(args) + crate::abi::Args::new(args) .string(slot) .ok_or_else(|| PrecompileError::other("governance: malformed string argument")) } @@ -67,7 +67,7 @@ fn btc_address_to_hash160( /// with `RLP.encodeBigInteger`, whose `asUnsignedByteArray` writes a negative /// number's shortest two's-complement bytes. What the entry reads back as, /// in rskj and here, is those bytes taken unsigned: -5 becomes 251. -fn one_off_max_transfer_value(max: &super::abi::Int) -> Option { +fn one_off_max_transfer_value(max: &crate::abi::Int) -> Option { let value = max.long_value_exact()?; if value >= 0 { return Some(value as u64); @@ -784,7 +784,7 @@ pub fn vote_fee_per_kb_change( // rskj `Bridge.voteFeePerKbChange` takes `Coin.valueOf(longValueExact())` // before anything else, and a value no long holds is GENERIC_ERROR: -10, // the same code an unauthorized caller gets. - let Some(fee) = super::abi::Args::new(args) + let Some(fee) = crate::abi::Args::new(args) .int(0) .and_then(|fee| fee.long_value_exact()) else { @@ -953,7 +953,7 @@ pub fn increase_locking_cap( // int256 satoshis; rskj getCoinFromBigInteger (longValueExact) and the // <= 0 check both end in BridgeIllegalArgumentException, before the // authorizer is looked at. - let new_cap = super::abi::Args::new(args) + let new_cap = crate::abi::Args::new(args) .int(0) .and_then(|cap| cap.long_value_exact()) .and_then(|cap| u64::try_from(cap).ok()) @@ -1051,7 +1051,7 @@ pub fn add_one_off_lock_whitelist_address( Err(_) => return Ok(encode_int_result(gas_cost, -2)), }; - let max_val = super::abi::Args::new(args) + let max_val = crate::abi::Args::new(args) .int(1) .as_ref() .and_then(one_off_max_transfer_value) @@ -1198,7 +1198,7 @@ pub fn set_lock_whitelist_disable_block_delay( // rskj `disableBlockDelayBI.intValueExact()`: a delay no int holds // throws. The sum is Java int arithmetic, so one that wraps is not above // the height either. - let delay = super::abi::Args::new(args) + let delay = crate::abi::Args::new(args) .int(0) .and_then(|delay| delay.int_value_exact()) .ok_or_else(|| { @@ -1321,7 +1321,7 @@ fn decode_bytes_arg(args: &[u8]) -> Result, PrecompileError> { /// Decode the `index`-th `bytes` ABI argument, as rskj's decoder does. fn decode_bytes_arg_at(args: &[u8], index: usize) -> Result, PrecompileError> { - super::abi::Args::new(args) + crate::abi::Args::new(args) .bytes(index) .map(<[u8]>::to_vec) .ok_or_else(|| PrecompileError::other("bytes arg: malformed")) @@ -1489,7 +1489,7 @@ mod tests { /// throws for one beyond a Java long -- all before the sender is checked. #[test] fn a_one_off_value_beyond_a_long_throws_before_the_sender_check() { - use crate::bridge::abi::encode::{self, Arg}; + use crate::abi::encode::{self, Arg}; use revm::MainContext; let config = super::super::constants::BridgeConstants::mainnet(); let tx_ctx = crate::precompiles::BridgeTxContext::default(); diff --git a/crates/execution/src/bridge/mod.rs b/crates/execution/src/bridge/mod.rs index 7147443..0453296 100644 --- a/crates/execution/src/bridge/mod.rs +++ b/crates/execution/src/bridge/mod.rs @@ -10,7 +10,6 @@ //! This module implements the consensus-critical subset (32 transaction-callable //! methods). Local-call-only getters are deferred to RPC support. -pub(crate) mod abi; pub mod btc_block_cache; #[cfg(test)] mod btc_block_cache_readpath_tests; @@ -68,7 +67,7 @@ pub(crate) fn compute_selector(sig: &str) -> [u8; 4] { // mainnet #6,223,797 tx[3] forwards `receiveHeaders(bytes[])` with a `bytes[]` // offset of 0xa0 into a 60-byte argument payload, so rskj's decode throws. // -// The decoding is `abi::Args`, which every method handler reads its +// The decoding is `crate::abi::Args`, which every method handler reads its // arguments through as well: a payload that passes here is read by each // handler as rskj's `Object[] args` holds it. @@ -77,7 +76,9 @@ pub(crate) fn compute_selector(sig: &str) -> [u8; 4] { /// Returns false where rskj's decode would throw (→ `parseData` null → flat /// 23_000 parse-failure cost). fn abi_args_decode_ok(args: &[u8], signature: &str) -> bool { - abi::Args::new(args).decode(signature).unwrap_or(false) + crate::abi::Args::new(args) + .decode(signature) + .unwrap_or(false) } // --------------------------------------------------------------------------- @@ -741,7 +742,7 @@ fn receive_headers_cost(args: &[u8], hardfork_cfg: &RskHardforkConfig, block_num let base: u64 = if rskip132 { 25_000 } else { 66_000 }; let per_additional_header: u64 = if rskip132 { 3_500 } else { 1_650 }; - match abi::Args::new(args) + match crate::abi::Args::new(args) .bytes_array(0) .map(|headers| headers.len() as u64) { @@ -763,7 +764,7 @@ fn btc_tx_confirmations_cost( const DOUBLE_HASH_COST: u64 = 144; // args: bytes32 btcTxHash, bytes32 btcBlockHash, uint256 path, bytes32[] hashes - let args = abi::Args::new(args); + let args = crate::abi::Args::new(args); let (block_hash, branch_hashes) = match (args.bytes32(1), args.bytes32_array(3)) { (Some(h), Some(hashes)) => (alloy_primitives::B256::from(h), hashes.len() as u64), _ => return BASIC_COST, @@ -1025,7 +1026,9 @@ mod tests { fn the_decoder_reads_every_signature_in_the_method_table() { for method in BRIDGE_METHODS.iter() { assert!( - abi::Args::new(&[]).decode(method.signature).is_some(), + crate::abi::Args::new(&[]) + .decode(method.signature) + .is_some(), "{}", method.signature ); @@ -1357,7 +1360,7 @@ mod tests { // Three headers. rskj counts the decoded array, so a count the // entries do not back is not three headers but a parse failure. - use abi::encode::{self, Arg}; + use crate::abi::encode::{self, Arg}; let header: &[u8] = &[0u8; 80]; let args = encode::args(&[Arg::BytesArray(&[header, header, header])]); diff --git a/crates/execution/src/bridge/peg.rs b/crates/execution/src/bridge/peg.rs index 5a4c00e..5690465 100644 --- a/crates/execution/src/bridge/peg.rs +++ b/crates/execution/src/bridge/peg.rs @@ -240,7 +240,7 @@ pub fn register_btc_transaction( ) -> Result { // rskj `Bridge.registerBtcTransaction`: the transaction, the height as // `intValue()`, the partial merkle tree, all as its decoder reads them. - let decoded = super::abi::Args::new(args); + let decoded = crate::abi::Args::new(args); let (Some(btc_tx_data), Some(height), Some(pmt_data)) = (decoded.bytes(0), decoded.int(1), decoded.bytes(2)) else { @@ -1645,7 +1645,7 @@ pub fn register_fast_bridge_btc_transaction( bool, ); let parse = (|| -> Option { - let args = super::abi::Args::new(args); + let args = crate::abi::Args::new(args); Some(( args.bytes(0)?.to_vec(), args.int(1)?.int_value(), @@ -4369,7 +4369,7 @@ pub fn add_signature( Ok(PrecompileOutput::new(gas_cost, Bytes::new())) } }; - let decoded = super::abi::Args::new(args); + let decoded = crate::abi::Args::new(args); let (Some(fed_key), Some(sigs), Some(rsk_tx_hash)) = (decoded.bytes(0), decoded.bytes_array(1), decoded.bytes(2)) else { @@ -5691,7 +5691,7 @@ pub fn get_estimated_fees_for_pegout_amount( // rskj `new co.rsk.core.Coin((BigInteger) args[0]).toBitcoin()`: the wei // as a signed number, divided by 10^10 toward zero, then `longValue()`, // its low 64 bits. A negative amount is below the minimum like any other. - let wei = super::abi::Args::new(args) + let wei = crate::abi::Args::new(args) .int(0) .ok_or_else(|| PrecompileError::other("getEstimatedFeesForPegOutAmount: no amount"))?; let negative = wei.0[0] & 0x80 != 0; @@ -7827,7 +7827,7 @@ mod tests { ); } - use crate::bridge::abi::encode::{self, Arg}; + use crate::abi::encode::{self, Arg}; /// A tree of one transaction, the one given, matched. Its root is the /// transaction's hash, so it proves any bytes at all. diff --git a/crates/execution/src/bridge/pmt.rs b/crates/execution/src/bridge/pmt.rs index 5809d4b..3b58c70 100644 --- a/crates/execution/src/bridge/pmt.rs +++ b/crates/execution/src/bridge/pmt.rs @@ -93,7 +93,7 @@ impl MerkleBranch { /// and the path as rskj's `BigInteger.intValue` reads it, its low 32 bits, /// all through the decoder the Bridge's argument check uses. pub fn from_abi_args(args: &[u8]) -> Option<(Self, [u8; 32], [u8; 32])> { - let args = super::abi::Args::new(args); + let args = crate::abi::Args::new(args); let tx_hash = args.bytes32(0)?; let block_hash = args.bytes32(1)?; let path = args.int(2)?.int_value(); diff --git a/crates/execution/src/bridge/tx.rs b/crates/execution/src/bridge/tx.rs index cf08a39..34c2f1a 100644 --- a/crates/execution/src/bridge/tx.rs +++ b/crates/execution/src/bridge/tx.rs @@ -177,7 +177,7 @@ pub fn register_btc_coinbase_transaction( // The five arguments, as rskj's decoder reads them: the transaction, the // block hash, the partial merkle tree, the witness merkle root and the // witness reserved value. - let decoded = super::abi::Args::new(args); + let decoded = crate::abi::Args::new(args); let ( Some(btc_tx_data), Some(block_hash), @@ -293,7 +293,7 @@ pub fn has_btc_block_coinbase_info( args: &[u8], gas_cost: u64, ) -> Result { - let block_hash = super::abi::Args::new(args).bytes32(0).ok_or_else(|| { + let block_hash = crate::abi::Args::new(args).bytes32(0).ok_or_else(|| { PrecompileError::other("hasBtcBlockCoinbaseTransactionInformation: no block hash") })?; let has_info = has_coinbase_information(ctx, &block_hash); diff --git a/crates/execution/src/bridge/union.rs b/crates/execution/src/bridge/union.rs index fe29e50..e3dafa2 100644 --- a/crates/execution/src/bridge/union.rs +++ b/crates/execution/src/bridge/union.rs @@ -247,7 +247,7 @@ pub fn set_contract_address_for_testnet( } // rskj `new RskAddress((DataWord) args[0])`: the address as its decoder // reads it, an empty argument list being the zero address. - let Some(new_address) = super::abi::Args::new(args).address(0) else { + let Some(new_address) = crate::abi::Args::new(args).address(0) else { return int_result(gas_cost, UnionResponse::GenericError.code()); }; store_long(ctx, UNION_CONTRACT_ADDRESS_KEY, &rlp_encode_bytes(new_address.as_slice())); @@ -291,7 +291,7 @@ pub fn increase_locking_cap( if sender != constants.change_locking_cap_authorizer { return int_result(gas_cost, UnionResponse::UnauthorizedCaller.code()); } - let Some(new_cap) = super::abi::Args::new(args).int(0) else { + let Some(new_cap) = crate::abi::Args::new(args).int(0) else { return int_result(gas_cost, UnionResponse::GenericError.code()); }; let current = locking_cap(ctx, hardfork_cfg); @@ -327,7 +327,7 @@ pub fn request_rbtc( if !request_enabled(ctx) { return int_result(gas_cost, UnionResponse::TransferDisabled.code()); } - let Some(amount) = super::abi::Args::new(args).int(0) else { + let Some(amount) = crate::abi::Args::new(args).int(0) else { return int_result(gas_cost, UnionResponse::GenericError.code()); }; // rskj `isAmountRequestedValid` refuses an amount that is not positive, @@ -422,7 +422,7 @@ pub fn set_transfer_permissions( } // rskj's `BoolType` looks at its word's low 32 bits only, and an empty // argument list reads as two falses. - let args = super::abi::Args::new(args); + let args = crate::abi::Args::new(args); let (Some(request), Some(release)) = (args.bool(0), args.bool(1)) else { return int_result(gas_cost, UnionResponse::GenericError.code()); }; @@ -469,7 +469,7 @@ pub fn set_event( hardfork_cfg: &RskHardforkConfig, sender: Address, ) -> Result { - let event = super::abi::Args::new(args) + let event = crate::abi::Args::new(args) .bytes(0) .map(<[u8]>::to_vec) .ok_or_else(|| { @@ -521,7 +521,7 @@ fn require_union_bridge_caller( /// rskj `new co.rsk.core.Coin((BigInteger) arg)`: the word as a signed /// number, `None` where it is negative. -fn coin(value: &super::abi::Int) -> Option { +fn coin(value: &crate::abi::Int) -> Option { let value = value.signed(); (!value.is_negative()).then(|| value.into_raw()) } diff --git a/crates/execution/src/executor.rs b/crates/execution/src/executor.rs index 9270c4b..5597c9c 100644 --- a/crates/execution/src/executor.rs +++ b/crates/execution/src/executor.rs @@ -372,6 +372,7 @@ impl RskExecutor { rsk_precompiles(&self.hardfork_cfg, header.number), &self.hardfork_cfg, Some(self.block_store.clone()), + header.parent_hash, self.remasc_config.clone(), ) .with_bridge_config(self.bridge_constants.clone()); @@ -503,6 +504,7 @@ impl RskExecutor { rsk_precompiles(&self.hardfork_cfg, header.number), &self.hardfork_cfg, Some(self.block_store.clone()), + header.parent_hash, self.remasc_config.clone(), ) .with_bridge_config(self.bridge_constants.clone()); @@ -670,6 +672,7 @@ impl RskExecutor { rsk_precompiles(&self.hardfork_cfg, header.number), &self.hardfork_cfg, Some(self.block_store.clone()), + header.parent_hash, self.remasc_config.clone(), ) .with_bridge_config(self.bridge_constants.clone()); @@ -1323,6 +1326,7 @@ impl RskExecutor { rsk_precompiles(&self.hardfork_cfg, header.number), &self.hardfork_cfg, Some(self.block_store.clone()), + header.parent_hash, self.remasc_config.clone(), ) .with_bridge_config(self.bridge_constants.clone()); @@ -7218,7 +7222,11 @@ mod tests { assert_eq!(result.gas_used, 21_064, "Environment costs 0 gas, total = intrinsic only"); } - /// Environment precompile with unknown selector should fail. + /// Environment with a selector that names no method: rskj's + /// `NativeContract.execute` throws, which in a transaction's own call is + /// the invisible exception, a success with no output that used the + /// intrinsic gas and nothing more (`getGasForData` is 0 for a payload that + /// does not parse). #[test] fn test_environment_precompile_unknown_selector() { let store = Arc::new(MemoryTrieStore::new()); @@ -7259,11 +7267,16 @@ mod tests { .execute_tx(&header, &tx, sender, &root, store) .unwrap(); - assert!(!result.success, "unknown selector should fail"); + assert!(result.success, "an invisible exception records success"); + assert!(result.output.is_empty()); + assert_eq!( + result.gas_used, 21_064, + "21,000 and four nonzero bytes at 16" + ); } - /// Environment precompile called via contract STATICCALL should return - /// depth > 1 (one extra level for the contract frame). + /// Environment called by the contract a transaction calls: rskj counts + /// that contract at depth 0, so the answer is 1 (`EnvironmentTest`). #[test] fn test_environment_precompile_via_contract() { let store = Arc::new(MemoryTrieStore::new()); @@ -7403,10 +7416,360 @@ mod tests { call_result.output[31], ]); + assert_eq!( + depth, 1, + "the contract the transaction calls is rskj's depth 0" + ); + } + + /// rskj's `nested_environment_calls.txt`: the contract a transaction calls + /// is at depth 0 and hears 1 from Environment; two calls further down, 3. + #[test] + fn environment_counts_the_depth_of_the_contract_that_asks() { + // STATICCALL Environment with getCallStackDepth(), return its answer. + let leaf_code = [ + 0x63, 0xe8, 0xce, 0x22, 0x74, 0x60, 0x00, 0x52, // mem[28..32] = selector + 0x60, 0x20, 0x60, 0x00, 0x60, 0x04, 0x60, 0x1c, // ret 0..32, args 28..32 + 0x63, 0x01, 0x00, 0x00, 0x11, 0x5a, 0xfa, 0x50, // Environment, GAS, STATICCALL + 0x60, 0x20, 0x60, 0x00, 0xf3, // RETURN mem[0..32] + ]; + // CALL the address in calldata's first word with the rest of the + // calldata, and return the callee's answer. + let relay_code = [ + 0x60, 0x20, 0x36, 0x03, 0x80, // n = CALLDATASIZE - 32 + 0x60, 0x20, 0x60, 0x00, 0x37, // mem[0..n] = calldata[32..] + 0x60, 0x20, 0x60, 0x00, 0x82, 0x60, 0x00, 0x60, + 0x00, // ret 0..32, args 0..n, no value + 0x60, 0x00, 0x35, 0x5a, 0xf1, 0x50, 0x50, // CALL calldata[0..32] + 0x60, 0x20, 0x60, 0x00, 0xf3, // RETURN mem[0..32] + ]; + let leaf = Address::repeat_byte(0xC0); + let relay = Address::repeat_byte(0xB0); + let word = |address: Address| { + let mut word = vec![0u8; 12]; + word.extend_from_slice(address.as_slice()); + word + }; + let depth = |to: Address, calldata: Vec| { + let store = Arc::new(MemoryTrieStore::new()); + let sender = Address::repeat_byte(0xAA); + let one_rbtc = U256::from(10u64).pow(U256::from(18)); + let root = put_account(&TrieNode::empty(), store.as_ref(), &sender, 0, one_rbtc); + let root = + put_account_with_code(&root, store.as_ref(), &leaf, 1, U256::ZERO, &leaf_code); + let root = + put_account_with_code(&root, store.as_ref(), &relay, 1, U256::ZERO, &relay_code); + let tx = rustock_core::Transaction { + nonce: 0, + gas_price: U256::ZERO, + gas_limit: U256::from(1_000_000), + to: Bytes::copy_from_slice(to.as_slice()), + value: U256::ZERO, + input: Bytes::from(calldata), + v: 0, + r: U256::ZERO, + s: U256::ZERO, + cached_rlp: None, + }; + let block_store = + Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let executor = RskExecutor::new(RskHardforkConfig::all_active(33), block_store); + let result = executor + .execute_tx(&dummy_header(8_000_000), &tx, sender, &root, store) + .unwrap(); + assert!(result.success); + U256::from_be_slice(&result.output) + }; + assert_eq!(depth(leaf, Vec::new()), U256::from(1)); + // The transaction calls the relay, which calls itself, which calls the leaf. + let mut path = word(relay); + path.extend(word(leaf)); + assert_eq!(depth(relay, path), U256::from(3)); + } + + /// The intrinsic gas of a transaction carrying `data`, at `nonzero` a + /// nonzero byte: 68, or 16 from RSKIP400. + fn intrinsic_gas(data: &[u8], nonzero: u64) -> u64 { + 21_000 + + data + .iter() + .map(|byte| if *byte == 0 { 4 } else { nonzero }) + .sum::() + } + + /// The extended key of rskj's `ExtractPublicKeyFromExtendedPublicKeyTest`. + const XPUB: &str = "xpub661MyMwAqRbcFMGNG2YcHvj3x63bAZN9U5cKikaiQ4zu2D1cvpnZYyXNR9nH62sGp4RR39Ui7SVQSq1PY4JbPuEuu5prVJJC3d5Pogft712"; + + /// HDWalletUtils' `extractPublicKeyFromExtendedPublicKey(xpub)`, which + /// costs 11,300 and throws for a string that is no extended key. + fn extract_public_key_call(xpub: &[u8]) -> Vec { + use crate::abi::encode::{self, Arg}; + let mut input = + crate::abi::selector("extractPublicKeyFromExtendedPublicKey(string)").to_vec(); + input.extend(encode::args(&[Arg::Bytes(xpub)])); + input + } + + /// One transaction from a funded sender at 60,000,000 wei a gas, in a + /// block of its own. + fn execute_alone( + cfg: RskHardforkConfig, + height: u64, + to: Address, + value: U256, + gas_limit: u64, + input: &[u8], + contracts: &[(Address, &[u8])], + ) -> BlockExecutionResult { + let store = Arc::new(MemoryTrieStore::new()); + let sender = Address::repeat_byte(0xAA); + let one_rbtc = U256::from(10u64).pow(U256::from(18)); + let mut root = put_account(&TrieNode::empty(), store.as_ref(), &sender, 0, one_rbtc); + for (address, code) in contracts { + root = put_account_with_code(&root, store.as_ref(), address, 1, U256::ZERO, code); + } + let tx = rustock_core::Transaction { + nonce: 0, + gas_price: U256::from(60_000_000u64), + gas_limit: U256::from(gas_limit), + to: Bytes::copy_from_slice(to.as_slice()), + value, + input: Bytes::copy_from_slice(input), + v: 0, + r: U256::ZERO, + s: U256::ZERO, + cached_rlp: None, + }; + let block_store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + RskExecutor::new(cfg, block_store) + .execute_block(&dummy_header(height), &[(tx, sender)], &root, store) + .expect("block executes") + } + + fn fee(gas: u64) -> U256 { + U256::from(gas) * U256::from(60_000_000u64) + } + + /// rskj's invisible exception (`TransactionExecutor.call`) for a native + /// contract: a transaction whose own call to HDWalletUtils throws succeeds + /// with no output, having used its intrinsic gas and the method's 11,300. + /// Its sender pays for the whole limit, which REMASC receives, and keeps + /// the value, and the precompile's account is left as it was. The same + /// call with a key it can read is a plain success. + #[test] + fn a_native_contract_that_throws_in_a_transactions_own_call_is_an_invisible_exception() { + let hd_wallet = crate::precompiles::HD_WALLET_UTILS_ADDR; + let sender = Address::repeat_byte(0xAA); + let one_rbtc = U256::from(10u64).pow(U256::from(18)); + let value = U256::from(7); + + let input = extract_public_key_call(b"this-is-not-an-xpub"); + let result = execute_alone( + RskHardforkConfig::all_active(33), + 8_000_000, + hd_wallet, + value, + 1_000_000, + &input, + &[], + ); + let receipt = &result.tx_results[0]; + assert!(receipt.success, "the receipt records success"); + assert!(receipt.output.is_empty()); + assert_eq!(receipt.gas_used, intrinsic_gas(&input, 16) + 11_300); + assert_eq!( + result.state_changes.get(&sender).unwrap().info.balance, + one_rbtc - fee(1_000_000), + "the sender pays for the whole limit and keeps the value" + ); + assert_eq!( + result + .state_changes + .get(&crate::precompiles::REMASC_ADDR) + .unwrap() + .info + .balance, + fee(1_000_000) + ); + assert_eq!(result.paid_fees, fee(1_000_000)); assert!( - depth >= 2, - "depth via contract call should be >= 2 (tx frame + contract frame), got {}", - depth + result + .state_changes + .get(&hd_wallet) + .is_none_or(|account| !account.is_touched()), + "the precompile's account is left as it was" + ); + assert!( + !result.markers.precompiles.contains(&hd_wallet), + "no setupContract" + ); + + let input = extract_public_key_call(XPUB.as_bytes()); + let result = execute_alone( + RskHardforkConfig::all_active(33), + 8_000_000, + hd_wallet, + value, + 1_000_000, + &input, + &[], + ); + let receipt = &result.tx_results[0]; + assert!(receipt.success); + let used = intrinsic_gas(&input, 16) + 11_300; + assert_eq!(receipt.gas_used, used); + assert_eq!( + result.paid_fees, + fee(used), + "a success pays for the gas it used" + ); + assert_eq!( + result.state_changes.get(&hd_wallet).unwrap().info.balance, + value + ); + assert!(result.markers.precompiles.contains(&hd_wallet)); + } + + /// A transaction's own call that cannot pay what a native contract asks + /// for fails, using all its gas (`TransactionExecutor.call`'s execError). + #[test] + fn a_direct_call_short_of_a_native_contracts_gas_fails_with_all_of_it() { + let hd_wallet = crate::precompiles::HD_WALLET_UTILS_ADDR; + let input = extract_public_key_call(XPUB.as_bytes()); + let enough = intrinsic_gas(&input, 16) + 11_300; + let result = execute_alone( + RskHardforkConfig::all_active(33), + 8_000_000, + hd_wallet, + U256::ZERO, + enough - 1, + &input, + &[], + ); + assert!(!result.tx_results[0].success); + assert_eq!(result.tx_results[0].gas_used, enough - 1); + assert_eq!(result.paid_fees, fee(enough - 1)); + let result = execute_alone( + RskHardforkConfig::all_active(33), + 8_000_000, + hd_wallet, + U256::ZERO, + enough, + &input, + &[], + ); + assert!(result.tx_results[0].success); + assert_eq!(result.tx_results[0].gas_used, enough); + } + + /// BN128 refusing a point (`BN128PrecompiledContract`): before RSKIP197 it + /// answers with no bytes, a plain success; from RSKIP197 it throws, which + /// in a transaction's own call is the invisible exception. + #[test] + fn bn128_refusing_a_point_answers_empty_before_rskip197_and_throws_from_it() { + let bn128_add = Address::with_last_byte(6); + // (1, 1) is not on the curve. + let mut off_curve = vec![0u8; 128]; + off_curve[31] = 1; + off_curve[63] = 1; + let used = intrinsic_gas(&off_curve, 68) + 150; + + let before = execute_alone( + RskHardforkConfig::mainnet(), + 3_000_000, + bn128_add, + U256::ZERO, + 1_000_000, + &off_curve, + &[], + ); + assert!(before.tx_results[0].success); + assert_eq!(before.tx_results[0].gas_used, used); + assert_eq!( + before.paid_fees, + fee(used), + "a success pays for the gas it used" + ); + + let from = execute_alone( + RskHardforkConfig::mainnet(), + 3_614_801, + bn128_add, + U256::ZERO, + 1_000_000, + &off_curve, + &[], + ); + assert!(from.tx_results[0].success); + assert_eq!(from.tx_results[0].gas_used, used); + assert_eq!( + from.paid_fees, + fee(1_000_000), + "an invisible exception pays for the whole limit" + ); + } + + /// A contract's call to HDWalletUtils that throws + /// (`Program.callToPrecompiledAddress`): from RSKIP197 the call fails and + /// costs what the method asked for, and the contract goes on; before it the + /// throw takes all the gas the call was given (#295). + #[test] + fn a_native_contract_that_throws_inside_a_call_fails_the_call() { + let hd_wallet = crate::precompiles::HD_WALLET_UTILS_ADDR; + let caller = Address::repeat_byte(0xBB); + // CALL HDWalletUtils with this transaction's calldata and all the gas, + // and return what the CALL pushed. + let mut code: Vec = vec![ + 0x36, 0x60, 0x00, 0x60, 0x00, 0x37, // mem[0..n] = calldata + 0x60, 0x00, 0x60, 0x00, 0x36, 0x60, 0x00, 0x60, + 0x00, // no return, args 0..n, no value + 0x73, // PUSH20 + ]; + code.extend_from_slice(hd_wallet.as_slice()); + code.extend_from_slice(&[ + 0x5a, 0xf1, // GAS, CALL + 0x60, 0x00, 0x52, 0x60, 0x20, 0x60, 0x00, 0xf3, // RETURN the pushed word + ]); + let contracts: &[(Address, &[u8])] = &[(caller, &code)]; + let call = |height: u64, input: &[u8]| { + let result = execute_alone( + RskHardforkConfig::mainnet(), + height, + caller, + U256::ZERO, + 1_000_000, + input, + contracts, + ); + result.tx_results.into_iter().next().unwrap() + }; + let bad = extract_public_key_call(b"this-is-not-an-xpub"); + + let from = call(3_614_801, &bad); + assert!(from.success, "the contract goes on"); + assert_eq!( + U256::from_be_slice(&from.output), + U256::ZERO, + "the CALL pushes 0" + ); + assert!( + from.gas_used < 100_000, + "from RSKIP197 the call costs 11,300 and its overhead, not the limit (got {})", + from.gas_used + ); + + let fine = call(3_614_801, &extract_public_key_call(XPUB.as_bytes())); + assert_eq!( + U256::from_be_slice(&fine.output), + U256::from(1), + "the CALL pushes 1" + ); + + let before = call(2_000_000, &bad); + assert!( + before.gas_used > 900_000, + "before RSKIP197 the throw takes the gas it was given (got {})", + before.gas_used ); } @@ -7528,6 +7891,7 @@ mod tests { let parent_number = current_number - 1; let mut parent_header = dummy_header(parent_number); parent_header.difficulty = U256::from(1); + parent_header.uncle_count = 2; let parent_hash = parent_header.hash(); block_store.put_header(&parent_header).unwrap(); block_store.put_canonical_hash(parent_number, parent_hash).unwrap(); @@ -7585,12 +7949,20 @@ mod tests { } /// Ported from rskj BlockHeaderContractTest.*_whenMethodDisabled_shouldThrowVME: - /// both methods fail before RSKIP536 activates (mainnet vetiver900). + /// before RSKIP536 (mainnet vetiver900) neither method parses, so the + /// contract throws, at no cost. In a transaction's own call that is rskj's + /// invisible exception: a success with no output and only the intrinsic gas. #[test] fn test_rskip536_methods_disabled_before_activation() { for sig in ["getDifficultyWithUncles(int256)", "getCumulativeWork(int256)"] { let result = rskip536_call(sig, RskHardforkConfig::mainnet(), 8_804_199); - assert!(!result.success, "{sig} must fail before vetiver900"); + assert!(result.success, "{sig}: the throw is invisible"); + assert!(result.output.is_empty(), "{sig}"); + assert_eq!( + result.gas_used, + intrinsic_gas(&bh_input(sig, 0), 16), + "{sig}" + ); } } @@ -7905,7 +8277,7 @@ mod tests { /// worked out, or its arguments read, before anything checks the sender. #[test] fn bridge_offsets_are_read_from_their_low_32_bits() { - use crate::bridge::abi::encode::{self, Arg}; + use crate::abi::encode::{self, Arg}; let run = |input: &[u8]| { let store = Arc::new(MemoryTrieStore::new()); let sender = Address::repeat_byte(0xAA); @@ -8415,9 +8787,11 @@ bf09f6e52420834e8e0e0b1a6df563aba550cf7f99e9724264187c45dcf3d73e8585a0e35196\ assert!(decoded.is_empty(), "should return empty bytes for depth >= 4000"); } - /// Negative depth should fail (rskj throws VMException). + /// A negative depth throws (rskj `BlockAccessor.getBlock`) after the method + /// is charged 4,000 and two a byte; in a transaction's own call that is + /// the invisible exception. #[test] - fn test_block_header_negative_depth_fails() { + fn test_block_header_negative_depth_throws() { let store = Arc::new(MemoryTrieStore::new()); let root = TrieNode::empty(); @@ -8438,17 +8812,21 @@ bf09f6e52420834e8e0e0b1a6df563aba550cf7f99e9724264187c45dcf3d73e8585a0e35196\ gas_limit: U256::from(100_000), to: Bytes::copy_from_slice(crate::precompiles::BLOCK_HEADER_ADDR.as_slice()), value: U256::ZERO, - input: Bytes::from(input), - v: 0, r: U256::ZERO, s: U256::ZERO, cached_rlp: None, + input: Bytes::from(input.clone()), + v: 0, + r: U256::ZERO, + s: U256::ZERO, + cached_rlp: None, }; - let executor = RskExecutor::new( - RskHardforkConfig::all_active(33), - block_store, - ); + let executor = RskExecutor::new(RskHardforkConfig::all_active(33), block_store); - let result = executor.execute_tx(&header, &tx, sender, &root, store).unwrap(); - assert!(!result.success, "negative depth should fail"); + let result = executor + .execute_tx(&header, &tx, sender, &root, store) + .unwrap(); + assert!(result.success, "an invisible exception records success"); + assert!(result.output.is_empty()); + assert_eq!(result.gas_used, intrinsic_gas(&input, 16) + 4_000 + 2 * 36); } /// Block not found at depth returns empty bytes. @@ -8705,7 +9083,8 @@ bf09f6e52420834e8e0e0b1a6df563aba550cf7f99e9724264187c45dcf3d73e8585a0e35196\ let mut uncle1 = dummy_header(98); uncle1.beneficiary = uncle1_coinbase; - let parent = dummy_header(99); + let mut parent = dummy_header(99); + parent.uncle_count = 2; let parent_hash = parent.hash(); block_store.put_header(&parent).unwrap(); block_store.put_canonical_hash(99, parent_hash).unwrap(); @@ -8780,7 +9159,8 @@ bf09f6e52420834e8e0e0b1a6df563aba550cf7f99e9724264187c45dcf3d73e8585a0e35196\ } /// Ported from rskj BlockHeaderContractTest.negativeUncleIndex: - /// Negative uncle index should fail. + /// a negative uncle index throws, which in a transaction's own call is + /// the invisible exception. #[test] fn test_rskj_block_header_negative_uncle_index() { let store = Arc::new(MemoryTrieStore::new()); @@ -8815,22 +9195,27 @@ bf09f6e52420834e8e0e0b1a6df563aba550cf7f99e9724264187c45dcf3d73e8585a0e35196\ gas_limit: U256::from(100_000), to: Bytes::copy_from_slice(crate::precompiles::BLOCK_HEADER_ADDR.as_slice()), value: U256::ZERO, - input: Bytes::from(input), - v: 0, r: U256::ZERO, s: U256::ZERO, cached_rlp: None, + input: Bytes::from(input.clone()), + v: 0, + r: U256::ZERO, + s: U256::ZERO, + cached_rlp: None, }; - let executor = RskExecutor::new( - RskHardforkConfig::all_active(33), - block_store, - ); + let executor = RskExecutor::new(RskHardforkConfig::all_active(33), block_store); - let result = executor.execute_tx(&header, &tx, sender, &root, store).unwrap(); - assert!(!result.success, "negative uncle index should fail"); + let result = executor + .execute_tx(&header, &tx, sender, &root, store) + .unwrap(); + assert!(result.success, "an invisible exception records success"); + assert!(result.output.is_empty()); + assert_eq!(result.gas_used, intrinsic_gas(&input, 16) + 4_000 + 2 * 68); } /// Ported from rskj BlockHeaderContractTest.getEmptyMergedMiningTags: - /// When parent has no bitcoin_merged_mining_coinbase_transaction, - /// getMergedMiningTags returns empty bytes. + /// when the parent's merged-mining coinbase has no RSK tag, + /// getMergedMiningTags returns empty bytes. (A block with no coinbase + /// at all throws, as rskj's does.) #[test] fn test_rskj_block_header_empty_merged_mining_tags() { let store = Arc::new(MemoryTrieStore::new()); @@ -8844,9 +9229,12 @@ bf09f6e52420834e8e0e0b1a6df563aba550cf7f99e9724264187c45dcf3d73e8585a0e35196\ BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap(), ); - // Parent with no merged mining data at all - let parent = dummy_header(99); - assert!(parent.bitcoin_merged_mining_coinbase_transaction.is_none()); + // Merged-mining fields whose coinbase has no RSKBLOCK: tag, as rskj's + // test chain has. A header holds the three together or none of them. + let mut parent = dummy_header(99); + parent.bitcoin_merged_mining_header = Some(Bytes::from(vec![0; 80])); + parent.bitcoin_merged_mining_merkle_proof = Some(Bytes::from(vec![0; 32])); + parent.bitcoin_merged_mining_coinbase_transaction = Some(Bytes::from(vec![0xAB; 64])); let parent_hash = parent.hash(); block_store.put_header(&parent).unwrap(); block_store.put_canonical_hash(99, parent_hash).unwrap(); @@ -8874,7 +9262,8 @@ bf09f6e52420834e8e0e0b1a6df563aba550cf7f99e9724264187c45dcf3d73e8585a0e35196\ assert!(result.success, "empty merged mining should succeed"); let decoded = decode_abi_bytes(&result.output); - assert!(decoded.is_empty(), "no coinbase tx → empty merged mining tags"); + assert_eq!(result.output.len(), 96, "empty bytes, encoded"); + assert!(decoded.is_empty(), "no RSK tag → empty merged mining tags"); } /// Ported from rskj BlockHeaderContractTest.invalidBlockDepth: diff --git a/crates/execution/src/lib.rs b/crates/execution/src/lib.rs index c5ad843..2c44adb 100644 --- a/crates/execution/src/lib.rs +++ b/crates/execution/src/lib.rs @@ -1,3 +1,4 @@ +pub(crate) mod abi; pub mod breakdown; pub mod bridge; pub mod collision; @@ -7,6 +8,7 @@ pub mod executor; pub mod gas_estimator; pub mod hardfork; pub mod mining; +pub(crate) mod native; pub mod precompiles; pub mod processor; pub mod raw_storage; diff --git a/crates/execution/src/native/block_header.rs b/crates/execution/src/native/block_header.rs new file mode 100644 index 0000000..3ee25ff --- /dev/null +++ b/crates/execution/src/native/block_header.rs @@ -0,0 +1,693 @@ +//! BlockHeader (0x01000010, RSKIP119): rskj's `co.rsk.pcc.blockheader`, which +//! answers questions about the executing block's ancestors. + +use alloy_primitives::{Bytes, B256, U256}; +use rustock_core::Header; +use rustock_storage::BlockStore; + +use super::{Failure, NativeContract}; +use crate::abi::{encode_bytes, Args, Int}; + +/// `BlockHeaderContract.MAX_DEPTH`, REMASC's maturity: nothing this deep or +/// deeper is answered for. +const MAX_DEPTH: i16 = 4000; + +/// The tag a merged-mining coinbase carries before the RSK block's hash. +const RSK_TAG: &[u8] = b"RSKBLOCK:"; + +/// The contract, reading the chain the executing block extends. +pub(crate) struct BlockHeader<'a> { + pub(crate) blocks: &'a BlockStore, + /// The executing block's parent, where rskj starts looking + /// (`getBlockAtDepthStartingAt`). + pub(crate) parent: B256, + /// RSKIP536, which adds `getDifficultyWithUncles` and `getCumulativeWork`. + pub(crate) rskip536: bool, +} + +/// The contract's methods, each taking a block depth and returning `bytes`. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) enum Method { + CoinbaseAddress, + BlockHash, + MergedMiningTags, + MinGasPrice, + GasLimit, + GasUsed, + Difficulty, + BitcoinHeader, + /// The only one with a second argument, the uncle's index. + UncleCoinbaseAddress, + DifficultyWithUncles, + CumulativeWork, +} + +/// `BlockHeaderContract.getMethods`, by signature. +const METHODS: [(&str, Method); 11] = [ + ("getCoinbaseAddress(int256)", Method::CoinbaseAddress), + ("getBlockHash(int256)", Method::BlockHash), + ("getMergedMiningTags(int256)", Method::MergedMiningTags), + ("getMinGasPrice(int256)", Method::MinGasPrice), + ("getGasLimit(int256)", Method::GasLimit), + ("getGasUsed(int256)", Method::GasUsed), + ("getDifficulty(int256)", Method::Difficulty), + ("getBitcoinHeader(int256)", Method::BitcoinHeader), + ( + "getUncleCoinbaseAddress(int256,int256)", + Method::UncleCoinbaseAddress, + ), + ( + "getDifficultyWithUncles(int256)", + Method::DifficultyWithUncles, + ), + ("getCumulativeWork(int256)", Method::CumulativeWork), +]; + +/// A method with its arguments. +pub(crate) struct Call { + method: Method, + depth: Int, + /// `getUncleCoinbaseAddress`'s index. + uncle: Option, +} + +impl NativeContract for BlockHeader<'_> { + type Call = Call; + + fn parse(&self, selector: [u8; 4], args: &[u8]) -> Option { + let method = super::method(&METHODS, selector)?; + // `isEnabled`: before RSKIP536 its two methods are not there. + if matches!( + method, + Method::DifficultyWithUncles | Method::CumulativeWork + ) && !self.rskip536 + { + return None; + } + let args = Args::new(args); + let depth = args.int(0)?; + let uncle = match method { + Method::UncleCoinbaseAddress => Some(args.int(1)?), + _ => None, + }; + Some(Call { + method, + depth, + uncle, + }) + } + + /// `BlockHeaderContractMethod.getGas`: 4000, and two for each byte of the + /// payload. + fn gas(&self, _call: &Call, input: &[u8]) -> u64 { + (input.len() as u64).saturating_mul(2).saturating_add(4_000) + } + + fn execute(&self, call: Call) -> Result, Failure> { + // `BlockHeaderContractMethod.execute`: a depth no short holds names no + // block, and neither does one `getBlock` finds nothing at. + let block = match call.depth.short_value_exact() { + Some(depth) => self.block_at(depth)?, + None => None, + }; + let Some((hash, header)) = block else { + return Ok(encode_bytes(&[])); + }; + Ok(match self.read(&call, hash, &header)? { + Some(value) => encode_bytes(&value), + // A method's `null`: `NativeContract.execute` returns no bytes at all. + None => Vec::new(), + }) + } +} + +impl BlockHeader<'_> { + /// `BlockAccessor.getBlock`: the block `depth` above the executing block's + /// parent, found as `IndexedBlockStore.getBlockAtDepthStartingAt` finds + /// it. That walks up from the parent by hash until it reaches a block of + /// the main chain, whose index then names the rest, so a block on a fork + /// reads its own ancestors. + fn block_at(&self, depth: i16) -> Result, Failure> { + if depth < 0 { + return Err(Failure::threw("a block depth cannot be negative")); + } + if depth >= MAX_DEPTH { + return Ok(None); + } + let depth = u64::from(depth.unsigned_abs()); + // The genesis block's parent is not there. + let Some(start) = self.header(self.parent)? else { + return Ok(None); + }; + if depth == 0 { + return Ok(Some((self.parent, start))); + } + // Past depth 0, a parent numbered `depth` or lower has no such + // ancestor, not even when it would be the genesis block. + if start.number <= depth { + return Ok(None); + } + let target = start.number - depth; + let (mut hash, mut block) = (self.parent, start); + for _ in 0..depth { + if self.canonical(block.number)? == Some(hash) { + let hash = self + .canonical(target)? + .ok_or_else(|| missing(format!("main-chain block #{target}")))?; + let header = self + .header(hash)? + .ok_or_else(|| missing(format!("header for block #{target}")))?; + return Ok(Some((hash, header))); + } + let child = block.number; + hash = block.parent_hash; + block = self + .header(hash)? + .ok_or_else(|| missing(format!("parent for block #{child}")))?; + } + Ok(Some((hash, block))) + } + + fn header(&self, hash: B256) -> Result, Failure> { + self.blocks.header(hash).map_err(fatal) + } + + /// The main chain's block at `number`. + fn canonical(&self, number: u64) -> Result, Failure> { + self.blocks.canonical_hash(number).map_err(fatal) + } + + /// The block's uncles: none if its header says so, else its body's, or, + /// once the body is pruned, the freezer's, which keeps them by number + /// for the main chain only. + fn uncles(&self, hash: B256, header: &Header) -> Result, Failure> { + if header.uncle_count == 0 { + return Ok(Vec::new()); + } + if let Some(uncles) = self.blocks.uncles(hash).map_err(fatal)? { + return Ok(uncles); + } + if self.canonical(header.number)? == Some(hash) { + if let Some(freezer) = self.blocks.freezer() { + if let Some(uncles) = freezer.uncles(header.number).map_err(fatal)? { + return Ok(uncles); + } + } + } + Err(missing(format!("uncles for block #{}", header.number))) + } + + /// The method's `internalExecute` on the block it names. `None` is rskj's + /// `null`. + fn read(&self, call: &Call, hash: B256, header: &Header) -> Result>, Failure> { + let value = match call.method { + Method::CoinbaseAddress => header.beneficiary.to_vec(), + Method::BlockHash => hash.to_vec(), + Method::MergedMiningTags => merged_mining_tags(header)?, + Method::MinGasPrice => big_integer_bytes(header.minimum_gas_price), + Method::GasLimit => rlp_bytes(header.gas_limit), + Method::GasUsed => big_integer_bytes(U256::from(header.gas_used)), + Method::Difficulty => big_integer_bytes(header.difficulty), + Method::BitcoinHeader => { + // A header field rskj read from an empty RLP item is null too. + return Ok(non_empty(&header.bitcoin_merged_mining_header).map(<[u8]>::to_vec)); + } + Method::UncleCoinbaseAddress => { + // The index is read first: one no short holds names no uncle, + // and a negative one throws. + let Some(index) = call.uncle.and_then(|index| index.short_value_exact()) else { + return Ok(Some(Vec::new())); + }; + let index = usize::try_from(index) + .map_err(|_| Failure::threw("an uncle index cannot be negative"))?; + self.uncles(hash, header)? + .get(index) + .map(|uncle| uncle.beneficiary.to_vec()) + .unwrap_or_default() + } + // `Block.getCumulativeDifficulty`. + Method::DifficultyWithUncles => big_integer_bytes( + self.uncles(hash, header)? + .iter() + .fold(header.difficulty, |sum, uncle| { + sum.saturating_add(uncle.difficulty) + }), + ), + // `getTotalDifficultyForHash`, which rskj holds for every block. + Method::CumulativeWork => big_integer_bytes( + self.blocks + .total_difficulty(hash) + .map_err(fatal)? + .ok_or_else(|| { + missing(format!("total difficulty for block #{}", header.number)) + })?, + ), + }; + Ok(Some(value)) + } +} + +/// A store that fails to read leaves the node with no answer. +fn fatal(error: impl std::fmt::Display) -> Failure { + Failure::Fatal(format!("BlockHeader: {error}")) +} + +/// rskj's store holds every block, body and total difficulty it is asked +/// for, so one this node lacks is no answer, not an empty one. +fn missing(what: String) -> Failure { + Failure::Fatal(format!("BlockHeader: the store has no {what}")) +} + +/// `GetMergedMiningTags.internalExecute`: what follows the block's hash +/// after the merged-mining coinbase's last `RSKBLOCK:` tag. +fn merged_mining_tags(header: &Header) -> Result, Failure> { + // rskj reads a block without a coinbase into a NullPointerException. + let coinbase = non_empty(&header.bitcoin_merged_mining_coinbase_transaction) + .ok_or_else(|| Failure::threw("the block has no merged-mining coinbase"))?; + let Some(tag) = coinbase + .windows(RSK_TAG.len()) + .rposition(|window| window == RSK_TAG) + else { + return Ok(Vec::new()); + }; + // `Arrays.copyOfRange` throws when the hash runs past the end. + coinbase + .get(tag + RSK_TAG.len() + 32..) + .map(<[u8]>::to_vec) + .ok_or_else(|| Failure::threw("the coinbase ends inside the block's hash")) +} + +/// A merged-mining field as rskj holds it: `RLPItem.getRLPData` reads an +/// empty item as null, so an empty field is no field. +fn non_empty(field: &Option) -> Option<&[u8]> { + field + .as_ref() + .map(|bytes| &bytes[..]) + .filter(|bytes| !bytes.is_empty()) +} + +/// `BigInteger.toByteArray()` of a non-negative number: its shortest +/// big-endian form, with a leading zero byte when the first one's top bit +/// is set, and `[0]` for zero. +fn big_integer_bytes(value: U256) -> Vec { + let mut bytes = rlp_bytes(value); + if bytes.first().is_none_or(|first| first & 0x80 != 0) { + bytes.insert(0, 0); + } + bytes +} + +/// A header field as its RLP holds it, which `GetGasLimit` returns as it is: +/// the shortest big-endian form, empty for zero. +fn rlp_bytes(value: U256) -> Vec { + let bytes = value.to_be_bytes::<32>(); + let start = bytes.iter().position(|b| *b != 0).unwrap_or(32); + bytes[start..].to_vec() +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::native::{call, Outcome}; + use alloy_primitives::{Address, Bloom}; + + fn header(number: u64, parent_hash: B256) -> Header { + Header { + parent_hash, + ommers_hash: B256::ZERO, + beneficiary: Address::with_last_byte(number as u8), + state_root: B256::ZERO, + transactions_root: B256::ZERO, + receipts_root: B256::ZERO, + logs_bloom: Bloom::ZERO, + extension_data: None, + difficulty: U256::from(1), + number, + gas_limit: U256::from(6_800_000), + gas_used: 0, + timestamp: number, + extra_data: Bytes::new(), + paid_fees: U256::ZERO, + minimum_gas_price: U256::ZERO, + uncle_count: 0, + umm_root: None, + version_fields: None, + bitcoin_merged_mining_header: Some(Bytes::from(vec![number as u8; 80])), + bitcoin_merged_mining_merkle_proof: None, + bitcoin_merged_mining_coinbase_transaction: None, + cached_hash: None, + cached_hash_for_merged_mining: None, + } + } + + /// A chain of blocks 0 to `top`, each canonical and block `n`'s coinbase + /// ending in `n`, as `edit` leaves them; and their hashes. + fn chain_with( + top: u64, + edit: impl Fn(&mut Header), + ) -> (tempfile::TempDir, BlockStore, Vec) { + let dir = tempfile::tempdir().unwrap(); + let blocks = BlockStore::open(dir.path()).unwrap(); + let mut hashes = Vec::new(); + for number in 0..=top { + let mut block = header(number, hashes.last().copied().unwrap_or(B256::ZERO)); + edit(&mut block); + blocks.put_header(&block).unwrap(); + blocks.put_canonical_hash(number, block.hash()).unwrap(); + hashes.push(block.hash()); + } + (dir, blocks, hashes) + } + + fn chain(top: u64) -> (tempfile::TempDir, BlockStore, Vec) { + chain_with(top, |_| {}) + } + + /// The contract as block `number` of the chain sees it. + fn at<'a>(blocks: &'a BlockStore, hashes: &[B256], number: usize) -> BlockHeader<'a> { + let parent = number + .checked_sub(1) + .map_or(B256::ZERO, |parent| hashes[parent]); + BlockHeader { + blocks, + parent, + rskip536: false, + } + } + + fn input(signature: &str, args: &[[u8; 32]]) -> Vec { + let mut input = crate::abi::selector(signature).to_vec(); + args.iter().for_each(|word| input.extend_from_slice(word)); + input + } + + fn int(n: i64) -> [u8; 32] { + crate::abi::encode::int(n) + } + + /// `getCoinbaseAddress(depth)`: the coinbase's last byte, which is the + /// block's number, or `None` for an empty answer. + fn coinbase(contract: &BlockHeader, depth: [u8; 32]) -> Option { + match call( + contract, + &input("getCoinbaseAddress(int256)", &[depth]), + 1_000_000, + ) { + Outcome::Returned { output, .. } if output[..] == encode_bytes(&[])[..] => None, + Outcome::Returned { output, .. } => Some(output[64 + 19]), + other => panic!("unexpected {other:?}"), + } + } + + #[test] + fn depth_zero_is_the_parent() { + let (_dir, blocks, hashes) = chain(10); + let contract = at(&blocks, &hashes, 11); + assert_eq!(coinbase(&contract, int(0)), Some(10)); + assert_eq!(coinbase(&contract, int(3)), Some(7)); + } + + /// `getBlockAtDepthStartingAt`: past depth 0, a parent numbered `depth` + /// has no such ancestor, so the genesis block is reachable only as some + /// block's parent. + #[test] + fn past_depth_zero_the_genesis_block_is_out_of_reach() { + let (_dir, blocks, hashes) = chain(10); + assert_eq!(coinbase(&at(&blocks, &hashes, 6), int(4)), Some(1)); + assert_eq!(coinbase(&at(&blocks, &hashes, 6), int(5)), None); + assert_eq!(coinbase(&at(&blocks, &hashes, 1), int(0)), Some(0)); + assert_eq!(coinbase(&at(&blocks, &hashes, 0), int(0)), None); + } + + /// A block on a fork reads its own ancestors, by hash, until its branch + /// meets the main chain. + #[test] + fn a_block_on_a_fork_reads_its_own_ancestors() { + let (_dir, blocks, hashes) = chain(10); + // Blocks 6' and 7' branch off block 5 and are not canonical. + let mut parent = hashes[5]; + for number in 6..=7 { + let mut block = header(number, parent); + block.beneficiary = Address::with_last_byte(0xF0 + number as u8); + blocks.put_header(&block).unwrap(); + parent = block.hash(); + } + // Block 8' executes on top of 7'. + let contract = BlockHeader { + blocks: &blocks, + parent, + rskip536: false, + }; + assert_eq!(coinbase(&contract, int(0)), Some(0xF7)); + assert_eq!(coinbase(&contract, int(1)), Some(0xF6)); + assert_eq!(coinbase(&contract, int(2)), Some(5)); + assert_eq!(coinbase(&contract, int(6)), Some(1)); + assert_eq!(coinbase(&contract, int(7)), None); + } + + #[test] + fn a_depth_no_short_holds_is_an_empty_answer_and_a_negative_one_throws() { + let (_dir, blocks, hashes) = chain(10); + let contract = at(&blocks, &hashes, 11); + assert_eq!(coinbase(&contract, int(4000)), None); + assert_eq!(coinbase(&contract, int(1 << 16)), None); + assert_eq!(coinbase(&contract, int(-40_000)), None, "no short holds it"); + let negative = input("getCoinbaseAddress(int256)", &[int(-1)]); + assert_eq!( + call(&contract, &negative, 1_000_000), + Outcome::Threw { + gas: 4_072, + reason: "a block depth cannot be negative".into() + } + ); + } + + /// rskj's decoder reads an empty payload as zero but refuses a short one. + #[test] + fn no_arguments_is_depth_zero_but_a_short_argument_is_no_call() { + let (_dir, blocks, hashes) = chain(10); + let contract = at(&blocks, &hashes, 11); + let bare = crate::abi::selector("getCoinbaseAddress(int256)"); + assert!(matches!( + call(&contract, &bare, 1_000_000), + Outcome::Returned { gas: 4_008, .. } + )); + let mut short = bare.to_vec(); + short.extend_from_slice(&[0; 31]); + assert!(matches!( + call(&contract, &short, 1_000_000), + Outcome::Threw { gas: 0, .. } + )); + } + + #[test] + fn the_gas_is_4000_and_two_a_byte() { + let (_dir, blocks, hashes) = chain(10); + let contract = at(&blocks, &hashes, 11); + let get = input("getBlockHash(int256)", &[int(0)]); + assert_eq!(call(&contract, &get, 4_071), Outcome::OutOfGas); + assert!(matches!( + call(&contract, &get, 4_072), + Outcome::Returned { gas: 4_072, .. } + )); + } + + #[test] + fn an_uncle_is_read_by_an_index_that_must_decode() { + let (_dir, blocks, hashes) = chain_with(10, |block| { + block.uncle_count = u64::from(block.number == 10) + }); + let mut uncle = header(9, hashes[8]); + uncle.beneficiary = Address::repeat_byte(0xD1); + blocks.put_body(hashes[10], &[], &[uncle]).unwrap(); + let contract = at(&blocks, &hashes, 11); + let get = |args: &[[u8; 32]]| { + call( + &contract, + &input("getUncleCoinbaseAddress(int256,int256)", args), + 1_000_000, + ) + }; + let returned = |value: &[u8]| Outcome::Returned { + gas: 4_136, + output: encode_bytes(value).into(), + }; + assert_eq!(get(&[int(0), int(0)]), returned(&[0xD1; 20])); + assert_eq!(get(&[int(0), int(1)]), returned(&[])); + assert_eq!( + get(&[int(0), int(1 << 15)]), + returned(&[]), + "no short holds it" + ); + assert!(matches!( + get(&[int(0), int(-1)]), + Outcome::Threw { gas: 4_136, .. } + )); + // Block 9 has no uncles, and needs no body to say so. + assert_eq!(get(&[int(1), int(0)]), returned(&[])); + // A missing index does not decode. + assert!(matches!(get(&[int(0)]), Outcome::Threw { gas: 0, .. })); + // A block that is not there has no uncles to ask about, negative or not. + assert!(matches!( + get(&[int(4_000), int(-1)]), + Outcome::Returned { .. } + )); + } + + #[test] + fn the_rskip536_methods_are_not_there_before_it() { + let (_dir, blocks, hashes) = chain_with(10, |block| { + block.uncle_count = u64::from(block.number == 10) + }); + blocks + .put_total_difficulty(hashes[10], U256::from(11_996)) + .unwrap(); + let mut uncle = header(9, hashes[8]); + uncle.difficulty = U256::from(2); + blocks.put_body(hashes[10], &[], &[uncle]).unwrap(); + for (signature, value) in [ + ("getDifficultyWithUncles(int256)", vec![3]), + ("getCumulativeWork(int256)", vec![0x2E, 0xDC]), + ] { + let get = input(signature, &[int(0)]); + let before = at(&blocks, &hashes, 11); + assert!(matches!( + call(&before, &get, 1_000_000), + Outcome::Threw { gas: 0, .. } + )); + let after = BlockHeader { + rskip536: true, + ..before + }; + assert_eq!( + call(&after, &get, 1_000_000), + Outcome::Returned { + gas: 4_072, + output: encode_bytes(&value).into() + } + ); + } + } + + /// rskj's store holds every block, body and total difficulty, so one this + /// store lacks stops the node rather than answering empty. + #[test] + fn what_the_store_lacks_is_no_answer() { + let (_dir, blocks, hashes) = chain_with(10, |block| { + block.uncle_count = u64::from(block.number == 10) + }); + let contract = BlockHeader { + rskip536: true, + ..at(&blocks, &hashes, 11) + }; + let get = |signature: &str, args: &[[u8; 32]]| { + call(&contract, &input(signature, args), 1_000_000) + }; + let fatal = |outcome: Outcome| matches!(outcome, Outcome::Fatal(_)); + // Block 10 has an uncle, and the store has no body for it. + assert!(fatal(get("getDifficultyWithUncles(int256)", &[int(0)]))); + assert!(fatal(get( + "getUncleCoinbaseAddress(int256,int256)", + &[int(0), int(0)] + ))); + // Block 9 has none, so it needs no body. + assert_eq!( + get("getDifficultyWithUncles(int256)", &[int(1)]), + Outcome::Returned { + gas: 4_072, + output: encode_bytes(&[1]).into() + } + ); + // No block's total difficulty is stored. + assert!(fatal(get("getCumulativeWork(int256)", &[int(1)]))); + // A hole in the main chain's index. + blocks.delete_canonical_hash(4).unwrap(); + assert!(fatal(get("getCoinbaseAddress(int256)", &[int(6)]))); + } + + #[test] + fn a_block_without_a_bitcoin_header_answers_with_no_bytes_at_all() { + let (_dir, blocks, hashes) = chain_with(12, |block| match block.number { + 11 => block.bitcoin_merged_mining_header = None, + // Stored and read back as an empty field, which rskj reads as null. + 12 => block.bitcoin_merged_mining_header = Some(Bytes::new()), + _ => {} + }); + let get = input("getBitcoinHeader(int256)", &[int(0)]); + for number in [12, 13] { + assert_eq!( + call(&at(&blocks, &hashes, number), &get, 1_000_000), + Outcome::Returned { + gas: 4_072, + output: Bytes::new() + }, + "block {}", + number - 1 + ); + } + assert_eq!( + call(&at(&blocks, &hashes, 11), &get, 1_000_000), + Outcome::Returned { + gas: 4_072, + output: encode_bytes(&[10; 80]).into() + } + ); + } + + #[test] + fn merged_mining_tags_follow_the_last_tag_and_its_hash() { + let mut block = header(1, B256::ZERO); + assert!( + matches!(merged_mining_tags(&block), Err(Failure::Threw(_))), + "no coinbase" + ); + block.bitcoin_merged_mining_coinbase_transaction = Some(Bytes::new()); + assert!( + matches!(merged_mining_tags(&block), Err(Failure::Threw(_))), + "an empty one" + ); + block.bitcoin_merged_mining_coinbase_transaction = Some(Bytes::from(vec![1, 2, 3])); + assert_eq!(merged_mining_tags(&block), Ok(Vec::new()), "no tag"); + let mut coinbase = b"prefix".to_vec(); + coinbase.extend_from_slice(RSK_TAG); + coinbase.extend_from_slice(&[0xAA; 32]); + coinbase.extend_from_slice(b"ALTBLOCK:extra"); + block.bitcoin_merged_mining_coinbase_transaction = Some(Bytes::from(coinbase.clone())); + assert_eq!(merged_mining_tags(&block), Ok(b"ALTBLOCK:extra".to_vec())); + // A tag whose hash runs past the end: `Arrays.copyOfRange` throws. + coinbase.extend_from_slice(RSK_TAG); + coinbase.extend_from_slice(&[0xAA; 31]); + block.bitcoin_merged_mining_coinbase_transaction = Some(Bytes::from(coinbase)); + assert!(matches!(merged_mining_tags(&block), Err(Failure::Threw(_)))); + } + + /// rskj's `BlockHeaderContractTest` constants (a gas limit of 3,000,000, + /// no gas used, a minimum gas price and difficulty of 1, 3 with uncles), + /// the sign byte `BigInteger` adds before a set top bit, and the gas + /// limit's raw form, which takes none. + #[test] + fn numbers_are_written_as_java_writes_them() { + assert_eq!(rlp_bytes(U256::from(3_000_000)), vec![0x2D, 0xC6, 0xC0]); + assert_eq!(big_integer_bytes(U256::ZERO), vec![0]); + assert_eq!(big_integer_bytes(U256::from(1)), vec![1]); + assert_eq!(big_integer_bytes(U256::from(3)), vec![3]); + assert_eq!(big_integer_bytes(U256::from(127)), vec![0x7F]); + assert_eq!(big_integer_bytes(U256::from(128)), vec![0, 0x80]); + assert_eq!(big_integer_bytes(U256::from(255)), vec![0, 0xFF]); + assert_eq!(big_integer_bytes(U256::from(256)), vec![1, 0]); + assert_eq!(big_integer_bytes(U256::from(32_768)), vec![0, 0x80, 0]); + assert_eq!( + big_integer_bytes(U256::from(500_000_000_000_000_000u64)), + vec![0x06, 0xF0, 0x5B, 0x59, 0xD3, 0xB2, 0x00, 0x00] + ); + assert_eq!( + big_integer_bytes(U256::from(10_000_000)), + vec![0, 0x98, 0x96, 0x80] + ); + let mut max = vec![0]; + max.extend_from_slice(&[0xFF; 32]); + assert_eq!(big_integer_bytes(U256::MAX), max); + assert_eq!(rlp_bytes(U256::from(10_000_000)), vec![0x98, 0x96, 0x80]); + assert_eq!(rlp_bytes(U256::ZERO), Vec::::new()); + } +} diff --git a/crates/execution/src/native/environment.rs b/crates/execution/src/native/environment.rs new file mode 100644 index 0000000..ddd304b --- /dev/null +++ b/crates/execution/src/native/environment.rs @@ -0,0 +1,90 @@ +//! Environment (0x01000011, RSKIP203): rskj's `co.rsk.pcc.environment`. + +use alloy_primitives::U256; + +use super::{Failure, NativeContract}; + +/// The contract, told how deep in the call stack its caller is. +pub(crate) struct Environment { + /// The caller's `ProgramInvoke.getCallDeep()`: 0 for the contract a + /// transaction calls, 1 for a contract that contract calls, and so on. + /// `None` when a transaction calls Environment itself: rskj then runs it + /// with no `ProgramInvoke` at all. + pub(crate) caller_depth: Option, +} + +/// Environment's one method. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) enum Call { + /// `getCallStackDepth() returns (uint32)` + GetCallStackDepth, +} + +impl NativeContract for Environment { + type Call = Call; + + /// The method takes no arguments, so whatever follows its selector + /// decodes. + fn parse(&self, selector: [u8; 4], _args: &[u8]) -> Option { + super::method( + &[("getCallStackDepth()", Call::GetCallStackDepth)], + selector, + ) + } + + /// `GetCallStackDepth.getGas`: nothing. + fn gas(&self, _call: &Call, _input: &[u8]) -> u64 { + 0 + } + + /// `GetCallStackDepth.execute`: one more than the caller's depth, or 1 + /// when there is no caller. + fn execute(&self, call: Call) -> Result, Failure> { + let Call::GetCallStackDepth = call; + let depth = self.caller_depth.map_or(1, |depth| depth.saturating_add(1)); + Ok(U256::from(depth).to_be_bytes::<32>().to_vec()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::native::{call, Outcome}; + + fn depth(caller_depth: Option, input: &[u8]) -> Outcome { + call(&Environment { caller_depth }, input, 0) + } + + fn word(n: u64) -> alloy_primitives::Bytes { + U256::from(n).to_be_bytes::<32>().to_vec().into() + } + + /// rskj's `NestedCallsStackDepthTest`: a contract the transaction calls + /// gets 1, and the third contract down a chain gets 3. + #[test] + fn the_depth_is_the_callers_plus_one() { + let selector = crate::abi::selector("getCallStackDepth()"); + for (caller, expected) in [(None, 1), (Some(0), 1), (Some(2), 3)] { + assert_eq!( + depth(caller, &selector), + Outcome::Returned { + gas: 0, + output: word(expected) + } + ); + } + let mut extra = selector.to_vec(); + extra.extend_from_slice(&[0xFF; 40]); + assert!(matches!(depth(Some(0), &extra), Outcome::Returned { .. })); + } + + #[test] + fn a_payload_that_names_no_method_throws_for_nothing() { + for payload in [&[][..], &[0xe8, 0xce, 0x22], &[0xDE, 0xAD, 0xBE, 0xEF]] { + assert!(matches!( + depth(Some(0), payload), + Outcome::Threw { gas: 0, .. } + )); + } + } +} diff --git a/crates/execution/src/native/hd_wallet.rs b/crates/execution/src/native/hd_wallet.rs new file mode 100644 index 0000000..7de9713 --- /dev/null +++ b/crates/execution/src/native/hd_wallet.rs @@ -0,0 +1,705 @@ +//! HDWalletUtils (0x01000009, RSKIP106): rskj's `co.rsk.pcc.bto`, on +//! bitcoinj's BIP32 and base58 code. + +use bitcoin::hashes::{hash160, hmac, sha512, Hash, HashEngine}; +use bitcoin::secp256k1::{PublicKey, Scalar, Secp256k1, VerifyOnly}; + +use super::{Failure, NativeContract}; +use crate::abi::{encode_bytes, Args, Int}; + +/// The contract. It reads nothing but its arguments. +pub(crate) struct HdWalletUtils; + +/// A method with its arguments. +pub(crate) enum Call { + /// `toBase58Check(bytes hash, int256 version) returns (string)` + ToBase58Check { hash: Vec, version: Int }, + /// `deriveExtendedPublicKey(string xpub, string path) returns (string)` + DeriveExtendedPublicKey { xpub: String, path: String }, + /// `extractPublicKeyFromExtendedPublicKey(string xpub) returns (bytes)` + ExtractPublicKey { xpub: String }, + /// `getMultisigScriptHash(int256 minimumSignatures, bytes[] keys) + /// returns (bytes)` + GetMultisigScriptHash { + minimum_signatures: Int, + keys: Vec>, + }, +} + +#[derive(Clone, Copy)] +enum Method { + ToBase58Check, + DeriveExtendedPublicKey, + ExtractPublicKey, + GetMultisigScriptHash, +} + +/// `HDWalletUtils.getMethods`, by signature. +const METHODS: [(&str, Method); 4] = [ + ("toBase58Check(bytes,int256)", Method::ToBase58Check), + ( + "deriveExtendedPublicKey(string,string)", + Method::DeriveExtendedPublicKey, + ), + ( + "extractPublicKeyFromExtendedPublicKey(string)", + Method::ExtractPublicKey, + ), + ( + "getMultisigScriptHash(int256,bytes[])", + Method::GetMultisigScriptHash, + ), +]; + +impl NativeContract for HdWalletUtils { + type Call = Call; + + fn parse(&self, selector: [u8; 4], args: &[u8]) -> Option { + let args = Args::new(args); + Some(match super::method(&METHODS, selector)? { + Method::ToBase58Check => Call::ToBase58Check { + hash: args.bytes(0)?.to_vec(), + version: args.int(1)?, + }, + Method::DeriveExtendedPublicKey => Call::DeriveExtendedPublicKey { + xpub: args.string(0)?, + path: args.string(1)?, + }, + Method::ExtractPublicKey => Call::ExtractPublicKey { + xpub: args.string(0)?, + }, + Method::GetMultisigScriptHash => Call::GetMultisigScriptHash { + minimum_signatures: args.int(0)?, + keys: args + .bytes_array(1)? + .into_iter() + .map(<[u8]>::to_vec) + .collect(), + }, + }) + } + + fn gas(&self, call: &Call, _input: &[u8]) -> u64 { + match call { + Call::ToBase58Check { .. } => 13_000, + Call::DeriveExtendedPublicKey { .. } => 107_000, + Call::ExtractPublicKey { .. } => 11_300, + // 20,000 covers the two keys a script needs, and each one more is + // 700, however many the call names. + Call::GetMultisigScriptHash { keys, .. } => (keys.len() as u64) + .saturating_sub(2) + .saturating_mul(700) + .saturating_add(20_000), + } + } + + fn execute(&self, call: Call) -> Result, Failure> { + match call { + Call::ToBase58Check { hash, version } => to_base58_check(&hash, version), + Call::DeriveExtendedPublicKey { xpub, path } => { + let secp = Secp256k1::verification_only(); + let mut key = ExtendedKey::from_base58(&xpub)?; + for index in derivation_path(&path)? { + key = key.child(&secp, index)?; + } + Ok(encode_bytes(key.to_base58().as_bytes())) + } + Call::ExtractPublicKey { xpub } => Ok(encode_bytes( + &ExtendedKey::from_base58(&xpub)?.key.serialize(), + )), + Call::GetMultisigScriptHash { + minimum_signatures, + keys, + } => multisig_script_hash(minimum_signatures, &keys), + } + } +} + +/// `ToBase58Check.execute`: a 20-byte hash behind a version byte, in +/// base58check. +fn to_base58_check(hash: &[u8], version: Int) -> Result, Failure> { + if hash.len() != 20 { + return Err(Failure::threw("a hash160 is 20 bytes")); + } + // `intValueExact`, then a check for 0 to 255. + let version = version + .int_value_exact() + .and_then(|version| u8::try_from(version).ok()) + .ok_or_else(|| Failure::threw("a version is a number from 0 to 255"))?; + let mut payload = vec![version]; + payload.extend_from_slice(hash); + Ok(encode_bytes( + bitcoin::base58::encode_check(&payload).as_bytes(), + )) +} + +/// `GetMultisigScriptHash.execute`: the hash160 of the redeem script bitcoinj's +/// `ScriptBuilder.createRedeemScript` builds, `OP_m`, the keys compressed and +/// sorted by their bytes, `OP_n` and `OP_CHECKMULTISIG`. +fn multisig_script_hash(minimum_signatures: Int, keys: &[Vec]) -> Result, Failure> { + const OP_0: u8 = 0x50; + const OP_CHECKMULTISIG: u8 = 0xAE; + let required = minimum_signatures + .int_value_exact() + .filter(|required| *required > 0) + .ok_or_else(|| Failure::threw("at least one signature is required"))?; + if keys.len() < 2 { + return Err(Failure::threw("at least 2 public keys are required")); + } + if (keys.len() as u64) < u64::from(required.unsigned_abs()) { + return Err(Failure::threw("fewer keys than required signatures")); + } + if keys.len() > 15 { + return Err(Failure::threw("at most 15 public keys are allowed")); + } + let mut compressed = keys + .iter() + .map(|key| compressed_key(key)) + .collect::, _>>()?; + compressed.sort_unstable(); + // Both counts are 1 to 15 here, so each is a single opcode. + let mut script = vec![OP_0 + required.unsigned_abs() as u8]; + for key in &compressed { + script.push(33); + script.extend_from_slice(key); + } + script.push(OP_0 + compressed.len() as u8); + script.push(OP_CHECKMULTISIG); + Ok(encode_bytes(hash160::Hash::hash(&script).as_byte_array())) +} + +/// A key as `BtcECKey.fromPublicOnly` reads it, compressed: 33 bytes, or 65 +/// uncompressed or hybrid, and a point on the curve. BouncyCastle's +/// `decodePoint` and libsecp256k1 take the same encodings. +fn compressed_key(key: &[u8]) -> Result<[u8; 33], Failure> { + if key.len() != 33 && key.len() != 65 { + return Err(Failure::threw("a public key is 33 or 65 bytes")); + } + PublicKey::from_slice(key) + .map(|key| key.serialize()) + .map_err(|_| Failure::threw("not a public key")) +} + +/// `getBip32HeaderPub` of bitcoinj's MainNet and TestNet3, the networks an +/// `xpub` and a `tpub` prefix name. +const MAINNET: [u8; 4] = [0x04, 0x88, 0xB2, 0x1E]; +const TESTNET: [u8; 4] = [0x04, 0x35, 0x87, 0xCF]; + +/// An extended public key, as bitcoinj's `DeterministicKey` reads and writes +/// one. +struct ExtendedKey { + /// The network's public header, which the key is read with and written + /// with. + header: [u8; 4], + depth: u8, + parent_fingerprint: [u8; 4], + child_number: [u8; 4], + chain_code: [u8; 32], + key: PublicKey, +} + +impl ExtendedKey { + /// `validateAndExtractNetworkFromExtendedPublicKey`, then + /// `DeterministicKey.deserializeB58` and the point its key bytes encode. + /// bitcoinj takes any depth, fingerprint and child number. + fn from_base58(text: &str) -> Result { + let header = if text.starts_with("xpub") { + MAINNET + } else if text.starts_with("tpub") { + TESTNET + } else { + return Err(Failure::threw( + "an extended public key starts with xpub or tpub", + )); + }; + let data = + bitcoin::base58::decode_check(text).map_err(|_| Failure::threw("not base58check"))?; + let data: [u8; 78] = data + .try_into() + .map_err(|_| Failure::threw("an extended key is 78 bytes"))?; + // bitcoinj would take the network's private header too, but a key + // written with it is spelled xprv or tprv, never xpub or tpub. + if data[..4] != header { + return Err(Failure::threw("not an extended public key of the network")); + } + let key = + PublicKey::from_slice(&data[45..]).map_err(|_| Failure::threw("not a public key"))?; + Ok(Self { + header, + depth: data[4], + parent_fingerprint: field(&data, 5), + child_number: field(&data, 9), + chain_code: field(&data, 13), + key, + }) + } + + /// `HDKeyDerivation.deriveChildKey` from the public key alone: BIP32's + /// CKDpub, at an index below 2^31. + fn child(&self, secp: &Secp256k1, index: u32) -> Result { + let parent = self.key.serialize(); + let mut engine = hmac::HmacEngine::::new(&self.chain_code); + engine.input(&parent); + engine.input(&index.to_be_bytes()); + let i = hmac::Hmac::::from_engine(engine).to_byte_array(); + let tweak = Scalar::from_be_bytes(field(&i, 0)) + .map_err(|_| Failure::threw("I_L is not below the curve's order"))?; + let key = self + .key + .add_exp_tweak(secp, &tweak) + .map_err(|_| Failure::threw("the derived key is the point at infinity"))?; + Ok(Self { + header: self.header, + // bitcoinj counts the depth in an int and writes its low byte. + depth: self.depth.wrapping_add(1), + parent_fingerprint: field(hash160::Hash::hash(&parent).as_byte_array(), 0), + child_number: index.to_be_bytes(), + chain_code: field(&i, 32), + key, + }) + } + + /// `DeterministicKey.serializePubB58`. + fn to_base58(&self) -> String { + let mut data = Vec::with_capacity(78); + data.extend_from_slice(&self.header); + data.push(self.depth); + data.extend_from_slice(&self.parent_fingerprint); + data.extend_from_slice(&self.child_number); + data.extend_from_slice(&self.chain_code); + data.extend_from_slice(&self.key.serialize()); + bitcoin::base58::encode_check(&data) + } +} + +/// The `N` bytes of `data` from `at`, a fixed place inside it. +fn field(data: &[u8], at: usize) -> [u8; N] { + let mut out = [0; N]; + out.copy_from_slice(&data[at..at + N]); + out +} + +/// The path checks of `DeriveExtendedPublicKey.execute`, then bitcoinj's +/// `HDUtils.parsePath`: one to ten indices with slashes between them, each a +/// string Java's `Integer.parseInt` reads as 0 or more. +/// +/// Java sees the path as UTF-16. Its first and last code units must each be +/// a digit, so neither end of `split("/")` is empty. +fn derivation_path(path: &str) -> Result, Failure> { + let invalid = || Failure::threw("invalid path"); + let units: Vec = path.encode_utf16().collect(); + let (Some(first), Some(last)) = (units.first(), units.last()) else { + return Err(invalid()); + }; + if !is_decimal(&[*first]) || !is_decimal(&[*last]) { + return Err(invalid()); + } + let chunks: Vec<&[u16]> = units.split(|unit| *unit == u16::from(b'/')).collect(); + if chunks.len() > 10 { + return Err(Failure::threw("a path has 10 levels at most")); + } + chunks + .into_iter() + .map(|chunk| { + parse_int(chunk) + .and_then(|index| u32::try_from(index).ok()) + .ok_or_else(invalid) + }) + .collect() +} + +/// `DeriveExtendedPublicKey.isDecimal`: `Integer.parseInt(s) >= 0`. +fn is_decimal(units: &[u16]) -> bool { + parse_int(units).is_some_and(|n| n >= 0) +} + +/// Java's `Integer.parseInt(s)`: an optional sign, when more follows it, then +/// decimal digits of any script, the value fitting an int. +fn parse_int(units: &[u16]) -> Option { + let (negative, digits) = match units.split_first()? { + (sign, rest) if *sign == u16::from(b'-') => (true, rest), + (sign, rest) if *sign == u16::from(b'+') => (false, rest), + _ => (false, units), + }; + if digits.is_empty() { + return None; + } + let mut value: i64 = 0; + for unit in digits { + value = value * 10 + i64::from(digit(*unit)?); + if value > 1 << 31 { + return None; + } + } + i32::try_from(if negative { -value } else { value }).ok() +} + +/// `Character.digit(char, 10)`: the value of a decimal digit of any script in +/// the Basic Multilingual Plane. A digit beyond it is two `char`s to Java, and +/// neither is a digit. +fn digit(unit: u16) -> Option { + ZEROS + .iter() + .find_map(|zero| unit.checked_sub(*zero).filter(|value| *value < 10)) + .map(u32::from) +} + +/// The first of each run of ten decimal digits (Unicode's Nd) in the Basic +/// Multilingual Plane. Unicode 13 to 16 have the same ones, so every Java +/// rskj runs on does. +const ZEROS: [u16; 37] = [ + 0x0030, 0x0660, 0x06F0, 0x07C0, 0x0966, 0x09E6, 0x0A66, 0x0AE6, 0x0B66, 0x0BE6, 0x0C66, 0x0CE6, + 0x0D66, 0x0DE6, 0x0E50, 0x0ED0, 0x0F20, 0x1040, 0x1090, 0x17E0, 0x1810, 0x1946, 0x19D0, 0x1A80, + 0x1A90, 0x1B50, 0x1BB0, 0x1C40, 0x1C50, 0xA620, 0xA8D0, 0xA900, 0xA9D0, 0xA9F0, 0xAA50, 0xABF0, + 0xFF10, +]; + +#[cfg(test)] +mod tests { + use super::*; + use crate::abi::encode::{self, Arg}; + use crate::native::{call, Outcome}; + + /// The keys of rskj's own tests. + const TPUB: &str = "tpubD6NzVbkrYhZ4YHQqwWz3Tm1ESZ9AidobeyLG4mEezB6hN8gFFWrcjczyF77Lw3HEs6Rjd2R11BEJ8Y9ptfxx9DFknkdujp58mFMx9H5dc1r"; + const XPUB: &str = "xpub661MyMwAqRbcFMGNG2YcHvj3x63bAZN9U5cKikaiQ4zu2D1cvpnZYyXNR9nH62sGp4RR39Ui7SVQSq1PY4JbPuEuu5prVJJC3d5Pogft712"; + + fn input(signature: &str, args: &[Arg]) -> Vec { + let mut input = crate::abi::selector(signature).to_vec(); + input.extend(encode::args(args)); + input + } + + fn run(input: &[u8]) -> Outcome { + call(&HdWalletUtils, input, u64::MAX) + } + + /// The one `bytes` or `string` a call returned. + fn returned(outcome: Outcome) -> Vec { + match outcome { + Outcome::Returned { output, .. } => crate::abi::Args::new(&output) + .bytes(0) + .expect("a bytes result") + .to_vec(), + other => panic!("expected a result, got {other:?}"), + } + } + + /// Whether a call threw after it was charged `gas`. + fn threw(gas: u64, outcome: &Outcome) -> bool { + matches!(outcome, Outcome::Threw { gas: charged, .. } if *charged == gas) + } + + fn derive(xpub: &str, path: &str) -> Outcome { + run(&input( + "deriveExtendedPublicKey(string,string)", + &[Arg::Bytes(xpub.as_bytes()), Arg::Bytes(path.as_bytes())], + )) + } + + fn derived(xpub: &str, path: &str) -> String { + String::from_utf8(returned(derive(xpub, path))).unwrap() + } + + fn extract(xpub: &[u8]) -> Outcome { + run(&input( + "extractPublicKeyFromExtendedPublicKey(string)", + &[Arg::Bytes(xpub)], + )) + } + + fn base58_check(hash: &[u8], version: i64) -> Outcome { + run(&input( + "toBase58Check(bytes,int256)", + &[Arg::Bytes(hash), Arg::Word(encode::int(version))], + )) + } + + fn multisig(required: i64, keys: &[Vec]) -> Outcome { + let keys: Vec<&[u8]> = keys.iter().map(Vec::as_slice).collect(); + run(&input( + "getMultisigScriptHash(int256,bytes[])", + &[Arg::Word(encode::int(required)), Arg::BytesArray(&keys)], + )) + } + + /// rskj's `GetMultisigScriptHashTest` keys. + fn keys() -> Vec> { + [ + "03b53899c390573471ba30e5054f78376c5f797fda26dde7a760789f02908cbad2", + "027319afb15481dbeb3c426bcc37f9a30e7f51ceff586936d85548d9395bcc2344", + "0355a2e9bf100c00fc0a214afd1bf272647c7824eb9cb055480962f0c382596a70", + "02566d5ded7c7db1aa7ee4ef6f76989fb42527fcfdcddcd447d6793b7d869e46f7", + "0294c817150f78607566e961b3c71df53a22022a80acbb982f83c0c8baac040adc", + "0372cd46831f3b6afd4c044d160b7667e8ebf659d6cb51a825a3104df6ee0638c6", + "0340df69f28d69eef60845da7d81ff60a9060d4da35c767f017b0dd4e20448fb44", + "02ac1901b6fba2c1dbd47d894d2bd76c8ba1d296d65f6ab47f1c6b22afb53e73eb", + "031aabbeb9b27258f98c2bf21f36677ae7bae09eb2d8c958ef41a20a6e88626d26", + "0245ef34f5ee218005c9c21227133e8568a4f3f11aeab919c66ff7b816ae1ffeea", + "02550cc87fa9061162b1dd395a16662529c9d8094c0feca17905a3244713d65fe8", + "02481f02b7140acbf3fcdd9f72cf9a7d9484d8125e6df7c9451cfa55ba3b077265", + "03f909ae15558c70cc751aff9b1f495199c325b13a9e5b934fd6299cd30ec50be8", + "02c6018fcbd3e89f3cf9c7f48b3232ea3638eb8bf217e59ee290f5f0cfb2fb9259", + "03b65694ccccda83cbb1e56b31308acd08e993114c33f66a456b627c2c1c68bed6", + ] + .iter() + .map(|key| hex::decode(key).unwrap()) + .collect() + } + + #[test] + fn a_hash_and_a_version_make_a_base58check_address() { + let hash = hex::decode("0d3bf5f30dda7584645546079318e97f0e1d044f").unwrap(); + let outcome = base58_check(&hash, 111); + assert!(matches!(outcome, Outcome::Returned { gas: 13_000, .. })); + assert_eq!(returned(outcome), b"mgivuh9jErcGdRr81cJ3A7YfgbJV7WNyZV"); + let (short, zero) = ([0xAA; 19], [0; 20]); + for (hash, version) in [ + (&short[..], 111), + (&zero[..], -1), + (&zero[..], 256), + (&zero[..], 1 << 40), + ] { + assert!(threw(13_000, &base58_check(hash, version)), "{version}"); + } + } + + /// rskj's `DeriveExtendedPublicKeyTest`. + #[test] + fn a_tpub_derives_as_rskjs_tests_say() { + assert_eq!( + derived(TPUB, "2/3/4"), + "tpubDCGMkPKredy7oh6zw8f4ExWFdTgQCrAHToF1ytny3gbVy9GkUNK2Nqh7NbKbh8dkd5VtjUiLJPkbEkeg29NVHwxYwzHJFt9SazGLZrrU4Y4" + ); + assert_eq!( + derived(TPUB, "0/0/0/0/0/0"), + "tpubDJ28nwFGUypUD6i8eGCQfMkwNGxzzabA5Mh7AcUdwm6ziFxCSWjy4HyhPXH5uU2ovdMMYLT9W3g3MrGo52TrprMvX8o1dzT2ZGz1pwCPTNv" + ); + assert_eq!( + derived(TPUB, "2147483647"), + "tpubD8fY35uPCY1rUjMUZwhkGUFi33pwkffMEBaCsTSw1he2AbM6DMbPaRR2guvk5qTWDfE9ubFB5pzuUNnMtsqbCeKAAjfepSvEWyetyF9Q4fG" + ); + assert!(matches!( + derive(TPUB, "2/3/4"), + Outcome::Returned { gas: 107_000, .. } + )); + assert!(derived(XPUB, "0/9").starts_with("xpub")); + assert!(threw( + 107_000, + &derive("this-is-not-an-xpub", "this-doesnt-matter") + )); + } + + /// Java's `Integer.parseInt` takes a sign before an index that is not the + /// first, leading zeros, and decimal digits of any script in the Basic + /// Multilingual Plane. + #[test] + fn a_path_is_read_as_javas_parse_int_reads_it() { + assert_eq!(derived(TPUB, "1/-0"), derived(TPUB, "1/0")); + assert_eq!(derived(TPUB, "1/+5"), derived(TPUB, "1/5")); + assert_eq!(derived(TPUB, "00/007"), derived(TPUB, "0/7")); + assert_eq!(derived(TPUB, "\u{0661}/\u{0662}"), derived(TPUB, "1/2")); + assert_eq!(derived(TPUB, "\u{FF11}"), derived(TPUB, "1")); + assert!(matches!( + derive(TPUB, "0/1/2/3/4/5/6/7/8/9"), + Outcome::Returned { .. } + )); + for path in [ + "this-is-not-a-path", + "", + "M/0/1/2", + "/0", + "0/", + "4'/5", + "0/-1", + "-0", + "+5", + "1//2", + "1/ 2", + "0/1/2/2147483648", + "0/1/2/3/4/5/6/7/8/9/10", + "\u{1D7CF}", + "1/\u{1D7CF}", + ] { + assert!(threw(107_000, &derive(TPUB, path)), "{path:?}"); + } + } + + /// bitcoinj writes a depth's low byte, so the child of a depth-255 key has + /// depth 0. + #[test] + fn a_depth_past_255_wraps() { + let mut data = bitcoin::base58::decode_check(TPUB).unwrap(); + data[4] = 255; + let deep = bitcoin::base58::encode_check(&data); + assert!(deep.starts_with("tpub")); + let child = bitcoin::base58::decode_check(&derived(&deep, "0")).unwrap(); + let mut expected = bitcoin::base58::decode_check(&derived(TPUB, "0")).unwrap(); + assert_eq!(expected[4], 1); + expected[4] = 0; + assert_eq!(child, expected); + } + + /// rskj's `ExtractPublicKeyFromExtendedPublicKeyTest`, and keys it refuses. + #[test] + fn an_xpub_gives_its_compressed_key() { + let outcome = extract(XPUB.as_bytes()); + assert!(matches!(outcome, Outcome::Returned { gas: 11_300, .. })); + assert_eq!( + hex::encode(returned(outcome)), + "02be517550b9e3be7fe42c80932d51e88e698663b4926e598b269d050e87e34d8c" + ); + let mut header = bitcoin::base58::decode_check(XPUB).unwrap(); + header[3] += 1; + let mut point = bitcoin::base58::decode_check(XPUB).unwrap(); + point[45] = 0x04; + for xpub in [ + "this-is-not-an-xpub".to_string(), + TPUB.replace("dc1r", "dc1s"), + "xpub".to_string(), + bitcoin::base58::encode_check(&header), + bitcoin::base58::encode_check(&point), + ] { + assert!(threw(11_300, &extract(xpub.as_bytes())), "{xpub}"); + } + // A string that is not UTF-8 still decodes, and then is no key. + assert!(threw(11_300, &extract(&[b'x', b'p', b'u', b'b', 0xFF]))); + } + + /// rskj's `GetMultisigScriptHashTest`. + #[test] + fn fifteen_keys_hash_as_rskjs_test_says() { + let outcome = multisig(8, &keys()); + assert!(matches!(outcome, Outcome::Returned { gas: 29_100, .. })); + assert_eq!( + hex::encode(returned(outcome)), + "51f103320b435b5fe417b3f3e0f18972ccc710a0" + ); + } + + /// A key may also come uncompressed, or in the hybrid form whose prefix + /// says whether y is odd; either is compressed first. + #[test] + fn every_encoding_of_a_key_hashes_the_same() { + let mut keys = keys(); + keys[0] = hex::decode("04b53899c390573471ba30e5054f78376c5f797fda26dde7a760789f02908cbad2aafaaa2611606699ec4f82777a268b708dab346de4880cd223969f7bbe5422bf").unwrap(); + keys[4] = hex::decode("0694c817150f78607566e961b3c71df53a22022a80acbb982f83c0c8baac040adcb17171aa9ec8d8587098e0771f686ee61ac35279f9e5aadf9b06b738aa6d3720").unwrap(); + keys[6] = hex::decode("0740df69f28d69eef60845da7d81ff60a9060d4da35c767f017b0dd4e20448fb44e1abebaea4c3c57c6e9e39e205b4df046f7110a8d3477c0d8e26a28be9692c29").unwrap(); + keys[9] = hex::decode("0445ef34f5ee218005c9c21227133e8568a4f3f11aeab919c66ff7b816ae1ffeeae024d50312de76a7950f8c6268fbf454335cf252f961a67c47e67dc06fa590ba").unwrap(); + assert_eq!( + hex::encode(returned(multisig(8, &keys))), + "51f103320b435b5fe417b3f3e0f18972ccc710a0" + ); + // A hybrid prefix that names the wrong parity is no key. + keys[4][0] = 0x07; + assert!(threw(29_100, &multisig(8, &keys))); + } + + #[test] + fn a_multisig_needs_2_to_15_keys_and_at_least_m() { + let keys = keys(); + assert!(matches!( + multisig(1, &keys[..2]), + Outcome::Returned { gas: 20_000, .. } + )); + for (required, keys) in [ + (0, &keys[..2]), + (-1, &keys[..2]), + (1 << 40, &keys[..2]), + (1, &keys[..1]), + (3, &keys[..2]), + ] { + assert!(threw(20_000, &multisig(required, keys)), "{required}"); + } + let mut sixteen = keys.clone(); + sixteen.push(keys[0].clone()); + assert!(threw(29_800, &multisig(3, &sixteen))); + let mut odd = keys[..2].to_vec(); + odd[1] = vec![0xAA, 0xBB, 0xCC]; + assert!(threw(20_000, &multisig(1, &odd))); + odd[1] = keys[0].clone(); + odd[1][0] = 0x08; + assert!(threw(20_000, &multisig(1, &odd))); + } + + /// Offsets and counts are a word's low 32 bits to rskj: the offsets that + /// once wrapped here, a count of 2^32 that was once allocated, and an + /// offset with its high bytes set. + #[test] + fn a_crafted_offset_or_count_reads_as_rskj_reads_it() { + let mut wrap = [0u8; 32]; + wrap[24..].copy_from_slice(&(u64::MAX - 31).to_be_bytes()); + let mut payload = crate::abi::selector("toBase58Check(bytes,int256)").to_vec(); + payload.extend_from_slice(&wrap); + payload.extend_from_slice(&encode::int(111)); + assert!(threw(0, &run(&payload)), "an offset of -32 holds nothing"); + let multisig_at = |offset: [u8; 32], tail: &[[u8; 32]]| { + let mut payload = + crate::abi::selector("getMultisigScriptHash(int256,bytes[])").to_vec(); + payload.extend_from_slice(&encode::int(1)); + payload.extend_from_slice(&offset); + tail.iter().for_each(|word| payload.extend_from_slice(word)); + run(&payload) + }; + assert!(threw(0, &multisig_at(wrap, &[]))); + assert!( + threw( + 20_000, + &multisig_at(encode::int(64), &[encode::int(1 << 32)]) + ), + "no keys at all" + ); + let hash = hex::decode("0d3bf5f30dda7584645546079318e97f0e1d044f").unwrap(); + let mut payload = input( + "toBase58Check(bytes,int256)", + &[Arg::Bytes(&hash), Arg::Word(encode::int(111))], + ); + payload[4] = 0xFF; + assert_eq!( + returned(run(&payload)), + b"mgivuh9jErcGdRr81cJ3A7YfgbJV7WNyZV" + ); + } + + /// No payload makes a method panic, whatever its words hold. + #[test] + fn no_payload_panics() { + let mut wrap = [0u8; 32]; + wrap[24..].copy_from_slice(&(u64::MAX - 31).to_be_bytes()); + let words: Vec<[u8; 32]> = [ + 0, + 1, + 20, + 32, + 33, + 64, + 96, + -1, + -32, + i64::from(i32::MAX), + i64::from(i32::MIN), + 1 << 32, + (1 << 32) + 64, + ] + .into_iter() + .map(encode::int) + .chain([[0xFF; 32], wrap]) + .collect(); + for (signature, _) in METHODS { + for a in &words { + for b in &words { + for c in &words { + let mut payload = crate::abi::selector(signature).to_vec(); + [a, b, c] + .iter() + .for_each(|word| payload.extend_from_slice(*word)); + payload.extend_from_slice(b"tpubD6NzVbkrYhZ4Y"); + for len in [36, 68, payload.len()] { + let _ = run(&payload[..len]); + } + } + } + } + } + } +} diff --git a/crates/execution/src/native/mod.rs b/crates/execution/src/native/mod.rs new file mode 100644 index 0000000..a5fe1d7 --- /dev/null +++ b/crates/execution/src/native/mod.rs @@ -0,0 +1,193 @@ +//! rskj's native contracts: HDWalletUtils (0x01000009), BlockHeader +//! (0x01000010) and Environment (0x01000011). +//! +//! rskj writes each one as a `NativeContract`: a list of methods, each a +//! `CallTransaction.Function` with its own gas and its own `execute`. A call +//! goes through three steps, and the rules of each are consensus: +//! +//! 1. `parseData` picks the method by its selector and decodes its arguments +//! ([`crate::abi`]). A payload of one to three bytes, an empty one (none of +//! these contracts has a default method), a selector no method has, a +//! method that is not enabled yet and arguments that do not decode all +//! leave no call. +//! 2. `getGasForData` is the method's gas, or 0 when there is no call. The +//! caller charges it before anything runs, and a call that cannot pay it is +//! out of gas. +//! 3. `execute` runs the method and ABI-encodes its result. It throws when +//! there is no call, and wherever the method refuses its arguments. +//! +//! What a throw costs depends on who made the call, a transaction or a +//! contract, and on RSKIP197: `RskPrecompileProvider` settles it. + +pub(crate) mod block_header; +pub(crate) mod environment; +pub(crate) mod hd_wallet; + +use alloy_primitives::Bytes; + +/// How a call to a precompile ended, in rskj's terms. +#[derive(Debug, PartialEq, Eq)] +pub(crate) enum Outcome { + /// `execute` returned `output`, and the call cost `gas`: its + /// `getGasForData`. + Returned { gas: u64, output: Bytes }, + /// `getGasForData` asked for more gas than the call carries, so nothing + /// ran. + OutOfGas, + /// `execute` threw, and the call cost `gas`: its `getGasForData`, which is + /// 0 when no method was called. + Threw { gas: u64, reason: String }, + /// The node could not read what the call needs, so it has no answer. + Fatal(String), +} + +/// Why a method did not return. +#[derive(Debug, PartialEq, Eq)] +pub(crate) enum Failure { + /// rskj's method throws. + Threw(String), + /// The node could not read what rskj's method reads. + Fatal(String), +} + +impl Failure { + pub(crate) fn threw(reason: impl Into) -> Self { + Failure::Threw(reason.into()) + } +} + +/// One of rskj's native contracts. +pub(crate) trait NativeContract { + /// A method with its arguments decoded. + type Call; + + /// `parseData` for a payload that has a selector: the method `selector` + /// names, with `args` decoded, or `None` where rskj finds no call. + fn parse(&self, selector: [u8; 4], args: &[u8]) -> Option; + + /// The method's `getGas`. `input` is the whole payload, selector included. + fn gas(&self, call: &Self::Call, input: &[u8]) -> u64; + + /// The method's `execute`, its result ABI-encoded. + fn execute(&self, call: Self::Call) -> Result, Failure>; +} + +/// Calls `contract` with `input` and `gas_limit` as rskj does: parse, charge, +/// execute. +pub(crate) fn call(contract: &C, input: &[u8], gas_limit: u64) -> Outcome { + // A selector takes four bytes, and none of these contracts has a default + // method for a payload without one. + let call = input + .split_first_chunk::<4>() + .and_then(|(selector, args)| contract.parse(*selector, args)); + let Some(call) = call else { + return Outcome::Threw { + gas: 0, + reason: "the payload names no method".into(), + }; + }; + let gas = contract.gas(&call, input); + if gas > gas_limit { + return Outcome::OutOfGas; + } + match contract.execute(call) { + Ok(output) => Outcome::Returned { + gas, + output: output.into(), + }, + Err(Failure::Threw(reason)) => Outcome::Threw { gas, reason }, + Err(Failure::Fatal(reason)) => Outcome::Fatal(reason), + } +} + +/// The method `selector` names among a contract's `methods`, each given by +/// its signature. +fn method(methods: &[(&str, M)], selector: [u8; 4]) -> Option { + methods + .iter() + .find(|(signature, _)| crate::abi::selector(signature) == selector) + .map(|(_, method)| *method) +} + +#[cfg(test)] +mod tests { + use super::*; + + /// A contract whose one method, `f(int256)`, costs its argument and + /// throws when that is 7. + struct Probe; + + impl NativeContract for Probe { + type Call = u64; + + fn parse(&self, selector: [u8; 4], args: &[u8]) -> Option { + method(&[("f(int256)", ())], selector)?; + crate::abi::Args::new(args) + .int(0)? + .long_value_exact()? + .try_into() + .ok() + } + + fn gas(&self, call: &u64, _: &[u8]) -> u64 { + *call + } + + fn execute(&self, call: u64) -> Result, Failure> { + match call { + 7 => Err(Failure::threw("seven")), + _ => Ok(vec![1]), + } + } + } + + fn input(n: u64) -> Vec { + let mut input = crate::abi::selector("f(int256)").to_vec(); + input.extend_from_slice(&alloy_primitives::U256::from(n).to_be_bytes::<32>()); + input + } + + #[test] + fn a_payload_without_a_whole_selector_calls_nothing_and_costs_nothing() { + for payload in [&[][..], &[1], &[1, 2, 3]] { + assert!(matches!( + call(&Probe, payload, 100), + Outcome::Threw { gas: 0, .. } + )); + } + let unknown = [0xFF; 36]; + assert!(matches!( + call(&Probe, &unknown, 100), + Outcome::Threw { gas: 0, .. } + )); + } + + #[test] + fn the_gas_is_charged_before_anything_runs() { + assert_eq!(call(&Probe, &input(50), 49), Outcome::OutOfGas); + assert_eq!( + call(&Probe, &input(50), 50), + Outcome::Returned { + gas: 50, + output: vec![1].into() + } + ); + } + + #[test] + fn a_method_that_throws_still_costs_its_gas() { + assert_eq!( + call(&Probe, &input(7), 100), + Outcome::Threw { + gas: 7, + reason: "seven".into() + } + ); + // An argument that does not decode is no call at all. + let seven = input(7); + assert!(matches!( + call(&Probe, &seven[..20], 100), + Outcome::Threw { gas: 0, .. } + )); + } +} diff --git a/crates/execution/src/precompiles.rs b/crates/execution/src/precompiles.rs index 2d38445..87a120d 100644 --- a/crates/execution/src/precompiles.rs +++ b/crates/execution/src/precompiles.rs @@ -16,7 +16,7 @@ /// | 0x01000006 | RSK (genesis) | Bridge | /// | 0x01000008 | RSK (genesis) | REMASC | /// | 0x01000009+ | RSK (various) | HDWalletUtils, BlockHeader, etc | -use alloy_primitives::{Address, Bytes, U256}; +use alloy_primitives::{Address, Bytes, B256}; use revm::context::{Cfg, LocalContextTr}; use revm::context_interface::{Block as BlockTr, ContextTr, JournalTr}; use revm::handler::PrecompileProvider; @@ -31,6 +31,10 @@ use std::sync::Arc; use crate::bridge::constants::BridgeConstants; use crate::hardfork::{RskHardforkConfig, RskNetworkUpgrade}; +use crate::native::block_header::BlockHeader; +use crate::native::environment::Environment; +use crate::native::hd_wallet::HdWalletUtils; +use crate::native::{self, Outcome}; use crate::remasc::RemascConfig; // --------------------------------------------------------------------------- @@ -122,48 +126,14 @@ fn bridge_run(_input: &[u8], gas_limit: u64) -> Result Result { - if input.len() < 4 { - return Err(PrecompileError::other("HDWalletUtils: input too short for selector")); - } - - let selector = [input[0], input[1], input[2], input[3]]; - let data = &input[4..]; - - if selector == selector_of("toBase58Check(bytes,int256)") { - run_to_base58check(data, gas_limit) - } else if selector == selector_of("deriveExtendedPublicKey(string,string)") { - run_derive_extended_public_key(data, gas_limit) - } else if selector == selector_of("extractPublicKeyFromExtendedPublicKey(string)") { - run_extract_pubkey(data, gas_limit) - } else if selector == selector_of("getMultisigScriptHash(int256,bytes[])") { - run_get_multisig_script_hash(data, gas_limit) - } else { - Err(PrecompileError::other("HDWalletUtils: unknown method selector")) - } -} - -/// BlockHeaderContract: exposes block header fields to smart contracts. -/// Stub — returns empty with base gas cost. -fn block_header_run(input: &[u8], gas_limit: u64) -> Result { - let gas_cost = 4_000u64 + 2 * input.len() as u64; - if gas_limit < gas_cost { - return Err(PrecompileError::OutOfGas); - } - Ok(PrecompileOutput::new(gas_cost, Vec::new().into())) -} - -/// Environment: provides call stack depth. -/// Gas cost: 0. -fn environment_run(_input: &[u8], _gas_limit: u64) -> Result { - Ok(PrecompileOutput::new(0, Vec::new().into())) +/// What the precompile set holds for HDWalletUtils, BlockHeader and +/// Environment, so each address is a precompile from its activation on. +/// `RskPrecompileProvider` runs these native contracts itself +/// ([`crate::native`]) and never calls this. +fn native_contract(_input: &[u8], _gas_limit: u64) -> Result { + Err(PrecompileError::Fatal( + "a native contract runs through RskPrecompileProvider".into(), + )) } /// Secp256k1 point addition. @@ -263,504 +233,6 @@ fn encode_secp256k1_point(point: k256::ProjectivePoint) -> Vec { out } -// --------------------------------------------------------------------------- -// BlockHeaderContract helpers -// --------------------------------------------------------------------------- - -/// Compute the 4-byte ABI method selector (keccak256 of the signature). -fn selector_of(sig: &str) -> [u8; 4] { - use sha3::{Digest, Keccak256}; - let h = Keccak256::digest(sig.as_bytes()); - [h[0], h[1], h[2], h[3]] -} - -#[derive(Debug)] -enum DepthError { - Overflow, - Negative, -} - -/// Parse a 32-byte ABI-encoded int256 into an i16 depth value, matching rskj's -/// `shortValueExact()` semantics: values outside [-32768, 32767] return Overflow, -/// negative values return Negative. -fn parse_int256_as_depth(data: &[u8]) -> Result { - let mut word = [0u8; 32]; - let len = data.len().min(32); - word[32 - len..].copy_from_slice(&data[..len]); - - // Check sign: int256 is two's complement, high bit indicates negative. - let is_negative = word[0] & 0x80 != 0; - - if is_negative { - // Negative: all leading bytes must be 0xFF and the result must fit i16. - let all_ff = word[..30].iter().all(|&b| b == 0xFF); - if !all_ff { - return Err(DepthError::Negative); - } - let val = i16::from_be_bytes([word[30], word[31]]); - if val >= 0 { - // e.g. 0xFFFF...FF0001 → the i16 portion is positive, meaning - // the int256 value is very large negative; still negative. - return Err(DepthError::Negative); - } - Err(DepthError::Negative) - } else { - // Positive: all leading bytes must be 0x00 and result must fit i16. - let all_zero = word[..30].iter().all(|&b| b == 0); - if !all_zero { - return Err(DepthError::Overflow); - } - let val = i16::from_be_bytes([word[30], word[31]]); - if val < 0 { - // i16 overflowed (e.g. 0x0000...8000 = 32768 doesn't fit signed i16) - return Err(DepthError::Overflow); - } - Ok(val) - } -} - -/// ABI-encode raw bytes as a Solidity `bytes` return value, replicating -/// ethereumj's `SolidityType.BytesType.encode`. -/// -/// Layout: 32-byte offset (0x20) | 32-byte length | data (right-padded to 32). -/// -/// ethereumj pads with `((len - 1) / 32 + 1) * 32` — Java's `-1/32 == 0` -/// makes EMPTY bytes pad to one full zero word (96 bytes total instead of -/// the canonical 64). The extra returndata word is consensus-visible -/// (RETURNDATACOPY copy gas): mainnet #1,669,062 calls -/// getCoinbaseAddress(4000) (≥ MAX_DEPTH → empty result) and rskj's reply -/// is 96 bytes. -fn abi_encode_bytes(data: &[u8]) -> Vec { - let padded_len = if data.is_empty() { - 32 - } else { - data.len().div_ceil(32) * 32 - }; - let mut out = vec![0u8; 64 + padded_len]; - // offset → 0x20 - out[31] = 0x20; - // length - let len_be = (data.len() as u64).to_be_bytes(); - out[56..64].copy_from_slice(&len_be); - // data - out[64..64 + data.len()].copy_from_slice(data); - out -} - -/// Convert a U256 to Java's `BigInteger.toByteArray()` format: -/// minimal big-endian two's complement with a leading 0x00 when the high bit -/// of the first data byte is set (to keep the value positive). -fn u256_to_java_bigint_bytes(val: U256) -> Vec { - if val.is_zero() { - return vec![0]; - } - let be = val.to_be_bytes::<32>(); - let start = be.iter().position(|&b| b != 0).unwrap_or(31); - let mut bytes = be[start..].to_vec(); - if bytes[0] & 0x80 != 0 { - bytes.insert(0, 0); - } - bytes -} - -/// Convert a u64 to Java's `BigInteger.valueOf(n).toByteArray()` format. -fn u64_to_java_bigint_bytes(val: u64) -> Vec { - u256_to_java_bigint_bytes(U256::from(val)) -} - -/// Minimal unsigned big-endian bytes (empty for zero) — the raw form of a -/// header RLP element. rskj's `GetGasLimit` returns `header.getGasLimit()` -/// verbatim (the decoded RLP bytes), so unlike the BigInteger-encoded fields -/// there is no 0x00 sign prefix when the high bit is set. -fn u256_to_raw_minimal_bytes(val: U256) -> Vec { - let be = val.to_be_bytes::<32>(); - let start = be.iter().position(|&b| b != 0).unwrap_or(32); - be[start..].to_vec() -} - -/// Extract merged mining tags from a block header's bitcoin coinbase tx. -/// -/// Finds the last occurrence of `RSKBLOCK:` in the coinbase transaction, -/// skips past the 32-byte block header hash, and returns the remaining bytes. -fn extract_merged_mining_tags(header: &rustock_core::Header) -> Vec { - let coinbase_tx = match &header.bitcoin_merged_mining_coinbase_transaction { - Some(tx) => tx.as_ref(), - None => return Vec::new(), - }; - - // Find the last occurrence of RSK_TAG - let tag_pos = coinbase_tx - .windows(RSK_TAG.len()) - .rposition(|w| w == RSK_TAG); - - let tag_pos = match tag_pos { - Some(p) => p, - None => return Vec::new(), - }; - - let start = tag_pos + RSK_TAG.len() + RSK_TAG_HASH_SIZE; - if start >= coinbase_tx.len() { - return Vec::new(); - } - coinbase_tx[start..].to_vec() -} - -// --------------------------------------------------------------------------- -// HDWalletUtils ABI helpers -// --------------------------------------------------------------------------- - -/// ABI-encode a string return value (identical layout to `abi_encode_bytes`). -fn abi_encode_string(s: &str) -> Vec { - abi_encode_bytes(s.as_bytes()) -} - -/// Read a big-endian uint256 from a 32-byte word, returning its value as usize. -/// Returns `None` if the value overflows usize. -fn abi_read_offset(data: &[u8], pos: usize) -> Option { - if pos + 32 > data.len() { - return None; - } - let word = &data[pos..pos + 32]; - if word[..24].iter().any(|&b| b != 0) { - return None; // too large for usize - } - let mut buf = [0u8; 8]; - buf.copy_from_slice(&word[24..32]); - Some(u64::from_be_bytes(buf) as usize) -} - -/// ABI-decode a `bytes` parameter. `param_slot` is the 32-byte slot index -/// (0 for first param, 1 for second, etc.) within `data` (already past the selector). -fn abi_decode_bytes(data: &[u8], param_slot: usize) -> Option> { - let offset = abi_read_offset(data, param_slot * 32)?; - let length = abi_read_offset(data, offset)?; - let start = offset + 32; - if start + length > data.len() { - return None; - } - Some(data[start..start + length].to_vec()) -} - -/// ABI-decode a `string` parameter. -fn abi_decode_string(data: &[u8], param_slot: usize) -> Option { - let bytes = abi_decode_bytes(data, param_slot)?; - String::from_utf8(bytes).ok() -} - -/// ABI-decode a static `int256` parameter as i32. Returns `None` if -/// the value doesn't fit in i32. -fn abi_decode_int256_as_i32(data: &[u8], param_slot: usize) -> Option { - let pos = param_slot * 32; - if pos + 32 > data.len() { - return None; - } - let word = &data[pos..pos + 32]; - let is_negative = word[0] & 0x80 != 0; - let fill = if is_negative { 0xFF } else { 0x00 }; - if word[..28].iter().any(|&b| b != fill) { - return None; - } - let val = i32::from_be_bytes([word[28], word[29], word[30], word[31]]); - if is_negative && val >= 0 { - return None; - } - if !is_negative && val < 0 { - return None; - } - Some(val) -} - -/// ABI-decode a `bytes[]` parameter. -fn abi_decode_bytes_array(data: &[u8], param_slot: usize) -> Option>> { - let array_offset = abi_read_offset(data, param_slot * 32)?; - let count = abi_read_offset(data, array_offset)?; - let mut items = Vec::with_capacity(count); - for i in 0..count { - let elem_rel_offset = abi_read_offset(data, array_offset + 32 + i * 32)?; - let elem_abs = array_offset + 32 + elem_rel_offset; - let elem_len = abi_read_offset(data, elem_abs)?; - let elem_start = elem_abs + 32; - if elem_start + elem_len > data.len() { - return None; - } - items.push(data[elem_start..elem_start + elem_len].to_vec()); - } - Some(items) -} - -// --------------------------------------------------------------------------- -// HDWalletUtils method implementations -// --------------------------------------------------------------------------- - -/// `toBase58Check(bytes hash160, int256 version) → string` -/// -/// Encodes a 20-byte hash with a version byte into a Base58Check string. -fn run_to_base58check(data: &[u8], gas_limit: u64) -> Result { - const GAS_COST: u64 = 13_000; - if gas_limit < GAS_COST { - return Err(PrecompileError::OutOfGas); - } - - let hash160 = abi_decode_bytes(data, 0) - .ok_or_else(|| PrecompileError::other("toBase58Check: cannot decode hash160"))?; - if hash160.len() != 20 { - return Err(PrecompileError::other("toBase58Check: Invalid hash160")); - } - - let version = abi_decode_int256_as_i32(data, 1) - .ok_or_else(|| PrecompileError::other("toBase58Check: cannot decode version"))?; - if !(0..256).contains(&version) { - return Err(PrecompileError::other( - "toBase58Check: version must be a numeric value between 0 and 255", - )); - } - - let mut payload = Vec::with_capacity(21); - payload.push(version as u8); - payload.extend_from_slice(&hash160); - - let encoded = bitcoin::base58::encode_check(&payload); - - Ok(PrecompileOutput::new(GAS_COST, abi_encode_string(&encoded).into())) -} - -/// `deriveExtendedPublicKey(string xpub, string path) → string` -/// -/// Derives a child extended public key along a BIP32 path. -fn run_derive_extended_public_key( - data: &[u8], - gas_limit: u64, -) -> Result { - const GAS_COST: u64 = 107_000; - if gas_limit < GAS_COST { - return Err(PrecompileError::OutOfGas); - } - - let xpub_str = abi_decode_string(data, 0) - .ok_or_else(|| PrecompileError::other("deriveExtendedPublicKey: cannot decode xpub"))?; - let path_str = abi_decode_string(data, 1) - .ok_or_else(|| PrecompileError::other("deriveExtendedPublicKey: cannot decode path"))?; - - let xpub = parse_xpub(&xpub_str)?; - validate_derivation_path(&path_str)?; - - let child_numbers = parse_derivation_path(&path_str)?; - - let secp = bitcoin::secp256k1::Secp256k1::verification_only(); - let mut derived = xpub; - for child in &child_numbers { - derived = derived.ckd_pub(&secp, *child).map_err(|e| { - PrecompileError::other(format!("deriveExtendedPublicKey: derivation failed: {e}")) - })?; - } - - let result_str = derived.to_string(); - - Ok(PrecompileOutput::new(GAS_COST, abi_encode_string(&result_str).into())) -} - -/// `extractPublicKeyFromExtendedPublicKey(string xpub) → bytes` -/// -/// Returns the 33-byte compressed public key from an extended public key. -fn run_extract_pubkey(data: &[u8], gas_limit: u64) -> Result { - const GAS_COST: u64 = 11_300; - if gas_limit < GAS_COST { - return Err(PrecompileError::OutOfGas); - } - - let xpub_str = abi_decode_string(data, 0).ok_or_else(|| { - PrecompileError::other("extractPublicKeyFromExtendedPublicKey: cannot decode xpub") - })?; - - let xpub = parse_xpub(&xpub_str)?; - let pubkey_bytes = xpub.public_key.serialize(); - - Ok(PrecompileOutput::new(GAS_COST, abi_encode_bytes(&pubkey_bytes).into())) -} - -/// `getMultisigScriptHash(int256 minimumSignatures, bytes[] publicKeys) → bytes` -/// -/// Builds a P2SH multisig redeem script and returns RIPEMD160(SHA256(script)). -fn run_get_multisig_script_hash( - data: &[u8], - gas_limit: u64, -) -> Result { - use k256::elliptic_curve::sec1::FromEncodedPoint; - - const BASE_COST: u64 = 20_000; - const COST_PER_EXTRA_KEY: u64 = 700; - - let min_sigs = abi_decode_int256_as_i32(data, 0) - .ok_or_else(|| PrecompileError::other("getMultisigScriptHash: cannot decode minimumSignatures"))?; - - let public_keys = abi_decode_bytes_array(data, 1); - let public_keys = match public_keys { - Some(keys) => keys, - None => { - return Err(PrecompileError::other("getMultisigScriptHash: At least 2 public keys are required")); - } - }; - - let gas_cost = if public_keys.len() >= 2 { - BASE_COST + (public_keys.len() as u64 - 2) * COST_PER_EXTRA_KEY - } else { - BASE_COST - }; - if gas_limit < gas_cost { - return Err(PrecompileError::OutOfGas); - } - - if min_sigs <= 0 { - return Err(PrecompileError::other( - "getMultisigScriptHash: Minimum required signatures must be present and greater than zero", - )); - } - - if public_keys.len() < 2 { - return Err(PrecompileError::other( - "getMultisigScriptHash: At least 2 public keys are required", - )); - } - - if public_keys.len() < min_sigs as usize { - return Err(PrecompileError::other( - "getMultisigScriptHash: public keys are less than the minimum required signatures", - )); - } - - if public_keys.len() > 15 { - return Err(PrecompileError::other( - "getMultisigScriptHash: public keys are more than the maximum allowed signatures", - )); - } - - let mut compressed_keys: Vec<[u8; 33]> = Vec::with_capacity(public_keys.len()); - for key_bytes in &public_keys { - if key_bytes.len() != 33 && key_bytes.len() != 65 { - return Err(PrecompileError::other(format!( - "getMultisigScriptHash: Invalid public key length: {}", - key_bytes.len() - ))); - } - - let prefix = key_bytes[0]; - if prefix != 0x02 && prefix != 0x03 && prefix != 0x04 { - return Err(PrecompileError::other("getMultisigScriptHash: Invalid public key format")); - } - - let compressed = if key_bytes.len() == 33 { - let mut arr = [0u8; 33]; - arr.copy_from_slice(key_bytes); - // Validate the key is on the curve - let encoded = k256::EncodedPoint::from_bytes(key_bytes) - .map_err(|_| PrecompileError::other("getMultisigScriptHash: Invalid public key format"))?; - let pt = k256::AffinePoint::from_encoded_point(&encoded); - if pt.is_none().into() { - return Err(PrecompileError::other("getMultisigScriptHash: Invalid public key format")); - } - arr - } else { - // 65-byte uncompressed: validate and compress - let encoded = k256::EncodedPoint::from_bytes(key_bytes) - .map_err(|_| PrecompileError::other("getMultisigScriptHash: Invalid public key format"))?; - let pt = k256::AffinePoint::from_encoded_point(&encoded); - if pt.is_none().into() { - return Err(PrecompileError::other("getMultisigScriptHash: Invalid public key format")); - } - let affine = pt.unwrap(); - let comp = k256::EncodedPoint::from(affine); - let comp_bytes = comp.compress().to_bytes(); - let mut arr = [0u8; 33]; - arr.copy_from_slice(&comp_bytes); - arr - }; - - compressed_keys.push(compressed); - } - - // Sort keys by compressed bytes (matches bitcoinj's createRedeemScript behavior) - compressed_keys.sort(); - - // Build multisig redeem script: - // OP_m ... OP_n OP_CHECKMULTISIG - let mut script = Vec::new(); - script.push(0x50 + min_sigs as u8); // OP_m - for key in &compressed_keys { - script.push(33); // OP_PUSHBYTES_33 - script.extend_from_slice(key); - } - script.push(0x50 + compressed_keys.len() as u8); // OP_n - script.push(0xAE); // OP_CHECKMULTISIG - - // RIPEMD160(SHA256(script)) - use sha2::Digest as Sha2Digest; - let sha256_hash = sha2::Sha256::digest(&script); - let script_hash = ripemd::Ripemd160::digest(sha256_hash); - - Ok(PrecompileOutput::new(gas_cost, abi_encode_bytes(&script_hash).into())) -} - -/// Parse an xpub/tpub string into a `bitcoin::bip32::Xpub`. -fn parse_xpub(xpub_str: &str) -> Result { - if !xpub_str.starts_with("xpub") && !xpub_str.starts_with("tpub") { - return Err(PrecompileError::other(format!( - "Invalid extended public key '{xpub_str}'" - ))); - } - - xpub_str - .parse::() - .map_err(|e| PrecompileError::other(format!("Invalid extended public key '{xpub_str}': {e}"))) -} - -/// Validate a BIP32 derivation path string per rskj rules: -/// - Non-empty, starts and ends with a digit -/// - No `M` prefix, no hardening `'`, no negative numbers -/// - Each segment < 2^31, at most 10 segments -fn validate_derivation_path(path: &str) -> Result<(), PrecompileError> { - if path.is_empty() { - return Err(PrecompileError::other("Invalid path")); - } - - let first_char = path.chars().next().unwrap(); - let last_char = path.chars().last().unwrap(); - if !first_char.is_ascii_digit() || !last_char.is_ascii_digit() { - return Err(PrecompileError::other("Invalid path")); - } - - let chunks: Vec<&str> = path.split('/').collect(); - if chunks.len() > 10 { - return Err(PrecompileError::other("Path should contain 10 levels at most")); - } - - for chunk in &chunks { - if chunk.contains('\'') || chunk.contains('H') { - return Err(PrecompileError::other("Invalid path")); - } - match chunk.parse::() { - Ok(n) if (0..(1i64 << 31)).contains(&n) => {} - _ => return Err(PrecompileError::other("Invalid path")), - } - } - - Ok(()) -} - -/// Parse a validated path string into ChildNumber values. -fn parse_derivation_path( - path: &str, -) -> Result, PrecompileError> { - path.split('/') - .map(|s| { - let idx: u32 = s - .parse() - .map_err(|_| PrecompileError::other("Invalid path"))?; - bitcoin::bip32::ChildNumber::from_normal_idx(idx) - .map_err(|_| PrecompileError::other("Invalid path")) - }) - .collect() -} - // --------------------------------------------------------------------------- // Precompile set builder // --------------------------------------------------------------------------- @@ -810,35 +282,29 @@ pub fn rsk_precompiles(hardfork_cfg: &RskHardforkConfig, block_number: u64) -> P // RSKIP106 (Wasabi100): HDWalletUtils if upgrade >= RskNetworkUpgrade::Wasabi100 { - precompiles.extend([ - Precompile::new( - PrecompileId::custom("rsk-hdwallet"), - HD_WALLET_UTILS_ADDR, - hd_wallet_utils_run, - ), - ]); + precompiles.extend([Precompile::new( + PrecompileId::custom("rsk-hdwallet"), + HD_WALLET_UTILS_ADDR, + native_contract, + )]); } // RSKIP119 (Wasabi100): BlockHeaderContract if upgrade >= RskNetworkUpgrade::Wasabi100 { - precompiles.extend([ - Precompile::new( - PrecompileId::custom("rsk-blockheader"), - BLOCK_HEADER_ADDR, - block_header_run, - ), - ]); + precompiles.extend([Precompile::new( + PrecompileId::custom("rsk-blockheader"), + BLOCK_HEADER_ADDR, + native_contract, + )]); } // RSKIP203 (Arrowhead600): Environment if upgrade >= RskNetworkUpgrade::Arrowhead600 { - precompiles.extend([ - Precompile::new( - PrecompileId::custom("rsk-environment"), - ENVIRONMENT_ADDR, - environment_run, - ), - ]); + precompiles.extend([Precompile::new( + PrecompileId::custom("rsk-environment"), + ENVIRONMENT_ADDR, + native_contract, + )]); } // RSKIP516 (Reed800): Secp256k1 Addition/Multiplication @@ -898,51 +364,42 @@ pub struct BridgeTxContext { pub tx_index: u32, } -/// RSKIP197 (iris300): the `requiredGas` (rskj `getGasForData`) charged to a -/// failing precompile CALL whose error is a *non-OOG* exception. Post-197 such -/// a call costs exactly this amount (the surplus forwarded gas is refunded and -/// execution continues with a `0` pushed) — `Program.executePrecompiledAndHandleError` -/// runs `refundGas(msg.gas - requiredGas)` in its `finally` block. -/// -/// Two families reach this path. The standard ETH precompiles that revm reports -/// a non-OOG `Err` for at iris: BN128 add/mul/pairing (0x06–0x08, throw on an -/// invalid point post-RSKIP197 — `BN128PrecompiledContract.unsafeExecute`) and -/// BLAKE2F (0x09, throws on a bad final-block byte or bad input length — -/// `Blake2F.execute`). For BN128 the non-OOG `Err` is only returned after revm's -/// internal `gas_used <= gas_limit` check, so `requiredGas <= gas_limit` always -/// holds, matching rskj. +/// The Ethereum precompiles rskj added after genesis. +const BN128_ADD_ADDR: Address = Address::new(hex_addr("0000000000000000000000000000000000000006")); +const BN128_MUL_ADDR: Address = Address::new(hex_addr("0000000000000000000000000000000000000007")); +const BN128_PAIRING_ADDR: Address = + Address::new(hex_addr("0000000000000000000000000000000000000008")); +const BLAKE2F_ADDR: Address = Address::new(hex_addr("0000000000000000000000000000000000000009")); + +/// rskj's `getGasForData` for the precompiles of the set whose `execute` can +/// throw: what a call costs whether it returns or throws, charged before the +/// precompile runs. /// -/// And, from reed800, the RSK secp256k1 precompiles (RSKIP516), which return a -/// non-OOG `Err` whenever a point is off-curve or a coordinate is >= p. rskj -/// charges their fixed cost regardless, because `getGasForData` never looks at -/// the input. +/// BN128 add, mul and pairing (0x06 to 0x08) refuse a point off the curve, +/// and BLAKE2F (0x09) a final-block byte other than 0 or 1, or an input that +/// is not 213 bytes. From reed800 the secp256k1 contracts (RSKIP516) refuse a +/// point off the curve or a coordinate of p or more; their gas never depends +/// on the input. /// -/// Returns `None` only for addresses that genuinely never reach the non-OOG -/// error path; `None` becomes 0 gas at the call site, so an omission here is a -/// silent undercharge rather than a failure. -fn rskip197_required_gas_on_error(addr: &Address, input: &[u8]) -> Option { - // EIP-1108 / Istanbul pricing, active for BN128 from papyrus200 in rskj - // (BN128Addition/Multiplication/Pairing.getGasForData). - const BN128_ADD: Address = Address::new(hex_addr("0000000000000000000000000000000000000006")); - const BN128_MUL: Address = Address::new(hex_addr("0000000000000000000000000000000000000007")); - const BN128_PAIR: Address = Address::new(hex_addr("0000000000000000000000000000000000000008")); - const BLAKE2F: Address = Address::new(hex_addr("0000000000000000000000000000000000000009")); +/// `None` for the precompiles that never throw. The native contracts work +/// out their own ([`crate::native`]). +fn gas_for_data(addr: &Address, input: &[u8]) -> Option { const BN128_PAIR_SIZE: usize = 192; - match *addr { - BN128_ADD => Some(150), - BN128_MUL => Some(6_000), - BN128_PAIR => Some(45_000 + 34_000 * (input.len() / BN128_PAIR_SIZE) as u64), - // Blake2F.getGasForData: malformed length -> 0 (can't read rounds); - // otherwise the big-endian u32 rounds count (rskj F_ROUND == 1). - BLAKE2F if input.len() == 213 => Some(u32::from_be_bytes( - input[..4].try_into().expect("len checked == 213"), - ) as u64), - BLAKE2F => Some(0), - // RSKIP516 (reed800). rskj's Secp256k1Addition/Multiplication return - // their fixed cost from getGasForData REGARDLESS of whether the input - // parses, so an off-curve point still costs 150 / 3000. Omitting them - // here charged 0, because the caller does `.unwrap_or(0)`. + // EIP-1108's prices, which rskj charges from RSKIP137 on. + BN128_ADD_ADDR => Some(150), + BN128_MUL_ADDR => Some(6_000), + BN128_PAIRING_ADDR => Some( + ((input.len() / BN128_PAIR_SIZE) as u64) + .saturating_mul(34_000) + .saturating_add(45_000), + ), + // `Blake2F.getGasForData`: the big-endian rounds count, F_ROUND being + // 1, or 0 when the input is the wrong length to hold one. + BLAKE2F_ADDR => Some(match input.split_first_chunk::<4>() { + Some((rounds, _)) if input.len() == 213 => u64::from(u32::from_be_bytes(*rounds)), + _ => 0, + }), SECP256K1_ADD_ADDR => Some(150), SECP256K1_MUL_ADDR => Some(3_000), _ => None, @@ -959,6 +416,8 @@ pub struct RskPrecompileProvider { hardfork_cfg: RskHardforkConfig, spec: SpecId, block_store: Option>, + /// The executing block's parent, where BlockHeader starts looking. + parent_hash: B256, remasc_config: RemascConfig, bridge_config: BridgeConstants, /// Shared slot for per-transaction context used by Bridge precompile. @@ -984,7 +443,11 @@ impl std::fmt::Debug for RskPrecompileProvider { f.debug_struct("RskPrecompileProvider") .field("hardfork_cfg", &self.hardfork_cfg) .field("spec", &self.spec) - .field("block_store", &self.block_store.as_ref().map(|_| "BlockStore")) + .field( + "block_store", + &self.block_store.as_ref().map(|_| "BlockStore"), + ) + .field("parent_hash", &self.parent_hash) .finish() } } @@ -1037,8 +500,13 @@ impl RskPrecompileProvider { } } -/// Addresses handled via the stateful dispatch path (have access to &mut CTX). -const STATEFUL_PRECOMPILES: [Address; 4] = [ENVIRONMENT_ADDR, BLOCK_HEADER_ADDR, REMASC_ADDR, BRIDGE_ADDR]; +/// REMASC and the Bridge, which run with the whole context: they read and +/// write the state themselves, the Bridge's storage beside revm's. +const STATEFUL_PRECOMPILES: [Address; 2] = [REMASC_ADDR, BRIDGE_ADDR]; + +/// rskj's native contracts (`co.rsk.pcc.NativeContract`), which +/// [`crate::native`] runs. +const NATIVE_CONTRACTS: [Address; 3] = [HD_WALLET_UTILS_ADDR, BLOCK_HEADER_ADDR, ENVIRONMENT_ADDR]; /// Precompiles a transaction may not reach from a parallel sublist. /// @@ -1053,20 +521,12 @@ const STATEFUL_PRECOMPILES: [Address; 4] = [ENVIRONMENT_ADDR, BLOCK_HEADER_ADDR, /// `run_stateful` and nowhere else. pub const SEQUENTIAL_ONLY_PRECOMPILES: [Address; 2] = [REMASC_ADDR, BRIDGE_ADDR]; -/// Maximum ancestor depth supported by BlockHeaderContract (REMASC maturity). -const MAX_BLOCK_DEPTH: i16 = 4000; - -/// RSK merged-mining tag in Bitcoin coinbase transactions. -const RSK_TAG: &[u8] = b"RSKBLOCK:"; - -/// Size of the block header hash embedded after the RSK tag. -const RSK_TAG_HASH_SIZE: usize = 32; - impl RskPrecompileProvider { pub fn new( precompiles: Precompiles, hardfork_cfg: &RskHardforkConfig, block_store: Option>, + parent_hash: B256, remasc_config: RemascConfig, ) -> Self { Self { @@ -1074,6 +534,7 @@ impl RskPrecompileProvider { hardfork_cfg: hardfork_cfg.clone(), spec: SpecId::default(), block_store, + parent_hash, remasc_config, bridge_config: BridgeConstants::mainnet(), bridge_tx_context: std::sync::Arc::new(std::sync::Mutex::new(BridgeTxContext::default())), @@ -1092,7 +553,7 @@ impl RskPrecompileProvider { } - /// Dispatch stateful RSK precompiles that need access to the execution context. + /// REMASC and the Bridge, which read and write the state themselves. /// /// Returns `Ok(Some(result))` if the address was handled, `Ok(None)` to /// fall through to the generic (pure-function) precompile path. @@ -1140,11 +601,7 @@ impl RskPrecompileProvider { context.journal_mut().touch_account(*addr); } - let exec_result = if *addr == ENVIRONMENT_ADDR { - self.run_environment(context, &input_bytes, inputs.gas_limit) - } else if *addr == BLOCK_HEADER_ADDR { - self.run_block_header(context, &input_bytes, inputs.gas_limit) - } else if *addr == BRIDGE_ADDR { + let exec_result = if *addr == BRIDGE_ADDR { self.run_bridge(context, &input_bytes, inputs.gas_limit, inputs.caller, inputs.scheme, inputs.call_value()) } else { self.run_remasc(context, &input_bytes, inputs.gas_limit) @@ -1266,203 +723,6 @@ impl RskPrecompileProvider { Ok(Some(result)) } - /// Environment precompile (0x01000011). - /// Single method: `getCallStackDepth()` → returns the current call depth. - /// Gas: 0. - /// - /// The depth value matches rskj's `programInvoke.getCallDeep() + 1`: - /// in revm, `journal().depth()` already includes the precompile's own - /// call frame, so no adjustment is needed. - fn run_environment( - &self, - context: &mut CTX, - input: &[u8], - _gas_limit: u64, - ) -> Result { - // keccak256("getCallStackDepth()")[0:4] - const GET_CALL_STACK_DEPTH: [u8; 4] = [0xe8, 0xce, 0x22, 0x74]; - - if input.len() < 4 { - return Err(PrecompileError::other( - "Environment: input too short for method selector", - )); - } - - if input[..4] != GET_CALL_STACK_DEPTH { - return Err(PrecompileError::other( - "Environment: unknown method selector", - )); - } - - let depth = context.journal().depth() as u32; - - // ABI-encode as uint32 (32-byte left-padded big-endian) - let mut output = vec![0u8; 32]; - output[28..32].copy_from_slice(&depth.to_be_bytes()); - - Ok(PrecompileOutput::new(0, output.into())) - } - - /// BlockHeaderContract precompile (0x01000010). - /// - /// Exposes block header fields to smart contracts via ABI method dispatch. - /// Gas: 4000 + 2*input.len(). 9 methods, each taking `int256 blockDepth` - /// (depth 0 = parent of executing block) and returning ABI-encoded `bytes`. - fn run_block_header( - &self, - context: &mut CTX, - input: &[u8], - gas_limit: u64, - ) -> Result { - let gas_cost = 4_000u64 + 2 * input.len() as u64; - if gas_limit < gas_cost { - return Err(PrecompileError::OutOfGas); - } - - if input.len() < 4 { - return Err(PrecompileError::other( - "BlockHeader: input too short for method selector", - )); - } - - if std::env::var_os("RUSTOCK_TRACE_BH").is_some() { - tracing::debug!("BH enter input=0x{}", alloy_primitives::hex::encode(input)); - } - let selector = [input[0], input[1], input[2], input[3]]; - let empty_result = || PrecompileOutput::new(gas_cost, abi_encode_bytes(&[]).into()); - - let is_uncle = selector == selector_of("getUncleCoinbaseAddress(int256,int256)"); - - // Parse blockDepth (int256, ABI-encoded starting at input[4]) - let depth = match parse_int256_as_depth(input.get(4..36).unwrap_or(&[])) { - Ok(d) => d, - Err(DepthError::Overflow) => return Ok(empty_result()), - Err(DepthError::Negative) => { - return Err(PrecompileError::other("BlockHeader: negative block depth")) - } - }; - - if depth >= MAX_BLOCK_DEPTH { - return Ok(empty_result()); - } - - let block_store = self.block_store.as_ref().ok_or_else(|| { - PrecompileError::other("BlockHeader: no block store configured") - })?; - - let current_number = context.block().number().to::(); - - // depth 0 → parent (current_number - 1), depth N → current_number - 1 - N - let target_number = current_number - .checked_sub(1) - .and_then(|n| n.checked_sub(depth as u64)); - - let target_number = match target_number { - Some(n) => n, - None => return Ok(empty_result()), - }; - - let hash = block_store - .canonical_hash(target_number) - .map_err(|e| PrecompileError::other(format!("BlockHeader: {e}")))?; - let hash = match hash { - Some(h) => h, - None => return Ok(empty_result()), - }; - - let header = block_store - .header(hash) - .map_err(|e| PrecompileError::other(format!("BlockHeader: {e}")))?; - let header = match header { - Some(h) => h, - None => return Ok(empty_result()), - }; - - let result_bytes: Vec = if selector == selector_of("getCoinbaseAddress(int256)") { - header.beneficiary.as_slice().to_vec() - } else if selector == selector_of("getMinGasPrice(int256)") { - u256_to_java_bigint_bytes(header.minimum_gas_price) - } else if selector == selector_of("getBlockHash(int256)") { - hash.as_slice().to_vec() - } else if selector == selector_of("getGasLimit(int256)") { - u256_to_raw_minimal_bytes(header.gas_limit) - } else if selector == selector_of("getGasUsed(int256)") { - u64_to_java_bigint_bytes(header.gas_used) - } else if selector == selector_of("getDifficulty(int256)") { - u256_to_java_bigint_bytes(header.difficulty) - } else if selector == selector_of("getBitcoinHeader(int256)") { - header - .bitcoin_merged_mining_header - .as_ref() - .map(|b| b.to_vec()) - .unwrap_or_default() - } else if selector == selector_of("getMergedMiningTags(int256)") { - extract_merged_mining_tags(&header) - } else if selector == selector_of("getDifficultyWithUncles(int256)") { - // RSKIP536: block difficulty including uncles - // (rskj Block.getCumulativeDifficulty). - if !self.hardfork_cfg.has_rskip536(current_number) { - return Err(PrecompileError::other("BlockHeader: method not enabled")); - } - let body = block_store - .body(hash) - .map_err(|e| PrecompileError::other(format!("BlockHeader: {e}")))?; - let (_, ommers) = body.unwrap_or_default(); - let cumulative = ommers - .iter() - .fold(header.difficulty, |acc, o| acc.saturating_add(o.difficulty)); - u256_to_java_bigint_bytes(cumulative) - } else if selector == selector_of("getCumulativeWork(int256)") { - // RSKIP536: chain work up to the block - // (rskj blockStore.getTotalDifficultyForHash). - if !self.hardfork_cfg.has_rskip536(current_number) { - return Err(PrecompileError::other("BlockHeader: method not enabled")); - } - match block_store - .total_difficulty(hash) - .map_err(|e| PrecompileError::other(format!("BlockHeader: {e}")))? - { - Some(td) => u256_to_java_bigint_bytes(td), - None => return Ok(empty_result()), - } - } else if is_uncle { - let uncle_idx = match parse_int256_as_depth(input.get(36..68).unwrap_or(&[])) { - Ok(d) => d, - Err(DepthError::Overflow) => return Ok(empty_result()), - Err(DepthError::Negative) => { - return Err(PrecompileError::other( - "BlockHeader: negative uncle index", - )) - } - }; - - let body = block_store - .body(hash) - .map_err(|e| PrecompileError::other(format!("BlockHeader: {e}")))?; - let (_, ommers) = body.unwrap_or_default(); - - if (uncle_idx as usize) >= ommers.len() { - return Ok(empty_result()); - } - - ommers[uncle_idx as usize].beneficiary.as_slice().to_vec() - } else { - return Err(PrecompileError::other( - "BlockHeader: unknown method selector", - )); - }; - - if std::env::var_os("RUSTOCK_TRACE_BH").is_some() { - tracing::debug!( - "BH input=0x{} target=#{} result=0x{}", - alloy_primitives::hex::encode(input), - target_number, - alloy_primitives::hex::encode(&result_bytes), - ); - } - Ok(PrecompileOutput::new(gas_cost, abi_encode_bytes(&result_bytes).into())) - } - /// Bridge precompile (0x01000006). /// BTC–RSK two-way peg with 69 ABI methods. fn run_bridge( @@ -1538,19 +798,118 @@ impl RskPrecompileProvider { "Remasc: invoked outside the block's REMASC system tx", )) } -} -impl Clone for RskPrecompileProvider { - fn clone(&self) -> Self { - Self { - precompiles: self.precompiles.clone(), - hardfork_cfg: self.hardfork_cfg.clone(), - spec: self.spec, - block_store: self.block_store.clone(), - remasc_config: self.remasc_config.clone(), - bridge_config: self.bridge_config.clone(), - bridge_tx_context: std::sync::Arc::clone(&self.bridge_tx_context), - invisible_exception: std::sync::Arc::clone(&self.invisible_exception), + /// One of rskj's native contracts, on the block executing. + fn run_native( + &self, + context: &CTX, + addr: Address, + input: &[u8], + gas_limit: u64, + ) -> Outcome { + match addr { + HD_WALLET_UTILS_ADDR => native::call(&HdWalletUtils, input, gas_limit), + BLOCK_HEADER_ADDR => { + let Some(blocks) = self.block_store.as_deref() else { + return Outcome::Fatal( + "BlockHeader reads a block store, and there is none".into(), + ); + }; + let number = context.block().number().to::(); + let contract = BlockHeader { + blocks, + parent: self.parent_hash, + rskip536: self.hardfork_cfg.has_rskip536(number), + }; + native::call(&contract, input, gas_limit) + } + ENVIRONMENT_ADDR => { + // A precompile's frame is one deeper than its caller's, and the + // transaction's own call is at depth 1, so a contract the + // transaction calls, rskj's depth 0, calls from depth 2. + let caller_depth = context + .journal() + .depth() + .checked_sub(2) + .and_then(|depth| u32::try_from(depth).ok()); + native::call(&Environment { caller_depth }, input, gas_limit) + } + _ => Outcome::Fatal(format!("{addr} is not a native contract")), + } + } + + /// Settles what a precompile did, as rskj's `TransactionExecutor.call` + /// does for a transaction's own call and `Program.callToPrecompiledAddress` + /// for a contract's. + /// + /// A precompile that throws in a transaction's own call is rskj's + /// invisible exception: the transaction succeeds with no output, having + /// used its intrinsic gas and the precompile's, while its sender pays for + /// the whole gas limit and keeps the value. The frame reverts here, so + /// revm hands the value back and forgets the account's touch, and + /// `RskHandler` turns the transaction's result back into a success. + /// + /// In a contract's call, from RSKIP197 a throw costs what the precompile + /// asked for and the call fails. Before it the throw escapes the VM, which + /// spending the call's gas approximates (#295). + fn finish( + &self, + context: &CTX, + addr: Address, + gas_limit: u64, + outcome: Outcome, + ) -> Result { + let mut result = InterpreterResult { + result: InstructionResult::Return, + gas: Gas::new(gas_limit), + output: Bytes::new(), + }; + let gas = match &outcome { + Outcome::Returned { gas, .. } | Outcome::Threw { gas, .. } => *gas, + Outcome::OutOfGas | Outcome::Fatal(_) => 0, + }; + // Held to the limit before the precompile ran, so this always fits. + if !result.gas.record_cost(gas) { + result.result = InstructionResult::PrecompileOOG; + return Ok(result); + } + match outcome { + Outcome::Returned { output, .. } => { + // rskj's setupContract, for a precompile that returns. + self.record_called(addr); + result.output = output; + } + Outcome::OutOfGas => result.result = InstructionResult::PrecompileOOG, + Outcome::Threw { reason, .. } => { + tracing::debug!(target: "rustock::precompile", %addr, %reason, "precompile threw"); + result.result = if context.journal().depth() == 1 { + self.invisible_exception + .store(true, std::sync::atomic::Ordering::Relaxed); + InstructionResult::Revert + } else if self.rskip197_active(context) { + InstructionResult::Revert + } else { + InstructionResult::PrecompileError + }; + } + Outcome::Fatal(reason) => return Err(reason), + } + Ok(result) + } +} + +impl Clone for RskPrecompileProvider { + fn clone(&self) -> Self { + Self { + precompiles: self.precompiles.clone(), + hardfork_cfg: self.hardfork_cfg.clone(), + spec: self.spec, + block_store: self.block_store.clone(), + parent_hash: self.parent_hash, + remasc_config: self.remasc_config.clone(), + bridge_config: self.bridge_config.clone(), + bridge_tx_context: std::sync::Arc::clone(&self.bridge_tx_context), + invisible_exception: std::sync::Arc::clone(&self.invisible_exception), called_precompiles: std::sync::Arc::clone(&self.called_precompiles), reached_sequential_only: std::sync::Arc::clone(&self.reached_sequential_only), } @@ -1574,13 +933,15 @@ impl PrecompileProvider for RskPrecompileProvider context: &mut CTX, inputs: &CallInputs, ) -> Result, String> { - // Stateful RSK precompiles are intercepted first — they need &mut CTX - // for journal depth, block store lookups, or storage access. + // REMASC and the Bridge first, for the whole context. if let Some(result) = self.run_stateful(context, inputs)? { return Ok(Some(result)); } - let Some(precompile) = self.precompiles.get(&inputs.bytecode_address) else { + // The set holds only what is active at this block; before its + // activation a precompile's address is an account like any other. + let addr = inputs.bytecode_address; + let Some(precompile) = self.precompiles.get(&addr) else { return Ok(None); }; @@ -1588,82 +949,33 @@ impl PrecompileProvider for RskPrecompileProvider // stateful path above). { use revm::context_interface::JournalTr; - let _ = context.journal_mut().load_account(inputs.bytecode_address); - context.journal_mut().touch_account(inputs.bytecode_address); + let _ = context.journal_mut().load_account(addr); + context.journal_mut().touch_account(addr); } - let mut result = InterpreterResult { - result: InstructionResult::Return, - gas: Gas::new(inputs.gas_limit), - output: Bytes::new(), - }; - - // RSKIP197 (iris300): `requiredGas` charged to this call if the - // precompile fails with a non-OOG error (computed from the input while - // it is still borrowed). Captured here so the error branch below can - // refund the surplus forwarded gas, matching rskj's - // `executePrecompiledAndHandleError` (gasUsed == getGasForData). - let required_gas_on_error; - let exec_result = { - let r; - let input_bytes = match &inputs.input { + let rskip197 = self.rskip197_active(context); + let outcome = { + let shared; + let input = match &inputs.input { CallInput::SharedBuffer(range) => { - if let Some(slice) = context.local().shared_memory_buffer_slice(range.clone()) { - r = slice; - r.as_ref() - } else { - &[] + match context.local().shared_memory_buffer_slice(range.clone()) { + Some(slice) => { + shared = slice; + shared.as_ref() + } + None => &[], } } - CallInput::Bytes(bytes) => bytes.0.iter().as_slice(), + CallInput::Bytes(bytes) => bytes.as_ref(), }; - required_gas_on_error = - rskip197_required_gas_on_error(&inputs.bytecode_address, input_bytes) - .unwrap_or(0); - precompile.execute(input_bytes, inputs.gas_limit) - }; - - match exec_result { - Ok(output) => { - result.gas.record_refund(output.gas_refunded); - let underflow = result.gas.record_cost(output.gas_used); - assert!(underflow, "Gas underflow is not possible"); - result.result = if output.reverted { - InstructionResult::Revert - } else { - // rskj setupContract on successful precompile calls. - self.record_called(inputs.bytecode_address); - InstructionResult::Return - }; - result.output = output.bytes; - } - Err(PrecompileError::Fatal(e)) => return Err(e), - Err(e) => { - if e.is_oog() { - result.result = InstructionResult::PrecompileOOG; - } else if self.rskip197_active(context) { - // RSKIP197 (iris300): a non-OOG precompile failure is - // handled — the call pushes 0 and continues, costing only - // `requiredGas` (the surplus forwarded gas is refunded). - // Modeled as a Revert with empty output: revm refunds the - // surplus (`is_ok_or_revert`) and pushes 0 (`!is_ok`), - // leaving the returnDataBuffer empty — matching rskj's - // `executePrecompiledAndHandleError` - // (returnDataBuffer = null, refundGas(msg.gas - requiredGas)). - let no_underflow = result.gas.record_cost(required_gas_on_error); - debug_assert!(no_underflow, "requiredGas <= gas_limit post-RSKIP197"); - result.result = InstructionResult::Revert; - } else { - result.result = InstructionResult::PrecompileError; - if context.journal().depth() == 1 { - context - .local_mut() - .set_precompile_error_context(e.to_string()); - } - } + if NATIVE_CONTRACTS.contains(&addr) { + self.run_native(context, addr, input, inputs.gas_limit) + } else { + run_precompile(precompile, &addr, input, inputs.gas_limit, rskip197) } - } - Ok(Some(result)) + }; + self.finish(context, addr, inputs.gas_limit, outcome) + .map(Some) } fn warm_addresses(&self) -> Box> { @@ -1675,6 +987,57 @@ impl PrecompileProvider for RskPrecompileProvider } } +/// A precompile of the set that is not one of rskj's native contracts, run as +/// rskj runs it: what `getGasForData` asks for is held to the gas limit +/// before it runs, and an input it refuses makes it throw. +fn run_precompile( + precompile: &Precompile, + addr: &Address, + input: &[u8], + gas_limit: u64, + rskip197: bool, +) -> Outcome { + let required = gas_for_data(addr, input); + if required.is_some_and(|required| required > gas_limit) { + return Outcome::OutOfGas; + } + match precompile.execute(input, gas_limit) { + Ok(output) => Outcome::Returned { + gas: output.gas_used, + output: output.bytes, + }, + Err(PrecompileError::Fatal(reason)) => Outcome::Fatal(reason), + // rskj's ModExp reads a length no int holds as `Integer.MAX_VALUE` + // (`DataWord.intValueSafe`), which prices the call above 10^16 gas. + Err(error) + if error.is_oog() || matches!(error, PrecompileError::ModexpEip7823LimitSize) => + { + Outcome::OutOfGas + } + Err(error) => match required { + // `BN128PrecompiledContract.safeExecute`: before RSKIP197 a point + // BN128 refuses gets an answer with no bytes. + Some(gas) + if !rskip197 + && [BN128_ADD_ADDR, BN128_MUL_ADDR, BN128_PAIRING_ADDR].contains(addr) => + { + Outcome::Returned { + gas, + output: Bytes::new(), + } + } + Some(gas) => Outcome::Threw { + gas, + reason: error.to_string(), + }, + // Every precompile that can throw has its price in `gas_for_data`. + None => Outcome::Fatal(format!( + "precompile {addr} threw, and rustock has no price for it: {error}" + )), + }, + } +} + /// Helper to check if an address is an RSK precompile at a given block. pub fn is_rsk_precompile( address: &Address, @@ -1719,35 +1082,22 @@ mod tests { use super::*; // ----------------------------------------------------------------------- - // RSKIP197 requiredGas on a non-OOG precompile error + // getGasForData, and what a precompile that throws comes to // ----------------------------------------------------------------------- - /// Audit FRCR-280. rskj charges a failing precompile CALL exactly - /// `getGasForData(input)` post-RSKIP197, and the secp256k1 contracts - /// return their flat cost from `getGasForData` without ever looking at the - /// input (`Secp256k1Addition.java:27` EC_256_ADDITION_GAS_COST = 150, - /// `Secp256k1Multiplication.java:27` = 3000). Before the fix these two - /// addresses fell through to `None`, which `.unwrap_or(0)` turned into a - /// free failed call — a consensus divergence from reed800 onwards. + /// Audit FRCR-280. rskj charges a precompile call `getGasForData(input)`, + /// whether it returns or throws, and the secp256k1 contracts return their + /// flat cost from `getGasForData` without ever looking at the input + /// (`Secp256k1Addition.java:27` EC_256_ADDITION_GAS_COST = 150, + /// `Secp256k1Multiplication.java:27` = 3000). Missing from the table, a + /// call that throws would be free: a consensus divergence from reed800 + /// onwards. #[test] - fn rskip197_required_gas_covers_secp256k1() { - assert_eq!( - rskip197_required_gas_on_error(&SECP256K1_ADD_ADDR, &[]), - Some(150) - ); - assert_eq!( - rskip197_required_gas_on_error(&SECP256K1_MUL_ADDR, &[]), - Some(3_000) - ); - // Independent of the input, exactly as getGasForData is. - assert_eq!( - rskip197_required_gas_on_error(&SECP256K1_ADD_ADDR, &[0xff; 128]), - Some(150) - ); - assert_eq!( - rskip197_required_gas_on_error(&SECP256K1_MUL_ADDR, &[0xff; 96]), - Some(3_000) - ); + fn the_secp256k1_contracts_cost_the_same_whatever_their_input() { + assert_eq!(gas_for_data(&SECP256K1_ADD_ADDR, &[]), Some(150)); + assert_eq!(gas_for_data(&SECP256K1_MUL_ADDR, &[]), Some(3_000)); + assert_eq!(gas_for_data(&SECP256K1_ADD_ADDR, &[0xff; 128]), Some(150)); + assert_eq!(gas_for_data(&SECP256K1_MUL_ADDR, &[0xff; 96]), Some(3_000)); } /// The failure above is only reachable if an off-curve point really does @@ -1770,35 +1120,105 @@ mod tests { assert!(!err.is_oog(), "must not be OOG, got {err:?}"); } - /// The other addresses that reach the same path, pinned together so the - /// table is read as a whole. rskj: `BN128Addition.getGasForData` = 150, + /// The other precompiles that can throw, pinned together so the table is + /// read as a whole. rskj: `BN128Addition.getGasForData` = 150, /// `BN128Multiplication` = 6000, `BN128Pairing` = 45000 + 34000*pairs, /// `Blake2F` = the big-endian rounds word (0 when the length is wrong). #[test] - fn rskip197_required_gas_bn128_and_blake2f() { - let bn128_add = Address::new(hex_addr("0000000000000000000000000000000000000006")); - let bn128_mul = Address::new(hex_addr("0000000000000000000000000000000000000007")); - let bn128_pair = Address::new(hex_addr("0000000000000000000000000000000000000008")); - let blake2f = Address::new(hex_addr("0000000000000000000000000000000000000009")); - - assert_eq!(rskip197_required_gas_on_error(&bn128_add, &[]), Some(150)); - assert_eq!(rskip197_required_gas_on_error(&bn128_mul, &[]), Some(6_000)); + fn bn128_and_blake2f_cost_what_rskj_charges() { + assert_eq!(gas_for_data(&BN128_ADD_ADDR, &[]), Some(150)); + assert_eq!(gas_for_data(&BN128_MUL_ADDR, &[]), Some(6_000)); assert_eq!( - rskip197_required_gas_on_error(&bn128_pair, &[0u8; 384]), + gas_for_data(&BN128_PAIRING_ADDR, &[0u8; 384]), Some(45_000 + 2 * 34_000) ); let mut blake_input = [0u8; 213]; blake_input[..4].copy_from_slice(&12u32.to_be_bytes()); - assert_eq!( - rskip197_required_gas_on_error(&blake2f, &blake_input), - Some(12) - ); - assert_eq!(rskip197_required_gas_on_error(&blake2f, &[0u8; 100]), Some(0)); + assert_eq!(gas_for_data(&BLAKE2F_ADDR, &blake_input), Some(12)); + assert_eq!(gas_for_data(&BLAKE2F_ADDR, &[0u8; 100]), Some(0)); - // ecrecover never throws a non-OOG error in rskj; it returns empty. + // ecrecover never throws in rskj; it returns empty. let ecrecover = Address::new(hex_addr("0000000000000000000000000000000000000001")); - assert_eq!(rskip197_required_gas_on_error(&ecrecover, &[]), None); + assert_eq!(gas_for_data(&ecrecover, &[]), None); + } + + /// What `run_precompile` makes of each kind of refusal: rskj's throw, for + /// what `getGasForData` asked; for BN128 before RSKIP197, an answer with + /// no bytes; and for a modexp length no int holds, running out of gas. + #[test] + fn a_precompile_that_refuses_its_input_throws_for_its_price() { + let set = rsk_precompiles(&RskHardforkConfig::all_active(33), 0); + let run = |addr: Address, input: &[u8], gas: u64, rskip197: bool| { + run_precompile(set.get(&addr).expect("active"), &addr, input, gas, rskip197) + }; + // (1, 1) is on neither BN128's curve nor secp256k1's. + let mut off_curve = [0u8; 128]; + off_curve[31] = 1; + off_curve[63] = 1; + assert!(matches!( + run(BN128_ADD_ADDR, &off_curve, 1_000, true), + Outcome::Threw { gas: 150, .. } + )); + assert_eq!( + run(BN128_ADD_ADDR, &off_curve, 1_000, false), + Outcome::Returned { + gas: 150, + output: Bytes::new() + } + ); + assert_eq!( + run(BN128_ADD_ADDR, &off_curve, 149, true), + Outcome::OutOfGas + ); + assert!(matches!( + run(BN128_MUL_ADDR, &off_curve[..96], 6_000, true), + Outcome::Threw { gas: 6_000, .. } + )); + assert_eq!( + run(BN128_MUL_ADDR, &off_curve[..96], 5_999, true), + Outcome::OutOfGas + ); + // A pairing input that is not whole pairs. + assert!(matches!( + run(BN128_PAIRING_ADDR, &[0; 100], 1_000_000, true), + Outcome::Threw { gas: 45_000, .. } + )); + // BLAKE2F's input is 213 bytes, or it costs nothing and throws. + assert!(matches!( + run(BLAKE2F_ADDR, &[0; 100], 1_000, true), + Outcome::Threw { gas: 0, .. } + )); + let mut blake = [0u8; 213]; + blake[3] = 12; + blake[212] = 2; + assert!( + matches!( + run(BLAKE2F_ADDR, &blake, 12, true), + Outcome::Threw { gas: 12, .. } + ), + "a bad final-block flag" + ); + assert_eq!(run(BLAKE2F_ADDR, &blake, 11, true), Outcome::OutOfGas); + assert!(matches!( + run(SECP256K1_ADD_ADDR, &off_curve, 1_000, true), + Outcome::Threw { gas: 150, .. } + )); + // A base length of 2^248. + let mut huge = [0u8; 96]; + huge[0] = 1; + assert_eq!( + run(Address::with_last_byte(5), &huge, 1_000_000, true), + Outcome::OutOfGas + ); + // What a precompile answers comes through as it is. + assert_eq!( + run(Address::with_last_byte(4), b"abc", 1_000, true), + Outcome::Returned { + gas: 18, + output: Bytes::from_static(b"abc") + } + ); } #[test] @@ -1971,12 +1391,6 @@ mod tests { assert!(matches!(oog, Err(PrecompileError::OutOfGas))); } - #[test] - fn test_block_header_gas_scales_with_input() { - let result = block_header_run(&[0u8; 100], 100_000).unwrap(); - assert_eq!(result.gas_used, 4_000 + 200); // 4000 + 2*100 - } - #[test] fn test_secp256k1_gas_costs() { let add = secp256k1_add_run(&[], 1_000).unwrap(); @@ -2265,11 +1679,11 @@ mod tests { #[test] fn test_stateful_precompiles_list() { - assert!(STATEFUL_PRECOMPILES.contains(&ENVIRONMENT_ADDR)); - assert!(STATEFUL_PRECOMPILES.contains(&BLOCK_HEADER_ADDR)); - assert!(STATEFUL_PRECOMPILES.contains(&REMASC_ADDR)); - assert!(STATEFUL_PRECOMPILES.contains(&BRIDGE_ADDR)); - assert!(!STATEFUL_PRECOMPILES.contains(&SECP256K1_ADD_ADDR)); + assert_eq!(STATEFUL_PRECOMPILES, [REMASC_ADDR, BRIDGE_ADDR]); + assert_eq!( + NATIVE_CONTRACTS, + [HD_WALLET_UTILS_ADDR, BLOCK_HEADER_ADDR, ENVIRONMENT_ADDR] + ); } #[test] @@ -2286,7 +1700,13 @@ mod tests { fn test_provider_contains_stateful_addresses() { let cfg = RskHardforkConfig::all_active(33); let precompiles = rsk_precompiles(&cfg, 0); - let provider = RskPrecompileProvider::new(precompiles, &cfg, None, RemascConfig::regtest()); + let provider = RskPrecompileProvider::new( + precompiles, + &cfg, + None, + B256::ZERO, + RemascConfig::regtest(), + ); assert!(provider.precompiles.contains(&ENVIRONMENT_ADDR)); assert!(provider.precompiles.contains(&BLOCK_HEADER_ADDR)); @@ -2294,941 +1714,4 @@ mod tests { assert!(provider.precompiles.contains(&BRIDGE_ADDR)); assert!(provider.precompiles.contains(&SECP256K1_ADD_ADDR)); } - - // ----------------------------------------------------------------------- - // Environment precompile tests - // ----------------------------------------------------------------------- - - #[test] - fn test_environment_selector_is_keccak256() { - use sha3::{Digest, Keccak256}; - let hash = Keccak256::digest(b"getCallStackDepth()"); - assert_eq!( - &hash[..4], - &[0xe8, 0xce, 0x22, 0x74], - "selector should be keccak256('getCallStackDepth()')[0:4]" - ); - } - - // ----------------------------------------------------------------------- - // BlockHeaderContract helper tests - // ----------------------------------------------------------------------- - - #[test] - fn test_block_header_selectors_are_consistent() { - use sha3::{Digest, Keccak256}; - - let sigs = [ - "getCoinbaseAddress(int256)", - "getMinGasPrice(int256)", - "getBlockHash(int256)", - "getMergedMiningTags(int256)", - "getGasLimit(int256)", - "getGasUsed(int256)", - "getDifficulty(int256)", - "getBitcoinHeader(int256)", - "getUncleCoinbaseAddress(int256,int256)", - ]; - - for sig in &sigs { - let hash = Keccak256::digest(sig.as_bytes()); - let expected = [hash[0], hash[1], hash[2], hash[3]]; - assert_eq!( - selector_of(sig), expected, - "selector_of({sig}) should match keccak256" - ); - } - - // All selectors must be distinct - let sels: Vec<[u8; 4]> = sigs.iter().map(|s| selector_of(s)).collect(); - for i in 0..sels.len() { - for j in (i + 1)..sels.len() { - assert_ne!(sels[i], sels[j], "selectors for {} and {} collide", sigs[i], sigs[j]); - } - } - } - - #[test] - fn test_abi_encode_bytes_empty() { - // ethereumj BytesType.encode pads EMPTY bytes to one full zero word - // ((0-1)/32+1 == 1 in Java), so the encoding is 96 bytes, not the - // canonical 64 (offset + length + one padding word). - let encoded = abi_encode_bytes(&[]); - assert_eq!(encoded.len(), 96); - // offset = 0x20 - assert_eq!(encoded[31], 0x20); - // length = 0, padding word = 0 - assert!(encoded[32..96].iter().all(|&b| b == 0)); - } - - #[test] - fn test_abi_encode_bytes_20_bytes() { - let data = [0xAA; 20]; - let encoded = abi_encode_bytes(&data); - // 32 (offset) + 32 (length) + 32 (padded data) = 96 - assert_eq!(encoded.len(), 96); - assert_eq!(encoded[31], 0x20); - assert_eq!(encoded[63], 20); // length - assert_eq!(&encoded[64..84], &[0xAA; 20]); - assert!(encoded[84..96].iter().all(|&b| b == 0)); // padding - } - - #[test] - fn test_abi_encode_bytes_32_bytes() { - let data = [0xBB; 32]; - let encoded = abi_encode_bytes(&data); - assert_eq!(encoded.len(), 96); - assert_eq!(encoded[31], 0x20); - assert_eq!(encoded[63], 32); - assert_eq!(&encoded[64..96], &[0xBB; 32]); - } - - #[test] - fn test_u256_to_java_bigint_bytes_zero() { - assert_eq!(u256_to_java_bigint_bytes(U256::ZERO), vec![0]); - } - - #[test] - fn test_u256_to_java_bigint_bytes_small() { - assert_eq!(u256_to_java_bigint_bytes(U256::from(127u64)), vec![0x7F]); - } - - #[test] - fn test_u256_to_java_bigint_bytes_needs_sign_byte() { - // 128 = 0x80, high bit set → needs leading 0x00 - assert_eq!(u256_to_java_bigint_bytes(U256::from(128u64)), vec![0x00, 0x80]); - } - - #[test] - fn test_u256_to_java_bigint_bytes_256() { - assert_eq!(u256_to_java_bigint_bytes(U256::from(256u64)), vec![0x01, 0x00]); - } - - #[test] - fn test_u64_to_java_bigint_bytes_gas_used() { - // gas_used = 0 → [0] - assert_eq!(u64_to_java_bigint_bytes(0), vec![0]); - // gas_used = 21000 = 0x5208 - assert_eq!(u64_to_java_bigint_bytes(21000), vec![0x52, 0x08]); - } - - #[test] - fn test_parse_int256_as_depth_zero() { - let data = [0u8; 32]; - assert_eq!(parse_int256_as_depth(&data).unwrap(), 0i16); - } - - #[test] - fn test_parse_int256_as_depth_positive() { - let mut data = [0u8; 32]; - data[31] = 5; - assert_eq!(parse_int256_as_depth(&data).unwrap(), 5i16); - } - - #[test] - fn test_parse_int256_as_depth_max_valid() { - let mut data = [0u8; 32]; - // 32767 = 0x7FFF - data[30] = 0x7F; - data[31] = 0xFF; - assert_eq!(parse_int256_as_depth(&data).unwrap(), 32767i16); - } - - #[test] - fn test_parse_int256_as_depth_overflow() { - let mut data = [0u8; 32]; - // 32768 = 0x8000 — doesn't fit in i16 - data[30] = 0x80; - data[31] = 0x00; - assert!(matches!(parse_int256_as_depth(&data), Err(DepthError::Overflow))); - } - - #[test] - fn test_parse_int256_as_depth_large_overflow() { - let mut data = [0u8; 32]; - data[0] = 0x01; // large positive number - assert!(matches!(parse_int256_as_depth(&data), Err(DepthError::Overflow))); - } - - #[test] - fn test_parse_int256_as_depth_negative() { - // -1 in int256 = all 0xFF - let data = [0xFF; 32]; - assert!(matches!(parse_int256_as_depth(&data), Err(DepthError::Negative))); - } - - #[test] - fn test_parse_int256_as_depth_empty_input() { - assert_eq!(parse_int256_as_depth(&[]).unwrap(), 0i16); - } - - #[test] - fn test_extract_merged_mining_tags() { - use rustock_core::Header; - use alloy_primitives::{B256, Bloom, Bytes as ABytes}; - - let mut header = Header { - parent_hash: B256::ZERO, ommers_hash: B256::ZERO, - beneficiary: Address::ZERO, state_root: B256::ZERO, - transactions_root: B256::ZERO, receipts_root: B256::ZERO, - logs_bloom: Bloom::ZERO, extension_data: None, - difficulty: U256::ZERO, number: 0, gas_limit: U256::ZERO, - gas_used: 0, timestamp: 0, extra_data: ABytes::new(), - paid_fees: U256::ZERO, minimum_gas_price: U256::ZERO, - uncle_count: 0, umm_root: None, version_fields: None, - bitcoin_merged_mining_header: None, - bitcoin_merged_mining_merkle_proof: None, - bitcoin_merged_mining_coinbase_transaction: None, - cached_hash: None, cached_hash_for_merged_mining: None, - }; - - // No coinbase tx → empty - assert!(extract_merged_mining_tags(&header).is_empty()); - - // Coinbase tx without RSK tag → empty - header.bitcoin_merged_mining_coinbase_transaction = Some(ABytes::from(vec![1, 2, 3])); - assert!(extract_merged_mining_tags(&header).is_empty()); - - // Coinbase tx with RSK tag + 32-byte hash + additional tags - let mut coinbase = Vec::new(); - coinbase.extend_from_slice(b"prefix"); - coinbase.extend_from_slice(RSK_TAG); - coinbase.extend_from_slice(&[0xAA; 32]); // block header hash - coinbase.extend_from_slice(b"ALTBLOCK:extra"); - header.bitcoin_merged_mining_coinbase_transaction = Some(ABytes::from(coinbase)); - - let tags = extract_merged_mining_tags(&header); - assert_eq!(tags, b"ALTBLOCK:extra"); - } - - // ----------------------------------------------------------------------- - // Java BigInteger / ABI compatibility tests using rskj constant values - // ----------------------------------------------------------------------- - - /// Verify u256_to_java_bigint_bytes matches Java's BigInteger.toByteArray() - /// for the exact constants used in rskj BlockHeaderContractTest. - #[test] - fn test_java_bigint_rskj_min_gas_price() { - // rskj: MIN_GAS_PRICE = new BigInteger("500000000000000000") - // 500000000000000000 = 0x06F05B59D3B20000 - // BigInteger.toByteArray() → [0x06, 0xF0, 0x5B, 0x59, 0xD3, 0xB2, 0x00, 0x00] - // (0x06 high bit clear → no leading zero needed) - let val = U256::from(500_000_000_000_000_000u64); - let bytes = u256_to_java_bigint_bytes(val); - assert_eq!( - bytes, - vec![0x06, 0xF0, 0x5B, 0x59, 0xD3, 0xB2, 0x00, 0x00], - "must match Java BigInteger.toByteArray() for 500000000000000000" - ); - // Verify round-trip: these bytes interpreted as big-endian unsigned = original - let mut padded = [0u8; 32]; - padded[32 - bytes.len()..].copy_from_slice(&bytes); - assert_eq!(U256::from_be_bytes(padded), val); - } - - #[test] - fn test_gas_limit_raw_bytes_no_sign_prefix() { - // rskj GetGasLimit returns the raw header bytes: 10,000,000 = - // 0x989680 (high bit of 0x98 set) stays 3 bytes — the signed - // BigInteger helper would prepend 0x00. - assert_eq!( - u256_to_raw_minimal_bytes(U256::from(10_000_000u64)), - vec![0x98, 0x96, 0x80] - ); - assert_eq!( - u256_to_java_bigint_bytes(U256::from(10_000_000u64)), - vec![0x00, 0x98, 0x96, 0x80] - ); - // 6,800,000 = 0x67C280: both forms agree (high bit clear). - assert_eq!( - u256_to_raw_minimal_bytes(U256::from(6_800_000u64)), - vec![0x67, 0xC2, 0x80] - ); - // Zero: raw RLP element is empty (vs BigInteger's [0x00]). - assert_eq!(u256_to_raw_minimal_bytes(U256::ZERO), Vec::::new()); - } - - #[test] - fn test_java_bigint_rskj_gas_limit() { - // rskj: GAS_LIMIT = new BigInteger("3000000") - // 3000000 = 0x2DC6C0 - // BigInteger.toByteArray() → [0x2D, 0xC6, 0xC0] - let val = U256::from(3_000_000u64); - let bytes = u256_to_java_bigint_bytes(val); - assert_eq!( - bytes, - vec![0x2D, 0xC6, 0xC0], - "must match Java BigInteger.toByteArray() for 3000000" - ); - } - - #[test] - fn test_java_bigint_rskj_difficulty() { - // rskj: RSK_DIFFICULTY = new BigInteger("1") - // BigInteger.toByteArray() → [0x01] - let val = U256::from(1u64); - let bytes = u256_to_java_bigint_bytes(val); - assert_eq!(bytes, vec![0x01], "must match Java BigInteger.toByteArray() for 1"); - } - - #[test] - fn test_java_bigint_rskj_gas_used_zero() { - // rskj: GAS_USED = new BigInteger("0") - // BigInteger.toByteArray() → [0x00] - let bytes = u64_to_java_bigint_bytes(0); - assert_eq!(bytes, vec![0x00], "must match Java BigInteger.toByteArray() for 0"); - } - - #[test] - fn test_java_bigint_high_bit_boundary() { - // 0x80 = 128 → BigInteger.toByteArray() → [0x00, 0x80] - // (high bit set → leading zero to keep positive) - assert_eq!(u256_to_java_bigint_bytes(U256::from(128u64)), vec![0x00, 0x80]); - // 0x7F = 127 → [0x7F] (high bit clear → no leading zero) - assert_eq!(u256_to_java_bigint_bytes(U256::from(127u64)), vec![0x7F]); - // 0xFF = 255 → [0x00, 0xFF] - assert_eq!(u256_to_java_bigint_bytes(U256::from(255u64)), vec![0x00, 0xFF]); - // 0x100 = 256 → [0x01, 0x00] - assert_eq!(u256_to_java_bigint_bytes(U256::from(256u64)), vec![0x01, 0x00]); - // 0x7FFF = 32767 → [0x7F, 0xFF] - assert_eq!(u256_to_java_bigint_bytes(U256::from(32767u64)), vec![0x7F, 0xFF]); - // 0x8000 = 32768 → [0x00, 0x80, 0x00] - assert_eq!(u256_to_java_bigint_bytes(U256::from(32768u64)), vec![0x00, 0x80, 0x00]); - } - - /// Verify the full ABI-encoded output for getCoinbaseAddress matches - /// what Solidity's `abi.encode(bytes(addr))` would produce. - /// - /// For a 20-byte address 0x3333...33, Solidity abi.encode(bytes) is: - /// offset: 0x0000...0020 (32 bytes) - /// length: 0x0000...0014 (32 bytes, 20 decimal) - /// data: 3333333333333333333333333333333333333333 + 12 zero bytes - /// Total: 96 bytes. - #[test] - fn test_abi_encode_bytes_solidity_compat_address() { - let addr = [0x33u8; 20]; - let encoded = abi_encode_bytes(&addr); - - let mut expected = vec![0u8; 96]; - // offset = 0x20 - expected[31] = 0x20; - // length = 0x14 (20) - expected[63] = 0x14; - // data = 20 bytes of 0x33 - expected[64..84].fill(0x33); - // remaining 12 bytes are zero (padding) - - assert_eq!(encoded, expected, "ABI encoding of 20-byte address must match Solidity spec"); - } - - /// Verify ABI encoding of a 32-byte hash matches Solidity spec. - #[test] - fn test_abi_encode_bytes_solidity_compat_hash() { - let hash = [0xAB; 32]; - let encoded = abi_encode_bytes(&hash); - - let mut expected = vec![0u8; 96]; - expected[31] = 0x20; - expected[63] = 0x20; // length = 32 - expected[64..96].fill(0xAB); - - assert_eq!(encoded, expected, "ABI encoding of 32-byte hash must match Solidity spec"); - } - - /// Verify ABI encoding of empty bytes matches ethereumj (NOT canonical - /// Solidity): offset=0x20, length=0, plus one zero padding word. - #[test] - fn test_abi_encode_bytes_solidity_compat_empty() { - let encoded = abi_encode_bytes(&[]); - - let mut expected = vec![0u8; 96]; - expected[31] = 0x20; - // length = 0, padding word = 0 (all zeros) - - assert_eq!(encoded, expected, "empty bytes must encode ethereumj-style to 96 bytes"); - } - - /// Verify ABI encoding of Java BigInteger bytes for rskj's MIN_GAS_PRICE. - /// This tests the full pipeline: value → Java BigInt bytes → ABI encode. - #[test] - fn test_full_abi_output_rskj_min_gas_price() { - let min_gas_price = U256::from(500_000_000_000_000_000u64); - let java_bytes = u256_to_java_bigint_bytes(min_gas_price); - let abi_output = abi_encode_bytes(&java_bytes); - - // java_bytes = [0x06, 0xF0, 0x5B, 0x59, 0xD3, 0xB2, 0x00, 0x00] (8 bytes) - // ABI: offset(32) + length(32) + padded_data(32) = 96 bytes - assert_eq!(abi_output.len(), 96); - - // offset - assert_eq!(&abi_output[0..32], &{ - let mut v = [0u8; 32]; v[31] = 0x20; v - }); - // length = 8 - assert_eq!(&abi_output[32..64], &{ - let mut v = [0u8; 32]; v[31] = 0x08; v - }); - // data (8 bytes) + 24 zero-padding - assert_eq!(&abi_output[64..72], &[0x06, 0xF0, 0x5B, 0x59, 0xD3, 0xB2, 0x00, 0x00]); - assert!(abi_output[72..96].iter().all(|&b| b == 0)); - - // Verify rskj round-trip: decode back to BigInteger value - let decoded_len = u64::from_be_bytes(abi_output[56..64].try_into().unwrap()) as usize; - let decoded_bytes = &abi_output[64..64 + decoded_len]; - let mut padded = [0u8; 32]; - padded[32 - decoded_bytes.len()..].copy_from_slice(decoded_bytes); - let decoded_val = U256::from_be_bytes(padded); - assert_eq!(decoded_val, min_gas_price, "round-trip must recover original value"); - } - - // ----------------------------------------------------------------------- - // HDWalletUtils tests - // ----------------------------------------------------------------------- - - #[test] - fn test_hdwallet_selectors_are_distinct() { - let sigs = [ - "toBase58Check(bytes,int256)", - "deriveExtendedPublicKey(string,string)", - "extractPublicKeyFromExtendedPublicKey(string)", - "getMultisigScriptHash(int256,bytes[])", - ]; - let sels: Vec<[u8; 4]> = sigs.iter().map(|s| selector_of(s)).collect(); - for i in 0..sels.len() { - for j in (i + 1)..sels.len() { - assert_ne!(sels[i], sels[j], "selectors for {} and {} collide", sigs[i], sigs[j]); - } - } - } - - #[test] - fn test_hdwallet_unknown_selector_fails() { - let input = [0xFF, 0xFF, 0xFF, 0xFF, 0x00]; - assert!(hd_wallet_utils_run(&input, 200_000).is_err()); - } - - #[test] - fn test_hdwallet_too_short_input_fails() { - assert!(hd_wallet_utils_run(&[0x00, 0x01], 200_000).is_err()); - } - - // --- ABI decode helpers --- - - #[test] - fn test_abi_decode_int256_as_i32() { - // Zero - let mut data = [0u8; 32]; - assert_eq!(abi_decode_int256_as_i32(&data, 0), Some(0)); - - // Positive: 111 - data[31] = 111; - assert_eq!(abi_decode_int256_as_i32(&data, 0), Some(111)); - - // Negative: -1 - let neg1 = [0xFF; 32]; - assert_eq!(abi_decode_int256_as_i32(&neg1, 0), Some(-1)); - - // Too large for i32 (overflow) - let mut big = [0u8; 32]; - big[0] = 0x01; - assert_eq!(abi_decode_int256_as_i32(&big, 0), None); - } - - #[test] - fn test_abi_decode_bytes_roundtrip() { - let original = b"hello world"; - let encoded = abi_encode_bytes(original); - // abi_decode_bytes expects: offset at slot 0 → data starts at offset - // Our encoded has offset=0x20, length=11, data at 64.. - // But abi_decode_bytes reads the offset from slot 0, then follows it - let decoded = abi_decode_bytes(&encoded, 0).unwrap(); - assert_eq!(decoded, original); - } - - #[test] - fn test_abi_decode_string_roundtrip() { - let original = "test string"; - let encoded = abi_encode_string(original); - let decoded = abi_decode_string(&encoded, 0).unwrap(); - assert_eq!(decoded, original); - } - - // --- toBase58Check tests --- - - fn build_to_base58check_input(hash160: &[u8], version: i32) -> Vec { - let selector = selector_of("toBase58Check(bytes,int256)"); - - // ABI: selector + [offset_hash160(32), version(32), length(32), data(32)] - // offset_hash160 = 0x40 (64, pointing past the 2 static param slots) - let mut input = Vec::new(); - input.extend_from_slice(&selector); - - // param 0: offset to bytes data = 0x40 (2 * 32 = 64) - let mut offset_word = [0u8; 32]; - offset_word[31] = 0x40; - input.extend_from_slice(&offset_word); - - // param 1: version as int256 - let mut version_word = [0u8; 32]; - if version >= 0 { - version_word[28..32].copy_from_slice(&version.to_be_bytes()); - } else { - version_word = [0xFF; 32]; - version_word[28..32].copy_from_slice(&version.to_be_bytes()); - } - input.extend_from_slice(&version_word); - - // bytes data: length + padded data - let mut len_word = [0u8; 32]; - len_word[31] = hash160.len() as u8; - input.extend_from_slice(&len_word); - - let padded_len = hash160.len().div_ceil(32) * 32; - let mut data_padded = vec![0u8; padded_len]; - data_padded[..hash160.len()].copy_from_slice(hash160); - input.extend_from_slice(&data_padded); - - input - } - - #[test] - fn test_to_base58check_rskj_vector() { - let hash160 = hex::decode("0d3bf5f30dda7584645546079318e97f0e1d044f").unwrap(); - let input = build_to_base58check_input(&hash160, 111); - let result = hd_wallet_utils_run(&input, 200_000).unwrap(); - assert_eq!(result.gas_used, 13_000); - - let decoded = abi_decode_string(&result.bytes, 0).unwrap(); - assert_eq!(decoded, "mgivuh9jErcGdRr81cJ3A7YfgbJV7WNyZV"); - } - - #[test] - fn test_to_base58check_invalid_hash_length() { - let hash = vec![0xAA; 19]; // wrong length - let input = build_to_base58check_input(&hash, 111); - assert!(hd_wallet_utils_run(&input, 200_000).is_err()); - } - - #[test] - fn test_to_base58check_version_negative() { - let hash = vec![0x00; 20]; - let input = build_to_base58check_input(&hash, -1); - assert!(hd_wallet_utils_run(&input, 200_000).is_err()); - } - - #[test] - fn test_to_base58check_version_too_large() { - let hash = vec![0x00; 20]; - let input = build_to_base58check_input(&hash, 256); - assert!(hd_wallet_utils_run(&input, 200_000).is_err()); - } - - #[test] - fn test_to_base58check_oog() { - let hash = hex::decode("0d3bf5f30dda7584645546079318e97f0e1d044f").unwrap(); - let input = build_to_base58check_input(&hash, 111); - assert!(matches!( - hd_wallet_utils_run(&input, 12_000), - Err(PrecompileError::OutOfGas) - )); - } - - // --- deriveExtendedPublicKey tests --- - - fn build_derive_xpub_input(xpub: &str, path: &str) -> Vec { - let selector = selector_of("deriveExtendedPublicKey(string,string)"); - let mut input = Vec::new(); - input.extend_from_slice(&selector); - - // Two dynamic params: offsets then data - // param 0 offset: 0x40 (past 2 offset slots) - // param 1 offset: computed after param 0 data - - let xpub_bytes = xpub.as_bytes(); - let path_bytes = path.as_bytes(); - - let xpub_padded = xpub_bytes.len().div_ceil(32) * 32; - let param0_offset = 64u64; // 2 * 32 - let param1_offset = param0_offset + 32 + xpub_padded as u64; - - // offset for param 0 - let mut w = [0u8; 32]; - w[24..32].copy_from_slice(¶m0_offset.to_be_bytes()); - input.extend_from_slice(&w); - - // offset for param 1 - let mut w = [0u8; 32]; - w[24..32].copy_from_slice(¶m1_offset.to_be_bytes()); - input.extend_from_slice(&w); - - // param 0 data: length + padded string - let mut w = [0u8; 32]; - w[24..32].copy_from_slice(&(xpub_bytes.len() as u64).to_be_bytes()); - input.extend_from_slice(&w); - let mut padded = vec![0u8; xpub_padded]; - padded[..xpub_bytes.len()].copy_from_slice(xpub_bytes); - input.extend_from_slice(&padded); - - // param 1 data: length + padded string - let path_padded = path_bytes.len().div_ceil(32) * 32; - let mut w = [0u8; 32]; - w[24..32].copy_from_slice(&(path_bytes.len() as u64).to_be_bytes()); - input.extend_from_slice(&w); - let mut padded = vec![0u8; path_padded]; - padded[..path_bytes.len()].copy_from_slice(path_bytes); - input.extend_from_slice(&padded); - - input - } - - const TEST_TPUB: &str = "tpubD6NzVbkrYhZ4YHQqwWz3Tm1ESZ9AidobeyLG4mEezB6hN8gFFWrcjczyF77Lw3HEs6Rjd2R11BEJ8Y9ptfxx9DFknkdujp58mFMx9H5dc1r"; - - #[test] - fn test_derive_xpub_rskj_vector_1() { - let input = build_derive_xpub_input(TEST_TPUB, "2/3/4"); - let result = hd_wallet_utils_run(&input, 200_000).unwrap(); - assert_eq!(result.gas_used, 107_000); - let derived = abi_decode_string(&result.bytes, 0).unwrap(); - assert_eq!( - derived, - "tpubDCGMkPKredy7oh6zw8f4ExWFdTgQCrAHToF1ytny3gbVy9GkUNK2Nqh7NbKbh8dkd5VtjUiLJPkbEkeg29NVHwxYwzHJFt9SazGLZrrU4Y4" - ); - } - - #[test] - fn test_derive_xpub_rskj_vector_2() { - let input = build_derive_xpub_input(TEST_TPUB, "0/0/0/0/0/0"); - let result = hd_wallet_utils_run(&input, 200_000).unwrap(); - let derived = abi_decode_string(&result.bytes, 0).unwrap(); - assert_eq!( - derived, - "tpubDJ28nwFGUypUD6i8eGCQfMkwNGxzzabA5Mh7AcUdwm6ziFxCSWjy4HyhPXH5uU2ovdMMYLT9W3g3MrGo52TrprMvX8o1dzT2ZGz1pwCPTNv" - ); - } - - #[test] - fn test_derive_xpub_rskj_vector_3_max_index() { - let input = build_derive_xpub_input(TEST_TPUB, "2147483647"); - let result = hd_wallet_utils_run(&input, 200_000).unwrap(); - let derived = abi_decode_string(&result.bytes, 0).unwrap(); - assert_eq!( - derived, - "tpubD8fY35uPCY1rUjMUZwhkGUFi33pwkffMEBaCsTSw1he2AbM6DMbPaRR2guvk5qTWDfE9ubFB5pzuUNnMtsqbCeKAAjfepSvEWyetyF9Q4fG" - ); - } - - #[test] - fn test_derive_xpub_invalid_key() { - let input = build_derive_xpub_input("this-is-not-an-xpub", "0"); - assert!(hd_wallet_utils_run(&input, 200_000).is_err()); - } - - #[test] - fn test_derive_xpub_path_empty() { - let input = build_derive_xpub_input(TEST_TPUB, ""); - assert!(hd_wallet_utils_run(&input, 200_000).is_err()); - } - - #[test] - fn test_derive_xpub_path_leading_m() { - let input = build_derive_xpub_input(TEST_TPUB, "M/0/1/2"); - assert!(hd_wallet_utils_run(&input, 200_000).is_err()); - } - - #[test] - fn test_derive_xpub_path_leading_slash() { - let input = build_derive_xpub_input(TEST_TPUB, "/0"); - assert!(hd_wallet_utils_run(&input, 200_000).is_err()); - } - - #[test] - fn test_derive_xpub_path_trailing_slash() { - let input = build_derive_xpub_input(TEST_TPUB, "0/"); - assert!(hd_wallet_utils_run(&input, 200_000).is_err()); - } - - #[test] - fn test_derive_xpub_path_hardening() { - let input = build_derive_xpub_input(TEST_TPUB, "4'/5"); - assert!(hd_wallet_utils_run(&input, 200_000).is_err()); - } - - #[test] - fn test_derive_xpub_path_negative() { - let input = build_derive_xpub_input(TEST_TPUB, "0/-1"); - assert!(hd_wallet_utils_run(&input, 200_000).is_err()); - } - - #[test] - fn test_derive_xpub_path_segment_too_large() { - let input = build_derive_xpub_input(TEST_TPUB, "0/1/2/2147483648"); - assert!(hd_wallet_utils_run(&input, 200_000).is_err()); - } - - #[test] - fn test_derive_xpub_path_too_many_segments() { - let input = build_derive_xpub_input(TEST_TPUB, "0/1/2/3/4/5/6/7/8/9/10"); - assert!(hd_wallet_utils_run(&input, 200_000).is_err()); - } - - #[test] - fn test_derive_xpub_oog() { - let input = build_derive_xpub_input(TEST_TPUB, "2/3/4"); - assert!(matches!( - hd_wallet_utils_run(&input, 100_000), - Err(PrecompileError::OutOfGas) - )); - } - - // --- extractPublicKeyFromExtendedPublicKey tests --- - - fn build_extract_pubkey_input(xpub: &str) -> Vec { - let selector = selector_of("extractPublicKeyFromExtendedPublicKey(string)"); - let mut input = Vec::new(); - input.extend_from_slice(&selector); - - let xpub_bytes = xpub.as_bytes(); - let xpub_padded = xpub_bytes.len().div_ceil(32) * 32; - - // offset = 0x20 (single dynamic param, offset past 1 slot) - let mut w = [0u8; 32]; - w[31] = 0x20; - input.extend_from_slice(&w); - - // length - let mut w = [0u8; 32]; - w[24..32].copy_from_slice(&(xpub_bytes.len() as u64).to_be_bytes()); - input.extend_from_slice(&w); - - // padded data - let mut padded = vec![0u8; xpub_padded]; - padded[..xpub_bytes.len()].copy_from_slice(xpub_bytes); - input.extend_from_slice(&padded); - - input - } - - #[test] - fn test_extract_pubkey_rskj_vector() { - let xpub = "xpub661MyMwAqRbcFMGNG2YcHvj3x63bAZN9U5cKikaiQ4zu2D1cvpnZYyXNR9nH62sGp4RR39Ui7SVQSq1PY4JbPuEuu5prVJJC3d5Pogft712"; - let input = build_extract_pubkey_input(xpub); - let result = hd_wallet_utils_run(&input, 200_000).unwrap(); - assert_eq!(result.gas_used, 11_300); - - let pubkey = abi_decode_bytes(&result.bytes, 0).unwrap(); - let expected = hex::decode("02be517550b9e3be7fe42c80932d51e88e698663b4926e598b269d050e87e34d8c").unwrap(); - assert_eq!(pubkey, expected); - } - - #[test] - fn test_extract_pubkey_invalid_key() { - let input = build_extract_pubkey_input("this-is-not-an-xpub"); - assert!(hd_wallet_utils_run(&input, 200_000).is_err()); - } - - #[test] - fn test_extract_pubkey_bad_checksum() { - // Last char changed: dc1r → dc1s - let input = build_extract_pubkey_input( - "tpubD6NzVbkrYhZ4YHQqwWz3Tm1ESZ9AidobeyLG4mEezB6hN8gFFWrcjczyF77Lw3HEs6Rjd2R11BEJ8Y9ptfxx9DFknkdujp58mFMx9H5dc1s" - ); - assert!(hd_wallet_utils_run(&input, 200_000).is_err()); - } - - #[test] - fn test_extract_pubkey_oog() { - let xpub = "xpub661MyMwAqRbcFMGNG2YcHvj3x63bAZN9U5cKikaiQ4zu2D1cvpnZYyXNR9nH62sGp4RR39Ui7SVQSq1PY4JbPuEuu5prVJJC3d5Pogft712"; - let input = build_extract_pubkey_input(xpub); - assert!(matches!( - hd_wallet_utils_run(&input, 10_000), - Err(PrecompileError::OutOfGas) - )); - } - - // --- getMultisigScriptHash tests --- - - fn build_multisig_input(min_sigs: i32, keys: &[Vec]) -> Vec { - let selector = selector_of("getMultisigScriptHash(int256,bytes[])"); - let mut input = Vec::new(); - input.extend_from_slice(&selector); - - // param 0: min_sigs (int256, static) - let mut w = [0u8; 32]; - if min_sigs >= 0 { - w[28..32].copy_from_slice(&min_sigs.to_be_bytes()); - } else { - w = [0xFF; 32]; - w[28..32].copy_from_slice(&min_sigs.to_be_bytes()); - } - input.extend_from_slice(&w); - - // param 1: offset to bytes[] data = 0x40 (past 2 param slots) - let mut w = [0u8; 32]; - w[31] = 0x40; - input.extend_from_slice(&w); - - // bytes[] encoding: count, then offsets, then each element - // count - let mut w = [0u8; 32]; - w[24..32].copy_from_slice(&(keys.len() as u64).to_be_bytes()); - input.extend_from_slice(&w); - - // Calculate offsets for each element (relative to start of array content after count) - // Each offset points past all offsets + accumulated element data - let offsets_size = keys.len() * 32; - let mut elem_data = Vec::new(); - let mut elem_offsets = Vec::new(); - for key in keys { - elem_offsets.push(offsets_size + elem_data.len()); - // length word - let mut lw = [0u8; 32]; - lw[24..32].copy_from_slice(&(key.len() as u64).to_be_bytes()); - elem_data.extend_from_slice(&lw); - // padded data - let padded = key.len().div_ceil(32) * 32; - let mut pd = vec![0u8; padded]; - pd[..key.len()].copy_from_slice(key); - elem_data.extend_from_slice(&pd); - } - - // Write offsets - for off in &elem_offsets { - let mut w = [0u8; 32]; - w[24..32].copy_from_slice(&(*off as u64).to_be_bytes()); - input.extend_from_slice(&w); - } - - // Write element data - input.extend_from_slice(&elem_data); - - input - } - - fn compressed_test_keys() -> Vec> { - vec![ - hex::decode("03b53899c390573471ba30e5054f78376c5f797fda26dde7a760789f02908cbad2").unwrap(), - hex::decode("027319afb15481dbeb3c426bcc37f9a30e7f51ceff586936d85548d9395bcc2344").unwrap(), - hex::decode("0355a2e9bf100c00fc0a214afd1bf272647c7824eb9cb055480962f0c382596a70").unwrap(), - hex::decode("02566d5ded7c7db1aa7ee4ef6f76989fb42527fcfdcddcd447d6793b7d869e46f7").unwrap(), - hex::decode("0294c817150f78607566e961b3c71df53a22022a80acbb982f83c0c8baac040adc").unwrap(), - hex::decode("0372cd46831f3b6afd4c044d160b7667e8ebf659d6cb51a825a3104df6ee0638c6").unwrap(), - hex::decode("0340df69f28d69eef60845da7d81ff60a9060d4da35c767f017b0dd4e20448fb44").unwrap(), - hex::decode("02ac1901b6fba2c1dbd47d894d2bd76c8ba1d296d65f6ab47f1c6b22afb53e73eb").unwrap(), - hex::decode("031aabbeb9b27258f98c2bf21f36677ae7bae09eb2d8c958ef41a20a6e88626d26").unwrap(), - hex::decode("0245ef34f5ee218005c9c21227133e8568a4f3f11aeab919c66ff7b816ae1ffeea").unwrap(), - hex::decode("02550cc87fa9061162b1dd395a16662529c9d8094c0feca17905a3244713d65fe8").unwrap(), - hex::decode("02481f02b7140acbf3fcdd9f72cf9a7d9484d8125e6df7c9451cfa55ba3b077265").unwrap(), - hex::decode("03f909ae15558c70cc751aff9b1f495199c325b13a9e5b934fd6299cd30ec50be8").unwrap(), - hex::decode("02c6018fcbd3e89f3cf9c7f48b3232ea3638eb8bf217e59ee290f5f0cfb2fb9259").unwrap(), - hex::decode("03b65694ccccda83cbb1e56b31308acd08e993114c33f66a456b627c2c1c68bed6").unwrap(), - ] - } - - #[test] - fn test_multisig_rskj_15_compressed_keys() { - let keys = compressed_test_keys(); - let input = build_multisig_input(8, &keys); - let result = hd_wallet_utils_run(&input, 200_000).unwrap(); - - // Gas: 20000 + (15 - 2) * 700 = 29100 - assert_eq!(result.gas_used, 29_100); - - let hash = abi_decode_bytes(&result.bytes, 0).unwrap(); - assert_eq!( - hex::encode(&hash), - "51f103320b435b5fe417b3f3e0f18972ccc710a0" - ); - } - - #[test] - fn test_multisig_mixed_compressed_uncompressed() { - let mut keys = compressed_test_keys(); - // Replace keys 0, 4, 6, 9 with uncompressed versions - let uncompressed_replacements = [ - (0, "04b53899c390573471ba30e5054f78376c5f797fda26dde7a760789f02908cbad2aafaaa2611606699ec4f82777a268b708dab346de4880cd223969f7bbe5422bf"), - (4, "0494c817150f78607566e961b3c71df53a22022a80acbb982f83c0c8baac040adcb17171aa9ec8d8587098e0771f686ee61ac35279f9e5aadf9b06b738aa6d3720"), - (6, "0440df69f28d69eef60845da7d81ff60a9060d4da35c767f017b0dd4e20448fb44e1abebaea4c3c57c6e9e39e205b4df046f7110a8d3477c0d8e26a28be9692c29"), - (9, "0445ef34f5ee218005c9c21227133e8568a4f3f11aeab919c66ff7b816ae1ffeeae024d50312de76a7950f8c6268fbf454335cf252f961a67c47e67dc06fa590ba"), - ]; - for (idx, hex_str) in &uncompressed_replacements { - keys[*idx] = hex::decode(hex_str).unwrap(); - } - - let input = build_multisig_input(8, &keys); - let result = hd_wallet_utils_run(&input, 200_000).unwrap(); - let hash = abi_decode_bytes(&result.bytes, 0).unwrap(); - assert_eq!( - hex::encode(&hash), - "51f103320b435b5fe417b3f3e0f18972ccc710a0", - "mixed keys must produce same hash as all-compressed" - ); - } - - #[test] - fn test_multisig_min_sigs_zero() { - let keys = compressed_test_keys()[..2].to_vec(); - let input = build_multisig_input(0, &keys); - assert!(hd_wallet_utils_run(&input, 200_000).is_err()); - } - - #[test] - fn test_multisig_less_than_two_keys() { - let keys = vec![compressed_test_keys()[0].clone()]; - let input = build_multisig_input(1, &keys); - assert!(hd_wallet_utils_run(&input, 200_000).is_err()); - } - - #[test] - fn test_multisig_keys_less_than_min_sigs() { - let keys = compressed_test_keys()[..2].to_vec(); - let input = build_multisig_input(3, &keys); - assert!(hd_wallet_utils_run(&input, 200_000).is_err()); - } - - #[test] - fn test_multisig_too_many_keys() { - // 16 keys (max is 15) - let mut keys = compressed_test_keys(); - keys.push(keys[0].clone()); - let input = build_multisig_input(3, &keys); - assert!(hd_wallet_utils_run(&input, 200_000).is_err()); - } - - #[test] - fn test_multisig_invalid_key_length() { - let keys = vec![ - compressed_test_keys()[0].clone(), - vec![0xAA, 0xBB, 0xCC], // wrong length - ]; - let input = build_multisig_input(1, &keys); - assert!(hd_wallet_utils_run(&input, 200_000).is_err()); - } - - #[test] - fn test_multisig_invalid_key_prefix() { - let mut bad_key = compressed_test_keys()[0].clone(); - bad_key[0] = 0x08; // invalid prefix - let keys = vec![compressed_test_keys()[1].clone(), bad_key]; - let input = build_multisig_input(1, &keys); - assert!(hd_wallet_utils_run(&input, 200_000).is_err()); - } - - #[test] - fn test_multisig_gas_base_for_two_keys() { - let keys = compressed_test_keys()[..2].to_vec(); - let input = build_multisig_input(1, &keys); - let result = hd_wallet_utils_run(&input, 200_000).unwrap(); - assert_eq!(result.gas_used, 20_000); - } - - #[test] - fn test_multisig_oog() { - let keys = compressed_test_keys(); - let input = build_multisig_input(8, &keys); - // Need 29100, supply 29000 - assert!(matches!( - hd_wallet_utils_run(&input, 29_000), - Err(PrecompileError::OutOfGas) - )); - } } diff --git a/crates/execution/src/rsk_handler.rs b/crates/execution/src/rsk_handler.rs index 631bcd6..2fe888c 100644 --- a/crates/execution/src/rsk_handler.rs +++ b/crates/execution/src/rsk_handler.rs @@ -41,7 +41,7 @@ use revm::handler::{ }; use revm::interpreter::interpreter::EthInterpreter; use revm::interpreter::interpreter_action::FrameInit; -use revm::interpreter::{CallOutcome, CreateOutcome, InstructionResult, InterpreterAction}; +use revm::interpreter::{CallOutcome, CreateOutcome, Gas, InstructionResult, InterpreterAction}; use revm::primitives::hardfork::SpecId; use revm::primitives::{Address, Bytes, U256}; use revm::inspector::JournalExt; @@ -284,6 +284,37 @@ where exec_result.gas_mut().set_final_refund(false); } + /// The mainnet settlement of the transaction's gas, after one change for + /// rskj's invisible exception (`TransactionExecutor.call`). When a + /// precompile throws in the transaction's own call, its frame reverts, so + /// revm hands the value back and forgets the touch. Here the transaction + /// becomes a success that used its intrinsic gas and what the precompile + /// asked for. `reimburse_caller` and `reward_beneficiary` charge the + /// whole gas limit. + fn last_frame_result( + &mut self, + evm: &mut Self::Evm, + frame_result: &mut FrameResult, + ) -> Result<(), Self::Error> { + use revm::context_interface::Transaction; + if self.invisible_exception.load(Ordering::Relaxed) { + frame_result.interpreter_result_mut().result = InstructionResult::Return; + } + let instruction_result = frame_result.interpreter_result().result; + let gas = frame_result.gas_mut(); + let remaining = gas.remaining(); + let refunded = gas.refunded(); + // Spend the gas limit; what the transaction left comes back below. + *gas = Gas::new_spent(evm.ctx().tx().gas_limit()); + if instruction_result.is_ok_or_revert() { + gas.erase_cost(remaining); + } + if instruction_result.is_ok() { + gas.record_refund(refunded); + } + Ok(()) + } + /// The mainnet exec loop with two additions: /// - immediately after a CREATE frame is initialized, reset the created /// account's nonce to 0 (rskj has no EIP-161; revm seeds it to 1 from From 20a74463e24cf8eac99927664dff697231da0593 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Sun, 8 Nov 2026 23:10:36 -0800 Subject: [PATCH 042/264] nothing a stranger sends can panic the node: peers, snapshot sync, blocks, RPC, precompiles Everything a stranger can send was tried, and what it broke is fixed. Peers. A panic in a session now ends that session and drops its peer, and nothing else: register_and_run_session runs the session under catch_unwind, where nothing caught a panic outside peer discovery. Panics a peer could cause: - discovery filed a node in one of 256 buckets by its 512-bit XOR distance, so a node ID whose high bits differ from ours indexed past the end. Nodes are now ordered as rskj's Kademlia table orders them, by the distance between keccak256(keccak256(id)) keys (DistanceCalculator), with the bucket read by get_mut; - the RLPx responder read the initiator's auth signature as 65 bytes without checking its length; - a frame header's long-form list length, any 8 bytes, overflowed when added to its start. And two that need no peer: `Instant::now() - d` panics on a machine booted less than d ago, which the outbound dialer's starved-warning timer and the snapshot session's phase report both did. Snapshot sync. A peer offering a state could make the node hang, abort, panic in a debug build, or grow its memory without limit: - checkpoint::max_work_between, the most work a span of blocks can hold, walked block by block over a span the peer chose; in a release build a span of u64::MAX looped for good. It now keeps only the ceilings that differ and counts a settled run at once, and gives what the walk gave (checked against it over a grid of divisors, spans and difficulties); - the sampler's fill_gaps placed its points one at a time over a gap the peer chose; it now skips to each next point, with the same result; - a skeleton answer is held to what rskj sends (NodeBlockProcessor.processSkeletonRequest): at most 21 identifiers, all within 3,840 blocks of the grid point at or below the start; - the offered header's proof of work is checked only against its own difficulty, so a state more than 10,000,000 blocks above the checkpoint, about 9.5 years of mainnet, is refused (SnapFailure::ImplausibleHeight), and the peer blamed as for an invalid block. The snap client measures a slice's remaining length with saturating_sub, as its rebuilt-chunk path already did. Blocks and transactions. A transaction's v overflowed while its recovery id was computed, and a receipt with a bloom over 256 bytes panicked. Locks. The 42 places that took a std lock with unwrap or expect panicked if another thread had panicked holding it; they take it with PoisonError::into_inner. RPC. A method that panics is answered as rskj's RskErrorResolver answers an exception it has no answer for, -32603 "Internal server error", where the client's connection used to drop with every other request in its batch. trace_filter and eth_getFilterLogs walked every number up to a toBlock as high as 2^64, a request that never ends; both stop at the head, as rskj's walk stops at the first block it doesn't find. eth_getLogs' span check overflowed for a fromBlock near 2^64. Tests. core::mutate varies real encodings byte by byte, and fuzz tests now cover: headers of every version with their merged-mining fields and extensions; transactions and receipts; rskj RLP; Bitcoin blocks and PMTs; RSKj's snapshot chunks, rebuilt against their roots; every RSK and P2P message, varied from real encodings and with random RLP for every type and id; the Bridge reading a peg-in's scripts, witnesses and outputs (sender shapes, peg-in instructions, federation spends); and every precompile, each method with ABI edge words, called directly, by CALL, STATICCALL and DELEGATECALL and as a local call, by a stranger and by the federators and authorizers, under genesis rules, mainnet's between Papyrus and Iris, testnet's, and everything on. The range tests run on a thread of their own with a minute to answer, so a regression fails rather than hangs. --- crates/core/src/btc_block.rs | 18 ++ crates/core/src/checkpoint.rs | 151 +++++++++-- crates/core/src/lib.rs | 3 + crates/core/src/mutate.rs | 130 +++++++++ crates/core/src/partial_merkle_tree.rs | 19 ++ crates/core/src/rskj_rlp.rs | 40 +++ crates/core/src/types/header.rs | 40 +++ crates/core/src/types/receipt.rs | 64 ++++- crates/core/src/types/transaction.rs | 76 +++++- crates/core/src/validation/tests.rs | 26 ++ crates/execution/src/bridge/mod.rs | 6 + crates/execution/src/bridge/peg.rs | 201 ++++++++++++++ crates/execution/src/executor.rs | 279 ++++++++++++++++++++ crates/execution/src/native/block_header.rs | 9 + crates/execution/src/native/hd_wallet.rs | 9 + crates/networking/src/discovery/table.rs | 150 ++++++++--- crates/networking/src/node.rs | 88 +++++- crates/networking/src/outbound.rs | 9 +- crates/networking/src/protocol/rsk.rs | 248 +++++++++++++++++ crates/networking/src/rlpx/ecies.rs | 30 ++- crates/networking/src/rlpx/frame.rs | 25 +- crates/rpc/src/logs.rs | 42 ++- crates/rpc/src/server.rs | 63 +++++ crates/rpc/src/tests.rs | 122 +++++++++ crates/rpc/src/trace.rs | 5 +- crates/sync/src/chain_activity.rs | 6 +- crates/sync/src/gas_price.rs | 21 +- crates/sync/src/manager.rs | 45 +++- crates/sync/src/sampler.rs | 135 +++++++++- crates/sync/src/snap/client.rs | 8 +- crates/sync/src/snap/driver.rs | 83 +++++- crates/sync/src/snap/session.rs | 15 +- crates/sync/src/snap/session_tests.rs | 58 ++++ crates/sync/src/tx_relay.rs | 6 +- crates/sync/src/txpool.rs | 90 +++++-- crates/trie/tests/rskj_vectors.rs | 53 ++++ 36 files changed, 2237 insertions(+), 136 deletions(-) create mode 100644 crates/core/src/mutate.rs diff --git a/crates/core/src/btc_block.rs b/crates/core/src/btc_block.rs index bd08f34..7cc8252 100644 --- a/crates/core/src/btc_block.rs +++ b/crates/core/src/btc_block.rs @@ -317,4 +317,22 @@ mod tests { assert!(bitcoinj_reads(&block(&[1], &[&script(0x0200_0000)]))); assert!(!bitcoinj_reads(&block(&[1], &[&script(0x0200_0001)]))); } + + /// No variation on a block's bytes makes reading it panic: below orchid a + /// merge-mined header's Bitcoin field can carry any bytes at all. + #[test] + fn no_variation_on_a_bitcoin_block_panics() { + let two = transaction(&[2], &[3]); + let seeds = [ + genesis(), + block(&[2], &[&two, &two]), + genesis()[..HEADER_SIZE].to_vec(), + ]; + for (i, seed) in seeds.iter().enumerate() { + assert!(bitcoinj_reads(seed)); + for bytes in crate::mutate::mutants(seed, 3_000, 600 + i as u64) { + let _ = bitcoinj_reads(&bytes); + } + } + } } diff --git a/crates/core/src/checkpoint.rs b/crates/core/src/checkpoint.rs index 9fb396d..6164b22 100644 --- a/crates/core/src/checkpoint.rs +++ b/crates/core/src/checkpoint.rs @@ -185,29 +185,58 @@ pub fn max_work_between( // out *below* a flat chain's real work for short spans, and would have // rejected honest peers. // - // `cap[i]` is the ceiling for the i-th block after `from`, built backwards - // from `to`: each step back multiplies by `divisor / (divisor - 1)`, the - // inverse of a maximal fall. It saturates, which only loosens the bound. - let mut cap = vec![U256::ZERO; span as usize + 1]; - cap[span as usize] = to; - for i in (0..span as usize).rev() { - let next = cap[i + 1]; - cap[i] = next + // `ceilings[k]` is the ceiling for the block `k` before the span's last, + // built backwards from `to`: each step back multiplies by + // `divisor / (divisor - 1)`, the inverse of a maximal fall. It saturates, + // which only loosens the bound. Grown back far enough it stops changing + // -- at `U256::MAX`, or below `divisor - 1`, where the growth rounds away + // -- and every block before that has the ceiling it stopped at. So only + // the ceilings that differ are kept, a few thousand at most. The span is + // the distance to a height a peer names, and a cell for every block let + // a peer naming a distant one make this node allocate without limit. + let mut ceilings = vec![to]; + while (ceilings.len() as u64) <= span { + let last = ceilings[ceilings.len() - 1]; + let grown = last .checked_mul(div) .map(|v| v / (div - U256::from(1u64))) .unwrap_or(U256::MAX); + if grown == last { + break; + } + ceilings.push(grown); } + let settled = ceilings.len() as u64 - 1; + // Blocks up to this one all have the settled ceiling. + let flat_until = span - settled; let mut total = U256::ZERO; let mut d = from; - #[allow(clippy::needless_range_loop)] // `step` is the step number, not just an index - for step in 1..=span as usize { - let risen = d.saturating_add(d / div); - d = risen.min(cap[step]); - if d < min_difficulty { - d = min_difficulty; + let mut step = 1; + loop { + let ceiling = ceilings[(span - step).min(settled) as usize]; + let next = d.saturating_add(d / div).min(ceiling).max(min_difficulty); + total = total.saturating_add(next); + if total == U256::MAX { + // Nothing added after it can take it below the most there is. + return total; + } + // Under the settled ceiling every block is worked out alike, so one + // whose difficulty is its parent's leaves every block after it + // there too, up to where the ceilings begin to differ. They are + // counted at once rather than one by one, which a span a peer names + // could make last for ever. + if next == d && step < flat_until { + total = total.saturating_add(next.saturating_mul(U256::from(flat_until - step))); + step = flat_until; + } + d = next; + // Stopping at the last block rather than past it: the span can be + // `u64::MAX`, and there is no step after that. + if step == span { + break; } - total = total.saturating_add(d); + step += 1; } total } @@ -515,6 +544,98 @@ mod tests { ); } + /// `max_work_between` as it was written first, a cell for every block, + /// kept to hold the bounded one to the same answers. + fn max_work_cell_by_cell(from: U256, to: U256, span: u64, divisor: u64, min: U256) -> U256 { + if span == 0 || divisor < 2 { + return U256::ZERO; + } + let div = U256::from(divisor); + let mut cap = vec![U256::ZERO; span as usize + 1]; + cap[span as usize] = to; + for i in (0..span as usize).rev() { + cap[i] = cap[i + 1] + .checked_mul(div) + .map(|v| v / (div - U256::from(1u64))) + .unwrap_or(U256::MAX); + } + let mut total = U256::ZERO; + let mut d = from; + for ceiling in &cap[1..] { + d = d.saturating_add(d / div).min(*ceiling).max(min); + total = total.saturating_add(d); + } + total + } + + /// Keeping only the ceilings that differ, and counting a settled run at + /// once, changes no answer. + #[test] + fn the_bound_is_the_one_worked_out_block_by_block() { + let big = U256::from(10u64).pow(U256::from(25u64)); + let froms = [ + U256::ZERO, + U256::from(1u64), + U256::from(48u64), + U256::from(50u64), + MIN, + MIN * U256::from(3u64), + big, + U256::MAX, + ]; + let tos = [ + U256::ZERO, + U256::from(1u64), + U256::from(49u64), + MIN, + MIN * U256::from(2u64), + big, + U256::MAX, + ]; + let mins = [U256::ZERO, U256::from(1u64), MIN]; + for divisor in [2u64, 3, 50, 400] { + for span in [1u64, 2, 3, 191, 768, 2_500] { + for &from in &froms { + for &to in &tos { + for &min in &mins { + assert_eq!( + max_work_between(from, to, span, divisor, min), + max_work_cell_by_cell(from, to, span, divisor, min), + "from {from}, to {to}, span {span}, divisor {divisor}, min {min}" + ); + } + } + } + } + } + } + + /// A span no cell-by-cell table could hold is answered, and at once: a + /// peer names the height it runs to. + #[test] + fn a_span_a_peer_names_is_answered_at_once() { + let started = std::time::Instant::now(); + // Rising without limit, the work passes the most there is. + assert_eq!( + max_work_between(MIN, U256::MAX, u64::MAX, 50, MIN), + U256::MAX + ); + assert_eq!(max_work_between(MIN, MIN, u64::MAX, 400, MIN), U256::MAX); + // Held flat, it is the span's blocks at the one difficulty, exactly. + let span = 1_000_000_000_000_000u64; + let one = U256::from(1u64); + assert_eq!(max_work_between(one, one, span, 50, one), U256::from(span)); + assert_eq!( + max_work_between(one, one, u64::MAX, 2048, one), + U256::from(u64::MAX) + ); + assert!( + started.elapsed() < std::time::Duration::from_secs(5), + "took {:?}", + started.elapsed() + ); + } + /// The bound must never be below what a flat chain actually carries, or it /// would reject honest peers. #[test] diff --git a/crates/core/src/lib.rs b/crates/core/src/lib.rs index 4de7e7e..6d57865 100644 --- a/crates/core/src/lib.rs +++ b/crates/core/src/lib.rs @@ -11,6 +11,9 @@ pub mod rskj_rlp; pub mod partial_merkle_tree; pub mod btc_block; +#[cfg(test)] +mod mutate; + pub use types::block::Block; pub use types::header::Header; pub use types::receipt::{Log, Receipt, ordered_trie_root, ordered_tx_trie_root}; diff --git a/crates/core/src/mutate.rs b/crates/core/src/mutate.rs new file mode 100644 index 0000000..3e74c1e --- /dev/null +++ b/crates/core/src/mutate.rs @@ -0,0 +1,130 @@ +//! Variations on real bytes, for showing that what decodes and checks +//! untrusted input answers each one with a value or an error, never a panic. +//! +//! Seeded from encodings the chain really holds, so the variations land +//! where a decoder does its work rather than failing at the first byte. + +use crate::config::{ActivationHeights, ChainConfig}; +use crate::types::header::Header; +use crate::types::header_extension::{BlockHeaderExtension, ExtensionCommitment}; +use crate::validation::HeaderVerifier; +use std::sync::Arc; + +/// A xorshift, so that a failure names the variation that caused it. +pub(crate) struct Rng(u64); + +impl Rng { + pub(crate) fn new(seed: u64) -> Self { + Self(seed | 1) + } + + pub(crate) fn next(&mut self) -> u64 { + let mut x = self.0; + x ^= x << 13; + x ^= x >> 7; + x ^= x << 17; + self.0 = x; + x + } + + /// From 0 up to, not including, `n`; 0 when `n` is. + pub(crate) fn below(&mut self, n: usize) -> usize { + if n == 0 { + 0 + } else { + (self.next() % n as u64) as usize + } + } +} + +/// Bytes the encodings treat apart: the edges of RLP's prefix ranges, and +/// Bitcoin's varint markers. +const EDGES: [u8; 14] = [ + 0x00, 0x01, 0x7f, 0x80, 0x81, 0xb7, 0xb8, 0xbf, 0xc0, 0xf7, 0xf8, 0xfd, 0xfe, 0xff, +]; + +/// One edit: a bit flipped, a byte set to an edge or to anything, a byte +/// put in, bytes taken out, repeated elsewhere or run to 0xff, or the end +/// cut off. +fn edit(bytes: &mut Vec, rng: &mut Rng) { + let len = bytes.len(); + if len == 0 { + bytes.push(rng.next() as u8); + return; + } + let at = rng.below(len); + match rng.below(8) { + 0 => bytes[at] ^= 1 << rng.below(8), + 1 => bytes[at] = EDGES[rng.below(EDGES.len())], + 2 => bytes[at] = rng.next() as u8, + 3 => bytes.insert(rng.below(len + 1), EDGES[rng.below(EDGES.len())]), + 4 => { + let n = 1 + rng.below(8.min(len - at)); + bytes.drain(at..at + n); + } + 5 => bytes.truncate(at), + 6 => { + let n = 1 + rng.below(16.min(len - at)); + let piece = bytes[at..at + n].to_vec(); + let to = rng.below(len + 1); + bytes.splice(to..to, piece); + } + _ => { + let n = (1 + rng.below(8)).min(len - at); + bytes[at..at + n].fill(0xff); + } + } +} + +/// `count` variations on `seed`, each of one to three edits. +pub(crate) fn mutants(seed: &[u8], count: usize, rng_seed: u64) -> Vec> { + let mut rng = Rng::new(rng_seed); + (0..count) + .map(|_| { + let mut bytes = seed.to_vec(); + for _ in 0..1 + rng.below(3) { + edit(&mut bytes, &mut rng); + } + bytes + }) + .collect() +} + +/// What this crate does with a header's bytes a peer sends, done to `bytes` +/// under each network's heights and rules: decoded both ways, hashed both +/// ways, encoded both ways, and checked by every rule, a header's own, an +/// uncle's and against a parent. Only that none of it panics is checked. +pub(crate) fn decode_and_check_header(bytes: &[u8]) { + let networks = [ + (ActivationHeights::mainnet(), ChainConfig::mainnet()), + (ActivationHeights::testnet(), ChainConfig::testnet()), + (ActivationHeights::regtest(), ChainConfig::regtest()), + ]; + for (activations, config) in networks { + let verifier = HeaderVerifier::default_rsk(Arc::new(config)); + let decoded = [ + Header::decode_with_hash_and_activations(&mut &bytes[..], &activations), + Header::decode_with_activations(&mut &bytes[..], &activations), + ]; + for header in decoded.into_iter().flatten() { + let _ = header.hash(); + let _ = header.hash_for_merged_mining(); + let _ = header.extension_commitment(); + let _ = header.extension(); + let _ = alloy_rlp::encode(&header); + let _ = header.encode_compressed(); + let _ = verifier.verify(&header, None); + let _ = verifier.verify_uncle(&header); + let _ = verifier.verify_against_parent(&header, &header); + for rule in verifier.uncle_parent_rules() { + let _ = rule.validate_with_parent(&header, &header); + } + let _ = crate::validation::merged_mining::extract_fork_detection_data(&header); + } + } + if let Ok(extension) = BlockHeaderExtension::from_encoded(bytes) { + let _ = extension.hash(); + let _ = extension.encoded(); + } + let _ = ExtensionCommitment::from_extension_data(bytes); +} diff --git a/crates/core/src/partial_merkle_tree.rs b/crates/core/src/partial_merkle_tree.rs index 512c308..a6589b3 100644 --- a/crates/core/src/partial_merkle_tree.rs +++ b/crates/core/src/partial_merkle_tree.rs @@ -660,4 +660,23 @@ mod tests { assert!(!has_expected_size(&[])); assert!(!has_expected_size(&count)); } + + /// No variation on a tree's bytes makes reading, sizing or walking it + /// panic: a peer's merge-mined header carries one below orchid, and a + /// peg-in carries one to the Bridge. + #[test] + fn no_variation_on_a_tree_panics() { + let leaves: Vec<[u8; 32]> = (0..33).map(leaf).collect(); + let shapes: [(usize, &[usize]); 4] = + [(1, &[0]), (7, &[2, 5]), (16, &[15]), (33, &[0, 17, 32])]; + for (i, (count, matched)) in shapes.into_iter().enumerate() { + let include: Vec = (0..count).map(|n| matched.contains(&n)).collect(); + let seed = build(&leaves[..count], &include); + assert!(walked(&seed).is_ok()); + for bytes in crate::mutate::mutants(&seed, 2_000, 500 + i as u64) { + let _ = has_expected_size(&bytes); + let _ = walked(&bytes); + } + } + } } diff --git a/crates/core/src/rskj_rlp.rs b/crates/core/src/rskj_rlp.rs index fe4124e..86c2d6e 100644 --- a/crates/core/src/rskj_rlp.rs +++ b/crates/core/src/rskj_rlp.rs @@ -195,4 +195,44 @@ mod tests { assert_eq!(items(&[0xf9, 0x00]), Err(RlpError)); assert_eq!(items(&[0x01, 0x82, 0xaa]), Err(RlpError)); } + + /// No variation on nested lists makes reading them, at any depth, panic: + /// rskj's decoder reads a fallback-mined header's signature from bytes + /// its miner wrote. + #[test] + fn no_variation_on_rlp_panics() { + fn walk(element: &Element<'_>) { + let _ = element.data(); + if let Ok(inner) = element.elements() { + inner.iter().for_each(walk); + } + } + let signature = [ + &[0xf8, 0x43, 0x1b, 0xa0][..], + &[0x7c; 32], + &[0xa0], + &[0x5c; 32], + ] + .concat(); + let long = [ + &[0xf9, 0x01, 0x06, 0xb9, 0x01, 0x00][..], + &[0xab; 256], + &[0xc2, 0x80, 0x01], + ] + .concat(); + let seeds = [ + signature, + long, + vec![0xc3, 0x1b, 0x81, 0x80], + vec![0xc5, 0xc4, 0xc3, 0xc2, 0xc1, 0xc0], + ]; + for (i, seed) in seeds.iter().enumerate() { + assert!(decode2(seed).is_ok()); + for bytes in crate::mutate::mutants(seed, 3_000, 700 + i as u64) { + if let Ok(elements) = decode2(&bytes) { + elements.iter().for_each(walk); + } + } + } + } } diff --git a/crates/core/src/types/header.rs b/crates/core/src/types/header.rs index 33ff31f..fb9f1f9 100644 --- a/crates/core/src/types/header.rs +++ b/crates/core/src/types/header.rs @@ -1905,4 +1905,44 @@ mod versioned_wire_header_tests { // each of two UMM roots. assert_eq!(layouts, 2 * (4 + 6 * 2)); } + + /// No variation on a versioned header's bytes, as testnet's peers send + /// them, makes decoding, hashing or checking it panic; nor on the + /// extensions they carry, as a body sends one apart from its header. + #[test] + fn no_variation_on_a_versioned_header_or_its_extension_panics() { + use crate::types::header_extension::BlockHeaderExtension; + let seeds = [ + V1_WIRE, + V2_WIRE, + V2_FULL, + V2_COMPRESSED, + V2_FROM_PEERS, + V2_SHORT_WIRE, + RSKJ_REGTEST_V2_FULL, + ]; + let compressed = from_the_network(V2_COMPRESSED); + for (i, seed) in seeds.iter().enumerate() { + let seed = alloy_primitives::hex::decode(seed).unwrap(); + crate::mutate::decode_and_check_header(&seed); + for bytes in crate::mutate::mutants(&seed, 300, 100 + i as u64) { + crate::mutate::decode_and_check_header(&bytes); + } + let testnet = crate::config::ActivationHeights::testnet(); + let Ok(header) = Header::decode_with_activations(&mut &seed[..], &testnet) else { + continue; + }; + let Some(extension) = header.extension() else { + continue; + }; + for bytes in crate::mutate::mutants(&extension.to_encoded(), 300, 200 + i as u64) { + if let Ok(extension) = BlockHeaderExtension::from_encoded(&bytes) { + let _ = extension.hash(); + let _ = compressed.accepts_extension(&extension); + let _ = compressed.with_extension(&extension); + let _ = header.with_extension(&extension); + } + } + } + } } diff --git a/crates/core/src/types/receipt.rs b/crates/core/src/types/receipt.rs index 776c826..fb5241f 100644 --- a/crates/core/src/types/receipt.rs +++ b/crates/core/src/types/receipt.rs @@ -106,7 +106,11 @@ impl Decodable for Receipt { } else { // rskj can encode shorter blooms; pad with leading zeros let mut full = [0u8; 256]; - let start = 256 - bloom_bytes.len(); + let start = 256usize + .checked_sub(bloom_bytes.len()) + .ok_or(alloy_rlp::Error::Custom( + "a receipt's bloom is over 256 bytes", + ))?; full[start..].copy_from_slice(bloom_bytes); Bloom::from_slice(&full) }; @@ -516,4 +520,62 @@ mod tests { // RLP.encodeInt(256) = 0x820100 assert_eq!(rlp_encode_int(256), vec![0x82, 0x01, 0x00]); } + + /// No variation on a receipt's bytes makes decoding it panic, and a bloom + /// longer than a bloom is refused rather than overrunning the one it fills. + #[test] + fn no_variation_on_a_receipt_panics() { + use alloy_rlp::Encodable; + let log = Log { + address: Address::repeat_byte(0x11), + topics: vec![B256::repeat_byte(0x22), B256::repeat_byte(0x33)], + data: Bytes::from(vec![0x44; 40]), + }; + let receipts = [ + Receipt { + post_tx_state: vec![1], + cumulative_gas_used: 42_000, + gas_used: 21_000, + logs_bloom: Bloom::repeat_byte(0x05), + logs: vec![log.clone(), log], + status: true, + }, + Receipt { + post_tx_state: Vec::new(), + cumulative_gas_used: 21_000, + gas_used: 21_000, + logs_bloom: Bloom::ZERO, + logs: Vec::new(), + status: false, + }, + ]; + for (i, receipt) in receipts.iter().enumerate() { + let mut seed = Vec::new(); + receipt.encode(&mut seed); + assert!(Receipt::decode(&mut seed.as_slice()).is_ok()); + for bytes in crate::mutate::mutants(&seed, 2_000, 400 + i as u64) { + let _ = Receipt::decode(&mut bytes.as_slice()); + } + } + + let mut payload = Vec::new(); + [1u8].as_slice().encode(&mut payload); + 21_000u64.encode(&mut payload); + [0u8; 300].as_slice().encode(&mut payload); + RlpHeader { + list: true, + payload_length: 0, + } + .encode(&mut payload); + 21_000u64.encode(&mut payload); + [1u8].as_slice().encode(&mut payload); + let mut long_bloom = Vec::new(); + RlpHeader { + list: true, + payload_length: payload.len(), + } + .encode(&mut long_bloom); + long_bloom.extend(payload); + assert!(Receipt::decode(&mut long_bloom.as_slice()).is_err()); + } } diff --git a/crates/core/src/types/transaction.rs b/crates/core/src/types/transaction.rs index 928ac89..915c880 100644 --- a/crates/core/src/types/transaction.rs +++ b/crates/core/src/types/transaction.rs @@ -312,8 +312,11 @@ impl Transaction { let hash = self.signing_hash(chain_id); let recovery_id = if self.is_eip155(chain_id) { - let rid = self.v as i64 - chain_id as i64 * 2 - 35; - if !(0..=1).contains(&rid) { + // `is_eip155` holds `v` at `chain_id * 2 + 35` or more, so this is + // the id, or past 1. Unsigned: in an `i64`, a `v` of 2^63 or more + // went negative, and the subtraction overflowed. + let rid = self.v - (chain_id * 2 + 35); + if rid > 1 { anyhow::bail!("invalid EIP-155 v value: {} for chain_id {}", self.v, chain_id); } rid as u8 @@ -850,3 +853,72 @@ mod remasc_derivation_tests { bytes.iter().map(|b| format!("{b:02x}")).collect() } } + +#[cfg(test)] +mod untrusted_input_tests { + use super::*; + use alloy_primitives::U256; + use alloy_rlp::Decodable; + + /// No variation on a signed transaction's bytes makes decoding it, or + /// working out who sent it, panic; and no `v` at all does. + #[test] + fn no_variation_on_a_transaction_panics() { + use k256::ecdsa::SigningKey; + let key = SigningKey::from_slice(&[1u8; 32]).unwrap(); + let mut seeds = Vec::new(); + for (chain_id, eip155) in [(33u64, true), (30, true), (31, false)] { + let mut tx = Transaction { + nonce: 7, + gas_price: U256::from(60_000_000u64), + gas_limit: U256::from(21_000u64), + to: Bytes::from(vec![0xbb; 20]), + value: U256::from(100u64), + input: Bytes::from(vec![0xa9, 0x05, 0x9c, 0xbb]), + ..Default::default() + }; + let hash = if eip155 { + tx.signing_hash_eip155(chain_id) + } else { + tx.signing_hash_legacy() + }; + let (signature, id) = key.sign_prehash_recoverable(hash.as_slice()).unwrap(); + let rs = signature.to_bytes(); + tx.r = U256::from_be_slice(&rs[..32]); + tx.s = U256::from_be_slice(&rs[32..]); + tx.v = if eip155 { chain_id * 2 + 35 } else { 27 } + u64::from(id.to_byte()); + assert!(tx.recover_sender(chain_id).is_ok()); + seeds.push(alloy_rlp::encode(&tx)); + } + for (i, seed) in seeds.iter().enumerate() { + for bytes in crate::mutate::mutants(seed, 1_000, 300 + i as u64) { + let Ok(tx) = Transaction::decode(&mut bytes.as_slice()) else { + continue; + }; + for chain_id in [30, 31, 33] { + let _ = tx.recover_sender(chain_id); + let _ = tx.recover_compressed_pubkey(chain_id); + let _ = tx.btc_sender_hash160(chain_id); + } + let _ = tx.tx_hash(); + let _ = tx.rlp_for_trie(); + } + } + // None of these is a recovery id on any network; 2^63 and up once + // overflowed the sum that finds it. + for v in [0, 26, 29, 34, 94, 99, 100, 103, 1 << 31, 1 << 63, u64::MAX] { + let tx = Transaction { + v, + r: U256::from(1u64), + s: U256::from(1u64), + ..Default::default() + }; + for chain_id in [30, 31, 33] { + assert!( + tx.recover_sender(chain_id).is_err(), + "v {v} on chain {chain_id}" + ); + } + } + } +} diff --git a/crates/core/src/validation/tests.rs b/crates/core/src/validation/tests.rs index 930b865..bb7b85b 100644 --- a/crates/core/src/validation/tests.rs +++ b/crates/core/src/validation/tests.rs @@ -2143,3 +2143,29 @@ fn fork_detection_data_joins_the_hash_from_wasabi100_and_block_449() { } } } + +/// No variation on a real header's bytes makes decoding, hashing or checking +/// it panic: mainnet's first fallback-mined blocks, its last whose coinbase a +/// partial merkle tree proves, and a merge-mined block of difficulty 1, whose +/// proof of work most variations still meet, so the rest of the rule runs. +#[test] +fn no_variation_on_a_real_header_panics() { + let mut mined = create_dummy_header(10, 1000, B256::ZERO); + mined.difficulty = U256::from(1); + let (btc, coinbase, proof) = build_mm_proof(&mined, 0x207fffff, None); + mined.bitcoin_merged_mining_header = Some(btc); + mined.bitcoin_merged_mining_coinbase_transaction = Some(coinbase); + mined.bitcoin_merged_mining_merkle_proof = Some(proof); + let seeds = [ + mainnet_rlp(1), + mainnet_rlp(2), + hex::decode(MAINNET_728999).unwrap(), + alloy_rlp::encode(&mined), + ]; + for (i, seed) in seeds.iter().enumerate() { + crate::mutate::decode_and_check_header(seed); + for bytes in crate::mutate::mutants(seed, 500, 1 + i as u64) { + crate::mutate::decode_and_check_header(&bytes); + } + } +} diff --git a/crates/execution/src/bridge/mod.rs b/crates/execution/src/bridge/mod.rs index 0453296..2ac55d4 100644 --- a/crates/execution/src/bridge/mod.rs +++ b/crates/execution/src/bridge/mod.rs @@ -437,6 +437,12 @@ static BRIDGE_SELECTOR_MAP: LazyLock> = LazyLock::new(|| .collect() }); +/// Every Bridge method's selector, enabled or not, for tests that call each. +#[cfg(test)] +pub(crate) fn every_selector() -> Vec<[u8; 4]> { + BRIDGE_METHODS.iter().map(|m| m.selector).collect() +} + /// Look up a Bridge method by its 4-byte ABI selector. pub fn find_bridge_method(selector: &[u8; 4]) -> Option<&'static BridgeMethodInfo> { BRIDGE_SELECTOR_MAP.get(selector).map(|&i| &BRIDGE_METHODS[i]) diff --git a/crates/execution/src/bridge/peg.rs b/crates/execution/src/bridge/peg.rs index 5690465..0cbd042 100644 --- a/crates/execution/src/bridge/peg.rs +++ b/crates/execution/src/bridge/peg.rs @@ -7299,6 +7299,207 @@ mod tests { ); } + /// **No Bitcoin transaction a peg-in can carry panics the Bridge.** + /// + /// Anyone can have a transaction of their own making mined into a Bitcoin + /// block and register it with a proof of inclusion, so every script, + /// witness push and output in it is theirs to choose: Bitcoin's consensus + /// asks for an input and leaves scripts free. A panic in reading one would + /// stop every node at the block that registers it. Each seed shape is varied + /// script by script and push by push, and each variation given to + /// everything that reads a peg-in or a federator's signature. + #[test] + fn no_variation_on_a_pegin_panics_the_bridge() { + use super::super::release_tx; + + struct Rng(u64); + impl Rng { + fn next(&mut self) -> u64 { + self.0 ^= self.0 << 13; + self.0 ^= self.0 >> 7; + self.0 ^= self.0 << 17; + self.0 + } + fn below(&mut self, n: usize) -> usize { + (self.next() % n.max(1) as u64) as usize + } + } + + // Bytes that mean something to a script reader: pushes of each size + // and their limits, small numbers, and the opcodes multisig, ERP, + // flyover and OP_RETURN scripts are built from. + const OPCODES: [u8; 26] = [ + 0x00, 0x01, 0x02, 0x14, 0x20, 0x21, 0x41, 0x47, 0x48, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, + 0x51, 0x52, 0x60, 0x63, 0x67, 0x68, 0x6a, 0x75, 0x87, 0xa9, 0xae, 0xb2, + ]; + + fn vary(bytes: &mut Vec, rng: &mut Rng) { + for _ in 0..1 + rng.below(3) { + let at = rng.below(bytes.len() + 1); + match rng.below(7) { + 0 if at < bytes.len() => bytes[at] ^= 1 << rng.below(8), + 1 if at < bytes.len() => bytes[at] = OPCODES[rng.below(OPCODES.len())], + 2 => bytes.insert(at, OPCODES[rng.below(OPCODES.len())]), + 3 => bytes.insert(at, rng.next() as u8), + 4 if at < bytes.len() => { + let n = 1 + rng.below(8.min(bytes.len() - at)); + bytes.drain(at..at + n); + } + 5 => bytes.truncate(at), + _ => { + let n = rng.below(80); + let fill = rng.next() as u8; + bytes.splice(at..at, std::iter::repeat_n(fill, n)); + } + } + } + } + + fn read_like_the_bridge(tx: &BtcTransaction) { + if let Ok(Some(sender)) = classify_pegin_sender(tx) { + let _ = sender_refund_target(&sender); + let _ = sender_type_name(Some(&sender)); + } + let _ = undetermined_input_shape(tx); + let _ = super::super::pegin_instructions::build_pegin_instructions(tx); + let _ = btc_txid_event_bytes(tx); + let _ = multisig_txid_without_signatures(tx); + let _ = release_tx::has_enough_signatures(tx); + let raw = bitcoin::consensus::serialize(tx); + let _ = has_inputs(&raw, true); + let _ = has_inputs(&raw, false); + let _ = super::super::tx::calculate_btc_tx_hash(&raw); + let mut scripts: Vec<&[u8]> = Vec::new(); + for input in &tx.input { + scripts.push(input.script_sig.as_bytes()); + scripts.extend(input.witness.iter()); + } + scripts.extend( + tx.output + .iter() + .map(|output| output.script_pubkey.as_bytes()), + ); + for script in scripts { + let _ = release_tx::parse_chunks(script); + let _ = p2sh_script_hash160(script); + let _ = is_der_signature(script); + let _ = is_btc_public_key(script); + let _ = compress_pubkey(script); + let _ = super::super::rskj_sender_compat::is_sent_to_multisig(script); + if let Some(redeem) = release_tx::extract_redeem_script(script) { + let _ = super::super::rskj_sender_compat::is_sent_to_multisig(&redeem); + let _ = release_tx::is_erp_redeem(&redeem); + } + } + } + + // A peg-in with outputs: an RSKT OP_RETURN carrying instructions with a + // refund address, a P2SH output and a P2PKH one. + let mut with_outputs = p2pkh_tx(); + let mut payload = vec![0x52, 0x53, 0x4b, 0x54, 0x01]; + payload.extend_from_slice(&[0x11; 20]); + payload.push(0x02); + payload.extend_from_slice(&[0x22; 20]); + let mut op_return = vec![0x6a, payload.len() as u8]; + op_return.extend_from_slice(&payload); + for script in [ + op_return, + p2sh_output_script_program(&[0x33; 20]), + p2pkh_output_script(&[0x44; 20]).to_bytes(), + ] { + with_outputs.output.push(bitcoin::TxOut { + value: bitcoin::Amount::from_sat(1_000_000), + script_pubkey: bitcoin::ScriptBuf::from_bytes(script), + }); + } + // A flyover peg-in: its redeem is the derivation hash, OP_DROP and + // the federation's multisig. + let flyover = { + let redeem = flyover_redeem_script(&[0x55; 32], &multisig_redeem()); + let mut script = vec![0x00, 71]; + script.extend_from_slice(&[0x30; 71]); + script.push(0x4c); + script.push(redeem.len() as u8); + script.extend_from_slice(&redeem); + tx_with(script, vec![]) + }; + let seeds = [ + p2pkh_tx(), + p2sh_p2wpkh_tx(), + p2sh_multisig_tx(), + p2sh_p2wsh_tx(), + with_outputs, + flyover, + ]; + + let mut rng = Rng(0xA076_1D64_78BD_642F); + let mut varied = 0; + for seed in &seeds { + read_like_the_bridge(seed); + for _ in 0..1_500 { + let mut tx = seed.clone(); + for _ in 0..1 + rng.below(3) { + match rng.below(6) { + 0 => { + let i = rng.below(tx.input.len()); + let mut script = tx.input[i].script_sig.to_bytes(); + vary(&mut script, &mut rng); + tx.input[i].script_sig = bitcoin::ScriptBuf::from_bytes(script); + } + 1 => { + let i = rng.below(tx.input.len()); + let mut pushes: Vec> = + tx.input[i].witness.iter().map(<[u8]>::to_vec).collect(); + match rng.below(3) { + 0 if !pushes.is_empty() => { + let p = rng.below(pushes.len()); + vary(&mut pushes[p], &mut rng); + } + 1 if !pushes.is_empty() => { + pushes.remove(rng.below(pushes.len())); + } + _ => { + let mut push = multisig_redeem(); + vary(&mut push, &mut rng); + let at = rng.below(pushes.len() + 1); + pushes.insert(at, push); + } + } + tx.input[i].witness = bitcoin::Witness::from_slice(&pushes); + } + 2 if !tx.output.is_empty() => { + let o = rng.below(tx.output.len()); + let mut script = tx.output[o].script_pubkey.to_bytes(); + vary(&mut script, &mut rng); + tx.output[o].script_pubkey = bitcoin::ScriptBuf::from_bytes(script); + } + 3 => { + let copy = tx.input[rng.below(tx.input.len())].clone(); + tx.input.push(copy); + } + 4 if tx.input.len() > 1 => { + tx.input.remove(rng.below(tx.input.len())); + } + _ => { + let mut script = vec![0x6a]; + let mut data = payload.clone(); + vary(&mut data, &mut rng); + script.push(data.len().min(75) as u8); + script.extend_from_slice(&data); + tx.output.push(bitcoin::TxOut { + value: bitcoin::Amount::ZERO, + script_pubkey: bitcoin::ScriptBuf::from_bytes(script), + }); + } + } + } + read_like_the_bridge(&tx); + varied += 1; + } + } + assert_eq!(varied, seeds.len() * 1_500); + } + /// The P2PKH branch accepts only a real curve point. rskj gets this from /// BtcECKey.fromPublicOnly throwing; rustock from `compress_pubkey` /// returning None. Without it a two-chunk scriptSig whose second push is diff --git a/crates/execution/src/executor.rs b/crates/execution/src/executor.rs index 5597c9c..dc564a8 100644 --- a/crates/execution/src/executor.rs +++ b/crates/execution/src/executor.rs @@ -7662,6 +7662,285 @@ mod tests { assert_eq!(result.tx_results[0].gas_used, enough); } + /// **No call to a precompile panics the node.** + /// + /// Anyone can send a transaction, or ask for an `eth_call`, that calls a + /// precompile with any bytes at all. Each precompile is called here by a + /// transaction of its own, from a contract (CALL, STATICCALL and + /// DELEGATECALL) and as a local call, with random payloads, with words at + /// the edges of ABI decoding, and with each of its methods' selectors + /// followed by such words. The callers are a stranger and the federators + /// and authorizers of every network, so the Bridge's guarded methods read + /// their arguments too, and the rules are genesis's, mainnet's between + /// Papyrus and Iris, and everything on. What each call answers is the + /// other tests' business; that the node answers is this one's. + #[test] + fn no_call_to_a_precompile_panics() { + use crate::bridge::constants::BridgeConstants; + use crate::precompiles as p; + use std::panic::{catch_unwind, AssertUnwindSafe}; + + struct Rng(u64); + impl Rng { + fn next(&mut self) -> u64 { + self.0 ^= self.0 << 13; + self.0 ^= self.0 >> 7; + self.0 ^= self.0 << 17; + self.0 + } + fn below(&mut self, n: usize) -> usize { + (self.next() % n.max(1) as u64) as usize + } + fn bytes(&mut self, n: usize) -> Vec { + (0..n).map(|_| self.next() as u8).collect() + } + /// An ABI word: an offset, a length, a limit of an integer type, an + /// address, a block number, or noise. + fn word(&mut self) -> [u8; 32] { + let small = |v: u64| U256::from(v).to_be_bytes::<32>(); + match self.below(11) { + 0 => [0; 32], + 1 => small([0x20, 0x40, 0x60, 0x80, 0xa0, 0xc0][self.below(6)]), + 2 => small( + [1, 2, 20, 31, 32, 33, 64, 65, 100, 255, 256, 1 << 20][self.below(12)], + ), + 3 => small( + [ + i32::MAX as u64, + 1 << 31, + u32::MAX as u64, + 1 << 32, + i64::MAX as u64, + 1 << 63, + u64::MAX, + ][self.below(7)], + ), + 4 => [0xff; 32], + 5 => { + let mut word = [0u8; 32]; + word[0] = 0x80; + word + } + 6 => { + let mut word = [0xff; 32]; + word[0] = 0x7f; + word + } + 7 => { + let mut word = [0u8; 32]; + word[12..].copy_from_slice(&self.bytes(20)); + word + } + 8 => small(self.next() % 10_000_000), + _ => { + let mut word = [0u8; 32]; + word.copy_from_slice(&self.bytes(32)); + word + } + } + } + } + + /// A contract that passes its calldata on to `target` with `opcode` + /// and returns whatever comes back. + fn forwarder(target: Address, opcode: u8) -> Vec { + let mut code = vec![0x36, 0x60, 0x00, 0x60, 0x00, 0x37]; // calldatacopy(0, 0, size) + code.extend([0x60, 0x00, 0x60, 0x00, 0x36, 0x60, 0x00]); // out 0,0; in 0,size + if opcode == 0xf1 { + code.extend([0x60, 0x00]); // no value + } + code.push(0x73); + code.extend_from_slice(target.as_slice()); + code.extend([0x5a, opcode, 0x50]); // gas, call, drop the flag + code.extend([0x3d, 0x60, 0x00, 0x60, 0x00, 0x3e]); // returndatacopy(0, 0, size) + code.extend([0x3d, 0x60, 0x00, 0xf3]); // return(0, size) + code + } + + let numbered = |n: u8| { + let mut bytes = [0u8; 20]; + bytes[19] = n; + Address::from(bytes) + }; + let mut targets: Vec<(Address, Vec<[u8; 4]>)> = + (1..=9).map(|n| (numbered(n), Vec::new())).collect(); + targets.extend([ + (p::BRIDGE_ADDR, crate::bridge::every_selector()), + ( + p::REMASC_ADDR, + vec![crate::abi::selector("processMinersFees()")], + ), + ( + p::HD_WALLET_UTILS_ADDR, + crate::native::hd_wallet::every_selector(), + ), + ( + p::BLOCK_HEADER_ADDR, + crate::native::block_header::every_selector(), + ), + ( + p::ENVIRONMENT_ADDR, + vec![crate::abi::selector("getCallStackDepth()")], + ), + (p::SECP256K1_ADD_ADDR, Vec::new()), + (p::SECP256K1_MUL_ADDR, Vec::new()), + ]); + + let mut senders = vec![Address::repeat_byte(0xAA)]; + for constants in [ + BridgeConstants::mainnet(), + BridgeConstants::testnet(), + BridgeConstants::regtest(), + ] { + for keys in [ + constants.genesis_federation_public_keys, + constants.authorized_free_tx_keys, + constants.whitelist_authorizer_keys, + constants.fee_per_kb_authorizer_keys, + constants.increase_locking_cap_authorizer_keys, + constants.federation_change_authorizer_keys, + ] { + senders.extend(keys.iter().take(2).filter_map(|key| { + let key = alloy_primitives::hex::decode(key).ok()?; + crate::bridge::federation::rsk_address_from_public_key(&key) + })); + } + } + senders.sort(); + senders.dedup(); + + let store = Arc::new(MemoryTrieStore::new()); + let one_rbtc = U256::from(10u64).pow(U256::from(18)); + let mut root = TrieNode::empty(); + for sender in &senders { + root = put_account(&root, store.as_ref(), sender, 0, one_rbtc); + } + const VIA: [(&str, u8); 3] = [("CALL", 0xf1), ("STATICCALL", 0xfa), ("DELEGATECALL", 0xf4)]; + let mut forwarders = Vec::new(); + for (i, (target, _)) in targets.iter().enumerate() { + let mut at = [Address::ZERO; 3]; + for (j, (_, opcode)) in VIA.iter().enumerate() { + let mut bytes = [0xEE; 20]; + bytes[0] = j as u8; + bytes[1] = i as u8; + at[j] = Address::from(bytes); + let code = forwarder(*target, *opcode); + root = put_account_with_code(&root, store.as_ref(), &at[j], 1, U256::ZERO, &code); + } + forwarders.push(at); + } + let dir = tempfile::tempdir().unwrap(); + let block_store = Arc::new(BlockStore::open(dir.path()).unwrap()); + + let mut rng = Rng(0x2545_F491_4F6C_DD1D); + let mut calls = 0usize; + let mut outcomes = [0usize; 4]; + for (rules, height) in [ + (RskHardforkConfig::mainnet(), 1u64), + (RskHardforkConfig::mainnet(), 3_000_000), + (RskHardforkConfig::testnet(), 5_000_000), + (RskHardforkConfig::all_active_parallel(33), 8_000_000), + ] { + let executor = RskExecutor::new(rules, block_store.clone()); + let header = dummy_header(height); + for (i, (target, selectors)) in targets.iter().enumerate() { + let mut payloads = vec![Vec::new()]; + for _ in 0..40 { + let n = rng.below(300); + payloads.push(rng.bytes(n)); + } + for _ in 0..40 { + let words = rng.below(8); + payloads.push((0..words).flat_map(|_| rng.word()).collect()); + } + // Blake2F's exact 213 bytes, with few enough rounds to run. + let mut blake = rng.bytes(213); + blake[..4].copy_from_slice(&(rng.below(16) as u32).to_be_bytes()); + blake[212] = rng.below(3) as u8; + payloads.push(blake); + for selector in selectors { + payloads.push(selector.to_vec()); + for _ in 0..20 { + let mut payload = selector.to_vec(); + for _ in 0..rng.below(7) { + payload.extend(rng.word()); + } + if rng.below(3) == 0 { + let n = rng.below(70); + payload.extend(rng.bytes(n)); + } + payloads.push(payload); + } + } + + for payload in payloads { + let sender = senders[rng.below(senders.len())]; + let value = if rng.below(4) == 0 { + U256::from(rng.below(3)) + } else { + U256::ZERO + }; + let local = rng.below(4) == 0; + let via = rng.below(VIA.len() + 1); + let (to, how) = match via { + 0 => (*target, "directly"), + _ => (forwarders[i][via - 1], VIA[via - 1].0), + }; + let tx = rustock_core::Transaction { + nonce: 0, + gas_price: U256::from(60_000_000u64), + gas_limit: U256::from(2_000_000u64), + to: Bytes::copy_from_slice(to.as_slice()), + value, + input: Bytes::copy_from_slice(&payload), + v: 0, + r: U256::ZERO, + s: U256::ZERO, + cached_rlp: None, + }; + let answered = catch_unwind(AssertUnwindSafe(|| { + let shape = |tx: &TxExecutionResult| (tx.success, !tx.output.is_empty()); + if local { + executor + .execute_local_call(&header, &tx, sender, &root, store.clone()) + .map(|tx| shape(&tx)) + } else { + executor + .execute_block( + &header, + &[(tx.clone(), sender)], + &root, + store.clone(), + ) + .map(|block| shape(&block.tx_results[0])) + } + })); + assert!( + answered.is_ok(), + "{target} panicked at #{height}, called {how} by {sender}{} with 0x{}", + if local { " as a local call" } else { "" }, + alloy_primitives::hex::encode(&payload) + ); + match answered.unwrap() { + Ok((true, true)) => outcomes[0] += 1, + Ok((true, false)) => outcomes[1] += 1, + Ok((false, _)) => outcomes[2] += 1, + Err(_) => outcomes[3] += 1, + } + calls += 1; + } + } + } + // A fuzz whose calls never got past their own parsing would pass + // without testing anything, so some must have run to an answer. + let [returned, ..] = outcomes; + assert!(calls > 10_000, "only {calls} calls"); + assert!( + returned > calls / 10, + "only {returned} of {calls} calls returned anything: {outcomes:?}" + ); + } + /// BN128 refusing a point (`BN128PrecompiledContract`): before RSKIP197 it /// answers with no bytes, a plain success; from RSKIP197 it throws, which /// in a transaction's own call is the invisible exception. diff --git a/crates/execution/src/native/block_header.rs b/crates/execution/src/native/block_header.rs index 3ee25ff..5cb71d1 100644 --- a/crates/execution/src/native/block_header.rs +++ b/crates/execution/src/native/block_header.rs @@ -42,6 +42,15 @@ pub(crate) enum Method { CumulativeWork, } +/// Every method's selector, for tests that call each. +#[cfg(test)] +pub(crate) fn every_selector() -> Vec<[u8; 4]> { + METHODS + .iter() + .map(|(signature, _)| crate::abi::selector(signature)) + .collect() +} + /// `BlockHeaderContract.getMethods`, by signature. const METHODS: [(&str, Method); 11] = [ ("getCoinbaseAddress(int256)", Method::CoinbaseAddress), diff --git a/crates/execution/src/native/hd_wallet.rs b/crates/execution/src/native/hd_wallet.rs index 7de9713..15e8310 100644 --- a/crates/execution/src/native/hd_wallet.rs +++ b/crates/execution/src/native/hd_wallet.rs @@ -34,6 +34,15 @@ enum Method { GetMultisigScriptHash, } +/// Every method's selector, for tests that call each. +#[cfg(test)] +pub(crate) fn every_selector() -> Vec<[u8; 4]> { + METHODS + .iter() + .map(|(signature, _)| crate::abi::selector(signature)) + .collect() +} + /// `HDWalletUtils.getMethods`, by signature. const METHODS: [(&str, Method); 4] = [ ("toBase58Check(bytes,int256)", Method::ToBase58Check), diff --git a/crates/networking/src/discovery/table.rs b/crates/networking/src/discovery/table.rs index 89b9349..601fb78 100644 --- a/crates/networking/src/discovery/table.rs +++ b/crates/networking/src/discovery/table.rs @@ -1,5 +1,5 @@ -use alloy_primitives::B512; use crate::discovery::message::DiscoveryNode; +use alloy_primitives::{keccak256, B256, B512}; use std::time::{Duration, Instant}; pub const BUCKET_SIZE: usize = 16; @@ -25,6 +25,8 @@ struct BucketEntry { pub struct NodeTable { local_id: B512, + /// `local_id`'s [`table_key`], which every distance is measured from. + local_key: B256, buckets: Vec>, } @@ -32,6 +34,7 @@ impl NodeTable { pub fn new(local_id: B512) -> Self { Self { local_id, + local_key: table_key(&local_id), // `BucketEntry` is intentionally not `Clone`, so build the buckets // with an iterator instead of the `vec![_; N]` (clone) macro. buckets: (0..NUM_BUCKETS).map(|_| Vec::new()).collect(), @@ -43,11 +46,14 @@ impl NodeTable { return false; } - let distance = self.xor_distance(&node.id); - let bucket_idx = self.distance_to_bucket(distance); + // rskj `NodeDistanceTable.getNodeBucket`: the distance less one, so + // 0 to 255, and a node's ID is whatever a peer said it was. + let bucket_idx = distance(&self.local_key, &table_key(&node.id)).saturating_sub(1); let now = Instant::now(); - let bucket = &mut self.buckets[bucket_idx]; + let Some(bucket) = self.buckets.get_mut(bucket_idx) else { + return false; + }; if let Some(pos) = bucket.iter().position(|e| e.node.id == node.id) { // Known node: refresh recency (move to end) and last-seen time. @@ -75,8 +81,14 @@ impl NodeTable { } pub fn closest_nodes(&self, target: &B512, count: usize) -> Vec { - let mut nodes: Vec<_> = self.buckets.iter().flat_map(|b| b.iter().map(|e| e.node.clone())).collect(); - nodes.sort_by_cached_key(|n| self.xor_distance_between(&n.id, target)); + let mut nodes: Vec<_> = self + .buckets + .iter() + .flat_map(|b| b.iter().map(|e| e.node.clone())) + .collect(); + // rskj `getClosestNodes`, sorted by `NodeDistanceComparator`. + let target = table_key(target); + nodes.sort_by_cached_key(|n| distance(&table_key(&n.id), &target)); nodes.truncate(count); nodes } @@ -180,29 +192,6 @@ impl NodeTable { Ok(self.add_node(node)) } - fn xor_distance(&self, other: &B512) -> [u8; 64] { - self.xor_distance_between(&self.local_id, other) - } - - fn xor_distance_between(&self, a: &B512, b: &B512) -> [u8; 64] { - let mut res = [0u8; 64]; - let a_bytes = a.as_slice(); - let b_bytes = b.as_slice(); - for i in 0..64 { - res[i] = a_bytes[i] ^ b_bytes[i]; - } - res - } - - fn distance_to_bucket(&self, distance: [u8; 64]) -> usize { - for (i, byte) in distance.iter().enumerate() { - if *byte != 0 { - return (i * 8) + (7 - byte.leading_zeros() as usize); - } - } - 0 - } - /// Test helper: age every entry past the eviction threshold so the next /// `add_node` into a full bucket treats the oldest entry as dead. #[cfg(test)] @@ -218,25 +207,58 @@ impl NodeTable { } } +/// The key rskj's Kademlia table orders a node by: `keccak256(keccak256(id))` +/// (`DistanceCalculator.calculateDistance`). +fn table_key(id: &B512) -> B256 { + keccak256(keccak256(id)) +} + +/// rskj's distance between two [`table_key`]s: 256 less the leading zero bits +/// of their XOR, so 0 for the same node and 256 for keys whose first bits +/// differ (`DistanceCalculator.msbPosition`). +fn distance(a: &B256, b: &B256) -> usize { + let mut leading_zeros = 0; + for (x, y) in a.iter().zip(b.iter()) { + let byte = x ^ y; + if byte != 0 { + return 256 - leading_zeros - byte.leading_zeros() as usize; + } + leading_zeros += 8; + } + 0 +} + #[cfg(test)] mod tests { use super::*; use alloy_primitives::Bytes; - /// All these IDs share the top bit set (byte 0 = 0x80) and differ only in - /// the last byte, so they collide in the same bucket — letting us fill one. - fn node_in_shared_bucket(tag: u8) -> DiscoveryNode { - let mut id = [0u8; 64]; - id[0] = 0x80; - id[63] = tag; + fn node(id: B512) -> DiscoveryNode { DiscoveryNode { ip: Bytes::from(vec![10, 0, 0, 1]), udp_port: 5050, tcp_port: 5050, - id: B512::from_slice(&id), + id, } } + /// The `tag`-th ID, counting by its last two bytes, whose table key differs + /// from the zero ID's in its first bit: all of them fall in bucket 255, so + /// a test can fill one bucket. + fn node_in_shared_bucket(tag: u8) -> DiscoveryNode { + let local = table_key(&B512::ZERO); + let id = (0..=u16::MAX) + .map(|n| { + let mut id = [0u8; 64]; + id[62..].copy_from_slice(&n.to_be_bytes()); + B512::from(id) + }) + .filter(|id| distance(&local, &table_key(id)) == 256) + .nth(usize::from(tag)) + .expect("half of all IDs fall in bucket 255"); + node(id) + } + #[test] fn full_bucket_keeps_fresh_nodes_and_rejects_newcomer() { let mut table = NodeTable::new(B512::ZERO); @@ -287,4 +309,60 @@ mod tests { assert!(table.all_nodes().iter().any(|n| n.id == node_in_shared_bucket(0).id)); assert!(!table.all_nodes().iter().any(|n| n.id == node_in_shared_bucket(1).id)); } + + /// rskj's `DistanceCalculatorTest` (a node is at distance 0 from itself, and + /// distance is symmetric), and the edges of `msbPosition`. + #[test] + fn distance_is_rskjs() { + let (a, b) = (B512::from([0x82; 64]), B512::with_last_byte(1)); + assert_eq!(distance(&table_key(&a), &table_key(&a)), 0); + assert_eq!( + distance(&table_key(&a), &table_key(&b)), + distance(&table_key(&b), &table_key(&a)) + ); + assert_eq!(distance(&B256::ZERO, &B256::with_last_byte(1)), 1); + assert_eq!(distance(&B256::ZERO, &B256::with_last_byte(0x80)), 8); + let mut first = [0u8; 32]; + first[0] = 0x01; + assert_eq!(distance(&B256::ZERO, &B256::from(first)), 249); + first[0] = 0x80; + assert_eq!(distance(&B256::ZERO, &B256::from(first)), 256); + } + + /// A peer names nodes by any 64 bytes it likes, among them IDs that match + /// ours in all but one bit, and each has a bucket. + #[test] + fn every_id_a_peer_can_name_has_a_bucket() { + let local = B512::from([0x5A; 64]); + let mut table = NodeTable::new(local); + for byte in 0..64 { + for bit in 0..8 { + let mut id = local.0; + id[byte] ^= 1 << bit; + table.add_node(node(B512::from(id))); + } + } + assert!(!table.is_empty()); + } + + /// rskj's `getClosestNodes`: the nodes nearest the target by rskj's distance. + #[test] + fn closest_nodes_are_nearest_by_rskjs_distance() { + let mut table = NodeTable::new(B512::ZERO); + for tag in 1..=40 { + table.add_node(node(B512::with_last_byte(tag))); + } + let target = table_key(&B512::with_last_byte(77)); + let distance_to = |n: &DiscoveryNode| distance(&table_key(&n.id), &target); + let closest = table.closest_nodes(&B512::with_last_byte(77), 10); + assert_eq!(closest.len(), 10); + assert!(closest + .windows(2) + .all(|w| distance_to(&w[0]) <= distance_to(&w[1]))); + let farthest = distance_to(&closest[9]); + assert!(table + .all_nodes() + .iter() + .all(|n| closest.iter().any(|c| c.id == n.id) || distance_to(n) >= farthest)); + } } diff --git a/crates/networking/src/node.rs b/crates/networking/src/node.rs index dd07d58..38cf3cb 100644 --- a/crates/networking/src/node.rs +++ b/crates/networking/src/node.rs @@ -480,7 +480,21 @@ pub(crate) async fn register_and_run_session( // Request peer list immediately after connecting peer_store.send_to_peer(&peer_id, P2pMessage::GetPeers).await; - let res = session.run().await; + // A panic in the session -- a decoder or handler meeting input nothing + // anticipated -- ends this peer's session and no more, so the peer is + // still removed below. Unwinding past here instead would leave it + // registered, holding a peer slot for good. rskj likewise closes the + // channel on an exception (`exceptionCaught`) without scoring the peer. + let res = match futures::FutureExt::catch_unwind(std::panic::AssertUnwindSafe(session.run())) + .await + { + Ok(res) => res, + Err(panic) => { + let what = panic_message(panic.as_ref()); + error!(target: "rustock::net", "Session with {:?} panicked, disconnecting: {}", &peer_id.0[..4], what); + Err(anyhow::anyhow!("session panicked: {what}")) + } + }; peer_store.remove_peer(&peer_id).await; if let Some(scoring) = &scoring { // rskj records DISCONNECTION on every session end, clean or not. It @@ -497,6 +511,15 @@ pub(crate) async fn register_and_run_session( res } +/// What a caught panic said, for the log. +pub(crate) fn panic_message(panic: &(dyn std::any::Any + Send)) -> String { + panic + .downcast_ref::<&str>() + .map(|s| (*s).to_string()) + .or_else(|| panic.downcast_ref::().cloned()) + .unwrap_or_else(|| "a panic with no message".to_string()) +} + /// Maximum time allowed for a connection handshake (RLPx + p2p Hello + status) /// to complete. Without this, a peer that opens a TCP connection and never /// speaks would pin a task and socket forever (rskj relies on Netty's @@ -761,3 +784,66 @@ mod isolation_tests { assert!(should_persist_table(false), "an ordinary node still saves what it learned"); } } + +#[cfg(test)] +mod session_panic_tests { + use super::*; + use crate::codec::P2pCodec; + use crate::handshake::{HandshakeCodec, PeerCapabilities}; + use futures::SinkExt; + use tokio_util::codec::Framed; + + /// A handler that meets a message it never expected. + struct Panics; + + impl P2pHandler for Panics { + fn handle_message(&self, _id: B512, _msg: &P2pMessage) -> Option { + panic!("a handler met input it did not expect") + } + } + + /// A panicking session ends, and its peer is still removed, so it can't + /// hold a peer slot after it is gone. + #[tokio::test] + async fn a_session_that_panics_ends_and_its_peer_is_removed() { + let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); + let address = listener.local_addr().unwrap(); + let store = Arc::new(PeerStore::new()); + let peer = B512::repeat_byte(7); + let server = tokio::spawn({ + let store = store.clone(); + async move { + let (stream, _) = listener.accept().await.unwrap(); + let status = RskStatus { + best_block_number: 1, + best_block_hash: B256::ZERO, + best_block_parent_hash: None, + total_difficulty: None, + earliest_block: None, + }; + register_and_run_session( + peer, + status, + PeerCapabilities::default(), + Framed::new(stream, HandshakeCodec::Plain(P2pCodec)), + vec![Arc::new(Panics)], + store, + None, + None, + ) + .await + } + }); + let mut client = Framed::new( + TcpStream::connect(address).await.unwrap(), + HandshakeCodec::Plain(P2pCodec), + ); + client.send(P2pMessage::GetPeers).await.unwrap(); + let ended = tokio::time::timeout(std::time::Duration::from_secs(10), server) + .await + .expect("the session ends") + .expect("the task does not unwind"); + assert!(ended.is_err(), "the panic is reported"); + assert_eq!(store.count().await, 0, "the peer is removed"); + } +} diff --git a/crates/networking/src/outbound.rs b/crates/networking/src/outbound.rs index dab38c2..61ef12e 100644 --- a/crates/networking/src/outbound.rs +++ b/crates/networking/src/outbound.rs @@ -116,7 +116,10 @@ impl OutboundConnector { let mut last_starved: Option<(usize, usize, usize, usize)> = None; - let mut last_starved_at = Instant::now() - STARVED_REPEAT; + // `None` until the first warning. Subtracting a minute from `now` would + // panic on a machine booted less than a minute ago, since the clock + // `Instant` reads starts at boot. + let mut last_starved_at: Option = None; loop { @@ -244,14 +247,14 @@ impl OutboundConnector { backoff_snapshot.len(), ); let changed = last_starved != Some(now); - if changed || last_starved_at.elapsed() >= STARVED_REPEAT { + if changed || last_starved_at.is_none_or(|at| at.elapsed() >= STARVED_REPEAT) { warn!( target: "rustock::net", "Below target ({}/{}) but no dialable candidates ({} nodes, {} pending, {} backed off)", now.0, self.max_outbound, now.1, now.2, now.3 ); last_starved = Some(now); - last_starved_at = Instant::now(); + last_starved_at = Some(Instant::now()); } } else { // Out of the condition: say so next time it happens. diff --git a/crates/networking/src/protocol/rsk.rs b/crates/networking/src/protocol/rsk.rs index a5fa3a8..0852f6f 100644 --- a/crates/networking/src/protocol/rsk.rs +++ b/crates/networking/src/protocol/rsk.rs @@ -2030,6 +2030,254 @@ mod headers_with_uncles_tests { .encode(&mut buf); assert!(RskMessage::decode(&mut buf.as_slice()).is_err()); } + + /// **No message a peer sends panics the decoder.** + /// + /// Each RSK message is built as a peer would send it, with content of + /// each kind in its body, and then varied byte by byte. Every type number + /// is also sent with a random tree of RLP for its body, so that each + /// message's field reads meet strings and lists of every shape. Only that + /// each decode returns is asserted. + #[test] + fn no_variation_on_a_message_panics_the_decoder() { + use rustock_core::Transaction; + + struct Rng(u64); + impl Rng { + fn next(&mut self) -> u64 { + self.0 ^= self.0 << 13; + self.0 ^= self.0 >> 7; + self.0 ^= self.0 << 17; + self.0 + } + fn below(&mut self, n: usize) -> usize { + (self.next() % n.max(1) as u64) as usize + } + } + + // The bytes that start RLP's items, at the edges of each form. + const EDGES: [u8; 12] = [ + 0x00, 0x7f, 0x80, 0x81, 0x9f, 0xa0, 0xb7, 0xb8, 0xbf, 0xc0, 0xf7, 0xff, + ]; + + fn vary(bytes: &mut Vec, rng: &mut Rng) { + for _ in 0..1 + rng.below(3) { + let at = rng.below(bytes.len() + 1); + match rng.below(6) { + 0 if at < bytes.len() => bytes[at] ^= 1 << rng.below(8), + 1 if at < bytes.len() => bytes[at] = EDGES[rng.below(EDGES.len())], + 2 => bytes.insert(at, EDGES[rng.below(EDGES.len())]), + 3 if at < bytes.len() => { + let n = 1 + rng.below(8.min(bytes.len() - at)); + bytes.drain(at..at + n); + } + 4 => bytes.truncate(at), + _ => bytes.insert(at, rng.next() as u8), + } + } + } + + /// A random RLP item: a string of a size fields come in, or a list of + /// up to five more items. + fn tree(rng: &mut Rng, depth: u32, out: &mut Vec) { + if depth == 0 || rng.below(3) == 0 { + let len = [0, 1, 1, 2, 8, 20, 32, 33, 65, 100][rng.below(10)]; + let bytes: Vec = (0..len).map(|_| rng.next() as u8).collect(); + bytes.as_slice().encode(out); + } else { + let mut body = Vec::new(); + for _ in 0..rng.below(6) { + tree(rng, depth - 1, &mut body); + } + RlpHeader { + list: true, + payload_length: body.len(), + } + .encode(out); + out.extend_from_slice(&body); + } + } + + /// `params` in the envelope `RskMessage::encode` writes: a list of a + /// list of the type and the parameters as a string. + fn envelope(kind: u8, params: &[u8]) -> Vec { + let mut message = Vec::new(); + kind.encode(&mut message); + RlpHeader { + list: false, + payload_length: params.len(), + } + .encode(&mut message); + message.extend_from_slice(params); + let mut wrapped = Vec::new(); + RlpHeader { + list: true, + payload_length: message.len(), + } + .encode(&mut wrapped); + wrapped.extend_from_slice(&message); + let mut out = Vec::new(); + RlpHeader { + list: true, + payload_length: wrapped.len(), + } + .encode(&mut out); + out.extend_from_slice(&wrapped); + out + } + + let tx = Transaction { + nonce: 1, + gas_price: U256::from(10), + gas_limit: U256::from(21000), + to: Bytes::from(vec![0x12; 20]), + value: U256::from(1000), + input: Bytes::from(vec![1, 2, 3]), + v: 27, + r: U256::from(88), + s: U256::from(99), + cached_rlp: None, + }; + let identifier = |n: u8| BlockIdentifier { + hash: B256::repeat_byte(n), + number: 192 * n as u64, + }; + let seeds = vec![ + RskSubMessage::Status(RskStatus { + best_block_number: 9, + best_block_hash: B256::repeat_byte(1), + best_block_parent_hash: Some(B256::repeat_byte(2)), + total_difficulty: Some(U256::from(1u64 << 63)), + earliest_block: Some(3), + }), + RskSubMessage::BlockHashRequest(BlockHashRequest { id: 1, height: 192 }), + RskSubMessage::BlockHashResponse(BlockHashResponse { + id: 1, + hash: B256::repeat_byte(4), + }), + RskSubMessage::BlockHeadersRequest(BlockHeadersRequest { + id: 2, + query: BlockHeadersQuery { + hash: B256::repeat_byte(5), + count: 192, + }, + }), + RskSubMessage::BlockHeadersResponse(BlockHeadersResponse { + id: 2, + headers: vec![plain(10, 1000), plain(9, 900)], + }), + RskSubMessage::SkeletonRequest(SkeletonRequest { + id: 3, + start_number: 5000, + }), + RskSubMessage::SkeletonResponse(SkeletonResponse { + id: 3, + block_identifiers: vec![identifier(1), identifier(2)], + }), + RskSubMessage::BodyRequest(BodyRequest { + id: 4, + hash: B256::repeat_byte(7), + }), + RskSubMessage::BodyResponse(BodyResponse { + id: 4, + transactions: vec![tx.clone()], + uncles: vec![plain(8, 800)], + extension: None, + }), + RskSubMessage::Transactions(vec![Bytes::from(alloy_rlp::encode(&tx))]), + RskSubMessage::BlockRangeUpdate(BlockRange { + earliest_block: 1, + latest_block: 2, + latest_block_hash: B256::repeat_byte(8), + }), + RskSubMessage::BlockHeadersWithUnclesRequest(BlockHeadersWithUnclesRequest { + id: 5, + query: BlockHeadersQuery { + hash: B256::repeat_byte(9), + count: 3, + }, + }), + RskSubMessage::BlockHeadersWithUnclesResponse(BlockHeadersWithUnclesResponse { + id: 5, + entries: vec![with_uncles(10, 1000, vec![plain(9, 700)])], + }), + ]; + + let mut messages: Vec<(String, Vec)> = seeds + .into_iter() + .map(|seed| { + let kind = format!("{:?}", seed.message_type()); + let mut bytes = Vec::new(); + RskMessage::new(seed).encode(&mut bytes); + (kind, bytes) + }) + .collect(); + // rustock never sends NewBlockHashes, so its encoder writes nothing for + // it; this is the list of [hash, number] pairs an rskj node sends. + let mut pairs = Vec::new(); + for n in 1..=3u8 { + let mut pair = Vec::new(); + B256::repeat_byte(n).encode(&mut pair); + (192 * n as u64).encode(&mut pair); + RlpHeader { + list: true, + payload_length: pair.len(), + } + .encode(&mut pairs); + pairs.extend_from_slice(&pair); + } + let mut params = Vec::new(); + RlpHeader { + list: true, + payload_length: pairs.len(), + } + .encode(&mut params); + params.extend_from_slice(&pairs); + messages.push(("NewBlockHashes".into(), envelope(6, ¶ms))); + + // As the session reads them: the P2P message id, then the message. + let rsk_id = { + let mut framed = Vec::new(); + let probe = RskSubMessage::BodyRequest(BodyRequest { + id: 0, + hash: B256::ZERO, + }); + crate::protocol::p2p::P2pMessage::RskMessage(RskMessage::new(probe)) + .encode(&mut framed); + framed[0] + }; + let mut rng = Rng(0x6A09_E667_F3BC_C908); + for (kind, message) in messages { + let mut honest = vec![rsk_id]; + honest.extend_from_slice(&message); + let decoded = crate::protocol::p2p::P2pMessage::decode(&mut honest.as_slice()); + assert!( + decoded.is_ok(), + "the {kind} seed does not decode, so varying it tests nothing: {decoded:?}" + ); + for _ in 0..1_500 { + let mut bytes = honest.clone(); + vary(&mut bytes, &mut rng); + let _ = crate::protocol::p2p::P2pMessage::decode(&mut bytes.as_slice()); + let _ = RskMessage::decode(&mut bytes.get(1..).unwrap_or_default()); + } + } + for kind in 0..=30u8 { + for _ in 0..300 { + let mut params = Vec::new(); + tree(&mut rng, 4, &mut params); + let _ = RskMessage::decode(&mut envelope(kind, ¶ms).as_slice()); + } + } + // Every P2P message id, each with a random body. + for id in 0..=u8::MAX { + for _ in 0..40 { + let mut framed = vec![id]; + tree(&mut rng, 4, &mut framed); + let _ = crate::protocol::p2p::P2pMessage::decode(&mut framed.as_slice()); + } + } + } } #[cfg(test)] diff --git a/crates/networking/src/rlpx/ecies.rs b/crates/networking/src/rlpx/ecies.rs index 0cadd3d..dc75fd6 100644 --- a/crates/networking/src/rlpx/ecies.rs +++ b/crates/networking/src/rlpx/ecies.rs @@ -65,6 +65,11 @@ impl AuthInitiate { } pub fn recover_ephemeral_public_key(&self, my_sk: &SecretKey) -> Result { + // The signature is whatever the initiator sent, so it is held to its + // 65 bytes before its parts are read. + let signature: &[u8; 65] = self.signature.as_ref().try_into().map_err(|_| { + anyhow::anyhow!("auth signature is {} bytes, not 65", self.signature.len()) + })?; let mut pub_bytes = [0u8; 65]; pub_bytes[0] = 0x04; pub_bytes[1..].copy_from_slice(self.public_key.as_slice()); @@ -83,9 +88,9 @@ impl AuthInitiate { } // Recover ephemeral public key from signature - let signature_bytes = k256::ecdsa::Signature::from_slice(&self.signature[..64]) + let signature_bytes = k256::ecdsa::Signature::from_slice(&signature[..64]) .map_err(|e| anyhow::anyhow!("Invalid signature format: {:?}", e))?; - let recovery_id = k256::ecdsa::RecoveryId::from_byte(self.signature[64]) + let recovery_id = k256::ecdsa::RecoveryId::from_byte(signature[64]) .ok_or_else(|| anyhow::anyhow!("Invalid recovery ID"))?; let vk = k256::ecdsa::VerifyingKey::recover_from_prehash(&signed_data, &signature_bytes, recovery_id) @@ -389,4 +394,25 @@ mod tests { let auth_resp = AuthResponse::new(&my_ephemeral_pk, nonce); assert_eq!(auth_resp.public_key, my_ephemeral_pk); } + + /// The responder reads the initiator's signature before it knows anything + /// about the peer, so a signature of the wrong length is an error. + #[test] + fn an_auth_signature_of_the_wrong_length_is_refused() { + let mut rng = k256::elliptic_curve::rand_core::OsRng; + let (my_sk, remote_sk) = (SecretKey::random(&mut rng), SecretKey::random(&mut rng)); + let encoded = remote_sk.public_key().to_encoded_point(false); + let remote_pk = B512::from_slice(&encoded.as_bytes()[1..]); + let mut auth = + AuthInitiate::new(&my_sk, &remote_pk, &SecretKey::random(&mut rng), B256::ZERO) + .unwrap(); + assert!(auth.recover_ephemeral_public_key(&remote_sk).is_ok()); + for length in [0, 1, 64, 66, 200] { + auth.signature = vec![0x11; length].into(); + assert!( + auth.recover_ephemeral_public_key(&remote_sk).is_err(), + "{length} bytes" + ); + } + } } diff --git a/crates/networking/src/rlpx/frame.rs b/crates/networking/src/rlpx/frame.rs index 3a210c8..06559bc 100644 --- a/crates/networking/src/rlpx/frame.rs +++ b/crates/networking/src/rlpx/frame.rs @@ -276,7 +276,10 @@ impl FrameCodec { (1 + ll, len) }; - let list_data = &data[list_start..data.len().min(list_start + list_len)]; + // A long-form length is any 8 bytes the peer chose, so the end is added + // without overflowing, and the list stops where the 13 bytes do. + let list_end = list_start.saturating_add(list_len).min(data.len()); + let list_data = &data[list_start..list_end]; // Decode individual elements from the list let mut elems: Vec = Vec::new(); @@ -436,6 +439,26 @@ mod tests { assert!(matches!(FrameCodec::parse_header_data(&data), FrameType::Normal)); } + /// A long-form list length is up to 8 bytes the peer chose; one that runs + /// past the 13 header-data bytes, or past `usize::MAX`, reads what is there. + #[test] + fn a_list_length_past_the_header_data_reads_what_is_there() { + for prefix in 0xf8..=0xffu8 { + for fill in [0x00, 0x01, 0x80, 0xff] { + let mut data = [fill; 13]; + data[0] = prefix; + let _ = FrameCodec::parse_header_data(&data); + } + } + // A length of usize::MAX before three chunked-frame elements. + let mut data = [0xff; 13]; + data[9..12].copy_from_slice(&[0x80, 0x01, 0x05]); + assert!(matches!( + FrameCodec::parse_header_data(&data), + FrameType::ChunkedFirst { total_size: 5, .. } + )); + } + // ---- Normal single-frame roundtrip ---- #[test] diff --git a/crates/rpc/src/logs.rs b/crates/rpc/src/logs.rs index d42e9b9..cd19953 100644 --- a/crates/rpc/src/logs.rs +++ b/crates/rpc/src/logs.rs @@ -6,7 +6,7 @@ use alloy_primitives::{Address, B256}; use serde_json::{json, Value}; use std::collections::HashMap; use std::sync::atomic::{AtomicU64, Ordering}; -use std::sync::{Arc, RwLock}; +use std::sync::{Arc, PoisonError, RwLock}; /// Block range an `eth_getLogs` may span when the operator has set no limit. /// @@ -43,16 +43,28 @@ impl FilterStore { fn insert(&self, filter: Filter) -> u64 { let id = self.next_id.fetch_add(1, Ordering::Relaxed); - self.filters.write().unwrap().insert(id, filter); + self.filters + .write() + .unwrap_or_else(PoisonError::into_inner) + .insert(id, filter); id } fn get(&self, id: u64) -> Option { - self.filters.read().unwrap().get(&id).cloned() + self.filters + .read() + .unwrap_or_else(PoisonError::into_inner) + .get(&id) + .cloned() } fn update_last_polled(&self, id: u64, block: u64) { - if let Some(f) = self.filters.write().unwrap().get_mut(&id) { + if let Some(f) = self + .filters + .write() + .unwrap_or_else(PoisonError::into_inner) + .get_mut(&id) + { f.last_polled_block = block; } } @@ -61,7 +73,12 @@ impl FilterStore { /// now. Called straight after the difference has been handed to the /// caller. fn replace_reported(&self, id: u64, now: std::collections::HashSet) { - if let Some(f) = self.filters.write().unwrap().get_mut(&id) { + if let Some(f) = self + .filters + .write() + .unwrap_or_else(PoisonError::into_inner) + .get_mut(&id) + { if let FilterKind::PendingTransaction { reported } = &mut f.kind { *reported = now; } @@ -69,7 +86,11 @@ impl FilterStore { } fn remove(&self, id: u64) -> bool { - self.filters.write().unwrap().remove(&id).is_some() + self.filters + .write() + .unwrap_or_else(PoisonError::into_inner) + .remove(&id) + .is_some() } } @@ -111,7 +132,9 @@ pub fn eth_get_logs(id: Value, params: &Value, state: &RpcState) -> JsonRpcRespo let to = filter.to_block.unwrap_or(head_num); let max_range = state.logs_max_blocks.max(1); - if to > from + max_range { + // Measured as a difference, which cannot overflow: a `fromBlock` near 2^64 + // would carry `from + max_range` past the top. + if to.saturating_sub(from) > max_range { return JsonRpcResponse::error( id, INVALID_PARAMS, @@ -274,6 +297,11 @@ fn collect_logs(from: u64, to: u64, filter: &LogFilter, state: &RpcState) -> Vec // nothing. let use_bloom = filter_constrains_anything(filter); + // Nothing above the head has a block to read, and a range a client names + // may end anywhere up to 2^64 - 1 (an installed filter's is never checked + // against the cap). Walked number by number, the rest would be a request + // that never ends, holding a worker thread for good. + let to = to.min(head_number(&state.store).unwrap_or(0)); for num in from..=to { let Some(block_hash) = state.store.canonical_hash(num).ok().flatten() else { continue; diff --git a/crates/rpc/src/server.rs b/crates/rpc/src/server.rs index 410ce34..b5bd1bb 100644 --- a/crates/rpc/src/server.rs +++ b/crates/rpc/src/server.rs @@ -204,7 +204,40 @@ async fn handle_rpc( } } +/// One request, answered whatever happens inside it. +/// +/// rskj's `RskErrorResolver` answers an exception it has no specific answer +/// for with -32603 "Internal server error", logs it, and goes on serving. A +/// panic here is the same failure, so it gets the same answer. Uncaught, it +/// would unwind the connection's task instead, and the client, with every +/// other request in its batch, would get a dropped connection, not a reply. async fn dispatch(state: &RpcState, req: JsonRpcRequest) -> JsonRpcResponse { + let id = req.id.clone().unwrap_or(Value::Null); + let method = req.method.clone(); + answered(id, &method, route(state, req)).await +} + +/// `answer`'s response, or rskj's internal error if it panics. +async fn answered( + id: Value, + method: &str, + answer: impl std::future::Future, +) -> JsonRpcResponse { + match futures::FutureExt::catch_unwind(std::panic::AssertUnwindSafe(answer)).await { + Ok(response) => response, + Err(panic) => { + let message = panic + .downcast_ref::<&str>() + .copied() + .or_else(|| panic.downcast_ref::().map(String::as_str)) + .unwrap_or("no message"); + tracing::error!("JSON-RPC method {method} panicked: {message}"); + JsonRpcResponse::error(id, INTERNAL_ERROR, "Internal server error") + } + } +} + +async fn route(state: &RpcState, req: JsonRpcRequest) -> JsonRpcResponse { let id = req.id.unwrap_or(Value::Null); let params = &req.params; @@ -446,3 +479,33 @@ pub async fn dispatch_rpc(state: &RpcState, req: JsonRpcRequest) -> JsonRpcRespo pub(crate) async fn dispatch_for_test(state: &RpcState, req: JsonRpcRequest) -> JsonRpcResponse { dispatch(state, req).await } + +#[cfg(test)] +mod panic_boundary_tests { + use super::*; + + #[tokio::test] + async fn a_method_that_panics_is_answered_as_rskj_answers_an_exception() { + let response = answered(json!(7), "eth_example", async { panic!("a bug") }).await; + let error = response.error.expect("an error, not a dropped connection"); + assert_eq!(error.code, INTERNAL_ERROR); + assert_eq!(error.message, "Internal server error"); + assert_eq!(response.id, json!(7)); + assert!(response.result.is_none()); + + // A panic carrying a formatted message is caught the same way. + let response = answered(Value::Null, "eth_example", async { panic!("{} bugs", 2) }).await; + assert_eq!(response.error.expect("an error").code, INTERNAL_ERROR); + } + + #[tokio::test] + async fn an_answer_passes_the_boundary_untouched() { + let response = answered(json!("a"), "eth_example", async { + JsonRpcResponse::success(json!("a"), json!("0x1")) + }) + .await; + assert!(response.error.is_none()); + assert_eq!(response.result, Some(json!("0x1"))); + assert_eq!(response.id, json!("a")); + } +} diff --git a/crates/rpc/src/tests.rs b/crates/rpc/src/tests.rs index 1f97f92..3424a05 100644 --- a/crates/rpc/src/tests.rs +++ b/crates/rpc/src/tests.rs @@ -1275,6 +1275,79 @@ async fn the_get_logs_range_cap_is_configurable() { assert_eq!(resp.result.unwrap().as_array().unwrap().len(), 3); } +/// Runs `request` on a thread of its own and fails if no answer comes within a +/// minute. The regressions the tests below guard against are loops that never +/// end, and a test that hangs with one says less than a test that fails. +fn answered_within_a_minute(state: &RpcState, request: JsonRpcRequest) -> JsonRpcResponse { + let state = state.clone(); + let (answer, answered) = std::sync::mpsc::channel(); + std::thread::spawn(move || { + let runtime = tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .expect("a runtime"); + let _ = answer.send(runtime.block_on(dispatch_for_test(&state, request))); + }); + answered + .recv_timeout(std::time::Duration::from_secs(60)) + .expect("no answer within a minute: the request walks a range that never ends") +} + +/// **A log range is walked no further than the head.** +/// +/// A client may name any block up to 2^64 - 1. `eth_getLogs` measures its span +/// against the cap, and that measurement must not overflow; an installed +/// filter's range is never measured at all. Either way nothing above the head +/// holds a block, and walking those numbers one by one would be a request +/// that never ends. +#[tokio::test] +async fn a_log_range_reaching_the_top_of_the_number_space_stops_at_the_head() { + let (state, _tmp) = setup_state_with_logs(); + let top = "0xffffffffffffffff"; + + let at_the_top = answered_within_a_minute( + &state, + make_request("eth_getLogs", json!([{"fromBlock": top, "toBlock": top}])), + ); + assert!(at_the_top + .result + .expect("an answer") + .as_array() + .unwrap() + .is_empty()); + + let to_the_head = dispatch_for_test( + &state, + make_request( + "eth_getLogs", + json!([{"fromBlock": "0x1", "toBlock": "latest"}]), + ), + ) + .await + .result + .expect("logs to the head"); + assert!( + !to_the_head.as_array().unwrap().is_empty(), + "nothing to compare" + ); + + let installed = dispatch_for_test( + &state, + make_request( + "eth_newFilter", + json!([{"fromBlock": "0x1", "toBlock": top}]), + ), + ) + .await + .result + .expect("a filter id"); + let to_the_top = answered_within_a_minute( + &state, + make_request("eth_getFilterLogs", json!([installed])), + ); + assert_eq!(to_the_top.result.expect("an answer"), to_the_head); +} + #[tokio::test] async fn test_eth_get_logs_by_address() { let (state, _tmp) = setup_state_with_logs(); @@ -3951,6 +4024,55 @@ async fn trace_filter_returns_the_range_and_enforces_its_limits() { assert!(too_many.error.unwrap().message.contains("Count value too big")); } +/// **`trace_filter` stops at the head, as rskj's does, whatever `toBlock` says.** +/// +/// rskj walks while `getBlockByNumber` finds a block. Walking every number up +/// to a `toBlock` of 2^64 - 1 instead would be a request that never ends. +#[tokio::test] +async fn a_trace_range_reaching_the_top_of_the_number_space_stops_at_the_head() { + let (state, _tmp, _tx, _sender, _caller, _callee) = setup_state_for_tracing(); + let top = "0xffffffffffffffff"; + + let to_the_head = dispatch_for_test( + &state, + make_request( + "trace_filter", + json!([{"fromBlock": "0x2a", "toBlock": "latest"}]), + ), + ) + .await + .result + .expect("traces to the head"); + assert!( + !to_the_head.as_array().unwrap().is_empty(), + "nothing to compare" + ); + + let to_the_top = answered_within_a_minute( + &state, + make_request( + "trace_filter", + json!([{"fromBlock": "0x2a", "toBlock": top}]), + ), + ); + assert_eq!(to_the_top.result.expect("an answer"), to_the_head); + + // Above the head there is nothing to trace: rskj finds no `fromBlock`. + let above = answered_within_a_minute( + &state, + make_request( + "trace_filter", + json!([{"fromBlock": "0xfffffffffffffff0", "toBlock": top}]), + ), + ); + assert!(above + .result + .expect("an answer") + .as_array() + .unwrap() + .is_empty()); +} + /// An unknown transaction is `null`, and the `trace_*` methods no longer /// answer "execution engine not available". #[tokio::test] diff --git a/crates/rpc/src/trace.rs b/crates/rpc/src/trace.rs index 86aae15..864f528 100644 --- a/crates/rpc/src/trace.rs +++ b/crates/rpc/src/trace.rs @@ -198,7 +198,10 @@ pub fn trace_filter(id: Value, params: &Value, state: &RpcState) -> JsonRpcRespo let mut seen = 0usize; let wanted = after.saturating_add(count); - for number in from..=to { + // rskj walks from `fromBlock` while `getBlockByNumber` finds a block, so it + // stops at the head whatever `toBlock` says. Walking every number up to a + // `toBlock` of 2^64 - 1 instead would never end. + for number in from..=to.min(head) { let Some(hash) = state.store.canonical_hash(number).ok().flatten() else { continue; }; diff --git a/crates/sync/src/chain_activity.rs b/crates/sync/src/chain_activity.rs index 03915f9..7849e36 100644 --- a/crates/sync/src/chain_activity.rs +++ b/crates/sync/src/chain_activity.rs @@ -9,7 +9,7 @@ //! last being the same fullness signal rskj's `GasPriceTracker` uses to decide //! whether the fee market is working (see issue #52). -use std::sync::RwLock; +use std::sync::{PoisonError, RwLock}; use std::time::Duration; /// CPU time consumed by the calling thread. @@ -117,7 +117,7 @@ impl ChainActivity { cpu: Duration, wall: Duration, ) { - let mut c = self.inner.write().unwrap(); + let mut c = self.inner.write().unwrap_or_else(PoisonError::into_inner); c.blocks += 1; c.transactions += transactions as u64; c.gas_used += gas_used as u128; @@ -131,7 +131,7 @@ impl ChainActivity { /// The window since the previous call, clearing the counters so each /// report covers exactly one window. pub fn take(&self) -> ChainActivitySummary { - let mut c = self.inner.write().unwrap(); + let mut c = self.inner.write().unwrap_or_else(PoisonError::into_inner); let summary = ChainActivitySummary { blocks: c.blocks, transactions: c.transactions, diff --git a/crates/sync/src/gas_price.rs b/crates/sync/src/gas_price.rs index f802b25..1f380bc 100644 --- a/crates/sync/src/gas_price.rs +++ b/crates/sync/src/gas_price.rs @@ -51,7 +51,7 @@ use alloy_primitives::U256; use rustock_core::{Header, Transaction}; -use std::sync::Mutex; +use std::sync::{Mutex, PoisonError}; /// Transactions sampled for the price percentile. rskj `TX_WINDOW_SIZE`. const TX_WINDOW_SIZE: usize = 512; @@ -130,7 +130,7 @@ impl GasPriceTracker { /// fullness enters the block window, and its transactions enter the price /// window. pub fn on_block(&self, header: &Header, transactions: &[Transaction]) { - let mut inner = self.inner.lock().unwrap(); + let mut inner = self.inner.lock().unwrap_or_else(PoisonError::into_inner); inner.default_price = header.minimum_gas_price; // rskj `trackBlockCompleteness`: gasUsed / gasLimit. @@ -161,14 +161,17 @@ impl GasPriceTracker { /// rskj `onBestBlock`: only the head's minimum gas price is kept, and only /// as the floor applied in `gas_price`. pub fn on_best_block(&self, header: &Header) { - self.inner.lock().unwrap().best_block_price = Some(header.minimum_gas_price); + self.inner + .lock() + .unwrap_or_else(PoisonError::into_inner) + .best_block_price = Some(header.minimum_gas_price); } /// rskj `getGasPrice`: the windowed percentile, floored at the best /// block's minimum times 1.1; the last seen block minimum until the window /// has 512 transactions in it. pub fn gas_price(&self) -> U256 { - let mut inner = self.inner.lock().unwrap(); + let mut inner = self.inner.lock().unwrap_or_else(PoisonError::into_inner); let Some(percentile) = inner.percentile() else { return inner.default_price; @@ -188,7 +191,7 @@ impl GasPriceTracker { /// The fullness test is what gates the rate limiter's low-gas-price /// factor. False is a normal answer on a chain with spare capacity. pub fn is_fee_market_working(&self) -> bool { - let inner = self.inner.lock().unwrap(); + let inner = self.inner.lock().unwrap_or_else(PoisonError::into_inner); // rskj checks the LAST slot, which is only written once 50 blocks have // gone by -- a full ring, not merely a non-empty one. if inner.block_window[BLOCK_WINDOW_SIZE - 1].is_none() { @@ -275,7 +278,13 @@ impl GasPriceTracker { /// How many of the 512 transaction slots are filled. For diagnostics and /// tests; rskj has no equivalent. pub fn sampled_transactions(&self) -> usize { - self.inner.lock().unwrap().tx_window.iter().filter(|s| s.is_some()).count() + self.inner + .lock() + .unwrap_or_else(PoisonError::into_inner) + .tx_window + .iter() + .filter(|s| s.is_some()) + .count() } } diff --git a/crates/sync/src/manager.rs b/crates/sync/src/manager.rs index 293908d..7979b52 100644 --- a/crates/sync/src/manager.rs +++ b/crates/sync/src/manager.rs @@ -8,7 +8,7 @@ use rustock_networking::protocol::{ use alloy_primitives::{B256, B512, U256}; use anyhow::Result; use std::collections::HashMap; -use std::sync::{Arc, Mutex}; +use std::sync::{Arc, Mutex, PoisonError}; use std::time::Instant; use tracing::{debug, trace, warn}; @@ -87,7 +87,7 @@ impl SyncManager { /// match the trunk header's `ommers_hash`, so these figures are as sound /// as the header's own proof of work. pub fn note_proven_difficulties(&self, entries: Vec) -> Vec
{ - let mut proven = self.proven.lock().unwrap(); + let mut proven = self.proven.lock().unwrap_or_else(PoisonError::into_inner); if proven.len() + entries.len() > MAX_PROVEN { // Everything in here is re-derivable from a later request, so // dropping the lot is cheaper than tracking an eviction order. @@ -148,13 +148,13 @@ impl SyncManager { // several of its own blocks and have the work counted each time -- // capped only by `uncleListLimit`, so up to eleven times the trunk // work actually performed. - let mut guard = self.uncle_guard.lock().unwrap(); - if let Err(why) = guard.uncles_are_admissible( - &self.store, - &self.verifier, - &entry.header, - &entry.uncles, - ) { + let mut guard = self + .uncle_guard + .lock() + .unwrap_or_else(PoisonError::into_inner); + if let Err(why) = + guard.uncles_are_admissible(&self.store, &self.verifier, &entry.header, &entry.uncles) + { warn!( target: "rustock::sync", "Block #{}: uncles not admissible ({why}); ignoring the \ @@ -297,7 +297,12 @@ impl SyncManager { // `rsk/62` peer, which is every rskj node -- the header difficulty // alone. That is a lower bound, and `crate::total_difficulty` // settles it once the body is in. - let proven = self.proven.lock().unwrap().get(&hash).copied(); + let proven = self + .proven + .lock() + .unwrap_or_else(PoisonError::into_inner) + .get(&hash) + .copied(); let contribution = proven.unwrap_or_else(|| self.held_contribution(hash, header)); // The parent is the block named by `parent_hash`, and nothing else. @@ -354,7 +359,10 @@ impl SyncManager { // search is verified normally. unlinked_here += 1; if let Some(p) = peer { - self.unlinked.lock().unwrap().record(p, hash, header.number, now); + self.unlinked + .lock() + .unwrap_or_else(PoisonError::into_inner) + .record(p, hash, header.number, now); } // Deliberately leave `prev_in_chunk` alone. A refused // header must not seed a total difficulty for the next @@ -432,14 +440,20 @@ impl SyncManager { /// /// Marks the search as taken, so the caller cannot forget to. pub fn consider_fork_search(&self, peer: &B512) -> Verdict { - self.unlinked.lock().unwrap().consider(peer, Instant::now()) + self.unlinked + .lock() + .unwrap_or_else(PoisonError::into_inner) + .consider(peer, Instant::now()) } /// Whether this peer is still sending headers we cannot connect to after a /// search already resolved where its chain diverges — a peer not serving /// the chain it agreed on. pub fn unlinked_persisted_after_search(&self, peer: &B512) -> bool { - self.unlinked.lock().unwrap().persisted_after_search(peer) + self.unlinked + .lock() + .unwrap_or_else(PoisonError::into_inner) + .persisted_after_search(peer) } /// See [`UnlinkedHeaders::backdate`]. @@ -450,7 +464,10 @@ impl SyncManager { /// Releases a disconnected peer's evidence. pub fn forget_peer(&self, peer: &B512) { - self.unlinked.lock().unwrap().clear_peer(peer); + self.unlinked + .lock() + .unwrap_or_else(PoisonError::into_inner) + .clear_peer(peer); } /// Helper to create a headers request message. diff --git a/crates/sync/src/sampler.rs b/crates/sync/src/sampler.rs index af76c8d..0603f71 100644 --- a/crates/sync/src/sampler.rs +++ b/crates/sync/src/sampler.rs @@ -94,6 +94,18 @@ pub const RANDOM_SAMPLES: usize = 340; pub const WALK_INSTEAD_BELOW: u64 = RANDOM_SAMPLES as u64 * crate::snap::headers::HEADER_CHUNK; +/// The widest window above the checkpoint the gate takes on: about nine and +/// a half years of mainnet's blocks at their thirty-odd seconds, more than +/// any chain a build of this age can follow through the upgrades between. +/// +/// What the gate costs grows with the window -- the skeletons it asks for, +/// the heights they name, the samples drawn among them -- and the window runs +/// to the height the peer's offered header names. That header's proof of +/// work is checked against the difficulty it states itself, so a peer can +/// name a height of 2^62 for next to nothing, and then answer for ever. Here +/// it costs at most some thirteen thousand samples and a few seconds' work. +pub const MAX_SAMPLED_WINDOW: u64 = 10_000_000; + /// Choose which of `available` heights to sample. /// /// Two properties, and they want opposite things: @@ -170,15 +182,40 @@ pub fn fill_gaps(available: &[u64], chosen: Vec, max_gap: u64) -> Vec for &at in chosen.iter().chain(std::iter::once(&highest)) { let gap = at.saturating_sub(previous); if gap > max_gap { - // `ceil(gap / max_gap) - 1` interior points, evenly spaced. + // `ceil(gap / max_gap) - 1` interior points, evenly spaced, each + // rounded to the nearest height the peer named. In u128, since + // `gap * piece` passes u64 for a gap a peer names. And a run of + // points rounding to the same height is stepped over at once, so + // the work is the heights in the gap rather than the gap: two + // heights far enough apart otherwise made this loop for ever. let pieces = gap.div_ceil(max_gap); - for piece in 1..pieces { - let want_at = previous + (gap * piece) / pieces; - if let Some(&nearest) = nearest_available(available, want_at) { - if nearest > previous && nearest < at { - filled.push(nearest); - } + let mut piece = 1; + while piece < pieces { + let want_at = + previous + (u128::from(gap) * u128::from(piece) / u128::from(pieces)) as u64; + let Some(&nearest) = nearest_available(available, want_at) else { + break; + }; + if nearest > previous && nearest < at { + filled.push(nearest); } + // Every point up to halfway to the next height rounds to + // `nearest` again, ties going to the lower. + let next = available + .binary_search(&nearest) + .ok() + .and_then(|i| available.get(i + 1)); + let Some(&next) = next else { break }; + let halfway = nearest + (next - nearest) / 2; + let past_halfway = match halfway.checked_sub(previous) { + // The first piece whose point lies beyond it. + Some(offset) => { + let piece = (u128::from(offset) + 1) * u128::from(pieces); + u64::try_from(piece.div_ceil(u128::from(gap))).unwrap_or(u64::MAX) + } + None => 0, + }; + piece = past_halfway.max(piece + 1); } } filled.push(at); @@ -1281,4 +1318,88 @@ mod choose_samples_tests { assert!(a.contains(at), "#{at} is not in the skeleton"); } } + + /// `fill_gaps` as it was written first, a point for every piece of every + /// gap, kept to hold the one that steps over runs to the same answers. + fn fill_gaps_piece_by_piece(available: &[u64], chosen: Vec, max_gap: u64) -> Vec { + if available.is_empty() { + return Vec::new(); + } + if max_gap == 0 { + return chosen; + } + let highest = *available.last().unwrap(); + let mut filled = Vec::new(); + let mut previous = available[0].saturating_sub(1); + for &at in chosen.iter().chain(std::iter::once(&highest)) { + let gap = at.saturating_sub(previous); + if gap > max_gap { + let pieces = gap.div_ceil(max_gap); + for piece in 1..pieces { + let want_at = previous + (gap * piece) / pieces; + if let Some(&nearest) = nearest_available(available, want_at) { + if nearest > previous && nearest < at { + filled.push(nearest); + } + } + } + } + filled.push(at); + previous = at; + } + filled.sort_unstable(); + filled.dedup(); + filled + } + + /// Stepping over the points that round to one height changes no choice, + /// over skeletons evenly spaced, clustered and sparse. + #[test] + fn gaps_are_filled_as_they_were_point_by_point() { + let mut seed = 0x9e37_79b9_7f4a_7c15u64; + let mut next = move |below: u64| { + seed ^= seed << 13; + seed ^= seed >> 7; + seed ^= seed << 17; + seed % below + }; + for round in 0..600 { + let spread = [50, 5_000, 200_000, 3_000_000][round % 4]; + let mut heights: Vec = (0..1 + next(60)).map(|_| 1 + next(spread)).collect(); + heights.sort_unstable(); + heights.dedup(); + let chosen: Vec = heights.iter().copied().filter(|_| next(3) == 0).collect(); + // The reference visits every piece, so only gaps it can finish. + for max_gap in [1, 7, 768, 40_000] + .into_iter() + .filter(|g| spread / g <= 20_000) + { + assert_eq!( + fill_gaps(&heights, chosen.clone(), max_gap), + fill_gaps_piece_by_piece(&heights, chosen.clone(), max_gap), + "heights {heights:?}, chosen {chosen:?}, max gap {max_gap}" + ); + } + } + } + + /// Two heights as far apart as a peer cares to name them: the gap + /// between has nothing to offer, and that takes no time to find out. + #[test] + fn a_gap_a_peer_names_is_filled_at_once() { + let started = std::time::Instant::now(); + let far = [1u64, 1 << 62, u64::MAX]; + assert_eq!(fill_gaps(&far, far.to_vec(), 768), far.to_vec()); + assert_eq!(fill_gaps(&far, Vec::new(), 1), far.to_vec()); + let both_ends = [9_020_001u64, 9_020_193, u64::MAX - 1]; + assert_eq!( + fill_gaps(&both_ends, vec![9_020_001], 768), + both_ends.to_vec() + ); + assert!( + started.elapsed() < std::time::Duration::from_secs(5), + "took {:?}", + started.elapsed() + ); + } } diff --git a/crates/sync/src/snap/client.rs b/crates/sync/src/snap/client.rs index de7a26f..3d491ec 100644 --- a/crates/sync/src/snap/client.rs +++ b/crates/sync/src/snap/client.rs @@ -52,7 +52,7 @@ use rustock_trie::snapshot_proof::{verify_chunk, ChunkProof, Entry, VerifyError} use rustock_trie::{TrieNode, TrieStore}; use std::sync::Arc; -use std::sync::Mutex; +use std::sync::{Mutex, PoisonError}; use std::time::{Duration, Instant}; use tracing::{debug, info, trace}; @@ -371,7 +371,9 @@ impl StateDownload { let mut advanced = 0; if let Some(i) = self.slice_for(from) { let slice = &mut self.slices[i]; - advanced = reached.saturating_sub(slice.cursor).min(slice.end - slice.cursor); + advanced = reached + .saturating_sub(slice.cursor) + .min(slice.end.saturating_sub(slice.cursor)); // A cell can begin before the cursor, so it never moves backwards: // the nodes before the cursor were already stored, and storing them // again is harmless but must not un-advance the slice. @@ -628,7 +630,7 @@ impl StateDownload { // percentage -- honest about what is knowable. let last_report = Mutex::new(Instant::now()); let report = move |seen: u64| { - let mut last = last_report.lock().unwrap(); + let mut last = last_report.lock().unwrap_or_else(PoisonError::into_inner); if last.elapsed() < REPORT_EVERY { return; } diff --git a/crates/sync/src/snap/driver.rs b/crates/sync/src/snap/driver.rs index b6997df..f35eaa8 100644 --- a/crates/sync/src/snap/driver.rs +++ b/crates/sync/src/snap/driver.rs @@ -152,6 +152,8 @@ fn blame_for(failure: &SnapFailure) -> Option { // the chain, not a malformed message, and the peer is answerable for // it. SnapFailure::OverstatedDifficulty { .. } => Some(EventType::InvalidBlock), + // So is offering a height no chain this build can follow has reached. + SnapFailure::ImplausibleHeight { .. } => Some(EventType::InvalidBlock), // Not answering is the peer's choice and its problem. Treating // silence as blameless would make stalling a free way past the gate. SnapFailure::UnsubstantiatedClaim { .. } => Some(EventType::InvalidMessage), @@ -579,6 +581,11 @@ impl SnapDriver { } /// Block identifiers: where to ask, nothing more. + /// + /// Only as many as an answer to the request can hold, and only where it + /// can hold them ([`as_rskj_answers`]): the walk, the ascent and the + /// sampling gate each keep what they are given, so how much that is is + /// not the peer's to choose. pub fn on_skeleton<'p>( &mut self, id: u64, @@ -586,9 +593,14 @@ impl SnapDriver { identifiers: &[BlockIdentifier], peers: impl Into>, ) -> Vec { + let start = match self.in_flight.get(&id).map(|r| r.what) { + Some(Pending::Skeleton { start }) => start, + _ => 0, + }; if !self.answers(id, sender, |p| matches!(p, Pending::Skeleton { .. })) { return Vec::new(); } + let identifiers = as_rskj_answers(start, identifiers); trace!( target: "rustock::snap", "skeleton of {} point(s) from {:?}, #{:?}..#{:?} (request {id})", @@ -599,7 +611,7 @@ impl SnapDriver { ); let peers = peers.into(); self.note(peers); - let actions = self.session.on_skeleton(identifiers); + let actions = self.session.on_skeleton(&identifiers); self.charge_for_failure(sender); self.dispatch(actions, peers) } @@ -726,3 +738,72 @@ impl SnapDriver { self.session.checkpoint().map(|h| h.state_root) } } + +/// What an answer to a skeleton request from `start` can hold, as rskj's +/// `processSkeletonRequest` and this node's own server build one: the grid +/// points from `start`'s chunk on, [`SKELETON_POINTS`] at most, then the +/// best block, no further on than the next of them would have been. So no +/// more than `SKELETON_POINTS + 1` identifiers, and none outside that +/// stretch; anything else is dropped. +/// +/// [`SKELETON_POINTS`]: super::headers::SKELETON_POINTS +fn as_rskj_answers(start: u64, identifiers: &[BlockIdentifier]) -> Vec { + use super::headers::{HEADER_CHUNK, SKELETON_POINTS}; + let first = start - start % HEADER_CHUNK; + let last = first.saturating_add(HEADER_CHUNK * SKELETON_POINTS); + identifiers + .iter() + .filter(|i| (first..=last).contains(&i.number)) + .take(SKELETON_POINTS as usize + 1) + .cloned() + .collect() +} + +#[cfg(test)] +mod skeleton_answer_tests { + use super::super::headers::{HEADER_CHUNK, SKELETON_POINTS}; + use super::*; + + fn at(number: u64) -> BlockIdentifier { + BlockIdentifier { + hash: B256::with_last_byte(number as u8), + number, + } + } + + /// What rskj sends for a request from #9,020,000 to a peer at #9,030,000: + /// twenty grid points from #9,019,968, then its best block's stand-in at + /// the end of the stretch. All of it passes. + #[test] + fn an_answer_as_rskj_builds_one_is_taken_whole() { + let first = 9_020_000 - 9_020_000 % HEADER_CHUNK; + let mut sent: Vec = (0..SKELETON_POINTS) + .map(|k| at(first + k * HEADER_CHUNK)) + .collect(); + sent.push(at(first + SKELETON_POINTS * HEADER_CHUNK)); + assert_eq!(as_rskj_answers(9_020_000, &sent), sent); + // A peer whose best block falls inside the stretch ends there. + let short = vec![ + at(first), + at(first + HEADER_CHUNK), + at(first + HEADER_CHUNK + 77), + ]; + assert_eq!(as_rskj_answers(9_020_000, &short), short); + } + + /// Identifiers outside the stretch, and any past the most an answer + /// holds, are not passed on to keep. + #[test] + fn an_answer_holds_no_more_than_rskj_would_send() { + let first = 9_020_000 - 9_020_000 % HEADER_CHUNK; + let flood: Vec = (0..100_000).map(|k| at(first + k)).collect(); + let kept = as_rskj_answers(9_020_000, &flood); + assert_eq!(kept.len() as u64, SKELETON_POINTS + 1); + let far = vec![at(first - 1), at(first), at(1 << 62), at(u64::MAX)]; + assert_eq!(as_rskj_answers(9_020_000, &far), vec![at(first)]); + assert_eq!( + as_rskj_answers(u64::MAX, &[at(u64::MAX)]), + vec![at(u64::MAX)] + ); + } +} diff --git a/crates/sync/src/snap/session.rs b/crates/sync/src/snap/session.rs index 1d2efb4..28eeb51 100644 --- a/crates/sync/src/snap/session.rs +++ b/crates/sync/src/snap/session.rs @@ -113,6 +113,11 @@ pub enum SnapFailure { OverstatedDifficulty { claimed: U256, ceiling: U256 }, #[error("the peer would not substantiate its claimed difficulty: {why}")] UnsubstantiatedClaim { why: String }, + #[error( + "the peer offered a state {window} blocks above the checkpoint, more than the {most} \ + a chain this build can follow has grown" + )] + ImplausibleHeight { window: u64, most: u64 }, #[error("block #{number}: the {what} are not the ones its header commits to")] BadBody { number: u64, what: &'static str }, } @@ -361,7 +366,9 @@ impl SnapSession { self.phase_started = Instant::now(); // Report the new phase promptly rather than up to five seconds // later: the first line is the one that says what is happening. - self.last_report = Instant::now() - Duration::from_secs(5); + // (`Instant` counts from boot, so this can't go below it.) + let now = Instant::now(); + self.last_report = now.checked_sub(Duration::from_secs(5)).unwrap_or(now); } self.phase = phase; } @@ -676,6 +683,12 @@ impl SnapSession { ); } if header.number > checkpoint.number && !walk_is_cheaper { + if window > crate::sampler::MAX_SAMPLED_WINDOW { + return self.fail(SnapFailure::ImplausibleHeight { + window, + most: crate::sampler::MAX_SAMPLED_WINDOW, + }); + } self.gate = Some(crate::sampler::SamplingGate::new( checkpoint, self.checkpoint_td, diff --git a/crates/sync/src/snap/session_tests.rs b/crates/sync/src/snap/session_tests.rs index eed322f..1b2b456 100644 --- a/crates/sync/src/snap/session_tests.rs +++ b/crates/sync/src/snap/session_tests.rs @@ -2096,3 +2096,61 @@ fn a_passed_gate_hands_over_to_the_ascent_not_the_walk() { "and hands over to the ascent, not the walk" ); } + +/// An offered header's proof of work is checked against the difficulty it +/// states, so a peer can name any height for next to nothing. The gate takes +/// on a window of [`crate::sampler::MAX_SAMPLED_WINDOW`] above the checkpoint +/// and refuses one wider, rather than ask for skeletons up to it for ever. +#[test] +fn a_state_further_above_the_checkpoint_than_a_chain_can_be_is_refused() { + use crate::sampler::MAX_SAMPLED_WINDOW; + use rustock_core::checkpoint::MAINNET_CHECKPOINT; + + let cp = MAINNET_CHECKPOINT; + for (window, refused) in [ + (MAX_SAMPLED_WINDOW, false), + (MAX_SAMPLED_WINDOW + 1, true), + (1 << 62, true), + ] { + let f = fixture(4); + let genesis = f.with_genesis(); + f.store.set_head(genesis).expect("head"); + let mut session = SnapSession::new( + SnapConfig { + client_enabled: true, + checkpoint: Some(cp), + checkpoint_defence: rustock_core::checkpoint::CheckpointDefence { + verify_hash: false, + bound_work: true, + }, + ..SnapConfig::default() + }, + f.store.clone(), + Arc::new(MemoryTrieStore::new()) as Arc, + Arc::new(rustock_core::validation::HeaderVerifier::new()), + ); + let offered = block(header(cp.number + window, B256::ZERO, f.state_root, 100)); + let _ = session.on_status( + std::slice::from_ref(&offered), + &[cp.cumulative_difficulty], + 1024, + 0, + ); + if refused { + assert_eq!( + session.failure(), + Some(&SnapFailure::ImplausibleHeight { + window, + most: MAX_SAMPLED_WINDOW + }) + ); + } else { + assert_eq!( + session.phase(), + Phase::SamplingClaim, + "{:?}", + session.failure() + ); + } + } +} diff --git a/crates/sync/src/tx_relay.rs b/crates/sync/src/tx_relay.rs index 02a1a8d..38dca03 100644 --- a/crates/sync/src/tx_relay.rs +++ b/crates/sync/src/tx_relay.rs @@ -1,5 +1,5 @@ use std::num::NonZeroUsize; -use std::sync::Mutex; +use std::sync::{Mutex, PoisonError}; use alloy_primitives::{B256, B512, Bytes}; use lru::LruCache; @@ -80,7 +80,7 @@ impl TxRelay { } fn filter_and_validate(&self, txs: &[Bytes], from: Option) -> Vec { - let mut seen = self.seen.lock().expect("seen cache lock poisoned"); + let mut seen = self.seen.lock().unwrap_or_else(PoisonError::into_inner); let mut valid_txs = Vec::new(); for tx_bytes in txs { let hash = Self::tx_hash(tx_bytes); @@ -132,7 +132,7 @@ impl TxRelay { }; { - let mut seen = self.seen.lock().expect("seen cache lock poisoned"); + let mut seen = self.seen.lock().unwrap_or_else(PoisonError::into_inner); seen.put(hash, ()); } self.announce_pending(hash); diff --git a/crates/sync/src/txpool.rs b/crates/sync/src/txpool.rs index 280abcf..db33dae 100644 --- a/crates/sync/src/txpool.rs +++ b/crates/sync/src/txpool.rs @@ -6,7 +6,7 @@ use rustock_trie::{account_key, code_key, AccountState, TrieKeySlice, TrieNode, use sha3::{Digest, Keccak256}; use std::collections::{BTreeMap, HashMap}; use std::fmt; -use std::sync::{Arc, RwLock}; +use std::sync::{Arc, PoisonError, RwLock}; use std::time::Instant; use tracing::debug; @@ -369,7 +369,10 @@ impl TransactionPool { /// Call `listener` with every transaction admitted from now on. pub fn set_on_added(&self, listener: Arc) { - *self.on_added.write().unwrap() = Some(listener); + *self + .on_added + .write() + .unwrap_or_else(PoisonError::into_inner) = Some(listener); } /// Apply the checks that depend on the chain's activations and @@ -410,7 +413,7 @@ impl TransactionPool { /// Intended for a periodic reporter that stays silent when nothing /// happened, so a line in the log always means something did. pub fn take_activity(&self) -> PoolActivity { - let mut stats = self.stats.write().unwrap(); + let mut stats = self.stats.write().unwrap_or_else(PoisonError::into_inner); let mut rejections: Vec<(&'static str, u64)> = stats.rejections.iter().map(|(k, v)| (*k, *v)).collect(); rejections.sort_by(|a, b| b.1.cmp(&a.1).then(a.0.cmp(b.0))); @@ -437,22 +440,34 @@ impl TransactionPool { /// than spawned internally so the caller owns the task; returns how many /// accounts were dropped. pub fn clean_quotas(&self) -> usize { - self.quota.write().unwrap().clean_max_quotas(Instant::now()) + self.quota + .write() + .unwrap_or_else(PoisonError::into_inner) + .clean_max_quotas(Instant::now()) } /// The cleaner interval, or `None` when the clean is disabled. pub fn quota_cleaner_period(&self) -> Option { - self.quota.read().unwrap().cleaner_period() + self.quota + .read() + .unwrap_or_else(PoisonError::into_inner) + .cleaner_period() } /// Virtual gas currently available to `address`, for diagnostics and tests. pub fn quota_of(&self, address: &Address) -> Option { - self.quota.read().unwrap().quota_of(address) + self.quota + .read() + .unwrap_or_else(PoisonError::into_inner) + .quota_of(address) } /// An address's quota as `debug_accountTransactionQuota` reports it. pub fn quota_report_of(&self, address: &Address) -> Option<(f64, u64)> { - self.quota.read().unwrap().quota_report_of(address, Instant::now()) + self.quota + .read() + .unwrap_or_else(PoisonError::into_inner) + .quota_report_of(address, Instant::now()) } /// Add a raw RLP-encoded transaction to the pool. @@ -460,7 +475,7 @@ impl TransactionPool { pub fn add_transaction(&self, raw: &[u8]) -> Result { let result = self.add_transaction_inner(raw); { - let mut stats = self.stats.write().unwrap(); + let mut stats = self.stats.write().unwrap_or_else(PoisonError::into_inner); stats.offered += 1; match &result { Ok(_) => stats.accepted += 1, @@ -527,7 +542,7 @@ impl TransactionPool { encoded_size, }; - let mut inner = self.inner.write().unwrap(); + let mut inner = self.inner.write().unwrap_or_else(PoisonError::into_inner); if inner.by_hash.contains_key(&hash) { return Err(PoolError::AlreadyKnown); @@ -628,21 +643,31 @@ impl TransactionPool { }), }; let receiver = (tx.to.len() == 20).then(|| Address::from_slice(tx.to.as_ref())); - let accepted = self.quota.write().unwrap().accept_tx( - sender, - receiver, - &tx_facts(&tx, encoded_size), - replaced_facts.as_ref(), - &ctx, - &accounts, - Instant::now(), - ); + let accepted = self + .quota + .write() + .unwrap_or_else(PoisonError::into_inner) + .accept_tx( + sender, + receiver, + &tx_facts(&tx, encoded_size), + replaced_facts.as_ref(), + &ctx, + &accounts, + Instant::now(), + ); if !accepted { debug!( tx_hash = %hash, sender = %sender, nonce = tx.nonce, "Transaction refused: account exceeds virtual gas quota" ); - *self.stats.write().unwrap().quota_by_sender.entry(sender).or_insert(0) += 1; + *self + .stats + .write() + .unwrap_or_else(PoisonError::into_inner) + .quota_by_sender + .entry(sender) + .or_insert(0) += 1; return Err(PoolError::AccountExceedsQuota); } } @@ -666,7 +691,11 @@ impl TransactionPool { debug!(tx_hash = %hash, sender = %sender, nonce = tx.nonce, "Transaction added to pool"); drop(inner); - let listener = self.on_added.read().unwrap().clone(); + let listener = self + .on_added + .read() + .unwrap_or_else(PoisonError::into_inner) + .clone(); if let Some(listener) = listener { listener(&tx); } @@ -774,7 +803,7 @@ impl TransactionPool { pub fn remove_mined(&self, transactions: &[Transaction]) { let mut removed = 0u64; - let mut inner = self.inner.write().unwrap(); + let mut inner = self.inner.write().unwrap_or_else(PoisonError::into_inner); for tx in transactions { let mut buf = Vec::new(); tx.encode(&mut buf); @@ -785,7 +814,10 @@ impl TransactionPool { } drop(inner); if removed > 0 { - self.stats.write().unwrap().mined += removed; + self.stats + .write() + .unwrap_or_else(PoisonError::into_inner) + .mined += removed; } } @@ -793,7 +825,7 @@ impl TransactionPool { /// template: rskj `BlockToMineBuilder.removePendingTransactions` on /// `BlockResult.getInvalidTransactions()`. Returns how many were there. pub fn remove_by_hashes(&self, hashes: &[B256]) -> usize { - let mut inner = self.inner.write().unwrap(); + let mut inner = self.inner.write().unwrap_or_else(PoisonError::into_inner); let mut removed = 0; for hash in hashes { let nonce = inner @@ -835,7 +867,7 @@ impl TransactionPool { /// Evict transactions older than the configured thresholds. pub fn evict_outdated(&self, current_block: u64) { - let mut inner = self.inner.write().unwrap(); + let mut inner = self.inner.write().unwrap_or_else(PoisonError::into_inner); let threshold = self.config.outdated_threshold; let timeout = std::time::Duration::from_secs(self.config.outdated_timeout_secs); let now = Instant::now(); @@ -894,7 +926,7 @@ impl TransactionPool { /// Get a transaction from the pool by hash. pub fn get(&self, hash: &B256) -> Option { - let inner = self.inner.read().unwrap(); + let inner = self.inner.read().unwrap_or_else(PoisonError::into_inner); let sender = inner.by_hash.get(hash)?; for map in [&inner.pending, &inner.queued] { if let Some(nonce_map) = map.get(sender) { @@ -910,7 +942,7 @@ impl TransactionPool { /// Return all pending transactions ordered by gas price (desc), nonce (asc) per sender. pub fn pending_transactions(&self) -> Vec { - let inner = self.inner.read().unwrap(); + let inner = self.inner.read().unwrap_or_else(PoisonError::into_inner); let mut result = Vec::new(); for nonce_map in inner.pending.values() { for ptx in nonce_map.values() { @@ -931,7 +963,7 @@ impl TransactionPool { /// would otherwise appear twice, or vanish. #[allow(clippy::type_complexity)] pub fn content(&self) -> (PoolGroups, PoolGroups) { - let inner = self.inner.read().unwrap(); + let inner = self.inner.read().unwrap_or_else(PoisonError::into_inner); let collect = |side: &HashMap>| -> PoolGroups { let mut out: Vec<(Address, Vec<(u64, Transaction)>)> = side .iter() @@ -955,7 +987,7 @@ impl TransactionPool { /// Return counts of pending and queued transactions. pub fn status(&self) -> (usize, usize) { - let inner = self.inner.read().unwrap(); + let inner = self.inner.read().unwrap_or_else(PoisonError::into_inner); let pending: usize = inner.pending.values().map(|m| m.len()).sum(); let queued: usize = inner.queued.values().map(|m| m.len()).sum(); (pending, queued) @@ -963,7 +995,7 @@ impl TransactionPool { /// Return the pending nonce for an address (state nonce + pending count). pub fn pending_nonce(&self, addr: &Address) -> Option { - let inner = self.inner.read().unwrap(); + let inner = self.inner.read().unwrap_or_else(PoisonError::into_inner); inner .pending .get(addr) diff --git a/crates/trie/tests/rskj_vectors.rs b/crates/trie/tests/rskj_vectors.rs index 1eb4334..5abd0d1 100644 --- a/crates/trie/tests/rskj_vectors.rs +++ b/crates/trie/tests/rskj_vectors.rs @@ -295,6 +295,59 @@ fn no_junk_blob_can_panic_the_rebuild() { } } +/// Nor may any variation on a chunk an rskj node really sent, rebuilt +/// against the root it really belongs to. Random bytes seldom decode at all, +/// so these are what reach the rebuild. +#[test] +fn no_variation_on_an_rskj_chunk_can_panic_the_rebuild() { + use rustock_trie::snapshot_legacy::{decode_blob, rebuild}; + + let mut seed = 0x2545F4914F6CDD1Du64; + let mut next = move |below: usize| { + seed ^= seed << 13; + seed ^= seed >> 7; + seed ^= seed << 17; + (seed % below.max(1) as u64) as usize + }; + let text = include_str!("rskj-vectors/chunks.txt"); + let mut keys = 0usize; + let mut seeds = 0; + for line in text.lines() { + if let Some(rest) = line.strip_prefix("KEYS ") { + keys = rest.split_whitespace().next().unwrap().parse().unwrap(); + continue; + } + let Some(rest) = line.strip_prefix("BLOB ") else { + continue; + }; + let (root, store) = trie(keys); + let root_hash = root.compute_hash(&store); + let blob = unhex(rest.trim()); + seeds += 1; + for _ in 0..800 { + let mut bytes = blob.clone(); + for _ in 0..1 + next(3) { + let at = next(bytes.len()); + match next(6) { + 0 if !bytes.is_empty() => bytes[at] ^= 1 << next(8), + 1 if !bytes.is_empty() => bytes[at] = [0x00, 0x7f, 0x80, 0xff][next(4)], + 2 if !bytes.is_empty() => bytes[at] = next(256) as u8, + 3 => bytes.insert(at, next(256) as u8), + 4 if !bytes.is_empty() => { + let n = 1 + next(8.min(bytes.len() - at)); + bytes.drain(at..at + n); + } + _ => bytes.truncate(at), + } + } + if let Some(chunk) = decode_blob(&bytes) { + let _ = rebuild(&chunk, root_hash); + } + } + } + assert!(seeds >= 6, "only {seeds} chunks to vary"); +} + fn unhex(text: &str) -> Vec { (0..text.len()) .step_by(2) From ac0c944b5b3944adfec180e4e7837c9dad6d47c9 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Sun, 8 Nov 2026 23:42:05 -0800 Subject: [PATCH 043/264] execution: DUPN, SWAPN and TXINDEX before RSKIP191, as rskj runs them Until RSKIP191 (iris300: mainnet #3,614,800, testnet #2,060,500) rskj's VM has three opcodes of RSK's own, and rustock had none of them: revm has no opcode at 0xa8 to 0xaa, so a contract that used one failed and spent all its gas where rskj ran on, and its block's state root split. - DUPN (0xa8, 3 gas) pops a depth d and is DUP(d + 1); SWAPN (0xa9, 3 gas) pops d and is SWAP(d + 1) (VM.doDUPN, doSWAPN). A depth the stack doesn't reach fails and spends all the gas, as verifyStackSize and DataWord.intValueCheck throw. - Both call program.step() twice, so the byte after them never runs, though a jump may land on it: Program.computeJumpDests skips only PUSH data, as revm's analysis does. Where the second step leaves the code, Program.setPC stops the program. revm pads code with as little as one STOP and reads the next opcode unchecked, so DUPN or SWAPN among the code's last two bytes stops there rather than stepping off the end. - TXINDEX (0xaa, 2 gas) pushes the index of the transaction in its block, counted from 0 as BlockExecutor counts, the same in the calls it makes (ProgramInvokeFactoryImpl), and 0 in a call outside a block (ReversibleTransactionExecutor). From RSKIP191 all three are invalid opcodes, now installed as such explicitly. The all-ops tracer dispatches them the same way. Tests: DUPN and SWAPN of every depth against DUP1-16, SWAP1-16 and rskj's stack beyond them, with their gas; depths the stack doesn't reach, and one no int holds; the byte after them and the end of the code; TXINDEX in a block, in a nested call and in a local call; and all of them failing from RSKIP191. --- crates/execution/src/executor.rs | 232 +++++++++++++++++++++++ crates/execution/src/hardfork.rs | 6 + crates/execution/src/rsk_instructions.rs | 124 ++++++++++++ 3 files changed, 362 insertions(+) diff --git a/crates/execution/src/executor.rs b/crates/execution/src/executor.rs index dc564a8..a212c86 100644 --- a/crates/execution/src/executor.rs +++ b/crates/execution/src/executor.rs @@ -412,6 +412,7 @@ impl RskExecutor { self.hardfork_cfg.has_rskip91(header.number), self.hardfork_cfg.has_rskip446(header.number), self.hardfork_cfg.has_rskip445(header.number), + !self.hardfork_cfg.has_rskip191(header.number), &extcodesize_max_precompiles, &extcodehash_precompiles, header.beneficiary, @@ -546,6 +547,7 @@ impl RskExecutor { self.hardfork_cfg.has_rskip91(header.number), self.hardfork_cfg.has_rskip446(header.number), self.hardfork_cfg.has_rskip445(header.number), + !self.hardfork_cfg.has_rskip191(header.number), &extcodesize_max_precompiles, &extcodehash_precompiles, header.beneficiary, @@ -714,6 +716,7 @@ impl RskExecutor { self.hardfork_cfg.has_rskip91(header.number), self.hardfork_cfg.has_rskip446(header.number), self.hardfork_cfg.has_rskip445(header.number), + !self.hardfork_cfg.has_rskip191(header.number), &extcodesize_max_precompiles, &extcodehash_precompiles, header.beneficiary, @@ -1366,6 +1369,7 @@ impl RskExecutor { self.hardfork_cfg.has_rskip91(header.number), self.hardfork_cfg.has_rskip446(header.number), self.hardfork_cfg.has_rskip445(header.number), + !self.hardfork_cfg.has_rskip191(header.number), &extcodesize_max_precompiles, &extcodehash_precompiles, header.beneficiary, @@ -1470,6 +1474,10 @@ impl RskExecutor { // RSKIP-144: everything this transaction touches belongs to the // sublist that runs it. Inert unless the block declares edges. crate::collision::begin_transaction(i); + // TXINDEX, before RSKIP191, pushes the transaction's place in its + // block, counted from 0 as rskj's `BlockExecutor` counts it. A + // miner's candidate that is dropped keeps its place, as in rskj. + crate::rsk_instructions::set_transaction_index(i as u64); // rskj `TransactionExecutor.gasIsValid`: a transaction's gas // **limit** must fit in the container's limit minus the gas @@ -7941,6 +7949,230 @@ mod tests { ); } + /// A call to `to` with no data and no value, with a gas limit of 1,000,000 + /// at a gas price of 0. + fn bare_call(nonce: u64, to: Address) -> rustock_core::Transaction { + rustock_core::Transaction { + nonce, + gas_price: U256::ZERO, + gas_limit: U256::from(1_000_000), + to: Bytes::copy_from_slice(to.as_slice()), + value: U256::ZERO, + input: Bytes::new(), + v: 0, + r: U256::ZERO, + s: U256::ZERO, + cached_rlp: None, + } + } + + /// Runs `code` as a contract, called by a [`bare_call`] from a funded + /// sender in a block at `height` on mainnet's rules. + fn run_contract(height: u64, code: &[u8]) -> TxExecutionResult { + let sender = Address::repeat_byte(0xC1); + let contract = Address::repeat_byte(0xC2); + let store = Arc::new(MemoryTrieStore::new()); + let one_rbtc = U256::from(10u64).pow(U256::from(18)); + let root = put_account(&TrieNode::empty(), store.as_ref(), &sender, 0, one_rbtc); + let root = put_account_with_code(&root, store.as_ref(), &contract, 1, U256::ZERO, code); + let dir = tempfile::tempdir().unwrap(); + let executor = RskExecutor::new( + RskHardforkConfig::mainnet(), + Arc::new(BlockStore::open(dir.path()).unwrap()), + ); + let mut block = executor + .execute_block( + &dummy_header(height), + &[(bare_call(0, contract), sender)], + &root, + store, + ) + .unwrap(); + block.tx_results.remove(0) + } + + /// DUPN and SWAPN before RSKIP191 (iris300), as rskj's `VM.doDUPN` and + /// `doSWAPN` run them: each pops a depth `d`, and DUPN is then DUP(d + 1) + /// and SWAPN is SWAP(d + 1), at the same 3 gas, stepping over the byte + /// after it, which never runs. Checked against DUP1 to DUP16 and SWAP1 to + /// SWAP16, and past them against rskj's stack. A depth the stack doesn't + /// reach fails and spends all the gas, as rskj's `verifyStackSize` and + /// `intValueCheck` throw. From RSKIP191 every one of them fails, rskj + /// throwing invalid-opcode. + #[test] + fn dupn_and_swapn_run_as_rskj_runs_them_before_rskip191() { + const IRIS: u64 = 3_614_800; + // Seventeen distinct words, 1 to 17, then `ops`, then `left` words of + // the stack written out from the top, and returned. + let program = |ops: &[u8], left: usize| { + let mut code: Vec = (1..=17u8).flat_map(|word| [0x60, word]).collect(); + code.extend_from_slice(ops); + for word in 0..left { + code.push(0x61); // PUSH2 + code.extend(((word * 32) as u16).to_be_bytes()); + code.push(0x52); // MSTORE + } + code.push(0x61); + code.extend(((left * 32) as u16).to_be_bytes()); + code.extend([0x60, 0x00, 0xf3]); // RETURN + code + }; + let words = |output: &[u8]| -> Vec { + output + .chunks(32) + .map(|word| U256::from_be_slice(word).saturating_to::()) + .collect() + }; + // rskj's stack after DUPN or SWAPN of `depth`, top first, or None + // where `verifyStackSize` throws. + let rskj = |swapn: bool, depth: usize| -> Option> { + let mut stack: Vec = (1..=17).collect(); + let top = stack.len() - 1; + if swapn { + stack.swap(top, top.checked_sub(depth + 1)?); + } else { + stack.push(stack[top.checked_sub(depth)?]); + } + stack.reverse(); + Some(stack) + }; + for depth in 0..=17u8 { + for (swapn, name, opcode, plain, left) in [ + (false, "DUPN", 0xa8, 0x80, 18), + (true, "SWAPN", 0xa9, 0x90, 17), + ] { + // INVALID after the opcode fails the call if it runs. + let code = program(&[0x60, depth, opcode, 0xfe], left); + let ran = run_contract(IRIS - 1, &code); + match rskj(swapn, depth.into()) { + Some(stack) => { + assert!(ran.success, "{name} of {depth}"); + assert_eq!(words(&ran.output), stack, "{name} of {depth}"); + if depth < 16 { + let same = run_contract(IRIS - 1, &program(&[plain + depth], left)); + assert_eq!(ran.output, same.output, "{name} of {depth}"); + // The PUSH1 of the depth is the 3 more. + assert_eq!(ran.gas_used, same.gas_used + 3, "{name} of {depth}"); + } + } + None => { + assert!(!ran.success, "{name} of {depth} reaches below the stack"); + assert_eq!(ran.gas_used, 1_000_000, "{name} of {depth}"); + } + } + let from = run_contract(IRIS, &code); + assert!(!from.success, "{name} is gone from RSKIP191"); + assert_eq!(from.gas_used, 1_000_000, "{name} from RSKIP191"); + } + } + // A depth no Java int holds fails too. + for opcode in [0xa8, 0xa9] { + let mut ops = vec![0x7f]; // PUSH32 + ops.extend([0xff; 32]); + ops.extend([opcode, 0xfe]); + let ran = run_contract(IRIS - 1, &program(&ops, 17)); + assert!(!ran.success); + assert_eq!(ran.gas_used, 1_000_000); + } + } + + /// The byte after DUPN or SWAPN never runs, rskj's `doDUPN` and `doSWAPN` + /// calling `program.step()` twice, though a jump may land on it: rskj's + /// jump analysis, like revm's, skips only PUSH data. Where the second + /// step leaves the code, rskj's `Program.setPC` stops the program, so + /// either opcode among the code's last two bytes ends the call in success, + /// where revm would read past its padding. + #[test] + fn dupn_and_swapn_step_over_the_next_byte_and_stop_past_the_end() { + const BEFORE_IRIS: u64 = 3_614_799; + let dupn = [0x60, 0x07, 0x60, 0x00, 0xa8]; // 7, depth 0, DUPN + let swapn = [0x60, 0x07, 0x60, 0x08, 0x60, 0x00, 0xa9]; // 7, 8, depth 0, SWAPN + for (start, gas) in [(&dupn[..], 9), (&swapn[..], 12)] { + for (after, succeeds) in [ + (&[][..], true), + (&[0xfe][..], true), + (&[0xfe, 0xfe][..], false), + ] { + let code = [start, after].concat(); + let ran = run_contract(BEFORE_IRIS, &code); + let hex = alloy_primitives::hex::encode(&code); + assert_eq!(ran.success, succeeds, "0x{hex}"); + if succeeds { + assert_eq!(ran.gas_used, 21_000 + gas, "0x{hex}"); + } + } + } + // Over DUPN onto the JUMPDEST after it. + let ran = run_contract(BEFORE_IRIS, &[0x60, 0x04, 0x56, 0xa8, 0x5b, 0x00]); + assert!(ran.success); + assert_eq!(ran.gas_used, 21_000 + 3 + 8 + 1); + } + + /// TXINDEX before RSKIP191, rskj `VM.doTXINDEX`: the index in its block of + /// the transaction being executed, counted from 0 as rskj's + /// `BlockExecutor` counts; the same in a call that transaction makes, as + /// `ProgramInvokeFactoryImpl` passes it on; and 0 in a call outside a + /// block, as `ReversibleTransactionExecutor` runs it. It costs 2 gas, and + /// from RSKIP191 it fails. + #[test] + fn txindex_pushes_the_transactions_place_in_its_block_before_rskip191() { + const IRIS: u64 = 3_614_800; + let sender = Address::repeat_byte(0xC1); + let reader = Address::repeat_byte(0xC2); + let caller = Address::repeat_byte(0xC3); + // TXINDEX, returned. + let reader_code = [0xaa, 0x60, 0x00, 0x52, 0x60, 0x20, 0x60, 0x00, 0xf3]; + // Calls `reader` and returns its answer. + let mut caller_code = vec![ + 0x60, 0x20, 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x73, + ]; + caller_code.extend_from_slice(reader.as_slice()); + caller_code.extend([0x5a, 0xf1, 0x50, 0x60, 0x20, 0x60, 0x00, 0xf3]); + let store = Arc::new(MemoryTrieStore::new()); + let one_rbtc = U256::from(10u64).pow(U256::from(18)); + let mut root = put_account(&TrieNode::empty(), store.as_ref(), &sender, 0, one_rbtc); + root = put_account_with_code(&root, store.as_ref(), &reader, 1, U256::ZERO, &reader_code); + root = put_account_with_code(&root, store.as_ref(), &caller, 1, U256::ZERO, &caller_code); + let dir = tempfile::tempdir().unwrap(); + let executor = RskExecutor::new( + RskHardforkConfig::mainnet(), + Arc::new(BlockStore::open(dir.path()).unwrap()), + ); + let txs = [ + (bare_call(0, reader), sender), + (bare_call(1, reader), sender), + (bare_call(2, caller), sender), + (bare_call(3, reader), sender), + ]; + let index = |result: &TxExecutionResult| { + assert!(result.success); + U256::from_be_slice(&result.output).saturating_to::() + }; + let block = executor + .execute_block(&dummy_header(IRIS - 1), &txs, &root, store.clone()) + .unwrap(); + assert_eq!( + block.tx_results.iter().map(index).collect::>(), + [0, 1, 2, 3] + ); + // TXINDEX 2, PUSH1 3, MSTORE 3 and 3 for a word of memory, two PUSH1s 6. + assert_eq!(block.tx_results[0].gas_used, 21_000 + 17); + let outside = executor + .execute_local_call( + &dummy_header(IRIS - 1), + &bare_call(0, reader), + sender, + &root, + store.clone(), + ) + .unwrap(); + assert_eq!(index(&outside), 0); + let from = executor + .execute_block(&dummy_header(IRIS), &txs[..1], &root, store) + .unwrap(); + assert!(!from.tx_results[0].success, "TXINDEX is gone from RSKIP191"); + } + /// BN128 refusing a point (`BN128PrecompiledContract`): before RSKIP197 it /// answers with no bytes, a plain success; from RSKIP197 it throws, which /// in a transaction's own call is the invisible exception. diff --git a/crates/execution/src/hardfork.rs b/crates/execution/src/hardfork.rs index 05bfa6d..6cff4d4 100644 --- a/crates/execution/src/hardfork.rs +++ b/crates/execution/src/hardfork.rs @@ -505,6 +505,12 @@ impl RskHardforkConfig { self.active_upgrade(block_number) >= RskNetworkUpgrade::Iris300 } + /// RSKIP191: DUPN, SWAPN and TXINDEX, RSK's own opcodes, are removed + /// (Iris300). Before it rskj's VM runs them (`VM.executeOpcode`). + pub fn has_rskip191(&self, block_number: u64) -> bool { + self.active_upgrade(block_number) >= RskNetworkUpgrade::Iris300 + } + /// RSKIP197: a failing precompile CALL is handled (push 0, refund the /// surplus, continue) instead of propagating an exception that aborts the /// calling frame and burns all its gas (Iris300). Post-197 a failed diff --git a/crates/execution/src/rsk_instructions.rs b/crates/execution/src/rsk_instructions.rs index ed7945c..317722c 100644 --- a/crates/execution/src/rsk_instructions.rs +++ b/crates/execution/src/rsk_instructions.rs @@ -49,6 +49,13 @@ const BALANCE_GAS: u64 = 400; /// is always active for RSK eras). Mirrors revm's TANGERINE+ static gas. const SUICIDE_STATIC_GAS: u64 = 5000; +/// rskj `OpCodes.OP_DUPN`, `OP_SWAPN` and `OP_TXINDEX`: RSK's own opcodes, +/// which RSKIP191 (iris300) removed. Not revm's `DUPN` and `SWAPN` (EIP-8024, +/// 0xe6 and 0xe7), which rskj doesn't have. +const RSK_DUPN: u8 = 0xa8; +const RSK_SWAPN: u8 = 0xa9; +const RSK_TXINDEX: u8 = 0xaa; + thread_local! { /// Addresses for which EXTCODESIZE must report `2^256-1` (rskj RSKIP90: /// `VM.doCODESIZE` returns `DataWord.MAX_VALUE` for any active precompile). @@ -99,12 +106,31 @@ fn assert_configured(what: &str) { ); } +thread_local! { + /// The index in its block of the transaction being executed, which + /// TXINDEX pushes before RSKIP191. rskj counts a block's transactions + /// from 0 (`BlockExecutor.executeInternal`), and a call a transaction + /// makes sees the transaction's index (`ProgramInvokeFactoryImpl` passes + /// it on). [`install`] sets it to 0, and a block sets each transaction's + /// with [`set_transaction_index`]. + static TX_INDEX: core::cell::Cell = const { core::cell::Cell::new(0) }; +} + +/// Sets the index TXINDEX pushes for the transaction about to run on this +/// thread: its place in its block, counted from 0. See `TX_INDEX`. +pub fn set_transaction_index(index: u64) { + TX_INDEX.with(|i| i.set(index)); +} + /// Install the rskj-semantics CALL family into an instruction table. /// /// `extcodehash_enabled` reflects RSKIP140 (papyrus): the spec is PETERSBURG /// from wasabi on (for the RSKIP120 shift opcodes and RSKIP125 CREATE2), but /// EXTCODEHASH only activates at papyrus — so before that it must throw an /// invalid-opcode exception (consuming all gas), matching rskj's VM. +/// +/// `dupn_swapn_txindex_enabled` is RSKIP191's absence: before iris300 rskj +/// runs RSK's own DUPN, SWAPN and TXINDEX. pub fn install( instructions: &mut EthInstructions, extcodehash_enabled: bool, @@ -114,6 +140,7 @@ pub fn install( staticcall_enabled: bool, transient_storage_enabled: bool, mcopy_enabled: bool, + dupn_swapn_txindex_enabled: bool, extcodesize_max_precompiles: &[Address], extcodehash_precompiles: &[Address], real_coinbase: Address, @@ -310,6 +337,25 @@ pub fn install( Instruction::new(rsk_coinbase::, 2), ); + // RSK's own opcodes, which RSKIP191 (iris300) removed. Before it rskj's VM + // runs DUPN (VERY_LOW, 3 gas), SWAPN (3) and TXINDEX (BASE, 2); from it + // rskj throws invalid-opcode, as revm does for a byte it has no opcode + // for. Installed either way, so that the table says which at every + // height. An EVM built for one call runs TXINDEX as rskj runs a call + // outside a block, at index 0 (`ReversibleTransactionExecutor`); a block + // sets each transaction's with `set_transaction_index`. + TX_INDEX.with(|i| i.set(0)); + if dupn_swapn_txindex_enabled { + instructions.insert_instruction(RSK_DUPN, Instruction::new(rsk_dupn::, 3)); + instructions.insert_instruction(RSK_SWAPN, Instruction::new(rsk_swapn::, 3)); + instructions + .insert_instruction(RSK_TXINDEX, Instruction::new(rsk_txindex::, 2)); + } else { + for op in [RSK_DUPN, RSK_SWAPN, RSK_TXINDEX] { + instructions.insert_instruction(op, Instruction::new(invalid_opcode::, 0)); + } + } + // Divergence-hunt diagnostic (env-gated, temporary): log pc/op/gas for // EVERY opcode. The wrapper re-dispatches to the default PETERSBURG // table, except for the rsk-custom opcodes which dispatch to the local @@ -320,6 +366,7 @@ pub fn install( PUSH0_ENABLED.with(|f| f.set(push0_enabled)); BASEFEE_ENABLED.with(|f| f.set(basefee_enabled)); STATICCALL_ENABLED.with(|f| f.set(staticcall_enabled)); + DUPN_SWAPN_TXINDEX_ENABLED.with(|f| f.set(dupn_swapn_txindex_enabled)); let default_table = revm::interpreter::instructions::instruction_table_gas_changes_spec::< WIRE, HOST, @@ -336,6 +383,8 @@ pub fn install( opcode::SELFBALANCE if istanbul_opcodes_enabled => 5, opcode::PUSH0 if push0_enabled => 2, opcode::BASEFEE if basefee_enabled => 2, + RSK_DUPN | RSK_SWAPN if dupn_swapn_txindex_enabled => 3, + RSK_TXINDEX if dupn_swapn_txindex_enabled => 2, _ => default_table[op as usize].static_gas(), }; instructions.insert_instruction( @@ -555,6 +604,72 @@ fn rsk_coinbase( } } +/// DUPN (before RSKIP191), rskj `VM.doDUPN`: pops a depth `d` and pushes a +/// copy of the item `d + 1` from the top, so DUPN of 0 is DUP1, then steps +/// over the next byte. A depth the stack doesn't reach throws +/// (`Program.verifyStackSize`, or `DataWord.intValueCheck` from 2^31), which +/// spends all of the frame's gas. +fn rsk_dupn(context: InstructionContext<'_, H, WIRE>) { + let Some([depth]) = context.interpreter.stack.popn::<1>() else { + context.interpreter.halt_underflow(); + return; + }; + // At least 1, which `dup` requires. + let item = depth.saturating_to::().checked_add(1); + if !item.is_some_and(|n| context.interpreter.stack.dup(n)) { + context.interpreter.halt_underflow(); + return; + } + step_over_next_byte(context.interpreter); +} + +/// SWAPN (before RSKIP191), rskj `VM.doSWAPN`: pops a depth `d` and swaps the +/// top with the item `d + 2` from the top, so SWAPN of 0 is SWAP1. It fails +/// as DUPN does, and steps over the next byte in the same way. +fn rsk_swapn(context: InstructionContext<'_, H, WIRE>) { + let Some([depth]) = context.interpreter.stack.popn::<1>() else { + context.interpreter.halt_underflow(); + return; + }; + // `exchange(0, m)` swaps the top with the item `m` below it; `m` is at + // least 1, which it requires. + let below = depth.saturating_to::().checked_add(1); + if !below.is_some_and(|m| context.interpreter.stack.exchange(0, m)) { + context.interpreter.halt_underflow(); + return; + } + step_over_next_byte(context.interpreter); +} + +/// rskj's second `program.step()` in `doDUPN` and `doSWAPN`: the counter +/// moves past the byte after the opcode, which never runs, though a jump may +/// still land on it (`Program.computeJumpDests` skips only PUSH data, as +/// revm's analysis does). Where that takes the counter past the code, +/// rskj's `Program.setPC` stops the program. revm pads code with as little +/// as one STOP and reads the next opcode unchecked, so the stop is made here +/// rather than by stepping off the end. +fn step_over_next_byte( + interpreter: &mut revm::interpreter::Interpreter, +) { + use revm::interpreter::interpreter_types::{Jumps, LegacyBytecode}; + // revm stepped past the opcode itself before running it. + if interpreter.bytecode.pc() + 1 >= interpreter.bytecode.bytecode_len() { + interpreter.halt(InstructionResult::Stop); + } else { + interpreter.bytecode.relative_jump(1); + } +} + +/// TXINDEX (before RSKIP191), rskj `VM.doTXINDEX`: pushes the index in its +/// block of the transaction being executed. See `TX_INDEX`. +fn rsk_txindex(context: InstructionContext<'_, H, WIRE>) { + assert_configured("TX_INDEX"); + let index = TX_INDEX.with(|i| i.get()); + if !context.interpreter.stack.push(U256::from(index)) { + context.interpreter.halt_overflow(); + } +} + /// SELFBALANCE without revm's ISTANBUL spec check (RSKIP151, papyrus200). fn rsk_selfbalance( context: InstructionContext<'_, H, WIRE>, @@ -627,6 +742,8 @@ thread_local! { static BASEFEE_ENABLED: core::cell::Cell = const { core::cell::Cell::new(false) }; /// RSKIP91 flag for the all-ops tracer's STATICCALL dispatch. static STATICCALL_ENABLED: core::cell::Cell = const { core::cell::Cell::new(true) }; + /// RSKIP191 flag for the all-ops tracer's DUPN/SWAPN/TXINDEX dispatch. + static DUPN_SWAPN_TXINDEX_ENABLED: core::cell::Cell = const { core::cell::Cell::new(false) }; } /// All-ops tracer: log pc/opcode/gas (post-static-charge), then dispatch to @@ -697,6 +814,12 @@ fn traced_all( invalid_opcode(context) } } + RSK_DUPN | RSK_SWAPN | RSK_TXINDEX if !DUPN_SWAPN_TXINDEX_ENABLED.with(|f| f.get()) => { + invalid_opcode(context) + } + RSK_DUPN => rsk_dupn(context), + RSK_SWAPN => rsk_swapn(context), + RSK_TXINDEX => rsk_txindex(context), _ => { let table = revm::interpreter::instructions::instruction_table_gas_changes_spec::< WIRE, @@ -1203,6 +1326,7 @@ mod thread_affinity_tests { true, false, false, + false, &[Address::new([0x01; 20])], &[Address::new([0x01; 20])], Address::new([0xbb; 20]), From 559b7f1002385f4467d6e376d3f4dfd1e15d9003 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Mon, 9 Nov 2026 00:16:09 -0800 Subject: [PATCH 044/264] execution: a precompile that throws before RSKIP197 fails its caller, as in rskj Before RSKIP197 (iris300: mainnet #3,614,800, testnet #2,060,500) rskj's Program.executePrecompiled has no error handling. A precompile that throws in a contract's call leaves the VM as a RuntimeException, and VM.steps fails the contract that made the call with all its gas; each frame runs under its own vm.play, so the contract above it gets 0 from its CALL and goes on, or the transaction fails with its whole limit if the contract was its own call. rustock spent the forwarded gas and let the caller go on. A caller that forwarded everything ran out anyway, since RSK has no 63/64 rule, but one that held gas back carried on where rskj stopped it (#295). - The precompile provider raises a flag where a pre-RSKIP197 throw reaches a contract's call (Bridge methods, REMASC, the native contracts and the rest), and RskHandler's exec loop halts that contract as the call's result reaches it. - REMASC from a contract: from RSKIP197 its throw costs its getGasForData, 0, so the caller gets all the forwarded gas back; it spent it all before. Before RSKIP197 it fails the caller like any other throw. - getStateForDebugging(), which REMASC answers for any caller, returns RemascState.getEncoded, ABI-encoded as rskj does, not nothing. Tests: a capped forward to a throwing Bridge charges the whole limit before RSKIP197 (it pinned 96,733 as a known divergence); a throw two calls deep fails only the inner contract, which loses all the gas it was given, and from RSKIP197 the inner one goes on; HDWalletUtils' throw with gas held back; REMASC refusing a contract either side of RSKIP197; getStateForDebugging from a transaction and from a contract; the state's RLP, worked by hand from rskj's RLP. Each fails without the change. The two Bridge helpers now pass the selector their comments describe (argsOffset 0x1c, not 0). --- crates/execution/src/bridge/mod.rs | 6 + crates/execution/src/executor.rs | 212 ++++++++++++++++++++++++---- crates/execution/src/precompiles.rs | 72 ++++++---- crates/execution/src/remasc.rs | 71 ++++++++++ crates/execution/src/rsk_handler.rs | 26 +++- 5 files changed, 327 insertions(+), 60 deletions(-) diff --git a/crates/execution/src/bridge/mod.rs b/crates/execution/src/bridge/mod.rs index 2ac55d4..e2944a5 100644 --- a/crates/execution/src/bridge/mod.rs +++ b/crates/execution/src/bridge/mod.rs @@ -502,6 +502,12 @@ pub fn internal_throw_gas(e: &PrecompileError) -> Option { marker_gas(e, INTERNAL_BRIDGE_THROW_MARKER) } +/// Build the internal (depth>1) throw error carrying its `requiredGas`. +/// REMASC throws through it too, with its `getGasForData` of 0. +pub fn internal_throw_marker_with_gas(gas: u64) -> PrecompileError { + marker_with_gas(INTERNAL_BRIDGE_THROW_MARKER, gas) +} + /// Marker prefix for the rskj `Program.callToPrecompiledAddress` insufficient- /// gas branch: when `requiredGas > msg.getGas()` for an INTERNAL (depth>1) /// call, rskj does `refundGas(0); stackPushZero(); track.rollback()` — the diff --git a/crates/execution/src/executor.rs b/crates/execution/src/executor.rs index a212c86..754efb4 100644 --- a/crates/execution/src/executor.rs +++ b/crates/execution/src/executor.rs @@ -384,6 +384,7 @@ impl RskExecutor { } } let invisible_flag = precompile_provider.invisible_exception_flag(); + let aborts_caller = precompile_provider.aborts_caller_flag(); let reached_sequential_only = precompile_provider.reached_sequential_only_slot(); let active_precompiles: Vec
= rsk_precompiles(&self.hardfork_cfg, header.number).addresses().copied().collect(); @@ -426,6 +427,7 @@ impl RskExecutor { !self.hardfork_cfg.has_rskip453(header.number), self.hardfork_cfg.has_rskip125(header.number), invisible_flag, + aborts_caller, pre_rskip150_precompiles, active_precompiles, self.hardfork_cfg.has_rskip171(header.number), @@ -517,6 +519,7 @@ impl RskExecutor { } } let invisible_flag = precompile_provider.invisible_exception_flag(); + let aborts_caller = precompile_provider.aborts_caller_flag(); let reached_sequential_only = precompile_provider.reached_sequential_only_slot(); let active_precompiles: Vec
= rsk_precompiles(&self.hardfork_cfg, header.number).addresses().copied().collect(); @@ -560,6 +563,7 @@ impl RskExecutor { !self.hardfork_cfg.has_rskip453(header.number), self.hardfork_cfg.has_rskip125(header.number), invisible_flag, + aborts_caller, pre_rskip150_precompiles, active_precompiles, self.hardfork_cfg.has_rskip171(header.number), @@ -686,6 +690,7 @@ impl RskExecutor { } } let invisible_flag = precompile_provider.invisible_exception_flag(); + let aborts_caller = precompile_provider.aborts_caller_flag(); let reached_sequential_only = precompile_provider.reached_sequential_only_slot(); let active_precompiles: Vec
= rsk_precompiles(&self.hardfork_cfg, header.number).addresses().copied().collect(); @@ -729,6 +734,7 @@ impl RskExecutor { !self.hardfork_cfg.has_rskip453(header.number), self.hardfork_cfg.has_rskip125(header.number), invisible_flag, + aborts_caller, pre_rskip150_precompiles, active_precompiles, self.hardfork_cfg.has_rskip171(header.number), @@ -1338,6 +1344,7 @@ impl RskExecutor { // it between transactions. let bridge_ctx_slot = precompile_provider.bridge_tx_context_slot(); let invisible_flag = precompile_provider.invisible_exception_flag(); + let aborts_caller = precompile_provider.aborts_caller_flag(); let called_precompiles = precompile_provider.called_precompiles_slot(); let reached_sequential_only = precompile_provider.reached_sequential_only_slot(); let active_precompiles: Vec
= @@ -1909,6 +1916,7 @@ impl RskExecutor { !self.hardfork_cfg.has_rskip453(header.number), self.hardfork_cfg.has_rskip125(header.number), invisible_flag.clone(), + aborts_caller.clone(), pre_rskip150_precompiles.clone(), active_precompiles.clone(), self.hardfork_cfg.has_rskip171(header.number), @@ -2443,25 +2451,15 @@ mod tests { ); } - /// KNOWN DIVERGENCE (#295): a caller that caps the gas it forwards - /// survives here, where rskj kills the transaction. - /// - /// rskj's escaping `RuntimeException` ends the transaction regardless of - /// how much gas the caller still held. Modelling the throw as a - /// spent-everything precompile error reproduces that only when the caller - /// forwarded everything, because then the caller runs out too. A caller - /// using `call{gas: n}` keeps the rest and carries on. - /// - /// Pinned rather than left unsaid, so the gap is visible and so that - /// closing it fails this test instead of passing silently. + /// Before RSKIP197 a throw fails the contract that made the call however + /// much gas it held back (#295): rskj's `RuntimeException` leaves the VM, + /// and `VM.steps` spends all the contract's gas. Here the contract is the + /// transaction's own call, so the transaction is charged its whole limit, + /// though it forwarded only 50,000. #[test] - fn a_capped_forward_still_survives_the_pre_rskip197_throw() { + fn a_pre_rskip197_throw_fails_the_caller_however_much_gas_it_held_back() { let used = bridge_throw_gas_with_capped_forward(2_060_499); - assert_eq!( - used, 96_733, - "only the forwarded 50,000 is burned; rskj would charge the whole \ - 1,000,000 limit -- see #295" - ); + assert_eq!(used, 1_000_000, "the caller fails with all its gas"); // From iris300 both agree: the caller continues on what it kept. let after = bridge_throw_gas_with_capped_forward(2_060_500); @@ -2489,7 +2487,7 @@ mod tests { 0x60, 0x00, // retSize 0x60, 0x00, // retOffset 0x60, 0x04, // argsSize - 0x60, 0x00, // argsOffset (selector is the word's high bytes) + 0x60, 0x1c, // argsOffset: the selector is the word's last four bytes 0x60, 0x00, // value 0x73, // PUSH20 ]; @@ -2549,7 +2547,7 @@ mod tests { 0x60, 0x00, // retSize 0x60, 0x00, // retOffset 0x60, 0x04, // argsSize - 0x60, 0x00, // argsOffset + 0x60, 0x1c, // argsOffset 0x60, 0x00, // value 0x73, // PUSH20 ]; @@ -8223,13 +8221,15 @@ mod tests { /// A contract's call to HDWalletUtils that throws /// (`Program.callToPrecompiledAddress`): from RSKIP197 the call fails and /// costs what the method asked for, and the contract goes on; before it the - /// throw takes all the gas the call was given (#295). + /// throw fails the contract too, with all its gas (#295). #[test] fn a_native_contract_that_throws_inside_a_call_fails_the_call() { let hd_wallet = crate::precompiles::HD_WALLET_UTILS_ADDR; let caller = Address::repeat_byte(0xBB); - // CALL HDWalletUtils with this transaction's calldata and all the gas, - // and return what the CALL pushed. + // CALL HDWalletUtils with this transaction's calldata and 500,000 gas, + // and return what the CALL pushed. The contract keeps the rest of its + // gas: RSK has no 63/64 rule, so with GAS it would keep none and run + // out after any throw, which would hide what the throw itself does. let mut code: Vec = vec![ 0x36, 0x60, 0x00, 0x60, 0x00, 0x37, // mem[0..n] = calldata 0x60, 0x00, 0x60, 0x00, 0x36, 0x60, 0x00, 0x60, @@ -8238,7 +8238,7 @@ mod tests { ]; code.extend_from_slice(hd_wallet.as_slice()); code.extend_from_slice(&[ - 0x5a, 0xf1, // GAS, CALL + 0x62, 0x07, 0xa1, 0x20, 0xf1, // PUSH3 500,000, CALL 0x60, 0x00, 0x52, 0x60, 0x20, 0x60, 0x00, 0xf3, // RETURN the pushed word ]); let contracts: &[(Address, &[u8])] = &[(caller, &code)]; @@ -8277,11 +8277,169 @@ mod tests { ); let before = call(2_000_000, &bad); - assert!( - before.gas_used > 900_000, - "before RSKIP197 the throw takes the gas it was given (got {})", - before.gas_used + assert!(!before.success, "before RSKIP197 the contract fails"); + assert_eq!(before.gas_used, 1_000_000, "with all its gas"); + } + + /// The one transaction's result of [`execute_alone`], on mainnet's rules, + /// with no value and a gas limit of 1,000,000. + fn run_alone( + height: u64, + to: Address, + input: &[u8], + contracts: &[(Address, &[u8])], + ) -> TxExecutionResult { + let block = execute_alone( + RskHardforkConfig::mainnet(), + height, + to, + U256::ZERO, + 1_000_000, + input, + contracts, ); + block.tx_results.into_iter().next().unwrap() + } + + /// Code that CALLs `to` with no data and no value, giving it `gas`, and + /// returns the word the CALL pushed. + fn call_and_return_pushed(to: Address, gas: u32) -> Vec { + // No return area, no arguments, no value. + let mut code = vec![ + 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x73, + ]; + code.extend_from_slice(to.as_slice()); + code.push(0x63); // PUSH4 the gas + code.extend_from_slice(&gas.to_be_bytes()); + code.extend_from_slice(&[ + 0xf1, // CALL + 0x60, 0x00, 0x52, 0x60, 0x20, 0x60, 0x00, 0xf3, // RETURN the pushed word + ]); + code + } + + /// Before RSKIP197 a precompile's throw fails the contract that made the + /// call and no more: the contract above it gets 0 from its CALL and goes + /// on, having lost all the gas it gave. From RSKIP197 the contract that + /// made the call goes on itself. + /// + /// The outer contract gives the inner one `given` gas. The inner one calls + /// HDWalletUtils with no payload, which names no method and throws, and + /// then stops. + #[test] + fn a_pre_rskip197_throw_fails_only_the_contract_that_made_the_call() { + let outer = Address::repeat_byte(0xB1); + let inner = Address::repeat_byte(0xB2); + let mut inner_code = vec![ + 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x73, + ]; + inner_code.extend_from_slice(crate::precompiles::HD_WALLET_UTILS_ADDR.as_slice()); + inner_code.extend_from_slice(&[0x5a, 0xf1, 0x00]); // GAS, CALL, STOP + let run = |height: u64, given: u32| { + let outer_code = call_and_return_pushed(inner, given); + let contracts: &[(Address, &[u8])] = &[(outer, &outer_code), (inner, &inner_code)]; + run_alone(height, outer, &[], contracts) + }; + + let (less, more) = (run(2_000_000, 200_000), run(2_000_000, 300_000)); + for before in [&less, &more] { + assert!(before.success, "the outer contract goes on"); + assert_eq!( + U256::from_be_slice(&before.output), + U256::ZERO, + "its CALL pushes 0" + ); + } + assert_eq!( + more.gas_used - less.gas_used, + 100_000, + "all the gas given is spent" + ); + + let (less, more) = (run(3_614_801, 200_000), run(3_614_801, 300_000)); + for from in [&less, &more] { + assert!(from.success); + assert_eq!( + U256::from_be_slice(&from.output), + U256::from(1), + "its CALL pushes 1" + ); + } + assert_eq!( + more.gas_used, less.gas_used, + "what the inner one left comes back" + ); + } + + /// A contract's call to REMASC, which refuses anything but + /// `getStateForDebugging()` from any transaction but the block's REMASC + /// one (`Remasc.processMinersFees`). From RSKIP197 the call fails and costs + /// REMASC's `getGasForData`, 0, so all the gas given comes back. Before it + /// the contract that made the call fails. + #[test] + fn a_contract_calling_remasc_is_refused_as_rskj_refuses_it() { + let caller = Address::repeat_byte(0xB3); + let run = |height: u64, given: u32| { + let code = call_and_return_pushed(crate::precompiles::REMASC_ADDR, given); + let contracts: &[(Address, &[u8])] = &[(caller, &code)]; + run_alone(height, caller, &[], contracts) + }; + + let (less, more) = (run(3_614_801, 200_000), run(3_614_801, 300_000)); + for from in [&less, &more] { + assert!(from.success, "the contract goes on"); + assert_eq!( + U256::from_be_slice(&from.output), + U256::ZERO, + "its CALL pushes 0" + ); + } + assert_eq!(more.gas_used, less.gas_used, "all the gas given comes back"); + + let before = run(2_000_000, 200_000); + assert!(!before.success, "before RSKIP197 the contract fails"); + assert_eq!(before.gas_used, 1_000_000, "with all its gas"); + } + + /// `getStateForDebugging()`, the REMASC method with no caller guard, + /// answers a transaction and a contract alike with REMASC's state: + /// `RemascState.getEncoded`, ABI-encoded. With nothing stored that is two + /// zero coins, the empty siblings list and a false flag. + #[test] + fn remasc_answers_get_state_for_debugging_to_any_caller() { + let remasc = crate::precompiles::REMASC_ADDR; + let selector = crate::remasc::GET_STATE_FOR_DEBUGGING; + let expected = crate::abi::encode_bytes(&[0xc4, 0x80, 0x80, 0xc0, 0x00]); + + let direct = run_alone(3_614_801, remasc, &selector, &[]); + assert!(direct.success); + assert_eq!(direct.output, expected); + assert_eq!( + direct.gas_used, + intrinsic_gas(&selector, 68), + "REMASC asks for no gas" + ); + + // A contract that makes the same call and returns what it got back. + let caller = Address::repeat_byte(0xB4); + let mut code = vec![0x63]; // PUSH4 the selector + code.extend_from_slice(&selector); + code.extend_from_slice(&[ + 0x60, 0x00, 0x52, // MSTORE: the selector is the word's last four bytes + 0x60, 0x00, 0x60, 0x00, // no return area + 0x60, 0x04, 0x60, 0x1c, 0x60, 0x00, // arguments 0x1c..0x20, no value + 0x73, // PUSH20 + ]); + code.extend_from_slice(remasc.as_slice()); + code.extend_from_slice(&[ + 0x5a, 0xf1, 0x50, // GAS, CALL, POP + 0x3d, 0x60, 0x00, 0x60, 0x00, 0x3e, // RETURNDATACOPY all of it to 0 + 0x3d, 0x60, 0x00, 0xf3, // RETURN it + ]); + let contracts: &[(Address, &[u8])] = &[(caller, &code)]; + let via = run_alone(3_614_801, caller, &[], contracts); + assert!(via.success); + assert_eq!(via.output, expected); } fn hex_to_bytes(hex: &str) -> Vec { diff --git a/crates/execution/src/precompiles.rs b/crates/execution/src/precompiles.rs index 87a120d..610e3dd 100644 --- a/crates/execution/src/precompiles.rs +++ b/crates/execution/src/precompiles.rs @@ -428,6 +428,12 @@ pub struct RskPrecompileProvider { /// finalization: no leftover refund, REMASC gets gasLimit * gasPrice. /// Cleared by `RskHandler::load_accounts` before each transaction. invisible_exception: std::sync::Arc, + /// Set when a precompile that a contract called throws before RSKIP197. + /// rskj's `Program.executePrecompiled` has no error handling: the throw + /// leaves the VM as a `RuntimeException`, and `VM.steps` fails the + /// contract that made the call, spending all its gas. `RskHandler` + /// halts that contract as it takes the call's result. + aborts_caller: std::sync::Arc, /// Precompiles successfully called during the current transaction. rskj /// writes the contract storage-prefix marker (`setupContract`) on every /// successful precompile call; the executor drains this after each tx and @@ -464,6 +470,11 @@ impl RskPrecompileProvider { std::sync::Arc::clone(&self.invisible_exception) } + /// Returns the shared caller-abort flag for `RskHandler` (see the field). + pub fn aborts_caller_flag(&self) -> std::sync::Arc { + std::sync::Arc::clone(&self.aborts_caller) + } + /// Returns the shared list of successfully-called precompiles. pub fn called_precompiles_slot(&self) -> std::sync::Arc>> { std::sync::Arc::clone(&self.called_precompiles) @@ -539,6 +550,7 @@ impl RskPrecompileProvider { bridge_config: BridgeConstants::mainnet(), bridge_tx_context: std::sync::Arc::new(std::sync::Mutex::new(BridgeTxContext::default())), invisible_exception: std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)), + aborts_caller: std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)), called_precompiles: std::sync::Arc::new(std::sync::Mutex::new(Vec::new())), reached_sequential_only: std::sync::Arc::new( std::sync::atomic::AtomicBool::new(false), @@ -643,30 +655,21 @@ impl RskPrecompileProvider { assert!(underflow, "gas pre-checked against the cost"); return Ok(Some(result)); } - // Internal (depth>1) Bridge method throw: rskj + // Internal (depth>1) throw, by a Bridge method or by REMASC: rskj // `Program.executePrecompiledAndHandleError` consumes only // `requiredGas` and the CALL fails (pushes zero). Record that cost // and revert so the caller refunds the forwarded leftover. if let Some(spent) = crate::bridge::internal_throw_gas(e) { if !self.rskip197_active(context) { - // Before RSKIP197 (iris300) there is no error handling at - // all. `Program.executePrecompiled` wraps the method's - // `VMException` in a `RuntimeException`, which escapes the - // VM: no `stackPushZero`, no rollback-and-continue. The - // transaction dies and is charged its whole gas limit. - // - // Modelled as a non-revert precompile error, which spends - // the entire forwarded gas. A caller that forwarded - // everything -- what a bare Solidity `.call()` does -- is - // then left with the 63/64 remainder and runs out, so the - // transaction ends where rskj ends it, with the same gas. - // A caller holding back enough to continue would carry on - // here and not there; that case is #295. - // - // Testnet #808,484 is the live instance: a constructor - // calls the Bridge with a selector that is not a Bridge - // method, which post-RSKIP88 throws. - let _ = spent; + // Before RSKIP197 (iris300) `Program.executePrecompiled` + // has no error handling: the throw leaves the VM and fails + // the contract that made the call, with all its gas, which + // `RskHandler` does on the flag. Testnet #808,484 is the + // live instance: a constructor calls the Bridge with a + // selector that is not a Bridge method, which post-RSKIP88 + // throws, and the transaction is charged its whole limit. + self.aborts_caller + .store(true, std::sync::atomic::Ordering::Relaxed); let underflow = result.gas.record_cost(inputs.gas_limit); assert!(underflow, "spending exactly the forwarded gas"); result.result = InstructionResult::PrecompileError; @@ -774,29 +777,32 @@ impl RskPrecompileProvider { /// which the executor dispatches directly — every invocation arriving /// here is a user tx or an internal CALL. rskj (Remasc.processMinersFees) /// throws RemascInvalidInvocationException for those, EXCEPT - /// getStateForDebugging() which has no caller guard and succeeds - /// (no state changes, so only its return data is modelled loosely). + /// getStateForDebugging(), which has no caller guard and answers with the + /// contract's state. (Its `save()` also writes the `siblings` cell, which + /// every block's REMASC transaction has written since block 1.) fn run_remasc( &self, context: &mut CTX, input: &[u8], _gas_limit: u64, ) -> Result { - const GET_STATE_FOR_DEBUGGING: [u8; 4] = [0x0d, 0x0c, 0xee, 0x93]; - if input.len() == 4 && input[..4] == GET_STATE_FOR_DEBUGGING { - return Ok(PrecompileOutput::new(0, Bytes::new())); + // `RemascContract.getFunction` takes a selector of exactly four bytes. + if input == crate::remasc::GET_STATE_FOR_DEBUGGING { + let state = crate::remasc::state_for_debugging(context); + return Ok(PrecompileOutput::new(0, state.into())); } // Everything else throws in rskj: invisible for a direct tx - // (receipt SUCCESS, intrinsic gas, sender charged the full limit), - // a plain CALL failure for internal calls. + // (receipt SUCCESS, intrinsic gas, sender charged the full limit). + // For a contract's call it is the Bridge's internal throw, costing + // REMASC's getGasForData: from RSKIP197 a failed CALL that gives all + // the forwarded gas back, and before it the end of the contract that + // made the call. use revm::context_interface::JournalTr; if context.journal().depth() == 1 { // REMASC getGasForData is 0; the invisible path forces 0 anyway. return Err(crate::bridge::invisible_exception_marker_with_gas(0)); } - Err(PrecompileError::other( - "Remasc: invoked outside the block's REMASC system tx", - )) + Err(crate::bridge::internal_throw_marker_with_gas(0)) } /// One of rskj's native contracts, on the block executing. @@ -850,8 +856,9 @@ impl RskPrecompileProvider { /// `RskHandler` turns the transaction's result back into a success. /// /// In a contract's call, from RSKIP197 a throw costs what the precompile - /// asked for and the call fails. Before it the throw escapes the VM, which - /// spending the call's gas approximates (#295). + /// asked for and the call fails. Before it the throw escapes the VM and + /// fails the contract that made the call, with all its gas: the call fails + /// here, and `RskHandler` halts that contract on the flag set here. fn finish( &self, context: &CTX, @@ -889,6 +896,8 @@ impl RskPrecompileProvider { } else if self.rskip197_active(context) { InstructionResult::Revert } else { + self.aborts_caller + .store(true, std::sync::atomic::Ordering::Relaxed); InstructionResult::PrecompileError }; } @@ -910,6 +919,7 @@ impl Clone for RskPrecompileProvider { bridge_config: self.bridge_config.clone(), bridge_tx_context: std::sync::Arc::clone(&self.bridge_tx_context), invisible_exception: std::sync::Arc::clone(&self.invisible_exception), + aborts_caller: std::sync::Arc::clone(&self.aborts_caller), called_precompiles: std::sync::Arc::clone(&self.called_precompiles), reached_sequential_only: std::sync::Arc::clone(&self.reached_sequential_only), } diff --git a/crates/execution/src/remasc.rs b/crates/execution/src/remasc.rs index eef3dd9..74305f0 100644 --- a/crates/execution/src/remasc.rs +++ b/crates/execution/src/remasc.rs @@ -184,6 +184,42 @@ pub fn remasc_load_bool(ctx: &mut CTX, key: U256) -> b remasc_sload(ctx, key) != U256::ZERO } +/// `getStateForDebugging()`'s selector. It is the one REMASC method that +/// answers any caller, a contract's call or any transaction. +pub const GET_STATE_FOR_DEBUGGING: [u8; 4] = [0x0d, 0x0c, 0xee, 0x93]; + +/// rskj `RemascContract.getStateForDebugging`: the contract's state as +/// `RemascState.getEncoded` writes it, ABI-encoded as the method's one +/// `bytes` output (`Function.encodeOutputs`). +pub fn state_for_debugging(ctx: &mut CTX) -> Vec { + let reward = remasc_sload(ctx, remasc_storage_key(REWARD_BALANCE_KEY)); + let burned = remasc_sload(ctx, remasc_storage_key(BURNED_BALANCE_KEY)); + let broken = remasc_load_bool(ctx, remasc_storage_key(BROKEN_SELECTION_RULE_KEY)); + crate::abi::encode_bytes(&encode_state(reward, burned, broken)) +} + +/// `RemascState.getEncoded`: an RLP list of the reward balance and the burned +/// balance (`RLP.encodeCoin`, which alloy's encoding of a `U256` matches: +/// `0x80` for zero, else the value's bytes as a string), an empty list where +/// the siblings once were, and the broken-selection-rule flag as a bare byte, +/// 0 or 1, which goes into the list without being encoded. +fn encode_state(reward: U256, burned: U256, broken: bool) -> Vec { + use alloy_rlp::Encodable; + let mut items = Vec::new(); + reward.encode(&mut items); + burned.encode(&mut items); + items.push(alloy_rlp::EMPTY_LIST_CODE); + items.push(u8::from(broken)); + let mut state = Vec::with_capacity(items.len() + 2); + alloy_rlp::Header { + list: true, + payload_length: items.len(), + } + .encode(&mut state); + state.extend_from_slice(&items); + state +} + // --------------------------------------------------------------------------- // Balance transfer helper // --------------------------------------------------------------------------- @@ -803,6 +839,41 @@ mod tests { } } + /// `getStateForDebugging()` as `Function.encodeSignature` hashes it. + #[test] + fn get_state_for_debugging_selector() { + assert_eq!( + GET_STATE_FOR_DEBUGGING, + crate::abi::selector("getStateForDebugging()") + ); + } + + /// `RemascState.getEncoded`, worked by hand from rskj's `RLP`: a zero coin + /// is `0x80`, a coin below `0x80` is its own byte and one from `0x80` a + /// string, the flag goes in bare, and a payload of 56 bytes or more takes + /// the long list header. + #[test] + fn remasc_state_encodes_as_rskj_encodes_it() { + assert_eq!( + encode_state(U256::ZERO, U256::ZERO, false), + [0xc4, 0x80, 0x80, 0xc0, 0x00] + ); + assert_eq!( + encode_state(U256::from(0x1234), U256::from(0x7f), true), + [0xc6, 0x82, 0x12, 0x34, 0x7f, 0xc0, 0x01] + ); + assert_eq!( + encode_state(U256::from(0x80), U256::ZERO, false), + [0xc5, 0x81, 0x80, 0x80, 0xc0, 0x00] + ); + let mut largest = vec![0xf8, 68, 0xa0]; + largest.extend([0xff; 32]); + largest.push(0xa0); + largest.extend([0xff; 32]); + largest.extend([0xc0, 0x01]); + assert_eq!(encode_state(U256::MAX, U256::MAX, true), largest); + } + // ----------------------------------------------------------------------- // Stage 6c unit tests: SelectionRule + SiblingPaymentCalculator // ----------------------------------------------------------------------- diff --git a/crates/execution/src/rsk_handler.rs b/crates/execution/src/rsk_handler.rs index 2fe888c..9dac769 100644 --- a/crates/execution/src/rsk_handler.rs +++ b/crates/execution/src/rsk_handler.rs @@ -68,6 +68,13 @@ pub struct RskHandler { /// receipt stays SUCCESS with only the spent gas (mainnet #764,123: /// receipt 47,408 gas, fee charged for the full 1,000,000 limit). invisible_exception: Arc, + /// Shared with `RskPrecompileProvider`: set when a precompile that a + /// contract called throws before RSKIP197. rskj's + /// `Program.executePrecompiled` lets the throw leave the VM, and + /// `VM.steps` fails the contract that made the call, spending all its gas + /// (testnet #808,484). The exec loop halts that contract as soon as the + /// call's result reaches it. + aborts_caller: Arc, /// Accumulate the fee instead of crediting it to the beneficiary. /// /// rskj builds every sublist executor with `postponeFeePayment = true` @@ -121,6 +128,7 @@ impl RskHandler { empty_create_on_unpayable_deposit, false, invisible_exception, + Arc::default(), None, Vec::new(), false, @@ -139,6 +147,7 @@ impl RskHandler { empty_create_on_unpayable_deposit: bool, rskip125_active: bool, invisible_exception: Arc, + aborts_caller: Arc, pre_rskip150_precompiles: Option>, active_precompiles: Vec
, rskip171_active: bool, @@ -147,6 +156,7 @@ impl RskHandler { empty_create_on_unpayable_deposit, rskip125_active, invisible_exception, + aborts_caller, pre_rskip150_precompiles, active_precompiles, rskip171_active, @@ -185,8 +195,9 @@ where type HaltReason = HaltReason; fn load_accounts(&self, evm: &mut Self::Evm) -> Result<(), Self::Error> { - // Clear any stale invisible-exception flag from a previous tx. + // Clear any stale flag a previous tx left set. self.invisible_exception.store(false, Ordering::Relaxed); + self.aborts_caller.store(false, Ordering::Relaxed); revm::handler::pre_execution::load_accounts::(evm)?; // The default sets the journal spec from CfgEnv; pin it back to // rskj's eternal frontier account semantics. @@ -315,13 +326,15 @@ where Ok(()) } - /// The mainnet exec loop with two additions: + /// The mainnet exec loop with rskj's rules added, among them: /// - immediately after a CREATE frame is initialized, reset the created /// account's nonce to 0 (rskj has no EIP-161; revm seeds it to 1 from /// the per-era CfgEnv spec). Must happen before the constructor runs — /// addresses the new contract derives via CREATE depend on its nonce. /// - frames run through `rsk_frame_run`, which applies the pre-RSKIP453 /// empty-deploy quirk to internal creates. + /// - before RSKIP197, a contract whose call to a precompile threw is + /// halted as it takes the call's result (see `aborts_caller`). fn run_exec_loop( &mut self, evm: &mut Self::Evm, @@ -503,6 +516,15 @@ where // Undo revm's insert_call_outcome buffer clear (EIP-211). evm.frame_stack().get().interpreter.return_data.set_buffer(buffer); } + // The precompile this contract just called threw before RSKIP197: + // the contract stops here, its gas spent and its changes undone, + // as rskj's `VM.steps` stops it. + if self.aborts_caller.swap(false, Ordering::Relaxed) { + evm.frame_stack() + .get() + .interpreter + .halt(InstructionResult::PrecompileError); + } } } } From 779d79566655d1c1107ee65964059521925e1987 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Mon, 9 Nov 2026 00:29:10 -0800 Subject: [PATCH 045/264] execution: ECRecover answers as rskj's, the point at infinity and v included rskj's ECRecover (PrecompiledContracts.ECRecover) is not Ethereum's, which revm runs, in two places, and rustock ran revm's: - A signature whose key is the point at infinity recovers the address rskj gives that key, keccak256("")[12..] (0xdcc703c0...a470), as b14c358 made rustock do for a transaction's sender. revm answered nothing. - isValid reads only the last byte of the v word, so a word whose other bytes aren't zero still recovers a key. revm answered nothing. Everything else already agreed, and the tests hold it: 3,000 gas; the first 128 bytes read; nothing for a v byte other than 27 or 28, an r or s of 0 or the curve's order or more, or a key that doesn't recover; an input too short to hold the hash, v and r answers nothing (rskj's System.arraycopy throws, and execute answers nothing for any exception), and a short s ends in zeros. rustock-core's recovery is shared: recover_address applies rskj's validateComponents and recovers the key as recover_key does, with the point at infinity where libsecp256k1 refuses a key that would have been it. The precompile at 0x01 is rskj's from genesis. Tests: signatures built so that R is the generator, with the hash 1 and s 1 (the point at infinity) or 2 (an ordinary key), for v 27 and 28, held against revm's ECRecover where the two should agree; v with other bytes set; short inputs; and recover_address against a k256 signature, the point at infinity, out-of-range r and s, and an R off the curve. --- crates/core/src/types/transaction.rs | 111 +++++++++++++++---- crates/execution/src/precompiles.rs | 157 ++++++++++++++++++++++++++- 2 files changed, 244 insertions(+), 24 deletions(-) diff --git a/crates/core/src/types/transaction.rs b/crates/core/src/types/transaction.rs index 915c880..8bc066e 100644 --- a/crates/core/src/types/transaction.rs +++ b/crates/core/src/types/transaction.rs @@ -277,6 +277,53 @@ fn recovers_infinity(signature: &[u8; 64], recovery_id: u8, hash: &[u8; 32]) -> } } +/// The key a signature recovers, as rskj recovers it: libsecp256k1's key, or +/// the point at infinity where libsecp256k1 refuses a key that would have +/// been it. `signature` is r then s, big-endian, and `recovery_id` 0 or 1. +fn recover(hash: &[u8; 32], signature: &[u8; 64], recovery_id: u8) -> anyhow::Result { + // `from_u8_masked` takes the low two bits rather than rejecting an + // out-of-range id, which is safe only because every caller has already + // confined `recovery_id` to 0 or 1. + let recid = secp256k1::ecdsa::RecoveryId::from_u8_masked(recovery_id); + let compact = secp256k1::ecdsa::RecoverableSignature::from_compact(signature, recid) + .map_err(|e| anyhow::anyhow!("invalid signature: {e}"))?; + let message = secp256k1::Message::from_digest(*hash); + match secp_context().recover_ecdsa(message, &compact) { + Ok(key) => Ok(Recovered::Key(key)), + Err(_) if recovers_infinity(signature, recovery_id, hash) => Ok(Recovered::Infinity), + Err(e) => Err(anyhow::anyhow!("ECDSA recovery failed: {e}")), + } +} + +/// The address of a key: the last 20 bytes of the keccak256 of its +/// uncompressed encoding, past the leading byte that marks the form. +fn address_of(key: &secp256k1::PublicKey) -> Address { + let pubkey_hash = Keccak256::digest(&key.serialize_uncompressed()[1..]); + Address::from_slice(&pubkey_hash[12..]) +} + +/// The address a signature recovers, as rskj's ECRecover precompile answers +/// it: `ECDSASignature.validateComponents` holds r and s from 1 to below the +/// curve's order, then `ECKey.getAddress` of `Secp256k1Service +/// .signatureToKey`, which is [`INFINITY_SENDER`] for the point at infinity. +/// `None` where rskj answers no address. `signature` is r then s, +/// big-endian; `recovery_id` is `v - 27`, so 0 or 1. +pub fn recover_address(hash: &[u8; 32], signature: &[u8; 64], recovery_id: u8) -> Option
{ + let order = U256::from_be_bytes(secp256k1::constants::CURVE_ORDER); + let (r, s) = signature.split_at(32); + let in_range = |half: &[u8]| { + let value = U256::from_be_slice(half); + !value.is_zero() && value < order + }; + if recovery_id > 1 || !in_range(r) || !in_range(s) { + return None; + } + Some(match recover(hash, signature, recovery_id).ok()? { + Recovered::Key(key) => address_of(&key), + Recovered::Infinity => INFINITY_SENDER, + }) +} + impl Transaction { /// Recover the sender address from the transaction signature. @@ -295,12 +342,7 @@ impl Transaction { /// gives it, [`INFINITY_SENDER`]. pub fn recover_sender(&self, chain_id: u64) -> anyhow::Result
{ Ok(match self.recover_key(chain_id)? { - // The leading byte is the uncompressed-form marker and is not part - // of the key the address is taken from. - Recovered::Key(key) => { - let pubkey_hash = Keccak256::digest(&key.serialize_uncompressed()[1..]); - Address::from_slice(&pubkey_hash[12..]) - } + Recovered::Key(key) => address_of(&key), Recovered::Infinity => INFINITY_SENDER, }) } @@ -330,22 +372,7 @@ impl Transaction { let mut sig_bytes = [0u8; 64]; sig_bytes[..32].copy_from_slice(&self.r.to_be_bytes::<32>()); sig_bytes[32..].copy_from_slice(&self.s.to_be_bytes::<32>()); - - // `from_u8_masked` takes the low two bits rather than rejecting an - // out-of-range id, which is safe only because the branches above - // have already confined `recovery_id` to 0 or 1. - let recid = secp256k1::ecdsa::RecoveryId::from_u8_masked(recovery_id); - let signature = secp256k1::ecdsa::RecoverableSignature::from_compact(&sig_bytes, recid) - .map_err(|e| anyhow::anyhow!("invalid signature: {e}"))?; - let message = secp256k1::Message::from_digest(hash.0); - - match secp_context().recover_ecdsa(message, &signature) { - Ok(key) => Ok(Recovered::Key(key)), - Err(_) if recovers_infinity(&sig_bytes, recovery_id, &hash.0) => { - Ok(Recovered::Infinity) - } - Err(e) => Err(anyhow::anyhow!("ECDSA recovery failed: {e}")), - } + recover(&hash.0, &sig_bytes, recovery_id) } pub fn is_eip155(&self, chain_id: u64) -> bool { @@ -714,6 +741,46 @@ mod tests { tx.s = U256::ZERO; assert!(tx.recover_sender(33).is_err()); } + + /// `recover_address`, as rskj's ECRecover answers: a key's address; the + /// infinity sender where sR = eG; and nothing for an r or s of 0 or the + /// curve's order or more, an id past 1, or an R that isn't on the curve. + #[test] + fn recover_address_answers_as_rskjs_ecrecover() { + use k256::ecdsa::SigningKey; + let key = SigningKey::from_slice(&[1u8; 32]).unwrap(); + // Below the curve's order, so the hash is its own value modulo it. + let hash = [0x11_u8; 32]; + let (signature, id) = key.sign_prehash_recoverable(&hash).unwrap(); + let id = id.to_byte(); + let mut ordinary = [0_u8; 64]; + ordinary.copy_from_slice(&signature.to_bytes()); + let point = key.verifying_key().to_encoded_point(false); + let address = Address::from_slice(&Keccak256::digest(&point.as_bytes()[1..])[12..]); + assert_eq!(recover_address(&hash, &ordinary, id), Some(address)); + assert_eq!(recover_address(&hash, &ordinary, 2), None); + + // R the generator, whose y is even, and s the hash: sR = eG. + let mut infinity = [0_u8; 64]; + infinity[..32].copy_from_slice(&secp256k1::constants::GENERATOR_X); + infinity[32..].copy_from_slice(&hash); + assert_eq!(recover_address(&hash, &infinity, 0), Some(INFINITY_SENDER)); + // With y odd R is -G, and the key an ordinary one. + let other = recover_address(&hash, &infinity, 1); + assert!(other.is_some_and(|other| other != INFINITY_SENDER)); + + let order = secp256k1::constants::CURVE_ORDER; + for (at, bad) in [(0, [0_u8; 32]), (32, [0_u8; 32]), (0, order), (32, order)] { + let mut signature = ordinary; + signature[at..at + 32].copy_from_slice(&bad); + assert_eq!(recover_address(&hash, &signature, id), None); + } + // 5³ + 7 has no square root modulo p. + let mut off_curve = [0_u8; 64]; + off_curve[31] = 5; + off_curve[63] = 1; + assert_eq!(recover_address(&hash, &off_curve, 0), None); + } } impl Transaction { diff --git a/crates/execution/src/precompiles.rs b/crates/execution/src/precompiles.rs index 610e3dd..c55ee32 100644 --- a/crates/execution/src/precompiles.rs +++ b/crates/execution/src/precompiles.rs @@ -136,6 +136,55 @@ fn native_contract(_input: &[u8], _gas_limit: u64) -> Result Result { + const GAS: u64 = 3_000; + if gas_limit < GAS { + return Err(PrecompileError::OutOfGas); + } + let answer = ecrecover(input).map_or_else(Vec::new, |word| word.to_vec()); + Ok(PrecompileOutput::new(GAS, answer.into())) +} + +/// The word ECRecover answers, the recovered address at its end, or `None` +/// for an empty answer. See [`rsk_ecrecover_run`]. +fn ecrecover(input: &[u8]) -> Option<[u8; 32]> { + if input.len() < 96 { + return None; + } + let v = input[63]; + if v != 27 && v != 28 { + return None; + } + let mut hash = [0_u8; 32]; + hash.copy_from_slice(&input[..32]); + let mut signature = [0_u8; 64]; + signature[..32].copy_from_slice(&input[64..96]); + let s = &input[96..input.len().min(128)]; + signature[32..32 + s.len()].copy_from_slice(s); + let address = rustock_core::types::transaction::recover_address(&hash, &signature, v - 27)?; + let mut word = [0_u8; 32]; + word[12..].copy_from_slice(address.as_slice()); + Some(word) +} + /// Secp256k1 point addition. /// Input: up to 128 bytes = [x1(32) | y1(32) | x2(32) | y2(32)], big-endian. /// Output: 64 bytes = [x(32) | y(32)]. @@ -247,9 +296,17 @@ pub fn rsk_precompiles(hardfork_cfg: &RskHardforkConfig, block_number: u64) -> P // rskj gates each precompile on its own RSKIP rather than Ethereum spec // buckets (PrecompiledContracts.getContractForAddress). From genesis: // 0x01-0x04 (ecrecover, sha256, ripemd160, identity) plus 0x05 modexp - // with EIP-198 (Byzantium) pricing. + // with EIP-198 (Byzantium) pricing. ECRecover is rskj's own, in place of + // revm's. let mut precompiles = Precompiles::new(PrecompileSpecId::HOMESTEAD).clone(); - precompiles.extend([revm::precompile::modexp::BYZANTIUM]); + precompiles.extend([ + Precompile::new( + PrecompileId::custom("rsk-ecrecover"), + ECRECOVER_ADDR, + rsk_ecrecover_run, + ), + revm::precompile::modexp::BYZANTIUM, + ]); // RSKIP137 (Papyrus200): BN128 add/mul/pairing, priced per EIP-1108 // (Istanbul: 150 / 6000 / 45000 + 34000 per pair) from activation. @@ -1260,6 +1317,102 @@ mod tests { assert!(precompiles.contains(&ecrecover)); } + // ----------------------------------------------------------------------- + // ECRecover, as rskj's + // ----------------------------------------------------------------------- + + /// The generator's x. Its y is even, so with recovery id 0 the point R + /// whose x is r is the generator itself. + const GENERATOR_X: [u8; 32] = + alloy_primitives::hex!("79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"); + + /// ECRecover's input for the hash 1, `v`, r the generator's x and s = + /// `s`. The key is r⁻¹(sR − G), where R is G for v 27 and −G for 28. + fn ecrecover_input(v: u8, s: u8) -> Vec { + let mut input = vec![0_u8; 128]; + input[31] = 1; + input[63] = v; + input[64..96].copy_from_slice(&GENERATOR_X); + input[127] = s; + input + } + + fn rskj_ecrecover(input: &[u8]) -> Bytes { + rsk_ecrecover_run(input, 3_000).unwrap().bytes + } + + fn ethereum_ecrecover(input: &[u8]) -> Bytes { + use revm::precompile::secp256k1::ec_recover_run; + ec_recover_run(input, 3_000).unwrap().bytes + } + + /// For an ordinary signature rskj's ECRecover recovers what Ethereum's + /// does, reading 128 bytes and ending a short s in zeros. + #[test] + fn ecrecover_recovers_an_ordinary_key_as_ethereums_does() { + for v in [27, 28] { + let input = ecrecover_input(v, 2); + let key = rskj_ecrecover(&input); + assert_eq!(key.len(), 32, "v {v} recovers a key"); + assert_eq!(key, ethereum_ecrecover(&input)); + let mut longer = input.clone(); + longer.extend([0xff; 32]); + assert_eq!(rskj_ecrecover(&longer), key, "only 128 bytes are read"); + } + // s = 2^249, all in its first byte. + let mut short = ecrecover_input(27, 0); + short[96] = 2; + let whole = rskj_ecrecover(&short); + assert_eq!(whole.len(), 32); + assert_eq!(rskj_ecrecover(&short[..97]), whole); + assert_eq!(ethereum_ecrecover(&short[..97]), whole); + + // Too short to hold r, a v of neither 27 nor 28, or no input: nothing. + assert!(rskj_ecrecover(&ecrecover_input(27, 2)[..95]).is_empty()); + assert!(rskj_ecrecover(&ecrecover_input(29, 2)).is_empty()); + assert!(rskj_ecrecover(&[]).is_empty()); + assert!(matches!( + rsk_ecrecover_run(&[], 2_999), + Err(PrecompileError::OutOfGas) + )); + } + + /// rskj's `isValid` reads only the last byte of the `v` word, so a word + /// with other bytes set still recovers the key. Ethereum's answers nothing. + #[test] + fn ecrecover_reads_only_the_last_byte_of_v() { + let clean = ecrecover_input(27, 2); + let mut dirty = clean.clone(); + dirty[32] = 0x01; + dirty[50] = 0xff; + assert_eq!(rskj_ecrecover(&dirty), rskj_ecrecover(&clean)); + assert!(ethereum_ecrecover(&dirty).is_empty()); + } + + /// With R = G and s = e = 1, sR = eG, and the key is the point at + /// infinity: rskj recovers the address it gives that key, from keccak256 of + /// nothing, where Ethereum's ECRecover answers nothing. With v 28, R is −G + /// and the key an ordinary one. The set from genesis runs rskj's. + #[test] + fn ecrecover_recovers_the_infinity_sender_as_rskj_does() { + let mut infinity = [0_u8; 32]; + infinity[12..] + .copy_from_slice(rustock_core::types::transaction::INFINITY_SENDER.as_slice()); + let infinity = Bytes::copy_from_slice(&infinity); + let input = ecrecover_input(27, 1); + assert_eq!(rskj_ecrecover(&input), infinity); + assert!(ethereum_ecrecover(&input).is_empty()); + + let ordinary = ecrecover_input(28, 1); + assert_ne!(rskj_ecrecover(&ordinary), infinity); + assert_eq!(rskj_ecrecover(&ordinary), ethereum_ecrecover(&ordinary)); + assert_eq!(rskj_ecrecover(&ordinary).len(), 32); + + let set = rsk_precompiles(&RskHardforkConfig::mainnet(), 0); + let ecrecover = set.get(&ECRECOVER_ADDR).unwrap(); + assert_eq!(ecrecover.execute(&input, 3_000).unwrap().bytes, infinity); + } + #[test] fn test_hdwallet_not_active_before_wasabi100() { let cfg = RskHardforkConfig::mainnet(); From b0a643e4bbda363a945a88de334d509d9aaf2524 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Mon, 9 Nov 2026 00:41:02 -0800 Subject: [PATCH 046/264] execution: a contract's input to a precompile is cut to what it reads (RSKIP516) From reed800 (mainnet #8,052,200, testnet #6,835,700) rskj cuts a contract's input to a precompile to the contract's getMaxInput (Program.getInputLength): ECRecover, BN128 addition and secp256k1 addition 128 bytes, BN128 and secp256k1 multiplication 96, BLAKE2F 213. The cut feeds both getGasForData and execute. A transaction's own data reaches a precompile whole (TransactionExecutor.call), and every other precompile takes all of it. rustock never cut it. Only BLAKE2F answers differently. The others never read past their limit, but BLAKE2F refuses an input that isn't exactly 213 bytes, and prices it at 0. So from reed800 a contract that hands BLAKE2F more than 213 bytes gets the compression of the first 213 and pays their rounds, where rustock refused the call. has_rskip516 joins the hardfork config, and the provider applies the cut where it reads a call's input, for a call at depth above 1. Test: a contract passes its own input to BLAKE2F. From reed800 a 214-byte input answers as the 213 bytes before it, and costs 16 gas more, RSKIP400's price for the extra byte of transaction data. At #8,052,199 BLAKE2F refuses it, 12 gas cheaper: 0 instead of its 12 rounds. A transaction sending the 214 bytes to BLAKE2F itself still gets no answer. Disabling the cut, or applying it to a transaction's own input as well, fails the test. --- crates/execution/src/executor.rs | 59 +++++++++++++++++++++++++++++ crates/execution/src/hardfork.rs | 7 ++++ crates/execution/src/precompiles.rs | 22 +++++++++++ 3 files changed, 88 insertions(+) diff --git a/crates/execution/src/executor.rs b/crates/execution/src/executor.rs index 754efb4..debc3f0 100644 --- a/crates/execution/src/executor.rs +++ b/crates/execution/src/executor.rs @@ -8442,6 +8442,65 @@ mod tests { assert_eq!(via.output, expected); } + /// Code that hands its own input to `to`, giving it 100,000 gas, and + /// returns the word the CALL pushed and the 64 bytes `to` answered into. + /// For an input shorter than 512 bytes. + fn pass_input_to(to: Address) -> Vec { + let mut code = vec![ + 0x36, 0x60, 0x00, 0x60, 0x00, 0x37, // CALLDATACOPY all of it to 0 + 0x60, 0x40, 0x61, 0x02, 0x20, // a return area of 64 bytes at 544 + 0x36, 0x60, 0x00, 0x60, 0x00, 0x73, // all of it as arguments, no value + ]; + code.extend_from_slice(to.as_slice()); + code.extend_from_slice(&[ + 0x62, 0x01, 0x86, 0xa0, 0xf1, // PUSH3 100,000, CALL + 0x61, 0x02, 0x00, 0x52, // MSTORE the pushed word at 512 + 0x60, 0x60, 0x61, 0x02, 0x00, 0xf3, // RETURN the word and the area + ]); + code + } + + /// RSKIP516 (reed800): a contract's input to BLAKE2F is cut to the 213 + /// bytes BLAKE2F reads (`Program.getInputLength`). Before it BLAKE2F + /// refuses a longer input, for its price on the wrong length, 0; from it + /// BLAKE2F takes the first 213 bytes and charges their rounds, and a byte + /// past them costs only as the transaction's data. A transaction's own + /// input is never cut (`TransactionExecutor.call`). + #[test] + fn a_contracts_input_to_blake2f_is_cut_to_213_bytes_from_reed800() { + let blake2f = Address::with_last_byte(9); + let caller = Address::repeat_byte(0xB5); + let code = pass_input_to(blake2f); + let contracts: &[(Address, &[u8])] = &[(caller, &code)]; + let answer = |result: &TxExecutionResult| { + assert!(result.success); + let pushed = U256::from_be_slice(&result.output[..32]); + (pushed, result.output[32..].to_vec()) + }; + // Twelve rounds of the final block. + let mut exact = vec![0_u8; 213]; + exact[3] = 12; + exact[212] = 1; + let mut long = exact.clone(); + long.push(0xff); + + let whole = run_alone(8_052_200, caller, &exact, contracts); + let (pushed, hash) = answer(&whole); + assert_eq!(pushed, U256::from(1)); + let cut = run_alone(8_052_200, caller, &long, contracts); + assert_eq!(answer(&cut), (pushed, hash.clone()), "the first 213 bytes"); + // RSKIP400's price for a byte of data that isn't zero. + assert_eq!(cut.gas_used - whole.gas_used, 16, "the byte past them"); + + let before = run_alone(8_052_199, caller, &long, contracts); + assert_eq!(answer(&before).0, U256::ZERO, "BLAKE2F refuses it"); + assert_eq!(cut.gas_used - before.gas_used, 12, "for 0, not 12 rounds"); + + assert_eq!(run_alone(8_052_200, blake2f, &exact, &[]).output, hash); + let direct = run_alone(8_052_200, blake2f, &long, &[]); + assert!(direct.output.is_empty(), "a transaction's input is whole"); + } + fn hex_to_bytes(hex: &str) -> Vec { (0..hex.len()) .step_by(2) diff --git a/crates/execution/src/hardfork.rs b/crates/execution/src/hardfork.rs index 6cff4d4..736b082 100644 --- a/crates/execution/src/hardfork.rs +++ b/crates/execution/src/hardfork.rs @@ -612,6 +612,13 @@ impl RskHardforkConfig { block_number >= height } + /// RSKIP516 (Reed800): the secp256k1 addition and multiplication + /// contracts, and a contract's input to a precompile cut to what the + /// precompile reads (`Program.getInputLength`). + pub fn has_rskip516(&self, block_number: u64) -> bool { + self.active_upgrade(block_number) >= RskNetworkUpgrade::Reed800 + } + /// RSKIP375: pegouts-waiting-for-signatures keyed by the pegout creation /// RSK tx hash again (Fingerroot500). pub fn has_rskip375(&self, block_number: u64) -> bool { diff --git a/crates/execution/src/precompiles.rs b/crates/execution/src/precompiles.rs index c55ee32..7b3a8b1 100644 --- a/crates/execution/src/precompiles.rs +++ b/crates/execution/src/precompiles.rs @@ -566,6 +566,26 @@ impl RskPrecompileProvider { self.hardfork_cfg .has_rskip197(context.block().number().to::()) } + + /// RSKIP516 (reed800): how much of a contract's input the precompile at + /// `addr` takes, its `getMaxInput` (`Program.getInputLength`), or `None` + /// for all of it. A transaction's own data reaches a precompile whole + /// (`TransactionExecutor.call`), and so does a contract's before reed800 + /// or to any other precompile. Only BLAKE2F answers differently for it: + /// the others never read past their limit, while BLAKE2F refuses an input + /// that isn't 213 bytes, at no cost. + fn max_input(&self, context: &CTX, addr: &Address) -> Option { + let block = context.block().number().to::(); + if context.journal().depth() == 1 || !self.hardfork_cfg.has_rskip516(block) { + return None; + } + match *addr { + ECRECOVER_ADDR | BN128_ADD_ADDR | SECP256K1_ADD_ADDR => Some(128), + BN128_MUL_ADDR | SECP256K1_MUL_ADDR => Some(96), + BLAKE2F_ADDR => Some(213), + _ => None, + } + } } /// REMASC and the Bridge, which run with the whole context: they read and @@ -1021,6 +1041,7 @@ impl PrecompileProvider for RskPrecompileProvider } let rskip197 = self.rskip197_active(context); + let max_input = self.max_input(context, &addr); let outcome = { let shared; let input = match &inputs.input { @@ -1035,6 +1056,7 @@ impl PrecompileProvider for RskPrecompileProvider } CallInput::Bytes(bytes) => bytes.as_ref(), }; + let input = max_input.map_or(input, |max| &input[..input.len().min(max)]); if NATIVE_CONTRACTS.contains(&addr) { self.run_native(context, addr, input, inputs.gas_limit) } else { From 124c83fa8157c5b31dae628185477f911bd42f18 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Mon, 9 Nov 2026 01:08:02 -0800 Subject: [PATCH 047/264] execution: before RSKIP552 a transaction's empty call to BLAKE2F fails its block rskj reads a transaction's empty data as null (RLPItem.getRLPData), and TransactionExecutor.call asks the precompile its price before its try: getGasForData(tx.getData()). Before RSKIP552 (vetiver900: mainnet #8,804,200, testnet #7,604,200) BLAKE2F's getGasForData reads the data's length, so a transaction that calls BLAKE2F with no data throws a NullPointerException out of the call, and rskj can't process its block. rustock took the call as an invisible exception and the block as valid. No canonical block holds one. Such a call is now Outcome::Fatal, which fails the block. RSKIP552 gives null data a price of 0 and refuses it like any other wrong length, which rustock already did, and that stays from vetiver900. Every other precompile's getGasForData takes null (ECRecover, SHA-256, RIPEMD-160, Identity, ModExp, the BN128 contracts, the secp256k1 contracts, the native contracts, the Bridge and REMASC), and a contract's call never hands a precompile null (Program.callToPrecompiledAddress). Test: at mainnet #8,804,199 the block fails; at #8,804,200 the transaction succeeds with no output and 21,000 gas; a contract's call with no data at #8,804,199 fails as it always did, pushing 0. Disabling the new rule fails the test. execute_alone's body moves to try_execute_alone, which returns what fails the block. --- crates/execution/src/executor.rs | 48 +++++++++++++++++++++++++++-- crates/execution/src/native/mod.rs | 3 +- crates/execution/src/precompiles.rs | 25 ++++++++++++++- 3 files changed, 71 insertions(+), 5 deletions(-) diff --git a/crates/execution/src/executor.rs b/crates/execution/src/executor.rs index debc3f0..643d2c8 100644 --- a/crates/execution/src/executor.rs +++ b/crates/execution/src/executor.rs @@ -7527,6 +7527,20 @@ mod tests { input: &[u8], contracts: &[(Address, &[u8])], ) -> BlockExecutionResult { + try_execute_alone(cfg, height, to, value, gas_limit, input, contracts) + .expect("block executes") + } + + /// [`execute_alone`], or what fails the block. + fn try_execute_alone( + cfg: RskHardforkConfig, + height: u64, + to: Address, + value: U256, + gas_limit: u64, + input: &[u8], + contracts: &[(Address, &[u8])], + ) -> Result { let store = Arc::new(MemoryTrieStore::new()); let sender = Address::repeat_byte(0xAA); let one_rbtc = U256::from(10u64).pow(U256::from(18)); @@ -7547,9 +7561,8 @@ mod tests { cached_rlp: None, }; let block_store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); - RskExecutor::new(cfg, block_store) - .execute_block(&dummy_header(height), &[(tx, sender)], &root, store) - .expect("block executes") + let executor = RskExecutor::new(cfg, block_store); + executor.execute_block(&dummy_header(height), &[(tx, sender)], &root, store) } fn fee(gas: u64) -> U256 { @@ -8501,6 +8514,35 @@ mod tests { assert!(direct.output.is_empty(), "a transaction's input is whole"); } + /// Before RSKIP552 (vetiver900) rskj can't process a block holding a + /// transaction that calls BLAKE2F with no data: it reads the data as null, + /// and BLAKE2F's `getGasForData` throws a NullPointerException that + /// `TransactionExecutor.call` doesn't catch. From RSKIP552 the call costs + /// 0 and is refused as any other wrong length, an invisible exception. A + /// contract's call with no data was always refused that way. + #[test] + fn a_transactions_empty_call_to_blake2f_fails_its_block_before_rskip552() { + let blake2f = Address::with_last_byte(9); + let alone = |height: u64, to: Address, contracts: &[(Address, &[u8])]| { + let cfg = RskHardforkConfig::mainnet(); + try_execute_alone(cfg, height, to, U256::ZERO, 1_000_000, &[], contracts) + }; + let error = alone(8_804_199, blake2f, &[]).unwrap_err().to_string(); + assert!(error.contains("BLAKE2F"), "{error}"); + + let from = alone(8_804_200, blake2f, &[]).unwrap(); + let direct = &from.tx_results[0]; + assert!(direct.success && direct.output.is_empty()); + assert_eq!(direct.gas_used, 21_000, "BLAKE2F asks for nothing"); + + let caller = Address::repeat_byte(0xB6); + let code = call_and_return_pushed(blake2f, 100_000); + let contracts: &[(Address, &[u8])] = &[(caller, &code)]; + let via = alone(8_804_199, caller, contracts).unwrap(); + let pushed = U256::from_be_slice(&via.tx_results[0].output); + assert_eq!(pushed, U256::ZERO, "a contract's call fails"); + } + fn hex_to_bytes(hex: &str) -> Vec { (0..hex.len()) .step_by(2) diff --git a/crates/execution/src/native/mod.rs b/crates/execution/src/native/mod.rs index a5fe1d7..c41525e 100644 --- a/crates/execution/src/native/mod.rs +++ b/crates/execution/src/native/mod.rs @@ -37,7 +37,8 @@ pub(crate) enum Outcome { /// `execute` threw, and the call cost `gas`: its `getGasForData`, which is /// 0 when no method was called. Threw { gas: u64, reason: String }, - /// The node could not read what the call needs, so it has no answer. + /// The call has no answer, and its block fails: the node could not read + /// what the call needs, or rskj can't process the block. Fatal(String), } diff --git a/crates/execution/src/precompiles.rs b/crates/execution/src/precompiles.rs index 7b3a8b1..32735be 100644 --- a/crates/execution/src/precompiles.rs +++ b/crates/execution/src/precompiles.rs @@ -586,6 +586,27 @@ impl RskPrecompileProvider { _ => None, } } + + /// Whether this is a transaction's own call to BLAKE2F with no data before + /// RSKIP552 (vetiver900), whose block rskj can't process. rskj reads empty + /// data as null (`RLPItem.getRLPData`), and BLAKE2F's `getGasForData` + /// reads its length: a NullPointerException, which + /// `TransactionExecutor.call` doesn't catch. RSKIP552 prices null data at + /// 0 and refuses it as any other wrong length. Every other precompile's + /// `getGasForData` takes null, and a contract's call never passes null + /// (`Program.callToPrecompiledAddress`). + fn blake2f_reads_null( + &self, + context: &CTX, + addr: &Address, + input: &[u8], + ) -> bool { + let block = context.block().number().to::(); + *addr == BLAKE2F_ADDR + && input.is_empty() + && context.journal().depth() == 1 + && !self.hardfork_cfg.has_rskip552(block) + } } /// REMASC and the Bridge, which run with the whole context: they read and @@ -1057,7 +1078,9 @@ impl PrecompileProvider for RskPrecompileProvider CallInput::Bytes(bytes) => bytes.as_ref(), }; let input = max_input.map_or(input, |max| &input[..input.len().min(max)]); - if NATIVE_CONTRACTS.contains(&addr) { + if self.blake2f_reads_null(context, &addr, input) { + Outcome::Fatal("BLAKE2F's getGasForData throws on null data".into()) + } else if NATIVE_CONTRACTS.contains(&addr) { self.run_native(context, addr, input, inputs.gas_limit) } else { run_precompile(precompile, &addr, input, inputs.gas_limit, rskip197) From 5566a2db8a4b0cbd242493a78b39ba89072da402 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Mon, 9 Nov 2026 02:23:38 -0800 Subject: [PATCH 048/264] core: a transaction as rskj reads, hashes, signs and accepts it rskj reads a transaction (Transaction(byte[])) as exactly one RLP list of nine elements, hashes it by an encoding it rebuilds rather than by the bytes it was sent, and takes its signature only if acceptTransactionSignature does. rustock read transactions leniently, hashed the bytes as they came, and checked less of the signature, so the two disagreed on which blocks are valid. Reading (Transaction::from_rskj). Exactly nine elements; `to` empty or 20 bytes; v absent, for no signature, or one byte with r and s present (ECDSASignature.fromComponents throws on none). validate()'s limits apply: a nonce or gas limit of at most 32 bytes, a gas price or value below 2^255, r and s of at most 32 significant bytes, and a gas limit present, which validate() dereferences. A block's transaction may come wrapped in a byte string, since BlockFactory hands each element's getRLPData to the constructor. Refused too is what rskj reads but no block or pool takes: a v naming a negative chain (below 34 but for 27 and 28) and a nonce past 64 bits. Hashing. rskj's hash and transactions root use getEncoded, rebuilt from the fields as it holds them: a nonce of the one byte 0 goes empty and any other stays as it came; the gas price stays empty if it came empty and is otherwise minimal, 0x00 for zero; the gas limit and data stay as they came; the value is minimal; v is rebuilt from the chain id and recovery id; r and s are minimal. That encoding is now the one kept, hashed, put in the trie and served. For what rskj sends the two are the same bytes, since its encoding reads back to itself; they differ for bytes rskj never writes. Signing. The signing hash is getEncodedRaw, from those fields and the chain v names. extractChainIdFromV divides in Java ints, so v of 34, 35 and 36 name no chain, as 27 and 28 do; rustock refused all three. getRealV is 28 for an even v and 27 for an odd one. Accepting. Every transaction of a block but REMASC passes acceptTransactionSignature (BlockTxsFieldsValidationRule): r from 1 to below the curve order, s from 1 to below half of it, and a chain of 0 or the network's. rustock took a high-s signature, so a block carrying one was valid here and invalid on the network. REMASC. One predicate, Transaction::is_remasc, is rskj's isRemascTransaction at all six places that ask: the block's last transaction, to REMASC, with no data and no signature, and value, gas price and gas limit zero. rustock took a REMASC-shaped transaction anywhere in the block, and with value, data or a gas price, as REMASC. Its sender is the zero address and is never cached: the sender cache keeps only signed transactions, whose hash decides their sender, as sender_cache's docs now argue. The pool keys a transaction by rskj's hash and removes a mined one by it. It removed by a canonical re-encoding, which missed every mined transaction whose rskj encoding isn't canonical. Its REMASC step, which could never fire, is gone. A transaction submitted to a node with no pool is read, to report rskj's hash; the relay keeps keying what it has seen by the digest of the bytes. Transaction::gas reads a gas limit as GasCost.toGas, for the executor to take up next. Tests read what rskj refuses and what it keeps, signatures as rskj takes them, every one-byte v, a wrapped transaction, toGas and isRemascTransaction. The pool takes a gas price sent with a leading zero as the same transaction as the minimal one. Tests that built bytes rskj can't read (an empty gas limit, empty r and s, junk) now build what rskj's own builder writes, and the REMASC wire fixtures use Transaction::remasc: their v, r and s of 0x00 are a signature to rskj, not REMASC's none. Dropping the low-s rule, REMASC's position, the rule that only signed senders are cached, or the re-encoding, or keying the pool by the bytes as sent, each fails a test. --- crates/cli/examples/validation_cost.rs | 35 +- crates/core/src/rskj_rlp.rs | 9 + crates/core/src/types/transaction.rs | 992 ++++++++++++++++++---- crates/core/src/validation/block_rules.rs | 48 +- crates/execution/src/executor.rs | 77 +- crates/execution/src/mining/template.rs | 9 +- crates/execution/src/mining/tests.rs | 27 +- crates/execution/src/processor.rs | 193 ++--- crates/execution/src/sender_cache.rs | 35 +- crates/networking/src/protocol/rsk.rs | 21 +- crates/networking/src/protocol/snap.rs | 23 +- crates/rpc/src/tests.rs | 54 +- crates/storage/src/lib.rs | 41 +- crates/sync/src/gas_price.rs | 8 +- crates/sync/src/tests.rs | 29 +- crates/sync/src/tx_relay.rs | 29 +- crates/sync/src/txpool.rs | 213 +++-- 17 files changed, 1231 insertions(+), 612 deletions(-) diff --git a/crates/cli/examples/validation_cost.rs b/crates/cli/examples/validation_cost.rs index 2adafca..34b75a0 100644 --- a/crates/cli/examples/validation_cost.rs +++ b/crates/cli/examples/validation_cost.rs @@ -53,29 +53,20 @@ fn stats(mut us: Vec) -> (f64, f64, f64, f64) { /// /// Deliberately the same shape as the `k256` version in /// `crates/core/src/types/transaction.rs`, down to the keccak of the -/// uncompressed key's tail, so the two are timed doing the same job. +/// uncompressed key's tail, so the two are timed doing the same job. Only +/// the recovery: every transaction timed has passed the network's rules for +/// a signature in `recover_sender` first. fn recover_with_libsecp( secp: &bitcoin::secp256k1::Secp256k1, tx: &Transaction, - chain_id: u64, ) -> Option { use bitcoin::secp256k1::ecdsa::{RecoverableSignature, RecoveryId}; use bitcoin::secp256k1::Message; use sha3::{Digest, Keccak256}; - let hash = tx.signing_hash(chain_id); - let recovery_id = if tx.is_eip155(chain_id) { - let rid = tx.v as i64 - chain_id as i64 * 2 - 35; - if !(0..=1).contains(&rid) { - return None; - } - rid as i32 - } else { - if tx.v != 27 && tx.v != 28 { - return None; - } - (tx.v - 27) as i32 - }; + let hash = tx.signing_hash(); + let (_, recovery_id) = tx.chain_and_recovery_id()?; + let recovery_id = i32::from(recovery_id); let mut sig_bytes = [0u8; 64]; sig_bytes[..32].copy_from_slice(&tx.r.to_be_bytes::<32>()); @@ -163,8 +154,6 @@ fn main() -> Result<()> { } } - // Same care: `signing_hash` can go through `cached_rlp`. - // // Failures are timed separately rather than mixed in. A transaction // whose `v` is not a valid recovery id -- REMASC's, among others -- // bails before touching the curve, in well under a microsecond, and @@ -174,10 +163,8 @@ fn main() -> Result<()> { let mut recover_us = Vec::with_capacity(txs.len()); let mut rejected_us = Vec::new(); for tx in &txs { - let mut t = tx.clone(); - t.cached_rlp = None; let at = Instant::now(); - let ok = t.recover_sender(MAINNET_CHAIN_ID).is_ok(); + let ok = tx.recover_sender(MAINNET_CHAIN_ID).is_ok(); let us = at.elapsed().as_secs_f64() * 1e6; if ok { recover_us.push(us) } else { rejected_us.push(us) } } @@ -192,11 +179,11 @@ fn main() -> Result<()> { let mut agreed = 0usize; let mut disagreed = 0usize; for tx in &txs { - let mut t = tx.clone(); - t.cached_rlp = None; - let Ok(expected) = t.recover_sender(MAINNET_CHAIN_ID) else { continue }; + let Ok(expected) = tx.recover_sender(MAINNET_CHAIN_ID) else { + continue; + }; let at = Instant::now(); - let got = recover_with_libsecp(&secp, &t, MAINNET_CHAIN_ID); + let got = recover_with_libsecp(&secp, tx); secp_us.push(at.elapsed().as_secs_f64() * 1e6); match got { Some(a) if a == expected => agreed += 1, diff --git a/crates/core/src/rskj_rlp.rs b/crates/core/src/rskj_rlp.rs index 86c2d6e..a56a2a6 100644 --- a/crates/core/src/rskj_rlp.rs +++ b/crates/core/src/rskj_rlp.rs @@ -56,6 +56,15 @@ pub fn decode2(data: &[u8]) -> Result>, RlpError> { Ok(elements) } +/// The first element `RLP.decode2` reads from `data`, and where the next one +/// starts. +pub fn first_element(data: &[u8]) -> Result<(Element<'_>, usize), RlpError> { + if data.is_empty() { + return Err(RlpError); + } + decode_element(data, 0) +} + /// `RLP.decodeElement`, with its lengths in Java `int`s. fn decode_element(data: &[u8], position: usize) -> Result<(Element<'_>, usize), RlpError> { let b0 = data[position]; diff --git a/crates/core/src/types/transaction.rs b/crates/core/src/types/transaction.rs index 8bc066e..3effccf 100644 --- a/crates/core/src/types/transaction.rs +++ b/crates/core/src/types/transaction.rs @@ -3,8 +3,6 @@ use alloy_rlp::{Decodable, Encodable, RlpEncodable}; use serde::{Deserialize, Serialize}; use sha3::{Digest, Keccak256}; -use crate::rlp_compat::{decode_u64_lenient, decode_u256_lenient}; - #[derive(Clone, Debug, Serialize, Deserialize, RlpEncodable)] pub struct Transaction { pub nonce: u64, @@ -17,9 +15,11 @@ pub struct Transaction { pub r: U256, pub s: U256, - /// Original RLP bytes received from the peer. Java's RLP encoding differs - /// from Rust's canonical encoding (leading-zero BigIntegers), so we cache - /// the original bytes and use them for transactions_root computation. + /// The encoding rskj hashes a decoded transaction by and serves it in + /// (`Transaction.getEncoded`), which rskj rebuilds from the fields as it + /// holds them rather than keeping the bytes that came: see + /// [`Transaction::from_rskj`]. A transaction built here has none, and + /// encodes canonically, as rskj would. #[serde(skip)] #[rlp(skip)] pub cached_rlp: Option>, @@ -59,37 +59,28 @@ impl Default for Transaction { } impl Decodable for Transaction { + /// The next transaction of a stream, as rskj reads a block's + /// (`BlockFactory.parseTxs`): the next RLP element as `RLP.decode2` cuts + /// it, whose `getRLPData` rskj hands to `new ImmutableTransaction(..)`. + /// That is a list's whole encoding but an item's payload, so a + /// transaction may come wrapped in a byte string; an empty item, rskj's + /// null, is refused. fn decode(buf: &mut &[u8]) -> alloy_rlp::Result { - let original = *buf; - let h = alloy_rlp::Header::decode(buf)?; - if !h.list { - return Err(alloy_rlp::Error::UnexpectedString); - } - let mut body = &buf[..h.payload_length]; - *buf = &buf[h.payload_length..]; - - let consumed = original.len() - buf.len(); - let cached = original[..consumed].to_vec(); - - Ok(Self { - nonce: decode_u64_lenient(&mut body)?, - gas_price: decode_u256_lenient(&mut body)?, - gas_limit: decode_u256_lenient(&mut body)?, - to: Bytes::decode(&mut body)?, - value: decode_u256_lenient(&mut body)?, - input: Bytes::decode(&mut body)?, - v: decode_u64_lenient(&mut body)?, - r: decode_u256_lenient(&mut body)?, - s: decode_u256_lenient(&mut body)?, - cached_rlp: Some(cached), - }) + let (element, next) = crate::rskj_rlp::first_element(buf) + .map_err(|_| alloy_rlp::Error::Custom("malformed transaction RLP"))?; + let raw = element + .data() + .ok_or(alloy_rlp::Error::Custom("an empty transaction"))?; + let tx = Self::from_rskj(raw)?; + *buf = &buf[next..]; + Ok(tx) } } impl Transaction { - /// Returns the RLP encoding to use for trie root computation. - /// Prefers the original bytes (from peer) to match Java's encoding; - /// falls back to re-encoding if no cached bytes are available. + /// The encoding the transactions root takes, and the hash: rskj's + /// (`getEncoded`) for a decoded transaction, and the canonical one, which + /// rskj's agrees with, for one built here. pub fn rlp_for_trie(&self) -> Vec { if let Some(ref cached) = self.cached_rlp { return cached.clone(); @@ -98,77 +89,188 @@ impl Transaction { self.encode(&mut buf); buf } -} -impl Transaction { - /// Compute the signing hash for this transaction, dispatching based on `v`. + /// A transaction as rskj reads `raw` (`Transaction(byte[] rawData)`), with + /// the encoding rskj hashes it by. + /// + /// rskj reads `raw` as exactly one RLP list (`RLP.decodeList`) of exactly + /// nine elements, each by `getRLPData`. Nonce, gas limit and data keep + /// their bytes; gas price and value are unsigned numbers, the gas price + /// remembering whether it came empty; `to` is empty or 20 bytes; and `v` + /// is absent, for no signature, when r and s go unread, or one byte, when + /// r and s must be there. + /// + /// Refused here as well is what rskj reads but no block can carry, since + /// every transaction of a block passes `BlockTxsFieldsValidationRule`, and + /// no pool takes either: + /// - what `validate()` refuses: a nonce or gas limit of more than 32 + /// bytes, a gas price or value of 2^255 or more (`BigInteger + /// .toByteArray` in 32 bytes), and r or s of more than 32 significant + /// bytes; and an empty gas limit, on which it throws; + /// - a `v` that names a negative chain, below 34 but for 27 and 28, whose + /// signature `acceptTransactionSignature` never takes; + /// - a nonce past 64 bits, which no account reaches. /// - /// EIP-155 transactions include `[chainId, 0, 0]` in the RLP; legacy ones do not. - pub fn signing_hash(&self, chain_id: u64) -> B256 { - if let Some(hash) = self.signing_hash_from_cached(chain_id) { - return hash; + /// So a `v` here is never 0, which keeps meaning no signature. + /// + /// rskj hashes a transaction by an encoding it rebuilds from those fields + /// (`getEncoded`) rather than by `raw`, and that is the one kept: a nonce + /// of the one byte 0 goes empty and any other stays as it came; the gas + /// price stays empty if it came empty, and is otherwise minimal, or 0x00 + /// for zero; the gas limit and data stay as they came; the value is + /// minimal, and empty for zero; `v` is rebuilt from the chain and + /// recovery id it names; r and s are minimal; and every length prefix is + /// canonical. The transaction's `v` is the one in that encoding, which + /// names the same chain and recovery id. + pub fn from_rskj(raw: &[u8]) -> alloy_rlp::Result { + use crate::rskj_rlp::Element; + let refuse = alloy_rlp::Error::Custom; + let elements = crate::rskj_rlp::decode2(raw).map_err(|_| refuse("malformed RLP"))?; + let [list] = elements.as_slice() else { + return Err(refuse("Expected one RLP item")); + }; + let fields = list + .elements() + .map_err(|_| refuse("The decoded element wasn't a list"))?; + let [nonce, gas_price, gas_limit, to, value, input, v, r, s] = fields.as_slice() else { + return Err(refuse("A transaction must have exactly 9 elements")); + }; + let nonce_bytes = nonce.data().unwrap_or_default(); + if nonce_bytes.len() > 32 { + return Err(refuse("Nonce is not valid")); } - if self.is_eip155(chain_id) { - self.signing_hash_eip155(chain_id) + let nonce = unsigned(nonce_bytes) + .and_then(|nonce| u64::try_from(nonce).ok()) + .ok_or(refuse("a nonce past 64 bits"))?; + let gas_limit_bytes = match gas_limit.data() { + Some(bytes) if bytes.len() <= 32 => bytes, + _ => return Err(refuse("Gas Limit is not valid")), + }; + let coin = |element: &Element, why| match element.data() { + None => Ok(U256::ZERO), + Some(bytes) => unsigned(bytes) + .filter(|coin| coin.bit_len() < 256) + .ok_or(refuse(why)), + }; + let gas_price_came_empty = gas_price.data().is_none(); + let gas_price = coin(gas_price, "Gas Price is not valid")?; + let value = coin(value, "Value is not valid")?; + let to = match to.data() { + None => Bytes::new(), + Some(address) if address.len() == 20 => Bytes::copy_from_slice(address), + Some(_) => return Err(refuse("An RSK address must be 20 bytes long")), + }; + let input = input.data().unwrap_or_default(); + let component = + |element: &Element, why| element.data().and_then(unsigned).ok_or(refuse(why)); + let (v, r, s) = match v.data() { + None => (0, U256::ZERO, U256::ZERO), + Some(&[v]) => { + let (chain, recovery_id) = + read_v(v).ok_or(refuse("a v that names a negative chain"))?; + ( + encoded_v(chain, recovery_id), + component(r, "Signature R is not valid")?, + component(s, "Signature S is not valid")?, + ) + } + Some(_) => return Err(refuse("Signature V is invalid")), + }; + + // `getEncoded`: `Transaction.encode`, field by field. + let mut payload = Vec::with_capacity(raw.len()); + let nonce_kept: &[u8] = if nonce_bytes == [0] { &[] } else { nonce_bytes }; + nonce_kept.encode(&mut payload); + if gas_price_came_empty { + payload.push(alloy_rlp::EMPTY_STRING_CODE); } else { - self.signing_hash_legacy() + unsigned_bytes(gas_price).as_slice().encode(&mut payload); } - } - - /// Signing hash built from the ORIGINAL (network) encoding. rskj signs - /// over the raw decoded byte arrays (Transaction.getEncodedRaw), which - /// may use non-canonical zero encodings — e.g. a literal 0x00 byte for a - /// zero gasLimit. Re-encoding the parsed fields canonically changes the - /// hash and recovers the wrong sender, so the first six item encodings - /// are reused verbatim. - fn signing_hash_from_cached(&self, chain_id: u64) -> Option { - let cached = self.cached_rlp.as_ref()?; - let mut buf = cached.as_slice(); - let header = alloy_rlp::Header::decode(&mut buf).ok()?; - if !header.list || buf.len() < header.payload_length { - return None; + gas_limit_bytes.encode(&mut payload); + to.as_ref().encode(&mut payload); + value + .to_be_bytes_trimmed_vec() + .as_slice() + .encode(&mut payload); + input.encode(&mut payload); + if v == 0 { + payload.extend_from_slice(&[alloy_rlp::EMPTY_STRING_CODE; 3]); + } else { + v.encode(&mut payload); + unsigned_bytes(r).as_slice().encode(&mut payload); + unsigned_bytes(s).as_slice().encode(&mut payload); } - let mut rest = &buf[..header.payload_length]; - let mut inner = Vec::with_capacity(header.payload_length + 8); - for _ in 0..6 { - let item = take_rlp_item(&mut rest)?; - inner.extend_from_slice(item); + let mut encoding = Vec::with_capacity(payload.len() + 9); + alloy_rlp::Header { + list: true, + payload_length: payload.len(), } - if self.is_eip155(chain_id) { - chain_id.encode(&mut inner); - 0u8.encode(&mut inner); - 0u8.encode(&mut inner); + .encode(&mut encoding); + encoding.extend_from_slice(&payload); + + Ok(Self { + nonce, + gas_price, + gas_limit: U256::from_be_slice(gas_limit_bytes), + to, + value, + input: Bytes::copy_from_slice(input), + v, + r, + s, + cached_rlp: Some(encoding), + }) + } +} + +impl Transaction { + /// What this transaction's signature signs, rskj's `getRawHash`: by the + /// chain its `v` names, or by none. + pub fn signing_hash(&self) -> B256 { + match self.chain_and_recovery_id() { + Some((chain, _)) if chain != 0 => self.signing_hash_eip155(u64::from(chain)), + _ => self.signing_hash_legacy(), } - Some(Self::rlp_wrap_and_hash(&inner)) } - /// EIP-155 signing hash: `keccak256(RLP([nonce, gasPrice, gasLimit, to, value, data, chainId, 0, 0]))`. + /// What a signature for the chain `chain_id` signs, rskj's + /// `getEncodedRaw` hashed: the six fields as rskj writes them, then the + /// chain id (`RLP.encodeByte`) and two empty items. pub fn signing_hash_eip155(&self, chain_id: u64) -> B256 { - let mut inner = Vec::with_capacity(256); - self.nonce.encode(&mut inner); - self.gas_price.encode(&mut inner); - self.gas_limit.encode(&mut inner); - self.to.encode(&mut inner); - self.value.encode(&mut inner); - self.input.encode(&mut inner); + let mut inner = self.signed_fields(); chain_id.encode(&mut inner); 0u8.encode(&mut inner); 0u8.encode(&mut inner); - Self::rlp_wrap_and_hash(&inner) } - /// Legacy (pre-EIP-155) signing hash: `keccak256(RLP([nonce, gasPrice, gasLimit, to, value, data]))`. + /// What a signature for no chain in particular signs, rskj's + /// `getEncodedRaw` hashed at chain 0: the six fields as rskj writes them. pub fn signing_hash_legacy(&self) -> B256 { - let mut inner = Vec::with_capacity(256); + Self::rlp_wrap_and_hash(&self.signed_fields()) + } + + /// The six fields a signature covers, each the RLP item rskj writes for it + /// (`Transaction.encode`): `cached_rlp`'s, for a transaction read from + /// bytes, and otherwise the fields' own, which are rskj's for fields that + /// came canonical. A zero gas limit of the one byte 0, which canonical RLP + /// writes empty, is why: mainnet's early bridge transactions carry one. + fn signed_fields(&self) -> Vec { + if let Some(fields) = self + .cached_rlp + .as_deref() + .and_then(|rlp| first_items(rlp, 6)) + { + return fields.to_vec(); + } + let mut inner = Vec::with_capacity(128 + self.input.len()); self.nonce.encode(&mut inner); self.gas_price.encode(&mut inner); self.gas_limit.encode(&mut inner); self.to.encode(&mut inner); self.value.encode(&mut inner); self.input.encode(&mut inner); - - Self::rlp_wrap_and_hash(&inner) + inner } fn rlp_wrap_and_hash(inner: &[u8]) -> B256 { @@ -177,6 +279,56 @@ impl Transaction { buf.extend_from_slice(inner); B256::from_slice(&Keccak256::digest(&buf)) } + + /// The chain this transaction's signature is for, 0 for none, and its + /// recovery id, as rskj reads them from `v` ([`read_v`]). `None` for no + /// signature, and for a `v` rskj takes no signature by: past a byte, or + /// naming a negative chain. + pub fn chain_and_recovery_id(&self) -> Option<(u8, u8)> { + u8::try_from(self.v).ok().and_then(read_v) + } + + /// The gas this transaction gives, as rskj reads its gas limit's bytes + /// (`GasCost.toGas`): more than eight bytes, leading zeros or not, read + /// as `Long.MAX_VALUE`, which no block's gas fits + /// (`TransactionExecutor.gasIsValid`). `None` for eight bytes with the + /// top bit set, a negative `long`, on which rskj throws. + pub fn gas(&self) -> Option { + let bytes = self.gas_limit_bytes(); + if bytes.len() > 8 { + return Some(i64::MAX as u64); + } + let gas = bytes + .iter() + .fold(0u64, |gas, byte| gas << 8 | u64::from(*byte)); + i64::try_from(gas).is_ok().then_some(gas) + } + + /// The gas limit's bytes as rskj holds them (`getGasLimit`): as they came, + /// leading zeros and all, for a transaction read from bytes. + fn gas_limit_bytes(&self) -> Vec { + self.cached_rlp + .as_deref() + .and_then(|rlp| list_item(rlp, 2)) + .map_or_else(|| self.gas_limit.to_be_bytes_trimmed_vec(), <[u8]>::to_vec) + } + + /// Whether this is the REMASC transaction of a block of `count` + /// transactions, at `index` (rskj: `Transaction.isRemascTransaction`): the + /// last, to REMASC, with no data and no signature, and value, gas limit + /// and gas price zero. Its gas limit has bytes, zero or not: rskj reads it + /// by `new BigInteger(1, getGasLimit())`, which throws on none, and the + /// block with it (`BlockFactory.parseTxs`). + pub fn is_remasc(&self, index: usize, count: usize) -> bool { + index + 1 == count + && self.to.as_ref() == crate::validation::block_rules::REMASC_ADDR + && self.input.is_empty() + && self.v == 0 + && self.value.is_zero() + && self.gas_price.is_zero() + && self.gas_limit.is_zero() + && !self.gas_limit_bytes().is_empty() + } } /// Advance past one RLP item, returning its full encoding (header + payload). @@ -193,6 +345,87 @@ fn take_rlp_item<'a>(buf: &mut &'a [u8]) -> Option<&'a [u8]> { Some(item) } +/// The payload of the RLP list `encoding`. +fn list_payload(encoding: &[u8]) -> Option<&[u8]> { + let mut buf = encoding; + let header = alloy_rlp::Header::decode(&mut buf).ok()?; + if !header.list { + return None; + } + buf.get(..header.payload_length) +} + +/// The first `count` items of the RLP list `encoding`, prefixes and all. +fn first_items(encoding: &[u8], count: usize) -> Option<&[u8]> { + let payload = list_payload(encoding)?; + let mut rest = payload; + for _ in 0..count { + take_rlp_item(&mut rest)?; + } + Some(&payload[..payload.len() - rest.len()]) +} + +/// The payload of item `index` of the RLP list `encoding`. +fn list_item(encoding: &[u8], index: usize) -> Option<&[u8]> { + let mut rest = list_payload(encoding)?; + for _ in 0..index { + take_rlp_item(&mut rest)?; + } + let mut item = take_rlp_item(&mut rest)?; + let header = alloy_rlp::Header::decode(&mut item).ok()?; + if header.list { + return None; + } + item.get(..header.payload_length) +} + +/// A number from big-endian bytes, as Java's `new BigInteger(1, bytes)` reads +/// them, where it fits 256 bits. +fn unsigned(bytes: &[u8]) -> Option { + let first = bytes + .iter() + .position(|byte| *byte != 0) + .unwrap_or(bytes.len()); + let significant = &bytes[first..]; + (significant.len() <= 32).then(|| U256::from_be_slice(significant)) +} + +/// Bouncy Castle's `BigIntegers.asUnsignedByteArray`: a number's minimal +/// big-endian bytes, and the one byte 0 for zero. +fn unsigned_bytes(value: U256) -> Vec { + if value.is_zero() { + vec![0] + } else { + value.to_be_bytes_trimmed_vec() + } +} + +/// What rskj reads from a signature's `v`: the chain the signature is for +/// (`extractChainIdFromV`), 0 for none, which every network takes; and the +/// recovery id, `getRealV` less 27. 27 and 28 name no chain; otherwise the +/// chain is `(v - 35) / 2` in Java's `int` division, which truncates toward +/// zero, so 34 to 36 name none either, and the real v is 28 for an even `v` +/// and 27 for an odd one. `None` below 34 but for 27 and 28: a negative +/// chain, which no network is. +fn read_v(v: u8) -> Option<(u8, u8)> { + let recovery_id = u8::from(v.is_multiple_of(2)); + match v { + 27 | 28 | 34..=255 => Some((v.saturating_sub(35) / 2, recovery_id)), + _ => None, + } +} + +/// The `v` rskj writes for a signature (`Transaction.rlpEncode`): its +/// recovery id plus 27, or plus `chain * 2 + 35` for a chain. +fn encoded_v(chain: u8, recovery_id: u8) -> u64 { + let base = if chain == 0 { + 27 + } else { + u64::from(chain) * 2 + 35 + }; + base + u64::from(recovery_id) +} + /// The verification context libsecp256k1 wants, built once per process. /// /// Creating one fills in precomputed multiplication tables, which costs far @@ -326,7 +559,9 @@ pub fn recover_address(hash: &[u8; 32], signature: &[u8; 64], recovery_id: u8) - impl Transaction { - /// Recover the sender address from the transaction signature. + /// The sender of a transaction the network `chain_id` takes, as rskj + /// finds it: refused unless rskj would take the signature there + /// (`acceptTransactionSignature`, see [`Transaction::recover_key`]). /// /// RSK chain IDs: mainnet=30, testnet=31, regtest=33. /// @@ -347,36 +582,37 @@ impl Transaction { }) } - /// The key this transaction's signature recovers, as rskj recovers it. + /// The key this transaction's signature recovers, as rskj recovers it, + /// where the network `chain_id` takes the signature, as rskj's + /// `acceptTransactionSignature` takes it from every transaction of a + /// block but REMASC (`BlockTxsFieldsValidationRule`) and from every one + /// it is sent: a signature for no chain or for `chain_id`, with r from 1 + /// to below the curve's order and s from 1 to below half of it. /// `recover_sender` and `btc_sender_hash160` both take it from here, so /// they cannot drift apart on what a signature means. + /// + /// A sender found here depends on nothing but the transaction's hash: the + /// encoding hashed holds the six fields and r and s, and its `v` names + /// the chain and recovery id, the same two for every `v` read here + /// ([`Transaction::from_rskj`]). So a cache of senders by hash is sound. fn recover_key(&self, chain_id: u64) -> anyhow::Result { - let hash = self.signing_hash(chain_id); - - let recovery_id = if self.is_eip155(chain_id) { - // `is_eip155` holds `v` at `chain_id * 2 + 35` or more, so this is - // the id, or past 1. Unsigned: in an `i64`, a `v` of 2^63 or more - // went negative, and the subtraction overflowed. - let rid = self.v - (chain_id * 2 + 35); - if rid > 1 { - anyhow::bail!("invalid EIP-155 v value: {} for chain_id {}", self.v, chain_id); - } - rid as u8 - } else { - if self.v != 27 && self.v != 28 { - anyhow::bail!("invalid legacy v value: {}", self.v); - } - (self.v - 27) as u8 + let Some((chain, recovery_id)) = self.chain_and_recovery_id() else { + anyhow::bail!("no signature rskj takes: v {}", self.v); }; - + if chain != 0 && u64::from(chain) != chain_id { + anyhow::bail!("a signature for chain {chain}, not {chain_id}"); + } + let order = U256::from_be_bytes(secp256k1::constants::CURVE_ORDER); + if self.r.is_zero() || self.r >= order { + anyhow::bail!("signature r out of range"); + } + if self.s.is_zero() || self.s >= order >> 1 { + anyhow::bail!("signature s out of range, or in the curve order's upper half"); + } let mut sig_bytes = [0u8; 64]; sig_bytes[..32].copy_from_slice(&self.r.to_be_bytes::<32>()); sig_bytes[32..].copy_from_slice(&self.s.to_be_bytes::<32>()); - recover(&hash.0, &sig_bytes, recovery_id) - } - - pub fn is_eip155(&self, chain_id: u64) -> bool { - self.v != 27 && self.v != 28 && self.v >= chain_id * 2 + 35 + recover(&self.signing_hash().0, &sig_bytes, recovery_id) } /// Recover the compressed (33-byte) secp256k1 public key from the transaction signature. @@ -433,7 +669,7 @@ mod tests { to: Bytes::from(vec![0x12; 20]), value: U256::from(1_000_000_000_000_000_000u64), input: Bytes::from(vec![0xde, 0xad, 0xbe, 0xef]), - v: 1, + v: 27, r: U256::from(123), s: U256::from(456), cached_rlp: None, @@ -460,17 +696,18 @@ mod tests { s: U256::from(1), cached_rlp: None, }; - assert!(tx.is_eip155(30)); - - let hash1 = tx.signing_hash(30); - let hash2 = tx.signing_hash(30); - assert_eq!(hash1, hash2, "signing hash should be deterministic"); + assert_eq!(tx.chain_and_recovery_id(), Some((30, 0))); + let hash = tx.signing_hash(); + assert_eq!(hash, tx.signing_hash_eip155(30), "v 95 signs for chain 30"); let hash_direct = tx.signing_hash_eip155(31); - assert_ne!(hash1, hash_direct, "different chain_id should produce different hash"); + assert_ne!( + hash, hash_direct, + "different chain_id should produce different hash" + ); let legacy = tx.signing_hash_legacy(); - assert_ne!(hash1, legacy, "EIP-155 hash differs from legacy hash"); + assert_ne!(hash, legacy, "EIP-155 hash differs from legacy hash"); } #[test] @@ -488,25 +725,58 @@ mod tests { cached_rlp: None, }; - let hash = tx.signing_hash(30); - assert_ne!(hash, B256::ZERO); + assert_eq!( + tx.signing_hash(), + tx.signing_hash_legacy(), + "v 27 signs for no chain" + ); } - /// Two transactions that differ only in `v` must hash differently. - /// - /// The sender cache in `rustock_execution::sender_cache` is keyed by - /// `tx_hash()`, and `v` selects both the signing hash (legacy or - /// EIP-155) and the recovery id -- so two wire forms that hashed alike - /// but recovered differently would let whichever was seen first decide - /// the sender of the second. + /// A transaction's hash is rskj's (`getHash`, of `getEncoded`), whose `v` + /// rskj rebuilds from the chain and recovery id it reads there. So every + /// `v` that names the same two hashes alike, as 35 does with 27, and any + /// two that name different ones hash apart. /// - /// Two things keep them apart, and this checks both. `v` is stored - /// verbatim and re-encoded verbatim, so nothing reconstructs it from a - /// chain id and folds two values onto one; and `tx_hash` prefers - /// `cached_rlp`, the bytes as received, which differ anyway. + /// The sender cache in `rustock_execution::sender_cache` is keyed by this + /// hash: a hash that came with two readings of `v` would let whichever + /// was seen first decide the sender of the other. #[test] - fn the_hash_separates_transactions_that_differ_only_in_v() { - let base = Transaction { + fn the_hash_follows_what_v_names() { + let mut readings = std::collections::HashMap::new(); + for v in 0..=255u8 { + let mut payload = vec![0x01, 0x01, 0x82, 0x52, 0x08, 0x94]; + payload.extend_from_slice(&[0x11; 20]); + payload.extend_from_slice(&[0x82, 0x03, 0xe8, 0x80]); + // As a one-byte string, so that 0 is the byte 00, not empty. + [v].as_slice().encode(&mut payload); + payload.extend_from_slice(&[0x82, 0x12, 0x34, 0x82, 0x56, 0x78]); + let mut raw = Vec::new(); + alloy_rlp::Header { + list: true, + payload_length: payload.len(), + } + .encode(&mut raw); + raw.extend_from_slice(&payload); + + let read = Transaction::decode(&mut raw.as_slice()); + let named = read_v(v); + assert_eq!(read.is_ok(), named.is_some(), "v {v}"); + let Ok(tx) = read else { continue }; + assert_eq!( + tx.chain_and_recovery_id(), + named, + "v {v} keeps what it names" + ); + if let Some(other) = readings.insert(tx.tx_hash(), named) { + assert_eq!(other, named, "v {v} hashes as a v that names otherwise"); + } + } + // 27 and 28, and 34 to 36, name no chain; 37 to 255 one each, two + // recovery ids apiece. + assert_eq!(readings.len(), 2 + 219); + + // A transaction built here hashes as rskj would its encoding. + let built = Transaction { nonce: 1, gas_price: U256::from(1u64), gas_limit: U256::from(21_000u64), @@ -518,27 +788,14 @@ mod tests { s: U256::from(0x5678u64), cached_rlp: None, }; - - // Every `v` a legacy or near-legacy wire form could carry, including - // the values either side of the canonical pair. - let mut seen = std::collections::HashMap::new(); - for v in [0u64, 26, 27, 28, 29, 35, 36, 37, 38] { - let tx = Transaction { v, ..base.clone() }; - if let Some(other) = seen.insert(tx.tx_hash(), v) { - panic!("v={v} and v={other} produce the same transaction hash"); - } - } - - // And again through a real decode, so the cached-bytes path is - // covered rather than only the re-encoding one. let mut encoded = Vec::new(); - base.encode(&mut encoded); + built.encode(&mut encoded); let decoded = Transaction::decode(&mut encoded.as_slice()).unwrap(); - assert!(decoded.cached_rlp.is_some(), "decode must record the wire bytes"); - assert_eq!(decoded.v, 28, "v must survive the round trip unchanged"); + assert_eq!(decoded.cached_rlp.as_deref(), Some(encoded.as_slice())); + assert_eq!(decoded, built); assert_eq!( decoded.tx_hash(), - base.tx_hash(), + built.tx_hash(), "the same bytes must hash the same whether decoded or built" ); } @@ -575,7 +832,7 @@ mod tests { tx.s = U256::from_be_slice(&sig_bytes[32..]); tx.v = chain_id * 2 + 35 + recid.to_byte() as u64; - // Now recover_sender uses signing_hash(chain_id) which checks v → EIP-155 + // v names chain 33, so recover_sender checks the signature for it. let recovered = tx.recover_sender(chain_id).unwrap(); let vk = signing_key.verifying_key(); @@ -587,10 +844,10 @@ mod tests { assert_eq!(recovered, expected_addr, "recovered sender should match signing key"); } - /// Regression for mainnet block #457: rskj signs over the ORIGINAL field - /// byte arrays, which may use non-canonical zero encodings (the free - /// bridge txs encode gasLimit as a literal 0x00 byte). The signing hash - /// must reuse the original encoding, not a canonical re-encoding. + /// Regression for mainnet block #457: rskj signs over the fields as it + /// holds them, and keeps a gas limit's bytes as they came, so the free + /// bridge transactions' zero gas limit, the one byte 0, is signed as that + /// byte, where canonical RLP writes zero empty. #[test] fn test_recover_sender_with_noncanonical_zero_gas_limit() { use k256::ecdsa::SigningKey; @@ -688,9 +945,11 @@ mod tests { assert_eq!(INFINITY_SENDER.as_slice(), &Keccak256::digest(b"")[12..]); } - /// A signature made so that sR = eG, with R the generator and s the - /// signing hash, recovers the point at infinity whatever the transaction; - /// the same r with y's other parity recovers an ordinary key. + /// A signature made so that sR = eG recovers the point at infinity + /// whatever the transaction: R the generator, whose y is even, and s the + /// signing hash e; or, where that s would be in the upper half rskj + /// refuses, R the generator's negation, whose y is odd, and s the order + /// less e. The same r with y's other parity recovers an ordinary key. #[test] fn a_signature_made_so_that_sr_is_eg_recovers_the_infinity_sender() { let mut tx = Transaction { @@ -706,10 +965,16 @@ mod tests { cached_rlp: None, }; let order = U256::from_be_bytes(secp256k1::constants::CURVE_ORDER); - tx.s = U256::from_be_bytes(tx.signing_hash_eip155(33).0) % order; + let e = U256::from_be_bytes(tx.signing_hash_eip155(33).0) % order; + if e < order >> 1 { + tx.s = e; + } else { + tx.v += 1; + tx.s = order - e; + } assert_eq!(tx.recover_sender(33).unwrap(), INFINITY_SENDER); - tx.v += 1; + tx.v = if tx.v == 101 { 102 } else { 101 }; let other = tx.recover_sender(33).unwrap(); assert_ne!(other, INFINITY_SENDER); assert!(tx.recover_compressed_pubkey(33).is_some()); @@ -971,9 +1236,11 @@ mod untrusted_input_tests { let _ = tx.rlp_for_trie(); } } - // None of these is a recovery id on any network; 2^63 and up once - // overflowed the sum that finds it. - for v in [0, 26, 29, 34, 94, 99, 100, 103, 1 << 31, 1 << 63, u64::MAX] { + // No network takes a signature by any of these: rskj reads no chain + // from 0, 26, 29 or past a byte (2^63 and up once overflowed the sum + // that found the recovery id), and 94, 99, 100 and 103 name chains + // 29, 32, 32 and 34. + for v in [0, 26, 29, 94, 99, 100, 103, 1 << 31, 1 << 63, u64::MAX] { let tx = Transaction { v, r: U256::from(1u64), @@ -989,3 +1256,418 @@ mod untrusted_input_tests { } } } + +#[cfg(test)] +mod rskj_reading_tests { + use super::*; + use crate::validation::block_rules::REMASC_ADDR; + use alloy_primitives::U256; + use alloy_rlp::Decodable; + + /// A list of items, each already its RLP. + fn list(items: &[Vec]) -> Vec { + let payload = items.concat(); + let mut raw = Vec::new(); + alloy_rlp::Header { + list: true, + payload_length: payload.len(), + } + .encode(&mut raw); + raw.extend_from_slice(&payload); + raw + } + + /// A byte string's canonical RLP. + fn item(bytes: &[u8]) -> Vec { + let mut out = Vec::new(); + bytes.encode(&mut out); + out + } + + /// A transfer's nine items as canonical RLP writes them, signed with v 27. + fn fields() -> Vec> { + vec![ + item(&[0x05]), + item(&[0x01]), + item(&[0x52, 0x08]), + item(&[0x11; 20]), + item(&[0x03, 0xe8]), + item(&[]), + item(&[27]), + item(&[0x12, 0x34]), + item(&[0x56, 0x78]), + ] + } + + fn with(at: usize, field: Vec) -> Vec> { + let mut fields = fields(); + fields[at] = field; + fields + } + + fn read(fields: &[Vec]) -> alloy_rlp::Result { + Transaction::decode(&mut list(fields).as_slice()) + } + + /// What rskj's `Transaction(byte[])` throws on, and what `validate()` + /// refuses in every block, is refused when read. + #[test] + fn what_rskj_cannot_take_is_refused_when_read() { + assert!(read(&fields()).is_ok()); + let mut eight = fields(); + eight.pop(); + assert!(read(&eight).is_err(), "eight fields"); + let mut ten = fields(); + ten.push(item(&[])); + assert!(read(&ten).is_err(), "ten fields"); + assert!(read(&with(3, item(&[0x11; 19]))).is_err(), "a 19-byte to"); + assert!(read(&with(3, item(&[0x11; 21]))).is_err(), "a 21-byte to"); + assert!( + read(&with(3, item(&[]))).unwrap().to.is_empty(), + "no to: a creation" + ); + assert!(read(&with(6, item(&[0x00, 27]))).is_err(), "a two-byte v"); + assert!(read(&with(7, item(&[]))).is_err(), "a v with no r"); + assert!(read(&with(8, item(&[]))).is_err(), "a v with no s"); + // No signature: r and s go unread. + let mut unsigned = with(6, item(&[])); + unsigned[7] = item(&[0xff; 40]); + let unsigned = read(&unsigned).unwrap(); + assert_eq!( + (unsigned.v, unsigned.r, unsigned.s), + (0, U256::ZERO, U256::ZERO) + ); + + // `validate()`'s limits, and the nonce an account can reach. + assert!( + read(&with(0, item(&[0x01; 33]))).is_err(), + "a 33-byte nonce" + ); + assert!( + read(&with(0, item(&[0x01; 9]))).is_err(), + "a nonce past 64 bits" + ); + assert!( + read(&with(2, item(&[0x00; 33]))).is_err(), + "a 33-byte gas limit" + ); + assert!(read(&with(2, item(&[]))).is_err(), "no gas limit"); + let mut half = [0u8; 32]; + half[0] = 0x80; + let mut below = [0xff; 32]; + below[0] = 0x7f; + for (at, what) in [(1, "gas price"), (4, "value")] { + assert!(read(&with(at, item(&half))).is_err(), "a {what} of 2^255"); + assert!( + read(&with(at, item(&below))).is_ok(), + "a {what} just below 2^255" + ); + } + assert!( + read(&with(7, item(&[0x01; 33]))).is_err(), + "r of 33 significant bytes" + ); + let padded = [&[0u8][..], &[0x01; 32]].concat(); + assert!( + read(&with(7, item(&padded))).is_ok(), + "33 bytes, the first zero" + ); + + // A v that names a negative chain. + for v in [0u8, 1, 26, 29, 32, 33] { + assert!(read(&with(6, item(&[v]))).is_err(), "v {v}"); + } + } + + /// rskj hashes a transaction by an encoding it rebuilds from the fields + /// it holds (`getEncoded`), and that is the one kept. + #[test] + fn the_encoding_kept_is_the_one_rskj_hashes() { + assert_eq!(read(&fields()).unwrap().rlp_for_trie(), list(&fields())); + let cases = [ + ( + 0, + item(&[0x00]), + item(&[]), + "a nonce of the one byte 0 goes empty", + ), + ( + 0, + item(&[0x00, 0x05]), + item(&[0x00, 0x05]), + "any other nonce stays", + ), + (1, item(&[0x00]), item(&[0x00]), "a zero gas price stays 00"), + (1, item(&[]), item(&[]), "an empty gas price stays empty"), + ( + 1, + item(&[0x00, 0x01]), + item(&[0x01]), + "a gas price is minimal", + ), + ( + 2, + item(&[0x00, 0x52, 0x08]), + item(&[0x00, 0x52, 0x08]), + "a gas limit stays", + ), + ( + 2, + item(&[0x00]), + item(&[0x00]), + "a zero gas limit of the one byte 0 stays", + ), + (4, item(&[0x00]), item(&[]), "a zero value goes empty"), + ( + 4, + item(&[0x00, 0x03, 0xe8]), + item(&[0x03, 0xe8]), + "a value is minimal", + ), + ( + 6, + item(&[35]), + item(&[27]), + "v 35 names no chain and recovery id 0", + ), + ( + 6, + item(&[34]), + item(&[28]), + "v 34 names no chain and recovery id 1", + ), + (6, item(&[36]), item(&[28]), "and so does 36"), + ( + 7, + item(&[0x00, 0x12, 0x34]), + item(&[0x12, 0x34]), + "r is minimal", + ), + ( + 8, + vec![0x81, 0x05], + item(&[0x05]), + "a single byte written long", + ), + ( + 5, + vec![0xb8, 0x02, 0xaa, 0xbb], + item(&[0xaa, 0xbb]), + "a long prefix, short data", + ), + ]; + for (at, came, kept, what) in cases { + let tx = read(&with(at, came)).unwrap_or_else(|e| panic!("{what}: {e}")); + assert_eq!(tx.rlp_for_trie(), list(&with(at, kept)), "{what}"); + } + // And the list's own prefix: a long one on a short list. + let payload = fields().concat(); + let long = [&[0xf8, payload.len() as u8][..], &payload].concat(); + let tx = Transaction::decode(&mut long.as_slice()).unwrap(); + assert_eq!(tx.rlp_for_trie(), list(&fields())); + } + + /// rskj reads each of a block's transactions by `getRLPData`, which for a + /// byte string is its payload: a transaction may come wrapped in one. + #[test] + fn a_transaction_may_come_wrapped_in_a_byte_string() { + let raw = list(&fields()); + let stream = [item(&raw), raw].concat(); + let mut buf = stream.as_slice(); + let wrapped = Transaction::decode(&mut buf).unwrap(); + let plain = Transaction::decode(&mut buf).unwrap(); + assert!(buf.is_empty()); + assert_eq!(wrapped.tx_hash(), plain.tx_hash()); + assert!( + Transaction::decode(&mut [0x80].as_slice()).is_err(), + "an empty item" + ); + } + + /// rskj takes a signature (`acceptTransactionSignature`) for no chain or + /// for its own, with r from 1 to below the curve's order and s from 1 to + /// below half of it. + #[test] + fn a_signature_is_taken_as_rskj_takes_it() { + use k256::ecdsa::SigningKey; + let key = SigningKey::from_slice(&[9u8; 32]).unwrap(); + let point = key.verifying_key().to_encoded_point(false); + let sender = Address::from_slice(&Keccak256::digest(&point.as_bytes()[1..])[12..]); + let unsigned = Transaction { + nonce: 5, + gas_price: U256::from(1u64), + gas_limit: U256::from(21_000u64), + to: Bytes::from(vec![0x11; 20]), + value: U256::from(1_000u64), + ..Default::default() + }; + let sign = |hash: B256, base: u64| { + let (signature, id) = key.sign_prehash_recoverable(hash.as_slice()).unwrap(); + let rs = signature.to_bytes(); + Transaction { + v: base + u64::from(id.to_byte()), + r: U256::from_be_slice(&rs[..32]), + s: U256::from_be_slice(&rs[32..]), + ..unsigned.clone() + } + }; + let order = U256::from_be_bytes(secp256k1::constants::CURVE_ORDER); + + // For chain 30: taken there, and only there. + let for_30 = sign(unsigned.signing_hash_eip155(30), 95); + assert_eq!(for_30.recover_sender(30).unwrap(), sender); + assert!(for_30.recover_sender(31).is_err()); + assert!(for_30.recover_sender(33).is_err()); + + // For no chain: taken on every network, by 27 or 28 and by 34 to 36. + let legacy = sign(unsigned.signing_hash_legacy(), 27); + for chain_id in [30, 31, 33] { + assert_eq!(legacy.recover_sender(chain_id).unwrap(), sender); + } + let others: &[u64] = if legacy.v == 27 { &[35] } else { &[34, 36] }; + for &v in others { + let mut raw = Vec::new(); + Transaction { + v, + ..legacy.clone() + } + .encode(&mut raw); + let read = Transaction::decode(&mut raw.as_slice()).unwrap(); + assert_eq!(read.v, legacy.v, "v {v} is written as {}", legacy.v); + assert_eq!(read.tx_hash(), legacy.tx_hash()); + assert_eq!(read.recover_sender(30).unwrap(), sender, "v {v}"); + } + + // The same signature with s in the upper half and the other + // recovery id: refused, though it is the same key's. + let high = Transaction { + v: 55 - legacy.v, + s: order - legacy.s, + ..legacy.clone() + }; + assert!(high.recover_sender(30).is_err(), "s in the upper half"); + let mut signature = [0u8; 64]; + signature[..32].copy_from_slice(&high.r.to_be_bytes::<32>()); + signature[32..].copy_from_slice(&high.s.to_be_bytes::<32>()); + let hash = unsigned.signing_hash_legacy().0; + assert_eq!( + recover_address(&hash, &signature, (high.v - 27) as u8), + Some(sender) + ); + + // Half the order, rounded down, is already too high; r and s of 0, and + // r of the order, are refused too. + let half = Transaction { + s: order >> 1, + ..legacy.clone() + }; + assert!(half.recover_sender(30).is_err()); + for (r, s) in [ + (U256::ZERO, legacy.s), + (order, legacy.s), + (legacy.r, U256::ZERO), + ] { + assert!(Transaction { + r, + s, + ..legacy.clone() + } + .recover_sender(30) + .is_err()); + } + assert!(unsigned.recover_sender(30).is_err(), "no signature"); + } + + /// A transaction's gas is its gas limit's bytes as rskj's `GasCost.toGas` + /// reads them. + #[test] + fn gas_is_read_as_rskjs_to_gas() { + let gas = |bytes: &[u8]| read(&with(2, item(bytes))).unwrap().gas(); + assert_eq!(gas(&[0x52, 0x08]), Some(21_000)); + assert_eq!(gas(&[0x00]), Some(0)); + assert_eq!( + gas(&[0, 0, 0, 0, 0, 0, 0x52, 0x08]), + Some(21_000), + "eight bytes" + ); + let nine = [0, 0, 0, 0, 0, 0, 0, 0x52, 0x08]; + assert_eq!( + gas(&nine), + Some(i64::MAX as u64), + "nine, though the number is small" + ); + let max = [0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]; + assert_eq!(gas(&max), Some(i64::MAX as u64)); + assert_eq!(gas(&[0x80, 0, 0, 0, 0, 0, 0, 0]), None, "a negative long"); + let built = Transaction { + gas_limit: U256::from(21_000u64), + ..Default::default() + }; + assert_eq!(built.gas(), Some(21_000)); + } + + /// rskj's `TransactionIsRemascTest`, on transactions read from bytes: the + /// REMASC transaction is a block's last, to the REMASC address, with no + /// data and no signature, and value, gas price and gas limit zero. + #[test] + fn remasc_is_rskjs_is_remasc_transaction() { + let remasc = |edit: &dyn Fn(&mut Vec>)| { + let mut fields = vec![ + item(&[]), + item(&[0x00]), + item(&[0x00]), + item(&REMASC_ADDR), + item(&[]), + item(&[]), + item(&[]), + item(&[]), + item(&[]), + ]; + edit(&mut fields); + read(&fields).unwrap() + }; + assert!( + remasc(&|_| {}).is_remasc(6, 7), + "validRemascTransactionNullData" + ); + assert!( + !remasc(&|_| {}).is_remasc(3, 7), + "notRemascTransactionNotLastTx" + ); + let data = remasc(&|f| f[5] = item(&[1, 2, 3, 4])); + assert!(!data.is_remasc(6, 7), "notRemascTransactionNotEmptyData"); + let signed = remasc(&|f| { + f[6] = item(&[27]); + f[7] = item(&[1]); + f[8] = item(&[1]); + }); + assert!(!signed.is_remasc(6, 7), "notRemascTransactionNotNullSig"); + let elsewhere = remasc(&|f| f[3] = item(&[0x22; 20])); + assert!( + !elsewhere.is_remasc(6, 7), + "notRemascTransactionReceiverIsNotRemasc" + ); + let value = remasc(&|f| f[4] = item(&[10])); + assert!(!value.is_remasc(6, 7), "notRemascTransactionValueIsNotZero"); + let price = remasc(&|f| f[1] = item(&[10])); + assert!( + !price.is_remasc(6, 7), + "notRemascTransactionGasPriceIsNotZero" + ); + let limit = remasc(&|f| f[2] = item(&[10])); + assert!( + !limit.is_remasc(6, 7), + "notRemascTransactionGasLimitIsNotZero" + ); + + // An empty gas price reads as zero (`getGasPrice`); a gas limit with + // no bytes can't be read at all. + assert!(remasc(&|f| f[1] = item(&[])).is_remasc(6, 7)); + assert!(Transaction::remasc(9).is_remasc(0, 1)); + let built = Transaction { + to: Bytes::copy_from_slice(&REMASC_ADDR), + ..Default::default() + }; + assert!(!built.is_remasc(0, 1), "a gas limit with no bytes"); + } +} diff --git a/crates/core/src/validation/block_rules.rs b/crates/core/src/validation/block_rules.rs index 6d92f8d..810c13c 100644 --- a/crates/core/src/validation/block_rules.rs +++ b/crates/core/src/validation/block_rules.rs @@ -19,16 +19,11 @@ pub(crate) const REMASC_ADDR: [u8; 20] = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01, 0x00, 0x00, 0x08, ]; -/// rskj `RemascTransaction`: `to == REMASC_ADDR`, zero signature, zero gas -/// limit. Same predicate `BlockProcessor::is_remasc_tx` uses to skip sender -/// recovery. -fn is_remasc_tx(tx: &crate::types::transaction::Transaction) -> bool { - tx.v == 0 - && tx.r.is_zero() - && tx.s.is_zero() - && tx.gas_limit.is_zero() - && tx.to.len() == 20 - && tx.to.as_ref() == REMASC_ADDR +/// Whether `block`'s transaction at `index` is its REMASC transaction, which +/// rskj makes a `RemascTransaction` (`Transaction.isRemascTransaction`): only +/// the last can be. +fn is_remasc(block: &Block, index: usize) -> bool { + block.transactions[index].is_remasc(index, block.transactions.len()) } /// rskj `TxsMinGasPriceRule`: every non-REMASC transaction must pay at least @@ -38,8 +33,8 @@ pub struct TxsMinGasPriceRule; impl BlockValidator for TxsMinGasPriceRule { fn validate_block(&self, block: &Block) -> Result<(), ValidationError> { let min = block.header.minimum_gas_price; - for tx in &block.transactions { - if is_remasc_tx(tx) { + for (index, tx) in block.transactions.iter().enumerate() { + if is_remasc(block, index) { continue; } if tx.gas_price < min { @@ -80,8 +75,8 @@ impl BlockValidator for BlockTxsMaxGasPriceRule { return Ok(()); } let cap = min.saturating_mul(U256::from(Self::FOR_BLOCK_MULTIPLIER)); - for tx in &block.transactions { - if is_remasc_tx(tx) { + for (index, tx) in block.transactions.iter().enumerate() { + if is_remasc(block, index) { continue; } if tx.gas_price > cap { @@ -101,8 +96,8 @@ pub struct RemascValidationRule; impl BlockValidator for RemascValidationRule { fn validate_block(&self, block: &Block) -> Result<(), ValidationError> { - match block.transactions.last() { - Some(tx) if is_remasc_tx(tx) => Ok(()), + match block.transactions.len().checked_sub(1) { + Some(last) if is_remasc(block, last) => Ok(()), _ => Err(ValidationError::RemascTxMissing), } } @@ -211,18 +206,7 @@ mod tests { } fn remasc_tx() -> Transaction { - Transaction { - nonce: 0, - gas_price: U256::ZERO, - gas_limit: U256::ZERO, - to: Bytes::copy_from_slice(&REMASC_ADDR), - value: U256::ZERO, - input: Bytes::new(), - v: 0, - r: U256::ZERO, - s: U256::ZERO, - cached_rlp: None, - } + Transaction::remasc(9_000_000) } fn user_tx(gas_price: u64) -> Transaction { @@ -258,9 +242,15 @@ mod tests { Err(ValidationError::TxGasPriceBelowMinimum { .. }) )); - // The REMASC transaction pays nothing and is exempt. + // The REMASC transaction pays nothing and is exempt; one of its shape + // before the last is no REMASC transaction, and is not. let only_remasc = block_at(9_000_000, 100, vec![remasc_tx()]); assert!(TxsMinGasPriceRule.validate_block(&only_remasc).is_ok()); + let early = block_at(9_000_000, 100, vec![remasc_tx(), user_tx(100), remasc_tx()]); + assert!(matches!( + TxsMinGasPriceRule.validate_block(&early), + Err(ValidationError::TxGasPriceBelowMinimum { .. }) + )); } /// rskj `BlockTxsMaxGasPriceRule` + `TxGasPriceCap.FOR_BLOCK` (RSKIP252). diff --git a/crates/execution/src/executor.rs b/crates/execution/src/executor.rs index 643d2c8..65ea806 100644 --- a/crates/execution/src/executor.rs +++ b/crates/execution/src/executor.rs @@ -1499,7 +1499,8 @@ impl RskExecutor { // only when this run's container is the block itself: a sublist // run is measured against the sublist's limit, which the // partition validator enforces separately. - if !Self::is_remasc_tx(tx) && (whole_block || discard_unfitting.is_some()) { + let is_remasc = tx.is_remasc(i, transactions.len()); + if !is_remasc && (whole_block || discard_unfitting.is_some()) { let tx_gas_limit = tx.gas_limit.saturating_to::(); let fits = total_gas.saturating_add(tx_gas_limit) <= block_gas_limit; let sender_dropped = dropped_senders.contains(sender); @@ -1528,7 +1529,7 @@ impl RskExecutor { } } - if Self::is_remasc_tx(tx) { + if is_remasc { debug!(tx_index = i, "executing REMASC system call"); if traced.includes(i) { // REMASC never enters the EVM here, so the tracer records @@ -2120,19 +2121,6 @@ impl RskExecutor { && self.free_bridge_senders.contains(&sender) } - /// Detect the REMASC synthetic transaction: v=0, r=0, s=0, gas_limit=0, - /// to=REMASC_ADDR. The gas_limit=0 check distinguishes real REMASC system - /// calls from normal transactions that happen to call REMASC with valid gas. - fn is_remasc_tx(tx: &rustock_core::Transaction) -> bool { - if tx.v != 0 || !tx.r.is_zero() || !tx.s.is_zero() { - return false; - } - if !tx.gas_limit.is_zero() { - return false; - } - tx.to.len() == 20 && tx.to.as_ref() == REMASC_ADDR.as_slice() - } - /// For contract creation transactions, compute the created address from /// sender + nonce (matching CREATE opcode address derivation). fn extract_created_address( @@ -9232,15 +9220,7 @@ bf09f6e52420834e8e0e0b1a6df563aba550cf7f99e9724264187c45dcf3d73e8585a0e35196\ block_store.put_canonical_hash(1, processing_hash).unwrap(); let header = dummy_header(4_001); - let remasc_tx = rustock_core::Transaction { - nonce: 0, - gas_price: U256::ZERO, - gas_limit: U256::ZERO, - to: Bytes::copy_from_slice(crate::precompiles::REMASC_ADDR.as_slice()), - value: U256::ZERO, - input: Bytes::new(), - v: 0, r: U256::ZERO, s: U256::ZERO, cached_rlp: None, - }; + let remasc_tx = rustock_core::Transaction::remasc(header.number); let executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); let result = executor @@ -9263,15 +9243,7 @@ bf09f6e52420834e8e0e0b1a6df563aba550cf7f99e9724264187c45dcf3d73e8585a0e35196\ // Block #1: REMASC returns early (pre-maturity) but still writes siblings. let header = dummy_header(1); - let remasc_tx = rustock_core::Transaction { - nonce: 0, - gas_price: U256::ZERO, - gas_limit: U256::ZERO, - to: Bytes::copy_from_slice(crate::precompiles::REMASC_ADDR.as_slice()), - value: U256::ZERO, - input: Bytes::new(), - v: 0, r: U256::ZERO, s: U256::ZERO, cached_rlp: None, - }; + let remasc_tx = rustock_core::Transaction::remasc(header.number); let executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); let result = executor @@ -10173,18 +10145,7 @@ bf09f6e52420834e8e0e0b1a6df563aba550cf7f99e9724264187c45dcf3d73e8585a0e35196\ header.beneficiary = Address::repeat_byte(0x99); // actual miner (not used for fee routing) // The REMASC system tx: unsigned, zero gas (rskj RemascTransaction). - let remasc_tx = rustock_core::Transaction { - nonce: 0, - gas_price: U256::from(0), - gas_limit: U256::ZERO, - to: Bytes::copy_from_slice(remasc_addr.as_slice()), - value: U256::ZERO, - input: Bytes::new(), - v: 0, - r: U256::ZERO, - s: U256::ZERO, - cached_rlp: None, - }; + let remasc_tx = rustock_core::Transaction::remasc(header.number); // The rskj remasc tests run regtest with a NON-EMPTY genesis // federation (injected at construction); our regtest constants have @@ -10389,18 +10350,7 @@ bf09f6e52420834e8e0e0b1a6df563aba550cf7f99e9724264187c45dcf3d73e8585a0e35196\ header.beneficiary = Address::repeat_byte(0x99); // The REMASC system tx: unsigned, zero gas (rskj RemascTransaction). - let remasc_tx = rustock_core::Transaction { - nonce: 0, - gas_price: U256::from(0), - gas_limit: U256::ZERO, - to: Bytes::copy_from_slice(remasc_addr.as_slice()), - value: U256::ZERO, - input: Bytes::new(), - v: 0, - r: U256::ZERO, - s: U256::ZERO, - cached_rlp: None, - }; + let remasc_tx = rustock_core::Transaction::remasc(header.number); let executor = RskExecutor::new( RskHardforkConfig::all_active(33), @@ -10725,18 +10675,7 @@ bf09f6e52420834e8e0e0b1a6df563aba550cf7f99e9724264187c45dcf3d73e8585a0e35196\ header.beneficiary = Address::repeat_byte(0x99); // The REMASC system tx: unsigned, zero gas (rskj RemascTransaction). - let remasc_tx = rustock_core::Transaction { - nonce: 0, - gas_price: U256::from(0), - gas_limit: U256::ZERO, - to: Bytes::copy_from_slice(remasc_addr.as_slice()), - value: U256::ZERO, - input: Bytes::new(), - v: 0, - r: U256::ZERO, - s: U256::ZERO, - cached_rlp: None, - }; + let remasc_tx = rustock_core::Transaction::remasc(header.number); let executor = RskExecutor::new( RskHardforkConfig::all_active(33), diff --git a/crates/execution/src/mining/template.rs b/crates/execution/src/mining/template.rs index 20bfaff..ea1fe8e 100644 --- a/crates/execution/src/mining/template.rs +++ b/crates/execution/src/mining/template.rs @@ -461,7 +461,7 @@ impl BlockTemplateBuilder { // REMASC closes the sequential sublist and is not placed by the // ordinary rule -- it is the block's last transaction by // definition. - if index + 1 == kept.len() && is_remasc(tx) { + if tx.is_remasc(index, kept.len()) { partitioner.add_remasc(index, result.gas_used); placed.push(index); continue; @@ -676,13 +676,6 @@ fn account_nonce(state_root: &TrieNode, store: &dyn TrieStore, addr: &Address) - /// transaction would give it a different hash and put a different /// transactions root in the header. rskj `RemascTransaction(long blockNumber)` /// with `Transaction.encode`. -/// Whether `tx` is the REMASC system transaction, by shape. -pub fn is_remasc(tx: &Transaction) -> bool { - tx.to.as_ref() == crate::precompiles::REMASC_ADDR.as_slice() - && tx.r.is_zero() - && tx.s.is_zero() -} - pub fn remasc_transaction(block_number: u64) -> Transaction { let nonce = block_number.saturating_sub(1); let to = Bytes::copy_from_slice(crate::precompiles::REMASC_ADDR.as_slice()); diff --git a/crates/execution/src/mining/tests.rs b/crates/execution/src/mining/tests.rs index 35bec47..1a63218 100644 --- a/crates/execution/src/mining/tests.rs +++ b/crates/execution/src/mining/tests.rs @@ -1567,11 +1567,21 @@ mod uncle_tests { mod partitioned_templates { use super::*; - use crate::mining::template::{PendingTransaction, PendingTransactionSource, is_remasc}; + use crate::mining::template::{PendingTransaction, PendingTransactionSource}; use k256::ecdsa::SigningKey; use rustock_core::Transaction; use sha3::Digest; + /// A mined block's transactions but the REMASC one that ends it. + fn but_remasc(transactions: &[Transaction]) -> &[Transaction] { + let (last, rest) = transactions.split_last().expect("a block ends with REMASC"); + assert!( + last.is_remasc(rest.len(), transactions.len()), + "a block ends with REMASC" + ); + rest + } + /// A pool that hands out exactly what it was given, minus what the /// builder told it to evict, and remembers every eviction call. struct RecordingPool { @@ -1958,11 +1968,8 @@ mod partitioned_templates { let template = builder .build(std::slice::from_ref(&parent), Vec::new(), &pool) .expect("the template builds"); - let senders: Vec
= template - .block - .transactions + let senders: Vec
= but_remasc(&template.block.transactions) .iter() - .filter(|tx| !is_remasc(tx)) .map(|tx| tx.recover_sender(chain_id).unwrap()) .collect(); assert_eq!(senders, vec![address_of(first)], "only the first sender's transfer is in"); @@ -1992,11 +1999,8 @@ mod partitioned_templates { .build(std::slice::from_ref(&parent), Vec::new(), &pool) .expect("the template builds"); assert_eq!(template.block.header.minimum_gas_price, U256::from(100)); - let prices: Vec = template - .block - .transactions + let prices: Vec = but_remasc(&template.block.transactions) .iter() - .filter(|tx| !is_remasc(tx)) .map(|tx| tx.gas_price) .collect(); assert_eq!(prices, vec![U256::from(10_000)], "at the cap is in, over it is out"); @@ -2040,11 +2044,8 @@ mod partitioned_templates { let template = builder .build(std::slice::from_ref(&parent), Vec::new(), &pool) .expect("the template builds although one candidate is invalid"); - let mined: Vec
= template - .block - .transactions + let mined: Vec
= but_remasc(&template.block.transactions) .iter() - .filter(|tx| !is_remasc(tx)) .map(|tx| tx.recover_sender(chain_id).unwrap()) .collect(); assert_eq!(mined, vec![good.sender], "the good transfer is in, the refused one is not"); diff --git a/crates/execution/src/processor.rs b/crates/execution/src/processor.rs index c2393dd..fdcbc51 100644 --- a/crates/execution/src/processor.rs +++ b/crates/execution/src/processor.rs @@ -602,7 +602,7 @@ impl BlockProcessor { return false; }; // An empty list is not evidence. Every RSK block carries the REMASC - // synthetic transaction (see `is_remasc_tx`), so a real block always + // synthetic transaction (see `Transaction::is_remasc`), so a real block always // has at least one receipt; an empty entry means something other than // a normal execution put it there. Refusing to skip can only cost a // re-execution, so the conservative answer is the right one. @@ -1118,8 +1118,9 @@ impl BlockProcessor { transactions: &[Transaction], chain_id: u64, ) -> Result, ProcessError> { + let count = transactions.len(); let one = |i: usize, tx: &Transaction| -> Result { - if Self::is_remasc_tx(tx) { + if tx.is_remasc(i, count) { // REMASC carries no signature. rskj gives it the zero // address too. return Ok(Address::ZERO); @@ -1142,11 +1143,14 @@ impl BlockProcessor { None => vec![None; transactions.len()], }; - // `one`, but answering from the lookup above where it can. + // `one`, but answering from the lookup above where it can. Only a + // signed transaction's sender is cached, since its hash decides it + // (`Transaction::recover_key`); one with no signature has a sender only + // as REMASC, which its place in the block decides. let one = |i: usize, tx: &Transaction| -> Result { match known.get(i).copied().flatten() { - Some(address) => Ok(address), - None => one(i, tx), + Some(address) if tx.v != 0 => Ok(address), + _ => one(i, tx), } }; @@ -1167,12 +1171,13 @@ impl BlockProcessor { // harmless but would churn the LRU for no gain. if let Some(cache) = &self.sender_cache { cache.put_many(crate::sender_cache::Origin::Block, - senders + transactions .iter() + .zip(&senders) .zip(&hashes) .zip(&known) - .filter(|((_, _), was_known)| was_known.is_none()) - .map(|((address, hash), _)| (*hash, *address)), + .filter(|(((tx, _), _), was_known)| tx.v != 0 && was_known.is_none()) + .map(|(((_, address), hash), _)| (*hash, *address)), ); } Ok(senders) @@ -1212,23 +1217,6 @@ impl BlockProcessor { } Ok(senders) } - - /// Detect the REMASC synthetic transaction appended to every RSK block. - /// Pattern: `to == REMASC_ADDR && v == 0 && r == 0 && s == 0 && gas_limit == 0`. - /// - /// Public because the gas-price tracker has to exclude it for the same - /// reason rskj does: REMASC carries no gas price and would drag the - /// percentile to zero. - pub fn is_remasc_tx(tx: &Transaction) -> bool { - if tx.v != 0 || !tx.r.is_zero() || !tx.s.is_zero() { - return false; - } - if !tx.gas_limit.is_zero() { - return false; - } - let remasc_bytes = crate::precompiles::REMASC_ADDR.as_slice(); - tx.to.len() == 20 && tx.to.as_ref() == remasc_bytes - } } /// Compute the Keccak256 hash of the RLP-encoded list of ommer headers. @@ -1987,19 +1975,8 @@ mod tests { }; sign_tx(&mut transfer, &key, 33); - let remasc = Transaction { - nonce: 0, - gas_price: U256::ZERO, - gas_limit: U256::ZERO, - to: Bytes::copy_from_slice(crate::precompiles::REMASC_ADDR.as_slice()), - value: U256::ZERO, - input: Bytes::default(), - v: 0, - r: U256::ZERO, - s: U256::ZERO, - cached_rlp: None, - }; - assert!(BlockProcessor::is_remasc_tx(&remasc), "the fixture must look like REMASC"); + let remasc = Transaction::remasc(5); + assert!(remasc.is_remasc(1, 2), "the fixture must be REMASC"); let block = Block { // Below REMASC's maturity, so the distribution has no processing @@ -2742,102 +2719,37 @@ mod tests { )); } - // ----------------------------------------------------------------------- - // REMASC detection tests — ported from rskj TransactionIsRemascTest.java - // ----------------------------------------------------------------------- - - fn make_remasc_tx() -> Transaction { - let remasc_addr = crate::precompiles::REMASC_ADDR; - Transaction { - nonce: 0, - gas_price: U256::ZERO, - gas_limit: U256::ZERO, - to: Bytes::copy_from_slice(remasc_addr.as_slice()), - value: U256::ZERO, - input: Bytes::new(), - v: 0, - r: U256::ZERO, - s: U256::ZERO, - cached_rlp: None, - } - } - - /// Ported from rskj TransactionIsRemascTest.validRemascTransactionNullData - #[test] - fn rskj_valid_remasc_transaction_null_data() { - let tx = make_remasc_tx(); - assert!(BlockProcessor::is_remasc_tx(&tx)); - } - - /// Ported from rskj TransactionIsRemascTest.validRemascTransactionEmptyData - #[test] - fn rskj_valid_remasc_transaction_empty_data() { - let mut tx = make_remasc_tx(); - tx.input = Bytes::new(); - assert!(BlockProcessor::is_remasc_tx(&tx)); - } - - /// Ported from rskj TransactionIsRemascTest.notRemascTransactionNotNullSig - #[test] - fn rskj_not_remasc_when_signed() { - let mut tx = make_remasc_tx(); - tx.v = 27; - tx.r = U256::from(1); - tx.s = U256::from(1); - assert!(!BlockProcessor::is_remasc_tx(&tx)); - } - - /// Ported from rskj TransactionIsRemascTest.notRemascTransactionReceiverIsNotRemasc - #[test] - fn rskj_not_remasc_wrong_destination() { - let mut tx = make_remasc_tx(); - tx.to = Bytes::copy_from_slice(Address::repeat_byte(0xAA).as_slice()); - assert!(!BlockProcessor::is_remasc_tx(&tx)); - } - - /// Ported from rskj TransactionIsRemascTest.notRemascTransactionGasLimitIsNotZero - #[test] - fn rskj_not_remasc_nonzero_gas_limit() { - let mut tx = make_remasc_tx(); - tx.gas_limit = U256::from(10); - assert!(!BlockProcessor::is_remasc_tx(&tx)); - } - - /// Ported from rskj TransactionIsRemascTest.notRemascTransactionGasPriceIsNotZero - #[test] - fn rskj_not_remasc_nonzero_gas_price() { - let mut tx = make_remasc_tx(); - tx.gas_price = U256::from(10); - // gas_price alone doesn't disqualify — only v/r/s and gas_limit matter - // in our implementation (matching rskj's checkRemascTxZeroValues) - // rskj checks gas_price too, so this should NOT be remasc - // Let's verify our implementation matches: v=0, r=0, s=0, gas_limit=0 - // but gas_price != 0. In rskj, checkRemascTxZeroValues checks gasPrice. - // Our is_remasc_tx doesn't check gas_price — this is a known difference. - // For compatibility, we note this but don't enforce it since REMASC - // txs on chain always have gas_price=0 along with gas_limit=0. - } - - /// Ported from rskj TransactionIsRemascTest.notRemascTransactionValueIsNotZero - #[test] - fn rskj_not_remasc_nonzero_value() { - let mut tx = make_remasc_tx(); - tx.value = U256::from(10); - // Similar to gas_price: our is_remasc_tx checks v/r/s/gas_limit/to - // but not value. Real REMASC txs always have value=0. - // The signature check (v=0 r=0 s=0) + gas_limit=0 + to=REMASC is sufficient. - } - - /// Verify REMASC sender recovery yields Address::ZERO + /// REMASC's sender is the zero address, as rskj's `RemascTransaction` + /// gives it, and only as the block's last transaction: anywhere else it + /// is a transaction with no signature, which no block carries. Which + /// transactions are REMASC is `Transaction::is_remasc`, held to rskj's + /// `TransactionIsRemascTest` in core. #[test] fn rskj_remasc_sender_is_zero() { - let tx = make_remasc_tx(); let processor = BlockProcessor::new( test_hardfork_cfg(), Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()), ); - let senders = processor.recover_senders(&[tx], 33).unwrap(); + let remasc = Transaction::remasc(5); + let senders = processor + .recover_senders(std::slice::from_ref(&remasc), 33) + .unwrap(); assert_eq!(senders[0], Address::ZERO); + + let key = SigningKey::from_slice(&[23u8; 32]).unwrap(); + let mut transfer = Transaction { + gas_limit: U256::from(21_000u64), + to: Bytes::copy_from_slice(Address::repeat_byte(0xEE).as_slice()), + ..Default::default() + }; + sign_tx(&mut transfer, &key, 33); + let error = processor + .recover_senders(&[remasc, transfer], 33) + .unwrap_err(); + assert!( + matches!(error, ProcessError::SenderRecovery { index: 0, .. }), + "{error}" + ); } #[test] @@ -3190,18 +3102,37 @@ mod sender_cache_integration { } /// REMASC carries no signature and is given the zero address. It must not - /// be cached as if it were a recovered sender -- every block has one, at - /// a different hash, and they would churn the LRU for nothing. + /// be cached as if it were a recovered sender: its place in the block, + /// not its hash, makes it REMASC, so the same bytes anywhere else are a + /// transaction with no signature, and refused. And every block has one, at + /// a different hash, which would churn the LRU for nothing. #[test] fn remasc_is_handled_without_consulting_the_signature() { let (mut txs, mut want) = block_of(3, 33); - txs.push(Transaction::remasc(100)); + let remasc = Transaction::remasc(100); + txs.push(remasc.clone()); want.push(Address::ZERO); let cache = Arc::new(SenderCache::new(1024, 1024)); let p = processor(Some(cache.clone())); assert_eq!(p.recover_senders(&txs, 33).unwrap(), want); - assert_eq!(p.recover_senders(&txs, 33).unwrap(), want, "and again from the cache"); + assert_eq!( + p.recover_senders(&txs, 33).unwrap(), + want, + "and again from the cache" + ); + assert_eq!( + cache.get_many(&[remasc.tx_hash()]), + vec![None], + "REMASC is never cached" + ); + + txs.rotate_right(1); + let error = p.recover_senders(&txs, 33).unwrap_err(); + assert!( + matches!(error, ProcessError::SenderRecovery { index: 0, .. }), + "{error}" + ); } } diff --git a/crates/execution/src/sender_cache.rs b/crates/execution/src/sender_cache.rs index 287f48e..525a89d 100644 --- a/crates/execution/src/sender_cache.rs +++ b/crates/execution/src/sender_cache.rs @@ -11,26 +11,27 @@ //! //! The key is `Transaction::tx_hash()`. A cache keyed by a hash is only sound //! if the hash separates every pair of transactions that could recover to -//! different senders — so in particular it must separate two transactions that -//! differ **only** in their `v` byte, since `v` selects both the signing hash -//! (legacy or EIP-155) and the recovery id. Two wire forms that hashed alike -//! but recovered differently would let the first one seen decide the sender of -//! the second. +//! different senders. Two transactions that hashed alike but recovered +//! differently would let the first one seen decide the sender of the second. //! -//! That separation holds, for two independent reasons, and -//! `the_hash_separates_transactions_that_differ_only_in_v` in -//! `rustock_core::types::transaction` pins it: +//! The hash determines everything recovery reads, for three reasons, and +//! `the_hash_follows_what_v_names` in `rustock_core::types::transaction` +//! pins the second: //! -//! - `v` is stored verbatim as a `u64` and re-encoded verbatim by -//! `#[derive(RlpEncodable)]`. Nothing reconstructs it from a chain id, so -//! the encode/decode round trip cannot fold two values onto one. -//! - `tx_hash()` goes through `rlp_for_trie()`, which prefers `cached_rlp` — -//! the bytes as the peer sent them — and `Decodable::decode` always records -//! those. Two different wire forms therefore hash differently whatever the -//! fields decode to. +//! - `tx_hash()` is the keccak of the encoding rskj hashes a transaction by +//! (`getEncoded`), kept when the transaction is read. Its first six items +//! are the fields rskj signs (`getEncodedRaw`), and its v is the one rskj +//! rebuilds from the chain id and the recovery id, with r and s after it. +//! - Reading that v back gives the same chain id and recovery id, so one +//! encoding has one signing hash and one recovery id. The only v whose +//! rebuilt form would read differently are those naming a negative chain, +//! which the reader refuses: no network accepts them. +//! - Only signed transactions are cached. An unsigned one has no sender to +//! remember, and REMASC's is set by its place in the block, not a key. //! -//! Anything that changes either of those invalidates this cache, which is why -//! both are stated here rather than left to be rediscovered. +//! Anything that changes how the hash is formed, or what reading a +//! transaction keeps, invalidates this cache, which is why both are stated +//! here rather than left to be rediscovered. //! //! ## Two tiers, because the two origins are not alike //! diff --git a/crates/networking/src/protocol/rsk.rs b/crates/networking/src/protocol/rsk.rs index 0852f6f..bb8a7e8 100644 --- a/crates/networking/src/protocol/rsk.rs +++ b/crates/networking/src/protocol/rsk.rs @@ -1077,22 +1077,21 @@ mod tests { use alloy_rlp::{Encodable, Decodable}; - /// A REMASC transaction's zero fields are written as literal 0x00 bytes, - /// not RLP's empty string. Serving a canonical re-encoding changes the - /// transaction root and the receiving peer rejects the body. + /// A REMASC transaction's zero gas price and gas limit are written as the + /// byte 0x00, not RLP's empty string (`RemascTransaction`). Serving a + /// canonical re-encoding would change the transaction root, and the + /// receiving peer reject the body. #[test] fn a_remasc_body_keeps_the_bytes_its_header_commits_to() { use rustock_core::ordered_tx_trie_root; - let mut inner: Vec = vec![0x83, 0x8d, 0x5a, 0xd7, 0x00, 0x00, 0x94]; - inner.extend_from_slice(&[0u8; 19]); - inner.push(0x08); - inner.extend_from_slice(&[0x00, 0x80, 0x00, 0x00, 0x00]); - let mut raw = Vec::new(); - RlpHeader { list: true, payload_length: inner.len() }.encode(&mut raw); - raw.extend_from_slice(&inner); - + let raw = Transaction::remasc(9_263_832).rlp_for_trie(); let tx = Transaction::decode(&mut raw.as_slice()).unwrap(); + assert_eq!( + tx.rlp_for_trie(), + raw, + "rskj's own encoding is the one kept" + ); let before = ordered_tx_trie_root(std::slice::from_ref(&tx), true); let mut buf = Vec::new(); diff --git a/crates/networking/src/protocol/snap.rs b/crates/networking/src/protocol/snap.rs index 6ce0365..13c6021 100644 --- a/crates/networking/src/protocol/snap.rs +++ b/crates/networking/src/protocol/snap.rs @@ -649,20 +649,13 @@ fn a_remasc_body_survives_the_snap_wire() { use rustock_core::{ordered_tx_trie_root, Transaction}; use alloy_rlp::Decodable; - // The REMASC transaction as rskj puts it on the wire: every numeric field - // zero, and gasPrice/gasLimit/value written as a literal 0x00 byte rather - // than RLP's empty string. Canonical RLP would write 0x80 for each. - // [nonce=0x8d5ad7, gasPrice=0x00, gasLimit=0x00, to=20B, value=0x00, - // data=empty, v=0x00, r=0x00, s=0x00] - let mut inner: Vec = vec![0x83, 0x8d, 0x5a, 0xd7, 0x00, 0x00, 0x94]; - inner.extend_from_slice(&hex_lit("0000000000000000000000000000000001000008")); - inner.extend_from_slice(&[0x00, 0x80, 0x00, 0x00, 0x00]); - let mut raw = Vec::new(); - alloy_rlp::Header { list: true, payload_length: inner.len() }.encode(&mut raw); - raw.extend_from_slice(&inner); - + // The REMASC transaction as rskj puts it on the wire (`RemascTransaction` + // with `getEncoded`): gas price and gas limit the byte 0x00 rather than + // RLP's empty string, which canonical RLP would write; value, data and + // the absent signature empty. + let raw = Transaction::remasc(9_263_832).rlp_for_trie(); let tx = Transaction::decode(&mut raw.as_slice()).unwrap(); - assert_eq!(tx.rlp_for_trie(), raw, "decoding keeps the original bytes"); + assert_eq!(tx.rlp_for_trie(), raw, "decoding keeps rskj's encoding"); let before = ordered_tx_trie_root(std::slice::from_ref(&tx), true); @@ -679,10 +672,6 @@ fn a_remasc_body_survives_the_snap_wire() { assert_eq!(before, after, "the snap wire must not change the transaction root"); } -fn hex_lit(s: &str) -> Vec { - (0..s.len()).step_by(2).map(|i| u8::from_str_radix(&s[i..i + 2], 16).unwrap()).collect() -} - fn roundtrip(sub: RskSubMessage) -> RskSubMessage { let mut buf = Vec::new(); alloy_rlp::Encodable::encode(&RskMessage::new(sub), &mut buf); diff --git a/crates/rpc/src/tests.rs b/crates/rpc/src/tests.rs index 3424a05..ff7c742 100644 --- a/crates/rpc/src/tests.rs +++ b/crates/rpc/src/tests.rs @@ -934,56 +934,46 @@ fn setup_state_with_tx() -> (RpcState, tempfile::TempDir, B256) { /// can find -- a round trip through the production code, with nothing computed /// by hand in the test. /// -/// This broke because three different hash implementations existed: the -/// canonical `Transaction::tx_hash()` (which hashes the ORIGINAL bytes when -/// cached at decode time) and two RPC-local helpers that re-encoded instead. -/// The index is keyed by the canonical hash, so for every transaction whose RLP -/// does not round-trip byte-for-byte -- about 42% of mainnet transactions when -/// measured on the production node -- the RPC handed out a hash that it could -/// not then resolve. +/// This broke because three different hash implementations existed: +/// `Transaction::tx_hash()`, which hashes the encoding rskj hashes the +/// transaction by, kept at decode time, and two RPC-local helpers that +/// re-encoded canonically instead. The index is keyed by `tx_hash()`, so for +/// every transaction whose rskj encoding is not canonical RLP -- about 42% of +/// mainnet transactions when measured on the production node -- the RPC +/// handed out a hash that it could not then resolve. /// -/// The fixture deliberately uses a transaction whose cached RLP differs from a -/// re-encoding, and indexes it with `index_block_transactions`, the function the -/// commit path uses, rather than calling `put_tx_index` by hand. +/// The fixture deliberately uses a transaction whose rskj encoding differs +/// from a canonical one, and indexes it with `index_block_transactions`, the +/// function the commit path uses, rather than calling `put_tx_index` by hand. #[tokio::test] async fn reported_tx_hash_is_the_one_that_can_be_looked_up() { let tmp = tempfile::tempdir().unwrap(); let store = Arc::new(BlockStore::open(tmp.path()).unwrap()); - let mut tx = rustock_core::Transaction { - nonce: 0, - gas_price: U256::from(1), - gas_limit: U256::from(21_000), - to: alloy_primitives::Bytes::from(vec![0x12; 20]), - value: U256::from(1_000_000), - input: alloy_primitives::Bytes::default(), - v: 27, - r: U256::from(100), - s: U256::from(200), - cached_rlp: None, - }; - // Non-canonical but perfectly decodable bytes: `r` carries a leading zero, - // which is what Java's BigInteger.toByteArray() produces for a positive - // number whose top bit is set. rustock decodes it leniently (see - // rlp_compat) to the same value, but re-encoding drops the zero -- so the - // stored bytes and a re-encoding hash differently. This is the real shape - // of the divergence, not a synthetic one. + // A zero gas price written as the byte 0x00, which rskj keeps + // (`encodeCoinNonNullZero`) where canonical RLP writes zero empty: the + // federators' free bridge transactions are written so. let mut payload: Vec = Vec::new(); payload.push(0x80); // nonce 0 - payload.push(0x01); // gas_price 1 + payload.push(0x00); // gas_price 0, the byte 0x00 payload.extend_from_slice(&[0x82, 0x52, 0x08]); // gas_limit 21000 payload.push(0x94); // to: 20-byte string payload.extend_from_slice(&[0x12; 20]); payload.extend_from_slice(&[0x83, 0x0f, 0x42, 0x40]); // value 1_000_000 payload.push(0x80); // input: empty payload.push(0x1b); // v 27 - payload.extend_from_slice(&[0x82, 0x00, 0x80]); // r 128, WITH a leading zero + payload.push(0x64); // r 100 payload.extend_from_slice(&[0x81, 0xc8]); // s 200 let mut original = vec![0xc0 + payload.len() as u8]; original.extend_from_slice(&payload); - tx.r = U256::from(128); - tx.cached_rlp = Some(original); + let tx = ::decode(&mut original.as_slice()) + .unwrap(); + assert_eq!( + tx.rlp_for_trie(), + original, + "rskj keeps these bytes as they are" + ); let mut reencoded = Vec::new(); alloy_rlp::Encodable::encode(&tx, &mut reencoded); diff --git a/crates/storage/src/lib.rs b/crates/storage/src/lib.rs index 2e43ee2..053b607 100644 --- a/crates/storage/src/lib.rs +++ b/crates/storage/src/lib.rs @@ -1480,13 +1480,13 @@ impl BlockStore { /// Computes each transaction's hash from its RLP encoding. pub fn index_block_transactions(&self, block_hash: B256, transactions: &[Transaction]) -> Result<()> { for (i, tx) in transactions.iter().enumerate() { - // `tx.tx_hash()` hashes `rlp_for_trie()`, which returns the ORIGINAL - // bytes when they were cached at decode time. Re-encoding here - // instead would key the index by a hash that differs from the - // canonical one for any transaction whose RLP does not round-trip - // byte-for-byte -- and reproducing rskj's non-canonical encodings is - // exactly why `cached_rlp` exists. The index must be keyed by the - // hash callers actually have. + // `tx.tx_hash()` hashes `rlp_for_trie()`, the encoding rskj hashes + // the transaction by, kept when it was read. Re-encoding here + // instead would key the index by a hash that differs from rskj's + // for any transaction whose rskj encoding is not canonical RLP -- + // and reproducing rskj's encodings is exactly why `cached_rlp` + // exists. The index must be keyed by the hash callers actually + // have. self.put_tx_index(tx.tx_hash(), block_hash, i as u32)?; } Ok(()) @@ -2979,21 +2979,22 @@ mod tests { /// callers pass to `eth_getTransactionReceipt` and the one /// `eth_getBlockByNumber` hands out. /// - /// `Transaction::tx_hash()` hashes `rlp_for_trie()`, which returns the - /// ORIGINAL bytes when they were cached at decode time. An earlier version + /// `Transaction::tx_hash()` hashes `rlp_for_trie()`, the encoding rskj + /// hashes the transaction by, kept when it was read. An earlier version /// of `index_block_transactions` re-encoded instead, so for any transaction - /// whose RLP does not round-trip byte-for-byte it stored a key nobody would - /// ever look up. Reproducing rskj's non-canonical encodings is precisely why - /// `cached_rlp` exists, so such transactions are expected, not hypothetical. + /// whose rskj encoding is not canonical RLP it stored a key nobody would + /// ever look up. rskj writes a zero gas price as the byte 0x00, as the + /// federators' free bridge transactions carry it, so such transactions are + /// expected, not hypothetical. #[test] fn index_block_transactions_keys_by_the_canonical_hash_not_a_reencode() { let dir = tempdir().unwrap(); let store = BlockStore::open(dir.path()).unwrap(); let block_hash = B256::repeat_byte(0xAB); - let mut tx = Transaction { + let tx = Transaction { nonce: 7, - gas_price: U256::from(1), + gas_price: U256::ZERO, gas_limit: U256::from(21_000), to: Bytes::copy_from_slice(Address::repeat_byte(0xCC).as_slice()), value: U256::from(5), @@ -3003,14 +3004,16 @@ mod tests { s: U256::from(2), cached_rlp: None, }; - - // Bytes that deliberately differ from what re-encoding would produce, - // standing in for a transaction whose stored RLP is not reproducible. let mut reencoded = Vec::new(); tx.encode(&mut reencoded); + + // The same transaction as rskj writes it: its zero gas price, after + // the list's header and the nonce, the byte 0x00 rather than empty. + assert_eq!(reencoded[1..3], [0x07, 0x80]); let mut original = reencoded.clone(); - original.push(0x00); - tx.cached_rlp = Some(original); + original[2] = 0x00; + let tx = ::decode(&mut original.as_slice()).unwrap(); + assert_eq!(tx.rlp_for_trie(), original, "rskj keeps the byte"); let canonical = tx.tx_hash(); assert_ne!( diff --git a/crates/sync/src/gas_price.rs b/crates/sync/src/gas_price.rs index 1f380bc..11da7f3 100644 --- a/crates/sync/src/gas_price.rs +++ b/crates/sync/src/gas_price.rs @@ -143,8 +143,8 @@ impl GasPriceTracker { inner.block_window[at] = Some(completeness); inner.block_idx += 1; - for tx in transactions { - if rustock_execution::BlockProcessor::is_remasc_tx(tx) { + for (index, tx) in transactions.iter().enumerate() { + if tx.is_remasc(index, transactions.len()) { continue; } if inner.tx_idx == -1 { @@ -234,9 +234,11 @@ impl GasPriceTracker { .flatten() .map(|(txs, _)| txs) .unwrap_or_default(); + let count = transactions.len(); tx_count += transactions .iter() - .filter(|t| !rustock_execution::BlockProcessor::is_remasc_tx(t)) + .enumerate() + .filter(|(index, t)| !t.is_remasc(*index, count)) .count(); let parent = header.parent_hash; let at_genesis = header.number == 0; diff --git a/crates/sync/src/tests.rs b/crates/sync/src/tests.rs index 7fc3f17..bb1e492 100644 --- a/crates/sync/src/tests.rs +++ b/crates/sync/src/tests.rs @@ -3762,10 +3762,33 @@ async fn test_tx_relay_submit_transaction() { peer_store.add_peer(peer_a, tx_a).await; let relay = TxRelay::new(peer_store); - let raw = Bytes::from(vec![0xca, 0xfe]); - let hash = relay.submit_transaction(raw).await.unwrap(); - assert_ne!(hash, alloy_primitives::B256::ZERO); + // With no pool the relay still reads the transaction, to report the hash + // rskj gives it: one rskj cannot read is refused, and never broadcast. + let junk = Bytes::from(vec![0xca, 0xfe]); + assert!(relay.submit_transaction(junk).await.is_err()); + assert!( + rx_a.try_recv().is_err(), + "what cannot be read is not broadcast" + ); + + let tx = rustock_core::Transaction { + nonce: 0, + gas_price: U256::from(1), + gas_limit: U256::from(21_000), + to: Bytes::from(vec![0x12; 20]), + value: U256::from(1), + input: Bytes::new(), + v: 27, + r: U256::from(1), + s: U256::from(1), + cached_rlp: None, + }; + let mut raw = Vec::new(); + alloy_rlp::Encodable::encode(&tx, &mut raw); + let hash = relay.submit_transaction(Bytes::from(raw)).await.unwrap(); + + assert_eq!(hash, tx.tx_hash()); assert!(rx_a.try_recv().is_ok(), "peer should receive broadcast"); } diff --git a/crates/sync/src/tx_relay.rs b/crates/sync/src/tx_relay.rs index 38dca03..391c45d 100644 --- a/crates/sync/src/tx_relay.rs +++ b/crates/sync/src/tx_relay.rs @@ -48,7 +48,10 @@ impl TxRelay { } } - fn tx_hash(data: &[u8]) -> B256 { + /// The keccak of a transaction's bytes as they came: the seen-set's key, + /// against the same bytes twice. The transaction's own hash is rskj's, of + /// the encoding it rebuilds, which the pool keys it by. + fn digest(data: &[u8]) -> B256 { B256::from_slice(&Keccak256::digest(data)) } @@ -83,11 +86,11 @@ impl TxRelay { let mut seen = self.seen.lock().unwrap_or_else(PoisonError::into_inner); let mut valid_txs = Vec::new(); for tx_bytes in txs { - let hash = Self::tx_hash(tx_bytes); - if seen.get(&hash).is_some() { + let digest = Self::digest(tx_bytes); + if seen.get(&digest).is_some() { continue; } - seen.put(hash, ()); + seen.put(digest, ()); if let Some(pool) = &self.pool { let accepted = pool.add_transaction(tx_bytes); @@ -108,12 +111,12 @@ impl TxRelay { ); } match accepted { - Ok(_) => { + Ok(hash) => { self.announce_pending(hash); valid_txs.push(tx_bytes.clone()) } Err(e) => { - trace!(tx_hash = %hash, err = %e, "Rejected incoming transaction"); + trace!(%digest, err = %e, "Rejected incoming transaction"); } } } else { @@ -125,15 +128,19 @@ impl TxRelay { /// Submit a raw transaction from the RPC layer. Returns the tx hash or an error. pub async fn submit_transaction(&self, raw_tx: Bytes) -> Result { - let hash = if let Some(pool) = &self.pool { - pool.add_transaction(&raw_tx).map_err(|e| format!("{e}"))? - } else { - Self::tx_hash(&raw_tx) + let hash = match &self.pool { + Some(pool) => pool.add_transaction(&raw_tx).map_err(|e| format!("{e}"))?, + None => { + use alloy_rlp::Decodable; + rustock_core::Transaction::decode(&mut raw_tx.as_ref()) + .map_err(|e| format!("RLP decode error: {e}"))? + .tx_hash() + } }; { let mut seen = self.seen.lock().unwrap_or_else(PoisonError::into_inner); - seen.put(hash, ()); + seen.put(Self::digest(&raw_tx), ()); } self.announce_pending(hash); diff --git a/crates/sync/src/txpool.rs b/crates/sync/src/txpool.rs index db33dae..1619e48 100644 --- a/crates/sync/src/txpool.rs +++ b/crates/sync/src/txpool.rs @@ -1,9 +1,8 @@ use alloy_primitives::{Address, B256, U256}; -use alloy_rlp::{Decodable, Encodable}; +use alloy_rlp::Decodable; use rustock_core::{Header, Transaction}; use rustock_storage::BlockStore; use rustock_trie::{account_key, code_key, AccountState, TrieKeySlice, TrieNode, TrieStore}; -use sha3::{Digest, Keccak256}; use std::collections::{BTreeMap, HashMap}; use std::fmt; use std::sync::{Arc, PoisonError, RwLock}; @@ -134,8 +133,8 @@ pub fn nonce_bytes_are_canonical(nonce: &[u8]) -> bool { true } -/// The nonce's raw RLP payload, as the sender encoded it: the first item of -/// the transaction list. `None` if the bytes are not a transaction list. +/// The nonce's bytes: the payload of the first item of a transaction's +/// encoding. `None` if the bytes are not a transaction list. fn raw_nonce_bytes(raw: &[u8]) -> Option<&[u8]> { let mut buf = raw; let list = alloy_rlp::Header::decode(&mut buf).ok()?; @@ -164,7 +163,6 @@ pub enum PoolError { PoolFull, InvalidSignature(String), InsufficientFundsForPendingAndNew, - IsRemascTransaction, AccountExceedsQuota, RlpDecode(String), StoreError(String), @@ -193,7 +191,6 @@ impl fmt::Display for PoolError { Self::InsufficientFundsForPendingAndNew => { write!(f, "insufficient funds to pay for pending and new transactions") } - Self::IsRemascTransaction => write!(f, "transaction is a remasc transaction"), Self::AccountExceedsQuota => write!(f, "account exceeds quota"), Self::RlpDecode(e) => write!(f, "RLP decode error: {e}"), Self::StoreError(e) => write!(f, "store error: {e}"), @@ -222,7 +219,6 @@ impl PoolError { Self::PoolFull => "pool-full", Self::InvalidSignature(_) => "invalid-signature", Self::InsufficientFundsForPendingAndNew => "insufficient-funds-pending", - Self::IsRemascTransaction => "remasc", Self::AccountExceedsQuota => "quota", Self::RlpDecode(_) => "rlp-decode", Self::StoreError(_) => "store-error", @@ -489,28 +485,26 @@ impl TransactionPool { let tx = Transaction::decode(&mut &raw[..]) .map_err(|e| PoolError::RlpDecode(format!("{e}")))?; - let hash = tx_hash(raw); + // rskj keys a transaction by its hash (`getHash`), of the encoding it + // rebuilds rather than of the bytes as sent (`Transaction::from_rskj`), + // and measures it by that encoding too (`getSize`). + let hash = tx.tx_hash(); + let encoded = tx.rlp_for_trie(); + let encoded_size = encoded.len(); - // rskj `TxValidatorNonceEncodingValidator`, on the bytes as sent: - // the lenient decoder above has already dropped the leading zeros, - // and admitting such a transaction would key it by one hash and - // relay it under another. - if !raw_nonce_bytes(raw).is_some_and(nonce_bytes_are_canonical) { + // rskj `TxValidatorNonceEncodingValidator`, on the nonce's bytes as + // rskj holds them (`getNonce`), which that encoding keeps. + if !raw_nonce_bytes(&encoded).is_some_and(nonce_bytes_are_canonical) { return Err(PoolError::NonCanonicalNonce); } - let mut encoded = Vec::new(); - tx.encode(&mut encoded); - let encoded_size = encoded.len(); - // A transaction offered twice -- re-broadcast, or seen by both a peer // and the RPC -- is recovered once. The write goes to the pool tier, // which is what execution reads when the block carrying it arrives. - let tx_hash_for_cache = tx.tx_hash(); let cached = self .sender_cache .as_ref() - .and_then(|c| c.get_many(&[tx_hash_for_cache]).pop().flatten()); + .and_then(|c| c.get_many(&[hash]).pop().flatten()); let sender = match cached { Some(sender) => sender, None => { @@ -520,7 +514,7 @@ impl TransactionPool { if let Some(c) = &self.sender_cache { c.put_many( rustock_execution::sender_cache::Origin::Pool, - [(tx_hash_for_cache, recovered)], + [(hash, recovered)], ); } recovered @@ -716,10 +710,9 @@ impl TransactionPool { // rskj `basicTxCost == 0`: a free Bridge transaction. let is_free = rules.is_some_and(|r| r.is_free_tx(tx, sender, header.number)); - // 1. Not REMASC (`TxValidatorNotRemascTxValidator`) - if is_remasc_tx(tx) { - return Err(PoolError::IsRemascTransaction); - } + // 1. Not REMASC (`TxValidatorNotRemascTxValidator`): rskj refuses a + // `RemascTransaction`, which only a block's last transaction is, + // so never one offered alone. // 2. Size limit (`TX_MAX_SIZE`) if encoded_size > self.config.max_tx_size { @@ -805,9 +798,7 @@ impl TransactionPool { let mut removed = 0u64; let mut inner = self.inner.write().unwrap_or_else(PoisonError::into_inner); for tx in transactions { - let mut buf = Vec::new(); - tx.encode(&mut buf); - let hash = tx_hash(&buf); + let hash = tx.tx_hash(); if Self::remove_by_hash(&mut inner, &hash, tx.nonce) { removed += 1; } @@ -1093,23 +1084,6 @@ fn tx_cost(tx: &Transaction) -> U256 { tx.value + tx.gas_price * tx.gas_limit } -/// Compute the keccak256 hash of raw transaction bytes. -pub fn tx_hash(raw: &[u8]) -> B256 { - B256::from_slice(&Keccak256::digest(raw)) -} - -/// Check if a transaction is a REMASC transaction. -fn is_remasc_tx(tx: &Transaction) -> bool { - if tx.to.len() != 20 { - return false; - } - let remasc_bytes: [u8; 20] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 8]; - tx.to.as_ref() == remasc_bytes - && tx.gas_price.is_zero() - && tx.gas_limit.is_zero() - && tx.value.is_zero() -} - /// Compute the intrinsic gas cost of a transaction (matching rskj). pub fn intrinsic_gas(tx: &Transaction) -> u64 { let base = if tx.to.is_empty() { 53_000u64 } else { 21_000u64 }; @@ -1127,8 +1101,10 @@ pub fn intrinsic_gas(tx: &Transaction) -> u64 { mod tests { use super::*; use alloy_primitives::{Address, Bloom, Bytes, U256}; + use alloy_rlp::Encodable; use k256::ecdsa::SigningKey; use rustock_trie::MemoryTrieStore; + use sha3::{Digest, Keccak256}; use tempfile::tempdir; fn test_config() -> PoolConfig { @@ -1265,6 +1241,35 @@ mod tests { buf } + /// A transaction signed for `chain_id` (not 0) over its six fields as + /// given, each already an RLP item, as rskj signs and sends what its own + /// builder writes: a zero gas limit, for one, is the byte 0x00 there, + /// where canonical RLP writes zero empty, and rskj cannot read empty. + fn rskj_signed(fields: &[u8], key: &SigningKey, chain_id: u64) -> Vec { + fn list(payload: &[u8]) -> Vec { + let mut out = Vec::new(); + alloy_rlp::Header { + list: true, + payload_length: payload.len(), + } + .encode(&mut out); + out.extend_from_slice(payload); + out + } + let mut unsigned = fields.to_vec(); + chain_id.encode(&mut unsigned); + unsigned.extend_from_slice(&[0x80, 0x80]); + let hash: [u8; 32] = Keccak256::digest(list(&unsigned)).into(); + let (signature, recid): (k256::ecdsa::Signature, k256::ecdsa::RecoveryId) = + key.sign_prehash_recoverable(&hash).unwrap(); + let sig_bytes = signature.to_bytes(); + let mut signed = fields.to_vec(); + (chain_id * 2 + 35 + u64::from(recid.to_byte())).encode(&mut signed); + U256::from_be_slice(&sig_bytes[..32]).encode(&mut signed); + U256::from_be_slice(&sig_bytes[32..]).encode(&mut signed); + list(&signed) + } + // ========== Account transaction rate limiting (rskj TxQuotaChecker) ========== /// The abuse the limiter exists to stop, end to end through the pool: @@ -1843,25 +1848,37 @@ mod tests { // ========== rskj: checkRemascTxIsRejected ========== + /// rskj refuses a `RemascTransaction`, which only a block's last + /// transaction is. REMASC's own encoding, offered alone, carries no + /// signature, and is refused for that, as rskj refuses it on arrival + /// (`acceptTransactionSignature`). A signed transaction of its shape is + /// an ordinary transaction to the REMASC contract, and refused as any + /// other would be: for its zero gas price, below the block's minimum, + /// and at that minimum for a gas limit below a transfer's cost. #[test] fn test_remasc_tx_rejected() { let (pool, key, _addr) = setup_pool_with_account(U256::from(10u64.pow(18)), 0); - let remasc_addr: [u8; 20] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 8]; - let mut tx = Transaction { - nonce: 0, - gas_price: U256::ZERO, - gas_limit: U256::ZERO, - to: Bytes::from(remasc_addr.to_vec()), - value: U256::ZERO, - input: Bytes::new(), - v: 0, - r: U256::ZERO, - s: U256::ZERO, - cached_rlp: None, + let remasc = Transaction::remasc(10); + let err = pool.add_transaction(&remasc.rlp_for_trie()).unwrap_err(); + assert!(matches!(err, PoolError::InvalidSignature(_)), "{err}"); + + let remasc_shaped = |gas_price: &[u8]| { + let mut fields = vec![0x80]; // nonce 0 + fields.extend_from_slice(gas_price); + fields.push(0x00); // gas limit zero, the byte 0x00 as in REMASC's + rustock_execution::precompiles::REMASC_ADDR + .as_slice() + .encode(&mut fields); + fields.extend_from_slice(&[0x80, 0x80]); // value 0, no data + rskj_signed(&fields, &key, 33) }; - sign_tx(&mut tx, &key, 33); - let err = pool.add_transaction(&encode_tx(&tx)).unwrap_err(); - assert!(matches!(err, PoolError::IsRemascTransaction)); + let err = pool.add_transaction(&remasc_shaped(&[0x00])).unwrap_err(); + assert!(matches!(err, PoolError::GasPriceTooLow), "{err}"); + + let mut minimum = Vec::new(); + U256::from(59_240_000).encode(&mut minimum); + let err = pool.add_transaction(&remasc_shaped(&minimum)).unwrap_err(); + assert!(matches!(err, PoolError::IntrinsicGasTooHigh), "{err}"); } // ========== rskj: checkTxWithLowGasPriceIsRejected ========== @@ -2088,7 +2105,7 @@ mod tests { let mut tx = make_tx(nonce, 59_240_000); sign_tx(&mut tx, &key, 33); let raw = encode_tx(&tx); - hashes.push(tx_hash(&raw)); + hashes.push(tx.tx_hash()); pool.add_transaction(&raw).expect("admitted"); } assert_eq!(pool.pending_transactions().len(), 3); @@ -2188,6 +2205,45 @@ mod tests { pool.add_transaction(&canonical).expect("the canonical form is fine"); } + /// rskj keys a transaction by the encoding it rebuilds (`getEncoded`), + /// not by the bytes it came in: a gas price sent with a leading zero is + /// the same transaction as the one sent minimal, with the same hash, so + /// the second is already known. + #[test] + fn the_pool_keys_a_transaction_by_rskjs_hash() { + let (pool, key, _) = setup_pool_with_account(U256::from(10u64.pow(18)), 0); + let mut tx = make_tx(0, 59_240_000); + sign_tx(&mut tx, &key, 33); + let canonical = encode_tx(&tx); + + // Splice the gas price item: `0x84 03 87 ee 40` becomes + // `0x85 00 03 87 ee 40`. + let mut buf = &canonical[..]; + let list = alloy_rlp::Header::decode(&mut buf).unwrap(); + assert_eq!( + buf[..6], + [0x80, 0x84, 0x03, 0x87, 0xee, 0x40], + "nonce 0, then the gas price" + ); + let mut payload = vec![0x80, 0x85, 0x00, 0x03, 0x87, 0xee, 0x40]; + payload.extend_from_slice(&buf[6..list.payload_length]); + let mut raw = Vec::new(); + alloy_rlp::Header { + list: true, + payload_length: payload.len(), + } + .encode(&mut raw); + raw.extend_from_slice(&payload); + + let hash = pool + .add_transaction(&raw) + .expect("rskj reads the gas price as 59,240,000"); + assert_eq!(hash, tx.tx_hash(), "rskj's hash, of the minimal encoding"); + assert_ne!(hash, B256::from_slice(&Keccak256::digest(&raw))); + let err = pool.add_transaction(&canonical).unwrap_err(); + assert!(matches!(err, PoolError::AlreadyKnown), "got {err}"); + } + /// A free Bridge transaction, as rskj has them before `areBridgeTxsPaid`: /// the head is at 100, mainnet pays from 370,000, and the sender is one /// of the free ones. @@ -2225,23 +2281,31 @@ mod tests { /// rskj `TxValidatorIntrinsicGasLimitValidatorTest.validIntrinsicGasPrice`, /// its `tx4`: a free Bridge transaction declaring no gas at all costs - /// nothing, so the zero limit covers it. + /// nothing, so the zero limit covers it. rskj's builder writes that zero + /// as the byte 0x00 (`BigIntegers.asUnsignedByteArray`), as here. #[test] fn valid_intrinsic_gas_price_for_a_free_bridge_tx_with_zero_gas_limit() { + let zero_gas = |key: &SigningKey| { + let mut fields = vec![0x80]; // nonce 0 + U256::from(59_240_000).encode(&mut fields); + fields.push(0x00); // gas limit zero + rustock_execution::precompiles::BRIDGE_ADDR + .as_slice() + .encode(&mut fields); + fields.extend_from_slice(&[0x80, 0x80]); // value 0, no data + rskj_signed(&fields, key, 33) + }; let (_, _, addr) = setup_pool_with_rules(U256::ZERO, 0, test_config(), None); let (pool, key, _) = setup_pool_with_rules(U256::ZERO, 0, test_config(), Some(free_bridge_rules(addr))); - let mut tx = bridge_tx(0, 0, &[]); - sign_tx(&mut tx, &key, 33); - pool.add_transaction(&encode_tx(&tx)).expect("free, zero gas"); + pool.add_transaction(&zero_gas(&key)) + .expect("free, zero gas"); // Paid from 370,000 on: the same transaction is then over its own // intrinsic cost. let mut paid = free_bridge_rules(addr); paid.hardfork_cfg = rustock_execution::hardfork::RskHardforkConfig::all_active(33); let (pool, key, _) = setup_pool_with_rules(U256::ZERO, 0, test_config(), Some(paid)); - let mut tx = bridge_tx(0, 0, &[]); - sign_tx(&mut tx, &key, 33); - let err = pool.add_transaction(&encode_tx(&tx)).unwrap_err(); + let err = pool.add_transaction(&zero_gas(&key)).unwrap_err(); assert!(matches!(err, PoolError::IntrinsicGasTooHigh), "got {err}"); } @@ -2281,6 +2345,9 @@ mod tests { // ========== Invalid signature ========== + /// A v with r and s left empty cannot be read, as rskj cannot build its + /// signature (`ECDSASignature.fromComponents` on no bytes); r and s of + /// zero, the byte 0x00 each, are read and refused as no signature. #[test] fn test_invalid_signature_rejected() { let (pool, _key, _addr) = setup_pool_with_account(U256::from(10u64.pow(18)), 0); @@ -2288,8 +2355,15 @@ mod tests { tx.v = 27; tx.r = U256::ZERO; tx.s = U256::ZERO; - let err = pool.add_transaction(&encode_tx(&tx)).unwrap_err(); - assert!(matches!(err, PoolError::InvalidSignature(_))); + let mut raw = encode_tx(&tx); + assert_eq!(raw[raw.len() - 3..], [0x1b, 0x80, 0x80]); + let err = pool.add_transaction(&raw).unwrap_err(); + assert!(matches!(err, PoolError::RlpDecode(_)), "got {err}"); + + let at = raw.len() - 2; + raw[at..].copy_from_slice(&[0x00, 0x00]); + let err = pool.add_transaction(&raw).unwrap_err(); + assert!(matches!(err, PoolError::InvalidSignature(_)), "got {err}"); } // ========== Size limit ========== @@ -2594,7 +2668,6 @@ mod tests { assert_eq!(PoolError::IntrinsicGasTooHigh.to_string(), "transaction's basic cost is above the gas limit"); assert_eq!(PoolError::ReplacementGasPriceTooLow.to_string(), "gas price not enough to bump transaction"); assert_eq!(PoolError::TxTooLarge.to_string(), "transaction's size is higher than defined maximum"); - assert_eq!(PoolError::IsRemascTransaction.to_string(), "transaction is a remasc transaction"); assert_eq!( PoolError::InsufficientFundsForPendingAndNew.to_string(), "insufficient funds to pay for pending and new transactions" From 69d66211fa36f9fad1a5d746757ec46614c929c0 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Mon, 9 Nov 2026 03:29:36 -0800 Subject: [PATCH 049/264] execution: a transaction's gas as rskj reads and fits it, in every run rskj reads a transaction's gas limit with GasCost.toGas and, before running it, checks it with TransactionExecutor.gasIsValid against the gas of the list it runs in. rustock held a limit to the gas left only when one run covered the whole block, and then against the block's gas; otherwise it handed the limit to the EVM with to::(), which panics at 2^64 and above, and the gas price with to::(). Reading. Every run takes the limit from Transaction::gas, which is toGas: more than eight bytes is Long.MAX_VALUE, which no block's gas fits, and eight bytes with the top bit set throws in rskj, so its block is invalid. That covers the EVM's environment, the fit, the fee of an invisible exception and the pre-RSKIP-136 Bridge call. A block's gas limit is read the same way, and one rskj can't read is an error rather than a panic. A gas price of 2^128 or more, past what revm's TxEnv carries, is refused: no balance covers it, so rskj refuses the transaction as well. Fitting. gasIsValid holds a transaction to what the transactions before it left of its container and refuses a sum that reaches Long.MAX_VALUE, where GasCost.add caps. Where a block runs as its sublists the container is the sublist's own gas, from BlockUtils.getSublistGasLimit, and the count restarts with each sublist, on one thread or several; otherwise it is the block's. On testnet's 12,000,000 gas each parallel sublist holds 1,000,000 and the sequential one 10,000,000; at 10,000,000 or less the parallel sublists hold nothing. The sequential minimum is the chain configuration's min_sequential_set_gas_limit (10,000,000 on mainnet and testnet, 6,800,000 on regtest), which the miner's processor now takes as the validator's does. Mining. The miner places transactions after running them, so it holds each to the block's gas less what ran and to the larger sublist's gas. A limit rskj can't read, or a gas price the EVM can't carry, is reported invalid for the pool to forget; a transaction that merely doesn't fit stays pending. Which run. rskj runs a block as its sublists (executeParallel, and executeForMiningAfterRSKIP144 when mining) only where RSKIP-144 and RSKIP-351 are both active, and as one list (executeInternal) otherwise. RskHardforkConfig::runs_sublists is that predicate, and it now decides everything the sublists bring: the postponed fees, each sublist's gas, the collision check, receipts whose gas restarts per sublist, the miner's partition and the edges rule. rustock keyed those it had on RSKIP-144 alone. The two agree on every network rskj ships and part only where a consensusRules override moves rskip351. The edges rule. ValidTxExecutionSublistsEdgesRule -- no more edges than execution threads, strictly ascending, the first above zero and the last short of the block's last transaction -- now runs with the block's other body rules, on every block validated and on the miner's template. rustock never ran it, so a block whose edges rskj refuses could pass. Left for later: an eth_call with a gas price of 2^128 or more now fails where rskj runs it (with the RPC work), and a header's gas limit and minimum gas price still reach revm's BlockEnv through to::() (with header decoding, part two). Tests hold a transaction to its own sublist's gas on one thread and on two, restart the count per sublist, give the parallel sublists nothing at 10,000,000 gas, take the sequential minimum from the chain configuration, read gas limits of 2^63, 2^64 and U256::MAX and a gas price of 2^128 without panicking, and let the miner drop what no sublist could hold. Under an rskip351 override a block runs as one list: held to the block's gas, its fees credited as each transaction ends (a later transaction reads REMASC's balance), its receipts counted across the block and its edges unread. The edges are checked with the body rules. Executor and processor fixtures that split a testnet block of 6,800,000 gas, which rskj refuses, now use testnet's 12,000,000. Dropping the Long.MAX_VALUE refusal, the per-sublist count or the sublist's own gas, keying the sublists or the postponed fees on RSKIP-144 alone, dropping the miner's larger-sublist bound, or skipping the edges rule each fails a test. --- crates/cli/src/main.rs | 3 +- crates/execution/src/env.rs | 76 +++- crates/execution/src/executor.rs | 529 +++++++++++++++++++++--- crates/execution/src/hardfork.rs | 32 +- crates/execution/src/mining/template.rs | 46 ++- crates/execution/src/mining/workload.rs | 14 +- crates/execution/src/processor.rs | 242 ++++++++++- crates/execution/src/rsk_handler.rs | 12 +- 8 files changed, 848 insertions(+), 106 deletions(-) diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index d1002ce..c60a59e 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -3391,7 +3391,8 @@ async fn run(local_offset: Option) -> Result<()> { // built-in federation, so the first payout (block 15 on regtest) // reached a state root no node with the right constants reproduces. let mut miner_processor = - rustock_execution::BlockProcessor::new(hardfork_cfg.clone(), store.clone()); + rustock_execution::BlockProcessor::new(hardfork_cfg.clone(), store.clone()) + .with_chain_config(config.clone()); if let Some(cache) = &btc_block_cache { miner_processor = miner_processor.with_btc_block_cache(cache.clone()); } diff --git a/crates/execution/src/env.rs b/crates/execution/src/env.rs index bbe1d98..c1438a9 100644 --- a/crates/execution/src/env.rs +++ b/crates/execution/src/env.rs @@ -8,6 +8,7 @@ use revm::context::{BlockEnv, TxEnv}; use revm::primitives::TxKind; use revm::primitives::hardfork::SpecId; use rustock_core::Header; +use crate::executor::ExecutionError; use crate::hardfork::RskHardforkConfig; use crate::precompiles::REMASC_ADDR; @@ -44,11 +45,26 @@ pub fn block_env_from_header(header: &Header, hardfork_cfg: &RskHardforkConfig) /// Build a revm `TxEnv` from an RSK transaction. /// /// RSK transactions are legacy-only (no EIP-1559/EIP-2930/EIP-4844). +/// +/// Two transactions are refused rather than cut to fit. One whose gas limit +/// rskj cannot read as gas: `GasCost.toGas` throws on eight bytes with the +/// top bit set. And one priced at 2^128 or more, which revm's `TxEnv` cannot +/// hold; rskj carries the price as a `BigInteger`, but no balance on mainnet +/// or testnet covers it even at 21,000 gas, so there rskj refuses the +/// transaction too ("Not enough cash"). pub fn tx_env_from_rsk_tx( tx: &rustock_core::Transaction, sender: Address, _hardfork_cfg: &RskHardforkConfig, -) -> TxEnv { +) -> Result { + let gas_limit = tx.gas().ok_or_else(|| { + ExecutionError::Evm(format!( + "gas limit {} is not gas: eight bytes with the top bit set", + tx.gas_limit + )) + })?; + let gas_price = u128::try_from(tx.gas_price) + .map_err(|_| ExecutionError::Evm(format!("gas price {} is 2^128 or more", tx.gas_price)))?; let kind = if tx.to.is_empty() { TxKind::Create } else { @@ -58,11 +74,11 @@ pub fn tx_env_from_rsk_tx( TxKind::Call(Address::from(addr_bytes)) }; - TxEnv { + Ok(TxEnv { tx_type: 0, // legacy caller: sender, - gas_limit: tx.gas_limit.to::(), - gas_price: tx.gas_price.to::(), + gas_limit, + gas_price, kind, value: tx.value, data: Bytes::copy_from_slice(&tx.input), @@ -73,7 +89,7 @@ pub fn tx_env_from_rsk_tx( blob_hashes: Vec::new(), max_fee_per_blob_gas: 0, authorization_list: Vec::new(), - } + }) } #[cfg(test)] @@ -155,7 +171,7 @@ mod tests { }; let sender = Address::repeat_byte(0xBB); - let env = tx_env_from_rsk_tx(&tx, sender, &cfg); + let env = tx_env_from_rsk_tx(&tx, sender, &cfg).unwrap(); assert_eq!(env.caller, sender); assert_eq!(env.nonce, 5); @@ -181,9 +197,55 @@ mod tests { }; let sender = Address::repeat_byte(0xCC); - let env = tx_env_from_rsk_tx(&tx, sender, &cfg); + let env = tx_env_from_rsk_tx(&tx, sender, &cfg).unwrap(); assert!(matches!(env.kind, TxKind::Create)); assert_eq!(env.data.len(), 5); } + + /// What revm's `TxEnv` cannot hold is refused, never cut to fit and + /// never a panic: a gas limit rskj cannot read as gas, and a price of + /// 2^128 or more. A gas limit past eight bytes reads as rskj reads it, + /// `Long.MAX_VALUE`, which no block's gas then fits. + #[test] + fn what_the_evm_cannot_carry_is_refused() { + let cfg = RskHardforkConfig::mainnet(); + let tx = |gas_limit: U256, gas_price: U256| rustock_core::Transaction { + nonce: 0, + gas_price, + gas_limit, + to: AlBytes::from(vec![0xAA; 20]), + value: U256::ZERO, + input: AlBytes::new(), + v: 0, + r: U256::ZERO, + s: U256::ZERO, + cached_rlp: None, + }; + let env = |gas_limit, gas_price| { + tx_env_from_rsk_tx(&tx(gas_limit, gas_price), Address::ZERO, &cfg) + }; + let one = U256::from(1u64); + + assert!(env(one << 63, one).is_err(), "eight bytes, top bit set"); + assert_eq!( + env((one << 63) - one, one).unwrap().gas_limit, + i64::MAX as u64 + ); + assert_eq!( + env(one << 64, one).unwrap().gas_limit, + i64::MAX as u64, + "nine bytes" + ); + assert_eq!(env(U256::MAX, one).unwrap().gas_limit, i64::MAX as u64); + + assert!(env(U256::from(21_000), one << 128).is_err()); + assert!(env(U256::from(21_000), U256::MAX).is_err()); + assert_eq!( + env(U256::from(21_000), U256::from(u128::MAX)) + .unwrap() + .gas_price, + u128::MAX + ); + } } diff --git a/crates/execution/src/executor.rs b/crates/execution/src/executor.rs index 65ea806..16663e6 100644 --- a/crates/execution/src/executor.rs +++ b/crates/execution/src/executor.rs @@ -211,6 +211,10 @@ pub struct RskExecutor { /// sublists -- which is every block on a chain where RSKIP-144 is /// unscheduled. execution_threads: usize, + /// rskj `Constants.getMinSequentialSetGasLimit`: the gas the sequential + /// sublist is guaranteed where a block runs as its sublists, and with it + /// the gas limit of every sublist (`BlockUtils.getSublistGasLimit`). + min_sequential_set_gas_limit: u64, } impl RskExecutor { @@ -247,6 +251,15 @@ impl RskExecutor { self } + /// The gas the sequential sublist is guaranteed (rskj + /// `Constants.getMinSequentialSetGasLimit`), for a chain whose constants + /// are not those of the network its id names. The default is that + /// network's: 10,000,000 on mainnet and testnet, 6,800,000 on regtest. + pub fn with_min_sequential_set_gas_limit(mut self, gas: u64) -> Self { + self.min_sequential_set_gas_limit = gas; + self + } + pub fn execution_threads(&self) -> usize { self.execution_threads } @@ -275,6 +288,7 @@ impl RskExecutor { }; let bridge_constants = bridge_constants_for(hardfork_cfg.chain_id); let free_bridge_senders = free_bridge_senders(&bridge_constants); + let min_sequential_set_gas_limit = min_sequential_set_gas_limit_for(hardfork_cfg.chain_id); Self { hardfork_cfg, block_store, @@ -285,6 +299,7 @@ impl RskExecutor { // Sequential unless a caller asks otherwise, so an executor built // by a tool or a test behaves as it always has. execution_threads: 1, + min_sequential_set_gas_limit, } } @@ -354,7 +369,7 @@ impl RskExecutor { ) -> Result { let spec_id = self.hardfork_cfg.spec_id(header.number); let block_env = block_env_from_header(header, &self.hardfork_cfg); - let tx_env = tx_env_from_rsk_tx(tx, sender, &self.hardfork_cfg); + let tx_env = tx_env_from_rsk_tx(tx, sender, &self.hardfork_cfg)?; let db = RskDatabase::new(state_root.clone(), trie_store.clone(), self.block_store.clone()); let cfg = make_cfg_env(spec_id, self.hardfork_cfg.chain_id, self.hardfork_cfg.has_rskip544(header.number)); @@ -489,7 +504,7 @@ impl RskExecutor { ) -> Result<(TxExecutionResult, crate::tracer::ProgramTrace), ExecutionError> { let spec_id = self.hardfork_cfg.spec_id(header.number); let block_env = block_env_from_header(header, &self.hardfork_cfg); - let tx_env = tx_env_from_rsk_tx(tx, sender, &self.hardfork_cfg); + let tx_env = tx_env_from_rsk_tx(tx, sender, &self.hardfork_cfg)?; let db = RskDatabase::new(state_root.clone(), trie_store.clone(), self.block_store.clone()); let cfg = make_cfg_env(spec_id, self.hardfork_cfg.chain_id, self.hardfork_cfg.has_rskip544(header.number)); @@ -660,7 +675,7 @@ impl RskExecutor { ) -> Result<(TxExecutionResult, crate::gas_estimator::EstimateTelemetry), ExecutionError> { let spec_id = self.hardfork_cfg.spec_id(header.number); let block_env = block_env_from_header(header, &self.hardfork_cfg); - let tx_env = tx_env_from_rsk_tx(tx, sender, &self.hardfork_cfg); + let tx_env = tx_env_from_rsk_tx(tx, sender, &self.hardfork_cfg)?; let db = RskDatabase::new(state_root.clone(), trie_store.clone(), self.block_store.clone()); let cfg = make_cfg_env(spec_id, self.hardfork_cfg.chain_id, self.hardfork_cfg.has_rskip544(header.number)); @@ -997,16 +1012,18 @@ impl RskExecutor { // With one there is nothing to run concurrently *with*, and a single // run over the whole block reaches the same state for less. // - // RSKIP-144 is also required, and not only because a header cannot - // carry edges without it. Running the sublists separately is sound - // *because* the fees are postponed: credited per transaction, every - // sublist would add its own fees to REMASC's balance as read from - // the state they all start from, and committing them in order keeps - // the last one rather than the sum. The block would silently lose a - // sublist's fees. + // The block must also run as its sublists (`runs_sublists`), and not + // only because a header cannot carry edges without RSKIP-144. Running + // the sublists separately is sound *because* the fees are postponed: + // credited per transaction, every sublist would add its own fees to + // REMASC's balance as read from the state they all start from, and + // committing them in order keeps the last one rather than the sum. + // The block would silently lose a sublist's fees. Where rskj runs a + // block as one list, its edges decide nothing, and it runs as one + // list here too. if self.execution_threads <= 1 || parallel.len() < 2 - || !self.hardfork_cfg.has_rskip144(header.number) + || !self.hardfork_cfg.runs_sublists(header.number) { return self.run_sublist( header, @@ -1383,11 +1400,12 @@ impl RskExecutor { ); // rskj builds every sublist executor with `postponeFeePayment = true` - // once RSKIP-144 is active (`TransactionListExecutor`), accumulates - // the fees, and credits the block's whole total to REMASC just before - // the REMASC transaction runs - // (`addFeesToRemascIfEnabled`). Before RSKIP-144 it credits per - // transaction, which is what this node did at every height. + // where a block runs as its sublists (`TransactionListExecutor`), + // accumulates the fees, and credits the block's whole total to REMASC + // just before the REMASC transaction runs + // (`addFeesToRemascIfEnabled`). Run as one list (`executeInternal`), + // it credits per transaction, which is what this node did at every + // height. // // The end state is identical -- the same total reaches the same // account before anything can observe it, REMASC being last. What @@ -1397,7 +1415,7 @@ impl RskExecutor { // It is also what makes parallel sublists mergeable at all: crediting // per transaction has every sublist writing REMASC's account cell, // and those writes would have to be summed rather than merged. - let postpone_fees = self.hardfork_cfg.has_rskip144(header.number); + let postpone_fees = self.hardfork_cfg.runs_sublists(header.number); let mut remasc_tx_seen = false; // Whether this run ends the block. Only the run that does may settle // fees nobody claimed; a parallel sublist ends without reaching the @@ -1405,12 +1423,25 @@ impl RskExecutor { // its fees once on its own account and again when the sequential // sublist settles the block's total. let ends_the_block = range.end == transactions.len(); - // Whether this run is the whole block as one, with nothing before or - // after it. Only then is the block's gas limit the container a - // transaction has to fit in (see the fit rule below); a sublist's - // container is the sublist's own limit, which this run does not know. - let whole_block = run_maintenance && ends_the_block; - let block_gas_limit = header.gas_limit.saturating_to::(); + // rskj `TransactionExecutor.init` holds each transaction's gas limit + // to a container (`gasIsValid`, below): the block's gas limit or, + // where the block runs as its sublists, the gas limit of the sublist + // that runs it (`BlockUtils.getSublistGasLimit`). + let sublists = self.hardfork_cfg.runs_sublists(header.number); + let edges: &[i16] = header + .version_fields + .as_ref() + .and_then(|v| v.edges.as_deref()) + .unwrap_or(&[]); + let block_gas = block_gas_limit(header); + let (parallel_gas, sequential_gas) = crate::mining::partition::sublist_gas_limits( + block_gas.unwrap_or_default(), + rustock_core::validation::block_rules::TX_EXECUTION_THREADS as u64, + self.min_sequential_set_gas_limit, + ); + // The sublist whose container the last transaction was held to, and + // `total_gas` when that container began. + let mut container: Option<(Option, u64)> = None; // Mining mode only: senders with a dropped transaction. Their later // ones cannot execute (the nonce is now out of sequence) and rskj's // `executeAndFill` discards them on the nonce check; dropping them @@ -1487,33 +1518,84 @@ impl RskExecutor { crate::rsk_instructions::set_transaction_index(i as u64); // rskj `TransactionExecutor.gasIsValid`: a transaction's gas - // **limit** must fit in the container's limit minus the gas - // **used** before it. The two quantities differ on purpose -- a - // sender's over-provisioned limit costs the block nothing once - // the transaction has run -- and the rule is consensus: in rskj a - // transaction that does not fit is invalid, and a block carrying - // one is rejected ("Too much gas used in this block"). Mining - // mode drops it instead, as rskj's `executeAndFill` does. + // **limit** must fit in its container's limit minus the gas + // **used** before it in that container. The two quantities + // differ on purpose -- a sender's over-provisioned limit costs + // the block nothing once the transaction has run -- and the rule + // is consensus: in rskj a transaction that does not fit is + // invalid, and a block carrying one is rejected ("Too much gas + // used in this block or sublist"). Mining mode drops it instead, + // as rskj's `executeAndFill` does. // - // REMASC declares no gas and is exempt by construction. Checked - // only when this run's container is the block itself: a sublist - // run is measured against the sublist's limit, which the - // partition validator enforces separately. + // Where the block runs as its sublists, the container is the + // transaction's sublist, and the count restarts with each one. + // On testnet's 12,000,000-gas blocks a parallel sublist has + // 1,000,000 and the sequential one 10,000,000; at 10,000,000 or + // less, the parallel sublists have nothing at all. REMASC declares + // no gas and so fits, unless what ran before it has already + // overflowed its container, as in rskj. + // + // The miner places transactions in sublists only after this run, + // so it holds each one to what any placement needs: the block's + // gas less all that ran before it, and the larger sublist's gas. + // rskj's miner, placing as it executes, holds it to the larger + // sublist less what its sequential sublist has used + // (`executeForMiningAfterRSKIP144`). let is_remasc = tx.is_remasc(i, transactions.len()); - if !is_remasc && (whole_block || discard_unfitting.is_some()) { - let tx_gas_limit = tx.gas_limit.saturating_to::(); - let fits = total_gas.saturating_add(tx_gas_limit) <= block_gas_limit; - let sender_dropped = dropped_senders.contains(sender); - if !fits || sender_dropped { + let Some(block_gas) = block_gas else { + // rskj throws reading it (`GasCost.toGas`), and the block is + // not imported. + return Err(ExecutionError::Evm(format!( + "the block's gas limit {} is not gas: eight bytes with the top bit set", + header.gas_limit + ))); + }; + let mining = discard_unfitting.is_some(); + let (container_used, container_limit) = if mining { + (total_gas, block_gas) + } else { + let sublist = sublists + .then(|| crate::collision::sublist_of(i, edges)) + .flatten(); + let since = match container { + Some((running, since)) if running == sublist => since, + _ => { + container = Some((sublist, total_gas)); + total_gas + } + }; + let limit = match (sublists, sublist) { + (false, _) => block_gas, + (true, Some(_)) => parallel_gas, + (true, None) => sequential_gas, + }; + (total_gas - since, limit) + }; + let fits = |gas: u64| { + gas_fits(gas, container_used, container_limit) + && !(mining && sublists && gas > parallel_gas.max(sequential_gas)) + }; + let tx_gas = match tx.gas() { + Some(gas) if fits(gas) && (is_remasc || !dropped_senders.contains(sender)) => gas, + unfit => { + let available = container_limit.saturating_sub(container_used); if let Some(dropped) = discard_unfitting.as_deref_mut() { debug!( tx_index = i, - tx_gas_limit, - available = block_gas_limit.saturating_sub(total_gas), - sender_dropped, + tx_gas = ?unfit, + available, + sender_dropped = dropped_senders.contains(sender), "mining: dropping a transaction that does not fit the block" ); - dropped.unfitting.push(i); + // A gas limit rskj cannot read throws, and its miner + // reports the transaction as invalid for the pool to + // forget (`invalidTransactions`). One that only does + // not fit stays pending. + if unfit.is_some() { + dropped.unfitting.push(i); + } else { + dropped.invalid.push(i); + } dropped_senders.insert(*sender); { use revm::context_interface::ContextTr; @@ -1521,13 +1603,16 @@ impl RskExecutor { } continue; } - return Err(ExecutionError::Evm(format!( - "Too much gas used in this block: available in block: {} tx sent: {}", - block_gas_limit.saturating_sub(total_gas), - tx_gas_limit - ))); + return Err(ExecutionError::Evm(match unfit { + Some(gas) => format!( + "Too much gas used in this block or sublist: available: {available} tx sent: {gas}" + ), + None => format!( + "transaction {i}'s gas limit is not gas: eight bytes with the top bit set" + ), + })); } - } + }; if is_remasc { debug!(tx_index = i, "executing REMASC system call"); @@ -1709,7 +1794,7 @@ impl RskExecutor { + tx.input.iter() .map(|b| if *b == 0 { 4u64 } else { 68 }) .sum::(); - let limit = tx.gas_limit.to::(); + let limit = tx_gas; if limit >= required { // gasUsed may exceed the limit; that is consensus here. (required.saturating_add(basic), true) @@ -1807,7 +1892,7 @@ impl RskExecutor { // An invisible exception charges the full gas limit // (rskj TransactionExecutionSummary.getFee when failed). let fee_gas = if invisible_exception { - tx.gas_limit.to::() + tx_gas } else { gas_used }; @@ -1852,7 +1937,25 @@ impl RskExecutor { continue; } - let tx_env = tx_env_from_rsk_tx(tx, *sender, &self.hardfork_cfg); + let tx_env = match tx_env_from_rsk_tx(tx, *sender, &self.hardfork_cfg) { + Ok(env) => env, + // A gas price this node's EVM cannot carry; see + // `tx_env_from_rsk_tx`. The miner drops it as one the EVM + // refused. + Err(e) if discard_unfitting.is_some() => { + debug!(tx_index = i, error = %e, "mining: dropping a transaction the EVM cannot carry"); + if let Some(drops) = discard_unfitting.as_deref_mut() { + drops.invalid.push(i); + } + dropped_senders.insert(*sender); + { + use revm::context_interface::ContextTr; + evm.ctx.chain_mut().raw_storage.discard_tx(); + } + continue; + } + Err(e) => return Err(e), + }; // Per-tx precompile marker tracking: drained below only if the // transaction commits (rskj cacheTrack rollback drops markers). @@ -2003,7 +2106,7 @@ impl RskExecutor { // Mirror what RskHandler paid REMASC: the full gas limit on an // invisible exception, the spent gas otherwise. let fee_gas = if invisible_flag.load(std::sync::atomic::Ordering::Relaxed) { - tx.gas_limit.to::() + tx_gas } else { gas_used }; @@ -2258,6 +2361,40 @@ pub fn free_bridge_senders(constants: &crate::bridge::constants::BridgeConstants .collect() } +/// rskj `Constants.getMinSequentialSetGasLimit` on the network a chain id +/// names, as [`bridge_constants_for`] picks its Bridge constants. +fn min_sequential_set_gas_limit_for(chain_id: u64) -> u64 { + use rustock_core::config::ChainConfig; + match chain_id { + crate::hardfork::RSK_MAINNET_CHAIN_ID => ChainConfig::mainnet(), + crate::hardfork::RSK_TESTNET_CHAIN_ID => ChainConfig::testnet(), + _ => ChainConfig::regtest(), + } + .min_sequential_set_gas_limit +} + +/// A block's gas limit as rskj reads it, `GasCost.toGas(getGasLimit())`: +/// past eight bytes it is `Long.MAX_VALUE`, and eight bytes with the top +/// bit set make a negative `long`, which rskj refuses by throwing. +pub(crate) fn block_gas_limit(header: &Header) -> Option { + match u64::try_from(header.gas_limit) { + Err(_) => Some(i64::MAX as u64), + Ok(gas) => i64::try_from(gas).is_ok().then_some(gas), + } +} + +/// rskj `TransactionExecutor.gasIsValid`, the half that reads the +/// container: a transaction's gas limit must fit in what the transactions +/// before it left of the container. The sum caps at `Long.MAX_VALUE` +/// (`GasCost.add`), and reaching the cap is refused whatever the container. +/// The other half, that the limit covers the intrinsic cost, is the EVM's +/// own check. +fn gas_fits(tx_gas: u64, used: u64, container: u64) -> bool { + const LONG_MAX: u64 = i64::MAX as u64; + let cumulative = tx_gas.saturating_add(used).min(LONG_MAX); + cumulative <= container && cumulative != LONG_MAX +} + /// Derives an RSK address from a secp256k1 public key in SEC1 hex /// (compressed or uncompressed): keccak256 of the uncompressed point's /// 64 coordinate bytes, last 20 bytes (rskj ECKey.getAddress). @@ -6486,6 +6623,9 @@ mod tests { edges: Some(vec![1, 2]), base_event: None, }); + // Testnet's gas limit: each parallel sublist gets 1,000,000 of it. At + // 10,000,000 or less they would get none, and no block could split. + header.gas_limit = U256::from(12_000_000); let transactions: Vec<(rustock_core::Transaction, Address)> = (0..3) .map(|i| { @@ -6573,6 +6713,7 @@ mod tests { ); let mut header = dummy_header(8_000_000); + header.gas_limit = U256::from(12_000_000); // testnet's, room in every sublist header.version_fields = Some(rustock_core::types::header::HeaderVersionFields { version: 1, edges: Some(vec![1, 2]), @@ -6623,6 +6764,288 @@ mod tests { ); } + /// Transfers of 1,000 wei, each from a sender of its own with the gas + /// limit given, in a testnet block above reed810 -- so one that runs as + /// its sublists -- with `block_gas` and `edges`. + #[allow(clippy::type_complexity)] + fn transfers( + gas_limits: &[u64], + block_gas: u64, + edges: &[i16], + ) -> ( + Header, + Vec<(rustock_core::Transaction, Address)>, + TrieNode, + Arc, + ) { + let store: Arc = Arc::new(MemoryTrieStore::new()); + let mut root = TrieNode::empty(); + let mut transactions = Vec::new(); + for (i, &gas) in gas_limits.iter().enumerate() { + let sender = Address::repeat_byte(0x10 + i as u8); + let rbtc = U256::from(10u64).pow(U256::from(18)); + root = put_account(&root, store.as_ref(), &sender, 0, rbtc); + let tx = rustock_core::Transaction { + nonce: 0, + gas_price: U256::from(1), + gas_limit: U256::from(gas), + to: Bytes::copy_from_slice(Address::repeat_byte(0xA0 + i as u8).as_slice()), + value: U256::from(1_000), + input: Bytes::new(), + v: 0, + r: U256::ZERO, + s: U256::ZERO, + cached_rlp: None, + }; + transactions.push((tx, sender)); + } + let mut header = dummy_header(8_000_000); // testnet reed810 is 7,139,600 + header.gas_limit = U256::from(block_gas); + header.version_fields = Some(rustock_core::types::header::HeaderVersionFields { + version: 1, + edges: Some(edges.to_vec()), + base_event: None, + }); + (header, transactions, root, store) + } + + fn execute( + executor: RskExecutor, + (header, transactions, root, store): ( + Header, + Vec<(rustock_core::Transaction, Address)>, + TrieNode, + Arc, + ), + ) -> Result { + executor.execute_block(&header, &transactions, &root, store) + } + + fn testnet_executor(threads: usize) -> RskExecutor { + let block_store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + RskExecutor::new(RskHardforkConfig::testnet(), block_store).with_execution_threads(threads) + } + + fn too_much_gas(result: Result) -> bool { + matches!(result, Err(e) if e.to_string().contains("Too much gas used in this block or sublist")) + } + + /// rskj `TransactionExecutor.gasIsValid` against the sublist's limit + /// (`BlockUtils.getSublistGasLimit`), not the block's. Testnet's + /// 12,000,000 gas gives each of the two parallel sublists 1,000,000 and + /// the sequential one 10,000,000, on one thread or on two. + #[test] + fn a_transaction_is_held_to_its_own_sublists_gas() { + for threads in [1, 2] { + // Sublist 0 is tx0, sublist 1 is tx1, and tx2 is sequential. + let exact = transfers(&[1_000_000, 1_000_000, 10_000_000], 12_000_000, &[1, 2]); + assert!( + execute(testnet_executor(threads), exact).is_ok(), + "each fills its own sublist exactly ({threads} threads)" + ); + let parallel = transfers(&[1_000_001, 21_000, 21_000], 12_000_000, &[1, 2]); + assert!( + too_much_gas(execute(testnet_executor(threads), parallel)), + "{threads} threads" + ); + // The block's 12,000,000 has room; the sequential sublist does not. + let sequential = transfers(&[21_000, 21_000, 10_000_001], 12_000_000, &[1, 2]); + assert!( + too_much_gas(execute(testnet_executor(threads), sequential)), + "{threads} threads" + ); + } + } + + /// The count restarts with each sublist: what one used is not taken from + /// another's gas, and within one, what ran is. + #[test] + fn each_sublist_counts_only_the_gas_its_own_transactions_used() { + // tx0 and tx1 share parallel sublist 0. tx0 uses 21,000 of its + // 1,000,000, so tx1 may declare 979,000 and no more. + let shared = transfers(&[21_000, 979_000, 21_000], 12_000_000, &[2]); + assert!(execute(testnet_executor(1), shared).is_ok()); + let shared = transfers(&[21_000, 979_001, 21_000], 12_000_000, &[2]); + assert!(too_much_gas(execute(testnet_executor(1), shared))); + // Apart, tx1 has its sublist to itself. + let apart = transfers(&[21_000, 1_000_000, 21_000], 12_000_000, &[1, 2]); + assert!(execute(testnet_executor(1), apart).is_ok()); + } + + /// rskj gives the parallel sublists nothing on a block of 10,000,000 gas + /// or less, testnet's sequential minimum, so such a block cannot split + /// however little its transactions declare. + #[test] + fn at_ten_million_gas_or_less_a_parallel_sublist_holds_nothing() { + let split = transfers(&[21_000, 21_000], 10_000_000, &[1]); + assert!(too_much_gas(execute(testnet_executor(1), split))); + let whole = transfers(&[21_000, 21_000], 10_000_000, &[]); + assert!(execute(testnet_executor(1), whole).is_ok()); + } + + /// Where rskip351 is moved off reed810, rskj runs an RSKIP-144 block as + /// one list (`executeInternal`): the block's gas is the container and the + /// edges decide nothing. + #[test] + fn a_block_run_as_one_list_is_held_to_the_blocks_gas() { + let (moved, _) = RskHardforkConfig::testnet() + .with_rskj_overrides(&[], &[("rskip351".to_string(), None)]) + .unwrap(); + assert!(moved.has_rskip144(8_000_000) && !moved.runs_sublists(8_000_000)); + let block_store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let executor = RskExecutor::new(moved, block_store).with_execution_threads(2); + let block = || transfers(&[2_000_000, 21_000, 21_000], 12_000_000, &[1, 2]); + assert!(execute(executor, block()).is_ok()); + assert!( + too_much_gas(execute(testnet_executor(2), block())), + "as testnet ships" + ); + } + + /// A gas limit past eight bytes reads as rskj reads it, `Long.MAX_VALUE`, + /// which no block's gas fits; eight bytes with the top bit set throw in + /// rskj. Either way the block is refused, and nothing panics. + #[test] + fn a_gas_limit_rskj_reads_past_any_block_is_refused() { + let one = U256::from(1u64); + for (gas_limit, refusal) in [ + (one << 64, "Too much gas used"), + (U256::MAX, "Too much gas used"), + (one << 63, "is not gas"), + ] { + let (header, mut transactions, root, store) = transfers(&[21_000], 12_000_000, &[]); + transactions[0].0.gas_limit = gas_limit; + let err = + execute(testnet_executor(1), (header, transactions, root, store)).unwrap_err(); + assert!(err.to_string().contains(refusal), "{gas_limit}: {err}"); + } + } + + /// A price of 2^128 or more is beyond revm's `TxEnv`. No balance on a real + /// network covers it, so rskj refuses the transaction too; here the block + /// is refused rather than the node panicking. + #[test] + fn a_gas_price_past_the_evm_is_refused() { + let (header, mut transactions, root, store) = transfers(&[21_000], 12_000_000, &[]); + transactions[0].0.gas_price = U256::from(1u64) << 128; + let err = execute(testnet_executor(1), (header, transactions, root, store)).unwrap_err(); + assert!(err.to_string().contains("2^128"), "{err}"); + } + + /// The miner places transactions only after running them, so it holds each + /// to what any placement needs: the block's gas less what ran, and the + /// larger sublist's gas -- on testnet's 12,000,000 the sequential + /// 10,000,000. A gas limit rskj cannot read is reported as invalid, for + /// the pool to forget, and one that only does not fit as unfitting. + #[test] + fn the_miner_drops_what_no_sublist_could_hold() { + let (header, mut transactions, root, store) = + transfers(&[10_000_001, 21_000, 21_000, 21_000], 12_000_000, &[]); + transactions[2].0.gas_limit = U256::from(1u64) << 63; + transactions[3].0.gas_limit = U256::from(1u64) << 64; + let (result, drops) = testnet_executor(1) + .execute_block_for_mining(&header, &transactions, &root, store.clone()) + .unwrap(); + assert_eq!(drops.unfitting, vec![0, 3]); + assert_eq!(drops.invalid, vec![2]); + assert_eq!(result.tx_results.len(), 1); + + // Where the block runs as one list, the block's gas is all there is. + let block_store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let (_, drops) = RskExecutor::new(RskHardforkConfig::mainnet(), block_store) + .execute_block_for_mining(&header, &transactions[..2], &root, store) + .unwrap(); + assert!(drops.is_empty(), "{drops:?}"); + } + + /// The sequential sublist's minimum decides every sublist's gas. Regtest's + /// 6,800,000 leaves a 12,000,000 block's parallel sublists 2,600,000 each, + /// against 1,000,000 under testnet's 10,000,000. + #[test] + fn the_sequential_minimum_sets_the_parallel_sublists_gas() { + let block = || transfers(&[2_600_000, 21_000], 12_000_000, &[1]); + assert!(too_much_gas(execute(testnet_executor(1), block()))); + let executor = testnet_executor(1).with_min_sequential_set_gas_limit(6_800_000); + assert!(execute(executor, block()).is_ok()); + let over = transfers(&[2_600_001, 21_000], 12_000_000, &[1]); + let executor = testnet_executor(1).with_min_sequential_set_gas_limit(6_800_000); + assert!(too_much_gas(execute(executor, over))); + } + + /// rskj's `GasCost`: a gas limit past eight bytes reads as + /// `Long.MAX_VALUE` and one of eight with the top bit set does not read + /// at all; and `gasIsValid` refuses a sum that reaches `Long.MAX_VALUE` + /// (`GasCost.add` caps there) even where the container is no smaller. + #[test] + fn gas_reads_and_fits_as_rskj_counts_it() { + const LONG_MAX: u64 = i64::MAX as u64; + let one = U256::from(1u64); + let header = |gas_limit: U256| Header { + gas_limit, + ..dummy_header(1) + }; + assert_eq!( + block_gas_limit(&header(U256::from(12_000_000))), + Some(12_000_000) + ); + assert_eq!(block_gas_limit(&header((one << 63) - one)), Some(LONG_MAX)); + assert_eq!(block_gas_limit(&header(one << 63)), None); + assert_eq!(block_gas_limit(&header(one << 64)), Some(LONG_MAX)); + assert_eq!(block_gas_limit(&header(U256::MAX)), Some(LONG_MAX)); + + assert!(gas_fits(1_000_000, 0, 1_000_000)); + assert!(!gas_fits(1_000_001, 0, 1_000_000)); + assert!(!gas_fits(1, 1_000_000, 1_000_000)); + assert!(gas_fits(LONG_MAX - 1, 0, LONG_MAX)); + assert!( + !gas_fits(LONG_MAX, 0, LONG_MAX), + "reaching the cap is refused" + ); + assert!(!gas_fits(1, LONG_MAX - 1, LONG_MAX)); + assert!( + !gas_fits(LONG_MAX, LONG_MAX, u64::MAX), + "the sum caps, it does not wrap" + ); + } + + /// Run as one list, rskj credits each fee to REMASC as its transaction + /// ends -- `executeInternal`'s executors do not postpone -- so a later + /// transaction sees it. Run as sublists, the fees wait for the REMASC + /// transaction. Only a transaction that reads REMASC's balance can tell. + #[test] + fn fees_wait_for_remasc_only_where_the_block_runs_as_sublists() { + // BALANCE(REMASC), returned as one word. + let mut code = vec![0x73]; + code.extend_from_slice(crate::precompiles::REMASC_ADDR.as_slice()); + code.extend_from_slice(&[0x31, 0x60, 0x00, 0x52, 0x60, 0x20, 0x60, 0x00, 0xf3]); + let reader = Address::repeat_byte(0xCC); + let seen = |cfg: RskHardforkConfig| -> U256 { + let (header, mut transactions, root, store) = + transfers(&[21_000, 100_000], 12_000_000, &[]); + let root = put_account_with_code(&root, store.as_ref(), &reader, 1, U256::ZERO, &code); + transactions[1].0.to = Bytes::copy_from_slice(reader.as_slice()); + let block_store = + Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let result = RskExecutor::new(cfg, block_store) + .execute_block(&header, &transactions, &root, store) + .unwrap(); + let read = &result.tx_results[1]; + assert!(read.success); + assert_eq!(read.output.len(), 32); + U256::from_be_slice(&read.output) + }; + + assert_eq!(seen(RskHardforkConfig::testnet()), U256::ZERO, "postponed"); + let (moved, _) = RskHardforkConfig::testnet() + .with_rskj_overrides(&[], &[("rskip351".to_string(), None)]) + .unwrap(); + assert_eq!( + seen(moved), + U256::from(21_000), + "tx0's fee, at a gas price of 1" + ); + } + /// The thread setting only decides *how*, never *whether*: a block with /// no sublists runs as one however many threads are allowed. #[test] diff --git a/crates/execution/src/hardfork.rs b/crates/execution/src/hardfork.rs index 736b082..2c987c4 100644 --- a/crates/execution/src/hardfork.rs +++ b/crates/execution/src/hardfork.rs @@ -843,6 +843,21 @@ impl RskHardforkConfig { .is_some_and(|h| block_number >= h) } + /// Whether a block runs as its sublists: rskj's + /// `areActive(RSKIP351, RSKIP144)`, which picks + /// `BlockExecutor.executeParallel` over `executeInternal`. + /// + /// Everything the sublists bring turns on with this, not with RSKIP-144 + /// alone: the fees postponed to the REMASC transaction, each sublist's + /// own gas limit, the edges rule, the collision check, and receipts + /// whose gas restarts at each sublist. The two predicates coincide on + /// every network rskj ships; they part only where a `consensusRules` + /// override moves rskip351 off reed810, and rskj then executes the + /// block as one list, edges or not. + pub fn runs_sublists(&self, block_number: u64) -> bool { + self.has_rskip144(block_number) && self.has_rskip351(block_number) + } + /// RSKIP502: the union bridge's seven Bridge methods (reed810). /// /// Not an ordinal comparison, for `has_rskip144`'s reason: mainnet @@ -958,7 +973,7 @@ fn upgrade_to_spec_id(upgrade: RskNetworkUpgrade) -> SpecId { mod fee_settlement_tests { use super::*; - /// Postponing the gas fee to the REMASC transaction is gated on + /// Postponing the gas fee to the REMASC transaction needs /// RSKIP-144, and RSKIP-144 is unscheduled on mainnet. So every mainnet /// block credits the fee inside the transaction that paid it, as it /// always has, and this change cannot reach the chain this node follows. @@ -1414,6 +1429,21 @@ mod override_tests { assert!(!cfg.has_rskip535(8_000_000)); assert_eq!(cfg.active_upgrade(7_604_200), RskNetworkUpgrade::Vetiver900); } + + /// rskj picks `executeParallel` by RSKIP-351 and RSKIP-144 together. + /// Moving rskip351 alone runs testnet's RSKIP-144 blocks as one list. + #[test] + fn a_block_runs_as_its_sublists_only_under_both_rules() { + let testnet = RskHardforkConfig::testnet(); + assert!(!testnet.runs_sublists(7_139_599)); + assert!(testnet.runs_sublists(7_139_600)); + assert!(!RskHardforkConfig::mainnet().runs_sublists(9_003_580)); + let (moved, _) = RskHardforkConfig::testnet() + .with_rskj_overrides(&[], &[h("rskip351", -1)]) + .unwrap(); + assert!(moved.has_rskip144(8_000_000)); + assert!(!moved.runs_sublists(8_000_000)); + } } /// Every rule this config answers for by predicate, against the height rskj diff --git a/crates/execution/src/mining/template.rs b/crates/execution/src/mining/template.rs index ea1fe8e..ac9520f 100644 --- a/crates/execution/src/mining/template.rs +++ b/crates/execution/src/mining/template.rs @@ -300,7 +300,7 @@ impl BlockTemplateBuilder { // to partition, and the final execution below is the one that counts. let partitioned = if has_candidates { let (kept, partitioned, invalid) = - self.probe_and_partition(number, gas_limit, transactions, &header, &state_root)?; + self.probe_and_partition(number, transactions, &header, &state_root)?; transactions = kept; header.transactions_root = ordered_tx_trie_root(&transactions, rskip126); // rskj `BlockToMineBuilder.build`: "Evicting {} invalid @@ -368,8 +368,9 @@ impl BlockTemplateBuilder { }) } - /// Execute the candidates once, keep the ones that fit, and under - /// RSKIP-144 place the kept ones in sublists, as rskj's miner does. + /// Execute the candidates once, keep the ones that fit, and where the + /// block runs as its sublists place the kept ones in them, as rskj's + /// miner does. /// /// `transactions` is the selection in price order, REMASC last. The /// first element of the result is what survived: every candidate the @@ -377,11 +378,11 @@ impl BlockTemplateBuilder { /// being refused by the EVM (see /// [`crate::RskExecutor::execute_block_for_mining`]), is gone, in the /// order the rest already had. The second is the partition, `None` when - /// the block declares none: RSKIP-144 inactive, or the partition came - /// out all-sequential, which rskj spells as an empty edges array and we - /// spell as no extension at all. The third is the indices, into - /// `transactions`, of the candidates the EVM refused: the pool should - /// hear about those. + /// the block declares none: it does not run as its sublists, or the + /// partition came out all-sequential, which rskj spells as an empty + /// edges array and we spell as no extension at all. The third is the + /// indices, into `transactions`, of the candidates the EVM refused: the + /// pool should hear about those. /// /// The placement is an approximation on purpose: a transaction is placed /// from an execution against the accumulating sequential state, while @@ -391,12 +392,11 @@ impl BlockTemplateBuilder { fn probe_and_partition( &self, number: u64, - gas_limit: U256, transactions: Vec, header: &Header, state_root: &TrieNode, ) -> Result<(Vec, Option, Vec), TemplateError> { - let rskip144 = self.hardfork_cfg.has_rskip144(number); + let sublists = self.hardfork_cfg.runs_sublists(number); let chain_id = self.hardfork_cfg.chain_id; let with_senders: Vec<(Transaction, Address)> = transactions .iter() @@ -406,7 +406,7 @@ impl BlockTemplateBuilder { }) .collect(); - if rskip144 { + if sublists { crate::collision::start_per_transaction(crate::collision::write_semantics(number)); } let executed = self.processor.executor().execute_block_for_mining( @@ -417,7 +417,11 @@ impl BlockTemplateBuilder { ); // Taken whether or not execution succeeded, so a failure cannot leave // this probe's keys behind for the next template. - let touched = if rskip144 { crate::collision::finish() } else { None }; + let touched = if sublists { + crate::collision::finish() + } else { + None + }; let (executed, dropped) = executed?; if !dropped.is_empty() { tracing::debug!( @@ -441,14 +445,22 @@ impl BlockTemplateBuilder { debug_assert_eq!(kept.len(), executed.tx_results.len(), "one result per executed transaction"); let kept_transactions: Vec = kept.iter().map(|(_, tx, _)| tx.clone()).collect(); - if !rskip144 { + if !sublists { return Ok((kept_transactions, None, invalid)); } let touched = touched.unwrap_or_default(); + // The probe has just read the block's gas limit as rskj reads it, + // and one it could not read failed the probe. + let block_gas = crate::executor::block_gas_limit(header).ok_or_else(|| { + crate::executor::ExecutionError::Evm(format!( + "the block's gas limit {} is not gas", + header.gas_limit + )) + })?; let mut partitioner = crate::mining::partition::SublistPartitioner::new( rustock_core::validation::block_rules::TX_EXECUTION_THREADS, - gas_limit.saturating_to::(), + block_gas, self.chain_config.min_sequential_set_gas_limit, ); @@ -467,10 +479,14 @@ impl BlockTemplateBuilder { continue; } + // rskj places by the gas limit as gas (`GasCost.toGas`). Every + // transaction the probe kept reads as gas: one that does not was + // dropped as invalid. + let Some(gas_limit) = tx.gas() else { continue }; let keys = touched.keys(*probe_index); let footprint = crate::mining::partition::TxFootprint { sender: *sender, - gas_limit: tx.gas_limit.saturating_to::(), + gas_limit, gas_used: result.gas_used, reads: keys.map(|k| k.read().iter().cloned().collect()).unwrap_or_default(), writes: keys.map(|k| k.written().iter().cloned().collect()).unwrap_or_default(), diff --git a/crates/execution/src/mining/workload.rs b/crates/execution/src/mining/workload.rs index bb00e95..e9a1fdd 100644 --- a/crates/execution/src/mining/workload.rs +++ b/crates/execution/src/mining/workload.rs @@ -579,11 +579,18 @@ impl Workload { chain_config.chain_id = self.chain_id as u8; chain_config.min_difficulty = U256::from(1); chain_config.max_gas_limit = block_gas_limit.max(1); - let processor = Arc::new(BlockProcessor::new(hardfork_cfg.clone(), block_store.clone())); + // The miner and the validator on the same constants: the sequential + // sublist's minimum sets every sublist's gas limit, which the + // validator holds the miner's partition to. + let chain_config = Arc::new(chain_config); + let processor = Arc::new( + BlockProcessor::new(hardfork_cfg.clone(), block_store.clone()) + .with_chain_config(chain_config.clone()), + ); let builder = BlockTemplateBuilder::new( processor, trie_store.clone(), - Arc::new(chain_config), + chain_config.clone(), hardfork_cfg.clone(), MiningConfig { coinbase_address: Address::repeat_byte(0xC0), @@ -606,7 +613,8 @@ impl Workload { .map(|i| crate::collision::sublist_of(i, &edges)) .collect(); - let validator = BlockProcessor::new(hardfork_cfg, block_store); + let validator = + BlockProcessor::new(hardfork_cfg, block_store).with_chain_config(chain_config); let parent_state = builder .load_state_root(parent.state_root) .map_err(|e| format!("loading the parent state: {e}"))?; diff --git a/crates/execution/src/processor.rs b/crates/execution/src/processor.rs index fdcbc51..f1ac808 100644 --- a/crates/execution/src/processor.rs +++ b/crates/execution/src/processor.rs @@ -204,8 +204,12 @@ impl BlockProcessor { /// The chain's consensus constants, for a node whose chain is not exactly /// the one its chain id names: the configuration the rest of the node - /// validates headers with. + /// validates headers with, and whose sequential-sublist minimum sets the + /// gas limit of every sublist the executor holds transactions to. pub fn with_chain_config(mut self, config: Arc) -> Self { + self.executor = self + .executor + .with_min_sequential_set_gas_limit(config.min_sequential_set_gas_limit); self.chain_config = config; self } @@ -360,23 +364,30 @@ impl BlockProcessor { // receipts root alone, which is how testnet #7,690,876 showed up. // Every earlier block verified because none of them actually split: // the edges field is present and empty from reed810 until here. - let sublist_starts: &[i16] = header - .version_fields - .as_ref() - .and_then(|v| v.edges.as_deref()) - .unwrap_or(&[]); + // + // The edges count only where the block runs as its sublists + // (`runs_sublists`): rskj executes any other block as one list and + // never reads them. + let sublist_starts: &[i16] = if self.hardfork_cfg.runs_sublists(header.number) { + header + .version_fields + .as_ref() + .and_then(|v| v.edges.as_deref()) + .unwrap_or(&[]) + } else { + &[] + }; // The sublists also have to be *independent*, and that is a rule the // validator applies rather than a property it may assume. Recording // is started only for a block that actually splits, so every block // mainnet has produced -- reed810 is unscheduled there, and the field // is absent or empty throughout -- pays nothing for it. - let check_collisions = - self.hardfork_cfg.has_rskip144(header.number) && !sublist_starts.is_empty(); + let check_collisions = !sublist_starts.is_empty(); if check_collisions { // No address is excluded on account of fees any more. The fees - // are postponed whenever RSKIP-144 is active -- which is whenever - // this check runs -- and settled once at the REMASC transaction, + // are postponed whenever the block runs as its sublists -- which + // is whenever this check runs -- and settled once at the REMASC transaction, // which is in the sequential set. So no parallel sublist writes // REMASC's account cell on their account, and excluding it would // only hide a genuine transfer to the Bridge's counterpart that @@ -416,7 +427,7 @@ impl BlockProcessor { // whatever the call returned. Checked for every block that declares a // partition, not only one this node ran in parallel: it is a property // of the block, and running it on one thread does not make it true. - if !sublist_starts.is_empty() && self.hardfork_cfg.has_rskip144(header.number) { + if !sublist_starts.is_empty() { for (index, tx_result) in exec_result.tx_results.iter().enumerate() { if !tx_result.reached_sequential_only { continue; @@ -765,8 +776,8 @@ impl BlockProcessor { /// The rules of [`Self::validate_block_rules`] that read only the block /// and its parent: the transaction gas price rules, REMASC's presence, - /// the extra data size and the minimum gas price range. No ancestry, no - /// merged-mining fields. + /// the extra data size, the sublist edges and the minimum gas price + /// range. No ancestry, no merged-mining fields. /// /// Split out for the miner, which runs them on the block it assembled /// before handing out work: these are the rules a block could break @@ -780,8 +791,8 @@ impl BlockProcessor { parent: Option<&rustock_core::Header>, ) -> Result<(), ValidationError> { use rustock_core::validation::block_rules::{ - BlockTxsMaxGasPriceRule, ExtraDataRule, PrevMinGasPriceRule, RemascValidationRule, - TxsMinGasPriceRule, + validate_tx_execution_sublists_edges, BlockTxsMaxGasPriceRule, ExtraDataRule, + PrevMinGasPriceRule, RemascValidationRule, TxsMinGasPriceRule, }; use rustock_core::validation::{BlockValidator, HeaderValidator, ParentHeaderValidator}; @@ -796,6 +807,19 @@ impl BlockProcessor { RemascValidationRule.validate_block(block)?; // rskj ExtraDataRule -- unconditional, 32 bytes. ExtraDataRule::default().validate(&block.header)?; + // rskj ValidTxExecutionSublistsEdgesRule -- where the block runs as + // its sublists. Without it a block could partition itself in a way + // rskj refuses: edges out of order, past the end, or more of them + // than there are execution threads. + validate_tx_execution_sublists_edges( + block + .header + .version_fields + .as_ref() + .and_then(|v| v.edges.as_deref()), + block.transactions.len(), + self.hardfork_cfg.runs_sublists(block.header.number), + )?; if let Some(parent) = parent { // rskj PrevMinGasPriceRule -- unconditional. @@ -1556,6 +1580,8 @@ mod tests { sign_tx(&mut tx, &key, chain_id); let mut header = dummy_header(8_000_000); + // Testnet's gas limit, so the parallel sublist has gas to hold it. + header.gas_limit = U256::from(12_000_000); header.version_fields = Some(rustock_core::types::header::HeaderVersionFields { version: 1, edges: Some(vec![1, 2]), @@ -1566,12 +1592,186 @@ mod tests { let processor = BlockProcessor::new(RskHardforkConfig::testnet(), block_store); let out = processor.execute_block(&block, &root, store); assert!( - !matches!( - out, - Err(ProcessError::SequentialOnlyPrecompileInParallelSublist { .. }) - ), - "a plain transfer reaches no precompile" + out.is_ok(), + "a plain transfer reaches no precompile: {:?}", + out.err() + ); + } + + /// rskj `ValidTxExecutionSublistsEdgesRule` runs with the block's other + /// body rules, where the block runs as its sublists. A REMASC transaction + /// alone cannot be given a parallel sublist: the last transaction must + /// stay sequential. + #[test] + fn the_sublist_edges_are_checked_with_the_body_rules() { + let block = |edges: Option>| { + let mut header = dummy_header(8_000_000); + header.version_fields = Some(rustock_core::types::header::HeaderVersionFields { + version: 1, + edges, + base_event: None, + }); + Block { + header, + transactions: vec![Transaction::remasc(8_000_000)], + ommers: vec![], + } + }; + let processor = |cfg: RskHardforkConfig| { + let block_store = + Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + BlockProcessor::new(cfg, block_store) + }; + + let testnet = processor(RskHardforkConfig::testnet()); + assert!(matches!( + testnet.validate_block_body_rules(&block(Some(vec![1])), None), + Err(ValidationError::InvalidSublistEdges { .. }) + )); + assert_eq!( + testnet.validate_block_body_rules(&block(Some(vec![])), None), + Ok(()) ); + assert_eq!( + testnet.validate_block_body_rules(&block(None), None), + Ok(()) + ); + + // With rskip351 moved off reed810, rskj runs the block as one list and + // never reads its edges. + let (moved, _) = RskHardforkConfig::testnet() + .with_rskj_overrides(&[], &[("rskip351".to_string(), None)]) + .unwrap(); + assert_eq!( + processor(moved).validate_block_body_rules(&block(Some(vec![1])), None), + Ok(()) + ); + } + + /// The chain configuration's sequential minimum reaches the executor. On + /// regtest's 6,800,000, a 12,000,000-gas block's parallel sublists hold + /// 2,600,000 each; testnet's 10,000,000 leaves them 1,000,000. + #[test] + fn the_chain_configuration_sets_the_sublists_gas() { + let store: Arc = Arc::new(MemoryTrieStore::new()); + let key = SigningKey::from_slice(&[5u8; 32]).unwrap(); + let sender = sender_address(&key); + let root = put_account( + &TrieNode::empty(), + store.as_ref(), + &sender, + 0, + U256::from(10u64).pow(U256::from(18)), + ); + let mut tx = Transaction { + nonce: 0, + gas_price: U256::from(0), + gas_limit: U256::from(2_600_000), + to: Bytes::copy_from_slice(Address::repeat_byte(0xBB).as_slice()), + value: U256::from(1), + input: Bytes::new(), + v: 0, + r: U256::ZERO, + s: U256::ZERO, + cached_rlp: None, + }; + sign_tx(&mut tx, &key, 31); + let mut header = dummy_header(8_000_000); + header.gas_limit = U256::from(12_000_000); + header.version_fields = Some(rustock_core::types::header::HeaderVersionFields { + version: 1, + edges: Some(vec![1]), + base_event: None, + }); + let block = Block { + header, + transactions: vec![tx], + ommers: vec![], + }; + let processor = || { + let block_store = + Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + BlockProcessor::new(RskHardforkConfig::testnet(), block_store) + }; + + let err = processor() + .execute_block(&block, &root, store.clone()) + .unwrap_err(); + assert!( + err.to_string() + .contains("Too much gas used in this block or sublist"), + "{err}" + ); + let regtest_minimum = Arc::new(rustock_core::config::ChainConfig { + min_sequential_set_gas_limit: 6_800_000, + ..rustock_core::config::ChainConfig::testnet() + }); + let out = processor() + .with_chain_config(regtest_minimum) + .execute_block(&block, &root, store); + assert!(out.is_ok(), "{:?}", out.err()); + } + + /// A receipt's cumulative gas restarts with each sublist where the block + /// runs as its sublists, and nowhere else: with rskip351 moved off + /// reed810, rskj runs the same block as one list and counts across it. + #[test] + fn receipts_restart_their_gas_only_where_the_block_runs_as_sublists() { + let store: Arc = Arc::new(MemoryTrieStore::new()); + let mut root = TrieNode::empty(); + let mut transactions = Vec::new(); + for seed in [3u8, 4] { + let key = SigningKey::from_slice(&[seed; 32]).unwrap(); + let sender = sender_address(&key); + let rbtc = U256::from(10u64).pow(U256::from(18)); + root = put_account(&root, store.as_ref(), &sender, 0, rbtc); + let mut tx = Transaction { + nonce: 0, + gas_price: U256::from(0), + gas_limit: U256::from(21_000), + to: Bytes::copy_from_slice(Address::repeat_byte(seed).as_slice()), + value: U256::from(1), + input: Bytes::new(), + v: 0, + r: U256::ZERO, + s: U256::ZERO, + cached_rlp: None, + }; + sign_tx(&mut tx, &key, 31); + transactions.push(tx); + } + let mut header = dummy_header(8_000_000); + header.gas_limit = U256::from(12_000_000); + header.version_fields = Some(rustock_core::types::header::HeaderVersionFields { + version: 1, + edges: Some(vec![1]), + base_event: None, + }); + let block = Block { + header, + transactions, + ommers: vec![], + }; + let cumulative = |cfg: RskHardforkConfig| -> Vec { + let block_store = + Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + BlockProcessor::new(cfg, block_store) + .execute_block(&block, &root, store.clone()) + .unwrap() + .receipts + .iter() + .map(|r| r.cumulative_gas_used) + .collect() + }; + + assert_eq!( + cumulative(RskHardforkConfig::testnet()), + vec![21_000, 21_000] + ); + let (moved, _) = RskHardforkConfig::testnet() + .with_rskj_overrides(&[], &[("rskip351".to_string(), None)]) + .unwrap(); + assert_eq!(cumulative(moved), vec![21_000, 42_000]); } /// A block whose first transaction calls the Bridge, with `edges` as @@ -1611,6 +1811,8 @@ mod tests { sign_tx(&mut tx, &key, chain_id); let mut header = dummy_header(8_000_000); + // Testnet's gas limit, so a parallel sublist has gas to hold the call. + header.gas_limit = U256::from(12_000_000); if let Some(edges) = edges { header.version_fields = Some(rustock_core::types::header::HeaderVersionFields { version: 1, diff --git a/crates/execution/src/rsk_handler.rs b/crates/execution/src/rsk_handler.rs index 9dac769..3905a03 100644 --- a/crates/execution/src/rsk_handler.rs +++ b/crates/execution/src/rsk_handler.rs @@ -78,11 +78,11 @@ invisible_exception: Arc, /// Accumulate the fee instead of crediting it to the beneficiary. /// /// rskj builds every sublist executor with `postponeFeePayment = true` - /// once RSKIP-144 is active, and settles the block's whole fee total into - /// REMASC just before the REMASC transaction - /// (`TransactionListExecutor.addFeesToRemascIfEnabled`). Before RSKIP-144 - /// it credits per transaction (`postponeFeePayment = false`), which is - /// what this node always did. + /// where a block runs as its sublists (RSKIP-144 with RSKIP-351), and + /// settles the block's whole fee total into REMASC just before the REMASC + /// transaction (`TransactionListExecutor.addFeesToRemascIfEnabled`). Run + /// as one list it credits per transaction (`postponeFeePayment = false`), + /// which is what this node always did. /// /// The end state is the same either way -- the same total reaches the /// same account before anything can observe it, since REMASC is the last @@ -236,7 +236,7 @@ where use revm::context_interface::{Block, Transaction}; if self.postpone_fees { // The executor accumulates the same total and settles it at the - // REMASC transaction, as rskj does once RSKIP-144 is active. + // REMASC transaction, as rskj does where a block runs as its sublists. return Ok(()); } if !self.invisible_exception.load(Ordering::Relaxed) { From ab8656ac875720b341b78b2da3d9bbf050b191e5 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Mon, 9 Nov 2026 03:55:30 -0800 Subject: [PATCH 050/264] execution: modexp priced and run as rskj's, lengths read as Java ints rskj's ModExp (PrecompiledContracts.BigIntegerModexp) is EIP-198's price and arithmetic, but it reads each length as a Java int: DataWord.intValueSafe takes 2^31 or more as Integer.MAX_VALUE. Its execute answers nothing where an ArithmeticException ends it: an offset past an int (Math.addExact), or an argument past what a BigInteger holds, 2^31 - 1 bits. rustock ran revm's EIP-198 modexp, which takes each length at its full width, so the two priced and answered differently once a length needed 31 bits. No call reaches the difference below about 10^8 gas. The cheapest is an exponent of 2^28 bytes with its top bit set and a one-byte base and modulus, 107,374,182 gas: rskj answers nothing, revm the result. An exponent of 2^32 bytes rskj prices as one of 2^31 - 1 bytes, 858,993,458 gas, and answers nothing; revm prices it at about twice that and runs out of gas. A base or modulus no int holds costs at least 1.44 x 10^16. A call has at most its block's gas, and blocks hold millions, so no block yet comes near; but a block's gas limit may rise by just under 1/1024 each block, with no ceiling. ModExp is now rskj's, step by step beside its source: the price from the lengths as ints, capped at Long.MAX_VALUE (GasCost.toGas); nothing for a zero base and modulus length, an offset past an int, or an argument past a BigInteger's bits, each measured before anything is read; zeros for a zero modulus; and otherwise revm's arithmetic, left-padded to the modulus's length. rustock's mapping of revm's EIP-7823 size error to out of gas, which stood in for rskj's price at an int's maximum, goes with it. Tests: EIP-198's examples; 300 generated inputs within rskj's limits, with lengths each side of 64 and of 1,024 bytes and inputs cut short, priced and answered as revm's modexp prices and answers them, and out of gas one below the price; a length read as an int; the 1.44 x 10^16 floor and the Long.MAX_VALUE cap; and the empty answers, for an exponent and a base of 2^28 bytes either side of a BigInteger's limit and for an offset past an int. Dropping the int reading, the offset check, the BigInteger limit or the cap, or changing EIP-198's complexity, each fails a test. --- crates/execution/src/precompiles.rs | 344 +++++++++++++++++++++++++++- 1 file changed, 333 insertions(+), 11 deletions(-) diff --git a/crates/execution/src/precompiles.rs b/crates/execution/src/precompiles.rs index 32735be..64db874 100644 --- a/crates/execution/src/precompiles.rs +++ b/crates/execution/src/precompiles.rs @@ -185,6 +185,166 @@ fn ecrecover(input: &[u8]) -> Option<[u8; 32]> { Some(word) } +/// ModExp's address, 0x05. +const MODEXP_ADDR: Address = Address::new(hex_addr("0000000000000000000000000000000000000005")); + +/// rskj's ModExp (`PrecompiledContracts.BigIntegerModexp`): EIP-198's price +/// and arithmetic, with the lengths read as rskj reads them. +/// +/// revm's modexp takes a length at its full width. rskj takes it as a Java +/// int (`DataWord.intValueSafe`), so that 2^31 or more counts as +/// `Integer.MAX_VALUE`, and answers nothing when an offset passes an int +/// (`Math.addExact`) or an argument passes what a `BigInteger` holds, 2^31 - 1 +/// bits. The two agree on every call that affords less than about 10^8 gas: +/// the cheapest call past those limits, an exponent of 2^28 bytes with a +/// one-byte base and modulus, costs 107,374,182. +fn rsk_modexp_run(input: &[u8], gas_limit: u64) -> Result { + let gas = modexp::gas_for_data(input); + if gas > gas_limit { + return Err(PrecompileError::OutOfGas); + } + Ok(PrecompileOutput::new(gas, modexp::execute(input)?.into())) +} + +/// rskj's `BigIntegerModexp`, step by step. +mod modexp { + use revm::precompile::PrecompileError; + + /// Where the base starts, after the three lengths. + const ARGS_OFFSET: i32 = 96; + + /// The most bits a `BigInteger` holds. + const BIG_INTEGER_BITS: u64 = i32::MAX as u64; + + /// `getGasForData`: EIP-198's price, at most `Long.MAX_VALUE` + /// (`GasCost.toGas`). + pub(super) fn gas_for_data(data: &[u8]) -> u64 { + let [base_len, exp_len, mod_len] = lengths(data); + let mult_complexity = mult_complexity(base_len.max(mod_len) as u64); + let exp_high_bytes = match ARGS_OFFSET.checked_add(base_len) { + Some(offset) => parse_bytes(data, offset, exp_len.min(32)), + None => Vec::new(), + }; + let adjusted = adjusted_exponent_length(&exp_high_bytes, exp_len); + let gas = u128::from(mult_complexity) * u128::from(adjusted.max(1)) / 20; + gas.min(i64::MAX as u128) as u64 + } + + /// `execute`: the result left-padded to the modulus's length, or nothing + /// where an `ArithmeticException` ends it. + pub(super) fn execute(data: &[u8]) -> Result, PrecompileError> { + let [base_len, exp_len, mod_len] = lengths(data); + if base_len == 0 && mod_len == 0 { + return Ok(Vec::new()); + } + // `Math.addExact` throws past an int. + let Some(exp_offset) = ARGS_OFFSET.checked_add(base_len) else { + return Ok(Vec::new()); + }; + let Some(mod_offset) = exp_offset.checked_add(exp_len) else { + return Ok(Vec::new()); + }; + let args = [ + (ARGS_OFFSET, base_len), + (exp_offset, exp_len), + (mod_offset, mod_len), + ]; + // `new BigInteger(1, bytes)` throws past its bits. rskj reads each + // argument whole first; here each is measured before any is read. + if args + .iter() + .any(|&(offset, len)| bit_length(data, offset, len) > BIG_INTEGER_BITS) + { + return Ok(Vec::new()); + } + let mut answer = vec![0; mod_len as usize]; + // A modulus of zero answers zeros. + if bit_length(data, mod_offset, mod_len) == 0 { + return Ok(answer); + } + let [base, exp, modulus] = args.map(|(offset, len)| parse_bytes(data, offset, len)); + let result = revm::precompile::crypto().modexp(&base, &exp, &modulus)?; + let result = &result[result.iter().take_while(|&&byte| byte == 0).count()..]; + let Some(start) = answer.len().checked_sub(result.len()) else { + return Err(PrecompileError::Fatal( + "modexp answered more than its modulus holds".into(), + )); + }; + answer[start..].copy_from_slice(result); + Ok(answer) + } + + /// `parseLen` of the three lengths, each word read as + /// `DataWord.intValueSafe` reads it: 2^31 or more is `Integer.MAX_VALUE`. + fn lengths(data: &[u8]) -> [i32; 3] { + [0, 1, 2].map(|index| { + let mut word = [0u8; 32]; + let bytes = parse_bytes(data, 32 * index, 32); + word[..bytes.len()].copy_from_slice(&bytes); + let low = i32::from_be_bytes([word[28], word[29], word[30], word[31]]); + if word[..28].iter().any(|&byte| byte != 0) || low < 0 { + i32::MAX + } else { + low + } + }) + } + + /// `ByteUtil.parseBytes`: `len` bytes of `data` from `offset`, zeros past + /// its end, and none at all from past its end or for `len` zero. + fn parse_bytes(data: &[u8], offset: i32, len: i32) -> Vec { + let (offset, len) = (offset as usize, len as usize); + if offset >= data.len() || len == 0 { + return Vec::new(); + } + let mut bytes = vec![0; len]; + let present = (data.len() - offset).min(len); + bytes[..present].copy_from_slice(&data[offset..offset + present]); + bytes + } + + /// How many bits the number `parse_bytes(data, offset, len)` reads holds, + /// found without reading it: its highest byte is one of `data`'s, the rest + /// being the zeros it is padded with. + fn bit_length(data: &[u8], offset: i32, len: i32) -> u64 { + let (offset, len) = (offset as usize, len as usize); + let present = data.get(offset..).unwrap_or_default(); + let present = &present[..present.len().min(len)]; + match present.iter().position(|&byte| byte != 0) { + None => 0, + Some(i) => 8 * (len - i) as u64 - u64::from(present[i].leading_zeros()), + } + } + + /// `getMultComplexity`. `x` is at most 2^31 - 1, so `x * x` fits, as in + /// rskj's long arithmetic. + fn mult_complexity(x: u64) -> u64 { + let x2 = x * x; + if x <= 64 { + x2 + } else if x <= 1024 { + x2 / 4 + 96 * x - 3072 + } else { + x2 / 16 + 480 * x - 199_680 + } + } + + /// `getAdjustedExponentLength`: the index of the highest bit among the + /// exponent's first 32 bytes, and 8 for each byte after them. + fn adjusted_exponent_length(exp_high_bytes: &[u8], exp_len: i32) -> u64 { + let leading_zeros = match exp_high_bytes.iter().position(|&byte| byte != 0) { + None => 8 * exp_high_bytes.len(), + Some(i) => 8 * i + exp_high_bytes[i].leading_zeros() as usize, + }; + let highest_bit = (8 * exp_high_bytes.len() - leading_zeros).saturating_sub(1) as u64; + if exp_len <= 32 { + highest_bit + } else { + 8 * (exp_len as u64 - 32) + highest_bit + } + } +} + /// Secp256k1 point addition. /// Input: up to 128 bytes = [x1(32) | y1(32) | x2(32) | y2(32)], big-endian. /// Output: 64 bytes = [x(32) | y(32)]. @@ -295,9 +455,8 @@ pub fn rsk_precompiles(hardfork_cfg: &RskHardforkConfig, block_number: u64) -> P // rskj gates each precompile on its own RSKIP rather than Ethereum spec // buckets (PrecompiledContracts.getContractForAddress). From genesis: - // 0x01-0x04 (ecrecover, sha256, ripemd160, identity) plus 0x05 modexp - // with EIP-198 (Byzantium) pricing. ECRecover is rskj's own, in place of - // revm's. + // 0x01-0x04 (ecrecover, sha256, ripemd160, identity) plus 0x05 modexp, + // EIP-198's. ECRecover and ModExp are rskj's own, in place of revm's. let mut precompiles = Precompiles::new(PrecompileSpecId::HOMESTEAD).clone(); precompiles.extend([ Precompile::new( @@ -305,7 +464,11 @@ pub fn rsk_precompiles(hardfork_cfg: &RskHardforkConfig, block_number: u64) -> P ECRECOVER_ADDR, rsk_ecrecover_run, ), - revm::precompile::modexp::BYZANTIUM, + Precompile::new( + PrecompileId::custom("rsk-modexp"), + MODEXP_ADDR, + rsk_modexp_run, + ), ]); // RSKIP137 (Papyrus200): BN128 add/mul/pairing, priced per EIP-1108 @@ -1119,13 +1282,7 @@ fn run_precompile( output: output.bytes, }, Err(PrecompileError::Fatal(reason)) => Outcome::Fatal(reason), - // rskj's ModExp reads a length no int holds as `Integer.MAX_VALUE` - // (`DataWord.intValueSafe`), which prices the call above 10^16 gas. - Err(error) - if error.is_oog() || matches!(error, PrecompileError::ModexpEip7823LimitSize) => - { - Outcome::OutOfGas - } + Err(error) if error.is_oog() => Outcome::OutOfGas, Err(error) => match required { // `BN128PrecompiledContract.safeExecute`: before RSKIP197 a point // BN128 refuses gets an answer with no bytes. @@ -1333,6 +1490,171 @@ mod tests { ); } + // ----------------------------------------------------------------------- + // ModExp, as rskj's BigIntegerModexp + // ----------------------------------------------------------------------- + + /// A modexp input: the base, exponent and modulus lengths as words, then + /// `rest`. + fn modexp_input(lengths: [alloy_primitives::U256; 3], rest: &[u8]) -> Vec { + let mut input: Vec = lengths + .iter() + .flat_map(|len| len.to_be_bytes::<32>()) + .collect(); + input.extend_from_slice(rest); + input + } + + fn run_modexp(input: &[u8], gas: u64) -> Result { + rsk_precompiles(&RskHardforkConfig::mainnet(), 0) + .get(&MODEXP_ADDR) + .expect("modexp is a genesis precompile") + .execute(input, gas) + } + + /// EIP-198's examples: 3^(p - 1) mod p is 1 for secp256k1's prime p, and + /// 0^(p - 1) mod p is 0, each for 13,056 gas -- a 32-byte modulus, + /// squared, times the exponent's top bit, 255, over 20. + #[test] + fn modexp_answers_eip198s_examples() { + use alloy_primitives::{hex, U256}; + let exponent = hex!("fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2e"); + let p = hex!("fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f"); + let (one, thirty_two) = (U256::from(1), U256::from(32)); + + let input = modexp_input( + [one, thirty_two, thirty_two], + &[&[3u8][..], &exponent[..], &p[..]].concat(), + ); + let mut answer = [0u8; 32]; + answer[31] = 1; + let out = run_modexp(&input, 13_056).unwrap(); + assert_eq!((out.gas_used, out.bytes.as_ref()), (13_056, &answer[..])); + + let input = modexp_input( + [U256::ZERO, thirty_two, thirty_two], + &[exponent, p].concat(), + ); + let out = run_modexp(&input, 13_056).unwrap(); + assert_eq!((out.gas_used, out.bytes.as_ref()), (13_056, &[0u8; 32][..])); + assert!(matches!( + run_modexp(&input, 13_055), + Err(PrecompileError::OutOfGas) + )); + } + + /// Where every length fits an int and every argument a `BigInteger`, + /// rskj's ModExp is EIP-198's, revm's: the same price and the same + /// answer, for lengths each side of 64 and of 1,024 bytes and inputs cut + /// short of what their lengths declare. + #[test] + fn modexp_is_eip198s_within_rskjs_limits() { + use alloy_primitives::U256; + let mut seed = 0x9e37_79b9_7f4a_7c15_u64; + let mut next = move || { + seed ^= seed << 13; + seed ^= seed >> 7; + seed ^= seed << 17; + seed + }; + for case in 0..300 { + let mut len = || { + if next() % 20 == 0 { + 1_000 + next() % 100 + } else { + next() % 70 + } + }; + let lengths = [len(), len(), len()]; + let declared: u64 = lengths.iter().sum(); + let rest: Vec = (0..next() % (declared + 16)) + .map(|_| next() as u8) + .collect(); + let input = modexp_input(lengths.map(U256::from), &rest); + + let ours = run_modexp(&input, u64::MAX).unwrap(); + let eip198 = revm::precompile::modexp::byzantium_run(&input, u64::MAX).unwrap(); + assert_eq!( + (ours.gas_used, &ours.bytes), + (eip198.gas_used, &eip198.bytes), + "case {case}: lengths {lengths:?}" + ); + if ours.gas_used > 0 { + let short = run_modexp(&input, ours.gas_used - 1); + assert!( + matches!(short, Err(PrecompileError::OutOfGas)), + "case {case}" + ); + } + } + } + + /// rskj reads a length as a Java int, one of 2^31 or more as + /// `Integer.MAX_VALUE`. An exponent 2^32 bytes long is priced as one of + /// 2^31 - 1 bytes, and with a one-byte base and modulus the call answers + /// nothing, the modulus starting past an int. revm prices the length it + /// was given, and runs out of gas. + #[test] + fn modexp_reads_a_length_as_a_java_int() { + use alloy_primitives::U256; + let one = U256::from(1); + let input = modexp_input([one, one << 32, one], &[2, 1]); + // The exponent's top bit among its first 32 bytes is bit 248: + // 8 * (2^31 - 1 - 32) + 248 = 17,179,869,168, over 20. + let out = run_modexp(&input, 858_993_458).unwrap(); + assert_eq!((out.gas_used, out.bytes.len()), (858_993_458, 0)); + assert!(matches!( + run_modexp(&input, 858_993_457), + Err(PrecompileError::OutOfGas) + )); + assert!(matches!( + revm::precompile::modexp::byzantium_run(&input, 858_993_458), + Err(PrecompileError::OutOfGas) + )); + + // A base no int holds: (2^31 - 1)^2 / 16 + 480 (2^31 - 1) - 199,680, + // over 20. No call with such a base or modulus costs less. + let input = modexp_input([one << 255, U256::ZERO, U256::ZERO], &[]); + let out = run_modexp(&input, 14_411_570_333_761_358).unwrap(); + assert_eq!((out.gas_used, out.bytes.len()), (14_411_570_333_761_358, 0)); + // The price stops at Long.MAX_VALUE. + let input = modexp_input([U256::MAX; 3], &[]); + assert_eq!( + run_modexp(&input, u64::MAX).unwrap().gas_used, + i64::MAX as u64 + ); + } + + /// rskj's `execute` answers nothing for an `ArithmeticException`: an + /// offset past an int (`Math.addExact`), or an argument past what a + /// `BigInteger` holds, 2^31 - 1 bits. The cheapest call to reach either + /// is an exponent of 2^28 bytes whose top bit is set, 107,374,182 gas; + /// with the bit below it the exponent fits, and the zero modulus past the + /// input's end answers a zero byte. + #[test] + fn modexp_answers_nothing_where_rskj_throws() { + use alloy_primitives::U256; + let one = U256::from(1); + let two_28 = one << 28; + for (top, answer) in [(0x80, &[][..]), (0x40, &[0][..])] { + let input = modexp_input([one, two_28, one], &[2, top]); + let out = run_modexp(&input, 107_374_182).unwrap(); + assert_eq!( + (out.gas_used, out.bytes.as_ref()), + (107_374_182, answer), + "{top:#x}" + ); + // The base, the same way, at whatever price. + let input = modexp_input([two_28, one, one], &[top]); + let out = run_modexp(&input, u64::MAX).unwrap(); + assert_eq!(out.bytes.as_ref(), answer, "base {top:#x}"); + } + // The modulus would start at 2^31, past an int. The exponent is all + // zeros, which a `BigInteger` holds whatever their number. + let input = modexp_input([one, U256::from(i32::MAX as u64 - 96), one], &[2]); + assert!(run_modexp(&input, u64::MAX).unwrap().bytes.is_empty()); + } + #[test] fn test_remasc_address() { assert_eq!( From 42102bc0938e1eb96b935b12ae1e63a80a0fa1a4 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Mon, 9 Nov 2026 05:41:49 -0800 Subject: [PATCH 051/264] core: a header's fields as rskj reads, keeps and writes them rskj reads a header (BlockFactory.decodeHeader) with its own RLP, which takes lengths canonical RLP refuses, takes each field as getRLPData hands it over, keeps some as they came, and hashes the header as it writes it back (getEncoded), not as it was sent. rustock read the fields with canonical RLP into fixed types and hashed the items as sent. So it could not read a field sent in a longer form than canonical RLP's, a block whose coinbase is empty, or an uncle whose uncles hash, roots or bloom are of another length or whose paid fees pass 256 bits, all of which rskj reads; and a number sent with leading zeros it hashed as sent, which is not rskj's hash. Reading (Header::decode_with_activations). The list and its fields are cut with rskj_rlp. The parent hash must be 32 bytes, which getParentHash requires. An empty coinbase is rskj's null address, and any other must be 20 bytes (RLP.parseRskAddress). The uncles hash, the three roots and the logs bloom may be of any length, as an uncle's may, since no rule reads them; a root of no bytes is the empty trie's hash. The difficulty and the minimum gas price are read signed, as new BigInteger does: one that is none, negative or past 256 bits is refused, since rskj can't read it or no rule lets a block or an uncle have it. The number, gas used and timestamp are Java longs and the uncle count a Java int, as before. The gas limit keeps the bytes it came with, which rskj never reads as a number. Paid fees may be of any length, past 256 bits read as U256::MAX. ummRoot, edges and the base event are read raw (getRLPRawData, now rskj_rlp's Element::raw_data). Three empty merged-mining fields are none, as rskj writes none (hasMiningFields), and from RSKIP-98 on, block 0 aside, a Bitcoin header that isn't 80 bytes is refused, an empty one being null to rskj (validateBitcoinMergedMiningHeader). Slot 6 is read by activation, as rskj reads it: where RSKIP-351 is active and no version follows ummRoot, a commitment RLP([version, hash]) there is the extension's; anything else there is the logs bloom. Keeping. What the typed fields can't hold goes in Header::kept (KeptFields): the gas limit's bytes where they aren't how rskj's miner writes its value, the null coinbase, an uncles hash, root or bloom of another length, and paid fees past 256 bits. A header an honest miner writes keeps nothing. Each writes back as rskj writes it, and the zero a typed field holds in a kept one's place is never taken for it: the processor compares a block's computed uncles hash, roots and bloom through has_ommers_hash, has_state_root, has_transactions_root, has_receipts_root and has_logs_bloom. Hashing. Both hashes come from the header as it writes back, which is rskj's form, so a field sent in a longer form, or a number with leading zeros, hashes as rskj writes it. decode_with_hash no longer rebuilds them from the items as sent. Stores. A BlockStore or Freezer reads headers under the activation heights it is given (with_activations), else the process's. The node installs its network's heights before it opens one. Tests that store testnet or regtest headers now say so; they leaned on slot 6 being read by its shape. verify_stored_headers now also hashes each header as rustock writes it back and compares that with the hash it is stored under, and takes --testnet. Every header from #1 to #9,392,253 of a mainnet store holding the whole chain hashes to its key, as do the newest 8,003 of a mainnet light node and 8,008 of a testnet node. Genesis is left out: rskj writes its difficulty with the leading zero its spec gives it, and its hash is known. Tests read each field in each form rskj reads or refuses, and check it writes back to rskj's bytes and hash. Twenty one-line mutations each fail a test: slot 6 read without RSKIP-351 or beside a version, the RSKIP-98 check dropped or moved by a block, the gas limit's bytes, paid fees or null coinbase not kept, an empty root read as zero, a Java bound or sign dropped, a parent hash or coinbase of any length taken, and a store or the freezer reading under the process's heights. What reads the gas limit and the coinbase -- the executor, REMASC, the BlockHeader contract -- takes them up next, and the UMM root is read by activation after that. --- crates/alerts/tests/contract_value.rs | 1 + crates/cli/examples/loadtest.rs | 1 + crates/cli/examples/verify_stored_headers.rs | 80 +- crates/core/src/config.rs | 3 + crates/core/src/rskj_genesis.rs | 1 + crates/core/src/rskj_rlp.rs | 12 +- crates/core/src/types/block.rs | 1 + crates/core/src/types/header.rs | 1071 ++++++++++++++---- crates/core/src/validation/block_rules.rs | 1 + crates/core/src/validation/difficulty.rs | 1 + crates/core/src/validation/header_rules.rs | 6 +- crates/core/src/validation/tests.rs | 1 + crates/execution/src/env.rs | 1 + crates/execution/src/executor.rs | 1 + crates/execution/src/mining/template.rs | 1 + crates/execution/src/mining/tests.rs | 1 + crates/execution/src/mining/workload.rs | 2 + crates/execution/src/native/block_header.rs | 1 + crates/execution/src/processor.rs | 13 +- crates/execution/src/remasc.rs | 1 + crates/networking/src/protocol/rsk.rs | 3 + crates/networking/src/protocol/snap.rs | 1 + crates/rpc/src/tests.rs | 27 +- crates/storage/src/freezer.rs | 33 +- crates/storage/src/lib.rs | 115 +- crates/storage/src/position.rs | 1 + crates/storage/src/pruner.rs | 1 + crates/storage/tests/position_simulator.rs | 1 + crates/storage/tests/stall_replays.rs | 1 + crates/sync/src/gas_price.rs | 1 + crates/sync/src/import.rs | 1 + crates/sync/src/invariant.rs | 1 + crates/sync/src/rollback.rs | 1 + crates/sync/src/sampler.rs | 1 + crates/sync/src/snap/forward_tests.rs | 1 + crates/sync/src/snap/header_tests.rs | 1 + crates/sync/src/snap/indexer_tests.rs | 1 + crates/sync/src/snap/session_tests.rs | 1 + crates/sync/src/snap/tests.rs | 2 + crates/sync/src/tests.rs | 23 +- crates/sync/src/txpool.rs | 1 + 41 files changed, 1132 insertions(+), 286 deletions(-) diff --git a/crates/alerts/tests/contract_value.rs b/crates/alerts/tests/contract_value.rs index d6eea33..1ae1186 100644 --- a/crates/alerts/tests/contract_value.rs +++ b/crates/alerts/tests/contract_value.rs @@ -70,6 +70,7 @@ fn header(number: u64) -> Header { bitcoin_merged_mining_coinbase_transaction: None, cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, } } diff --git a/crates/cli/examples/loadtest.rs b/crates/cli/examples/loadtest.rs index faf3b15..296f1dd 100644 --- a/crates/cli/examples/loadtest.rs +++ b/crates/cli/examples/loadtest.rs @@ -885,6 +885,7 @@ fn bench_header(number: u64, parent_hash: B256, state_root: B256, edges: Vec [lowest-block] [highest-block] +//! And it hashes each header as rustock writes it back, which is how rskj +//! hashes one, against the hash it is stored under, which the header above +//! it names: a header read in a form rustock can't write back the same +//! would hash as another block. +//! +//! The chain is mainnet's unless named, and its heights are installed +//! before any header is read, as the node does. +//! +//! Usage: verify_stored_headers [--testnet] [lowest-block] [highest-block] use alloy_primitives::B256; use anyhow::Context; @@ -30,6 +38,15 @@ use std::time::Instant; /// How many failing block numbers to keep per rule. const EXAMPLES: usize = 8; +/// A header that hashes, as rustock writes it back, to another hash than +/// the one it is stored under. +#[derive(Debug)] +#[allow(dead_code)] // Read through `Debug`, by `record`. +struct Rehashed { + stored: B256, + computed: B256, +} + #[derive(Default)] struct Failures { count: u64, @@ -102,10 +119,19 @@ fn carry( } fn main() -> anyhow::Result<()> { - let mut args = std::env::args().skip(1); - let dir = args - .next() - .context("usage: verify_stored_headers [lowest-block] [highest-block]")?; + let mut args = std::env::args().skip(1).peekable(); + let testnet = args.next_if_eq("--testnet").is_some(); + let dir = args.next().context( + "usage: verify_stored_headers [--testnet] [lowest-block] [highest-block]", + )?; + let lowest = args.next(); + let highest = args.next(); + let config = Arc::new(if testnet { + ChainConfig::testnet() + } else { + ChainConfig::mainnet() + }); + config.activation_heights.clone().install(); let store = BlockStore::open_read_only(&dir)?; // A snapshot-synced node indexes by number only the window it fetched // bodies for, and answers every height below from the freezer. @@ -123,20 +149,13 @@ fn main() -> anyhow::Result<()> { let head = store .header(head_hash)? .context("the head header is missing")?; - let lowest: u64 = args - .next() - .map(|s| s.parse()) - .transpose()? - .unwrap_or(1) - .max(1); - let highest: u64 = args - .next() + let lowest: u64 = lowest.map(|s| s.parse()).transpose()?.unwrap_or(1).max(1); + let highest: u64 = highest .map(|s| s.parse()) .transpose()? .unwrap_or(head.number); anyhow::ensure!(lowest <= highest, "#{lowest} is above #{highest}"); - let config = Arc::new(ChainConfig::mainnet()); let wasabi100 = config.activation_heights.wasabi100; let verifier = HeaderVerifier::default_rsk(config.clone()); let threads = std::thread::available_parallelism() @@ -144,7 +163,8 @@ fn main() -> anyhow::Result<()> { .unwrap_or(4); let span = (highest - lowest + 1).div_ceil(threads); println!( - "store {dir}\n head #{}\n verifying #{highest} down to #{lowest} on {threads} threads", + "store {dir}, chain {}\n head #{}\n verifying #{highest} down to #{lowest} on {threads} threads", + config.chain_id, head.number ); @@ -162,6 +182,8 @@ fn main() -> anyhow::Result<()> { // And those below orchid that answer for it with a Bitcoin header, whose // coinbase a bitcoinj partial merkle tree proves. let trees = AtomicU64::new(0); + // Headers hashed as rustock writes them back, against their keys. + let rehashed = AtomicU64::new(0); let started = Instant::now(); std::thread::scope(|scope| -> anyhow::Result<()> { @@ -177,14 +199,30 @@ fn main() -> anyhow::Result<()> { let (store, verifier, config, failures, checked, not_later, orphaned, missing) = ( &store, &verifier, &config, &failures, &checked, ¬_later, &orphaned, &missing, ); - let (fork_checked, fallback, trees) = (&fork_checked, &fallback, &trees); + let (fork_checked, fallback, trees, rehashed) = + (&fork_checked, &fallback, &trees, &rehashed); workers.push(scope.spawn(move || -> anyhow::Result<()> { + // A header read under `stored`, hashed as rustock writes it. + // Not genesis: rskj's `GenesisHeader` writes its difficulty + // with the leading zero its spec gives it, which no decoder + // keeps, and its hash is known instead + // (`ChainConfig::known_genesis_hash`). + let rehash = |header: &Header, stored: B256| { + if header.number == 0 { + return; + } + let computed = header.hash(); + if computed != stored { + record(failures, header.number, &Rehashed { stored, computed }); + } + rehashed.fetch_add(1, Ordering::Relaxed); + }; // The highest block from `number` down to this range's bottom // that the store holds, counting the numbers it does not. let held_from = |mut number: u64| -> anyhow::Result> { loop { let found = match store.canonical_hash(number)? { - Some(hash) => store.header(hash)?, + Some(hash) => store.header(hash)?.inspect(|h| rehash(h, hash)), None => None, }; if found.is_some() { @@ -204,7 +242,9 @@ fn main() -> anyhow::Result<()> { // this walk reaches after it: each header is held till then. let mut window: VecDeque
= VecDeque::new(); let below = loop { - let parent = store.header(header.parent_hash)?; + let parent = store + .header(header.parent_hash)? + .inspect(|p| rehash(p, header.parent_hash)); if merged_mining::is_fallback_mined(&header, config) { fallback.fetch_add(1, Ordering::Relaxed); } else if header.number < config.activation_heights.orchid { @@ -300,6 +340,10 @@ fn main() -> anyhow::Result<()> { " merge-mined below orchid, their partial merkle trees checked: {}", trees.into_inner() ); + println!( + " hashed as rustock writes them back, against the hash each is stored under: {}", + rehashed.into_inner() + ); let expected = highest - lowest + 1; anyhow::ensure!( checked + missing == expected, diff --git a/crates/core/src/config.rs b/crates/core/src/config.rs index c19cc4b..eb3fad2 100644 --- a/crates/core/src/config.rs +++ b/crates/core/src/config.rs @@ -354,6 +354,7 @@ impl ChainConfig { bitcoin_merged_mining_coinbase_transaction: Some(Bytes::from_static(&[0x00])), cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, }, 31 => Header { parent_hash: B256::ZERO, @@ -380,6 +381,7 @@ impl ChainConfig { bitcoin_merged_mining_coinbase_transaction: None, cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, }, _ => Header { parent_hash: B256::ZERO, @@ -418,6 +420,7 @@ impl ChainConfig { bitcoin_merged_mining_coinbase_transaction: None, cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, }, } } diff --git a/crates/core/src/rskj_genesis.rs b/crates/core/src/rskj_genesis.rs index 5bfff2a..134b7b1 100644 --- a/crates/core/src/rskj_genesis.rs +++ b/crates/core/src/rskj_genesis.rs @@ -245,6 +245,7 @@ impl GenesisSpec { ), cached_hash: Some(self.hash()), cached_hash_for_merged_mining: None, + kept: None, } } } diff --git a/crates/core/src/rskj_rlp.rs b/crates/core/src/rskj_rlp.rs index a56a2a6..0b7353a 100644 --- a/crates/core/src/rskj_rlp.rs +++ b/crates/core/src/rskj_rlp.rs @@ -27,11 +27,17 @@ impl<'a> Element<'a> { /// `getRLPData`: none for an empty item, and a list's whole encoding, /// which is never empty. pub fn data(&self) -> Option<&'a [u8]> { - let bytes = match *self { + let bytes = self.raw_data(); + (!bytes.is_empty()).then_some(bytes) + } + + /// `getRLPRawData`: an item's payload, empty rather than none for an + /// empty item, and a list's whole encoding. + pub fn raw_data(&self) -> &'a [u8] { + match *self { Element::Item(payload) => payload, Element::List { encoding, .. } => encoding, - }; - (!bytes.is_empty()).then_some(bytes) + } } /// A list's elements, decoded as `RLPList.checkElements` decodes them. diff --git a/crates/core/src/types/block.rs b/crates/core/src/types/block.rs index 18bf5c1..077f8fa 100644 --- a/crates/core/src/types/block.rs +++ b/crates/core/src/types/block.rs @@ -52,6 +52,7 @@ mod tests { bitcoin_merged_mining_coinbase_transaction: None, cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, }; let tx = Transaction { diff --git a/crates/core/src/types/header.rs b/crates/core/src/types/header.rs index fb9f1f9..a8aced5 100644 --- a/crates/core/src/types/header.rs +++ b/crates/core/src/types/header.rs @@ -1,7 +1,7 @@ use alloy_primitives::{Address, Bloom, B256, U256, Bytes}; use alloy_rlp::Encodable; use serde::{Deserialize, Serialize}; -use crate::rlp_compat::{decode_u64_lenient, decode_u256_lenient}; + #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] pub struct Header { @@ -42,6 +42,12 @@ pub struct Header { pub bitcoin_merged_mining_merkle_proof: Option, pub bitcoin_merged_mining_coinbase_transaction: Option, + /// What rskj read from this header that the fields above can't hold, + /// kept so that it encodes and hashes as rskj writes it: see + /// [`KeptFields`]. `None` for every header an honest miner writes. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub kept: Option>, + /// The block hash, cached by `decode_with_hash` from the peer's own /// encoding of each field. Java's RLP encoding may differ from Rust's /// canonical encoding (e.g. leading zeros in BigInteger values), so the @@ -77,62 +83,128 @@ pub struct HeaderVersionFields { pub base_event: Option, } -/// Number of complete RLP items in `buf`, stopping at the first that does not -/// fit. Used to tell a header's trailing fields apart by position. -fn count_rlp_items(buf: &[u8]) -> usize { - let mut count = 0usize; - let mut rest = buf; - while !rest.is_empty() { - let mut temp = rest; - match alloy_rlp::Header::decode(&mut temp) { - Ok(h) if h.payload_length <= temp.len() => { - rest = &temp[h.payload_length..]; - count += 1; - } - _ => break, - } - } - count +/// What rskj's `BlockFactory` reads from a header that [`Header`]'s typed +/// fields can't hold. +/// +/// rskj keeps these as it reads them and writes them back the same way, so +/// they are in the header's hash, and no rule refuses them where they can +/// appear: a miner picks them. Each is kept only where it differs from +/// what the typed field writes, so a header an honest miner wrote has none. +#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +pub struct KeptFields { + /// The gas limit's bytes, where they aren't how rskj's miner writes its + /// value (`BigInteger.toByteArray`): more leading zeros, no sign byte, + /// or none at all. rskj never holds the gas limit as a number. It + /// compares its value with the parent's, but `GasCost.toGas` reads + /// more than eight bytes as `Long.MAX_VALUE`, `DataWord.valueOf` + /// refuses more than 32, and the BlockHeader contract returns the + /// bytes. `gas_limit` holds the value, or `U256::MAX` past 256 bits. + pub gas_limit: Option, + /// An empty coinbase, which rskj reads as its null address, an + /// `RskAddress` of no bytes, and writes back empty. `beneficiary` + /// holds zero, which is what COINBASE reads for it. + pub null_coinbase: bool, + /// An uncles hash that isn't 32 bytes. No rule reads an uncle's, and a + /// block's must be the hash of its uncles. `ommers_hash` holds zero. + pub ommers_hash: Option, + /// The state root, where it isn't 32 bytes: an uncle's, which no rule + /// reads. rskj reads none as the empty trie's hash, which a typed + /// field holds. `state_root` holds zero. + pub state_root: Option, + /// The transactions root, likewise. + pub transactions_root: Option, + /// The receipts root, likewise. + pub receipts_root: Option, + /// The logs bloom, where it isn't 256 bytes: an uncle's, which no rule + /// reads, and which a versioned uncle commits to through its + /// extension's hash. `logs_bloom` holds zero. + pub logs_bloom: Option, + /// Paid fees past 256 bits, without leading zeros: an uncle's, which + /// nothing bounds. `paid_fees` holds `U256::MAX`, which compares with + /// any block's fees, doubled or not, as the value itself does. + pub paid_fees: Option, +} + +/// rskj refuses the header: `BlockFactory.decodeHeader` can't read it, or +/// reads it as one that no rule lets a block or an uncle have. +fn refused(why: &'static str) -> alloy_rlp::Error { + alloy_rlp::Error::Custom(why) +} + +/// rskj's `HashUtil.EMPTY_TRIE_HASH`, which it reads a trie root of no +/// bytes as. +const EMPTY_TRIE_HASH: B256 = + alloy_primitives::b256!("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"); + +/// A number as `BigIntegers.fromUnsignedByteArray` reads it, none being +/// zero: its bytes from the first that isn't zero. +fn significant(bytes: Option<&[u8]>) -> &[u8] { + let bytes = bytes.unwrap_or_default(); + &bytes[bytes + .iter() + .position(|byte| *byte != 0) + .unwrap_or(bytes.len())..] +} + +/// A header's number, gas used or timestamp, read so and then with +/// `longValueExact`, which throws past a Java `long`. +fn java_long(bytes: Option<&[u8]>) -> alloy_rlp::Result { + let value = significant(bytes); + (value.len() <= 8) + .then(|| { + value + .iter() + .fold(0u64, |acc, byte| (acc << 8) | u64::from(*byte)) + }) + .filter(|value| *value <= i64::MAX as u64) + .ok_or(refused("a header field past a Java long")) +} + +/// Its uncle count, with `intValueExact`, which throws past a Java `int`. +fn java_int(bytes: Option<&[u8]>) -> alloy_rlp::Result { + java_long(bytes) + .ok() + .filter(|value| *value <= i32::MAX as u64) + .ok_or(refused("an uncle count past a Java int")) } -/// The items of the RLP list `encoded`, each in its whole encoding, header -/// and payload, as the peer spelled it. `None` unless `encoded` is exactly -/// one list of complete items. -fn rlp_list_items(encoded: &[u8]) -> Option> { - let mut rest = encoded; - let list = alloy_rlp::Header::decode(&mut rest).ok()?; - if !list.list || rest.len() != list.payload_length { +/// `new BigInteger(bytes)`, which reads the difficulty and the minimum gas +/// price: two's complement, so a first byte from 0x80 up is negative. +/// `None` for a negative value and for one past 256 bits. +fn java_signed(bytes: &[u8]) -> Option { + if bytes.first().is_some_and(|byte| byte & 0x80 != 0) { return None; } - let mut items = Vec::new(); - while !rest.is_empty() { - let mut temp = rest; - let item = alloy_rlp::Header::decode(&mut temp).ok()?; - let length = (rest.len() - temp.len()).checked_add(item.payload_length)?; - items.push(rest.get(..length)?); - rest = &rest[length..]; + let value = significant(Some(bytes)); + (value.len() <= 32).then(|| U256::from_be_slice(value)) +} + +/// A hash rskj keeps as it read it: in `kept` unless it is 32 bytes. +fn hash_field(data: Option<&[u8]>, kept: &mut Option) -> B256 { + match data { + Some(bytes) if bytes.len() == 32 => B256::from_slice(bytes), + other => { + *kept = Some(Bytes::copy_from_slice(other.unwrap_or_default())); + B256::ZERO + } } - Some(items) } -/// rskj reads a header's number, gas used and timestamp with -/// `longValueExact`, which throws past a Java `long`: it cannot read a header -/// with one over `i64::MAX`. -fn decode_java_long(buf: &mut &[u8]) -> alloy_rlp::Result { - let value = decode_u64_lenient(buf)?; - if value > i64::MAX as u64 { - return Err(alloy_rlp::Error::Custom("a header field past a Java long")); +/// A trie root, which rskj reads as the empty trie's hash where it has no +/// bytes (`BlockFactory`: `if (stateRoot == null)`). +fn root_field(data: Option<&[u8]>, kept: &mut Option) -> B256 { + match data { + None => EMPTY_TRIE_HASH, + some => hash_field(some, kept), } - Ok(value) } -/// And its uncle count with `intValueExact`, which throws past a Java `int`. -fn decode_java_int(buf: &mut &[u8]) -> alloy_rlp::Result { - let value = decode_u64_lenient(buf)?; - if value > i32::MAX as u64 { - return Err(alloy_rlp::Error::Custom("an uncle count past a Java int")); +/// A field rskj kept as it read it, if it did, else the typed field. +fn encode_kept(list: &mut Vec, kept: Option<&Bytes>, typed: &dyn Encodable) { + match kept { + Some(bytes) => bytes.encode(list), + None => typed.encode(list), } - Ok(value) } /// rskj `ByteUtil.rlpToShorts`: little-endian pairs. @@ -231,7 +303,12 @@ impl Header { fn literal_extension(&self) -> Option { use crate::types::header_extension::BlockHeaderExtension; let v = self.version_fields.as_ref()?; - let bloom = self.logs_bloom.as_slice().to_vec(); + // An uncle's bloom of another length commits through the extension's + // hash as rskj kept it. + let bloom = match self.kept().and_then(|kept| kept.logs_bloom.as_ref()) { + Some(bytes) => bytes.to_vec(), + None => self.logs_bloom.as_slice().to_vec(), + }; match v.version { 1 => Some(BlockHeaderExtension::V1 { logs_bloom: bloom, @@ -293,12 +370,36 @@ impl Header { } fn encode_base_fields(&self, list: &mut Vec, compressed: bool) { + // A field rskj read as something the typed field can't hold goes + // back as rskj writes it from what it kept. + let kept = self.kept.as_deref(); self.parent_hash.encode(list); - self.ommers_hash.encode(list); - self.beneficiary.encode(list); - self.state_root.encode(list); - self.transactions_root.encode(list); - self.receipts_root.encode(list); + encode_kept( + list, + kept.and_then(|k| k.ommers_hash.as_ref()), + &self.ommers_hash, + ); + if kept.is_some_and(|k| k.null_coinbase) { + // `RLP.encodeRskAddress`: the null address is an empty string. + list.push(alloy_rlp::EMPTY_STRING_CODE); + } else { + self.beneficiary.encode(list); + } + encode_kept( + list, + kept.and_then(|k| k.state_root.as_ref()), + &self.state_root, + ); + encode_kept( + list, + kept.and_then(|k| k.transactions_root.as_ref()), + &self.transactions_root, + ); + encode_kept( + list, + kept.and_then(|k| k.receipts_root.as_ref()), + &self.receipts_root, + ); // RSKIP-351: a V1/V2 header carries `RLP([version, extension_hash])` // in the slot a V0 header fills with its 256-byte bloom. The bytes are // already RLP, so they are appended rather than re-encoded. @@ -321,15 +422,26 @@ impl Header { // hash it yields simply will not match, which is visible. None => self.logs_bloom.encode(list), }, - _ => self.logs_bloom.encode(list), + _ => encode_kept( + list, + kept.and_then(|k| k.logs_bloom.as_ref()), + &self.logs_bloom, + ), } java_signed_bytes(&self.difficulty).as_slice().encode(list); self.number.encode(list); - java_signed_bytes(&self.gas_limit).as_slice().encode(list); + match kept.and_then(|k| k.gas_limit.as_ref()) { + Some(bytes) => bytes.encode(list), + None => java_signed_bytes(&self.gas_limit).as_slice().encode(list), + } self.gas_used.encode(list); self.timestamp.encode(list); self.extra_data.encode(list); - self.paid_fees.encode(list); + encode_kept( + list, + kept.and_then(|k| k.paid_fees.as_ref()), + &self.paid_fees, + ); java_signed_bytes(&self.minimum_gas_price).as_slice().encode(list); self.uncle_count.encode(list); } @@ -405,108 +517,150 @@ impl alloy_rlp::Decodable for Header { } impl Header { - /// Decode a header under explicit activation heights. Only RSKIP-144's - /// height is consulted: it decides what a lone item after a V2 version - /// byte is (see below). + /// Decode a header as rskj's `BlockFactory.decodeHeader` reads one, + /// under explicit activation heights. + /// + /// The list and its fields are cut with rskj's own RLP + /// ([`crate::rskj_rlp`]), which takes lengths canonical RLP refuses, + /// and each field is read as rskj reads it: what rskj can't read, or + /// reads as something no rule lets a block or an uncle have, is + /// refused. A field rskj keeps in a form the typed fields can't hold + /// goes in [`KeptFields`], and the header hashes as rskj writes it back + /// from what it read, which is not always as it was sent. + /// + /// Of the heights, RSKIP-144's, RSKIP-351's and orchid's (RSKIP-98) are + /// read here; which fields a header carries at its height is + /// `HeaderLayoutRule`'s to say. pub fn decode_with_activations( buf: &mut &[u8], activations: &crate::config::ActivationHeights, ) -> alloy_rlp::Result { - use alloy_rlp::Decodable; - let h = alloy_rlp::Header::decode(buf)?; - if !h.list { return Err(alloy_rlp::Error::UnexpectedString); } - let mut body = &buf[..h.payload_length]; - *buf = &buf[h.payload_length..]; - - let parent_hash = B256::decode(&mut body)?; - let ommers_hash = B256::decode(&mut body)?; - let beneficiary = Address::decode(&mut body)?; - let state_root = B256::decode(&mut body)?; - let transactions_root = B256::decode(&mut body)?; - let receipts_root = B256::decode(&mut body)?; - - // Field 6: logs bloom (256 bytes) OR compressed extension data (shorter). - // Peek at the RLP header to determine which one. - let (logs_bloom, extension_data) = { - let mut peek = body; - let rlp_h = alloy_rlp::Header::decode(&mut peek)?; - if !rlp_h.list && rlp_h.payload_length == 256 { - // Standard logs bloom (256 bytes) - (Bloom::decode(&mut body)?, None) + let (list, next) = crate::rskj_rlp::first_element(buf) + .map_err(|_| refused("a header rskj's RLP can't read"))?; + let fields = list + .elements() + .map_err(|_| refused("a header that is not a list of fields"))?; + let header = Self::from_fields(&fields, activations)?; + *buf = &buf[next..]; + Ok(header) + } + + /// A header's fields, each read as `BlockFactory.decodeHeader` reads it. + fn from_fields( + fields: &[crate::rskj_rlp::Element<'_>], + activations: &crate::config::ActivationHeights, + ) -> alloy_rlp::Result { + // rskj reads each field with `getRLPData`: none for an empty item, + // and a list's whole encoding for a list, which it reads as bytes. + let field = |index: usize| { + fields + .get(index) + .map(|element| element.data()) + .ok_or(refused("a header with too few fields")) + }; + let mut kept = KeptFields::default(); + + // rskj reads any bytes here, but `getParentHash` throws unless they + // are 32, and every rule that finds a header's parent calls it. + let parent_hash = match field(0)? { + Some(bytes) if bytes.len() == 32 => B256::from_slice(bytes), + _ => return Err(refused("a parent hash that is not 32 bytes")), + }; + let ommers_hash = hash_field(field(1)?, &mut kept.ommers_hash); + // `RLP.parseRskAddress`: none is the null address, and anything but + // 20 bytes throws. + let beneficiary = match field(2)? { + None => { + kept.null_coinbase = true; + Address::ZERO + } + Some(bytes) if bytes.len() == 20 => Address::from_slice(bytes), + Some(_) => return Err(refused("a coinbase neither empty nor 20 bytes")), + }; + let state_root = root_field(field(3)?, &mut kept.state_root); + let transactions_root = root_field(field(4)?, &mut kept.transactions_root); + let receipts_root = root_field(field(5)?, &mut kept.receipts_root); + let number = java_long(field(8)?)?; + + // Slot 6, the logs bloom or a commitment in its place, is read once + // the fields after it say which (below). + let slot6 = field(6)?.unwrap_or_default(); + + // `RLP.parseBlockDifficulty`: `new BigInteger(bytes)`, so a negative + // difficulty, which `BlockDifficulty` throws on. rskj reads none as + // zero, and `DifficultyRule` refuses zero wherever rskj takes a + // header, as it does a difficulty past 256 bits: none of its + // parents' leads there. + let difficulty = field(7)?.and_then(java_signed).ok_or(refused( + "a difficulty that is none, negative or past 256 bits", + ))?; + // rskj keeps the gas limit's bytes: see `KeptFields::gas_limit`. + let gas_limit = { + let bytes = field(9)?.unwrap_or_default(); + let value = significant(Some(bytes)); + let gas_limit = if value.len() > 32 { + U256::MAX } else { - // Compressed extension data (V1/V2 RSKIP-351). - // Could be an RLP list or string — read the raw bytes either way. - let header_len = body.len() - peek.len(); - let total_len = header_len + rlp_h.payload_length; - let raw = Bytes::copy_from_slice(&body[..total_len]); - body = &body[total_len..]; - (Bloom::default(), Some(raw)) + U256::from_be_slice(value) + }; + if value.len() > 32 || bytes != java_signed_bytes(&gas_limit).as_slice() { + kept.gas_limit = Some(Bytes::copy_from_slice(bytes)); } + gas_limit }; - - let mut header = Self { - parent_hash, - ommers_hash, - beneficiary, - state_root, - transactions_root, - receipts_root, - logs_bloom, - extension_data, - difficulty: decode_u256_lenient(&mut body)?, - number: decode_java_long(&mut body)?, - gas_limit: decode_u256_lenient(&mut body)?, - gas_used: decode_java_long(&mut body)?, - timestamp: decode_java_long(&mut body)?, - extra_data: Bytes::decode(&mut body)?, - paid_fees: decode_u256_lenient(&mut body)?, - minimum_gas_price: decode_u256_lenient(&mut body)?, - uncle_count: decode_java_int(&mut body)?, - umm_root: None, - version_fields: None, - bitcoin_merged_mining_header: None, - bitcoin_merged_mining_merkle_proof: None, - bitcoin_merged_mining_coinbase_transaction: None, - cached_hash: None, - cached_hash_for_merged_mining: None, + let gas_used = java_long(field(10)?)?; + let timestamp = java_long(field(11)?)?; + let extra_data = Bytes::copy_from_slice(field(12)?.unwrap_or_default()); + // `RLP.parseCoin`: unsigned and of any length, none or all zeros + // being zero. + let paid_fees = { + let value = significant(field(13)?); + if value.len() > 32 { + kept.paid_fees = Some(Bytes::copy_from_slice(value)); + U256::MAX + } else { + U256::from_be_slice(value) + } }; + // `RLP.parseSignedCoinNonNullZero`: signed, and none is none. + // `PrevMinGasPriceRule` refuses none wherever rskj takes a header, + // and no parent's range holds a negative price or one past 256 bits. + let minimum_gas_price = field(14)?.and_then(java_signed).ok_or(refused( + "a minimum gas price that is none, negative or past 256 bits", + ))?; + let uncle_count = java_int(field(15)?)?; // What is left is told apart by count, since nothing here knows the // network: none is a header without merged-mining fields, three are // those fields, and four or more begin with a UMM root (RSKIPUMM, // papyrus200). `HeaderLayoutRule` holds what was read to the fields // rskj reads at the header's height. - let remaining_items = count_rlp_items(body); - - if remaining_items >= 4 { - header.umm_root = Some(Bytes::decode(&mut body)?); + let mut rest = &fields[16..]; + let mut umm_root = None; + if rest.len() >= 4 { + // `getRLPRawData`: an empty root is kept, and written back, empty. + umm_root = Some(Bytes::copy_from_slice(rest[0].raw_data())); + rest = &rest[1..]; } // RSKIP-351: a versioned header puts its version, its RSKIP-144 edges // and (V2) its base event here, between `ummRoot` and the merged // mining fields. rskj picks them out by activation height; nothing - // here knows the network, so the version byte is used as its own - // marker instead. It is `RLP.encodeByte(1|2)`, a bare 0x01 or 0x02 -- - // a value no other field in this position can take. `ummRoot` is - // empty or 32 bytes, edges are an even-length string, the base event - // is empty or long, and a merged-mining header is 80 bytes. - // - // Dropping these was invisible for as long as nothing re-derived a - // hash: the decoder cached the one the peer sent. It cost every - // header from testnet reed810 on its correct hash, which first - // surfaced as REMASC's selection rule comparing the wrong ones. - if matches!(body.first(), Some(1 | 2)) { - let version = body[0]; - body = &body[1..]; - + // here knows the network, so the version is used as its own marker + // instead: a single byte, 1 or 2, a value no other field in this + // position can take. `ummRoot` is empty or 20 bytes, edges are an + // even-length string, the base event is empty or long, and a + // merged-mining header is 80 bytes. + let mut version_fields = None; + if let Some(&[version @ (1 | 2)]) = rest.first().map(|element| element.raw_data()) { + rest = &rest[1..]; // Whatever is left belongs to the merged mining fields, except // what the version says comes first. rskj emits the edges only // when the block has any, so their presence is positional: with a // base event to follow (V2) there is one more item here than // mining alone accounts for. - let after_version = count_rlp_items(body); - let mining_items = after_version.min(3); - let extras = after_version - mining_items; + let mining = rest.len().min(3); + let extras = rest.len() - mining; // Edges come first and are emitted whenever RSKIP144 is active -- // as an empty string when the block has no sublists -- so they are @@ -517,42 +671,118 @@ impl Header { // (`BlockFactory.isInBaseEventRecoveryWindow`). Reading the // trailing element as the base event in that case takes the edges // for it and hashes the header wrong. - let mut edges = None; - let mut base_event = None; // // Where RSKIP-144 is not active there are no edges, and a V2 // header's one trailing item is its base event (rskj's regtest: // rskip144 = -1, rskip535 = 1). rskj tells the cases apart by - // activation, and so does this. - let rskip144 = activations.rskip144.is_some_and(|h| header.number >= h); + // activation, and so does this. Both are read raw + // (`getRLPRawData`). + let mut edges = None; + let mut base_event = None; + let rskip144 = activations.rskip144.is_some_and(|height| number >= height); if version >= 2 && extras == 1 && !rskip144 { - base_event = Some(Bytes::decode(&mut body)?); + base_event = Some(Bytes::copy_from_slice(rest[0].raw_data())); + rest = &rest[1..]; } else { if extras >= 1 { - edges = Some(rlp_to_shorts(&Bytes::decode(&mut body)?)); + edges = Some(rlp_to_shorts(rest[0].raw_data())); + rest = &rest[1..]; } if version >= 2 && extras >= 2 { - base_event = Some(Bytes::decode(&mut body)?); + base_event = Some(Bytes::copy_from_slice(rest[0].raw_data())); + rest = &rest[1..]; } } - header.version_fields = Some(HeaderVersionFields { version, edges, base_event }); + version_fields = Some(HeaderVersionFields { + version, + edges, + base_event, + }); } + // Slot 6 holds the logs bloom, of any length: rskj reads whatever is + // there as the bloom (`BlockHeaderV0.getLogsBloom`, and a versioned + // header's extension when it comes in full), and no rule reads an + // uncle's. Only where RSKIP-351 is active does a header that travels + // compressed hold there instead the commitment to its extension, + // `RLP([version, hash])`, with no version after `ummRoot`. rskj writes + // either back as one string (`RLP.encodeElement`), whatever form it + // came in. + let rskip351 = activations.rskip351.is_some_and(|height| number >= height); + let commitment = rskip351 + && version_fields.is_none() + && crate::types::header_extension::ExtensionCommitment::from_extension_data(slot6) + .is_ok(); + let (logs_bloom, extension_data) = if commitment { + let mut encoded = Vec::new(); + slot6.encode(&mut encoded); + (Bloom::ZERO, Some(Bytes::from(encoded))) + } else if slot6.len() == 256 { + (Bloom::from_slice(slot6), None) + } else { + kept.logs_bloom = Some(Bytes::copy_from_slice(slot6)); + (Bloom::ZERO, None) + }; + // rskj reads the three merged-mining fields together or not at all, // and `canBeDecoded` counts a header's fields exactly: with one or two - // of them, or anything after them, it cannot read the header. - if !body.is_empty() { - header.bitcoin_merged_mining_header = Some(Bytes::decode(&mut body)?); - header.bitcoin_merged_mining_merkle_proof = Some(Bytes::decode(&mut body)?); - header.bitcoin_merged_mining_coinbase_transaction = Some(Bytes::decode(&mut body)?); - if !body.is_empty() { - return Err(alloy_rlp::Error::Custom( - "a header with fields after its merged-mining fields", - )); + // of them, or anything after them, it can't read the header. It + // writes them back only where one of them has bytes + // (`hasMiningFields`). From RSKIP-98 on, block 0 aside, it refuses + // them unless the Bitcoin header is 80 bytes, none being null to it + // (`validateBitcoinMergedMiningHeader`), as `ProofOfWorkRule` and + // `MergedMiningRule` do again. + let (btc_header, merkle_proof, coinbase) = match rest { + [] => (None, None, None), + [header, proof, coinbase] => { + let size = crate::validation::merged_mining::BTC_HEADER_SIZE; + if number != 0 && number >= activations.orchid && header.raw_data().len() != size { + return Err(refused( + "a Bitcoin header that is not 80 bytes from RSKIP-98 on", + )); + } + let mining = [header.raw_data(), proof.raw_data(), coinbase.raw_data()]; + if mining.iter().all(|field| field.is_empty()) { + (None, None, None) + } else { + let [header, proof, coinbase] = mining.map(Bytes::copy_from_slice); + (Some(header), Some(proof), Some(coinbase)) + } } - } + _ => { + return Err(refused( + "a header whose merged-mining fields aren't all three or none", + )) + } + }; - Ok(header) + Ok(Self { + parent_hash, + ommers_hash, + beneficiary, + state_root, + transactions_root, + receipts_root, + logs_bloom, + extension_data, + difficulty, + number, + gas_limit, + gas_used, + timestamp, + extra_data, + paid_fees, + minimum_gas_price, + uncle_count, + umm_root, + version_fields, + bitcoin_merged_mining_header: btc_header, + bitcoin_merged_mining_merkle_proof: merkle_proof, + bitcoin_merged_mining_coinbase_transaction: coinbase, + kept: (kept != KeptFields::default()).then(|| Box::new(kept)), + cached_hash: None, + cached_hash_for_merged_mining: None, + }) } } @@ -564,6 +794,44 @@ impl Header { self.number >= crate::config::ActivationHeights::current().orchid } + /// What rskj kept of this header that the typed fields can't hold. + pub fn kept(&self) -> Option<&KeptFields> { + self.kept.as_deref() + } + + /// Whether this header's uncles hash is `hash`. rskj compares the bytes + /// it read, and ones kept for not being 32 bytes are no hash. + pub fn has_ommers_hash(&self, hash: B256) -> bool { + self.kept().is_none_or(|kept| kept.ommers_hash.is_none()) && self.ommers_hash == hash + } + + /// Whether this header's state root is `root`, likewise. + pub fn has_state_root(&self, root: B256) -> bool { + self.kept().is_none_or(|kept| kept.state_root.is_none()) && self.state_root == root + } + + /// Whether this header's transactions root is `root`, likewise. + pub fn has_transactions_root(&self, root: B256) -> bool { + self.kept() + .is_none_or(|kept| kept.transactions_root.is_none()) + && self.transactions_root == root + } + + /// Whether this header's receipts root is `root`, likewise. + pub fn has_receipts_root(&self, root: B256) -> bool { + self.kept().is_none_or(|kept| kept.receipts_root.is_none()) && self.receipts_root == root + } + + /// Whether this header's logs bloom is `bloom`, likewise: one kept for + /// not being 256 bytes is no bloom, not even the empty one a block + /// without logs computes, which the zero held in its place would be. Nor + /// is a commitment held in its place, whose extension never came. + pub fn has_logs_bloom(&self, bloom: &Bloom) -> bool { + self.kept().is_none_or(|kept| kept.logs_bloom.is_none()) + && self.extension_data.is_none() + && self.logs_bloom == *bloom + } + pub fn hash(&self) -> B256 { if let Some(h) = self.cached_hash { return h; @@ -594,19 +862,22 @@ impl Header { out } - /// Decode a header from RLP bytes and cache both of its hashes from those - /// bytes, since re-encoding need not reproduce a peer's Java spelling of - /// every field. + /// Decode a header and cache both of its hashes. /// - /// rskj does not hash the form it sends. It hashes the compressed form, + /// rskj hashes a header as it writes it back from what it read + /// (`getHash` over `getEncodedForHash`), not as it was sent: a field + /// sent in a longer RLP form than rskj's, or a number with leading + /// zeros, hashes as rskj writes it, which is what [`Self::hash`] + /// encodes, from the typed fields and what [`KeptFields`] holds. + /// + /// Nor does rskj hash the form it sends. It hashes the compressed form, /// which leaves out the merged-mining proof and coinbase (RSKIP-92) and, /// for a versioned (RSKIP-351) header, the version, edges and base event /// that follow `ummRoot` on the wire, whose commitment takes the bloom's - /// place in slot 6. So both hashes are built from the items as sent, - /// less those, with slot 6 swapped. Hashing a versioned header as sent - /// gave every testnet header from reed810 on a hash no other node agrees - /// with: no header named its parent, and the snapshot sync refused every - /// chain it was offered. + /// place in slot 6. Hashing a versioned header as sent gave every + /// testnet header from reed810 on a hash no other node agrees with: no + /// header named its parent, and the snapshot sync refused every chain it + /// was offered. pub fn decode_with_hash(buf: &mut &[u8]) -> alloy_rlp::Result { Self::decode_with_hash_and_activations(buf, crate::config::ActivationHeights::current()) } @@ -618,77 +889,9 @@ impl Header { buf: &mut &[u8], activations: &crate::config::ActivationHeights, ) -> alloy_rlp::Result { - let original = *buf; let mut header = Self::decode_with_activations(buf, activations)?; - let encoded = &original[..original.len() - buf.len()]; - - // `decode` has just read this list, so its items split; were one too - // lenient for that, both hashes would be left to come from this - // node's own encoding instead. - let Some(items) = rlp_list_items(encoded) else { - return Ok(header); - }; - // The sixteen base fields and `ummRoot`, then on the wire a versioned - // header's own fields, then the merged-mining fields. - let base = 16 + usize::from(header.umm_root.is_some()); - let versioned = header.version_fields.as_ref().map_or(0, |v| { - 1 + usize::from(v.edges.is_some()) + usize::from(v.base_event.is_some()) - }); - let (Some(base_items), Some(mining)) = (items.get(..base), items.get(base + versioned..)) - else { - return Ok(header); - }; - // A versioned header that sent its literal bloom is hashed with its - // commitment in slot 6, as `encode_base_fields` does. - let commitment = if header.extension_data.is_none() { - header.compressed_extension_field() - } else { - None - }; - let hash_with = |mining: &[&[u8]]| { - let mut payload = Vec::with_capacity(encoded.len()); - for (slot, item) in base_items.iter().enumerate() { - match &commitment { - Some(field) if slot == 6 => payload.extend_from_slice(field), - _ => payload.extend_from_slice(item), - } - } - for item in mining { - payload.extend_from_slice(item); - } - let mut list = Vec::with_capacity(payload.len() + 9); - alloy_rlp::Header { - list: true, - payload_length: payload.len(), - } - .encode(&mut list); - list.extend_from_slice(&payload); - alloy_primitives::keccak256(&list) - }; - - // RSKIP-92: the block hash keeps the merged-mining header and leaves - // out the proof and the coinbase. - let hashed_mining = if header.rskip92_active() { - &mining[..mining.len().min(1)] - } else { - mining - }; - header.cached_hash = Some(hash_with(hashed_mining)); - - // The merged-mining hash leaves out all three, and a UMM block then - // folds its root in: keccak(the first 20 bytes ++ ummRoot). - let for_merged_mining = hash_with(&[]); - header.cached_hash_for_merged_mining = Some( - match header.umm_root.as_ref().filter(|umm| !umm.is_empty()) { - Some(umm) => { - let mut input = Vec::with_capacity(20 + umm.len()); - input.extend_from_slice(&for_merged_mining.as_slice()[..20]); - input.extend_from_slice(umm.as_ref()); - alloy_primitives::keccak256(&input) - } - None => for_merged_mining, - }, - ); + header.cached_hash = Some(header.hash()); + header.cached_hash_for_merged_mining = Some(header.hash_for_merged_mining()); Ok(header) } @@ -763,6 +966,7 @@ mod tests { bitcoin_merged_mining_coinbase_transaction: Some(Bytes::from("coinbase")), cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, } } @@ -821,7 +1025,11 @@ mod tests { #[test] fn rskj_reads_no_header_past_a_java_long_or_int() { let decodes = |change: &dyn Fn(&mut Header)| { - let mut header = standard_test_header(); + // From RSKIP-98 on, a Bitcoin header of 80 bytes. + let mut header = Header { + bitcoin_merged_mining_header: Some(Bytes::from(vec![0; 80])), + ..standard_test_header() + }; change(&mut header); let mut bytes = Vec::new(); header.encode(&mut bytes); @@ -872,6 +1080,400 @@ mod tests { assert!(read(5).is_err(), "a field after the merged-mining fields"); } + /// An RLP string, as rskj's `RLP.encodeElement` writes one. + fn element(bytes: &[u8]) -> Vec { + let mut out = Vec::new(); + bytes.encode(&mut out); + out + } + + /// The same string with a long-form length, which rskj reads and + /// canonical RLP refuses. + fn long_form(bytes: &[u8]) -> Vec { + [&[0xb8, bytes.len() as u8][..], bytes].concat() + } + + /// `standard_test_header`'s fields as rskj writes them. It is below + /// orchid, so its hash is Keccak-256 of all of them. + fn standard_items() -> Vec> { + let mut bytes = Vec::new(); + standard_test_header().encode(&mut bytes); + items_of(&bytes) + } + + /// Those fields with field `index` sent as `spelling`, and what they + /// decode to. + fn respelled(index: usize, spelling: Vec) -> (Vec>, alloy_rlp::Result
) { + let mut items = standard_items(); + items[index] = spelling; + let decoded = Header::decode_with_hash(&mut list_of(&items).as_slice()); + (items, decoded) + } + + /// A header rskj read writes back as `items`, hashes as them, and reads + /// back from what it writes as itself, as the store reads what it wrote. + fn writes_back_as(header: &Header, items: &[Vec]) { + let mut written = Vec::new(); + header.encode(&mut written); + assert_eq!(written, list_of(items)); + assert_eq!(header.hash(), keccak256(&written)); + assert_eq!( + &Header::decode_with_hash(&mut written.as_slice()).unwrap(), + header + ); + } + + /// rskj's RLP reads a field in a longer form than its own, a single byte + /// as a one-byte string or a length in long form, and rskj hashes the + /// header as it writes it back, in its own form: the header sent so is + /// the one rskj wrote, the same block. + #[test] + fn a_field_sent_in_a_longer_form_is_the_field_rskj_writes() { + let header = standard_test_header(); + let items = standard_items(); + let mut longer = items.clone(); + longer[0] = long_form(header.parent_hash.as_slice()); + longer[8] = vec![0x81, 42]; + longer[12] = long_form(&header.extra_data); + // The list too, its length in three bytes where two do. + let payload = longer.concat(); + let mut sent = vec![0xfa, 0]; + sent.extend_from_slice(&(payload.len() as u16).to_be_bytes()); + sent.extend_from_slice(&payload); + assert!(alloy_rlp::Header::decode(&mut sent.as_slice()).is_err()); + + let decoded = Header::decode_with_hash(&mut sent.as_slice()).unwrap(); + assert_eq!(decoded.hash(), keccak256(list_of(&items))); + writes_back_as(&decoded, &items); + } + + /// rskj reads a number with leading zeros as its value and writes it + /// without them: the difficulty and minimum gas price as Java writes + /// them, the rest unsigned and minimal, and a zero as none. + #[test] + fn a_number_with_leading_zeros_is_the_number_rskj_writes() { + for (index, spelled) in [ + (7, &[0, 0, 0x12, 0xd6, 0x87][..]), + (8, &[0, 0, 42]), + (10, &[0, 0x01, 0xe2, 0x40]), + (11, &[0, 0x65, 0x53, 0xf1, 0x00]), + (13, &[0, 0, 100]), + (14, &[0, 0, 1]), + (15, &[0]), + ] { + let (_, decoded) = respelled(index, element(spelled)); + writes_back_as(&decoded.unwrap(), &standard_items()); + } + } + + /// rskj reads every field with `getRLPData`, which gives a list's whole + /// encoding: a list where a field's bytes should be is those bytes. + #[test] + fn a_list_where_a_field_is_is_its_encoding() { + let list = vec![0xc3, 1, 2, 3]; + let (items, decoded) = respelled(12, list.clone()); + let decoded = decoded.unwrap(); + assert_eq!(decoded.extra_data, Bytes::from(list.clone())); + let mut expected = items; + expected[12] = element(&list); + writes_back_as(&decoded, &expected); + } + + /// rskj keeps the gas limit's bytes as they came, so a miner who writes + /// them otherwise than rskj's miner does makes another header. + #[test] + fn the_gas_limit_keeps_the_bytes_it_came_with() { + for (bytes, value, kept) in [ + (&[0x00, 0x98, 0x96, 0x80][..], 10_000_000u64, false), + (&[0x98, 0x96, 0x80], 10_000_000, true), + (&[0, 0, 0, 0x98, 0x96, 0x80], 10_000_000, true), + (&[], 0, true), + ] { + let (items, decoded) = respelled(9, element(bytes)); + let decoded = decoded.unwrap(); + assert_eq!(decoded.gas_limit, U256::from(value)); + assert_eq!(decoded.kept().is_some_and(|k| k.gas_limit.is_some()), kept); + writes_back_as(&decoded, &items); + } + // Past 256 bits the value can't be held, and the bytes are. + let past = [&[1][..], &[0; 32]].concat(); + let (items, decoded) = respelled(9, element(&past)); + let decoded = decoded.unwrap(); + assert_eq!(decoded.gas_limit, U256::MAX); + assert_eq!(decoded.kept().unwrap().gas_limit, Some(Bytes::from(past))); + writes_back_as(&decoded, &items); + } + + /// rskj reads an empty coinbase as its null address, and anything but + /// that or 20 bytes not at all. + #[test] + fn an_empty_coinbase_is_rskjs_null_address() { + let (items, decoded) = respelled(2, element(&[])); + let decoded = decoded.unwrap(); + assert!(decoded.kept().unwrap().null_coinbase); + assert_eq!(decoded.beneficiary, Address::ZERO); + writes_back_as(&decoded, &items); + let zero = Header { + beneficiary: Address::ZERO, + ..standard_test_header() + }; + assert_ne!( + decoded.hash(), + zero.hash(), + "the zero address is another coinbase" + ); + for length in [1, 19, 21, 32] { + assert!( + respelled(2, element(&vec![0x33; length])).1.is_err(), + "{length} bytes" + ); + } + } + + /// No rule reads an uncle's uncles hash or roots, so rskj takes them of + /// any length, a root of none being the empty trie's hash. None of them + /// is a hash a block can be checked against. + #[test] + fn an_uncles_hash_or_root_of_any_length_is_kept() { + for index in [1, 3, 4, 5] { + for length in [1, 31, 33] { + let (items, decoded) = respelled(index, element(&vec![0x22; length])); + let decoded = decoded.unwrap(); + assert!(decoded.kept().is_some(), "field {index}, {length} bytes"); + assert!(!decoded.has_ommers_hash(B256::ZERO)); + writes_back_as(&decoded, &items); + } + } + let (items, decoded) = respelled(1, element(&[])); + writes_back_as(&decoded.unwrap(), &items); + assert!(!respelled(3, element(&[0x44; 31])) + .1 + .unwrap() + .has_state_root(B256::ZERO)); + assert!(!respelled(4, element(&[0x55; 33])) + .1 + .unwrap() + .has_transactions_root(B256::ZERO)); + assert!(!respelled(5, element(&[0x66; 1])) + .1 + .unwrap() + .has_receipts_root(B256::ZERO)); + for index in [3, 4, 5] { + let (mut items, decoded) = respelled(index, element(&[])); + let decoded = decoded.unwrap(); + assert!( + decoded.kept().is_none(), + "field {index} is the empty trie's hash" + ); + items[index] = element(EMPTY_TRIE_HASH.as_slice()); + writes_back_as(&decoded, &items); + } + } + + /// A version-0 header's logs bloom is whatever slot 6 holds, of any + /// length, and rskj checks a block's against the bloom it computes, which + /// a bloom kept for its length is not, even 256 zeros where it is none. + #[test] + fn a_logs_bloom_of_any_length_is_kept_and_is_no_blocks() { + let commitment = [&[0xe2, 0x01, 0xa0][..], &[0x55; 32]].concat(); + for bloom in [vec![0; 255], vec![], commitment] { + let (items, decoded) = respelled(6, element(&bloom)); + let decoded = decoded.unwrap(); + assert!(!decoded.has_logs_bloom(&Bloom::ZERO)); + writes_back_as(&decoded, &items); + } + let header = standard_test_header(); + assert!(header.has_logs_bloom(&header.logs_bloom)); + } + + /// Only where RSKIP-351 is active does slot 6 of a header without its + /// version, as one sent compressed, hold the commitment to its extension. + /// Before it, rskj reads the same bytes as the bloom. Either way they go + /// back as they came, and hash the same. + #[test] + fn a_commitment_in_the_blooms_slot_is_one_only_from_rskip351() { + use crate::config::ActivationHeights; + let commitment = [&[0xe2, 0x01, 0xa0][..], &[0x55; 32]].concat(); + let mut items = standard_items(); + items[6] = element(&commitment); + let bytes = list_of(&items); + let read = |activations: &ActivationHeights| { + Header::decode_with_hash_and_activations(&mut bytes.as_slice(), activations).unwrap() + }; + // Mainnet's heights, and the same with RSKIP-351 from block 1, as + // rskj's regtest has it. The header is #42. + let before = ActivationHeights::mainnet(); + let from = ActivationHeights { + rskip351: Some(1), + ..ActivationHeights::mainnet() + }; + + let bloom = read(&before); + assert_eq!(bloom.extension_data, None); + assert_eq!( + bloom.kept().unwrap().logs_bloom, + Some(Bytes::from(commitment.clone())) + ); + let committed = read(&from); + assert_eq!( + committed.extension_data, + Some(Bytes::from(element(&commitment))) + ); + assert!(committed.kept().is_none()); + assert!(committed.extension_commitment().is_some_and(|c| c.is_ok())); + assert!( + !committed.has_logs_bloom(&Bloom::ZERO), + "a commitment is no bloom" + ); + for header in [&bloom, &committed] { + let mut written = Vec::new(); + header.encode(&mut written); + assert_eq!(written, bytes); + assert_eq!(header.hash(), keccak256(&bytes)); + } + } + + /// A header that carries its version holds its bloom in slot 6, whatever + /// its shape, as rskj reads one that comes in full, and is hashed with + /// the commitment to the extension rskj builds from it: an uncle's bloom + /// of another length goes into its extension's hash as it came. + #[test] + fn a_versioned_headers_bloom_of_any_shape_is_its_bloom() { + use crate::config::ActivationHeights; + use crate::types::header_extension::BlockHeaderExtension; + let header = Header { + version_fields: Some(HeaderVersionFields { + version: 1, + edges: None, + base_event: None, + }), + ..standard_test_header() + }; + let mut written = Vec::new(); + header.encode(&mut written); + let mut items = items_of(&written); + let shaped = [&[0xe2, 0x01, 0xa0][..], &[0x55; 32]].concat(); + items[6] = element(&shaped); + let bytes = list_of(&items); + let from = ActivationHeights { + rskip351: Some(1), + ..ActivationHeights::mainnet() + }; + + let decoded = + Header::decode_with_hash_and_activations(&mut bytes.as_slice(), &from).unwrap(); + assert_eq!(decoded.extension_data, None); + assert_eq!( + decoded.kept().unwrap().logs_bloom, + Some(Bytes::from(shaped.clone())) + ); + let mut again = Vec::new(); + decoded.encode(&mut again); + assert_eq!(again, bytes, "written back as it came"); + // Hashed compressed: the commitment in slot 6, and no version after + // the UMM root. + let extension = BlockHeaderExtension::V1 { + logs_bloom: shaped, + edges: None, + }; + let mut hashed = items.clone(); + hashed[6] = element(&extension.create_extension_data()); + hashed.remove(17); + assert_eq!(decoded.hash(), keccak256(list_of(&hashed))); + } + + /// rskj reads paid fees of any length, where no rule bounds them: an + /// uncle's. Past 256 bits they are kept, without leading zeros. + #[test] + fn paid_fees_past_256_bits_are_kept() { + let past = [&[0, 1][..], &[0; 32]].concat(); + let (mut items, decoded) = respelled(13, element(&past)); + let decoded = decoded.unwrap(); + assert_eq!(decoded.paid_fees, U256::MAX); + assert_eq!( + decoded.kept().unwrap().paid_fees, + Some(Bytes::copy_from_slice(&past[1..])) + ); + items[13] = element(&past[1..]); + writes_back_as(&decoded, &items); + } + + /// What rskj can't read, or reads as a header no rule lets a block or an + /// uncle have, is refused. + #[test] + fn what_rskj_cannot_read_or_no_block_can_have_is_refused() { + for (index, spelling, what) in [ + (0, element(&[0x11; 31]), "a parent hash of 31 bytes"), + (7, element(&[]), "no difficulty"), + (7, element(&[0x80]), "a negative difficulty"), + (7, element(&[1; 33]), "a difficulty past 256 bits"), + (14, element(&[]), "no minimum gas price"), + (14, element(&[0xff]), "a negative minimum gas price"), + (14, element(&[1; 33]), "a minimum gas price past 256 bits"), + ] { + assert!(respelled(index, spelling).1.is_err(), "{what}"); + } + let read = |index, bytes: &[u8]| respelled(index, element(bytes)).1.unwrap(); + assert_eq!(read(7, &[0, 0x80]).difficulty, U256::from(0x80)); + assert_eq!(read(14, &[0, 0xff]).minimum_gas_price, U256::from(0xff)); + } + + /// rskj writes the merged-mining fields back only where one of them has + /// bytes (`hasMiningFields`), so three empty ones hash as none. + #[test] + fn three_empty_merged_mining_fields_are_none() { + let mut items = standard_items(); + items.truncate(17); + let none = list_of(&items); + items.extend([element(&[]), element(&[]), element(&[])]); + let decoded = Header::decode_with_hash(&mut list_of(&items).as_slice()).unwrap(); + assert!(decoded.bitcoin_merged_mining_header.is_none()); + assert_eq!(decoded.hash(), keccak256(&none)); + } + + /// From RSKIP-98 on, block 0 aside, rskj reads the merged-mining fields + /// only with a Bitcoin header of 80 bytes (`validateBitcoinMergedMiningHeader`), + /// and three empty ones not at all, an empty Bitcoin header being null + /// to it. + #[test] + fn from_rskip98_the_bitcoin_header_is_80_bytes() { + use crate::config::ActivationHeights; + let read_under = + |heights: &ActivationHeights, number: u64, btc_header: usize, filled: bool| { + let other = if filled { + Bytes::from_static(b"x") + } else { + Bytes::new() + }; + let header = Header { + number, + bitcoin_merged_mining_header: Some(Bytes::from(vec![0x0b; btc_header])), + bitcoin_merged_mining_merkle_proof: Some(other.clone()), + bitcoin_merged_mining_coinbase_transaction: Some(other), + ..standard_test_header() + }; + let mut bytes = Vec::new(); + header.encode(&mut bytes); + Header::decode_with_activations(&mut bytes.as_slice(), heights) + }; + let mainnet = ActivationHeights::mainnet(); + let read = |number, btc_header, filled| read_under(&mainnet, number, btc_header, filled); + let orchid = mainnet.orchid; + assert!(read(orchid, 80, true).is_ok()); + for (btc_header, filled) in [(79, true), (81, true), (0, true), (0, false)] { + assert!( + read(orchid, btc_header, filled).is_err(), + "{btc_header} bytes" + ); + } + assert!(read(orchid - 1, 79, true).is_ok()); + assert!(read(orchid - 1, 0, false).is_ok_and(|h| h.bitcoin_merged_mining_header.is_none())); + // Testnet has RSKIP-98 from genesis, which is no merged-mined block. + let testnet = ActivationHeights::testnet(); + assert!(read_under(&testnet, 0, 79, true).is_ok()); + assert!(read_under(&testnet, 1, 79, true).is_err()); + } + #[test] fn test_decode_with_hash_caches_hash() { let header = standard_test_header(); @@ -917,6 +1519,7 @@ mod tests { bitcoin_merged_mining_coinbase_transaction: Some(Bytes::from(hex::decode("00000000000000c08c2bae9460a30c6a03f7a557813a68cebdc1d38c70ffd6363ac537fb20c15d4cbd3de0fd7404d31200000000000000002952534b424c4f434b3aa40e3d3aad7ee477a2f38bd83c6e22413b231f48e0b831214589ecde9fb7e24e00000000").unwrap())), cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, }; let expected = b256!("d444f85747420e624055e16285d2fd5a14a38fb1932a378a336d1e39e66e3db1"); @@ -978,6 +1581,7 @@ mod tests { bitcoin_merged_mining_coinbase_transaction: Some(Bytes::from(hex::decode("000000000000014013a11ba36836286d0115b1c5c58a42ad692d3aeeaedae25686175928146d440f8931d770ea29b3d5f1dd2eabaf3724518831d656f447230000000000000000002b6a2952534b424c4f434b3afe829f5c494b038c03268d18d0e338bb162f06b69ebc2a5e8f871a1d008d425000000000").unwrap())), cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, }; assert_eq!( @@ -1075,6 +1679,7 @@ mod tests { bitcoin_merged_mining_coinbase_transaction: Some(Bytes::new()), cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, }; let mut encoded = Vec::new(); @@ -1182,6 +1787,7 @@ mod tests { bitcoin_merged_mining_coinbase_transaction: Some(Bytes::from(hex::decode("000000000000008010685626cadc65f677c14b223755cfb6de56a4f933683312b254ee9307a397c46088ac0000000000000000266a24aa21a9ed401b9dfa5baf569d3af3b0e004a20541d5fb64a037d8670b764a7dbc40ad247500000000000000002a6a52534b424c4f434b3a92c99df0f40bcf1e4dc6c2d3ac5407b2caa26cfc09d51fa4e030f318005b8d8000000000").unwrap())), cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, }; assert_eq!(header.hash(), b256!("afe29f290437b18474a9d540ffe7379ee4f9d0149746644dc13de067d4cf13cd")); } @@ -1465,6 +2071,7 @@ mod versioned_wire_header_tests { let derived = Header { cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, ..completed.clone() }; assert_eq!( diff --git a/crates/core/src/validation/block_rules.rs b/crates/core/src/validation/block_rules.rs index 810c13c..9bcfbd7 100644 --- a/crates/core/src/validation/block_rules.rs +++ b/crates/core/src/validation/block_rules.rs @@ -202,6 +202,7 @@ mod tests { bitcoin_merged_mining_coinbase_transaction: None, cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, } } diff --git a/crates/core/src/validation/difficulty.rs b/crates/core/src/validation/difficulty.rs index c7f994b..fecfe2f 100644 --- a/crates/core/src/validation/difficulty.rs +++ b/crates/core/src/validation/difficulty.rs @@ -165,6 +165,7 @@ mod rskip97_tests { bitcoin_merged_mining_coinbase_transaction: None, cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, }; h.cached_hash = None; h diff --git a/crates/core/src/validation/header_rules.rs b/crates/core/src/validation/header_rules.rs index a948493..bcaea0f 100644 --- a/crates/core/src/validation/header_rules.rs +++ b/crates/core/src/validation/header_rules.rs @@ -161,8 +161,10 @@ impl HeaderValidator for GasLimitBoundsRule { /// regtest has `rskipUMM = 1`, and both regtests are read here, rustock's /// own and one imported from rskj. /// - No RSKIP351 fields where RSKIP351 is inactive, as it is on mainnet: no -/// version, edges or base event after the UMM root, and a 256-byte logs -/// bloom rather than the extension's commitment in its place. +/// version, edges or base event after the UMM root, and no commitment to +/// an extension in the logs bloom's place. The decoder reads whatever is +/// there as the bloom, of any length, as rskj does, so only a header built +/// here could hold one. /// /// An uncle is read the same way, at its own height. pub struct HeaderLayoutRule { diff --git a/crates/core/src/validation/tests.rs b/crates/core/src/validation/tests.rs index bb7b85b..51eef4d 100644 --- a/crates/core/src/validation/tests.rs +++ b/crates/core/src/validation/tests.rs @@ -36,6 +36,7 @@ fn create_dummy_header(number: u64, timestamp: u64, parent_hash: B256) -> Header bitcoin_merged_mining_coinbase_transaction: None, cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, } } diff --git a/crates/execution/src/env.rs b/crates/execution/src/env.rs index c1438a9..788f63c 100644 --- a/crates/execution/src/env.rs +++ b/crates/execution/src/env.rs @@ -123,6 +123,7 @@ mod tests { bitcoin_merged_mining_coinbase_transaction: None, cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, } } diff --git a/crates/execution/src/executor.rs b/crates/execution/src/executor.rs index 16663e6..ea3c91c 100644 --- a/crates/execution/src/executor.rs +++ b/crates/execution/src/executor.rs @@ -2502,6 +2502,7 @@ mod tests { bitcoin_merged_mining_coinbase_transaction: None, cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, } } diff --git a/crates/execution/src/mining/template.rs b/crates/execution/src/mining/template.rs index ac9520f..9852e76 100644 --- a/crates/execution/src/mining/template.rs +++ b/crates/execution/src/mining/template.rs @@ -272,6 +272,7 @@ impl BlockTemplateBuilder { bitcoin_merged_mining_coinbase_transaction: None, cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, }; // The probe: execute the candidates once, in selection order, to diff --git a/crates/execution/src/mining/tests.rs b/crates/execution/src/mining/tests.rs index 1a63218..2c64cd7 100644 --- a/crates/execution/src/mining/tests.rs +++ b/crates/execution/src/mining/tests.rs @@ -91,6 +91,7 @@ fn header_at(number: u64, parent_hash: B256, state_root: B256) -> Header { bitcoin_merged_mining_coinbase_transaction: None, cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, } } diff --git a/crates/execution/src/mining/workload.rs b/crates/execution/src/mining/workload.rs index e9a1fdd..dc7ebc6 100644 --- a/crates/execution/src/mining/workload.rs +++ b/crates/execution/src/mining/workload.rs @@ -726,6 +726,7 @@ fn chain_header( bitcoin_merged_mining_coinbase_transaction: None, cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, } } @@ -1077,6 +1078,7 @@ mod tests { bitcoin_merged_mining_coinbase_transaction: None, cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, } } } diff --git a/crates/execution/src/native/block_header.rs b/crates/execution/src/native/block_header.rs index 5cb71d1..e3b83f3 100644 --- a/crates/execution/src/native/block_header.rs +++ b/crates/execution/src/native/block_header.rs @@ -346,6 +346,7 @@ mod tests { bitcoin_merged_mining_coinbase_transaction: None, cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, } } diff --git a/crates/execution/src/processor.rs b/crates/execution/src/processor.rs index f1ac808..7b73d5f 100644 --- a/crates/execution/src/processor.rs +++ b/crates/execution/src/processor.rs @@ -624,7 +624,7 @@ impl BlockProcessor { &receipts, self.hardfork_cfg.has_unitrie_state_root(header.number), ); - root == header.receipts_root + header.has_receipts_root(root) } /// Trace one transaction of a block, by re-executing the block. @@ -905,7 +905,7 @@ impl BlockProcessor { &block.transactions, self.hardfork_cfg.has_unitrie_state_root(header.number), ); - if computed_tx_root != header.transactions_root { + if !header.has_transactions_root(computed_tx_root) { return Err(ProcessError::TransactionsRootMismatch { header: header.transactions_root, computed: computed_tx_root, @@ -913,7 +913,7 @@ impl BlockProcessor { } let computed_ommers_hash = compute_ommers_hash(&block.ommers); - if computed_ommers_hash != header.ommers_hash { + if !header.has_ommers_hash(computed_ommers_hash) { return Err(ProcessError::OmmersHashMismatch { header: header.ommers_hash, computed: computed_ommers_hash, @@ -953,7 +953,7 @@ impl BlockProcessor { } if self.hardfork_cfg.has_unitrie_state_root(header.number) { - if result.state_root_hash != header.state_root { + if !header.has_state_root(result.state_root_hash) { return Err(ProcessError::StateRootMismatch { header: header.state_root, computed: result.state_root_hash, @@ -991,7 +991,7 @@ impl BlockProcessor { } } - if result.receipts_root != header.receipts_root { + if !header.has_receipts_root(result.receipts_root) { // Dump computed receipts to help pinpoint the diverging transaction. for (i, r) in result.receipts.iter().enumerate() { debug!( @@ -1015,7 +1015,7 @@ impl BlockProcessor { }); } - if result.logs_bloom != header.logs_bloom { + if !header.has_logs_bloom(&result.logs_bloom) { return Err(ProcessError::LogsBloomMismatch); } @@ -1310,6 +1310,7 @@ mod tests { bitcoin_merged_mining_coinbase_transaction: None, cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, } } diff --git a/crates/execution/src/remasc.rs b/crates/execution/src/remasc.rs index 74305f0..1266f12 100644 --- a/crates/execution/src/remasc.rs +++ b/crates/execution/src/remasc.rs @@ -904,6 +904,7 @@ mod tests { bitcoin_merged_mining_coinbase_transaction: None, cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, } } diff --git a/crates/networking/src/protocol/rsk.rs b/crates/networking/src/protocol/rsk.rs index bb8a7e8..dcd0191 100644 --- a/crates/networking/src/protocol/rsk.rs +++ b/crates/networking/src/protocol/rsk.rs @@ -1484,6 +1484,7 @@ mod tests { bitcoin_merged_mining_coinbase_transaction: None, cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, } } @@ -1554,6 +1555,7 @@ mod tests { bitcoin_merged_mining_coinbase_transaction: None, cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, }; let resp = BodyResponse { @@ -1924,6 +1926,7 @@ mod headers_with_uncles_tests { bitcoin_merged_mining_coinbase_transaction: None, cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, } } diff --git a/crates/networking/src/protocol/snap.rs b/crates/networking/src/protocol/snap.rs index 13c6021..fc35ab0 100644 --- a/crates/networking/src/protocol/snap.rs +++ b/crates/networking/src/protocol/snap.rs @@ -641,6 +641,7 @@ mod tests { bitcoin_merged_mining_coinbase_transaction: None, cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, } } diff --git a/crates/rpc/src/tests.rs b/crates/rpc/src/tests.rs index ff7c742..c5061f6 100644 --- a/crates/rpc/src/tests.rs +++ b/crates/rpc/src/tests.rs @@ -1,7 +1,7 @@ use crate::server::{dispatch_for_test, RpcState}; use crate::types::*; use alloy_primitives::{Address, Bloom, B256, U256, Bytes}; -use rustock_core::config::ChainConfig; +use rustock_core::config::{ActivationHeights, ChainConfig}; use rustock_core::types::header::Header; use rustock_networking::peers::PeerStore; use rustock_storage::BlockStore; @@ -39,12 +39,23 @@ fn test_header(number: u64) -> Header { bitcoin_merged_mining_coinbase_transaction: None, cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, } } fn setup_state() -> (RpcState, tempfile::TempDir) { + setup_state_with(|store| store) +} + +/// [`setup_state`] over a store that reads headers as a testnet node's does, +/// under testnet's heights: for a test that holds testnet's headers. +fn setup_testnet_state() -> (RpcState, tempfile::TempDir) { + setup_state_with(|store| store.with_activations(ActivationHeights::testnet())) +} + +fn setup_state_with(store: impl FnOnce(BlockStore) -> BlockStore) -> (RpcState, tempfile::TempDir) { let tmp = tempfile::tempdir().unwrap(); - let store = Arc::new(BlockStore::open(tmp.path()).unwrap()); + let store = Arc::new(store(BlockStore::open(tmp.path()).unwrap())); let header = test_header(42); store.update_head(&header, U256::from(42_000)).unwrap(); @@ -6374,10 +6385,11 @@ const TESTNET_FROM_PEERS: &str = concat!( "352ad0c0b7e56a1ba4fcac4da0d7693e516ef40a007e0c7000000000", ); -/// A header as the network reads one. +/// A header as testnet's nodes read one. fn testnet_header(hex: &str) -> Header { let bytes = hex::decode(hex).unwrap(); - Header::decode_with_hash(&mut bytes.as_slice()).unwrap() + Header::decode_with_hash_and_activations(&mut bytes.as_slice(), &ActivationHeights::testnet()) + .unwrap() } /// A header in full, as hex. @@ -6527,7 +6539,7 @@ async fn raw_blocks_give_a_run_of_the_chain_oldest_first() { /// held. #[tokio::test] async fn a_testnet_block_comes_with_its_header_compressed() { - let (state, _tmp) = setup_state(); + let (state, _tmp) = setup_testnet_state(); follow(&state.store, &testnet_header(TESTNET_FULL)); assert_eq!( blocks_of(raw_blocks(&state, "0x7e0c99", "0x7e0c99").await), @@ -6608,8 +6620,9 @@ async fn raw_blocks_stop_before_a_block_not_held_whole_or_not_following() { assert_eq!(blocks_of(raw_blocks(&state, "0x64", "0x68").await).len(), 2); assert!(blocks_of(raw_blocks(&state, "0x66", "0x68").await).is_empty()); - // It holds them only compressed, as no block carries an uncle. - let (state, _tmp) = setup_state(); + // It holds them only compressed, as no block carries an uncle: a store + // reading them as a testnet node's does. + let (state, _tmp) = setup_testnet_state(); let hashes = follow_run( &state.store, 100..=104, diff --git a/crates/storage/src/freezer.rs b/crates/storage/src/freezer.rs index 848d84a..32dcba6 100644 --- a/crates/storage/src/freezer.rs +++ b/crates/storage/src/freezer.rs @@ -65,6 +65,7 @@ use alloy_primitives::B256; use anyhow::{anyhow, bail, Context, Result}; +use rustock_core::config::ActivationHeights; use rustock_core::Header; use alloy_rlp::Encodable; use std::fs::{File, OpenOptions}; @@ -286,6 +287,8 @@ pub struct Freezer { uncles: Mutex, /// Opened by [`Freezer::open_read_only`]: every write is refused. read_only: bool, + /// The heights headers are read under: see [`Freezer::with_activations`]. + activations: Option, } /// File-name stems for the two parallel stores. @@ -321,9 +324,27 @@ impl Freezer { write: Mutex::new(headers), uncles: Mutex::new(uncles), read_only: true, + activations: None, }) } + /// This freezer reading its headers under `activations` rather than the + /// process-wide heights, as + /// [`BlockStore::with_activations`](crate::BlockStore::with_activations) + /// does. + pub fn with_activations(mut self, activations: ActivationHeights) -> Self { + self.activations = Some(activations); + self + } + + /// The heights this freezer reads headers under. + fn activations(&self) -> &ActivationHeights { + match &self.activations { + Some(activations) => activations, + None => ActivationHeights::current(), + } + } + /// Refuses a write to a freezer opened read-only. Rolling to a new data /// file opens it to create and truncate, which in another process's /// freezer is that writer's next file, so read-only handles alone would @@ -380,6 +401,7 @@ impl Freezer { write: Mutex::new(headers), uncles: Mutex::new(uncles), read_only: false, + activations: None, }) } @@ -632,7 +654,7 @@ impl Freezer { let mut out = Vec::new(); while !body.is_empty() { out.push( - Header::decode_with_hash(&mut body) + Header::decode_with_hash_and_activations(&mut body, self.activations()) .map_err(|e| anyhow!("decoding frozen uncle for #{number}: {e:?}"))?, ); } @@ -732,7 +754,8 @@ impl Freezer { for i in slots { let e = entries[i]; - out[i] = decode_header(&buf[(e.start - lo) as usize..(e.end - lo) as usize]); + let bytes = &buf[(e.start - lo) as usize..(e.end - lo) as usize]; + out[i] = decode_header(bytes, self.activations()); } } @@ -850,9 +873,8 @@ fn furthest_end(index: &mut File, end_number: u64, file: u16) -> Result { Ok(furthest) } -fn decode_header(bytes: &[u8]) -> Option
{ - use alloy_rlp::Decodable; - Header::decode(&mut &bytes[..]).ok() +fn decode_header(bytes: &[u8], activations: &ActivationHeights) -> Option
{ + Header::decode_with_activations(&mut &bytes[..], activations).ok() } #[cfg(test)] @@ -890,6 +912,7 @@ pub(crate) mod tests { bitcoin_merged_mining_coinbase_transaction: None, cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, } } diff --git a/crates/storage/src/lib.rs b/crates/storage/src/lib.rs index 053b607..6d05ddd 100644 --- a/crates/storage/src/lib.rs +++ b/crates/storage/src/lib.rs @@ -23,6 +23,7 @@ pub use flat_trie_store::FlatTrieStore; pub use epoch_backend::TrieEngine; use rocksdb::{DB, Options, ColumnFamilyDescriptor, WriteBatch}; +use rustock_core::config::ActivationHeights; use rustock_core::{Block, Header, Receipt, Transaction}; use alloy_primitives::{B256, U256}; use alloy_rlp::{Decodable, Encodable, Header as RlpHeader}; @@ -164,6 +165,8 @@ pub struct BlockStore { /// the freezer holds is also in `CF_HEADERS`, so this only ever changes how /// fast a header is found, never whether it is found. freezer: std::sync::RwLock>>, + /// The heights headers are read under: see [`BlockStore::with_activations`]. + activations: Option, } /// RocksDB settings for a bulk import. @@ -325,7 +328,11 @@ impl BlockStore { DB::open_cf_for_read_only(&opts, path.as_ref(), existing, false) .context("Failed to open RocksDB read-only")? }; - Ok(Self { db: Arc::new(db), freezer: std::sync::RwLock::new(None) }) + Ok(Self { + db: Arc::new(db), + freezer: std::sync::RwLock::new(None), + activations: None, + }) } fn open_with>(path: P, tuning: Option) -> Result { @@ -365,7 +372,35 @@ impl BlockStore { ]; let db = DB::open_cf_descriptors(&opts, path, cfs).context("Failed to open RocksDB")?; - Ok(Self { db: Arc::new(db), freezer: std::sync::RwLock::new(None) }) + Ok(Self { + db: Arc::new(db), + freezer: std::sync::RwLock::new(None), + activations: None, + }) + } + + /// This store reading its headers under `activations` rather than the + /// process-wide heights ([`ActivationHeights::install`]), as a node of the + /// chain they belong to does: for a test that holds another chain's + /// headers. Whether a header's slot 6 holds its logs bloom or the + /// commitment to its extension, for one, depends on them, as it does for + /// rskj's store, which reads through its `BlockFactory`. + pub fn with_activations(mut self, activations: ActivationHeights) -> Self { + self.activations = Some(activations); + self + } + + /// The heights this store reads headers under. + fn activations(&self) -> &ActivationHeights { + match &self.activations { + Some(activations) => activations, + None => ActivationHeights::current(), + } + } + + /// A stored header, read under this store's heights. + fn read_header(&self, buf: &mut &[u8]) -> alloy_rlp::Result
{ + Header::decode_with_activations(buf, self.activations()) } /// Scans the headers column family for the highest block number. @@ -385,7 +420,7 @@ impl BlockStore { continue; } let mut slice: &[u8] = &v; - if let Ok(h) = Header::decode(&mut slice) { + if let Ok(h) = self.read_header(&mut slice) { if best.is_none_or(|(n, _)| h.number > n) { best = Some((h.number, B256::from_slice(&k))); } @@ -507,7 +542,7 @@ impl BlockStore { // The header is keyed by the hash the node computed for it, which // for genesis and for Java-encoded headers is not the hash of a // re-encoding. Index under the stored key, never a recomputed one. - let number = match Header::decode(&mut value.as_ref()) { + let number = match self.read_header(&mut value.as_ref()) { Ok(h) => h.number, Err(e) => { warn!( @@ -550,7 +585,7 @@ impl BlockStore { let bytes = self.db.get_cf(self.cf(CF_HEADERS)?, hash.as_slice()) .context("Failed to read header")?; if let Some(b) = bytes { - return Ok(Some(Header::decode(&mut b.as_slice())?)); + return Ok(Some(self.read_header(&mut b.as_slice())?)); } // Not in the database. It may have been frozen and then deleted, in @@ -605,7 +640,7 @@ impl BlockStore { let one = |h: &B256| -> Option
{ let cf = self.cf(CF_HEADERS).ok()?; let bytes = self.db.get_cf(cf, h.as_slice()).ok()??; - Header::decode(&mut bytes.as_slice()).ok() + self.read_header(&mut bytes.as_slice()).ok() }; let threads = read_threads(); @@ -1360,7 +1395,7 @@ impl BlockStore { let mut ommers_buf = &body[..ommers_h.payload_length]; let mut ommers = Vec::new(); while !ommers_buf.is_empty() { - ommers.push(Header::decode(&mut ommers_buf)?); + ommers.push(self.read_header(&mut ommers_buf)?); } Ok(Some((transactions, ommers))) @@ -1388,7 +1423,7 @@ impl BlockStore { let mut ommers_buf = &body[..ommers_h.payload_length]; let mut ommers = Vec::new(); while !ommers_buf.is_empty() { - ommers.push(Header::decode(&mut ommers_buf)?); + ommers.push(self.read_header(&mut ommers_buf)?); } Ok(Some(ommers)) } @@ -2073,6 +2108,70 @@ mod tests { bitcoin_merged_mining_coinbase_transaction: None, cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, + } + } + + /// A store reads its headers under its chain's heights, the process's + /// unless set: where RSKIP-351 is active, slot 6 of a header held + /// compressed is the commitment to its extension, and before it the + /// same bytes are the logs bloom, as rskj's `BlockFactory` reads them. + /// The freezer reads its own alike. + #[test] + fn a_store_reads_headers_under_its_chains_heights() { + use rustock_core::config::ActivationHeights; + let mut commitment = vec![0xe2, 0x01, 0xa0]; + commitment.extend_from_slice(&[0x55; 32]); + let mut slot = Vec::new(); + commitment.as_slice().encode(&mut slot); + let compressed = Header { + extension_data: Some(Bytes::from(slot)), + ..dummy_header(1) + }; + let hash = compressed.hash(); + // rskj's regtest has RSKIP-351 from block 1. + let regtest = ActivationHeights::regtest(); + + let dir = tempdir().unwrap(); + { + let store = BlockStore::open(dir.path()) + .unwrap() + .with_activations(regtest.clone()); + store.put_header(&compressed).unwrap(); + store + .put_body(hash, &[], std::slice::from_ref(&compressed)) + .unwrap(); + assert_eq!(store.header(hash).unwrap(), Some(compressed.clone())); + assert_eq!(store.uncles(hash).unwrap(), Some(vec![compressed.clone()])); + } + // This process's heights are mainnet's, which has no RSKIP-351. + let store = BlockStore::open(dir.path()).unwrap(); + let read = store.header(hash).unwrap().unwrap(); + assert_eq!(read.extension_data, None); + assert_eq!(read.hash(), hash, "the same bytes, the same header"); + + let frozen = tempdir().unwrap(); + let freezer = crate::freezer::Freezer::open(frozen.path()) + .unwrap() + .with_activations(regtest); + freezer.put(1, &compressed).unwrap(); + freezer + .put_uncles(1, std::slice::from_ref(&compressed)) + .unwrap(); + freezer.sync().unwrap(); + let read_back = |freezer: crate::freezer::Freezer| { + let header = freezer.header(1).unwrap().unwrap(); + [header, freezer.uncles(1).unwrap().unwrap().remove(0)] + }; + let committed = read_back(freezer); + let bloom = read_back(crate::freezer::Freezer::open(frozen.path()).unwrap()); + for (header, uncle) in [(&committed[0], &committed[1]), (&bloom[0], &bloom[1])] { + assert_eq!(header.extension_data, uncle.extension_data); + } + assert!(committed[0].extension_data.is_some()); + assert_eq!(bloom[0].extension_data, None); + for header in committed.iter().chain(&bloom) { + assert_eq!(header.hash(), hash); } } diff --git a/crates/storage/src/position.rs b/crates/storage/src/position.rs index 3934f70..b07e776 100644 --- a/crates/storage/src/position.rs +++ b/crates/storage/src/position.rs @@ -573,6 +573,7 @@ mod tests { bitcoin_merged_mining_coinbase_transaction: None, cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, } } diff --git a/crates/storage/src/pruner.rs b/crates/storage/src/pruner.rs index 77a13f7..46f8817 100644 --- a/crates/storage/src/pruner.rs +++ b/crates/storage/src/pruner.rs @@ -466,6 +466,7 @@ mod tests { bitcoin_merged_mining_coinbase_transaction: None, cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, } } diff --git a/crates/storage/tests/position_simulator.rs b/crates/storage/tests/position_simulator.rs index 312bcc1..a85de04 100644 --- a/crates/storage/tests/position_simulator.rs +++ b/crates/storage/tests/position_simulator.rs @@ -84,6 +84,7 @@ fn header(number: u64, parent: B256, salt: u64) -> Header { bitcoin_merged_mining_coinbase_transaction: None, cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, } } diff --git a/crates/storage/tests/stall_replays.rs b/crates/storage/tests/stall_replays.rs index ee439c1..24d25d1 100644 --- a/crates/storage/tests/stall_replays.rs +++ b/crates/storage/tests/stall_replays.rs @@ -51,6 +51,7 @@ fn header(number: u64, parent: B256, salt: u64) -> Header { bitcoin_merged_mining_coinbase_transaction: None, cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, } } diff --git a/crates/sync/src/gas_price.rs b/crates/sync/src/gas_price.rs index 11da7f3..0a5f96e 100644 --- a/crates/sync/src/gas_price.rs +++ b/crates/sync/src/gas_price.rs @@ -336,6 +336,7 @@ mod tests { bitcoin_merged_mining_coinbase_transaction: None, cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, } } diff --git a/crates/sync/src/import.rs b/crates/sync/src/import.rs index 704b92f..f4d8590 100644 --- a/crates/sync/src/import.rs +++ b/crates/sync/src/import.rs @@ -465,6 +465,7 @@ mod tests { bitcoin_merged_mining_coinbase_transaction: None, cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, }; let block = Block { header, diff --git a/crates/sync/src/invariant.rs b/crates/sync/src/invariant.rs index a7ebb40..b10de0d 100644 --- a/crates/sync/src/invariant.rs +++ b/crates/sync/src/invariant.rs @@ -353,6 +353,7 @@ mod tests { bitcoin_merged_mining_coinbase_transaction: None, cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, } } diff --git a/crates/sync/src/rollback.rs b/crates/sync/src/rollback.rs index 66e938a..f119211 100644 --- a/crates/sync/src/rollback.rs +++ b/crates/sync/src/rollback.rs @@ -278,6 +278,7 @@ mod tests { bitcoin_merged_mining_coinbase_transaction: None, cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, } } diff --git a/crates/sync/src/sampler.rs b/crates/sync/src/sampler.rs index 0603f71..098f10b 100644 --- a/crates/sync/src/sampler.rs +++ b/crates/sync/src/sampler.rs @@ -543,6 +543,7 @@ pub(crate) mod tests { bitcoin_merged_mining_coinbase_transaction: None, cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, } } diff --git a/crates/sync/src/snap/forward_tests.rs b/crates/sync/src/snap/forward_tests.rs index fa58d86..1fa700f 100644 --- a/crates/sync/src/snap/forward_tests.rs +++ b/crates/sync/src/snap/forward_tests.rs @@ -38,6 +38,7 @@ fn header(number: u64, parent: B256, difficulty: u64) -> Header { bitcoin_merged_mining_coinbase_transaction: None, cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, } } diff --git a/crates/sync/src/snap/header_tests.rs b/crates/sync/src/snap/header_tests.rs index 6fe7e1b..7606cd1 100644 --- a/crates/sync/src/snap/header_tests.rs +++ b/crates/sync/src/snap/header_tests.rs @@ -38,6 +38,7 @@ fn header(number: u64, parent: B256) -> Header { bitcoin_merged_mining_coinbase_transaction: None, cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, } } diff --git a/crates/sync/src/snap/indexer_tests.rs b/crates/sync/src/snap/indexer_tests.rs index af57f6b..1054728 100644 --- a/crates/sync/src/snap/indexer_tests.rs +++ b/crates/sync/src/snap/indexer_tests.rs @@ -31,6 +31,7 @@ fn header(number: u64, parent: B256) -> Header { bitcoin_merged_mining_coinbase_transaction: None, cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, } } diff --git a/crates/sync/src/snap/session_tests.rs b/crates/sync/src/snap/session_tests.rs index 1b2b456..1a69073 100644 --- a/crates/sync/src/snap/session_tests.rs +++ b/crates/sync/src/snap/session_tests.rs @@ -72,6 +72,7 @@ fn header(number: u64, parent: B256, state_root: B256, difficulty: u64) -> Heade bitcoin_merged_mining_coinbase_transaction: None, cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, } } diff --git a/crates/sync/src/snap/tests.rs b/crates/sync/src/snap/tests.rs index 47beede..f5c1f93 100644 --- a/crates/sync/src/snap/tests.rs +++ b/crates/sync/src/snap/tests.rs @@ -630,6 +630,7 @@ fn server_fixture(n: usize) -> (Arc, Peer, u64) { bitcoin_merged_mining_coinbase_transaction: None, cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, }; let hash = header.hash(); store.put_header(&header).expect("put header"); @@ -1151,6 +1152,7 @@ fn put_checkpoint(store: &Arc, number: u64, root: B bitcoin_merged_mining_coinbase_transaction: None, cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, }; let hash = header.hash(); store.put_header(&header).expect("put header"); diff --git a/crates/sync/src/tests.rs b/crates/sync/src/tests.rs index bb1e492..e7602ff 100644 --- a/crates/sync/src/tests.rs +++ b/crates/sync/src/tests.rs @@ -40,6 +40,7 @@ pub(crate) fn dummy_header(number: u64, parent: B256, difficulty: U256) -> Heade bitcoin_merged_mining_coinbase_transaction: None, cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, } } @@ -7642,10 +7643,22 @@ fn compressed( bitcoin_merged_mining_coinbase_transaction: Some(Bytes::new()), cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, ..header } } +/// A store that reads headers as a node of a chain with RSKIP-351 from block +/// 1 does, rskj's regtest: there a header sent compressed holds the +/// commitment to its extension in its logs bloom's slot. +fn rskip351_store() -> Arc { + Arc::new( + BlockStore::open(tempfile::tempdir().unwrap().path()) + .unwrap() + .with_activations(rustock_core::config::ActivationHeights::regtest()), + ) +} + /// A service over `store` with one peer, `asked`, connected. async fn service_with_a_peer(store: Arc, asked: B512) -> SyncService { let verifier = Arc::new(HeaderVerifier::new()); @@ -7666,7 +7679,7 @@ async fn service_with_a_peer(store: Arc, asked: B512) -> SyncService /// and the batch carries the completed header to execution. #[tokio::test] async fn a_compressed_header_is_stored_with_the_extension_that_came_beside_its_body() { - let store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let store = rskip351_store(); let genesis = dummy_header(0, B256::ZERO, U256::from(1)); store.update_head(&genesis, U256::from(1)).unwrap(); let extension = an_extension(77); @@ -7765,7 +7778,7 @@ async fn a_compressed_header_is_stored_with_the_extension_that_came_beside_its_b /// header for execution. #[tokio::test] async fn a_followed_compressed_header_takes_the_extension_that_came_beside_its_body() { - let store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let store = rskip351_store(); let genesis = dummy_header(0, B256::ZERO, U256::from(1)); store.update_head(&genesis, U256::from(1)).unwrap(); let extension = an_extension(3); @@ -7820,7 +7833,7 @@ impl ExternalBlockSource for EveryBody { /// keeps its body. #[tokio::test] async fn a_body_stored_without_its_header_extension_is_asked_for_again() { - let store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let store = rskip351_store(); let genesis = dummy_header(0, B256::ZERO, U256::from(1)); store.update_head(&genesis, U256::from(1)).unwrap(); let b1 = compressed(derivable_header(1, genesis.hash()), &an_extension(5)); @@ -7866,7 +7879,7 @@ async fn a_body_stored_without_its_header_extension_is_asked_for_again() { /// bloom, and asks for it again. #[tokio::test] async fn follow_mode_asks_again_for_a_body_stored_without_its_header_extension() { - let store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let store = rskip351_store(); let genesis = dummy_header(0, B256::ZERO, U256::from(1)); store.update_head(&genesis, U256::from(1)).unwrap(); store.put_body(genesis.hash(), &[], &[]).unwrap(); @@ -7905,7 +7918,7 @@ async fn a_served_body_carries_its_header_extension() { use rustock_networking::protocol::rsk::BodyRequest; use rustock_networking::protocol::{P2pHandler, P2pMessage, RskMessage, RskSubMessage}; - let store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let store = rskip351_store(); let extension = an_extension(9); let whole = compressed(dummy_header(1, B256::ZERO, U256::from(1)), &extension) .with_extension(&extension) diff --git a/crates/sync/src/txpool.rs b/crates/sync/src/txpool.rs index 1619e48..06c2f35 100644 --- a/crates/sync/src/txpool.rs +++ b/crates/sync/src/txpool.rs @@ -1147,6 +1147,7 @@ mod tests { bitcoin_merged_mining_coinbase_transaction: None, cached_hash: None, cached_hash_for_merged_mining: None, + kept: None, } } From ea9b13b9b8653f4b9691a651dc53b196266160cd Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Mon, 9 Nov 2026 06:52:47 -0800 Subject: [PATCH 052/264] execution: a header's gas limit read from its bytes, as rskj reads it rskj never holds a header's gas limit as a number. It keeps the bytes the header came with (getGasLimit), and each reader reads them its own way: GasCost.toGas, which holds a block's transactions to it, reads more than eight bytes as Long.MAX_VALUE whatever they hold and throws on eight with the top bit set; ValidGasUsedRule keeps the low 64 bits as a long (BigInteger.longValue); a program's invocation puts the bytes in a DataWord, which throws past 32 (ProgramInvokeImpl), and GASLIMIT pushes that word; the BlockHeader contract returns the bytes; the RPC prints them as a quantity. rustock read the value everywhere, as a u64 where it read it at all. So: - A block whose gas limit is written in nine bytes or more, which a miner may do with leading zeros and rskj keeps, holds its transactions to Long.MAX_VALUE in rskj and to the value here, so rustock refused blocks rskj takes. - GasUsedRule compared a u64 where rskj compares the low 64 bits as a signed long: from 2^63 up nothing fits under the limit in rskj. - block_env_from_header and eth_estimateGas panicked on a minimum gas price, or gas limit, of 2^64 or more (to::). Miners raising the minimum gas price as fast as rskj allows reach 2^64 in about 2,700 blocks, a day. - GASLIMIT and BASEFEE pushed revm's u64s. - getGasLimit returned the value's shortest bytes, where rskj returns the bytes as written: testnet's 12,000,000 is 00 b7 1b 00, sign byte and all. Header::gas_limit_bytes now gives the bytes as rskj holds them, gas_limit_as_gas reads them as GasCost.toGas does, and gas_limit_low_long as ValidGasUsedRule does. The decoder refuses a header with no gas limit, which is null to rskj, and every rule that reads it throws on null. The executor holds transactions to the gas limit read as gas and sizes sublists from it; block_gas_limit, which read the value, is gone. GasUsedRule reads the low long, and GasUsedExceedsLimit's limit is an i64. A program's words. rskj builds a program's invocation from the header for each transaction that runs code (TransactionExecutor.call into code, create with data), and throws, uncaught, where the gas limit is written in more than 32 bytes or the difficulty or minimum gas price is 2^255 or more, whose toByteArray takes a 33rd byte for its sign. The block is not imported, and a miner leaves the transaction out (BlockExecutor.executeInternal). env::header_fits_program_invoke says whether a header fits, and RskHandler::failing_programs fails a transaction as its first frame opens to run code: an error, which fails a block and which the miner drops. GASLIMIT and BASEFEE push the whole values, which rsk_instructions::install takes in BlockWords beside COINBASE's real miner. The block env holds the gas limit as gas and the minimum gas price cut to fit, and nothing panics. getGasLimit returns the bytes, and eth_getBlockBy* and newHeads print the gas limit from them as HexUtils.toQuantityJsonHex does. The miner's step towards its target stops at the top of 256 bits instead of wrapping. Tests read the bytes rskj can be sent each reader's way, run a block whose nine-byte gas limit leaves its sublists room for more, push GASLIMIT and BASEFEE past 2^64, fail what runs code under a header past a program's words while a transfer, an empty creation and the miner go on, and read getGasLimit's bytes and the RPC's gasLimit. Twenty-three one-line mutations each fail a test. Traced runs (eth_estimateGas, debug_trace*, trace_block) go through revm's inspect_run_exec_loop, which skips RskHandler's exec loop, this check and its older rules with it; that is for the RPC work. rustock's own maximum gas limit, which rskj doesn't have, goes next. --- crates/core/src/types/header.rs | 94 ++++++-- crates/core/src/validation/header_rules.rs | 15 +- crates/core/src/validation/mod.rs | 3 +- crates/core/src/validation/tests.rs | 18 ++ crates/core/src/validation/uncles.rs | 4 +- crates/execution/src/env.rs | 86 +++++++- crates/execution/src/executor.rs | 226 ++++++++++++++++---- crates/execution/src/mining/template.rs | 10 +- crates/execution/src/mining/tests.rs | 26 +++ crates/execution/src/native/block_header.rs | 53 +++-- crates/execution/src/rsk_handler.rs | 29 +++ crates/execution/src/rsk_instructions.rs | 95 ++++++-- crates/rpc/src/call.rs | 4 +- crates/rpc/src/helpers.rs | 29 ++- crates/rpc/src/subscribe.rs | 11 +- crates/rpc/src/tests.rs | 22 ++ 16 files changed, 623 insertions(+), 102 deletions(-) diff --git a/crates/core/src/types/header.rs b/crates/core/src/types/header.rs index a8aced5..22fe248 100644 --- a/crates/core/src/types/header.rs +++ b/crates/core/src/types/header.rs @@ -1,6 +1,7 @@ use alloy_primitives::{Address, Bloom, B256, U256, Bytes}; use alloy_rlp::Encodable; use serde::{Deserialize, Serialize}; +use std::borrow::Cow; #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] @@ -93,12 +94,10 @@ pub struct HeaderVersionFields { #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] pub struct KeptFields { /// The gas limit's bytes, where they aren't how rskj's miner writes its - /// value (`BigInteger.toByteArray`): more leading zeros, no sign byte, - /// or none at all. rskj never holds the gas limit as a number. It - /// compares its value with the parent's, but `GasCost.toGas` reads - /// more than eight bytes as `Long.MAX_VALUE`, `DataWord.valueOf` - /// refuses more than 32, and the BlockHeader contract returns the - /// bytes. `gas_limit` holds the value, or `U256::MAX` past 256 bits. + /// value (`BigInteger.toByteArray`): more leading zeros, or no sign + /// byte. rskj never holds the gas limit as a number, and each of its + /// readers reads the bytes its own way: see [`Header::gas_limit_bytes`]. + /// `gas_limit` holds the value, or `U256::MAX` past 256 bits. pub gas_limit: Option, /// An empty coinbase, which rskj reads as its null address, an /// `RskAddress` of no bytes, and writes back empty. `beneficiary` @@ -231,6 +230,15 @@ fn java_signed_bytes(value: &U256) -> Vec { out } +/// `new BigInteger(1, bytes).longValue()`: the low 64 bits of the unsigned +/// value, as a two's-complement `long`. +fn low_long(bytes: &[u8]) -> i64 { + let low = &bytes[bytes.len().saturating_sub(8)..]; + let mut word = [0u8; 8]; + word[8 - low.len()..].copy_from_slice(low); + i64::from_be_bytes(word) +} + impl Header { /// The sixteen fields every RSK header carries, in wire order, encoded the /// way rskj encodes them. @@ -430,10 +438,7 @@ impl Header { } java_signed_bytes(&self.difficulty).as_slice().encode(list); self.number.encode(list); - match kept.and_then(|k| k.gas_limit.as_ref()) { - Some(bytes) => bytes.encode(list), - None => java_signed_bytes(&self.gas_limit).as_slice().encode(list), - } + self.gas_limit_bytes()[..].encode(list); self.gas_used.encode(list); self.timestamp.encode(list); self.extra_data.encode(list); @@ -595,8 +600,11 @@ impl Header { "a difficulty that is none, negative or past 256 bits", ))?; // rskj keeps the gas limit's bytes: see `KeptFields::gas_limit`. + // None is null to it, and every rule that reads a header's gas limit + // throws on null (`new BigInteger(1, getGasLimit())`): a block's + // `GasLimitRule` and an uncle's `ValidGasUsedRule`, for two. let gas_limit = { - let bytes = field(9)?.unwrap_or_default(); + let bytes = field(9)?.ok_or(refused("no gas limit"))?; let value = significant(Some(bytes)); let gas_limit = if value.len() > 32 { U256::MAX @@ -799,6 +807,38 @@ impl Header { self.kept.as_deref() } + /// The gas limit's bytes, as rskj holds them (`getGasLimit`): as they + /// came, or as its miner writes the value. Its readers read them + /// differently: the BlockHeader contract returns them, `GasCost.toGas` + /// and `ValidGasUsedRule` read them as below, a program's invocation + /// can't hold more than 32 (`DataWord.valueOf`), and the rules that + /// compare it with the parent's read the whole value. + pub fn gas_limit_bytes(&self) -> Cow<'_, [u8]> { + match self.kept().and_then(|kept| kept.gas_limit.as_ref()) { + Some(bytes) => Cow::Borrowed(bytes), + None => Cow::Owned(java_signed_bytes(&self.gas_limit)), + } + } + + /// The gas limit as gas, `GasCost.toGas`, which is how rskj holds a + /// block's transactions to it: more than eight bytes are + /// `Long.MAX_VALUE` whatever they hold, and eight with the top bit set + /// are a negative `long`, which it throws on (`None`). + pub fn gas_limit_as_gas(&self) -> Option { + let bytes = self.gas_limit_bytes(); + if bytes.len() > 8 { + return Some(i64::MAX as u64); + } + u64::try_from(low_long(&bytes)).ok() + } + + /// The gas limit as `ValidGasUsedRule` reads it, + /// `new BigInteger(1, bytes).longValue()`: its low 64 bits, as a + /// two's-complement `long`. + pub fn gas_limit_low_long(&self) -> i64 { + low_long(&self.gas_limit_bytes()) + } + /// Whether this header's uncles hash is `hash`. rskj compares the bytes /// it read, and ones kept for not being 32 bytes are no hash. pub fn has_ommers_hash(&self, hash: B256) -> bool { @@ -1187,7 +1227,6 @@ mod tests { (&[0x00, 0x98, 0x96, 0x80][..], 10_000_000u64, false), (&[0x98, 0x96, 0x80], 10_000_000, true), (&[0, 0, 0, 0x98, 0x96, 0x80], 10_000_000, true), - (&[], 0, true), ] { let (items, decoded) = respelled(9, element(bytes)); let decoded = decoded.unwrap(); @@ -1204,6 +1243,36 @@ mod tests { writes_back_as(&decoded, &items); } + /// Each of rskj's readers reads the gas limit's bytes its own way: + /// `GasCost.toGas` takes more than eight as `Long.MAX_VALUE` and throws + /// on eight with the top bit set, and `ValidGasUsedRule` keeps the low + /// 64 bits as a `long`. + #[test] + fn the_gas_limit_reads_as_each_of_rskjs_readers_reads_it() { + const LONG_MAX: u64 = i64::MAX as u64; + let past = [&[1][..], &[0; 31], &[1]].concat(); + for (bytes, gas, long) in [ + // As rskj's miner writes 10,000,000. + (&[0x00, 0x98, 0x96, 0x80][..], Some(10_000_000), 10_000_000), + // The same value in nine bytes. + ( + &[0, 0, 0, 0, 0, 0, 0x98, 0x96, 0x80], + Some(LONG_MAX), + 10_000_000, + ), + // 2^63 in eight bytes, and as rskj's miner writes it. + (&[0x80, 0, 0, 0, 0, 0, 0, 0], None, i64::MIN), + (&[0, 0x80, 0, 0, 0, 0, 0, 0, 0], Some(LONG_MAX), i64::MIN), + // Past 256 bits, held only as bytes. + (&past, Some(LONG_MAX), 1), + ] { + let header = respelled(9, element(bytes)).1.unwrap(); + assert_eq!(&header.gas_limit_bytes()[..], bytes); + assert_eq!(header.gas_limit_as_gas(), gas, "{bytes:02x?}"); + assert_eq!(header.gas_limit_low_long(), long, "{bytes:02x?}"); + } + } + /// rskj reads an empty coinbase as its null address, and anything but /// that or 20 bytes not at all. #[test] @@ -1407,6 +1476,7 @@ mod tests { (7, element(&[]), "no difficulty"), (7, element(&[0x80]), "a negative difficulty"), (7, element(&[1; 33]), "a difficulty past 256 bits"), + (9, element(&[]), "no gas limit"), (14, element(&[]), "no minimum gas price"), (14, element(&[0xff]), "a negative minimum gas price"), (14, element(&[1; 33]), "a minimum gas price past 256 bits"), diff --git a/crates/core/src/validation/header_rules.rs b/crates/core/src/validation/header_rules.rs index bcaea0f..ae391bb 100644 --- a/crates/core/src/validation/header_rules.rs +++ b/crates/core/src/validation/header_rules.rs @@ -114,14 +114,23 @@ impl ParentHeaderValidator for TimestampRule { } } +/// rskj `ValidGasUsedRule`: a header's gas used may not pass its gas limit, +/// which it reads as `new BigInteger(1, bytes).longValue()`, the low 64 +/// bits as a `long`. Where they are 2^63 or more that `long` is negative, +/// and no gas used fits under it, not even none. pub struct GasUsedRule; impl HeaderValidator for GasUsedRule { fn validate(&self, header: &Header) -> Result<(), ValidationError> { - let gas_limit = gas_limit_as_u64(header.gas_limit)?; - if header.gas_used > gas_limit { + let limit = header.gas_limit_low_long(); + // rskj reads the gas used exactly, so it is a `long` and never + // negative. + if i64::try_from(header.gas_used) + .ok() + .is_none_or(|used| used > limit) + { return Err(ValidationError::GasUsedExceedsLimit { used: header.gas_used, - limit: gas_limit, + limit, }); } Ok(()) diff --git a/crates/core/src/validation/mod.rs b/crates/core/src/validation/mod.rs index eed6edb..15eb4e8 100644 --- a/crates/core/src/validation/mod.rs +++ b/crates/core/src/validation/mod.rs @@ -19,8 +19,9 @@ pub enum ValidationError { #[error("Failed to read system time")] SystemTimeError, + /// `limit` is the gas limit's low 64 bits, as `ValidGasUsedRule` reads it. #[error("Gas used {used} exceeds gas limit {limit}")] - GasUsedExceedsLimit { used: u64, limit: u64 }, + GasUsedExceedsLimit { used: u64, limit: i64 }, #[error("Gas limit overflows u64")] GasLimitOverflow, diff --git a/crates/core/src/validation/tests.rs b/crates/core/src/validation/tests.rs index 51eef4d..1badaa2 100644 --- a/crates/core/src/validation/tests.rs +++ b/crates/core/src/validation/tests.rs @@ -174,6 +174,24 @@ fn test_gas_used_rule() { header.gas_used = 110; assert!(matches!(rule.validate(&header), Err(ValidationError::GasUsedExceedsLimit { .. }))); + + // rskj reads the gas limit's low 64 bits as a `long`. From 2^63 that is + // negative, and nothing fits under it, not even no gas. + header.gas_limit = U256::from(1u64 << 63); + header.gas_used = 0; + assert_eq!( + rule.validate(&header), + Err(ValidationError::GasUsedExceedsLimit { + used: 0, + limit: i64::MIN + }) + ); + // Past 2^64 only the low 64 bits count. + header.gas_limit = (U256::from(1) << 64) + U256::from(100); + header.gas_used = 100; + assert!(rule.validate(&header).is_ok()); + header.gas_used = 101; + assert!(rule.validate(&header).is_err()); } #[test] diff --git a/crates/core/src/validation/uncles.rs b/crates/core/src/validation/uncles.rs index 1a9dbc9..c70e2eb 100644 --- a/crates/core/src/validation/uncles.rs +++ b/crates/core/src/validation/uncles.rs @@ -33,9 +33,7 @@ pub const UNCLE_GENERATION_LIMIT: u64 = 7; /// /// Not the trunk header rules: rskj holds no uncle to the minimum gas limit /// or the 32-byte extra-data cap, so an uncle may carry either and still be -/// one. `GasUsedRule` reads the gas limit as a `u64` where rskj's -/// `ValidGasUsedRule` reads Java's `longValue`; the two part only above -/// 2^63, which the parent rule's bound keeps out of an uncle's reach. +/// one. pub fn rsk_header_rules(config: Arc) -> Vec> { vec![ Box::new(HeaderLayoutRule { config: config.clone() }), diff --git a/crates/execution/src/env.rs b/crates/execution/src/env.rs index 788f63c..d50a906 100644 --- a/crates/execution/src/env.rs +++ b/crates/execution/src/env.rs @@ -31,17 +31,39 @@ pub fn block_env_from_header(header: &Header, hardfork_cfg: &RskHardforkConfig) number: U256::from(header.number), beneficiary: REMASC_ADDR, timestamp: U256::from(header.timestamp), - // RSK stores gas_limit as U256 but revm expects u64 - gas_limit: header.gas_limit.to::(), + // revm reads this for GASLIMIT, which `rsk_instructions` replaces to + // push the whole value, and to hold a transaction's gas limit to the + // block's. rskj holds it to the gas limit read as gas + // (`GasCost.toGas`), or to its sublist's, which the executor checks + // first. A gas limit that isn't gas fails its block before any + // transaction runs. + gas_limit: header.gas_limit_as_gas().unwrap_or(u64::MAX), difficulty: header.difficulty, - // RSK uses minimum_gas_price instead of EIP-1559 basefee. - basefee: header.minimum_gas_price.to::(), + // RSK uses minimum_gas_price instead of EIP-1559 basefee. revm reads + // it for BASEFEE, which `rsk_instructions` replaces to push the + // whole value, and to hold a transaction's gas price to at least + // this: cut to fit, it is still no more than a valid one's. + basefee: header.minimum_gas_price.saturating_to(), prevrandao, blob_excess_gas_and_price: None, ..Default::default() } } +/// Whether rskj can build a program's invocation from this header, as it +/// does for each transaction that runs code (`ProgramInvokeFactoryImpl`). +/// It puts the gas limit's bytes, and the difficulty's and the minimum gas +/// price's `toByteArray`, each in a `DataWord`, and `DataWord.valueOf` +/// throws past 32 bytes: a gas limit written in more than 32, or a +/// difficulty or minimum gas price of 2^255 or more, which takes a sign +/// byte. Nothing catches it: the block is not imported, and a miner leaves +/// the transaction out. +pub fn header_fits_program_invoke(header: &Header) -> bool { + header.gas_limit_bytes().len() <= 32 + && header.difficulty.bit_len() < 256 + && header.minimum_gas_price.bit_len() < 256 +} + /// Build a revm `TxEnv` from an RSK transaction. /// /// RSK transactions are legacy-only (no EIP-1559/EIP-2930/EIP-4844). @@ -153,6 +175,62 @@ mod tests { assert!(env.prevrandao.is_none()); } + /// Past 64 bits nothing panics: the gas limit is held as gas + /// (`GasCost.toGas`) and the minimum gas price cut to fit, while GASLIMIT + /// and BASEFEE push both whole (`rsk_instructions`). + #[test] + fn header_values_past_64_bits_are_held_as_revm_can() { + let cfg = RskHardforkConfig::mainnet(); + let past = U256::from(1) << 64; + let header = Header { + gas_limit: past, + minimum_gas_price: past, + ..dummy_header(7_500_000) + }; + let env = block_env_from_header(&header, &cfg); + assert_eq!(env.gas_limit, i64::MAX as u64); + assert_eq!(env.basefee, u64::MAX); + } + + /// rskj's program holds the gas limit's bytes, and the difficulty's and + /// the minimum gas price's `toByteArray`, each in a `DataWord` of 32. + #[test] + fn a_program_holds_the_headers_words_in_32_bytes() { + let one = U256::from(1); + let below = (one << 255) - one; + let fits = |header: Header| header_fits_program_invoke(&header); + assert!(fits(Header { + gas_limit: below, + difficulty: below, + minimum_gas_price: below, + ..dummy_header(1) + })); + // 2^255 takes a sign byte, which makes 33. + assert!(!fits(Header { + gas_limit: one << 255, + ..dummy_header(1) + })); + assert!(!fits(Header { + difficulty: one << 255, + ..dummy_header(1) + })); + assert!(!fits(Header { + minimum_gas_price: one << 255, + ..dummy_header(1) + })); + // The gas limit counts as written: one in 33 bytes is past. + let mut written = vec![0; 33]; + written[32] = 1; + assert!(!fits(Header { + gas_limit: one, + kept: Some(Box::new(rustock_core::types::header::KeptFields { + gas_limit: Some(written.into()), + ..Default::default() + })), + ..dummy_header(1) + })); + } + #[test] fn test_tx_env_call() { let cfg = RskHardforkConfig::mainnet(); diff --git a/crates/execution/src/executor.rs b/crates/execution/src/executor.rs index ea3c91c..e618089 100644 --- a/crates/execution/src/executor.rs +++ b/crates/execution/src/executor.rs @@ -16,7 +16,7 @@ use std::sync::Arc; use tracing::debug; use crate::database::RskDatabase; -use crate::env::{block_env_from_header, tx_env_from_rsk_tx}; +use crate::env::{block_env_from_header, header_fits_program_invoke, tx_env_from_rsk_tx}; use crate::hardfork::RskHardforkConfig; use crate::precompiles::{rsk_precompiles, BridgeTxContext, RskPrecompileProvider, REMASC_ADDR}; @@ -431,7 +431,7 @@ impl RskExecutor { !self.hardfork_cfg.has_rskip191(header.number), &extcodesize_max_precompiles, &extcodehash_precompiles, - header.beneficiary, + crate::rsk_instructions::BlockWords::of(header), ); let exec_out = { @@ -446,7 +446,8 @@ impl RskExecutor { pre_rskip150_precompiles, active_precompiles, self.hardfork_cfg.has_rskip171(header.number), - ); + ) + .failing_programs(!header_fits_program_invoke(header)); let out = handler .run(&mut evm) .map_err(|e: revm::context::result::EVMError<_>| { @@ -568,7 +569,7 @@ impl RskExecutor { !self.hardfork_cfg.has_rskip191(header.number), &extcodesize_max_precompiles, &extcodehash_precompiles, - header.beneficiary, + crate::rsk_instructions::BlockWords::of(header), ); let exec_out = { @@ -582,7 +583,8 @@ impl RskExecutor { pre_rskip150_precompiles, active_precompiles, self.hardfork_cfg.has_rskip171(header.number), - ); + ) + .failing_programs(!header_fits_program_invoke(header)); use revm::inspector::InspectorHandler; let out = handler .inspect_run(&mut evm) @@ -739,7 +741,7 @@ impl RskExecutor { !self.hardfork_cfg.has_rskip191(header.number), &extcodesize_max_precompiles, &extcodehash_precompiles, - header.beneficiary, + crate::rsk_instructions::BlockWords::of(header), ); let exec_out = { @@ -753,7 +755,8 @@ impl RskExecutor { pre_rskip150_precompiles, active_precompiles, self.hardfork_cfg.has_rskip171(header.number), - ); + ) + .failing_programs(!header_fits_program_invoke(header)); use revm::inspector::InspectorHandler; let out = handler .inspect_run(&mut evm) @@ -1396,7 +1399,7 @@ impl RskExecutor { !self.hardfork_cfg.has_rskip191(header.number), &extcodesize_max_precompiles, &extcodehash_precompiles, - header.beneficiary, + crate::rsk_instructions::BlockWords::of(header), ); // rskj builds every sublist executor with `postponeFeePayment = true` @@ -1433,7 +1436,10 @@ impl RskExecutor { .as_ref() .and_then(|v| v.edges.as_deref()) .unwrap_or(&[]); - let block_gas = block_gas_limit(header); + let block_gas = header.gas_limit_as_gas(); + // Where rskj can't build a program from this header, every + // transaction that runs code fails (`RskHandler::failing_programs`). + let fails_programs = !header_fits_program_invoke(header); let (parallel_gas, sequential_gas) = crate::mining::partition::sublist_gas_limits( block_gas.unwrap_or_default(), rustock_core::validation::block_rules::TX_EXECUTION_THREADS as u64, @@ -1546,8 +1552,8 @@ impl RskExecutor { // rskj throws reading it (`GasCost.toGas`), and the block is // not imported. return Err(ExecutionError::Evm(format!( - "the block's gas limit {} is not gas: eight bytes with the top bit set", - header.gas_limit + "the block's gas limit 0x{} is not gas: eight bytes with the top bit set", + alloy_primitives::hex::encode(header.gas_limit_bytes()) ))); }; let mining = discard_unfitting.is_some(); @@ -2025,7 +2031,8 @@ impl RskExecutor { active_precompiles.clone(), self.hardfork_cfg.has_rskip171(header.number), ) - .postponing_fees(postpone_fees); + .postponing_fees(postpone_fees) + .failing_programs(fails_programs); // `inspect_run` for the one transaction being traced, `run` // for every other -- and `run` is the same code path an EVM // built without an inspector takes, because revm carries the @@ -2373,16 +2380,6 @@ fn min_sequential_set_gas_limit_for(chain_id: u64) -> u64 { .min_sequential_set_gas_limit } -/// A block's gas limit as rskj reads it, `GasCost.toGas(getGasLimit())`: -/// past eight bytes it is `Long.MAX_VALUE`, and eight bytes with the top -/// bit set make a negative `long`, which rskj refuses by throwing. -pub(crate) fn block_gas_limit(header: &Header) -> Option { - match u64::try_from(header.gas_limit) { - Err(_) => Some(i64::MAX as u64), - Ok(gas) => i64::try_from(gas).is_ok().then_some(gas), - } -} - /// rskj `TransactionExecutor.gasIsValid`, the half that reads the /// container: a transaction's gas limit must fit in what the transactions /// before it left of the container. The sum caps at `Long.MAX_VALUE` @@ -6973,26 +6970,13 @@ mod tests { assert!(too_much_gas(execute(executor, over))); } - /// rskj's `GasCost`: a gas limit past eight bytes reads as - /// `Long.MAX_VALUE` and one of eight with the top bit set does not read - /// at all; and `gasIsValid` refuses a sum that reaches `Long.MAX_VALUE` - /// (`GasCost.add` caps there) even where the container is no smaller. + /// rskj's `GasCost`: `gasIsValid` refuses a sum that reaches + /// `Long.MAX_VALUE` (`GasCost.add` caps there) even where the container + /// is no smaller. A block's gas limit reads as gas from its bytes + /// (`Header::gas_limit_as_gas`): the next test. #[test] - fn gas_reads_and_fits_as_rskj_counts_it() { + fn gas_fits_as_rskj_counts_it() { const LONG_MAX: u64 = i64::MAX as u64; - let one = U256::from(1u64); - let header = |gas_limit: U256| Header { - gas_limit, - ..dummy_header(1) - }; - assert_eq!( - block_gas_limit(&header(U256::from(12_000_000))), - Some(12_000_000) - ); - assert_eq!(block_gas_limit(&header((one << 63) - one)), Some(LONG_MAX)); - assert_eq!(block_gas_limit(&header(one << 63)), None); - assert_eq!(block_gas_limit(&header(one << 64)), Some(LONG_MAX)); - assert_eq!(block_gas_limit(&header(U256::MAX)), Some(LONG_MAX)); assert!(gas_fits(1_000_000, 0, 1_000_000)); assert!(!gas_fits(1_000_001, 0, 1_000_000)); @@ -7009,6 +6993,168 @@ mod tests { ); } + /// rskj holds a block's transactions to its gas limit read as gas from + /// its bytes (`GasCost.toGas`): the same 12,000,000 written in nine is + /// `Long.MAX_VALUE`, and leaves every sublist room for more. + #[test] + fn a_block_holds_its_transactions_to_its_gas_limit_read_from_its_bytes() { + let block = || transfers(&[21_000, 21_000, 10_000_001], 12_000_000, &[1, 2]); + assert!(too_much_gas(execute(testnet_executor(1), block()))); + let (mut header, transactions, root, store) = block(); + header.kept = Some(Box::new(rustock_core::types::header::KeptFields { + gas_limit: Some(Bytes::from(vec![0, 0, 0, 0, 0, 0, 0xb7, 0x1b, 0x00])), + ..Default::default() + })); + assert!(execute(testnet_executor(1), (header, transactions, root, store)).is_ok()); + } + + /// GASLIMIT and BASEFEE push the header's values whole, as rskj's program + /// holds them (`DataWord`s), where revm's block holds `u64`s. + #[test] + fn gaslimit_and_basefee_push_the_whole_values() { + // Runtime: GASLIMIT; PUSH1 0; MSTORE; BASEFEE; PUSH1 32; MSTORE; + // PUSH1 64; PUSH1 0; RETURN. + let runtime: Vec = vec![ + 0x45, 0x60, 0x00, 0x52, 0x48, 0x60, 0x20, 0x52, 0x60, 0x40, 0x60, 0x00, 0xf3, + ]; + // Init: CODECOPY the 13-byte runtime and RETURN it. + let mut init = vec![ + 0x60, 0x0d, 0x60, 0x0c, 0x60, 0x00, 0x39, 0x60, 0x0d, 0x60, 0x00, 0xf3, + ]; + init.extend_from_slice(&runtime); + + let gas_limit = (U256::from(1) << 64) + U256::from(5); + let price = (U256::from(1) << 64) + U256::from(7); + // lovell700, where BASEFEE is active. + let header = Header { + gas_limit, + minimum_gas_price: price, + ..dummy_header(7_338_024) + }; + let store = Arc::new(MemoryTrieStore::new()); + let sender = Address::repeat_byte(0x45); + let rich = U256::from(10u64).pow(U256::from(30)); + let root = put_account(&TrieNode::empty(), store.as_ref(), &sender, 0, rich); + let block_store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); + let tx = |nonce: u64, to: Bytes, input: Vec| rustock_core::Transaction { + nonce, + gas_price: price, + gas_limit: U256::from(1_000_000), + to, + value: U256::ZERO, + input: Bytes::from(input), + v: 0, + r: U256::ZERO, + s: U256::ZERO, + cached_rlp: None, + }; + let deployed = executor + .execute_block( + &header, + &[(tx(0, Bytes::new(), init), sender)], + &root, + store.clone(), + ) + .expect("deploy block"); + assert!(deployed.tx_results[0].success); + let root = crate::state::apply_state_changes( + &root, + store.as_ref(), + &deployed.state_changes, + &deployed.markers, + ); + let contract = Bytes::copy_from_slice(sender.create(0).as_slice()); + let called = executor + .execute_block( + &header, + &[(tx(1, contract, Vec::new()), sender)], + &root, + store, + ) + .expect("call block"); + let output = &called.tx_results[0].output; + assert_eq!(U256::from_be_slice(&output[..32]), gas_limit); + assert_eq!(U256::from_be_slice(&output[32..]), price); + } + + /// rskj builds a program's invocation from the block's header for each + /// transaction that runs code, and throws where a field is past 32 + /// bytes. Nothing catches it: the block is not imported, and a miner + /// leaves the transaction out. A transaction that runs no code builds + /// none. + #[test] + fn a_header_past_a_programs_words_fails_what_runs_code() { + let store = Arc::new(MemoryTrieStore::new()); + let sender = Address::repeat_byte(0x46); + let rbtc = U256::from(10u64).pow(U256::from(18)); + let root = put_account(&TrieNode::empty(), store.as_ref(), &sender, 0, rbtc); + let block_store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); + let tx = |to: Bytes, input: &[u8]| rustock_core::Transaction { + nonce: 0, + gas_price: U256::ZERO, + gas_limit: U256::from(1_000_000), + to, + value: U256::ZERO, + input: Bytes::copy_from_slice(input), + v: 0, + r: U256::ZERO, + s: U256::ZERO, + cached_rlp: None, + }; + // A creation whose code is STOP, one with no code, and a transfer. + let runs_code = tx(Bytes::new(), &[0x00]); + let creates_nothing = tx(Bytes::new(), &[]); + let transfer = tx( + Bytes::copy_from_slice(Address::repeat_byte(0x47).as_slice()), + &[], + ); + + let one = U256::from(1); + for (header, fits) in [ + ( + Header { + gas_limit: (one << 255) - one, + ..dummy_header(892_000) + }, + true, + ), + // 2^255 takes a sign byte: 33 bytes. + ( + Header { + gas_limit: one << 255, + ..dummy_header(892_000) + }, + false, + ), + ( + Header { + difficulty: one << 255, + ..dummy_header(892_000) + }, + false, + ), + ] { + let run = |tx: &rustock_core::Transaction| { + executor.execute_block(&header, &[(tx.clone(), sender)], &root, store.clone()) + }; + assert_eq!(run(&runs_code).is_ok(), fits); + assert!(run(&creates_nothing).is_ok()); + assert!(run(&transfer).is_ok()); + let (mined, drops) = executor + .execute_block_for_mining( + &header, + &[(runs_code.clone(), sender)], + &root, + store.clone(), + ) + .expect("a miner leaves the transaction out"); + assert_eq!(mined.tx_results.len(), usize::from(fits)); + assert_eq!(drops.invalid, if fits { vec![] } else { vec![0] }); + } + } + /// Run as one list, rskj credits each fee to REMASC as its transaction /// ends -- `executeInternal`'s executors do not postpone -- so a later /// transaction sees it. Run as sublists, the fees wait for the REMASC diff --git a/crates/execution/src/mining/template.rs b/crates/execution/src/mining/template.rs index 9852e76..87a3f82 100644 --- a/crates/execution/src/mining/template.rs +++ b/crates/execution/src/mining/template.rs @@ -453,10 +453,10 @@ impl BlockTemplateBuilder { // The probe has just read the block's gas limit as rskj reads it, // and one it could not read failed the probe. - let block_gas = crate::executor::block_gas_limit(header).ok_or_else(|| { + let block_gas = header.gas_limit_as_gas().ok_or_else(|| { crate::executor::ExecutionError::Evm(format!( - "the block's gas limit {} is not gas", - header.gas_limit + "the block's gas limit 0x{} is not gas", + alloy_primitives::hex::encode(header.gas_limit_bytes()) )) })?; let mut partitioner = crate::mining::partition::SublistPartitioner::new( @@ -763,14 +763,14 @@ fn timestamp_for_child(parent_timestamp: u64) -> u64 { /// The consensus rule is only that a child's gas limit stays within /// `parent / 1024` of the parent's, so inheriting it unchanged -- which is /// what `None` does -- is always valid. rskj `GasLimitCalculator`. -fn next_gas_limit(parent: &Header, target: Option) -> U256 { +pub(super) fn next_gas_limit(parent: &Header, target: Option) -> U256 { let parent_limit = parent.gas_limit; let Some(target) = target else { return parent_limit; }; let delta = parent_limit / U256::from(1024); if target > parent_limit { - target.min(parent_limit + delta) + target.min(parent_limit.saturating_add(delta)) } else { target.max(parent_limit.saturating_sub(delta)) } diff --git a/crates/execution/src/mining/tests.rs b/crates/execution/src/mining/tests.rs index 2c64cd7..988d06d 100644 --- a/crates/execution/src/mining/tests.rs +++ b/crates/execution/src/mining/tests.rs @@ -994,6 +994,32 @@ fn cant_get_mgp_to_be_negative() { assert_eq!(next_minimum_gas_price(U256::ZERO, Some(U256::ZERO)), U256::ZERO); } +/// rskj `GasLimitCalculator`: one step towards the target, at most a 1024th +/// of the parent's, which near the top of 256 bits stops there rather than +/// wrapping round to a gas limit no parent allows. +#[test] +fn the_gas_limit_steps_a_1024th_and_stops_at_the_top() { + use super::template::next_gas_limit; + let parent = |gas_limit: U256| Header { + gas_limit, + ..header_at(1, B256::ZERO, B256::ZERO) + }; + let start = U256::from(1_024_000); + assert_eq!( + next_gas_limit(&parent(start), Some(U256::MAX)), + U256::from(1_025_000) + ); + assert_eq!( + next_gas_limit(&parent(start), Some(U256::ZERO)), + U256::from(1_023_000) + ); + assert_eq!(next_gas_limit(&parent(start), None), start); + assert_eq!( + next_gas_limit(&parent(U256::MAX - U256::from(1)), Some(U256::MAX)), + U256::MAX + ); +} + /// A coinbase tagged with a hash this node never handed out cannot be matched /// to a block, and the message has to say which of the two reasons it is. #[test] diff --git a/crates/execution/src/native/block_header.rs b/crates/execution/src/native/block_header.rs index e3b83f3..266bdb0 100644 --- a/crates/execution/src/native/block_header.rs +++ b/crates/execution/src/native/block_header.rs @@ -214,7 +214,9 @@ impl BlockHeader<'_> { Method::BlockHash => hash.to_vec(), Method::MergedMiningTags => merged_mining_tags(header)?, Method::MinGasPrice => big_integer_bytes(header.minimum_gas_price), - Method::GasLimit => rlp_bytes(header.gas_limit), + // The bytes as the header holds them: rskj never holds the gas + // limit as a number (`KeptFields::gas_limit`). + Method::GasLimit => header.gas_limit_bytes().into_owned(), Method::GasUsed => big_integer_bytes(U256::from(header.gas_used)), Method::Difficulty => big_integer_bytes(header.difficulty), Method::BitcoinHeader => { @@ -299,16 +301,15 @@ fn non_empty(field: &Option) -> Option<&[u8]> { /// big-endian form, with a leading zero byte when the first one's top bit /// is set, and `[0]` for zero. fn big_integer_bytes(value: U256) -> Vec { - let mut bytes = rlp_bytes(value); + let mut bytes = shortest_bytes(value); if bytes.first().is_none_or(|first| first & 0x80 != 0) { bytes.insert(0, 0); } bytes } -/// A header field as its RLP holds it, which `GetGasLimit` returns as it is: -/// the shortest big-endian form, empty for zero. -fn rlp_bytes(value: U256) -> Vec { +/// The shortest big-endian form, empty for zero. +fn shortest_bytes(value: U256) -> Vec { let bytes = value.to_be_bytes::<32>(); let start = bytes.iter().position(|b| *b != 0).unwrap_or(32); bytes[start..].to_vec() @@ -671,13 +672,11 @@ mod tests { assert!(matches!(merged_mining_tags(&block), Err(Failure::Threw(_)))); } - /// rskj's `BlockHeaderContractTest` constants (a gas limit of 3,000,000, - /// no gas used, a minimum gas price and difficulty of 1, 3 with uncles), - /// the sign byte `BigInteger` adds before a set top bit, and the gas - /// limit's raw form, which takes none. + /// rskj's `BlockHeaderContractTest` constants (no gas used, a minimum gas + /// price and difficulty of 1, 3 with uncles), and the sign byte + /// `BigInteger` adds before a set top bit. #[test] fn numbers_are_written_as_java_writes_them() { - assert_eq!(rlp_bytes(U256::from(3_000_000)), vec![0x2D, 0xC6, 0xC0]); assert_eq!(big_integer_bytes(U256::ZERO), vec![0]); assert_eq!(big_integer_bytes(U256::from(1)), vec![1]); assert_eq!(big_integer_bytes(U256::from(3)), vec![3]); @@ -697,7 +696,37 @@ mod tests { let mut max = vec![0]; max.extend_from_slice(&[0xFF; 32]); assert_eq!(big_integer_bytes(U256::MAX), max); - assert_eq!(rlp_bytes(U256::from(10_000_000)), vec![0x98, 0x96, 0x80]); - assert_eq!(rlp_bytes(U256::ZERO), Vec::::new()); + assert_eq!( + shortest_bytes(U256::from(10_000_000)), + vec![0x98, 0x96, 0x80] + ); + assert_eq!(shortest_bytes(U256::ZERO), Vec::::new()); + } + + /// `getGasLimit` returns the gas limit's bytes as the header holds them: + /// 12,000,000 as rskj's miner writes it, with a sign byte, as testnet's + /// blocks carry it, and bytes another miner wrote as they were written. + #[test] + fn the_gas_limit_comes_back_as_its_bytes() { + let nine = vec![0, 0, 0, 0, 0, 0, 0xB7, 0x1B, 0x00]; + for (kept, bytes) in [ + (None, vec![0x00, 0xB7, 0x1B, 0x00]), + (Some(nine.clone()), nine), + ] { + let (_dir, blocks, hashes) = chain_with(2, |block| { + block.gas_limit = U256::from(12_000_000); + block.kept = kept.clone().map(|gas_limit: Vec| { + Box::new(rustock_core::types::header::KeptFields { + gas_limit: Some(gas_limit.into()), + ..Default::default() + }) + }); + }); + let get = input("getGasLimit(int256)", &[int(0)]); + assert!(matches!( + call(&at(&blocks, &hashes, 2), &get, 1_000_000), + Outcome::Returned { output, .. } if output[..] == encode_bytes(&bytes)[..] + )); + } } } diff --git a/crates/execution/src/rsk_handler.rs b/crates/execution/src/rsk_handler.rs index 3905a03..16ff505 100644 --- a/crates/execution/src/rsk_handler.rs +++ b/crates/execution/src/rsk_handler.rs @@ -116,6 +116,11 @@ invisible_exception: Arc, /// account — preserving the prior call's 32-byte output sent it down a /// different branch and out of gas). rskip171_active: bool, + /// Fail a transaction as it starts to run code. rskj can't build its + /// program's invocation from the block's header + /// (`env::header_fits_program_invoke`), throws, and catches nothing: + /// the block is not imported, and a miner leaves the transaction out. + fail_programs: bool, _phantom: core::marker::PhantomData<(CTX, ERROR, FRAME)>, } @@ -142,6 +147,13 @@ impl RskHandler { self } + /// Fail every transaction that runs code, where rskj can't build its + /// program from the block's header. See the field. + pub fn failing_programs(mut self, fail: bool) -> Self { + self.fail_programs = fail; + self + } + #[allow(clippy::too_many_arguments)] pub fn with_rskip125( empty_create_on_unpayable_deposit: bool, @@ -162,6 +174,7 @@ impl RskHandler { rskip171_active, // Per-transaction, as before RSKIP-144; `postponing_fees` opts in. postpone_fees: false, + fail_programs: false, _phantom: core::marker::PhantomData, } } @@ -340,10 +353,26 @@ where evm: &mut Self::Evm, first_frame_input: FrameInit, ) -> Result { + // rskj builds a program's invocation only for a transaction that runs + // code (`TransactionExecutor.call` and `create`); revm opens a frame + // for a creation with no data too, which runs nothing. + let runs_code = match &first_frame_input.frame_input { + revm::interpreter::interpreter_action::FrameInput::Create(inputs) => { + !inputs.init_code().is_empty() + } + _ => true, + }; let res = evm.frame_init(first_frame_input)?; if let ItemOrResult::Result(frame_result) = res { return Ok(frame_result); } + if self.fail_programs && runs_code { + return Err(ERROR::from_string( + "rskj can't build this transaction's program: a field of the \ + block's header is past 32 bytes" + .into(), + )); + } // Top-level CREATE transaction: rskj's TransactionExecutor.create never // bumps the nonce (even post-RSKIP125), so it stays 0. set_created_contract_nonce(evm, 0); diff --git a/crates/execution/src/rsk_instructions.rs b/crates/execution/src/rsk_instructions.rs index 317722c..eb6d50c 100644 --- a/crates/execution/src/rsk_instructions.rs +++ b/crates/execution/src/rsk_instructions.rs @@ -72,22 +72,53 @@ thread_local! { /// rskj's `OpCode.COINBASE` returns the actual miner. Set by `install`. static REAL_COINBASE: core::cell::Cell
= const { core::cell::Cell::new(Address::ZERO) }; + /// The block's gas limit and minimum gas price, whole, which GASLIMIT and + /// BASEFEE push. rskj's program holds each in a `DataWord` + /// (`ProgramInvokeImpl`), where revm's block holds a `u64`. Set by + /// `install`. + static GAS_LIMIT: core::cell::Cell = const { core::cell::Cell::new(U256::ZERO) }; + static MINIMUM_GAS_PRICE: core::cell::Cell = + const { core::cell::Cell::new(U256::ZERO) }; /// Whether [`install`] has run on *this* thread. /// - /// The three entries above are consensus configuration living in - /// thread-local storage, and an unset one reads as a perfectly plausible - /// value: no precompile gets RSKIP90's `EXTCODESIZE`, no precompile gets - /// the empty-code `EXTCODEHASH`, and `COINBASE` pushes `0x0`. When - /// RSKIP-144 sublists began running on worker threads, configuring on one - /// thread and executing on another would have produced a state root no - /// other node reaches, with nothing failing to compile and no test - /// noticing. + /// The entries above are consensus configuration living in thread-local + /// storage, and an unset one reads as a perfectly plausible value: no + /// precompile gets RSKIP90's `EXTCODESIZE`, no precompile gets the + /// empty-code `EXTCODEHASH`, and `COINBASE`, `GASLIMIT` and `BASEFEE` + /// push `0x0`. When RSKIP-144 sublists began running on worker threads, + /// configuring on one thread and executing on another would have + /// produced a state root no other node reaches, with nothing failing to + /// compile and no test noticing. /// /// So the rule is that an EVM is configured on the thread that will use /// it, and this is what lets the rule be checked instead of trusted. static CONFIGURED: core::cell::Cell = const { core::cell::Cell::new(false) }; } +/// What the block's header gives the opcodes that read it, as rskj's +/// program holds it (`ProgramInvokeImpl`). +#[derive(Clone, Copy, Debug)] +pub struct BlockWords { + /// COINBASE's: the block's real miner (see `REAL_COINBASE`). + pub coinbase: Address, + /// GASLIMIT's: the gas limit's whole value. + pub gas_limit: U256, + /// BASEFEE's: the minimum gas price's whole value. + pub minimum_gas_price: U256, +} + +impl BlockWords { + /// The words `header` gives. Past 32 bytes no program runs at all (see + /// `env::header_fits_program_invoke`), so none needs more. + pub fn of(header: &rustock_core::Header) -> Self { + Self { + coinbase: header.beneficiary, + gas_limit: header.gas_limit, + minimum_gas_price: header.minimum_gas_price, + } + } +} + /// Whether this thread has had an instruction table installed on it. /// /// Callers that build an EVM on one thread and run it on another are the @@ -131,6 +162,8 @@ pub fn set_transaction_index(index: u64) { /// /// `dupn_swapn_txindex_enabled` is RSKIP191's absence: before iris300 rskj /// runs RSK's own DUPN, SWAPN and TXINDEX. +/// +/// `block` is what the block's header gives COINBASE, GASLIMIT and BASEFEE. pub fn install( instructions: &mut EthInstructions, extcodehash_enabled: bool, @@ -143,7 +176,7 @@ pub fn install( dupn_swapn_txindex_enabled: bool, extcodesize_max_precompiles: &[Address], extcodehash_precompiles: &[Address], - real_coinbase: Address, + block: BlockWords, ) where WIRE: InterpreterTypes, HOST: Host, @@ -330,12 +363,20 @@ pub fn install( // rskj `OpCode.COINBASE` returns the block's real miner; rustock keeps // `block.beneficiary = REMASC_ADDR` (for gas-fee routing) so the default // COINBASE would return REMASC. Push the real coinbase instead. - REAL_COINBASE.with(|c| c.set(real_coinbase)); + REAL_COINBASE.with(|c| c.set(block.coinbase)); + // GASLIMIT and BASEFEE push the whole values, which revm's block cuts to + // a `u64` (see `GAS_LIMIT`). + GAS_LIMIT.with(|c| c.set(block.gas_limit)); + MINIMUM_GAS_PRICE.with(|c| c.set(block.minimum_gas_price)); CONFIGURED.with(|c| c.set(true)); instructions.insert_instruction( opcode::COINBASE, Instruction::new(rsk_coinbase::, 2), ); + instructions.insert_instruction( + opcode::GASLIMIT, + Instruction::new(rsk_gaslimit::, 2), + ); // RSK's own opcodes, which RSKIP191 (iris300) removed. Before it rskj's VM // runs DUPN (VERY_LOW, 3 gas), SWAPN (3) and TXINDEX (BASE, 2); from it @@ -502,14 +543,15 @@ fn rsk_push0( } /// BASEFEE (RSKIP412, arrowhead600) without revm's LONDON spec check: pushes -/// the block's base fee, which in rustock carries RSK's `minimumGasPrice` -/// (`env.rs`: `basefee: header.minimum_gas_price`). rskj `VM.doBASEFEE` pushes -/// `program.getMinimumGasPrice()` verbatim. Static gas (2, BASE_TIER) comes -/// from the instruction table entry installed in `install`. +/// the block's minimum gas price, whole. rskj `VM.doBASEFEE` pushes +/// `program.getMinimumGasPrice()`, a `DataWord`, where revm's block holds a +/// `u64`. Static gas (2, BASE_TIER) comes from the instruction table entry +/// installed in `install`. fn rsk_basefee( context: InstructionContext<'_, H, WIRE>, ) { - if !context.interpreter.stack.push(U256::from(context.host.basefee())) { + assert_configured("MINIMUM_GAS_PRICE"); + if !context.interpreter.stack.push(MINIMUM_GAS_PRICE.with(|c| c.get())) { context.interpreter.halt_overflow(); } } @@ -604,6 +646,18 @@ fn rsk_coinbase( } } +/// GASLIMIT pushing the block's gas limit whole. rskj `VM.doGASLIMIT` pushes +/// `program.getGasLimit()`, a `DataWord` of the header's bytes, where +/// revm's block holds the limit as gas, a `u64`. See `GAS_LIMIT`. +fn rsk_gaslimit( + context: InstructionContext<'_, H, WIRE>, +) { + assert_configured("GAS_LIMIT"); + if !context.interpreter.stack.push(GAS_LIMIT.with(|c| c.get())) { + context.interpreter.halt_overflow(); + } +} + /// DUPN (before RSKIP191), rskj `VM.doDUPN`: pops a depth `d` and pushes a /// copy of the item `d + 1` from the top, so DUPN of 0 is DUP1, then steps /// over the next byte. A depth the stack doesn't reach throws @@ -782,6 +836,8 @@ fn traced_all( } } opcode::SELFDESTRUCT => rsk_selfdestruct(context), + opcode::COINBASE => rsk_coinbase(context), + opcode::GASLIMIT => rsk_gaslimit(context), opcode::EXTCODEHASH if !EXTCODEHASH_ENABLED.with(|f| f.get()) => { invalid_opcode(context) } @@ -1329,11 +1385,16 @@ mod thread_affinity_tests { false, &[Address::new([0x01; 20])], &[Address::new([0x01; 20])], - Address::new([0xbb; 20]), + BlockWords { + coinbase: Address::new([0xbb; 20]), + gas_limit: U256::from(6_800_000), + minimum_gas_price: U256::from(60_000_000), + }, ); ( is_configured_on_this_thread(), REAL_COINBASE.with(|c| c.get()), + GAS_LIMIT.with(|c| c.get()), EXTCODESIZE_MAX_PRECOMPILES.with(|p| p.borrow().len()), ) }) @@ -1342,7 +1403,7 @@ mod thread_affinity_tests { assert_eq!( child_saw, - (true, Address::new([0xbb; 20]), 1), + (true, Address::new([0xbb; 20]), U256::from(6_800_000), 1), "the thread that installed sees its own configuration" ); assert!( diff --git a/crates/rpc/src/call.rs b/crates/rpc/src/call.rs index 2a5cd38..c707e9b 100644 --- a/crates/rpc/src/call.rs +++ b/crates/rpc/src/call.rs @@ -56,7 +56,9 @@ pub fn eth_estimate_gas(id: Value, params: &Value, state: &RpcState) -> JsonRpcR let sender = call_req.from.unwrap_or(Address::ZERO); let executor = RskExecutor::new(hardfork_cfg, state.store.clone()); - let gas_cap = call_req.gas.unwrap_or(header.gas_limit.to::()); + let gas_cap = call_req + .gas + .unwrap_or(header.gas_limit.saturating_to::()); let mut tx = build_call_tx(&call_req, &header, &root, sender, state); tx.gas_limit = U256::from(gas_cap); diff --git a/crates/rpc/src/helpers.rs b/crates/rpc/src/helpers.rs index 93abfe7..1c711a5 100644 --- a/crates/rpc/src/helpers.rs +++ b/crates/rpc/src/helpers.rs @@ -26,6 +26,17 @@ pub fn to_hex_bytes(v: &[u8]) -> String { format!("0x{}", hex::encode(v)) } +/// Formats raw bytes as a quantity, as rskj's `HexUtils.toQuantityJsonHex` +/// does: their hex without its leading zero digits, and `0x0` for none. +/// rskj prints the gas limit so, from the bytes it holds. +pub fn to_hex_quantity_bytes(v: &[u8]) -> String { + let digits = hex::encode(v); + match digits.trim_start_matches('0') { + "" => "0x0".to_string(), + digits => format!("0x{digits}"), + } +} + /// Parses a 0x-prefixed hex string to a B256. Returns None on failure. pub fn parse_b256(s: &str) -> Option { s.parse::().ok() @@ -203,7 +214,7 @@ impl BlockResultDto { logs_bloom: to_hex_bytes(header.logs_bloom.as_ref()), difficulty: to_hex_u256(&header.difficulty), total_difficulty: to_hex_u256(&total_difficulty), - gas_limit: to_hex_u256(&header.gas_limit), + gas_limit: to_hex_quantity_bytes(&header.gas_limit_bytes()), gas_used: to_hex_u64(header.gas_used), timestamp: to_hex_u64(header.timestamp), extra_data: to_hex_bytes(&header.extra_data), @@ -335,6 +346,22 @@ mod tests { assert_eq!(to_hex_u256(&U256::from(0xff)), "0xff"); } + /// rskj's `HexUtils.toQuantityJsonHex`: leading zero digits go, a sign + /// byte's included, and nothing left is `0x0`. + #[test] + fn bytes_print_as_a_quantity() { + assert_eq!(to_hex_quantity_bytes(&[]), "0x0"); + assert_eq!(to_hex_quantity_bytes(&[0, 0]), "0x0"); + assert_eq!(to_hex_quantity_bytes(&[0x00, 0xb7, 0x1b, 0x00]), "0xb71b00"); + assert_eq!(to_hex_quantity_bytes(&[0x09, 0x00]), "0x900"); + let mut past = vec![1]; + past.extend_from_slice(&[0; 32]); + assert_eq!( + to_hex_quantity_bytes(&past), + format!("0x1{}", "0".repeat(64)) + ); + } + #[test] fn test_parse_block_number() { assert_eq!(parse_block_number("latest", 100), Some(100)); diff --git a/crates/rpc/src/subscribe.rs b/crates/rpc/src/subscribe.rs index bf34233..9e6b344 100644 --- a/crates/rpc/src/subscribe.rs +++ b/crates/rpc/src/subscribe.rs @@ -200,8 +200,10 @@ fn notification(id: &str, result: Value) -> Value { /// A block header in the shape `eth_getBlockByNumber` returns, minus the /// body-dependent fields a header notification has no business carrying. -fn header_dto(header: &Header, hash: B256) -> Value { - use crate::helpers::{to_hex_b256, to_hex_bytes, to_hex_u256, to_hex_u64}; +pub(crate) fn header_dto(header: &Header, hash: B256) -> Value { + use crate::helpers::{ + to_hex_b256, to_hex_bytes, to_hex_quantity_bytes, to_hex_u256, to_hex_u64, + }; let mut out = Map::new(); out.insert("hash".into(), Value::String(to_hex_b256(&hash))); out.insert("parentHash".into(), Value::String(to_hex_b256(&header.parent_hash))); @@ -222,7 +224,10 @@ fn header_dto(header: &Header, hash: B256) -> Value { ); out.insert("difficulty".into(), Value::String(to_hex_u256(&header.difficulty))); out.insert("number".into(), Value::String(to_hex_u64(header.number))); - out.insert("gasLimit".into(), Value::String(to_hex_u256(&header.gas_limit))); + out.insert( + "gasLimit".into(), + Value::String(to_hex_quantity_bytes(&header.gas_limit_bytes())), + ); out.insert("gasUsed".into(), Value::String(to_hex_u64(header.gas_used))); out.insert("timestamp".into(), Value::String(to_hex_u64(header.timestamp))); out.insert("extraData".into(), Value::String(to_hex_bytes(header.extra_data.as_ref()))); diff --git a/crates/rpc/src/tests.rs b/crates/rpc/src/tests.rs index c5061f6..0236b5b 100644 --- a/crates/rpc/src/tests.rs +++ b/crates/rpc/src/tests.rs @@ -43,6 +43,28 @@ fn test_header(number: u64) -> Header { } } +/// rskj prints a header's gas limit from the bytes it holds +/// (`HexUtils.toQuantityJsonHex`), so one past 256 bits prints whole, in a +/// block and in a `newHeads` notification alike. +#[test] +fn a_gas_limit_past_256_bits_prints_whole() { + let mut past = vec![1]; + past.extend_from_slice(&[0; 32]); + let header = Header { + gas_limit: U256::MAX, + kept: Some(Box::new(rustock_core::types::header::KeptFields { + gas_limit: Some(past.into()), + ..Default::default() + })), + ..test_header(1) + }; + let whole = format!("0x1{}", "0".repeat(64)); + let block = crate::helpers::BlockResultDto::from_header(&header, B256::ZERO, U256::ZERO); + assert_eq!(block.gas_limit, whole); + let notified = crate::subscribe::header_dto(&header, B256::ZERO); + assert_eq!(notified["gasLimit"], json!(whole)); +} + fn setup_state() -> (RpcState, tempfile::TempDir) { setup_state_with(|store| store) } From 8b5cc8c391e7647710bf42d45f627741782bc717 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Mon, 9 Nov 2026 07:31:30 -0800 Subject: [PATCH 053/264] core: no maximum gas limit, and its bounds read whole, as rskj's rskj has no maximum gas limit. GasLimitRule holds a block's gas limit to Constants.getMinGasLimit() alone, and BlockParentGasLimitRule bounds it by its parent's, reading both whole (new BigInteger(1, getGasLimit())), of any size. rustock refused a gas limit above 1,000,000,000 on mainnet and testnet and 10,000,000 on regtest, which miners raising it a 1,024th a block pass in about 5,100 blocks (under two days). And it compared U256s, which hold U256::MAX past 256 bits, so there it took children rskj refuses and refused ones rskj takes. GasLimitBoundsRule is now GasLimitRule, minimum only. max_gas_limit is gone from ChainConfig, and with it --max-gas-limit and [chain] max_gas_limit, which existed only to lift the bound (perf-lab's seed passes u64::MAX): a node given the flag now stops on it, so the lab's flags drop it. GasLimitOverflow goes with the u64 read that raised it. Both rules read Header::gas_limit_value, the gas limit's whole value from its bytes, as num-bigint's BigUint, which core and execution now take (it was in the lock file already, through ruint), and BlockParentGasLimitRule computes parent * (d - 1) / d and parent * (d + 1) / d as rskj writes them. The errors carry the values whole. The miner steps from the parent's whole value too, by the chain's divisor rather than a literal 1024, and writes its gas limit as rskj's miner does (toByteArray) through Header::set_gas_limit, which keeps the bytes past 256 bits. On a parent past 256 bits it wrote U256::MAX, which rskj's parent rule refuses there; it keeps the parent's value now. Nothing else read the bound. Every reader of a header's gas limit as a number reads its bytes (the previous commit) or saturates, and the pool's saturated bound meets TRANSACTION_GAS_CAP (2^60) first, as rskj's does, so it decides as rskj's does. Tests: gas limits of 10^9 + 1, 2^64 and 2^300 are no longer too large; the parent rule at 2^300 + 12,345 takes 2^300 - 2^290 + 12,332 to 2^300 + 2^290 + 12,357, leading zeros or not, and nothing either side, and lets a child of 2^256 - 1 reach 2^256 + 2^246 - 2 and no further; set_gas_limit writes toByteArray's bytes, keeps the rest of what was kept and leaves no stale hash; the miner steps a 1,024th, or its chain's divisor, at any size, and a template on a parent of 2^300 keeps its value and passes the parent rule. Sixteen one-line mutations each fail a test. The README, the example configs and the load test drop the bound too. A minimum gas price can pass 256 bits the same way, at 1% a block, and the decoder still refuses one; that is next. --- Cargo.lock | 2 + Cargo.toml | 3 + README.md | 7 +- crates/cli/examples/loadtest.rs | 6 +- crates/cli/src/config_file.rs | 6 +- crates/cli/src/main.rs | 35 ++---- crates/cli/tests/import_blocks.rs | 7 +- crates/core/Cargo.toml | 1 + crates/core/src/config.rs | 4 - crates/core/src/types/header.rs | 128 ++++++++++++++++++--- crates/core/src/validation/header_rules.rs | 52 +++------ crates/core/src/validation/mod.rs | 18 ++- crates/core/src/validation/tests.rs | 89 +++++++++++++- crates/execution/Cargo.toml | 1 + crates/execution/src/mining/template.rs | 28 +++-- crates/execution/src/mining/tests.rs | 63 +++++++--- crates/execution/src/mining/workload.rs | 1 - node-light.example.toml | 3 +- node.example.toml | 3 +- 19 files changed, 314 insertions(+), 143 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 800bcaa..c9ac731 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3331,6 +3331,7 @@ dependencies = [ "hex", "hex-literal", "k256", + "num-bigint", "ripemd", "rustock-trie", "secp256k1 0.31.1", @@ -3353,6 +3354,7 @@ dependencies = [ "hex", "k256", "lru", + "num-bigint", "rand 0.8.6", "revm", "ripemd", diff --git a/Cargo.toml b/Cargo.toml index 3da254c..71bf2a4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -68,6 +68,9 @@ ripemd = "0.1" futures = "0.3.31" rocksdb = "0.25.0" libc = "0.2" +# rskj's BigInteger, for what a header holds at any size: the rules that +# bound its gas limit read the whole value. +num-bigint = "0.4.6" [profile.dev] # Line tables only: backtraces keep file and line numbers, but the type and diff --git a/README.md b/README.md index 147ad86..1f4ec9a 100644 --- a/README.md +++ b/README.md @@ -146,15 +146,14 @@ independent cross-check. ### A regtest chain rskj runs -rustock's regtest is its own chain: its own genesis, no Bridge federation, and -gas limits of its own. To run the chain an rskj regtest node runs, so that -blocks rskj mined connect here, start from rskj's genesis file and its rules: +rustock's regtest is its own chain: its own genesis and no Bridge federation. +To run the chain an rskj regtest node runs, so that blocks rskj mined connect +here, start from rskj's genesis file and its rules: ```bash rustock --network-id 33 \ --genesis genesis.json \ --consensus-rule rskip97=0 \ - --min-gas-limit 3000000 --max-gas-limit 18446744073709551615 \ --skip-pow-validation ``` diff --git a/crates/cli/examples/loadtest.rs b/crates/cli/examples/loadtest.rs index 296f1dd..8e2502d 100644 --- a/crates/cli/examples/loadtest.rs +++ b/crates/cli/examples/loadtest.rs @@ -218,11 +218,7 @@ fn main() -> Result<()> { // Regtest's activation heights are all zero, so every rule -- orchid, // wasabi100, iris300 -- is live from block one. On mainnet's heights a // 40-block run would skip merged mining entirely and measure nothing. - // Regtest's bounds, with the gas limit raised to the one the - // benchmark's headers have always carried. Changing the header - // instead would move every figure already recorded, and the bound is - // not what is being measured. - let chain = Arc::new(ChainConfig { max_gas_limit: 100_000_000, ..ChainConfig::regtest() }); + let chain = Arc::new(ChainConfig::regtest()); let header_verifier = HeaderVerifier::default_rsk(chain.clone()); let block_verifier = BlockVerifier::default_rsk(&chain); diff --git a/crates/cli/src/config_file.rs b/crates/cli/src/config_file.rs index 112b303..80045a8 100644 --- a/crates/cli/src/config_file.rs +++ b/crates/cli/src/config_file.rs @@ -65,8 +65,8 @@ pub struct FileConfig { } /// Regtest chain settings, for a chain that has to match one an rskj node -/// runs: its genesis file, its consensus-rule overrides, its gas-limit -/// bounds, and whether proof of work is checked. +/// runs: its genesis file, its consensus-rule overrides, its minimum gas +/// limit, and whether proof of work is checked. #[derive(Debug, Default, Deserialize, Serialize)] #[serde(deny_unknown_fields)] pub struct ChainSection { @@ -78,7 +78,6 @@ pub struct ChainSection { /// (-1 disables). pub consensus_rules: Option>, pub min_gas_limit: Option, - pub max_gas_limit: Option, /// rskj `blockchain.config.hardforkActivationHeights`, as `name = height` /// (-1 takes the upgrade off the ladder). pub hardforks: Option>, @@ -515,7 +514,6 @@ pub const CONFIGURABLE: &[&str] = &[ "skip_pow_validation", "consensus_rule", "min_gas_limit", - "max_gas_limit", ]; #[cfg(test)] diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index c60a59e..af929de 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -1319,10 +1319,6 @@ struct Args { #[arg(long, value_name = "GAS")] min_gas_limit: Option, - /// Regtest: largest gas limit a header may declare (rskj has none). - #[arg(long, value_name = "GAS")] - max_gas_limit: Option, - /// Regtest: schedule an rskj network upgrade, as `name=height` (`-1` /// takes it off the ladder), like rskj's /// `blockchain.config.hardforkActivationHeights`. Repeatable. Names are @@ -1334,8 +1330,8 @@ struct Args { #[arg(long, value_name = "NAME=HEIGHT")] hardfork: Vec, - /// Take --hardfork, --consensus-rule, --skip-pow-validation and the - /// gas-limit bounds on mainnet and testnet too, for replaying blocks + /// Take --hardfork, --consensus-rule, --skip-pow-validation and + /// --min-gas-limit on mainnet and testnet too, for replaying blocks /// mined on a copy of the chain under rules it has not activated. Never /// for a node that follows the real network. --genesis stays refused: /// a genesis file would make this another chain. @@ -1400,7 +1396,6 @@ fn apply_file_config( }); apply(matches, "consensus_rule", rules.as_ref(), &mut a.consensus_rule); apply_opt(matches, "min_gas_limit", f.chain.min_gas_limit.as_ref(), &mut a.min_gas_limit); - apply_opt(matches, "max_gas_limit", f.chain.max_gas_limit.as_ref(), &mut a.max_gas_limit); let hardforks = f.chain.hardforks.as_ref().map(|r| { r.iter().map(|(k, v)| format!("{k}={v}")).collect::>() }); @@ -3856,7 +3851,7 @@ fn run_export_blocks( } /// Apply the regtest chain options (`--genesis`, `--skip-pow-validation`, -/// `--consensus-rule`, `--hardfork`, `--min-gas-limit`, `--max-gas-limit`). +/// `--consensus-rule`, `--hardfork`, `--min-gas-limit`). /// Refused on mainnet and testnet, whose rules are not the operator's to /// change, unless `--allow-chain-overrides` says this is a copy of the /// chain; `--genesis` is refused there regardless. @@ -3865,17 +3860,16 @@ fn apply_chain_options(mut config: ChainConfig, args: &Args) -> Result Result Command { .arg("--genesis") .arg(Path::new(DATA).join("rskj-regtest-genesis.json")) .args(["--skip-pow-validation", "--consensus-rule", "rskip97=0"]) - .args([ - "--min-gas-limit", - "3000000", - "--max-gas-limit", - "18446744073709551615", - ]) + .args(["--min-gas-limit", "3000000"]) .arg("--import-blocks") .arg(blocks); cmd diff --git a/crates/core/Cargo.toml b/crates/core/Cargo.toml index fd7a19c..daf84f8 100644 --- a/crates/core/Cargo.toml +++ b/crates/core/Cargo.toml @@ -18,6 +18,7 @@ anyhow = { workspace = true } bitcoin = { workspace = true } hex = { workspace = true } hex-literal = "0.4.1" +num-bigint = { workspace = true } serde_json = "1" secp256k1 = { workspace = true } serde = { workspace = true } diff --git a/crates/core/src/config.rs b/crates/core/src/config.rs index eb3fad2..9202aa8 100644 --- a/crates/core/src/config.rs +++ b/crates/core/src/config.rs @@ -195,7 +195,6 @@ pub struct ChainConfig { /// rskj `Constants.getMinGasLimit()`: 3,000,000 on every network /// (`GasLimitRule`). Every rskj genesis declares more. pub min_gas_limit: u64, - pub max_gas_limit: u64, /// The gas the sequential sublist is guaranteed under RSKIP-144 /// (rskj `Constants.getMinSequentialSetGasLimit`). Absent from the /// RSKIP, which says every sublist gets `block gas limit / (N + 1)`; @@ -235,7 +234,6 @@ impl ChainConfig { max_future_block_time: 60, gas_limit_bound_divisor: 1024, min_gas_limit: 3_000_000, - max_gas_limit: 1_000_000_000, min_sequential_set_gas_limit: 10_000_000, activation_heights: ActivationHeights::mainnet(), genesis_spec: None, @@ -256,7 +254,6 @@ impl ChainConfig { max_future_block_time: 540, gas_limit_bound_divisor: 1024, min_gas_limit: 3_000_000, - max_gas_limit: 1_000_000_000, min_sequential_set_gas_limit: 10_000_000, activation_heights: ActivationHeights::testnet(), genesis_spec: None, @@ -277,7 +274,6 @@ impl ChainConfig { max_future_block_time: 0, gas_limit_bound_divisor: 1024, min_gas_limit: 3_000_000, - max_gas_limit: 10_000_000, min_sequential_set_gas_limit: 6_800_000, activation_heights: ActivationHeights::regtest(), genesis_spec: None, diff --git a/crates/core/src/types/header.rs b/crates/core/src/types/header.rs index 22fe248..281f2cb 100644 --- a/crates/core/src/types/header.rs +++ b/crates/core/src/types/header.rs @@ -1,5 +1,6 @@ use alloy_primitives::{Address, Bloom, B256, U256, Bytes}; use alloy_rlp::Encodable; +use num_bigint::BigUint; use serde::{Deserialize, Serialize}; use std::borrow::Cow; @@ -239,6 +240,21 @@ fn low_long(bytes: &[u8]) -> i64 { i64::from_be_bytes(word) } +/// A gas limit's bytes as the typed field holds them: the value, or +/// `U256::MAX` past 256 bits, with the bytes themselves where they aren't how +/// rskj's miner writes that value (`KeptFields::gas_limit`). +fn read_gas_limit(bytes: &[u8]) -> (U256, Option) { + let value = significant(Some(bytes)); + let gas_limit = if value.len() > 32 { + U256::MAX + } else { + U256::from_be_slice(value) + }; + let kept = (value.len() > 32 || bytes != java_signed_bytes(&gas_limit).as_slice()) + .then(|| Bytes::copy_from_slice(bytes)); + (gas_limit, kept) +} + impl Header { /// The sixteen fields every RSK header carries, in wire order, encoded the /// way rskj encodes them. @@ -605,15 +621,8 @@ impl Header { // `GasLimitRule` and an uncle's `ValidGasUsedRule`, for two. let gas_limit = { let bytes = field(9)?.ok_or(refused("no gas limit"))?; - let value = significant(Some(bytes)); - let gas_limit = if value.len() > 32 { - U256::MAX - } else { - U256::from_be_slice(value) - }; - if value.len() > 32 || bytes != java_signed_bytes(&gas_limit).as_slice() { - kept.gas_limit = Some(Bytes::copy_from_slice(bytes)); - } + let (gas_limit, bytes) = read_gas_limit(bytes); + kept.gas_limit = bytes; gas_limit }; let gas_used = java_long(field(10)?)?; @@ -812,7 +821,7 @@ impl Header { /// differently: the BlockHeader contract returns them, `GasCost.toGas` /// and `ValidGasUsedRule` read them as below, a program's invocation /// can't hold more than 32 (`DataWord.valueOf`), and the rules that - /// compare it with the parent's read the whole value. + /// bound it read the whole value. pub fn gas_limit_bytes(&self) -> Cow<'_, [u8]> { match self.kept().and_then(|kept| kept.gas_limit.as_ref()) { Some(bytes) => Cow::Borrowed(bytes), @@ -839,6 +848,30 @@ impl Header { low_long(&self.gas_limit_bytes()) } + /// The gas limit's whole value, `new BigInteger(1, getGasLimit())`, as + /// rskj's `GasLimitRule` and `BlockParentGasLimitRule` read it: of any + /// size, and whatever leading zeros its bytes have. + pub fn gas_limit_value(&self) -> BigUint { + BigUint::from_bytes_be(&self.gas_limit_bytes()) + } + + /// Sets the gas limit as rskj's miner writes it, `toByteArray()`: the + /// fewest bytes, with a sign byte where the top bit is set. Past 256 + /// bits, which the typed field can't hold, the bytes are kept. + pub fn set_gas_limit(&mut self, value: &BigUint) { + let mut bytes = value.to_bytes_be(); + if bytes[0] & 0x80 != 0 { + bytes.insert(0, 0); + } + let (gas_limit, written) = read_gas_limit(&bytes); + self.gas_limit = gas_limit; + let mut kept = self.kept.take().map(|kept| *kept).unwrap_or_default(); + kept.gas_limit = written; + self.kept = (kept != KeptFields::default()).then(|| Box::new(kept)); + self.cached_hash = None; + self.cached_hash_for_merged_mining = None; + } + /// Whether this header's uncles hash is `hash`. rskj compares the bytes /// it read, and ones kept for not being 32 bytes are no hash. pub fn has_ommers_hash(&self, hash: B256) -> bool { @@ -1245,32 +1278,93 @@ mod tests { /// Each of rskj's readers reads the gas limit's bytes its own way: /// `GasCost.toGas` takes more than eight as `Long.MAX_VALUE` and throws - /// on eight with the top bit set, and `ValidGasUsedRule` keeps the low - /// 64 bits as a `long`. + /// on eight with the top bit set, `ValidGasUsedRule` keeps the low 64 + /// bits as a `long`, and the rules that bound it read the whole value. #[test] fn the_gas_limit_reads_as_each_of_rskjs_readers_reads_it() { const LONG_MAX: u64 = i64::MAX as u64; + let two = BigUint::from(2u8); + let ten_million = BigUint::from(10_000_000u32); let past = [&[1][..], &[0; 31], &[1]].concat(); - for (bytes, gas, long) in [ + for (bytes, gas, long, value) in [ // As rskj's miner writes 10,000,000. - (&[0x00, 0x98, 0x96, 0x80][..], Some(10_000_000), 10_000_000), + ( + &[0x00, 0x98, 0x96, 0x80][..], + Some(10_000_000), + 10_000_000, + ten_million.clone(), + ), // The same value in nine bytes. ( &[0, 0, 0, 0, 0, 0, 0x98, 0x96, 0x80], Some(LONG_MAX), 10_000_000, + ten_million, ), // 2^63 in eight bytes, and as rskj's miner writes it. - (&[0x80, 0, 0, 0, 0, 0, 0, 0], None, i64::MIN), - (&[0, 0x80, 0, 0, 0, 0, 0, 0, 0], Some(LONG_MAX), i64::MIN), + (&[0x80, 0, 0, 0, 0, 0, 0, 0], None, i64::MIN, two.pow(63)), + ( + &[0, 0x80, 0, 0, 0, 0, 0, 0, 0], + Some(LONG_MAX), + i64::MIN, + two.pow(63), + ), // Past 256 bits, held only as bytes. - (&past, Some(LONG_MAX), 1), + (&past, Some(LONG_MAX), 1, two.pow(256) + 1u8), ] { let header = respelled(9, element(bytes)).1.unwrap(); assert_eq!(&header.gas_limit_bytes()[..], bytes); assert_eq!(header.gas_limit_as_gas(), gas, "{bytes:02x?}"); assert_eq!(header.gas_limit_low_long(), long, "{bytes:02x?}"); + assert_eq!(header.gas_limit_value(), value, "{bytes:02x?}"); + } + } + + /// rskj's miner writes a gas limit's value with `toByteArray`, and the + /// header holds what it wrote: past 256 bits as its bytes, and under + /// that in the typed field alone, leaving whatever else was kept. + #[test] + fn a_gas_limit_is_set_as_rskjs_miner_writes_it() { + let two = BigUint::from(2u8); + let (mut items, header) = respelled(2, element(&[])); + let mut header = header.unwrap(); + for (value, bytes, typed) in [ + ( + two.pow(256) + 5u8, + [&[1][..], &[0; 31], &[5]].concat(), + U256::MAX, + ), + ( + two.pow(255), + [&[0, 0x80][..], &[0; 31]].concat(), + U256::from(1) << 255, + ), + ( + BigUint::from(12_000_000u32), + vec![0x00, 0xb7, 0x1b, 0x00], + U256::from(12_000_000), + ), + ] { + header.set_gas_limit(&value); + assert_eq!(&header.gas_limit_bytes()[..], bytes, "{value}"); + assert_eq!(header.gas_limit, typed, "{value}"); + assert_eq!(header.gas_limit_value(), value); + assert!(header.kept().unwrap().null_coinbase, "the rest kept stays"); + items[9] = element(&bytes); + let mut written = Vec::new(); + header.encode(&mut written); + assert_eq!(written, list_of(&items)); + assert_eq!(header.hash(), keccak256(&written), "no stale hash"); + let read = Header::decode_with_hash(&mut written.as_slice()).unwrap(); + assert_eq!(read.gas_limit_value(), value); } + assert!(header.kept().unwrap().gas_limit.is_none()); + // With nothing else kept, a gas limit under 256 bits keeps nothing. + let mut plain = standard_test_header(); + plain.set_gas_limit(&two.pow(300)); + assert!(plain.kept().is_some()); + plain.set_gas_limit(&BigUint::from(3_000_000u32)); + assert!(plain.kept().is_none()); } /// rskj reads an empty coinbase as its null address, and anything but diff --git a/crates/core/src/validation/header_rules.rs b/crates/core/src/validation/header_rules.rs index ae391bb..6669b51 100644 --- a/crates/core/src/validation/header_rules.rs +++ b/crates/core/src/validation/header_rules.rs @@ -1,13 +1,8 @@ use super::{HeaderValidator, ParentHeaderValidator, ValidationError}; use crate::types::header::Header; -use alloy_primitives::U256; +use num_bigint::BigUint; use std::time::{SystemTime, UNIX_EPOCH}; -/// Converts a U256 gas limit to u64, returning an error on overflow. -fn gas_limit_as_u64(gas_limit: U256) -> Result { - gas_limit.try_into().map_err(|_| ValidationError::GasLimitOverflow) -} - pub struct BlockNumberRule; impl ParentHeaderValidator for BlockNumberRule { fn validate_with_parent(&self, header: &Header, parent: &Header) -> Result<(), ValidationError> { @@ -137,21 +132,20 @@ impl HeaderValidator for GasUsedRule { } } -/// rskj `GasLimitRule` (`Constants.getMinGasLimit()`, 3,000,000 on every -/// network) and the upper bound rustock adds for its own arithmetic. -pub struct GasLimitBoundsRule { +/// rskj `GasLimitRule`: a block's gas limit, read whole, may not be below +/// `Constants.getMinGasLimit()`, 3,000,000 on every network. Nothing bounds +/// it from above but its parent's (`BlockParentGasLimitRule`). +pub struct GasLimitRule { pub min_gas_limit: u64, - pub max_gas_limit: u64, } -impl HeaderValidator for GasLimitBoundsRule { +impl HeaderValidator for GasLimitRule { fn validate(&self, header: &Header) -> Result<(), ValidationError> { - let gas_limit = gas_limit_as_u64(header.gas_limit)?; - if gas_limit < self.min_gas_limit || gas_limit > self.max_gas_limit { - return Err(ValidationError::GasLimitOutOfBounds { + let got = header.gas_limit_value(); + if got < BigUint::from(self.min_gas_limit) { + return Err(ValidationError::GasLimitBelowMinimum { min: self.min_gas_limit, - max: self.max_gas_limit, - got: gas_limit, + got, }); } Ok(()) @@ -210,25 +204,17 @@ pub struct BlockParentGasLimitRule { impl ParentHeaderValidator for BlockParentGasLimitRule { fn validate_with_parent(&self, header: &Header, parent: &Header) -> Result<(), ValidationError> { - // rskj compares in BigInteger against `parent * (d - 1) / d` and - // `parent * (d + 1) / d`. As integers those are - // `parent - ceil(parent / d)` and `parent + floor(parent / d)`, so the - // lower bound sits one below `parent - parent / d` whenever `d` does - // not divide the parent's limit. - let divisor = U256::from(self.config.gas_limit_bound_divisor); - let parent_limit = parent.gas_limit; - let step = parent_limit / divisor; - let lower = if (parent_limit % divisor).is_zero() { - parent_limit - step - } else { - parent_limit - step - U256::from(1u8) - }; - let upper = parent_limit.saturating_add(step); - - if header.gas_limit < lower || header.gas_limit > upper { + // rskj reads both whole, of any size, and compares in BigInteger + // against `parent * (d - 1) / d` and `parent * (d + 1) / d`. + let got = header.gas_limit_value(); + let parent_limit = parent.gas_limit_value(); + let divisor = self.config.gas_limit_bound_divisor; + let lower = &parent_limit * (divisor - 1) / divisor; + let upper = &parent_limit * (divisor + 1) / divisor; + if got < lower || got > upper { return Err(ValidationError::GasLimitInvalid { parent: parent_limit, - got: header.gas_limit, + got, }); } Ok(()) diff --git a/crates/core/src/validation/mod.rs b/crates/core/src/validation/mod.rs index 15eb4e8..60bb12a 100644 --- a/crates/core/src/validation/mod.rs +++ b/crates/core/src/validation/mod.rs @@ -1,6 +1,7 @@ use crate::types::header::Header; use thiserror::Error; use alloy_primitives::{B256, U256}; +use num_bigint::BigUint; #[derive(Error, Debug, PartialEq, Eq)] pub enum ValidationError { @@ -23,14 +24,12 @@ pub enum ValidationError { #[error("Gas used {used} exceeds gas limit {limit}")] GasUsedExceedsLimit { used: u64, limit: i64 }, - #[error("Gas limit overflows u64")] - GasLimitOverflow, - - #[error("Gas limit {got} out of bounds: [{min}, {max}]")] - GasLimitOutOfBounds { min: u64, max: u64, got: u64 }, + #[error("Gas limit {got} is below the minimum {min}")] + GasLimitBelowMinimum { min: u64, got: BigUint }, + /// Both read whole, as rskj's BigIntegers. #[error("Gas limit {got} is invalid compared to parent {parent}")] - GasLimitInvalid { parent: U256, got: U256 }, + GasLimitInvalid { parent: BigUint, got: BigUint }, #[error("Difficulty mismatch: expected {expected}, got {got}")] DifficultyMismatch { expected: U256, got: U256 }, @@ -227,9 +226,8 @@ impl HeaderVerifier { // First, as rskj reads a header with these fields or not at all. .with_static_rule(HeaderLayoutRule { config: config.clone() }) .with_static_rule(GasUsedRule) - .with_static_rule(GasLimitBoundsRule { - min_gas_limit: config.min_gas_limit, - max_gas_limit: config.max_gas_limit + .with_static_rule(GasLimitRule { + min_gas_limit: config.min_gas_limit, }) .with_static_rule(MergedMiningRule { config: config.clone() }) .with_static_rule(BitcoinTimestampRule { config: config.clone() }) @@ -339,7 +337,7 @@ pub mod header_rules; pub mod difficulty; pub mod merged_mining; -pub use header_rules::{BlockNumberRule, ParentHashRule, TimestampRule, GasUsedRule, GasLimitBoundsRule, BlockParentGasLimitRule, HeaderLayoutRule}; +pub use header_rules::{BlockNumberRule, ParentHashRule, TimestampRule, GasUsedRule, GasLimitRule, BlockParentGasLimitRule, HeaderLayoutRule}; pub use difficulty::DifficultyRule; pub use merged_mining::{BitcoinTimestampRule, MergedMiningRule}; pub use block_rules::{ diff --git a/crates/core/src/validation/tests.rs b/crates/core/src/validation/tests.rs index 1badaa2..abb8e49 100644 --- a/crates/core/src/validation/tests.rs +++ b/crates/core/src/validation/tests.rs @@ -7,6 +7,7 @@ use bitcoin::transaction::{Transaction as BtcTransaction, TxOut, TxIn, OutPoint, use bitcoin::hashes::Hash; use bitcoin::script::PushBytes; use bitcoin::ScriptBuf; +use num_bigint::BigUint; use sha2::{Sha256, Digest}; use std::sync::Arc; @@ -1302,8 +1303,10 @@ fn the_chain_configs_carry_rskj_constants() { // ── rskj GasLimitRuleTests ──────────────────────────────────────────── -fn gas_limit_rule() -> GasLimitBoundsRule { - GasLimitBoundsRule { min_gas_limit: 3_000_000, max_gas_limit: u64::MAX } +fn gas_limit_rule() -> GasLimitRule { + GasLimitRule { + min_gas_limit: 3_000_000, + } } fn with_gas_limit(gas_limit: u64) -> Header { @@ -1327,10 +1330,25 @@ fn gas_limit_equal_minimum_gas_limit() { /// rskj `gasLimitLessThanMinimumGasLimit`. #[test] fn gas_limit_less_than_minimum_gas_limit() { - assert!(matches!( + assert_eq!( gas_limit_rule().validate(&with_gas_limit(3_000_000 - 1)), - Err(ValidationError::GasLimitOutOfBounds { min: 3_000_000, got: 2_999_999, .. }) - )); + Err(ValidationError::GasLimitBelowMinimum { + min: 3_000_000, + got: BigUint::from(2_999_999u32), + }) + ); +} + +/// rskj has no maximum gas limit: `GasLimitRule` holds a block to the +/// minimum alone, and reads its gas limit whole, of any size. +#[test] +fn no_gas_limit_is_too_large() { + let two = BigUint::from(2u8); + let mut header = with_gas_limit(3_000_000); + for value in [BigUint::from(1_000_000_001u32), two.pow(64), two.pow(300)] { + header.set_gas_limit(&value); + assert_eq!(gas_limit_rule().validate(&header), Ok(()), "{value}"); + } } // --- rskj's exact parent gas-limit bound, and the rules each uncle must pass @@ -1359,6 +1377,62 @@ fn the_parent_gas_limit_bounds_are_rskjs_when_the_divisor_leaves_a_remainder() { assert!(rule.validate_with_parent(&with(6_806_641), &parent).is_err()); } +/// Past 256 bits, where the typed field holds `U256::MAX`, rskj still reads +/// both gas limits whole, leading zeros and all, and bounds the child's as +/// it bounds any other. +#[test] +fn the_parent_gas_limit_bounds_hold_past_256_bits() { + let rule = BlockParentGasLimitRule { + config: Arc::new(crate::config::ChainConfig::mainnet()), + }; + let two = BigUint::from(2u8); + let with = |parent: &Header, value: &BigUint| { + let mut header = create_dummy_header(11, 1010, parent.hash()); + header.set_gas_limit(value); + header + }; + + // 2^300 + 12,345: a 1,024th of it is 2^290 + 12.06. + let mut parent = create_dummy_header(10, 1000, B256::ZERO); + let value = two.pow(300) + 12_345u32; + parent.set_gas_limit(&value); + let lower = two.pow(300) - two.pow(290) + 12_332u32; + let upper = two.pow(300) + two.pow(290) + 12_357u32; + for ok in [&lower, &value, &upper] { + assert_eq!( + rule.validate_with_parent(&with(&parent, ok), &parent), + Ok(()) + ); + } + assert_eq!( + rule.validate_with_parent(&with(&parent, &(&lower - 1u8)), &parent), + Err(ValidationError::GasLimitInvalid { + parent: value.clone(), + got: &lower - 1u8 + }) + ); + assert!(rule + .validate_with_parent(&with(&parent, &(&upper + 1u8)), &parent) + .is_err()); + // The parent's value with ten more leading zeros is the parent's value. + let mut padded = with(&parent, &value); + let bytes = [&[0u8; 10][..], &padded.gas_limit_bytes()[..]].concat(); + padded.kept.as_mut().unwrap().gas_limit = Some(Bytes::from(bytes)); + assert_eq!(rule.validate_with_parent(&padded, &parent), Ok(())); + + // A parent of 2^256 - 1, the most the typed field holds, may have a + // child past it, up to 2^256 + 2^246 - 2. + parent.set_gas_limit(&(two.pow(256) - 1u8)); + let upper = two.pow(256) + two.pow(246) - 2u8; + assert_eq!( + rule.validate_with_parent(&with(&parent, &upper), &parent), + Ok(()) + ); + assert!(rule + .validate_with_parent(&with(&parent, &(upper + 1u8)), &parent) + .is_err()); +} + /// rskj `BtcHeaderSizeRule`: from orchid the field holds a Bitcoin header and /// nothing else. A byte appended changes neither the proof of work nor the /// merkle proof, which is why it needs a check of its own. @@ -1719,7 +1793,10 @@ fn default_rsk_holds_uncles_to_rskjs_uncle_rules() { assert_eq!(verifier.verify_uncle(&thin), Ok(())); assert_eq!( verifier.verify(&thin, None), - Err(ValidationError::GasLimitOutOfBounds { min: 3_000_000, max: 10_000_000, got: 2_999_999 }) + Err(ValidationError::GasLimitBelowMinimum { + min: 3_000_000, + got: BigUint::from(2_999_999u32) + }) ); let mut adrift = uncle_of(&parent, 1010); diff --git a/crates/execution/Cargo.toml b/crates/execution/Cargo.toml index 1e3db11..8bcad39 100644 --- a/crates/execution/Cargo.toml +++ b/crates/execution/Cargo.toml @@ -24,6 +24,7 @@ ripemd = { workspace = true } k256 = { workspace = true, features = ["arithmetic"] } bitcoin = { workspace = true } hex = { workspace = true } +num-bigint = { workspace = true } rand = { workspace = true } [dev-dependencies] diff --git a/crates/execution/src/mining/template.rs b/crates/execution/src/mining/template.rs index 87a3f82..ee8ef12 100644 --- a/crates/execution/src/mining/template.rs +++ b/crates/execution/src/mining/template.rs @@ -8,6 +8,7 @@ //! that will later re-execute it on the way in. use alloy_primitives::{Address, B256, Bloom, Bytes, U256}; +use num_bigint::BigUint; use rustock_core::config::ChainConfig; use rustock_core::validation::{DifficultyRule, ValidationError}; use rustock_core::{Block, Header, Transaction, ordered_tx_trie_root}; @@ -206,7 +207,11 @@ impl BlockTemplateBuilder { let timestamp = timestamp_for_child(parent.timestamp); let difficulty = DifficultyRule { config: self.chain_config.clone() } .difficulty_for_child(parent, number, timestamp, ommers.len() as u64); - let gas_limit = next_gas_limit(parent, self.mining_config.gas_limit_target); + let gas_limit = next_gas_limit( + parent, + self.mining_config.gas_limit_target, + self.chain_config.gas_limit_bound_divisor, + ); let minimum_gas_price = next_minimum_gas_price(parent.minimum_gas_price, self.mining_config.min_gas_price_target); @@ -259,7 +264,8 @@ impl BlockTemplateBuilder { version_fields: None, difficulty, number, - gas_limit, + // Written below, as rskj's miner writes it. + gas_limit: U256::ZERO, gas_used: 0, timestamp, extra_data: self.mining_config.extra_data.clone(), @@ -274,6 +280,7 @@ impl BlockTemplateBuilder { cached_hash_for_merged_mining: None, kept: None, }; + header.set_gas_limit(&gas_limit); // The probe: execute the candidates once, in selection order, to // learn two things only execution can tell. @@ -758,21 +765,24 @@ fn timestamp_for_child(parent_timestamp: u64) -> u64 { now.max(parent_timestamp + 1) } -/// One step towards `target`, bounded by what the parent allows. +/// One step towards `target`, bounded by what the parent allows: a +/// `divisor`th of its gas limit, which this reads whole, as the parent rule +/// does, and of any size. /// /// The consensus rule is only that a child's gas limit stays within -/// `parent / 1024` of the parent's, so inheriting it unchanged -- which is +/// `parent / divisor` of the parent's, so inheriting it unchanged -- which is /// what `None` does -- is always valid. rskj `GasLimitCalculator`. -pub(super) fn next_gas_limit(parent: &Header, target: Option) -> U256 { - let parent_limit = parent.gas_limit; +pub(super) fn next_gas_limit(parent: &Header, target: Option, divisor: u64) -> BigUint { + let parent_limit = parent.gas_limit_value(); let Some(target) = target else { return parent_limit; }; - let delta = parent_limit / U256::from(1024); + let target = BigUint::from_bytes_be(&target.to_be_bytes::<32>()); + let delta = &parent_limit / divisor; if target > parent_limit { - target.min(parent_limit.saturating_add(delta)) + target.min(parent_limit + delta) } else { - target.max(parent_limit.saturating_sub(delta)) + target.max(parent_limit - delta) } } diff --git a/crates/execution/src/mining/tests.rs b/crates/execution/src/mining/tests.rs index 988d06d..aac8ceb 100644 --- a/crates/execution/src/mining/tests.rs +++ b/crates/execution/src/mining/tests.rs @@ -36,7 +36,6 @@ fn test_chain_config() -> Arc { max_future_block_time: 0, gas_limit_bound_divisor: 1024, min_gas_limit: 1, - max_gas_limit: 10_000_000, // regtest's, as rskj's own partitioner tests use. min_sequential_set_gas_limit: 6_800_000, activation_heights: ActivationHeights { @@ -995,29 +994,59 @@ fn cant_get_mgp_to_be_negative() { } /// rskj `GasLimitCalculator`: one step towards the target, at most a 1024th -/// of the parent's, which near the top of 256 bits stops there rather than -/// wrapping round to a gas limit no parent allows. +/// of the parent's gas limit, read whole: past 256 bits, where the typed +/// field holds `U256::MAX`, the step is as exact, and with no target the +/// parent's value is kept. #[test] -fn the_gas_limit_steps_a_1024th_and_stops_at_the_top() { +fn the_gas_limit_steps_a_1024th_at_any_size() { use super::template::next_gas_limit; - let parent = |gas_limit: U256| Header { - gas_limit, - ..header_at(1, B256::ZERO, B256::ZERO) + use num_bigint::BigUint; + let step_by = |gas_limit: &BigUint, target: Option, divisor: u64| { + let mut parent = header_at(1, B256::ZERO, B256::ZERO); + parent.set_gas_limit(gas_limit); + next_gas_limit(&parent, target, divisor) }; - let start = U256::from(1_024_000); + let step = |gas_limit: &BigUint, target: Option| step_by(gas_limit, target, 1024); + let start = BigUint::from(1_024_000u32); + assert_eq!(step(&start, Some(U256::MAX)), BigUint::from(1_025_000u32)); + assert_eq!(step(&start, Some(U256::ZERO)), BigUint::from(1_023_000u32)); assert_eq!( - next_gas_limit(&parent(start), Some(U256::MAX)), - U256::from(1_025_000) + step(&start, Some(U256::from(1_024_500))), + BigUint::from(1_024_500u32) ); + assert_eq!(step(&start, None), start); + // The chain's own divisor: a 2048th steps half as far. assert_eq!( - next_gas_limit(&parent(start), Some(U256::ZERO)), - U256::from(1_023_000) - ); - assert_eq!(next_gas_limit(&parent(start), None), start); - assert_eq!( - next_gas_limit(&parent(U256::MAX - U256::from(1)), Some(U256::MAX)), - U256::MAX + step_by(&start, Some(U256::MAX), 2048), + BigUint::from(1_024_500u32) ); + + let two = BigUint::from(2u8); + let past = two.pow(300); + assert_eq!(step(&past, Some(U256::MAX)), &past - two.pow(290)); + assert_eq!(step(&past, None), past); + // A target no further than the top of 256 bits is as far as it goes. + let top = two.pow(256) - 1u8; + assert_eq!(step(&(&top - 1u8), Some(U256::MAX)), top); +} + +/// On a parent whose gas limit is past 256 bits, a template keeps the +/// parent's value, as rskj's miner writes it, which the parent rule takes. +/// The `U256::MAX` the typed field holds is no gas limit the chain allows +/// there. +#[test] +fn a_template_on_a_gas_limit_past_256_bits_keeps_its_value() { + use rustock_core::validation::{BlockParentGasLimitRule, ParentHeaderValidator}; + let mut fx = fixture(20); + let value = num_bigint::BigUint::from(2u8).pow(300); + fx.parent.set_gas_limit(&value); + let header = fx.build_on(&fx.parent).unwrap().block.header; + assert_eq!(header.gas_limit_value(), value); + assert_eq!(header.gas_limit, U256::MAX); + let rule = BlockParentGasLimitRule { + config: fx.chain_config.clone(), + }; + assert_eq!(rule.validate_with_parent(&header, &fx.parent), Ok(())); } /// A coinbase tagged with a hash this node never handed out cannot be matched diff --git a/crates/execution/src/mining/workload.rs b/crates/execution/src/mining/workload.rs index dc7ebc6..93cc138 100644 --- a/crates/execution/src/mining/workload.rs +++ b/crates/execution/src/mining/workload.rs @@ -578,7 +578,6 @@ impl Workload { let mut chain_config = rustock_core::config::ChainConfig::testnet(); chain_config.chain_id = self.chain_id as u8; chain_config.min_difficulty = U256::from(1); - chain_config.max_gas_limit = block_gas_limit.max(1); // The miner and the validator on the same constants: the sequential // sublist's minimum sets every sublist's gas limit, which the // validator holds the miner's partition to. diff --git a/node-light.example.toml b/node-light.example.toml index 58c113b..6e31a02 100644 --- a/node-light.example.toml +++ b/node-light.example.toml @@ -331,7 +331,6 @@ index_history = false # light: this index exists to answer RPC abo # genesis = "genesis.json" # default: unset — an rskj genesis JSON file, instead of rustock's regtest genesis # skip_pow_validation = false # default — rskj's miner.server.skipPowValidation # consensus_rules = { rskip97 = 0 } # default: unset — rskj's blockchain.config.consensusRules; -1 disables -# min_gas_limit = 3000000 # default: unset (regtest: 1) — rskj's regtest minimum -# max_gas_limit = 1000000000 # default: unset (regtest: 10000000) — rskj has no upper bound +# min_gas_limit = 3000000 # default: unset (3000000) — rskj's minimum, on every network; it has no maximum # hardforks = { reed810 = 9002504 } # default: unset — rskj's blockchain.config.hardforkActivationHeights; -1 takes an upgrade off # allow_overrides = false # default — take these on a copy of mainnet or testnet too (genesis excepted) diff --git a/node.example.toml b/node.example.toml index 0246306..8b2a56c 100644 --- a/node.example.toml +++ b/node.example.toml @@ -326,7 +326,6 @@ # genesis = "genesis.json" # default: unset — an rskj genesis JSON file, instead of rustock's regtest genesis # skip_pow_validation = false # default — rskj's miner.server.skipPowValidation # consensus_rules = { rskip97 = 0 } # default: unset — rskj's blockchain.config.consensusRules; -1 disables -# min_gas_limit = 3000000 # default: unset (regtest: 1) — rskj's regtest minimum -# max_gas_limit = 1000000000 # default: unset (regtest: 10000000) — rskj has no upper bound +# min_gas_limit = 3000000 # default: unset (3000000) — rskj's minimum, on every network; it has no maximum # hardforks = { reed810 = 9002504 } # default: unset — rskj's blockchain.config.hardforkActivationHeights; -1 takes an upgrade off # allow_overrides = false # default — take these on a copy of mainnet or testnet too (genesis excepted) From 09a9451c68b7e0fc5c73ba2c3074aa68c1174d43 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Mon, 9 Nov 2026 08:29:26 -0800 Subject: [PATCH 054/264] core: a minimum gas price of any size, as rskj takes it rskj reads a header's minimum gas price as new BigInteger(bytes), of any length (RLP.parseSignedCoinNonNullZero), and holds it as a Coin. PrevMinGasPriceRule lets it move 1% a block either way with nothing above it, so miners raising it as fast as the rule allows pass 2^256 in about 16,000 blocks, under six days. No transaction can pay it there, since a block's transactions are priced under 2^255, but blocks of REMASC alone go on. rustock refused the header, so it would have stopped following the chain. The decoder now keeps a minimum gas price past 256 bits in KeptFields, as rskj writes it back (toByteArray: no leading zeros, and a sign byte where the top bit is set), with U256::MAX in the typed field, and the encoder writes it from there. Header::minimum_gas_price_bytes is Coin.getBytes() and minimum_gas_price_value the whole value; set_minimum_gas_price writes one as rskj's miner does, and shares set_kept and to_byte_array with set_gas_limit. Where it is read: - PrevMinGasPriceRule computes BlockGasPriceRange in BigUint from both whole values, as rskj does in Coin. Its error carries them, and TxsMinGasPriceRule's names the whole minimum. - The BlockHeader contract's getMinGasPrice returns Coin.getBytes() of the whole value. A contract can read a header up to 4,000 blocks back, and it got U256::MAX's bytes. - The miner steps from the parent's whole price within the rule's own range (MinimumGasPriceCalculator is a clamp to it) and writes it as rskj's miner does. With a target set, near 2^256 it wrapped parent + delta and then clamped with the lower bound above the upper, which panics; past 256 bits it wrote U256::MAX. Its transaction filter holds them to U256::MAX there, which none reaches. - eth_getBlockBy*, eth_getUncleBy* and newHeads print the whole value, as rskj's toQuantityJsonHex(getBytes()) does. - GasPriceTracker's floor, best * 11 / 10, wrapped from 2^256 / 11 up, since ruint's operators wrap. It is now exact wherever the answer fits, as rskj's BigDecimal product is. - The rest read the typed field and decide as the whole value would: TxsMinGasPriceRule and BlockTxsMaxGasPriceRule, since no transaction pays 2^255; a program's invocation, whose DataWord takes nothing from 2^255 up; revm's base fee, cut to 64 bits, which is below any price that passes those rules; and the pool's checks. Not here, and tracked separately. With the fee market not working, rskj's eth_gasPrice multiplies the minimum in doubles (EthereumImpl.getGasPrice), which differs from x * 11 / 10 from about 2^49 wei and throws past 2^1024. The pool's quota reads the minimum and the gas limit with longValue, the low 64 bits, where rustock saturates. And rskj's eth_call and eth_sendTransaction default to a gas price of 0, where rustock takes the minimum or the tracker's price. Tests cover: - decoding past 256 bits, with and without leading zeros and sign bytes, and writing it back; at the typed field's own top value nothing is kept; - the setter; - PrevMinGasPriceRule past 256 bits at its exact bounds, and across 2^256 from a parent below it; - TxsMinGasPriceRule past 256 bits; - the miner's step at any size, a template on a parent past 256 bits, and its filter; - the contract's answer, the RPC's, and the tracker's floor near the top of 256 bits. Twenty-nine one-line mutations each fail a test. --- crates/core/src/types/header.rs | 189 +++++++++++++++++--- crates/core/src/validation/block_rules.rs | 105 +++++++++-- crates/core/src/validation/mod.rs | 12 +- crates/execution/src/executor.rs | 49 +++++ crates/execution/src/mining/template.rs | 46 +++-- crates/execution/src/mining/tests.rs | 114 +++++++++++- crates/execution/src/native/block_header.rs | 3 +- crates/rpc/src/helpers.rs | 2 +- crates/rpc/src/subscribe.rs | 2 +- crates/rpc/src/tests.rs | 22 +++ crates/sync/src/gas_price.rs | 26 ++- 11 files changed, 500 insertions(+), 70 deletions(-) diff --git a/crates/core/src/types/header.rs b/crates/core/src/types/header.rs index 281f2cb..c303481 100644 --- a/crates/core/src/types/header.rs +++ b/crates/core/src/types/header.rs @@ -46,7 +46,8 @@ pub struct Header { /// What rskj read from this header that the fields above can't hold, /// kept so that it encodes and hashes as rskj writes it: see - /// [`KeptFields`]. `None` for every header an honest miner writes. + /// [`KeptFields`]. `None` for every header rskj's miner writes, until + /// its gas limit or minimum gas price passes 256 bits. #[serde(default, skip_serializing_if = "Option::is_none")] pub kept: Option>, @@ -91,7 +92,8 @@ pub struct HeaderVersionFields { /// rskj keeps these as it reads them and writes them back the same way, so /// they are in the header's hash, and no rule refuses them where they can /// appear: a miner picks them. Each is kept only where it differs from -/// what the typed field writes, so a header an honest miner wrote has none. +/// what the typed field writes, so a header rskj's miner wrote has none, +/// until its gas limit or minimum gas price passes 256 bits. #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] pub struct KeptFields { /// The gas limit's bytes, where they aren't how rskj's miner writes its @@ -123,6 +125,13 @@ pub struct KeptFields { /// nothing bounds. `paid_fees` holds `U256::MAX`, which compares with /// any block's fees, doubled or not, as the value itself does. pub paid_fees: Option, + /// The minimum gas price past 256 bits, as rskj writes it back + /// (`toByteArray`). `PrevMinGasPriceRule` lets it rise 1% a block with + /// nothing above it, past 2^256 in about 16,000 blocks: no transaction + /// can pay it then, but blocks of REMASC alone go on. + /// `minimum_gas_price` holds `U256::MAX`, which no transaction's price + /// reaches either; see [`Header::minimum_gas_price_value`]. + pub minimum_gas_price: Option, } /// rskj refuses the header: `BlockFactory.decodeHeader` can't read it, or @@ -169,14 +178,21 @@ fn java_int(bytes: Option<&[u8]>) -> alloy_rlp::Result { } /// `new BigInteger(bytes)`, which reads the difficulty and the minimum gas -/// price: two's complement, so a first byte from 0x80 up is negative. -/// `None` for a negative value and for one past 256 bits. -fn java_signed(bytes: &[u8]) -> Option { +/// price, where it isn't negative: two's complement, so a first byte from +/// 0x80 up is. Its bytes from the first that isn't zero. +fn non_negative(bytes: &[u8]) -> Option<&[u8]> { if bytes.first().is_some_and(|byte| byte & 0x80 != 0) { return None; } - let value = significant(Some(bytes)); - (value.len() <= 32).then(|| U256::from_be_slice(value)) + Some(significant(Some(bytes))) +} + +/// The difficulty, `new BigInteger(bytes)`. `None` for a negative value and +/// for one past 256 bits. +fn java_signed(bytes: &[u8]) -> Option { + non_negative(bytes) + .filter(|value| value.len() <= 32) + .map(U256::from_be_slice) } /// A hash rskj keeps as it read it: in `kept` unless it is 32 bytes. @@ -255,6 +271,28 @@ fn read_gas_limit(bytes: &[u8]) -> (U256, Option) { (gas_limit, kept) } +/// A minimum gas price of `value`, its bytes without leading zeros, as the +/// typed field holds it: the value, or `U256::MAX` past 256 bits with what +/// rskj writes back there (`KeptFields::minimum_gas_price`). +fn minimum_gas_price_of(value: &[u8]) -> (U256, Option) { + if value.len() > 32 { + return (U256::MAX, Some(Bytes::from(to_byte_array(value)))); + } + (U256::from_be_slice(value), None) +} + +/// `BigInteger.toByteArray()` of a value that isn't negative, from its +/// bytes without leading zeros: with a sign byte where the top bit is set, +/// and `[0]` for zero. +fn to_byte_array(value: &[u8]) -> Vec { + let mut bytes = Vec::with_capacity(value.len() + 1); + if value.first().is_none_or(|first| first & 0x80 != 0) { + bytes.push(0); + } + bytes.extend_from_slice(value); + bytes +} + impl Header { /// The sixteen fields every RSK header carries, in wire order, encoded the /// way rskj encodes them. @@ -463,7 +501,7 @@ impl Header { kept.and_then(|k| k.paid_fees.as_ref()), &self.paid_fees, ); - java_signed_bytes(&self.minimum_gas_price).as_slice().encode(list); + self.minimum_gas_price_bytes()[..].encode(list); self.uncle_count.encode(list); } @@ -639,12 +677,15 @@ impl Header { U256::from_be_slice(value) } }; - // `RLP.parseSignedCoinNonNullZero`: signed, and none is none. - // `PrevMinGasPriceRule` refuses none wherever rskj takes a header, - // and no parent's range holds a negative price or one past 256 bits. - let minimum_gas_price = field(14)?.and_then(java_signed).ok_or(refused( - "a minimum gas price that is none, negative or past 256 bits", - ))?; + // `RLP.parseSignedCoinNonNullZero`: signed, of any length, and none + // is none. `PrevMinGasPriceRule` refuses none wherever rskj takes a + // header, and no parent's range holds a negative price. Past 256 + // bits it is kept: see `KeptFields::minimum_gas_price`. + let (minimum_gas_price, bytes) = field(14)? + .and_then(non_negative) + .map(minimum_gas_price_of) + .ok_or(refused("a minimum gas price that is none or negative"))?; + kept.minimum_gas_price = bytes; let uncle_count = java_int(field(15)?)?; // What is left is told apart by count, since nothing here knows the @@ -859,14 +900,40 @@ impl Header { /// fewest bytes, with a sign byte where the top bit is set. Past 256 /// bits, which the typed field can't hold, the bytes are kept. pub fn set_gas_limit(&mut self, value: &BigUint) { - let mut bytes = value.to_bytes_be(); - if bytes[0] & 0x80 != 0 { - bytes.insert(0, 0); - } - let (gas_limit, written) = read_gas_limit(&bytes); + let (gas_limit, written) = read_gas_limit(&to_byte_array(&value.to_bytes_be())); self.gas_limit = gas_limit; + self.set_kept(|kept| kept.gas_limit = written); + } + + /// The minimum gas price's bytes as rskj writes and returns them, + /// `Coin.getBytes()`, which is `toByteArray()`: what the BlockHeader + /// contract and the RPC answer, of any size. + pub fn minimum_gas_price_bytes(&self) -> Cow<'_, [u8]> { + match self.kept().and_then(|kept| kept.minimum_gas_price.as_ref()) { + Some(bytes) => Cow::Borrowed(bytes), + None => Cow::Owned(java_signed_bytes(&self.minimum_gas_price)), + } + } + + /// The minimum gas price's whole value, as rskj's `Coin` holds it and + /// `PrevMinGasPriceRule` and its miner read it: of any size. + pub fn minimum_gas_price_value(&self) -> BigUint { + BigUint::from_bytes_be(&self.minimum_gas_price_bytes()) + } + + /// Sets the minimum gas price as rskj's miner writes it, `toByteArray()`: + /// past 256 bits, which the typed field can't hold, it is kept. + pub fn set_minimum_gas_price(&mut self, value: &BigUint) { + let (price, written) = minimum_gas_price_of(&value.to_bytes_be()); + self.minimum_gas_price = price; + self.set_kept(|kept| kept.minimum_gas_price = written); + } + + /// Changes what is kept, keeping nothing where nothing differs, and drops + /// the cached hashes, which a field set with it makes stale. + fn set_kept(&mut self, change: impl FnOnce(&mut KeptFields)) { let mut kept = self.kept.take().map(|kept| *kept).unwrap_or_default(); - kept.gas_limit = written; + change(&mut kept); self.kept = (kept != KeptFields::default()).then(|| Box::new(kept)); self.cached_hash = None; self.cached_hash_for_merged_mining = None; @@ -1561,6 +1628,82 @@ mod tests { writes_back_as(&decoded, &items); } + /// rskj reads a minimum gas price of any length, which nothing bounds but + /// its parent's, and writes it back with `toByteArray`. Past 256 bits the + /// header keeps what it writes, without leading zeros and with a sign + /// byte where the top bit is set, and its value reads whole. + #[test] + fn a_minimum_gas_price_past_256_bits_is_kept() { + let two = BigUint::from(2u8); + let past = [&[1][..], &[0; 32]].concat(); + let signed = [&[0, 0x80][..], &[0; 32]].concat(); + for (spelled, written, value) in [ + (past.clone(), &past, two.pow(256)), + ([&[0, 0][..], &past[..]].concat(), &past, two.pow(256)), + (signed.clone(), &signed, two.pow(263)), + ([&[0][..], &signed[..]].concat(), &signed, two.pow(263)), + ] { + let (mut items, decoded) = respelled(14, element(&spelled)); + let decoded = decoded.unwrap(); + assert_eq!(decoded.minimum_gas_price, U256::MAX); + assert_eq!(&decoded.minimum_gas_price_bytes()[..], &written[..]); + assert_eq!(decoded.minimum_gas_price_value(), value); + items[14] = element(written); + writes_back_as(&decoded, &items); + } + // The typed field's own largest value keeps nothing. + let top = respelled(14, element(&[&[0][..], &[0xff; 32]].concat())) + .1 + .unwrap(); + assert!(top.kept().is_none()); + assert_eq!(top.minimum_gas_price_value(), two.pow(256) - 1u8); + } + + /// rskj's miner writes a minimum gas price with `toByteArray`, and the + /// header holds what it wrote: past 256 bits as its bytes, and under that + /// in the typed field alone, leaving whatever else was kept. + #[test] + fn a_minimum_gas_price_is_set_as_rskjs_miner_writes_it() { + let two = BigUint::from(2u8); + let (mut items, header) = respelled(2, element(&[])); + let mut header = header.unwrap(); + for (value, bytes, typed) in [ + ( + two.pow(256) + 5u8, + [&[1][..], &[0; 31], &[5]].concat(), + U256::MAX, + ), + (two.pow(263), [&[0, 0x80][..], &[0; 32]].concat(), U256::MAX), + ( + two.pow(255), + [&[0, 0x80][..], &[0; 31]].concat(), + U256::from(1) << 255, + ), + (BigUint::from(0x80u8), vec![0, 0x80], U256::from(0x80)), + (BigUint::ZERO, vec![0], U256::ZERO), + ] { + header.set_minimum_gas_price(&value); + assert_eq!(&header.minimum_gas_price_bytes()[..], bytes, "{value}"); + assert_eq!(header.minimum_gas_price, typed, "{value}"); + assert_eq!(header.minimum_gas_price_value(), value); + assert!(header.kept().unwrap().null_coinbase, "the rest kept stays"); + items[14] = element(&bytes); + let mut written = Vec::new(); + header.encode(&mut written); + assert_eq!(written, list_of(&items)); + assert_eq!(header.hash(), keccak256(&written), "no stale hash"); + let read = Header::decode_with_hash(&mut written.as_slice()).unwrap(); + assert_eq!(read.minimum_gas_price_value(), value); + } + assert!(header.kept().unwrap().minimum_gas_price.is_none()); + // With nothing else kept, a price under 256 bits keeps nothing. + let mut plain = standard_test_header(); + plain.set_minimum_gas_price(&two.pow(300)); + assert!(plain.kept().is_some()); + plain.set_minimum_gas_price(&BigUint::from(1_000u32)); + assert!(plain.kept().is_none()); + } + /// What rskj can't read, or reads as a header no rule lets a block or an /// uncle have, is refused. #[test] @@ -1573,7 +1716,11 @@ mod tests { (9, element(&[]), "no gas limit"), (14, element(&[]), "no minimum gas price"), (14, element(&[0xff]), "a negative minimum gas price"), - (14, element(&[1; 33]), "a minimum gas price past 256 bits"), + ( + 14, + element(&[0x80; 33]), + "a negative minimum gas price past 256 bits", + ), ] { assert!(respelled(index, spelling).1.is_err(), "{what}"); } diff --git a/crates/core/src/validation/block_rules.rs b/crates/core/src/validation/block_rules.rs index 9bcfbd7..3902c16 100644 --- a/crates/core/src/validation/block_rules.rs +++ b/crates/core/src/validation/block_rules.rs @@ -10,6 +10,7 @@ use super::{BlockValidator, HeaderValidator, ParentHeaderValidator, ValidationEr use crate::types::block::Block; use crate::types::header::Header; use alloy_primitives::U256; +use num_bigint::BigUint; /// The REMASC contract address (`…01000008`), which the synthetic last /// transaction of every RSK block is sent to. Mirrors @@ -28,6 +29,10 @@ fn is_remasc(block: &Block, index: usize) -> bool { /// rskj `TxsMinGasPriceRule`: every non-REMASC transaction must pay at least /// the block's `minimumGasPrice`. Unconditional — no activation gate. +/// +/// A minimum past 256 bits is `U256::MAX` here. No transaction's price +/// reaches either, since a block's transactions pay under 2^255 +/// (`Transaction::from_rskj`), so the rule decides as rskj's does. pub struct TxsMinGasPriceRule; impl BlockValidator for TxsMinGasPriceRule { @@ -40,7 +45,7 @@ impl BlockValidator for TxsMinGasPriceRule { if tx.gas_price < min { return Err(ValidationError::TxGasPriceBelowMinimum { tx_gas_price: tx.gas_price, - block_minimum: min, + block_minimum: block.header.minimum_gas_price_value(), }); } } @@ -138,20 +143,26 @@ impl HeaderValidator for ExtraDataRule { /// upper = center + delta /// lower = max(0, center - delta) /// ``` -/// and the header's value must lie in `[lower, upper]` inclusive. +/// and the header's value must lie in `[lower, upper]` inclusive. rskj +/// computes it in `Coin`, a `BigInteger`, and nothing bounds the price from +/// above, so both are read whole, of any size. pub struct PrevMinGasPriceRule; impl PrevMinGasPriceRule { /// The inclusive `[lower, upper]` band a child's minimum gas price may /// occupy, given the parent's. - pub fn range(parent_min_gas_price: U256) -> (U256, U256) { + pub fn range(parent_min_gas_price: &BigUint) -> (BigUint, BigUint) { let center = parent_min_gas_price; - let mut delta = center / U256::from(100u64); - if delta.is_zero() { - delta = U256::from(1u64); + let mut delta = center / 100u32; + if delta == BigUint::ZERO { + delta = BigUint::from(1u32); } - let upper = center.saturating_add(delta); - let lower = center.saturating_sub(delta); + let upper = center + δ + let lower = if *center >= delta { + center - &delta + } else { + BigUint::ZERO + }; (lower, upper) } } @@ -161,8 +172,8 @@ impl ParentHeaderValidator for PrevMinGasPriceRule { if header.number == 0 { return Ok(()); } - let (lower, upper) = Self::range(parent.minimum_gas_price); - let got = header.minimum_gas_price; + let (lower, upper) = Self::range(&parent.minimum_gas_price_value()); + let got = header.minimum_gas_price_value(); if got < lower || got > upper { return Err(ValidationError::MinGasPriceOutOfRange { lower, upper, got }); } @@ -319,15 +330,15 @@ mod tests { #[test] fn prev_min_gas_price_allows_one_percent_either_way() { // center 1000 -> delta 10 -> [990, 1010] inclusive. - let (lower, upper) = PrevMinGasPriceRule::range(U256::from(1000u64)); - assert_eq!(lower, U256::from(990u64)); - assert_eq!(upper, U256::from(1010u64)); + let (lower, upper) = PrevMinGasPriceRule::range(&BigUint::from(1000u32)); + assert_eq!(lower, BigUint::from(990u32)); + assert_eq!(upper, BigUint::from(1010u32)); // A zero parent still gets delta 1, so it can rise to 1 (and the lower // bound is clamped at zero rather than going negative). - let (lower, upper) = PrevMinGasPriceRule::range(U256::ZERO); - assert_eq!(lower, U256::ZERO); - assert_eq!(upper, U256::from(1u64)); + let (lower, upper) = PrevMinGasPriceRule::range(&BigUint::ZERO); + assert_eq!(lower, BigUint::ZERO); + assert_eq!(upper, BigUint::from(1u32)); let mut parent = bare_header(8_999_999); parent.minimum_gas_price = U256::from(1000u64); @@ -350,6 +361,68 @@ mod tests { Err(ValidationError::MinGasPriceOutOfRange { .. }) )); } + + /// rskj computes the range in `BigInteger`, so it holds past 256 bits, + /// where the typed field holds `U256::MAX`: 1% either way, exactly. + #[test] + fn prev_min_gas_price_is_exact_past_256_bits() { + let two = BigUint::from(2u8); + let at = |number: u64, price: &BigUint| { + let mut header = bare_header(number); + header.set_minimum_gas_price(price); + header + }; + let check = |parent: &BigUint, price: &BigUint| { + PrevMinGasPriceRule.validate_with_parent(&at(9_000_000, price), &at(8_999_999, parent)) + }; + // 1% of 100 * 2^294 + 12,345 is 2^294 + 123. + let parent = two.pow(294) * 100u32 + 12_345u32; + let lower = two.pow(294) * 99u32 + 12_222u32; + let upper = two.pow(294) * 101u32 + 12_468u32; + assert_eq!( + PrevMinGasPriceRule::range(&parent), + (lower.clone(), upper.clone()) + ); + for price in [&lower, &parent, &upper] { + assert_eq!(check(&parent, price), Ok(())); + } + assert_eq!( + check(&parent, &(&lower - 1u8)), + Err(ValidationError::MinGasPriceOutOfRange { + lower: lower.clone(), + upper: upper.clone(), + got: &lower - 1u8, + }) + ); + assert!(check(&parent, &(&upper + 1u8)).is_err()); + // A parent under 2^256 that 100 divides: its child may rise past + // 2^256 by exactly 1%, and no further. + let parent = two.pow(256) - 36u8; + let upper = &parent / 100u32 * 101u32; + assert!(upper > two.pow(256)); + assert_eq!(check(&parent, &upper), Ok(())); + assert!(check(&parent, &(&upper + 1u8)).is_err()); + } + + /// A minimum past 256 bits refuses every transaction but REMASC, as rskj's + /// does, and the refusal names the minimum whole. + #[test] + fn a_minimum_gas_price_past_256_bits_refuses_all_but_remasc() { + let minimum = BigUint::from(2u8).pow(300); + let mut block = block_at(9_000_000, 0, vec![remasc_tx()]); + block.header.set_minimum_gas_price(&minimum); + assert_eq!(TxsMinGasPriceRule.validate_block(&block), Ok(())); + let mut highest = user_tx(0); + highest.gas_price = (U256::from(1) << 255) - U256::from(1); + block.transactions.insert(0, highest.clone()); + assert_eq!( + TxsMinGasPriceRule.validate_block(&block), + Err(ValidationError::TxGasPriceBelowMinimum { + tx_gas_price: highest.gas_price, + block_minimum: minimum, + }) + ); + } } /// rskj `ValidTxExecutionSublistsEdgesRule` (RSKIP144 + RSKIP351). diff --git a/crates/core/src/validation/mod.rs b/crates/core/src/validation/mod.rs index 60bb12a..9b95284 100644 --- a/crates/core/src/validation/mod.rs +++ b/crates/core/src/validation/mod.rs @@ -80,7 +80,10 @@ pub enum ValidationError { VersionedBeforeRskip351 { number: u64 }, #[error("Tx gas price {tx_gas_price} is below the block minimum {block_minimum}")] - TxGasPriceBelowMinimum { tx_gas_price: U256, block_minimum: U256 }, + TxGasPriceBelowMinimum { + tx_gas_price: U256, + block_minimum: BigUint, + }, #[error("Tx gas price {tx_gas_price} is above the cap {cap}")] TxGasPriceAboveCap { tx_gas_price: U256, cap: U256 }, @@ -91,8 +94,13 @@ pub enum ValidationError { #[error("Extra data is {got} bytes, over the {max}-byte maximum")] ExtraDataTooLarge { max: usize, got: usize }, + /// Read whole, as rskj's `Coin`s. #[error("Minimum gas price {got} outside the allowed range [{lower}, {upper}]")] - MinGasPriceOutOfRange { lower: U256, upper: U256, got: U256 }, + MinGasPriceOutOfRange { + lower: BigUint, + upper: BigUint, + got: BigUint, + }, #[error("Uncle list has {got} entries, over the limit of {max}")] TooManyUncles { max: usize, got: usize }, diff --git a/crates/execution/src/executor.rs b/crates/execution/src/executor.rs index e618089..a9cbea3 100644 --- a/crates/execution/src/executor.rs +++ b/crates/execution/src/executor.rs @@ -10272,6 +10272,55 @@ bf09f6e52420834e8e0e0b1a6df563aba550cf7f99e9724264187c45dcf3d73e8585a0e35196\ assert_eq!(decoded, vec![0x03, 0x87, 0xEE, 0x40]); } + /// getMinGasPrice returns a minimum gas price past 256 bits whole, as + /// rskj's `Coin.getBytes()` does, where the typed field holds `U256::MAX`. + #[test] + fn test_block_header_get_min_gas_price_past_256_bits() { + let store = Arc::new(MemoryTrieStore::new()); + let root = TrieNode::empty(); + + let sender = Address::repeat_byte(0xAA); + let one_rbtc = U256::from(10u64).pow(U256::from(18)); + let root = put_account(&root, store.as_ref(), &sender, 0, one_rbtc); + + let block_store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + + let mut parent = dummy_header(99); + parent.set_minimum_gas_price(&num_bigint::BigUint::from(2u8).pow(263)); + let parent_hash = parent.hash(); + block_store.put_header(&parent).unwrap(); + block_store.put_canonical_hash(99, parent_hash).unwrap(); + + let mut header = dummy_header(100); + header.parent_hash = parent_hash; + + let input = bh_input("getMinGasPrice(int256)", 0); + let tx = rustock_core::Transaction { + nonce: 0, + gas_price: U256::from(0), + gas_limit: U256::from(100_000), + to: Bytes::copy_from_slice(crate::precompiles::BLOCK_HEADER_ADDR.as_slice()), + value: U256::ZERO, + input: Bytes::from(input), + v: 0, + r: U256::ZERO, + s: U256::ZERO, + cached_rlp: None, + }; + + let executor = RskExecutor::new(RskHardforkConfig::all_active(33), block_store); + + let result = executor + .execute_tx(&header, &tx, sender, &root, store) + .unwrap(); + assert!(result.success); + // 2^263, with the sign byte `toByteArray` writes. + assert_eq!( + decode_abi_bytes(&result.output), + [&[0, 0x80][..], &[0; 32]].concat() + ); + } + /// getGasLimit returns the parent's gas limit. #[test] fn test_block_header_get_gas_limit() { diff --git a/crates/execution/src/mining/template.rs b/crates/execution/src/mining/template.rs index ee8ef12..5b03bd0 100644 --- a/crates/execution/src/mining/template.rs +++ b/crates/execution/src/mining/template.rs @@ -10,7 +10,7 @@ use alloy_primitives::{Address, B256, Bloom, Bytes, U256}; use num_bigint::BigUint; use rustock_core::config::ChainConfig; -use rustock_core::validation::{DifficultyRule, ValidationError}; +use rustock_core::validation::{DifficultyRule, PrevMinGasPriceRule, ValidationError}; use rustock_core::{Block, Header, Transaction, ordered_tx_trie_root}; use rustock_trie::{AccountState, TrieKeySlice, TrieNode, TrieStore, account_key}; use std::collections::HashMap; @@ -212,11 +212,19 @@ impl BlockTemplateBuilder { self.mining_config.gas_limit_target, self.chain_config.gas_limit_bound_divisor, ); - let minimum_gas_price = - next_minimum_gas_price(parent.minimum_gas_price, self.mining_config.min_gas_price_target); + let minimum_gas_price = next_minimum_gas_price( + &parent.minimum_gas_price_value(), + self.mining_config.min_gas_price_target, + ); - let (candidates, to_remove) = - self.select_transactions(pending, &state_root, minimum_gas_price, number); + // Past 256 bits the filter holds transactions to U256::MAX, which no + // transaction's price reaches, as none reaches the value. + let (candidates, to_remove) = self.select_transactions( + pending, + &state_root, + u256_or_max(&minimum_gas_price), + number, + ); // rskj `BlockToMineBuilder.build`: `removePendingTransactions(txsToRemove)` // right after the filter, before anything executes. if !to_remove.is_empty() { @@ -270,7 +278,8 @@ impl BlockTemplateBuilder { timestamp, extra_data: self.mining_config.extra_data.clone(), paid_fees: U256::ZERO, - minimum_gas_price, + // Written below, as rskj's miner writes it. + minimum_gas_price: U256::ZERO, uncle_count: ommers.len() as u64, umm_root, bitcoin_merged_mining_header: None, @@ -281,6 +290,7 @@ impl BlockTemplateBuilder { kept: None, }; header.set_gas_limit(&gas_limit); + header.set_minimum_gas_price(&minimum_gas_price); // The probe: execute the candidates once, in selection order, to // learn two things only execution can tell. @@ -791,18 +801,24 @@ pub(super) fn next_gas_limit(parent: &Header, target: Option, divisor: u64 /// rskj `MinimumGasPriceCalculator` (RSKIP-09) with `BlockGasPriceRange`: a /// child may move the minimum gas price by at most 1% of the parent's, or by /// one wei when 1% rounds to nothing. The parent's own value is always in -/// range, so `None` -- inherit -- is always valid. -pub fn next_minimum_gas_price(parent_price: U256, target: Option) -> U256 { +/// range, so `None` -- inherit -- is always valid. rskj's miner takes the +/// parent's price whole, a `Coin`, as the rule reads it: of any size. +pub fn next_minimum_gas_price(parent_price: &BigUint, target: Option) -> BigUint { let Some(target) = target else { - return parent_price; + return parent_price.clone(); }; - let mut delta = parent_price / U256::from(100); - if delta.is_zero() { - delta = U256::from(1); + let (lower, upper) = PrevMinGasPriceRule::range(parent_price); + BigUint::from_bytes_be(&target.to_be_bytes::<32>()).clamp(lower, upper) +} + +/// `value`, or `U256::MAX` past 256 bits, as a header's typed field holds it. +pub(super) fn u256_or_max(value: &BigUint) -> U256 { + let bytes = value.to_bytes_be(); + if bytes.len() > 32 { + U256::MAX + } else { + U256::from_be_slice(&bytes) } - let upper = parent_price + delta; - let lower = parent_price.saturating_sub(delta); - target.clamp(lower, upper) } /// rskj `DifficultyUtils.difficultyToTarget`, the validator's own, so that the diff --git a/crates/execution/src/mining/tests.rs b/crates/execution/src/mining/tests.rs index aac8ceb..1f5205f 100644 --- a/crates/execution/src/mining/tests.rs +++ b/crates/execution/src/mining/tests.rs @@ -8,6 +8,7 @@ use alloy_primitives::{Address, B256, Bytes, U256, address, b256}; use bitcoin::hashes::Hash as _; +use num_bigint::BigUint; use rustock_core::config::{ActivationHeights, ChainConfig}; use rustock_core::validation::merged_mining::{compress_coinbase, compute_coinbase_hash}; use rustock_core::validation::{HeaderValidator, MergedMiningRule, ValidationError}; @@ -953,44 +954,65 @@ fn rebuilds_on_every_batch_when_there_is_no_candidate_to_compare_against() { /// rskj `increaseMgp`: 1000 toward 2000 steps to 1010. #[test] fn increase_mgp() { - assert_eq!(next_minimum_gas_price(U256::from(1000), Some(U256::from(2000))), U256::from(1010)); + assert_eq!( + next_minimum_gas_price(&BigUint::from(1000u32), Some(U256::from(2000))), + BigUint::from(1010u32) + ); } /// rskj `decreaseMGP`: 1000 toward 900 steps to 990. #[test] fn decrease_mgp() { - assert_eq!(next_minimum_gas_price(U256::from(1000), Some(U256::from(900))), U256::from(990)); + assert_eq!( + next_minimum_gas_price(&BigUint::from(1000u32), Some(U256::from(900))), + BigUint::from(990u32) + ); } /// rskj `mgpOnRage`: a target inside the 1% band is reached at once. #[test] fn mgp_on_range() { - assert_eq!(next_minimum_gas_price(U256::from(1000), Some(U256::from(995))), U256::from(995)); + assert_eq!( + next_minimum_gas_price(&BigUint::from(1000u32), Some(U256::from(995))), + BigUint::from(995u32) + ); } /// rskj `previousMgpEqualsTarget`. #[test] fn previous_mgp_equals_target() { - assert_eq!(next_minimum_gas_price(U256::from(1000), Some(U256::from(1000))), U256::from(1000)); + assert_eq!( + next_minimum_gas_price(&BigUint::from(1000u32), Some(U256::from(1000))), + BigUint::from(1000u32) + ); } /// rskj `previousValueIsZero`: 1% of nothing is one wei. #[test] fn previous_value_is_zero() { - assert_eq!(next_minimum_gas_price(U256::ZERO, Some(U256::from(1000))), U256::from(1)); + assert_eq!( + next_minimum_gas_price(&BigUint::ZERO, Some(U256::from(1000))), + BigUint::from(1u32) + ); } /// rskj `previousValueIsSmallTargetIsZero`: one wei can step down to zero. #[test] fn previous_value_is_small_target_is_zero() { - assert_eq!(next_minimum_gas_price(U256::from(1), Some(U256::ZERO)), U256::ZERO); + assert_eq!( + next_minimum_gas_price(&BigUint::from(1u32), Some(U256::ZERO)), + BigUint::ZERO + ); } /// rskj `cantGetMGPtoBeNegative`: a negative target has no counterpart in /// an unsigned type; what is checked is that nothing steps below zero. #[test] fn cant_get_mgp_to_be_negative() { - assert_eq!(next_minimum_gas_price(U256::ZERO, Some(U256::ZERO)), U256::ZERO); + assert_eq!( + next_minimum_gas_price(&BigUint::ZERO, Some(U256::ZERO)), + BigUint::ZERO + ); } /// rskj `GasLimitCalculator`: one step towards the target, at most a 1024th @@ -1000,7 +1022,6 @@ fn cant_get_mgp_to_be_negative() { #[test] fn the_gas_limit_steps_a_1024th_at_any_size() { use super::template::next_gas_limit; - use num_bigint::BigUint; let step_by = |gas_limit: &BigUint, target: Option, divisor: u64| { let mut parent = header_at(1, B256::ZERO, B256::ZERO); parent.set_gas_limit(gas_limit); @@ -1038,7 +1059,7 @@ fn the_gas_limit_steps_a_1024th_at_any_size() { fn a_template_on_a_gas_limit_past_256_bits_keeps_its_value() { use rustock_core::validation::{BlockParentGasLimitRule, ParentHeaderValidator}; let mut fx = fixture(20); - let value = num_bigint::BigUint::from(2u8).pow(300); + let value = BigUint::from(2u8).pow(300); fx.parent.set_gas_limit(&value); let header = fx.build_on(&fx.parent).unwrap().block.header; assert_eq!(header.gas_limit_value(), value); @@ -1049,6 +1070,57 @@ fn a_template_on_a_gas_limit_past_256_bits_keeps_its_value() { assert_eq!(rule.validate_with_parent(&header, &fx.parent), Ok(())); } +/// rskj's `MinimumGasPriceCalculator` steps from the parent's price read +/// whole: past 256 bits, where the typed field holds `U256::MAX`, by exactly +/// 1%, down towards any target a U256 can name. +#[test] +fn the_minimum_gas_price_steps_one_percent_at_any_size() { + let two = BigUint::from(2u8); + // 1% of 100 * 2^294 + 12,345 is 2^294 + 123. + let parent = two.pow(294) * 100u32 + 12_345u32; + let lower = two.pow(294) * 99u32 + 12_222u32; + assert_eq!(next_minimum_gas_price(&parent, Some(U256::ZERO)), lower); + assert_eq!(next_minimum_gas_price(&parent, Some(U256::MAX)), lower); + assert_eq!(next_minimum_gas_price(&parent, None), parent); + // Below 2^256 every target is a U256, so none steps past it. + let top = two.pow(256) - 1u8; + assert_eq!(next_minimum_gas_price(&(&top - 1u8), Some(U256::MAX)), top); +} + +/// The template's filter holds transactions to the minimum, and past 256 +/// bits to `U256::MAX`, which no transaction's price reaches either. revm's +/// base fee, the minimum cut to 64 bits, would refuse only those priced +/// under 2^64, and a sender rich enough could pay more. +#[test] +fn past_256_bits_the_filter_holds_transactions_to_u256_max() { + use super::template::u256_or_max; + let two = BigUint::from(2u8); + assert_eq!(u256_or_max(&two.pow(300)), U256::MAX); + assert_eq!(u256_or_max(&(two.pow(256) - 1u8)), U256::MAX); + assert_eq!( + u256_or_max(&(two.pow(256) - 2u8)), + U256::MAX - U256::from(1) + ); + assert_eq!(u256_or_max(&BigUint::from(1_000u32)), U256::from(1_000)); +} + +/// On a parent whose minimum gas price is past 256 bits, a template keeps the +/// parent's value, as rskj's miner writes it, which the parent rule takes. +#[test] +fn a_template_on_a_minimum_gas_price_past_256_bits_keeps_its_value() { + use rustock_core::validation::{ParentHeaderValidator, PrevMinGasPriceRule}; + let mut fx = fixture(20); + let value = BigUint::from(2u8).pow(300); + fx.parent.set_minimum_gas_price(&value); + let header = fx.build_on(&fx.parent).unwrap().block.header; + assert_eq!(header.minimum_gas_price_value(), value); + assert_eq!(header.minimum_gas_price, U256::MAX); + assert_eq!( + PrevMinGasPriceRule.validate_with_parent(&header, &fx.parent), + Ok(()) + ); +} + /// A coinbase tagged with a hash this node never handed out cannot be matched /// to a block, and the message has to say which of the two reasons it is. #[test] @@ -2173,6 +2245,30 @@ mod partitioned_templates { assert_eq!(pool.pending().len(), 1); } + /// Past 256 bits no transaction can pay the minimum, which the filter + /// holds them to as `U256::MAX`: the template leaves out even one its + /// sender could afford, and the pool is told to drop it, as rskj's + /// `isLowGasPriced` has it. + #[test] + fn past_256_bits_the_minimum_leaves_every_transaction_out() { + let (builder, mut parent, keys, _, _) = + fixture_with_forks(1, RskHardforkConfig::all_active(33)); + parent.set_minimum_gas_price(&BigUint::from(2u8).pow(300)); + let affordable = transfer_with( + &keys[0], + Address::repeat_byte(0xE7), + 0, + 33, + 1_000_000_000, + 21_000, + ); + let pool = Pool(vec![affordable.clone()]); + let template = builder + .build(std::slice::from_ref(&parent), Vec::new(), &pool) + .expect("builds"); + assert_eq!(template.block.transactions.len(), 1, "REMASC alone"); + assert_eq!(pool.evictions(), vec![vec![affordable.hash]]); + } } // ── RSKIP-252: the miner's own price ceiling ────────────────────────── diff --git a/crates/execution/src/native/block_header.rs b/crates/execution/src/native/block_header.rs index 266bdb0..21ee381 100644 --- a/crates/execution/src/native/block_header.rs +++ b/crates/execution/src/native/block_header.rs @@ -213,7 +213,8 @@ impl BlockHeader<'_> { Method::CoinbaseAddress => header.beneficiary.to_vec(), Method::BlockHash => hash.to_vec(), Method::MergedMiningTags => merged_mining_tags(header)?, - Method::MinGasPrice => big_integer_bytes(header.minimum_gas_price), + // `Coin.getBytes()`, of any size. + Method::MinGasPrice => header.minimum_gas_price_bytes().into_owned(), // The bytes as the header holds them: rskj never holds the gas // limit as a number (`KeptFields::gas_limit`). Method::GasLimit => header.gas_limit_bytes().into_owned(), diff --git a/crates/rpc/src/helpers.rs b/crates/rpc/src/helpers.rs index 1c711a5..2e276fb 100644 --- a/crates/rpc/src/helpers.rs +++ b/crates/rpc/src/helpers.rs @@ -218,7 +218,7 @@ impl BlockResultDto { gas_used: to_hex_u64(header.gas_used), timestamp: to_hex_u64(header.timestamp), extra_data: to_hex_bytes(&header.extra_data), - minimum_gas_price: to_hex_u256(&header.minimum_gas_price), + minimum_gas_price: to_hex_quantity_bytes(&header.minimum_gas_price_bytes()), transactions, uncles, size: to_hex_u64(size), diff --git a/crates/rpc/src/subscribe.rs b/crates/rpc/src/subscribe.rs index 9e6b344..f00b833 100644 --- a/crates/rpc/src/subscribe.rs +++ b/crates/rpc/src/subscribe.rs @@ -233,7 +233,7 @@ pub(crate) fn header_dto(header: &Header, hash: B256) -> Value { out.insert("extraData".into(), Value::String(to_hex_bytes(header.extra_data.as_ref()))); out.insert( "minimumGasPrice".into(), - Value::String(to_hex_u256(&header.minimum_gas_price)), + Value::String(to_hex_quantity_bytes(&header.minimum_gas_price_bytes())), ); Value::Object(out) } diff --git a/crates/rpc/src/tests.rs b/crates/rpc/src/tests.rs index 0236b5b..ad0afb8 100644 --- a/crates/rpc/src/tests.rs +++ b/crates/rpc/src/tests.rs @@ -65,6 +65,28 @@ fn a_gas_limit_past_256_bits_prints_whole() { assert_eq!(notified["gasLimit"], json!(whole)); } +/// rskj prints a header's minimum gas price from `Coin.getBytes()`, so one +/// past 256 bits, sign byte and all, prints whole, in a block and in a +/// `newHeads` notification alike. +#[test] +fn a_minimum_gas_price_past_256_bits_prints_whole() { + let mut past = vec![0, 0x80]; + past.extend_from_slice(&[0; 32]); + let header = Header { + minimum_gas_price: U256::MAX, + kept: Some(Box::new(rustock_core::types::header::KeptFields { + minimum_gas_price: Some(past.into()), + ..Default::default() + })), + ..test_header(1) + }; + let whole = format!("0x80{}", "0".repeat(64)); + let block = crate::helpers::BlockResultDto::from_header(&header, B256::ZERO, U256::ZERO); + assert_eq!(block.minimum_gas_price, whole); + let notified = crate::subscribe::header_dto(&header, B256::ZERO); + assert_eq!(notified["minimumGasPrice"], json!(whole)); +} + fn setup_state() -> (RpcState, tempfile::TempDir) { setup_state_with(|store| store) } diff --git a/crates/sync/src/gas_price.rs b/crates/sync/src/gas_price.rs index 0a5f96e..122fd13 100644 --- a/crates/sync/src/gas_price.rs +++ b/crates/sync/src/gas_price.rs @@ -178,10 +178,7 @@ impl GasPriceTracker { }; match inner.best_block_price { None => percentile, - Some(best) => { - let floor = best * U256::from(MULTIPLIER_NUM) / U256::from(MULTIPLIER_DEN); - percentile.max(floor) - } + Some(best) => percentile.max(times_multiplier(best)), } } @@ -305,6 +302,17 @@ impl Inner { } } +/// `value` times 1.1, truncated, as rskj's `BigDecimal` product is: exact +/// while the answer fits in 256 bits, which multiplying by 11 first is not +/// from 2^256 / 11 up. Past it, `U256::MAX`: no account can pay that for +/// 21,000 gas, so the fee market, which needs full blocks, never works there. +fn times_multiplier(value: U256) -> U256 { + let (num, den) = (U256::from(MULTIPLIER_NUM), U256::from(MULTIPLIER_DEN)); + (value / den) + .saturating_mul(num) + .saturating_add(value % den * num / den) +} + #[cfg(test)] mod tests { use super::*; @@ -417,6 +425,16 @@ mod tests { // A floor below it does not. tracker.on_best_block(&header(3, 0, 6_800_000, 10)); assert_eq!(tracker.gas_price(), U256::from(129)); + + // Exact near the top of 256 bits, where 11 times the minimum is not. + let mut best = header(4, 0, 6_800_000, 0); + best.minimum_gas_price = U256::from(1) << 253; + tracker.on_best_block(&best); + let expected: U256 = + "15921412270130976870741010438694587329824622891525577555425417801088055325491" + .parse() + .unwrap(); + assert_eq!(tracker.gas_price(), expected, "2^253 * 1.1"); } /// REMASC carries no gas price; counting it would drag the percentile to From e1f04e4c960b2374711a5caa167363e4a0cb7685 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Mon, 9 Nov 2026 09:45:20 -0800 Subject: [PATCH 055/264] execution: pay an empty coinbase at rskj's null address rskj reads an empty coinbase as RskAddress.nullAddress(), an address of no bytes, and no rule refuses it. REMASC pays it as any coinbase: RemascFeesPayer takes the amount from REMASC and adds it to the payee, which creates the payee's account even for nothing, and logs the payee as a word of zero. The null address's account sits at its own trie key, 0x00 || keccak("")[0..10], 11 bytes where an address's is 31. rustock kept an empty coinbase only to encode the header again, and everywhere else read it as the zero address. REMASC paid that, another account at another key, so rustock would have computed another state root and refused the block. Header::coinbase is the coinbase as rskj reads it, None for the null address. revm cannot hold an address of no bytes, so the payment leaves the journal: - REMASC reads the processing block's coinbase, a sibling's and its publisher's with coinbase(). pay_mining_fees takes an Option, and pay_null_address debits REMASC in the journal and adds the amount to RskChainExt::null_account. - The executor takes it into ContractMarkers::null_account after the REMASC transaction, and merge_markers sums it across sublists. - apply_state_changes credits the null account at its own key after the batch, as it bumps the REMASC sender. - The supply checks, the transaction's and the block's, count the credit as inflow (SupplyReport::with_null_account). Without it each such payment would be logged as value burnt and raise an alert. Where else rskj reads the coinbase: - The BlockHeader contract's getCoinbaseAddress and getUncleCoinbaseAddress return getBytes(), no bytes here. - eth_getBlockBy* and eth_getUncleBy* print the miner as "0x" (toUnformattedJsonHex), and newHeads as "0x00" (toJsonHex). - eth_hashrate counts a block as this node's by RskAddress.equals, by which the null address is no miner's, the zero address's included. - COINBASE already pushed zero, as rskj's DataWord of no bytes does. The trie's key classifier names an 11-byte account key as the null address's. rskj pays the coinbase itself only with REMASC disabled, a test hook rustock lacks. The block rskj builds for a precompile that a contract calls has the zero address as its coinbase, but no precompile reads it there: REMASC refuses such a call first, and the BlockHeader contract reads stored blocks. Not here, and tracked separately: rskj traces REMASC's payments and the Bridge's transfers as CALL subtraces, and its block DTO has fields rustock's leaves out. Tests cover: - decoding an empty coinbase, and twenty zeros; - REMASC paying the null address as the processing block's coinbase, a sibling's, its publisher's and, beside siblings, the processing block's again: the log, the sums, the account at its key, and nothing for the zero address; and creating the account for nothing before RSKIP85; - merging markers, the credit at its key, and the supply report; - both supply checks reporting nothing burnt, read from what they log, in the executor and in the block processor; - the contract, the RPC's printing, eth_hashrate and the classifier. Twenty-eight one-line mutations each fail a test. --- crates/core/src/types/header.rs | 14 +- crates/execution/src/executor.rs | 266 +++++++++++++++++++- crates/execution/src/native/block_header.rs | 50 +++- crates/execution/src/processor.rs | 60 +++++ crates/execution/src/raw_storage.rs | 5 + crates/execution/src/remasc.rs | 69 +++-- crates/execution/src/state.rs | 45 ++++ crates/execution/src/supply.rs | 80 ++++++ crates/rpc/src/eth.rs | 4 +- crates/rpc/src/helpers.rs | 6 +- crates/rpc/src/subscribe.rs | 7 +- crates/rpc/src/tests.rs | 79 ++++++ crates/trie/src/key_mapper.rs | 29 ++- 13 files changed, 685 insertions(+), 29 deletions(-) diff --git a/crates/core/src/types/header.rs b/crates/core/src/types/header.rs index c303481..91b0095 100644 --- a/crates/core/src/types/header.rs +++ b/crates/core/src/types/header.rs @@ -104,7 +104,8 @@ pub struct KeptFields { pub gas_limit: Option, /// An empty coinbase, which rskj reads as its null address, an /// `RskAddress` of no bytes, and writes back empty. `beneficiary` - /// holds zero, which is what COINBASE reads for it. + /// holds zero, which is what COINBASE reads for it; where rskj reads + /// the address itself, see [`Header::coinbase`]. pub null_coinbase: bool, /// An uncles hash that isn't 32 bytes. No rule reads an uncle's, and a /// block's must be the hash of its uncles. `ommers_hash` holds zero. @@ -857,6 +858,15 @@ impl Header { self.kept.as_deref() } + /// The coinbase as rskj holds it: `None` for its null address, which an + /// empty coinbase reads as (`RskAddress.nullAddress()`), an address of + /// no bytes. Its `getBytes()` is empty, and REMASC pays its account at + /// a trie key of its own. + pub fn coinbase(&self) -> Option
{ + let null = self.kept().is_some_and(|kept| kept.null_coinbase); + (!null).then_some(self.beneficiary) + } + /// The gas limit's bytes, as rskj holds them (`getGasLimit`): as they /// came, or as its miner writes the value. Its readers read them /// differently: the BlockHeader contract returns them, `GasCost.toGas` @@ -1442,11 +1452,13 @@ mod tests { let decoded = decoded.unwrap(); assert!(decoded.kept().unwrap().null_coinbase); assert_eq!(decoded.beneficiary, Address::ZERO); + assert_eq!(decoded.coinbase(), None); writes_back_as(&decoded, &items); let zero = Header { beneficiary: Address::ZERO, ..standard_test_header() }; + assert_eq!(zero.coinbase(), Some(Address::ZERO)); assert_ne!( decoded.hash(), zero.hash(), diff --git a/crates/execution/src/executor.rs b/crates/execution/src/executor.rs index a9cbea3..a9db53a 100644 --- a/crates/execution/src/executor.rs +++ b/crates/execution/src/executor.rs @@ -149,6 +149,10 @@ fn merge_markers(into: &mut crate::state::ContractMarkers, from: crate::state::C } into.bump_remasc_sender |= from.bump_remasc_sender; into.raw_storage.extend(from.raw_storage); + into.null_account = match (into.null_account, from.null_account) { + (Some(paid), Some(more)) => Some(paid + more), + (paid, more) => paid.or(more), + }; } /// Fold one sublist's state into the block's reported state. @@ -810,6 +814,8 @@ impl RskExecutor { /// Runs on the journal as it stands immediately after the transaction, so /// the balances are the ones that transaction produced. No database reads: /// the baseline comes from revm's per-transaction `original_info`. + /// `null_account` is what REMASC paid rskj's null address, which revm + /// doesn't hold (`ContractMarkers::null_account`). fn check_tx_supply( state: &revm::state::EvmState, transaction_id: usize, @@ -817,9 +823,11 @@ impl RskExecutor { index: usize, fees_withheld: U256, fees_settled: U256, + null_account: Option, ) -> Result<(), ExecutionError> { let report = crate::supply::transaction_supply_change(state, transaction_id) - .with_fees_in_transit(fees_withheld, fees_settled); + .with_fees_in_transit(fees_withheld, fees_settled) + .with_null_account(null_account); if report.is_balanced() { return Ok(()); } @@ -1683,6 +1691,8 @@ impl RskExecutor { .raw_storage .put(REMASC_ADDR, siblings_slot, Some(vec![0xc0])); evm.ctx.chain_mut().raw_storage.commit_call(); + // What REMASC paid rskj's null address, for the trie. + markers.null_account = evm.ctx.chain_mut().null_account.take(); } // Drain the mining_fee_topic logs into the receipt. @@ -1714,7 +1724,15 @@ impl RskExecutor { if crate::supply::per_transaction_enabled() { use revm::context_interface::ContextTr; let st = &evm.ctx.journal_mut().inner.state; - Self::check_tx_supply(st, supply_tid, header.number, i, U256::ZERO, settled_now)?; + Self::check_tx_supply( + st, + supply_tid, + header.number, + i, + U256::ZERO, + settled_now, + markers.null_account, + )?; } // RSKIP-144: what this transaction read and wrote, attributed // to its sublist. Inert unless the block declares sublist @@ -1928,7 +1946,15 @@ impl RskExecutor { if crate::supply::per_transaction_enabled() { use revm::context_interface::ContextTr; let st = &evm.ctx.journal_mut().inner.state; - Self::check_tx_supply(st, supply_tid, header.number, i, withheld_now, U256::ZERO)?; + Self::check_tx_supply( + st, + supply_tid, + header.number, + i, + withheld_now, + U256::ZERO, + None, + )?; } // RSKIP-144: what this transaction read and wrote, attributed to // its sublist. Inert unless the block declares sublist edges. @@ -2135,7 +2161,15 @@ impl RskExecutor { if crate::supply::per_transaction_enabled() { use revm::context_interface::ContextTr; let st = &evm.ctx.journal_mut().inner.state; - Self::check_tx_supply(st, supply_tid, header.number, i, withheld_now, U256::ZERO)?; + Self::check_tx_supply( + st, + supply_tid, + header.number, + i, + withheld_now, + U256::ZERO, + None, + )?; } // RSKIP-144: what this transaction read and wrote, attributed to // its sublist. Inert unless the block declares sublist edges. @@ -11434,6 +11468,230 @@ bf09f6e52420834e8e0e0b1a6df563aba550cf7f99e9724264187c45dcf3d73e8585a0e35196\ assert!(acct.is_created(), "frontier materialises the empty account"); } + /// A header whose coinbase is empty: rskj's null address. + fn with_null_coinbase(mut header: Header) -> Header { + header.beneficiary = Address::ZERO; + header.kept = Some(Box::new(rustock_core::types::header::KeptFields { + null_coinbase: true, + ..Default::default() + })); + header + } + + /// REMASC at the height after `chain`, each block with its uncles, from a + /// state where it holds `balance`: what it did, and the state after it. + fn remasc_after( + chain: &[(Header, Vec
)], + config: crate::remasc::RemascConfig, + balance: U256, + ) -> (BlockExecutionResult, TrieNode, Arc) { + let blocks = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let mut parent_hash = B256::ZERO; + for (number, (block, uncles)) in chain.iter().enumerate() { + let mut block = block.clone(); + block.parent_hash = parent_hash; + let hash = block.hash(); + blocks.put_header_with_hash(hash, &block).unwrap(); + blocks.put_canonical_hash(number as u64, hash).unwrap(); + if !uncles.is_empty() { + blocks.put_body(hash, &[], uncles).unwrap(); + } + parent_hash = hash; + } + let store = Arc::new(MemoryTrieStore::new()); + let sender = Address::repeat_byte(0xAA); + let ether = U256::from(10u64).pow(U256::from(18)); + let root = put_account(&TrieNode::empty(), store.as_ref(), &sender, 0, ether); + let root = put_account(&root, store.as_ref(), &REMASC_ADDR, 0, balance); + let header = dummy_header(chain.len() as u64); + let remasc = rustock_core::Transaction::remasc(header.number); + let result = RskExecutor::new(RskHardforkConfig::all_active(33), blocks) + .with_remasc_config(config) + .with_bridge_constants(crate::bridge::constants::BridgeConstants::mainnet()) + .execute_block(&header, &[(remasc, sender)], &root, store.clone()) + .unwrap(); + let after = crate::state::apply_state_changes( + &root, + store.as_ref(), + &result.state_changes, + &result.markers, + ); + (result, after, store) + } + + /// The null account in `state`, if it is there. + fn null_account_in(state: &TrieNode, store: &MemoryTrieStore) -> Option { + let key = rustock_trie::account_key_from_bytes(&[]); + state + .get(&TrieKeySlice::from_key(&key), store) + .and_then(|data| AccountState::decode(&data).ok()) + } + + /// rskj pays an empty coinbase at its null address, an account of no + /// address bytes, what it would pay any coinbase: the same sums, the same + /// log but for a payee word of zero, and an account at its own trie key. + /// The zero address is another account, and gets nothing. + #[test] + fn remasc_pays_an_empty_coinbase_at_the_null_address() { + let miner = Address::repeat_byte(0x11); + let fees = U256::from(21_000); + let chain = |coinbase: fn(Header) -> Header| -> Vec<(Header, Vec
)> { + (0..15) + .map(|number| { + let mut block = dummy_header(number); + if number == 5 { + block.beneficiary = miner; + block.paid_fees = fees; + block = coinbase(block); + } + (block, vec![]) + }) + .collect() + }; + let config = crate::remasc::RemascConfig::regtest; + let (paid, _, _) = remasc_after(&chain(|block| block), config(), fees); + let ((null, state, store), imbalances) = crate::supply::imbalances_while(|| { + remasc_after(&chain(with_null_coinbase), config(), fees) + }); + + // `remasc_basic_distribution_no_siblings`: the coinbase is paid 3,327. + assert_eq!(paid.state_changes[&miner].info.balance, U256::from(3_327)); + assert_eq!(paid.markers.null_account, None); + assert_eq!(null.markers.null_account, Some(U256::from(3_327))); + // It leaves the journal for the null account's own key, and the + // transaction's supply check counts it there: moved, not burnt. + assert_eq!(imbalances, Vec::::new()); + assert!(!null.state_changes.contains_key(&miner)); + assert!(!null.state_changes.contains_key(&Address::ZERO)); + assert_eq!( + null.state_changes[&REMASC_ADDR].info.balance, + paid.state_changes[&REMASC_ADDR].info.balance + ); + let (paid_logs, null_logs) = (&paid.tx_results[0].logs, &null.tx_results[0].logs); + assert_eq!(null_logs.len(), paid_logs.len()); + let (paid_log, null_log) = (paid_logs.last().unwrap(), null_logs.last().unwrap()); + assert_eq!( + paid_log.topics()[1], + B256::left_padding_from(miner.as_slice()) + ); + assert_eq!(null_log.topics()[1], B256::ZERO); + // RLP([the processing block's hash, 3,327]), each block's hash its own. + assert_eq!(null_log.data.data[34..], [0x82, 0x0c, 0xff]); + assert_eq!(paid_log.data.data[34..], [0x82, 0x0c, 0xff]); + + let account = null_account_in(&state, &store); + assert_eq!( + account.map(|account| account.balance), + Some(U256::from(3_327)) + ); + let zero = account_key(&Address::ZERO); + assert_eq!( + state.get(&TrieKeySlice::from_key(&zero), store.as_ref()), + None + ); + } + + /// So are a sibling's empty coinbase and that of the block that included + /// it, the publisher's share and the sibling miner's, and the processing + /// block's own where it has siblings. + #[test] + fn remasc_pays_empty_sibling_coinbases_at_the_null_address() { + let miner = Address::repeat_byte(0x11); + let fees = U256::from(21_000); + let chain = |processing: fn(Header) -> Header| -> Vec<(Header, Vec
)> { + (0..15) + .map(|number| { + let mut block = dummy_header(number); + match number { + 5 => { + block.beneficiary = miner; + block.paid_fees = fees; + (processing(block), vec![]) + } + 6 => { + let uncle = make_uncle(5, Address::ZERO, U256::from(31_500), 0); + (with_null_coinbase(block), vec![with_null_coinbase(uncle)]) + } + _ => (block, vec![]), + } + }) + .collect() + }; + let config = crate::remasc::RemascConfig::regtest; + + // `remasc_with_one_sibling`: the publisher 332, each miner 1,497. + let (result, state, store) = remasc_after(&chain(|block| block), config(), fees); + assert_eq!(result.markers.null_account, Some(U256::from(332 + 1_497))); + assert_eq!(result.state_changes[&miner].info.balance, U256::from(1_497)); + assert!(!result.state_changes.contains_key(&Address::ZERO)); + let account = null_account_in(&state, &store); + assert_eq!( + account.map(|account| account.balance), + Some(U256::from(1_829)) + ); + + let (result, _, _) = remasc_after(&chain(with_null_coinbase), config(), fees); + assert_eq!( + result.markers.null_account, + Some(U256::from(332 + 1_497 + 1_497)) + ); + assert!(!result.state_changes.contains_key(&miner)); + assert!(!result.state_changes.contains_key(&Address::ZERO)); + } + + /// What REMASC paid the null address is summed across sublists, as their + /// state is, though the one REMASC transaction pays it all. + #[test] + fn merging_markers_sums_what_remasc_paid_the_null_address() { + let paid = |amount: Option| crate::state::ContractMarkers { + null_account: amount.map(U256::from), + ..Default::default() + }; + for (into, from, merged) in [ + (None, None, None), + (None, Some(5), Some(5)), + (Some(3), None, Some(3)), + (Some(3), Some(5), Some(8)), + ] { + let mut markers = paid(into); + merge_markers(&mut markers, paid(from)); + assert_eq!( + markers.null_account, + merged.map(U256::from), + "{into:?} {from:?}" + ); + } + } + + /// rskj's `addBalance` creates the account it pays, even for nothing + /// (`remasc_zero_payment_creates_recipient_account`), the null address's + /// too. + #[test] + fn remasc_creates_the_null_account_even_for_nothing() { + let mut config = crate::remasc::RemascConfig::regtest(); + // Before RSKIP85, nothing is not too little to pay. + config.rskip85_activation_height = u64::MAX; + let chain: Vec<(Header, Vec
)> = (0..15) + .map(|number| { + let block = dummy_header(number); + ( + if number == 5 { + with_null_coinbase(block) + } else { + block + }, + vec![], + ) + }) + .collect(); + let (result, state, store) = remasc_after(&chain, config, U256::ZERO); + assert_eq!(result.markers.null_account, Some(U256::ZERO)); + assert_eq!( + null_account_in(&state, &store), + Some(AccountState::default()) + ); + } + /// Port of rskj `BridgeSupportIT.callUpdateCollectionsChangeGetsOutOfDust` /// (regression for mainnet #3,103,055): when a pegout's change output is /// dusty, bitcoinj raises it to the non-dust minimum at the recipient's diff --git a/crates/execution/src/native/block_header.rs b/crates/execution/src/native/block_header.rs index 21ee381..96768c5 100644 --- a/crates/execution/src/native/block_header.rs +++ b/crates/execution/src/native/block_header.rs @@ -210,7 +210,7 @@ impl BlockHeader<'_> { /// `null`. fn read(&self, call: &Call, hash: B256, header: &Header) -> Result>, Failure> { let value = match call.method { - Method::CoinbaseAddress => header.beneficiary.to_vec(), + Method::CoinbaseAddress => coinbase_bytes(header), Method::BlockHash => hash.to_vec(), Method::MergedMiningTags => merged_mining_tags(header)?, // `Coin.getBytes()`, of any size. @@ -234,7 +234,7 @@ impl BlockHeader<'_> { .map_err(|_| Failure::threw("an uncle index cannot be negative"))?; self.uncles(hash, header)? .get(index) - .map(|uncle| uncle.beneficiary.to_vec()) + .map(coinbase_bytes) .unwrap_or_default() } // `Block.getCumulativeDifficulty`. @@ -298,6 +298,14 @@ fn non_empty(field: &Option) -> Option<&[u8]> { .filter(|bytes| !bytes.is_empty()) } +/// `getCoinbase().getBytes()`: none for rskj's null address, which an +/// empty coinbase reads as. +fn coinbase_bytes(header: &Header) -> Vec { + header + .coinbase() + .map_or_else(Vec::new, |coinbase| coinbase.to_vec()) +} + /// `BigInteger.toByteArray()` of a non-negative number: its shortest /// big-endian form, with a leading zero byte when the first one's top bit /// is set, and `[0]` for zero. @@ -504,6 +512,44 @@ mod tests { )); } + /// rskj's null address, which an empty coinbase reads as, has no bytes, + /// for a block's coinbase and an uncle's alike. The zero address has 20. + #[test] + fn the_null_address_is_no_bytes() { + let null = || { + Some(Box::new(rustock_core::types::header::KeptFields { + null_coinbase: true, + ..Default::default() + })) + }; + let (_dir, blocks, hashes) = chain_with(10, |block| { + if block.number == 10 { + block.beneficiary = Address::ZERO; + block.kept = null(); + block.uncle_count = 1; + } + }); + let mut uncle = header(9, hashes[8]); + uncle.beneficiary = Address::ZERO; + uncle.kept = null(); + blocks.put_body(hashes[10], &[], &[uncle]).unwrap(); + let contract = at(&blocks, &hashes, 11); + let returned = |gas, value: &[u8]| Outcome::Returned { + gas, + output: encode_bytes(value).into(), + }; + let get = input("getCoinbaseAddress(int256)", &[int(0)]); + assert_eq!(call(&contract, &get, 1_000_000), returned(4_072, &[])); + let get = input("getUncleCoinbaseAddress(int256,int256)", &[int(0), int(0)]); + assert_eq!(call(&contract, &get, 1_000_000), returned(4_136, &[])); + // Block 0's coinbase is the zero address, which is 20 bytes. + let genesis = input("getCoinbaseAddress(int256)", &[int(0)]); + assert_eq!( + call(&at(&blocks, &hashes, 1), &genesis, 1_000_000), + returned(4_072, &[0; 20]) + ); + } + #[test] fn an_uncle_is_read_by_an_index_that_must_decode() { let (_dir, blocks, hashes) = chain_with(10, |block| { diff --git a/crates/execution/src/processor.rs b/crates/execution/src/processor.rs index 7b73d5f..2f6aecd 100644 --- a/crates/execution/src/processor.rs +++ b/crates/execution/src/processor.rs @@ -488,6 +488,7 @@ impl BlockProcessor { trie_store.as_ref(), &exec_result.state_changes, ) + .with_null_account(exec_result.markers.null_account) } else { crate::supply::SupplyReport::default() }; @@ -1649,6 +1650,65 @@ mod tests { ); } + /// What REMASC pays the null address reaches its account after the + /// transactions, outside their state changes, and the block's supply check + /// counts it there: the payment moves value and burns none. + #[test] + fn paying_the_null_address_burns_nothing() { + let block_store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let mut parent_hash = B256::ZERO; + for number in 0..15 { + let mut header = dummy_header(number); + header.parent_hash = parent_hash; + if number == 5 { + // An empty coinbase: rskj's null address. + header.beneficiary = Address::ZERO; + header.paid_fees = U256::from(21_000); + header.kept = Some(Box::new(rustock_core::types::header::KeptFields { + null_coinbase: true, + ..Default::default() + })); + } + let hash = header.hash(); + block_store.put_header_with_hash(hash, &header).unwrap(); + block_store.put_canonical_hash(number, hash).unwrap(); + parent_hash = hash; + } + let store: Arc = Arc::new(MemoryTrieStore::new()); + let remasc = crate::precompiles::REMASC_ADDR; + let root = put_account( + &TrieNode::empty(), + store.as_ref(), + &remasc, + 0, + U256::from(21_000), + ); + let mut header = dummy_header(15); + header.parent_hash = parent_hash; + let block = Block { + header, + transactions: vec![Transaction::remasc(15)], + ommers: vec![], + }; + + let processor = BlockProcessor::new(test_hardfork_cfg(), block_store); + let (processed, imbalances) = crate::supply::imbalances_while(|| { + processor.execute_block(&block, &root, store.clone()) + }); + // `remasc_basic_distribution_no_siblings`: the coinbase is paid 3,327. + let null = rustock_trie::account_key_from_bytes(&[]); + let account = processed + .unwrap() + .new_state_root + .get(&TrieKeySlice::from_key(&null), store.as_ref()) + .and_then(|data| AccountState::decode(&data).ok()); + assert_eq!( + account.map(|account| account.balance), + Some(U256::from(3_327)) + ); + assert_eq!(imbalances, Vec::::new()); + } + /// The chain configuration's sequential minimum reaches the executor. On /// regtest's 6,800,000, a 12,000,000-gas block's parallel sublists hold /// 2,600,000 each; testnet's 10,000,000 leaves them 1,000,000. diff --git a/crates/execution/src/raw_storage.rs b/crates/execution/src/raw_storage.rs index 1ccaca4..72246ac 100644 --- a/crates/execution/src/raw_storage.rs +++ b/crates/execution/src/raw_storage.rs @@ -39,6 +39,11 @@ pub struct RskChainExt { /// from outside to survive; `None` disables caching entirely, which is /// what tests and one-shot tools use. pub btc_block_cache: Option>, + /// What REMASC paid rskj's null address in this block, if it paid it: + /// the account of a header whose coinbase is empty. The null address + /// has no bytes, so revm can't hold its account, and the trie is + /// credited at the block's end instead (`ContractMarkers::null_account`). + pub null_account: Option, } /// Raw-bytes storage overlay for the block being executed. diff --git a/crates/execution/src/remasc.rs b/crates/execution/src/remasc.rs index 1266f12..448ee89 100644 --- a/crates/execution/src/remasc.rs +++ b/crates/execution/src/remasc.rs @@ -253,10 +253,11 @@ pub fn remasc_transfer(ctx: &mut CTX, recipient: Addre #[derive(Debug, Clone)] pub struct Sibling { pub hash: B256, - pub coinbase: Address, + /// The uncle's coinbase: `None` for rskj's null address ([`Header::coinbase`]). + pub coinbase: Option
, pub paid_fees: U256, - /// Coinbase of the block that included this uncle. - pub included_block_coinbase: Address, + /// Coinbase of the block that included this uncle, likewise. + pub included_block_coinbase: Option
, /// Height of the block that included this uncle. pub included_height: u64, pub uncle_count: u64, @@ -300,9 +301,9 @@ fn collect_siblings( if ommer.number == candidate_number { siblings.push(Sibling { hash: ommer.hash(), - coinbase: ommer.beneficiary, + coinbase: ommer.coinbase(), paid_fees: ommer.paid_fees, - included_block_coinbase: including_header.beneficiary, + included_block_coinbase: including_header.coinbase(), included_height: height, uncle_count: ommer.uncle_count, }); @@ -404,20 +405,26 @@ impl SiblingPayment { /// rskj `RemascFeesPayer.payMiningFees`: transfer plus a `mining_fee_topic` /// log (topics: [topic, payee-as-word], data: RLP([processingBlockHash, value])). -/// Format groundtruthed by the mainnet #4010 REMASC receipt. +/// Format groundtruthed by the mainnet #4010 REMASC receipt. `to` is `None` +/// for rskj's null address, whose word is zero (`DataWord.valueOf` of no +/// bytes). fn pay_mining_fees( ctx: &mut CTX, processing_block_hash: alloy_primitives::B256, - to: Address, + to: Option
, value: U256, ) { use alloy_rlp::Encodable; use revm::context_interface::JournalTr; - remasc_transfer(ctx, to, value); - let mut topic_payee = [0u8; 32]; - topic_payee[12..].copy_from_slice(to.as_slice()); + match to { + Some(to) => { + remasc_transfer(ctx, to, value); + topic_payee[12..].copy_from_slice(to.as_slice()); + } + None => pay_null_address(ctx, value), + } let mut inner = Vec::with_capacity(40); processing_block_hash.as_slice().encode(&mut inner); @@ -436,6 +443,26 @@ fn pay_mining_fees( )); } +/// rskj's `addBalance(nullAddress, value)`, after REMASC's own. The null +/// address has no bytes for revm to hold, so REMASC's balance goes down +/// here and the null account's goes up in the trie at the block's end +/// (`RskChainExt::null_account`), which creates it if it is new, even for +/// nothing, as `addBalance` does. +fn pay_null_address(ctx: &mut CTX, value: U256) { + use revm::context_interface::journaled_state::account::JournaledAccountTr; + if !value.is_zero() { + // As `remasc_transfer`: a payment REMASC can't cover isn't made. + let paid = ctx + .journal_mut() + .load_account_mut(REMASC_ADDR) + .is_ok_and(|mut remasc| remasc.decr_balance(value)); + if !paid { + return; + } + } + *ctx.chain_mut().null_account.get_or_insert(U256::ZERO) += value; +} + /// Active federation members as RSK addresses: the Bridge-stored federation, /// or the genesis federation while none is stored. Members are ordered by /// compressed BTC public key (rskj Federation constructor), groundtruthed by @@ -526,7 +553,7 @@ fn pay_to_federation( let last = federators.len() - 1; for (k, federator) in federators.iter().enumerate() { let amount = if k == last { per_federator + remainder } else { per_federator }; - pay_mining_fees(ctx, processing_block_hash, *federator, amount); + pay_mining_fees(ctx, processing_block_hash, Some(*federator), amount); } federation_reward } @@ -631,7 +658,7 @@ pub fn process_miners_fees( // Pay RSK Labs (use RSKIP218 address if active) let rsk_labs_pay = synthetic_reward / U256::from(config.rsk_labs_divisor); let labs_addr = config.labs_address_at(current_number); - pay_mining_fees(ctx, processing_hash, labs_addr, rsk_labs_pay); + pay_mining_fees(ctx, processing_hash, Some(labs_addr), rsk_labs_pay); let mut remaining = synthetic_reward - rsk_labs_pay; // Pay the federation its cut (rskj Remasc.payToFederation) @@ -657,7 +684,12 @@ pub fn process_miners_fees( remaining -= punishment; add_to_burned(ctx, punishment); } - pay_mining_fees(ctx, processing_hash, processing_header.beneficiary, remaining); + pay_mining_fees( + ctx, + processing_hash, + processing_header.coinbase(), + remaining, + ); } Ok(()) @@ -706,7 +738,12 @@ fn pay_with_siblings( } // Pay main chain miner - pay_mining_fees(ctx, processing_hash, processing_header.beneficiary, calc.individual_miner_reward); + pay_mining_fees( + ctx, + processing_hash, + processing_header.coinbase(), + calc.individual_miner_reward, + ); } /// Add amount to burnedBalance storage. @@ -913,9 +950,9 @@ mod tests { hash.0[0] = hash_byte; Sibling { hash, - coinbase: Address::repeat_byte(0x22), + coinbase: Some(Address::repeat_byte(0x22)), paid_fees: U256::from(paid_fees), - included_block_coinbase: Address::repeat_byte(0x33), + included_block_coinbase: Some(Address::repeat_byte(0x33)), included_height: 6, uncle_count, } diff --git a/crates/execution/src/state.rs b/crates/execution/src/state.rs index ad738d1..6ec9386 100644 --- a/crates/execution/src/state.rs +++ b/crates/execution/src/state.rs @@ -34,6 +34,12 @@ pub struct ContractMarkers { /// value per unitrie storage key, bypassing revm's U256-valued slots. /// `None` deletes the entry. pub raw_storage: Vec, + /// What REMASC paid rskj's null address, if it paid it: the account of + /// a coinbase that is empty (`RskAddress.nullAddress()`), whose address + /// has no bytes, at the trie key of none. revm can't hold it, so it is + /// credited here, as `bump_remasc_sender` is, and created if it is new, + /// even for nothing, as rskj's `addBalance` creates it. + pub null_account: Option, } /// Apply all state changes from EVM execution to the trie, returning the new root. @@ -152,6 +158,16 @@ pub fn apply_state_changes( new_root = new_root.put(&key, &acct.encode(), store); } + if let Some(paid) = markers.null_account { + let key = TrieKeySlice::from_key(&rustock_trie::account_key_from_bytes(&[])); + let mut account = new_root + .get(&key, store) + .and_then(|data| AccountState::decode(&data).ok()) + .unwrap_or_default(); + account.balance += paid; + new_root = new_root.put(&key, &account.encode(), store); + } + new_root } @@ -477,6 +493,35 @@ mod tests { assert_eq!(new_root.get(&marker_key, &store), Some(vec![1])); } + /// What REMASC paid rskj's null address lands at the account key of no + /// address bytes: on top of what it held, and created, even for nothing, + /// where it held nothing. The zero address is another account. + #[test] + fn the_null_account_is_credited_at_its_own_key() { + let (store, root) = empty_trie(); + let null_key = rustock_trie::account_key_from_bytes(&[]); + let null = |root: &TrieNode| { + root.get(&TrieKeySlice::from_key(&null_key), &store) + .and_then(|data| AccountState::decode(&data).ok()) + }; + let paid = |amount: u64| ContractMarkers { + null_account: Some(U256::from(amount)), + ..Default::default() + }; + let none = EvmState::default(); + let created = apply_state_changes(&root, &store, &none, &paid(0)); + assert_eq!(null(&created), Some(AccountState::default())); + let credited = apply_state_changes(&created, &store, &none, &paid(3_327)); + let again = apply_state_changes(&credited, &store, &none, &paid(1_000)); + assert_eq!( + null(&again).map(|account| account.balance), + Some(U256::from(4_327)) + ); + assert!(read_account(&again, &store, &Address::ZERO).is_none()); + let unpaid = apply_state_changes(&root, &store, &none, &ContractMarkers::default()); + assert_eq!(null(&unpaid), None); + } + #[test] fn test_apply_selfdestruct() { let (store, root) = empty_trie(); diff --git a/crates/execution/src/supply.rs b/crates/execution/src/supply.rs index 45a0750..43826a5 100644 --- a/crates/execution/src/supply.rs +++ b/crates/execution/src/supply.rs @@ -118,6 +118,16 @@ impl SupplyReport { self } + /// Account for what REMASC paid rskj's null address, the account of an + /// empty coinbase. revm can't hold it, so REMASC's side is in the state + /// and the null account's isn't: the trie credits it at the block's end + /// (`ContractMarkers::null_account`). The value moves, and counts as + /// though it had been credited. + pub fn with_null_account(mut self, paid: Option) -> Self { + self.inflow = self.inflow.saturating_add(paid.unwrap_or_default()); + self + } + /// BTC was destroyed. Worth recording, not fatal. pub fn destroyed_supply(&self) -> bool { self.outflow > self.inflow @@ -393,6 +403,45 @@ pub fn report_transaction(block: u64, index: usize, report: &SupplyReport) { } } +/// What `f` logs as a supply imbalance on this thread, a line per event. An +/// imbalance that burns value is only logged, so this is how a test sees that +/// the checks balanced; `report_transaction`'s lines name a `tx`. +#[cfg(test)] +pub(crate) fn imbalances_while(f: impl FnOnce() -> R) -> (R, Vec) { + use std::fmt::Write as _; + use std::sync::{Arc, Mutex}; + + struct Line(String); + impl tracing::field::Visit for Line { + fn record_debug(&mut self, field: &tracing::field::Field, value: &dyn std::fmt::Debug) { + let _ = write!(self.0, " {}={value:?}", field.name()); + } + } + struct Recorder(Arc>>); + impl tracing::Subscriber for Recorder { + fn enabled(&self, metadata: &tracing::Metadata<'_>) -> bool { + metadata.target() == "rustock::supply" + } + fn new_span(&self, _: &tracing::span::Attributes<'_>) -> tracing::span::Id { + tracing::span::Id::from_u64(1) + } + fn record(&self, _: &tracing::span::Id, _: &tracing::span::Record<'_>) {} + fn record_follows_from(&self, _: &tracing::span::Id, _: &tracing::span::Id) {} + fn event(&self, event: &tracing::Event<'_>) { + let mut line = Line(event.metadata().level().to_string()); + event.record(&mut line); + self.0.lock().unwrap().push(line.0); + } + fn enter(&self, _: &tracing::span::Id) {} + fn exit(&self, _: &tracing::span::Id) {} + } + + let lines = Arc::new(Mutex::new(Vec::new())); + let out = tracing::subscriber::with_default(Recorder(lines.clone()), f); + let lines = std::mem::take(&mut *lines.lock().unwrap()); + (out, lines) +} + #[cfg(test)] mod movement_tests { use super::*; @@ -616,4 +665,35 @@ mod tests { let r = account_supply_change(&root, &store, &state); assert!(r.is_balanced(), "a peg-in must not change the total: {r:?}"); } + + /// REMASC paying rskj's null address, whose account revm doesn't hold: + /// it is a payment, in the block and in the transaction. + #[test] + fn a_payment_to_the_null_address_moves_value() { + let remasc = crate::precompiles::REMASC_ADDR; + let (store, root) = store_with(&[(remasc, 10_000)]); + let before = AccountInfo { + balance: U256::from(10_000), + ..Default::default() + }; + let mut paid = Account::from(before.clone()); + paid.original_info = Box::new(before); + paid.info.balance = U256::from(6_673); + paid.transaction_id = 1; + paid.mark_touch(); + let mut state = EvmState::default(); + state.insert(remasc, paid); + + let block = account_supply_change(&root, &store, &state); + assert_eq!(block.net(), (false, U256::from(3_327)), "{block:?}"); + let block = block.with_null_account(Some(U256::from(3_327))); + assert!(block.is_balanced(), "{block:?}"); + let transaction = transaction_supply_change(&state, 1); + assert!(transaction.destroyed_supply(), "{transaction:?}"); + let transaction = transaction.with_null_account(Some(U256::from(3_327))); + assert!(transaction.is_balanced(), "{transaction:?}"); + assert!(SupplyReport::default() + .with_null_account(None) + .is_balanced()); + } } diff --git a/crates/rpc/src/eth.rs b/crates/rpc/src/eth.rs index 9a28bda..fc1e2e5 100644 --- a/crates/rpc/src/eth.rs +++ b/crates/rpc/src/eth.rs @@ -789,7 +789,9 @@ fn hashrate_over_window( // which are counted. An uncle is credited to the block that included // it, as rskj does -- `getCumulativeDifficulty` sums them into the // including block and the ownership test is applied to that block. - let counts = coinbase.is_none_or(|want| block.header.beneficiary == want); + // That test is `RskAddress.equals`: an empty coinbase is the null + // address, which is no miner's, not even the zero address's. + let counts = coinbase.is_none_or(|want| block.header.coinbase() == Some(want)); if counts { total = total.saturating_add(block.header.difficulty); for uncle in &block.ommers { diff --git a/crates/rpc/src/helpers.rs b/crates/rpc/src/helpers.rs index 2e276fb..2d2b8d6 100644 --- a/crates/rpc/src/helpers.rs +++ b/crates/rpc/src/helpers.rs @@ -207,7 +207,11 @@ impl BlockResultDto { hash: to_hex_b256(&hash), parent_hash: to_hex_b256(&header.parent_hash), sha3_uncles: to_hex_b256(&header.ommers_hash), - miner: format!("{:#x}", header.beneficiary), + // `toUnformattedJsonHex(getCoinbase().getBytes())`: `0x` for the + // null address an empty coinbase reads as. + miner: header + .coinbase() + .map_or_else(|| "0x".to_owned(), |coinbase| format!("{coinbase:#x}")), state_root: to_hex_b256(&header.state_root), transactions_root: to_hex_b256(&header.transactions_root), receipts_root: to_hex_b256(&header.receipts_root), diff --git a/crates/rpc/src/subscribe.rs b/crates/rpc/src/subscribe.rs index f00b833..3ccf56c 100644 --- a/crates/rpc/src/subscribe.rs +++ b/crates/rpc/src/subscribe.rs @@ -210,7 +210,12 @@ pub(crate) fn header_dto(header: &Header, hash: B256) -> Value { out.insert("sha3Uncles".into(), Value::String(to_hex_b256(&header.ommers_hash))); out.insert( "miner".into(), - Value::String(format!("0x{}", hex::encode(header.beneficiary.as_slice()))), + // `toJsonHex(getCoinbase().getBytes())`, which writes the null + // address's no bytes as `0x00`. + Value::String(header.coinbase().map_or_else( + || "0x00".to_owned(), + |coinbase| format!("0x{}", hex::encode(coinbase.as_slice())), + )), ); out.insert("stateRoot".into(), Value::String(to_hex_b256(&header.state_root))); out.insert( diff --git a/crates/rpc/src/tests.rs b/crates/rpc/src/tests.rs index ad0afb8..7c09a25 100644 --- a/crates/rpc/src/tests.rs +++ b/crates/rpc/src/tests.rs @@ -87,6 +87,34 @@ fn a_minimum_gas_price_past_256_bits_prints_whole() { assert_eq!(notified["minimumGasPrice"], json!(whole)); } +/// rskj prints the null address, which an empty coinbase reads as, from +/// its no bytes: `0x` in a block (`toUnformattedJsonHex`) and `0x00` in a +/// `newHeads` notification (`toJsonHex`). The zero address is 20 bytes. +#[test] +fn the_null_address_prints_as_no_bytes() { + let null = Header { + beneficiary: Address::ZERO, + kept: Some(Box::new(rustock_core::types::header::KeptFields { + null_coinbase: true, + ..Default::default() + })), + ..test_header(1) + }; + let block = crate::helpers::BlockResultDto::from_header(&null, B256::ZERO, U256::ZERO); + assert_eq!(block.miner, "0x"); + let notified = crate::subscribe::header_dto(&null, B256::ZERO); + assert_eq!(notified["miner"], json!("0x00")); + let zero = Header { + beneficiary: Address::ZERO, + ..test_header(1) + }; + let twenty = format!("0x{}", "0".repeat(40)); + let block = crate::helpers::BlockResultDto::from_header(&zero, B256::ZERO, U256::ZERO); + assert_eq!(block.miner, twenty); + let notified = crate::subscribe::header_dto(&zero, B256::ZERO); + assert_eq!(notified["miner"], json!(twenty)); +} + fn setup_state() -> (RpcState, tempfile::TempDir) { setup_state_with(|store| store) } @@ -5086,6 +5114,57 @@ async fn hashrate_is_zero_without_a_miner_and_counts_only_our_blocks_with_one() assert_eq!(none.result.unwrap(), json!("0x0")); } +/// rskj's ownership test is `RskAddress.equals`: a block whose coinbase is +/// empty was mined by the null address, which is no miner's, not even that of +/// a node whose coinbase is the zero address. +#[tokio::test] +async fn hashrate_counts_no_block_with_an_empty_coinbase_as_ours() { + use alloy_primitives::Address; + + let tmp = tempfile::tempdir().unwrap(); + let store = Arc::new(BlockStore::open(tmp.path()).unwrap()); + let now = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_secs(); + + // Two blocks in the window, both of the zero address as the typed field + // reads it: the first's coinbase is empty, the second's twenty zeros. + let mut parent = B256::ZERO; + for number in 0u64..2 { + let header = Header { + parent_hash: parent, + timestamp: now - 60 + number, + difficulty: U256::from(3_600_000u64), + beneficiary: Address::ZERO, + kept: (number == 0).then(|| { + Box::new(rustock_core::types::header::KeptFields { + null_coinbase: true, + ..Default::default() + }) + }), + ..test_header(number) + }; + store.put_header(&header).unwrap(); + store.put_canonical_hash(number, header.hash()).unwrap(); + store + .put_total_difficulty(header.hash(), U256::from(1u64)) + .unwrap(); + store.put_body(header.hash(), &[], &[]).unwrap(); + store.set_head(header.hash()).unwrap(); + parent = header.hash(); + } + + // The network figure counts both; the zero address mined only the second. + let net = crate::eth::eth_net_hashrate(json!(1), &store); + assert_eq!(net.result.unwrap().as_u64().unwrap(), 7_200_000 / 3600); + let mine = crate::eth::eth_hashrate_for(json!(1), &store, Some(Address::ZERO)); + assert_eq!( + mine.result.unwrap(), + json!(format!("0x{:x}", 3_600_000u64 / 3600)) + ); +} + /// Administrative methods are reported as unknown rather than forbidden, so a /// node without them looks the same as one that never had them. #[tokio::test] diff --git a/crates/trie/src/key_mapper.rs b/crates/trie/src/key_mapper.rs index 5cd3f6a..0641005 100644 --- a/crates/trie/src/key_mapper.rs +++ b/crates/trie/src/key_mapper.rs @@ -98,7 +98,9 @@ pub fn storage_key_from_bytes(addr_bytes: &[u8], slot_bytes: &[u8]) -> Vec { #[derive(Debug, Clone, PartialEq, Eq)] pub enum TrieKeyKind { /// `0x00 || keccak(addr)[0:10] || addr`. The address is raw bytes because - /// RSK has short addresses: REMASC's sender is the single byte `0x00`. + /// RSK has short addresses: REMASC's sender is the single byte `0x00`, + /// and the null address, which REMASC pays where a block's coinbase is + /// empty, has none. Account { address: Vec }, /// `account_key || 0x80`. Code { address: Vec }, @@ -123,13 +125,17 @@ const MAX_ADDR_LEN: usize = 20; /// 13 bytes, which no length rule distinguishes from some other key's prefix. /// Verifying the digest makes the decode exact. fn recover_address(key: &[u8]) -> Option<&[u8]> { - if key.first() != Some(&DOMAIN_PREFIX) || key.len() < 1 + SECURE_KEY_SIZE + 1 { + if key.first() != Some(&DOMAIN_PREFIX) || key.len() < 1 + SECURE_KEY_SIZE { return None; } let body = &key[1 + SECURE_KEY_SIZE..]; let claimed = &key[1..1 + SECURE_KEY_SIZE]; let max = MAX_ADDR_LEN.min(body.len()); - (1..=max).rev().find(|&len| secure_prefix(&body[..len]) == claimed).map(|len| &body[..len]) + // Down to no bytes, the null address's. + (0..=max) + .rev() + .find(|&len| secure_prefix(&body[..len]) == claimed) + .map(|len| &body[..len]) } /// Recovers the full 32-byte slot from the stripped tail. Storage slots are @@ -331,6 +337,23 @@ mod classify_tests { } } + #[test] + fn the_null_addresss_account_key_names_no_address_bytes() { + // rskj's null address, which REMASC pays where a block's coinbase is + // empty, has no bytes: its account key is the domain byte and the + // secure prefix of nothing. + let account = account_key_from_bytes(&[]); + assert_eq!(account.len(), 11); + assert_eq!( + classify_key(&account), + TrieKeyKind::Account { address: vec![] } + ); + // The prefix is checked, as for any address. + let mut forged = account.clone(); + forged[1] ^= 0xff; + assert_eq!(classify_key(&forged), TrieKeyKind::Unknown); + } + #[test] fn keys_this_mapper_never_builds_are_reported_unknown_not_guessed() { // Wrong domain byte. From dafac67943515e9ca2344335403c0ca4f90db894 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Mon, 9 Nov 2026 10:45:44 -0800 Subject: [PATCH 056/264] core, execution, cli: headers laid out by rskj's heights, and a miner that builds them rskj reads a header's UMM root, version, edges and base event by height (BlockFactory.canBeDecoded) and builds them by height (BlockHeaderBuilder, BlockExecutor.fill). rustock held a header to papyrus200 for its UMM root, had no RSKIP535 height at all, and its miner wrote a version only for a partitioned block, so its own regtest blocks were version 0 under RSKIP351. The heights. ActivationHeights gains rskipUMM, rskip535 and reed810, and BlockFactory's testnet recovery bound (#7,800,000), each as rskj's reference.conf, testnet.conf and regtest.conf set them: rskj's regtest has rskipUMM, rskip144, rskip351 and rskip535 at 1. Moving papyrus200 moves rskipUMM, and moving reed810 moves rskip144 and rskip351, where the chain keeps them at the upgrade, as reference.conf maps them; regtest pins its own and keeps them. --consensus-rule takes rskip144 and rskipUMM too, and rskip144, rskip351 and rskip535 move the header's heights and the ladder together. HeaderLayoutRule holds the UMM root to RSKIPUMM on every chain; regtest is no longer exempt. The miner. The UMM root from RSKIPUMM; the version from ActivationConfig.getHeaderVersion (2 with RSKIP535, 1 with RSKIP351 alone); the edges, empty for a block that runs as one list, where RSKIP351 and RSKIP144 are both active; and a version-2 header's base event from the Union Bridge's storage once the block has run, so a base event the block itself sets is the one it carries. rskj's own miner reads that base event from its trie store at the new state root, which executeAndFill never saves: it logs "Failed to set baseEvent in block header" and leaves the field empty, unless the block left the state as it was, as the block in its own test does. This reads the state the block built, as fill means to. No rule checks the field, so either way the block is valid; testnet's Union Bridge holds no base event today, so both write it empty there. rskj's regtest blocks in the import fixture were mined while its regtest.conf had rskip144 = -1 (a base event and no edges), so the import now says so with --consensus-rule rskip144=-1. They carry a UMM root from block 1, which the layout rule now requires there, and still import and export byte for byte. --- crates/cli/src/main.rs | 51 ++++- crates/cli/tests/import_blocks.rs | 6 + crates/core/src/config.rs | 245 +++++++++++++++++++-- crates/core/src/types/header.rs | 18 +- crates/core/src/validation/header_rules.rs | 15 +- crates/core/src/validation/tests.rs | 31 ++- crates/execution/src/hardfork.rs | 47 ++-- crates/execution/src/mining/template.rs | 67 ++++-- crates/execution/src/mining/tests.rs | 237 ++++++++++++++++++-- crates/execution/src/mining/workload.rs | 7 + crates/execution/src/processor.rs | 4 +- 11 files changed, 639 insertions(+), 89 deletions(-) diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index af929de..b425123 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -3897,12 +3897,20 @@ fn apply_chain_options(mut config: ChainConfig, args: &Args) -> Result config.activation_heights.rskip97 = height, - // The ladder reads it from the overrides, and the header layout - // rule from the activation heights: both must move. - "rskip351" => { - config.activation_heights.rskip351 = height; + // The ladder reads these from the overrides, and the header's + // layout from the activation heights: both must move. + "rskip144" | "rskip351" | "rskip535" => { + let heights = &mut config.activation_heights; + let field = match name.as_str() { + "rskip144" => &mut heights.rskip144, + "rskip351" => &mut heights.rskip351, + _ => &mut heights.rskip535, + }; + *field = height; config.consensus_rule_overrides.push((name, height)); } + // The header's layout alone reads it. + "rskipUMM" => config.activation_heights.rskip_umm = height, _ => config.consensus_rule_overrides.push((name, height)), } } @@ -5354,7 +5362,35 @@ mod genesis_tests { let config = apply_chain_options(ChainConfig::regtest(), &disabled).unwrap(); assert_eq!(config.activation_heights.rskip97, None); - let unknown = regtest_args(&["--consensus-rule", "rskip144=0"]); + // RSKIP-144 moves apart from reed810, for the header's layout and + // the ladder both, as an rskj node replaying older regtest blocks + // would set it. + let moved = regtest_args(&["--consensus-rule", "rskip144=-1"]); + let config = apply_chain_options(ChainConfig::regtest(), &moved).unwrap(); + assert_eq!(config.activation_heights.rskip144, None); + let (ladder, _) = hardfork_config(&config).unwrap(); + assert!(!ladder.has_rskip144(1)); + let moved = regtest_args(&["--consensus-rule", "rskip144=5"]); + let config = apply_chain_options(ChainConfig::regtest(), &moved).unwrap(); + assert_eq!(config.activation_heights.rskip144, Some(5)); + let (ladder, _) = hardfork_config(&config).unwrap(); + assert!(!ladder.has_rskip144(4) && ladder.has_rskip144(5)); + // Only the header's layout reads RSKIPUMM. + let umm = regtest_args(&["--consensus-rule", "rskipUMM=5"]); + let config = apply_chain_options(ChainConfig::regtest(), &umm).unwrap(); + assert_eq!(config.activation_heights.rskip_umm, Some(5)); + + // Moving reed810 on a copy of mainnet moves RSKIP144 and RSKIP351 + // with it, in the header's heights as on the ladder. + let reed810 = mainnet_args(&["--allow-chain-overrides", "--hardfork", "reed810=100"]); + let config = apply_chain_options(ChainConfig::mainnet(), &reed810).unwrap(); + let heights = &config.activation_heights; + assert_eq!((heights.rskip144, heights.rskip351), (Some(100), Some(100))); + let (ladder, _) = hardfork_config(&config).unwrap(); + assert!(ladder.runs_sublists(100) && !ladder.runs_sublists(99)); + + // A rule this node does not schedule on its own is refused. + let unknown = regtest_args(&["--consensus-rule", "rskip126=0"]); assert!(apply_chain_options(ChainConfig::regtest(), &unknown).is_err()); let mainnet = Args::parse_from(["rustock-cli", "--skip-pow-validation"]); @@ -5421,8 +5457,11 @@ mod genesis_tests { let config = apply_chain_options(ChainConfig::mainnet(), &mainnet_args(SEED_FLAGS)).unwrap(); assert_eq!(config.chain_id, 30); assert_eq!(config.activation_heights.rskip144, Some(9_002_504)); - // The header layout rule reads RSKIP351 here, the ladder below. + // The header's layout reads RSKIP351 and RSKIP535 here, the ladder + // below; RSKIPUMM only here. assert_eq!(config.activation_heights.rskip351, Some(9_002_504)); + assert_eq!(config.activation_heights.rskip535, Some(9_002_504)); + assert_eq!(config.activation_heights.rskip_umm, Some(2_392_700)); assert_eq!(config.activation_heights.orchid, 729_000); assert_eq!(config.activation_heights.rskip97, Some(729_000)); assert_eq!(config.activation_heights.papyrus200, 2_392_700); diff --git a/crates/cli/tests/import_blocks.rs b/crates/cli/tests/import_blocks.rs index 77cfc9c..195a18e 100644 --- a/crates/cli/tests/import_blocks.rs +++ b/crates/cli/tests/import_blocks.rs @@ -5,6 +5,11 @@ //! `ExportBlocks` writes them: version-2 headers, twenty contract calls a //! block, a flush at block 10 and REMASC's first payout at block 15, which //! pays rskj's regtest federation. +//! +//! That regtest ran RSKIP-144 at -1, as rskj's `regtest.conf` then had it: +//! its headers carry a base event and no edges. rskj's `regtest.conf` now +//! has `rskip144 = 1`, so the import names the rule the blocks were mined +//! under, as an rskj node replaying them would have to. use std::path::Path; use std::process::Command; @@ -27,6 +32,7 @@ fn import_command(blocks: &Path, dir: &Path) -> Command { .arg("--genesis") .arg(Path::new(DATA).join("rskj-regtest-genesis.json")) .args(["--skip-pow-validation", "--consensus-rule", "rskip97=0"]) + .args(["--consensus-rule", "rskip144=-1"]) .args(["--min-gas-limit", "3000000"]) .arg("--import-blocks") .arg(blocks); diff --git a/crates/core/src/config.rs b/crates/core/src/config.rs index 9202aa8..5e826a4 100644 --- a/crates/core/src/config.rs +++ b/crates/core/src/config.rs @@ -38,6 +38,11 @@ pub struct ActivationHeights { /// Bitcoin-to-RSK timestamp window on testnet (5 minutes to 2 hours). /// Read only on testnet; mainnet keeps 5 minutes at every height. pub hop400: u64, + /// Reed810 hardfork, where `reference.conf` puts RSKIP144 and RSKIP351. + /// Read here only so that moving it moves them where the chain keeps + /// them there ([`Self::set_rskj_hardfork`]); the execution ladder + /// schedules the upgrade itself. + pub reed810: u64, /// Height at which RSKIP97 -- "no 10-minute difficulty drop" -- activates, /// or `None` where it never does. /// @@ -54,11 +59,9 @@ pub struct ActivationHeights { /// Height at which RSKIP-144 (parallel transaction sublists) activates, /// or `None` where it never does. rskj gates it on `reed810`. /// - /// The header decoder needs it: after an RSKIP-351 version byte, one - /// trailing item is the sublist edges where RSKIP-144 is active and the - /// V2 base event where it is not (rskj `BlockFactory` decides by the same - /// activation). rskj's own regtest is the second case: `rskip144 = -1` - /// with `rskip535 = 1`. + /// The header's layout needs it: from here on, where RSKIP-351 is active + /// too, a header carries the sublist edges, empty for a block that runs + /// as one list (rskj `BlockFactory` and `BlockHeaderBuilder`). pub rskip144: Option, /// Height at which RSKIP351 -- versioned headers, the version and its /// extension where a header had only a logs bloom -- activates, or @@ -72,6 +75,23 @@ pub struct ActivationHeights { /// alone (`Constants.getMinimumDifficulty`), so on the other chains it /// is recorded but changes nothing: see [`ChainConfig::min_difficulty_at`]. pub rskip290: Option, + /// Height at which RSKIPUMM -- a UMM root after the uncle count -- + /// activates, or `None` where it never does. `reference.conf` sets it at + /// papyrus200, which mainnet and testnet keep, and rskj's regtest at + /// block 1. rskj's `BlockFactory` reads the field by this height, and its + /// miner writes it, empty, from here on (`BlockHeaderBuilder`). + pub rskip_umm: Option, + /// Height at which RSKIP535 -- a versioned header's base event -- + /// activates, or `None` where it never does. `reference.conf` leaves it + /// at -1, which mainnet keeps; testnet sets it at vetiver900 (#7,604,200) + /// and regtest at 1. With RSKIP351 it makes a header version 2 + /// (`ActivationConfig.getHeaderVersion`). + pub rskip535: Option, + /// Below this height rskj takes a full header one field short of its + /// layout where the base event is enabled, and gives it an empty base + /// event (`BlockFactory.isInBaseEventRecoveryWindow`): on testnet alone, + /// below `BASE_EVENT_RECOVERY_UPPER_BOUND`, #7,800,000. `None` elsewhere. + pub base_event_recovery_below: Option, } /// Process-wide activation heights, consulted by block-hash computation @@ -93,22 +113,80 @@ impl ActivationHeights { /// Set the height this struct carries for an rskj upgrade name, for the /// names it carries: `orchid`, `wasabi100`, `papyrus200`, `iris300`, - /// `fingerroot500`, and `reed810`, which is RSKIP-144's height. `None` - /// is rskj's `-1`. Any other name is the execution ladder's to judge and - /// is left alone here. + /// `fingerroot500` and `reed810`. `None` is rskj's `-1`. Any other name is + /// the execution ladder's to judge and is left alone here. + /// + /// The rules `reference.conf` puts at an upgrade move with it where the + /// chain keeps them there: RSKIPUMM with papyrus200, RSKIP144 and + /// RSKIP351 with reed810. A chain that sets its own, as rskj's regtest + /// does, keeps them. pub fn set_rskj_hardfork(&mut self, name: &str, height: Option) { let never = u64::MAX; + let at = |upgrade: u64| (upgrade != never).then_some(upgrade); match name { "orchid" => self.orchid = height.unwrap_or(never), "wasabi100" => self.wasabi100 = height.unwrap_or(never), - "papyrus200" => self.papyrus200 = height.unwrap_or(never), + "papyrus200" => { + if self.rskip_umm == at(self.papyrus200) { + self.rskip_umm = height; + } + self.papyrus200 = height.unwrap_or(never); + } "iris300" => self.iris300 = height.unwrap_or(never), "fingerroot500" => self.fingerroot500 = height.unwrap_or(never), - "reed810" => self.rskip144 = height, + "reed810" => { + let followed = at(self.reed810); + for rule in [&mut self.rskip144, &mut self.rskip351] { + if *rule == followed { + *rule = height; + } + } + self.reed810 = height.unwrap_or(never); + } _ => {} } } + /// Whether `number` is at or past `height`, rskj's `isActive`. + fn reached(height: Option, number: u64) -> bool { + height.is_some_and(|height| number >= height) + } + + /// Whether a header at `number` carries a UMM root (RSKIPUMM). + pub fn umm_active(&self, number: u64) -> bool { + Self::reached(self.rskip_umm, number) + } + + /// Whether RSKIP-144's sublist edges are a header field at `number`: + /// rskj reads and writes them where RSKIP351 and RSKIP144 are both + /// active. + pub fn edges_active(&self, number: u64) -> bool { + Self::reached(self.rskip351, number) && Self::reached(self.rskip144, number) + } + + /// Whether a header at `number` is versioned (RSKIP351). + pub fn rskip351_active(&self, number: u64) -> bool { + Self::reached(self.rskip351, number) + } + + /// rskj `BlockFactory.isBaseEventEnabled`: RSKIP535 and RSKIP351 both. + pub fn base_event_enabled(&self, number: u64) -> bool { + Self::reached(self.rskip535, number) && Self::reached(self.rskip351, number) + } + + /// rskj `ActivationConfig.getHeaderVersion`: 2 with RSKIP535, 1 without + /// it, and 0 before RSKIP351. + pub fn header_version(&self, number: u64) -> u8 { + match ( + self.rskip351_active(number), + Self::reached(self.rskip535, number), + ) { + (false, _) => 0, + (true, false) => 1, + (true, true) => 2, + } + } + pub fn mainnet() -> Self { Self { orchid: 729_000, @@ -117,14 +195,20 @@ impl ActivationHeights { iris300: 3_614_800, fingerroot500: 5_468_000, hop400: 4_598_500, + // main.conf: `reed810 = -1`. + reed810: u64::MAX, // reference.conf: `rskip97 = orchid`, not overridden by main.conf. rskip97: Some(729_000), - // main.conf: reed810 = -1. + // reference.conf: `rskip144 = reed810` and `rskip351 = reed810`. rskip144: None, - // main.conf: `reed810 = -1`. rskip351: None, // reference.conf: `rskip290 = hop400`. Mainnet's floor stays 7e15. rskip290: Some(4_598_500), + // reference.conf: `rskipUMM = papyrus200`. + rskip_umm: Some(2_392_700), + // reference.conf: `rskip535 = -1`, which main.conf keeps. + rskip535: None, + base_event_recovery_below: None, } } @@ -138,15 +222,21 @@ impl ActivationHeights { iris300: 2_060_500, fingerroot500: 4_015_800, hop400: 3_103_000, + // testnet.conf: `reed810 = 7139600`. + reed810: 7_139_600, // testnet.conf: `rskip97 = -1 # disable orchid difficulty drop`. rskip97: None, - - // testnet.conf: reed810. + // reference.conf: `rskip144 = reed810` and `rskip351 = reed810`. rskip144: Some(7_139_600), - // testnet.conf: `reed810 = 7139600`. rskip351: Some(7_139_600), // testnet.conf: `rskip290 = 2581800`, not hop400. rskip290: Some(2_581_800), + // reference.conf: `rskipUMM = papyrus200`. + rskip_umm: Some(863_000), + // testnet.conf: `rskip535 = 7604200`, vetiver900's height. + rskip535: Some(7_604_200), + // BlockFactory.BASE_EVENT_RECOVERY_UPPER_BOUND, testnet's alone. + base_event_recovery_below: Some(7_800_000), } } @@ -154,19 +244,33 @@ impl ActivationHeights { Self { orchid: 0, wasabi100: 0, + // regtest.conf puts papyrus200 at 0, but nothing read here from it + // applies on regtest: RSKIP156's divisor skips it + // (`Constants.getDifficultyBoundDivisor`), and its UMM root has + // its own height below. papyrus200: u64::MAX, iris300: 0, fingerroot500: 0, hop400: 0, + // regtest.conf: `reed810 = 0`, though rustock's regtest ladder + // does not schedule it yet; RSKIP144 and RSKIP351 have their + // own heights below, so they do not move with it. + reed810: 0, // regtest.conf: `rskip97 = -1`, same as testnet. rskip97: None, - // regtest.conf: `rskip144 = -1`; rustock's regtest ladder does - // not schedule reed810 either. - rskip144: None, + // regtest.conf: `rskip144 = 1`. rustock's regtest ladder does not + // run blocks as their sublists yet, so its blocks run as one + // list and carry empty edges. + rskip144: Some(1), // regtest.conf: `rskip351 = 1`. rskip351: Some(1), // reference.conf's hop400, which regtest.conf puts at genesis. rskip290: Some(0), + // regtest.conf: `rskipUMM = 1`, not papyrus200. + rskip_umm: Some(1), + // regtest.conf: `rskip535 = 1`. + rskip535: Some(1), + base_event_recovery_below: None, } } } @@ -668,6 +772,111 @@ mod tests { assert_eq!(heights.papyrus200, u64::MAX); } + /// The heights a header's layout is read by, as rskj's `reference.conf`, + /// `testnet.conf` and `regtest.conf` set them, and `BlockFactory`'s + /// testnet recovery bound. + #[test] + fn header_layout_heights_are_rskjs() { + let layout = |h: ActivationHeights| { + ( + h.rskip_umm, + h.rskip144, + h.rskip351, + h.rskip535, + h.base_event_recovery_below, + ) + }; + assert_eq!( + layout(ActivationHeights::mainnet()), + (Some(2_392_700), None, None, None, None) + ); + assert_eq!( + layout(ActivationHeights::testnet()), + ( + Some(863_000), + Some(7_139_600), + Some(7_139_600), + Some(7_604_200), + Some(7_800_000) + ) + ); + assert_eq!( + layout(ActivationHeights::regtest()), + (Some(1), Some(1), Some(1), Some(1), None) + ); + } + + /// rskj `ActivationConfig.getHeaderVersion` and the fields `BlockFactory` + /// reads by height, on testnet's heights: the UMM root from papyrus200, + /// version 1 with edges from reed810, version 2 with a base event from + /// RSKIP535. + #[test] + fn a_header_is_laid_out_by_its_height() { + let t = ActivationHeights::testnet(); + assert!(!t.umm_active(862_999)); + assert!(t.umm_active(863_000)); + for (number, version, edges, base_event) in [ + (7_139_599, 0, false, false), + (7_139_600, 1, true, false), + (7_604_199, 1, true, false), + (7_604_200, 2, true, true), + ] { + assert_eq!(t.header_version(number), version, "#{number}"); + assert_eq!(t.rskip351_active(number), version > 0, "#{number}"); + assert_eq!(t.edges_active(number), edges, "#{number}"); + assert_eq!(t.base_event_enabled(number), base_event, "#{number}"); + } + // RSKIP535 without RSKIP351 is no version and no base event; RSKIP351 + // without RSKIP144, no edges. + let alone = ActivationHeights { + rskip351: None, + ..ActivationHeights::regtest() + }; + assert_eq!(alone.header_version(5), 0); + assert!(!alone.base_event_enabled(5)); + let no_144 = ActivationHeights { + rskip144: None, + ..ActivationHeights::regtest() + }; + assert_eq!(no_144.header_version(5), 2); + assert!(!no_144.edges_active(5)); + } + + /// `reference.conf` has `rskipUMM = papyrus200`, and `rskip144` and + /// `rskip351` at reed810: moving the upgrade moves them where the chain + /// keeps them there, and not on regtest, which sets its own. + #[test] + fn rules_follow_their_upgrade_where_the_chain_has_them_follow() { + let mut mainnet = ActivationHeights::mainnet(); + mainnet.set_rskj_hardfork("papyrus200", Some(100)); + assert_eq!((mainnet.papyrus200, mainnet.rskip_umm), (100, Some(100))); + mainnet.set_rskj_hardfork("papyrus200", None); + assert_eq!((mainnet.papyrus200, mainnet.rskip_umm), (u64::MAX, None)); + mainnet.set_rskj_hardfork("papyrus200", Some(7)); + assert_eq!(mainnet.rskip_umm, Some(7)); + + mainnet.set_rskj_hardfork("reed810", Some(9_002_504)); + let reed = |h: &ActivationHeights| (h.reed810, h.rskip144, h.rskip351); + assert_eq!( + reed(&mainnet), + (9_002_504, Some(9_002_504), Some(9_002_504)) + ); + mainnet.set_rskj_hardfork("reed810", None); + assert_eq!(reed(&mainnet), (u64::MAX, None, None)); + let mut testnet = ActivationHeights::testnet(); + testnet.set_rskj_hardfork("reed810", Some(8_000_000)); + assert_eq!( + reed(&testnet), + (8_000_000, Some(8_000_000), Some(8_000_000)) + ); + + let mut regtest = ActivationHeights::regtest(); + regtest.set_rskj_hardfork("papyrus200", Some(0)); + assert_eq!((regtest.papyrus200, regtest.rskip_umm), (0, Some(1))); + regtest.set_rskj_hardfork("reed810", Some(5)); + assert_eq!(reed(®test), (5, Some(1), Some(1))); + } + #[test] fn test_mainnet_chain_config() { let config = ChainConfig::mainnet(); diff --git a/crates/core/src/types/header.rs b/crates/core/src/types/header.rs index 91b0095..9d6c9f5 100644 --- a/crates/core/src/types/header.rs +++ b/crates/core/src/types/header.rs @@ -732,8 +732,8 @@ impl Header { // for it and hashes the header wrong. // // Where RSKIP-144 is not active there are no edges, and a V2 - // header's one trailing item is its base event (rskj's regtest: - // rskip144 = -1, rskip535 = 1). rskj tells the cases apart by + // header's one trailing item is its base event (rskj's regtest + // once had rskip144 = -1, rskip535 = 1). rskj tells the cases apart by // activation, and so does this. Both are read raw // (`getRLPRawData`). let mut edges = None; @@ -2576,10 +2576,11 @@ mod versioned_wire_header_tests { "d50308ca8a1200741eb600000000", ); - /// Block 1 of a chain rskj's regtest mined (regtest.conf: rskip144 = -1, - /// rskip351 = 1, rskip535 = 1), in rskj's full encoding as `ExportBlocks` - /// writes it. Its one trailing item after the version byte is an empty - /// base event, not edges: there are no edges without RSKIP-144. + /// Block 1 of a chain rskj's regtest mined while its `regtest.conf` had + /// `rskip144 = -1` (with rskip351 = 1 and rskip535 = 1; it has rskip144 = 1 + /// now), in rskj's full encoding as `ExportBlocks` writes it. Its one + /// trailing item after the version byte is an empty base event, not + /// edges: there are no edges without RSKIP-144. const RSKJ_REGTEST_V2_FULL: &str = concat!( "f902b1a0227d8e8d835f5e7290e333f4177e551ef1222dde4704f677ba4a120051c31cc2a01dcc4de8dec75d7aab85b5", "67b6ccd41ad312451b948a7413f0a142fd40d4934794916d3652976d82540412b561bf674f72cf5174f7a095f20004ac", @@ -2601,7 +2602,10 @@ mod versioned_wire_header_tests { #[test] fn a_v2_header_without_rskip144_carries_a_base_event_not_edges() { let bytes = alloy_primitives::hex::decode(RSKJ_REGTEST_V2_FULL).unwrap(); - let regtest = crate::config::ActivationHeights::regtest(); + let regtest = crate::config::ActivationHeights { + rskip144: None, + ..crate::config::ActivationHeights::regtest() + }; let h = Header::decode_with_activations(&mut bytes.as_slice(), ®test).unwrap(); let v = h.version_fields.as_ref().unwrap(); assert_eq!(v.version, 2); diff --git a/crates/core/src/validation/header_rules.rs b/crates/core/src/validation/header_rules.rs index 6669b51..747e566 100644 --- a/crates/core/src/validation/header_rules.rs +++ b/crates/core/src/validation/header_rules.rs @@ -158,11 +158,8 @@ impl HeaderValidator for GasLimitRule { /// The decoder here reads by shape rather than by height, so this holds what /// it read to what rskj would have. /// -/// - A UMM root from RSKIPUMM on, and none before it. rskj's `reference.conf` -/// puts RSKIPUMM at papyrus200, which mainnet and testnet keep. A chain -/// with no papyrus200, rustock's regtest, is not held to either: rskj's -/// regtest has `rskipUMM = 1`, and both regtests are read here, rustock's -/// own and one imported from rskj. +/// - A UMM root from RSKIPUMM on, and none before it: papyrus200 on mainnet +/// and testnet, as `reference.conf` has it, and 1 on rskj's regtest. /// - No RSKIP351 fields where RSKIP351 is inactive, as it is on mainnet: no /// version, edges or base event after the UMM root, and no commitment to /// an extension in the logs bloom's place. The decoder reads whatever is @@ -177,16 +174,14 @@ pub struct HeaderLayoutRule { impl HeaderValidator for HeaderLayoutRule { fn validate(&self, header: &Header) -> Result<(), ValidationError> { let heights = &self.config.activation_heights; - let umm = header.number >= heights.papyrus200; - if heights.papyrus200 != u64::MAX && header.umm_root.is_some() != umm { + let umm = heights.umm_active(header.number); + if header.umm_root.is_some() != umm { return Err(ValidationError::UmmRootMisplaced { number: header.number, present: header.umm_root.is_some(), }); } - let rskip351 = heights - .rskip351 - .is_some_and(|height| header.number >= height); + let rskip351 = heights.rskip351_active(header.number); if !rskip351 && (header.version_fields.is_some() || header.extension_data.is_some()) { return Err(ValidationError::VersionedBeforeRskip351 { number: header.number, diff --git a/crates/core/src/validation/tests.rs b/crates/core/src/validation/tests.rs index abb8e49..37b4da9 100644 --- a/crates/core/src/validation/tests.rs +++ b/crates/core/src/validation/tests.rs @@ -1601,10 +1601,13 @@ impl uncles::AncestorSource for Ancestry { } /// Regtest's constants with rskj's timestamp rule switched on, so that every -/// rule an uncle faces runs. +/// rule an uncle faces runs, and without RSKIPUMM, so these headers need no +/// UMM root: `a_header_carries_the_fields_rskj_reads_at_its_height` holds +/// regtest's to it. fn uncle_config() -> Arc { let mut config = crate::config::ChainConfig::regtest(); config.max_future_block_time = 60; + config.activation_heights.rskip_umm = None; Arc::new(config) } @@ -1809,7 +1812,8 @@ fn default_rsk_holds_uncles_to_rskjs_uncle_rules() { } /// rskj reads a header's fields by its height (`BlockFactory.canBeDecoded`): -/// a UMM root from RSKIPUMM, papyrus200, on and only then, and no RSKIP351 +/// a UMM root from RSKIPUMM on and only then -- papyrus200 on mainnet and +/// testnet, 1 on regtest -- and no RSKIP351 /// fields where RSKIP351 is inactive, which is all of mainnet. The decoder /// reads by shape, so the verifier holds what it read to that, for a block /// and for an uncle alike, before anything else. @@ -1832,6 +1836,29 @@ fn a_header_carries_the_fields_rskj_reads_at_its_height() { assert_eq!(rule.validate(&at(papyrus - 1, true)), misplaced(papyrus - 1, true)); assert_eq!(rule.validate(&at(papyrus, true)), Ok(())); assert_eq!(rule.validate(&at(papyrus, false)), misplaced(papyrus, false)); + // RSKIPUMM's own height, not papyrus200's, where the two part. + let apart = HeaderLayoutRule { + config: Arc::new(ChainConfig { + activation_heights: crate::config::ActivationHeights { + rskip_umm: Some(papyrus + 10), + ..mainnet.activation_heights.clone() + }, + ..(*mainnet).clone() + }), + }; + assert_eq!(apart.validate(&at(papyrus, false)), Ok(())); + assert_eq!( + apart.validate(&at(papyrus + 10, false)), + misplaced(papyrus + 10, false) + ); + // rskj's regtest: from block 1, as `regtest.conf` has it. + let regtest = HeaderLayoutRule { + config: Arc::new(ChainConfig::regtest()), + }; + assert_eq!(regtest.validate(&at(0, false)), Ok(())); + assert_eq!(regtest.validate(&at(0, true)), misplaced(0, true)); + assert_eq!(regtest.validate(&at(1, true)), Ok(())); + assert_eq!(regtest.validate(&at(1, false)), misplaced(1, false)); let versioned = |mut header: Header| { header.version_fields = diff --git a/crates/execution/src/hardfork.rs b/crates/execution/src/hardfork.rs index 2c987c4..212f87c 100644 --- a/crates/execution/src/hardfork.rs +++ b/crates/execution/src/hardfork.rs @@ -86,6 +86,7 @@ pub const TESTNET_ACTIVATIONS: &[(u64, RskNetworkUpgrade)] = &[ /// by name are here; every other rule follows its upgrade. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum ConsensusRule { + Rskip144, Rskip351, Rskip535, Rskip536, @@ -95,6 +96,7 @@ impl ConsensusRule { /// The rule rskj calls `name` in `consensusRules`. pub fn from_rskj_name(name: &str) -> Option { Some(match name { + "rskip144" => Self::Rskip144, "rskip351" => Self::Rskip351, "rskip535" => Self::Rskip535, "rskip536" => Self::Rskip536, @@ -294,10 +296,10 @@ impl RskHardforkConfig { /// - `orchid060` and `tbd1000`: nothing here reads them. `tbd1000` only /// turns on RSKIP-559 (deterministic pegout sorting in the Bridge), /// which this node does not implement. Accepted and reported as such. - /// - `rskip351`, `rskip535`, `rskip536`: scheduled apart from their - /// upgrade. - /// - `rskipUMM`: follows papyrus200 here, so it is accepted at that height - /// and refused at any other. + /// - `rskip144`, `rskip351`, `rskip535`, `rskip536`: scheduled apart from + /// their upgrade. + /// - `rskipUMM`: accepted at any height. Only a header's layout reads it, + /// from the chain's activation heights, which the caller moves. /// /// Returns the names that were accepted without effect, for the log. pub fn with_rskj_overrides( @@ -326,17 +328,11 @@ impl RskHardforkConfig { if let Some(rule) = ConsensusRule::from_rskj_name(name) { self = self.with_consensus_rule(rule, *height); } else if name == "rskipUMM" { - let papyrus200 = self.activation_height(RskNetworkUpgrade::Papyrus200); - if *height != papyrus200 { - return Err(format!( - "consensus rule rskipUMM: this node reads it at papyrus200 \ - ({papyrus200:?}) and cannot move it" - )); - } + // Nothing on the ladder reads it. } else { return Err(format!( "consensus rule {name}: not one this node schedules on its own \ - (rskip351, rskip535, rskip536, rskipUMM)" + (rskip144, rskip351, rskip535, rskip536, rskipUMM)" )); } } @@ -823,7 +819,13 @@ impl RskHardforkConfig { /// inert only because nothing read this predicate yet and mainnet headers /// carry no sublist edges. The first thing to depend on it would have /// forked the node. + /// + /// A `consensusRules` entry moves it off reed810, as rskj's + /// `regtest.conf` does (`rskip144 = 1`). pub fn has_rskip144(&self, block_number: u64) -> bool { + if let Some(height) = self.rule_override(ConsensusRule::Rskip144) { + return height.is_some_and(|h| block_number >= h); + } self.activation_height(RskNetworkUpgrade::Reed810) .is_some_and(|h| block_number >= h) } @@ -1408,9 +1410,13 @@ mod override_tests { let moved = RskHardforkConfig::mainnet() .with_rskj_overrides(&[h("hop401", 5_000_000)], &[]); assert!(moved.is_err()); - let umm = RskHardforkConfig::mainnet() - .with_rskj_overrides(&[], &[h("rskipUMM", 1)]); - assert!(umm.is_err()); + // Only a header's layout reads RSKIPUMM, from the chain's heights: + // the ladder takes it at any height and changes nothing. + let (umm, unmodelled) = RskHardforkConfig::mainnet() + .with_rskj_overrides(&[], &[h("rskipUMM", 1)]) + .unwrap(); + assert_eq!(umm.activations, RskHardforkConfig::mainnet().activations); + assert_eq!((umm.rule_overrides, unmodelled), (Vec::new(), Vec::::new())); let unknown = RskHardforkConfig::mainnet() .with_rskj_overrides(&[h("quartz", 1)], &[]); assert!(unknown.is_err()); @@ -1443,6 +1449,17 @@ mod override_tests { .unwrap(); assert!(moved.has_rskip144(8_000_000)); assert!(!moved.runs_sublists(8_000_000)); + // rskip144 moves apart from reed810 too, as rskj's regtest has it. + let (moved, _) = RskHardforkConfig::testnet() + .with_rskj_overrides(&[], &[h("rskip144", 7_500_000)]) + .unwrap(); + assert!(moved.has_rskip351(7_139_600) && !moved.has_rskip144(7_499_999)); + assert!(!moved.runs_sublists(7_499_999)); + assert!(moved.runs_sublists(7_500_000)); + let (off, _) = RskHardforkConfig::testnet() + .with_rskj_overrides(&[], &[h("rskip144", -1)]) + .unwrap(); + assert!(!off.has_rskip144(8_000_000) && !off.runs_sublists(8_000_000)); } } diff --git a/crates/execution/src/mining/template.rs b/crates/execution/src/mining/template.rs index 5b03bd0..136badb 100644 --- a/crates/execution/src/mining/template.rs +++ b/crates/execution/src/mining/template.rs @@ -247,15 +247,13 @@ impl BlockTemplateBuilder { transactions.push(remasc_transaction(number)); let rskip126 = self.hardfork_cfg.has_unitrie_state_root(number); - // rskj sets ummRoot to the empty byte array from papyrus200 on (the - // UMM contracts were never deployed, so it is never non-empty). Its - // mere presence changes both the block hash and the merged-mining - // hash, so a header built without it is rejected by every peer. - let umm_root = if number >= self.chain_config.activation_heights.papyrus200 { - Some(Bytes::new()) - } else { - None - }; + // rskj `BlockHeaderBuilder` sets ummRoot to the empty byte array from + // RSKIPUMM on (the UMM contracts were never deployed, so it is never + // non-empty). Its mere presence changes both the block hash and the + // merged-mining hash, so a header built without it is rejected by + // every peer. + let heights = &self.chain_config.activation_heights; + let umm_root = heights.umm_active(number).then(Bytes::new); #[allow(unused_mut)] let mut header = Header { @@ -339,14 +337,24 @@ impl BlockTemplateBuilder { } else { None }; - if let Some(partitioned) = partitioned { - transactions = partitioned.transactions; - header.transactions_root = ordered_tx_trie_root(&transactions, rskip126); + let edges = match partitioned { + Some(partitioned) => { + transactions = partitioned.transactions; + header.transactions_root = ordered_tx_trie_root(&transactions, rskip126); + Some(partitioned.edges) + } + // rskj `BlockHeaderBuilder`: an empty field where RSKIP351 and + // RSKIP144 make the edges one, for a block that runs as one list. + None => heights.edges_active(number).then(Vec::new), + }; + // rskj `ActivationConfig.getHeaderVersion`: 2 with RSKIP535, 1 with + // RSKIP351 alone, and none before RSKIP351, where no block runs as its + // sublists either. + let version = heights.header_version(number); + if version > 0 { header.version_fields = Some(rustock_core::types::header::HeaderVersionFields { - // RSKIP-351 travels with RSKIP-144; the extension is what - // carries the edges. - version: 1, - edges: Some(partitioned.edges), + version, + edges, base_event: None, }); } @@ -360,6 +368,20 @@ impl BlockTemplateBuilder { header.logs_bloom = executed.logs_bloom; header.gas_used = executed.gas_used; header.paid_fees = executed.paid_fees; + // rskj `BlockExecutor.fill`: a version-2 header carries the base event + // the Union Bridge holds once the block has run, which a transaction + // in this very block may have set, and an empty one while it holds + // none. + if let Some(fields) = header + .version_fields + .as_mut() + .filter(|fields| fields.version >= 2) + { + fields.base_event = Some(stored_base_event( + &executed.new_state_root, + self.trie_store.as_ref(), + )); + } let hash_for_merged_mining = fork_detection::apply_to_hash(header.hash_for_merged_mining(), &fork_data); @@ -702,6 +724,19 @@ fn account_nonce(state_root: &TrieNode, store: &dyn TrieStore, addr: &Address) - } } +/// The base event the Union Bridge holds in `state` (RSKIP529), empty while +/// it holds none: rskj `UnionBridgeStorageProviderImpl.getBaseEvent`, which +/// takes the cell's bytes as they are. +fn stored_base_event(state: &TrieNode, store: &dyn TrieStore) -> Bytes { + use crate::bridge::union::{union_key, BASE_EVENT_KEY}; + let slot = B256::from(union_key(BASE_EVENT_KEY)); + let key = rustock_trie::storage_key(&crate::precompiles::BRIDGE_ADDR, &slot); + state + .get(&TrieKeySlice::from_key(&key), store) + .map(Bytes::from) + .unwrap_or_default() +} + /// The REMASC transaction for a block, byte-for-byte as rskj encodes it. /// /// `cached_rlp` is not an optimisation here. rskj writes the zero gas price diff --git a/crates/execution/src/mining/tests.rs b/crates/execution/src/mining/tests.rs index 1f5205f..0041f3d 100644 --- a/crates/execution/src/mining/tests.rs +++ b/crates/execution/src/mining/tests.rs @@ -24,7 +24,7 @@ use crate::processor::BlockProcessor; // ── fixtures ────────────────────────────────────────────────────────── /// Activation heights with merged mining live from block 0 and UMM never, so -/// tests can work at low block numbers. `papyrus200` is raised out of reach +/// tests can work at low block numbers. RSKIPUMM is left out of reach /// deliberately: a separate test covers the `ummRoot` a real chain carries. fn test_chain_config() -> Arc { Arc::new(ChainConfig { @@ -46,15 +46,20 @@ fn test_chain_config() -> Arc { iris300: 0, fingerroot500: 0, hop400: 0, + reed810: u64::MAX, // Active from genesis: this fixture mines blocks back to back, // so the ten-minute drop would only add noise. rskip97: Some(0), + // Neither, as `all_active`, the ladder `fixture_on` runs; the + // fixtures that run the sublists take theirs from their ladder + // (`with_heights_of`). rskip144: None, - // From genesis too: a partitioned template carries the version - // fields RSKIP351 brings. - rskip351: Some(0), + rskip351: None, // Testnet's floor alone, and this chain is not testnet. rskip290: None, + rskip_umm: None, + rskip535: None, + base_event_recovery_below: None, }, genesis_spec: None, skip_pow_validation: false, @@ -130,9 +135,9 @@ fn fixture(height: u64) -> MiningFixture { fixture_with(height, test_chain_config(), U256::from(2)) } -fn fixture_with_papyrus_at(papyrus200: u64, height: u64) -> MiningFixture { +fn fixture_with_umm_at(rskip_umm: u64, height: u64) -> MiningFixture { let mut config = (*test_chain_config()).clone(); - config.activation_heights.papyrus200 = papyrus200; + config.activation_heights.rskip_umm = Some(rskip_umm); fixture_with(height, Arc::new(config), U256::from(2)) } @@ -619,16 +624,26 @@ fn filling_the_mining_fields_does_not_move_the_merged_mining_hash() { assert_eq!(before, header.hash_for_merged_mining()); } -/// From papyrus200 rskj puts an empty `ummRoot` in every header it builds. -/// Its presence changes both hashes, so a miner that decided by activation -/// height differently from its peers produces blocks nobody accepts. +/// From RSKIPUMM rskj puts an empty `ummRoot` in every header it builds +/// (`BlockHeaderBuilder`). Its presence changes both hashes, so a miner that +/// decided by activation height differently from its peers produces blocks +/// nobody accepts. RSKIPUMM is papyrus200 on mainnet and testnet, and 1 on +/// rskj's regtest, so papyrus200 alone does not say. #[test] -fn umm_root_appears_once_papyrus_is_active() { +fn umm_root_appears_once_rskip_umm_is_active() { let fx = fixture(20); assert!(fx.build().unwrap().block.header.umm_root.is_none()); - let fx = fixture_with_papyrus_at(0, 20); + // The template is #21: a height above it is not reached yet. + let fx = fixture_with_umm_at(22, 20); + assert!(fx.build().unwrap().block.header.umm_root.is_none()); + let fx = fixture_with_umm_at(21, 20); assert_eq!(fx.build().unwrap().block.header.umm_root, Some(Bytes::new())); + + let mut papyrus = (*test_chain_config()).clone(); + papyrus.activation_heights.papyrus200 = 0; + let fx = fixture_with(20, Arc::new(papyrus), U256::from(2)); + assert!(fx.build().unwrap().block.header.umm_root.is_none()); } /// Inheriting the parent's gas limit and minimum gas price is always valid -- @@ -1410,12 +1425,12 @@ fn a_solution_is_imported_on_a_chain_deep_enough_for_remasc_to_pay() { } /// And the round trip with `ummRoot` present, which is what mainnet actually -/// carries. Elsewhere these tests keep papyrus200 out of reach so they can +/// carries. Elsewhere these tests keep RSKIPUMM out of reach so they can /// work at low block numbers, which means the field every real header has is /// absent from the block they mine. #[test] fn a_solution_is_imported_with_umm_root_present() { - let fx = fixture_with_papyrus_at(0, 20); + let fx = fixture_with_umm_at(0, 20); let work = fx.server.get_work().unwrap(); let raw = bitcoin::consensus::serialize(&solve(&work)); @@ -1833,6 +1848,40 @@ mod partitioned_templates { senders: u8, hardfork_cfg: RskHardforkConfig, minimum_gas_price: u64, + ) -> ( + BlockTemplateBuilder, + Header, + Vec, + Arc, + Arc, + ) { + fixture_with_state(senders, hardfork_cfg, minimum_gas_price, |root, _| root) + } + + /// The test chain with the header heights `ladder` runs: RSKIP144, + /// RSKIP351 and RSKIP535 from the first block that has them, as a node + /// moves them with its ladder (`--consensus-rule`). These ladders + /// schedule each at 0 or 1, if at all. + fn with_heights_of(ladder: &RskHardforkConfig) -> Arc { + let first = |active: fn(&RskHardforkConfig, u64) -> bool| { + (0..=1).find(|&number| active(ladder, number)) + }; + let mut config = (*test_chain_config()).clone(); + let heights = &mut config.activation_heights; + heights.rskip144 = first(RskHardforkConfig::has_rskip144); + heights.rskip351 = first(RskHardforkConfig::has_rskip351); + heights.rskip535 = first(RskHardforkConfig::has_rskip535); + Arc::new(config) + } + + /// `fixture_with_forks_priced` on a state that `seed` adds to once the + /// senders are funded. + #[allow(clippy::type_complexity)] + fn fixture_with_state( + senders: u8, + hardfork_cfg: RskHardforkConfig, + minimum_gas_price: u64, + seed: impl FnOnce(TrieNode, &dyn TrieStore) -> TrieNode, ) -> (BlockTemplateBuilder, Header, Vec, Arc, Arc) { let dir = Box::leak(Box::new(tempfile::tempdir().unwrap())); let store = Arc::new(BlockStore::open(dir.path()).unwrap()); @@ -1849,6 +1898,7 @@ mod partitioned_templates { let k = rustock_trie::TrieKeySlice::from_key(&rustock_trie::account_key(&addr)); root = root.put(&k, &acct.encode(), trie_store.as_ref()); } + let mut root = seed(root, trie_store.as_ref()); let state_root = root.compute_hash(trie_store.as_ref()); root.save(trie_store.as_ref(), true); // The seeding is the fixture's whole premise; a silent failure here @@ -1880,7 +1930,7 @@ mod partitioned_templates { let builder = BlockTemplateBuilder::new( Arc::new(BlockProcessor::new(hardfork_cfg.clone(), store.clone())), trie_store.clone(), - test_chain_config(), + with_heights_of(&hardfork_cfg), hardfork_cfg, MiningConfig { coinbase_address: address!("00000000000000000000000000000000c0117b45"), @@ -2034,6 +2084,165 @@ mod partitioned_templates { .expect("the block validates"); } + /// rskj `BlockHeaderBuilder`, `ActivationConfig.getHeaderVersion` and + /// `BlockExecutor.fill`: the fields a template carries by its height, + /// partitioned or not. RSKIP351 makes it version 1 and RSKIP535 version + /// 2, whose base event is empty while the Union Bridge holds none; with + /// RSKIP144 and RSKIP351 a block that runs as one list has an empty list + /// of edges, not none. + #[test] + fn a_template_carries_the_fields_its_height_brings() { + use crate::hardfork::ConsensusRule; + use rustock_core::types::header::HeaderVersionFields; + let fields = |version, edges: Option>, base_event: Option| { + Some(HeaderVersionFields { + version, + edges, + base_event, + }) + }; + let parallel = RskHardforkConfig::all_active_parallel(33); + let parallel_without_535 = parallel + .clone() + .with_consensus_rule(ConsensusRule::Rskip535, None); + let only_351 = RskHardforkConfig::all_active(33) + .with_consensus_rule(ConsensusRule::Rskip351, Some(0)) + .with_consensus_rule(ConsensusRule::Rskip535, None); + for (ladder, expected) in [ + (RskHardforkConfig::all_active(33), None), + (only_351, fields(1, None, None)), + (parallel_without_535, fields(1, Some(vec![]), None)), + (parallel, fields(2, Some(vec![]), Some(Bytes::new()))), + ] { + let (builder, parent, _, trie_store, store) = fixture_with_forks(1, ladder.clone()); + let template = builder + .build(std::slice::from_ref(&parent), Vec::new(), &Pool(Vec::new())) + .expect("the template builds"); + assert_eq!(template.block.header.version_fields, expected, "{ladder:?}"); + + let processor = BlockProcessor::new(ladder, store); + let parent_state = builder.load_state_root(parent.state_root).unwrap(); + processor + .process_block(&template.block, &parent_state, trie_store) + .expect("the block validates"); + } + } + + /// A call from `from` to `to` carrying `input` and no value, signed. + fn call( + from: &SigningKey, + to: Address, + nonce: u64, + chain_id: u64, + input: Vec, + ) -> PendingTransaction { + let mut tx = Transaction { + nonce, + gas_price: U256::from(1), + gas_limit: U256::from(100_000), + to: Bytes::copy_from_slice(to.as_slice()), + value: U256::ZERO, + input: Bytes::from(input), + v: 0, + r: U256::ZERO, + s: U256::ZERO, + cached_rlp: None, + }; + sign(&mut tx, from, chain_id); + let hash = B256::from_slice(&sha3::Keccak256::digest(tx.rlp_for_trie())); + PendingTransaction { + tx, + sender: address_of(from), + hash, + } + } + + /// `setBaseEvent(bytes)`'s call data: the selector, then rskj's + /// `BytesType`, an offset, the length and the bytes padded to a word. + fn set_base_event(event: &[u8]) -> Vec { + let mut input = sha3::Keccak256::digest(b"setBaseEvent(bytes)")[..4].to_vec(); + input.extend_from_slice(&U256::from(32u64).to_be_bytes::<32>()); + input.extend_from_slice(&U256::from(event.len()).to_be_bytes::<32>()); + input.extend_from_slice(event); + input.resize(4 + 64 + event.len().div_ceil(32) * 32, 0); + input + } + + /// rskj `BlockExecutor.fill`: a version-2 header carries the base event + /// the Union Bridge holds once its block has run -- here the one the + /// block's own transaction sets, which its parent's state does not hold. + #[test] + fn a_version_2_template_carries_the_base_event_its_block_sets() { + use crate::bridge::union::{union_key, BASE_EVENT_KEY, UNION_CONTRACT_ADDRESS_KEY}; + use crate::precompiles::BRIDGE_ADDR; + use rustock_trie::{ + account_key, storage_key, storage_prefix_key, AccountState, TrieKeySlice, + }; + let ladder = RskHardforkConfig::all_active_parallel(33); + let union_bridge = address_of(&signing_key(1)); + let (builder, parent, keys, trie_store, store) = + fixture_with_state(1, ladder.clone(), 0, |root, trie_store| { + // The Bridge, and the cell naming the union bridge contract as + // rskj's `serializeRskAddress` writes it: RLP of the twenty + // bytes. + let at = |key: &[u8]| TrieKeySlice::from_key(key); + let account = AccountState::new(U256::ZERO, U256::ZERO).encode(); + let root = root.put(&at(&account_key(&BRIDGE_ADDR)), &account, trie_store); + let root = root.put(&at(&storage_prefix_key(&BRIDGE_ADDR)), &[1], trie_store); + let slot = B256::from(union_key(UNION_CONTRACT_ADDRESS_KEY)); + let named = [&[0x94][..], union_bridge.as_slice()].concat(); + root.put(&at(&storage_key(&BRIDGE_ADDR, &slot)), &named, trie_store) + }); + let base_event = |state: B256| { + let root = builder.load_state_root(state).unwrap(); + let slot = B256::from(union_key(BASE_EVENT_KEY)); + root.get( + &TrieKeySlice::from_key(&storage_key(&BRIDGE_ADDR, &slot)), + trie_store.as_ref(), + ) + }; + assert_eq!(base_event(parent.state_root), None, "the parent holds none"); + + let event = b"a base event".to_vec(); + let pool = Pool(vec![call( + &keys[0], + BRIDGE_ADDR, + 0, + 33, + set_base_event(&event), + )]); + let template = builder + .build(std::slice::from_ref(&parent), Vec::new(), &pool) + .expect("the template builds"); + assert_eq!( + but_remasc(&template.block.transactions).len(), + 1, + "the call is in" + ); + assert_eq!( + base_event(template.block.header.state_root), + Some(event.clone()) + ); + let fields = template + .block + .header + .version_fields + .clone() + .expect("versioned"); + assert_eq!(fields.version, 2); + assert_eq!( + fields.base_event, + Some(Bytes::from(event)), + "read after the block" + ); + + let processor = BlockProcessor::new(ladder, store); + let parent_state = builder.load_state_root(parent.state_root).unwrap(); + processor + .process_block(&template.block, &parent_state, trie_store) + .expect("a block this miner built must be one this node would accept"); + } + // ── packing by gas used, and the RSKIP252 cap ───────────────────── /// Ten transfers, each declaring 3 M gas and using 21,000, on a 10 M diff --git a/crates/execution/src/mining/workload.rs b/crates/execution/src/mining/workload.rs index 93cc138..3c95c8f 100644 --- a/crates/execution/src/mining/workload.rs +++ b/crates/execution/src/mining/workload.rs @@ -578,6 +578,13 @@ impl Workload { let mut chain_config = rustock_core::config::ChainConfig::testnet(); chain_config.chain_id = self.chain_id as u8; chain_config.min_difficulty = U256::from(1); + // The header's layout on the ladder's schedule, which runs blocks as + // their sublists from genesis: RSKIP144 and RSKIP351 there, and + // RSKIP535 where the ladder has it by the first block. + let heights = &mut chain_config.activation_heights; + heights.rskip144 = Some(0); + heights.rskip351 = Some(0); + heights.rskip535 = hardfork_cfg.has_rskip535(1).then_some(1); // The miner and the validator on the same constants: the sequential // sublist's minimum sets every sublist's gas limit, which the // validator holds the miner's partition to. diff --git a/crates/execution/src/processor.rs b/crates/execution/src/processor.rs index 2f6aecd..aa5c90f 100644 --- a/crates/execution/src/processor.rs +++ b/crates/execution/src/processor.rs @@ -3078,10 +3078,12 @@ mod tests { } /// Regtest's constants with rskj's timestamp rule on, so every rule an - /// uncle faces runs. + /// uncle faces runs, and without RSKIPUMM, so these headers need no UMM + /// root. fn uncle_config() -> Arc { let mut config = rustock_core::config::ChainConfig::regtest(); config.max_future_block_time = 60; + config.activation_heights.rskip_umm = None; Arc::new(config) } From 28f03216c99152ed50d23d03cdb9a220c74433be Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Mon, 9 Nov 2026 12:49:11 -0800 Subject: [PATCH 057/264] core, storage, networking: headers read by rskj's heights, in the form they came in rskj reads a header by its height and by the form it came in (BlockFactory.decodeHeader): in full, as blocks and their uncles carry headers, a version, edges and base event follow the UMM root; compressed, as a chain's headers go to peers (BlockHeadersResponse), slot 6 holds a commitment that names the version, and nothing follows it. canBeDecoded counts the fields exactly, so a header with one too many or too few at its height is one rskj can't read. rustock read by shape, and held what it read to rskj's heights afterwards (HeaderLayoutRule). The decoder. Header::decode_in reads in a HeaderForm, full, compressed or stored, under explicit heights; decode and decode_with_hash read in full under the process's, which the CLI installs at startup. It counts the fields as canBeDecoded does, with testnet's base-event recovery window below #7,800,000, and takes the version as rskj does: getRLPData()[0] in full, and compressed the first byte of the first item of the one list in slot 6. Version 1 builds a V1, which ignores a base event it read; 2 a V2; any other a V0, in either form, which holds the bloom, edges and base event it read and writes them with no version. HeaderLayoutRule goes: what it checked, the decoder now refuses. The store. rskj's full encoding of a header doesn't always read back as that header: a V0's writes no version, and a V1's no base event, a field too few from RSKIP535 on. A header held compressed has no full encoding at all. So from RSKIP351 on the store lays a header out in its own way, after a mark where the full encoding has a version: the version, then the edges and the base event, each an empty list where the header has none; or, for a header held compressed, the mark alone. It reads only what rskj reads at the header's height, and what the store wrote before, rskj's full encoding and a header held compressed, unmarked, still reads. store_headers_batch wrote rskj's full encoding rather than the store's own; every path that writes a header now writes encode_header's. The wire. A chain's headers go to a peer compressed and come from one compressed (message 10), as rskj sends and reads them. Trunk headers with their uncles (message 28) go between rustock nodes in the store's layout, so a header held compressed arrives as held, and a peer can't send one with fields rskj doesn't read at its height. Tests read each fixture in the form it was taken in and under its chain's heights. The export after an import names the rule the blocks were imported under, since a node reads its store, as rskj does, under its chain's heights. --- crates/cli/tests/import_blocks.rs | 3 + crates/core/src/mutate.rs | 23 +- crates/core/src/types/header.rs | 1360 +++++++++++++++----- crates/core/src/validation/header_rules.rs | 39 - crates/core/src/validation/mod.rs | 10 +- crates/core/src/validation/tests.rs | 79 -- crates/core/src/validation/uncles.rs | 6 +- crates/execution/src/executor.rs | 5 +- crates/execution/src/mining/tests.rs | 10 +- crates/execution/src/processor.rs | 5 +- crates/networking/src/protocol/rsk.rs | 24 +- crates/networking/tests/testnet_headers.rs | 157 +++ crates/rpc/src/tests.rs | 25 +- crates/storage/src/freezer.rs | 28 +- crates/storage/src/lib.rs | 125 +- crates/storage/tests/stall_replays.rs | 5 +- crates/sync/src/tests.rs | 18 +- 17 files changed, 1404 insertions(+), 518 deletions(-) create mode 100644 crates/networking/tests/testnet_headers.rs diff --git a/crates/cli/tests/import_blocks.rs b/crates/cli/tests/import_blocks.rs index 195a18e..87524a8 100644 --- a/crates/cli/tests/import_blocks.rs +++ b/crates/cli/tests/import_blocks.rs @@ -154,6 +154,9 @@ fn an_export_after_an_import_reproduces_rskjs_file() { .arg(dir.path().join("db")) .args(["--trie-backend", "external", "--trie-dir"]) .arg(dir.path().join("trie")) + // The rule the blocks were imported under: a node reads its + // store, as rskj does, under its chain's heights. + .args(["--consensus-rule", "rskip144=-1"]) .arg("--export-blocks") .arg(&file) .args(["--export-from", from]); diff --git a/crates/core/src/mutate.rs b/crates/core/src/mutate.rs index 3e74c1e..37848f6 100644 --- a/crates/core/src/mutate.rs +++ b/crates/core/src/mutate.rs @@ -91,10 +91,12 @@ pub(crate) fn mutants(seed: &[u8], count: usize, rng_seed: u64) -> Vec> } /// What this crate does with a header's bytes a peer sends, done to `bytes` -/// under each network's heights and rules: decoded both ways, hashed both -/// ways, encoded both ways, and checked by every rule, a header's own, an -/// uncle's and against a parent. Only that none of it panics is checked. +/// under each network's heights and rules: decoded in each form, the hash +/// cached or not, hashed both ways, encoded every way, and checked by every +/// rule, a header's own, an uncle's and against a parent. Only that none of +/// it panics is checked. pub(crate) fn decode_and_check_header(bytes: &[u8]) { + use crate::types::header::HeaderForm; let networks = [ (ActivationHeights::mainnet(), ChainConfig::mainnet()), (ActivationHeights::testnet(), ChainConfig::testnet()), @@ -102,17 +104,22 @@ pub(crate) fn decode_and_check_header(bytes: &[u8]) { ]; for (activations, config) in networks { let verifier = HeaderVerifier::default_rsk(Arc::new(config)); - let decoded = [ - Header::decode_with_hash_and_activations(&mut &bytes[..], &activations), - Header::decode_with_activations(&mut &bytes[..], &activations), - ]; - for header in decoded.into_iter().flatten() { + let decoded = [HeaderForm::Full, HeaderForm::Compressed, HeaderForm::Stored] + .into_iter() + .flat_map(|form| { + [ + Header::decode_with_hash_in(&mut &bytes[..], &activations, form), + Header::decode_in(&mut &bytes[..], &activations, form), + ] + }); + for header in decoded.flatten() { let _ = header.hash(); let _ = header.hash_for_merged_mining(); let _ = header.extension_commitment(); let _ = header.extension(); let _ = alloy_rlp::encode(&header); let _ = header.encode_compressed(); + let _ = header.encode_stored(); let _ = verifier.verify(&header, None); let _ = verifier.verify_uncle(&header); let _ = verifier.verify_against_parent(&header, &header); diff --git a/crates/core/src/types/header.rs b/crates/core/src/types/header.rs index 9d6c9f5..816c8f5 100644 --- a/crates/core/src/types/header.rs +++ b/crates/core/src/types/header.rs @@ -67,25 +67,52 @@ pub struct Header { pub cached_hash_for_merged_mining: Option, } -/// What a versioned (RSKIP-351) header carries beyond a version-0 one. +/// What a header carries after `ummRoot` from RSKIP-351 on: its version, +/// RSKIP-144's edges and RSKIP-535's base event. /// -/// rskj appends these after `ummRoot` in the uncompressed encoding only +/// rskj writes a V1's and a V2's after `ummRoot` in the full encoding only /// (`BlockHeaderV1/V2.addExtraFieldsToEncodedHeader`, guarded by -/// `!usingCompressedEncoding`). The compressed encoding -- the one that is -/// hashed -- replaces the bloom field with a commitment to them instead. +/// `!usingCompressedEncoding`); the compressed encoding, which is hashed, +/// carries a commitment to them in the bloom's slot instead. A +/// `BlockHeaderV0` writes its edges and base event in both. `None` on a +/// header from before RSKIP-351, and on a V1 or a V2 read compressed. #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] pub struct HeaderVersionFields { - /// 1 or 2. A version-0 header has no record of a version at all, which is - /// why this lives behind an `Option` rather than defaulting to 0. + /// 1 or 2; or 0 for a header whose version is any other byte, which rskj + /// builds as a `BlockHeaderV0`: that holds the edges and base event it + /// read, writes them with no version in both encodings, and hashes them + /// as they are, with no extension. pub version: u8, - /// RSKIP-144 transaction execution sublist edges, when the block has any. - /// rskj omits the field entirely when they are absent. + /// RSKIP-144 transaction execution sublist edges, read where RSKIP-351 + /// and RSKIP-144 are both active, and written only where present. pub edges: Option>, - /// RSKIP-535 base event. V2 only, and empty rather than absent when the - /// block carries none. + /// RSKIP-535 base event, read where it's enabled, and empty where the + /// header has no field for it. Always `None` on version 1, which ignores + /// one it reads. pub base_event: Option, } +/// Which of rskj's encodings of a header is being read. +/// +/// rskj reads a header by its height, and from RSKIP-351 on the same header +/// has two encodings that lay its fields out differently. rskj tells what a +/// field is by where it sits, so a reader has to be told which encoding it +/// has. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum HeaderForm { + /// `getFullEncoded`, as blocks and their uncles carry headers + /// (`BlockFactory.decodeBlock`, and a body's uncles): the logs bloom in + /// slot 6, and the version, edges and base event after `ummRoot`. + Full, + /// `getEncodedCompressed`, as a chain's headers are sent + /// (`BlockHeadersResponseMessage`, read with `decodeHeader(.., true)`): + /// a versioned header's commitment to its extension in slot 6, and + /// nothing of it after `ummRoot`. A version-0 header has the one form. + Compressed, + /// This node's store: [`Header::encode_stored`]. + Stored, +} + /// What rskj's `BlockFactory` reads from a header that [`Header`]'s typed /// fields can't hold. /// @@ -294,6 +321,215 @@ fn to_byte_array(value: &[u8]) -> Vec { bytes } +/// rskj `BlockFactory.canBeDecoded`: whether rskj reads a header of `count` +/// fields at `number`, in its full or its compressed encoding. It counts +/// them exactly: the sixteen every header has; `ummRoot` from RSKIPUMM; from +/// RSKIP-351 on and in the full encoding only, the version, the edges where +/// RSKIP-144 is active and the base event where RSKIP-535 is; and the three +/// merged-mining fields or none. On testnet below #7,800,000 it also reads a +/// full header one field short, and gives it an empty base event +/// (`isInBaseEventRecoveryWindow`). +fn rskj_reads( + count: usize, + activations: &crate::config::ActivationHeights, + number: u64, + compressed: bool, +) -> bool { + let full = !compressed && activations.rskip351_active(number); + let base_event = full && activations.base_event_enabled(number); + let fields = 16 + + usize::from(activations.umm_active(number)) + + usize::from(full) + + usize::from(full && activations.edges_active(number)) + + usize::from(base_event); + let recovering = base_event + && activations + .base_event_recovery_below + .is_some_and(|below| number < below); + count == fields + || count == fields + 3 + || (recovering && (count + 1 == fields || count + 1 == fields + 3)) +} + +/// The version of a header rskj reads compressed from RSKIP-351 on: +/// `RLP.decodeList(extensionData).get(0).getRLPData()[0]`, the first byte of +/// the first item of the one list in slot 6. Where there is no such byte, +/// rskj throws and can't read the header. +fn compressed_version(slot6: Option<&[u8]>) -> alloy_rlp::Result { + use crate::rskj_rlp::Element; + let none = || refused("a compressed header whose slot 6 names no version"); + let elements = crate::rskj_rlp::decode2(slot6.ok_or_else(none)?).map_err(|_| none())?; + let [list @ Element::List { .. }] = elements.as_slice() else { + return Err(none()); + }; + let items = list.elements().map_err(|_| none())?; + items + .first() + .and_then(Element::data) + .map(|data| data[0]) + .ok_or_else(none) +} + +/// Where a version sits from RSKIP-351 on, the mark that begins this node's +/// own layout of a header in its store ([`Header::encode_stored`]): with the +/// version after it for a header that holds its own fields, and alone for +/// one held compressed. rskj's full encoding has a version of one byte +/// there, as the store wrote headers before this layout, and never this one. +const STORED_LAYOUT: u8 = 0xff; + +/// The next of a header's fields. +fn take<'h, 'a>( + rest: &mut &'h [crate::rskj_rlp::Element<'a>], +) -> Option<&'h crate::rskj_rlp::Element<'a>> { + let (first, others) = rest.split_first()?; + *rest = others; + Some(first) +} + +/// What follows a header's sixteen fields, as read at its height. +struct Trailer<'h, 'a> { + umm_root: Option, + /// From RSKIP-351 on: the full encoding's own, or the one the commitment + /// in slot 6 names. + version: Option, + edges: Option>, + base_event: Option, + mining: &'h [crate::rskj_rlp::Element<'a>], + compressed: bool, +} + +impl<'h, 'a> Trailer<'h, 'a> { + /// What `BlockFactory.decodeHeader` reads after the sixteen fields of a + /// header at `number`, in its full or its compressed encoding. + fn read( + mut rest: &'h [crate::rskj_rlp::Element<'a>], + slot6: Option<&[u8]>, + activations: &crate::config::ActivationHeights, + number: u64, + compressed: bool, + ) -> alloy_rlp::Result { + if !rskj_reads(16 + rest.len(), activations, number, compressed) { + return Err(refused( + "a header with more or fewer fields than rskj reads at its height", + )); + } + // `getRLPRawData`: an empty root is kept, and written back, empty. + let umm_root = match activations.umm_active(number) { + true => { + let root = take(&mut rest).ok_or(refused("a header without its UMM root"))?; + Some(Bytes::copy_from_slice(root.raw_data())) + } + false => None, + }; + let version = if !activations.rskip351_active(number) { + None + } else if compressed { + Some(compressed_version(slot6)?) + } else { + // `getRLPData()[0]`: rskj throws on none. + let version = take(&mut rest) + .and_then(crate::rskj_rlp::Element::data) + .ok_or(refused("a versioned header whose version is none"))?; + Some(version[0]) + }; + // `rlpToShorts(getRLPRawData())`, where a field is left for them. + let edges = if !compressed && !rest.is_empty() && activations.edges_active(number) { + take(&mut rest).map(|edges| rlp_to_shorts(edges.raw_data())) + } else { + None + }; + // Where no field is left for it, or only the three merged-mining + // fields are, rskj gives the header an empty base event. + let base_event = if !compressed && activations.base_event_enabled(number) { + Some(match rest.len() { + 0 | 3 => Bytes::new(), + _ => Bytes::copy_from_slice( + take(&mut rest).map_or(&[][..], |field| field.raw_data()), + ), + }) + } else { + None + }; + Ok(Self { + umm_root, + version, + edges, + base_event, + mining: rest, + compressed, + }) + } + + /// What follows the sixteen fields of a header in this node's store, and + /// in the trunk headers rustock nodes send each other: in + /// [`Header::encode_stored`]'s own layout, holding only the fields rskj + /// reads a header with at its height; or, as the store wrote headers + /// before that layout, in rskj's full encoding, or in the compressed one + /// for a header held compressed. + fn stored( + rest: &'h [crate::rskj_rlp::Element<'a>], + slot6: Option<&[u8]>, + activations: &crate::config::ActivationHeights, + number: u64, + ) -> alloy_rlp::Result { + use crate::rskj_rlp::Element; + if !activations.rskip351_active(number) { + return Self::read(rest, slot6, activations, number, false); + } + let umm = usize::from(activations.umm_active(number)); + match rest.get(umm) { + // Held compressed: the mark alone, then the merged-mining fields. + Some(Element::Item([STORED_LAYOUT])) => Ok(Self { + mining: &rest[umm + 1..], + ..Self::read(&rest[..umm], slot6, activations, number, true)? + }), + Some(Element::Item([STORED_LAYOUT, version @ 0..=2])) => { + let unstored = || refused("a stored header in no layout this node writes"); + let [edges, base_event, mining @ ..] = rest.get(umm + 1..).unwrap_or_default() + else { + return Err(unstored()); + }; + // An empty list where a header has no such field. + let held = |field: &'h Element<'a>| match field { + Element::List { + encoding: [0xc0], .. + } => None, + field => Some(field), + }; + let (edges, base_event) = (held(edges), held(base_event)); + // What rskj reads at the header's height: in full, the edges + // where RSKIP-144 is active and a base event where RSKIP-535 + // is, which a V1 ignores; compressed, a V0 with neither, whose + // slot 6 names its version. + let in_full = edges.is_some() == activations.edges_active(number) + && base_event.is_some() + == (*version != 1 && activations.base_event_enabled(number)); + let compressed = *version == 0 + && edges.is_none() + && base_event.is_none() + && compressed_version(slot6).is_ok_and(|named| !matches!(named, 1 | 2)); + if !in_full && !compressed { + return Err(unstored()); + } + Ok(Self { + umm_root: rest[..umm] + .first() + .map(|root| Bytes::copy_from_slice(root.raw_data())), + version: Some(*version), + edges: edges.map(|edges| rlp_to_shorts(edges.raw_data())), + base_event: base_event + .map(|base_event| Bytes::copy_from_slice(base_event.raw_data())), + mining, + compressed: false, + }) + } + // A version of one byte, as rskj's full encoding has it. + Some(Element::Item([_])) => Self::read(rest, slot6, activations, number, false), + _ => Self::read(rest, slot6, activations, number, true), + } + } +} + impl Header { /// The sixteen fields every RSK header carries, in wire order, encoded the /// way rskj encodes them. @@ -463,29 +699,23 @@ impl Header { kept.and_then(|k| k.receipts_root.as_ref()), &self.receipts_root, ); - // RSKIP-351: a V1/V2 header carries `RLP([version, extension_hash])` - // in the slot a V0 header fills with its 256-byte bloom. The bytes are - // already RLP, so they are appended rather than re-encoded. - // - // Encoding the bloom here regardless was invisible for as long as the - // decoder cached the hash from the peer's original bytes: nothing ever - // asked this encoder to reproduce a versioned header. Re-deriving the - // hash of a testnet V1 block is what surfaced it. - // rskj: `compressed ? getExtensionData() : getLogsBloom()`. Only the - // compressed form is hashed; the wire form keeps the literal bloom and - // carries the version, edges and base event after `ummRoot` instead. - match (compressed, &self.extension_data, &self.version_fields) { - // Already-compressed input: the literal bloom was never received, - // so this is all there is to emit either way. - (_, Some(ext), _) => list.extend_from_slice(ext), - (true, None, Some(_)) => match self.compressed_extension_field() { + // Slot 6, rskj's `compressed ? getExtensionData() : getLogsBloom()`: a + // versioned header's commitment to its extension in the compressed + // encoding and its bloom in the full one; a version-0 header's bloom + // in both. A header held compressed has only its commitment. The + // commitment is already RLP, so it is appended rather than + // re-encoded. + let versioned = self.version_fields.as_ref().is_some_and(|v| v.version != 0); + match &self.extension_data { + Some(ext) => list.extend_from_slice(ext), + None if compressed && versioned => match self.compressed_extension_field() { Some(field) => list.extend_from_slice(&field), // An extension that will not build cannot be hashed into a // commitment; emitting the bloom keeps this total, and the // hash it yields simply will not match, which is visible. None => self.logs_bloom.encode(list), }, - _ => encode_kept( + None => encode_kept( list, kept.and_then(|k| k.logs_bloom.as_ref()), &self.logs_bloom, @@ -506,38 +736,72 @@ impl Header { self.uncle_count.encode(list); } - /// rskj BlockHeader.getEncoded(true, with_merkle_proof_and_coinbase): - /// list payload of all header fields; the merkle proof and coinbase tx - /// are excluded from RSKIP92 block hashes but kept on the wire. - fn encode_payload(&self, with_merkle_proof_and_coinbase: bool, compressed: bool) -> Vec { + /// rskj `BlockHeader.getEncoded(withMergedMiningFields, + /// withMerkleProofAndCoinbase, compressed)`: the list payload of the + /// header's fields in `form`. RSKIP-92 block hashes leave out the merkle + /// proof and coinbase transaction, and the merged-mining hash all three + /// merged-mining fields. + fn encode_payload( + &self, + form: HeaderForm, + with_mining_fields: bool, + with_merkle_proof_and_coinbase: bool, + ) -> Vec { + let compressed = form == HeaderForm::Compressed; let mut list = Vec::new(); self.encode_base_fields(&mut list, compressed); if let Some(umm) = &self.umm_root { umm.encode(&mut list); } - // rskj `BlockHeaderV1/V2.addExtraFieldsToEncodedHeader`: the version, - // the edges when the block has any, and (V2) the base event, in that - // order and only when not compressed. - if !compressed { - if let Some(v) = &self.version_fields { - list.push(v.version); - if let Some(edges) = &v.edges { - // Already an RLP string; appending rather than encoding, - // or `Vec` would go out as a list of single bytes. - list.extend_from_slice( - &crate::types::header_extension::shorts_to_rlp(edges), - ); + // rskj `addExtraFieldsToEncodedHeader`. The edges are already an RLP + // string; encoding them as a `Vec` would write a list of bytes. + if let Some(v) = &self.version_fields { + let edges = v + .edges + .as_deref() + .map(crate::types::header_extension::shorts_to_rlp); + if form == HeaderForm::Stored { + // This node's own layout, which reads back as this header + // whatever rskj would make of it: the version after a mark, + // then the edges and the base event, each an empty list + // where the header has none. + [STORED_LAYOUT, v.version].as_slice().encode(&mut list); + match &edges { + Some(edges) => list.extend_from_slice(edges), + None => list.push(alloy_rlp::EMPTY_LIST_CODE), + } + match &v.base_event { + Some(base_event) => base_event.encode(&mut list), + None => list.push(alloy_rlp::EMPTY_LIST_CODE), } - if let Some(be) = &v.base_event { - be.encode(&mut list); + } else if v.version == 0 || !compressed { + // A V1 or a V2 writes its version, then the edges where the + // block has them and (V2) the base event, in the full + // encoding only. A `BlockHeaderV0` writes its edges and base + // event in both, with no version. + if v.version != 0 { + list.push(v.version); + } + if let Some(edges) = &edges { + list.extend_from_slice(edges); + } + if let Some(base_event) = v.base_event.as_ref().filter(|_| v.version != 1) { + base_event.encode(&mut list); } } + } else if form == HeaderForm::Stored && self.extension_data.is_some() { + // A header held compressed, marked as one in the store. + [STORED_LAYOUT].as_slice().encode(&mut list); } // rskj emits the merkle proof and coinbase tx whenever the mining // header is present — as empty elements if need be — rather than // omitting them (BlockHeader.getEncoded with merged mining fields). - if let Some(btc) = &self.bitcoin_merged_mining_header { + if let Some(btc) = self + .bitcoin_merged_mining_header + .as_ref() + .filter(|_| with_mining_fields) + { btc.encode(&mut list); if with_merkle_proof_and_coinbase { match &self.bitcoin_merged_mining_merkle_proof { @@ -556,7 +820,7 @@ impl Header { impl Encodable for Header { fn encode(&self, out: &mut dyn alloy_rlp::BufMut) { - let list = self.encode_payload(true, false); + let list = self.encode_payload(HeaderForm::Full, true, true); alloy_rlp::Header { list: true, payload_length: list.len() }.encode(out); out.put_slice(&list); } @@ -569,7 +833,8 @@ impl Encodable for Header { } impl alloy_rlp::Decodable for Header { - /// Decodes under the process-wide activation heights + /// Decodes rskj's full encoding, as blocks carry headers, under the + /// process-wide activation heights /// ([`ActivationHeights::current`](crate::config::ActivationHeights::current)). fn decode(buf: &mut &[u8]) -> alloy_rlp::Result { Self::decode_with_activations(buf, crate::config::ActivationHeights::current()) @@ -577,8 +842,18 @@ impl alloy_rlp::Decodable for Header { } impl Header { - /// Decode a header as rskj's `BlockFactory.decodeHeader` reads one, - /// under explicit activation heights. + /// Decode a header as rskj's `BlockFactory.decodeHeader` reads one in its + /// full encoding, as blocks and their uncles carry headers, under + /// explicit activation heights: see [`Self::decode_in`]. + pub fn decode_with_activations( + buf: &mut &[u8], + activations: &crate::config::ActivationHeights, + ) -> alloy_rlp::Result { + Self::decode_in(buf, activations, HeaderForm::Full) + } + + /// Decode a header as rskj's `BlockFactory.decodeHeader` reads one in + /// `form`, under explicit activation heights. /// /// The list and its fields are cut with rskj's own RLP /// ([`crate::rskj_rlp`]), which takes lengths canonical RLP refuses, @@ -588,27 +863,30 @@ impl Header { /// goes in [`KeptFields`], and the header hashes as rskj writes it back /// from what it read, which is not always as it was sent. /// - /// Of the heights, RSKIP-144's, RSKIP-351's and orchid's (RSKIP-98) are - /// read here; which fields a header carries at its height is - /// `HeaderLayoutRule`'s to say. - pub fn decode_with_activations( + /// After the sixteen fields every header has, rskj reads by the header's + /// height, as the heights here say: how many fields there are, and what + /// each is. A header with any other count is refused. + pub fn decode_in( buf: &mut &[u8], activations: &crate::config::ActivationHeights, + form: HeaderForm, ) -> alloy_rlp::Result { let (list, next) = crate::rskj_rlp::first_element(buf) .map_err(|_| refused("a header rskj's RLP can't read"))?; let fields = list .elements() .map_err(|_| refused("a header that is not a list of fields"))?; - let header = Self::from_fields(&fields, activations)?; + let header = Self::from_fields(&fields, activations, form)?; *buf = &buf[next..]; Ok(header) } - /// A header's fields, each read as `BlockFactory.decodeHeader` reads it. + /// A header's fields, each read as `BlockFactory.decodeHeader` reads it + /// in `form`. fn from_fields( fields: &[crate::rskj_rlp::Element<'_>], activations: &crate::config::ActivationHeights, + form: HeaderForm, ) -> alloy_rlp::Result { // rskj reads each field with `getRLPData`: none for an empty item, // and a list's whole encoding for a list, which it reads as bytes. @@ -643,8 +921,8 @@ impl Header { let number = java_long(field(8)?)?; // Slot 6, the logs bloom or a commitment in its place, is read once - // the fields after it say which (below). - let slot6 = field(6)?.unwrap_or_default(); + // the fields after the sixteen say which (below). + let slot6 = field(6)?; // `RLP.parseBlockDifficulty`: `new BigInteger(bytes)`, so a negative // difficulty, which `BlockDifficulty` throws on. rskj reads none as @@ -689,100 +967,70 @@ impl Header { kept.minimum_gas_price = bytes; let uncle_count = java_int(field(15)?)?; - // What is left is told apart by count, since nothing here knows the - // network: none is a header without merged-mining fields, three are - // those fields, and four or more begin with a UMM root (RSKIPUMM, - // papyrus200). `HeaderLayoutRule` holds what was read to the fields - // rskj reads at the header's height. - let mut rest = &fields[16..]; - let mut umm_root = None; - if rest.len() >= 4 { - // `getRLPRawData`: an empty root is kept, and written back, empty. - umm_root = Some(Bytes::copy_from_slice(rest[0].raw_data())); - rest = &rest[1..]; - } + // What follows the sixteen fields, rskj reads by the header's height. + let rest = &fields[16..]; + let Trailer { + umm_root, + version, + edges, + base_event, + mining, + compressed, + } = match form { + HeaderForm::Full => Trailer::read(rest, slot6, activations, number, false)?, + HeaderForm::Compressed => Trailer::read(rest, slot6, activations, number, true)?, + HeaderForm::Stored => Trailer::stored(rest, slot6, activations, number)?, + }; - // RSKIP-351: a versioned header puts its version, its RSKIP-144 edges - // and (V2) its base event here, between `ummRoot` and the merged - // mining fields. rskj picks them out by activation height; nothing - // here knows the network, so the version is used as its own marker - // instead: a single byte, 1 or 2, a value no other field in this - // position can take. `ummRoot` is empty or 20 bytes, edges are an - // even-length string, the base event is empty or long, and a - // merged-mining header is 80 bytes. + // Slot 6 holds a versioned header's commitment to its extension where + // it comes compressed, `RLP([version, hash])`, which rskj keeps as it + // came (`extensionData`) and writes, and hashes, as one string. + // Otherwise it holds the logs bloom, of any length: rskj reads + // whatever is there as the bloom, and no rule reads an uncle's. + let mut logs_bloom = Bloom::ZERO; + let mut extension_data = None; let mut version_fields = None; - if let Some(&[version @ (1 | 2)]) = rest.first().map(|element| element.raw_data()) { - rest = &rest[1..]; - // Whatever is left belongs to the merged mining fields, except - // what the version says comes first. rskj emits the edges only - // when the block has any, so their presence is positional: with a - // base event to follow (V2) there is one more item here than - // mining alone accounts for. - let mining = rest.len().min(3); - let extras = rest.len() - mining; - - // Edges come first and are emitted whenever RSKIP144 is active -- - // as an empty string when the block has no sublists -- so they are - // present on every versioned header seen here. The base event - // follows, and on testnet between vetiver900 and #7,800,000 it can - // be missing altogether: rskj accepts a header one element short - // there and supplies an empty base event for it - // (`BlockFactory.isInBaseEventRecoveryWindow`). Reading the - // trailing element as the base event in that case takes the edges - // for it and hashes the header wrong. - // - // Where RSKIP-144 is not active there are no edges, and a V2 - // header's one trailing item is its base event (rskj's regtest - // once had rskip144 = -1, rskip535 = 1). rskj tells the cases apart by - // activation, and so does this. Both are read raw - // (`getRLPRawData`). - let mut edges = None; - let mut base_event = None; - let rskip144 = activations.rskip144.is_some_and(|height| number >= height); - if version >= 2 && extras == 1 && !rskip144 { - base_event = Some(Bytes::copy_from_slice(rest[0].raw_data())); - rest = &rest[1..]; - } else { - if extras >= 1 { - edges = Some(rlp_to_shorts(rest[0].raw_data())); - rest = &rest[1..]; + match version { + Some(1 | 2) if compressed => { + let mut encoded = Vec::new(); + slot6.unwrap_or_default().encode(&mut encoded); + extension_data = Some(Bytes::from(encoded)); + } + _ => { + // rskj's V1 and V2 hash their bloom into their extension's + // hash, and none is null to rskj, which throws on it. + if matches!(version, Some(1 | 2)) && slot6.is_none() { + return Err(refused("a versioned header whose logs bloom is none")); } - if version >= 2 && extras >= 2 { - base_event = Some(Bytes::copy_from_slice(rest[0].raw_data())); - rest = &rest[1..]; + let bloom = slot6.unwrap_or_default(); + if bloom.len() == 256 { + logs_bloom = Bloom::from_slice(bloom); + } else { + kept.logs_bloom = Some(Bytes::copy_from_slice(bloom)); } + // rskj builds a V1 or a V2 from version 1 or 2 in full, a V1 + // ignoring the base event it read, and a `BlockHeaderV0` from + // any other version, in either encoding. + version_fields = version.map(|version| match version { + 1 => HeaderVersionFields { + version, + edges, + base_event: None, + }, + 2 => HeaderVersionFields { + version, + edges, + base_event, + }, + _ => HeaderVersionFields { + version: 0, + edges, + base_event, + }, + }); } - version_fields = Some(HeaderVersionFields { - version, - edges, - base_event, - }); } - // Slot 6 holds the logs bloom, of any length: rskj reads whatever is - // there as the bloom (`BlockHeaderV0.getLogsBloom`, and a versioned - // header's extension when it comes in full), and no rule reads an - // uncle's. Only where RSKIP-351 is active does a header that travels - // compressed hold there instead the commitment to its extension, - // `RLP([version, hash])`, with no version after `ummRoot`. rskj writes - // either back as one string (`RLP.encodeElement`), whatever form it - // came in. - let rskip351 = activations.rskip351.is_some_and(|height| number >= height); - let commitment = rskip351 - && version_fields.is_none() - && crate::types::header_extension::ExtensionCommitment::from_extension_data(slot6) - .is_ok(); - let (logs_bloom, extension_data) = if commitment { - let mut encoded = Vec::new(); - slot6.encode(&mut encoded); - (Bloom::ZERO, Some(Bytes::from(encoded))) - } else if slot6.len() == 256 { - (Bloom::from_slice(slot6), None) - } else { - kept.logs_bloom = Some(Bytes::copy_from_slice(slot6)); - (Bloom::ZERO, None) - }; - // rskj reads the three merged-mining fields together or not at all, // and `canBeDecoded` counts a header's fields exactly: with one or two // of them, or anything after them, it can't read the header. It @@ -791,7 +1039,7 @@ impl Header { // them unless the Bitcoin header is 80 bytes, none being null to it // (`validateBitcoinMergedMiningHeader`), as `ProofOfWorkRule` and // `MergedMiningRule` do again. - let (btc_header, merkle_proof, coinbase) = match rest { + let (btc_header, merkle_proof, coinbase) = match mining { [] => (None, None, None), [header, proof, coinbase] => { let size = crate::validation::merged_mining::BTC_HEADER_SIZE; @@ -986,22 +1234,21 @@ impl Header { if let Some(h) = self.cached_hash { return h; } - let payload = self.encode_payload(!self.rskip92_active(), true); - let mut buffer = Vec::with_capacity(payload.len() + 4); - alloy_rlp::Header { list: true, payload_length: payload.len() }.encode(&mut buffer); - buffer.extend_from_slice(&payload); - alloy_primitives::keccak256(&buffer) + alloy_primitives::keccak256(self.encoded( + HeaderForm::Compressed, + true, + !self.rskip92_active(), + )) } - /// The header as rskj sends a chain's headers to its peers - /// (`BlockHeader.getEncodedCompressed`): every field and all three - /// merged-mining fields, with a versioned header's commitment to its - /// extension in slot 6 and none of its own fields after `ummRoot`. rskj - /// reads a `BlockHeadersResponseMessage`'s headers so - /// (`decodeHeader(.., true)`), and a header taken from one is held so here. - /// A version-0 header has this one form. - pub fn encode_compressed(&self) -> Vec { - let payload = self.encode_payload(true, true); + /// [`Self::encode_payload`] as an RLP list. + fn encoded( + &self, + form: HeaderForm, + with_mining_fields: bool, + with_merkle_proof_and_coinbase: bool, + ) -> Vec { + let payload = self.encode_payload(form, with_mining_fields, with_merkle_proof_and_coinbase); let mut out = Vec::with_capacity(payload.len() + 4); alloy_rlp::Header { list: true, @@ -1012,7 +1259,33 @@ impl Header { out } - /// Decode a header and cache both of its hashes. + /// The header as rskj sends a chain's headers to its peers + /// (`BlockHeader.getEncodedCompressed`): every field and all three + /// merged-mining fields, with a versioned header's commitment to its + /// extension in slot 6 and none of its own fields after `ummRoot`. rskj + /// reads a `BlockHeadersResponseMessage`'s headers so + /// (`decodeHeader(.., true)`), and a header taken from one is held so here. + /// A version-0 header has this one form. + pub fn encode_compressed(&self) -> Vec { + self.encoded(HeaderForm::Compressed, true, true) + } + + /// The header as this node's store writes it, which [`HeaderForm::Stored`] + /// reads back as this header. One with RSKIP-351's fields is in a layout + /// of this node's own, marked where rskj's full encoding has a version: + /// a versioned one with its version after the mark, then its edges and + /// its base event, each an empty list where it has none; one held + /// compressed, compressed, with the mark alone. Any other is as rskj + /// writes it in full. rskj's full encoding of a versioned header doesn't + /// always read back as that header: one of version 0 writes no version, + /// and rskj then reads a version from its edges, and a V1 writes no base + /// event, so where RSKIP-535 is active rskj counts a field too few. + pub fn encode_stored(&self) -> Vec { + self.encoded(HeaderForm::Stored, true, true) + } + + /// Decode a header in rskj's full encoding, as blocks carry headers, and + /// cache both of its hashes. /// /// rskj hashes a header as it writes it back from what it read /// (`getHash` over `getEncodedForHash`), not as it was sent: a field @@ -1022,24 +1295,32 @@ impl Header { /// /// Nor does rskj hash the form it sends. It hashes the compressed form, /// which leaves out the merged-mining proof and coinbase (RSKIP-92) and, - /// for a versioned (RSKIP-351) header, the version, edges and base event - /// that follow `ummRoot` on the wire, whose commitment takes the bloom's - /// place in slot 6. Hashing a versioned header as sent gave every - /// testnet header from reed810 on a hash no other node agrees with: no - /// header named its parent, and the snapshot sync refused every chain it - /// was offered. + /// for a header of version 1 or 2 (RSKIP-351), the version, edges and + /// base event that follow `ummRoot` in its full encoding, whose + /// commitment takes the bloom's place in slot 6. Hashing such a header as + /// a block carries it gave every testnet header from reed810 on a hash + /// no other node agrees with: no header named its parent, and the + /// snapshot sync refused every chain it was offered. pub fn decode_with_hash(buf: &mut &[u8]) -> alloy_rlp::Result { Self::decode_with_hash_and_activations(buf, crate::config::ActivationHeights::current()) } - /// [`Self::decode_with_hash`] under explicit activation heights, which - /// decide what a lone item after a V2 version byte is, as they do for - /// [`Self::decode_with_activations`]. + /// [`Self::decode_with_hash`] under explicit activation heights. pub fn decode_with_hash_and_activations( buf: &mut &[u8], activations: &crate::config::ActivationHeights, ) -> alloy_rlp::Result { - let mut header = Self::decode_with_activations(buf, activations)?; + Self::decode_with_hash_in(buf, activations, HeaderForm::Full) + } + + /// [`Self::decode_with_hash`] in `form`, under explicit activation + /// heights. + pub fn decode_with_hash_in( + buf: &mut &[u8], + activations: &crate::config::ActivationHeights, + form: HeaderForm, + ) -> alloy_rlp::Result { + let mut header = Self::decode_in(buf, activations, form)?; header.cached_hash = Some(header.hash()); header.cached_hash_for_merged_mining = Some(header.hash_for_merged_mining()); Ok(header) @@ -1056,21 +1337,13 @@ impl Header { let is_umm_block = self.umm_root.as_ref().is_some_and(|u| !u.is_empty()); - // The same encoder the block hash uses. Encoding difficulty, gas limit + // rskj `getBaseHashForMergedMining`: `getEncoded(false, false, true)`, + // the same encoder the block hash uses. Encoding difficulty, gas limit // and minimum gas price any other way produces a hash no other node // agrees with -- and since only a header this node built ever reaches // this path, nothing that merely replays the chain would notice. - let mut payload = Vec::new(); - self.encode_base_fields(&mut payload, true); - if let Some(umm) = &self.umm_root { - umm.encode(&mut payload); - } - - let mut out = Vec::new(); - alloy_rlp::Header { list: true, payload_length: payload.len() }.encode(&mut out); - out.extend_from_slice(&payload); - - let base_hash = alloy_primitives::keccak256(&out); + let base_hash = + alloy_primitives::keccak256(self.encoded(HeaderForm::Compressed, false, false)); if is_umm_block { let umm_bytes = self.umm_root.as_ref().unwrap(); @@ -1090,7 +1363,9 @@ mod tests { use alloy_primitives::{Address, B256, U256, Bytes, keccak256, b256, address, hex}; use alloy_rlp::{Decodable, Encodable}; - fn standard_test_header() -> Header { + /// A mainnet header from below RSKIPUMM, as the heights `Header::decode` + /// reads under say it is: no UMM root. + pub(super) fn standard_test_header() -> Header { Header { parent_hash: B256::repeat_byte(0x11), ommers_hash: B256::repeat_byte(0x22), @@ -1109,7 +1384,7 @@ mod tests { paid_fees: U256::from(100), minimum_gas_price: U256::from(1), uncle_count: 0, - umm_root: Some(Bytes::from("umm")), + umm_root: None, version_fields: None, bitcoin_merged_mining_header: Some(Bytes::from("btc_header")), bitcoin_merged_mining_merkle_proof: Some(Bytes::from("proof")), @@ -1140,7 +1415,7 @@ mod tests { } /// The items of an RLP list, each with its own header, as written. - fn items_of(encoded: &[u8]) -> Vec> { + pub(super) fn items_of(encoded: &[u8]) -> Vec> { let mut buf = encoded; let list = alloy_rlp::Header::decode(&mut buf).unwrap(); assert!(list.list); @@ -1157,7 +1432,7 @@ mod tests { } /// An RLP list of items already written. - fn list_of(items: &[Vec]) -> Vec { + pub(super) fn list_of(items: &[Vec]) -> Vec { let payload = items.concat(); let mut list = Vec::new(); alloy_rlp::Header { @@ -1175,12 +1450,16 @@ mod tests { #[test] fn rskj_reads_no_header_past_a_java_long_or_int() { let decodes = |change: &dyn Fn(&mut Header)| { - // From RSKIP-98 on, a Bitcoin header of 80 bytes. + // From RSKIP-98 on, a Bitcoin header of 80 bytes, and from + // RSKIPUMM a UMM root. let mut header = Header { bitcoin_merged_mining_header: Some(Bytes::from(vec![0; 80])), ..standard_test_header() }; change(&mut header); + header.umm_root = crate::config::ActivationHeights::mainnet() + .umm_active(header.number) + .then(Bytes::new); let mut bytes = Vec::new(); header.encode(&mut bytes); Header::decode(&mut bytes.as_slice()).is_ok() @@ -1203,11 +1482,7 @@ mod tests { #[test] fn rskj_reads_the_merged_mining_fields_all_three_or_none() { let mut bytes = Vec::new(); - Header { - umm_root: None, - ..standard_test_header() - } - .encode(&mut bytes); + standard_test_header().encode(&mut bytes); let items = items_of(&bytes); assert_eq!( items.len(), @@ -1215,23 +1490,83 @@ mod tests { "sixteen fields and three for merged mining" ); // The sixteen fields, then `extra` items from the three. - let read = |extra: usize| { + let fields = |extra: usize| { let mut fields = items[..16].to_vec(); fields.extend(items[16..].iter().cycle().take(extra).cloned()); - Header::decode(&mut list_of(&fields).as_slice()) + list_of(&fields) }; + let read = |extra: usize| Header::decode(&mut fields(extra).as_slice()); assert!(read(0).is_ok(), "no merged-mining fields"); assert!(read(1).is_err()); assert!(read(2).is_err()); assert!(read(3).is_ok(), "the three merged-mining fields"); - // Four are a UMM root and the three: which a header may carry is its - // height's to say, in `HeaderLayoutRule`. - assert!(read(4).is_ok_and(|header| header.umm_root.is_some())); + // Four are a UMM root and the three, which rskj reads only from + // RSKIPUMM on. + assert!(read(4).is_err(), "no UMM root below RSKIPUMM"); + let umm = crate::config::ActivationHeights { + rskip_umm: Some(0), + ..crate::config::ActivationHeights::mainnet() + }; + assert!( + Header::decode_with_activations(&mut fields(4).as_slice(), &umm) + .is_ok_and(|header| header.umm_root.is_some()) + ); assert!(read(5).is_err(), "a field after the merged-mining fields"); } + /// rskj reads a header's fields by its height (`BlockFactory.canBeDecoded`): + /// a UMM root from RSKIPUMM on and only then -- papyrus200 on mainnet and + /// testnet, RSKIPUMM's own height where the two part, 1 on regtest -- and + /// a version only from RSKIP-351 on, which is none of mainnet and + /// testnet's from reed810. A header with any other fields is one rskj + /// can't read. + #[test] + fn a_header_carries_the_fields_rskj_reads_at_its_height() { + use crate::config::ActivationHeights; + let reads = |heights: &ActivationHeights, number: u64, umm: bool, version: Option| { + let header = Header { + number, + umm_root: umm.then(Bytes::new), + version_fields: version.map(|version| HeaderVersionFields { + version, + edges: Some(Vec::new()), + base_event: None, + }), + bitcoin_merged_mining_header: Some(Bytes::from(vec![0; 80])), + ..standard_test_header() + }; + let mut bytes = Vec::new(); + header.encode(&mut bytes); + Header::decode_with_activations(&mut bytes.as_slice(), heights).is_ok() + }; + let mainnet = ActivationHeights::mainnet(); + let papyrus = mainnet.papyrus200; + assert!(reads(&mainnet, papyrus - 1, false, None)); + assert!(!reads(&mainnet, papyrus - 1, true, None)); + assert!(reads(&mainnet, papyrus, true, None)); + assert!(!reads(&mainnet, papyrus, false, None)); + let apart = ActivationHeights { + rskip_umm: Some(papyrus + 10), + ..mainnet.clone() + }; + assert!(reads(&apart, papyrus, false, None)); + assert!(!reads(&apart, papyrus + 10, false, None)); + assert!(reads(&apart, papyrus + 10, true, None)); + let regtest = ActivationHeights::regtest(); + assert!(reads(®test, 0, false, None)); + assert!(!reads(®test, 0, true, None)); + // A version where RSKIP-351 is inactive, as on all of mainnet, is a + // field too many. + assert!(!reads(&mainnet, 9_400_000, true, Some(1))); + let testnet = ActivationHeights::testnet(); + let reed810 = 7_139_600; + assert!(!reads(&testnet, reed810 - 1, true, Some(1))); + assert!(reads(&testnet, reed810, true, Some(1))); + assert!(!reads(&testnet, reed810, true, None)); + } + /// An RLP string, as rskj's `RLP.encodeElement` writes one. - fn element(bytes: &[u8]) -> Vec { + pub(super) fn element(bytes: &[u8]) -> Vec { let mut out = Vec::new(); bytes.encode(&mut out); out @@ -1528,10 +1863,10 @@ mod tests { assert!(header.has_logs_bloom(&header.logs_bloom)); } - /// Only where RSKIP-351 is active does slot 6 of a header without its - /// version, as one sent compressed, hold the commitment to its extension. - /// Before it, rskj reads the same bytes as the bloom. Either way they go - /// back as they came, and hash the same. + /// Only where RSKIP-351 is active does slot 6 of a header read compressed + /// hold the commitment to its extension. Before it, rskj reads the same + /// bytes as the bloom, and has one encoding. Either way they go back as + /// they came, and hash the same. #[test] fn a_commitment_in_the_blooms_slot_is_one_only_from_rskip351() { use crate::config::ActivationHeights; @@ -1539,8 +1874,8 @@ mod tests { let mut items = standard_items(); items[6] = element(&commitment); let bytes = list_of(&items); - let read = |activations: &ActivationHeights| { - Header::decode_with_hash_and_activations(&mut bytes.as_slice(), activations).unwrap() + let read = |activations: &ActivationHeights, form| { + Header::decode_with_hash_in(&mut bytes.as_slice(), activations, form) }; // Mainnet's heights, and the same with RSKIP-351 from block 1, as // rskj's regtest has it. The header is #42. @@ -1550,13 +1885,16 @@ mod tests { ..ActivationHeights::mainnet() }; - let bloom = read(&before); + let bloom = read(&before, HeaderForm::Compressed).unwrap(); + assert_eq!(read(&before, HeaderForm::Full).unwrap(), bloom); assert_eq!(bloom.extension_data, None); assert_eq!( bloom.kept().unwrap().logs_bloom, Some(Bytes::from(commitment.clone())) ); - let committed = read(&from); + // In full, from RSKIP-351 on, the header lacks its version. + assert!(read(&from, HeaderForm::Full).is_err()); + let committed = read(&from, HeaderForm::Compressed).unwrap(); assert_eq!( committed.extension_data, Some(Bytes::from(element(&commitment))) @@ -1612,15 +1950,14 @@ mod tests { let mut again = Vec::new(); decoded.encode(&mut again); assert_eq!(again, bytes, "written back as it came"); - // Hashed compressed: the commitment in slot 6, and no version after - // the UMM root. + // Hashed compressed: the commitment in slot 6, and no version. let extension = BlockHeaderExtension::V1 { logs_bloom: shaped, edges: None, }; let mut hashed = items.clone(); hashed[6] = element(&extension.create_extension_data()); - hashed.remove(17); + hashed.remove(16); assert_eq!(decoded.hash(), keccak256(list_of(&hashed))); } @@ -1746,7 +2083,7 @@ mod tests { #[test] fn three_empty_merged_mining_fields_are_none() { let mut items = standard_items(); - items.truncate(17); + items.truncate(16); let none = list_of(&items); items.extend([element(&[]), element(&[]), element(&[])]); let decoded = Header::decode_with_hash(&mut list_of(&items).as_slice()).unwrap(); @@ -2180,7 +2517,11 @@ mod tests { // The network's decoder reaches it from the bytes as sent, edges and // all, and the merged-mining hash the coinbase commits to. - let decoded = Header::decode_with_hash(&mut raw.as_slice()).unwrap(); + let decoded = Header::decode_with_hash_and_activations( + &mut raw.as_slice(), + &crate::config::ActivationHeights::testnet(), + ) + .unwrap(); assert_eq!(decoded.hash(), alloy_primitives::keccak256(&buf)); let coinbase = decoded .bitcoin_merged_mining_coinbase_transaction @@ -2334,13 +2675,13 @@ mod versioned_wire_header_tests { assert_eq!(wire(&full), hex(V2_FULL)); // Read back as rskj's peers send it: the same hashes, and held // compressed, no other form. - let read = from_the_network(V2_COMPRESSED); + let read = from_peers(V2_COMPRESSED); assert_eq!(read.hash(), hash); assert_eq!(read.hash_for_merged_mining(), full.hash_for_merged_mining()); assert_eq!(read.encode_compressed(), hex(V2_COMPRESSED)); assert_eq!(wire(&read), hex(V2_COMPRESSED)); // So for one that rskj's peers sent. - let peers = from_the_network(V2_FROM_PEERS); + let peers = from_peers(V2_FROM_PEERS); let peers_hash: B256 = "0xf343f40fdd2152318c13fda9abdc80bb5e8a57a9166e831dfbdfb448f7821bef" .parse() .unwrap(); @@ -2373,8 +2714,7 @@ mod versioned_wire_header_tests { let extension = full .extension() .expect("a versioned header's own extension"); - let compressed = - from_the_network(&alloy_primitives::hex::encode(full.encode_compressed())); + let compressed = from_peers(&alloy_primitives::hex::encode(full.encode_compressed())); assert!(compressed.extension_data.is_some()); assert_eq!(compressed.extension(), None, "only the commitment"); let mut both = compressed.clone(); @@ -2445,7 +2785,7 @@ mod versioned_wire_header_tests { edges: None, base_event: Some(Vec::new()), }; - let mut committed = from_the_network(V2_COMPRESSED); + let mut committed = from_peers(V2_COMPRESSED); let mut slot = Vec::new(); short.create_extension_data().as_slice().encode(&mut slot); committed.extension_data = Some(Bytes::from(slot)); @@ -2511,14 +2851,14 @@ mod versioned_wire_header_tests { ); } - /// The store re-encodes every header it writes, so anything the wire form - /// cannot round-trip is lost from the database rather than from a single - /// message. The first attempt at this encoded the edges with `Vec`, - /// which alloy emits as a list of single bytes, and the result would not - /// decode at all. + /// A header goes back out as rskj wrote it, and into the store in this + /// node's own layout, and either reads back as the same header. The first + /// attempt at this encoded the edges with `Vec`, which alloy emits as + /// a list of single bytes, and the result would not decode at all. #[test] fn a_versioned_header_survives_being_re_encoded() { - for wire in [V1_WIRE, V2_WIRE] { + let testnet = crate::config::ActivationHeights::testnet(); + for wire in [V1_WIRE, V2_WIRE, V2_FULL] { let original = decode(wire); let mut buf = Vec::new(); original.encode(&mut buf); @@ -2527,17 +2867,13 @@ mod versioned_wire_header_tests { wire, "re-encoding must reproduce rskj's own bytes" ); - - let mut slice = buf.as_slice(); - let mut again = Header::decode(&mut slice).unwrap(); - again.cached_hash = None; - assert_eq!(again.hash(), original.hash()); - assert_eq!(again.version_fields, original.version_fields); - assert_eq!(again.logs_bloom, original.logs_bloom); - assert_eq!( - again.bitcoin_merged_mining_header, - original.bitcoin_merged_mining_header - ); + for (bytes, form) in [ + (buf, HeaderForm::Full), + (original.encode_stored(), HeaderForm::Stored), + ] { + let again = Header::decode_in(&mut bytes.as_slice(), &testnet, form).unwrap(); + assert_eq!(again, original, "{form:?}"); + } } } @@ -2627,7 +2963,11 @@ mod versioned_wire_header_tests { let v = h.version_fields.as_ref().unwrap(); assert_eq!(v.version, 2); assert_eq!(v.edges.as_deref(), Some(&[][..]), "the edges field is present and empty"); - assert_eq!(v.base_event, None, "no base event element on the wire"); + assert_eq!( + v.base_event, + Some(Bytes::new()), + "no base event element on the wire, and rskj gives it an empty one" + ); assert_eq!( h.hash(), "0x119296ea9d8d7efa82550bfb447ae9441ea75f9537f47917f1ca38e0b8982c05" @@ -2636,46 +2976,62 @@ mod versioned_wire_header_tests { ); } - /// And the short form must round-trip as the short form: re-emitting an - /// empty base event would add an element rskj never wrote. + /// rskj gives the short form an empty base event and writes that back: + /// the header goes out one element longer, as rskj's nodes send it, and + /// reads back as the same header, with the same hash. #[test] - fn the_short_v2_form_is_re_encoded_as_the_short_form() { + fn the_short_v2_form_goes_back_out_as_rskj_writes_it() { + use super::tests::{items_of, list_of}; let original = decode(V2_SHORT_WIRE); + let mut items = items_of(&alloy_primitives::hex::decode(V2_SHORT_WIRE).unwrap()); + assert_eq!( + (items.len(), &items[18][..]), + (22, &[0x80][..]), + "empty edges, then mining" + ); + items.insert(19, vec![0x80]); let mut buf = Vec::new(); original.encode(&mut buf); - assert_eq!(alloy_primitives::hex::encode(&buf), V2_SHORT_WIRE); + assert_eq!(buf, list_of(&items)); + let testnet = crate::config::ActivationHeights::testnet(); + let again = Header::decode_with_activations(&mut buf.as_slice(), &testnet).unwrap(); + assert_eq!(again, original); } - /// The schedule perf-lab's mainnet seed runs under puts RSKIP-144 at - /// #9,002,504. Above it, a V2 header with one trailing item carries - /// edges (rskj in the base-event recovery window); under mainnet's own - /// schedule the same bytes carry a base event. The override reaches the - /// decoder through `rskip144`, the way regtest's does. + /// The schedule perf-lab's mainnet seed runs under puts reed810, and with + /// it RSKIP-144 and RSKIP-351, at #9,002,504. The override reaches the + /// decoder: above it a header carries its version and edges, as rskj + /// reads one there, and under mainnet's own heights, with neither + /// active, rskj reads no such header. #[test] - fn a_reed810_override_turns_a_lone_trailing_item_into_edges() { + fn a_reed810_override_moves_where_headers_carry_their_version() { let mut h = decode(V2_SHORT_WIRE); h.number = 9_003_580; - h.cached_hash = None; - let v = h.version_fields.as_mut().unwrap(); - assert_eq!((v.version, v.base_event.as_ref()), (2, None)); - v.edges = Some(vec![]); + // Mainnet has no RSKIP-535, so no base event. + h.version_fields.as_mut().unwrap().base_event = None; let mut buf = Vec::new(); h.encode(&mut buf); let mut seed = crate::config::ActivationHeights::mainnet(); seed.set_rskj_hardfork("reed810", Some(9_002_504)); - assert_eq!(seed.rskip144, Some(9_002_504)); + assert_eq!( + (seed.rskip144, seed.rskip351), + (Some(9_002_504), Some(9_002_504)) + ); let under_seed = Header::decode_with_activations(&mut buf.as_slice(), &seed).unwrap(); - let v = under_seed.version_fields.as_ref().unwrap(); - assert_eq!(v.edges.as_deref(), Some(&[][..])); - assert_eq!(v.base_event, None); + assert_eq!(under_seed, h); + assert_eq!( + under_seed.version_fields, + Some(HeaderVersionFields { + version: 2, + edges: Some(vec![]), + base_event: None + }) + ); let mainnet = crate::config::ActivationHeights::mainnet(); - assert_eq!(mainnet.rskip144, None); - let under_mainnet = Header::decode_with_activations(&mut buf.as_slice(), &mainnet).unwrap(); - let v = under_mainnet.version_fields.as_ref().unwrap(); - assert_eq!(v.edges, None); - assert_eq!(v.base_event.as_ref().map(|b| b.len()), Some(0)); + assert_eq!((mainnet.rskip144, mainnet.rskip351), (None, None)); + assert!(Header::decode_with_activations(&mut buf.as_slice(), &mainnet).is_err()); } /// A version-0 header must stay untouched by any of this: mainnet is @@ -2693,17 +3049,29 @@ mod versioned_wire_header_tests { assert_eq!(again.version_fields, None); } - /// A header decoded as the network decodes one: `decode_with_hash`, over - /// the whole of `hex`, under testnet's heights, whose headers these are. - fn from_the_network(hex: &str) -> Header { + /// A header decoded as the network decodes one in `form`: + /// `decode_with_hash_in`, over the whole of `hex`, under testnet's + /// heights, whose headers these are. + fn from_the_network_in(hex: &str, form: HeaderForm) -> Header { let bytes = alloy_primitives::hex::decode(hex).unwrap(); let mut rest = bytes.as_slice(); let testnet = crate::config::ActivationHeights::testnet(); - let h = Header::decode_with_hash_and_activations(&mut rest, &testnet).unwrap(); + let h = Header::decode_with_hash_in(&mut rest, &testnet, form).unwrap(); assert!(rest.is_empty(), "bytes after the header"); h } + /// A header as blocks carry one: in full. + fn from_the_network(hex: &str) -> Header { + from_the_network_in(hex, HeaderForm::Full) + } + + /// A header as rskj's peers send a chain's headers + /// (`BlockHeadersResponse`): compressed. + fn from_peers(hex: &str) -> Header { + from_the_network_in(hex, HeaderForm::Compressed) + } + /// What the miner committed to in the coinbase after `RSKBLOCK:`: the /// first 20 bytes of the merged-mining hash. fn committed(h: &Header) -> Vec { @@ -2751,13 +3119,17 @@ mod versioned_wire_header_tests { } } - /// Every layout the decoder reads, built here and read back as the - /// network reads it: what `decode_with_hash` caches from the items sent - /// is what this node derives by encoding. The captured headers above tie - /// both paths to rskj; this ties down where `ummRoot`, a versioned - /// header's own fields and the mining fields sit, across layouts no - /// captured header has: a UMM root, edges, a base event, no mining - /// fields, a header from before RSKIP-92. + /// Every layout the decoder reads, built here and read back under the + /// heights where rskj reads it, in rskj's full encoding and in the + /// store's own: each reads back as the header it was, so what + /// `decode_with_hash` caches is what this node derives by encoding. The + /// captured headers above tie both to rskj; this ties down where + /// `ummRoot`, a versioned header's own fields and the mining fields sit, + /// across layouts no captured header has: a UMM root or none, edges, a + /// base event, a version-0 header from RSKIP-351 on, no mining fields, a + /// header from before RSKIP-92. rskj can't read a version-0 header's + /// full encoding back (`a_version_0_header_from_rskip351_is_rskjs_v0`), + /// so that one goes through the store's form alone. #[test] fn every_layout_caches_the_hashes_it_would_encode_to() { let real = decode(V1_WIRE); @@ -2769,40 +3141,37 @@ mod versioned_wire_header_tests { base_event, }) }; - // RSKIP-144 decides what a lone item after a V2 version byte is: the - // edges where it is active, the base event where it is not. Each - // layout is read under heights that make it one rskj reads. - let with_144 = crate::config::ActivationHeights { - rskip144: Some(0), - ..crate::config::ActivationHeights::testnet() - }; - let without_144 = crate::config::ActivationHeights { - rskip144: None, - ..crate::config::ActivationHeights::testnet() - }; + let base_event = || Some(Bytes::from_static(b"base event")); let versions = [ - (None, &with_144), - (fields(1, None, None), &with_144), - (fields(1, Some(vec![1, 3]), None), &with_144), - (fields(2, Some(vec![]), Some(Bytes::new())), &with_144), - (fields(2, Some(vec![2]), None), &with_144), - (fields(2, Some(vec![2]), Some(Bytes::from_static(b"base event"))), &with_144), - (fields(2, None, Some(Bytes::from_static(b"base event"))), &without_144), + None, + fields(0, None, None), + fields(0, Some(vec![1, 3]), base_event()), + fields(1, None, None), + fields(1, Some(vec![1, 3]), None), + fields(2, None, None), + fields(2, Some(vec![]), Some(Bytes::new())), + fields(2, Some(vec![2]), None), + fields(2, Some(vec![2]), base_event()), + fields(2, None, base_event()), ]; let mut layouts = 0; for number in [100, real.number] { for umm_root in &umm { - for (version_fields, activations) in &versions { + for version_fields in &versions { + let has = |field: fn(&HeaderVersionFields) -> bool| { + version_fields.as_ref().is_some_and(field) + }; + // Each field from the height its RSKIP is active. + let from = |active: bool| active.then_some(0); + let heights = crate::config::ActivationHeights { + rskip_umm: from(umm_root.is_some()), + rskip351: from(version_fields.is_some()), + rskip144: from(has(|v| v.edges.is_some())), + rskip535: from(has(|v| v.base_event.is_some())), + base_event_recovery_below: None, + ..crate::config::ActivationHeights::testnet() + }; for mining in [true, false] { - // The decoder tells `ummRoot` from the mining fields - // by count, so it reads one only beside them, and - // the version fields only after one. - if (umm_root.is_some() || version_fields.is_some()) && !mining { - continue; - } - if version_fields.is_some() && umm_root.is_none() { - continue; - } let mut header = Header { number, umm_root: umm_root.clone(), @@ -2814,30 +3183,36 @@ mod versioned_wire_header_tests { header.bitcoin_merged_mining_merkle_proof = None; header.bitcoin_merged_mining_coinbase_transaction = None; } - let mut wire = Vec::new(); - header.encode(&mut wire); - assert_eq!( - Header::decode_with_activations(&mut wire.as_slice(), activations).unwrap(), - header, - "the layout reads back" - ); - let cached = - Header::decode_with_hash_and_activations(&mut wire.as_slice(), activations) - .unwrap(); - assert_eq!(cached.cached_hash, Some(header.hash()), "{header:?}"); - assert_eq!( - cached.cached_hash_for_merged_mining, - Some(header.hash_for_merged_mining()), - "{header:?}" - ); + let mut full = Vec::new(); + header.encode(&mut full); + let forms = [ + (full, HeaderForm::Full), + (header.encode_stored(), HeaderForm::Stored), + ]; + let readable = if has(|v| v.version == 0) { + &forms[1..] + } else { + &forms[..] + }; + for (bytes, form) in readable { + let read = Header::decode_in(&mut bytes.as_slice(), &heights, *form); + assert_eq!(read.unwrap(), header, "{form:?}"); + let cached = + Header::decode_with_hash_in(&mut bytes.as_slice(), &heights, *form) + .unwrap(); + assert_eq!(cached.cached_hash, Some(header.hash()), "{header:?}"); + assert_eq!( + cached.cached_hash_for_merged_mining, + Some(header.hash_for_merged_mining()), + "{header:?}" + ); + } layouts += 1; } } } } - // Per height: four version-0 layouts, and six versioned ones on - // each of two UMM roots. - assert_eq!(layouts, 2 * (4 + 6 * 2)); + assert_eq!(layouts, 2 * 3 * 10 * 2); } /// No variation on a versioned header's bytes, as testnet's peers send @@ -2855,7 +3230,7 @@ mod versioned_wire_header_tests { V2_SHORT_WIRE, RSKJ_REGTEST_V2_FULL, ]; - let compressed = from_the_network(V2_COMPRESSED); + let compressed = from_peers(V2_COMPRESSED); for (i, seed) in seeds.iter().enumerate() { let seed = alloy_primitives::hex::decode(seed).unwrap(); crate::mutate::decode_and_check_header(&seed); @@ -2879,4 +3254,355 @@ mod versioned_wire_header_tests { } } } + + /// From RSKIP-351 on, rskj reads a version other than 1 or 2 as a + /// `BlockHeaderV0`'s: the bloom in slot 6, and the edges and base event + /// read, hashed and written in either encoding with no version. Its full + /// encoding is then one rskj can't read back, a field short, so the store + /// keeps it in its own layout. + #[test] + fn a_version_0_header_from_rskip351_is_rskjs_v0() { + use super::tests::{items_of, list_of}; + let testnet = crate::config::ActivationHeights::testnet(); + let full = from_the_network(V2_FULL); + let v2 = full.version_fields.clone().unwrap(); + let items = items_of(&alloy_primitives::hex::decode(V2_FULL).unwrap()); + assert_eq!((items.len(), &items[17][..]), (23, &[0x02][..])); + for version in [vec![0x00], vec![0x03], vec![0x81, 0xff]] { + let mut sent = items.clone(); + sent[17] = version; + let bytes = list_of(&sent); + let v0 = Header::decode_with_activations(&mut bytes.as_slice(), &testnet).unwrap(); + assert_eq!( + v0.version_fields, + Some(HeaderVersionFields { + version: 0, + ..v2.clone() + }) + ); + assert_eq!(v0.logs_bloom, full.logs_bloom); + // Either encoding: the fields as sent, but for the version. + let mut written = sent; + written.remove(17); + let mut out = Vec::new(); + v0.encode(&mut out); + assert_eq!(out, list_of(&written)); + assert_eq!(v0.encode_compressed(), out); + // Hashed compressed, without the merged-mining proof and coinbase. + written.truncate(written.len() - 2); + assert_eq!(v0.hash(), alloy_primitives::keccak256(list_of(&written))); + assert!(Header::decode_with_activations(&mut out.as_slice(), &testnet).is_err()); + let stored = v0.encode_stored(); + let again = Header::decode_in(&mut stored.as_slice(), &testnet, HeaderForm::Stored); + assert_eq!(again.unwrap(), v0); + } + } + + /// rskj reads a base event wherever RSKIP-535 is active, and a V1 drops + /// the one it read: it hashes and writes none. Its full encoding is then + /// a field short of what rskj reads there, so the store keeps it in its + /// own layout. + #[test] + fn a_v1_header_drops_the_base_event_rskj_reads() { + use super::tests::{element, items_of, list_of}; + let with_535 = crate::config::ActivationHeights { + rskip535: Some(0), + base_event_recovery_below: None, + ..crate::config::ActivationHeights::testnet() + }; + let wire = alloy_primitives::hex::decode(V1_WIRE).unwrap(); + let mut items = items_of(&wire); + let mining = items.len() - 3; + items.insert(mining, element(b"base event")); + let read = + Header::decode_with_activations(&mut list_of(&items).as_slice(), &with_535).unwrap(); + assert_eq!(read, decode(V1_WIRE)); + let mut out = Vec::new(); + read.encode(&mut out); + assert_eq!(out, wire); + assert!(Header::decode_with_activations(&mut out.as_slice(), &with_535).is_err()); + let stored = read.encode_stored(); + let again = Header::decode_in(&mut stored.as_slice(), &with_535, HeaderForm::Stored); + assert_eq!(again.unwrap(), read); + } + + /// rskj takes the version of a header read compressed from slot 6: the + /// first byte of the first item of the one list there + /// (`RLP.decodeList(extensionData).get(0).getRLPData()[0]`). Where that + /// finds no byte, it can't read the header. A version other than 1 or 2 + /// is a `BlockHeaderV0`'s, which holds slot 6 as its bloom. + #[test] + fn a_compressed_headers_version_is_the_first_byte_slot_6_names() { + use super::tests::{element, items_of, list_of}; + let testnet = crate::config::ActivationHeights::testnet(); + let items = items_of(&alloy_primitives::hex::decode(V2_COMPRESSED).unwrap()); + let with = |slot6: &[u8]| { + let mut sent = items.clone(); + sent[6] = element(slot6); + list_of(&sent) + }; + let read = |slot6: &[u8]| { + Header::decode_in( + &mut with(slot6).as_slice(), + &testnet, + HeaderForm::Compressed, + ) + }; + for (slot6, what) in [ + (&[][..], "none"), + (&[0; 256][..], "a bloom"), + (&[0x02][..], "no list"), + (&[0xc0][..], "an empty list"), + (&[0xc1, 0x80][..], "a list whose first item is none"), + (&[0xc1, 0x02, 0xc0][..], "a list, then more"), + (&[0xc2, 0x02][..], "a list cut short"), + ] { + assert!(read(slot6).is_err(), "{what}"); + } + // Version 2, the first byte of a longer item. + let longer = read(&[0xc3, 0x82, 0x02, 0x99]).unwrap(); + assert!(longer.extension_data.is_some() && longer.version_fields.is_none()); + // Version 2, the first item's, of two. + let first = read(&[0xc2, 0x02, 0x03]).unwrap(); + assert!(first.extension_data.is_some() && first.version_fields.is_none()); + // Any other version is a V0's. + let v0 = read(&[0xc1, 0x03]).unwrap(); + assert_eq!(v0.extension_data, None); + assert_eq!( + v0.version_fields, + Some(HeaderVersionFields { + version: 0, + edges: None, + base_event: None + }) + ); + assert_eq!( + v0.kept().unwrap().logs_bloom, + Some(Bytes::from_static(&[0xc1, 0x03])) + ); + assert_eq!(v0.encode_compressed(), with(&[0xc1, 0x03])); + } + + /// rskj counts a header's fields exactly at its height (`canBeDecoded`), + /// in either encoding. On testnet below #7,800,000 it also reads a full + /// header one field short, and gives it an empty base event + /// (`isInBaseEventRecoveryWindow`); never a compressed one. + #[test] + fn rskj_counts_a_headers_fields_at_its_height() { + use super::tests::{element, items_of, list_of}; + let testnet = crate::config::ActivationHeights::testnet(); + let read = |items: &[Vec], form| { + Header::decode_in(&mut list_of(items).as_slice(), &testnet, form) + }; + let at = |items: &[Vec], number: u64| { + let mut items = items.to_vec(); + items[8] = element(&number.to_be_bytes()[number.leading_zeros() as usize / 8..]); + items + }; + // The short form, a field short, to the window's end. + let short = items_of(&alloy_primitives::hex::decode(V2_SHORT_WIRE).unwrap()); + assert!(read(&at(&short, 7_799_999), HeaderForm::Full).is_ok()); + assert!(read(&at(&short, 7_800_000), HeaderForm::Full).is_err()); + let mut two_short = short.clone(); + two_short.remove(18); + assert!(read(&two_short, HeaderForm::Full).is_err()); + let mut whole = short.clone(); + whole.insert(19, element(&[])); + assert!(read(&at(&whole, 7_800_000), HeaderForm::Full).is_ok()); + whole.insert(19, element(&[])); + assert!(read(&whole, HeaderForm::Full).is_err(), "a field over"); + // A compressed header is counted exactly, in the window too. + let compressed = items_of(&alloy_primitives::hex::decode(V2_COMPRESSED).unwrap()); + let compressed = at(&compressed, 7_610_038); + assert!(read(&compressed, HeaderForm::Compressed).is_ok()); + let mut without_umm = compressed.clone(); + without_umm.remove(16); + assert!(read(&without_umm, HeaderForm::Compressed).is_err()); + } + + /// rskj hashes a V1's or a V2's bloom into its extension's hash, and + /// throws on none. A `BlockHeaderV0` has no extension, and takes none as + /// its bloom. + #[test] + fn a_versioned_header_needs_a_bloom_but_a_v0_does_not() { + use super::tests::{element, items_of, list_of}; + let testnet = crate::config::ActivationHeights::testnet(); + let read = |items: &[Vec]| { + Header::decode_with_activations(&mut list_of(items).as_slice(), &testnet) + }; + let mut items = items_of(&alloy_primitives::hex::decode(V2_FULL).unwrap()); + items[6] = element(&[]); + assert!(read(&items).is_err(), "a V2"); + items[17] = element(&[1]); + assert!(read(&items).is_err(), "a V1"); + items[17] = element(&[0]); + let v0 = read(&items).unwrap(); + assert_eq!(v0.kept().unwrap().logs_bloom, Some(Bytes::new())); + } + + /// The store's own form reads back every header as it was held. One held + /// compressed is marked as one, so even a Bitcoin header of one byte, + /// which rskj's full encoding would have as a version, reads back as the + /// merged-mining field it is. What the store wrote before it had this + /// layout reads back too: rskj's full encoding, and a header held + /// compressed, unmarked. + #[test] + fn the_store_reads_back_every_header_as_it_was_held() { + use super::tests::{element, items_of, list_of}; + use crate::config::ActivationHeights; + let read = |bytes: &[u8], activations: &ActivationHeights, form| { + Header::decode_in(&mut &bytes[..], activations, form).unwrap() + }; + let testnet = ActivationHeights::testnet(); + for hex in [V2_COMPRESSED, V2_FROM_PEERS] { + let bytes = alloy_primitives::hex::decode(hex).unwrap(); + let held = read(&bytes, &testnet, HeaderForm::Compressed); + assert!(held.extension_data.is_some()); + assert_eq!( + read(&held.encode_stored(), &testnet, HeaderForm::Stored), + held + ); + assert_eq!(read(&bytes, &testnet, HeaderForm::Stored), held, "unmarked"); + } + for hex in [V1_WIRE, V2_WIRE, V2_FULL, V2_SHORT_WIRE] { + let header = decode(hex); + assert_eq!( + read(&header.encode_stored(), &testnet, HeaderForm::Stored), + header + ); + let full = alloy_primitives::hex::decode(hex).unwrap(); + assert_eq!(read(&full, &testnet, HeaderForm::Stored), header, "in full"); + } + // Where no rule holds a Bitcoin header to 80 bytes. + let anywhere = ActivationHeights { + orchid: u64::MAX, + ..ActivationHeights::testnet() + }; + let mut items = items_of(&alloy_primitives::hex::decode(V2_COMPRESSED).unwrap()); + items[17] = element(&[0x07]); + let bytes = list_of(&items); + let held = read(&bytes, &anywhere, HeaderForm::Compressed); + assert_eq!( + read(&held.encode_stored(), &anywhere, HeaderForm::Stored), + held + ); + // Unmarked, as the store wrote one before, it reads as rskj's full + // encoding would have it: with a version. + assert_ne!(read(&bytes, &anywhere, HeaderForm::Stored), held); + // Before RSKIP-351 a header is as rskj writes it in full, even one + // whose Bitcoin header is the mark. + let early = Header { + bitcoin_merged_mining_header: Some(Bytes::from(vec![STORED_LAYOUT])), + ..super::tests::standard_test_header() + }; + let mainnet = ActivationHeights { + orchid: u64::MAX, + ..ActivationHeights::mainnet() + }; + assert_eq!( + read(&early.encode_stored(), &mainnet, HeaderForm::Stored), + early + ); + } + + /// Rustock nodes send each other trunk headers in the store's layout, so + /// it reads only what rskj reads a header with at its height: in full, + /// the edges where RSKIP-144 is active and a base event where RSKIP-535 + /// is, which a V1 ignores; compressed, a V0 with neither, whose slot 6 + /// names its version. A header with other fields is one rskj can't read, + /// and so can't take. + #[test] + fn the_stored_layout_holds_only_what_rskj_reads_at_a_height() { + use super::tests::{element, items_of, list_of}; + use crate::config::ActivationHeights; + let testnet = ActivationHeights::testnet(); + let stored = |header: &Header, heights: &ActivationHeights| { + let bytes = header.encode_stored(); + Header::decode_in(&mut bytes.as_slice(), heights, HeaderForm::Stored) + }; + // Testnet's #8,260,761, with RSKIP-144's edges and RSKIP-535's base + // event. + let v2 = decode(V2_FULL); + let fields = v2.version_fields.clone().unwrap(); + let (edges, base_event) = (fields.edges, fields.base_event); + assert!(edges.is_some() && base_event.is_some()); + let with = |version: u8, edges: &Option>, base_event: &Option| Header { + version_fields: Some(HeaderVersionFields { + version, + edges: edges.clone(), + base_event: base_event.clone(), + }), + ..v2.clone() + }; + for version in [0, 2] { + let header = with(version, &edges, &base_event); + assert_eq!(stored(&header, &testnet).unwrap(), header); + assert!( + stored(&with(version, &None, &base_event), &testnet).is_err(), + "no edges" + ); + assert!( + stored(&with(version, &edges, &None), &testnet).is_err(), + "no base event" + ); + // Before RSKIP-535, a base event is a field too many. + let before = ActivationHeights { + rskip535: None, + ..testnet.clone() + }; + assert!(stored(&header, &before).is_err()); + assert!(stored(&with(version, &edges, &None), &before).is_ok()); + } + assert!(stored(&with(1, &edges, &None), &testnet).is_ok()); + assert!( + stored(&with(1, &edges, &base_event), &testnet).is_err(), + "a V1's base event" + ); + assert!( + stored(&with(1, &None, &None), &testnet).is_err(), + "no edges" + ); + // A V0 with neither field reads only as rskj reads one compressed: + // where slot 6 names its version, which a bloom of 256 bytes doesn't. + assert!(stored(&with(0, &None, &None), &testnet).is_err()); + let mut items = items_of(&alloy_primitives::hex::decode(V2_COMPRESSED).unwrap()); + items[6] = element(&[0xc1, 0x03]); + let compressed = Header::decode_in( + &mut list_of(&items).as_slice(), + &testnet, + HeaderForm::Compressed, + ) + .unwrap(); + assert_eq!( + compressed.version_fields, + Some(HeaderVersionFields { + version: 0, + edges: None, + base_event: None + }) + ); + assert_eq!(stored(&compressed, &testnet).unwrap(), compressed); + // But not one whose slot 6 names version 1 or 2, which rskj reads + // as held compressed, nor a V1 or a V2 with neither field. + let mut named = compressed.clone(); + named.kept.as_mut().unwrap().logs_bloom = Some(Bytes::from_static(&[0xc1, 0x02])); + assert!(stored(&named, &testnet).is_err()); + for version in [1, 2] { + let neither = Header { + version_fields: Some(HeaderVersionFields { + version, + edges: None, + base_event: None, + }), + ..compressed.clone() + }; + assert!(stored(&neither, &testnet).is_err()); + } + // No version after the mark but those this node writes. + let mut items = items_of(&v2.encode_stored()); + assert_eq!(&items[17][..], &[0x82, 0xff, 0x02][..]); + items[17] = element(&[0xff, 0x03]); + let bytes = list_of(&items); + assert!(Header::decode_in(&mut bytes.as_slice(), &testnet, HeaderForm::Stored).is_err()); + } } diff --git a/crates/core/src/validation/header_rules.rs b/crates/core/src/validation/header_rules.rs index 747e566..f8d081b 100644 --- a/crates/core/src/validation/header_rules.rs +++ b/crates/core/src/validation/header_rules.rs @@ -152,45 +152,6 @@ impl HeaderValidator for GasLimitRule { } } -/// The fields rskj's `BlockFactory.decodeHeader` reads a header with, which -/// depend on its height: `canBeDecoded` counts them exactly, so a header with -/// one too many or one too few is one rskj cannot read, and so cannot take. -/// The decoder here reads by shape rather than by height, so this holds what -/// it read to what rskj would have. -/// -/// - A UMM root from RSKIPUMM on, and none before it: papyrus200 on mainnet -/// and testnet, as `reference.conf` has it, and 1 on rskj's regtest. -/// - No RSKIP351 fields where RSKIP351 is inactive, as it is on mainnet: no -/// version, edges or base event after the UMM root, and no commitment to -/// an extension in the logs bloom's place. The decoder reads whatever is -/// there as the bloom, of any length, as rskj does, so only a header built -/// here could hold one. -/// -/// An uncle is read the same way, at its own height. -pub struct HeaderLayoutRule { - pub config: std::sync::Arc, -} - -impl HeaderValidator for HeaderLayoutRule { - fn validate(&self, header: &Header) -> Result<(), ValidationError> { - let heights = &self.config.activation_heights; - let umm = heights.umm_active(header.number); - if header.umm_root.is_some() != umm { - return Err(ValidationError::UmmRootMisplaced { - number: header.number, - present: header.umm_root.is_some(), - }); - } - let rskip351 = heights.rskip351_active(header.number); - if !rskip351 && (header.version_fields.is_some() || header.extension_data.is_some()) { - return Err(ValidationError::VersionedBeforeRskip351 { - number: header.number, - }); - } - Ok(()) - } -} - /// rskj `BlockParentGasLimitRule`: a header's gas limit may move from its /// parent's by at most a `gas_limit_bound_divisor`th, either way. pub struct BlockParentGasLimitRule { diff --git a/crates/core/src/validation/mod.rs b/crates/core/src/validation/mod.rs index 9b95284..70da361 100644 --- a/crates/core/src/validation/mod.rs +++ b/crates/core/src/validation/mod.rs @@ -73,12 +73,6 @@ pub enum ValidationError { #[error("UMM root is {len} bytes, must be empty or 20")] UmmRootInvalid { len: usize }, - #[error("Header #{number} has a UMM root: {present}, which rskj reads from RSKIPUMM on and only then")] - UmmRootMisplaced { number: u64, present: bool }, - - #[error("Header #{number} carries RSKIP351 fields where RSKIP351 is inactive")] - VersionedBeforeRskip351 { number: u64 }, - #[error("Tx gas price {tx_gas_price} is below the block minimum {block_minimum}")] TxGasPriceBelowMinimum { tx_gas_price: U256, @@ -231,8 +225,6 @@ impl HeaderVerifier { /// and cannot be reproduced by our standard `Header::hash()`. pub fn default_rsk(config: std::sync::Arc) -> Self { let mut verifier = Self::new() - // First, as rskj reads a header with these fields or not at all. - .with_static_rule(HeaderLayoutRule { config: config.clone() }) .with_static_rule(GasUsedRule) .with_static_rule(GasLimitRule { min_gas_limit: config.min_gas_limit, @@ -345,7 +337,7 @@ pub mod header_rules; pub mod difficulty; pub mod merged_mining; -pub use header_rules::{BlockNumberRule, ParentHashRule, TimestampRule, GasUsedRule, GasLimitRule, BlockParentGasLimitRule, HeaderLayoutRule}; +pub use header_rules::{BlockNumberRule, ParentHashRule, TimestampRule, GasUsedRule, GasLimitRule, BlockParentGasLimitRule}; pub use difficulty::DifficultyRule; pub use merged_mining::{BitcoinTimestampRule, MergedMiningRule}; pub use block_rules::{ diff --git a/crates/core/src/validation/tests.rs b/crates/core/src/validation/tests.rs index 37b4da9..144f6da 100644 --- a/crates/core/src/validation/tests.rs +++ b/crates/core/src/validation/tests.rs @@ -1811,85 +1811,6 @@ fn default_rsk_holds_uncles_to_rskjs_uncle_rules() { assert_eq!(verifier.uncle_parent_rules().len(), 5, "rskj's five, in its order"); } -/// rskj reads a header's fields by its height (`BlockFactory.canBeDecoded`): -/// a UMM root from RSKIPUMM on and only then -- papyrus200 on mainnet and -/// testnet, 1 on regtest -- and no RSKIP351 -/// fields where RSKIP351 is inactive, which is all of mainnet. The decoder -/// reads by shape, so the verifier holds what it read to that, for a block -/// and for an uncle alike, before anything else. -#[test] -fn a_header_carries_the_fields_rskj_reads_at_its_height() { - use crate::config::ChainConfig; - use crate::types::header::HeaderVersionFields; - - let mainnet = Arc::new(ChainConfig::mainnet()); - let papyrus = mainnet.activation_heights.papyrus200; - let rule = HeaderLayoutRule { config: mainnet.clone() }; - let at = |number: u64, umm: bool| { - let mut header = create_dummy_header(number, 1000, B256::ZERO); - header.umm_root = umm.then(Bytes::new); - header - }; - let misplaced = |number, present| Err(ValidationError::UmmRootMisplaced { number, present }); - - assert_eq!(rule.validate(&at(papyrus - 1, false)), Ok(())); - assert_eq!(rule.validate(&at(papyrus - 1, true)), misplaced(papyrus - 1, true)); - assert_eq!(rule.validate(&at(papyrus, true)), Ok(())); - assert_eq!(rule.validate(&at(papyrus, false)), misplaced(papyrus, false)); - // RSKIPUMM's own height, not papyrus200's, where the two part. - let apart = HeaderLayoutRule { - config: Arc::new(ChainConfig { - activation_heights: crate::config::ActivationHeights { - rskip_umm: Some(papyrus + 10), - ..mainnet.activation_heights.clone() - }, - ..(*mainnet).clone() - }), - }; - assert_eq!(apart.validate(&at(papyrus, false)), Ok(())); - assert_eq!( - apart.validate(&at(papyrus + 10, false)), - misplaced(papyrus + 10, false) - ); - // rskj's regtest: from block 1, as `regtest.conf` has it. - let regtest = HeaderLayoutRule { - config: Arc::new(ChainConfig::regtest()), - }; - assert_eq!(regtest.validate(&at(0, false)), Ok(())); - assert_eq!(regtest.validate(&at(0, true)), misplaced(0, true)); - assert_eq!(regtest.validate(&at(1, true)), Ok(())); - assert_eq!(regtest.validate(&at(1, false)), misplaced(1, false)); - - let versioned = |mut header: Header| { - header.version_fields = - Some(HeaderVersionFields { version: 1, edges: Some(Vec::new()), base_event: None }); - header - }; - let compressed = |mut header: Header| { - header.extension_data = Some(Bytes::from(vec![0xc0])); - header - }; - let tip = 9_400_000; - for header in [versioned(at(tip, true)), compressed(at(tip, true))] { - assert_eq!( - rule.validate(&header), - Err(ValidationError::VersionedBeforeRskip351 { number: tip }) - ); - } - // Testnet has RSKIP351 from reed810, #7,139,600. - let testnet = HeaderLayoutRule { config: Arc::new(ChainConfig::testnet()) }; - let reed810 = 7_139_600; - assert_eq!( - testnet.validate(&versioned(at(reed810 - 1, true))), - Err(ValidationError::VersionedBeforeRskip351 { number: reed810 - 1 }) - ); - assert_eq!(testnet.validate(&versioned(at(reed810, true))), Ok(())); - - let verifier = HeaderVerifier::default_rsk(mainnet); - assert_eq!(verifier.verify(&at(papyrus, false), None), misplaced(papyrus, false)); - assert_eq!(verifier.verify_uncle(&at(papyrus, false)), misplaced(papyrus, false)); -} - /// rskj's trunk takes the timestamp rule on the header alone. A block no /// later than its parent is one `BlockValidatorImpl` imports -- the parent /// form is for uncles, and for rskj's relay -- so this node must take it diff --git a/crates/core/src/validation/uncles.rs b/crates/core/src/validation/uncles.rs index c70e2eb..57bf5c4 100644 --- a/crates/core/src/validation/uncles.rs +++ b/crates/core/src/validation/uncles.rs @@ -8,7 +8,7 @@ use super::fork_detection::MainchainView; use super::{ BitcoinTimestampRule, BlockNumberRule, BlockParentGasLimitRule, DifficultyRule, GasUsedRule, - HeaderLayoutRule, HeaderValidator, MergedMiningRule, ParentHeaderValidator, + HeaderValidator, MergedMiningRule, ParentHeaderValidator, PrevMinGasPriceRule, TimestampRule, ValidationError, }; use crate::config::ChainConfig; @@ -28,15 +28,13 @@ pub const UNCLE_GENERATION_LIMIT: u64 = 7; /// rule and gas used, its `BlockHeaderCompositeRule`. rskj's timestamp rule /// is two here: [`TimestampRule`], the clock, and [`BitcoinTimestampRule`], /// RSKIP179. The fourth, fork detection, reads the chain rather than the -/// header and is [`UnclesValidationRule::fork_detection`]. Before them, the -/// layout `BlockFactory` reads every header in, an uncle's at its own height. +/// header and is [`UnclesValidationRule::fork_detection`]. /// /// Not the trunk header rules: rskj holds no uncle to the minimum gas limit /// or the 32-byte extra-data cap, so an uncle may carry either and still be /// one. pub fn rsk_header_rules(config: Arc) -> Vec> { vec![ - Box::new(HeaderLayoutRule { config: config.clone() }), Box::new(MergedMiningRule { config: config.clone() }), Box::new(TimestampRule::new(config.max_future_block_time)), Box::new(BitcoinTimestampRule { config }), diff --git a/crates/execution/src/executor.rs b/crates/execution/src/executor.rs index a9db53a..95ebaf8 100644 --- a/crates/execution/src/executor.rs +++ b/crates/execution/src/executor.rs @@ -2526,7 +2526,10 @@ mod tests { paid_fees: U256::ZERO, minimum_gas_price: U256::from(0), uncle_count: 0, - umm_root: None, + // A UMM root where a store's heights, this process's, read one. + umm_root: rustock_core::config::ActivationHeights::current() + .umm_active(number) + .then(Bytes::new), version_fields: None, bitcoin_merged_mining_header: None, bitcoin_merged_mining_merkle_proof: None, diff --git a/crates/execution/src/mining/tests.rs b/crates/execution/src/mining/tests.rs index 0041f3d..f25f387 100644 --- a/crates/execution/src/mining/tests.rs +++ b/crates/execution/src/mining/tests.rs @@ -211,7 +211,14 @@ fn fixture_on( flush_every: u64, ) -> MiningFixture { let dir = tempfile::tempdir().unwrap(); - let store = Arc::new(BlockStore::open(dir.path()).unwrap()); + // A store that reads headers under the chain's heights, as a node's does + // under the heights it installs at startup. + let heights = chain_config.activation_heights.clone(); + let store = Arc::new( + BlockStore::open(dir.path()) + .unwrap() + .with_activations(heights.clone()), + ); let empty_root = TrieNode::empty().compute_hash(trie_store.as_ref()); let mut parent_hash = B256::ZERO; @@ -219,6 +226,7 @@ fn fixture_on( for number in 0..=height { let mut header = header_at(number, parent_hash, empty_root); header.difficulty = parent_difficulty; + header.umm_root = heights.umm_active(number).then(Bytes::new); let hash = header.hash(); store.put_header_with_hash(hash, &header).unwrap(); store.put_canonical_hash(number, hash).unwrap(); diff --git a/crates/execution/src/processor.rs b/crates/execution/src/processor.rs index aa5c90f..e1b0c17 100644 --- a/crates/execution/src/processor.rs +++ b/crates/execution/src/processor.rs @@ -1304,7 +1304,10 @@ mod tests { paid_fees: U256::ZERO, minimum_gas_price: U256::from(0), uncle_count: 0, - umm_root: None, + // A UMM root where a store's heights, this process's, read one. + umm_root: rustock_core::config::ActivationHeights::current() + .umm_active(number) + .then(Bytes::new), version_fields: None, bitcoin_merged_mining_header: None, bitcoin_merged_mining_merkle_proof: None, diff --git a/crates/networking/src/protocol/rsk.rs b/crates/networking/src/protocol/rsk.rs index dcd0191..f38dcb1 100644 --- a/crates/networking/src/protocol/rsk.rs +++ b/crates/networking/src/protocol/rsk.rs @@ -1,6 +1,7 @@ use alloy_rlp::{Decodable, Encodable, RlpDecodable, RlpEncodable, Header as RlpHeader}; use alloy_primitives::{B256, Bytes, U256}; use rustock_core::{Header, Transaction}; +use rustock_core::types::header::HeaderForm; use rustock_core::types::header_extension::BlockHeaderExtension; use rustock_core::rlp_compat::{decode_u8_lenient, decode_u64_lenient, decode_u256_lenient, decode_u32_lenient}; use super::snap; @@ -477,7 +478,10 @@ impl RskSubMessage { let mut entries_payload = Vec::new(); for entry in &r.entries { let mut one = Vec::new(); - entry.header.encode(&mut one); + // A header as this node holds it, in the store's form, + // which a header held compressed has too; its uncles in + // full, the form its uncles hash commits to. + one.extend_from_slice(&entry.header.encode_stored()); one.extend_from_slice(&entry.encoded_uncles()); RlpHeader { list: true, payload_length: one.len() } @@ -495,10 +499,11 @@ impl RskSubMessage { out.extend_from_slice(¶ms); } RskSubMessage::BlockHeadersResponse(r) => { - // RLP([id, RLP([RLP([headers])])]) + // RLP([id, RLP([RLP([headers])])]), each header compressed, as + // rskj sends and reads them (`getEncodedCompressed`). let mut headers_payload = Vec::new(); for h in &r.headers { - h.encode(&mut headers_payload); + headers_payload.extend_from_slice(&h.encode_compressed()); } let mut headers_list = Vec::new(); @@ -786,7 +791,12 @@ impl Decodable for RskMessage { let mut headers = Vec::new(); while !inner_body.is_empty() { - headers.push(Header::decode_with_hash(&mut inner_body)?); + // rskj `decodeHeader(.., true)`: compressed. + headers.push(Header::decode_with_hash_in( + &mut inner_body, + rustock_core::config::ActivationHeights::current(), + HeaderForm::Compressed, + )?); } RskSubMessage::BlockHeadersResponse(BlockHeadersResponse { id, headers }) } @@ -1020,7 +1030,11 @@ impl Decodable for RskMessage { let mut entry_body = &entries_body[..entry_h.payload_length]; entries_body = &entries_body[entry_h.payload_length..]; - let header = Header::decode_with_hash(&mut entry_body)?; + let header = Header::decode_with_hash_in( + &mut entry_body, + rustock_core::config::ActivationHeights::current(), + HeaderForm::Stored, + )?; // Hash the uncle list exactly as it arrived. Re-encoding it // and hashing that would compare our spelling of the bytes diff --git a/crates/networking/tests/testnet_headers.rs b/crates/networking/tests/testnet_headers.rs new file mode 100644 index 0000000..8cd9edd --- /dev/null +++ b/crates/networking/tests/testnet_headers.rs @@ -0,0 +1,157 @@ +//! Testnet's headers over the wire, under testnet's heights. The message +//! decoder reads headers under the process's heights, as a testnet node's +//! does under those it installs at startup; this test binary is its own +//! process, and installs testnet's. + +use alloy_primitives::hex; +use alloy_rlp::{Decodable, Encodable}; +use rustock_core::config::ActivationHeights; +use rustock_core::types::header::{HeaderForm, HeaderVersionFields}; +use rustock_core::Header; +use rustock_networking::protocol::rsk::{ + BlockHeadersResponse, BlockHeadersWithUnclesResponse, HeaderWithUncles, +}; +use rustock_networking::{RskMessage, RskSubMessage}; + +/// Testnet #8,260,761 in full, as its block carries it (core's `V2_FULL`). +const FULL: &str = concat!( + "f9029ba0285f7c3513c466f54fde5f1b73b2d93b35374515472192250bd3a56396dc649ea01dcc4de8dec75d7aab85b5", + "67b6ccd41ad312451b948a7413f0a142fd40d4934794b774aa2876145b2f6f3de27e5e6ac970aa12d771a04aa4b67c3e", + "30508692f13e9e10758d907e7d851234cd6cd00d65aa49e5a1ac44a0f40c99e8151d350e30309fa0a74234c365961aa0", + "81ad18e51a4281c513ec76d0a066cfdb731f620cd96e2c2cb0f7d3c3a2879c29b40014aa27efbbf3cf9cd3b0f6b90100", + "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "00000000000000000000000000000000844edfab4b837e0c998400b71b0080846aec1a5c92d1018f564554495645522d", + "3733316239643880835a74cf8080028080b8507111010000000000000000000000000000000000000000000000000000", + "00000000000000135534881310aada94b2b5867af37e4e2fc48be028c35beeaa7f1dc4cf501e825d1aec6affff0f1d0d", + "16caea80b85800000000000003c09e1ed89b4ab8f092a3dc3a4b852db4552dfd95136f298514ba947d076032a8f20bbe", + "a052534b424c4f434b3a6e7fa68f56e3bc6085e7e02e325250f1c72ad9eef1a0d7693e516e10007e0c9900000000", +); + +/// Testnet #8,260,720 as rskj's peers sent it, compressed (core's +/// `V2_FROM_PEERS`). +const FROM_PEERS: &str = concat!( + "f901f9a00bdcd28d517b22bc7b65bf669b05924ac8ba8a862ccb4b17d619fdbd8b9789f8a01dcc4de8dec75d7aab85b5", + "67b6ccd41ad312451b948a7413f0a142fd40d4934794ad418c1d48780005f6d847ef0a5e3bd93ea09090a0c545101cc3", + "df462f9c369b22f722ec66ade0f031b528512f12fceca574d0ecc8a01db2709b879ae45385439bc35bc1ce8e821de8ae", + "b4a4e8ec9c94cf9f3dc55f7ea01ddfe2c6ff70e83e21bc8a905eb71278aab2fc921dd08543c20451a2ccbf286da3e202", + "a03c015999cbb7ab1fdb80e23b658a251e424af79a99262579f2fd59bd50fddc78844f476ad3837e0c708400b71b0082", + "e8f8846aec16be92d1018f564554495645522d37333162396438855a76a29740835a5a088080b85000000020785e2a8b", + "51231a7478b788a2427a2a4c190c0f4e418e25c4ca0c000000000000ba37f569d2f87d917d83e79d50af85497b7d35f0", + "ebe4e7c57ddd76e80fb58dfca130ec6af0ff0f1a007dc5fa80b8910000000000000080f880b614920083ca51e27b69b7", + "d6dba5262da24a347487e62e8406463eb94d6a6088ac0000000000000000266a24aa21a9ede2f61c3f71d1defd3fa999", + "dfa36953755c690689799962b48bebd836974e8cf900000000000000002a6a52534b424c4f434b3a3d8461c1058c9db8", + "352ad0c0b7e56a1ba4fcac4da0d7693e516ef40a007e0c7000000000", +); + +/// Testnet's heights, installed for this process before anything reads +/// the process's. +fn testnet() -> &'static ActivationHeights { + ActivationHeights::testnet().install(); + let heights = ActivationHeights::current(); + assert_eq!( + heights.rskip351, + ActivationHeights::testnet().rskip351, + "testnet's heights" + ); + heights +} + +/// A header read as testnet's nodes read one in `form`. +fn read(hex: &str, form: HeaderForm) -> Header { + let bytes = hex::decode(hex).unwrap(); + Header::decode_with_hash_in(&mut bytes.as_slice(), testnet(), form).unwrap() +} + +/// `sub` sent and read back. +fn roundtrip(sub: RskSubMessage) -> RskSubMessage { + testnet(); + let mut buf = Vec::new(); + RskMessage::new(sub).encode(&mut buf); + RskMessage::decode(&mut buf.as_slice()) + .expect("decodes") + .sub_message +} + +/// A header without the hashes a decoder caches. +fn uncached(header: &Header) -> Header { + Header { + cached_hash: None, + cached_hash_for_merged_mining: None, + ..header.clone() + } +} + +/// **A chain's headers go to a peer compressed** (`BlockHeadersResponse`), +/// as rskj sends and reads them: one held in full arrives with only the +/// commitment to its extension, and the block's hash; one held compressed +/// arrives as held. +#[test] +fn a_chains_headers_go_to_a_peer_compressed() { + let full = read(FULL, HeaderForm::Full); + let peers = read(FROM_PEERS, HeaderForm::Compressed); + let sent = BlockHeadersResponse { + id: 7, + headers: vec![full.clone(), peers.clone()], + }; + let RskSubMessage::BlockHeadersResponse(got) = + roundtrip(RskSubMessage::BlockHeadersResponse(sent)) + else { + panic!("another message"); + }; + let [arrived, held] = got.headers.as_slice() else { + panic!("two headers"); + }; + assert_eq!(arrived.hash(), full.hash()); + assert!(arrived.extension_data.is_some() && arrived.version_fields.is_none()); + assert_eq!(arrived.encode_compressed(), full.encode_compressed()); + let extension = full.extension().expect("a versioned header's own"); + assert_eq!( + arrived.with_extension(&extension).map(|h| h.hash()), + Some(full.hash()) + ); + assert_eq!(held, &peers); +} + +/// **Trunk headers and their uncles go between rustock nodes as held** +/// (`BlockHeadersWithUnclesResponse`): each header in the store's form, so +/// a header held compressed arrives so, and a version-0 header from +/// RSKIP-351 on, whose full encoding rskj can't read back, arrives whole. +#[test] +fn headers_with_uncles_carry_each_header_as_held() { + let full = read(FULL, HeaderForm::Full); + let peers = read(FROM_PEERS, HeaderForm::Compressed); + let v0 = Header { + version_fields: Some(HeaderVersionFields { + version: 0, + ..full.version_fields.clone().expect("a versioned header") + }), + ..uncached(&full) + }; + let entries: Vec = [full, peers, v0] + .into_iter() + .map(|header| HeaderWithUncles { + header, + uncles: Vec::new(), + }) + .collect(); + let sent = BlockHeadersWithUnclesResponse { + id: 8, + entries: entries.clone(), + }; + let RskSubMessage::BlockHeadersWithUnclesResponse(got) = + roundtrip(RskSubMessage::BlockHeadersWithUnclesResponse(sent)) + else { + panic!("another message"); + }; + let held = |entries: &[HeaderWithUncles]| -> Vec
{ + entries + .iter() + .map(|entry| uncached(&entry.header)) + .collect() + }; + assert_eq!(held(&got.entries), held(&entries)); +} diff --git a/crates/rpc/src/tests.rs b/crates/rpc/src/tests.rs index 7c09a25..7a62a80 100644 --- a/crates/rpc/src/tests.rs +++ b/crates/rpc/src/tests.rs @@ -6508,11 +6508,20 @@ const TESTNET_FROM_PEERS: &str = concat!( "352ad0c0b7e56a1ba4fcac4da0d7693e516ef40a007e0c7000000000", ); -/// A header as testnet's nodes read one. -fn testnet_header(hex: &str) -> Header { +/// A header as testnet's nodes read one in `form`. +fn testnet_header_in(hex: &str, form: rustock_core::types::header::HeaderForm) -> Header { let bytes = hex::decode(hex).unwrap(); - Header::decode_with_hash_and_activations(&mut bytes.as_slice(), &ActivationHeights::testnet()) - .unwrap() + Header::decode_with_hash_in(&mut bytes.as_slice(), &ActivationHeights::testnet(), form).unwrap() +} + +/// A header as testnet's blocks carry one: in full. +fn testnet_header(hex: &str) -> Header { + testnet_header_in(hex, rustock_core::types::header::HeaderForm::Full) +} + +/// A header as rskj's peers send a chain's headers: compressed. +fn testnet_header_compressed(hex: &str) -> Header { + testnet_header_in(hex, rustock_core::types::header::HeaderForm::Compressed) } /// A header in full, as hex. @@ -6668,7 +6677,7 @@ async fn a_testnet_block_comes_with_its_header_compressed() { blocks_of(raw_blocks(&state, "0x7e0c99", "0x7e0c99").await), vec![(TESTNET_COMPRESSED.to_string(), Vec::new())] ); - follow(&state.store, &testnet_header(TESTNET_FROM_PEERS)); + follow(&state.store, &testnet_header_compressed(TESTNET_FROM_PEERS)); assert_eq!( blocks_of(raw_blocks(&state, "0x7e0c70", "0x7e0c70").await), vec![(TESTNET_FROM_PEERS.to_string(), Vec::new())] @@ -6681,7 +6690,9 @@ async fn a_testnet_block_comes_with_its_header_compressed() { async fn raw_blocks_carry_uncles_in_full_from_the_body_or_the_freezer() { use rustock_execution::processor::compute_ommers_hash; - let (state, _tmp) = setup_state(); + // A testnet node's store, which reads a testnet uncle as testnet's + // nodes do. + let (state, _tmp) = setup_testnet_state(); let testnet = testnet_header(TESTNET_FULL); let other = Header { extra_data: Bytes::from_static(b"an uncle"), @@ -6721,7 +6732,7 @@ async fn raw_blocks_carry_uncles_in_full_from_the_body_or_the_freezer() { async fn raw_blocks_stop_before_a_block_not_held_whole_or_not_following() { use rustock_execution::processor::compute_ommers_hash; - let compressed = testnet_header(TESTNET_FROM_PEERS); + let compressed = testnet_header_compressed(TESTNET_FROM_PEERS); let naming = |at: u64, uncles: Vec
| { move |number: u64| Header { ommers_hash: if number == at { diff --git a/crates/storage/src/freezer.rs b/crates/storage/src/freezer.rs index 32dcba6..39ff684 100644 --- a/crates/storage/src/freezer.rs +++ b/crates/storage/src/freezer.rs @@ -67,7 +67,6 @@ use alloy_primitives::B256; use anyhow::{anyhow, bail, Context, Result}; use rustock_core::config::ActivationHeights; use rustock_core::Header; -use alloy_rlp::Encodable; use std::fs::{File, OpenOptions}; use std::io::{Read, Seek, SeekFrom, Write}; use std::path::{Path, PathBuf}; @@ -526,16 +525,16 @@ impl Freezer { /// Stores the uncle headers `number` references. /// - /// Written as the RLP list the header's `ommers_hash` commits to, so a - /// reader can recompute the commitment and a peer can be served the bytes - /// directly. A block with no uncles stores the empty list, `0xc0`, rather - /// than nothing: a zero-length run would be indistinguishable from an - /// index entry that was never written, and "this block has no uncles" and - /// "this block is not frozen" must not be the same answer. + /// Written as an RLP list of the uncles in the store's form + /// ([`Header::encode_stored`]). A block with no uncles stores the empty + /// list, `0xc0`, rather than nothing: a zero-length run would be + /// indistinguishable from an index entry that was never written, and + /// "this block has no uncles" and "this block is not frozen" must not be + /// the same answer. pub fn put_uncles(&self, number: u64, uncles: &[Header]) -> Result<()> { let mut payload = Vec::new(); for uncle in uncles { - uncle.encode(&mut payload); + payload.extend_from_slice(&uncle.encode_stored()); } let mut encoded = Vec::with_capacity(payload.len() + 9); alloy_rlp::Header { list: true, payload_length: payload.len() }.encode(&mut encoded); @@ -654,7 +653,11 @@ impl Freezer { let mut out = Vec::new(); while !body.is_empty() { out.push( - Header::decode_with_hash_and_activations(&mut body, self.activations()) + Header::decode_with_hash_in( + &mut body, + self.activations(), + rustock_core::types::header::HeaderForm::Stored, + ) .map_err(|e| anyhow!("decoding frozen uncle for #{number}: {e:?}"))?, ); } @@ -874,7 +877,12 @@ fn furthest_end(index: &mut File, end_number: u64, file: u16) -> Result { } fn decode_header(bytes: &[u8], activations: &ActivationHeights) -> Option
{ - Header::decode_with_activations(&mut &bytes[..], activations).ok() + Header::decode_in( + &mut &bytes[..], + activations, + rustock_core::types::header::HeaderForm::Stored, + ) + .ok() } #[cfg(test)] diff --git a/crates/storage/src/lib.rs b/crates/storage/src/lib.rs index 6d05ddd..6119e1a 100644 --- a/crates/storage/src/lib.rs +++ b/crates/storage/src/lib.rs @@ -195,15 +195,15 @@ impl Default for ImportTuning { } } -/// RLP-encodes a header exactly as the headers column family stores it. +/// RLP-encodes a header exactly as the headers column family stores it +/// ([`Header::encode_stored`]). pub fn encode_header(header: &Header) -> Vec { - let mut buf = Vec::new(); - header.encode(&mut buf); - buf + header.encode_stored() } /// RLP-encodes a block body exactly as the bodies column family stores it: -/// `[[transactions], [ommers]]`. +/// `[[transactions], [ommers]]`, each uncle in the store's form +/// ([`Header::encode_stored`]). pub fn encode_body(transactions: &[Transaction], ommers: &[Header]) -> Vec { let mut txs_payload = Vec::new(); for tx in transactions { @@ -211,7 +211,7 @@ pub fn encode_body(transactions: &[Transaction], ommers: &[Header]) -> Vec { } let mut ommers_payload = Vec::new(); for uncle in ommers { - uncle.encode(&mut ommers_payload); + ommers_payload.extend_from_slice(&uncle.encode_stored()); } let body_len = RlpHeader { list: true, payload_length: txs_payload.len() }.length() @@ -400,7 +400,11 @@ impl BlockStore { /// A stored header, read under this store's heights. fn read_header(&self, buf: &mut &[u8]) -> alloy_rlp::Result
{ - Header::decode_with_activations(buf, self.activations()) + Header::decode_in( + buf, + self.activations(), + rustock_core::types::header::HeaderForm::Stored, + ) } /// Scans the headers column family for the highest block number. @@ -869,9 +873,7 @@ impl BlockStore { for &(header, td) in entries { let hash = header.hash(); - let mut header_buf = Vec::new(); - header.encode(&mut header_buf); - batch.put_cf(cf_headers, hash.as_slice(), &header_buf); + batch.put_cf(cf_headers, hash.as_slice(), encode_header(header)); batch.put_cf(cf_td, hash.as_slice(), encode_td(td)); // Fork headers arrive through here too, and they are precisely the @@ -2113,10 +2115,10 @@ mod tests { } /// A store reads its headers under its chain's heights, the process's - /// unless set: where RSKIP-351 is active, slot 6 of a header held - /// compressed is the commitment to its extension, and before it the - /// same bytes are the logs bloom, as rskj's `BlockFactory` reads them. - /// The freezer reads its own alike. + /// unless set, as rskj's `BlockFactory` reads them, by height: a header + /// held compressed, as one is from RSKIP-351 on, reads back as held under + /// regtest's heights, and not at all under this process's, mainnet's, + /// which read no such header at #1. The freezer reads its own alike. #[test] fn a_store_reads_headers_under_its_chains_heights() { use rustock_core::config::ActivationHeights; @@ -2124,12 +2126,13 @@ mod tests { commitment.extend_from_slice(&[0x55; 32]); let mut slot = Vec::new(); commitment.as_slice().encode(&mut slot); + // rskj's regtest has RSKIPUMM and RSKIP-351 from block 1. let compressed = Header { extension_data: Some(Bytes::from(slot)), + umm_root: Some(Bytes::new()), ..dummy_header(1) }; let hash = compressed.hash(); - // rskj's regtest has RSKIP-351 from block 1. let regtest = ActivationHeights::regtest(); let dir = tempdir().unwrap(); @@ -2144,11 +2147,9 @@ mod tests { assert_eq!(store.header(hash).unwrap(), Some(compressed.clone())); assert_eq!(store.uncles(hash).unwrap(), Some(vec![compressed.clone()])); } - // This process's heights are mainnet's, which has no RSKIP-351. let store = BlockStore::open(dir.path()).unwrap(); - let read = store.header(hash).unwrap().unwrap(); - assert_eq!(read.extension_data, None); - assert_eq!(read.hash(), hash, "the same bytes, the same header"); + assert!(store.header(hash).is_err()); + assert!(store.uncles(hash).is_err()); let frozen = tempdir().unwrap(); let freezer = crate::freezer::Freezer::open(frozen.path()) @@ -2159,19 +2160,81 @@ mod tests { .put_uncles(1, std::slice::from_ref(&compressed)) .unwrap(); freezer.sync().unwrap(); - let read_back = |freezer: crate::freezer::Freezer| { - let header = freezer.header(1).unwrap().unwrap(); - [header, freezer.uncles(1).unwrap().unwrap().remove(0)] + assert_eq!(freezer.header(1).unwrap(), Some(compressed.clone())); + let uncle = freezer.uncles(1).unwrap().unwrap().remove(0); + assert_eq!(uncle.extension_data, compressed.extension_data); + assert_eq!(uncle.hash(), hash); + let unread = crate::freezer::Freezer::open(frozen.path()).unwrap(); + assert_eq!(unread.header(1).unwrap(), None); + assert!(unread.uncles(1).is_err()); + } + + /// Every path that writes a header writes it in the store's own form + /// ([`encode_header`]), so it reads back as it was held: a header at a + /// time, a batch, as the header sync writes one, a body's uncles, and the + /// freezer's headers and uncles. In rskj's full encoding, a header of + /// version 0 from RSKIP-351 on, which writes no version, would read back + /// as nothing rskj reads there. + #[test] + fn every_path_that_writes_a_header_reads_it_back_as_held() { + use rustock_core::config::ActivationHeights; + use rustock_core::types::header::HeaderVersionFields; + let regtest = ActivationHeights::regtest(); + let v0 = |number| Header { + umm_root: Some(Bytes::new()), + version_fields: Some(HeaderVersionFields { + version: 0, + edges: Some(vec![1]), + base_event: Some(Bytes::new()), + }), + ..dummy_header(number) }; - let committed = read_back(freezer); - let bloom = read_back(crate::freezer::Freezer::open(frozen.path()).unwrap()); - for (header, uncle) in [(&committed[0], &committed[1]), (&bloom[0], &bloom[1])] { - assert_eq!(header.extension_data, uncle.extension_data); - } - assert!(committed[0].extension_data.is_some()); - assert_eq!(bloom[0].extension_data, None); - for header in committed.iter().chain(&bloom) { - assert_eq!(header.hash(), hash); + let mut commitment = vec![0xe2, 0x02, 0xa0]; + commitment.extend_from_slice(&[0x55; 32]); + let mut slot = Vec::new(); + commitment.as_slice().encode(&mut slot); + let held = Header { + umm_root: Some(Bytes::new()), + extension_data: Some(Bytes::from(slot)), + ..dummy_header(6) + }; + let (batched, single) = (v0(5), v0(7)); + let uncles = vec![batched.clone(), held.clone()]; + + let dir = tempdir().unwrap(); + let store = BlockStore::open(dir.path()) + .unwrap() + .with_activations(regtest.clone()); + // A total difficulty no entry passes, so the head stays where it is. + let entries = [(&batched, U256::from(1)), (&held, U256::from(2))]; + store + .store_headers_batch(&entries, None, U256::MAX) + .unwrap(); + store.put_header(&single).unwrap(); + store.put_body(single.hash(), &[], &uncles).unwrap(); + for header in [&batched, &held, &single] { + assert_eq!(store.header(header.hash()).unwrap().as_ref(), Some(header)); + } + assert_eq!(store.uncles(single.hash()).unwrap(), Some(uncles.clone())); + + let frozen = tempdir().unwrap(); + let freezer = crate::freezer::Freezer::open(frozen.path()) + .unwrap() + .with_activations(regtest); + freezer.put(7, &single).unwrap(); + freezer.put_uncles(7, &uncles).unwrap(); + freezer.sync().unwrap(); + assert_eq!(freezer.header(7).unwrap(), Some(single)); + // The freezer's uncles come with the hashes its reader caches. + let read: Vec<_> = freezer.uncles(7).unwrap().unwrap(); + assert_eq!(read.len(), uncles.len()); + for (read, uncle) in read.into_iter().zip(&uncles) { + let uncached = Header { + cached_hash: None, + cached_hash_for_merged_mining: None, + ..read + }; + assert_eq!(&uncached, uncle); } } diff --git a/crates/storage/tests/stall_replays.rs b/crates/storage/tests/stall_replays.rs index 24d25d1..9e21cdd 100644 --- a/crates/storage/tests/stall_replays.rs +++ b/crates/storage/tests/stall_replays.rs @@ -25,7 +25,10 @@ use rustock_core::Header; use rustock_storage::{BlockStore, LineageBreak, Transition, Validated, verify_local_coherence}; use tempfile::TempDir; +/// A header at `number`, laid out as the store's heights, this process's, +/// read one there: with a UMM root from RSKIPUMM on. fn header(number: u64, parent: B256, salt: u64) -> Header { + let umm = rustock_core::config::ActivationHeights::current().umm_active(number); Header { number, parent_hash: parent, @@ -44,7 +47,7 @@ fn header(number: u64, parent: B256, salt: u64) -> Header { paid_fees: U256::ZERO, minimum_gas_price: U256::ZERO, uncle_count: 0, - umm_root: None, + umm_root: umm.then(Bytes::new), version_fields: None, bitcoin_merged_mining_header: None, bitcoin_merged_mining_merkle_proof: None, diff --git a/crates/sync/src/tests.rs b/crates/sync/src/tests.rs index e7602ff..077fe55 100644 --- a/crates/sync/src/tests.rs +++ b/crates/sync/src/tests.rs @@ -33,7 +33,10 @@ pub(crate) fn dummy_header(number: u64, parent: B256, difficulty: U256) -> Heade paid_fees: U256::ZERO, minimum_gas_price: U256::ZERO, uncle_count: 0, - umm_root: None, + // A UMM root where the store's heights, this process's, read one. + umm_root: rustock_core::config::ActivationHeights::current() + .umm_active(number) + .then(Bytes::new), version_fields: None, bitcoin_merged_mining_header: None, bitcoin_merged_mining_merkle_proof: None, @@ -7613,7 +7616,8 @@ async fn a_body_from_a_peer_that_was_not_asked_is_ignored() { /// A version-2 extension whose bloom has one bit set: bit `bit`. Its edges /// are empty, as rskj emits them on every versioned header while RSKIP144 is -/// active and the block has no sublists; the store's decoder counts on it. +/// active and the block has no sublists: a header given it has the fields +/// rskj reads at regtest's heights. fn an_extension(bit: usize) -> rustock_core::types::header_extension::BlockHeaderExtension { let mut logs_bloom = vec![0u8; 256]; logs_bloom[bit / 8] |= 0x80 >> (bit % 8); @@ -7625,8 +7629,8 @@ fn an_extension(bit: usize) -> rustock_core::types::header_extension::BlockHeade } /// `header` as rskj sends it to its peers, holding only the commitment to -/// `extension` in its bloom's slot, with the UMM root and merged-mining fields -/// that every versioned header has, and that the store's decoder counts on. +/// `extension` in its bloom's slot, with the UMM root rskj reads at +/// regtest's heights and the merged-mining fields every versioned header has. fn compressed( header: Header, extension: &rustock_core::types::header_extension::BlockHeaderExtension, @@ -7837,7 +7841,11 @@ async fn a_body_stored_without_its_header_extension_is_asked_for_again() { let genesis = dummy_header(0, B256::ZERO, U256::from(1)); store.update_head(&genesis, U256::from(1)).unwrap(); let b1 = compressed(derivable_header(1, genesis.hash()), &an_extension(5)); - let b2 = dummy_header(2, b1.hash(), U256::from(1)); + // A header that holds its own fields, as one does once given the + // extension its commitment names. + let b2 = compressed(dummy_header(2, b1.hash(), U256::from(1)), &an_extension(6)) + .with_extension(&an_extension(6)) + .unwrap(); let hash = b1.hash(); for (number, h) in [(1, &b1), (2, &b2)] { store.put_header(h).unwrap(); From 0300a287193a2d89e6ab736cb6e1e20ef679d244 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Mon, 9 Nov 2026 13:28:58 -0800 Subject: [PATCH 058/264] execution: the Bridge writes the lock whitelist back whenever a call read it rskj's Bridge holds the lock whitelist from the moment a call reads it (WhitelistStorageProviderImpl.getLockWhitelist) and writes both cells back as the call returns (save), whatever it answers and whether or not it changed the list; a call that throws writes nothing. #290 made the peg-in do that. The whitelist methods still wrote only what they changed (#291): an address already listed, one that wasn't there to remove, a delay already set or not above the height, all read the list and left it unwritten, and so did the getters a transaction reaches before RSKIP88 made them local calls. LockWhitelist holds the list as a call reads it, and every path that reads it saves it: the one-off and unlimited adds once the address parses and the sender may change the list, a removal before it parses the address, as rskj's does, the disable delay after the sender check, and the getters, the by-address one only for an address that parses. It reads as rskj's initializeLockWhitelist does: with no one-off cell the list is empty and the unlimited cell isn't read, and the unlimited cell is read only from RSKIP87 on, which the peg-in's path didn't hold to. It is written under the activation it was read under. setLockWhitelistDisableBlockDelay reads the BTC chain's height before it asks who is calling (BridgeSupport.getBtcBlockchainBestChainHeight), which seeds a chain nothing has seeded yet. The delay now counts from the seeded chain's head, the checkpoint on an empty store, where it counted from 0. --- crates/execution/src/bridge/getters.rs | 94 +++++- crates/execution/src/bridge/governance.rs | 374 +++++++++++++++++----- crates/execution/src/bridge/mod.rs | 20 +- crates/execution/src/bridge/peg.rs | 94 +++++- crates/execution/src/bridge/storage.rs | 69 ++++ 5 files changed, 544 insertions(+), 107 deletions(-) diff --git a/crates/execution/src/bridge/getters.rs b/crates/execution/src/bridge/getters.rs index 00978eb..e7479c0 100644 --- a/crates/execution/src/bridge/getters.rs +++ b/crates/execution/src/bridge/getters.rs @@ -291,11 +291,17 @@ pub fn is_btc_tx_hash_already_processed( // was found from the *serialized* whitelist cells at mainnet #1,590,999.) /// `getLockWhitelistSize()` → int256, one-off plus unlimited entries. +/// +/// Every lock-whitelist getter that reads the whitelist writes it back as the +/// call returns, as rskj's `save()` does for whatever the call loaded (see +/// `save_lock_whitelist`). Only a getter run in a transaction keeps that +/// write, so only one reached before RSKIP88 made them local calls. pub fn get_lock_whitelist_size( ctx: &mut CTX, gas_cost: u64, + hardfork_cfg: &crate::hardfork::RskHardforkConfig, ) -> Result { - let entries = merged_whitelist(ctx); + let entries = merged_whitelist(&loaded_and_saved_whitelist(ctx, hardfork_cfg)); Ok(PrecompileOutput::new(gas_cost, abi_encode_int(entries.len() as i64))) } @@ -311,9 +317,10 @@ pub fn get_lock_whitelist_address( args: &[u8], gas_cost: u64, config: &BridgeConstants, + hardfork_cfg: &crate::hardfork::RskHardforkConfig, ) -> Result { let index = index_arg(args, "getLockWhitelistAddress")?; - let entries = merged_whitelist(ctx); + let entries = merged_whitelist(&loaded_and_saved_whitelist(ctx, hardfork_cfg)); // rskj `getLockWhitelistEntryByIndex`: a negative index is not found either. let address = match usize::try_from(index).ok().and_then(|i| entries.get(i)) { @@ -344,6 +351,7 @@ pub fn get_lock_whitelist_entry_by_address( args: &[u8], gas_cost: u64, config: &BridgeConstants, + hardfork_cfg: &crate::hardfork::RskHardforkConfig, ) -> Result { let Some(addr_str) = crate::abi::Args::new(args).string(0) else { return Err(PrecompileError::other( @@ -351,11 +359,13 @@ pub fn get_lock_whitelist_entry_by_address( )); }; + // rskj parses the address before it reads the whitelist, so one that + // doesn't parse leaves the whitelist unread and unwritten. let Some(hash160) = parse_network_address_hash160(&addr_str, config) else { return Ok(PrecompileOutput::new(gas_cost, abi_encode_int(ADDRESS_NOT_EXIST))); }; - let value = merged_whitelist(ctx) + let value = merged_whitelist(&loaded_and_saved_whitelist(ctx, hardfork_cfg)) .into_iter() .find(|(h, _)| *h == hash160) .map_or(ADDRESS_NOT_EXIST, |(_, cap)| match cap { @@ -367,16 +377,28 @@ pub fn get_lock_whitelist_entry_by_address( Ok(PrecompileOutput::new(gas_cost, abi_encode_int(value))) } +/// rskj `getLockWhitelist` for the block being run, written back as the call +/// returns. +fn loaded_and_saved_whitelist( + ctx: &mut CTX, + hardfork_cfg: &crate::hardfork::RskHardforkConfig, +) -> LockWhitelist { + let block_number = revm::context_interface::Block::number(ctx.block()).to::(); + let whitelist = LockWhitelist::load(ctx, hardfork_cfg.has_rskip87(block_number)); + whitelist.save(ctx); + whitelist +} + /// The whitelist as rskj's `LockWhitelist` holds it: one-off and unlimited /// entries in one map, ordered by hash160 ascending. `None` marks an unlimited /// entry, `Some(cap)` a one-off entry's `maxTransferValue`. -fn merged_whitelist(ctx: &mut CTX) -> Vec<([u8; 20], Option)> { - let mut entries: Vec<([u8; 20], Option)> = load_one_off_whitelist(ctx) - .0 - .into_iter() - .map(|(hash160, cap)| (hash160, Some(cap))) +fn merged_whitelist(whitelist: &LockWhitelist) -> Vec<([u8; 20], Option)> { + let mut entries: Vec<([u8; 20], Option)> = whitelist + .one_off + .iter() + .map(|&(hash160, cap)| (hash160, Some(cap))) .collect(); - entries.extend(load_unlimited_whitelist(ctx).into_iter().map(|h| (h, None))); + entries.extend(whitelist.unlimited.iter().map(|&h| (h, None))); // `UnsignedBytes.lexicographicalComparator()` over the 20-byte hash160. entries.sort_by(|a, b| a.0.cmp(&b.0)); entries @@ -1076,7 +1098,7 @@ mod tests { store_one_off_whitelist(&mut ctx, &[([0x11; 20], 500), ([0x22; 20], 600)], 1_000); store_unlimited_whitelist(&mut ctx, &[[0x33; 20]]); - let out = get_lock_whitelist_size(&mut ctx, 0).unwrap(); + let out = get_lock_whitelist_size(&mut ctx, 0, &hardforks()).unwrap(); assert_eq!(as_int(&out), 3); } @@ -1138,7 +1160,53 @@ mod tests { address: &str, config: &BridgeConstants, ) -> PrecompileOutput { - get_lock_whitelist_entry_by_address(ctx, &abi_string_arg(address), 0, config).unwrap() + get_lock_whitelist_entry_by_address(ctx, &abi_string_arg(address), 0, config, &hardforks()) + .unwrap() + } + + /// A getter that reads the whitelist writes it back as the call returns, + /// as rskj's `save()` does for whatever the call loaded (upstream #291), + /// so on a store that never held it the cells appear: the unlimited one + /// from RSKIP87 on. The by-address getter parses the address first, and + /// one that doesn't parse leaves the whitelist unread. + #[test] + fn the_whitelist_getters_write_the_whitelist_back() { + fn cells(ctx: &mut CTX) -> (String, String) { + let mut cell = + |key: &str| alloy_primitives::hex::encode(bridge_load_bytes_named(ctx, key)); + (cell(LOCK_WHITELIST_KEY), cell(UNLIMITED_LOCK_WHITELIST_KEY)) + } + let read = ("c5847fffffff".to_string(), "c0".to_string()); + let config = mainnet(); + + let mut ctx = ctx_at(1_000_000); + assert_eq!( + as_int(&get_lock_whitelist_size(&mut ctx, 0, &hardforks()).unwrap()), + 0 + ); + assert_eq!(cells(&mut ctx), read); + + let mut ctx = ctx_at(728_999); + get_lock_whitelist_size(&mut ctx, 0, &hardforks()).unwrap(); + assert_eq!( + cells(&mut ctx), + ("c5847fffffff".to_string(), String::new()), + "before RSKIP87, the one-off cell alone" + ); + + let mut ctx = ctx_at(1_000_000); + get_lock_whitelist_address(&mut ctx, &[0u8; 32], 0, &config, &hardforks()).unwrap(); + assert_eq!(cells(&mut ctx), read); + + let mut ctx = ctx_at(1_000_000); + assert_eq!( + as_int(&query_whitelist(&mut ctx, "not an address", &config)), + -1 + ); + assert_eq!(cells(&mut ctx), (String::new(), String::new()), "unread"); + let stranger = p2pkh_base58_address(&[0x99; 20], &config); + assert_eq!(as_int(&query_whitelist(&mut ctx, &stranger, &config)), -1); + assert_eq!(cells(&mut ctx), read); } /// ABI head+tail for a single `string` argument. @@ -1180,7 +1248,7 @@ mod tests { let at = |ctx: &mut _, i: u64| { let mut arg = [0u8; 32]; arg[24..32].copy_from_slice(&i.to_be_bytes()); - as_string(&get_lock_whitelist_address(ctx, &arg, 0, &config).unwrap()) + as_string(&get_lock_whitelist_address(ctx, &arg, 0, &config, &hardforks()).unwrap()) }; assert_eq!( at(&mut ctx, 0), @@ -1197,7 +1265,7 @@ mod tests { fn lock_whitelist_order_is_unsigned() { let mut ctx = ctx_at(1_000_000); store_one_off_whitelist(&mut ctx, &[([0xff; 20], 1), ([0x01; 20], 2)], 1_000); - let merged = merged_whitelist(&mut ctx); + let merged = merged_whitelist(&LockWhitelist::load(&mut ctx, true)); assert_eq!( merged.iter().map(|(h, _)| h[0]).collect::>(), vec![0x01, 0xff] diff --git a/crates/execution/src/bridge/governance.rs b/crates/execution/src/bridge/governance.rs index 902f375..ec0d90e 100644 --- a/crates/execution/src/bridge/governance.rs +++ b/crates/execution/src/bridge/governance.rs @@ -1003,9 +1003,10 @@ pub fn add_lock_whitelist_address( args: &[u8], gas_cost: u64, config: &super::constants::BridgeConstants, + hardfork_cfg: &crate::hardfork::RskHardforkConfig, tx_ctx: &crate::precompiles::BridgeTxContext, ) -> Result { - add_one_off_lock_whitelist_address(ctx, args, gas_cost, config, tx_ctx) + add_one_off_lock_whitelist_address(ctx, args, gas_cost, config, hardfork_cfg, tx_ctx) } /// rskj AddressBasedAuthorizer (minimum ONE): the tx sender must be the RSK @@ -1036,6 +1037,7 @@ pub fn add_one_off_lock_whitelist_address( args: &[u8], gas_cost: u64, config: &super::constants::BridgeConstants, + hardfork_cfg: &crate::hardfork::RskHardforkConfig, tx_ctx: &crate::precompiles::BridgeTxContext, ) -> Result { let addr_str = match abi_decode_string_arg(args, 0) { @@ -1063,19 +1065,18 @@ pub fn add_one_off_lock_whitelist_address( return Ok(encode_int_result(gas_cost, -10)); } - let (mut entries, disable_h) = load_one_off_whitelist(ctx); - - // Duplicates across BOTH whitelists (rskj LockWhitelist.isWhitelisted) - if entries.iter().any(|(h, _)| h == &hash160) - || load_unlimited_whitelist(ctx).contains(&hash160) - { - return Ok(encode_int_result(gas_cost, -1)); - } - - entries.push((hash160, max_val)); - store_one_off_whitelist(ctx, &entries, disable_h); - - Ok(encode_int_result(gas_cost, 1)) + // rskj reads the whitelist from here on, so it writes it back however the + // call ends. + let block_number = revm::context_interface::Block::number(ctx.block()).to::(); + let mut whitelist = LockWhitelist::load(ctx, hardfork_cfg.has_rskip87(block_number)); + let code = if whitelist.contains(&hash160) { + -1 + } else { + whitelist.one_off.push((hash160, max_val)); + 1 + }; + whitelist.save(ctx); + Ok(encode_int_result(gas_cost, code)) } /// `addUnlimitedLockWhitelistAddress(string address)` → int256 @@ -1092,6 +1093,7 @@ pub fn add_unlimited_lock_whitelist_address( args: &[u8], gas_cost: u64, config: &super::constants::BridgeConstants, + hardfork_cfg: &crate::hardfork::RskHardforkConfig, tx_ctx: &crate::precompiles::BridgeTxContext, ) -> Result { let addr_str = match abi_decode_string_arg(args, 0) { @@ -1108,19 +1110,18 @@ pub fn add_unlimited_lock_whitelist_address( return Ok(encode_int_result(gas_cost, -10)); } - let mut entries = load_unlimited_whitelist(ctx); - - // Duplicates across BOTH whitelists (rskj LockWhitelist.isWhitelisted) - if entries.iter().any(|h| h == &hash160) - || load_one_off_whitelist(ctx).0.iter().any(|(h, _)| h == &hash160) - { - return Ok(encode_int_result(gas_cost, -1)); - } - - entries.push(hash160); - store_unlimited_whitelist(ctx, &entries); - - Ok(encode_int_result(gas_cost, 1)) + // rskj reads the whitelist from here on, so it writes it back however the + // call ends. + let block_number = revm::context_interface::Block::number(ctx.block()).to::(); + let mut whitelist = LockWhitelist::load(ctx, hardfork_cfg.has_rskip87(block_number)); + let code = if whitelist.contains(&hash160) { + -1 + } else { + whitelist.unlimited.push(hash160); + 1 + }; + whitelist.save(ctx); + Ok(encode_int_result(gas_cost, code)) } /// `removeLockWhitelistAddress(string address)` → int256 @@ -1136,6 +1137,7 @@ pub fn remove_lock_whitelist_address( args: &[u8], gas_cost: u64, config: &super::constants::BridgeConstants, + hardfork_cfg: &crate::hardfork::RskHardforkConfig, tx_ctx: &crate::precompiles::BridgeTxContext, ) -> Result { if !is_whitelist_change_authorized(config, tx_ctx.rsk_sender) { @@ -1147,30 +1149,17 @@ pub fn remove_lock_whitelist_address( Err(_) => return Ok(encode_int_result(gas_cost, -2)), }; - let hash160 = match btc_address_to_hash160(&addr_str, config) { - Ok(h) => h, - Err(_) => return Ok(encode_int_result(gas_cost, -2)), + // rskj reads the whitelist before it parses the address, so it writes it + // back however the call ends, for an address that doesn't parse too. + let block_number = revm::context_interface::Block::number(ctx.block()).to::(); + let mut whitelist = LockWhitelist::load(ctx, hardfork_cfg.has_rskip87(block_number)); + let code = match btc_address_to_hash160(&addr_str, config) { + Err(_) => -2, + Ok(hash160) if whitelist.remove(&hash160) => 1, + Ok(_) => -1, }; - - // Try to remove from one-off whitelist first - let (mut one_off, disable_h) = load_one_off_whitelist(ctx); - let before_len = one_off.len(); - one_off.retain(|(h, _)| h != &hash160); - if one_off.len() < before_len { - store_one_off_whitelist(ctx, &one_off, disable_h); - return Ok(encode_int_result(gas_cost, 1)); - } - - // Try unlimited whitelist - let mut unlimited = load_unlimited_whitelist(ctx); - let before_len = unlimited.len(); - unlimited.retain(|h| h != &hash160); - if unlimited.len() < before_len { - store_unlimited_whitelist(ctx, &unlimited); - return Ok(encode_int_result(gas_cost, 1)); - } - - Ok(encode_int_result(gas_cost, -1)) + whitelist.save(ctx); + Ok(encode_int_result(gas_cost, code)) } /// `setLockWhitelistDisableBlockDelay(int256 delay)` → int256 @@ -1184,36 +1173,48 @@ pub fn set_lock_whitelist_disable_block_delay( args: &[u8], gas_cost: u64, config: &super::constants::BridgeConstants, + hardfork_cfg: &crate::hardfork::RskHardforkConfig, + use_v2: bool, tx_ctx: &crate::precompiles::BridgeTxContext, ) -> Result { - if !is_whitelist_change_authorized(config, tx_ctx.rsk_sender) { - return Ok(encode_int_result(gas_cost, -10)); - } - - let (entries, disable_h) = load_one_off_whitelist(ctx); - if disable_h < i32::MAX { - return Ok(encode_int_result(gas_cost, -1)); // delay already set - } - - // rskj `disableBlockDelayBI.intValueExact()`: a delay no int holds - // throws. The sum is Java int arithmetic, so one that wraps is not above - // the height either. - let delay = crate::abi::Args::new(args) - .int(0) - .and_then(|delay| delay.int_value_exact()) - .ok_or_else(|| { - PrecompileError::other("setLockWhitelistDisableBlockDelay: the delay is not an int") - })?; + // rskj `BridgeSupport.setLockWhitelistDisableBlockDelay` reads the BTC + // chain's height before anything asks who is calling, and seeds the chain + // where nothing has yet (`ensureBtcBlockStore`), whoever calls. + let block_number = revm::context_interface::Block::number(ctx.block()).to::(); + let rskip199 = hardfork_cfg.has_rskip199(block_number); + super::btc_chain::ensure_btc_chain_seeded(ctx, config, use_v2, rskip199); let best_height = super::btc_store::load_chain_head(ctx) .map(|h| h.height as i32) .unwrap_or(0); - if delay.wrapping_add(best_height) <= best_height { - return Ok(encode_int_result(gas_cost, -2)); // invalid delay - } - store_one_off_whitelist(ctx, &entries, best_height.wrapping_add(delay)); + if !is_whitelist_change_authorized(config, tx_ctx.rsk_sender) { + return Ok(encode_int_result(gas_cost, -10)); + } - Ok(encode_int_result(gas_cost, 1)) + // rskj reads the whitelist from here on, so it writes it back however the + // call ends but a throw. + let mut whitelist = LockWhitelist::load(ctx, hardfork_cfg.has_rskip87(block_number)); + let code = if whitelist.disable_height < i32::MAX { + -1 // delay already set + } else { + // rskj `disableBlockDelayBI.intValueExact()`: a delay no int holds + // throws. The sum is Java int arithmetic, so one that wraps is not + // above the height either. + let delay = crate::abi::Args::new(args) + .int(0) + .and_then(|delay| delay.int_value_exact()) + .ok_or_else(|| { + PrecompileError::other("setLockWhitelistDisableBlockDelay: the delay is not an int") + })?; + if delay.wrapping_add(best_height) <= best_height { + -2 // invalid delay + } else { + whitelist.disable_height = best_height.wrapping_add(delay); + 1 + } + }; + whitelist.save(ctx); + Ok(encode_int_result(gas_cost, code)) } // --------------------------------------------------------------------------- @@ -1492,12 +1493,13 @@ mod tests { use crate::abi::encode::{self, Arg}; use revm::MainContext; let config = super::super::constants::BridgeConstants::mainnet(); + let hardforks = crate::hardfork::RskHardforkConfig::mainnet(); let tx_ctx = crate::precompiles::BridgeTxContext::default(); let mut ctx = revm::Context::mainnet().with_chain(crate::raw_storage::RskChainExt::default()); let mut call = |address: &str, value: [u8; 32]| { let args = encode::args(&[Arg::Bytes(address.as_bytes()), Arg::Word(value)]); - add_one_off_lock_whitelist_address(&mut ctx, &args, 0, &config, &tx_ctx) + add_one_off_lock_whitelist_address(&mut ctx, &args, 0, &config, &hardforks, &tx_ctx) .map(|out| i64::from_be_bytes(out.bytes[24..32].try_into().unwrap())) }; let address = "1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2"; @@ -1511,4 +1513,228 @@ mod tests { "then the sender" ); } + + /// A Bridge call that reads the whitelist writes it back as the call + /// returns, whatever it answers (upstream #291): `save()` writes whatever + /// the call loaded. The changes read it after the sender check, the adds + /// only once the address parses, and a removal before it parses. On a store + /// that never held the whitelist, the cells show which answers read it. + #[test] + fn a_whitelist_change_writes_the_whitelist_back_once_it_has_read_it() { + use crate::abi::encode::{self, Arg}; + use crate::precompiles::BridgeTxContext; + use revm::MainContext; + + /// The call's answer, then the one-off and unlimited cells after it. + fn run( + mut ctx: CTX, + call: impl FnOnce(&mut CTX) -> Result, + ) -> (i64, String, String) { + let out = call(&mut ctx).unwrap(); + let code = i64::from_be_bytes(out.bytes[24..32].try_into().unwrap()); + let mut cell = + |key: &str| alloy_primitives::hex::encode(bridge_load_bytes_named(&mut ctx, key)); + ( + code, + cell(LOCK_WHITELIST_KEY), + cell(UNLIMITED_LOCK_WHITELIST_KEY), + ) + } + let fresh = + || revm::Context::mainnet().with_chain(crate::raw_storage::RskChainExt::default()); + let config = super::super::constants::BridgeConstants::mainnet(); + // Block 0, where regtest has RSKIP87 and mainnet doesn't. + let (all, mainnet) = ( + crate::hardfork::RskHardforkConfig::regtest(), + crate::hardfork::RskHardforkConfig::mainnet(), + ); + let authorizer = + alloy_primitives::hex::decode(config.whitelist_authorizer_keys[0]).unwrap(); + let authorized = BridgeTxContext { + rsk_sender: super::super::federation::rsk_address_from_public_key(&authorizer).unwrap(), + ..BridgeTxContext::default() + }; + let stranger = BridgeTxContext::default(); + let address = "1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2"; + let hash160 = btc_address_to_hash160(address, &config).unwrap(); + let string = |s: &str| encode::args(&[Arg::Bytes(s.as_bytes())]); + let one_off = + |s: &str| encode::args(&[Arg::Bytes(s.as_bytes()), Arg::Word(encode::int(1_000))]); + let delay = |n: i64| encode::args(&[Arg::Word(encode::int(n))]); + let read = |code: i64| (code, "c5847fffffff".to_string(), "c0".to_string()); + let unread = |code: i64| (code, String::new(), String::new()); + // The address on the one-off list, in a store with no unlimited cell. + let listed = || { + let mut ctx = fresh(); + store_one_off_whitelist(&mut ctx, &[(hash160, 1_000)], i32::MAX); + ctx + }; + let listed_cell = alloy_primitives::hex::encode(serialize_one_off_whitelist( + &[(hash160, 1_000)], + i32::MAX, + )); + + // The address on the unlimited list. + let unlimited_listed = || { + let mut ctx = fresh(); + store_one_off_whitelist(&mut ctx, &[], i32::MAX); + store_unlimited_whitelist(&mut ctx, &[hash160]); + ctx + }; + let unlimited_cell = + alloy_primitives::hex::encode(serialize_unlimited_whitelist(&[hash160])); + + let add = |ctx, args: Vec, tx: &BridgeTxContext, hardforks| { + run(ctx, |ctx| { + add_one_off_lock_whitelist_address(ctx, &args, 0, &config, hardforks, tx) + }) + }; + assert_eq!(add(fresh(), one_off(address), &stranger, &all), unread(-10)); + assert_eq!( + add(fresh(), one_off("not an address"), &authorized, &all), + unread(-2) + ); + assert_eq!( + add(listed(), one_off(address), &authorized, &all), + (-1, listed_cell.clone(), "c0".to_string()), + "already listed, and written back" + ); + assert_eq!( + add(listed(), one_off(address), &authorized, &mainnet), + (-1, listed_cell.clone(), String::new()) + ); + assert_eq!( + add(unlimited_listed(), one_off(address), &authorized, &all), + (-1, "c5847fffffff".to_string(), unlimited_cell.clone()), + "listed as unlimited" + ); + let unlimited = |ctx, args: Vec| { + run(ctx, |ctx| { + add_unlimited_lock_whitelist_address(ctx, &args, 0, &config, &all, &authorized) + }) + }; + assert_eq!(unlimited(fresh(), string("not an address")), unread(-2)); + assert_eq!( + unlimited(listed(), string(address)), + (-1, listed_cell, "c0".to_string()) + ); + assert_eq!( + unlimited(unlimited_listed(), string(address)), + (-1, "c5847fffffff".to_string(), unlimited_cell) + ); + + let remove = |ctx, args: Vec, tx: &BridgeTxContext, hardforks| { + run(ctx, |ctx| { + remove_lock_whitelist_address(ctx, &args, 0, &config, hardforks, tx) + }) + }; + assert_eq!( + remove(fresh(), string(address), &stranger, &all), + unread(-10) + ); + assert_eq!( + remove(fresh(), string("not an address"), &authorized, &all), + read(-2), + "read before the address is parsed" + ); + assert_eq!( + remove(fresh(), string(address), &authorized, &all), + read(-1) + ); + assert_eq!( + remove(fresh(), string(address), &authorized, &mainnet), + (-1, "c5847fffffff".to_string(), String::new()), + "the unlimited cell is written from RSKIP87 on" + ); + assert_eq!( + remove(unlimited_listed(), string(address), &authorized, &all), + read(1) + ); + + let set = |ctx, args: Vec, tx: &BridgeTxContext, hardforks| { + run(ctx, |ctx| { + set_lock_whitelist_disable_block_delay(ctx, &args, 0, &config, hardforks, true, tx) + }) + }; + assert_eq!(set(fresh(), delay(100), &stranger, &all), unread(-10)); + assert_eq!(set(fresh(), delay(0), &authorized, &all), read(-2)); + assert_eq!( + set(fresh(), delay(0), &authorized, &mainnet), + (-2, "c5847fffffff".to_string(), String::new()) + ); + let disabled = || { + let mut ctx = fresh(); + store_one_off_whitelist(&mut ctx, &[], 50); + ctx + }; + assert_eq!( + set(disabled(), delay(100), &authorized, &all), + (-1, "c132".to_string(), "c0".to_string()) + ); + let mut ctx = fresh(); + let beyond_an_int = encode::args(&[Arg::Word(encode::int(1 << 31))]); + assert!(set_lock_whitelist_disable_block_delay( + &mut ctx, + &beyond_an_int, + 0, + &config, + &all, + true, + &authorized + ) + .is_err()); + assert!( + bridge_load_bytes_named(&mut ctx, LOCK_WHITELIST_KEY).is_empty(), + "a throw ends the call before it saves" + ); + } + + /// rskj `setLockWhitelistDisableBlockDelay` reads the BTC chain's height + /// first, which seeds a chain nothing has seeded yet, whoever calls; the + /// disable height is then counted from the checkpoint. + #[test] + fn the_disable_delay_counts_from_the_seeded_btc_chain() { + use crate::abi::encode::{self, Arg}; + use revm::MainContext; + let config = super::super::constants::BridgeConstants::mainnet(); + let hardforks = crate::hardfork::RskHardforkConfig::regtest(); + let checkpoint = config.btc_checkpoint.unwrap().1 as i32; + let authorizer = + alloy_primitives::hex::decode(config.whitelist_authorizer_keys[0]).unwrap(); + let authorized = crate::precompiles::BridgeTxContext { + rsk_sender: super::super::federation::rsk_address_from_public_key(&authorizer).unwrap(), + ..crate::precompiles::BridgeTxContext::default() + }; + let args = encode::args(&[Arg::Word(encode::int(100))]); + let fresh = + || revm::Context::mainnet().with_chain(crate::raw_storage::RskChainExt::default()); + + let mut ctx = fresh(); + let stranger = crate::precompiles::BridgeTxContext::default(); + set_lock_whitelist_disable_block_delay( + &mut ctx, &args, 0, &config, &hardforks, true, &stranger, + ) + .unwrap(); + assert_eq!( + super::super::btc_store::load_chain_head(&mut ctx).map(|head| head.height as i32), + Some(checkpoint), + "seeded before the sender check" + ); + + let mut ctx = fresh(); + set_lock_whitelist_disable_block_delay( + &mut ctx, + &args, + 0, + &config, + &hardforks, + true, + &authorized, + ) + .unwrap(); + assert_eq!( + LockWhitelist::load(&mut ctx, true).disable_height, + checkpoint + 100 + ); + } } diff --git a/crates/execution/src/bridge/mod.rs b/crates/execution/src/bridge/mod.rs index e2944a5..6623019 100644 --- a/crates/execution/src/bridge/mod.rs +++ b/crates/execution/src/bridge/mod.rs @@ -941,11 +941,13 @@ fn execute_method( "addFederatorPublicKeyMultikey" => governance::add_federator_public_key_multikey(ctx, args, gas_cost, config, hardfork_cfg, tx_ctx), "voteFeePerKbChange" => governance::vote_fee_per_kb_change(ctx, args, gas_cost, config, tx_ctx), "increaseLockingCap" => governance::increase_locking_cap(ctx, args, gas_cost, config, tx_ctx), - "addLockWhitelistAddress" => governance::add_lock_whitelist_address(ctx, args, gas_cost, config, tx_ctx), - "addOneOffLockWhitelistAddress" => governance::add_one_off_lock_whitelist_address(ctx, args, gas_cost, config, tx_ctx), - "addUnlimitedLockWhitelistAddress" => governance::add_unlimited_lock_whitelist_address(ctx, args, gas_cost, config, tx_ctx), - "removeLockWhitelistAddress" => governance::remove_lock_whitelist_address(ctx, args, gas_cost, config, tx_ctx), - "setLockWhitelistDisableBlockDelay" => governance::set_lock_whitelist_disable_block_delay(ctx, args, gas_cost, config, tx_ctx), + "addLockWhitelistAddress" => governance::add_lock_whitelist_address(ctx, args, gas_cost, config, hardfork_cfg, tx_ctx), + "addOneOffLockWhitelistAddress" => governance::add_one_off_lock_whitelist_address(ctx, args, gas_cost, config, hardfork_cfg, tx_ctx), + "addUnlimitedLockWhitelistAddress" => governance::add_unlimited_lock_whitelist_address(ctx, args, gas_cost, config, hardfork_cfg, tx_ctx), + "removeLockWhitelistAddress" => governance::remove_lock_whitelist_address(ctx, args, gas_cost, config, hardfork_cfg, tx_ctx), + "setLockWhitelistDisableBlockDelay" => { + governance::set_lock_whitelist_disable_block_delay(ctx, args, gas_cost, config, hardfork_cfg, use_v2, tx_ctx) + } // Transaction-callable read-only "getActivePowpegRedeemScript" => governance::get_active_powpeg_redeem_script(ctx, gas_cost), @@ -984,9 +986,11 @@ fn execute_method( "getProposedFederationCreationTime" => getters::get_proposed_federation_creation_time(ctx, gas_cost), "getProposedFederationCreationBlockNumber" => getters::get_proposed_federation_creation_block_number(ctx, gas_cost), "getProposedFederatorPublicKeyOfType" => getters::get_proposed_federator_public_key_of_type(ctx, args, gas_cost), - "getLockWhitelistSize" => getters::get_lock_whitelist_size(ctx, gas_cost), - "getLockWhitelistAddress" => getters::get_lock_whitelist_address(ctx, args, gas_cost, config), - "getLockWhitelistEntryByAddress" => getters::get_lock_whitelist_entry_by_address(ctx, args, gas_cost, config), + "getLockWhitelistSize" => getters::get_lock_whitelist_size(ctx, gas_cost, hardfork_cfg), + "getLockWhitelistAddress" => getters::get_lock_whitelist_address(ctx, args, gas_cost, config, hardfork_cfg), + "getLockWhitelistEntryByAddress" => { + getters::get_lock_whitelist_entry_by_address(ctx, args, gas_cost, config, hardfork_cfg) + } "getBtcTxHashProcessedHeight" => getters::get_btc_tx_hash_processed_height(ctx, args, gas_cost), "isBtcTxHashAlreadyProcessed" => getters::is_btc_tx_hash_already_processed(ctx, args, gas_cost), "getBtcBlockchainInitialBlockHeight" => btc_chain::get_initial_block_height(gas_cost, config), diff --git a/crates/execution/src/bridge/peg.rs b/crates/execution/src/bridge/peg.rs index 0cbd042..301212d 100644 --- a/crates/execution/src/bridge/peg.rs +++ b/crates/execution/src/bridge/peg.rs @@ -1464,14 +1464,16 @@ fn whitelist_allows_and_consume( height: i32, rskip87: bool, ) -> bool { - // rskj `getLockWhitelist()` deserializes BOTH cells together, so reaching - // this function reads both even where the decision needs only one. - let (mut one_off, disable_h) = load_one_off_whitelist(ctx); - let unlimited = load_unlimited_whitelist(ctx); - let disabled = i64::from(height) > (disable_h as i64); - - let allowed = if let Some(pos) = one_off.iter().position(|(h, _)| h == sender_hash160) { - let allowed = disabled || amount <= one_off[pos].1; + let mut whitelist = super::storage::LockWhitelist::load(ctx, rskip87); + let disabled = height > whitelist.disable_height; + + let one_off = whitelist + .one_off + .iter() + .position(|(h, _)| h == sender_hash160); + let allowed = if let Some(pos) = one_off { + let entry_max = whitelist.one_off[pos].1; + let allowed = disabled || amount <= entry_max; if allowed { // A one-off entry is spent here and gone from the next block's // state, with nothing logged to say it was ever used. @@ -1480,25 +1482,25 @@ fn whitelist_allows_and_consume( vec![ ("senderHash160", FieldValue::Bytes(sender_hash160.to_vec())), ("amountSat", FieldValue::U64(amount)), - ("entryMaxSat", FieldValue::U64(one_off[pos].1)), + ("entryMaxSat", FieldValue::U64(entry_max)), ("whitelistDisabled", FieldValue::Flag(disabled)), ], ); // rskj consumes the matching one-off entry only on success. - one_off.remove(pos); + whitelist.one_off.remove(pos); } allowed } else if disabled { true } else { - unlimited.contains(sender_hash160) + whitelist.unlimited.contains(sender_hash160) }; // Unconditional, including on rejection: rskj's save() writes the // whitelist back because the call loaded it, not because it changed it. // See `save_lock_whitelist` -- this write is why testnet #6,602 has a // `lockWhitelist` cell at all. - super::storage::save_lock_whitelist(ctx, &one_off, disable_h, &unlimited, rskip87); + whitelist.save(ctx); allowed } @@ -5919,6 +5921,9 @@ mod tests { fn an_unlimited_entry_survives_the_lock_it_authorises() { let mut ctx = queue_ctx(); let user = [0x33u8; 20]; + // Every save writes the one-off cell, so it is there beside the + // unlimited one on any chain. + store_one_off_whitelist(&mut ctx, &[], i32::MAX); store_unlimited_whitelist(&mut ctx, &[user]); assert!(whitelist_allows_and_consume(&mut ctx, &user, 1_000_000, 100, true)); @@ -5929,6 +5934,71 @@ mod tests { assert_eq!(load_unlimited_whitelist(&mut ctx), vec![user]); } + /// rskj reads the unlimited cell only where the one-off cell exists + /// (`initializeLockWhitelist`), so without one an unlimited entry + /// authorises nothing, and the save writes the empty whitelist it read. + /// No chain holds that state, since every save writes the one-off cell. + #[test] + fn without_a_one_off_cell_the_unlimited_cell_is_not_read() { + let mut ctx = queue_ctx(); + let user = [0x33u8; 20]; + store_unlimited_whitelist(&mut ctx, &[user]); + + assert!(!whitelist_allows_and_consume( + &mut ctx, &user, 1_000, 100, true + )); + assert_eq!( + to_hex(&bridge_load_bytes_named(&mut ctx, LOCK_WHITELIST_KEY)), + "c5847fffffff" + ); + assert_eq!( + to_hex(&bridge_load_bytes_named( + &mut ctx, + UNLIMITED_LOCK_WHITELIST_KEY + )), + "c0" + ); + } + + /// Before RSKIP87 rskj reads the one-off cell alone, so an unlimited + /// entry authorises nothing there, and its cell is neither read nor + /// written. + #[test] + fn before_rskip87_the_unlimited_cell_is_not_read() { + let mut ctx = queue_ctx(); + let user = [0x33u8; 20]; + store_one_off_whitelist(&mut ctx, &[], i32::MAX); + store_unlimited_whitelist(&mut ctx, &[user]); + + assert!(!whitelist_allows_and_consume( + &mut ctx, &user, 1_000, 100, false + )); + assert_eq!(load_unlimited_whitelist(&mut ctx), vec![user]); + } + + /// The whitelist turns off only past its disable height (rskj + /// `height > disableBlockHeight`): at the height itself it still gates. + #[test] + fn at_its_disable_height_the_whitelist_still_gates() { + let mut ctx = queue_ctx(); + store_one_off_whitelist(&mut ctx, &[], 50); + + assert!(!whitelist_allows_and_consume( + &mut ctx, + &[0x99; 20], + 1_000, + 50, + true + )); + assert!(whitelist_allows_and_consume( + &mut ctx, + &[0x99; 20], + 1_000, + 51, + true + )); + } + /// Past the disable height the whitelist stops gating, but it is still /// loaded -- and so still saved. #[test] diff --git a/crates/execution/src/bridge/storage.rs b/crates/execution/src/bridge/storage.rs index 89a3d41..3149f6b 100644 --- a/crates/execution/src/bridge/storage.rs +++ b/crates/execution/src/bridge/storage.rs @@ -404,6 +404,75 @@ pub fn save_lock_whitelist( } } +/// rskj's `LockWhitelist` as a Bridge call holds it once it has read it +/// (`WhitelistStorageProviderImpl.getLockWhitelist`): the one-off entries with +/// the disable height, and the unlimited entries. rskj writes it back when the +/// call returns, whatever the call answers and whether or not it changed it +/// ([`save_lock_whitelist`]), so every path that reads it saves it. +pub struct LockWhitelist { + pub one_off: Vec<([u8; 20], u64)>, + pub disable_height: i32, + pub unlimited: Vec<[u8; 20]>, + /// The activation it was read under, which `save` writes under too. + rskip87: bool, +} + +impl LockWhitelist { + /// rskj `initializeLockWhitelist`. With no one-off cell the whitelist is + /// empty and the unlimited cell is never read; with one, the unlimited + /// cell is read from RSKIP87 on. + pub fn load(ctx: &mut CTX, rskip87: bool) -> Self { + let data = bridge_load_bytes_named(ctx, LOCK_WHITELIST_KEY); + if data.is_empty() { + return Self { + one_off: Vec::new(), + disable_height: i32::MAX, + unlimited: Vec::new(), + rskip87, + }; + } + let (one_off, disable_height) = + deserialize_one_off_whitelist(&data).unwrap_or((Vec::new(), i32::MAX)); + let unlimited = if rskip87 { + load_unlimited_whitelist(ctx) + } else { + Vec::new() + }; + Self { + one_off, + disable_height, + unlimited, + rskip87, + } + } + + /// rskj `LockWhitelist.isWhitelisted`: in either list. + pub fn contains(&self, hash160: &[u8; 20]) -> bool { + self.one_off.iter().any(|(h, _)| h == hash160) || self.unlimited.contains(hash160) + } + + /// rskj `LockWhitelist.remove`: out of whichever list holds it, and + /// whether one did. + pub fn remove(&mut self, hash160: &[u8; 20]) -> bool { + let held = self.one_off.len() + self.unlimited.len(); + self.one_off.retain(|(h, _)| h != hash160); + self.unlimited.retain(|h| h != hash160); + self.one_off.len() + self.unlimited.len() < held + } + + /// rskj `WhitelistStorageProviderImpl.save`, as the call returns: both + /// cells, the unlimited one from RSKIP87 on. + pub fn save(&self, ctx: &mut CTX) { + save_lock_whitelist( + ctx, + &self.one_off, + self.disable_height, + &self.unlimited, + self.rskip87, + ); + } +} + /// Load the unlimited lock whitelist from Bridge storage. pub fn load_unlimited_whitelist(ctx: &mut CTX) -> Vec<[u8; 20]> { let data = bridge_load_bytes_named(ctx, UNLIMITED_LOCK_WHITELIST_KEY); From 9a9a01aec187fb199dc41bdb0f0c281814220279 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Mon, 9 Nov 2026 15:15:35 -0800 Subject: [PATCH 059/264] Bridge: the BTC methods seed the BTC store as RSKj's ensureBtcBlockStore RSKj runs BridgeSupport.ensureBtcBlockStore before every read of the BTC chain, and it seeds an empty store. rustock's getters read the chain head without seeding, so on an empty store (regtest, where the RSKIP220 getters are open to transactions from Iris) they answered from nothing and left the state as it was, where RSKj seeds it. Now these seed first, at RSKj's points: the best chain height, the best block header, a header by hash or height and a parent header, the hash at a depth, the block locator, getStateForDebugging, the coinbase registration (before anything else) and getBtcTransactionConfirmations' cost (once its arguments parse). setLockWhitelistDisableBlockDelay uses the same helper, which takes the layout (RSKIP454) and the height index (RSKIP199) from the block. Seeding is RSKj's two steps, each setting the head: checkIfInitialized heads an empty store with the genesis block, then a store still at its genesis block takes the checkpoint (CheckpointManager.checkpoint). From RSKIP199 both heads are in the height index; rustock set the head once and indexed only the checkpoint. And the store's writes follow RSKj's repositories. RSKj's BTC store (RepositoryBtcBlockStoreWithCache.put, setChainHead) writes straight into the repository the Bridge was given, where the storage provider writes only when a method returns. A transaction's own call to a precompile is given the transaction's repository, which TransactionExecutor.go commits however the call ended. So a top-level call that seeds the store and then throws or runs out of gas keeps the seeded blocks and head, but not the height index, which goes through the provider; rustock dropped it all with the call. RawStorage gains kept writes, which neither discard_call nor discard_tx drops, and the BTC store makes them from a transaction's own call (journal depth 1, or 0 for the cost before RSKIP136). A contract's call keeps nothing of a call that fails, as Program.callToPrecompiledAddress rolls its repository back. Before RSKIP136, a call short of its cost now settles what the cost wrote, which was left pending. Tests: each getter on an empty store; regtest's genesis by height; the two seeding steps and their index entries; what a transaction's own call and a contract's keep of a call that fails; RawStorage's kept writes; the coinbase registration's early return; the confirmations cost on an empty store; and through the executor, a top-level call that throws, one out of gas before and after RSKIP136 and one that returns, with the cells' bytes in each block's layout. --- crates/execution/src/bridge/btc_chain.rs | 321 +++++++++++++++++++--- crates/execution/src/bridge/btc_store.rs | 23 +- crates/execution/src/bridge/getters.rs | 20 +- crates/execution/src/bridge/governance.rs | 16 +- crates/execution/src/bridge/mod.rs | 57 +++- crates/execution/src/bridge/tx.rs | 39 +++ crates/execution/src/executor.rs | 160 +++++++++++ crates/execution/src/raw_storage.rs | 90 +++++- 8 files changed, 654 insertions(+), 72 deletions(-) diff --git a/crates/execution/src/bridge/btc_chain.rs b/crates/execution/src/bridge/btc_chain.rs index eaeb080..0144337 100644 --- a/crates/execution/src/bridge/btc_chain.rs +++ b/crates/execution/src/bridge/btc_chain.rs @@ -410,51 +410,67 @@ pub fn main_chain_block_at_height( } } -/// Seed the bridge BTC chain on first use. rskj does this in two steps that -/// both persist: `RepositoryBtcBlockStoreWithCache.checkIfInitialized` writes -/// the BTC GENESIS stored block as chain head, then +/// Seed the bridge BTC chain on first use, in rskj's two steps, each of which +/// sets the head. `RepositoryBtcBlockStoreWithCache.checkIfInitialized` makes +/// a store with no head take the BTC GENESIS stored block as its head; then /// `BridgeSupport.ensureBtcBlockStore` runs bitcoinj's -/// `CheckpointManager.checkpoint`, which adds the checkpoint block (see -/// `BridgeConstants::btc_checkpoint`) and moves the chain head to it — BOTH -/// stored-block entries remain in bridge storage (mainnet block #457). +/// `CheckpointManager.checkpoint` on a store whose head is still the genesis +/// block, which adds the checkpoint block (see +/// `BridgeConstants::btc_checkpoint`) and moves the head to it. BOTH +/// stored-block entries remain in bridge storage (mainnet block #457), and +/// from RSKIP199 both heads are in the height index. pub fn ensure_btc_chain_seeded( ctx: &mut CTX, config: &BridgeConstants, use_v2: bool, rskip199: bool, ) { - if load_chain_head(ctx).is_some() { + let head = load_chain_head(ctx); + // rskj checkpoints a store whose head is still its genesis block, the + // only block at height 0, so a head above it has been through both steps. + if head.as_ref().is_some_and(|head| head.height > 0) { return; } + if head.is_none() { + let network = match config.btc_network { + super::constants::BtcNetwork::Mainnet => bitcoin::Network::Bitcoin, + super::constants::BtcNetwork::Testnet => bitcoin::Network::Testnet, + super::constants::BtcNetwork::Regtest => bitcoin::Network::Regtest, + }; + let genesis = bitcoin::constants::genesis_block(network).header; + let stored = StoredBlock::new(genesis, 0, compute_work(genesis.bits)); + put_stored_block(ctx, &stored, use_v2); + set_chain_head(ctx, &stored, use_v2, rskip199); + } - let network = match config.btc_network { - super::constants::BtcNetwork::Mainnet => bitcoin::Network::Bitcoin, - super::constants::BtcNetwork::Testnet => bitcoin::Network::Testnet, - super::constants::BtcNetwork::Regtest => bitcoin::Network::Regtest, + let Some((header_hex, height, chain_work_hex)) = config.btc_checkpoint else { + return; }; - let genesis = bitcoin::constants::genesis_block(network); - let genesis_stored = StoredBlock::new(genesis.header, 0, compute_work(genesis.header.bits)); - put_stored_block(ctx, &genesis_stored, use_v2); - - let head = match config.btc_checkpoint { - Some((header_hex, height, chain_work_hex)) => { - let bytes = match alloy_primitives::hex::decode(header_hex) { - Ok(b) => b, - Err(_) => return, - }; - let header: BtcHeader = match deserialize(&bytes) { - Ok(h) => h, - Err(_) => return, - }; - let work = U256::from_be_slice(&alloy_primitives::hex::decode(chain_work_hex).unwrap_or_default()); - let checkpoint = StoredBlock::new(header, height, work); - put_stored_block(ctx, &checkpoint, use_v2); - checkpoint - } - None => genesis_stored, + let Some(header) = alloy_primitives::hex::decode(header_hex) + .ok() + .and_then(|bytes| deserialize::(&bytes).ok()) + else { + return; }; + let work = + U256::from_be_slice(&alloy_primitives::hex::decode(chain_work_hex).unwrap_or_default()); + let checkpoint = StoredBlock::new(header, height, work); + put_stored_block(ctx, &checkpoint, use_v2); + set_chain_head(ctx, &checkpoint, use_v2, rskip199); +} - set_chain_head(ctx, &head, use_v2, rskip199); +/// rskj `BridgeSupport.ensureBtcBlockStore`, which every Bridge method that +/// reads the BTC chain runs before it reads, whoever calls and whatever the +/// method then answers: a store nothing has seeded yet gets its genesis +/// block and checkpoint ([`ensure_btc_chain_seeded`]), in the block's layout. +pub fn ensure_btc_block_store( + ctx: &mut CTX, + config: &BridgeConstants, + hardfork_cfg: &RskHardforkConfig, +) { + let block_number = revm::context_interface::Block::number(ctx.block()).to::(); + let use_v2 = hardfork_cfg.has_stored_block_v2(block_number); + ensure_btc_chain_seeded(ctx, config, use_v2, hardfork_cfg.has_rskip199(block_number)); } pub fn receive_headers( @@ -552,7 +568,6 @@ pub fn receive_headers( // BTC chain query methods // --------------------------------------------------------------------------- -/// `getBtcBlockchainBestChainHeight()` → int256 /// `getBtcBlockchainBlockLocator()` → string[], a bitcoin-style block locator /// over the Bridge's BTC header chain. /// @@ -577,6 +592,7 @@ pub fn get_block_locator( /// rskj `maxHashesToInform`. const MAX_HASHES: usize = 100; + ensure_btc_block_store(ctx, config, hardfork_cfg); let Some(head) = load_chain_head(ctx) else { return Err(PrecompileError::other("getBtcBlockchainBlockLocator: no BTC chain head")); }; @@ -674,6 +690,7 @@ pub fn get_block_hash_at_depth( .int_value(), ); + ensure_btc_block_store(ctx, config, hardfork_cfg); let head = load_chain_head(ctx).ok_or_else(|| { PrecompileError::other("getBtcBlockchainBlockHashAtDepth: no BTC chain head") })?; @@ -709,10 +726,14 @@ fn lowest_stored_height(config: &BridgeConstants) -> Option { config.btc_checkpoint.map(|(_, height, _)| height) } +/// `getBtcBlockchainBestChainHeight()` → int256, the BTC chain head's height. pub fn get_best_chain_height( ctx: &mut CTX, gas_cost: u64, + config: &BridgeConstants, + hardfork_cfg: &RskHardforkConfig, ) -> Result { + ensure_btc_block_store(ctx, config, hardfork_cfg); let height = match load_chain_head(ctx) { Some(head) => head.height as i64, None => 0, @@ -720,18 +741,23 @@ pub fn get_best_chain_height( Ok(encode_int_result(gas_cost, height)) } -/// `getBtcBlockchainBestBlockHeader()` → bytes +/// `getBtcBlockchainBestBlockHeader()` → bytes, the BTC chain head's header. pub fn get_best_block_header( ctx: &mut CTX, gas_cost: u64, + config: &BridgeConstants, + hardfork_cfg: &RskHardforkConfig, ) -> Result { - match load_chain_head(ctx) { - Some(head) => { - let header_bytes = serialize(&head.header); - Ok(PrecompileOutput::new(gas_cost, encode_abi_bytes(&header_bytes).into())) - } - None => Ok(PrecompileOutput::new(gas_cost, Bytes::new())), - } + ensure_btc_block_store(ctx, config, hardfork_cfg); + // A seeded store always has a head; rskj's `serializeBlockHeader` of none + // is `EMPTY_BYTE_ARRAY`, ABI-encoded like any other `byte[]`. + let header_bytes = load_chain_head(ctx) + .map(|head| serialize(&head.header)) + .unwrap_or_default(); + Ok(PrecompileOutput::new( + gas_cost, + encode_abi_bytes(&header_bytes).into(), + )) } /// `getBtcBlockchainBlockHeaderByHash(bytes32)` → bytes @@ -739,11 +765,14 @@ pub fn get_block_header_by_hash( ctx: &mut CTX, args: &[u8], gas_cost: u64, + config: &BridgeConstants, + hardfork_cfg: &RskHardforkConfig, ) -> Result { let hash = crate::abi::Args::new(args) .bytes32(0) .ok_or_else(|| PrecompileError::other("getBlockHeaderByHash: no hash"))?; let hash = b256_to_bitcoin_hash(&alloy_primitives::B256::from(hash)); + ensure_btc_block_store(ctx, config, hardfork_cfg); // rskj's `serializeBlockHeader` answers a block it does not hold with // `EMPTY_BYTE_ARRAY`, which `Bridge.execute` ABI-encodes like any other @@ -777,6 +806,7 @@ pub fn get_block_header_by_height( .int(0) .ok_or_else(|| PrecompileError::other("getBlockHeaderByHeight: no height"))? .int_value(); + ensure_btc_block_store(ctx, config, hardfork_cfg); let block_number = revm::context_interface::Block::number(ctx.block()).to::(); let rskip199 = hardfork_cfg.has_rskip199(block_number); // A negative height is below every search limit, or walks off the stored @@ -801,11 +831,14 @@ pub fn get_parent_block_header_by_hash( ctx: &mut CTX, args: &[u8], gas_cost: u64, + config: &BridgeConstants, + hardfork_cfg: &RskHardforkConfig, ) -> Result { let hash = crate::abi::Args::new(args) .bytes32(0) .ok_or_else(|| PrecompileError::other("getParentBlockHeaderByHash: no hash"))?; let hash = b256_to_bitcoin_hash(&alloy_primitives::B256::from(hash)); + ensure_btc_block_store(ctx, config, hardfork_cfg); // As in `get_block_header_by_height`, an absent block yields rskj's // `EMPTY_BYTE_ARRAY`, which `Bridge.execute` still ABI-encodes as an empty @@ -1123,7 +1156,9 @@ mod tests { /// not have as the empty byte array, which encodes to 96 bytes. #[test] fn an_unknown_block_hash_answers_the_empty_bytes() { - let out = get_block_header_by_hash(&mut empty_ctx(), &[0x11; 32], 0).unwrap(); + let (config, cfg) = (BridgeConstants::mainnet(), RskHardforkConfig::mainnet()); + let out = + get_block_header_by_hash(&mut empty_ctx(), &[0x11; 32], 0, &config, &cfg).unwrap(); assert_eq!(out.bytes.to_vec(), encode_abi_bytes(&[])); } @@ -1136,4 +1171,206 @@ mod tests { let (config, cfg) = (BridgeConstants::mainnet(), RskHardforkConfig::mainnet()); assert!(receive_headers(&mut empty_ctx(), &args, 0, &config, false, &cfg).is_err()); } + + /// rskj runs `ensureBtcBlockStore` before every read of the BTC chain, so a + /// getter on a store nothing has seeded yet seeds it, and answers from it: + /// on mainnet the checkpoint is then the head. (Unseeded, the height was 0, + /// the best header empty, and the depth and the locator failed.) + #[test] + fn the_btc_getters_seed_an_empty_store_and_answer_from_it() { + use crate::abi::encode; + /// The getter's answer on an empty store, and the head it leaves. + fn seeded( + mut ctx: CTX, + getter: impl FnOnce(&mut CTX) -> Result, + ) -> (Vec, Option) { + let out = getter(&mut ctx).unwrap(); + ( + out.bytes.to_vec(), + load_chain_head(&mut ctx).map(|head| head.height), + ) + } + let (config, cfg) = (BridgeConstants::mainnet(), RskHardforkConfig::mainnet()); + let (header_hex, height, _) = config.btc_checkpoint.unwrap(); + let header_bytes = alloy_primitives::hex::decode(header_hex).unwrap(); + let checkpoint: BtcHeader = deserialize(&header_bytes).unwrap(); + let hash = bitcoin_hash_to_b256(&checkpoint.block_hash()); + let (header, at) = (encode_abi_bytes(&header_bytes), Some(height)); + + assert_eq!( + seeded(empty_ctx(), |ctx| get_best_chain_height( + ctx, 0, &config, &cfg + )), + (encode_int_result(0, i64::from(height)).bytes.to_vec(), at) + ); + assert_eq!( + seeded(empty_ctx(), |ctx| get_best_block_header( + ctx, 0, &config, &cfg + )), + (header.clone(), at) + ); + assert_eq!( + seeded(empty_ctx(), |ctx| get_block_header_by_hash( + ctx, + hash.as_slice(), + 0, + &config, + &cfg + )), + (header.clone(), at) + ); + assert_eq!( + seeded(empty_ctx(), |ctx| { + get_block_header_by_height(ctx, &encode::int(i64::from(height)), 0, &config, &cfg) + }), + (header, at) + ); + assert_eq!( + seeded(empty_ctx(), |ctx| get_parent_block_header_by_hash( + ctx, + hash.as_slice(), + 0, + &config, + &cfg + )), + (encode_abi_bytes(&[]), at), + "the checkpoint's parent isn't held" + ); + assert_eq!( + seeded(empty_ctx(), |ctx| get_block_hash_at_depth( + ctx, + &encode::int(0), + 0, + &config, + &cfg + )), + (encode_abi_bytes(hash.as_slice()), at) + ); + assert_eq!( + seeded(empty_ctx(), |ctx| get_block_locator(ctx, 0, &config, &cfg)), + ( + super::super::getters::abi_encode_string_array_pub(&[hex::encode(hash)]).to_vec(), + at + ) + ); + + // In the block's layout and with its index: block 0 is before RSKIP199, + // so the checkpoint's height isn't indexed. + let mut ctx = empty_ctx(); + ensure_btc_block_store(&mut ctx, &config, &cfg); + assert_eq!( + crate::bridge::storage::bridge_load_btc_block_hash_by_height(&mut ctx, height), + None + ); + } + + /// With no checkpoint, as on regtest, the seeded store holds Bitcoin + /// regtest's genesis alone, the head at height 0, found by its height. + #[test] + fn a_seeded_regtest_store_answers_its_genesis_by_height() { + use crate::abi::encode; + let (config, cfg) = (BridgeConstants::regtest(), RskHardforkConfig::regtest()); + let genesis = bitcoin::constants::genesis_block(bitcoin::Network::Regtest).header; + let mut ctx = empty_ctx(); + let out = get_block_header_by_height(&mut ctx, &encode::int(0), 0, &config, &cfg).unwrap(); + assert_eq!(out.bytes.to_vec(), encode_abi_bytes(&serialize(&genesis))); + assert_eq!( + load_chain_head(&mut ctx).map(|head| head.header), + Some(genesis) + ); + } + + /// rskj seeds in two steps that each set the head: the genesis block + /// (`checkIfInitialized`), then, on a store still at its genesis block, + /// the checkpoint (`CheckpointManager.checkpoint`). So from RSKIP199 both + /// are in the height index, and a store left at its genesis block takes + /// the checkpoint when it's next seeded, but a seeded store isn't seeded + /// again. + #[test] + fn seeding_heads_the_store_with_its_genesis_block_then_the_checkpoint() { + use crate::bridge::storage::bridge_load_btc_block_hash_by_height as indexed; + let config = BridgeConstants::mainnet(); + let height = config.btc_checkpoint.unwrap().1; + let genesis = bitcoin::constants::genesis_block(bitcoin::Network::Bitcoin).header; + + let mut ctx = empty_ctx(); + ensure_btc_chain_seeded(&mut ctx, &config, true, true); + let head = load_chain_head(&mut ctx).unwrap(); + assert_eq!(head.height, height); + assert_eq!( + indexed(&mut ctx, 0), + Some(bitcoin_hash_to_b256(&genesis.block_hash())) + ); + assert_eq!( + indexed(&mut ctx, height), + Some(bitcoin_hash_to_b256(&head.header.block_hash())) + ); + + let mut ctx = empty_ctx(); + ensure_btc_chain_seeded(&mut ctx, &config, true, false); + assert_eq!(indexed(&mut ctx, 0), None, "no index before RSKIP199"); + + let mut ctx = empty_ctx(); + let stored = StoredBlock::new(genesis, 0, compute_work(genesis.bits)); + put_stored_block(&mut ctx, &stored, true); + crate::bridge::btc_store::store_chain_head(&mut ctx, &stored, true); + ensure_btc_chain_seeded(&mut ctx, &config, true, true); + assert_eq!( + load_chain_head(&mut ctx).map(|head| head.height), + Some(height) + ); + + // Regtest has no checkpoint: a store seeded before RSKIP199 stays at + // its genesis block, and keeps it out of the index after. + let regtest = BridgeConstants::regtest(); + let mut ctx = empty_ctx(); + ensure_btc_chain_seeded(&mut ctx, ®test, true, false); + ensure_btc_chain_seeded(&mut ctx, ®test, true, true); + assert_eq!(indexed(&mut ctx, 0), None); + } + + /// rskj's BTC store writes straight into the repository its call was + /// given, which a transaction's own call keeps however it ends: a seeding + /// it made stays when its other writes go, but the height index, which + /// goes through the storage provider, goes with them. A contract's call, a + /// frame deeper, keeps nothing of a call that fails. + #[test] + fn a_transactions_own_call_keeps_the_seeding_its_failure_drops() { + use revm::context_interface::JournalTr; + let config = BridgeConstants::regtest(); + let genesis = bitcoin::constants::genesis_block(bitcoin::Network::Regtest).header; + // The head and the hash indexed at height 0 once a call `frames` deep + // has seeded the store and then ended, well or not. + let seeded = |frames: usize, ended_well: bool| { + let mut ctx = empty_ctx(); + for _ in 0..frames { + ctx.journal_mut().checkpoint(); + } + ensure_btc_chain_seeded(&mut ctx, &config, true, true); + let raw = &mut ctx.chain_mut().raw_storage; + if ended_well { + raw.commit_call(); + } else { + raw.discard_call(); + } + ( + load_chain_head(&mut ctx).map(|head| head.header), + crate::bridge::storage::bridge_load_btc_block_hash_by_height(&mut ctx, 0), + ) + }; + let indexed = Some(bitcoin_hash_to_b256(&genesis.block_hash())); + assert_eq!(seeded(1, true), (Some(genesis), indexed)); + assert_eq!( + seeded(1, false), + (Some(genesis), None), + "a transaction's own call" + ); + assert_eq!( + seeded(0, false), + (Some(genesis), None), + "its cost, before RSKIP136" + ); + assert_eq!(seeded(2, false), (None, None), "a contract's call"); + assert_eq!(seeded(2, true), (Some(genesis), indexed)); + } } diff --git a/crates/execution/src/bridge/btc_store.rs b/crates/execution/src/bridge/btc_store.rs index 7a629c5..0b9f16b 100644 --- a/crates/execution/src/bridge/btc_store.rs +++ b/crates/execution/src/bridge/btc_store.rs @@ -258,10 +258,27 @@ pub fn get_stored_block(ctx: &mut CTX, hash: &BlockHas // (rskj Repository.addStorageBytes / getStorageBytes). // --------------------------------------------------------------------------- +/// Write a cell of the BTC block store. rskj's store +/// (`RepositoryBtcBlockStoreWithCache.put`, `setChainHead`) writes straight +/// into the repository the Bridge was given, not through the Bridge's storage +/// provider, which writes only when a method returns (and which the height +/// index, `setMainChainBlock`, goes through). A transaction's own call to the +/// Bridge is given the transaction's repository, which +/// `TransactionExecutor.go` commits however the call ended, so these writes +/// stay though the method then throws or the call runs out of gas. A +/// contract's call is given a repository of its own, which a failed call +/// rolls back (`Program.callToPrecompiledAddress`). fn store_raw_bytes(ctx: &mut CTX, base_key: U256, data: &[u8]) { - ctx.chain_mut() - .raw_storage - .put(BRIDGE_ADDR, base_key, Some(data.to_vec())); + use revm::context_interface::JournalTr; + // A transaction's own call runs in the first frame. Before RSKIP136 its + // cost is worked out before that frame opens, at depth 0. + let transactions_own_call = ctx.journal().depth() <= 1; + let raw = &mut ctx.chain_mut().raw_storage; + if transactions_own_call { + raw.put_kept(BRIDGE_ADDR, base_key, Some(data.to_vec())); + } else { + raw.put(BRIDGE_ADDR, base_key, Some(data.to_vec())); + } } fn load_raw_bytes(ctx: &mut CTX, base_key: U256) -> Vec { diff --git a/crates/execution/src/bridge/getters.rs b/crates/execution/src/bridge/getters.rs index e7479c0..ed205ee 100644 --- a/crates/execution/src/bridge/getters.rs +++ b/crates/execution/src/bridge/getters.rs @@ -892,8 +892,12 @@ pub fn get_state_for_svp_client( pub fn get_state_for_debugging( ctx: &mut CTX, gas_cost: u64, + config: &BridgeConstants, hardfork_cfg: &crate::hardfork::RskHardforkConfig, ) -> Result { + // rskj builds the state from `getBtcBlockchainBestChainHeight()`, which + // seeds a store nothing has seeded yet (`ensureBtcBlockStore`). + super::btc_chain::ensure_btc_block_store(ctx, config, hardfork_cfg); let block_number = revm::context_interface::Block::number(ctx.block()).to::(); // rskj `BridgeState.shouldUsePapyrusEncoding` == RSKIP146. let papyrus = hardfork_cfg.has_rskip146(block_number); @@ -1528,16 +1532,24 @@ mod tests { fn state_for_debugging_has_six_fields_with_the_height_as_an_integer() { let mut ctx = ctx_at(6_500_000); let forks = hardforks(); - let out = get_state_for_debugging(&mut ctx, 0, &forks).unwrap(); + let config = mainnet(); + let out = get_state_for_debugging(&mut ctx, 0, &config, &forks).unwrap(); let rlp = as_bytes(&out); let fields = crate::bridge::serialization::rlp_decode_list(&rlp) .expect("BridgeState is an RLP list"); assert_eq!(fields.len(), 6, "btcHeight, utxos, waitingFS, queue, pegouts, nextHeight"); - // An empty store gives height 0, which `encodeBigInteger` renders as - // the empty byte string rather than a zero byte. - assert!(fields[0].is_empty() || fields[0] == vec![0u8], "height as an integer"); + // The height comes from `getBtcBlockchainBestChainHeight()`, which + // seeds an empty store, so it's the checkpoint's: a minimal-length + // integer, as `encodeBigInteger` writes it. + let checkpoint = config.btc_checkpoint.unwrap().1; + let minimal: Vec = checkpoint + .to_be_bytes() + .into_iter() + .skip_while(|b| *b == 0) + .collect(); + assert_eq!(fields[0], minimal, "height as an integer"); // The middle four are byte strings holding their own RLP lists. for (i, field) in fields.iter().enumerate().take(5).skip(1) { assert!( diff --git a/crates/execution/src/bridge/governance.rs b/crates/execution/src/bridge/governance.rs index ec0d90e..508a05d 100644 --- a/crates/execution/src/bridge/governance.rs +++ b/crates/execution/src/bridge/governance.rs @@ -1174,15 +1174,12 @@ pub fn set_lock_whitelist_disable_block_delay( gas_cost: u64, config: &super::constants::BridgeConstants, hardfork_cfg: &crate::hardfork::RskHardforkConfig, - use_v2: bool, tx_ctx: &crate::precompiles::BridgeTxContext, ) -> Result { // rskj `BridgeSupport.setLockWhitelistDisableBlockDelay` reads the BTC // chain's height before anything asks who is calling, and seeds the chain // where nothing has yet (`ensureBtcBlockStore`), whoever calls. - let block_number = revm::context_interface::Block::number(ctx.block()).to::(); - let rskip199 = hardfork_cfg.has_rskip199(block_number); - super::btc_chain::ensure_btc_chain_seeded(ctx, config, use_v2, rskip199); + super::btc_chain::ensure_btc_block_store(ctx, config, hardfork_cfg); let best_height = super::btc_store::load_chain_head(ctx) .map(|h| h.height as i32) .unwrap_or(0); @@ -1193,6 +1190,7 @@ pub fn set_lock_whitelist_disable_block_delay( // rskj reads the whitelist from here on, so it writes it back however the // call ends but a throw. + let block_number = revm::context_interface::Block::number(ctx.block()).to::(); let mut whitelist = LockWhitelist::load(ctx, hardfork_cfg.has_rskip87(block_number)); let code = if whitelist.disable_height < i32::MAX { -1 // delay already set @@ -1653,7 +1651,7 @@ mod tests { let set = |ctx, args: Vec, tx: &BridgeTxContext, hardforks| { run(ctx, |ctx| { - set_lock_whitelist_disable_block_delay(ctx, &args, 0, &config, hardforks, true, tx) + set_lock_whitelist_disable_block_delay(ctx, &args, 0, &config, hardforks, tx) }) }; assert_eq!(set(fresh(), delay(100), &stranger, &all), unread(-10)); @@ -1679,7 +1677,6 @@ mod tests { 0, &config, &all, - true, &authorized ) .is_err()); @@ -1711,10 +1708,8 @@ mod tests { let mut ctx = fresh(); let stranger = crate::precompiles::BridgeTxContext::default(); - set_lock_whitelist_disable_block_delay( - &mut ctx, &args, 0, &config, &hardforks, true, &stranger, - ) - .unwrap(); + set_lock_whitelist_disable_block_delay(&mut ctx, &args, 0, &config, &hardforks, &stranger) + .unwrap(); assert_eq!( super::super::btc_store::load_chain_head(&mut ctx).map(|head| head.height as i32), Some(checkpoint), @@ -1728,7 +1723,6 @@ mod tests { 0, &config, &hardforks, - true, &authorized, ) .unwrap(); diff --git a/crates/execution/src/bridge/mod.rs b/crates/execution/src/bridge/mod.rs index 6623019..df30302 100644 --- a/crates/execution/src/bridge/mod.rs +++ b/crates/execution/src/bridge/mod.rs @@ -738,7 +738,7 @@ pub fn bridge_call_gas_cost( let function_cost = match method.gas_cost { BridgeGasCost::Fixed(c) => c, BridgeGasCost::InputDependent => receive_headers_cost(args, hardfork_cfg, block_number), - BridgeGasCost::Dynamic => btc_tx_confirmations_cost(ctx, args, config), + BridgeGasCost::Dynamic => btc_tx_confirmations_cost(ctx, args, config, hardfork_cfg), }; // rskj Bridge.getGasForData: totalCost = functionCost + data.length * 2 Ok(function_cost.saturating_add(2 * input.len() as u64)) @@ -770,6 +770,7 @@ fn btc_tx_confirmations_cost( ctx: &mut CTX, args: &[u8], config: &BridgeConstants, + hardfork_cfg: &RskHardforkConfig, ) -> u64 { const BASIC_COST: u64 = 27_000; const STEP_COST: u64 = 315; @@ -782,6 +783,13 @@ fn btc_tx_confirmations_cost( _ => return BASIC_COST, }; + // rskj seeds a store nothing has seeded yet before it looks the block up + // (`ensureBtcBlockStore`), so on an empty store the genesis block and + // the checkpoint are found. A transaction's own call keeps the seeding + // though it then runs out of gas, since rskj's `TransactionExecutor.go` + // commits what the store wrote (`btc_store::store_raw_bytes`); a + // contract's call that runs out of gas drops it. + crate::bridge::btc_chain::ensure_btc_block_store(ctx, config, hardfork_cfg); let block_hash = crate::bridge::btc_chain::b256_to_bitcoin_hash(&block_hash); let Some(block) = crate::bridge::btc_store::get_stored_block(ctx, &block_hash) else { return BASIC_COST; @@ -852,15 +860,21 @@ fn execute_method( "receiveHeaders" => { btc_chain::receive_headers(ctx, args, gas_cost, config, use_v2, hardfork_cfg) } - "getBtcBlockchainBestChainHeight" => btc_chain::get_best_chain_height(ctx, gas_cost), - "getBtcBlockchainBestBlockHeader" => btc_chain::get_best_block_header(ctx, gas_cost), + "getBtcBlockchainBestChainHeight" => { + btc_chain::get_best_chain_height(ctx, gas_cost, config, hardfork_cfg) + } + "getBtcBlockchainBestBlockHeader" => { + btc_chain::get_best_block_header(ctx, gas_cost, config, hardfork_cfg) + } "getBtcBlockchainBlockHeaderByHash" => { - btc_chain::get_block_header_by_hash(ctx, args, gas_cost) + btc_chain::get_block_header_by_hash(ctx, args, gas_cost, config, hardfork_cfg) } "getBtcBlockchainBlockHeaderByHeight" => { btc_chain::get_block_header_by_height(ctx, args, gas_cost, config, hardfork_cfg) } - "getBtcBlockchainParentBlockHeaderByHash" => btc_chain::get_parent_block_header_by_hash(ctx, args, gas_cost), + "getBtcBlockchainParentBlockHeaderByHash" => { + btc_chain::get_parent_block_header_by_hash(ctx, args, gas_cost, config, hardfork_cfg) + } // RSKIP502: the union bridge, authorized against the *immediate* // caller. @@ -918,7 +932,9 @@ fn execute_method( } // Phase 3: BTC transaction verification - "registerBtcCoinbaseTransaction" => tx::register_btc_coinbase_transaction(ctx, args, gas_cost), + "registerBtcCoinbaseTransaction" => { + tx::register_btc_coinbase_transaction(ctx, args, gas_cost, config, hardfork_cfg) + } "hasBtcBlockCoinbaseTransactionInformation" => tx::has_btc_block_coinbase_info(ctx, args, gas_cost), "getBtcTransactionConfirmations" => { tx::get_btc_transaction_confirmations(ctx, args, gas_cost, config, use_v2, hardfork_cfg) @@ -946,7 +962,7 @@ fn execute_method( "addUnlimitedLockWhitelistAddress" => governance::add_unlimited_lock_whitelist_address(ctx, args, gas_cost, config, hardfork_cfg, tx_ctx), "removeLockWhitelistAddress" => governance::remove_lock_whitelist_address(ctx, args, gas_cost, config, hardfork_cfg, tx_ctx), "setLockWhitelistDisableBlockDelay" => { - governance::set_lock_whitelist_disable_block_delay(ctx, args, gas_cost, config, hardfork_cfg, use_v2, tx_ctx) + governance::set_lock_whitelist_disable_block_delay(ctx, args, gas_cost, config, hardfork_cfg, tx_ctx) } // Transaction-callable read-only @@ -999,7 +1015,7 @@ fn execute_method( "getBtcBlockchainBlockLocator" => btc_chain::get_block_locator(ctx, gas_cost, config, hardfork_cfg), "getStateForBtcReleaseClient" => getters::get_state_for_btc_release_client(ctx, gas_cost), "getStateForSvpClient" => getters::get_state_for_svp_client(ctx, gas_cost), - "getStateForDebugging" => getters::get_state_for_debugging(ctx, gas_cost, hardfork_cfg), + "getStateForDebugging" => getters::get_state_for_debugging(ctx, gas_cost, config, hardfork_cfg), // Every method in `bridge_method_table` must appear above. A name that // reaches here is a table entry with no implementation, which is a bug @@ -1389,4 +1405,29 @@ mod tests { assert_eq!(receive_headers_cost(&[], &cfg, 1_591_000), 25_000); assert_eq!(receive_headers_cost(&args[..64], &cfg, 1_591_000), 25_000); } + + /// rskj's `getBtcTransactionConfirmationsGetCost` seeds the BTC store + /// before it looks the block up (`ensureBtcBlockStore`), so on an empty + /// regtest store the genesis block is found at depth 0, and the branch is + /// charged for. Unseeded, it wasn't found: the base cost alone. + #[test] + fn the_confirmations_cost_seeds_an_empty_store_first() { + use crate::abi::encode::{self, Arg}; + use revm::MainContext; + let (config, cfg) = (BridgeConstants::regtest(), RskHardforkConfig::regtest()); + let genesis = bitcoin::constants::genesis_block(bitcoin::Network::Regtest).header; + let block_hash = btc_chain::bitcoin_hash_to_b256(&genesis.block_hash()); + let args = encode::args(&[ + Arg::Word([0x11; 32]), + Arg::Word(block_hash.0), + Arg::Word(encode::int(0)), + Arg::Words(&[[0x22; 32], [0x33; 32]]), + ]); + let mut ctx = + revm::Context::mainnet().with_chain(crate::raw_storage::RskChainExt::default()); + assert_eq!( + btc_tx_confirmations_cost(&mut ctx, &args, &config, &cfg), + 27_000 + 2 * 144 + ); + } } diff --git a/crates/execution/src/bridge/tx.rs b/crates/execution/src/bridge/tx.rs index 34c2f1a..b0573d1 100644 --- a/crates/execution/src/bridge/tx.rs +++ b/crates/execution/src/bridge/tx.rs @@ -173,6 +173,8 @@ pub fn register_btc_coinbase_transaction( ctx: &mut CTX, args: &[u8], gas_cost: u64, + config: &super::constants::BridgeConstants, + hardfork_cfg: &crate::hardfork::RskHardforkConfig, ) -> Result { // The five arguments, as rskj's decoder reads them: the transaction, the // block hash, the partial merkle tree, the witness merkle root and the @@ -198,6 +200,10 @@ pub fn register_btc_coinbase_transaction( }; let (btc_tx_data, pmt_data) = (btc_tx_data.to_vec(), pmt_data.to_vec()); + // rskj `ensureBtcBlockStore` comes first, so a store nothing has seeded + // yet is seeded even where the call then returns without registering. + super::btc_chain::ensure_btc_block_store(ctx, config, hardfork_cfg); + // Compute BTC tx hash (double-SHA256) let btc_tx_hash = calculate_btc_tx_hash(&btc_tx_data); @@ -681,6 +687,39 @@ mod tests { assert_ne!(calculated, expected, "mismatching commitment must be rejected"); } + /// rskj's `registerBtcCoinbaseTransaction` seeds the BTC store before + /// anything else (`ensureBtcBlockStore`), so a store nothing has seeded + /// yet is seeded even by a call that then registers nothing, as one whose + /// transaction isn't in its tree. + #[test] + fn a_coinbase_registration_seeds_an_empty_store_first() { + use crate::abi::encode::{self, Arg}; + use revm::MainContext; + let config = super::super::constants::BridgeConstants::mainnet(); + let cfg = crate::hardfork::RskHardforkConfig::mainnet(); + let mut ctx = + revm::Context::mainnet().with_chain(crate::raw_storage::RskChainExt::default()); + // A tree of one transaction, not the one sent. + let mut pmt = 1u32.to_le_bytes().to_vec(); + pmt.push(1); + pmt.extend_from_slice(&[0xaa; 32]); + pmt.extend_from_slice(&[1, 0x01]); + let args = encode::args(&[ + Arg::Bytes(&[]), + Arg::Word([0x11; 32]), + Arg::Bytes(&pmt), + Arg::Word([0; 32]), + Arg::Word([0; 32]), + ]); + + let out = register_btc_coinbase_transaction(&mut ctx, &args, 0, &config, &cfg).unwrap(); + assert!(out.bytes.is_empty(), "registered nothing"); + assert_eq!( + load_chain_head(&mut ctx).map(|head| head.height), + Some(config.btc_checkpoint.unwrap().1) + ); + } + /// `find_witness_commitment` must take the LAST matching output (BIP141 / rskj /// `findWitnessCommitment` iterates outputs reversed) and ignore non-commitment /// outputs. diff --git a/crates/execution/src/executor.rs b/crates/execution/src/executor.rs index 95ebaf8..4c2d6f2 100644 --- a/crates/execution/src/executor.rs +++ b/crates/execution/src/executor.rs @@ -1909,6 +1909,11 @@ impl RskExecutor { } } } else { + // Out of gas before `execute`. rskj's `go()` commits the + // repository all the same, with what `getGasForData` wrote + // straight into it (a BTC store its cost seeded): the + // writes this keeps of the cost's. + evm.ctx.chain_mut().raw_storage.discard_call(); (false, Vec::new(), Vec::new()) }; @@ -8276,6 +8281,161 @@ mod tests { assert_eq!(result.tx_results[0].gas_used, enough); } + /// rskj's BTC block store writes straight into the repository a + /// transaction's own call to the Bridge is given, which + /// `TransactionExecutor.go` commits however the call ends. So such a call + /// that seeds a store nothing has seeded yet keeps the genesis block and + /// the head though it then throws (an invisible exception) or runs out of + /// gas, and only the height index, which the Bridge's storage provider + /// writes when a method returns, is left out. A call that returns writes + /// all three, each in its block's layout. + #[test] + fn a_transactions_own_bridge_call_keeps_the_btc_store_it_seeded_however_it_ends() { + use crate::abi::encode::{self, Arg}; + use crate::bridge::btc_store::{btc_hash_to_storage_key, compute_work, StoredBlock}; + use crate::bridge::storage::{bridge_storage_key, compound_key, BTC_BLOCK_HEIGHT_KEY}; + let bridge = crate::precompiles::BRIDGE_ADDR; + let genesis = bitcoin::constants::genesis_block(bitcoin::Network::Regtest).header; + let cells = [ + btc_hash_to_storage_key(&genesis.block_hash()), + bridge_storage_key("blockStoreChainHead"), + compound_key(BTC_BLOCK_HEIGHT_KEY, "-", "0"), + ]; + // What the block last wrote to the genesis block's cell, the head's + // and the index entry's, after its one transaction to the Bridge. + let written = |input: &[u8], gas_limit: u64| { + let result = execute_alone( + RskHardforkConfig::all_active(33), + 8_000_000, + bridge, + U256::ZERO, + gas_limit, + input, + &[], + ); + let last = |cell: &U256| { + let raw = &result.markers.raw_storage; + let write = raw + .iter() + .rev() + .find(|(address, key, _)| *address == bridge && key == cell); + write.and_then(|(_, _, value)| value.clone()) + }; + ( + result.tx_results[0].success, + cells.iter().map(last).collect::>(), + ) + }; + // The genesis block in RSKIP454's layout, which its cell and the head + // both hold, and its hash as the index holds it. + let stored = + StoredBlock::new(genesis, 0, compute_work(genesis.bits)).serialize_compact_v2(); + let hash = crate::bridge::btc_chain::bitcoin_hash_to_b256(&genesis.block_hash()); + let indexed = crate::bridge::serialization::rlp_encode_element(hash.as_slice()); + let seeded = vec![Some(stored.clone()), Some(stored.clone()), None]; + + // Seeded first, then refused for a tree of the wrong size. + let mut input = crate::abi::selector( + "registerBtcCoinbaseTransaction(bytes,bytes32,bytes,bytes32,bytes32)", + ) + .to_vec(); + input.extend(encode::args(&[ + Arg::Bytes(&[]), + Arg::Word([0x11; 32]), + Arg::Bytes(&[]), + Arg::Word([0; 32]), + Arg::Word([0; 32]), + ])); + assert_eq!(written(&input, 1_000_000), (true, seeded.clone())); + + // Seeded by its cost, then out of gas: the cost is 27,000 at least. + let mut input = crate::abi::selector( + "getBtcTransactionConfirmations(bytes32,bytes32,uint256,bytes32[])", + ) + .to_vec(); + input.extend(encode::args(&[ + Arg::Word([0x11; 32]), + Arg::Word([0x22; 32]), + Arg::Word(encode::int(0)), + Arg::Words(&[]), + ])); + let short = intrinsic_gas(&input, 68) + 1_000; + assert_eq!(written(&input, short), (false, seeded)); + + let input = crate::abi::selector("getBtcBlockchainBestChainHeight()").to_vec(); + let all = vec![Some(stored.clone()), Some(stored), Some(indexed)]; + assert_eq!(written(&input, 1_000_000), (true, all)); + } + + /// A transaction's own call to the Bridge that can't pay what its + /// `getGasForData` works out keeps what working that out wrote straight + /// into the repository, since `go()` commits it all the same: here the + /// store `getBtcTransactionConfirmations`' cost seeds with mainnet's + /// checkpoint, in the block's layout, and none of the height index. On + /// both of rskj's paths: from RSKIP136, and before it, where the limit + /// is held to the cost alone. No chain has the method before RSKIP136 + /// (mainnet's bahamas is #3,397, its wasabi100 #1,591,000), so the second + /// runs every upgrade but RSKIP136, at mainnet's #1,000. + #[test] + fn a_bridge_call_short_of_its_cost_keeps_the_store_the_cost_seeded() { + use crate::abi::encode::{self, Arg}; + use crate::bridge::btc_store::{btc_hash_to_storage_key, StoredBlock}; + use crate::bridge::storage::{bridge_storage_key, compound_key, BTC_BLOCK_HEIGHT_KEY}; + let bridge = crate::precompiles::BRIDGE_ADDR; + let config = crate::bridge::constants::BridgeConstants::mainnet(); + let (header_hex, height, work_hex) = config.btc_checkpoint.unwrap(); + let checkpoint: bitcoin::block::Header = + bitcoin::consensus::deserialize(&alloy_primitives::hex::decode(header_hex).unwrap()) + .unwrap(); + let work = U256::from_be_slice(&alloy_primitives::hex::decode(work_hex).unwrap()); + let stored = StoredBlock::new(checkpoint, height, work); + let genesis = bitcoin::constants::genesis_block(bitcoin::Network::Bitcoin).header; + let mut input = crate::abi::selector( + "getBtcTransactionConfirmations(bytes32,bytes32,uint256,bytes32[])", + ) + .to_vec(); + input.extend(encode::args(&[ + Arg::Word([0x11; 32]), + Arg::Word([0x22; 32]), + Arg::Word(encode::int(0)), + Arg::Words(&[]), + ])); + // Enough for the transaction, short of the cost: 27,000 and the data's. + let limit = intrinsic_gas(&input, 68); + assert!(limit < 27_000 + 2 * input.len() as u64); + + // What the block wrote to the head, once it's checked the rest. + let head = |cfg: RskHardforkConfig, number: u64| { + let result = execute_alone(cfg, number, bridge, U256::ZERO, limit, &input, &[]); + assert!(!result.tx_results[0].success); + assert_eq!(result.tx_results[0].gas_used, limit); + let last = |cell: U256| { + let raw = &result.markers.raw_storage; + let write = raw + .iter() + .rev() + .find(|(address, key, _)| *address == bridge && *key == cell); + write.and_then(|(_, _, value)| value.clone()) + }; + assert!(last(btc_hash_to_storage_key(&genesis.block_hash())).is_some()); + assert!(last(btc_hash_to_storage_key(&checkpoint.block_hash())).is_some()); + for indexed in [0, height] { + let cell = compound_key(BTC_BLOCK_HEIGHT_KEY, "-", &indexed.to_string()); + assert_eq!(last(cell), None); + } + last(bridge_storage_key("blockStoreChainHead")) + }; + let after = RskHardforkConfig::mainnet(); + assert!(after.has_rskip136(2_000_000)); + assert_eq!( + head(after, 2_000_000), + Some(stored.serialize_compact_legacy()) + ); + let before = RskHardforkConfig::all_active(crate::hardfork::RSK_MAINNET_CHAIN_ID); + assert!(!before.has_rskip136(1_000)); + assert_eq!(head(before, 1_000), Some(stored.serialize_compact_v2())); + } + /// **No call to a precompile panics the node.** /// /// Anyone can send a transaction, or ask for an `eth_call`, that calls a diff --git a/crates/execution/src/raw_storage.rs b/crates/execution/src/raw_storage.rs index 72246ac..a610a6a 100644 --- a/crates/execution/src/raw_storage.rs +++ b/crates/execution/src/raw_storage.rs @@ -13,7 +13,11 @@ //! - per precompile call: `commit_call` on method success, `discard_call` on //! failure (rskj commits the call's child track only on success); //! - per transaction: `begin_tx` / `discard_tx` (a failed tx reverts all of -//! its calls' committed writes). +//! its calls' committed writes); +//! - but a kept write (`put_kept`) stays through both: what rskj writes +//! straight into the repository a transaction's own call to a precompile +//! was given, which `TransactionExecutor.go` commits however the call +//! ended, even after a throw or running out of gas. //! //! Known limitation (consciously accepted): if a contract internally calls a //! state-writing bridge method, the bridge call succeeds, and the CALLER @@ -55,6 +59,11 @@ pub struct RawStorage { pending: Vec, /// `committed` length when the current transaction started. tx_start: usize, + /// The current transaction's kept writes ([`RawStorage::put_kept`]), in + /// write order. + kept: Vec, + /// How many of `kept` are in `committed` already. + kept_committed: usize, /// Read-through to the parent block's state. reader: Option<(Arc, TrieNode)>, } @@ -106,26 +115,46 @@ impl RawStorage { self.pending.push((addr, key, value)); } + /// [`RawStorage::put`] a write that stays however the in-flight call and + /// its transaction end: neither [`RawStorage::discard_call`] nor + /// [`RawStorage::discard_tx`] drops it. The call reads it in order with + /// its other writes. + pub fn put_kept(&mut self, addr: Address, key: U256, value: Option>) { + let write = (addr, key, value.filter(|v| !v.is_empty())); + self.pending.push(write.clone()); + self.kept.push(write); + } + /// Promote the in-flight call's writes (precompile method succeeded). pub fn commit_call(&mut self) { self.committed.append(&mut self.pending); + self.kept_committed = self.kept.len(); } - /// Drop the in-flight call's writes (precompile method failed). + /// Drop the in-flight call's writes (precompile method failed), but its + /// kept ones. pub fn discard_call(&mut self) { self.pending.clear(); + self.committed + .extend_from_slice(&self.kept[self.kept_committed..]); + self.kept_committed = self.kept.len(); } /// Mark the start of a transaction. pub fn begin_tx(&mut self) { debug_assert!(self.pending.is_empty()); self.tx_start = self.committed.len(); + self.kept.clear(); + self.kept_committed = 0; } - /// Revert every write of the current transaction (tx failed). + /// Revert every write of the current transaction (tx failed), but its + /// kept ones. pub fn discard_tx(&mut self) { self.committed.truncate(self.tx_start); self.pending.clear(); + self.committed.extend_from_slice(&self.kept); + self.kept_committed = self.kept.len(); } /// The writes the current transaction has committed so far. @@ -134,7 +163,8 @@ impl RawStorage { /// where RSKIP-144 collision tracking reads it: these are unitrie cells /// like any other, and two parallel sublists that both drive the Bridge /// into writing one have collided. Already net of - /// [`RawStorage::discard_tx`], so a failed transaction reports nothing. + /// [`RawStorage::discard_tx`], so a failed transaction reports only its + /// kept writes, which are made all the same. pub fn tx_writes(&self) -> &[RawWrite] { &self.committed[self.tx_start..] } @@ -143,6 +173,8 @@ impl RawStorage { pub fn drain(&mut self) -> Vec { self.pending.clear(); self.tx_start = 0; + self.kept.clear(); + self.kept_committed = 0; std::mem::take(&mut self.committed) } } @@ -219,4 +251,54 @@ mod tests { raw.put(ADDR, key(7), None); assert_eq!(raw.get(ADDR, key(7)), None); } + + /// A kept write reads in order with its call's other writes, and stays + /// when the call and then its transaction are discarded, where an + /// ordinary write goes. + #[test] + fn a_kept_write_stays_however_its_call_and_transaction_end() { + let mut raw = RawStorage::default(); + raw.begin_tx(); + raw.put_kept(ADDR, key(1), Some(vec![1])); + raw.put(ADDR, key(1), Some(vec![2])); + raw.put(ADDR, key(2), Some(vec![3])); + assert_eq!(raw.get(ADDR, key(1)), Some(vec![2]), "the later write wins"); + raw.discard_call(); + assert_eq!(raw.get(ADDR, key(1)), Some(vec![1])); + assert_eq!(raw.get(ADDR, key(2)), None); + + // Once committed, a kept write is like any other: a later write + // wins, and a later call's failure doesn't bring it back. + raw.put_kept(ADDR, key(2), Some(vec![4])); + raw.commit_call(); + raw.put(ADDR, key(2), Some(vec![5])); + raw.commit_call(); + raw.put(ADDR, key(3), Some(vec![6])); + raw.discard_call(); + assert_eq!(raw.get(ADDR, key(2)), Some(vec![5])); + + raw.discard_tx(); + let kept = [(ADDR, key(1), Some(vec![1])), (ADDR, key(2), Some(vec![4]))]; + assert_eq!(raw.tx_writes(), kept); + + // The next transaction keeps its own and nothing of this one's: what + // a discarded call kept, and what its unfinished call kept, once. + raw.begin_tx(); + raw.put_kept(ADDR, key(3), Some(vec![7])); + raw.discard_call(); + raw.put_kept(ADDR, key(4), Some(vec![8])); + raw.put(ADDR, key(1), Some(vec![9])); + raw.discard_tx(); + raw.discard_call(); + let mut all = kept.to_vec(); + all.extend([(ADDR, key(3), Some(vec![7])), (ADDR, key(4), Some(vec![8]))]); + assert_eq!(raw.drain(), all); + + // A drained overlay starts afresh, and keeps a write once however + // often its call is discarded. + raw.put_kept(ADDR, key(5), Some(vec![10])); + raw.discard_call(); + raw.discard_call(); + assert_eq!(raw.drain(), [(ADDR, key(5), Some(vec![10]))]); + } } From 9821143375a8f7e137f43661c21aec8af8eb7813 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Mon, 9 Nov 2026 16:00:56 -0800 Subject: [PATCH 060/264] execution: what precompiles write outside revm's journal goes back with a frame that fails RSKj gives a contract's call to a precompile a repository of its own (Program.callToPrecompiledAddress), commits it into the caller's when the call returns, and rolls it back with the caller's when the caller fails: a call or create that reverts or fails takes back what the calls under it committed. In rustock two things a precompile writes in that repository sit outside revm's journal: the raw bytes of the Bridge's and REMASC's state (RawStorage), and the storage root callToPrecompiledAddress gives the precompile (setupContract, the list of called precompiles). So a contract whose Bridge call returned and which then reverted, in a transaction that went on, kept both, where RSKj keeps neither; RawStorage's comment called this a known limitation. Any contract reaches it whose Bridge call writes and that then reverts, and on regtest any contract that reads the BTC chain while the store is empty, since that seeds it. RskHandler now keeps a mark for each frame of contract code: where the raw writes (RawStorage::frame_start) and the called precompiles made under it begin. A frame opens when frame_init gives a new one and closes when rsk_frame_run gives its result, which is ok exactly when the frame committed, for creates as for calls (revm's return_create and rsk_return_create leave an error result on every path that reverts). A frame that fails takes back its raw writes (RawStorage::revert_frame) and its calls. The writes it took back stay in tx_writes, since RSKj's RSKIP-144 tracker counts a key as it's written and never takes one back, so what the tracker sees is as before. Traced runs go through revm's inspect loop, not RskHandler's, so they don't follow frames yet, as they miss the loop's other rules. Tests: RawStorage's frame marks, nested frames and what the tracker sees; and through the executor, a contract that seeds the store and reverts, one whose caller reverts after it returned, a constructor that reverts, and a seeding made by a frame that returned, which a later frame's failure leaves; identity's storage root stays each time, and the Bridge's goes with its frame. --- crates/execution/src/collision.rs | 2 +- crates/execution/src/executor.rs | 137 +++++++++++++++++++++++++++- crates/execution/src/precompiles.rs | 6 +- crates/execution/src/raw_storage.rs | 128 ++++++++++++++++++++++---- crates/execution/src/rsk_handler.rs | 81 +++++++++++++++- 5 files changed, 327 insertions(+), 27 deletions(-) diff --git a/crates/execution/src/collision.rs b/crates/execution/src/collision.rs index 0f86ed8..f04b69a 100644 --- a/crates/execution/src/collision.rs +++ b/crates/execution/src/collision.rs @@ -712,7 +712,7 @@ pub fn note_storage_write_attempt(addr: Address, key: B256) { /// Writes only: the read side of that overlay has no hook, which leaves a /// sublist that merely *reads* Bridge state unrecorded. That is the one /// remaining place where our sets are narrower than rskj's. -pub fn record_raw_writes(writes: &[crate::raw_storage::RawWrite]) { +pub fn record_raw_writes<'a>(writes: impl IntoIterator) { with_sublist(|rec| { for (addr, cell, _) in writes { if rec.excluded.contains(addr) { diff --git a/crates/execution/src/executor.rs b/crates/execution/src/executor.rs index 4c2d6f2..8b25a3c 100644 --- a/crates/execution/src/executor.rs +++ b/crates/execution/src/executor.rs @@ -2063,7 +2063,8 @@ impl RskExecutor { self.hardfork_cfg.has_rskip171(header.number), ) .postponing_fees(postpone_fees) - .failing_programs(fails_programs); + .failing_programs(fails_programs) + .with_called_precompiles(called_precompiles.clone()); // `inspect_run` for the one transaction being traced, `run` // for every other -- and `run` is the same code path an EVM // built without an inspector takes, because revm carries the @@ -8436,6 +8437,140 @@ mod tests { assert_eq!(head(before, 1_000), Some(stored.serialize_compact_v2())); } + /// rskj gives a contract's call to a precompile a track of its own, + /// commits it into the caller's when the call returns, and rolls it back + /// with the caller's when the caller fails: what the Bridge wrote, and the + /// storage root `callToPrecompiledAddress` gave it (`setupContract`). So a + /// contract that seeds an empty BTC store through a getter leaves nothing + /// of it behind if it then reverts, or the contract that called it does, + /// or it was a constructor that reverts, and the transaction goes on all + /// the same. What came before the frame that failed stays: identity's + /// storage root, and a seeding a frame that returned made. + #[test] + fn a_frame_that_fails_takes_back_what_the_precompiles_under_it_wrote() { + use crate::bridge::btc_store::{btc_hash_to_storage_key, compute_work, StoredBlock}; + use crate::bridge::storage::{bridge_storage_key, compound_key, BTC_BLOCK_HEIGHT_KEY}; + // Code that CALLs `to` with `data` (32 bytes at most), all its gas + // and no value, and drops what the CALL pushed. + fn call(to: Address, data: &[u8]) -> Vec { + let mut word = [0u8; 32]; + word[..data.len()].copy_from_slice(data); + let mut code = vec![0x7f]; // PUSH32 the data, left-aligned + code.extend_from_slice(&word); + code.extend_from_slice(&[0x60, 0x00, 0x52]); // MSTORE it at 0 + let size = u8::try_from(data.len()).unwrap(); + // No return area, the data as the arguments, no value. + code.extend_from_slice(&[0x60, 0x00, 0x60, 0x00, 0x60, size, 0x60, 0x00, 0x60, 0x00]); + code.push(0x73); // PUSH20 to + code.extend_from_slice(to.as_slice()); + code.extend_from_slice(&[0x5a, 0xf1, 0x50]); // GAS, CALL, POP + code + } + fn stop(mut code: Vec) -> Vec { + code.push(0x00); + code + } + fn revert(mut code: Vec) -> Vec { + code.extend_from_slice(&[0x60, 0x00, 0x60, 0x00, 0xfd]); + code + } + let bridge = crate::precompiles::BRIDGE_ADDR; + let identity = Address::with_last_byte(4); + let seed = crate::abi::selector("getBtcBlockchainBestChainHeight()"); + + let seeds = Address::repeat_byte(0xC1); + let seeds_code = stop(call(bridge, &seed)); + let seeds_and_reverts = Address::repeat_byte(0xC2); + let seeds_and_reverts_code = revert(call(bridge, &seed)); + let reverts_after_seeds = Address::repeat_byte(0xC3); + let reverts_after_seeds_code = revert(call(seeds, &[])); + // CODECOPY the constructor from behind this prefix, CREATE with it, + // drop the address and stop. + let creates = Address::repeat_byte(0xC4); + let creates_code = { + let init = revert(call(bridge, &seed)); + let size = u8::try_from(init.len()).unwrap(); + let mut code = vec![0x60, size, 0x60, 16, 0x60, 0x00, 0x39]; + code.extend_from_slice(&[0x60, size, 0x60, 0x00, 0x60, 0x00, 0xf0, 0x50, 0x00]); + code.extend_from_slice(&init); + code + }; + + let genesis = bitcoin::constants::genesis_block(bitcoin::Network::Regtest).header; + let cells = [ + btc_hash_to_storage_key(&genesis.block_hash()), + bridge_storage_key("blockStoreChainHead"), + compound_key(BTC_BLOCK_HEIGHT_KEY, "-", "0"), + ]; + // A transaction to a contract that calls identity, then `targets`, + // and stops: whether it succeeds, what the block last wrote to the + // genesis block's cell, the head's and the index entry's, and whether + // identity and the Bridge got their storage roots. + let entry = Address::repeat_byte(0xE0); + let run = |targets: &[Address]| { + let calls = targets.iter().map(|target| call(*target, &[])); + let entry_code = stop( + std::iter::once(call(identity, b"id")) + .chain(calls) + .collect::>() + .concat(), + ); + let result = execute_alone( + RskHardforkConfig::all_active(33), + 8_000_000, + entry, + U256::ZERO, + 1_000_000, + &[], + &[ + (entry, &entry_code[..]), + (seeds, &seeds_code[..]), + (seeds_and_reverts, &seeds_and_reverts_code[..]), + (reverts_after_seeds, &reverts_after_seeds_code[..]), + (creates, &creates_code[..]), + ], + ); + let last = |cell: &U256| { + let raw = &result.markers.raw_storage; + let write = raw + .iter() + .rev() + .find(|(address, key, _)| *address == bridge && key == cell); + write.and_then(|(_, _, value)| value.clone()) + }; + ( + result.tx_results[0].success, + cells.iter().map(last).collect::>(), + result.markers.precompiles.contains(&identity), + result.markers.precompiles.contains(&bridge), + ) + }; + + let stored = + StoredBlock::new(genesis, 0, compute_work(genesis.bits)).serialize_compact_v2(); + let hash = crate::bridge::btc_chain::bitcoin_hash_to_b256(&genesis.block_hash()); + let indexed = crate::bridge::serialization::rlp_encode_element(hash.as_slice()); + let seeded = vec![Some(stored.clone()), Some(stored), Some(indexed)]; + assert_eq!(run(&[seeds]), (true, seeded.clone(), true, true)); + let nothing = vec![None, None, None]; + for target in [seeds_and_reverts, reverts_after_seeds, creates] { + assert_eq!( + run(&[target]), + (true, nothing.clone(), true, false), + "{target}" + ); + } + // Seeded by a frame that returned, then a frame that fails writes + // nothing and takes nothing back. + for then in [seeds_and_reverts, reverts_after_seeds, creates] { + assert_eq!( + run(&[seeds, then]), + (true, seeded.clone(), true, true), + "{then}" + ); + } + } + /// **No call to a precompile panics the node.** /// /// Anyone can send a transaction, or ask for an `eth_call`, that calls a diff --git a/crates/execution/src/precompiles.rs b/crates/execution/src/precompiles.rs index 64db874..e5988dd 100644 --- a/crates/execution/src/precompiles.rs +++ b/crates/execution/src/precompiles.rs @@ -656,8 +656,10 @@ pub struct RskPrecompileProvider { aborts_caller: std::sync::Arc, /// Precompiles successfully called during the current transaction. rskj /// writes the contract storage-prefix marker (`setupContract`) on every - /// successful precompile call; the executor drains this after each tx and - /// applies the markers to the trie if the tx committed. + /// successful precompile call, in the call's own track; the executor + /// drains this after each tx and applies the markers to the trie if the + /// tx committed. A frame that fails takes its calls back off it + /// (`RskHandler`), as rskj rolls that track back with the frame's. called_precompiles: std::sync::Arc>>, /// Set when this transaction reaches REMASC or the Bridge. See /// [`RskPrecompileProvider::reached_sequential_only_slot`]. diff --git a/crates/execution/src/raw_storage.rs b/crates/execution/src/raw_storage.rs index a610a6a..ff6cd5f 100644 --- a/crates/execution/src/raw_storage.rs +++ b/crates/execution/src/raw_storage.rs @@ -12,19 +12,17 @@ //! Commit discipline mirrors rskj's repository tracks: //! - per precompile call: `commit_call` on method success, `discard_call` on //! failure (rskj commits the call's child track only on success); +//! - per frame of contract code: `RskHandler` takes `frame_start` as a frame +//! opens and calls `revert_frame` if it fails, since rskj rolls back the +//! track of a call or create that reverts or fails, with what the +//! precompiles it called committed into it; //! - per transaction: `begin_tx` / `discard_tx` (a failed tx reverts all of //! its calls' committed writes); -//! - but a kept write (`put_kept`) stays through both: what rskj writes -//! straight into the repository a transaction's own call to a precompile -//! was given, which `TransactionExecutor.go` commits however the call -//! ended, even after a throw or running out of gas. -//! -//! Known limitation (consciously accepted): if a contract internally calls a -//! state-writing bridge method, the bridge call succeeds, and the CALLER -//! frame later reverts, rskj rolls the bridge writes back with the parent -//! track while this overlay keeps them. No pre-wasabi mainnet block does -//! this; the per-block orchid state-root check would halt right at such a -//! block if one existed. +//! - but a kept write (`put_kept`) stays through all of these: what rskj +//! writes straight into the repository a transaction's own call to a +//! precompile was given, which `TransactionExecutor.go` commits however +//! the call ended, even after a throw or running out of gas. That call +//! runs no frame, so no frame takes one back. use alloy_primitives::{Address, B256, U256}; use rustock_trie::{storage_key, TrieKeySlice, TrieNode, TrieStore}; use std::sync::Arc; @@ -64,6 +62,10 @@ pub struct RawStorage { kept: Vec, /// How many of `kept` are in `committed` already. kept_committed: usize, + /// The current transaction's writes that a failed frame took back + /// ([`RawStorage::revert_frame`]): gone from the state, but written all + /// the same as far as RSKIP-144 is concerned ([`RawStorage::tx_writes`]). + reverted: Vec, /// Read-through to the parent block's state. reader: Option<(Arc, TrieNode)>, } @@ -140,12 +142,28 @@ impl RawStorage { self.kept_committed = self.kept.len(); } + /// Where the writes made under a frame of contract code that opens now + /// begin, for [`RawStorage::revert_frame`]. + pub fn frame_start(&self) -> usize { + self.committed.len() + } + + /// Take back what the calls under a frame that failed committed since its + /// [`RawStorage::frame_start`]: rskj rolls back the frame's track, with + /// what they committed into it. Only the current transaction's writes + /// can go, whatever `start` says. + pub fn revert_frame(&mut self, start: usize) { + let start = start.clamp(self.tx_start, self.committed.len()); + self.reverted.extend(self.committed.drain(start..)); + } + /// Mark the start of a transaction. pub fn begin_tx(&mut self) { debug_assert!(self.pending.is_empty()); self.tx_start = self.committed.len(); self.kept.clear(); self.kept_committed = 0; + self.reverted.clear(); } /// Revert every write of the current transaction (tx failed), but its @@ -155,18 +173,21 @@ impl RawStorage { self.pending.clear(); self.committed.extend_from_slice(&self.kept); self.kept_committed = self.kept.len(); + self.reverted.clear(); } - /// The writes the current transaction has committed so far. + /// The writes the current transaction's calls have committed so far, with + /// those a failed frame took back. /// /// Valid between [`RawStorage::begin_tx`] and the next one, which is /// where RSKIP-144 collision tracking reads it: these are unitrie cells /// like any other, and two parallel sublists that both drive the Bridge - /// into writing one have collided. Already net of - /// [`RawStorage::discard_tx`], so a failed transaction reports only its - /// kept writes, which are made all the same. - pub fn tx_writes(&self) -> &[RawWrite] { - &self.committed[self.tx_start..] + /// into writing one have collided. rskj's tracker counts a key as it's + /// written, so a frame's failure doesn't take a write back from it. + /// Already net of [`RawStorage::discard_tx`], so a failed transaction + /// reports only its kept writes, which are made all the same. + pub fn tx_writes(&self) -> impl Iterator { + self.committed[self.tx_start..].iter().chain(&self.reverted) } /// Take all committed writes for trie projection (block end). @@ -175,6 +196,7 @@ impl RawStorage { self.tx_start = 0; self.kept.clear(); self.kept_committed = 0; + self.reverted.clear(); std::mem::take(&mut self.committed) } } @@ -279,7 +301,7 @@ mod tests { raw.discard_tx(); let kept = [(ADDR, key(1), Some(vec![1])), (ADDR, key(2), Some(vec![4]))]; - assert_eq!(raw.tx_writes(), kept); + assert_eq!(raw.tx_writes().cloned().collect::>(), kept); // The next transaction keeps its own and nothing of this one's: what // a discarded call kept, and what its unfinished call kept, once. @@ -301,4 +323,74 @@ mod tests { raw.discard_call(); assert_eq!(raw.drain(), [(ADDR, key(5), Some(vec![10]))]); } + + /// A frame that fails takes back what the calls under it committed, a + /// frame under it that returned included, and nothing from before it. + /// What it took back stays written for RSKIP-144, and leaves the block. + #[test] + fn a_failed_frame_takes_back_what_the_calls_under_it_committed() { + let mut raw = RawStorage::default(); + raw.begin_tx(); + raw.put(ADDR, key(1), Some(vec![1])); + raw.commit_call(); + + let outer = raw.frame_start(); + raw.put(ADDR, key(1), Some(vec![2])); + raw.commit_call(); + // A frame under it fails: only its own writes go. + let inner = raw.frame_start(); + raw.put(ADDR, key(2), Some(vec![3])); + raw.commit_call(); + raw.revert_frame(inner); + assert_eq!(raw.get(ADDR, key(1)), Some(vec![2])); + assert_eq!(raw.get(ADDR, key(2)), None); + // Another one returns, then the outer frame fails and takes it along. + raw.frame_start(); + raw.put(ADDR, key(3), Some(vec![4])); + raw.commit_call(); + raw.revert_frame(outer); + assert_eq!(raw.get(ADDR, key(1)), Some(vec![1])); + assert_eq!(raw.get(ADDR, key(3)), None); + + let mut written: Vec<_> = raw.tx_writes().map(|(_, cell, _)| *cell).collect(); + written.sort(); + assert_eq!(written, [key(1), key(1), key(2), key(3)]); + assert_eq!(raw.drain(), [(ADDR, key(1), Some(vec![1]))]); + assert_eq!( + raw.tx_writes().count(), + 0, + "a drained overlay starts afresh" + ); + } + + /// What a failed frame took back is its transaction's: the next one + /// doesn't report it, nor does its own once it fails. And a frame takes + /// back only its own transaction's writes, whatever start it's given. + #[test] + fn what_a_failed_frame_took_back_ends_with_its_transaction() { + let mut raw = RawStorage::default(); + raw.begin_tx(); + let frame = raw.frame_start(); + raw.put(ADDR, key(1), Some(vec![1])); + raw.commit_call(); + raw.revert_frame(frame); + assert_eq!(raw.tx_writes().count(), 1); + raw.begin_tx(); + assert_eq!(raw.tx_writes().count(), 0); + + let frame = raw.frame_start(); + raw.put(ADDR, key(2), Some(vec![2])); + raw.commit_call(); + raw.revert_frame(frame); + raw.discard_tx(); + assert_eq!(raw.tx_writes().count(), 0); + + raw.begin_tx(); + raw.put(ADDR, key(3), Some(vec![3])); + raw.commit_call(); + raw.begin_tx(); + raw.revert_frame(0); + raw.revert_frame(usize::MAX); + assert_eq!(raw.get(ADDR, key(3)), Some(vec![3])); + } } diff --git a/crates/execution/src/rsk_handler.rs b/crates/execution/src/rsk_handler.rs index 16ff505..1f4ec1f 100644 --- a/crates/execution/src/rsk_handler.rs +++ b/crates/execution/src/rsk_handler.rs @@ -47,7 +47,7 @@ use revm::primitives::{Address, Bytes, U256}; use revm::inspector::JournalExt; use revm::state::EvmState; use std::sync::atomic::{AtomicBool, Ordering}; -use std::sync::Arc; +use std::sync::{Arc, Mutex}; /// `MainnetHandler` with rskj's eternal-frontier account semantics /// (see module docs). @@ -121,6 +121,18 @@ invisible_exception: Arc, /// (`env::header_fits_program_invoke`), throws, and catches nothing: /// the block is not imported, and a miner leaves the transaction out. fail_programs: bool, + /// Shared with `RskPrecompileProvider`: the precompiles that returned in + /// this transaction, each of which rskj's `callToPrecompiledAddress` + /// gives a storage root (`setupContract`) in the call's own track. `None` + /// where the run applies none of them. + called_precompiles: Option>>>, + /// For each frame of contract code running, innermost last: where what + /// the precompiles called under it write outside revm's journal begins, + /// in `RawStorage` (`frame_start`) and in `called_precompiles`. A frame + /// that fails takes it back, as rskj rolls back the track of a call or + /// create that reverts or fails, with what the calls under it committed + /// into it. + frames: Vec<(usize, usize)>, _phantom: core::marker::PhantomData<(CTX, ERROR, FRAME)>, } @@ -154,6 +166,13 @@ impl RskHandler { self } + /// The list of the precompiles called, which the run applies: a frame + /// that fails takes its calls back off it. See the field. + pub fn with_called_precompiles(mut self, called: Arc>>) -> Self { + self.called_precompiles = Some(called); + self + } + #[allow(clippy::too_many_arguments)] pub fn with_rskip125( empty_create_on_unpayable_deposit: bool, @@ -175,6 +194,8 @@ impl RskHandler { // Per-transaction, as before RSKIP-144; `postponing_fees` opts in. postpone_fees: false, fail_programs: false, + called_precompiles: None, + frames: Vec::new(), _phantom: core::marker::PhantomData, } } @@ -192,8 +213,13 @@ where // `JournalExt` is what exposes the entry log, and it is the bound // revm's own inspectors carry. RSKIP-144 collision tracking needs it // for the same reason: a write is only visible while it still stands, - // and a revert pops the entry that recorded it. - Context: ContextTr + JournalExt>, + // and a revert pops the entry that recorded it. `RskChainExt` holds + // what the precompiles write outside the journal, which a frame that + // fails takes back too (see `frames`). + Context: ContextTr< + Chain = crate::raw_storage::RskChainExt, + Journal: JournalTr + JournalExt, + >, Frame = EthFrame, Instructions: InstructionProvider< Context = ::Context, @@ -348,6 +374,9 @@ where /// empty-deploy quirk to internal creates. /// - before RSKIP197, a contract whose call to a precompile threw is /// halted as it takes the call's result (see `aborts_caller`). + /// - what the precompiles called under a frame of contract code write + /// outside revm's journal goes back with the frame if it fails (see + /// `frames`). fn run_exec_loop( &mut self, evm: &mut Self::Evm, @@ -376,6 +405,7 @@ where // Top-level CREATE transaction: rskj's TransactionExecutor.create never // bumps the nonce (even post-RSKIP125), so it stays 0. set_created_contract_nonce(evm, 0); + self.open_frame(evm); loop { let call_or_result = rsk_frame_run(evm, self.empty_create_on_unpayable_deposit)?; @@ -445,6 +475,7 @@ where // created contract's nonce to 1; before, it is 0. let nonce = u64::from(self.rskip125_active); set_created_contract_nonce(evm, nonce); + self.open_frame(evm); continue; } // Do not pop the frame since no new frame was created: @@ -502,7 +533,12 @@ where } } } - ItemOrResult::Result(result) => result, + // A frame of contract code ended, having committed exactly + // when its result is ok (`rsk_frame_run`). + ItemOrResult::Result(result) => { + self.close_frame(evm, result.instruction_result().is_ok()); + result + } }; if *TRACE_FRAMES { tracing::debug!( @@ -558,6 +594,38 @@ where } } +impl RskHandler> +where + EVM: EvmTr, +{ + /// A frame of contract code opens (see `frames`). + fn open_frame(&mut self, evm: &mut EVM) { + let raw = evm.ctx().chain().raw_storage.frame_start(); + let called = self + .called_precompiles + .as_ref() + .and_then(|list| list.lock().ok().map(|list| list.len())) + .unwrap_or_default(); + self.frames.push((raw, called)); + } + + /// The innermost frame of contract code ends, having `committed` or not. + /// One that didn't takes back what the precompiles called under it wrote + /// outside revm's journal (see `frames`). + fn close_frame(&mut self, evm: &mut EVM, committed: bool) { + let Some((raw, called)) = self.frames.pop() else { + return; + }; + if committed { + return; + } + evm.ctx().chain_mut().raw_storage.revert_frame(raw); + if let Some(Ok(mut list)) = self.called_precompiles.as_ref().map(|list| list.lock()) { + list.truncate(called); + } + } +} + /// Hands the journal's entries to the RSKIP-144 collision tracker. /// /// Called immediately before every point that can revert a checkpoint. Under @@ -818,7 +886,10 @@ where impl revm::inspector::InspectorHandler for RskHandler> where EVM: revm::inspector::InspectorEvmTr< - Context: ContextTr>, + Context: ContextTr< + Chain = crate::raw_storage::RskChainExt, + Journal: JournalTr, + >, Frame = EthFrame, Instructions: InstructionProvider< Context = ::Context, From 809e263e21b76e60bceb7d6e109fb2e4ea3efd61 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Mon, 9 Nov 2026 16:42:56 -0800 Subject: [PATCH 061/264] core: a header hashes by the heights it was read with (RSKIP-92) RSKj's BlockHeader holds useRskip92Encoding as a final field, set by the BlockFactory that reads it or the BlockHeaderBuilder that builds it from their activation config at the header's number, and its hash leaves the merged-mining proof and coinbase out where it's set. rustock's Header::rskip92_active read this process's heights instead (ActivationHeights::current), so a header decoded under explicit heights that place orchid elsewhere, as tests read regtest's and testnet's headers in a process whose heights are mainnet's, hashed by the wrong rule, and decode_with_hash cached that hash. A node installs its network's heights before it reads any header, so a node never hashed one otherwise; but the heights a header was read under should decide. A header now keeps RSKIP-92 in KeptFields where the heights it was read under say otherwise than this process's at its height, so a header of this process's chain keeps nothing more than before. Header::with_activations builds a header under explicit heights, as RSKj's builder does, for a header of another chain built in code. Tests that read headers under regtest's or testnet's heights now build the headers they compare with under the same heights: the store's and the freezer's round trips, every layout's cached hashes, and the sync's compressed regtest header, whose merged-mining fields make the two hashes differ, so its hash is now regtest's. Tests: a header each side of this process's orchid, read under heights whose boundary is on the other side, keeps RSKIP-92 and hashes and caches as they say; read under this process's heights it keeps nothing; and with_activations gives the header read, dropping a cached hash it makes stale. --- crates/core/src/types/header.rs | 115 +++++++++++++++++++++++++++++--- crates/storage/src/lib.rs | 30 +++++---- crates/sync/src/tests.rs | 9 ++- 3 files changed, 130 insertions(+), 24 deletions(-) diff --git a/crates/core/src/types/header.rs b/crates/core/src/types/header.rs index 816c8f5..28344bf 100644 --- a/crates/core/src/types/header.rs +++ b/crates/core/src/types/header.rs @@ -114,13 +114,15 @@ pub enum HeaderForm { } /// What rskj's `BlockFactory` reads from a header that [`Header`]'s typed -/// fields can't hold. +/// fields can't hold, and how it reads it. /// -/// rskj keeps these as it reads them and writes them back the same way, so -/// they are in the header's hash, and no rule refuses them where they can -/// appear: a miner picks them. Each is kept only where it differs from -/// what the typed field writes, so a header rskj's miner wrote has none, -/// until its gas limit or minimum gas price passes 256 bits. +/// rskj keeps the fields as it reads them and writes them back the same +/// way, so they are in the header's hash, and no rule refuses them where +/// they can appear: a miner picks them. Each is kept only where it differs +/// from what the typed field writes, so a header rskj's miner wrote has +/// none, until its gas limit or minimum gas price passes 256 bits. How +/// rskj hashes the header is kept only where this process's heights would +/// hash it otherwise ([`KeptFields::rskip92`]). #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] pub struct KeptFields { /// The gas limit's bytes, where they aren't how rskj's miner writes its @@ -160,6 +162,25 @@ pub struct KeptFields { /// `minimum_gas_price` holds `U256::MAX`, which no transaction's price /// reaches either; see [`Header::minimum_gas_price_value`]. pub minimum_gas_price: Option, + /// Whether RSKIP-92 is active for this header, where the heights it was + /// read or built under ([`Header::with_activations`]) say otherwise than + /// this process's at its height. rskj's `BlockFactory` and + /// `BlockHeaderBuilder` set a header's `useRskip92Encoding` by the + /// heights they're given, and its hash leaves the merged-mining proof and + /// coinbase out where it's set. Only a header of another chain than this + /// process's has one. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub rskip92: Option, +} + +impl KeptFields { + /// Keeps whether RSKIP-92 is active at `number` as `activations` say, + /// where this process's heights say otherwise: see [`KeptFields::rskip92`]. + fn set_rskip92(&mut self, number: u64, activations: &crate::config::ActivationHeights) { + let active = number >= activations.orchid; + let here = number >= crate::config::ActivationHeights::current().orchid; + self.rskip92 = (active != here).then_some(active); + } } /// rskj refuses the header: `BlockFactory.decodeHeader` can't read it, or @@ -1063,6 +1084,9 @@ impl Header { } }; + // How rskj will hash it, by the heights it's read under. + kept.set_rskip92(number, activations); + Ok(Self { parent_hash, ommers_hash, @@ -1096,9 +1120,13 @@ impl Header { impl Header { /// RSKIP92 (orchid): the block hash is computed over the header encoding /// WITHOUT the merged-mining merkle proof and coinbase transaction - /// (rskj BlockHeader.getHash → getEncodedForHash, useRskip92Encoding). + /// (rskj BlockHeader.getHash → getEncodedForHash, useRskip92Encoding), + /// as the heights the header was read or built under say + /// ([`KeptFields::rskip92`]), or else this process's. fn rskip92_active(&self) -> bool { - self.number >= crate::config::ActivationHeights::current().orchid + self.kept() + .and_then(|kept| kept.rskip92) + .unwrap_or_else(|| self.number >= crate::config::ActivationHeights::current().orchid) } /// What rskj kept of this header that the typed fields can't hold. @@ -1187,6 +1215,18 @@ impl Header { self.set_kept(|kept| kept.minimum_gas_price = written); } + /// This header as one read or built under `activations` rather than this + /// process's heights: it hashes as they say RSKIP-92 is at its height + /// ([`KeptFields::rskip92`]). A header decoded under explicit heights is + /// one already. One built in code hashes under this process's heights, + /// which in a node are its chain's, so this is for a header of another + /// chain. + pub fn with_activations(mut self, activations: &crate::config::ActivationHeights) -> Self { + let number = self.number; + self.set_kept(|kept| kept.set_rskip92(number, activations)); + self + } + /// Changes what is kept, keeping nothing where nothing differs, and drops /// the cached hashes, which a field set with it makes stale. fn set_kept(&mut self, change: impl FnOnce(&mut KeptFields)) { @@ -1414,6 +1454,62 @@ mod tests { assert_ne!(hash, B256::ZERO); } + /// rskj's `BlockFactory` sets a header's `useRskip92Encoding` by the + /// heights it reads with, and the header keeps it. So a header read under + /// heights that answer RSKIP-92 otherwise than this process's hashes as + /// they say, without its merged-mining proof and coinbase where RSKIP-92 + /// is active and with them where it isn't, and is sent with that hash. + /// Read under this process's heights, it keeps nothing. Built under + /// either (`with_activations`), it is the header read under them. + #[test] + fn a_header_hashes_as_the_heights_it_was_read_under_say() { + let here = crate::config::ActivationHeights::current(); + // Each side of this process's RSKIP-92 boundary, read under heights + // whose boundary is on the other side of the header. + for number in [here.orchid.saturating_sub(1), here.orchid] { + let header = Header { + number, + bitcoin_merged_mining_header: Some(Bytes::from(vec![0; 80])), + ..standard_test_header() + }; + let mut bytes = Vec::new(); + header.encode(&mut bytes); + let active_here = here.orchid <= number; + let there = crate::config::ActivationHeights { + orchid: if active_here { number + 1 } else { number }, + ..here.clone() + }; + let read = + |heights| Header::decode_with_activations(&mut bytes.as_slice(), heights).unwrap(); + let (read_here, read_there) = (read(here), read(&there)); + assert_eq!(read_here.kept(), None, "#{number}"); + assert_eq!( + read_there.kept().and_then(|kept| kept.rskip92), + Some(!active_here), + "#{number}" + ); + + let hash = + |active: bool| keccak256(header.encoded(HeaderForm::Compressed, true, !active)); + assert_ne!( + hash(true), + hash(false), + "only one has the proof and coinbase" + ); + assert_eq!(read_here.hash(), hash(active_here), "#{number}"); + assert_eq!(read_there.hash(), hash(!active_here), "#{number}"); + let sent = + Header::decode_with_hash_and_activations(&mut bytes.as_slice(), &there).unwrap(); + assert_eq!(sent.cached_hash, Some(hash(!active_here)), "#{number}"); + + assert_eq!(header.clone().with_activations(&there), read_there); + assert_eq!(header.clone().with_activations(here), read_here); + assert_eq!(read_there.with_activations(here), read_here); + // The hash it was sent with goes with the heights it was read under. + assert_eq!(sent.with_activations(here).hash(), hash(active_here)); + } + } + /// The items of an RLP list, each with its own header, as written. pub(super) fn items_of(encoded: &[u8]) -> Vec> { let mut buf = encoded; @@ -3177,7 +3273,8 @@ mod versioned_wire_header_tests { umm_root: umm_root.clone(), version_fields: version_fields.clone(), ..real.clone() - }; + } + .with_activations(&heights); if !mining { header.bitcoin_merged_mining_header = None; header.bitcoin_merged_mining_merkle_proof = None; diff --git a/crates/storage/src/lib.rs b/crates/storage/src/lib.rs index 6119e1a..8060ab7 100644 --- a/crates/storage/src/lib.rs +++ b/crates/storage/src/lib.rs @@ -2126,14 +2126,16 @@ mod tests { commitment.extend_from_slice(&[0x55; 32]); let mut slot = Vec::new(); commitment.as_slice().encode(&mut slot); - // rskj's regtest has RSKIPUMM and RSKIP-351 from block 1. + // rskj's regtest has RSKIPUMM and RSKIP-351 from block 1, and its + // headers hash as its heights say. + let regtest = ActivationHeights::regtest(); let compressed = Header { extension_data: Some(Bytes::from(slot)), umm_root: Some(Bytes::new()), ..dummy_header(1) - }; + } + .with_activations(®test); let hash = compressed.hash(); - let regtest = ActivationHeights::regtest(); let dir = tempdir().unwrap(); { @@ -2180,14 +2182,17 @@ mod tests { use rustock_core::config::ActivationHeights; use rustock_core::types::header::HeaderVersionFields; let regtest = ActivationHeights::regtest(); - let v0 = |number| Header { - umm_root: Some(Bytes::new()), - version_fields: Some(HeaderVersionFields { - version: 0, - edges: Some(vec![1]), - base_event: Some(Bytes::new()), - }), - ..dummy_header(number) + let v0 = |number| { + Header { + umm_root: Some(Bytes::new()), + version_fields: Some(HeaderVersionFields { + version: 0, + edges: Some(vec![1]), + base_event: Some(Bytes::new()), + }), + ..dummy_header(number) + } + .with_activations(®test) }; let mut commitment = vec![0xe2, 0x02, 0xa0]; commitment.extend_from_slice(&[0x55; 32]); @@ -2197,7 +2202,8 @@ mod tests { umm_root: Some(Bytes::new()), extension_data: Some(Bytes::from(slot)), ..dummy_header(6) - }; + } + .with_activations(®test); let (batched, single) = (v0(5), v0(7)); let uncles = vec![batched.clone(), held.clone()]; diff --git a/crates/sync/src/tests.rs b/crates/sync/src/tests.rs index 077fe55..3ce92f0 100644 --- a/crates/sync/src/tests.rs +++ b/crates/sync/src/tests.rs @@ -7628,9 +7628,11 @@ fn an_extension(bit: usize) -> rustock_core::types::header_extension::BlockHeade } } -/// `header` as rskj sends it to its peers, holding only the commitment to -/// `extension` in its bloom's slot, with the UMM root rskj reads at -/// regtest's heights and the merged-mining fields every versioned header has. +/// `header` as rskj's regtest sends it to its peers, holding only the +/// commitment to `extension` in its bloom's slot, with the UMM root rskj +/// reads at regtest's heights and the merged-mining fields every versioned +/// header has, and hashed as regtest's heights say: without the +/// merged-mining proof and coinbase (RSKIP-92). fn compressed( header: Header, extension: &rustock_core::types::header_extension::BlockHeaderExtension, @@ -7650,6 +7652,7 @@ fn compressed( kept: None, ..header } + .with_activations(&rustock_core::config::ActivationHeights::regtest()) } /// A store that reads headers as a node of a chain with RSKIP-351 from block From 10878aa6b868805a97cb592ce795898be776003e Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Mon, 9 Nov 2026 18:18:39 -0800 Subject: [PATCH 062/264] execution: releaseBtc reads a pegout's satoshis as RSKj's long RSKj's Coin.toBitcoin takes a pegout's satoshis as a Java long, the low 64 bits of the wei over 10^10, signed, and requestRelease compares that long with the minimum. Before RSKIP427 a rejection's refund is Coin.fromBitcoin of the long, and the events carry the long, which SolidityType.IntType.encodeInt writes sign-extended. rustock read the long but took a negative one as zero, so its events carried zero and its refund was nothing, where RSKj's refund is negative and Repository.transfer moves a negative amount the other way, from the sender to the Bridge. No mainnet or testnet balance comes near 2^63 satoshis, but regtest's genesis funds accounts with 10^30 wei, which wrap. release_btc now keeps the long as RSKj does: a negative one is below every minimum, legacy included; before RSKIP427 the rejection's event and the contract caller's carry it sign-extended, and the refund is its satoshis back in wei, from the sender to the Bridge where it is negative. A sender holding less would go below zero in RSKj, which no balance here can; then nothing moves. An accepted pegout's long is at least the minimum, so it is queued and logged as before. Tests: a pegout whose satoshis wrap negative, or positive, logged and refunded as RSKj does short of RSKIP427, and refunded in full from it; a contract's refused with no refund; and the minimums' edges, inclusive from RSKIP219 and exclusive before it, which no test held. --- crates/execution/src/bridge/peg.rs | 61 +++++++--- crates/execution/src/executor.rs | 181 +++++++++++++++++++++++++++++ 2 files changed, 223 insertions(+), 19 deletions(-) diff --git a/crates/execution/src/bridge/peg.rs b/crates/execution/src/bridge/peg.rs index 301212d..2b35ae0 100644 --- a/crates/execution/src/bridge/peg.rs +++ b/crates/execution/src/bridge/peg.rs @@ -2436,13 +2436,14 @@ pub fn release_btc( let satoshis_per_wei = U256::from(10_000_000_000u64); let amount_satoshis_u256 = call_value_wei / satoshis_per_wei; // rskj `co.rsk.core.Coin.toBitcoin()` takes the satoshis' `longValue()`, - // their low 64 bits, and the minimum, the event and the refund before - // RSKIP427 all use that long. No mainnet or testnet balance comes near - // 2^63 satoshis, but regtest's genesis funds accounts with 10^30 wei, - // which wrap. A long that wraps negative is below every minimum; rskj - // would then log it negative and refund a negative amount, which this - // reads as zero (a regtest-only difference). - let amount_satoshis = u64::try_from(amount_satoshis_u256.as_limbs()[0] as i64).unwrap_or(0); + // their low 64 bits as a signed long, and the minimum, the events and the + // refund before RSKIP427 all use that long. No mainnet or testnet balance + // comes near 2^63 satoshis, but regtest's genesis funds accounts with + // 10^30 wei, which wrap, and wrap negative past 2^63: a negative long is + // below every minimum, and rskj logs and refunds it as it is. + let satoshis = amount_satoshis_u256.as_limbs()[0] as i64; + // The amount the events carry before RSKIP427. + let satoshis_word = long_word(satoshis); // rskj `BridgeSupport.releaseBtc` (BridgeSupport.java:891) rejects a peg-out // requested by a CONTRACT before any min-value check, because a BTC address @@ -2460,7 +2461,7 @@ pub fn release_btc( let event_amount = if hardfork_cfg.has_rskip427(block_number) { call_value_wei } else { - U256::from(amount_satoshis) + satoshis_word }; super::events::log_release_request_rejected( ctx, @@ -2495,14 +2496,14 @@ pub fn release_btc( config, ); let min_value = config.minimum_pegout_tx_value.max(require_funds_for_fee); - if amount_satoshis < min_value { + if i128::from(satoshis) < i128::from(min_value) { // rskj: FEE_ABOVE_VALUE when minValue == requireFundsForFee, else // LOW_AMOUNT. RejectedPegoutReason: LOW_AMOUNT=1, FEE_ABOVE_VALUE=3. Some(if min_value == require_funds_for_fee { 3 } else { 1 }) } else { None } - } else if amount_satoshis <= config.legacy_minimum_pegout_tx_value { + } else if i128::from(satoshis) <= i128::from(config.legacy_minimum_pegout_tx_value) { Some(1) // legacy: always LOW_AMOUNT } else { None @@ -2518,21 +2519,28 @@ pub fn release_btc( // `Coin.fromBitcoin(weis.toBitcoin())` — the wei truncated to satoshi // granularity, which silently kept the sub-satoshi remainder in the // Bridge (mainnet #8,010,127: a 340,353-sat pegout below the 400,000 - // minimum, 4,349,191,903 wei of sub-satoshi remainder). - let refund_wei = if hardfork_cfg.has_rskip427(block_number) { - call_value_wei + // minimum, 4,349,191,903 wei of sub-satoshi remainder). That is the + // long's satoshis back in wei, negative where the long is, and + // rskj's `Repository.transfer` moves a negative amount the other way: + // from the sender to the Bridge. A sender holding less would go + // below zero in rskj, which no balance here can; then nothing moves. + let (from, to, refund_wei) = if hardfork_cfg.has_rskip427(block_number) { + (BRIDGE_ADDR, tx_ctx.rsk_sender, call_value_wei) } else { - U256::from(amount_satoshis) * satoshis_per_wei + let refund = U256::from(satoshis.unsigned_abs()) * satoshis_per_wei; + if satoshis < 0 { + (tx_ctx.rsk_sender, BRIDGE_ADDR, refund) + } else { + (BRIDGE_ADDR, tx_ctx.rsk_sender, refund) + } }; - let _ = ctx - .journal_mut() - .transfer(BRIDGE_ADDR, tx_ctx.rsk_sender, refund_wei); + let _ = ctx.journal_mut().transfer(from, to, refund_wei); // Pre-RSKIP427 the event amount is in satoshis; post-427 it is the // full wei call value (rskj logs `releaseRequestedValueInWeis`). let event_amount = if hardfork_cfg.has_rskip427(block_number) { call_value_wei } else { - U256::from(amount_satoshis) + satoshis_word }; super::events::log_release_request_rejected( ctx, @@ -2544,6 +2552,9 @@ pub fn release_btc( return Ok(PrecompileOutput::new(gas_cost, Bytes::new())); } + // Accepted: the long is at least the minimum, so not negative. + let amount_satoshis = satoshis.unsigned_abs(); + // BTC destination: RIPEMD160(SHA256(compressed_pubkey)) derived from tx sender. // tx_ctx.btc_sender_hash160 was computed by the executor from the RSK tx signature, // matching rskj's BridgeUtils.recoverBtcAddressFromEthTransaction. @@ -2587,7 +2598,7 @@ pub fn release_btc( let event_amount = if hardfork_cfg.has_rskip427(block_number) { call_value_wei } else { - U256::from(amount_satoshis) + satoshis_word }; super::events::log_release_request_received( ctx, @@ -2602,6 +2613,18 @@ pub fn release_btc( Ok(PrecompileOutput::new(gas_cost, Bytes::new())) } +/// A Java `long` as rskj writes it into an event's word +/// (`SolidityType.IntType.encodeInt`): sign-extended to 32 bytes, in a +/// `uint256` field as in an `int256` one. +fn long_word(long: i64) -> U256 { + let magnitude = U256::from(long.unsigned_abs()); + if long < 0 { + magnitude.wrapping_neg() + } else { + magnitude + } +} + // --------------------------------------------------------------------------- // updateCollections // --------------------------------------------------------------------------- diff --git a/crates/execution/src/executor.rs b/crates/execution/src/executor.rs index 8b25a3c..ecab45f 100644 --- a/crates/execution/src/executor.rs +++ b/crates/execution/src/executor.rs @@ -8571,6 +8571,187 @@ mod tests { } } + /// rskj reads a pegout's satoshis as a Java `long` (`Coin.toBitcoin`, + /// `longValue()`), so 2^63 satoshis or more wrap, as regtest's balances + /// of 10^30 wei can reach, and the long decides the rejection. Before + /// RSKIP427 a rejection's event carries that long, sign-extended where + /// it is negative (`SolidityType.IntType.encodeInt`), and its refund is + /// `Coin.fromBitcoin` of it, which `Repository.transfer` moves from the + /// sender to the Bridge where it is negative. A contract's call is + /// refused with no refund, and its event carries the long alike. From + /// RSKIP427 the refund and the event are the call's wei. + #[test] + fn a_pegout_whose_satoshis_wrap_is_logged_and_refunded_as_rskj_does() { + let bridge = crate::precompiles::BRIDGE_ADDR; + let sender = Address::repeat_byte(0xAA); + let contract = Address::repeat_byte(0xC5); + let plenty = U256::from(10u64).pow(U256::from(30)); + let in_wei = |satoshis: U256| satoshis * U256::from(10_000_000_000u64); + let two_63 = U256::from(1u64) << 63; + // A pegout of `satoshis` on mainnet at `height`, past RSKIP185 and + // RSKIP219, sent by the sender or through a contract that sends it + // to the Bridge with no data, which is releaseBtc: the rejection's + // sender and words, and what the sender (its fee aside), the + // contract and the Bridge hold after. + let run = |height: u64, satoshis: U256, through_contract: bool| { + let value = in_wei(satoshis); + let store = Arc::new(MemoryTrieStore::new()); + let mut root = put_account(&TrieNode::empty(), store.as_ref(), &sender, 0, plenty); + let (to, tx_value) = if through_contract { + // No return area, no data; the value; the Bridge; all the gas. + let mut code = vec![0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x7f]; + code.extend_from_slice(&value.to_be_bytes::<32>()); + code.push(0x73); + code.extend_from_slice(bridge.as_slice()); + code.extend_from_slice(&[0x5a, 0xf1, 0x50, 0x00]); // GAS, CALL, POP, STOP + root = put_account_with_code(&root, store.as_ref(), &contract, 1, plenty, &code); + (contract, U256::ZERO) + } else { + (bridge, value) + }; + let gas_price = U256::from(60_000_000u64); + let tx = rustock_core::Transaction { + nonce: 0, + gas_price, + gas_limit: U256::from(1_000_000), + to: Bytes::copy_from_slice(to.as_slice()), + value: tx_value, + input: Bytes::new(), + v: 0, + r: U256::ZERO, + s: U256::ZERO, + cached_rlp: None, + }; + let block_store = + Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let result = RskExecutor::new(RskHardforkConfig::mainnet(), block_store) + .execute_block(&dummy_header(height), &[(tx, sender)], &root, store) + .expect("block executes"); + let tx = &result.tx_results[0]; + assert!(tx.success); + let [log] = &tx.logs[..] else { + panic!("one event, not {}", tx.logs.len()); + }; + let topics = log.data.topics(); + assert_eq!( + topics[0], + alloy_primitives::keccak256("release_request_rejected(address,uint256,int256)") + ); + let words: Vec = log.data.data.chunks(32).map(U256::from_be_slice).collect(); + let holds = |address: &Address| { + result + .state_changes + .get(address) + .map_or(U256::ZERO, |account| account.info.balance) + }; + let fee = U256::from(tx.gas_used) * gas_price; + ( + Address::from_word(topics[1]), + words, + holds(&sender) + fee, + holds(&contract), + holds(&bridge), + ) + }; + + // Short of RSKIP427. Whichever minimum binds, a satoshi is below it. + let before = 7_000_000; + let reason = run(before, U256::from(1u64), false).1[1]; + let negative = two_63 + U256::from(5u64); // a long of 5 - 2^63 + let below_zero = U256::ZERO.wrapping_sub(two_63 - U256::from(5u64)); + let positive = (two_63 << 1) + U256::from(5u64); // a long of 5 + let two_64 = in_wei(two_63 << 1); + // A long that wraps negative is logged sign-extended and refunded + // negative: the Bridge takes 2^63 - 5 satoshis more from the sender. + assert_eq!( + run(before, negative, false), + ( + sender, + vec![below_zero, reason], + plenty - two_64, + U256::ZERO, + two_64 + ) + ); + // One that wraps positive is logged and refunded as the long it is. + assert_eq!( + run(before, positive, false), + ( + sender, + vec![U256::from(5u64), reason], + plenty - two_64, + U256::ZERO, + two_64 + ) + ); + // A contract's is refused with no refund, its long logged alike. + let sent = in_wei(negative); + assert_eq!( + run(before, negative, true), + ( + contract, + vec![below_zero, U256::from(2u64)], + plenty, + plenty - sent, + sent + ) + ); + + // From RSKIP427 (lovell700) the long still refuses the pegout, and + // the sender gets all its wei back, which the event carries. + let lovell700 = 7_338_024; + let reason = run(lovell700, U256::from(1u64), false).1[1]; + assert_eq!( + run(lovell700, negative, false), + (sender, vec![sent, reason], plenty, U256::ZERO, U256::ZERO) + ); + } + + /// rskj `BridgeSupport.requestRelease`: from RSKIP219 a pegout of the + /// minimum itself is queued and one a satoshi short refused; before it, + /// one of the legacy minimum is refused and one a satoshi over queued. + #[test] + fn a_pegout_of_the_minimum_is_queued_from_rskip219_and_refused_before_it() { + use crate::bridge::storage::{ + bridge_storage_key, RELEASE_REQUEST_QUEUE_KEY, RELEASE_REQUEST_QUEUE_WITH_TXHASH_KEY, + }; + let bridge = crate::precompiles::BRIDGE_ADDR; + let queues = [ + RELEASE_REQUEST_QUEUE_KEY, + RELEASE_REQUEST_QUEUE_WITH_TXHASH_KEY, + ] + .map(bridge_storage_key); + let rejected = + alloy_primitives::keccak256("release_request_rejected(address,uint256,int256)"); + // Whether a pegout of `satoshis` at `height` reaches a release queue, + // and the reason its rejection logs, if it logs one. + let run = |cfg: RskHardforkConfig, height: u64, satoshis: u64| { + let value = U256::from(satoshis) * U256::from(10_000_000_000u64); + let result = execute_alone(cfg, height, bridge, value, 1_000_000, &[], &[]); + let tx = &result.tx_results[0]; + assert!(tx.success); + let raw = &result.markers.raw_storage; + let queued = raw + .iter() + .any(|(address, key, _)| *address == bridge && queues.contains(key)); + let log = tx.logs.iter().find(|log| log.data.topics()[0] == rejected); + ( + queued, + log.map(|log| U256::from_be_slice(&log.data.data[32..])), + ) + }; + // rustock's regtest has no federation, so its fee estimate is far + // below its 250,000-satoshi minimum, which binds: LOW_AMOUNT. + let regtest = RskHardforkConfig::regtest; + assert_eq!(run(regtest(), 1, 250_000), (true, None)); + assert_eq!(run(regtest(), 1, 249_999), (false, Some(U256::from(1u64)))); + // Mainnet before iris300, which brings RSKIP219 and RSKIP185's + // events and refunds: the legacy 800,000 satoshis are refused. + let mainnet = RskHardforkConfig::mainnet; + assert_eq!(run(mainnet(), 3_000_000, 800_001), (true, None)); + assert_eq!(run(mainnet(), 3_000_000, 800_000), (false, None)); + } + /// **No call to a precompile panics the node.** /// /// Anyone can send a transaction, or ask for an `eth_call`, that calls a From 78aff7f25758c2a732bd090e7c4945f2c4573d94 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Mon, 9 Nov 2026 18:18:40 -0800 Subject: [PATCH 063/264] core: testnet2's Bitcoin timestamp window is testnet's, as RSKj's RSKj's Constants.getMaxTimestampsDiffInSecs gives two hours from RSKIP297 to testnet and to testnet2 (chain 34), and five minutes elsewhere. rustock gave the two hours to chain 31 alone, though its min_difficulty_at already treats the two chains alike, so a chain 34 header merge-mined in a Bitcoin block more than five minutes and less than two hours from it was refused where RSKj takes it. Tests: chain 34 under testnet's heights widens the window from hop400, and regtest keeps five minutes. --- crates/core/src/validation/merged_mining.rs | 11 ++++------- crates/core/src/validation/tests.rs | 16 +++++++++++++--- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/crates/core/src/validation/merged_mining.rs b/crates/core/src/validation/merged_mining.rs index 7232178..0a4cbcc 100644 --- a/crates/core/src/validation/merged_mining.rs +++ b/crates/core/src/validation/merged_mining.rs @@ -47,7 +47,7 @@ pub struct MergedMiningRule { /// iris300): the Bitcoin block a header was merged-mined in and the header /// itself must be timestamped within `max_timestamps_diff` seconds of each /// other, strictly. rskj's `Constants.getMaxTimestampsDiffInSecs`: 5 minutes, -/// or 2 hours on testnet from RSKIP297 (hop400) on. +/// or 2 hours on testnet and testnet2 from RSKIP297 (hop400) on. /// /// From the activation on a header without a decodable merged-mining header /// is invalid outright, as it is in rskj. The rule does not depend on @@ -66,14 +66,11 @@ impl BitcoinTimestampRule { pub const DEFAULT_MAX_TIMESTAMPS_DIFF_IN_SECS: u64 = 5 * 60; /// rskj `Constants.TESTNET_MAX_TIMESTAMPS_DIFF_IN_SECS`. pub const TESTNET_MAX_TIMESTAMPS_DIFF_IN_SECS: u64 = 120 * 60; - /// rskj `Constants.TESTNET_CHAIN_ID`. - const TESTNET_CHAIN_ID: u8 = 31; - /// rskj `Constants.getMaxTimestampsDiffInSecs` for a block at `number`. pub fn max_timestamps_diff(&self, number: u64) -> u64 { - if self.config.chain_id == Self::TESTNET_CHAIN_ID - && number >= self.config.activation_heights.hop400 - { + // rskj's TESTNET_CHAIN_ID and TESTNET2_CHAIN_ID. + let testnet = matches!(self.config.chain_id, 31 | 34); + if testnet && number >= self.config.activation_heights.hop400 { Self::TESTNET_MAX_TIMESTAMPS_DIFF_IN_SECS } else { Self::DEFAULT_MAX_TIMESTAMPS_DIFF_IN_SECS diff --git a/crates/core/src/validation/tests.rs b/crates/core/src/validation/tests.rs index 144f6da..915faae 100644 --- a/crates/core/src/validation/tests.rs +++ b/crates/core/src/validation/tests.rs @@ -1207,14 +1207,24 @@ fn a_zero_period_turns_rskip179_off_too() { assert!(rule.validate(&header).is_ok(), "a million seconds away"); } -/// rskj `Constants.getMaxTimestampsDiffInSecs`: testnet widens the window to -/// two hours from RSKIP297 (hop400); mainnet keeps five minutes. +/// rskj `Constants.getMaxTimestampsDiffInSecs`: testnet and testnet2 widen the +/// window to two hours from RSKIP297 (hop400); mainnet and regtest keep five +/// minutes. #[test] -fn testnet_widens_the_window_from_rskip297() { +fn testnet_and_testnet2_widen_the_window_from_rskip297() { let testnet = rskip179_rule(crate::config::ChainConfig::testnet()); let hop400 = testnet.config.activation_heights.hop400; assert_eq!(testnet.max_timestamps_diff(hop400 - 1), 5 * 60); assert_eq!(testnet.max_timestamps_diff(hop400), 120 * 60); + // rskj's TESTNET2_CHAIN_ID, under testnet's heights. + let mut testnet2 = crate::config::ChainConfig::testnet(); + testnet2.chain_id = 34; + let testnet2 = rskip179_rule(testnet2); + assert_eq!(testnet2.max_timestamps_diff(hop400 - 1), 5 * 60); + assert_eq!(testnet2.max_timestamps_diff(hop400), 120 * 60); + let regtest = rskip179_rule(crate::config::ChainConfig::regtest()); + let hop400 = regtest.config.activation_heights.hop400; + assert_eq!(regtest.max_timestamps_diff(hop400), 5 * 60); let mainnet = rskip179_rule(crate::config::ChainConfig::mainnet()); let hop400 = mainnet.config.activation_heights.hop400; From 7ef69d609f172dca5097391cb1bccd03480edc48 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Mon, 9 Nov 2026 18:54:00 -0800 Subject: [PATCH 064/264] execution: regtest's federation changes are voted by RSKj's three RSKj's FederationRegTestConstants names three federation-change authorizers, GenNodeKey's 'auth-a' to 'auth-c', and builds a majority authorizer from them, so two votes decide. rustock's regtest listed five, those three and two more, so it took three votes where RSKj takes two. Every other Bridge key on regtest (lock whitelist, fee per KB, locking cap, ERP), and every key on mainnet and testnet, is RSKj's, in RSKj's order. Tests: on regtest one authorizer's vote waits and a second decides. --- crates/execution/src/bridge/constants.rs | 5 ++-- crates/execution/src/bridge/governance.rs | 30 +++++++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/crates/execution/src/bridge/constants.rs b/crates/execution/src/bridge/constants.rs index af4c74f..5a0b664 100644 --- a/crates/execution/src/bridge/constants.rs +++ b/crates/execution/src/bridge/constants.rs @@ -414,13 +414,12 @@ impl BridgeConstants { increase_locking_cap_authorizer_keys: &[ "04450bbaab83ec48b3cb8fbb077c950ee079733041c039a8c4f1539e5181ca1a27589eeaf0fbf430e49d2909f14c767bf6909ad6845831f683416ee12b832e36ed", ], - // FederationRegTestConstants authorizer keys (majority = 3 of 5) + // FederationRegTestConstants authorizer keys, GenNodeKey's 'auth-a' + // to 'auth-c' (majority = 2 of 3) federation_change_authorizer_keys: &[ "04dde17c5fab31ffc53c91c2390136c325bb8690dc135b0840075dd7b86910d8ab9e88baad0c32f3eea8833446a6bc5ff1cd2efa99ecb17801bcb65fc16fc7d991", "04af886c67231476807e2a8eee9193878b9d94e30aa2ee469a9611d20e1e1c1b438e5044148f65e6e61bf03e9d72e597cb9cdea96d6fc044001b22099f9ec403e2", "045d4dedf9c69ab3ea139d0f0da0ad00160b7663d01ce7a6155cd44a3567d360112b0480ab6f31cac7345b5f64862205ea7ccf555fcf218f87fa0d801008fecb61", - "04709f002ac4642b6a87ea0a9dc76eeaa93f71b3185985817ec1827eae34b46b5d869320efb5c5cbe2a5c13f96463fe0210710b53352a4314188daffe07bd54154", - "04aff62315e9c18004392a5d9e39496ff5794b2d9f43ab4e8ade64740d7fdfe896969be859b43f26ef5aa4b5a0d11808277b4abfa1a07cc39f2839b89cc2bc6b4c", ], federation_activation_age_legacy: 10, federation_activation_age: 150, diff --git a/crates/execution/src/bridge/governance.rs b/crates/execution/src/bridge/governance.rs index 508a05d..691de6c 100644 --- a/crates/execution/src/bridge/governance.rs +++ b/crates/execution/src/bridge/governance.rs @@ -1512,6 +1512,36 @@ mod tests { ); } + /// rskj `FederationRegTestConstants`: regtest's federation changes are + /// voted on by three authorizers ('auth-a' to 'auth-c'), so two decide. + #[test] + fn two_of_regtests_three_authorizers_decide_a_federation_change() { + use revm::MainContext; + fn create( + ctx: &mut CTX, + sender: alloy_primitives::Address, + ) -> i64 { + let config = super::super::constants::BridgeConstants::regtest(); + let hardforks = crate::hardfork::RskHardforkConfig::regtest(); + let tx_ctx = crate::precompiles::BridgeTxContext { + rsk_sender: sender, + ..Default::default() + }; + let out = create_federation(ctx, 0, &config, &hardforks, &tx_ctx).unwrap(); + i64::from_be_bytes(out.bytes[24..32].try_into().unwrap()) + } + let regtest = super::super::constants::BridgeConstants::regtest(); + let authorizers = federation_change_authorizers(®test); + assert_eq!(authorizers.len(), 3); + let mut ctx = + revm::Context::mainnet().with_chain(crate::raw_storage::RskChainExt::default()); + assert_eq!(create(&mut ctx, authorizers[0]), 1); + let pending = |ctx: &mut _| bridge_load_bytes_named(ctx, PENDING_FEDERATION_KEY); + assert!(pending(&mut ctx).is_empty(), "one vote waits"); + assert_eq!(create(&mut ctx, authorizers[1]), 1); + assert!(!pending(&mut ctx).is_empty(), "two decide"); + } + /// A Bridge call that reads the whitelist writes it back as the call /// returns, whatever it answers (upstream #291): `save()` writes whatever /// the call loaded. The changes read it after the sender check, the adds From 2a577c0c6123964db6f3b6a1469147ee552ca91c Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Mon, 9 Nov 2026 20:45:12 -0800 Subject: [PATCH 065/264] execution: the Bridge adds BTC headers as bitcoinj-thin's add receiveHeader and receiveHeaders checked only that a header's hash was under the target the header itself claimed. rskj adds a header through bitcoinj-thin's BtcAbstractBlockChain.add (v0.14.4-rsk-20), which also holds the target to the network's range and to what the parent's chain calls for. Since RSKIP200 anyone can call receiveHeader, so a header at an easy target of the caller's choice was stored by rustock and refused by rskj (UNEXPECTED_EXCEPTION): the Bridge's storage, and the state root, split. The new btc_rules module ports bitcoinj's checks exactly: - verifyHeader: the target decoded as decodeCompactBits does, refused when zero, negative (the compact's sign bit) or above the network's maximum (regtest's is 2^263 - 1, past 256 bits); the hash under it. - checkDifficultyTransitions: between retargets the parent's bits as written; at a retarget the parent's target scaled by the interval's length, clamped and capped, compared in the precision the block's own exponent gives it (accuracyBytes); testnet3's twenty-minute rule after 16 February 2012; no retargets on regtest. Around them, as rskj and bitcoinj-thin run: - maxDepthBlockchainAccepted is 25 on every network, not 4,320, so receiveHeader answers BLOCK_TOO_OLD for a header more than 25 below the head. - receiveHeaders takes every header it's given (no cap of 500), looks a header's parent up first and stops at cannotProcessNextBlock's limit even for a header already stored. Before RSKIP124, what follows a header's 80 bytes must parse as a transaction count of 0, or the whole call fails, as makeBlock throws. - connectBlock walks a fork back to the head's chain (findSplit) before storing it, and refuses it where the walk runs out of stored blocks, as for a fork from the genesis block on mainnet and testnet, whose stored chain begins at the checkpoint. findSplit and getPartialChain no longer panic there. Tests: Bitcoin Core's retarget vectors (also checked with an independent port of bitcoinj's arithmetic), testnet3's rule and walk, the target range, regtest's chain, real mainnet headers 849,134 to 849,137; every one of 40 mutations of the new code is caught. Real data: the testnet node's Bridge holds testnet3 headers #5,293,206 to #5,323,205, and all 27,983 that have a whole interval below them pass: 14 retargets, 21,619 through the twenty-minute walk, 6,350 more than twenty minutes after their parent. --- crates/execution/src/bridge/btc_chain.rs | 555 +++++++++++++++++----- crates/execution/src/bridge/btc_rules.rs | 581 +++++++++++++++++++++++ crates/execution/src/bridge/btc_store.rs | 28 +- crates/execution/src/bridge/constants.rs | 26 +- crates/execution/src/bridge/mod.rs | 1 + crates/execution/src/executor.rs | 14 +- 6 files changed, 1058 insertions(+), 147 deletions(-) create mode 100644 crates/execution/src/bridge/btc_rules.rs diff --git a/crates/execution/src/bridge/btc_chain.rs b/crates/execution/src/bridge/btc_chain.rs index 0144337..858dba0 100644 --- a/crates/execution/src/bridge/btc_chain.rs +++ b/crates/execution/src/bridge/btc_chain.rs @@ -21,11 +21,12 @@ use bitcoin::BlockHash; use revm::context_interface::{Block as BlockTr, ContextTr}; use revm::precompile::{PrecompileError, PrecompileOutput}; +use super::btc_rules::{check_difficulty_transitions, verify_header}; use super::btc_store::{ - check_proof_of_work, compute_work, get_stored_block, load_chain_head, put_stored_block, - store_chain_head, StoredBlock, + compute_work, get_stored_block, load_chain_head, put_stored_block, store_chain_head, + StoredBlock, }; -use super::constants::BridgeConstants; +use super::constants::{BridgeConstants, BtcNetwork}; use crate::hardfork::RskHardforkConfig; // receiveHeader result codes — must match rskj BridgeSupport exactly @@ -135,23 +136,19 @@ pub fn receive_header( return Ok(encode_int_result(gas_cost, ERR_UNEXPECTED_EXCEPTION)); } - // 6. btcBlockChain.add(header) — bitcoinj throws on bad PoW or any other - // error, which rskj catches and maps to UNEXPECTED_EXCEPTION. - if !check_proof_of_work(&btc_header) { + // 6. btcBlockChain.add(header): where bitcoinj throws, rskj answers + // UNEXPECTED_EXCEPTION. + if !add( + ctx, + &btc_header, + &parent, + config.btc_network, + use_v2, + rskip199, + ) { return Ok(encode_int_result(gas_cost, ERR_UNEXPECTED_EXCEPTION)); } - // Compute chain work - let work = compute_work(btc_header.bits); - let new_chain_work = parent.chain_work.wrapping_add(work); - - let stored = StoredBlock::new(btc_header, new_height, new_chain_work); - - // Store the block and, when it extends the best chain or wins a reorg, - // update the chain head and the height->hash main-chain index - // (bitcoinj `BtcAbstractBlockChain.connectBlock`). - connect_block(ctx, &stored, &parent, use_v2, rskip199); - // Update timestamp (rskj serializeLong -> RLP) super::storage::bridge_store_timestamp( ctx, @@ -162,79 +159,94 @@ pub fn receive_header( Ok(encode_int_result(gas_cost, SUCCESS)) } -/// Connect a freshly built `StoredBlock` to the BTC header chain, mirroring +/// bitcoinj `BtcAbstractBlockChain.add`, for a header that isn't stored, on +/// its stored `parent`: the header's target must be in range and its hash +/// under it (`verifyHeader`), and its target the one its parent's chain calls +/// for (`checkDifficultyTransitions`); then it's connected (`connectBlock`). +/// `false` where bitcoinj throws. +fn add( + ctx: &mut CTX, + header: &BtcHeader, + parent: &StoredBlock, + network: BtcNetwork, + use_v2: bool, + rskip199: bool, +) -> bool { + verify_header(header, network) + && check_difficulty_transitions(ctx, parent, header, network) + && connect_block(ctx, header, parent, use_v2, rskip199) +} + /// bitcoinj `BtcAbstractBlockChain.connectBlock` in header-only mode -/// (`shouldVerifyTransactions() == false`, which is how the Bridge runs it). +/// (`shouldVerifyTransactions() == false`, which is how the Bridge runs it), +/// for a header that isn't stored, on its stored `parent` (bitcoinj's +/// `storedPrev`). `false` where bitcoinj throws. /// -/// `stored` is the new block (height/work already computed from `parent`); the -/// caller has already verified PoW, rejected already-known blocks, and resolved -/// `parent` (= bitcoinj `storedPrev`). `connectBlock` itself always persists the -/// block via `addToBlockStore`, then: +/// - A header on the head (`storedPrev.equals(chainHead)`) is stored and +/// becomes the head. +/// - Any other is a fork. `findSplit` walks it and the head back to the block +/// they share, and throws where a walk runs out of stored blocks, as it +/// does for a fork from the genesis block on mainnet and testnet, whose +/// stored chain begins at the checkpoint: then nothing is stored. +/// Otherwise the fork is stored, and if it has **strictly more work** than +/// the head (`StoredBlock.moreWorkThan`, so a tie doesn't reorganize), +/// `handleNewBestChain` makes it the head. /// -/// - if the block extends the current best chain (`parent == chainHead`), it -/// becomes the new head directly; or -/// - if it forks the chain but has **strictly more work** than the head -/// (`StoredBlock.moreWorkThan`, i.e. `chainWork >` — ties do NOT reorg), it -/// triggers `handleNewBestChain`, which rewrites the height->hash main-chain -/// index for **every** block on the new branch above the split point, then -/// sets the head. -/// -/// rustock previously only ever set the head's single index entry, so a BTC -/// reorg deeper than one block left stale entries at the intermediate heights — -/// the divergence localized to the `btcBlockHeight-` cell. +/// bitcoinj's checkpoint lock-in decides nothing here: mainnet's checkpoints +/// are at heights only a fork from the genesis block reaches, and testnet3 +/// and regtest have none. fn connect_block( ctx: &mut CTX, - stored: &StoredBlock, + header: &BtcHeader, parent: &StoredBlock, use_v2: bool, rskip199: bool, -) { - // `addToBlockStore`: persist the block unconditionally. - put_stored_block(ctx, stored, use_v2); - +) -> bool { + // `StoredBlock.build`. + let stored = StoredBlock::new( + *header, + parent.height + 1, + parent.chain_work.wrapping_add(compute_work(header.bits)), + ); let head = match load_chain_head(ctx) { - Some(h) => h, - // No head yet (chain not seeded): treat as a direct extension. - None => { - set_chain_head(ctx, stored, use_v2, rskip199); - return; + Some(head) if head.header.block_hash() != parent.header.block_hash() => head, + // On the head, or with no head yet (the chain not seeded). + _ => { + put_stored_block(ctx, &stored, use_v2); + set_chain_head(ctx, &stored, use_v2, rskip199); + return true; } }; - - // Direct extension of the best chain (`storedPrev.equals(chainHead)`). - if parent.header.block_hash() == head.header.block_hash() { - set_chain_head(ctx, stored, use_v2, rskip199); - return; - } - - // Fork. Only reorganize if the new block has strictly more work than the - // current head (bitcoinj `StoredBlock.moreWorkThan` uses `>` — a tie keeps - // the existing head). A fork with <= work is stored but does not move the - // head or touch the main-chain index. + let Some(split) = find_split(&stored, &head, |hash| get_stored_block(ctx, hash)) else { + return false; + }; if stored.chain_work > head.chain_work { - handle_new_best_chain(ctx, stored, &head, use_v2, rskip199); + handle_new_best_chain(ctx, &stored, &split, use_v2, rskip199) + } else { + put_stored_block(ctx, &stored, use_v2); + true } } -/// bitcoinj `handleNewBestChain`: reindex the new branch and set the head. -/// Rewrites `setMainChainBlock(height, hash)` for every block on the new branch -/// from the split point (exclusive) up to the new head, then sets the head -/// (which itself reindexes the head height). +/// bitcoinj `handleNewBestChain`, given the `split` `findSplit` found: every +/// block on the new branch above the split (`getPartialChain`) takes its +/// height in the height->hash main-chain index (`setMainChainBlock`, from +/// RSKIP199), then the new block is stored and becomes the head, which +/// indexes its height again. rustock once indexed the head's height alone, +/// so a reorg deeper than one block left stale entries at the heights below +/// it, in the `btcBlockHeight-` cells. `false` where bitcoinj throws. fn handle_new_best_chain( ctx: &mut CTX, new_head: &StoredBlock, - old_head: &StoredBlock, + split: &StoredBlock, use_v2: bool, rskip199: bool, -) { +) -> bool { + let Some(new_branch) = get_partial_chain(new_head, split, |hash| get_stored_block(ctx, hash)) + else { + return false; + }; if rskip199 { - // Resolve a block's parent (bitcoinj `StoredBlock.getPrev`) from - // Bridge storage. Collected eagerly because the reindex below also - // mutates `ctx`, so we cannot hold a borrowing closure across it. - let split = find_split(new_head, old_head, |h| get_stored_block(ctx, h)); - // bitcoinj getPartialChain(newChainHead, splitPoint): the new branch - // blocks above the split, walked from the head down via getPrev. - let new_branch = get_partial_chain(new_head, &split, |h| get_stored_block(ctx, h)); for block in new_branch { super::storage::bridge_store_btc_block_hash_by_height( ctx, @@ -243,52 +255,52 @@ fn handle_new_best_chain( ); } } + put_stored_block(ctx, new_head, use_v2); set_chain_head(ctx, new_head, use_v2, rskip199); + true } /// bitcoinj `findSplit(newChainHead, oldChainHead, store)`: walk both branches -/// back (by `getPrev`) until they meet at the common ancestor. `get_prev` -/// resolves a block's parent stored block. Returns the split-point stored block. +/// back (by `getPrev`), the higher first, until they meet at the block they +/// share. `get_prev` resolves a block's parent. `None` where a walk runs out +/// of stored blocks, where bitcoinj throws ("Attempt to follow an orphan +/// chain"). fn find_split( new_head: &StoredBlock, old_head: &StoredBlock, mut get_prev: impl FnMut(&BlockHash) -> Option, -) -> StoredBlock { +) -> Option { let mut current_chain_cursor = old_head.clone(); let mut new_chain_cursor = new_head.clone(); while current_chain_cursor.header.block_hash() != new_chain_cursor.header.block_hash() { if current_chain_cursor.height > new_chain_cursor.height { - current_chain_cursor = get_prev(¤t_chain_cursor.header.prev_blockhash) - .expect("findSplit: attempt to follow an orphan chain"); + current_chain_cursor = get_prev(¤t_chain_cursor.header.prev_blockhash)?; } else { - new_chain_cursor = get_prev(&new_chain_cursor.header.prev_blockhash) - .expect("findSplit: attempt to follow an orphan chain"); + new_chain_cursor = get_prev(&new_chain_cursor.header.prev_blockhash)?; } } - current_chain_cursor + Some(current_chain_cursor) } /// bitcoinj `getPartialChain(higher, lower, store)`: blocks from `higher` down /// to (but excluding) `lower`, in head→split order. Walks from the in-memory -/// `higher` and resolves ancestors via `get_prev` (the head's own parent is -/// already persisted by the time we get here). +/// `higher` and resolves its ancestors through `get_prev`. `None` where the +/// walk runs out of stored blocks ("Ran off the end of the chain"). fn get_partial_chain( higher: &StoredBlock, lower: &StoredBlock, mut get_prev: impl FnMut(&BlockHash) -> Option, -) -> Vec { +) -> Option> { debug_assert!(higher.height > lower.height, "higher and lower are reversed"); let mut results = Vec::new(); let mut cursor = higher.clone(); loop { results.push(cursor.clone()); - cursor = get_prev(&cursor.header.prev_blockhash) - .expect("getPartialChain: ran off the end of the chain"); + cursor = get_prev(&cursor.header.prev_blockhash)?; if cursor.header.block_hash() == lower.header.block_hash() { - break; + return Some(results); } } - results } /// Update the chain head (rskj `RepositoryBtcBlockStoreWithCache.setChainHead` + @@ -473,6 +485,25 @@ pub fn ensure_btc_block_store( ensure_btc_chain_seeded(ctx, config, use_v2, hardfork_cfg.has_rskip199(block_number)); } +/// Whether bitcoinj's `BtcBlock.parseTransactions` reads `rest`, the at most +/// five bytes that may follow a header's 80 before RSKIP124, as no +/// transactions: nothing at all, or a transaction count of 0 in any of +/// `VarInt`'s widths, after which it reads no further. Any other count asks +/// for transactions that five bytes can't hold (or, above +/// `Integer.MAX_VALUE`, for a list of negative size), and bitcoinj throws. +fn no_transactions_follow(rest: &[u8]) -> bool { + let zero = |bytes: Option<&[u8]>| bytes.is_some_and(|bytes| bytes.iter().all(|&b| b == 0)); + match rest.first() { + None => true, + Some(0xfd) => zero(rest.get(1..3)), + Some(0xfe) => zero(rest.get(1..5)), + Some(0xff) => zero(rest.get(1..9)), + Some(&count) => count == 0, + } +} + +/// rskj `Bridge.receiveHeaders` and `BridgeSupport.receiveHeaders`: every +/// header, in order, through bitcoinj's `add`, each it can't add skipped. pub fn receive_headers( ctx: &mut CTX, args: &[u8], @@ -502,6 +533,16 @@ pub fn receive_headers( "receiveHeaders: a header is not a header's size", )); } + // Then it parses every header (`makeBlock`), and refuses the whole call + // for one it can't parse. + if headers + .iter() + .any(|header| !no_transactions_follow(&header[80..])) + { + return Err(PrecompileError::other( + "receiveHeaders: a header could not be parsed", + )); + } let rskip434 = hardfork_cfg.has_rskip434(block_number); let rskip199 = hardfork_cfg.has_rskip199(block_number); @@ -509,49 +550,37 @@ pub fn receive_headers( ensure_btc_chain_seeded(ctx, config, use_v2, rskip199); let is_mainnet = matches!(config.btc_network, super::constants::BtcNetwork::Mainnet); - let max_headers = config.max_btc_headers_per_rsk_block as usize; - let count = headers.len().min(max_headers); - - for header_bytes in headers.iter().take(count) { - let btc_header: BtcHeader = match deserialize(&header_bytes[..80]) { - Ok(h) => h, - Err(_) => continue, - }; - - let block_hash = btc_header.block_hash(); - - // Skip if already known - if get_stored_block(ctx, &block_hash).is_some() { + // However many there are. + for header_bytes in &headers { + let Ok(btc_header) = deserialize::(&header_bytes[..80]) else { continue; - } - - // Check PoW - if !check_proof_of_work(&btc_header) { + }; + // The parent first: with none stored, `cannotProcessNextBlock` throws + // on it, and rskj skips the header. + let Some(parent) = get_stored_block(ctx, &btc_header.prev_blockhash) else { continue; - } - - // Find parent - let parent = match get_stored_block(ctx, &btc_header.prev_blockhash) { - Some(p) => p, - None => continue, }; - // rskj cannotProcessNextBlock: bitcoinj's 12-byte chainwork // overflows at BTC mainnet #849,138; headers from there cannot be - // processed until RSKIP434. + // processed until RSKIP434. It stops the loop before `add` looks at + // the header, so even at a header already stored. if parent.height + 1 >= config.block_with_too_much_chain_work_height && is_mainnet && !rskip434 { break; } - - let work = compute_work(btc_header.bits); - let new_chain_work = parent.chain_work.wrapping_add(work); - let new_height = parent.height + 1; - - let stored = StoredBlock::new(btc_header, new_height, new_chain_work); - connect_block(ctx, &stored, &parent, use_v2, rskip199); + // `btcBlockChain.add`, which takes a header already stored as it is. + if get_stored_block(ctx, &btc_header.block_hash()).is_none() { + add( + ctx, + &btc_header, + &parent, + config.btc_network, + use_v2, + rskip199, + ); + } } // rskj `receiveHeaders` is a VOID method (BridgeMethods.RECEIVE_HEADERS @@ -981,12 +1010,12 @@ mod tests { let get_prev = |h: &BlockHash| store.get(h).cloned(); // Split point is the fork ancestor `a`. - let split = find_split(&n4, &o3, get_prev); + let split = find_split(&n4, &o3, get_prev).unwrap(); assert_eq!(split.header.block_hash(), a.header.block_hash()); assert_eq!(split.height, 101); // New branch above the split, head-first: [n4, n3, n2]. - let partial = get_partial_chain(&n4, &split, get_prev); + let partial = get_partial_chain(&n4, &split, get_prev).unwrap(); let hashes: Vec<_> = partial.iter().map(|b| b.header.block_hash()).collect(); assert_eq!( hashes, @@ -1010,14 +1039,28 @@ mod tests { let store = store_of(&[a.clone(), b.clone()]); let get_prev = |h: &BlockHash| store.get(h).cloned(); - let split = find_split(&b, &a, get_prev); + let split = find_split(&b, &a, get_prev).unwrap(); assert_eq!(split.header.block_hash(), a.header.block_hash()); - let partial = get_partial_chain(&b, &a, get_prev); + let partial = get_partial_chain(&b, &a, get_prev).unwrap(); assert_eq!(partial.len(), 1); assert_eq!(partial[0].header.block_hash(), b.header.block_hash()); } + /// bitcoinj's `findSplit` throws where either walk runs out of stored + /// blocks; rustock's panicked. + #[test] + fn find_split_fails_where_a_walk_runs_out() { + let a = link(BlockHash::all_zeros(), 1, 101, 110); + let b = link(a.header.block_hash(), 2, 102, 120); + // Its parent isn't stored. + let stray = link(BlockHash::all_zeros(), 3, 5, 10); + let store = store_of(&[a.clone(), b.clone(), stray.clone()]); + let get_prev = |h: &BlockHash| store.get(h).cloned(); + assert!(find_split(&stray, &b, get_prev).is_none()); + assert!(find_split(&b, &stray, get_prev).is_none()); + } + /// The inlined mainnet checkpoint must decode to the bitcoinj checkpoint /// rskj seeds from: BTC #499,968, /// 000000000000000000296e10eb4987c4eb9b7ba0841102dec4480e5d6b89acb5. @@ -1373,4 +1416,276 @@ mod tests { assert_eq!(seeded(2, false), (None, None), "a contract's call"); assert_eq!(seeded(2, true), (Some(genesis), indexed)); } + + /// A writable context at `block_number`, in a block whose time is well + /// past any `receiveHeader` interval. + fn ctx_at(block_number: u64) -> impl crate::RskContextTr { + use revm::MainContext; + let block = revm::context::BlockEnv { + number: U256::from(block_number), + timestamp: U256::from(1_700_000_000_u64), + ..revm::context::BlockEnv::default() + }; + revm::Context::mainnet() + .with_block(block) + .with_chain(crate::raw_storage::RskChainExt::default()) + } + + fn regtest_genesis() -> BlockHash { + bitcoin::constants::genesis_block(bitcoin::Network::Regtest) + .header + .block_hash() + } + + /// A regtest header on `prev`, mined: at regtest's target about every + /// other nonce does. + fn mine_regtest(prev: BlockHash, seed: u8) -> BtcHeader { + (0..) + .map(|nonce| BtcHeader { + version: Version::from_consensus(1), + prev_blockhash: prev, + merkle_root: TxMerkleNode::from_byte_array([seed; 32]), + time: 1_700_000_000, + bits: CompactTarget::from_consensus(0x207f_ffff), + nonce, + }) + .find(|header| verify_header(header, BtcNetwork::Regtest)) + .unwrap() + } + + /// `length` regtest headers from the genesis block, each on the one + /// before. + fn regtest_chain(length: usize) -> Vec { + let mut prev = regtest_genesis(); + (0..length) + .map(|_| { + let header = mine_regtest(prev, 0x11); + prev = header.block_hash(); + header + }) + .collect() + } + + fn send_headers( + ctx: &mut CTX, + headers: &[BtcHeader], + config: &BridgeConstants, + cfg: &RskHardforkConfig, + ) { + use crate::abi::encode::{self, Arg}; + let encoded: Vec> = headers.iter().map(serialize).collect(); + let slices: Vec<&[u8]> = encoded.iter().map(Vec::as_slice).collect(); + let args = encode::args(&[Arg::BytesArray(&slices)]); + receive_headers(ctx, &args, 0, config, true, cfg).unwrap(); + } + + /// `receiveHeader`'s answer to `header`. + fn send_header( + ctx: &mut CTX, + header: &BtcHeader, + config: &BridgeConstants, + cfg: &RskHardforkConfig, + ) -> Vec { + use crate::abi::encode::{self, Arg}; + let args = encode::args(&[Arg::Bytes(&serialize(header))]); + receive_header(ctx, &args, 0, config, true, cfg) + .unwrap() + .bytes + .to_vec() + } + + fn answer(code: i64) -> Vec { + encode_int_result(0, code).bytes.to_vec() + } + + /// rskj's `receiveHeaders` takes every header it's given: rustock stopped + /// at 500. + #[test] + fn receive_headers_takes_every_header_it_is_given() { + let (config, cfg) = (BridgeConstants::regtest(), RskHardforkConfig::regtest()); + let mut ctx = ctx_at(0); + send_headers(&mut ctx, ®test_chain(501), &config, &cfg); + assert_eq!(load_chain_head(&mut ctx).map(|head| head.height), Some(501)); + } + + /// rskj's `receiveHeader` takes a header 25 below the head + /// (`maxDepthBlockchainAccepted`) and refuses one 26 below: rustock took + /// them down to 4,320. + #[test] + fn receive_header_takes_a_header_25_below_the_head_but_not_26() { + let (config, cfg) = (BridgeConstants::regtest(), RskHardforkConfig::regtest()); + let chain = regtest_chain(27); + // The head is at 27. A header on block 1 is at 2, 25 below it; one on + // the genesis block is 26 below. + for (prev, code) in [ + (chain[0].block_hash(), SUCCESS), + (regtest_genesis(), ERR_BLOCK_TOO_OLD), + ] { + let mut ctx = ctx_at(0); + send_headers(&mut ctx, &chain, &config, &cfg); + let fork = mine_regtest(prev, 0xf0); + assert_eq!(send_header(&mut ctx, &fork, &config, &cfg), answer(code)); + } + } + + /// rskj's `receiveHeader` answers UNEXPECTED_EXCEPTION for a header + /// bitcoinj's `add` throws on: one whose hash is above its target + /// (`verifyHeader`), or one whose hash is under its target but whose target + /// isn't the one its parent's chain calls for + /// (`checkDifficultyTransitions`). rustock checked the hash alone, so + /// anyone could add a header at a target of their choice through the + /// public `receiveHeader`, where rskj refuses it. + #[test] + fn receive_header_refuses_a_header_bitcoinj_cant_add() { + let (config, cfg) = (BridgeConstants::regtest(), RskHardforkConfig::regtest()); + let on_target = mine_regtest(regtest_genesis(), 0x11); + let above_target = (0..) + .map(|nonce| BtcHeader { nonce, ..on_target }) + .find(|header| !verify_header(header, BtcNetwork::Regtest)) + .unwrap(); + let easier = (0..) + .map(|nonce| BtcHeader { + bits: CompactTarget::from_consensus(0x2100_ffff), + nonce, + ..on_target + }) + .find(|header| verify_header(header, BtcNetwork::Regtest)) + .unwrap(); + for refused in [above_target, easier] { + let mut ctx = ctx_at(0); + assert_eq!( + send_header(&mut ctx, &refused, &config, &cfg), + answer(ERR_UNEXPECTED_EXCEPTION) + ); + assert!(get_stored_block(&mut ctx, &refused.block_hash()).is_none()); + } + let mut ctx = ctx_at(0); + assert_eq!( + send_header(&mut ctx, &on_target, &config, &cfg), + answer(SUCCESS) + ); + } + + /// rskj's `receiveHeaders` looks a header's parent up first, and stops at + /// the first header whose height would be past `cannotProcessNextBlock`'s + /// limit, even one already stored, taking none after it; from RSKIP434 it + /// goes on. Real mainnet headers 849,134 and 849,135, after a header + /// stored on 849,137. + #[test] + fn receive_headers_stops_at_a_header_past_the_chain_work_limit_even_one_stored() { + let config = BridgeConstants::mainnet(); + let limit = config.block_with_too_much_chain_work_height; + let real = |hex: &str| { + deserialize::(&alloy_primitives::hex::decode(hex).unwrap()).unwrap() + }; + let h849134 = real("0080b92c24f123130ae29e899f0cab72653722e54cdf3b30445202000000000000000000c72ead65a3b78ab637d1876c00414a77e47bcc5b52667ac1e573633563bea5a695aa7766255d031728d182a8"); + let h849135 = real("00004020bf67910b5d3996ee594848b482ee84d0e28c97a9a2d601000000000000000000865e218552bb92df36c962f5163e84a6c2542584fb36be2fa8b2a4246c73a701f1ae7766255d031791836b22"); + let h849137 = real("00e00820925b77c9ff4d0036aa29f3238cde12e9af9d55c34ed30200000000000000000032a9fa3e12ef87a2327b55db6a16a1227bb381db8b269d90aa3a6e38cf39665f91b47766255d0317c1b1575f"); + let past_the_limit = BtcHeader { + prev_blockhash: h849137.block_hash(), + ..h849135 + }; + let takes_849135 = |block_number: u64| { + let mut ctx = ctx_at(block_number); + let head = StoredBlock::new(h849134, 849_134, U256::from(1)); + put_stored_block(&mut ctx, &head, true); + store_chain_head(&mut ctx, &head, true); + for (header, height) in [(h849137, limit - 1), (past_the_limit, limit)] { + put_stored_block( + &mut ctx, + &StoredBlock::new(header, height, U256::from(1)), + true, + ); + } + let cfg = RskHardforkConfig::mainnet(); + send_headers(&mut ctx, &[past_the_limit, h849135], &config, &cfg); + get_stored_block(&mut ctx, &h849135.block_hash()).is_some() + }; + assert!(!takes_849135(0), "before RSKIP434"); + assert!(takes_849135(7_000_000), "from RSKIP434"); + } + + /// bitcoinj's `connectBlock` walks a fork back to the head's chain + /// (`findSplit`) before it stores it, and throws where the walk runs out of + /// stored blocks. Mainnet's store holds the genesis block and the chain from + /// the checkpoint, so Bitcoin's block 1, on the genesis block, is refused, + /// where rustock stored it. On regtest, whose chain is whole from the + /// genesis block, a fork is stored, and becomes the head only with more + /// work than the head, when every height on its branch is indexed to it. + #[test] + fn a_fork_that_cant_be_walked_back_to_the_heads_chain_is_refused() { + let block_1: BtcHeader = deserialize( + &alloy_primitives::hex::decode( + "010000006fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000\ + 982051fd1e4ba744bbbe680e1fee14677ba1a3c3540bf7b1cdb606e857233e0e\ + 61bc6649ffff001d01e36299", + ) + .unwrap(), + ) + .unwrap(); + assert_eq!( + block_1.block_hash().to_string(), + "00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048" + ); + let (config, cfg) = (BridgeConstants::mainnet(), RskHardforkConfig::mainnet()); + let mut ctx = ctx_at(0); + send_headers(&mut ctx, &[block_1], &config, &cfg); + assert!(get_stored_block(&mut ctx, &block_1.block_hash()).is_none()); + + let (config, cfg) = (BridgeConstants::regtest(), RskHardforkConfig::regtest()); + let chain = regtest_chain(1); + let fork = mine_regtest(regtest_genesis(), 0xf1); + let past = mine_regtest(fork.block_hash(), 0xf2); + let mut ctx = ctx_at(0); + let head = |ctx: &mut _| load_chain_head(ctx).map(|head| head.header); + send_headers(&mut ctx, &[chain[0], fork], &config, &cfg); + assert!(get_stored_block(&mut ctx, &fork.block_hash()).is_some()); + assert_eq!(head(&mut ctx), Some(chain[0]), "a tie keeps the head"); + send_headers(&mut ctx, &[past], &config, &cfg); + assert_eq!(head(&mut ctx), Some(past)); + for (height, block) in [(1, fork), (2, past)] { + assert_eq!( + crate::bridge::storage::bridge_load_btc_block_hash_by_height(&mut ctx, height), + Some(bitcoin_hash_to_b256(&block.block_hash())) + ); + } + } + + /// Before RSKIP124 up to five bytes may follow a header, which rskj's + /// `makeBlock` parses as the block's transactions: a count of 0 parses, in + /// any of `VarInt`'s widths, and anything else refuses the whole call. + #[test] + fn before_rskip124_what_follows_a_header_must_count_no_transactions() { + let cases: [(&[u8], bool); 11] = [ + (&[], true), + (&[0], true), + (&[0, 0xab], true), + (&[0xfd, 0, 0], true), + (&[0xfe, 0, 0, 0, 0], true), + (&[1], false), + (&[0xfc], false), + (&[0xfd, 0], false), + (&[0xfd, 1, 0], false), + (&[0xfe, 0, 0, 0, 0x80], false), + (&[0xff, 0, 0, 0, 0], false), + ]; + for (rest, parses) in cases { + assert_eq!(no_transactions_follow(rest), parses, "{rest:02x?}"); + } + + use crate::abi::encode::{self, Arg}; + // Mainnet's ladder has RSKIP124 from wasabi100. + let (config, cfg) = (BridgeConstants::regtest(), RskHardforkConfig::mainnet()); + let header = mine_regtest(regtest_genesis(), 0x11); + let takes = |rest: &[u8]| { + let mut bytes = serialize(&header); + bytes.extend_from_slice(rest); + let args = encode::args(&[Arg::BytesArray(&[&bytes])]); + let mut ctx = ctx_at(0); + receive_headers(&mut ctx, &args, 0, &config, false, &cfg).is_ok() + && get_stored_block(&mut ctx, &header.block_hash()).is_some() + }; + assert!(takes(&[0])); + assert!(!takes(&[1])); + } } diff --git a/crates/execution/src/bridge/btc_rules.rs b/crates/execution/src/bridge/btc_rules.rs new file mode 100644 index 0000000..b81d955 --- /dev/null +++ b/crates/execution/src/bridge/btc_rules.rs @@ -0,0 +1,581 @@ +//! bitcoinj-thin's checks on a Bitcoin header the Bridge adds to its chain +//! (`BtcAbstractBlockChain.add`, tag `v0.14.4-rsk-20`). The header's target +//! must be in range and its hash under it (`BtcBlock.verifyHeader`), and the +//! target must be the one its parent's chain calls for +//! (`NetworkParameters.checkDifficultyTransitions`, as each network has it). + +use alloy_primitives::U512; +use bitcoin::block::Header as BtcHeader; +use bitcoin::hashes::Hash; + +use super::btc_store::{get_stored_block, StoredBlock}; +use super::constants::BtcNetwork; + +/// `NetworkParameters.TARGET_TIMESPAN`: the two weeks an interval should +/// take, in seconds. +const TARGET_TIMESPAN: i32 = 14 * 24 * 60 * 60; +/// `NetworkParameters.TARGET_SPACING`: the ten minutes a block should take, +/// in seconds. +const TARGET_SPACING: i64 = 10 * 60; +/// `NetworkParameters.INTERVAL`, `TARGET_TIMESPAN / TARGET_SPACING`: the +/// blocks between retargets on mainnet and testnet3. +const INTERVAL: u32 = 2016; +/// `TestNet3Params.testnetDiffDate`, 16 February 2012, in seconds. Testnet3's +/// own rule applies to the blocks after it. +const TESTNET_DIFF_DATE: u64 = 1_329_264_000; + +/// `NetworkParameters.interval`. Regtest's is `Integer.MAX_VALUE`, so its +/// target never changes. +fn interval(network: BtcNetwork) -> u32 { + match network { + BtcNetwork::Mainnet | BtcNetwork::Testnet => INTERVAL, + BtcNetwork::Regtest => i32::MAX as u32, + } +} + +/// `NetworkParameters.maxTarget`: `0x1d00ffff` decoded on mainnet and +/// testnet3, and on regtest `RegTestParams.MAX_TARGET`, 2^263 - 1, more than +/// 256 bits hold. +fn max_target(network: BtcNetwork) -> U512 { + match network { + BtcNetwork::Mainnet | BtcNetwork::Testnet => U512::from(0xffff_u32) << 208_usize, + BtcNetwork::Regtest => (U512::from(1_u8) << 263_usize) - U512::from(1_u8), + } +} + +/// bitcoinj `Utils.decodeCompactBits`: the number `bits` writes, as its sign +/// and its magnitude. The top byte counts the bytes of a big-endian number +/// whose first three are the low bytes of `bits`, cut to that count, and the +/// first of them carries the sign in its top bit. `None` past 512 bits, +/// beyond every network's maximum target. +fn decode_compact(bits: u32) -> Option<(bool, U512)> { + let size = (bits >> 24) as usize; + let negative = size >= 1 && bits & 0x0080_0000 != 0; + let mantissa = bits & 0x007f_ffff; + if size <= 3 { + return Some((negative, U512::from(mantissa >> (8 * (3 - size))))); + } + let shift = 8 * (size - 3); + if mantissa != 0 && (32 - mantissa.leading_zeros()) as usize + shift > 512 { + return None; + } + Some((negative, U512::from(mantissa) << shift)) +} + +/// bitcoinj `Utils.encodeCompactBits` for a number that isn't negative: its +/// length in bytes as `BigInteger.toByteArray()` counts it, then its first +/// three bytes. That length leaves room for a sign bit, so the first of them +/// never has its top bit set, and bitcoinj's adjustment for one that has +/// never applies. +fn encode_compact(value: U512) -> u64 { + let size = value.bit_len() / 8 + 1; + let mantissa = if size <= 3 { + value.as_limbs()[0] << (8 * (3 - size)) + } else { + (value >> (8 * (size - 3))).as_limbs()[0] + }; + mantissa | (size as u64) << 24 +} + +/// bitcoinj `BtcBlock.getDifficultyTargetAsInteger`: the target `bits` +/// claims, or `None` where bitcoinj throws "Difficulty target is bad": +/// zero, negative, or above the network's maximum. +fn difficulty_target(bits: u32, network: BtcNetwork) -> Option { + let (negative, target) = decode_compact(bits)?; + (!negative && !target.is_zero() && target <= max_target(network)).then_some(target) +} + +/// bitcoinj `BtcBlock.verifyHeader`, which is `checkProofOfWork(true)`: the +/// target in range, and the hash, read as a number, at or under it. +/// bitcoinj-thin leaves out bitcoinj's timestamp check, which reads the +/// local clock. +pub(crate) fn verify_header(header: &BtcHeader, network: BtcNetwork) -> bool { + difficulty_target(header.bits.to_consensus(), network).is_some_and(|target| { + // `getHash().toBigInteger()`: the hash in display order read + // big-endian, which is its bytes read little-endian. + U512::from_le_slice(&header.block_hash().to_byte_array()) <= target + }) +} + +/// `NetworkParameters.checkDifficultyTransitions` for the Bridge's network: +/// whether `next`, whose parent is `prev`, claims the target `prev`'s chain +/// calls for. `false` where bitcoinj throws, including where the stored +/// chain it walks back runs out. +pub(crate) fn check_difficulty_transitions( + ctx: &mut CTX, + prev: &StoredBlock, + next: &BtcHeader, + network: BtcNetwork, +) -> bool { + // `TestNet3Params`: its own rule between retargets, for a block after + // `testnetDiffDate` (`nextBlock.getTime().after(testnetDiffDate)`). + if network == BtcNetwork::Testnet + && !is_transition_point(prev, network) + && u64::from(next.time) > TESTNET_DIFF_DATE + { + return testnet3(ctx, prev, next); + } + retarget(ctx, prev, next, network) +} + +/// `isDifficultyTransitionPoint`: the block after `prev` begins an interval. +fn is_transition_point(prev: &StoredBlock, network: BtcNetwork) -> bool { + (u64::from(prev.height) + 1).is_multiple_of(u64::from(interval(network))) +} + +/// `AbstractBitcoinNetParams.checkDifficultyTransitions`. Between retargets a +/// block claims its parent's `bits`, compared as written. At a retarget it +/// claims the parent's target scaled by the time the interval took, held to +/// between a quarter of the two weeks and four times them and to the +/// network's maximum, then cut to the precision the block's own exponent +/// gives it. +fn retarget( + ctx: &mut CTX, + prev: &StoredBlock, + next: &BtcHeader, + network: BtcNetwork, +) -> bool { + if !is_transition_point(prev, network) { + return next.bits == prev.header.bits; + } + // The interval's first block, `interval - 1` blocks back from `prev`, each + // of them stored. + let mut first = prev.clone(); + for _ in 1..interval(network) { + match get_stored_block(ctx, &first.header.prev_blockhash) { + Some(block) => first = block, + None => return false, + } + } + // `(int) (prev.getTimeSeconds() - blockIntervalAgo.getTimeSeconds())`. + let timespan = (i64::from(prev.header.time) - i64::from(first.header.time)) as i32; + let timespan = timespan.clamp(TARGET_TIMESPAN / 4, TARGET_TIMESPAN * 4); + // `Utils.decodeCompactBits(prev.getDifficultyTarget())`. A stored block's + // target is in range, so never negative. + let Some((false, prev_target)) = decode_compact(prev.header.bits.to_consensus()) else { + return false; + }; + let new_target = (prev_target * U512::from(timespan as u32) + / U512::from(TARGET_TIMESPAN as u32)) + .min(max_target(network)); + // Kept to the three bytes below the block's own exponent, written as + // compact bits, and compared with the block's. + let accuracy_bytes = (next.bits.to_consensus() >> 24) as i32 - 3; + let mask = if accuracy_bytes >= 0 { + let shift = 8 * accuracy_bytes as usize; + if shift < 512 { + U512::from(0x00ff_ffff_u32) << shift + } else { + U512::ZERO + } + } else { + U512::from(0x00ff_ffff_u32 >> (8 * accuracy_bytes.unsigned_abs())) + }; + encode_compact(new_target & mask) == u64::from(next.bits.to_consensus()) +} + +/// `TestNet3Params.checkDifficultyTransitions` between retargets. A block more +/// than twenty minutes after its parent, or before it, may claim any target +/// in range: bitcoinj checks nothing then. Otherwise it claims the target of +/// the last block, from its parent back, that isn't at the easiest target, +/// walking back no further than the interval's first block. (bitcoinj also +/// stops at the genesis block, which is at height 0, the first block of an +/// interval.) The targets are compared as numbers here, not as written. +fn testnet3(ctx: &mut CTX, prev: &StoredBlock, next: &BtcHeader) -> bool { + let delta = i64::from(next.time) - i64::from(prev.header.time); + if !(0..=2 * TARGET_SPACING).contains(&delta) { + return true; + } + let network = BtcNetwork::Testnet; + let easiest = max_target(network); + let mut cursor = prev.clone(); + while !cursor.height.is_multiple_of(INTERVAL) { + match difficulty_target(cursor.header.bits.to_consensus(), network) { + Some(target) if target == easiest => {} + Some(_) => break, + None => return false, + } + match get_stored_block(ctx, &cursor.header.prev_blockhash) { + Some(block) => cursor = block, + None => return false, + } + } + match ( + difficulty_target(cursor.header.bits.to_consensus(), network), + difficulty_target(next.bits.to_consensus(), network), + ) { + (Some(cursor_target), Some(next_target)) => cursor_target == next_target, + _ => false, + } +} + +#[cfg(test)] +mod tests { + use super::*; + use alloy_primitives::U256; + use bitcoin::block::Version; + use bitcoin::{BlockHash, CompactTarget, TxMerkleNode}; + + use crate::bridge::btc_store::put_stored_block; + + const EASIEST: u32 = 0x1d00_ffff; + const HARD: u32 = 0x1c0f_ffff; + + fn empty_ctx() -> impl crate::RskContextTr { + use revm::MainContext; + revm::Context::mainnet().with_chain(crate::raw_storage::RskChainExt::default()) + } + + fn header(prev: BlockHash, time: u32, bits: u32, nonce: u32) -> BtcHeader { + BtcHeader { + version: Version::from_consensus(1), + prev_blockhash: prev, + merkle_root: TxMerkleNode::all_zeros(), + time, + bits: CompactTarget::from_consensus(bits), + nonce, + } + } + + /// A chain from `height`, each block with its time and bits, pointing at + /// the one before. + fn chain(height: u32, blocks: &[(u32, u32)]) -> Vec { + let mut prev = BlockHash::all_zeros(); + blocks + .iter() + .zip(height..) + .map(|(&(time, bits), height)| { + let block = StoredBlock::new(header(prev, time, bits, height), height, U256::ZERO); + prev = block.header.block_hash(); + block + }) + .collect() + } + + fn store(ctx: &mut CTX, blocks: &[StoredBlock]) { + for block in blocks { + put_stored_block(ctx, block, true); + } + } + + fn child(prev: &StoredBlock, time: u32, bits: u32) -> BtcHeader { + header(prev.header.block_hash(), time, bits, 0) + } + + fn hash_of(header: &BtcHeader) -> U512 { + U512::from_le_slice(&header.block_hash().to_byte_array()) + } + + /// bitcoinj's `getDifficultyTargetAsInteger` refuses a target that's zero + /// or negative (the compact's sign bit), or above the network's maximum, + /// which on regtest is past 256 bits. + #[test] + fn a_target_is_in_range_where_bitcoinj_takes_it() { + use BtcNetwork::{Mainnet, Regtest, Testnet}; + let easiest = U512::from(0xffff_u32) << 208_usize; + assert_eq!(difficulty_target(EASIEST, Mainnet), Some(easiest)); + assert_eq!(difficulty_target(EASIEST, Testnet), Some(easiest)); + assert_eq!( + difficulty_target(0x1d01_0000, Mainnet), + None, + "one past the maximum" + ); + assert_eq!( + difficulty_target(0x207f_ffff, Testnet), + None, + "regtest's target" + ); + assert_eq!( + difficulty_target(0x1c00_ffff, Mainnet), + Some(U512::from(0xffff_u32) << 200_usize) + ); + assert_eq!( + difficulty_target(0x1c80_ffff, Mainnet), + None, + "the sign bit" + ); + assert_eq!( + difficulty_target(0x0112_3456, Mainnet), + Some(U512::from(0x12_u8)), + "a short exponent cuts the mantissa" + ); + assert_eq!(difficulty_target(0x0100_3456, Mainnet), None, "cut to zero"); + assert_eq!( + difficulty_target(0x0012_3456, Mainnet), + None, + "no bytes at all" + ); + assert_eq!( + difficulty_target(0x207f_ffff, Regtest), + Some(U512::from(0x7f_ffff_u32) << 232_usize) + ); + assert_eq!( + difficulty_target(0x2300_007f, Regtest), + Some(U512::from(0x7f_u8) << 256_usize), + "past 256 bits" + ); + assert_eq!( + difficulty_target(0x2300_0080, Regtest), + None, + "2^263, past regtest's maximum" + ); + assert_eq!( + difficulty_target(0xff7f_ffff, Regtest), + None, + "past 512 bits" + ); + } + + /// A header mined at regtest's target has its hash under its own target, + /// but its target is above mainnet's and testnet's maximum. + #[test] + fn a_header_at_regtests_target_is_out_of_range_on_mainnet_and_testnet() { + let mined = (0..) + .map(|nonce| header(BlockHash::all_zeros(), 1_700_000_000, 0x207f_ffff, nonce)) + .find(|header| verify_header(header, BtcNetwork::Regtest)) + .unwrap(); + assert!(!verify_header(&mined, BtcNetwork::Mainnet)); + assert!(!verify_header(&mined, BtcNetwork::Testnet)); + } + + /// With the compact's sign bit set, no hash is under the target, not even + /// one under the number the other bits write. + #[test] + fn a_negative_target_takes_no_hash() { + let written = U512::from(0xffff_u32) << 232_usize; + let mined = (0..) + .map(|nonce| header(BlockHash::all_zeros(), 1_700_000_000, 0x2080_ffff, nonce)) + .find(|header| hash_of(header) <= written) + .unwrap(); + assert!(!verify_header(&mined, BtcNetwork::Regtest)); + } + + /// Bitcoin Core's `pow_tests`, which bitcoinj computes alike: the time of + /// the interval's first block; the parent's height, time and bits; and the + /// bits the next block must claim. + const RETARGETS: [(u32, u32, u32, u32, u32); 4] = [ + ( + 1_261_130_161, + 32_255, + 1_262_152_739, + 0x1d00_ffff, + 0x1d00_d86a, + ), + // Held to the maximum. + ( + 1_231_006_505, + 2_015, + 1_233_061_996, + 0x1d00_ffff, + 0x1d00_ffff, + ), + // Held to a quarter of the two weeks. + ( + 1_279_008_237, + 68_543, + 1_279_297_671, + 0x1c05_a3f4, + 0x1c01_68fd, + ), + // Held to four times the two weeks. + ( + 1_263_163_443, + 46_367, + 1_269_211_443, + 0x1c38_7f6f, + 0x1d00_e1fd, + ), + ]; + + /// The interval ending at `prev_height`, every block at `bits`. + fn interval_to( + prev_height: u32, + first_time: u32, + prev_time: u32, + bits: u32, + ) -> Vec { + let blocks: Vec<(u32, u32)> = (0..INTERVAL) + .map(|i| match i { + 0 => (first_time, bits), + i if i == INTERVAL - 1 => (prev_time, bits), + i => (first_time + i, bits), + }) + .collect(); + chain(prev_height + 1 - INTERVAL, &blocks) + } + + /// On mainnet and on testnet3, and on testnet3 also after its own rule + /// began, which leaves retargets alone: a retarget reads only how long its + /// interval took, so moving the vectors' times past 16 February 2012 + /// changes nothing else. + #[test] + fn a_retarget_claims_the_target_bitcoinj_computes() { + for (first_time, prev_height, prev_time, bits, expected) in RETARGETS { + for (network, later) in [ + (BtcNetwork::Mainnet, 0), + (BtcNetwork::Testnet, 0), + (BtcNetwork::Testnet, 200_000_000), + ] { + let (first_time, prev_time) = (first_time + later, prev_time + later); + let blocks = interval_to(prev_height, first_time, prev_time, bits); + let prev = blocks.last().unwrap(); + let claims = |ctx: &mut _, bits| { + check_difficulty_transitions( + ctx, + prev, + &child(prev, prev_time + 600, bits), + network, + ) + }; + let mut ctx = empty_ctx(); + store(&mut ctx, &blocks); + assert!( + claims(&mut ctx, expected), + "{expected:#x} after #{prev_height}" + ); + for wrong in [expected - 1, expected + 1] { + assert!(!claims(&mut ctx, wrong), "{wrong:#x} after #{prev_height}"); + } + // Without the interval's first block the walk back runs out. + let mut ctx = empty_ctx(); + store(&mut ctx, &blocks[1..]); + assert!(!claims(&mut ctx, expected)); + } + } + } + + /// bitcoinj compares a retarget's target with the block's in the block's + /// own precision (`accuracyBytes`): the target's three bytes below the + /// exponent the block claims. So a block may also claim the target's next + /// bytes down at a smaller exponent, a harder target, which bitcoinj then + /// holds its hash to: after #32,255, two of them, after #68,543 one. + #[test] + fn a_retarget_takes_the_target_in_the_precision_the_block_claims() { + for (prev_height, harder) in [ + (32_255, &[0x1b6a_528b, 0x1a52_8bc8][..]), + (68_543, &[0x1b68_fd00][..]), + ] { + let (first_time, _, prev_time, bits, _) = + RETARGETS.into_iter().find(|r| r.1 == prev_height).unwrap(); + let blocks = interval_to(prev_height, first_time, prev_time, bits); + let prev = blocks.last().unwrap(); + let mut ctx = empty_ctx(); + store(&mut ctx, &blocks); + for &claimed in harder { + let next = child(prev, prev_time + 600, claimed); + assert!( + check_difficulty_transitions(&mut ctx, prev, &next, BtcNetwork::Mainnet), + "{claimed:#x} after #{prev_height}" + ); + } + } + } + + /// Between retargets a block claims its parent's bits as written: the same + /// number written another way isn't them. Regtest never retargets. + #[test] + fn between_retargets_a_block_claims_its_parents_bits() { + // 0x1c120000 and 0x1d001200 write the same number. + let blocks = chain(100, &[(1_700_000_000, 0x1c12_0000)]); + let mut ctx = empty_ctx(); + store(&mut ctx, &blocks); + let prev = &blocks[0]; + let claims = |ctx: &mut _, bits, network| { + check_difficulty_transitions(ctx, prev, &child(prev, 1_700_000_600, bits), network) + }; + assert!(claims(&mut ctx, 0x1c12_0000, BtcNetwork::Mainnet)); + assert!(!claims(&mut ctx, 0x1d00_1200, BtcNetwork::Mainnet)); + assert!(!claims(&mut ctx, EASIEST, BtcNetwork::Mainnet)); + + // At mainnet's retarget height, regtest still claims its parent's bits. + let blocks = chain(2015, &[(1_700_000_000, 0x207f_ffff)]); + let mut ctx = empty_ctx(); + store(&mut ctx, &blocks); + let prev = &blocks[0]; + let claims = |ctx: &mut _, bits| { + check_difficulty_transitions( + ctx, + prev, + &child(prev, 1_700_000_600, bits), + BtcNetwork::Regtest, + ) + }; + assert!(claims(&mut ctx, 0x207f_ffff)); + assert!(!claims(&mut ctx, 0x207f_fffe)); + } + + /// Whether testnet3 takes `bits` for a block `seconds` after the last of + /// `blocks`, stored from `height`. + fn testnet3_takes(height: u32, blocks: &[(u32, u32)], seconds: i64, bits: u32) -> bool { + let blocks = chain(height, blocks); + let mut ctx = empty_ctx(); + store(&mut ctx, &blocks); + let prev = blocks.last().unwrap(); + let time = (i64::from(prev.header.time) + seconds) as u32; + check_difficulty_transitions( + &mut ctx, + prev, + &child(prev, time, bits), + BtcNetwork::Testnet, + ) + } + + /// Within twenty minutes of its parent a testnet3 block claims the last + /// target before it that isn't the easiest, found past easy blocks. More + /// than twenty minutes after its parent, or before it, any target in + /// range. + #[test] + fn testnet3_takes_the_easiest_target_only_after_twenty_minutes() { + let t = 1_700_000_000; + let blocks = [(t, HARD), (t + 1300, EASIEST), (t + 2600, EASIEST)]; + let height = INTERVAL * 1000; + for seconds in [0, 1200] { + assert!(testnet3_takes(height, &blocks, seconds, HARD)); + assert!(!testnet3_takes(height, &blocks, seconds, EASIEST)); + } + for bits in [HARD, EASIEST, 0x1c7f_ffff] { + assert!(testnet3_takes(height, &blocks, 1201, bits)); + assert!(testnet3_takes(height, &blocks, -1, bits)); + } + } + + /// Testnet3's walk starts at the parent, stops at the interval's first + /// block whatever its target, and refuses where it runs out of blocks. + #[test] + fn testnet3s_walk_starts_at_the_parent_and_stops_at_the_interval() { + let t = 1_700_000_000; + let start = INTERVAL * 1000; + let easy_start = [(t, EASIEST), (t + 1300, EASIEST)]; + assert!(testnet3_takes(start, &easy_start, 600, EASIEST)); + assert!(!testnet3_takes(start, &easy_start, 600, HARD)); + let hard_parent = [(t, EASIEST), (t + 1300, HARD)]; + assert!(testnet3_takes(start + 5, &hard_parent, 600, HARD)); + assert!(!testnet3_takes(start + 5, &hard_parent, 600, EASIEST)); + for bits in [HARD, EASIEST] { + assert!( + !testnet3_takes(start + 5, &easy_start, 600, bits), + "no block before the chain" + ); + } + // 0x1c0fff00 and 0x1d000fff write the same target. + let written_another_way = [(t, 0x1c0f_ff00)]; + assert!(testnet3_takes( + start + 5, + &written_another_way, + 600, + 0x1d00_0fff + )); + } + + /// Up to 16 February 2012, testnet3 had mainnet's rule. + #[test] + fn testnet3s_rule_starts_after_16_february_2012() { + let blocks = [(1_329_262_000, HARD), (1_329_263_500, EASIEST)]; + let height = INTERVAL * 1000 + 5; + // 1_329_264_000 is the date itself, not after it. + assert!(testnet3_takes(height, &blocks, 500, EASIEST)); + assert!(!testnet3_takes(height, &blocks, 500, HARD)); + assert!(testnet3_takes(height, &blocks, 501, HARD)); + assert!(!testnet3_takes(height, &blocks, 501, EASIEST)); + } +} diff --git a/crates/execution/src/bridge/btc_store.rs b/crates/execution/src/bridge/btc_store.rs index 0b9f16b..c8dfc56 100644 --- a/crates/execution/src/bridge/btc_store.rs +++ b/crates/execution/src/bridge/btc_store.rs @@ -326,20 +326,7 @@ pub fn compact_target_to_u256(bits: CompactTarget) -> U256 { } } -/// Check if a block hash meets the proof-of-work target. -/// The hash (interpreted as a LE U256) must be ≤ target. -pub fn check_proof_of_work(header: &BtcHeader) -> bool { - let target = compact_target_to_u256(header.bits); - let hash = header.block_hash(); - let raw = hash.to_raw_hash(); - let hash_bytes: &[u8; 32] = raw.as_byte_array(); - // bitcoin crate stores hashes in "internal byte order" (raw SHA256 output). - // For PoW, these bytes are interpreted as a little-endian 256-bit number - // (matching Bitcoin Core's UintToArith256). Valid blocks have trailing - // zero bytes in internal order (leading zeros in display order). - let hash_u256 = U256::from_le_bytes(*hash_bytes); - hash_u256 <= target -} + // --------------------------------------------------------------------------- // Tests @@ -352,6 +339,9 @@ mod tests { use bitcoin::hashes::Hash; use bitcoin::{CompactTarget, TxMerkleNode}; + use crate::bridge::btc_rules::verify_header; + use crate::bridge::constants::BtcNetwork; + fn test_header(version: i32, nonce: u32) -> BtcHeader { BtcHeader { version: Version::from_consensus(version), @@ -539,7 +529,10 @@ mod tests { #[test] fn rskj_mainnet_849137_passes_pow() { let header = parse_mainnet_header(); - assert!(check_proof_of_work(&header), "mainnet block 849137 should pass PoW"); + assert!( + verify_header(&header, BtcNetwork::Mainnet), + "mainnet block 849137 should pass PoW" + ); } /// Verify compute_work produces a non-zero result for mainnet difficulty. @@ -573,7 +566,10 @@ mod tests { let bytes = hex_to_bytes(hdr_hex); assert_eq!(bytes.len(), 80, "header {i} wrong size"); let hdr: BtcHeader = deserialize(&bytes).expect("header {i} deserialize"); - assert!(check_proof_of_work(&hdr), "mainnet block at index {i} should pass PoW"); + assert!( + verify_header(&hdr, BtcNetwork::Mainnet), + "mainnet block at index {i} should pass PoW" + ); } } diff --git a/crates/execution/src/bridge/constants.rs b/crates/execution/src/bridge/constants.rs index 5a0b664..6b6e64c 100644 --- a/crates/execution/src/bridge/constants.rs +++ b/crates/execution/src/bridge/constants.rs @@ -18,11 +18,10 @@ pub struct BridgeConstants { // -- Header relay -- - /// Maximum BTC headers accepted in a single `receiveHeaders` call. - pub max_btc_headers_per_rsk_block: u32, /// Minimum seconds between `receiveHeader` calls (rate limiting). pub min_seconds_between_calls_receive_header: u64, - /// Maximum depth for BTC blockchain queries. + /// rskj `maxDepthBlockchainAccepted`: how far below the BTC chain's head + /// `receiveHeader` takes a header, 25 on every network. pub max_depth_blockchain_accepted: u32, // -- Peg-in / peg-out limits -- @@ -165,9 +164,8 @@ impl BridgeConstants { btc_network: BtcNetwork::Mainnet, btc2rsk_minimum_acceptable_confirmations: 100, rsk2btc_minimum_acceptable_confirmations: 4000, - max_btc_headers_per_rsk_block: 500, min_seconds_between_calls_receive_header: 300, - max_depth_blockchain_accepted: 4320, + max_depth_blockchain_accepted: 25, legacy_minimum_pegin_tx_value: 1_000_000, // 0.01 BTC minimum_pegin_tx_value: 500_000, // 0.005 BTC legacy_minimum_pegout_tx_value: 800_000, // 0.008 BTC @@ -271,9 +269,8 @@ impl BridgeConstants { btc_network: BtcNetwork::Testnet, btc2rsk_minimum_acceptable_confirmations: 10, rsk2btc_minimum_acceptable_confirmations: 10, - max_btc_headers_per_rsk_block: 500, min_seconds_between_calls_receive_header: 300, - max_depth_blockchain_accepted: 4320, + max_depth_blockchain_accepted: 25, legacy_minimum_pegin_tx_value: 1_000_000, minimum_pegin_tx_value: 500_000, legacy_minimum_pegout_tx_value: 500_000, @@ -381,9 +378,8 @@ impl BridgeConstants { btc_network: BtcNetwork::Regtest, btc2rsk_minimum_acceptable_confirmations: 1, rsk2btc_minimum_acceptable_confirmations: 10, - max_btc_headers_per_rsk_block: 500, min_seconds_between_calls_receive_header: 0, - max_depth_blockchain_accepted: 4320, + max_depth_blockchain_accepted: 25, legacy_minimum_pegin_tx_value: 1_000_000, minimum_pegin_tx_value: 500_000, legacy_minimum_pegout_tx_value: 500_000, @@ -508,4 +504,16 @@ mod tests { assert_eq!(BridgeConstants::testnet().legacy_minimum_pegout_tx_value, 500_000); assert_eq!(BridgeConstants::regtest().legacy_minimum_pegout_tx_value, 500_000); } + + /// rskj's `maxDepthBlockchainAccepted` is 25 on every network. + #[test] + fn rskj_max_depth_blockchain_accepted() { + for c in [ + BridgeConstants::mainnet(), + BridgeConstants::testnet(), + BridgeConstants::regtest(), + ] { + assert_eq!(c.max_depth_blockchain_accepted, 25); + } + } } diff --git a/crates/execution/src/bridge/mod.rs b/crates/execution/src/bridge/mod.rs index df30302..3988b60 100644 --- a/crates/execution/src/bridge/mod.rs +++ b/crates/execution/src/bridge/mod.rs @@ -14,6 +14,7 @@ pub mod btc_block_cache; #[cfg(test)] mod btc_block_cache_readpath_tests; pub mod btc_chain; +pub mod btc_rules; pub mod btc_store; pub mod constants; pub mod events; diff --git a/crates/execution/src/executor.rs b/crates/execution/src/executor.rs index ecab45f..2a9d073 100644 --- a/crates/execution/src/executor.rs +++ b/crates/execution/src/executor.rs @@ -5710,7 +5710,12 @@ mod tests { let mine = |prev: BlockHash, merkle_seed: u8| -> bitcoin::block::Header { (0u32..10_000) .map(|nonce| hdr(prev, merkle_seed, nonce)) - .find(crate::bridge::btc_store::check_proof_of_work) + .find(|header| { + crate::bridge::btc_rules::verify_header( + header, + crate::bridge::constants::BtcNetwork::Regtest, + ) + }) .expect("regtest PoW is trivial") }; @@ -5942,7 +5947,12 @@ mod tests { bits: CompactTarget::from_consensus(0x207f_ffff), nonce, }) - .find(crate::bridge::btc_store::check_proof_of_work) + .find(|header| { + crate::bridge::btc_rules::verify_header( + header, + crate::bridge::constants::BtcNetwork::Regtest, + ) + }) .expect("regtest PoW is trivial"); let block_hash = crate::bridge::btc_chain::bitcoin_hash_to_b256(&header.block_hash()).0; From 4098f463fe1f333a244e63f5ebe30035ab031bca Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Mon, 9 Nov 2026 21:24:17 -0800 Subject: [PATCH 066/264] execution: the Bridge's block index height and testnet's federation age, as RSKj's Two Bridge constants differed from RSKj's on mainnet and testnet. btcHeightWhenBlockIndexActivates. From RSKIP199, RSKj's getStoredBlockAtMainChainHeight finds a block below this height only within maxDepthToSearchBlocksBelowIndexActivation (4,320) of the head, and throws below that. rustock had 696,000 on mainnet and 2,000,000 on testnet; RSKj has 696,022 and 2,039,594. Once the head was more than 4,320 above it, rustock looked up a height in 696,000..696,021 on mainnet, or in 2,000,000..2,039,593 on testnet, where RSKj fails the call. Three methods look a height up this way: getBtcBlockchainBlockHeaderByHeight, getBtcTransactionConfirmations and registerBtcTransaction's validations. preLovellActivationAge. RSKj's FederationTestNetConstants overrides getFederationActivationAge: a federation becomes active 60 blocks after its commitment before RSKIP383, 120 from RSKIP383 until RSKIP419, and 2,400 from there. rustock went from 60 straight to 2,400 at RSKIP383. The new field federation_activation_age_pre_lovell holds testnet's 120; mainnet and regtest have none. Testnet's history shows the step. Nine federations were committed between RSKIP383 (#4,015,800) and RSKIP419 (#6,110,487), from #4,019,051 to #6,068,377, and RSKj's public testnet node answers getFederationCreationBlockNumber with each one's block from exactly 120 blocks after its commitment, and with the previous one's the block before. rustock kept the previous federation active 2,280 blocks longer, so a full sync of testnet would have split at #4,019,172: the first updateCollections from #4,019,171, where RSKj records the new federation's creation block (RSKIP186) and rustock didn't. Tests: the two heights; the activation age at each edge of its steps on testnet and mainnet; a lookup below the index height refused and one at it made, on both networks; and the nine federations of testnet's history, each active from its 120th block and not at its 119th, through getFederationCreationBlockNumber. 6 of 6 mutants caught. --- crates/execution/src/bridge/btc_chain.rs | 27 ++++++++++++ crates/execution/src/bridge/constants.rs | 30 ++++++++++++-- crates/execution/src/bridge/getters.rs | 31 ++++++++++++++ crates/execution/src/bridge/governance.rs | 50 +++++++++++++++++++++-- 4 files changed, 131 insertions(+), 7 deletions(-) diff --git a/crates/execution/src/bridge/btc_chain.rs b/crates/execution/src/bridge/btc_chain.rs index 858dba0..c98cf50 100644 --- a/crates/execution/src/bridge/btc_chain.rs +++ b/crates/execution/src/bridge/btc_chain.rs @@ -1688,4 +1688,31 @@ mod tests { assert!(takes(&[0])); assert!(!takes(&[1])); } + + /// From RSKIP199 rskj finds a main-chain block below + /// `btcHeightWhenBlockIndexActivates` only within + /// `maxDepthToSearchBlocksBelowIndexActivation` of the head, and throws + /// below that, so a getter's call fails where the lookup would go on. With + /// the head more than 4,320 above it, that's below 696,022 on mainnet and + /// below 2,039,594 on testnet. + #[test] + fn below_the_index_activation_rskj_looks_only_so_deep() { + let any_header = bitcoin::constants::genesis_block(bitcoin::Network::Bitcoin).header; + for (config, limit) in [ + (BridgeConstants::mainnet(), 696_022), + (BridgeConstants::testnet(), 2_039_594), + ] { + let mut ctx = empty_ctx(); + let head = StoredBlock::new(any_header, limit + 100_000, U256::ZERO); + store_chain_head(&mut ctx, &head, true); + let lookup = + |ctx: &mut _, height| main_chain_block_at_height(ctx, height, true, Some(&config)); + assert!(matches!( + lookup(&mut ctx, limit - 1), + MainChainLookup::StoreError + )); + // From the limit on the lookup goes on, here to a head not stored. + assert!(matches!(lookup(&mut ctx, limit), MainChainLookup::Absent)); + } + } } diff --git a/crates/execution/src/bridge/constants.rs b/crates/execution/src/bridge/constants.rs index 6b6e64c..560206f 100644 --- a/crates/execution/src/bridge/constants.rs +++ b/crates/execution/src/bridge/constants.rs @@ -53,7 +53,10 @@ pub struct BridgeConstants { // -- BTC block index -- - /// BTC height when the block index feature activates. + /// rskj `btcHeightWhenBlockIndexActivates`: from RSKIP199, a main-chain + /// height below it is found only within + /// `max_depth_to_search_blocks_below_index_activation` of the head + /// (`getStoredBlockAtMainChainHeight`). pub btc_height_when_block_index_activates: u32, /// Max depth to search blocks below the index activation height. pub max_depth_to_search_blocks_below_index_activation: u32, @@ -105,6 +108,10 @@ pub struct BridgeConstants { pub federation_activation_age_legacy: u64, /// Post-RSKIP383 activation age. pub federation_activation_age: u64, + /// rskj `FederationTestNetConstants.preLovellActivationAge`: testnet's + /// activation age from RSKIP383 until RSKIP419. The other networks have + /// none, and use `federation_activation_age` from RSKIP383 on. + pub federation_activation_age_pre_lovell: Option, /// RSKIP419 SVP validation window, in RSK blocks: a proposed federation /// must pass the Sign Validation Protocol within this many blocks of its /// creation or the proposal is discarded (rskj FederationConstants @@ -174,7 +181,7 @@ impl BridgeConstants { max_inputs_per_pegout_transaction: 50, number_of_blocks_between_pegouts: 360, update_bridge_execution_period: 1, - btc_height_when_block_index_activates: 696_000, + btc_height_when_block_index_activates: 696_022, max_depth_to_search_blocks_below_index_activation: 4320, btc_transaction_confirmation_max_depth: 4320, // 5 × MILLICOIN = 5 × 100_000 = 500_000 sat/KB @@ -236,6 +243,7 @@ impl BridgeConstants { ], federation_activation_age_legacy: 18_500, federation_activation_age: 40_320, + federation_activation_age_pre_lovell: None, validation_period_duration_in_blocks: 20_000, funds_migration_age_begin: 0, funds_migration_age_end: 10_585, @@ -279,7 +287,7 @@ impl BridgeConstants { max_inputs_per_pegout_transaction: 50, number_of_blocks_between_pegouts: 360, update_bridge_execution_period: 1, - btc_height_when_block_index_activates: 2_000_000, + btc_height_when_block_index_activates: 2_039_594, max_depth_to_search_blocks_below_index_activation: 4320, btc_transaction_confirmation_max_depth: 4320, // 1 × MILLICOIN = 100_000 sat/KB @@ -330,6 +338,7 @@ impl BridgeConstants { ], federation_activation_age_legacy: 60, federation_activation_age: 2_400, + federation_activation_age_pre_lovell: Some(120), validation_period_duration_in_blocks: 2_000, funds_migration_age_begin: 60, funds_migration_age_end: 900, @@ -419,6 +428,7 @@ impl BridgeConstants { ], federation_activation_age_legacy: 10, federation_activation_age: 150, + federation_activation_age_pre_lovell: None, validation_period_duration_in_blocks: 125, funds_migration_age_begin: 15, funds_migration_age_end: 150, @@ -516,4 +526,18 @@ mod tests { assert_eq!(c.max_depth_blockchain_accepted, 25); } } + + /// rskj's `btcHeightWhenBlockIndexActivates`: 696,022 on mainnet and + /// 2,039,594 on testnet. + #[test] + fn rskj_btc_height_when_block_index_activates() { + assert_eq!( + BridgeConstants::mainnet().btc_height_when_block_index_activates, + 696_022 + ); + assert_eq!( + BridgeConstants::testnet().btc_height_when_block_index_activates, + 2_039_594 + ); + } } diff --git a/crates/execution/src/bridge/getters.rs b/crates/execution/src/bridge/getters.rs index ed205ee..5722c72 100644 --- a/crates/execution/src/bridge/getters.rs +++ b/crates/execution/src/bridge/getters.rs @@ -1433,6 +1433,37 @@ mod tests { ); } + /// Testnet's history: each of the nine federations committed between + /// RSKIP383 (#4,015,800) and RSKIP419 (#6,110,487) became the active one + /// 120 blocks after its commitment. rskj's public testnet node answers + /// `getFederationCreationBlockNumber` with the previous federation's block + /// at the 119th block and with the new one's at the 120th. + #[test] + fn testnets_federations_before_lovell_became_active_after_120_blocks() { + let (config, forks) = (BridgeConstants::testnet(), RskHardforkConfig::testnet()); + // (the previous federation's creation block, the committed one's) + let history = [ + (3_945_006, 4_019_051), + (4_019_051, 4_063_391), + (4_063_391, 4_258_297), + (4_258_297, 4_487_930), + (4_487_930, 5_334_163), + (5_334_163, 5_787_959), + (5_787_959, 5_854_737), + (5_854_737, 6_065_057), + (6_065_057, 6_068_377), + ]; + for (previous, committed) in history { + for (block, active) in [(committed + 119, previous), (committed + 120, committed)] { + let mut ctx = ctx_at(block); + store_federation(&mut ctx, NEW_FEDERATION_KEY, &[member(1)], 1_000, committed); + store_federation(&mut ctx, OLD_FEDERATION_KEY, &[member(2)], 900, previous); + let out = get_federation_creation_block_number(&mut ctx, 0, &config, &forks); + assert_eq!(as_int(&out.unwrap()), active as i64, "testnet #{block}"); + } + } + } + // ---- creation time units ---- /// RSKIP419 changes the unit from milliseconds to seconds. Storage keeps diff --git a/crates/execution/src/bridge/governance.rs b/crates/execution/src/bridge/governance.rs index 691de6c..bb13530 100644 --- a/crates/execution/src/bridge/governance.rs +++ b/crates/execution/src/bridge/governance.rs @@ -376,15 +376,21 @@ pub(crate) fn federation_format_version( } } +/// rskj `FederationConstants.getFederationActivationAge`: the legacy age +/// before RSKIP383 and the age after it, with testnet's own step between +/// RSKIP383 and RSKIP419 (`FederationTestNetConstants` overrides it to answer +/// `preLovellActivationAge` there). pub(crate) fn federation_activation_age( config: &super::constants::BridgeConstants, hardfork_cfg: &crate::hardfork::RskHardforkConfig, block_number: u64, ) -> u64 { - if hardfork_cfg.has_rskip383(block_number) { - config.federation_activation_age - } else { - config.federation_activation_age_legacy + if !hardfork_cfg.has_rskip383(block_number) { + return config.federation_activation_age_legacy; + } + match config.federation_activation_age_pre_lovell { + Some(age) if !hardfork_cfg.has_rskip419(block_number) => age, + _ => config.federation_activation_age, } } @@ -1483,6 +1489,42 @@ mod tests { assert_eq!(serialization::rlp_encode_u64(next_creation), vec![0x83, 0x46, 0xfe, 0xed]); } + /// rskj's testnet constants activate a federation 60 blocks after its + /// commitment before RSKIP383 (fingerroot500, #4,015,800), 120 until + /// RSKIP419 (lovell700, #6,110,487), then 2,400. Mainnet has no middle step: + /// 18,500 before RSKIP383, 40,320 after. + #[test] + fn testnets_activation_age_has_a_step_before_lovell() { + use super::super::constants::BridgeConstants; + use crate::hardfork::RskHardforkConfig; + let (testnet, hf) = (BridgeConstants::testnet(), RskHardforkConfig::testnet()); + for (block, age) in [ + (4_015_799, 60), + (4_015_800, 120), + (6_110_486, 120), + (6_110_487, 2_400), + ] { + assert_eq!( + federation_activation_age(&testnet, &hf, block), + age, + "testnet #{block}" + ); + } + let (mainnet, hf) = (BridgeConstants::mainnet(), RskHardforkConfig::mainnet()); + for (block, age) in [ + (5_467_999, 18_500), + (5_468_000, 40_320), + (7_338_023, 40_320), + (7_338_024, 40_320), + ] { + assert_eq!( + federation_activation_age(&mainnet, &hf, block), + age, + "mainnet #{block}" + ); + } + } + /// rskj `addOneOffLockWhitelistAddress` parses the address first, -2 where /// it does not parse, then reads the value with `longValueExact`, which /// throws for one beyond a Java long -- all before the sender is checked. From eef16e3d75219905a97e165d0e0a8bc0d34db3cf Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Mon, 9 Nov 2026 22:24:37 -0800 Subject: [PATCH 067/264] execution: RSKj's regtest Bridge, genesis federation and free senders A chain started from an RSKj genesis file (--genesis) is RSKj's regtest and runs on BridgeConstants::rskj_regtest. rustock's own regtest is another chain, with quicker settings of its own, and keeps them. rskj_regtest held only RSKj's genesis federation keys. It now holds the rest of BridgeRegTestConstants and FederationRegTestConstants where they differ from rustock's regtest: 3 confirmations each way, receiveHeader at most every 300 seconds, pegouts every 50 blocks, the block index from BTC height 10 searched 5 deep, and no block with too much chain work (Integer.MAX_VALUE). The genesis federation is built in the format its constants name, as RSKj's FederationSupportImpl.getGenesisFederation builds it from genesisFederationType (the new genesis_federation_format): standard multisig on mainnet and testnet, as before, and P2SH-P2WSH ERP, with the regtest ERP keys and a 500-block delay, on RSKj's regtest. rustock built every genesis federation as standard multisig, so on RSKj's regtest getFederationAddress answered 2N5muMepJizJE1gR7FbHJU6CD18V3BpNF9p where RSKj answers 2MvQSG61B4LtKLECeo1dRDeDWXgJfnr3fC7: peg-ins to RSKj's address went unrecognized, and the federation's spends used the wrong script. The first federation change now moves it to the old slot as format 4000 and retires it under its own script, in the legacy handover and in RSKIP419's. Free Bridge senders are derived as RSKj's BridgeUtils.isFreeBridgeTx derives them: the genesis federation's members and the federation-change, lock-whitelist and fee-per-KB authorizers, taken from those lists rather than from a separate hand-kept union (authorized_free_tx_keys, the same 7 keys on mainnet and testnet, gone). On RSKj's regtest that is 3 federators and 5 authorizers, and the transaction pool takes them too, as the block processor does. RSKj's regtest never activates areBridgeTxsPaid (afterBridgeSync = -1), but rustock's regtest activations turn it on at block 0, so nothing is free there yet. updateBridgeExecutionPeriod is RSKj's federator's interval between updateCollections calls, in milliseconds: 180,000 on mainnet and testnet, 15,000 on RSKj's regtest. No node reads it; rustock had 1. Tests: rskj_regtest's values against RSKj's, and rustock's regtest unchanged; getFederationAddress at block 1 with nothing stored, on mainnet and testnet as RSKj's public nodes answer, and on RSKj's regtest as an independent port of RSKj's P2shErpRedeemScriptBuilder computes it; the genesis federation leaving in its own format and script through both handovers; RSKj's regtest's free senders, in the executor and in the pool. 12 of 12 mutants caught. --- crates/cli/src/main.rs | 19 +++- crates/execution/src/bridge/constants.rs | 106 ++++++++++++++-------- crates/execution/src/bridge/getters.rs | 28 ++++++ crates/execution/src/bridge/governance.rs | 103 +++++++++++++++++++-- crates/execution/src/bridge/peg.rs | 7 +- crates/execution/src/executor.rs | 36 +++++++- crates/sync/src/txpool.rs | 28 ++++++ 7 files changed, 268 insertions(+), 59 deletions(-) diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index b425123..e676618 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -2485,10 +2485,21 @@ async fn run(local_offset: Option) -> Result<()> { trie_store_for_pool.clone(), ) .with_gas_price_tracker(gas_price_tracker.clone()) - .with_chain_rules(rustock_sync::txpool::ChainRules::for_chain( - hardfork_cfg_for_pool, - config.min_sequential_set_gas_limit, - )); + .with_chain_rules({ + let rules = rustock_sync::txpool::ChainRules::for_chain( + hardfork_cfg_for_pool, + config.min_sequential_set_gas_limit, + ); + // A chain from an rskj genesis file is rskj's regtest: its federators' + // and authorizers' Bridge calls are free, as the block processor counts. + if config.genesis_spec.is_some() { + rules.with_bridge_constants( + &rustock_execution::bridge::constants::BridgeConstants::rskj_regtest(), + ) + } else { + rules + } + }); if let Some(cache) = &sender_cache { pool_builder = pool_builder.with_sender_cache(cache.clone()); } diff --git a/crates/execution/src/bridge/constants.rs b/crates/execution/src/bridge/constants.rs index 560206f..d36c0d4 100644 --- a/crates/execution/src/bridge/constants.rs +++ b/crates/execution/src/bridge/constants.rs @@ -48,7 +48,9 @@ pub struct BridgeConstants { // -- Update period -- - /// How often `updateCollections` should be called (in RSK blocks). + /// rskj `updateBridgeExecutionPeriod`: how often a federator calls + /// `updateCollections`, in milliseconds. Only rskj's federator reads it; + /// neither node does. pub update_bridge_execution_period: u64, // -- BTC block index -- @@ -73,13 +75,11 @@ pub struct BridgeConstants { // -- Free bridge transactions (pre-areBridgeTxsPaid) -- /// Genesis federation BTC public keys (compressed hex), from rskj - /// Federation*NetConstants. Their derived RSK addresses may send free - /// transactions to the Bridge before areBridgeTxsPaid activates. + /// Federation*NetConstants. Their derived RSK addresses, and the + /// federation-change, lock-whitelist and fee-per-KB authorizers', may send + /// free transactions to the Bridge before areBridgeTxsPaid activates + /// (rskj `BridgeUtils.isFreeBridgeTx`; `executor::free_bridge_senders`). pub genesis_federation_public_keys: &'static [&'static str], - /// Authorized senders for free bridge txs (uncompressed hex): the - /// federation-change, lock-whitelist and feePerKb authorizer keys - /// (rskj BridgeUtils.isFromAuthorizedSender). - pub authorized_free_tx_keys: &'static [&'static str], /// Lock-whitelist change authorizer keys (rskj WhitelistConstants, /// AddressBasedAuthorizer with minimum ONE). pub whitelist_authorizer_keys: &'static [&'static str], @@ -130,6 +130,11 @@ pub struct BridgeConstants { /// in a millis field: Instant.ofEpochMilli semantics, rskj /// genesisFederationCreationTime). pub genesis_federation_creation_time_millis: u64, + /// rskj `FederationConstants.genesisFederationType`, the format the genesis + /// federation is built in (`FederationSupportImpl.getGenesisFederation`): + /// 1000, standard multisig, on mainnet and testnet; 4000, P2SH-P2WSH ERP + /// with the ERP keys and delay, on rskj's regtest. + pub genesis_federation_format: u64, /// Hash160 of the hardcoded "old federation" P2SH address (rskj /// FederationConstants.oldFederationAddress). A btc tx spending this @@ -180,7 +185,7 @@ impl BridgeConstants { minimum_pegout_value_percentage_to_receive_after_fee: 80, max_inputs_per_pegout_transaction: 50, number_of_blocks_between_pegouts: 360, - update_bridge_execution_period: 1, + update_bridge_execution_period: 180_000, btc_height_when_block_index_activates: 696_022, max_depth_to_search_blocks_below_index_activation: 4320, btc_transaction_confirmation_max_depth: 4320, @@ -204,18 +209,6 @@ impl BridgeConstants { "02c6018fcbd3e89f3cf9c7f48b3232ea3638eb8bf217e59ee290f5f0cfb2fb9259", "03b65694ccccda83cbb1e56b31308acd08e993114c33f66a456b627c2c1c68bed6", ], - // federationChange + lockWhitelist + feePerKb authorizers - // (FederationMainNetConstants, WhitelistMainNetConstants, - // FeePerKbMainNetConstants) - authorized_free_tx_keys: &[ - "04e593d4cde25137b13f19462bc4c02e97ba2ed5a3860813497abf9b4eeb9259e37e0384d12cfd2d9a7a0ba606b31ee34317a9d7f4a8591c6bcf5dfd5563248b2f", - "045e7f2563e73d44d149c19cffca36e1898597dc759d76166b8104103c0d3f351a8a48e3a224544e9a649ad8ebcfdbd6c39744ddb85925f19c7e3fd48f07fc1c06", - "0441945e4e272936106f6200b36162f3510e8083535c15e175ac82deaf828da955b85fd72b7534f2a34cedfb45fa63b728cc696a2bd3c5d39ec799ec2618e9aa9f", - "041a2449e9d63409c5a8ea3a21c4109b1a6634ee88fd57176d45ea46a59713d5e0b688313cf252128a3e49a0b2effb4b413e5a2525a6fa5894d059f815c9d9efa6", - "0448f51638348b034995b1fd934fe14c92afde783e69f120a46ee16eb6bdc2e4f6b5e37772094c68c0dea2b1be3d96ea9651a9eebda7304914c8047f4e3e251378", - "0484c66f75548baf93e322574adac4e4579b6a53f8d11fab640e14c90118e6983ef24b0de349a3e88f72e81e771ae1c897cef446fd7f4da71778c532aee3b6c41b", - "04bb6435dc1ea12da843ebe213893d136c1624acd681fff82551498ae00bf28e9323164b00daf925fa75177463b8254a2aae8a1713e4d851a84ea369c193e9ce51", - ], // WhitelistMainNetConstants single authorizer key whitelist_authorizer_keys: &[ "041a2449e9d63409c5a8ea3a21c4109b1a6634ee88fd57176d45ea46a59713d5e0b688313cf252128a3e49a0b2effb4b413e5a2525a6fa5894d059f815c9d9efa6", @@ -249,6 +242,7 @@ impl BridgeConstants { funds_migration_age_end: 10_585, special_case_funds_migration_age_end: 172_800, genesis_federation_creation_time_millis: 1_514_948_400, + genesis_federation_format: 1000, // 35JUi1FxabGdhygLhnNUEFG4AgvpNMgxK1 old_federation_address_hash160: [ 0x27, 0x9d, 0x4b, 0x44, 0xe8, 0xcf, 0x5e, 0x3f, 0x04, 0xc0, 0xea, 0x21, 0xc7, @@ -286,7 +280,7 @@ impl BridgeConstants { minimum_pegout_value_percentage_to_receive_after_fee: 80, max_inputs_per_pegout_transaction: 50, number_of_blocks_between_pegouts: 360, - update_bridge_execution_period: 1, + update_bridge_execution_period: 180_000, btc_height_when_block_index_activates: 2_039_594, max_depth_to_search_blocks_below_index_activation: 4320, btc_transaction_confirmation_max_depth: 4320, @@ -299,18 +293,6 @@ impl BridgeConstants { "0344a3c38cd59afcba3edcebe143e025574594b001700dec41e59409bdbd0f2a09", "034844a99cd7028aa319476674cc381df006628be71bc5593b8b5fdb32bb42ef85", ], - // federationChange + lockWhitelist + feePerKb authorizers - // (FederationTestNetConstants, WhitelistTestNetConstants, - // FeePerKbTestNetConstants) - authorized_free_tx_keys: &[ - "04d9052c2022f6f35da53f04f02856ff5e59f9836eec03daad0328d12c5c66140205da540498e46cd05bf63c1201382dd84c100f0d52a10654159965aea452c3f2", - "04bf889f2035c8c441d7d1054b6a449742edd04d202f44a29348b4140b34e2a81ce66e388f40046636fd012bd7e3cecd9b951ffe28422334722d20a1cf6c7926fb", - "047e707e4f67655c40c539363fb435d89574b8fe400971ba0290de9c2adbb2bd4e1e5b35a2188b9409ff2cc102292616efc113623483056bb8d8a02bf7695670ea", - "04bf7e3bca7f7c58326382ed9c2516a8773c21f1b806984bb1c5c33bd18046502d97b28c0ea5b16433fbb2b23f14e95b36209f304841e814017f1ede1ecbdcfce3", - "04701d1d27f8c2ae97912d96fb1f82f10c2395fd320e7a869049268c6b53d2060dfb2e22e3248955332d88cd2ae29a398f8f3858e48dd6d8ffbc37dfd6d1aa4934", - "045ef89e4a5645dc68895dbc33b4c966c3a0a52bb837ecdd2ba448604c4f47266456d1191420e1d32bbe8741f8315fde4d1440908d400e5998dbed6549d499559b", - "0455db9b3867c14e84a6f58bd2165f13bfdba0703cb84ea85788373a6a109f3717e40483aa1f8ef947f435ccdf10e530dd8b3025aa2d4a7014f12180ee3a301d27", - ], // WhitelistTestNetConstants single authorizer key whitelist_authorizer_keys: &[ "04bf7e3bca7f7c58326382ed9c2516a8773c21f1b806984bb1c5c33bd18046502d97b28c0ea5b16433fbb2b23f14e95b36209f304841e814017f1ede1ecbdcfce3", @@ -344,6 +326,7 @@ impl BridgeConstants { funds_migration_age_end: 900, special_case_funds_migration_age_end: 900, genesis_federation_creation_time_millis: 1_538_967_600, + genesis_federation_format: 1000, // 2N7ZgQyhFKm17RbaLqygYbS7KLrQfapyZzu old_federation_address_hash160: [ 0x9d, 0x0f, 0x4e, 0xb1, 0xed, 0xb9, 0xbc, 0xb7, 0x07, 0x8b, 0x36, 0xf4, 0x13, @@ -366,18 +349,33 @@ impl BridgeConstants { } } - /// rskj's `BridgeRegTestConstants`: [`BridgeConstants::regtest`] with - /// rskj's default regtest genesis federation, which REMASC pays from the - /// first payout block on. rustock's own regtest has no federation; a chain - /// started from an rskj genesis file (`--genesis`) is rskj's regtest and - /// needs this one, or the federators' accounts are never created. + /// rskj's `BridgeRegTestConstants` and `FederationRegTestConstants`. A chain + /// started from an rskj genesis file (`--genesis`) is rskj's regtest and runs + /// on these; rustock's own regtest ([`BridgeConstants::regtest`]) is another + /// chain, with no federation and quicker settings of its own. + /// + /// The genesis federation is rskj's default regtest one: three keys, built + /// as a P2SH-P2WSH ERP federation with the regtest ERP keys and a 500-block + /// delay, at 2MvQSG61B4LtKLECeo1dRDeDWXgJfnr3fC7. REMASC pays it from the + /// first payout block on, or the federators' accounts are never created, + /// and its members' Bridge transactions are free while `areBridgeTxsPaid` + /// is off. pub fn rskj_regtest() -> Self { Self { + btc2rsk_minimum_acceptable_confirmations: 3, + rsk2btc_minimum_acceptable_confirmations: 3, + min_seconds_between_calls_receive_header: 300, + number_of_blocks_between_pegouts: 50, + update_bridge_execution_period: 15_000, + btc_height_when_block_index_activates: 10, + max_depth_to_search_blocks_below_index_activation: 5, genesis_federation_public_keys: &[ "0362634ab57dae9cb373a5d536e66a8c4f67468bbcfb063809bab643072d78a124", "03c5946b3fbae03a654237da863c9ed534e0878657175b132b8ca630f245df04db", "02cd53fc53a07f211641a677d250f6de99caf620e8e77071e811a28b3bcddf0be1", ], + genesis_federation_format: 4000, + block_with_too_much_chain_work_height: i32::MAX as u32, ..Self::regtest() } } @@ -401,9 +399,8 @@ impl BridgeConstants { max_depth_to_search_blocks_below_index_activation: 4320, btc_transaction_confirmation_max_depth: 4320, genesis_fee_per_kb: 100_000, - // Regtest has areBridgeTxsPaid from genesis; no free senders needed. + // rustock's own regtest has no federation; `rskj_regtest` has rskj's. genesis_federation_public_keys: &[], - authorized_free_tx_keys: &[], // WhitelistRegTestConstants single authorizer key whitelist_authorizer_keys: &[ "04641fb250d7ca7a1cb4f530588e978013038ec4294d084d248869dd54d98873e45c61d00ceeaeeb9e35eab19fa5fbd8f07cb8a5f0ddba26b4d4b18349c09199ad", @@ -434,6 +431,7 @@ impl BridgeConstants { funds_migration_age_end: 150, special_case_funds_migration_age_end: 150, genesis_federation_creation_time_millis: 1_451_606_400_000, + genesis_federation_format: 1000, // 2N7ZgQyhFKm17RbaLqygYbS7KLrQfapyZzu (same as testnet) old_federation_address_hash160: [ 0x9d, 0x0f, 0x4e, 0xb1, 0xed, 0xb9, 0xbc, 0xb7, 0x07, 0x8b, 0x36, 0xf4, 0x13, @@ -540,4 +538,32 @@ mod tests { 2_039_594 ); } + + /// A chain from an rskj genesis file runs on rskj's `BridgeRegTestConstants` + /// and `FederationRegTestConstants`, not on rustock's own regtest's. + #[test] + fn rskj_regtest_is_rskjs_regtest() { + let rskj = BridgeConstants::rskj_regtest(); + assert_eq!(rskj.btc2rsk_minimum_acceptable_confirmations, 3); + assert_eq!(rskj.rsk2btc_minimum_acceptable_confirmations, 3); + assert_eq!(rskj.min_seconds_between_calls_receive_header, 300); + assert_eq!(rskj.number_of_blocks_between_pegouts, 50); + assert_eq!(rskj.update_bridge_execution_period, 15_000); + assert_eq!(rskj.btc_height_when_block_index_activates, 10); + assert_eq!(rskj.max_depth_to_search_blocks_below_index_activation, 5); + assert_eq!(rskj.genesis_federation_public_keys.len(), 3); + assert_eq!(rskj.genesis_federation_format, 4000); + assert_eq!(rskj.block_with_too_much_chain_work_height, i32::MAX as u32); + assert_eq!(rskj.erp_fed_activation_delay, 500); + + // rustock's own regtest keeps its own settings and has no federation. + let own = BridgeConstants::regtest(); + assert!(own.genesis_federation_public_keys.is_empty()); + assert_eq!(own.number_of_blocks_between_pegouts, 1); + + for c in [BridgeConstants::mainnet(), BridgeConstants::testnet()] { + assert_eq!(c.genesis_federation_format, 1000); + assert_eq!(c.update_bridge_execution_period, 180_000); + } + } } diff --git a/crates/execution/src/bridge/getters.rs b/crates/execution/src/bridge/getters.rs index 5722c72..f82eacc 100644 --- a/crates/execution/src/bridge/getters.rs +++ b/crates/execution/src/bridge/getters.rs @@ -1464,6 +1464,34 @@ mod tests { } } + /// With nothing stored, the active federation is the genesis federation, + /// built in the format its network's constants name. Mainnet's and testnet's + /// are standard multisig, as rskj's public nodes answer + /// `getFederationAddress` at block 1. rskj's regtest's is a P2SH-P2WSH ERP + /// federation, as an independent port of rskj's `P2shErpRedeemScriptBuilder` + /// computes it (scratch `t294/regtest_genesis_fed.py`); before, rustock built + /// every genesis federation as standard multisig. + #[test] + fn the_genesis_federation_is_built_in_its_networks_format() { + for (config, forks, address) in [ + (mainnet(), hardforks(), "39AHNvUmzaYgewA8yCtBtNsfRz7QD7ZJYi"), + ( + BridgeConstants::testnet(), + RskHardforkConfig::testnet(), + "2N1N5mdHWmRUfv49f7GnUeFfuyLcBQK7hj2", + ), + ( + BridgeConstants::rskj_regtest(), + RskHardforkConfig::regtest(), + "2MvQSG61B4LtKLECeo1dRDeDWXgJfnr3fC7", + ), + ] { + let mut ctx = ctx_at(1); + let out = get_federation_address(&mut ctx, 0, &config, &forks).unwrap(); + assert_eq!(as_string(&out), address); + } + } + // ---- creation time units ---- /// RSKIP419 changes the unit from milliseconds to seconds. Storage keeps diff --git a/crates/execution/src/bridge/governance.rs b/crates/execution/src/bridge/governance.rs index bb13530..6590ec6 100644 --- a/crates/execution/src/bridge/governance.rs +++ b/crates/execution/src/bridge/governance.rs @@ -479,7 +479,13 @@ pub(crate) fn commit_proposed_federation( ), }; let old_keys: Vec<[u8; 33]> = old_members.iter().map(|m| m.btc).collect(); - let old_format = federation_format_version(hardfork_cfg, old_block); + // A stored federation keeps the format its creation block gave it; the + // genesis federation, the one its constants name + // (`FederationSupportImpl.getGenesisFederation`). + let old_format = match &active { + Some(_) => federation_format_version(hardfork_cfg, old_block), + None => config.genesis_federation_format, + }; // moveUTXOsFromNewToOldFederation. let active_utxos = load_federation_utxos(ctx, hardfork_cfg, block_number); @@ -511,9 +517,15 @@ pub(crate) fn commit_proposed_federation( // RSKIP186: saveLastRetiredFederationScript (the retiring = old fed) + // setNextFederationCreationBlockHeight(proposed fed's creation block). if hardfork_cfg.has_rskip186(block_number) { - let old_redeem = super::peg::build_committed_federation_redeem_script( - &old_keys, config, hardfork_cfg, old_block, - ); + let old_redeem = match &active { + Some(_) => super::peg::build_committed_federation_redeem_script( + &old_keys, + config, + hardfork_cfg, + old_block, + ), + None => super::peg::federation_redeem_for_format_pub(&old_keys, old_format, config), + }; let old_hash160 = super::peg::redeem_script_hash160_pub(&old_redeem); // RSKIP377: getDefaultP2SHScript() = getOutputScript(getDefaultRedeemScript()) // — the *default* (standard multisig) redeem, wrapped P2SH-P2WSH for a @@ -592,7 +604,12 @@ fn commit_pending_federation( // 1000 standard multisig, 2000 non-standard ERP, 3000 P2SH-ERP, 4000 // P2SH-P2WSH-ERP. The old fed's type is fixed by its creation block; the // built fed's by this block. - let old_format = federation_format_version(hardfork_cfg, old_block); + // The genesis federation's type is the one its constants name + // (`FederationSupportImpl.getGenesisFederation`). + let old_format = match &active { + Some(_) => federation_format_version(hardfork_cfg, old_block), + None => config.genesis_federation_format, + }; let new_format = federation_format_version(hardfork_cfg, block_number); // Redeem scripts / hash160s: needed for the commit_federation event in BOTH @@ -600,9 +617,15 @@ fn commit_pending_federation( // legacy handover. The retiring fed's redeem is rebuilt as the type it was // CREATED with (keyed on its creation block); the built fed's by THIS // block's activations (rskj PendingFederation.buildFederation). - let old_redeem = super::peg::build_committed_federation_redeem_script( - &old_keys, config, hardfork_cfg, old_block, - ); + let old_redeem = match &active { + Some(_) => super::peg::build_committed_federation_redeem_script( + &old_keys, + config, + hardfork_cfg, + old_block, + ), + None => super::peg::federation_redeem_for_format_pub(&old_keys, old_format, config), + }; let new_redeem = super::peg::build_committed_federation_redeem_script( &new_keys, config, hardfork_cfg, block_number, ); @@ -1584,6 +1607,70 @@ mod tests { assert!(!pending(&mut ctx).is_empty(), "two decide"); } + /// rskj builds the genesis federation in the format its constants name + /// (`FederationSupportImpl.getGenesisFederation`), so the first federation + /// change moves rskj's regtest one into the old slot as P2SH-P2WSH ERP + /// (4000) and retires it under its own P2SH-P2WSH script, whatever block + /// 1's heights say. Under heights where block 1 predates every ERP + /// federation, as mainnet's, the format and script taken from the creation + /// block's heights, as before, were standard multisig's. Both handovers: + /// the legacy one, and RSKIP419's of a proposed federation. + #[test] + fn the_genesis_federation_leaves_in_its_own_format() { + use revm::MainContext; + let config = super::super::constants::BridgeConstants::rskj_regtest(); + let hardforks = crate::hardfork::RskHardforkConfig::mainnet(); + assert!(!hardforks.has_rskip201(1)); + let ctx_at = |block: u64| { + revm::Context::mainnet() + .with_block(revm::context::BlockEnv { + number: alloy_primitives::U256::from(block), + ..Default::default() + }) + .with_chain(crate::raw_storage::RskChainExt::default()) + }; + let members: Vec = super::super::peg::genesis_federation_keys( + &super::super::constants::BridgeConstants::testnet(), + ) + .into_iter() + .map(StoredMember::from_btc) + .collect(); + let old_format = + |ctx: &mut _| bridge_load_bytes_named(ctx, OLD_FEDERATION_FORMAT_VERSION_KEY); + + // hop401: multikey cells (RSKIP123), the legacy handover, and RSKIP186's + // retired script, which before RSKIP377 is the federation's own. + let block = 5_000_000u64; + assert!(hardforks.has_rskip123(block) && hardforks.has_rskip186(block)); + assert!(!hardforks.has_rskip377(block) && !hardforks.has_rskip419(block)); + let mut ctx = ctx_at(block); + commit_pending_federation(&mut ctx, &members, &config, &hardforks); + assert_eq!(old_format(&mut ctx), serialization::rlp_encode_u64(4000)); + // 2MvQSG61B4LtKLECeo1dRDeDWXgJfnr3fC7's script + let regtest_federation: [u8; 20] = hex::decode("22a65d9485781e6cdc0e49fc405ba628365e28c7") + .unwrap() + .try_into() + .unwrap(); + assert_eq!( + bridge_load_bytes_named(&mut ctx, LAST_RETIRED_FEDERATION_P2SH_SCRIPT_KEY), + serialization::rlp_encode_list(&[serialization::rlp_encode_element( + &super::super::peg::p2sh_output_script_program(®test_federation), + )]) + ); + + // lovell700: RSKIP419 promotes the proposed federation. + let block = 7_400_000u64; + assert!(hardforks.has_rskip419(block)); + let mut ctx = ctx_at(block); + bridge_store_bytes_named( + &mut ctx, + PROPOSED_FEDERATION_KEY, + &super::super::federation::serialize_federation_multikey(&members, 1_000, block - 10), + ); + commit_proposed_federation(&mut ctx, &config, &hardforks); + assert_eq!(old_format(&mut ctx), serialization::rlp_encode_u64(4000)); + } + /// A Bridge call that reads the whitelist writes it back as the call /// returns, whatever it answers (upstream #291): `save()` writes whatever /// the call loaded. The changes read it after the sender check, the adds diff --git a/crates/execution/src/bridge/peg.rs b/crates/execution/src/bridge/peg.rs index 2b35ae0..03de5fe 100644 --- a/crates/execution/src/bridge/peg.rs +++ b/crates/execution/src/bridge/peg.rs @@ -4120,9 +4120,12 @@ pub(crate) fn active_federation_keys_and_redeem( } } (Some(n), None) => (n.btc_keys(), super::storage::NEW_FEDERATION_FORMAT_VERSION_KEY), + // rskj `FederationSupportImpl.getGenesisFederation`: the genesis + // federation is built in the format its constants name. (None, _) => return { let keys = genesis_federation_keys(config); - let redeem = build_federation_redeem_script(&keys, keys.len() / 2 + 1); + let redeem = + federation_redeem_for_format(&keys, config.genesis_federation_format, config); (keys, redeem) }, }; @@ -4152,7 +4155,7 @@ pub(crate) fn active_federation_format( } } (Some(_), None) => super::storage::NEW_FEDERATION_FORMAT_VERSION_KEY, - (None, _) => return 1000, // genesis federation: plain multisig + (None, _) => return config.genesis_federation_format, }; federation_format_version(ctx, format_key) } diff --git a/crates/execution/src/executor.rs b/crates/execution/src/executor.rs index 2a9d073..3b15553 100644 --- a/crates/execution/src/executor.rs +++ b/crates/execution/src/executor.rs @@ -2396,14 +2396,17 @@ pub fn bridge_constants_for(chain_id: u64) -> crate::bridge::constants::BridgeCo } /// The senders whose transactions to the Bridge are free before -/// `areBridgeTxsPaid`: the genesis federation and the authorized free-tx -/// keys (rskj `BridgeUtils.isFreeBridgeTx`). Shared by the executor and the -/// transaction pool, which must agree on what costs nothing. +/// `areBridgeTxsPaid` (rskj `BridgeUtils.isFreeBridgeTx`): the genesis +/// federation's members and the federation-change, lock-whitelist and +/// fee-per-KB authorizers (`isFromAuthorizedSender`). Shared by the executor +/// and the transaction pool, which must agree on what costs nothing. pub fn free_bridge_senders(constants: &crate::bridge::constants::BridgeConstants) -> Vec
{ constants .genesis_federation_public_keys .iter() - .chain(constants.authorized_free_tx_keys.iter()) + .chain(constants.federation_change_authorizer_keys) + .chain(constants.whitelist_authorizer_keys) + .chain(constants.fee_per_kb_authorizer_keys) .filter_map(|hex| rsk_address_from_pubkey_hex(hex)) .collect() } @@ -8894,7 +8897,6 @@ mod tests { ] { for keys in [ constants.genesis_federation_public_keys, - constants.authorized_free_tx_keys, constants.whitelist_authorizer_keys, constants.fee_per_kb_authorizer_keys, constants.increase_locking_cap_authorizer_keys, @@ -10057,6 +10059,30 @@ mod tests { assert_eq!(unique.len(), 22); } + /// rskj's regtest, from a genesis file: its three federators and its five + /// authorizers (federation change, lock whitelist, fee per KB) send free + /// Bridge transactions while areBridgeTxsPaid is off + /// (`BridgeUtils.isFreeBridgeTx`). rustock's own regtest has no federators. + #[test] + fn rskj_regtests_free_bridge_senders() { + use crate::bridge::constants::BridgeConstants; + let federator = rsk_address_from_pubkey_hex( + "0362634ab57dae9cb373a5d536e66a8c4f67468bbcfb063809bab643072d78a124", + ) + .unwrap(); + let rskj: std::collections::HashSet<_> = + free_bridge_senders(&BridgeConstants::rskj_regtest()) + .into_iter() + .collect(); + assert_eq!(rskj.len(), 8); + assert!(rskj.contains(&federator)); + let own: std::collections::HashSet<_> = free_bridge_senders(&BridgeConstants::regtest()) + .into_iter() + .collect(); + assert_eq!(own.len(), 5); + assert!(!own.contains(&federator)); + } + /// Runs a gas_limit-0 tx to the Bridge through execute_block at the /// given mainnet height from the given sender. fn run_bridge_tx_gas0(block_number: u64, sender: Address) -> Result { diff --git a/crates/sync/src/txpool.rs b/crates/sync/src/txpool.rs index 06c2f35..63a8bdf 100644 --- a/crates/sync/src/txpool.rs +++ b/crates/sync/src/txpool.rs @@ -80,6 +80,18 @@ impl ChainRules { } } + /// The free senders of `constants`, for a chain whose Bridge constants + /// aren't the ones its id names: rskj's regtest, started from a genesis + /// file, runs on `BridgeConstants::rskj_regtest`, and the pool must agree + /// with the block processor on what costs nothing. + pub fn with_bridge_constants( + mut self, + constants: &rustock_execution::bridge::constants::BridgeConstants, + ) -> Self { + self.free_bridge_senders = rustock_execution::executor::free_bridge_senders(constants); + self + } + /// rskj `Transaction.transactionCost == 0`: a federator's call to the /// Bridge before `areBridgeTxsPaid`. fn is_free_tx(&self, tx: &Transaction, sender: &Address, number: u64) -> bool { @@ -1210,6 +1222,22 @@ mod tests { ChainRules::for_chain(rustock_execution::hardfork::RskHardforkConfig::all_active(33), 6_800_000) } + /// A node on rskj's regtest (an rskj genesis file) gives the pool rskj's + /// regtest Bridge constants, so the pool counts the federators' Bridge + /// calls free as the block processor does; rustock's own regtest has none. + #[test] + fn the_pool_takes_rskj_regtests_free_senders() { + let rskj = rustock_execution::bridge::constants::BridgeConstants::rskj_regtest(); + let expected = rustock_execution::executor::free_bridge_senders(&rskj); + let rules = regtest_rules().with_bridge_constants(&rskj); + assert_eq!(rules.free_bridge_senders, expected); + // rskj's regtest adds its three federators to the five authorizers. + assert_eq!( + regtest_rules().free_bridge_senders.len() + 3, + expected.len() + ); + } + fn sign_tx(tx: &mut Transaction, key: &SigningKey, chain_id: u64) { let hash = tx.signing_hash_eip155(chain_id); let (signature, recid): (k256::ecdsa::Signature, k256::ecdsa::RecoveryId) = key From e5cc8c9a2ed2bf1452d8d89f46d3ac25b9b8966b Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Mon, 9 Nov 2026 22:59:15 -0800 Subject: [PATCH 068/264] execution: getActivePowpegRedeemScript answers the active federation's script RSKj's Bridge answers getActivePowpegRedeemScript() with the active federation's redeem script, ABI-encoded as bytes (FederationSupportImpl.getActiveFederationRedeemScript). RSKIP293 opens the method, and RSKj answers no bytes only before it, so there is always a script: the stored active federation's, in the format it was stored with, or the genesis federation's when none is stored. rustock answered no bytes at all, not even an empty bytes value: at testnet #8,278,738 RSKj's public node answers the active federation's 491-byte P2SH-P2WSH ERP script, and rustock answered 0x. Flyover's peg-in contracts read it in validatePeginDepositAddress, a view function their clients call to check a deposit address, so through a rustock node that check got no script to build the address from. Any contract may also call it in a transaction, at 30,000 gas, where rustock's answer would part its state from RSKj's. The getter now answers through active_federation_keys_and_redeem, the selection and formats getFederationAddress already uses. Tests: RSKj's public nodes' answers on mainnet and testnet, one for each federation format each has had since the method opened (mainnet's standard multisig at hop400, then non-standard ERP, P2SH ERP and P2SH-P2WSH ERP; testnet's non-standard ERP, P2SH ERP and P2SH-P2WSH ERP), each federation stored with its members and the format its creation block gives it; and with nothing stored on RSKj's regtest, the genesis federation's P2SH-P2WSH ERP script. 2 of 2 mutants caught. --- crates/execution/src/bridge/governance.rs | 117 +++++++++++++++++- crates/execution/src/bridge/mod.rs | 4 +- .../testdata/active_powpeg_redeem_scripts.txt | 11 ++ 3 files changed, 126 insertions(+), 6 deletions(-) create mode 100644 crates/execution/src/bridge/testdata/active_powpeg_redeem_scripts.txt diff --git a/crates/execution/src/bridge/governance.rs b/crates/execution/src/bridge/governance.rs index 6590ec6..cb3f1ac 100644 --- a/crates/execution/src/bridge/governance.rs +++ b/crates/execution/src/bridge/governance.rs @@ -27,7 +27,7 @@ //! Serialization matches rskj `BridgeSerializationUtils.serializeOneOffLockWhitelist`: //! RLP_list [ hash160_0, bigint(maxVal_0), ..., bigint(disableBlockHeight) ] -use alloy_primitives::{Bytes, U256}; +use alloy_primitives::U256; use revm::context_interface::ContextTr; use revm::precompile::{PrecompileError, PrecompileOutput}; @@ -1248,13 +1248,24 @@ pub fn set_lock_whitelist_disable_block_delay( // Read-only governance methods // --------------------------------------------------------------------------- -/// `getActivePowpegRedeemScript()` → bytes +/// `getActivePowpegRedeemScript()` → bytes: rskj +/// `FederationSupportImpl.getActiveFederationRedeemScript`, the active +/// federation's redeem script in the format it was stored with, or the genesis +/// federation's with none stored. rskj answers no bytes before RSKIP293, and +/// the method opens with RSKIP293, so there is always a script to answer. pub fn get_active_powpeg_redeem_script( - _ctx: &mut CTX, + ctx: &mut CTX, gas_cost: u64, + config: &super::constants::BridgeConstants, + hardfork_cfg: &crate::hardfork::RskHardforkConfig, ) -> Result { - // Return empty bytes (no federation configured yet) - Ok(PrecompileOutput::new(gas_cost, Bytes::new())) + let block_number = revm::context_interface::Block::number(ctx.block()).to::(); + let (_keys, redeem) = + super::peg::active_federation_keys_and_redeem(ctx, config, hardfork_cfg, block_number); + Ok(PrecompileOutput::new( + gas_cost, + super::getters::abi_encode_bytes(&redeem).into(), + )) } /// `getActiveFederationCreationBlockHeight()` → uint256 @@ -1671,6 +1682,102 @@ mod tests { assert_eq!(old_format(&mut ctx), serialization::rlp_encode_u64(4000)); } + /// rskj answers `getActivePowpegRedeemScript` with the active federation's + /// redeem script (`FederationSupportImpl.getActiveFederationRedeemScript`), + /// where rustock answered no bytes at all. The vectors are RSKj's public + /// nodes' answers, one for each federation format each network has had + /// since the method opened: each federation is stored with the members its + /// script names and the format its creation block gives it, and asked at + /// the block RSKj was asked at. With nothing stored, the genesis federation + /// answers: on RSKj's regtest, the P2SH-P2WSH ERP script an independent + /// port of RSKj's `P2shErpRedeemScriptBuilder` computes. + #[test] + fn the_active_powpeg_redeem_script_is_rskjs() { + use super::super::constants::BridgeConstants; + use crate::hardfork::RskHardforkConfig; + use revm::MainContext; + let ctx_at = |block: u64| { + revm::Context::mainnet() + .with_block(revm::context::BlockEnv { + number: alloy_primitives::U256::from(block), + ..Default::default() + }) + .with_chain(crate::raw_storage::RskChainExt::default()) + }; + let answer = |script: &[u8]| super::super::getters::abi_encode_bytes(script); + + let mut formats = std::collections::BTreeSet::new(); + let vectors = include_str!("testdata/active_powpeg_redeem_scripts.txt"); + for line in vectors + .lines() + .filter(|l| !l.starts_with('#') && !l.trim().is_empty()) + { + let [network, block, creation, script] = + line.split_whitespace().collect::>()[..] + else { + panic!("{line}"); + }; + let (config, forks) = match network { + "mainnet" => (BridgeConstants::mainnet(), RskHardforkConfig::mainnet()), + _ => (BridgeConstants::testnet(), RskHardforkConfig::testnet()), + }; + let (block, creation): (u64, u64) = (block.parse().unwrap(), creation.parse().unwrap()); + let script = hex::decode(script).unwrap(); + // The members, as the script names them: past OP_NOTIF and the threshold. + let mut at = usize::from(script[0] == 0x64) + 1; + let mut members = Vec::new(); + while script[at] == 0x21 { + members.push(StoredMember::from_btc( + script[at + 1..at + 34].try_into().unwrap(), + )); + at += 34; + } + let format = federation_format_version(&forks, creation); + formats.insert((network, format)); + let mut ctx = ctx_at(block); + bridge_store_bytes_named( + &mut ctx, + NEW_FEDERATION_KEY, + &super::super::federation::serialize_federation_multikey(&members, 0, creation), + ); + bridge_store_bytes_named( + &mut ctx, + NEW_FEDERATION_FORMAT_VERSION_KEY, + &serialization::rlp_encode_u64(format), + ); + let out = get_active_powpeg_redeem_script(&mut ctx, 0, &config, &forks).unwrap(); + assert_eq!(out.bytes.to_vec(), answer(&script), "{network} #{block}"); + } + let expected_formats = [ + ("mainnet", 1000), + ("mainnet", 2000), + ("mainnet", 3000), + ("mainnet", 4000), + ("testnet", 2000), + ("testnet", 3000), + ("testnet", 4000), + ]; + assert_eq!(formats.into_iter().collect::>(), expected_formats); + + let regtest_genesis = hex::decode(concat!( + "64522102cd53fc53a07f211641a677d250f6de99caf620e8e77071e811a28b3bcddf0be1210362634ab57dae9cb373a5", + "d536e66a8c4f67468bbcfb063809bab643072d78a1242103c5946b3fbae03a654237da863c9ed534e0878657175b132b", + "8ca630f245df04db53ae6702f401b2755321029cecea902067992d52c38b28bf0bb2345bda9b21eca76b16a17c477a64", + "e433012103284178e5fbcc63c54c3b38e3ef88adf2da6c526313650041b0ef955763634ebd2103ab0e2cd7ed158687fc", + "13b88019990860cdb72b1f5777b58513312550ea1584bc2103b9fc46657cf72a1afa007ecf431de1cd27ff5cc8829fa6", + "25b66ca47b967e6b2454ae68", + )) + .unwrap(); + let out = get_active_powpeg_redeem_script( + &mut ctx_at(1), + 0, + &BridgeConstants::rskj_regtest(), + &RskHardforkConfig::regtest(), + ) + .unwrap(); + assert_eq!(out.bytes.to_vec(), answer(®test_genesis)); + } + /// A Bridge call that reads the whitelist writes it back as the call /// returns, whatever it answers (upstream #291): `save()` writes whatever /// the call loaded. The changes read it after the sender check, the adds diff --git a/crates/execution/src/bridge/mod.rs b/crates/execution/src/bridge/mod.rs index 3988b60..d17453f 100644 --- a/crates/execution/src/bridge/mod.rs +++ b/crates/execution/src/bridge/mod.rs @@ -967,7 +967,9 @@ fn execute_method( } // Transaction-callable read-only - "getActivePowpegRedeemScript" => governance::get_active_powpeg_redeem_script(ctx, gas_cost), + "getActivePowpegRedeemScript" => { + governance::get_active_powpeg_redeem_script(ctx, gas_cost, config, hardfork_cfg) + } "getActiveFederationCreationBlockHeight" => governance::get_active_federation_creation_block_height(ctx, gas_cost), "getNextPegoutCreationBlockNumber" => peg::get_next_pegout_creation_block_number(ctx, gas_cost), "getQueuedPegoutsCount" => peg::get_queued_pegouts_count(ctx, gas_cost), diff --git a/crates/execution/src/bridge/testdata/active_powpeg_redeem_scripts.txt b/crates/execution/src/bridge/testdata/active_powpeg_redeem_scripts.txt new file mode 100644 index 0000000..4261599 --- /dev/null +++ b/crates/execution/src/bridge/testdata/active_powpeg_redeem_scripts.txt @@ -0,0 +1,11 @@ +# RSKj public nodes' answers to the Bridge's getActivePowpegRedeemScript(), read on 10 November 2026, +# one for each federation format on mainnet and testnet. Columns: network, block, +# the active federation's creation block (getFederationCreationBlockNumber at that block), +# and the redeem script RSKj answered. +mainnet 4598500 3137167 57210231a395e332dde8688800a0025cccc5771ea1aa874a633b8ab6e5c89d300c7c3621026b472f7d59d201ff1f540f111b6eb329e071c30a9d23e3d2bcd128fe73dc254c21027319afb15481dbeb3c426bcc37f9a30e7f51ceff586936d85548d9395bcc2344210294c817150f78607566e961b3c71df53a22022a80acbb982f83c0c8baac040adc2103250c11be0561b1d7ae168b1f59e39cbc1fd1ba3cf4d2140c1a365b2723a2bf9321033ada6ef3b1d93a1978b595c7a9e2aa613860b26d4f5a7abb88576aa42b3432ad210357f7ed4c118e581f49cd3b4d9dd1edb4295f4def49d6dcf2faaaaac87a1a0a42210372cd46831f3b6afd4c044d160b7667e8ebf659d6cb51a825a3104df6ee0638c62103ae72827d25030818c4947a800187b1fbcc33ae751e248ae60094cc989fb880f62103b3a7aa25702000c5c1faa300600e8e2bd89cde2be7fb1ec898a39c50d9de90d12103b53899c390573471ba30e5054f78376c5f797fda26dde7a760789f02908cbad22103e05bf6002b62651378b1954820539c36ca405cbb778c225395dd9ebff67802992103ecd8af1e93c57a1b8c7f917bd9980af798adeb0205e9687865673353eb041e8d5dae +mainnet 4848500 4652781 645521020ace50bab1230f8002a0bfe619482af74b338cc9e4c956add228df47e6adae1c210231a395e332dde8688800a0025cccc5771ea1aa874a633b8ab6e5c89d300c7c3621025093f439fb8006fd29ab56605ffec9cdc840d16d2361004e1337a2f86d8bd2db21026b472f7d59d201ff1f540f111b6eb329e071c30a9d23e3d2bcd128fe73dc254c2103250c11be0561b1d7ae168b1f59e39cbc1fd1ba3cf4d2140c1a365b2723a2bf93210357f7ed4c118e581f49cd3b4d9dd1edb4295f4def49d6dcf2faaaaac87a1a0a422103ae72827d25030818c4947a800187b1fbcc33ae751e248ae60094cc989fb880f62103e05bf6002b62651378b1954820539c36ca405cbb778c225395dd9ebff67802992103ecd8af1e93c57a1b8c7f917bd9980af798adeb0205e9687865673353eb041e8d59670350cd00b275532102370a9838e4d15708ad14a104ee5606b36caaaaf739d833e67770ce9fd9b3ec80210257c293086c4d4fe8943deda5f890a37d11bebd140e220faa76258a41d077b4d42103c2660a46aa73078ee6016dee953488566426cf55fc8011edd0085634d75395f92103cd3e383ec6e12719a6c69515e5559bcbe037d0aa24c187e1e26ce932e22ad7b35468ae +mainnet 5098500 4980299 645521020ace50bab1230f8002a0bfe619482af74b338cc9e4c956add228df47e6adae1c21025093f439fb8006fd29ab56605ffec9cdc840d16d2361004e1337a2f86d8bd2db21026b472f7d59d201ff1f540f111b6eb329e071c30a9d23e3d2bcd128fe73dc254c210275d473555de2733c47125f9702b0f870df1d817379f5587f09b6c40ed2c6c9492103250c11be0561b1d7ae168b1f59e39cbc1fd1ba3cf4d2140c1a365b2723a2bf93210357f7ed4c118e581f49cd3b4d9dd1edb4295f4def49d6dcf2faaaaac87a1a0a422103ae72827d25030818c4947a800187b1fbcc33ae751e248ae60094cc989fb880f62103b58a5da144f5abab2e03e414ad044b732300de52fa25c672a7f7b358887719062103e05bf6002b62651378b1954820539c36ca405cbb778c225395dd9ebff678029959ae670350cd00b275532102370a9838e4d15708ad14a104ee5606b36caaaaf739d833e67770ce9fd9b3ec80210257c293086c4d4fe8943deda5f890a37d11bebd140e220faa76258a41d077b4d42103c2660a46aa73078ee6016dee953488566426cf55fc8011edd0085634d75395f92103cd3e383ec6e12719a6c69515e5559bcbe037d0aa24c187e1e26ce932e22ad7b354ae68 +mainnet 8598500 8517961 6455210274c7a2c584eb9dbc3d78fede48d1258b974cd537d82fce4d18f0e85604774e652102daa3cd74c9d06bc7928e1214f0de4ee5744c694975d70e160f562c5e1c4f587d2102f0844d9c1df50f5597118659badf40ea6956903f405f6a604402c8014d926fd42103250c11be0561b1d7ae168b1f59e39cbc1fd1ba3cf4d2140c1a365b2723a2bf93210328f0386fe2b2840a424d3a5073be89a4cb643c434752b8c865e867611d00418321038cc42c8d3cee8360671d2e70226ef95eb840adf4b9c4c5c31b85ffc26eeca7a82103b8faf3ebcf3fe89085fe8c6046dd9cece45bb56e466103f96b4fca2f21072ded2103e59ed1ba1240ac6ff0cb9234afc0e44d6e7653219982d4f1eb55844395e934962103e8f68168606392c5e17f2f9bf1fd3fa55c295cad3990e72e7e3e4470ed644c8f59ae670350cd00b275532102370a9838e4d15708ad14a104ee5606b36caaaaf739d833e67770ce9fd9b3ec80210257c293086c4d4fe8943deda5f890a37d11bebd140e220faa76258a41d077b4d42103c2660a46aa73078ee6016dee953488566426cf55fc8011edd0085634d75395f92103cd3e383ec6e12719a6c69515e5559bcbe037d0aa24c187e1e26ce932e22ad7b354ae68 +testnet 3103000 2787176 6453210208f40073a9e43b3e9103acec79767a6de9b0409749884e989960fee578012fce210225e892391625854128c5c4ea4340de0c2a70570f33db53426fc9c746597a03f421025a2f522aea776fab5241ad72f7f05918e8606676461cb6ce38265a52d4ca9ed62102afc230c2d355b1a577682b07bc2646041b5d0177af0f98395a46018da699b6da210344a3c38cd59afcba3edcebe143e025574594b001700dec41e59409bdbd0f2a0955670350cd00b27552210216c23b2ea8e4f11c3f9e22711addb1d16a93964796913830856b568cc3ea21d3210275562901dd8faae20de0a4166362a4f82188db77dbed4ca887422ea1ec185f1421034db69f2112f4fb1bb6141bf6e2bd6631f0484d0bd95b16767902c9fe219d4a6f5368ae +testnet 3603000 3399807 6453210225e892391625854128c5c4ea4340de0c2a70570f33db53426fc9c746597a03f421025a2f522aea776fab5241ad72f7f05918e8606676461cb6ce38265a52d4ca9ed62102afc230c2d355b1a577682b07bc2646041b5d0177af0f98395a46018da699b6da21032822626c45fc1c4e3a3def5b4983636d6291a7a6677f66874c337e78bc3b77842103fb8e1d5d0392d35ca8c3656acb6193dbf392b3e89b9b7b86693f5c80f7ce858155ae670350cd00b27552210216c23b2ea8e4f11c3f9e22711addb1d16a93964796913830856b568cc3ea21d3210275562901dd8faae20de0a4166362a4f82188db77dbed4ca887422ea1ec185f1421034db69f2112f4fb1bb6141bf6e2bd6631f0484d0bd95b16767902c9fe219d4a6f53ae68 +testnet 8103000 8011267 64562102099fd69cf6a350679a05593c3ff814bfaa281eb6dde505c953cf2875979b1209210222caa9b1436ebf8cdf0c97233a8ca6713ed37b5105bcbbc674fd91353f43d9f7210227e1a5773a58e9be74cd1eaa670150f3ca39bead7cf0fbb6d6d53faca6e6b45c21022a159227df514c7b7808ee182ae07d71770b67eda1e5ee668272761eefb2c24c210292039a22480290feccf059d7be7cb541cba6af97897526befbf3e411dfa9ef8d210297443877c0bc9e1aa572ff0ea7ad5ce396a4cfac48ca0104945de65b3b1e1c372102afc230c2d355b1a577682b07bc2646041b5d0177af0f98395a46018da699b6da2102b1645d3f0cff938e3b3382b93d2d5c082880b86cbb70b6600f5276f235c2839221036a65b700e8dbb4a8c01a022b19b41e8c3501d2e3d3dbaa1953d2e1412e4111c62103857b9b09e19ffe17e39dd0274266f005ebfb99778a467469ea0f37e5d4bb0c482103cac7e78da7ca46910b3326438a9e784e8b1af5a3253fe7bd6cfac914449390155bae670350cd00b27552210216c23b2ea8e4f11c3f9e22711addb1d16a93964796913830856b568cc3ea21d3210275562901dd8faae20de0a4166362a4f82188db77dbed4ca887422ea1ec185f1421034db69f2112f4fb1bb6141bf6e2bd6631f0484d0bd95b16767902c9fe219d4a6f53ae68 From e34e14e0bf422b5cc97b690739431d356e089fbd Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Mon, 9 Nov 2026 23:42:35 -0800 Subject: [PATCH 069/264] execution: rskj's regtest runs on its regtest.conf's schedule A chain started from an rskj genesis file is rskj's regtest, and since eef16e3 it runs on rskj's regtest Bridge constants; rustock's own regtest is another chain (5e0daf7). Its execution ladder was still rustock's own regtest's. That has no reed810, so rustock ran rskj's regtest blocks as one list where rskj runs them as their sublists from block 1 (the fees postponed to REMASC's transaction, each sublist's gas limit, receipts whose gas restarts at each sublist), and the union Bridge's methods (RSKIP502, RSKIP529) stayed closed. And it pays for the federators' and authorizers' Bridge calls from block 0, where regtest.conf leaves afterBridgeSync at -1 and keeps them free. RskHardforkConfig::rskj_regtest is regtest.conf's schedule: every upgrade the ladder models at block 0 but arrowhead631, RSKIP144, RSKIP351 and RSKIP535 at block 1, and afterBridgeSync never. --genesis gives it to the block processor, the miner and the pool. afterBridgeSync is now a height the ladder holds, so --hardfork moves it as rskj's hardforkActivationHeights does, where it was fixed per chain. cardamom1000, the name rskj's master and its regtest.conf give its next upgrade, is taken and changes nothing, as tbd1000, the name perf-lab's seed schedule passes, already was; this node doesn't implement RSKIP378, RSKIP455 or RSKIP559 yet. The pool built its ladder with for_network, apart from the one the block processor runs, so it ignored --hardfork and --consensus-rule: under perf-lab's mainnet seed schedule, whose blocks run as their sublists from #9,002,504, it held a transaction to the whole block's gas, not to the larger sublist's as rskj's pool does. pool_chain_rules builds the pool's checks on hardfork_config's ladder. The --consensus-rule help now names every rule it takes. Tests: every rule the ladder answers for activates on rskj_regtest where regtest.conf puts it, and regtest.conf passed name by name gives the same ladder, rule for rule; afterBridgeSync moves, and -1 keeps the calls free; cardamom1000 and tbd1000 are taken; --genesis runs the schedule, with the header's edges from block 1, and an operator's rule still moves it; the pool checks on the processor's ladder; a free sender's Bridge call stays free on rskj's regtest and is paid on rustock's own; on rskj_regtest the miner splits independent transfers across both parallel sublists and the validator takes the split. rskj's regtest blocks in the import fixture still reach rskj's state roots. 13 of 13 mutants caught. --- crates/cli/src/main.rs | 131 ++++++++++++++---- crates/core/src/config.rs | 12 +- crates/execution/src/hardfork.rs | 199 ++++++++++++++++++++++++--- crates/execution/src/mining/tests.rs | 28 ++++ crates/sync/src/txpool.rs | 31 +++++ 5 files changed, 344 insertions(+), 57 deletions(-) diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index e676618..4cdaaf5 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -1300,6 +1300,8 @@ struct Args { /// Regtest: start from this rskj genesis JSON file instead of the /// built-in genesis, so blocks an rskj node mined from it connect here. + /// The chain is then rskj's regtest: its `regtest.conf` schedule, its + /// Bridge constants and its genesis federation. #[arg(long, value_name = "FILE")] genesis: Option, @@ -1311,7 +1313,8 @@ struct Args { /// Regtest: override an rskj consensus rule, as `name=height` (`-1` /// disables it), like rskj's `blockchain.config.consensusRules`. - /// Repeatable. Supported: rskip97. + /// Repeatable. Supported: rskip97, rskip144, rskip351, rskip535, + /// rskip536 and rskipUMM. #[arg(long, value_name = "NAME=HEIGHT")] consensus_rule: Vec, @@ -1322,11 +1325,13 @@ struct Args { /// Regtest: schedule an rskj network upgrade, as `name=height` (`-1` /// takes it off the ladder), like rskj's /// `blockchain.config.hardforkActivationHeights`. Repeatable. Names are - /// rskj's, orchid to vetiver900. bahamas, afterBridgeSync and hop401 are - /// accepted at the height this node already reads them at; orchid060 and - /// tbd1000 are accepted and change nothing (tbd1000 only turns on - /// RSKIP-559, pegout sorting in the Bridge, which this node does not - /// implement). + /// rskj's: orchid to vetiver900, and afterBridgeSync, where the + /// federators' and authorizers' Bridge transactions start to be paid. + /// bahamas and hop401 are accepted at the height this node already reads + /// them at; orchid060, cardamom1000 and tbd1000 are accepted and change + /// nothing (cardamom1000 turns on RSKIP378, RSKIP455 and RSKIP559 in the + /// Bridge's pegout code, which this node doesn't implement yet; tbd1000 + /// is perf-lab's name for RSKIP559's upgrade). #[arg(long, value_name = "NAME=HEIGHT")] hardfork: Vec, @@ -2471,13 +2476,6 @@ async fn run(local_offset: Option) -> Result<()> { info!("Sender cache disabled (--sigcache=false); every sender is recovered"); } - // The checks that read the chain's activations and constants, as rskj's - // TxPendingValidator does: the RSKIP252 per-transaction price cap, the - // sublist gas limit under RSKIP144, the free Bridge transactions. - // The same ladder `hardfork_cfg` below is built from, needed earlier - // because the pool is constructed before it. - let hardfork_cfg_for_pool = - rustock_execution::RskHardforkConfig::for_network(config.chain_id as u64); let mut pool_builder = rustock_sync::TransactionPool::new( pool_config, config.chain_id.into(), @@ -2485,21 +2483,7 @@ async fn run(local_offset: Option) -> Result<()> { trie_store_for_pool.clone(), ) .with_gas_price_tracker(gas_price_tracker.clone()) - .with_chain_rules({ - let rules = rustock_sync::txpool::ChainRules::for_chain( - hardfork_cfg_for_pool, - config.min_sequential_set_gas_limit, - ); - // A chain from an rskj genesis file is rskj's regtest: its federators' - // and authorizers' Bridge calls are free, as the block processor counts. - if config.genesis_spec.is_some() { - rules.with_bridge_constants( - &rustock_execution::bridge::constants::BridgeConstants::rskj_regtest(), - ) - } else { - rules - } - }); + .with_chain_rules(pool_chain_rules(&config)?); if let Some(cache) = &sender_cache { pool_builder = pool_builder.with_sender_cache(cache.clone()); } @@ -3775,15 +3759,41 @@ fn run_import_blocks( /// The execution ladder for `config`: the chain's own, with the operator's /// `--hardfork` and `--consensus-rule` overrides applied. Also returns the -/// names accepted without effect. +/// names accepted without effect. A chain from an rskj genesis file is +/// rskj's regtest, on its `regtest.conf`'s schedule. fn hardfork_config( config: &ChainConfig, ) -> Result<(rustock_execution::RskHardforkConfig, Vec)> { - rustock_execution::RskHardforkConfig::for_network(config.chain_id as u64) + let ladder = if config.genesis_spec.is_some() { + rustock_execution::RskHardforkConfig::rskj_regtest() + } else { + rustock_execution::RskHardforkConfig::for_network(config.chain_id as u64) + }; + ladder .with_rskj_overrides(&config.hardfork_overrides, &config.consensus_rule_overrides) .map_err(anyhow::Error::msg) } +/// The checks the pool makes that read the chain's activations and +/// constants, as rskj's `TxPendingValidator` does: the RSKIP252 +/// per-transaction price cap, the sublist gas limit under RSKIP144 and the +/// free Bridge transactions. On the ladder the block processor runs, the +/// operator's overrides included. A chain from an rskj genesis file is +/// rskj's regtest, whose federators' and authorizers' Bridge calls are free, +/// as the block processor counts them. +fn pool_chain_rules(config: &ChainConfig) -> Result { + let (ladder, _) = hardfork_config(config)?; + let rules = + rustock_sync::txpool::ChainRules::for_chain(ladder, config.min_sequential_set_gas_limit); + Ok(if config.genesis_spec.is_some() { + rules.with_bridge_constants( + &rustock_execution::bridge::constants::BridgeConstants::rskj_regtest(), + ) + } else { + rules + }) +} + /// `name=height` as rskj's config writes it, `-1` for never. fn parse_rskj_height(flag: &str, spec: &str) -> Result<(String, Option)> { let (name, height) = spec @@ -5355,6 +5365,67 @@ mod genesis_tests { assert_eq!(config.genesis_alloc().len(), 16); } + /// A chain from an rskj genesis file runs on rskj's regtest schedule: + /// its blocks as their sublists from block 1, with the header's edges + /// there too, the union Bridge open, and the free Bridge calls never + /// paid. An operator's `--consensus-rule` still moves a rule, as the + /// import of rskj's older regtest blocks needs. rustock's own regtest is + /// another chain and keeps its own schedule. + #[test] + fn an_rskj_genesis_file_runs_on_rskjs_regtest_schedule() { + let path = concat!( + env!("CARGO_MANIFEST_DIR"), + "/tests/fixtures/rskj-regtest-genesis.json" + ); + let config = + apply_chain_options(ChainConfig::regtest(), ®test_args(&["--genesis", path])) + .unwrap(); + let (ladder, _) = hardfork_config(&config).unwrap(); + assert!(!ladder.runs_sublists(0) && ladder.runs_sublists(1)); + let heights = &config.activation_heights; + assert!(!heights.edges_active(0) && heights.edges_active(1)); + assert!(ladder.has_rskip502(0) && ladder.has_rskip529(0)); + assert!(!ladder.has_are_bridge_txs_paid(1_000_000)); + + let older = regtest_args(&["--genesis", path, "--consensus-rule", "rskip144=-1"]); + let config = apply_chain_options(ChainConfig::regtest(), &older).unwrap(); + let (ladder, _) = hardfork_config(&config).unwrap(); + assert!(ladder.has_rskip351(1) && !ladder.runs_sublists(1)); + + let own = apply_chain_options(ChainConfig::regtest(), ®test_args(&[])).unwrap(); + let (ladder, _) = hardfork_config(&own).unwrap(); + assert!(!ladder.runs_sublists(1) && !ladder.has_rskip502(0)); + assert!(ladder.has_are_bridge_txs_paid(0)); + } + + /// The pool checks a transaction on the ladder the block processor runs, + /// the operator's overrides included, where it read the chain's bare + /// schedule: under perf-lab's seed schedule, whose blocks run as their + /// sublists from #9,002,504, it held a transaction to the whole block's + /// gas, not to the larger sublist's as rskj's pool does. From an rskj + /// genesis file the pool is on rskj's regtest, as the processor is. + #[test] + fn the_pool_checks_on_the_ladder_the_processor_runs() { + let seed = apply_chain_options(ChainConfig::mainnet(), &mainnet_args(SEED_FLAGS)).unwrap(); + let rules = pool_chain_rules(&seed).unwrap(); + assert!(rules.hardfork_cfg.runs_sublists(9_003_580)); + + let path = concat!( + env!("CARGO_MANIFEST_DIR"), + "/tests/fixtures/rskj-regtest-genesis.json" + ); + let rskj = apply_chain_options(ChainConfig::regtest(), ®test_args(&["--genesis", path])) + .unwrap(); + let rules = pool_chain_rules(&rskj).unwrap(); + assert!(rules.hardfork_cfg.runs_sublists(1)); + assert!(!rules.hardfork_cfg.has_are_bridge_txs_paid(1_000_000)); + let constants = rustock_execution::bridge::constants::BridgeConstants::rskj_regtest(); + assert_eq!( + rules.free_bridge_senders, + rustock_execution::executor::free_bridge_senders(&constants) + ); + } + #[test] fn chain_options_apply_on_regtest_only() { let args = regtest_args(&[ diff --git a/crates/core/src/config.rs b/crates/core/src/config.rs index 5e826a4..bbf8f1c 100644 --- a/crates/core/src/config.rs +++ b/crates/core/src/config.rs @@ -252,15 +252,15 @@ impl ActivationHeights { iris300: 0, fingerroot500: 0, hop400: 0, - // regtest.conf: `reed810 = 0`, though rustock's regtest ladder - // does not schedule it yet; RSKIP144 and RSKIP351 have their - // own heights below, so they do not move with it. + // regtest.conf: `reed810 = 0`, which rskj's regtest ladder + // schedules (`--genesis`) and rustock's own doesn't; RSKIP144 and + // RSKIP351 have their own heights below, so they don't move with it. reed810: 0, // regtest.conf: `rskip97 = -1`, same as testnet. rskip97: None, - // regtest.conf: `rskip144 = 1`. rustock's regtest ladder does not - // run blocks as their sublists yet, so its blocks run as one - // list and carry empty edges. + // regtest.conf: `rskip144 = 1`. On rskj's regtest blocks run as + // their sublists from here; rustock's own regtest ladder runs + // none, so its blocks run as one list and carry empty edges. rskip144: Some(1), // regtest.conf: `rskip351 = 1`. rskip351: Some(1), diff --git a/crates/execution/src/hardfork.rs b/crates/execution/src/hardfork.rs index 212f87c..2caec27 100644 --- a/crates/execution/src/hardfork.rs +++ b/crates/execution/src/hardfork.rs @@ -108,8 +108,8 @@ impl ConsensusRule { impl RskNetworkUpgrade { /// The upgrade rskj calls `name` in `hardforkActivationHeights`, for the /// upgrades this ladder models. `bahamas`, `afterBridgeSync`, `orchid060`, - /// `hop401` and `tbd1000` are rskj names too, but not rungs here; see - /// [`RskHardforkConfig::with_rskj_overrides`]. + /// `hop401`, `cardamom1000` and `tbd1000` are rskj names too, but not + /// rungs here; see [`RskHardforkConfig::with_rskj_overrides`]. pub fn from_rskj_name(name: &str) -> Option { Some(match name { "orchid" => Self::Orchid, @@ -136,8 +136,14 @@ pub struct RskHardforkConfig { activations: Vec<(u64, RskNetworkUpgrade)>, pub chain_id: u64, /// Rules scheduled apart from their upgrade (`consensusRules`). `None` - /// is rskj's `-1`: never. Empty on every built-in chain. + /// is rskj's `-1`: never. Empty on every built-in chain but rskj's + /// regtest. rule_overrides: Vec<(ConsensusRule, Option)>, + /// rskj's `afterBridgeSync`, where `areBridgeTxsPaid` activates: before + /// it, the federators' and authorizers' Bridge transactions cost nothing + /// (`BridgeUtils.isFreeBridgeTx`). `None` is rskj's `-1`, as on rskj's + /// regtest, where they stay free. + after_bridge_sync: Option, } impl RskHardforkConfig { @@ -146,6 +152,7 @@ impl RskHardforkConfig { activations: MAINNET_ACTIVATIONS.to_vec(), chain_id: RSK_MAINNET_CHAIN_ID, rule_overrides: Vec::new(), + after_bridge_sync: Self::after_bridge_sync_for(RSK_MAINNET_CHAIN_ID), } } @@ -154,14 +161,44 @@ impl RskHardforkConfig { activations: TESTNET_ACTIVATIONS.to_vec(), chain_id: RSK_TESTNET_CHAIN_ID, rule_overrides: Vec::new(), + after_bridge_sync: Self::after_bridge_sync_for(RSK_TESTNET_CHAIN_ID), } } + /// rustock's own regtest: every upgrade on the ladder from block 0, with + /// RSKIP535 from block 1 and every Bridge transaction paid from block 0. + /// It schedules neither reed810 nor arrowhead631, so its blocks run as + /// one list and the union Bridge's methods stay closed. A chain started + /// from an rskj genesis file is rskj's regtest, [`Self::rskj_regtest`]. pub fn regtest() -> Self { Self { activations: vec![(0, RskNetworkUpgrade::Vetiver900)], chain_id: 33, rule_overrides: Vec::new(), + after_bridge_sync: Self::after_bridge_sync_for(33), + } + } + + /// rskj's regtest, as its `regtest.conf` schedules it: every upgrade on + /// the ladder from block 0 but arrowhead631, which it never schedules; + /// RSKIP144, RSKIP351 and RSKIP535 from block 1, so blocks run as their + /// sublists from there; and `afterBridgeSync` never, so the federators' + /// and authorizers' Bridge transactions stay free. A chain started from + /// an rskj genesis file runs on it. `regtest.conf` has `cardamom1000` at + /// 0 too, which this ladder doesn't model yet. + pub fn rskj_regtest() -> Self { + Self { + activations: vec![ + (0, RskNetworkUpgrade::Reed810), + (0, RskNetworkUpgrade::Vetiver900), + ], + chain_id: 33, + rule_overrides: vec![ + (ConsensusRule::Rskip144, Some(1)), + (ConsensusRule::Rskip351, Some(1)), + (ConsensusRule::Rskip535, Some(1)), + ], + after_bridge_sync: None, } } @@ -191,6 +228,7 @@ impl RskHardforkConfig { ], chain_id, rule_overrides: Vec::new(), + after_bridge_sync: Self::after_bridge_sync_for(chain_id), } } @@ -207,6 +245,7 @@ impl RskHardforkConfig { ], chain_id, rule_overrides: Vec::new(), + after_bridge_sync: Self::after_bridge_sync_for(chain_id), } } @@ -268,11 +307,10 @@ impl RskHardforkConfig { } /// A rung this ladder does not model as such but reads at one height - /// per chain: rskj's `bahamas`, `afterBridgeSync` and `hop401`. + /// per chain: rskj's `bahamas` and `hop401`. fn fixed_rskj_height(&self, name: &str) -> Option { let (mainnet, testnet) = match name { "bahamas" => (3_397, 0), - "afterBridgeSync" => (370_000, 114_000), "hop401" => (4_976_300, 3_362_200), _ => return None, }; @@ -283,6 +321,18 @@ impl RskHardforkConfig { }) } + /// rskj's `afterBridgeSync` on the chain `chain_id` names: `main.conf`'s + /// and `testnet.conf`'s heights, and block 0 elsewhere, as rustock's own + /// regtest has it. rskj's regtest never reaches it + /// ([`Self::rskj_regtest`]). + fn after_bridge_sync_for(chain_id: u64) -> Option { + Some(match chain_id { + RSK_MAINNET_CHAIN_ID => 370_000, + RSK_TESTNET_CHAIN_ID => 114_000, + _ => 0, + }) + } + /// Apply rskj's `hardforkActivationHeights` and `consensusRules`, by /// rskj's names, with `None` for `-1`. For replaying blocks mined on a /// copy of a chain under rules it has not activated. @@ -291,11 +341,16 @@ impl RskHardforkConfig { /// /// - A rung of this ladder (`orchid` to `vetiver900`): scheduled at the /// height, or removed. - /// - `bahamas`, `afterBridgeSync`, `hop401`: read at one fixed height per - /// chain, so they are accepted at that height and refused at any other. - /// - `orchid060` and `tbd1000`: nothing here reads them. `tbd1000` only - /// turns on RSKIP-559 (deterministic pegout sorting in the Bridge), - /// which this node does not implement. Accepted and reported as such. + /// - `afterBridgeSync`: where the federators' and authorizers' Bridge + /// transactions start to be paid, or never. + /// - `bahamas`, `hop401`: read at one fixed height per chain, so they are + /// accepted at that height and refused at any other. + /// - `orchid060`, `cardamom1000` and `tbd1000`: nothing here reads them. + /// `cardamom1000` turns on RSKIP378, RSKIP455 and RSKIP559 in the + /// Bridge's pegout code, which this node doesn't implement yet. + /// `tbd1000` is the name perf-lab's seed schedule passes for RSKIP559's + /// upgrade, which rskj's master calls `cardamom1000`. Accepted and + /// reported as such. /// - `rskip144`, `rskip351`, `rskip535`, `rskip536`: scheduled apart from /// their upgrade. /// - `rskipUMM`: accepted at any height. Only a header's layout reads it, @@ -311,6 +366,8 @@ impl RskHardforkConfig { for (name, height) in hardforks { if let Some(upgrade) = RskNetworkUpgrade::from_rskj_name(name) { self = self.with_hardfork(upgrade, *height); + } else if name == "afterBridgeSync" { + self.after_bridge_sync = *height; } else if let Some(fixed) = self.fixed_rskj_height(name) { if *height != Some(fixed) { return Err(format!( @@ -318,7 +375,7 @@ impl RskHardforkConfig { cannot move it" )); } - } else if matches!(name.as_str(), "orchid060" | "tbd1000") { + } else if matches!(name.as_str(), "orchid060" | "cardamom1000" | "tbd1000") { unmodelled.push(name.clone()); } else { return Err(format!("hardfork {name}: not an rskj upgrade name")); @@ -790,17 +847,12 @@ impl RskHardforkConfig { self.active_upgrade(block_number) >= RskNetworkUpgrade::Wasabi100 } - /// areBridgeTxsPaid (rskj reference.conf: afterBridgeSync — mainnet - /// 370_000, testnet 114_000). Before activation, federation/authorized - /// transactions to the Bridge execute for free with zero gas - /// (BridgeUtils.isFreeBridgeTx). + /// areBridgeTxsPaid (rskj `reference.conf`: `afterBridgeSync`; mainnet + /// 370,000, testnet 114,000, never on rskj's regtest). Before it, the + /// federators' and authorizers' transactions to the Bridge execute for + /// free with zero gas (`BridgeUtils.isFreeBridgeTx`). pub fn has_are_bridge_txs_paid(&self, block_number: u64) -> bool { - let height = match self.chain_id { - RSK_MAINNET_CHAIN_ID => 370_000, - RSK_TESTNET_CHAIN_ID => 114_000, - _ => 0, - }; - block_number >= height + self.after_bridge_sync.is_some_and(|h| block_number >= h) } /// RSKIP144: parallel transaction execution, and the @@ -1461,6 +1513,35 @@ mod override_tests { .unwrap(); assert!(!off.has_rskip144(8_000_000) && !off.runs_sublists(8_000_000)); } + + /// `afterBridgeSync` is a height this ladder reads and moves, as rskj's + /// `hardforkActivationHeights` sets it; at `-1` the free senders' Bridge + /// calls stay free for good, as on rskj's regtest. `cardamom1000`, rskj + /// master's name, and `tbd1000`, perf-lab's, are taken and change + /// nothing: neither is a rung. + #[test] + fn after_bridge_sync_moves_and_cardamom1000_is_taken() { + let mainnet = RskHardforkConfig::mainnet(); + assert!(!mainnet.has_are_bridge_txs_paid(369_999)); + assert!(mainnet.has_are_bridge_txs_paid(370_000)); + let (never, _) = RskHardforkConfig::mainnet() + .with_rskj_overrides(&[h("afterBridgeSync", -1)], &[]) + .unwrap(); + assert!(!never.has_are_bridge_txs_paid(u64::MAX)); + let (paid, _) = RskHardforkConfig::rskj_regtest() + .with_rskj_overrides(&[h("afterBridgeSync", 5)], &[]) + .unwrap(); + assert!(!paid.has_are_bridge_txs_paid(4) && paid.has_are_bridge_txs_paid(5)); + + let (named, unmodelled) = RskHardforkConfig::rskj_regtest() + .with_rskj_overrides(&[h("cardamom1000", 0), h("tbd1000", 0)], &[]) + .unwrap(); + assert_eq!(unmodelled, ["cardamom1000", "tbd1000"]); + assert_eq!( + named.activations, + RskHardforkConfig::rskj_regtest().activations + ); + } } /// Every rule this config answers for by predicate, against the height rskj @@ -1613,4 +1694,80 @@ mod rskj_schedule_tests { wrong.join("\n") ); } + + /// Where rskj's `regtest.conf` puts each rule: every upgrade at block 0 + /// but `afterBridgeSync` and arrowhead631, which it leaves at `-1`, and + /// its `consensusRules` put RSKIP144, RSKIP351 and RSKIP535 at block 1. + /// `reference.conf` maps every rule here to an upgrade, but RSKIP535, + /// which regtest moves. + fn on_rskj_regtest(rule: &str) -> Option { + match rule { + "areBridgeTxsPaid" | "rskip434" => None, + "rskip144" | "rskip351" | "rskip535" => Some(1), + _ => Some(0), + } + } + + #[test] + fn every_rule_activates_where_rskjs_regtest_conf_puts_it() { + let regtest = RskHardforkConfig::rskj_regtest(); + let wrong: Vec = SCHEDULE + .iter() + .filter_map(|&(rule, active, _, _)| { + let rskj = on_rskj_regtest(rule); + let e = check(®test, active, rskj).err()?; + Some(format!("{rule}: {e}; rskj: {rskj:?}")) + }) + .collect(); + assert!( + wrong.is_empty(), + "rules off rskj's regtest schedule:\n{}", + wrong.join("\n") + ); + } + + /// rskj's `regtest.conf`: its `hardforkActivationHeights`, and the + /// `consensusRules` the ladder takes (`rskip97` is the header's alone, + /// and the CLI takes it), with `-1` for never. + #[rustfmt::skip] + const REGTEST_CONF_HARDFORKS: &[(&str, i64)] = &[ + ("bahamas", 0), ("afterBridgeSync", -1), ("orchid", 0), ("orchid060", 0), + ("wasabi100", 0), ("twoToThree", 0), ("papyrus200", 0), ("iris300", 0), + ("hop400", 0), ("hop401", 0), ("fingerroot500", 0), ("arrowhead600", 0), + ("arrowhead631", -1), ("lovell700", 0), ("reed800", 0), ("reed810", 0), + ("vetiver900", 0), ("cardamom1000", 0), + ]; + const REGTEST_CONF_RULES: &[(&str, i64)] = &[ + ("rskipUMM", 1), + ("rskip144", 1), + ("rskip351", 1), + ("rskip535", 1), + ]; + + /// rskj's `regtest.conf`, name by name, on rustock's own regtest ladder + /// is `rskj_regtest`, rule for rule: what a node replaying rskj's + /// regtest blocks would pass by hand, and what `--genesis` gives it. + /// `cardamom1000` is taken and changes nothing. + #[test] + fn rskj_regtest_conf_by_name_is_rskj_regtest() { + let named = |list: &[(&str, i64)]| -> Vec<(String, Option)> { + list.iter() + .map(|&(n, h)| (n.to_string(), u64::try_from(h).ok())) + .collect() + }; + let (by_name, unmodelled) = RskHardforkConfig::regtest() + .with_rskj_overrides(&named(REGTEST_CONF_HARDFORKS), &named(REGTEST_CONF_RULES)) + .unwrap(); + assert_eq!(unmodelled, ["orchid060", "cardamom1000"]); + let built_in = RskHardforkConfig::rskj_regtest(); + for &(rule, active, _, _) in SCHEDULE { + for n in [0, 1, 2, LATER] { + assert_eq!(active(&by_name, n), active(&built_in, n), "{rule} at #{n}"); + } + } + for n in [0, 1, LATER] { + assert_eq!(by_name.runs_sublists(n), built_in.runs_sublists(n), "#{n}"); + assert_eq!(by_name.spec_id(n), built_in.spec_id(n), "#{n}"); + } + } } diff --git a/crates/execution/src/mining/tests.rs b/crates/execution/src/mining/tests.rs index f25f387..436e167 100644 --- a/crates/execution/src/mining/tests.rs +++ b/crates/execution/src/mining/tests.rs @@ -2092,6 +2092,34 @@ mod partitioned_templates { .expect("the block validates"); } + /// rskj's regtest, as a node started from an rskj genesis file runs it, + /// runs its blocks as their sublists from block 1: the miner splits + /// independent transfers across both parallel sublists, and the + /// validator takes the split. + #[test] + fn rskj_regtest_blocks_run_as_their_sublists() { + let ladder = RskHardforkConfig::rskj_regtest(); + let (builder, parent, keys, trie_store, store) = fixture_with_forks(4, ladder.clone()); + let pool = Pool( + keys.iter() + .enumerate() + .map(|(i, k)| transfer(k, Address::repeat_byte(0xB0 + i as u8), 0, 33)) + .collect(), + ); + let template = builder + .build(std::slice::from_ref(&parent), Vec::new(), &pool) + .expect("the template builds"); + let fields = template.block.header.version_fields.as_ref(); + let edges = fields.and_then(|v| v.edges.as_ref()).map(Vec::len); + assert_eq!(edges, Some(2), "both parallel sublists in use"); + + let processor = BlockProcessor::new(ladder, store); + let parent_state = builder.load_state_root(parent.state_root).unwrap(); + processor + .process_block(&template.block, &parent_state, trie_store) + .expect("the validator takes the split"); + } + /// rskj `BlockHeaderBuilder`, `ActivationConfig.getHeaderVersion` and /// `BlockExecutor.fill`: the fields a template carries by its height, /// partitioned or not. RSKIP351 makes it version 1 and RSKIP535 version diff --git a/crates/sync/src/txpool.rs b/crates/sync/src/txpool.rs index 63a8bdf..f428d90 100644 --- a/crates/sync/src/txpool.rs +++ b/crates/sync/src/txpool.rs @@ -2338,6 +2338,37 @@ mod tests { assert!(matches!(err, PoolError::IntrinsicGasTooHigh), "got {err}"); } + /// rskj's regtest never reaches `afterBridgeSync`, so a free sender's + /// Bridge call costs nothing at any height there, as the block processor + /// counts it; on rustock's own regtest the same call is paid from block 0. + /// The head is at #100, past every height regtest moves a rule to. + #[test] + fn on_rskj_regtest_a_free_senders_bridge_call_stays_free() { + let (_, _, addr) = setup_pool_with_rules(U256::ZERO, 0, test_config(), None); + let rules = |ladder| { + let mut rules = ChainRules::for_chain(ladder, 6_800_000); + rules.free_bridge_senders = vec![addr]; + rules + }; + let call = |key: &SigningKey| { + let mut tx = bridge_tx(0, 21_071, &[0x00, 0x01]); + sign_tx(&mut tx, key, 33); + encode_tx(&tx) + }; + let rskj = rules(rustock_execution::hardfork::RskHardforkConfig::rskj_regtest()); + let (pool, key, _) = setup_pool_with_rules(U256::ZERO, 0, test_config(), Some(rskj)); + pool.add_transaction(&call(&key)) + .expect("free on rskj's regtest"); + + let own = rules(rustock_execution::hardfork::RskHardforkConfig::regtest()); + let (pool, key, _) = setup_pool_with_rules(U256::ZERO, 0, test_config(), Some(own)); + let err = pool.add_transaction(&call(&key)).unwrap_err(); + assert!( + matches!(err, PoolError::InsufficientFunds), + "paid on rustock's own: {err}" + ); + } + /// rskj `TxValidatorNonceRangeValidatorTest.illegalAccountSlotsValue_Zero`: /// a pool with no slots would refuse everything, so it is refused itself. #[test] From a1cdd41ef9cd339fd97bc266bf7b6c42cb3d2768 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Tue, 10 Nov 2026 06:57:11 -0800 Subject: [PATCH 070/264] execution: registerBtcTransaction classifies by rskj's pegout index From RSKIP379 rskj indexes each pegout it builds by its first input's sighash, and registerBtcTransaction reads that index for a transaction in a Bitcoin block from btcHeightWhenPegoutTxIndexActivates plus a grace period on (PegUtils.getTransactionType): mainnet 841,909, testnet 2,590,993, regtest 350. Only there does it look for the SVP transactions first. rustock wrote the index but never read it: it classified every transaction by the legacy rules (PegUtilsLegacy), and looked for the SVP transactions at every height. The two part where the index and the legacy rules disagree. A pegout registered after its federation stopped being live is a pegout to the index and a peg-in to the legacy rules. A transaction the index doesn't hold that pays no live federation is left alone, with no mark and no events, where the legacy rules take it for a peg-in of nothing and turn it down. And an SVP transaction in a block below the index's height is classified by the legacy rules. BridgeConstants carries rskj's btcHeightWhenPegoutTxIndexActivates and pegoutTxIndexGracePeriodInBtcBlocks for each network. Signatures are stripped as rskj's BitcoinUtils.removeSignaturesFromMultiSigTransaction strips them, input by input, and for the SVP checks before RSKIP305 as its legacy form, with bitcoinj's redeem-script extraction and signature count ported into rskj_sender_compat beside the parsers they share. What rskj's catch (IllegalArgumentException) takes is no sighash, as a scriptSig asking for more than 16 signatures is, and any other throw fails the call, as it does in rskj. An input whose witness asks for more signatures than OP_CHECKMULTISIG's 20 keys allow can't belong to any pegout the index holds, so its stripped transaction isn't built. Tests: eight signed pegouts rskj released, two with legacy and two with segwit inputs on each of mainnet and testnet, from the index's first weeks and from now, strip to the sighashes whose keys rskj's Bridge storage holds (read from rskj's public nodes, where the control keys hold nothing); a pegout built for a standard, a P2SH-ERP and a P2SH-P2WSH federation strips back to the sighash it was indexed by; each of rskj's answers where it can't strip, and the order it reads the inputs in; the SVP stripping before and after RSKIP305; the index's height on every network; and registrations on a regtest Bitcoin chain at heights 349 and 350 for a late pegout, an unknown transaction and an SVP fund transaction. 25 of 26 mutants caught; the 26th strips for the SVP checks with no hash stored, which changes no answer where RSKIP305 is active with RSKIP419, as on every network. --- crates/execution/src/bridge/constants.rs | 33 + crates/execution/src/bridge/peg.rs | 797 ++++++++++++++++-- .../src/bridge/rskj_sender_compat.rs | 107 ++- .../testdata/pegout_index_sighashes.txt | 14 + 4 files changed, 881 insertions(+), 70 deletions(-) create mode 100644 crates/execution/src/bridge/testdata/pegout_index_sighashes.txt diff --git a/crates/execution/src/bridge/constants.rs b/crates/execution/src/bridge/constants.rs index d36c0d4..b12c692 100644 --- a/crates/execution/src/bridge/constants.rs +++ b/crates/execution/src/bridge/constants.rs @@ -63,6 +63,16 @@ pub struct BridgeConstants { /// Max depth to search blocks below the index activation height. pub max_depth_to_search_blocks_below_index_activation: u32, + // -- Pegout index -- + /// rskj `btcHeightWhenPegoutTxIndexActivates`: the Bitcoin height the + /// pegout index (RSKIP379) was estimated to start at. + pub btc_height_when_pegout_tx_index_activates: u32, + /// rskj `pegoutTxIndexGracePeriodInBtcBlocks`. From RSKIP379, + /// `registerBtcTransaction` classifies a transaction by the index when its + /// block is at least this far past `btc_height_when_pegout_tx_index_activates`, + /// and by the legacy rules below it (`PegUtils.getTransactionType`). + pub pegout_tx_index_grace_period_in_btc_blocks: u32, + /// BTC transaction confirmation max depth. pub btc_transaction_confirmation_max_depth: u32, @@ -188,6 +198,9 @@ impl BridgeConstants { update_bridge_execution_period: 180_000, btc_height_when_block_index_activates: 696_022, max_depth_to_search_blocks_below_index_activation: 4320, + btc_height_when_pegout_tx_index_activates: 837_589, + // THIRTY_DAYS_IN_BTC_BLOCKS + pegout_tx_index_grace_period_in_btc_blocks: 4320, btc_transaction_confirmation_max_depth: 4320, // 5 × MILLICOIN = 5 × 100_000 = 500_000 sat/KB genesis_fee_per_kb: 500_000, @@ -283,6 +296,8 @@ impl BridgeConstants { update_bridge_execution_period: 180_000, btc_height_when_block_index_activates: 2_039_594, max_depth_to_search_blocks_below_index_activation: 4320, + btc_height_when_pegout_tx_index_activates: 2_589_553, + pegout_tx_index_grace_period_in_btc_blocks: 1440, btc_transaction_confirmation_max_depth: 4320, // 1 × MILLICOIN = 100_000 sat/KB genesis_fee_per_kb: 100_000, @@ -397,6 +412,9 @@ impl BridgeConstants { update_bridge_execution_period: 1, btc_height_when_block_index_activates: 0, max_depth_to_search_blocks_below_index_activation: 4320, + // rskj's regtest's, which `rskj_regtest` keeps too. + btc_height_when_pegout_tx_index_activates: 250, + pegout_tx_index_grace_period_in_btc_blocks: 100, btc_transaction_confirmation_max_depth: 4320, genesis_fee_per_kb: 100_000, // rustock's own regtest has no federation; `rskj_regtest` has rskj's. @@ -539,6 +557,19 @@ mod tests { ); } + /// rskj's pegout index starts at Bitcoin height 841,909 on mainnet + /// (837,589 and 30 days) and 2,590,993 on testnet (2,589,553 and 10 days). + #[test] + fn rskj_pegout_tx_index_heights() { + for (c, activates, grace) in [ + (BridgeConstants::mainnet(), 837_589, 4320), + (BridgeConstants::testnet(), 2_589_553, 1440), + ] { + assert_eq!(c.btc_height_when_pegout_tx_index_activates, activates); + assert_eq!(c.pegout_tx_index_grace_period_in_btc_blocks, grace); + } + } + /// A chain from an rskj genesis file runs on rskj's `BridgeRegTestConstants` /// and `FederationRegTestConstants`, not on rustock's own regtest's. #[test] @@ -555,6 +586,8 @@ mod tests { assert_eq!(rskj.genesis_federation_format, 4000); assert_eq!(rskj.block_with_too_much_chain_work_height, i32::MAX as u32); assert_eq!(rskj.erp_fed_activation_delay, 500); + assert_eq!(rskj.btc_height_when_pegout_tx_index_activates, 250); + assert_eq!(rskj.pegout_tx_index_grace_period_in_btc_blocks, 100); // rustock's own regtest keeps its own settings and has no federation. let own = BridgeConstants::regtest(); diff --git a/crates/execution/src/bridge/peg.rs b/crates/execution/src/bridge/peg.rs index 03de5fe..382ec86 100644 --- a/crates/execution/src/bridge/peg.rs +++ b/crates/execution/src/bridge/peg.rs @@ -360,22 +360,39 @@ pub fn register_btc_transaction( // frames down that have no block of their own. crate::silent::set_position(rsk_height, tx_ctx.tx_index); + // rskj `PegUtils.getTransactionType` reads the pegout index from RSKIP379 + // for a transaction in a Bitcoin block at least the index's start height + // high, and there, and only there, looks for the SVP transactions first. + // Below that height the legacy rules classify it. + let pegout_index = pegout_tx_index_enabled(config, hardfork_cfg, rsk_height, btc_block_height); + // RSKIP419: an SVP fund/spend transaction is recognized by its - // signature-stripped txid matching the stored unsigned hash. rskj's - // PegUtils.getTransactionType checks this BEFORE the pegin/pegout - // classification; the matched tx is handled by registerSvpFundTx / - // registerSvpSpendTx (= registerNewUtxos + the SVP state transition) and - // nothing else. - if hardfork_cfg.has_rskip419(rsk_height) { - if let Some(without_sigs) = multisig_txid_without_signatures(&btc_tx) { - let fund_hash = super::serialization::rlp_decode_element(&bridge_load_bytes_named( - ctx, - super::storage::SVP_FUND_TX_HASH_UNSIGNED_KEY, - )); - let spend_hash = super::serialization::rlp_decode_element(&bridge_load_bytes_named( - ctx, - super::storage::SVP_SPEND_TX_HASH_UNSIGNED_KEY, - )); + // signature-stripped txid matching the stored unsigned hash; the matched + // tx is handled by registerSvpFundTx / registerSvpSpendTx (= + // registerNewUtxos + the SVP state transition) and nothing else. rskj + // strips the signatures only when a hash is stored to compare with, and + // what the stripping throws fails the call. + if pegout_index && hardfork_cfg.has_rskip419(rsk_height) { + let fund_hash = super::serialization::rlp_decode_element(&bridge_load_bytes_named( + ctx, + super::storage::SVP_FUND_TX_HASH_UNSIGNED_KEY, + )); + let spend_hash = super::serialization::rlp_decode_element(&bridge_load_bytes_named( + ctx, + super::storage::SVP_SPEND_TX_HASH_UNSIGNED_KEY, + )); + let without_sigs = if fund_hash.is_some() || spend_hash.is_some() { + multisig_tx_hash_without_signatures(&btc_tx, hardfork_cfg.has_rskip305(rsk_height)).map_err( + |why| { + PrecompileError::other(format!( + "registerBtcTransaction: rskj aborts stripping this transaction's signatures ({why})" + )) + }, + )? + } else { + None + }; + if let Some(without_sigs) = without_sigs { let is_fund = fund_hash.as_deref() == Some(&without_sigs[..]); let is_spend = spend_hash.as_deref() == Some(&without_sigs[..]); if is_fund || is_spend { @@ -533,19 +550,54 @@ pub fn register_btc_transaction( }; let is_migration = spends_retired_or_retiring && move_to_active; - if tx_from_old_fed || is_migration || spends_live_fed { + // Peg-in value: the outputs paying any live federation + // (rskj computeTotalAmountSent over the live-federations wallet). + let pays_live_federation = |script: &bitcoin::ScriptBuf| { + *script == fed_script || retiring.as_ref().is_some_and(|(_, rs)| *script == *rs) + }; + let live_output_values: Vec = btc_tx + .output + .iter() + .filter(|o| pays_live_federation(&o.script_pubkey)) + .map(|o| o.value.to_sat()) + .collect(); + let total_value: u64 = live_output_values.iter().sum(); + + // rskj `PegUtils.getTransactionTypeUsingPegoutIndex`: a transaction whose + // first input's sighash the index holds is a pegout or a migration, else one + // paying a live federation is a peg-in, and anything else is left + // unregistered. Below the index's height, `PegUtilsLegacy.getTransactionType`. + let pegout_or_migration = if pegout_index { + let sig_hash = sig_hash_for_pegout_index(&btc_tx).map_err(|why| { + PrecompileError::other(format!( + "registerBtcTransaction: rskj aborts stripping this transaction's signatures ({why})" + )) + })?; + if sig_hash.is_some_and(|hash| has_pegout_tx_sig_hash(ctx, &hash)) { + Some("PEGOUT_OR_MIGRATION") + } else if live_output_values.is_empty() { + tracing::debug!("registerBtcTransaction: unknown to the pegout index and pays no live federation; not registered"); + return Ok(PrecompileOutput::new(gas_cost, Bytes::new())); + } else { + None + } + } else if tx_from_old_fed { + Some("FROM_OLD_FEDERATION") + } else if is_migration { + Some("MIGRATION") + } else if spends_live_fed { + Some("PEGOUT") + } else { + None + }; + + if let Some(kind) = pegout_or_migration { record_pegin( SilentKind::BtcTxMarkedProcessed, &btc_tx, tx_ctx, active_output_values.iter().sum(), - if tx_from_old_fed { - "FROM_OLD_FEDERATION" - } else if is_migration { - "MIGRATION" - } else { - "PEGOUT" - }, + kind, vec![], ); let active_utxo_key = active_federation_utxo_key(ctx, config, hardfork_cfg, rsk_height); @@ -562,19 +614,6 @@ pub fn register_btc_transaction( return Ok(PrecompileOutput::new(gas_cost, Bytes::new())); } - // Peg-in value: the outputs paying any live federation - // (rskj computeTotalAmountSent over the live-federations wallet). - let pays_live_federation = |script: &bitcoin::ScriptBuf| { - *script == fed_script || retiring.as_ref().is_some_and(|(_, rs)| *script == *rs) - }; - let live_output_values: Vec = btc_tx - .output - .iter() - .filter(|o| pays_live_federation(&o.script_pubkey)) - .map(|o| o.value.to_sat()) - .collect(); - let total_value: u64 = live_output_values.iter().sum(); - // rskj PegUtilsLegacy.isValidPegInTx minimum gate: under RSKIP293 it checks // EACH UTXO (isAnyUTXOAmountBelowMinimum), otherwise the total amount sent // (valueSentToMe.isLessThan). Crucially, an EMPTY live-output set is not @@ -2004,17 +2043,49 @@ fn mark_flyover_derivation_hash_used( super::storage::bridge_store_raw(ctx, key, Some(vec![1])); } +/// rskj `BridgeStorageIndexKey.PEGOUT_TX_SIG_HASH` for one sighash. The key's +/// identifier is the sighash's `Sha256Hash.toString()`, which for a +/// `hashForSignature` result is bitcoinj `Sha256Hash.twiceOf` = INTERNAL byte +/// order — NOT reversed, unlike a txid's `getHash().toString()` +/// (`wrapReversed`). So the hex is taken directly, not via +/// `btc_hash_hex_display`. +fn pegout_tx_sig_hash_key(sig_hash: &[u8; 32]) -> U256 { + compound_key( + super::storage::PEGOUT_TX_SIG_HASH_KEY, + "-", + &to_hex(sig_hash), + ) +} + /// rskj `BridgeStorageProvider.setPegoutTxSigHash`/`savePegoutTxSigHashes` /// (RSKIP379): index a created pegout by the legacy SIGHASH_ALL of its first /// input (`BitcoinUtils.getSigHashForPegoutIndex` → `hashForSignature(0, -/// redeemScript, ALL, false)`). The key identifier is the sigHash's -/// `Sha256Hash.toString()`, which for a `hashForSignature` result is bitcoinj -/// `Sha256Hash.twiceOf` = INTERNAL byte order — NOT reversed, unlike a txid's -/// `getHash().toString()` (`wrapReversed`). So the hex is taken directly, not -/// via `btc_hash_hex_display`. Value is a single TRUE byte (`new byte[]{1}`). +/// redeemScript, ALL, false)`). Value is a single TRUE byte (`new byte[]{1}`). fn save_pegout_tx_sig_hash(ctx: &mut CTX, sig_hash: &[u8; 32]) { - let key = compound_key(super::storage::PEGOUT_TX_SIG_HASH_KEY, "-", &to_hex(sig_hash)); - super::storage::bridge_store_raw(ctx, key, Some(vec![1])); + super::storage::bridge_store_raw(ctx, pegout_tx_sig_hash_key(sig_hash), Some(vec![1])); +} + +/// rskj `BridgeStorageProvider.hasPegoutTxSigHash`: the index holds a sighash +/// when its key holds the single TRUE byte. Read only from RSKIP379, where +/// `pegout_tx_index_enabled` sends the classification. +fn has_pegout_tx_sig_hash(ctx: &mut CTX, sig_hash: &[u8; 32]) -> bool { + super::storage::bridge_load_raw(ctx, pegout_tx_sig_hash_key(sig_hash)) + .is_some_and(|data| data.len() == 1 && data[0] == 1) +} + +/// rskj `PegUtils.isPegoutTxIndexEnabled`: from RSKIP379, a transaction in a +/// Bitcoin block at least `btc_height_when_pegout_tx_index_activates` plus +/// `pegout_tx_index_grace_period_in_btc_blocks` high is classified by the +/// pegout index, and one below it by the legacy rules. +fn pegout_tx_index_enabled( + config: &BridgeConstants, + hardfork_cfg: &RskHardforkConfig, + rsk_height: u64, + btc_height: i32, +) -> bool { + let start = i64::from(config.btc_height_when_pegout_tx_index_activates) + + i64::from(config.pegout_tx_index_grace_period_in_btc_blocks); + hardfork_cfg.has_rskip379(rsk_height) && i64::from(btc_height) >= start } /// rskj `BitcoinUtils.getSigHashForPegoutIndex`: index a created pegout by the @@ -3528,32 +3599,107 @@ fn svp_is_ongoing( .is_some_and(|p| block_number < p.creation_block + config.validation_period_duration_in_blocks) } -/// rskj `BitcoinUtils.getMultiSigTransactionHashWithoutSignatures`: the txid a -/// signed multisig tx would have with its signatures stripped (each input's -/// scriptSig rebuilt as the redeem placeholder). Returns the display-order hash, -/// or `None` if any input is not a standard P2SH multisig (rskj throws → not an -/// SVP tx). Witness txs use the plain txid. -fn multisig_txid_without_signatures(tx: &BtcTransaction) -> Option<[u8; 32]> { - // Witness txs use the plain txid (rskj getMultiSigTransactionHashWithoutSignatures). - if tx.input.iter().any(|i| !i.witness.is_empty()) { - return Some(btc_txid_event_bytes(tx)); - } +/// The most signatures a federation's input can ask for: `OP_CHECKMULTISIG` +/// takes at most 20 keys. Only the federation's own pegouts are in the index, +/// and only its own SVP transactions are stored unsigned, so an input asking +/// for more can't belong to any of them, and its stripped transaction isn't +/// built. +const MAX_FEDERATION_SIGNATURES: i64 = 20; + +/// rskj `BitcoinUtils.removeSignaturesFromMultiSigTransaction`, run on a copy +/// (`getMultiSigTransactionWithoutSignatures`), or before RSKIP305 its +/// `BitcoinUtilsLegacy` form, which skips the multisig check. Every input's +/// redeem script is extracted and its scriptSig, or its witness and +/// scriptSig, rebuilt with empty signatures, input by input. +/// +/// `Ok(None)` is the `IllegalArgumentException` the callers catch: an input +/// with no redeem script, from RSKIP305 one that isn't a multisig, a scriptSig +/// asking for more than 16 signatures (`createMultiSigInputScriptBytes`), or a +/// witness sized past Java's `int` (`createBaseWitnessThatSpendsFromErpRedeemScript`). +/// It is also the answer when an input asks for more signatures than a +/// federation can, whose stripped transaction couldn't match. `Err` is an +/// exception nothing between it and `Bridge.execute` catches. +fn without_multisig_signatures( + tx: &BtcTransaction, + before_rskip305: bool, +) -> Result, &'static str> { + use super::rskj_sender_compat::{self as bitcoinj, Verdict}; let mut copy = tx.clone(); + let mut matchable = true; for input in &mut copy.input { - // Each input must be a P2SH multisig (standard or ERP): extract its - // redeem and rebuild the placeholder scriptSig. `placeholder_scriptsig` - // adds the extra OP_0 for the ERP OP_NOTIF branch, matching bitcoinj - // `createEmptyInputScript`. A non-multisig input (no redeem push, or - // threshold 0) is not an SVP tx (rskj throws → caught → false). - let redeem = super::release_tx::extract_redeem_script(input.script_sig.as_bytes())?; - let threshold = super::release_tx::redeem_script_threshold(&redeem); - if threshold == 0 { - return None; + let Some(redeem) = + bitcoinj::redeem_script_of_input(input.script_sig.as_bytes(), input.witness.last())? + else { + return Ok(None); + }; + if !before_rskip305 { + match bitcoinj::is_sent_to_multisig(&redeem) { + Verdict::Multisig => {} + Verdict::NotMultisig => return Ok(None), + Verdict::RskjThrows(why) => return Err(why), + } } - input.script_sig = - bitcoin::ScriptBuf::from_bytes(super::release_tx::placeholder_scriptsig(&redeem, threshold)); + let signatures = bitcoinj::signatures_required_to_spend(&redeem)?; + if input.witness.is_empty() { + if signatures > 16 { + return Ok(None); + } + input.script_sig = bitcoin::ScriptBuf::from_bytes( + super::release_tx::placeholder_scriptsig(&redeem, signatures as usize), + ); + } else { + // Its pushes: the CHECKMULTISIG dummy, the signatures, the OP_NOTIF + // flag and the redeem script, counted in Java's int. + if signatures + 3 > i64::from(i32::MAX) { + return Ok(None); + } + if signatures > MAX_FEDERATION_SIGNATURES { + matchable = false; + continue; + } + let (script_sig, witness) = + super::release_tx::segwit_base_input(&redeem, signatures as usize); + input.script_sig = script_sig; + input.witness = witness; + } + } + Ok(matchable.then_some(copy)) +} + +/// rskj `BitcoinUtils.getMultiSigTransactionHashWithoutSignatures`, or before +/// RSKIP305 `BitcoinUtilsLegacy.getMultiSigTransactionHashWithoutSignaturesBeforeRSKIP305`: +/// a segwit transaction's own txid, else the txid of its copy with every +/// input's signatures stripped (display order, as the SVP hashes are stored). +/// `Ok(None)` and `Err` are `without_multisig_signatures`'s. +fn multisig_tx_hash_without_signatures( + tx: &BtcTransaction, + rskip305: bool, +) -> Result, &'static str> { + if tx.input.iter().any(|i| !i.witness.is_empty()) { + return Ok(Some(btc_txid_event_bytes(tx))); } - Some(btc_txid_event_bytes(©)) + Ok(without_multisig_signatures(tx, !rskip305)?.map(|stripped| btc_txid_event_bytes(&stripped))) +} + +/// rskj `BitcoinUtils.getSigHashForPegoutIndex` for a registered transaction: +/// the sighash of its first input over its copy with every input's signatures +/// stripped, which is the sighash the pegout's unsigned transaction was +/// indexed by when it was created. `Ok(None)` where rskj answers empty, and +/// `Err` where it throws past its `catch (IllegalArgumentException)`. +fn sig_hash_for_pegout_index(tx: &BtcTransaction) -> Result, &'static str> { + use super::rskj_sender_compat as bitcoinj; + let Some(first) = tx.input.first() else { + return Ok(None); + }; + let Some(redeem0) = + bitcoinj::redeem_script_of_input(first.script_sig.as_bytes(), first.witness.last())? + else { + return Ok(None); + }; + let Some(stripped) = without_multisig_signatures(tx, false)? else { + return Ok(None); + }; + Ok(Some(first_input_sig_hash(&stripped, &redeem0, 0))) } /// rskj `ReleaseTransactionBuilder.buildSvpFundTransaction`: two fixed outputs @@ -7459,7 +7605,9 @@ mod tests { let _ = undetermined_input_shape(tx); let _ = super::super::pegin_instructions::build_pegin_instructions(tx); let _ = btc_txid_event_bytes(tx); - let _ = multisig_txid_without_signatures(tx); + let _ = multisig_tx_hash_without_signatures(tx, true); + let _ = multisig_tx_hash_without_signatures(tx, false); + let _ = sig_hash_for_pegout_index(tx); let _ = release_tx::has_enough_signatures(tx); let raw = bitcoin::consensus::serialize(tx); let _ = has_inputs(&raw, true); @@ -7482,6 +7630,8 @@ mod tests { let _ = is_btc_public_key(script); let _ = compress_pubkey(script); let _ = super::super::rskj_sender_compat::is_sent_to_multisig(script); + let _ = super::super::rskj_sender_compat::signatures_required_to_spend(script); + let _ = super::super::rskj_sender_compat::redeem_script_of_input(script, None); if let Some(redeem) = release_tx::extract_redeem_script(script) { let _ = super::super::rskj_sender_compat::is_sent_to_multisig(&redeem); let _ = release_tx::is_erp_redeem(&redeem); @@ -8335,4 +8485,519 @@ mod tests { Bytes::new() ); } + + // --- the pegout index (RSKIP379) --------------------------------------- + + /// A script pushing each item in turn. + fn pushes(items: &[&[u8]]) -> bitcoin::ScriptBuf { + let mut builder = bitcoin::script::Builder::new(); + for item in items { + builder = builder.push_slice(<&bitcoin::script::PushBytes>::try_from(*item).unwrap()); + } + builder.into_script() + } + + /// Three keys, sorted as given. + fn test_keys(first: u8) -> Vec<[u8; 33]> { + (0..3u8) + .map(|i| { + let mut key = [first + i; 33]; + key[0] = 0x02; + key + }) + .collect() + } + + /// A transaction spending one outpoint per input to `outputs`. + fn spend( + inputs: Vec<(bitcoin::ScriptBuf, bitcoin::Witness)>, + outputs: &[(u64, bitcoin::ScriptBuf)], + ) -> BtcTransaction { + BtcTransaction { + version: bitcoin::transaction::Version::TWO, + lock_time: bitcoin::absolute::LockTime::ZERO, + input: inputs + .into_iter() + .zip(0u32..) + .map(|((script_sig, witness), vout)| bitcoin::TxIn { + previous_output: bitcoin::OutPoint { + txid: bitcoin::Txid::from_byte_array([0x5a; 32]), + vout, + }, + script_sig, + sequence: bitcoin::Sequence::MAX, + witness, + }) + .collect(), + output: outputs + .iter() + .map(|(value, script_pubkey)| bitcoin::TxOut { + value: bitcoin::Amount::from_sat(*value), + script_pubkey: script_pubkey.clone(), + }) + .collect(), + } + } + + /// An input with `script_sig` and no witness. + fn legacy(script_sig: Vec) -> (bitcoin::ScriptBuf, bitcoin::Witness) { + ( + bitcoin::ScriptBuf::from_bytes(script_sig), + bitcoin::Witness::new(), + ) + } + + /// An input spending `redeem`, a 2-of-n multisig, signed in its scriptSig. + fn signed_multisig(redeem: &[u8]) -> (bitcoin::ScriptBuf, bitcoin::Witness) { + ( + pushes(&[&[], &[0x30; 71], &[0x31; 72], redeem]), + bitcoin::Witness::new(), + ) + } + + /// A one-key multisig asking for the signatures `m` pushes, which bitcoinj + /// takes as a multisig whatever the count. + fn asking(m: &[u8]) -> Vec { + let mut script = vec![m.len() as u8]; + script.extend_from_slice(m); + script.push(0x21); + script.extend_from_slice(&[0x02; 33]); + script.extend_from_slice(&[0x51, 0xae]); + script + } + + /// RSKj indexes a pegout as it builds it, by its first input's sighash over + /// the unsigned transaction, and finds it when the signed one is registered, + /// by stripping the signatures again. The two meet for a standard, a + /// P2SH-ERP and a P2SH-P2WSH federation. + #[test] + fn a_signed_pegout_strips_to_the_sighash_it_was_indexed_by() { + use super::super::release_tx::{is_erp_redeem, placeholder_scriptsig, segwit_base_input}; + let keys = test_keys(0x10); + let standard = build_federation_redeem_script(&keys, 2); + let erp = build_erp_redeem_script(&keys, 2, &test_keys(0x40), 2, 52_560, true); + let outputs = [(40_000, p2sh_output_script(&[0x77; 20]))]; + for redeem in [&standard, &erp] { + let unsigned = spend(vec![legacy(placeholder_scriptsig(redeem, 2)); 2], &outputs); + let indexed = first_input_sig_hash(&unsigned, redeem, 0); + // OP_0, the signatures, and for an ERP script its default branch's OP_0. + let mut items: Vec<&[u8]> = vec![&[], &[0x30; 71], &[0x31; 72]]; + if is_erp_redeem(redeem) { + items.push(&[]); + } + items.push(redeem); + let signed = spend(vec![(pushes(&items), bitcoin::Witness::new()); 2], &outputs); + assert_eq!(sig_hash_for_pegout_index(&signed), Ok(Some(indexed))); + } + let (script_sig, base) = segwit_base_input(&erp, 2); + let unsigned = spend(vec![(script_sig.clone(), base); 2], &outputs); + let indexed = first_input_sig_hash(&unsigned, &erp, 0); + let witness = bitcoin::Witness::from_slice(&[ + vec![], + vec![0x30; 71], + vec![0x31; 72], + vec![], + erp.clone(), + ]); + let signed = spend(vec![(script_sig, witness); 2], &outputs); + // The witnesses are in what this sighash covers, so only stripping finds it. + assert_ne!(first_input_sig_hash(&signed, &erp, 0), indexed); + assert_eq!(sig_hash_for_pegout_index(&signed), Ok(Some(indexed))); + } + + /// Where RSKj can't strip a registered transaction's signatures: what its + /// `catch (IllegalArgumentException)` takes is no sighash, and anything else + /// fails the call. The inputs are read in order, and the first throw ends + /// the reading. + #[test] + fn the_pegout_index_reads_a_transaction_it_cannot_strip_as_rskj_does() { + let redeem = build_federation_redeem_script(&test_keys(0x10), 2); + let outputs = [(40_000, p2sh_output_script(&[0x77; 20]))]; + let index = |inputs| sig_hash_for_pegout_index(&spend(inputs, &outputs)); + let pushing = |items: &[&[u8]]| legacy(pushes(items).into_bytes()); + let witness = |m: &[u8]| { + ( + bitcoin::ScriptBuf::new(), + bitcoin::Witness::from_slice(&[vec![], asking(m)]), + ) + }; + let not_a_script = || legacy(vec![0x4c]); + // Nothing to read in the first input: no inputs, an empty scriptSig, one + // ending in an opcode, or one ending in a push that isn't a script. + assert_eq!(index(vec![]), Ok(None)); + assert_eq!(index(vec![legacy(vec![])]), Ok(None)); + assert_eq!(index(vec![legacy(vec![0x51])]), Ok(None)); + assert_eq!(index(vec![legacy(vec![0x01, 0x4c])]), Ok(None)); + // A first scriptSig that isn't a script throws before RSKj's catch. + assert!(index(vec![not_a_script()]).is_err()); + // A later input with no redeem script, or one that isn't a multisig, + // is caught. One that isn't a script, or has OP_0 for its M, throws + // something else. + let multisig = || signed_multisig(&redeem); + assert_eq!(index(vec![multisig(), legacy(vec![])]), Ok(None)); + assert_eq!( + index(vec![multisig(), pushing(&[&[0x30; 71], &[0x02; 33]])]), + Ok(None) + ); + assert!(index(vec![multisig(), not_a_script()]).is_err()); + assert!(index(vec![ + multisig(), + pushing(&[&[0x00, 0x01, 0xaa, 0x01, 0xbb, 0xae]]) + ]) + .is_err()); + // A scriptSig takes at most 16 empty signatures, checked once they're + // made, and that throw is caught and ends the reading. + assert!(matches!( + index(vec![multisig(), pushing(&[&asking(&[16])])]), + Ok(Some(_)) + )); + assert_eq!( + index(vec![multisig(), pushing(&[&asking(&[17])]), not_a_script()]), + Ok(None) + ); + assert!(index(vec![multisig(), pushing(&[&asking(&[16])]), not_a_script()]).is_err()); + // A witness's pushes are counted in Java's int: past it the list can't + // be made, which is caught; below it the inputs after are still read. + assert!(matches!( + index(vec![multisig(), witness(&[20])]), + Ok(Some(_)) + )); + assert_eq!(index(vec![multisig(), witness(&[21])]), Ok(None)); + assert_eq!( + index(vec![ + multisig(), + witness(&[0xfd, 0xff, 0xff, 0x7f]), + not_a_script() + ]), + Ok(None) + ); + assert!(index(vec![ + multisig(), + witness(&[0xfc, 0xff, 0xff, 0x7f]), + not_a_script() + ]) + .is_err()); + } + + /// The SVP checks strip as RSKj's `getMultiSigTransactionHashWithoutSignatures`, + /// and before RSKIP305 as its legacy form, which doesn't ask whether an + /// input is a multisig. A segwit transaction is its own hash. + #[test] + fn svp_hashes_strip_as_rskj_before_and_after_rskip305() { + use super::super::release_tx::{placeholder_scriptsig, segwit_base_input}; + let redeem = build_federation_redeem_script(&test_keys(0x10), 2); + let outputs = [(40_000, p2sh_output_script(&[0x77; 20]))]; + let to_address = [&[0x76, 0xa9, 0x14][..], &[0x33; 20][..], &[0x88, 0xac][..]].concat(); + let tx = spend( + vec![ + signed_multisig(&redeem), + legacy(pushes(&[&[0x30; 71], &to_address]).into_bytes()), + ], + &outputs, + ); + assert_eq!(multisig_tx_hash_without_signatures(&tx, true), Ok(None)); + // Before RSKIP305 a pay-to-address script asks for one signature. + let stripped = spend( + vec![ + legacy(placeholder_scriptsig(&redeem, 2)), + legacy(placeholder_scriptsig(&to_address, 1)), + ], + &outputs, + ); + assert_eq!( + multisig_tx_hash_without_signatures(&tx, false), + Ok(Some(btc_txid_event_bytes(&stripped))) + ); + // A pay-to-pubkey script asks for one too. + let to_key = [&[0x21][..], &[0x02; 33][..], &[0xac][..]].concat(); + let tx = spend( + vec![ + signed_multisig(&redeem), + legacy(pushes(&[&[0x30; 71], &to_key]).into_bytes()), + ], + &outputs, + ); + let stripped = spend( + vec![ + legacy(placeholder_scriptsig(&redeem, 2)), + legacy(placeholder_scriptsig(&to_key, 1)), + ], + &outputs, + ); + assert_eq!( + multisig_tx_hash_without_signatures(&tx, false), + Ok(Some(btc_txid_event_bytes(&stripped))) + ); + assert_eq!(multisig_tx_hash_without_signatures(&tx, true), Ok(None)); + // A pay-to-script-hash script's count bitcoinj can't tell, and throws. + let to_script = p2sh_output_script(&[0x44; 20]).into_bytes(); + let tx = spend( + vec![ + signed_multisig(&redeem), + legacy(pushes(&[&to_script]).into_bytes()), + ], + &outputs, + ); + assert!(multisig_tx_hash_without_signatures(&tx, false).is_err()); + assert_eq!(multisig_tx_hash_without_signatures(&tx, true), Ok(None)); + // Nothing in a segwit transaction is stripped. + let segwit = spend( + vec![segwit_base_input(&redeem, 2), legacy(vec![0x4c])], + &outputs, + ); + assert_eq!( + multisig_tx_hash_without_signatures(&segwit, true), + Ok(Some(btc_txid_event_bytes(&segwit))) + ); + } + + /// RSKj reads the index from RSKIP379 on, for Bitcoin blocks from the index's + /// activation height plus its grace period. + #[test] + fn the_pegout_index_is_read_from_its_bitcoin_height_once_rskip379_is_active() { + let networks = [ + ( + BridgeConstants::rskj_regtest(), + RskHardforkConfig::rskj_regtest(), + 350, + ), + ( + BridgeConstants::mainnet(), + RskHardforkConfig::mainnet(), + 841_909, + ), + ( + BridgeConstants::testnet(), + RskHardforkConfig::testnet(), + 2_590_993, + ), + ]; + for (config, cfg, start) in &networks { + // Past every upgrade of the three. + let rsk_height = 10_000_000; + assert!(!pegout_tx_index_enabled(config, cfg, rsk_height, start - 1)); + assert!(pegout_tx_index_enabled(config, cfg, rsk_height, *start)); + } + // Before RSKIP379 the legacy rules read every height. + let (config, cfg, start) = &networks[1]; + assert!(!pegout_tx_index_enabled(config, cfg, 0, *start)); + } + + /// RSK block 10 on RSKj's regtest, whose Bridge holds a Bitcoin chain to + /// height 352 with `tx` the only transaction of blocks 349 and 350: one + /// below the height RSKj's regtest reads the pegout index from, and one at + /// it. + fn regtest_bridge_with(tx: &BtcTransaction) -> impl crate::RskContextTr { + use revm::MainContext; + let block = revm::context::BlockEnv { + number: U256::from(10u64), + timestamp: U256::from(1_700_000_000u64), + ..revm::context::BlockEnv::default() + }; + let mut ctx = revm::Context::mainnet() + .with_block(block) + .with_chain(crate::raw_storage::RskChainExt::default()); + let root = calculate_btc_tx_hash(&btc_serialize(tx)); + let mut prev = bitcoin::constants::genesis_block(bitcoin::Network::Regtest) + .header + .block_hash(); + let headers: Vec> = (1..=352u32) + .map(|height| { + let merkle = if height == 349 || height == 350 { + root + } else { + [0x11; 32] + }; + let header = (0..) + .map(|nonce| bitcoin::block::Header { + version: bitcoin::block::Version::from_consensus(1), + prev_blockhash: prev, + merkle_root: bitcoin::TxMerkleNode::from_byte_array(merkle), + time: 1_700_000_000, + bits: bitcoin::CompactTarget::from_consensus(0x207f_ffff), + nonce, + }) + .find(|header| { + super::super::btc_rules::verify_header( + header, + super::super::constants::BtcNetwork::Regtest, + ) + }) + .unwrap(); + prev = header.block_hash(); + btc_serialize(&header) + }) + .collect(); + let slices: Vec<&[u8]> = headers.iter().map(Vec::as_slice).collect(); + let args = encode::args(&[Arg::BytesArray(&slices)]); + super::super::btc_chain::receive_headers( + &mut ctx, + &args, + 0, + &BridgeConstants::rskj_regtest(), + true, + &RskHardforkConfig::rskj_regtest(), + ) + .unwrap(); + ctx + } + + /// `registerBtcTransaction` of `tx` at Bitcoin `height` on RSKj's regtest, + /// with `pegout_sig_hash` in the pegout index and `svp_fund_hash` as the + /// unsigned SVP fund transaction's hash. Answers whether the call + /// succeeded, whether it marked the transaction processed, how many events + /// it logged, and how many UTXOs the active federation then holds. + fn registered( + tx: &BtcTransaction, + height: i64, + pegout_sig_hash: Option<[u8; 32]>, + svp_fund_hash: Option<[u8; 32]>, + ) -> (bool, bool, usize, usize) { + let config = BridgeConstants::rskj_regtest(); + let cfg = RskHardforkConfig::rskj_regtest(); + let mut ctx = regtest_bridge_with(tx); + if let Some(sig_hash) = pegout_sig_hash { + save_pegout_tx_sig_hash(&mut ctx, &sig_hash); + } + if let Some(hash) = svp_fund_hash { + bridge_store_bytes_named( + &mut ctx, + super::super::storage::SVP_FUND_TX_HASH_UNSIGNED_KEY, + &rlp_encode_element(&hash), + ); + } + let raw = btc_serialize(tx); + let tree = one_leaf_tree(&raw); + let args = encode::args(&[ + Arg::Bytes(&raw), + Arg::Word(encode::int(height)), + Arg::Bytes(&tree), + ]); + let ok = register_btc_transaction( + &mut ctx, + &args, + 0, + &config, + &cfg, + &BridgeTxContext::default(), + ) + .is_ok(); + let logs = ctx.journal_mut().take_logs().len(); + let processed = is_btc_tx_processed(&mut ctx, &legacy_btc_txid(tx)); + let utxos = super::super::storage::load_federation_utxos(&mut ctx, &cfg, 10).len(); + (ok, processed, logs, utxos) + } + + /// RSKj's regtest federation's output script. + fn regtest_federation_script() -> bitcoin::ScriptBuf { + let (config, cfg) = ( + BridgeConstants::rskj_regtest(), + RskHardforkConfig::rskj_regtest(), + ); + let mut ctx = queue_ctx(); + let (_, redeem) = active_federation_keys_and_redeem(&mut ctx, &config, &cfg, 10); + let format = active_federation_format(&mut ctx, &config, &cfg, 10); + p2sh_output_script(&federation_output_hash160(&redeem, format)) + } + + /// From the index's height, a transaction whose first input's sighash the + /// index holds is a pegout, whatever it spends; one the index doesn't hold + /// that pays a live federation is a peg-in. Below that height the legacy + /// rules take the same transaction for a peg-in too. Here the spent multisig + /// is no live federation's, as for a pegout registered after its federation + /// retired. + #[test] + fn the_pegout_index_finds_a_pegout_the_legacy_rules_take_for_a_peg_in() { + let redeem = build_federation_redeem_script(&test_keys(0x10), 2); + let tx = spend( + vec![signed_multisig(&redeem)], + &[ + (40_000, p2sh_output_script(&[0x77; 20])), + (1_000_000_000, regtest_federation_script()), + ], + ); + let sig_hash = sig_hash_for_pegout_index(&tx).unwrap(); + // Its change is registered as the federation's, and nothing is logged. + assert_eq!(registered(&tx, 350, sig_hash, None), (true, true, 0, 1)); + // As a peg-in it's turned down: marked processed, two events, and + // nothing kept for the federation. + let peg_in = (true, true, 2, 0); + assert_eq!(registered(&tx, 350, None, None), peg_in); + assert_eq!(registered(&tx, 349, sig_hash, None), peg_in); + } + + /// From the index's height, a transaction the index doesn't hold that pays + /// no live federation is left alone: not marked processed, nothing logged. + /// Below it the legacy rules take it for a peg-in. + #[test] + fn the_pegout_index_leaves_unregistered_what_it_does_not_know() { + let redeem = build_federation_redeem_script(&test_keys(0x10), 2); + let tx = spend( + vec![signed_multisig(&redeem)], + &[(40_000, p2sh_output_script(&[0x77; 20]))], + ); + assert_eq!(registered(&tx, 350, None, None), (true, false, 0, 0)); + // A peg-in of nothing: turned down with two events, not marked processed. + assert_eq!(registered(&tx, 349, None, None), (true, false, 2, 0)); + } + + /// RSKj looks for its SVP transactions only where it reads the pegout + /// index: there a transaction whose stripped hash is the stored fund + /// transaction's is registered as it, and below it the legacy rules + /// classify the same transaction. It strips as RSKIP305 has it, so a + /// transaction that only the earlier stripping would match isn't the fund + /// transaction. + #[test] + fn svp_transactions_are_found_only_where_the_pegout_index_is_read() { + let redeem = build_federation_redeem_script(&test_keys(0x10), 2); + let outputs = [(40_000, p2sh_output_script(&[0x77; 20]))]; + let tx = spend(vec![signed_multisig(&redeem)], &outputs); + let fund_hash = multisig_tx_hash_without_signatures(&tx, true).unwrap(); + assert_eq!(registered(&tx, 350, None, fund_hash), (true, true, 0, 0)); + assert_eq!(registered(&tx, 350, None, None), (true, false, 0, 0)); + assert_eq!(registered(&tx, 349, None, fund_hash), (true, false, 2, 0)); + let to_address = [&[0x76, 0xa9, 0x14][..], &[0x33; 20][..], &[0x88, 0xac][..]].concat(); + let tx = spend( + vec![ + signed_multisig(&redeem), + legacy(pushes(&[&[0x30; 71], &to_address]).into_bytes()), + ], + &outputs, + ); + let before_rskip305 = multisig_tx_hash_without_signatures(&tx, false).unwrap(); + assert!(before_rskip305.is_some()); + assert_eq!( + registered(&tx, 350, None, before_rskip305), + (true, false, 0, 0) + ); + } + + /// Signed pegouts RSKj released on mainnet and testnet, with legacy and + /// with segwit inputs: each strips to the sighash whose key RSKj's Bridge + /// storage holds, as read from RSKj's public nodes. + #[test] + fn real_pegouts_strip_to_the_sighashes_rskj_indexed() { + let vectors = include_str!("testdata/pegout_index_sighashes.txt"); + let (mut read, mut segwit) = (0, 0); + for line in vectors + .lines() + .filter(|line| !line.is_empty() && !line.starts_with('#')) + { + let [network, block, sig_hash, key, raw] = line.split(' ').collect::>()[..] + else { + panic!("a line of five columns: {line}"); + }; + let tx: BtcTransaction = deserialize(&hex::decode(raw).unwrap()).unwrap(); + let stripped = sig_hash_for_pegout_index(&tx).unwrap().unwrap(); + assert_eq!(to_hex(&stripped), sig_hash, "{network} {block}"); + assert_eq!( + format!("{:#066x}", pegout_tx_sig_hash_key(&stripped)), + key, + "{network} {block}" + ); + read += 1; + segwit += usize::from(tx.input.iter().any(|input| !input.witness.is_empty())); + } + assert_eq!((read, segwit), (8, 4)); + } } diff --git a/crates/execution/src/bridge/rskj_sender_compat.rs b/crates/execution/src/bridge/rskj_sender_compat.rs index f5032ad..2570de2 100644 --- a/crates/execution/src/bridge/rskj_sender_compat.rs +++ b/crates/execution/src/bridge/rskj_sender_compat.rs @@ -1,4 +1,5 @@ -//! rskj-compatible peg-in sender detection for P2SH-multisig inputs. +//! rskj-compatible peg-in sender detection for P2SH-multisig inputs, and the +//! redeem-script parsing it shares with signature stripping. //! //! # Why this file exists, and why it is separate //! @@ -20,12 +21,19 @@ //! //! * [`is_sent_to_multisig`] -- the port. Nothing else in the tree parses //! redeem scripts this way. -//! * [`set_enabled`] -- turns the whole path off. When the RSKIP activates, -//! the node sets it false at the activation height and this file can then be -//! deleted outright, along with its two callers in `peg.rs`. +//! * [`set_enabled`] -- turns the sender path off. When the RSKIP activates, +//! the node sets it false at the activation height, and the path's two +//! callers in `peg.rs` can then be deleted. //! * [`set_observer`] -- fires whenever the path is actually exercised, so an //! operator learns that a peg-in depended on code we intend to remove. //! +//! rskj strips a transaction's signatures with the same parsers +//! (`BitcoinUtils.removeSignaturesFromMultiSigTransaction`, which the SVP +//! checks and the pegout index run on every input), so the port, +//! [`redeem_script_of_input`] and [`signatures_required_to_spend`] serve that +//! too. That use isn't the sender path: it doesn't follow [`set_enabled`], and +//! it stays when the sender path goes. +//! //! # Fidelity notes //! //! Verified against `co.rsk.bitcoinj:bitcoinj-thin:0.14.4-rsk-18`, the artifact @@ -60,6 +68,10 @@ const OP_DROP: u8 = 0x75; const OP_CHECKMULTISIG: u8 = 0xae; const OP_CHECKMULTISIGVERIFY: u8 = 0xaf; const OP_CHECKSEQUENCEVERIFY: u8 = 0xb2; +const OP_DUP: u8 = 0x76; +const OP_EQUALVERIFY: u8 = 0x88; +const OP_HASH160: u8 = 0xa9; +const OP_CHECKSIG: u8 = 0xac; /// `RedeemScriptParserFactory.NON_STANDARD_ERP_TESTNET_REDEEM_SCRIPT_SERIALIZED`. /// @@ -471,6 +483,93 @@ fn dispatch(chunks: &[JChunk], depth: usize) -> Verdict { Verdict::NotMultisig // factory throws "unknown redeem script", caught } +// --- signature stripping ---------------------------------------------------- + +/// `BitcoinUtils.extractRedeemScriptFromInput`: the last push of an input's +/// witness if it has one, else the last chunk of its scriptSig, in either case +/// only if bitcoinj can parse it as a script. +/// +/// `Err` is the `ScriptException` `TransactionInput.getScriptSig()` throws on a +/// scriptSig it can't parse, which nothing between it and `Bridge.execute` +/// catches. +pub fn redeem_script_of_input( + script_sig: &[u8], + last_witness_push: Option<&[u8]>, +) -> Result>, &'static str> { + if let Some(push) = last_witness_push { + return Ok(parse_script_program(push).map(|_| push.to_vec())); + } + let Some(chunks) = parse_script_program(script_sig) else { + return Err("ScriptException parsing an input's scriptSig"); + }; + // An opcode's data is null, which finds no script. OP_0's is empty, which + // finds the empty one. + let Some(program) = chunks.last().and_then(|c| c.data.as_ref()) else { + return Ok(None); + }; + Ok(parse_script_program(program).map(|_| program.clone())) +} + +/// `Script.getNumberOfSignaturesRequiredToSpend()`: a multisig's M, and one for +/// a pay-to-address or pay-to-pubkey script. bitcoinj throws for anything else: +/// an `IllegalStateException`, or `isSentToAddress`'s `NullPointerException` +/// for a script that opens `OP_DUP OP_HASH160` with an opcode where the hash +/// belongs. Nothing between it and `Bridge.execute` catches any of them. +/// Callers pass only scripts bitcoinj has parsed, so the unparseable answer is +/// never reached. +pub fn signatures_required_to_spend(script: &[u8]) -> Result { + let Some(chunks) = parse_script_program(script) else { + return Err("ScriptException parsing the script"); + }; + match dispatch(&chunks, 0) { + Verdict::Multisig => multisig_m(&chunks), + Verdict::RskjThrows(why) => Err(why), + Verdict::NotMultisig if is_sent_to_address(&chunks) || is_sent_to_raw_pub_key(&chunks) => { + Ok(1) + } + Verdict::NotMultisig => Err("bitcoinj can't count the signatures this script needs"), + } +} + +/// `getRedeemScriptParser().getM()` for a script `dispatch` finds a multisig: +/// a flyover script's inner script's, a standard multisig's first chunk, and an +/// ERP script's default multisig's first chunk, which follows its `OP_NOTIF`. +/// The structure checks have decoded that chunk already, so the error can't be +/// reached. +fn multisig_m(chunks: &[JChunk]) -> Result { + if has_flyover_structure(chunks) { + return multisig_m(&chunks[2..]); + } + let first = if has_standard_structure(chunks) == Ok(true) { + &chunks[0] + } else { + &chunks[1] + }; + first + .decode_positive_n() + .map_err(|_| "decoding a multisig's M") +} + +/// `Script.isSentToAddress()`. Where Java's missing null check throws, for an +/// opcode in the hash's place, this answers false: its one caller fails the +/// same either way. +fn is_sent_to_address(chunks: &[JChunk]) -> bool { + chunks.len() == 5 + && chunks[0].equals_op_code(OP_DUP) + && chunks[1].equals_op_code(OP_HASH160) + && chunks[2].data.as_ref().is_some_and(|hash| hash.len() == 20) + && chunks[3].equals_op_code(OP_EQUALVERIFY) + && chunks[4].equals_op_code(OP_CHECKSIG) +} + +/// `Script.isSentToRawPubKey()`. +fn is_sent_to_raw_pub_key(chunks: &[JChunk]) -> bool { + chunks.len() == 2 + && chunks[1].equals_op_code(OP_CHECKSIG) + && !chunks[0].is_op_code() + && chunks[0].data.as_ref().is_some_and(|d| d.len() > 1) +} + fn hex_decode(s: &str) -> Vec { (0..s.len()) .step_by(2) diff --git a/crates/execution/src/bridge/testdata/pegout_index_sighashes.txt b/crates/execution/src/bridge/testdata/pegout_index_sighashes.txt new file mode 100644 index 0000000..d025698 --- /dev/null +++ b/crates/execution/src/bridge/testdata/pegout_index_sighashes.txt @@ -0,0 +1,14 @@ +# Signed pegouts RSKj released (the Bridge's release_btc events), read on 10 November 2026: +# on each network, two just after RSKIP379 began the pegout index (legacy P2SH inputs) and two +# recent ones (segwit P2SH-P2WSH inputs). For each, RSKj's public node holds 0x01 in the Bridge's +# storage at the key pegoutTxSigHash-, and nothing at the key whose sighash has its first +# bit flipped. Columns: network, the RSK block of release_btc, the sighash, its storage key, and +# the signed transaction. +mainnet 6241417 e39e744870ec1706b6e478993d674b58422e7bd3f9f64e412906f8ec11b119be 0x387a434549630a1b91cd452a5b62f55c48c85e943a483463b6b55e757a66103a 0200000002fada7591466f2c973812a58195a019f271a74d84cdeea46ff794f3d089a41d7f01000000fd370300473044022004b686fc610f4af5a9dc869deb34e891b00ae93339347c1c3964bc07ed641c530220288853821015f7eb3419b3ebc713b0864d70b43fd6031e9b00b5f9e019c2094d014730440220369caa395cc2c869de7986bff8ffa133cfbedf028713e90af2f10ba92c06c82c02205db2551eec9487c72d5a36e168276626531ccb97db0a53ba6848aa559eb4bf8901473044022021a63d0cba24c8a912f56942d1be233be1456030def03cd148818c40321a54c702206c784d14c726177751a25d94061331cf1a2602270fd4f908955e6504f90d82a401473044022067b5122ea5e5b10a662fc9a304eed5be2b018ebb682aaa713d501a4ff03b562b022014d607b0bfa1427be2bddbab71c556a3fdb5fc9762a5431687d9c0e4d19be66401483045022100dc93fc14ae272cb3ba14b0460e3b7f0e84d43ebad747607224aa5c0f179dbb5d02203f84931754954251d12d69bc8c0cdf13e71af13913a5dd8381db278fd332956601004dc901645521020ace50bab1230f8002a0bfe619482af74b338cc9e4c956add228df47e6adae1c21025093f439fb8006fd29ab56605ffec9cdc840d16d2361004e1337a2f86d8bd2db210275d473555de2733c47125f9702b0f870df1d817379f5587f09b6c40ed2c6c9492102a95f095d0ce8cb3b9bf70cc837e3ebe1d107959b1fa3f9b2d8f33446f9c8cbdb2103250c11be0561b1d7ae168b1f59e39cbc1fd1ba3cf4d2140c1a365b2723a2bf9321034851379ec6b8a701bd3eef8a0e2b119abb4bdde7532a3d6bcbff291b0daf3f25210350179f143a632ce4e6ac9a755b82f7f4266cfebb116a42cadb104c2c2a3350f92103b04fbd87ef5e2c0946a684c8c93950301a45943bbe56d979602038698facf9032103b58a5da144f5abab2e03e414ad044b732300de52fa25c672a7f7b3588877190659ae670350cd00b275532102370a9838e4d15708ad14a104ee5606b36caaaaf739d833e67770ce9fd9b3ec80210257c293086c4d4fe8943deda5f890a37d11bebd140e220faa76258a41d077b4d42103c2660a46aa73078ee6016dee953488566426cf55fc8011edd0085634d75395f92103cd3e383ec6e12719a6c69515e5559bcbe037d0aa24c187e1e26ce932e22ad7b354ae68fffffffff0c149c2484cb4bd4746928cd5445ef94386a744779545b7d8349bde77277bc201000000fd3803004730440220245cadb8d759ce879042720eba05a7eff2a0621f50c661fbdb2a3685feab810302200ed294cad0aa37cb5456a81d8876060914d4bc4f11a2470966bf53cdd79ad396014730440220492d4f3d20db09d6b711952e90a1461c73af258a955a3b082eae44435cecc29a02207616664316efabd8461a0481975b41d39548d271137198b0592d700b231cdc3f01473044022072a550e1d3ccd49b3956452e2d134cf4d612c1c568b5b8021cc43f5749e479d902202ba07513179323baf1c4737571cdfe9c47becc0c581a98ea0474818feb0174aa01483045022100b200e38b30a3c1b334cfded420fd4210533523c5144f357b88957879ad8cde5b02202081fbce1b7aa115b6ebeeb5f69d4389b2d224b93c0bccf60d9e5342a079eb0f01483045022100f4d1edb1b9b500ff6cf017acc4b6a4de97555f12dc1585789add829e15f1d3840220449af38d066891eae3f9c362b570ed6d80d2f7fe6b7fbcde9dc1b50617d8c3bc01004dc901645521020ace50bab1230f8002a0bfe619482af74b338cc9e4c956add228df47e6adae1c21025093f439fb8006fd29ab56605ffec9cdc840d16d2361004e1337a2f86d8bd2db210275d473555de2733c47125f9702b0f870df1d817379f5587f09b6c40ed2c6c9492102a95f095d0ce8cb3b9bf70cc837e3ebe1d107959b1fa3f9b2d8f33446f9c8cbdb2103250c11be0561b1d7ae168b1f59e39cbc1fd1ba3cf4d2140c1a365b2723a2bf9321034851379ec6b8a701bd3eef8a0e2b119abb4bdde7532a3d6bcbff291b0daf3f25210350179f143a632ce4e6ac9a755b82f7f4266cfebb116a42cadb104c2c2a3350f92103b04fbd87ef5e2c0946a684c8c93950301a45943bbe56d979602038698facf9032103b58a5da144f5abab2e03e414ad044b732300de52fa25c672a7f7b3588877190659ae670350cd00b275532102370a9838e4d15708ad14a104ee5606b36caaaaf739d833e67770ce9fd9b3ec80210257c293086c4d4fe8943deda5f890a37d11bebd140e220faa76258a41d077b4d42103c2660a46aa73078ee6016dee953488566426cf55fc8011edd0085634d75395f92103cd3e383ec6e12719a6c69515e5559bcbe037d0aa24c187e1e26ce932e22ad7b354ae68ffffffff02f09fa30d000000001976a914b4e2dfb3e472aff52c386d3fb8b3826b9380a38888ac4e08b91c0000000017a914d3530b561910c250f58fbd572d2f7a7d847354ef8700000000 +mainnet 6245419 e544dd15e3da2a2b2fea7b8c6462e818758cc6bb1a8b4ff5048a102dd02d165a 0xa792c524e712b1068c2561cd504182f6fa5707b114506675dbd6235ca0b81a4a 02000000034ca464208288cb600e6e42bc8419a7f4695eb62247c5e0618b0aeec53169c39d01000000fd38030047304402206329db5dce5d83c8efbe1e682fb5ca6aae964d7950de0c44ec12363861fe582b02204d0822051651d28f79e8ba5ea48ed3dd0bb7573c69af6da1e2c7784f59c4cd3101483045022100b4b7581da37175c42880edeba07033525ad3e0b54d5e80a048ef6f1db5566151022014c02613705523a318cf85b04c51b11b453b6a58d1983553dafb3498ad88f18c0147304402206895765bab4e660689aeb51e7e224dd8ae03ad66a2629c67c8c1305574ad813e022004b2f312a4ae30089a57f4fa924df1a9664bff0ed42089fc36129379ccbf4d4401473044022011a839aacd1df342304e3a281b12538323af82604e5c6bf4de123373b9e01e1c02206fb69867b54bb20b61ad21ca83d6dbeab3d2c756cc1a06c3d175dd6ddd58dd76014830450221009855406fc4e5eee50cef8da97be53adbdab50043bf515eaadc0ecd36946be455022068732148ada33527aba75cbdabf83bf7a5d1f4f58ad83521acda642a77864fda01004dc901645521020ace50bab1230f8002a0bfe619482af74b338cc9e4c956add228df47e6adae1c21025093f439fb8006fd29ab56605ffec9cdc840d16d2361004e1337a2f86d8bd2db210275d473555de2733c47125f9702b0f870df1d817379f5587f09b6c40ed2c6c9492102a95f095d0ce8cb3b9bf70cc837e3ebe1d107959b1fa3f9b2d8f33446f9c8cbdb2103250c11be0561b1d7ae168b1f59e39cbc1fd1ba3cf4d2140c1a365b2723a2bf9321034851379ec6b8a701bd3eef8a0e2b119abb4bdde7532a3d6bcbff291b0daf3f25210350179f143a632ce4e6ac9a755b82f7f4266cfebb116a42cadb104c2c2a3350f92103b04fbd87ef5e2c0946a684c8c93950301a45943bbe56d979602038698facf9032103b58a5da144f5abab2e03e414ad044b732300de52fa25c672a7f7b3588877190659ae670350cd00b275532102370a9838e4d15708ad14a104ee5606b36caaaaf739d833e67770ce9fd9b3ec80210257c293086c4d4fe8943deda5f890a37d11bebd140e220faa76258a41d077b4d42103c2660a46aa73078ee6016dee953488566426cf55fc8011edd0085634d75395f92103cd3e383ec6e12719a6c69515e5559bcbe037d0aa24c187e1e26ce932e22ad7b354ae68ffffffff8554bd2644572fa43548120cf0db96d6c071bff75664c052650e008fef6b4db001000000fd3703004730440220377ba10d111580ff624b0026710abaf098361ca7441c77b822279e90b7a408600220348ccb932dc680f0b9b39d164933681e195d3c440b9ee971efd0a7fd93a6cd630147304402200a8c81468a498a0b372426988659adcbd209f542d610a083c08fc11761392ed302200749fbe44f565a4a40e3a1d466a89cb692408e15c932739d7ea35b29ce89904401473044022013ba81ae027f7ff050a985e8438e236a262ce1e71831414390a88bf076d80333022042d4b9424930e499d39b55fe548325ffe393c2245c736978947d5f587f778bb20147304402207758b8a257b49d13f1936b47897e21c554a5738d304417ae004e1a7ed7befcc20220544cf9a154562daf55e79307723750934bf9503cc7b6bdb1b7c11fbb011469b30148304502210094cc5135f969219e45a1f8f71e26b48484bd49bfa6240ad166cc98830e75cd7902202ba5e2e42e2e83b761c72c665176808885c4961933ee9a0ff6f26f0d90b69b3001004dc901645521020ace50bab1230f8002a0bfe619482af74b338cc9e4c956add228df47e6adae1c21025093f439fb8006fd29ab56605ffec9cdc840d16d2361004e1337a2f86d8bd2db210275d473555de2733c47125f9702b0f870df1d817379f5587f09b6c40ed2c6c9492102a95f095d0ce8cb3b9bf70cc837e3ebe1d107959b1fa3f9b2d8f33446f9c8cbdb2103250c11be0561b1d7ae168b1f59e39cbc1fd1ba3cf4d2140c1a365b2723a2bf9321034851379ec6b8a701bd3eef8a0e2b119abb4bdde7532a3d6bcbff291b0daf3f25210350179f143a632ce4e6ac9a755b82f7f4266cfebb116a42cadb104c2c2a3350f92103b04fbd87ef5e2c0946a684c8c93950301a45943bbe56d979602038698facf9032103b58a5da144f5abab2e03e414ad044b732300de52fa25c672a7f7b3588877190659ae670350cd00b275532102370a9838e4d15708ad14a104ee5606b36caaaaf739d833e67770ce9fd9b3ec80210257c293086c4d4fe8943deda5f890a37d11bebd140e220faa76258a41d077b4d42103c2660a46aa73078ee6016dee953488566426cf55fc8011edd0085634d75395f92103cd3e383ec6e12719a6c69515e5559bcbe037d0aa24c187e1e26ce932e22ad7b354ae68ffffffff48b12aec74330c3923a16fa1b110c8f3d70b5a49c35fc5862c3872d0a16112b602000000fd3803004730440220101151449e6c72925f6afaff730c3c69a0753b037dbffc237579a636dcf35f850220624b89974b08d5db43c3c1648868be3d43794dce7e32bbc7c6aa4f946feeb2cb01473044022067e8f660ab337394bfbca70176431d599bdd31bfbab9e074136676c4ee20f6c7022057a3795a1f68cc1e392eb4618c5f37f41aac088a126320b83fd27d901d156f090147304402202e0e9edb6d81f09ddfb5be0f3dbf9601c250a5192d9e8b884209e295e1ada94e0220407e9e71250b7adf269152d46456752334ff67a9d10b64f09c47966cfc81fbaa01483045022100ffb59d1c9e2586162028d1fb707c3571dfcdd482bb12a1a14d63136c6ef066b1022079a733b8f10b0ee9bc9edb67c20429cfc8c29918c73afe0c7db3d13ae3cfff49014830450221008904348814a2306a54c85dbe88395653712e0cfdc01f09d394e1985ede632389022077451b192f310859327daa6350234505fcdc2cd1a1afdfbd91bae85e5e4cc46f01004dc901645521020ace50bab1230f8002a0bfe619482af74b338cc9e4c956add228df47e6adae1c21025093f439fb8006fd29ab56605ffec9cdc840d16d2361004e1337a2f86d8bd2db210275d473555de2733c47125f9702b0f870df1d817379f5587f09b6c40ed2c6c9492102a95f095d0ce8cb3b9bf70cc837e3ebe1d107959b1fa3f9b2d8f33446f9c8cbdb2103250c11be0561b1d7ae168b1f59e39cbc1fd1ba3cf4d2140c1a365b2723a2bf9321034851379ec6b8a701bd3eef8a0e2b119abb4bdde7532a3d6bcbff291b0daf3f25210350179f143a632ce4e6ac9a755b82f7f4266cfebb116a42cadb104c2c2a3350f92103b04fbd87ef5e2c0946a684c8c93950301a45943bbe56d979602038698facf9032103b58a5da144f5abab2e03e414ad044b732300de52fa25c672a7f7b3588877190659ae670350cd00b275532102370a9838e4d15708ad14a104ee5606b36caaaaf739d833e67770ce9fd9b3ec80210257c293086c4d4fe8943deda5f890a37d11bebd140e220faa76258a41d077b4d42103c2660a46aa73078ee6016dee953488566426cf55fc8011edd0085634d75395f92103cd3e383ec6e12719a6c69515e5559bcbe037d0aa24c187e1e26ce932e22ad7b354ae68ffffffff02a8563800000000001976a914cea4978de0156d6d998467c8b70d7926aec87dea88acd852d0010000000017a914d3530b561910c250f58fbd572d2f7a7d847354ef8700000000 +mainnet 9324054 13c80ddba645f84c73ca4ca16048e8f246d9c293035301a36964f3e833f35c75 0xefe630f78f2348f39edff45110decaac1ddf7fc59903dc5151c51e3e17b4a7c9 020000000001016122c54d6e37bc34216746d8ce90e3e5f179851f3b59abbccea21bd9cf703d1a01000000232200202f03c5cf40f834d0662838848744295f4999fdb828cd606b1704d4d12d8f032dffffffff02a6a20a00000000001976a91474478ef0c3d4dadb83eb82dffb724842da5c20f288ac1b9017071c00000017a914a2aaed588a584e3b190d8219d3668749f52ece8a870800483045022100ec45ca642b309e70598365e31b48bc1199654267655689af43cbedd7a7b85933022010d3457e6b47b35e9d05aa69923ec37d028f47ea65288cc511bb06c0173bb95a0147304402205c56486b24edfebf95807dd8fec28de46f89518ebd8359f82d7673cf306175f402200883707d6fb3b7e3bfd468b3240682c24e32271761fd814e22350e2c4b2308b20147304402206cb284bc32fd864299171184b26b21d90d12436f59b697757c1f50a42c648cda02203d0a627e201bed08ecde754858ca3e55f804cafe2dc3a1f9583e24219e6600c40147304402205a722969c52a609568ca6c8b90568d98b4d961444ccb14c73276b0d7af3db09f0220159a53ec4d56bf6d61e9526ef7a21de2c2d24b3fe2703f810ca9f3ef9bba1f250147304402203035e61a479747ccbc6597a726b053f4cd109be60a165d2893844bbfa40a571c022073d3a3d677219e94056d4e9c71b5fd18584cc6c12e96f90397805f6115209cde0100fdc9016455210274c7a2c584eb9dbc3d78fede48d1258b974cd537d82fce4d18f0e85604774e652102daa3cd74c9d06bc7928e1214f0de4ee5744c694975d70e160f562c5e1c4f587d2102f0844d9c1df50f5597118659badf40ea6956903f405f6a604402c8014d926fd42103250c11be0561b1d7ae168b1f59e39cbc1fd1ba3cf4d2140c1a365b2723a2bf93210328f0386fe2b2840a424d3a5073be89a4cb643c434752b8c865e867611d00418321038cc42c8d3cee8360671d2e70226ef95eb840adf4b9c4c5c31b85ffc26eeca7a82103b8faf3ebcf3fe89085fe8c6046dd9cece45bb56e466103f96b4fca2f21072ded2103e59ed1ba1240ac6ff0cb9234afc0e44d6e7653219982d4f1eb55844395e934962103e8f68168606392c5e17f2f9bf1fd3fa55c295cad3990e72e7e3e4470ed644c8f59ae670350cd00b275532102370a9838e4d15708ad14a104ee5606b36caaaaf739d833e67770ce9fd9b3ec80210257c293086c4d4fe8943deda5f890a37d11bebd140e220faa76258a41d077b4d42103c2660a46aa73078ee6016dee953488566426cf55fc8011edd0085634d75395f92103cd3e383ec6e12719a6c69515e5559bcbe037d0aa24c187e1e26ce932e22ad7b354ae6800000000 +mainnet 9328761 a1f3378a0acd189420fd6f4ad90d49b655722bb2055f82f4aa0ee3421e9e6d95 0x9e4f76257c3838d33f764c28fe696a6715e8314b6bec555aa8c31505b2690e5b 02000000000101354d67291a8408593b74c4f4f7ddaccfb6a597345281c33ce9f25d918fc6855a02000000232200202f03c5cf40f834d0662838848744295f4999fdb828cd606b1704d4d12d8f032dffffffff02c5781e00000000001976a91474478ef0c3d4dadb83eb82dffb724842da5c20f288ac332acd020000000017a914a2aaed588a584e3b190d8219d3668749f52ece8a8708004830450221009a3bdaf4da9ea950df059396d71a678c0c068834470a5a6a06e826d86834a75b0220644b1f8d940f3fb45ea1fbdfe59d19619aec6d4edb20b67700d039c599db3f6201483045022100dc04edbd2b81c0fb1e7482748f108e1af5888d2645210d2c717dfeff6cbc17480220058d81ffd012db5788424575c68a6d3f42cf30a73565f7b39ef4489d85c9c2f101473044022056e94e448237e77d01cd268eb926c98b86631ac0b415c510a28b98c49bed54fe0220326de8305d1ed6464ac515f61e2a354f795f123b7e941286682f10734318c9b601483045022100b88d2c39bb6972cfe5260f98b9d6f511f32930fd7f1a8e395a636edd332aebfa022049c7936ea6d451e87a7e12b578185fa4207c61db1cd9041312010a051b745a7901473044022031286d5557b865fe26f884b11f6a47f31f6b833353d3e48f547e0e80682ee1c002207a4ae0e9ddefdc07fe03af9e695ae65e2fde41dc6ee6357199c826c748d345830100fdc9016455210274c7a2c584eb9dbc3d78fede48d1258b974cd537d82fce4d18f0e85604774e652102daa3cd74c9d06bc7928e1214f0de4ee5744c694975d70e160f562c5e1c4f587d2102f0844d9c1df50f5597118659badf40ea6956903f405f6a604402c8014d926fd42103250c11be0561b1d7ae168b1f59e39cbc1fd1ba3cf4d2140c1a365b2723a2bf93210328f0386fe2b2840a424d3a5073be89a4cb643c434752b8c865e867611d00418321038cc42c8d3cee8360671d2e70226ef95eb840adf4b9c4c5c31b85ffc26eeca7a82103b8faf3ebcf3fe89085fe8c6046dd9cece45bb56e466103f96b4fca2f21072ded2103e59ed1ba1240ac6ff0cb9234afc0e44d6e7653219982d4f1eb55844395e934962103e8f68168606392c5e17f2f9bf1fd3fa55c295cad3990e72e7e3e4470ed644c8f59ae670350cd00b275532102370a9838e4d15708ad14a104ee5606b36caaaaf739d833e67770ce9fd9b3ec80210257c293086c4d4fe8943deda5f890a37d11bebd140e220faa76258a41d077b4d42103c2660a46aa73078ee6016dee953488566426cf55fc8011edd0085634d75395f92103cd3e383ec6e12719a6c69515e5559bcbe037d0aa24c187e1e26ce932e22ad7b354ae6800000000 +testnet 5000929 21c5d7230266eefcbcaea726bd81b1bd8d8bb1a68de470cb6d4ff7eda4f70ee3 0xe536901a96741773dc17a10f5e234b3ed34b66929a56ebd6832be1cd601d55a9 02000000026d3cd9ded9ca42535a9819f0d5952b65446da92cc4e4b910b2aa36af6413352901000000fd8c0200483045022100a9f5bcc3afde6911b3b1158eb324401a0698cbc99ed53d2215c86ef26ba94565022041aa1f03fbd0e755e05f65d21fca1589c188289177e38676048e3941c33609f301483045022100a2289d3972fa2502530a1755e305b71ec17b9da0448306b33688d7c97f06330b022070d9aff7380ed0f0f1ae5089d94bf8cf30679ce104f052d97d9c7508a72a9719014830450221008a47b8533364dd393a175bd530e4669b0127ddf4852e4ded973ff2be5c3f25c00220569a31acda6e8a2fd2ccea6ba571f148ac8f5030cfeccbbec12deb5e3e3cfc9b014830450221009537d242fb6853a78b40580fa5988c61e7b4d0f1b566a7cc00935a4518a590eb02204ca58bda44a79d189e7d172177631498433c1f46c18668ba741500f45cd81e6f01004d630164542102099fd69cf6a350679a05593c3ff814bfaa281eb6dde505c953cf2875979b120921022a159227df514c7b7808ee182ae07d71770b67eda1e5ee668272761eefb2c24c2102afc230c2d355b1a577682b07bc2646041b5d0177af0f98395a46018da699b6da21037ce55a309160df8c4b3baaa7a01e08e1f83c2a81907c45f0a3303c328fdc28062103ac2cc99a1a91b6f61e5323e7b3f442fa65bf8ad518ac3c0e26921e96f0e6126a2103e3929be608a537713c078c356af1c2a82720cc2e7f94718e589948dce5f27c602103fb8e1d5d0392d35ca8c3656acb6193dbf392b3e89b9b7b86693f5c80f7ce858157ae670350cd00b27552210216c23b2ea8e4f11c3f9e22711addb1d16a93964796913830856b568cc3ea21d3210275562901dd8faae20de0a4166362a4f82188db77dbed4ca887422ea1ec185f1421034db69f2112f4fb1bb6141bf6e2bd6631f0484d0bd95b16767902c9fe219d4a6f53ae68ffffffff0add9727a29932ee3ec8a87915b7016bb7b9ca20bbcc8279a1e8b1998f9b574d01000000fd8a0200483045022100c1a2430b12a0eb3aa2e11a0e567e520cb0ee722a60a0a93fc2607ea6e0b0f75d0220195aa134657eb41e620f503c6aa9386793388481cd7ba4d381e60117e9bfd7dd0147304402202cf4e97cffa28761cd92aa84940e43a3ad90877eb2d07069dd1fd4bf018ce3510220016e0705b8a5f620877eb019b57f961c448eaa18f90038e497c26951dd31f51e01483045022100c702f297fa6e253a94990d582a5e9bb734b3fca9adc0a5d51a790ef0553079c202200c92898cda8777c816160ff45fdc89b8be8c37a4f57c5397f1956a4c65b9d3540147304402204ca432c8ad871bbdd43e900aa299ac4a44125e82ab0eeb00f1cf1c42837f682202207f8d4c53c2a68f53af6a044daace44b6bde58b65818d1672ae6c1f949506f1ed01004d630164542102099fd69cf6a350679a05593c3ff814bfaa281eb6dde505c953cf2875979b120921022a159227df514c7b7808ee182ae07d71770b67eda1e5ee668272761eefb2c24c2102afc230c2d355b1a577682b07bc2646041b5d0177af0f98395a46018da699b6da21037ce55a309160df8c4b3baaa7a01e08e1f83c2a81907c45f0a3303c328fdc28062103ac2cc99a1a91b6f61e5323e7b3f442fa65bf8ad518ac3c0e26921e96f0e6126a2103e3929be608a537713c078c356af1c2a82720cc2e7f94718e589948dce5f27c602103fb8e1d5d0392d35ca8c3656acb6193dbf392b3e89b9b7b86693f5c80f7ce858157ae670350cd00b27552210216c23b2ea8e4f11c3f9e22711addb1d16a93964796913830856b568cc3ea21d3210275562901dd8faae20de0a4166362a4f82188db77dbed4ca887422ea1ec185f1421034db69f2112f4fb1bb6141bf6e2bd6631f0484d0bd95b16767902c9fe219d4a6f53ae68ffffffff0281900600000000001976a9146595581a52c94b07f16ac76e954dc85b5d41b4d588acaba208000000000017a9146e4b5ae85d86e4db0e6e5db09f8c276328cdbf3f8700000000 +testnet 5003151 af7ba98c90a4f7172f830e4f394439ba226b67d2007f079496c8f0db3a67166e 0x5dc837467b2ee05ced0fd6ba15de8daa13a5504dcaa0bf0133e46c1152791ed5 020000000533d2f85a00ab1352b61cd3d5bcde66c0deece07d62eb40c92d99d04b9c4c424601000000fd8a0200483045022100e6f3763a2b3324457338f382b8044384b0ac5f7c021f62f1a80bd0dedc936ed802201f64683d068fad287f588a2682fcae5f6588a12b04a01c6f3d7439263a914ad00147304402203ca8786f8e18cf72a85e94ba1c0e833500a7462589cc9b04c25a77073a77616602205f3effdd0322f28acdc423405a754eeeb1c6ef9e6b4e863900283d9c8501a7bf01483045022100c3746b2440bfafa4eb8baaac63ddfee94810344bc8f619c8870d35e1723131ed02207476726345430c201bb906130aaa69c8b5ba8785796418acad53f5349877564101473044022044a6fcb71983d68ef82e25e448947e61d07b9c9fa53fdc7e94dab4d4afd57d5002200fac74ceef77861a52f4310146a8e0546ea940e72def24a663235c095aaffa8d01004d630164542102099fd69cf6a350679a05593c3ff814bfaa281eb6dde505c953cf2875979b120921022a159227df514c7b7808ee182ae07d71770b67eda1e5ee668272761eefb2c24c2102afc230c2d355b1a577682b07bc2646041b5d0177af0f98395a46018da699b6da21037ce55a309160df8c4b3baaa7a01e08e1f83c2a81907c45f0a3303c328fdc28062103ac2cc99a1a91b6f61e5323e7b3f442fa65bf8ad518ac3c0e26921e96f0e6126a2103e3929be608a537713c078c356af1c2a82720cc2e7f94718e589948dce5f27c602103fb8e1d5d0392d35ca8c3656acb6193dbf392b3e89b9b7b86693f5c80f7ce858157ae670350cd00b27552210216c23b2ea8e4f11c3f9e22711addb1d16a93964796913830856b568cc3ea21d3210275562901dd8faae20de0a4166362a4f82188db77dbed4ca887422ea1ec185f1421034db69f2112f4fb1bb6141bf6e2bd6631f0484d0bd95b16767902c9fe219d4a6f53ae68ffffffff6a8b787daa331c2d8857e1d22878aacb30fe3c9e1864ff29a00eaa00dac3efa601000000fd8a020047304402201242769ac0c4865ab9adf4a591d33e369cc948a166ecf94f13fc7cde5dd4c7640220314f3ceb647703adace22127f9fff8ea2dec3ef4f4b066e92aeb0dc8bdf38c7801473044022020c8736f4adcde9a1f5cf017e2a9af1c2a84b2e3bf1c136b71796b846da2454f02201ae2d238c7aca47f34e2ee2ac895c59a29c1868bdcda7f4f6acb6ba4e593926301483045022100cdc539f88bdc829ca02f410f1b35a9ee4075df5a19e8308f6e67a897256439dd022048ab754334d6695b52f54c71a29a13d639b36812d634bbbed818626d60584302014830450221008a9748375689676da2bac6097b7fe2151b1476238391cc8fbaea33099708f07b02201ad08f856bf709129b17af15eb86a330a13ec62ddff4b1e4d2a4d8140f822bb701004d630164542102099fd69cf6a350679a05593c3ff814bfaa281eb6dde505c953cf2875979b120921022a159227df514c7b7808ee182ae07d71770b67eda1e5ee668272761eefb2c24c2102afc230c2d355b1a577682b07bc2646041b5d0177af0f98395a46018da699b6da21037ce55a309160df8c4b3baaa7a01e08e1f83c2a81907c45f0a3303c328fdc28062103ac2cc99a1a91b6f61e5323e7b3f442fa65bf8ad518ac3c0e26921e96f0e6126a2103e3929be608a537713c078c356af1c2a82720cc2e7f94718e589948dce5f27c602103fb8e1d5d0392d35ca8c3656acb6193dbf392b3e89b9b7b86693f5c80f7ce858157ae670350cd00b27552210216c23b2ea8e4f11c3f9e22711addb1d16a93964796913830856b568cc3ea21d3210275562901dd8faae20de0a4166362a4f82188db77dbed4ca887422ea1ec185f1421034db69f2112f4fb1bb6141bf6e2bd6631f0484d0bd95b16767902c9fe219d4a6f53ae68ffffffff3cef3f54f103d86eac202f9681368abcbc931b43dbaf2b76b1fd980faf0981b801000000fd8b0200483045022100bf3c9d57945ab9dd6f836ed3bf5fb67ca1cac7b10875cac903a3fd1c44e55e95022072fe39078f559fd3ab2126c2f9ca7c055880db043af9d2f62fe9b29b49a7fc780147304402201e6fca99ee5454173755d06a880cf7c5796bf16ed7d4362df488a3be01d761d7022073303c4fa28b6628370e1ed1d400af3a760eeced21b3b6be39b1d962eb8003d401483045022100b428ec8b17f4964f15686c1ea877f04945cf4baa1a852a809b65db9e0fa1cb7d02205aff67109f1781c9cbfc42b37d448e6f4441f1120cc535ebeaeb91b118b10ee301483045022100f8bc0048e7b3c108b510c5b621a09263c0d499e156d7622b434d214105fd31f602201c09b281314256ee7bedce4f5ff531d740aea4d79d7dc8145bae64c022f8471201004d630164542102099fd69cf6a350679a05593c3ff814bfaa281eb6dde505c953cf2875979b120921022a159227df514c7b7808ee182ae07d71770b67eda1e5ee668272761eefb2c24c2102afc230c2d355b1a577682b07bc2646041b5d0177af0f98395a46018da699b6da21037ce55a309160df8c4b3baaa7a01e08e1f83c2a81907c45f0a3303c328fdc28062103ac2cc99a1a91b6f61e5323e7b3f442fa65bf8ad518ac3c0e26921e96f0e6126a2103e3929be608a537713c078c356af1c2a82720cc2e7f94718e589948dce5f27c602103fb8e1d5d0392d35ca8c3656acb6193dbf392b3e89b9b7b86693f5c80f7ce858157ae670350cd00b27552210216c23b2ea8e4f11c3f9e22711addb1d16a93964796913830856b568cc3ea21d3210275562901dd8faae20de0a4166362a4f82188db77dbed4ca887422ea1ec185f1421034db69f2112f4fb1bb6141bf6e2bd6631f0484d0bd95b16767902c9fe219d4a6f53ae68ffffffff5b08968640e1daf7a45ad1abf646aa776a3ee5f6f8ae4cf9110329dc775adac201000000fd8b0200483045022100828d67a6e3782b41eb5ef0a6684dae915438969ef11d2df53d1947f20911c342022045b1e8dd66aeec6b67937cad2875bc8396c99146a2b30b89401790ac6039a76801483045022100b035c1de0f34363b148b235bb3d23900e94fe640e50b8927dc1f05fa1f8046e50220045c2f23771abf32684a4e73f3df6d9a6f095657e11e22c73a65ad9e5ddcb65b014730440220339ef01b340fe5c420001ed62ab8e554e2494b7aabe5db935f319717078c3f310220153c687381e36e44ec4c43524ecb9eced3e68d71a5ebd6bcaaf06f6d5dba657d01483045022100c7007bfb410ac9d14e79c72d4c7f1c6e11352f974a16b45b95bb0b243c24fece02207b32865eada01383414b5e8be893b749cb700a6850cf5a5ac04d5c8f6c52256401004d630164542102099fd69cf6a350679a05593c3ff814bfaa281eb6dde505c953cf2875979b120921022a159227df514c7b7808ee182ae07d71770b67eda1e5ee668272761eefb2c24c2102afc230c2d355b1a577682b07bc2646041b5d0177af0f98395a46018da699b6da21037ce55a309160df8c4b3baaa7a01e08e1f83c2a81907c45f0a3303c328fdc28062103ac2cc99a1a91b6f61e5323e7b3f442fa65bf8ad518ac3c0e26921e96f0e6126a2103e3929be608a537713c078c356af1c2a82720cc2e7f94718e589948dce5f27c602103fb8e1d5d0392d35ca8c3656acb6193dbf392b3e89b9b7b86693f5c80f7ce858157ae670350cd00b27552210216c23b2ea8e4f11c3f9e22711addb1d16a93964796913830856b568cc3ea21d3210275562901dd8faae20de0a4166362a4f82188db77dbed4ca887422ea1ec185f1421034db69f2112f4fb1bb6141bf6e2bd6631f0484d0bd95b16767902c9fe219d4a6f53ae68ffffffffba8048fcb74742de825977864caee15dc686c12519b5cd7ced8eeaadfc697cc301000000fd8a020048304502210092bd75df683fa029f8dcf67d85a59bf1fc9f63526b0d98288009a09fb9f97d1702201898b3e3fe38089198dea966fdb02ea2d5f81e943cbed84a5143725e39fc1f6701483045022100f77601bfba1646c59fcb8983287a18cd48e867e82b6727385bd158de162ff33d02204d3f21d7f25b82a6dcccbb2618459cde38e111c17a108ad7c429456989c7e1020147304402201b98ac5d2ba09f6c15a5b3eb8952915b7d395c9b42a2cdd2b9137eebe4dffb6302205318408ce98ba300e4ef6996def4e4e3dd79b3b721a42da26ded1f80ff01db4601473044022024c6be015c952ba963524ae6bab9670770cd6b0109f00b115b4d1cb17c11fbe00220721fe476f99fc4ce3db9c2c95f7ef57b3534cd9b70e12dbd195f1dda1032da2001004d630164542102099fd69cf6a350679a05593c3ff814bfaa281eb6dde505c953cf2875979b120921022a159227df514c7b7808ee182ae07d71770b67eda1e5ee668272761eefb2c24c2102afc230c2d355b1a577682b07bc2646041b5d0177af0f98395a46018da699b6da21037ce55a309160df8c4b3baaa7a01e08e1f83c2a81907c45f0a3303c328fdc28062103ac2cc99a1a91b6f61e5323e7b3f442fa65bf8ad518ac3c0e26921e96f0e6126a2103e3929be608a537713c078c356af1c2a82720cc2e7f94718e589948dce5f27c602103fb8e1d5d0392d35ca8c3656acb6193dbf392b3e89b9b7b86693f5c80f7ce858157ae670350cd00b27552210216c23b2ea8e4f11c3f9e22711addb1d16a93964796913830856b568cc3ea21d3210275562901dd8faae20de0a4166362a4f82188db77dbed4ca887422ea1ec185f1421034db69f2112f4fb1bb6141bf6e2bd6631f0484d0bd95b16767902c9fe219d4a6f53ae68ffffffff0610ee0800000000001976a914ae0ffa08b80e91a3dd631b868816edc9e956984188acb0740a00000000001976a914ae0ffa08b80e91a3dd631b868816edc9e956984188ac10ee0800000000001976a914ae0ffa08b80e91a3dd631b868816edc9e956984188ac808e0700000000001976a914a278e6bc0735f7090f152d0d2a2af07e44d1480a88ac808e0700000000001976a914a278e6bc0735f7090f152d0d2a2af07e44d1480a88acbbc908000000000017a9146e4b5ae85d86e4db0e6e5db09f8c276328cdbf3f8700000000 +testnet 8200354 52f3bb86f5e3a5d703f611fd7b08aff3c59696ea1dbd7b44cc503497d6de78fb 0x640fc8dc585f2ff70b10105d7e01ae5e0cd9be80d428df6d8b07fed8fc1b8681 020000000001016800d89b51e4c35a65e36545eed84bdbef61f9a2344eeedecf79b20d70b9e70d0100000023220020a3a7d8d33d2e79ff2573480e1246b157afbbf5de6332084ec55c808f6f37869affffffff0100960700000000001976a9141e7eaf376db07dc88fbb56f05c22048d2e1b54c388ac0900483045022100d11dc61d0963fa420e070ce55a62217753069235a3d13704271fea1f3270f4bd02201404e1d3f73afb75be20463fb8ab7a0169e0b1cca3cfd195f0a25deb0f36be81014730440220178856ee46fa7e9d71079d6f9ec4c7ee2090a73c608821f9c5823e26271c82a90220379efb1452ae1528a9fb7ca1b94275486552457c7e1608b004b1764a4ccacc0501483045022100f7bf31d74f9fb133b6196090f46b45528dc92edcfe989ae24687419f67eb91850220310ee24ba93cb1c763857a4deab751c374bfd30d3955f787bd647f42f0081e0501473044022011ed1f0dac48eff23524ea5e75a9ff0b1c19009d5c5fc83fe5fc64e2a8154c130220593b620e05547777ff6b4d4c8b78d198e3ef14475b14fed0c9ffbc794eed35ad01483045022100d2f8e2fe68c67d8aa7ace60bc70b297aa2e3faf6f6f6b32b0c46a1f34e10fa7202206128872330f3b3bcc17d37c078531002e31bd9d2612f1bee0655dcfba541f6630147304402204b3083ff6f2dc568b6756d24c62174bc8f5f5ee1ec23442f364a2e5b6f55ec050220137385601222a4546f99595c8214b212a847e1cebb759c1267912983d0259cb60100fdeb0164562102099fd69cf6a350679a05593c3ff814bfaa281eb6dde505c953cf2875979b1209210222caa9b1436ebf8cdf0c97233a8ca6713ed37b5105bcbbc674fd91353f43d9f7210227e1a5773a58e9be74cd1eaa670150f3ca39bead7cf0fbb6d6d53faca6e6b45c21022a159227df514c7b7808ee182ae07d71770b67eda1e5ee668272761eefb2c24c210292039a22480290feccf059d7be7cb541cba6af97897526befbf3e411dfa9ef8d210297443877c0bc9e1aa572ff0ea7ad5ce396a4cfac48ca0104945de65b3b1e1c372102afc230c2d355b1a577682b07bc2646041b5d0177af0f98395a46018da699b6da2102b1645d3f0cff938e3b3382b93d2d5c082880b86cbb70b6600f5276f235c2839221036a65b700e8dbb4a8c01a022b19b41e8c3501d2e3d3dbaa1953d2e1412e4111c62103857b9b09e19ffe17e39dd0274266f005ebfb99778a467469ea0f37e5d4bb0c482103cac7e78da7ca46910b3326438a9e784e8b1af5a3253fe7bd6cfac914449390155bae670350cd00b27552210216c23b2ea8e4f11c3f9e22711addb1d16a93964796913830856b568cc3ea21d3210275562901dd8faae20de0a4166362a4f82188db77dbed4ca887422ea1ec185f1421034db69f2112f4fb1bb6141bf6e2bd6631f0484d0bd95b16767902c9fe219d4a6f53ae6800000000 +testnet 8220159 c2c05fc3579ae764fa57df3b90b55949dd3007d430758761797f174024095716 0xdc7ed1bce7896ccb14c81a6c90516b8ea96ac5fbafa2436c1ead4d257c250e44 02000000000101d94bac67db1b007e2dd4b444e9261e82a86b18459efa64af1e84d44bbdaffc130000000023220020d1b7cd229ac89ccf138e6708a8b55aeda73a99fa87a2e1136e9f57ddf8994669ffffffff02bcc50300000000001976a9140045e546433b1fff8cad41a6a501ea9716aa5e2688acd0dd06000000000017a9143bc37fdb6023d3a04e1d674a44d29d2ffbee8e318709004730440220347b6618d2772e67f1a332e337a697654977cc45424d7dc4596cba7331f68f7e0220501c67dc7c9aad99cce7521f30a6a21e20128e0db61cc2e2050bb3bed096cb1701483045022100cc772162ca09c7bb6175f3a6d497a28cf17ee1c9754e1132191e8fa60c52043f02200f34e13a67a2bf98117f4ae4296ea8fcc64c7bfeafc5ab4a132cae552f671217014830450221009a1fcb7e90bdf0fe6fd20984e4e6d1ad2cb15d1a29e6ebc79e8e5128ebbfd528022010cbdd5992b811883f593e25ac0205d2450ca28b788682eff2f950cdc31466210147304402204c207b0c34ca0e66769875bfb3764c4c96f94986a03bd66857988a6a4010bac10220530320b20ce9581fcbce4a356d2a7017bf70eb38f3daf401132efec4fa52e6de0148304502210092ad04a4d9f3ce0023a83877f07151002fa3f0f6fd201f9d7b5bc7be06d635810220193aa15557789c5a5a2d285b4c2a2688ae6bb8d601799b71abf6f530ec3eff0201463043021f566b5fc529147251a70c174608e8d56cc47c08c6e44522006b4f0c3e921bf6022040e9c68529f9f245c2b2d5fc918dfa72f5c7c22230a3dec3df128bfdecdf0d010100fd0d0220d56b131110cb82efd7c140270aef9f83e2f34a08290a7e186cc5949eb23341da7564562102099fd69cf6a350679a05593c3ff814bfaa281eb6dde505c953cf2875979b1209210222caa9b1436ebf8cdf0c97233a8ca6713ed37b5105bcbbc674fd91353f43d9f7210227e1a5773a58e9be74cd1eaa670150f3ca39bead7cf0fbb6d6d53faca6e6b45c21022a159227df514c7b7808ee182ae07d71770b67eda1e5ee668272761eefb2c24c210292039a22480290feccf059d7be7cb541cba6af97897526befbf3e411dfa9ef8d210297443877c0bc9e1aa572ff0ea7ad5ce396a4cfac48ca0104945de65b3b1e1c372102afc230c2d355b1a577682b07bc2646041b5d0177af0f98395a46018da699b6da2102b1645d3f0cff938e3b3382b93d2d5c082880b86cbb70b6600f5276f235c2839221036a65b700e8dbb4a8c01a022b19b41e8c3501d2e3d3dbaa1953d2e1412e4111c62103857b9b09e19ffe17e39dd0274266f005ebfb99778a467469ea0f37e5d4bb0c482103cac7e78da7ca46910b3326438a9e784e8b1af5a3253fe7bd6cfac914449390155bae670350cd00b27552210216c23b2ea8e4f11c3f9e22711addb1d16a93964796913830856b568cc3ea21d3210275562901dd8faae20de0a4166362a4f82188db77dbed4ca887422ea1ec185f1421034db69f2112f4fb1bb6141bf6e2bd6631f0484d0bd95b16767902c9fe219d4a6f53ae6800000000 From 70245e6c3977a4d63294ecf2ef32305b892aba08 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Tue, 10 Nov 2026 07:49:42 -0800 Subject: [PATCH 071/264] execution: below the pegout index, classify as rskj's legacy rules do Below the pegout index's Bitcoin height, rskj classifies a registered transaction with PegUtilsLegacy.getTransactionType. It asks isValidPegInTx first, and isMigrationTx only of what that refuses. rustock asked in another order and missed three of its rules. - isValidPegInTx refuses an input that spends the last retired federation, as it refuses one spending a live federation: by its script (scriptCorrectlySpendsTx, which hashes the scriptSig's last push, a P2SH-P2WSH input's witness program), or from RSKIP201 by its standard redeem script's legacy P2SH. Such a transaction, neither a migration nor a pegout, is left alone. rustock took it for a peg-in. - isValidPegInTx's own minimum decides first, from RSKIP379 as before. Below it the transaction is left alone with no events. rustock had evaluatePegin turn it down with two, and mark it processed where RSKIP459 is active without RSKIP551. - moveToActive is isValidPegInTx against the active federation, whose minimum from RSKIP293 is met by paying it nothing. A migration is only what isValidPegInTx refused, so a spend of a retired segwit federation, stored by its P2SH-P2WSH script, is a peg-in. None of this applies from the index's height. rustock matches an input to a federation by the scripts it carries, where bitcoinj runs them, signatures and all. They can differ only for an input whose federation redeem script Bitcoin never ran: a non-standard spend of some other script. Tests register on a regtest Bitcoin chain at heights 349 and 350, with the retired federation stored in each of its forms. --- crates/execution/src/bridge/peg.rs | 215 ++++++++++++++++++++++++----- 1 file changed, 180 insertions(+), 35 deletions(-) diff --git a/crates/execution/src/bridge/peg.rs b/crates/execution/src/bridge/peg.rs index 382ec86..2912a46 100644 --- a/crates/execution/src/bridge/peg.rs +++ b/crates/execution/src/bridge/peg.rs @@ -528,15 +528,42 @@ pub fn register_btc_transaction( || retiring_std_hash160.is_some_and(|t| input_matches_fed(&r, t)) }) }); - // (3) isMigrationTx: an input spends the retired or retiring federation AND - // an output funds the active federation (moveFromRetiringOrRetired && - // moveToActive). + // (3) isMigrationTx: an input spends the retired or retiring federation + // (moveFromRetiringOrRetired) and the transaction moves to the active + // one (moveToActive: isValidPegInTx against the active federation + // alone, whose minimum from RSKIP293 asks only that no UTXO to it be + // below it, so paying it nothing qualifies). let spends_retired_or_retiring = btc_tx.input.iter().any(|i| { input_redeem(i).is_some_and(|r| { retired_hash160.is_some_and(|t| input_matches_fed(&r, t)) || retiring_std_hash160.is_some_and(|t| input_matches_fed(&r, t)) }) }); + // (2) isValidPegInTx refuses a spend of the last retired federation as it + // refuses a live one's: by an input that spends its script + // (`scriptCorrectlySpendsTx`), or from RSKIP201 by one whose standard + // redeem script's legacy P2SH is it. bitcoinj runs the scriptSig, then + // the script: the scriptSig's last push must hash to it and, run as a + // redeem script, accept the signatures pushed before it, as a + // federation's own spends do. A P2SH-P2WSH input's last push is its + // witness program, which bitcoinj runs without the witness. A + // transaction refused that is neither a migration nor a pegout is left + // alone. + let rskip201 = hardfork_cfg.has_rskip201(rsk_height); + let spends_last_retired = retired_hash160.is_some_and(|retired| { + btc_tx.input.iter().any(|i| { + let last_push = super::release_tx::extract_redeem_script(i.script_sig.as_bytes()); + last_push.is_some_and(|script| redeem_script_hash160(&script) == retired) + || (rskip201 + && input_redeem(i).is_some_and(|r| { + let standard = build_federation_redeem_script( + &super::release_tx::spending_redeem_keys(&r), + super::release_tx::redeem_script_threshold(&r), + ); + redeem_script_hash160(&standard) == retired + })) + }) + }); let active_output_values: Vec = btc_tx .output .iter() @@ -544,7 +571,7 @@ pub fn register_btc_transaction( .map(|o| o.value.to_sat()) .collect(); let move_to_active = if hardfork_cfg.has_rskip293(rsk_height) { - !active_output_values.is_empty() && active_output_values.iter().all(|v| *v >= min_pegin) + active_output_values.iter().all(|v| *v >= min_pegin) } else { active_output_values.iter().sum::() >= min_pegin }; @@ -562,6 +589,28 @@ pub fn register_btc_transaction( .map(|o| o.value.to_sat()) .collect(); let total_value: u64 = live_output_values.iter().sum(); + // rskj PegUtilsLegacy.isValidPegInTx minimum gate: under RSKIP293 it checks + // EACH UTXO (isAnyUTXOAmountBelowMinimum), otherwise the total amount sent + // (valueSentToMe.isLessThan). Crucially, an EMPTY live-output set is not + // "below minimum" under RSKIP293 (anyMatch over an empty stream is false), + // so a tx that pays no live federation (e.g. only a now-retired one, once + // the retiring federation has been cleared earlier in the block) is still a + // valid peg-in — processed with amount 0 (pegin_btc amount=0, destination + // account created). Pre-RSKIP293 a 0 total is below minimum → not a peg-in. + // Below the index's height this gate decides whether the transaction is a + // peg-in at all, before RSKIP379 and after: one it turns down is neither a + // migration nor a pegout here, so rskj leaves it alone. + let not_a_peg_in = !pegout_index + && pegin_below_minimum( + &live_output_values, + min_pegin, + hardfork_cfg.has_rskip293(rsk_height), + ); + // rskj asks isMigrationTx only of a transaction isValidPegInTx refused, for + // an input or for this minimum. A spend of a retired segwit federation, + // stored by its P2SH-P2WSH script, matches neither input check, so it's a + // peg-in even where it moves to the active federation. + let refused_as_peg_in = spends_live_fed || spends_last_retired || not_a_peg_in; // rskj `PegUtils.getTransactionTypeUsingPegoutIndex`: a transaction whose // first input's sighash the index holds is a pegout or a migration, else one @@ -583,10 +632,13 @@ pub fn register_btc_transaction( } } else if tx_from_old_fed { Some("FROM_OLD_FEDERATION") - } else if is_migration { + } else if is_migration && refused_as_peg_in { Some("MIGRATION") } else if spends_live_fed { Some("PEGOUT") + } else if spends_last_retired { + tracing::debug!("registerBtcTransaction: spends the last retired federation and isn't a migration; not registered"); + return Ok(PrecompileOutput::new(gas_cost, Bytes::new())); } else { None }; @@ -614,14 +666,6 @@ pub fn register_btc_transaction( return Ok(PrecompileOutput::new(gas_cost, Bytes::new())); } - // rskj PegUtilsLegacy.isValidPegInTx minimum gate: under RSKIP293 it checks - // EACH UTXO (isAnyUTXOAmountBelowMinimum), otherwise the total amount sent - // (valueSentToMe.isLessThan). Crucially, an EMPTY live-output set is not - // "below minimum" under RSKIP293 (anyMatch over an empty stream is false), - // so a tx that pays no live federation (e.g. only a now-retired one, once - // the retiring federation has been cleared earlier in the block) is still a - // valid peg-in — processed with amount 0 (pegin_btc amount=0, destination - // account created). Pre-RSKIP293 a 0 total is below minimum → not a peg-in. let rskip379 = hardfork_cfg.has_rskip379(rsk_height); // RSKIP379 (Arrowhead600) moves the minimum-value gate to // `PegUtils.allUTXOsToFedAreAboveMinimumPeginValue`, which EXPLICITLY rejects @@ -629,15 +673,16 @@ pub fn register_btc_transaction( // empty (or any-below-minimum) set is below minimum — the OPPOSITE of the // pre-379 legacy rule where `anyMatch`/`!isAnyUTXOAmountBelowMinimum` is // vacuously satisfied by an empty set (a valid amount-0 peg-in, #5,171,600). - let below_min = if rskip379 { - live_output_values.is_empty() || live_output_values.iter().any(|&v| v < min_pegin) - } else { - pegin_below_minimum(&live_output_values, min_pegin, hardfork_cfg.has_rskip293(rsk_height)) - }; + // It applies to every peg-in, whichever rules classified it. + let evaluated = rskip379 && !not_a_peg_in; + let below_min = not_a_peg_in + || (evaluated + && (live_output_values.is_empty() + || live_output_values.iter().any(|&v| v < min_pegin))); if below_min { - // Pre-RSKIP379: rskj getTransactionType returns UNKNOWN (neither a + // Not a peg-in: rskj getTransactionType returns UNKNOWN (neither a // migration nor a peg-out), so registerBtcTransaction ignores it silently. - // Post-RSKIP379: evaluatePegin returns (NO_REFUND, INVALID_AMOUNT), and + // A peg-in from RSKIP379: evaluatePegin returns (NO_REFUND, INVALID_AMOUNT), and // BridgeSupport.handleNonRefundablePegin logs rejected_pegin(INVALID_AMOUNT=5) // then unrefundable_pegin(NonRefundablePeginReason.INVALID_AMOUNT=3); it // marks the tx processed only when shouldMarkRejectedPeginAsProcessed @@ -651,17 +696,17 @@ pub fn register_btc_transaction( vec![ ("minimumSat", FieldValue::U64(min_pegin)), ("fedOutputs", FieldValue::U64(live_output_values.len() as u64)), - // Whether the chain also logged this. Before RSKIP379 it did - // not, which is the gap these records exist to fill. - ("logged", FieldValue::Flag(rskip379)), + // Whether the chain also logged this. Where it isn't a peg-in it + // did not, which is the gap these records exist to fill. + ("logged", FieldValue::Flag(evaluated)), ], ); - if rskip379 { + if evaluated { let hash = btc_txid_event_bytes(&btc_tx); super::events::log_rejected_pegin(ctx, &hash, 5); super::events::log_unrefundable_pegin(ctx, &hash, 3); } - if should_mark_rejected_pegin_as_processed(hardfork_cfg, rsk_height) { + if evaluated && should_mark_rejected_pegin_as_processed(hardfork_cfg, rsk_height) { set_btc_tx_processed(ctx, &legacy_txid, rsk_height, hardfork_cfg.has_rskip134(rsk_height)); } return Ok(PrecompileOutput::new(gas_cost, Bytes::new())); @@ -8844,17 +8889,13 @@ mod tests { /// `registerBtcTransaction` of `tx` at Bitcoin `height` on RSKj's regtest, /// with `pegout_sig_hash` in the pegout index and `svp_fund_hash` as the - /// unsigned SVP fund transaction's hash. Answers whether the call - /// succeeded, whether it marked the transaction processed, how many events - /// it logged, and how many UTXOs the active federation then holds. + /// unsigned SVP fund transaction's hash. Answers as [`outcome`]. fn registered( tx: &BtcTransaction, height: i64, pegout_sig_hash: Option<[u8; 32]>, svp_fund_hash: Option<[u8; 32]>, ) -> (bool, bool, usize, usize) { - let config = BridgeConstants::rskj_regtest(); - let cfg = RskHardforkConfig::rskj_regtest(); let mut ctx = regtest_bridge_with(tx); if let Some(sig_hash) = pegout_sig_hash { save_pegout_tx_sig_hash(&mut ctx, &sig_hash); @@ -8866,6 +8907,19 @@ mod tests { &rlp_encode_element(&hash), ); } + outcome(&mut ctx, tx, height, &RskHardforkConfig::rskj_regtest()) + } + + /// `registerBtcTransaction` of `tx` at Bitcoin `height` in `ctx`, with RSKj's + /// regtest constants and `cfg`. Answers whether the call succeeded, whether + /// it marked the transaction processed, how many events it logged, and how + /// many UTXOs the active federation then holds. + fn outcome( + ctx: &mut CTX, + tx: &BtcTransaction, + height: i64, + cfg: &RskHardforkConfig, + ) -> (bool, bool, usize, usize) { let raw = btc_serialize(tx); let tree = one_leaf_tree(&raw); let args = encode::args(&[ @@ -8874,17 +8928,17 @@ mod tests { Arg::Bytes(&tree), ]); let ok = register_btc_transaction( - &mut ctx, + ctx, &args, 0, - &config, - &cfg, + &BridgeConstants::rskj_regtest(), + cfg, &BridgeTxContext::default(), ) .is_ok(); let logs = ctx.journal_mut().take_logs().len(); - let processed = is_btc_tx_processed(&mut ctx, &legacy_btc_txid(tx)); - let utxos = super::super::storage::load_federation_utxos(&mut ctx, &cfg, 10).len(); + let processed = is_btc_tx_processed(ctx, &legacy_btc_txid(tx)); + let utxos = super::super::storage::load_federation_utxos(ctx, cfg, 10).len(); (ok, processed, logs, utxos) } @@ -9000,4 +9054,95 @@ mod tests { } assert_eq!((read, segwit), (8, 4)); } + + /// Below the index's height, `isValidPegInTx` refuses a spend of the last + /// retired federation as it refuses a live one's, and `isMigrationTx`, + /// asked next, knows the retired federation by its standard redeem script. + /// Stored by its whole ERP redeem script, as before RSKIP377, the retired + /// federation's spend is neither, and is left alone. Stored by its standard + /// redeem script, the spend is a migration, from RSKIP293 even when it pays + /// the active federation nothing. A segwit one, stored by the P2SH-P2WSH + /// script of its standard redeem script, isn't refused, so its spend is a + /// peg-in; stored by its whole redeem script's, it's refused, as bitcoinj + /// matches the scriptSig's witness program to it, and its spend is left alone. + #[test] + fn below_the_index_a_spend_of_the_last_retired_federation_is_no_peg_in() { + let keys = test_keys(0x10); + let standard = build_federation_redeem_script(&keys, 2); + let erp = build_erp_redeem_script(&keys, 2, &test_keys(0x40), 2, 52_560, true); + let with_retired = |retired: &[u8; 20], tx: &BtcTransaction| { + let mut ctx = regtest_bridge_with(tx); + let script = p2sh_output_script(retired).into_bytes(); + bridge_store_bytes_named( + &mut ctx, + super::super::storage::LAST_RETIRED_FEDERATION_P2SH_SCRIPT_KEY, + &rlp_encode_list(&[rlp_encode_element(&script)]), + ); + outcome(&mut ctx, tx, 349, &RskHardforkConfig::rskj_regtest()) + }; + let to_federation = (1_000_000_000, regtest_federation_script()); + let paying_nothing = spend( + vec![signed_multisig(&erp)], + &[(40_000, p2sh_output_script(&[0x77; 20]))], + ); + let migrating = spend( + vec![signed_multisig(&erp)], + std::slice::from_ref(&to_federation), + ); + // With no federation retired, both are peg-ins. + assert_eq!( + registered(&paying_nothing, 349, None, None), + (true, false, 2, 0) + ); + assert_eq!(registered(&migrating, 349, None, None), (true, true, 2, 0)); + let whole = redeem_script_hash160(&erp); + assert_eq!(with_retired(&whole, &paying_nothing), (true, false, 0, 0)); + assert_eq!(with_retired(&whole, &migrating), (true, false, 0, 0)); + let members = redeem_script_hash160(&standard); + assert_eq!(with_retired(&members, &paying_nothing), (true, true, 0, 0)); + assert_eq!(with_retired(&members, &migrating), (true, true, 0, 1)); + let (script_sig, _) = super::super::release_tx::segwit_base_input(&erp, 2); + let witness = bitcoin::Witness::from_slice(&[ + vec![], + vec![0x30; 71], + vec![0x31; 72], + vec![], + erp.clone(), + ]); + let segwit = spend(vec![(script_sig, witness)], &[to_federation]); + let peg_in = registered(&segwit, 349, None, None); + assert!(peg_in.0); + assert_eq!( + with_retired(&federation_output_hash160(&erp, 4000), &segwit), + (true, false, 0, 0) + ); + assert_eq!( + with_retired(&federation_output_hash160(&standard, 4000), &segwit), + peg_in + ); + } + + /// Below the index's height `isValidPegInTx`'s own minimum decides whether a + /// transaction is a peg-in: from RSKIP293 one UTXO to a live federation + /// below it makes it none, and it's left alone. From the index's height + /// every transaction paying a live federation is a peg-in, and + /// `evaluatePegin` turns a small one down with its two events. Where rskj + /// marks a rejected peg-in processed (RSKIP459 without RSKIP551), it marks + /// only that one. + #[test] + fn below_the_index_a_small_utxo_makes_no_peg_in() { + let redeem = build_federation_redeem_script(&test_keys(0x10), 2); + let tx = spend( + vec![signed_multisig(&redeem)], + &[(1, regtest_federation_script())], + ); + assert_eq!(registered(&tx, 349, None, None), (true, false, 0, 0)); + assert_eq!(registered(&tx, 350, None, None), (true, false, 2, 0)); + let marking = RskHardforkConfig::rskj_regtest() + .with_hardfork(crate::hardfork::RskNetworkUpgrade::Vetiver900, None); + assert!(should_mark_rejected_pegin_as_processed(&marking, 10)); + let marked = |height| outcome(&mut regtest_bridge_with(&tx), &tx, height, &marking); + assert_eq!(marked(349), (true, false, 0, 0)); + assert_eq!(marked(350), (true, true, 2, 0)); + } } From 13229cab11e2b4f321ef510016a40f1292e44680 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Tue, 10 Nov 2026 07:59:25 -0800 Subject: [PATCH 072/264] sync: give every block of a forward run its own total difficulty rskj's BlockChainImpl gives every block it connects a total difficulty, its parent's plus its own and its uncles'. The BlockHeader contract's getCumulativeWork reads it for any of the last 4,000 blocks and stops the node at a block without one. The forward sync wrote the total of each run's newest block only. --- crates/sync/src/snap/forward.rs | 46 ++++++++++++++++----- crates/sync/src/snap/forward_tests.rs | 59 +++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 10 deletions(-) diff --git a/crates/sync/src/snap/forward.rs b/crates/sync/src/snap/forward.rs index d483a34..d5af0d1 100644 --- a/crates/sync/src/snap/forward.rs +++ b/crates/sync/src/snap/forward.rs @@ -106,6 +106,13 @@ struct Run { /// rather than when it arrives: a run that has not linked yet is still /// only a peer's word, and writing it would publish an unproven chain. blocks: Vec<(u64, B256)>, + /// What each of `blocks` adds to the chain's work, in the same order: its + /// own difficulty and its proven uncles', rskj's + /// `Block.getCumulativeDifficulty`. + /// + /// Kept so that the commit can give every block its own total, as rskj + /// gives every block it stores one. + difficulties: Vec, } /// Ascends from an anchor toward a target height, proving the work as it goes. @@ -354,16 +361,29 @@ impl ForwardSync { out } - /// Write a consumed run to the canonical index, and move the head and the - /// accumulated work with it. + /// Write a consumed run to the canonical index with each block's total + /// difficulty, and move the head with it. + /// + /// `below` is the total of the block the run builds on. Each block's total + /// is its parent's plus what it adds, as rskj's + /// `BlockChainImpl.internalTryToConnect` gives every block it connects one + /// (`parentTotalDifficulty.add(block.getCumulativeDifficulty())`). The + /// BlockHeader contract's `getCumulativeWork` reads it for any of the last + /// 4,000 blocks, and stops the node at a block whose total is missing: + /// writing the newest block's alone left every other block without one. /// /// The head is what `SnapSession::anchor` reads on the next start, so this - /// is the whole of resuming. The work is recorded against the new head for - /// the same reason: it is the figure the ascent would otherwise have to - /// re-derive by walking from genesis again. - fn commit(&self, run: &Run) { - for (number, hash) in &run.blocks { - if let Err(e) = self.store.put_canonical_hash(*number, *hash) { + /// is the whole of resuming. The newest block's total is the figure the + /// ascent would otherwise have to re-derive by walking from genesis again. + fn commit(&self, run: &Run, below: U256) { + let mut total = below; + for ((number, hash), difficulty) in run.blocks.iter().zip(&run.difficulties) { + total = total.saturating_add(*difficulty); + if let Err(e) = self + .store + .put_canonical_hash(*number, *hash) + .and_then(|()| self.store.put_total_difficulty(*hash, total)) + { warn!( target: "rustock::snap", "forward sync: could not index #{number}: {e}" @@ -400,7 +420,6 @@ impl ForwardSync { } } - let _ = self.store.put_total_difficulty(run.newest, self.work); if let Err(e) = self.store.set_head(run.newest) { warn!(target: "rustock::snap", "forward sync: could not move the head: {e}"); } @@ -511,6 +530,11 @@ impl ForwardSync { let mut uncle_headers: Vec<(u64, Vec
)> = entries.iter().map(|e| (e.header.number, e.uncles.clone())).collect(); uncle_headers.reverse(); + let mut difficulties: Vec = entries + .iter() + .map(HeaderWithUncles::cumulative_difficulty) + .collect(); + difficulties.reverse(); self.satisfied.insert(point); self.runs.insert( oldest.number, @@ -522,6 +546,7 @@ impl ForwardSync { work, uncles, blocks, + difficulties, uncle_headers, }, ); @@ -600,6 +625,7 @@ impl ForwardSync { return; } let run = self.runs.remove(&(self.have_number + 1)).expect("just read"); + let below = self.work; self.work = self.work.saturating_add(run.work); self.counted_uncles.extend(run.uncles.iter().copied()); self.have_number = run.newest_number; @@ -622,7 +648,7 @@ impl ForwardSync { // every run that links is permanently true, and writing it means a // sync that is interrupted resumes from where it stopped instead // of starting again at genesis. - self.commit(&run); + self.commit(&run, below); self.points.remove(&run.newest_number); self.satisfied.remove(&run.newest_number); diff --git a/crates/sync/src/snap/forward_tests.rs b/crates/sync/src/snap/forward_tests.rs index 1fa700f..1d4a1e4 100644 --- a/crates/sync/src/snap/forward_tests.rs +++ b/crates/sync/src/snap/forward_tests.rs @@ -435,6 +435,65 @@ fn a_consumed_run_is_committed_so_the_next_start_resumes() { ); } +/// Every block of a committed run gets its own total difficulty: its parent's +/// plus its own difficulty and its uncles', as rskj's +/// `BlockChainImpl.internalTryToConnect` gives every block it connects. The +/// BlockHeader contract's `getCumulativeWork` reads it for any of the last +/// 4,000 blocks and stops the node at a block without one, and the commit used +/// to write the newest block's alone. +#[test] +fn every_committed_block_gets_its_own_total() { + let dir = tempfile::tempdir().expect("tempdir"); + let store = Arc::new(BlockStore::open(dir.path()).expect("open")); + let anchor = header(0, B256::ZERO, 100); + + let mut s = ForwardSync::new( + 0, + anchor.hash(), + U256::from(1_000), + 8, + B256::repeat_byte(0xff), + true, + store.clone(), + Arc::new(HeaderVerifier::new()), + ); + + // An uncle worth 50 on #2 and one worth 30 on #6. The upper run arrives + // first and waits, so both are consumed in one pass, each from where the + // one below it ended. + let lower = run_of(1, 4, anchor.hash(), 100, |n| { + if n == 2 { + vec![header(1, B256::repeat_byte(9), 50)] + } else { + Vec::new() + } + }); + let upper = run_of(5, 4, lower[0].header.hash(), 100, |n| { + if n == 6 { + vec![header(5, B256::repeat_byte(7), 30)] + } else { + Vec::new() + } + }); + s.on_headers_with_uncles(8, &upper) + .expect("internally valid"); + assert_eq!(s.frontier(), 0, "the upper run waits for the lower one"); + s.on_headers_with_uncles(4, &lower).expect("honest run"); + assert_eq!(s.frontier(), 8); + + let mut blocks: Vec<&Header> = lower.iter().chain(&upper).map(|e| &e.header).collect(); + blocks.sort_by_key(|h| h.number); + let totals = [1_100u64, 1_250, 1_350, 1_450, 1_550, 1_680, 1_780, 1_880]; + for (block, total) in blocks.into_iter().zip(totals) { + assert_eq!( + store.total_difficulty(block.hash()).unwrap(), + Some(U256::from(total)), + "#{} carries its parent's total plus its own work", + block.number + ); + } +} + /// A run that has not linked yet is *not* committed. /// /// Until it links it is only a peer's word, and publishing it would put an From b29eb51caf6f2006034bc2a1ac07480a982686bb Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Tue, 10 Nov 2026 07:59:25 -0800 Subject: [PATCH 073/264] sync: keep the forward ascent going when a peer's skeleton stops short The chunk under the target is asked for from the target's own hash, whatever its alignment, so an ascent whose peers are still behind the target has a run to bridge down from (upstream #262). A bare header answer during the ascent is handed back to be asked again, where it left the range marked as asked and the ascent stopped there. --- crates/sync/src/snap/forward.rs | 34 ++++++--- crates/sync/src/snap/forward_tests.rs | 105 ++++++++++++++++++++++++-- crates/sync/src/snap/session.rs | 11 +++ crates/sync/src/snap/session_tests.rs | 35 +++++++++ 4 files changed, 167 insertions(+), 18 deletions(-) diff --git a/crates/sync/src/snap/forward.rs b/crates/sync/src/snap/forward.rs index d5af0d1..78b8b77 100644 --- a/crates/sync/src/snap/forward.rs +++ b/crates/sync/src/snap/forward.rs @@ -328,19 +328,25 @@ impl ForwardSync { } } - // The stretch above the last grid point, which no skeleton identifier - // covers. Asked for from the target's own hash, the one hash that is - // known without a skeleton. Without this the ascent can never satisfy - // `have_number >= target` for a target off the grid, and stalls at - // 99.9% having done all the work. - let grid_top = (self.target / HEADER_CHUNK) * HEADER_CHUNK; - if self.target > grid_top - && self.have_number < self.target + // The chunk under the target, asked for from the target's own hash: the + // one hash above the prefix that is known without a skeleton. + // + // Off the grid, no skeleton identifier names the target, and without + // this the ascent could never satisfy `have_number >= target`: it + // stalled at 99.9% having done all the work. On the grid, a skeleton + // names it only if the peer answering reaches that far. A peer still + // behind the target ends its skeleton at its own best block instead, + // and the ascent was then left with no point above the prefix, no run + // to bridge down from, and nothing to ask for. Asked for whatever its + // alignment, the run under the target is always there to bridge down + // from. + if self.have_number < self.target && out.len() < budget && !self.headers_sent.contains(&self.target) && !self.satisfied.contains(&self.target) { - let count = (self.target - grid_top).min(self.target - self.have_number); + let below = ((self.target - 1) / HEADER_CHUNK) * HEADER_CHUNK; + let count = (self.target - below).min(self.target - self.have_number); self.headers_sent.insert(self.target); out.push(Want::Headers { from: self.target_hash, @@ -455,12 +461,16 @@ impl ForwardSync { self.skeleton_todo.push(*start); } } - Want::Headers { point, .. } => { - self.headers_sent.remove(point); - } + Want::Headers { point, .. } => self.release_headers(*point), } } + /// Hand back the header request for `point`, so it is asked again: for one + /// nobody answered, and for an answer this walk cannot use. + pub fn release_headers(&mut self, point: u64) { + self.headers_sent.remove(&point); + } + /// Skeleton identifiers: where to ask, and nothing more. pub fn on_skeleton(&mut self, identifiers: &[BlockIdentifier]) { for id in identifiers { diff --git a/crates/sync/src/snap/forward_tests.rs b/crates/sync/src/snap/forward_tests.rs index 1d4a1e4..e04a452 100644 --- a/crates/sync/src/snap/forward_tests.rs +++ b/crates/sync/src/snap/forward_tests.rs @@ -293,18 +293,45 @@ fn a_target_off_the_grid_is_still_asked_for() { } } -/// A target *on* the grid needs no bespoke chunk: the skeleton names it. +/// A target *on* the grid is asked for by its own hash too: a skeleton names it +/// only if the peer answering reaches that far. #[test] -fn a_target_on_the_grid_gets_no_bespoke_chunk() { +fn a_target_on_the_grid_is_asked_for_by_its_own_hash_too() { let anchor = header(0, B256::ZERO, 100); let target = HEADER_CHUNK * 3; let (mut s, _d) = sync(&anchor, target, HeaderVerifier::new()); - let asked_by_hash = s + let by_hash: Vec = s .wants(8) .into_iter() - .any(|w| matches!(w, Want::Headers { from, .. } if from == B256::repeat_byte(0xff))); - assert!(!asked_by_hash, "nothing above the grid, so nothing to ask for by hash"); + .filter(|w| matches!(w, Want::Headers { from, .. } if *from == B256::repeat_byte(0xff))) + .collect(); + assert_eq!( + by_hash, + vec![Want::Headers { + from: B256::repeat_byte(0xff), + count: HEADER_CHUNK as u32, + point: target, + }], + "the whole chunk under the target, from the target's own hash" + ); + + // Once answered it waits for the prefix, and is not asked for again. + let top = run_of( + HEADER_CHUNK * 2 + 1, + HEADER_CHUNK as usize, + B256::repeat_byte(0xaa), + 100, + |_| Vec::new(), + ); + s.on_headers_with_uncles(target, &top) + .expect("internally valid"); + assert!( + !s.wants(8) + .iter() + .any(|w| matches!(w, Want::Headers { point, .. } if *point == target)), + "an answered chunk is not asked for again while it waits" + ); } /// A request nobody answers is asked again after it is released. @@ -318,8 +345,12 @@ fn a_released_request_is_asked_again() { let (mut s, _d) = sync(&anchor, HEADER_CHUNK * 4, HeaderVerifier::new()); s.on_skeleton(&[BlockIdentifier { number: HEADER_CHUNK, hash: B256::repeat_byte(1) }]); + // The point the skeleton named. The chunk under the target is asked for + // too, from the target's own hash, and is not what this is about. let headers_in = |ws: Vec| -> Vec { - ws.into_iter().filter(|w| matches!(w, Want::Headers { .. })).collect() + ws.into_iter() + .filter(|w| matches!(w, Want::Headers { point, .. } if *point == HEADER_CHUNK)) + .collect() }; let first = headers_in(s.wants(8)); @@ -595,6 +626,68 @@ fn a_gap_the_skeleton_missed_is_bridged_from_the_run_above() { ); } +/// A peer still behind the target does not stop the ascent. +/// +/// Its skeleton ends at its own best block, below the target, so nothing names +/// a block above that, and the run asked for from the best block itself lies +/// across the grid the prefix climbs by. With the target on the grid the ascent +/// was then left with no point above its prefix, no run to bridge down from, +/// and nothing to ask for. The chunk under the target is asked for from the +/// target's own hash, and the ascent bridges down from it. +#[test] +fn a_peer_behind_the_target_does_not_stop_the_ascent() { + let anchor = header(0, B256::ZERO, 100); + // On the grid. The chain is newest first: `chain[k]` is block #target - k. + let target = HEADER_CHUNK * 4; + let chain = run_of(1, target as usize, anchor.hash(), 100, |_| Vec::new()); + let at = |number: u64| chain[(target - number) as usize].header.hash(); + let dir = tempfile::tempdir().expect("tempdir"); + let store = Arc::new(BlockStore::open(dir.path()).expect("open")); + let mut s = ForwardSync::new( + 0, + anchor.hash(), + U256::ZERO, + target, + at(target), + true, + store, + Arc::new(HeaderVerifier::new()), + ); + + // The peer's best block is #400: its skeleton names the grid points under + // it and then #400 itself, as rskj's `processSkeletonRequest` builds one. + let skeleton: Vec = [HEADER_CHUNK, 2 * HEADER_CHUNK, 400] + .into_iter() + .map(|number| BlockIdentifier { + number, + hash: at(number), + }) + .collect(); + + for _ in 0..32 { + let wants = s.wants(8); + if wants.is_empty() { + break; + } + for want in wants { + match want { + Want::Skeleton { .. } => s.on_skeleton(&skeleton), + Want::Headers { from, count, point } => { + let newest = chain + .iter() + .position(|e| e.header.hash() == from) + .expect("a block of the chain"); + let end = (newest + count as usize).min(chain.len()); + s.on_headers_with_uncles(point, &chain[newest..end]) + .expect("honest run"); + } + } + } + } + assert_eq!(s.frontier(), target, "the ascent reaches the target"); + assert!(s.done()); +} + /// The uncle-reuse ledger is a window, not a ledger. /// /// Consensus lets a block reference an uncle only within diff --git a/crates/sync/src/snap/session.rs b/crates/sync/src/snap/session.rs index 28eeb51..a62e4f5 100644 --- a/crates/sync/src/snap/session.rs +++ b/crates/sync/src/snap/session.rs @@ -950,6 +950,17 @@ impl SnapSession { } pub fn on_headers(&mut self, point: u64, headers: &[Header]) -> Vec { + if self.phase == Phase::AscendingHeaders { + // The ascent asked for these with their uncles, and totals the work + // exactly, so bare headers are no answer to it. Hand the range back + // to be asked again. The driver has already struck the request off, + // so nothing would time it out, and left marked as asked it was + // never asked again: one bare answer stopped the ascent there. + if let Some(ascent) = self.ascent.as_mut() { + ascent.release_headers(point); + } + return self.poll(); + } self.ingest_headers(point, headers, &std::collections::HashMap::new()) } diff --git a/crates/sync/src/snap/session_tests.rs b/crates/sync/src/snap/session_tests.rs index 1a69073..c48fef4 100644 --- a/crates/sync/src/snap/session_tests.rs +++ b/crates/sync/src/snap/session_tests.rs @@ -2048,6 +2048,41 @@ mod ascent_tests { "a peer that could serve the ascent does not by itself select it" ); } + + /// A bare header answer during the ascent hands its range back. + /// + /// The ascent asks for headers with their uncles, and the driver strikes a + /// request off as soon as any header answer to it arrives. Bare headers + /// are no use to the ascent, and they were dropped with the range still + /// marked as asked: nothing timed it out and nothing asked again, so one + /// stray answer stopped the ascent at that height. + #[test] + fn a_bare_header_answer_during_the_ascent_is_asked_again() { + let (mut session, f) = session_with(true, true); + let offered = block(header(5_000, B256::ZERO, f.state_root, 100)); + let asks_for_the_top = |actions: &[Action]| { + actions + .iter() + .any(|a| matches!(a, Action::RequestHeaders { point: 5_000, .. })) + }; + let first = session.on_status( + std::slice::from_ref(&offered), + &[U256::from(1_000u64)], + 1024, + 0, + ); + assert_eq!(session.phase(), Phase::AscendingHeaders); + assert!( + asks_for_the_top(&first), + "the chunk under the target: {first:?}" + ); + + let again = session.on_headers(5_000, &[]); + assert!( + asks_for_the_top(&again), + "asked for again after a bare answer: {again:?}" + ); + } } /// The gate and the ascent coexist: passing the gate must land in the phase the From d54ce9e5400420adfdb4d5d96ef99108cda80e90 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Tue, 10 Nov 2026 07:59:25 -0800 Subject: [PATCH 074/264] sync: store a snapshot block's own header beside its body From RSKIP351 the walk's copy of a header came compressed, holding only the commitment to the block's extension. rskj stores snapshot blocks whole (SnapshotProcessor.processSnapBlocksResponse); the hash checked commits to the extension, so nothing unchecked comes with it. --- crates/sync/src/snap/session.rs | 9 ++ crates/sync/src/snap/session_tests.rs | 129 ++++++++++++++++++++++++++ crates/sync/src/tests.rs | 6 +- 3 files changed, 142 insertions(+), 2 deletions(-) diff --git a/crates/sync/src/snap/session.rs b/crates/sync/src/snap/session.rs index a62e4f5..9363bf4 100644 --- a/crates/sync/src/snap/session.rs +++ b/crates/sync/src/snap/session.rs @@ -1406,6 +1406,15 @@ impl SnapSession { return self.fail(why); } + // The header with it, as the block carries it. From RSKIP-351 on the + // walk's copy came compressed and holds only the commitment to the + // block's extension: its logs bloom, execution edges and base + // event. rskj stores these blocks whole + // (`SnapshotProcessor.processSnapBlocksResponse`, then + // `BlockConnectorHelper.startConnecting`'s `saveBlock`). The hash + // checked above commits to the extension, so nothing unchecked + // comes with it. + let _ = self.store.put_header_with_hash(hash, &block.header); let _ = self.store.put_body(hash, &block.transactions, &block.ommers); let _ = self.store.put_total_difficulty(hash, *td); oldest = oldest.min(block.header.number); diff --git a/crates/sync/src/snap/session_tests.rs b/crates/sync/src/snap/session_tests.rs index c48fef4..23fb60f 100644 --- a/crates/sync/src/snap/session_tests.rs +++ b/crates/sync/src/snap/session_tests.rs @@ -663,6 +663,135 @@ fn a_body_that_does_not_match_its_header_is_refused() { ); } +/// A snapshot's blocks below its checkpoint complete the walk's compressed +/// headers. +/// +/// From RSKIP-351 a chain's headers come compressed: a version-1 or version-2 +/// header holds only the commitment to its extension, the block's logs bloom, +/// execution edges and base event, and the walk stores it so. The blocks +/// below the checkpoint come whole, and rskj stores each of them whole +/// (`SnapshotProcessor.processSnapBlocksResponse`, then +/// `BlockConnectorHelper.startConnecting`'s `saveBlock`). This node kept the +/// walk's compressed header beside the body: no bloom to answer with, and no +/// extension to send beside the body it serves. +#[test] +fn the_blocks_below_the_checkpoint_complete_the_walks_compressed_headers() { + use crate::tests::{an_extension, compressed}; + use rustock_core::config::ActivationHeights; + + // Stores that read headers as rskj's regtest does: RSKIP-351 from block 1. + let regtest_store = |dir: &tempfile::TempDir| { + Arc::new( + BlockStore::open(dir.path()) + .expect("open") + .with_activations(ActivationHeights::regtest()), + ) + }; + let mut f = fixture(20); + let dir = tempfile::tempdir().expect("tempdir"); + f.store = regtest_store(&dir); + f._dir = dir; + let genesis = f.with_genesis(); + + // Each block whole, and its header as the walk is sent it. + let mut parent = genesis; + let (mut whole, mut walked, mut tds) = (Vec::new(), Vec::new(), Vec::new()); + for number in 1..=3u64 { + let extension = an_extension(number as usize); + let sent = compressed(header(number, parent, f.state_root, 100), &extension); + let full = sent + .with_extension(&extension) + .expect("the extension it names"); + assert_eq!(full.hash(), sent.hash(), "both forms hash alike"); + parent = sent.hash(); + walked.insert(0, sent); + whole.push(block(full)); + tds.push(U256::from(100 + 100 * number)); + } + + // The status carries the checkpoint, whole; the walk brings the headers + // under it, compressed. + let mut session = f.session(HeaderVerifier::new()); + session.on_status(std::slice::from_ref(&whole[2]), &tds[2..], 5_000, 0); + let started = session.on_headers(3, &walked); + assert_eq!( + session.phase(), + Phase::DownloadingState, + "{:?}", + session.failure() + ); + for b in &whole[..2] { + let held = f.store.header(b.header.hash()).unwrap().expect("walked"); + assert_eq!( + held.extension(), + None, + "the walk holds #{} compressed", + b.header.number + ); + } + + let peer_dir = tempfile::tempdir().expect("tempdir"); + let peer_store = regtest_store(&peer_dir); + for (b, td) in whole.iter().zip(tds.iter()) { + let hash = b.header.hash(); + peer_store + .put_header_with_hash(hash, &b.header) + .expect("header"); + peer_store + .put_canonical_hash(b.header.number, hash) + .expect("index"); + peer_store.put_total_difficulty(hash, *td).expect("td"); + } + let server = SnapServer::new( + peer_store, + f.trie.clone() as Arc, + SnapConfig { + server_enabled: true, + ..SnapConfig::default() + }, + ); + let mut actions = started; + let mut guard = 0; + while session.phase() == Phase::DownloadingState { + guard += 1; + assert!(guard < 1000, "state download stalled"); + if actions.is_empty() { + actions = session.poll(); + assert!(!actions.is_empty(), "no work and not finished"); + } + if let Action::RequestChunk { from, budget, .. } = actions.remove(0) { + let response = server + .chunk(&SnapChunkRequest { + id: 1, + block_number: 3, + from, + chunk_size: budget, + state_root: Some(f.state_root), + }) + .expect("server answers"); + actions.extend(session.on_chunk(from, &response.payload, Refusal::None)); + } + } + assert_eq!( + session.phase(), + Phase::DownloadingBlocks, + "{:?}", + session.failure() + ); + + session.on_blocks(&whole[..2], &tds[..2]); + assert_eq!(session.phase(), Phase::Done, "{:?}", session.failure()); + for (b, bit) in whole[..2].iter().zip(1..) { + let held = f.store.header(b.header.hash()).unwrap().expect("held"); + assert_eq!( + held.extension(), + Some(an_extension(bit)), + "#{} is held whole, with the extension its hash commits to", + b.header.number + ); + } +} + // ------------------------------------------------------- blaming the peer use alloy_primitives::B512; diff --git a/crates/sync/src/tests.rs b/crates/sync/src/tests.rs index 3ce92f0..6f7faeb 100644 --- a/crates/sync/src/tests.rs +++ b/crates/sync/src/tests.rs @@ -7618,7 +7618,9 @@ async fn a_body_from_a_peer_that_was_not_asked_is_ignored() { /// are empty, as rskj emits them on every versioned header while RSKIP144 is /// active and the block has no sublists: a header given it has the fields /// rskj reads at regtest's heights. -fn an_extension(bit: usize) -> rustock_core::types::header_extension::BlockHeaderExtension { +pub(crate) fn an_extension( + bit: usize, +) -> rustock_core::types::header_extension::BlockHeaderExtension { let mut logs_bloom = vec![0u8; 256]; logs_bloom[bit / 8] |= 0x80 >> (bit % 8); rustock_core::types::header_extension::BlockHeaderExtension::V2 { @@ -7633,7 +7635,7 @@ fn an_extension(bit: usize) -> rustock_core::types::header_extension::BlockHeade /// reads at regtest's heights and the merged-mining fields every versioned /// header has, and hashed as regtest's heights say: without the /// merged-mining proof and coinbase (RSKIP-92). -fn compressed( +pub(crate) fn compressed( header: Header, extension: &rustock_core::types::header_extension::BlockHeaderExtension, ) -> Header { From ca2a87bbff6e68525d2c10be895f0ff88722d88b Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Tue, 10 Nov 2026 07:59:25 -0800 Subject: [PATCH 075/264] sync: keep the uncles of every block whose body a sync doesn't fetch The uncles sent beside the headers were archived only below the freeze horizon, 20,000 blocks under the target, though bodies come only for the last 6,000 (rskj's BLOCKS_REQUIRED), so the uncles between were lost. They're now kept below the body window, and rsk/63 serves uncles from the freezer once a body is gone (upstream #270). --- crates/sync/src/handler.rs | 18 +++--- crates/sync/src/snap/forward.rs | 28 ++++++++-- crates/sync/src/snap/headers.rs | 38 ++++++++++--- crates/sync/src/snap/session.rs | 39 ++++++++----- crates/sync/src/snap/session_tests.rs | 80 +++++++++++++++++++++++++++ crates/sync/src/tests.rs | 78 ++++++++++++++++++++++++++ 6 files changed, 244 insertions(+), 37 deletions(-) diff --git a/crates/sync/src/handler.rs b/crates/sync/src/handler.rs index 0af146c..e8e875a 100644 --- a/crates/sync/src/handler.rs +++ b/crates/sync/src/handler.rs @@ -342,10 +342,14 @@ impl SyncHandler { /// the plain request would return, each paired with the uncle headers it /// references. /// - /// The uncles come out of the body, which is the only place they exist. A - /// block whose body this node no longer holds therefore ends the run -- - /// never an empty uncle list, which would be a quiet lie about the chain's - /// work. Peers learn which blocks are answerable from the served range this + /// The uncles come out of the body, or once the body is gone, out of the + /// freezer's uncle store, which keeps them by height for the canonical + /// chain: where a snapshot sync puts the uncles it was sent for every + /// block it fetched no body for. Served from bodies alone, a node that + /// synced so could not serve the sync it had just done (#270). A block + /// whose uncles this node holds in neither place ends the run -- never an + /// empty uncle list, which would be a quiet lie about the chain's work. + /// Peers learn which blocks are answerable from the served range this /// node announces. fn serve_headers_with_uncles_request( &self, @@ -369,9 +373,9 @@ impl SyncHandler { let uncles = if header.uncle_count == 0 { Vec::new() } else { - match store.body(header.hash()) { - Ok(Some((_txs, ommers))) => ommers, - _ => break, + match crate::total_difficulty::held_uncles(store, header.hash(), &header) { + Some(uncles) => uncles, + None => break, } }; diff --git a/crates/sync/src/snap/forward.rs b/crates/sync/src/snap/forward.rs index 78b8b77..5ed5081 100644 --- a/crates/sync/src/snap/forward.rs +++ b/crates/sync/src/snap/forward.rs @@ -140,6 +140,9 @@ pub struct ForwardSync { /// Whether to archive uncle headers; see `SnapConfig::archive_uncles`. archive_uncles: bool, + /// The blocks under this height have their uncles archived as their run + /// is committed: see [`Self::with_bodies_from`]. + uncles_below: u64, /// Where skeleton starts are measured from, and how far apart. skeleton_base: u64, @@ -209,6 +212,7 @@ impl ForwardSync { target, target_hash, archive_uncles, + uncles_below: target.saturating_sub(rustock_storage::freezer::FREEZE_DEPTH), skeleton_base: anchor_number, skeleton_span: span, skeleton_todo, @@ -223,6 +227,21 @@ impl ForwardSync { } } + /// The lowest block whose body the sync goes on to fetch. The uncles sent + /// beside every header under it are archived as its run is committed: + /// they are the only copy this node will get, and without them it can + /// neither re-derive its own chain's work nor serve `rsk/63`. + /// + /// Without this they were archived only under the freeze horizon, 20,000 + /// blocks below the target, on the reasoning that every block above it is + /// inside the body window and gets its uncles with its body. The window is + /// 6,000 blocks (rskj's `BLOCKS_REQUIRED`), so the blocks between got + /// neither, and their uncles were lost (#270). + pub fn with_bodies_from(mut self, lowest: u64) -> Self { + self.uncles_below = lowest; + self + } + pub fn done(&self) -> bool { self.done } @@ -405,15 +424,12 @@ impl ForwardSync { // own chain's work and cannot serve `rsk/63` to anyone else -- which // is to say, cannot serve the sync it just performed. // - // Only below the freeze horizon. Nearer the target the chain can still - // move, and the freezer is for settled history; those blocks are - // inside the body window the sync fetches anyway, so their uncles - // arrive with the bodies. + // Only below the blocks whose bodies the sync fetches: those bring + // their uncles with them (`with_bodies_from`). if self.archive_uncles { if let Some(freezer) = self.store.freezer() { - let horizon = self.target.saturating_sub(rustock_storage::freezer::FREEZE_DEPTH); for (number, uncles) in &run.uncle_headers { - if *number < horizon { + if *number < self.uncles_below { if let Err(e) = freezer.put_uncles(*number, uncles) { warn!( target: "rustock::snap", diff --git a/crates/sync/src/snap/headers.rs b/crates/sync/src/snap/headers.rs index ed5369b..b15321f 100644 --- a/crates/sync/src/snap/headers.rs +++ b/crates/sync/src/snap/headers.rs @@ -191,6 +191,9 @@ pub struct HeaderWalk { /// Blocks strictly below this may be frozen: `top - FREEZE_DEPTH`. freeze_horizon: u64, + /// The blocks under this height have their uncles staged beside the + /// headers: see [`Self::with_bodies_from`]. + uncles_below: u64, /// Headers written against a checkpoint that is still only a peer's claim. /// /// `None` when there is no freezer, or when it already holds something -- @@ -266,10 +269,25 @@ impl HeaderWalk { reaching: Vec::new(), anchor_difficulty: None, freeze_horizon: number.saturating_sub(rustock_storage::freezer::FREEZE_DEPTH), + uncles_below: number.saturating_sub(rustock_storage::freezer::FREEZE_DEPTH), staging, } } + /// The lowest block whose body the sync goes on to fetch. The uncles sent + /// beside every header under it are staged with the headers: they are the + /// only copy this node will get. + /// + /// Without this they were staged only under the freeze horizon, 20,000 + /// blocks below the top, on the reasoning that every block above it is + /// inside the body window. The window is 6,000 blocks (rskj's + /// `BLOCKS_REQUIRED`), so the blocks between got neither, and their uncles + /// were lost (#270). + pub fn with_bodies_from(mut self, lowest: u64) -> Self { + self.uncles_below = lowest; + self + } + /// Difficulty summed across the headers of every run that has joined the /// chain. /// @@ -562,12 +580,9 @@ impl HeaderWalk { Ok(()) } - /// Writes a verified run to the staging freezer. - /// - /// Only blocks below the freeze horizon: anything younger can still be - /// replaced by a reorg, and a file addressed by height has no way to say - /// "this height means something else now". - /// Stage the uncles a run of headers referenced, beside the headers. + /// Stage the uncles a run of headers referenced, beside the headers: those + /// of the blocks below the ones whose bodies the sync fetches + /// ([`Self::with_bodies_from`]), which bring their own. /// /// Same provisional footing as the headers: the chain is a peer's word /// until the walk reaches ground, so these go to the staging freezer and @@ -578,17 +593,22 @@ impl HeaderWalk { /// chain's work nor serve `rsk/63` -- the sync it just performed. pub fn stage_uncles(&mut self, uncles: &[(u64, Vec)]) { let Some(staging) = self.staging.as_ref() else { return }; - let horizon = self.freeze_horizon; - if horizon == 0 { + let below = self.uncles_below; + if below == 0 { return; } for (number, list) in uncles { - if *number < horizon { + if *number < below { let _ = staging.put_uncles(*number, list); } } } + /// Writes a verified run to the staging freezer. + /// + /// Only blocks below the freeze horizon: anything younger can still be + /// replaced by a reorg, and a file addressed by height has no way to say + /// "this height means something else now". fn freeze_provisionally(&mut self, headers: &[rustock_core::Header]) { let Some(staging) = self.staging.as_ref() else { return }; let horizon = self.freeze_horizon; diff --git a/crates/sync/src/snap/session.rs b/crates/sync/src/snap/session.rs index 9363bf4..a9603d8 100644 --- a/crates/sync/src/snap/session.rs +++ b/crates/sync/src/snap/session.rs @@ -715,16 +715,22 @@ impl SnapSession { "ascending the header chain from #{number} to #{}, with uncles", header.number ); - self.ascent = Some(super::forward::ForwardSync::new( - number, - hash, - work, - header.number, - header.hash(), - self.config.archive_uncles, - self.store.clone(), - self.verifier.clone(), - )); + // Bodies come only for the blocks from `blocks_wanted_to` + // up, so the uncles sent beside every header under it + // are the only copy this node will get. + self.ascent = Some( + super::forward::ForwardSync::new( + number, + hash, + work, + header.number, + header.hash(), + self.config.archive_uncles, + self.store.clone(), + self.verifier.clone(), + ) + .with_bodies_from(self.blocks_wanted_to), + ); } _ => { warn!( @@ -737,11 +743,14 @@ impl SnapSession { } } - self.walk = Some(super::headers::HeaderWalk::new( - header.clone(), - self.store.clone(), - self.verifier.clone(), - )); + self.walk = Some( + super::headers::HeaderWalk::new( + header.clone(), + self.store.clone(), + self.verifier.clone(), + ) + .with_bodies_from(self.blocks_wanted_to), + ); self.checkpoint = Some(header); // The walk is built either way, but the gate — when there is one — diff --git a/crates/sync/src/snap/session_tests.rs b/crates/sync/src/snap/session_tests.rs index 23fb60f..f7bc42e 100644 --- a/crates/sync/src/snap/session_tests.rs +++ b/crates/sync/src/snap/session_tests.rs @@ -2262,6 +2262,86 @@ fn a_passed_gate_hands_over_to_the_ascent_not_the_walk() { ); } +/// A session that keeps the uncles it is sent: a store with a freezer, and a +/// body window of `bodies` blocks under the checkpoint it is offered. +fn archiving_session(forward: bool, bodies: u64) -> (SnapSession, Fixture, B256) { + let f = fixture(4); + let freezer = + rustock_storage::freezer::Freezer::open(f._dir.path().join("freezer")).expect("freezer"); + f.store.set_freezer(Arc::new(freezer)); + let genesis = f.with_genesis(); + f.store.set_head(genesis).expect("head"); + let mut session = SnapSession::new( + SnapConfig { + client_enabled: true, + forward_headers: forward, + blocks_required: bodies, + ..SnapConfig::default() + }, + f.store.clone(), + Arc::new(MemoryTrieStore::new()) as Arc, + Arc::new(HeaderVerifier::new()), + ); + session.set_peer_serves_uncles(forward); + (session, f, genesis) +} + +/// The uncles an `rsk/63` peer sends beside the headers of the blocks under +/// the body window reach the freezer: by either walk, and not only under the +/// freeze horizon (#270). +/// +/// Bodies, and the uncles in them, come only for the window under the +/// checkpoint; the freeze horizon is 20,000 blocks under it. The uncles of +/// the blocks between came only beside the headers, and both walks dropped +/// them, so a synced node could neither re-derive its own chain's work there +/// nor serve the sync it had just done. +#[test] +fn the_uncles_below_the_bodies_reach_the_freezer_by_either_walk() { + use rustock_networking::protocol::HeaderWithUncles; + + for forward in [true, false] { + let (mut session, f, genesis) = archiving_session(forward, 2); + // Five blocks from genesis, each naming one uncle. The bodies come + // for #3 up. + let (blocks, tds) = chain_with_uncles(1, 5, genesis, 100, f.state_root); + let checkpoint = blocks.last().expect("a checkpoint"); + let _ = session.on_status(std::slice::from_ref(checkpoint), &tds[4..], 1024, 0); + let entries: Vec = blocks + .iter() + .rev() + .map(|b| HeaderWithUncles { + header: b.header.clone(), + uncles: b.ommers.clone(), + }) + .collect(); + let _ = session.on_headers_with_uncles(5, &entries); + + let freezer = f.store.freezer().expect("a freezer"); + for b in &blocks[..2] { + let held = freezer + .uncles(b.header.number) + .expect("read") + .unwrap_or_else(|| { + panic!( + "forward={forward}: #{} is under the bodies", + b.header.number + ) + }); + assert_eq!( + held.iter().map(Header::hash).collect::>(), + b.ommers.iter().map(Header::hash).collect::>(), + "forward={forward}: the uncles #{} named", + b.header.number + ); + } + assert_eq!( + freezer.uncles(3).expect("read"), + None, + "forward={forward}: #3 gets its body, and its uncles with it" + ); + } +} + /// An offered header's proof of work is checked against the difficulty it /// states, so a peer can name any height for next to nothing. The gate takes /// on a window of [`crate::sampler::MAX_SAMPLED_WINDOW`] above the checkpoint diff --git a/crates/sync/src/tests.rs b/crates/sync/src/tests.rs index 6f7faeb..81f46a1 100644 --- a/crates/sync/src/tests.rs +++ b/crates/sync/src/tests.rs @@ -3221,6 +3221,84 @@ async fn test_serve_skeleton_request() { } } +/// A node serves `rsk/63` from the uncles its freezer keeps once a block's +/// body is gone (#270). +/// +/// A snapshot-synced node holds bodies only for the window under its +/// checkpoint, and keeps the uncles of the blocks below it in the freezer, +/// where the sync put them as they arrived beside the headers. Read from +/// bodies alone, the run it served stopped at the first such block that named +/// an uncle: it could not serve the sync it had just done. +#[tokio::test] +async fn headers_with_uncles_are_served_from_the_freezer_once_the_body_is_gone() { + use rustock_networking::protocol::{ + BlockHeadersQuery, BlockHeadersWithUnclesRequest, P2pHandler, P2pMessage, RskMessage, + RskSubMessage, + }; + + let dir = tempdir().unwrap(); + let store = Arc::new(BlockStore::open(dir.path()).unwrap()); + let freezer = + Arc::new(rustock_storage::freezer::Freezer::open(dir.path().join("freezer")).unwrap()); + store.set_freezer(freezer.clone()); + + // #0 to #3, of which #2 names an uncle and holds no body. + let uncle = dummy_header(1, B256::repeat_byte(0x0c), U256::from(1)); + let mut parent = B256::ZERO; + for number in 0..=3u64 { + let mut header = dummy_header(number, parent, U256::from(1)); + if number == 2 { + header.uncle_count = 1; + header.ommers_hash = + rustock_execution::processor::compute_ommers_hash(std::slice::from_ref(&uncle)); + } + parent = header.hash(); + store.put_header(&header).unwrap(); + store.put_canonical_hash(number, parent).unwrap(); + } + store.set_head(parent).unwrap(); + freezer.put_uncles(2, std::slice::from_ref(&uncle)).unwrap(); + freezer.sync().unwrap(); + + let manager = Arc::new(SyncManager::new( + store, + Arc::new(HeaderVerifier::new()), + Arc::new(rustock_networking::peers::PeerStore::new()), + )); + let (event_tx, _event_rx) = mpsc::unbounded_channel(); + let handler = SyncHandler::new(manager, event_tx); + let request = P2pMessage::RskMessage(RskMessage::new( + RskSubMessage::BlockHeadersWithUnclesRequest(BlockHeadersWithUnclesRequest { + id: 7, + query: BlockHeadersQuery { + hash: parent, + count: 4, + }, + }), + )); + let Some(P2pMessage::RskMessage(answer)) = handler.handle_message(B512::ZERO, &request) else { + panic!("the request is answered"); + }; + let RskSubMessage::BlockHeadersWithUnclesResponse(response) = answer.sub_message else { + panic!("answered with headers and their uncles"); + }; + let numbers: Vec = response.entries.iter().map(|e| e.header.number).collect(); + assert_eq!( + numbers, + vec![3, 2, 1, 0], + "the whole run, past the block without a body" + ); + assert_eq!( + response.entries[1] + .uncles + .iter() + .map(Header::hash) + .collect::>(), + vec![uncle.hash()], + "with the uncle the freezer kept" + ); +} + #[tokio::test] async fn test_serve_skeleton_request_unknown_start() { use rustock_networking::protocol::{P2pMessage, RskMessage, RskSubMessage}; From 489851c3f3579e454a1cbfd6de6eb5d4f2e775a1 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Tue, 10 Nov 2026 07:59:26 -0800 Subject: [PATCH 076/264] sync: announce, relay and serve new blocks as rskj does A block this node mines goes whole to the square root of its peers and by hash to the rest (ChannelManagerImpl.broadcastBlock). A block taken at the tip is announced to the peers not known to hold it (MessageVisitor.relayBlock), remembered for rskj's 1,000 blocks (BlockNodeInformation). GetBlock is answered with the whole block. A whole Block message is taken while following, within rskj's height window, its body held to its header and its header verified as any header at the tip. --- crates/cli/src/main.rs | 36 +- crates/networking/src/protocol/rsk.rs | 231 +++++++++++- crates/sync/src/announce.rs | 251 +++++++++++++ crates/sync/src/block_nodes.rs | 126 +++++++ crates/sync/src/events.rs | 6 + crates/sync/src/handler.rs | 42 +++ crates/sync/src/lib.rs | 3 + crates/sync/src/manager.rs | 23 ++ crates/sync/src/service.rs | 172 ++++++++- crates/sync/src/tests.rs | 506 ++++++++++++++++++++++++++ 10 files changed, 1374 insertions(+), 22 deletions(-) create mode 100644 crates/sync/src/announce.rs create mode 100644 crates/sync/src/block_nodes.rs diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index 4cdaaf5..a24460a 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -3402,20 +3402,28 @@ async fn run(local_offset: Option) -> Result<()> { hardfork_cfg.clone(), mining_config, ); - let chain = Arc::new(rustock_execution::mining::StoreChainAccess::with_flush_every( - store.clone(), - trie_store_for_pool.clone(), - // What the sync service's import path does for a block that - // arrived from a peer, done here for one this node mined. - Some({ - let pool = pool.clone(); - Arc::new(move |block: &rustock_core::Block| { - pool.remove_mined(&block.transactions); - pool.evict_outdated(block.header.number); - }) - }), - args.flush_every, - )); + // What the sync service's import path does for a block that arrived + // from a peer, done here for one this node mined; then what rskj's + // `EthereumImpl.addNewMinedBlock` does with a mined block once it is + // stored, best or not: send it to the peers. + let on_committed: Arc = { + let pool = pool.clone(); + let broadcast = + rustock_sync::on_mined(peer_store.clone(), tokio::runtime::Handle::current()); + Arc::new(move |block: &rustock_core::Block| { + pool.remove_mined(&block.transactions); + pool.evict_outdated(block.header.number); + broadcast(block); + }) + }; + let chain = Arc::new( + rustock_execution::mining::StoreChainAccess::with_flush_every( + store.clone(), + trie_store_for_pool.clone(), + Some(on_committed), + args.flush_every, + ), + ); let mut server = rustock_execution::MinerServer::new( builder, chain, diff --git a/crates/networking/src/protocol/rsk.rs b/crates/networking/src/protocol/rsk.rs index f38dcb1..90eaaa3 100644 --- a/crates/networking/src/protocol/rsk.rs +++ b/crates/networking/src/protocol/rsk.rs @@ -1,6 +1,6 @@ use alloy_rlp::{Decodable, Encodable, RlpDecodable, RlpEncodable, Header as RlpHeader}; use alloy_primitives::{B256, Bytes, U256}; -use rustock_core::{Header, Transaction}; +use rustock_core::{Block, Header, Transaction}; use rustock_core::types::header::HeaderForm; use rustock_core::types::header_extension::BlockHeaderExtension; use rustock_core::rlp_compat::{decode_u8_lenient, decode_u64_lenient, decode_u256_lenient, decode_u32_lenient}; @@ -284,6 +284,14 @@ pub struct SkeletonResponse { #[repr(u8)] pub enum RskMessageType { Status = 1, + /// A whole block: rskj `BlockMessage`, which a node sends to some of its + /// peers when it mines one (`ChannelManagerImpl.broadcastBlock`) and to a + /// peer that asks for one by hash (`NodeBlockProcessor.processGetBlock`). + Block = 2, + /// A whole block asked for by its hash: rskj `GetBlockMessage`, which a + /// node sends to the peer that announced a block it does not hold + /// (`NodeBlockProcessor.processNewBlockHashesMessage`). + GetBlock = 3, NewBlockHashes = 6, Transactions = 7, BlockHashRequest = 8, @@ -366,6 +374,8 @@ impl Decodable for BlockRange { #[derive(Debug, Clone)] pub enum RskSubMessage { Status(RskStatus), + Block(Box), + GetBlock(B256), BlockHashRequest(BlockHashRequest), BlockHeadersRequest(BlockHeadersRequest), BlockHeadersResponse(BlockHeadersResponse), @@ -392,6 +402,8 @@ impl RskSubMessage { pub fn message_type(&self) -> RskMessageType { match self { RskSubMessage::Status(_) => RskMessageType::Status, + RskSubMessage::Block(_) => RskMessageType::Block, + RskSubMessage::GetBlock(_) => RskMessageType::GetBlock, RskSubMessage::BlockHashRequest(_) => RskMessageType::BlockHashRequest, RskSubMessage::BlockHeadersRequest(_) => RskMessageType::BlockHeadersRequest, RskSubMessage::BlockHeadersResponse(_) => RskMessageType::BlockHeadersResponse, @@ -672,8 +684,54 @@ impl RskSubMessage { RskSubMessage::SnapChunkResponse(m) => out.extend_from_slice(&m.encode_body()), RskSubMessage::SnapBlocksRequest(m) => out.extend_from_slice(&m.encode_body()), RskSubMessage::SnapBlocksResponse(m) => out.extend_from_slice(&m.encode_body()), - RskSubMessage::NewBlockHashes(_) | RskSubMessage::Unknown(_) => { - // Receive-only messages are not encoded/sent + RskSubMessage::Block(block) => { + // rskj `BlockMessage.getEncodedMessage`: + // `RLP.encodeList(RLP.encode(block.getEncoded()))`, the block's own + // encoding as the one string in a list. + let mut payload = Vec::new(); + Bytes::from(snap::encode_block(block)).encode(&mut payload); + RlpHeader { + list: true, + payload_length: payload.len(), + } + .encode(out); + out.extend_from_slice(&payload); + } + RskSubMessage::GetBlock(hash) => { + // rskj `GetBlockMessage.getEncodedMessage`: the hash, alone in a list. + let mut payload = Vec::new(); + hash.encode(&mut payload); + RlpHeader { + list: true, + payload_length: payload.len(), + } + .encode(out); + out.extend_from_slice(&payload); + } + RskSubMessage::NewBlockHashes(identifiers) => { + // rskj `NewBlockHashesMessage.encode`: a list of + // `BlockIdentifier.getEncoded`, each `[hash, number]`. + let mut payload = Vec::new(); + for identifier in identifiers { + let mut pair = Vec::new(); + identifier.hash.encode(&mut pair); + identifier.number.encode(&mut pair); + RlpHeader { + list: true, + payload_length: pair.len(), + } + .encode(&mut payload); + payload.extend_from_slice(&pair); + } + RlpHeader { + list: true, + payload_length: payload.len(), + } + .encode(out); + out.extend_from_slice(&payload); + } + RskSubMessage::Unknown(_) => { + // A type this node does not know is never sent. } } } @@ -763,6 +821,26 @@ impl Decodable for RskMessage { } RskSubMessage::Status(status) } + 2 => { + // rskj `MessageType.BLOCK_MESSAGE`: the block's own encoding, the + // one string in a list, read whole (`BlockFactory.decodeBlock`). + let list_h = RlpHeader::decode(&mut body_params)?; + if !list_h.list || body_params.len() < list_h.payload_length { + return Err(alloy_rlp::Error::Custom("malformed block message")); + } + let mut list_body = &body_params[..list_h.payload_length]; + let encoded = Bytes::decode(&mut list_body)?; + RskSubMessage::Block(Box::new(snap::decode_block(&mut encoded.as_ref())?)) + } + 3 => { + // rskj `MessageType.GET_BLOCK_MESSAGE`: the hash, alone in a list. + let list_h = RlpHeader::decode(&mut body_params)?; + if !list_h.list || body_params.len() < list_h.payload_length { + return Err(alloy_rlp::Error::Custom("malformed get-block message")); + } + let mut list_body = &body_params[..list_h.payload_length]; + RskSubMessage::GetBlock(B256::decode(&mut list_body)?) + } 9 => { let list_h = RlpHeader::decode(&mut body_params)?; let mut list_body = &body_params[..list_h.payload_length]; @@ -1914,7 +1992,7 @@ mod headers_with_uncles_tests { use super::*; use alloy_primitives::Address; - fn plain(number: u64, difficulty: u64) -> Header { + pub(super) fn plain(number: u64, difficulty: u64) -> Header { Header { number, parent_hash: B256::repeat_byte(number as u8), @@ -1955,7 +2033,7 @@ mod headers_with_uncles_tests { HeaderWithUncles { header, uncles: entry.uncles } } - fn roundtrip(sub: RskSubMessage) -> RskSubMessage { + pub(super) fn roundtrip(sub: RskSubMessage) -> RskSubMessage { let mut buf = Vec::new(); RskMessage::new(sub).encode(&mut buf); RskMessage::decode(&mut buf.as_slice()).expect("decodes").sub_message @@ -2217,6 +2295,13 @@ mod headers_with_uncles_tests { id: 5, entries: vec![with_uncles(10, 1000, vec![plain(9, 700)])], }), + RskSubMessage::Block(Box::new(rustock_core::Block { + header: plain(11, 1100), + transactions: vec![tx.clone()], + ommers: vec![plain(10, 1000)], + })), + RskSubMessage::GetBlock(B256::repeat_byte(6)), + RskSubMessage::NewBlockHashes(vec![identifier(1), identifier(2)]), ]; let mut messages: Vec<(String, Vec)> = seeds @@ -2228,8 +2313,8 @@ mod headers_with_uncles_tests { (kind, bytes) }) .collect(); - // rustock never sends NewBlockHashes, so its encoder writes nothing for - // it; this is the list of [hash, number] pairs an rskj node sends. + // And the list of [hash, number] pairs an rskj node sends, built by + // hand rather than by this node's encoder. let mut pairs = Vec::new(); for n in 1..=3u8 { let mut pair = Vec::new(); @@ -2379,3 +2464,135 @@ mod difficulty_encoding_tests { assert_eq!(back.total_difficulty, Some(td)); } } + +/// The three messages rskj announces and hands over new blocks with, as rskj +/// writes them: built here by hand from its encoders, not by this node's. +#[cfg(test)] +mod block_message_tests { + use super::headers_with_uncles_tests::{plain, roundtrip}; + use super::*; + + /// RLP's string: one byte below 0x80 stands for itself. + fn string(bytes: &[u8]) -> Vec { + if let [byte] = bytes { + if *byte < 0x80 { + return vec![*byte]; + } + } + let mut out = Vec::new(); + RlpHeader { + list: false, + payload_length: bytes.len(), + } + .encode(&mut out); + out.extend_from_slice(bytes); + out + } + + fn list(payload: &[u8]) -> Vec { + let mut out = Vec::new(); + RlpHeader { + list: true, + payload_length: payload.len(), + } + .encode(&mut out); + out.extend_from_slice(payload); + out + } + + /// `Message.getEncoded`: `[type, string(params)]`, inside the list + /// `RskMessage.encode` adds. + fn frame(kind: u8, params: &[u8]) -> Vec { + let mut message = string(&[kind]); + message.extend(string(params)); + list(&list(&message)) + } + + fn written(sub: RskSubMessage) -> Vec { + let mut out = Vec::new(); + RskMessage::new(sub).encode(&mut out); + out + } + + /// `NewBlockHashesMessage.encode`: a list of `BlockIdentifier.getEncoded`, + /// each `RLP.encodeList(encodeElement(hash), encodeBigInteger(number))`. + #[test] + fn new_block_hashes_are_written_as_rskj_writes_them() { + let identifiers = vec![ + BlockIdentifier { + hash: B256::repeat_byte(0x11), + number: 0x7f, + }, + BlockIdentifier { + hash: B256::repeat_byte(0x22), + number: 9_348_613, + }, + ]; + let mut pairs = Vec::new(); + for identifier in &identifiers { + let number = identifier.number.to_be_bytes(); + let first = number.iter().position(|b| *b != 0).unwrap_or(number.len()); + let mut pair = string(identifier.hash.as_slice()); + pair.extend(string(&number[first..])); + pairs.extend(list(&pair)); + } + assert_eq!( + written(RskSubMessage::NewBlockHashes(identifiers.clone())), + frame(6, &list(&pairs)) + ); + let RskSubMessage::NewBlockHashes(back) = + roundtrip(RskSubMessage::NewBlockHashes(identifiers.clone())) + else { + panic!("read back as another message"); + }; + assert_eq!(back, identifiers); + } + + /// `BlockMessage.getEncodedMessage`: `RLP.encodeList(RLP.encode( + /// block.getEncoded()))`, the block as the one string in a list, which + /// `MessageType.BLOCK_MESSAGE` hands whole to `BlockFactory.decodeBlock`. + #[test] + fn a_block_is_written_as_rskj_writes_it() { + let block = Block { + header: Header { + uncle_count: 1, + ..plain(7, 700) + }, + transactions: Vec::new(), + ommers: vec![plain(6, 600)], + }; + assert_eq!( + written(RskSubMessage::Block(Box::new(block.clone()))), + frame(2, &list(&string(&snap::encode_block(&block)))) + ); + let RskSubMessage::Block(back) = roundtrip(RskSubMessage::Block(Box::new(block.clone()))) + else { + panic!("read back as another message"); + }; + assert_eq!(back.hash(), block.hash()); + assert_eq!(back.ommers.len(), 1); + assert_eq!(back.ommers[0].hash(), block.ommers[0].hash()); + } + + /// `GetBlockMessage.getEncodedMessage`: `RLP.encodeList(encodeElement(hash))`. + #[test] + fn a_block_request_is_written_as_rskj_writes_it() { + let hash = B256::repeat_byte(0x33); + assert_eq!( + written(RskSubMessage::GetBlock(hash)), + frame(3, &list(&string(hash.as_slice()))) + ); + let RskSubMessage::GetBlock(back) = roundtrip(RskSubMessage::GetBlock(hash)) else { + panic!("read back as another message"); + }; + assert_eq!(back, hash); + } + + /// rskj's numbers for the two types, which no other message uses. + #[test] + fn the_block_messages_have_rskjs_type_numbers() { + assert_eq!(RskMessageType::Block as u8, 2); + assert_eq!(RskMessageType::GetBlock as u8, 3); + assert_eq!(RskMessageType::NewBlockHashes as u8, 6); + } +} diff --git a/crates/sync/src/announce.rs b/crates/sync/src/announce.rs new file mode 100644 index 0000000..f0e78b7 --- /dev/null +++ b/crates/sync/src/announce.rs @@ -0,0 +1,251 @@ +//! Telling peers about new blocks, as rskj does. +//! +//! A block this node mined goes out whole to a few peers and as its hash to +//! the rest (`EthereumImpl.addNewMinedBlock` through +//! `ChannelManagerImpl.broadcastBlock`). A block it took from a peer at the +//! tip goes out as its hash to the peers not known to hold it +//! (`MessageVisitor.relayBlock`). A peer that hears a hash it lacks asks for +//! the block with `GetBlockMessage`, which `SyncHandler` answers. + +use alloy_primitives::{B256, B512}; +use rustock_core::Block; +use rustock_networking::peers::PeerStore; +use rustock_networking::protocol::{BlockIdentifier, P2pMessage, RskMessage, RskSubMessage}; +use std::collections::HashSet; +use tracing::trace; + +/// Which peers get a mined block whole and which get its hash: rskj +/// `ChannelManagerImpl.broadcastBlock` (L194-222). The peers are shuffled, and +/// the first `(int) Math.floor(Math.sqrt(peers.size()))` get the block; the +/// rest get a `NewBlockHashesMessage` naming it. +pub fn broadcast_split( + mut peers: Vec, + rng: &mut R, +) -> (Vec, Vec) { + use rand::seq::SliceRandom; + peers.shuffle(rng); + let whole = (peers.len() as f64).sqrt().floor() as usize; + let hashes = peers.split_off(whole); + (peers, hashes) +} + +fn new_block_hashes(hash: B256, number: u64) -> P2pMessage { + P2pMessage::RskMessage(RskMessage::new(RskSubMessage::NewBlockHashes(vec![ + BlockIdentifier { hash, number }, + ]))) +} + +/// Sends a block this node mined to every connected peer: rskj +/// `EthereumImpl.addNewMinedBlock` (L60-68), which calls `broadcastBlock` for +/// a mined block once it is in the store, best or not. Returns the peers that +/// got it whole. +pub async fn broadcast_block(peer_store: &PeerStore, block: &Block) -> Vec { + let peers = peer_store.peers().await; + let (whole, hashes) = broadcast_split(peers, &mut rand::thread_rng()); + let hash = block.hash(); + let number = block.header.number; + trace!( + target: "rustock::sync", + "Mined block #{number} to {} peer(s) whole and {} by hash", + whole.len(), + hashes.len() + ); + let message = P2pMessage::RskMessage(RskMessage::new(RskSubMessage::Block(Box::new( + block.clone(), + )))); + for peer in &whole { + peer_store.send_to_peer(peer, message.clone()).await; + } + let announcement = new_block_hashes(hash, number); + for peer in &hashes { + peer_store.send_to_peer(peer, announcement.clone()).await; + } + whole +} + +/// What this node does with a block it mined once the block is stored, best +/// or not: send it to the peers, as rskj's `EthereumImpl.addNewMinedBlock` +/// (L60-68) does through [`broadcast_block`]. The miner commits from a +/// blocking thread, so the sending is handed to `runtime`. +pub fn on_mined( + peer_store: std::sync::Arc, + runtime: tokio::runtime::Handle, +) -> impl Fn(&Block) + Send + Sync { + move |block: &Block| { + let peer_store = peer_store.clone(); + let block = block.clone(); + runtime.spawn(async move { + broadcast_block(&peer_store, &block).await; + }); + } +} + +/// Announces a block taken from a peer to every connected peer not known to +/// hold it: rskj `MessageVisitor.relayBlock` (L265-276), a +/// `NewBlockHashesMessage` to the known peers less those +/// `BlockNodeInformation.getNodesByBlock` names. Returns how many it went to. +pub(crate) async fn relay_block( + peer_store: &PeerStore, + holders: &HashSet, + hash: B256, + number: u64, +) -> usize { + let announcement = new_block_hashes(hash, number); + let mut sent = 0; + for peer in peer_store.peers().await { + if !holders.contains(&peer) && peer_store.send_to_peer(&peer, announcement.clone()).await { + sent += 1; + } + } + trace!(target: "rustock::sync", "Relayed #{number} {hash:?} to {sent} peer(s)"); + sent +} + +#[cfg(test)] +mod tests { + use super::*; + use rand::SeedableRng; + use tokio::sync::mpsc; + + fn peer(n: u8) -> B512 { + B512::repeat_byte(n) + } + + /// rskj sends the whole block to the floor of the square root of the + /// peer count, and the hash to everyone else, each peer once. + #[test] + fn the_whole_block_goes_to_the_square_root_of_the_peers() { + let mut rng = rand::rngs::StdRng::seed_from_u64(7); + for (count, whole) in [ + (0, 0), + (1, 1), + (2, 1), + (3, 1), + (4, 2), + (8, 2), + (9, 3), + (24, 4), + (25, 5), + ] { + let peers: Vec = (1..=count).map(peer).collect(); + let (to_whole, to_hash) = broadcast_split(peers.clone(), &mut rng); + assert_eq!(to_whole.len(), whole, "{count} peers"); + assert_eq!(to_hash.len(), count as usize - whole, "{count} peers"); + let mut all: Vec = to_whole.iter().chain(&to_hash).copied().collect(); + all.sort(); + assert_eq!(all, peers, "{count} peers: each once"); + } + } + + /// Shuffled first, as rskj shuffles them, so the same few peers are not + /// always the ones that get the block whole. + #[test] + fn the_peers_that_get_the_whole_block_are_drawn_at_random() { + let peers: Vec = (1..=16).map(peer).collect(); + let mut rng = rand::rngs::StdRng::seed_from_u64(1); + let drawn: HashSet> = (0..20) + .map(|_| broadcast_split(peers.clone(), &mut rng).0) + .collect(); + assert!(drawn.len() > 1, "twenty draws all chose {drawn:?}"); + } + + fn block(number: u64) -> Block { + let header = crate::tests::dummy_header( + number, + B256::repeat_byte(1), + alloy_primitives::U256::from(1u64), + ); + Block { + header, + transactions: Vec::new(), + ommers: Vec::new(), + } + } + + async fn store_of(count: u8) -> (PeerStore, Vec<(B512, mpsc::Receiver)>) { + let store = PeerStore::new(); + let mut inboxes = Vec::new(); + for n in 1..=count { + let (tx, rx) = mpsc::channel(16); + store.add_peer(peer(n), tx).await; + inboxes.push((peer(n), rx)); + } + (store, inboxes) + } + + #[tokio::test] + async fn a_mined_block_reaches_every_peer_once() { + let (store, mut inboxes) = store_of(9).await; + let mined = block(42); + let whole: HashSet = broadcast_block(&store, &mined).await.into_iter().collect(); + assert_eq!(whole.len(), 3, "the square root of nine"); + for (id, inbox) in &mut inboxes { + let Ok(P2pMessage::RskMessage(message)) = inbox.try_recv() else { + panic!("{id:?} got nothing"); + }; + match message.sub_message { + RskSubMessage::Block(got) => { + assert!(whole.contains(id), "{id:?} got the block but was not drawn"); + assert_eq!(got.hash(), mined.hash()); + } + RskSubMessage::NewBlockHashes(ids) => { + assert!( + !whole.contains(id), + "{id:?} was drawn but got only the hash" + ); + assert_eq!( + ids, + vec![BlockIdentifier { + hash: mined.hash(), + number: 42 + }] + ); + } + other => panic!("{id:?} got {other:?}"), + } + assert!(inbox.try_recv().is_err(), "{id:?} heard of the block twice"); + } + } + + /// The miner commits from a blocking thread: the block still reaches every + /// peer. + #[tokio::test] + async fn a_block_mined_on_a_blocking_thread_reaches_every_peer() { + let (store, mut inboxes) = store_of(4).await; + let on_mined = on_mined( + std::sync::Arc::new(store), + tokio::runtime::Handle::current(), + ); + let mined = block(7); + tokio::task::spawn_blocking(move || on_mined(&mined)) + .await + .unwrap(); + for (id, inbox) in &mut inboxes { + let heard = + tokio::time::timeout(std::time::Duration::from_secs(10), inbox.recv()).await; + assert!(matches!(heard, Ok(Some(_))), "{id:?} heard nothing"); + } + } + + #[tokio::test] + async fn a_relayed_block_skips_the_peers_that_hold_it() { + let (store, mut inboxes) = store_of(4).await; + let holders = HashSet::from([peer(1), peer(3)]); + let hash = B256::repeat_byte(0xab); + assert_eq!(relay_block(&store, &holders, hash, 7).await, 2); + for (id, inbox) in &mut inboxes { + let got = inbox.try_recv(); + if holders.contains(id) { + assert!(got.is_err(), "{id:?} holds the block and was told of it"); + continue; + } + let Ok(P2pMessage::RskMessage(message)) = got else { + panic!("{id:?} was not told"); + }; + let RskSubMessage::NewBlockHashes(ids) = message.sub_message else { + panic!("{id:?} got another message"); + }; + assert_eq!(ids, vec![BlockIdentifier { hash, number: 7 }]); + } + } +} diff --git a/crates/sync/src/block_nodes.rs b/crates/sync/src/block_nodes.rs new file mode 100644 index 0000000..9570931 --- /dev/null +++ b/crates/sync/src/block_nodes.rs @@ -0,0 +1,126 @@ +//! Which peers hold which blocks: rskj's `BlockNodeInformation`. +//! +//! A new block is announced only to the peers not known to hold it already +//! (`MessageVisitor.relayBlock`), so this node remembers, for the blocks it +//! heard of lately, the peers it knows hold each one: the peer that announced +//! it (`NodeBlockProcessor.processNewBlockHashesMessage`), the peer that sent +//! it (`BlockSyncService.preprocessBlock`) and each peer that asked for it +//! (`NodeBlockProcessor.processGetBlock`). +//! +//! Bounded as rskj bounds it: the 1,000 blocks touched last, the least recently +//! touched dropped first. A block's peers are not bounded, because there are +//! few peers beside blocks. + +use alloy_primitives::{B256, B512}; +use lru::LruCache; +use std::collections::HashSet; +use std::num::NonZeroUsize; + +/// rskj `BlockNodeInformation()`: `this(1000)`. +pub(crate) const MAX_BLOCKS: usize = 1000; + +pub(crate) struct BlockNodes { + /// rskj `nodesByBlock`: a `LinkedHashMap` in access order, so a read + /// touches a block as a write does. + nodes_by_block: LruCache>, +} + +impl BlockNodes { + pub(crate) fn new() -> Self { + Self::with_capacity(MAX_BLOCKS) + } + + pub(crate) fn with_capacity(max_blocks: usize) -> Self { + Self { + nodes_by_block: LruCache::new( + NonZeroUsize::new(max_blocks.max(1)).expect("at least one block"), + ), + } + } + + /// rskj `addBlockToNode`: `node` holds `block`. + pub(crate) fn add(&mut self, block: B256, node: B512) { + match self.nodes_by_block.get_mut(&block) { + Some(nodes) => { + nodes.insert(node); + } + None => { + self.nodes_by_block.put(block, HashSet::from([node])); + } + } + } + + /// rskj `getNodesByBlock`: the peers known to hold `block`, and none for a + /// block not remembered. + pub(crate) fn nodes(&mut self, block: B256) -> HashSet { + self.nodes_by_block.get(&block).cloned().unwrap_or_default() + } +} + +impl Default for BlockNodes { + fn default() -> Self { + Self::new() + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn block(n: u8) -> B256 { + B256::repeat_byte(n) + } + + fn node(n: u8) -> B512 { + B512::repeat_byte(n) + } + + #[test] + fn a_block_remembers_every_peer_that_holds_it() { + let mut nodes = BlockNodes::new(); + nodes.add(block(1), node(1)); + nodes.add(block(1), node(2)); + nodes.add(block(1), node(1)); + nodes.add(block(2), node(3)); + assert_eq!(nodes.nodes(block(1)), HashSet::from([node(1), node(2)])); + assert_eq!(nodes.nodes(block(2)), HashSet::from([node(3)])); + assert!( + nodes.nodes(block(3)).is_empty(), + "a block never heard of has no peers" + ); + } + + /// rskj keeps 1,000 blocks, and drops the one touched longest ago, a read + /// counting as a touch (`LinkedHashMap` in access order). + #[test] + fn the_block_touched_longest_ago_is_dropped_first() { + let mut nodes = BlockNodes::with_capacity(2); + nodes.add(block(1), node(1)); + nodes.add(block(2), node(2)); + // Read the first, so the second is now the one touched longest ago. + assert!(!nodes.nodes(block(1)).is_empty()); + nodes.add(block(3), node(3)); + assert_eq!(nodes.nodes(block(1)), HashSet::from([node(1)])); + assert!( + nodes.nodes(block(2)).is_empty(), + "the block touched longest ago went" + ); + assert_eq!(nodes.nodes(block(3)), HashSet::from([node(3)])); + } + + /// rskj's own figure, not the constant: the thousand-and-first block + /// touched pushes out the first, and only it. + #[test] + fn rskj_keeps_a_thousand_blocks() { + let mut nodes = BlockNodes::new(); + for n in 0..=1_000u64 { + nodes.add(B256::from(alloy_primitives::U256::from(n)), node(1)); + } + assert!(nodes + .nodes(B256::from(alloy_primitives::U256::ZERO)) + .is_empty()); + assert!(!nodes + .nodes(B256::from(alloy_primitives::U256::from(1u64))) + .is_empty()); + } +} diff --git a/crates/sync/src/events.rs b/crates/sync/src/events.rs index f34f2fa..b77c90b 100644 --- a/crates/sync/src/events.rs +++ b/crates/sync/src/events.rs @@ -51,6 +51,12 @@ pub enum SyncEvent { peer: B512, identifiers: Vec, }, + /// A whole block a peer sent unasked, as rskj sends one it mined + /// (`BlockMessage`), or in answer to a `GetBlockMessage`. + NewBlock { + peer: B512, + block: Box, + }, /// A peer's offer of a state it can serve. The blocks are unverified: /// their headers still have to be checked back to a block this node diff --git a/crates/sync/src/handler.rs b/crates/sync/src/handler.rs index e8e875a..299206b 100644 --- a/crates/sync/src/handler.rs +++ b/crates/sync/src/handler.rs @@ -301,6 +301,39 @@ impl SyncHandler { ))) } + /// Answer a `GetBlockMessage` with the whole block: rskj + /// `NodeBlockProcessor.processGetBlock` (L147-157), which answers from any + /// chain it holds, and first remembers that the asker holds the block now, + /// so it is not announced back to it. + /// + /// A peer sends one to the node that announced a block it lacks + /// (`processNewBlockHashesMessage`), so without an answer this node's + /// announcements could not be followed. A block whose header this node + /// holds compressed has no full form to send, and one without its body is + /// not whole; neither is answered, as rskj answers nothing for a block it + /// lacks. + fn serve_get_block(&self, peer: B512, hash: alloy_primitives::B256) -> Option { + let store = &self.manager.store; + let header = store.header(hash).ok()??; + if !self.may_serve(header.number) || header.extension_data.is_some() { + return None; + } + let (transactions, ommers) = store.body(hash).ok()??; + self.manager.note_block_holder(hash, peer); + trace!( + target: "rustock::sync", + "Serving block #{} {:?} to {:?}", + header.number, hash, &peer.0[..4] + ); + Some(P2pMessage::RskMessage(RskMessage::new( + RskSubMessage::Block(Box::new(rustock_core::Block { + header, + transactions, + ommers, + })), + ))) + } + /// Respond to a BlockHeadersRequest by walking backwards from the given /// hash, collecting up to `count` headers (same logic as rskj). fn serve_headers_request( @@ -613,6 +646,12 @@ impl P2pHandler for SyncHandler { identifiers: blocks.clone(), }); } + RskSubMessage::Block(block) => { + let _ = self.event_tx.send(SyncEvent::NewBlock { + peer: id, + block: block.clone(), + }); + } RskSubMessage::BodyResponse(r) => { trace!( @@ -649,6 +688,9 @@ impl P2pHandler for SyncHandler { RskSubMessage::BodyRequest(r) => { return self.serve_body_request(r.id, r.hash); } + RskSubMessage::GetBlock(hash) => { + return self.serve_get_block(id, *hash); + } // --- Snapshot sync --- RskSubMessage::SnapStatusRequest(r) => { diff --git a/crates/sync/src/lib.rs b/crates/sync/src/lib.rs index a2dcced..27f386b 100644 --- a/crates/sync/src/lib.rs +++ b/crates/sync/src/lib.rs @@ -1,3 +1,5 @@ +pub mod announce; +mod block_nodes; pub mod execution_commit; pub mod import; pub mod sampler; @@ -23,6 +25,7 @@ pub mod txpool; pub mod watchdog; // Re-exports for external consumers +pub use announce::{broadcast_block, on_mined}; pub use events::SyncEvent; pub use handler::SyncHandler; pub use manager::SyncManager; diff --git a/crates/sync/src/manager.rs b/crates/sync/src/manager.rs index 7979b52..e641d47 100644 --- a/crates/sync/src/manager.rs +++ b/crates/sync/src/manager.rs @@ -50,6 +50,10 @@ pub struct SyncManager { /// header difficulty. Bounded, because it is an optimisation and a peer /// could otherwise grow it without limit. proven: Mutex>, + /// The peers known to hold each block heard of lately, so that a block + /// is announced only to the peers that lack it. See + /// [`crate::block_nodes`]. + block_nodes: Mutex, } /// How many proved cumulative difficulties to keep. A skeleton chunk is 192 @@ -70,6 +74,7 @@ impl SyncManager { unlinked: Mutex::new(UnlinkedHeaders::new()), uncle_guard: Mutex::new(crate::uncle_guard::UncleGuard::new()), proven: Mutex::new(HashMap::new()), + block_nodes: Mutex::new(crate::block_nodes::BlockNodes::new()), } } @@ -470,6 +475,24 @@ impl SyncManager { .clear_peer(peer); } + /// Remembers that `peer` holds `block`: rskj + /// `BlockNodeInformation.addBlockToNode`. + pub(crate) fn note_block_holder(&self, block: B256, peer: B512) { + self.block_nodes + .lock() + .unwrap_or_else(PoisonError::into_inner) + .add(block, peer); + } + + /// The peers known to hold `block`: rskj + /// `BlockNodeInformation.getNodesByBlock`. + pub(crate) fn block_holders(&self, block: B256) -> std::collections::HashSet { + self.block_nodes + .lock() + .unwrap_or_else(PoisonError::into_inner) + .nodes(block) + } + /// Helper to create a headers request message. pub fn create_headers_request(&self, start_hash: B256, count: u32) -> P2pMessage { let req = BlockHeadersRequest { diff --git a/crates/sync/src/service.rs b/crates/sync/src/service.rs index 7aaa77e..e418b42 100644 --- a/crates/sync/src/service.rs +++ b/crates/sync/src/service.rs @@ -327,6 +327,11 @@ pub(crate) const STATUS_REFRESH: Duration = Duration::from_secs(10); /// Matches RSKj's `longSyncLimit` (default 24). const LONG_SYNC_LIMIT: u64 = 24; +/// How far above its best block rskj takes a block a peer names or sends: +/// `syncConfiguration.getChunkSize() * getMaxSkeletonChunks()`, 192 x 20 +/// (`NodeBlockProcessor.isAdvancedBlock`, `BlockSyncService.preprocessBlock`). +const SYNC_MAX_DISTANCE: u64 = 192 * MAX_SKELETON_CHUNKS as u64; + /// How far below an orphaned head to retreat when the fork point cannot be /// computed, because the branch that replaced us was never downloaded. /// @@ -3545,6 +3550,7 @@ impl SyncService { | SyncEvent::SkeletonResponse { .. } | SyncEvent::BlockHashResponse { .. } | SyncEvent::NewBlockHashes { .. } + | SyncEvent::NewBlock { .. } | SyncEvent::SnapStatusResponse { .. } | SyncEvent::SnapChunkResponse { .. } | SyncEvent::SnapBlocksResponse { .. } => { @@ -3583,7 +3589,8 @@ impl SyncService { | SyncEvent::SnapBlocksResponse { .. } => true, SyncEvent::BodyResponse { .. } | SyncEvent::BlockHashResponse { .. } - | SyncEvent::NewBlockHashes { .. } => false, + | SyncEvent::NewBlockHashes { .. } + | SyncEvent::NewBlock { .. } => false, }; if !claimed { trace!( @@ -3677,6 +3684,9 @@ impl SyncService { SyncEvent::NewBlockHashes { peer, identifiers } => { self.on_new_block_hashes(peer, identifiers).await; } + SyncEvent::NewBlock { peer, block } => { + self.on_new_block(peer, *block).await; + } } } @@ -4141,6 +4151,14 @@ impl SyncService { let our_height = self.our_head_number(); for id in &identifiers { + // rskj `processNewBlockHashesMessage` (L107-119): the announcer + // holds each block it names that is not too far ahead and not + // held here. + if id.number <= self.executed_number().saturating_add(SYNC_MAX_DISTANCE) + && !self.holds_block(id.hash) + { + self.manager.note_block_holder(id.hash, peer); + } if id.number > our_height { // New block ahead of our tip — request it let metadata = rustock_networking::peers::PeerMetadata { @@ -4187,6 +4205,149 @@ impl SyncService { } } + /// The height of the executed best block: rskj's + /// `blockchain.getBestBlock().getNumber()`, which its guards on a block a + /// peer names or sends measure from. + fn executed_number(&self) -> u64 { + let store = &self.manager.store; + store + .exec_head() + .ok() + .flatten() + .and_then(|(hash, _)| store.header(hash).ok().flatten()) + .map_or(0, |header| header.number) + } + + /// Whether this node holds the block whole, header and body: rskj's + /// `hasBlockInSomeBlockchain`, whose store holds only whole blocks. + fn holds_block(&self, hash: B256) -> bool { + let store = &self.manager.store; + matches!(store.header(hash), Ok(Some(_))) && matches!(store.body(hash), Ok(Some(_))) + } + + /// Announces a block taken from a peer to the peers not known to hold it: + /// rskj `MessageVisitor.tryRelayBlock` (L258-263), which relays only while + /// it has no better block to sync. Called only while following the tip, + /// this node's counterpart. See [`crate::announce::relay_block`]. + async fn relay_block(&self, hash: B256, number: u64) { + let holders = self.manager.block_holders(hash); + crate::announce::relay_block(&self.peer_store, &holders, hash, number).await; + } + + /// A whole block a peer sent: rskj `MessageVisitor.apply(BlockMessage)` + /// (L83-122) through `AsyncNodeBlockProcessor.processBlock` (L83-126), + /// rskj's default (`peer.fastBlockPropagation`), on this node's follow + /// path. + /// + /// rskj drops a block that is genesis, further above its best block than a + /// skeleton reaches, too far below it to be any block's uncle, or held + /// already; remembers that the sender holds it; checks its header, against + /// its parent too; and then relays it and queues it to be connected, best + /// or not. Here the body is held to its header's commitments, the header is + /// verified and stored as any header at the tip is (the same rules, the + /// extra data's size among them), the block is relayed, and it is executed + /// once it is the canonical block at its height. A block whose parent this + /// node lacks is taken as an announcement of it: the headers up to it are + /// asked of the sender, where rskj asks it for the missing block. + pub(crate) async fn on_new_block(&mut self, peer: B512, block: Block) { + // rskj processes it while syncing too, but relays nothing then + // (`hasBetterBlockToSync`); this node takes nothing unasked at the tip + // until it follows it, as with an announcement. + if !matches!(self.state, SyncState::Following) { + trace!( + target: "rustock::sync", + "Ignoring block #{} from {:?}: sync in progress", + block.header.number, + &peer.0[..4] + ); + return; + } + let number = block.header.number; + let best = self.executed_number(); + // rskj `isGenesis`, `isAdvancedBlock` and `canBeIgnoredForUnclesRewards` + // (`blockNumber < best - uncleGenerationLimit`), in that order. + if number == 0 + || number > best.saturating_add(SYNC_MAX_DISTANCE) + || number.saturating_add(rustock_core::validation::uncles::UNCLE_GENERATION_LIMIT) + < best + { + return; + } + let hash = block.hash(); + if self.holds_block(hash) { + return; + } + // `BlockSyncService.preprocessBlock` (L97-99). + self.manager.note_block_holder(hash, peer); + + let Block { + header, + transactions, + ommers, + } = block; + if let Some(what) = crate::body_check::body_mismatch(&header, &transactions, &ommers) { + self.charge_bad_body(peer, number, what); + return; + } + let before_height = self.our_head_number(); + let before_hash = self.manager.store.head().ok().flatten(); + match self + .manager + .handle_headers_response(vec![header.clone()], peer) + { + Ok(ingested) if ingested.rejected > 0 => { + if let Some(scoring) = &self.scoring { + scoring.record_peer(peer, rustock_networking::scoring::EventType::InvalidBlock); + } + return; + } + Ok(ingested) if ingested.unlinked > 0 => { + self.on_new_block_hashes(peer, vec![BlockIdentifier { hash, number }]) + .await; + return; + } + Ok(_) => {} + Err(e) => { + warn!( + target: "rustock::sync", + "Could not store the header of #{number} from {:?}: {e:?}", + &peer.0[..4] + ); + return; + } + } + if let Err(e) = self.manager.store.put_body(hash, &transactions, &ommers) { + error!(target: "rustock::sync", "Failed to store the body of #{number}: {e:?}"); + return; + } + self.relay_block(hash, number).await; + + // A block beside the chain is kept, and relayed, for a miner to name + // as an uncle; only the canonical one is executed. + if self.manager.store.canonical_hash(number).ok().flatten() != Some(hash) { + return; + } + let after_height = self.our_head_number(); + if self.manager.store.head().ok().flatten() != before_hash { + if after_height > before_height { + info!(target: "rustock::sync", "New tip: #{after_height}"); + } else { + info!( + target: "rustock::sync", + "Reorg: #{before_height} -> #{after_height} (new head {hash:?})" + ); + } + } + // Buffered as a fetched body is, in place of any request still out + // for it, whose answer would buffer the block again after it ran. + self.pending_follow_bodies + .retain(|_, pending| pending.0 != hash); + self.body_providers.insert(number, peer); + self.follow_buffer + .insert(number, (hash, header, transactions, ommers)); + self.drain_follow_buffer().await; + } + /// In Following state, periodically check if we've fallen too far behind /// and need to switch back to skeleton sync. pub(crate) async fn check_follow_gap(&mut self) { @@ -5133,6 +5294,7 @@ impl SyncService { }; self.body_providers.insert(header.number, peer); + let new_block = !self.holds_block(hash); if let Err(e) = self.manager.store.put_body(hash, &transactions, &uncles) { error!( target: "rustock::sync", @@ -5140,6 +5302,14 @@ impl SyncService { ); return; } + // rskj remembers the peer a block came from + // (`BlockSyncService.preprocessBlock`) and, once the block is + // whole and its header holds against its parent, announces it + // to the peers that lack it (`MessageVisitor.tryRelayBlock`). + self.manager.note_block_holder(hash, peer); + if new_block { + self.relay_block(hash, header.number).await; + } debug!( target: "rustock::sync", "Stored body for block #{} ({} txs) in follow mode", diff --git a/crates/sync/src/tests.rs b/crates/sync/src/tests.rs index 81f46a1..3922b76 100644 --- a/crates/sync/src/tests.rs +++ b/crates/sync/src/tests.rs @@ -3489,6 +3489,512 @@ async fn test_new_block_hashes_same_hash_ignored() { assert!(rx.try_recv().is_err(), "Should not request headers for same hash"); } +// -- Announcing and relaying new blocks, as rskj's `MessageVisitor` does ---- + +/// A node following the tip from genesis, with peers 1 to `peers` +/// connected, and each peer's inbox, to read what it was sent. +struct TipNode { + service: SyncService, + manager: Arc, + store: Arc, + genesis: B256, + inboxes: Vec<( + B512, + mpsc::Receiver, + )>, + _dir: tempfile::TempDir, +} + +async fn tip_node(peers: u8) -> TipNode { + tip_node_verifying(peers, HeaderVerifier::new()).await +} + +/// [`tip_node`], with the header rules `verifier` holds. +async fn tip_node_verifying(peers: u8, verifier: HeaderVerifier) -> TipNode { + let dir = tempdir().unwrap(); + let store = Arc::new(BlockStore::open(dir.path()).unwrap()); + let genesis = dummy_header(0, B256::ZERO, U256::from(1)); + store.update_head(&genesis, U256::from(1)).unwrap(); + let peer_store = Arc::new(rustock_networking::peers::PeerStore::new()); + let mut inboxes = Vec::new(); + for n in 1..=peers { + let (tx, rx) = mpsc::channel(rustock_networking::peers::PEER_CHANNEL_CAPACITY); + peer_store.add_peer(B512::repeat_byte(n), tx).await; + inboxes.push((B512::repeat_byte(n), rx)); + } + let manager = Arc::new(SyncManager::new( + store.clone(), + Arc::new(verifier), + peer_store.clone(), + )); + let (_event_tx, event_rx) = mpsc::unbounded_channel(); + let mut service = SyncService::new(manager.clone(), peer_store, event_rx); + service.state = SyncState::Following; + TipNode { + service, + manager, + store, + genesis: genesis.hash(), + inboxes, + _dir: dir, + } +} + +fn whole(header: Header) -> rustock_core::Block { + rustock_core::Block { + header, + transactions: Vec::new(), + ommers: Vec::new(), + } +} + +/// Every message a peer was sent, drained. +fn sent( + inbox: &mut mpsc::Receiver, +) -> Vec { + let mut out = Vec::new(); + while let Ok(message) = inbox.try_recv() { + match message { + rustock_networking::protocol::P2pMessage::RskMessage(m) => out.push(m.sub_message), + other => panic!("sent {other:?}"), + } + } + out +} + +/// The blocks a peer was told of, and nothing else sent to it. +fn told_of( + inbox: &mut mpsc::Receiver, +) -> Vec { + let mut out = Vec::new(); + for message in sent(inbox) { + match message { + rustock_networking::protocol::RskSubMessage::NewBlockHashes(ids) => out.extend(ids), + other => panic!("sent {other:?}"), + } + } + out +} + +/// rskj `MessageVisitor.apply(BlockMessage)`: a whole block a peer sends at +/// the tip is taken, and announced to the peers not known to hold it -- not to +/// the peer that sent it, nor to one that said it had it. A request still +/// out for its body is dropped, so the answer cannot buffer it again. +#[tokio::test] +async fn a_whole_block_at_the_tip_is_taken_and_relayed_to_the_peers_that_lack_it() { + let mut node = tip_node(3).await; + let (sender, other, holder) = ( + B512::repeat_byte(1), + B512::repeat_byte(2), + B512::repeat_byte(3), + ); + let b1 = dummy_header(1, node.genesis, U256::from(1)); + let hash = b1.hash(); + node.manager.note_block_holder(hash, holder); + node.service + .pending_follow_bodies + .insert(9, (hash, b1.clone(), Instant::now(), other)); + + node.service.on_new_block(sender, whole(b1)).await; + + assert!( + !node.service.pending_follow_bodies.contains_key(&9), + "the request for its body is dropped" + ); + + assert!( + node.store.header(hash).unwrap().is_some(), + "its header is stored" + ); + assert!(node.store.body(hash).unwrap().is_some(), "and its body"); + assert_eq!(node.store.head().unwrap(), Some(hash), "it is the new head"); + assert_eq!( + node.service.follow_buffer.get(&1).map(|entry| entry.0), + Some(hash), + "and waits to run" + ); + assert_eq!( + node.service.body_providers.get(&1), + Some(&sender), + "the sender is credited once it runs" + ); + for (id, inbox) in &mut node.inboxes { + let told = told_of(inbox); + if *id == other { + assert_eq!(told, vec![BlockIdentifier { hash, number: 1 }]); + } else { + assert!(told.is_empty(), "{id:?} holds the block and was told of it"); + } + } +} + +/// rskj relays a block it does not make its best too (`IMPORTED_NOT_BEST`): +/// a miner may name it as an uncle. It is kept, not run. Sent here as the +/// sync service receives it, as an event. +#[tokio::test] +async fn a_block_beside_the_chain_is_kept_and_relayed_but_not_run() { + let mut node = tip_node(3).await; + let b1 = dummy_header(1, node.genesis, U256::from(1)); + node.service + .on_new_block(B512::repeat_byte(1), whole(b1.clone())) + .await; + for (_, inbox) in &mut node.inboxes { + let _ = sent(inbox); + } + + let sibling = fork_header(1, node.genesis, U256::from(1), 0xee); + let hash = sibling.hash(); + node.service + .handle_event(crate::SyncEvent::NewBlock { + peer: B512::repeat_byte(2), + block: Box::new(whole(sibling)), + }) + .await; + + assert!(node.store.header(hash).unwrap().is_some()); + assert!(node.store.body(hash).unwrap().is_some(), "kept whole"); + assert_eq!( + node.store.head().unwrap(), + Some(b1.hash()), + "the head stays" + ); + assert_eq!( + node.service.follow_buffer.get(&1).map(|entry| entry.0), + Some(b1.hash()), + "the canonical block is the one that runs" + ); + for (id, inbox) in &mut node.inboxes { + let told = told_of(inbox); + if *id == B512::repeat_byte(2) { + assert!(told.is_empty(), "the sender was told of its own block"); + } else { + assert_eq!(told, vec![BlockIdentifier { hash, number: 1 }], "{id:?}"); + } + } +} + +/// rskj's guards on a block a peer sends (`MessageVisitor.apply`): genesis, +/// further above the best block than a skeleton reaches, so far below it that +/// it can be no block's uncle, held already, or sent while syncing. None of +/// them is stored or relayed; nor is a block whose body is not its header's, +/// or whose header fails its rules. Each case passes every guard but its own. +#[tokio::test] +async fn what_a_node_cannot_use_is_neither_taken_nor_relayed() { + // The executed head at #10, so a block below #3 can be no block's uncle. + let at_ten = || async { + let node = tip_node(2).await; + let h10 = dummy_header(10, B256::repeat_byte(0x10), U256::from(1)); + node.store.put_header(&h10).unwrap(); + node.store.set_exec_head(h10.hash(), B256::ZERO).unwrap(); + node + }; + let sender = B512::repeat_byte(1); + let unseen = |node: &mut TipNode, hash: B256, what: &str| { + assert!(node.store.header(hash).unwrap().is_none(), "{what}: stored"); + for (id, inbox) in &mut node.inboxes { + assert!(sent(inbox).is_empty(), "{what}: {id:?} was sent something"); + } + }; + + let mut node = tip_node(2).await; + let genesis_again = fork_header(0, B256::ZERO, U256::from(1), 0x01); + let hash = genesis_again.hash(); + node.service + .on_new_block(sender, whole(genesis_again)) + .await; + unseen(&mut node, hash, "genesis"); + + let mut node = at_ten().await; + let too_far = dummy_header(10 + 3_841, B256::repeat_byte(0x22), U256::from(1)); + let hash = too_far.hash(); + node.service.on_new_block(sender, whole(too_far)).await; + unseen(&mut node, hash, "too far ahead"); + + let mut node = at_ten().await; + let too_old = dummy_header(2, B256::repeat_byte(0x33), U256::from(1)); + let hash = too_old.hash(); + node.service.on_new_block(sender, whole(too_old)).await; + unseen(&mut node, hash, "too old for an uncle"); + + let mut node = tip_node(2).await; + node.service.state = SyncState::DownloadingSkeleton { + peer: sender, + peer_best: 99, + connection_point: 0, + }; + let b1 = dummy_header(1, node.genesis, U256::from(1)); + let hash = b1.hash(); + node.service.on_new_block(sender, whole(b1)).await; + unseen(&mut node, hash, "while syncing"); + + let mut node = tip_node(2).await; + let b1 = dummy_header(1, node.genesis, U256::from(1)); + let hash = b1.hash(); + let mut not_its_body = whole(b1); + not_its_body + .ommers + .push(dummy_header(0, B256::repeat_byte(0x44), U256::from(1))); + node.service.on_new_block(sender, not_its_body).await; + unseen(&mut node, hash, "a body that is not the header's"); + + // On genesis, but numbered #5: `BlockNumberRule` refuses it. + let mut node = tip_node_verifying( + 2, + HeaderVerifier::new().with_parent_rule(rustock_core::validation::BlockNumberRule), + ) + .await; + let misnumbered = dummy_header(5, node.genesis, U256::from(1)); + let hash = misnumbered.hash(); + node.service.on_new_block(sender, whole(misnumbered)).await; + unseen(&mut node, hash, "a header its rules refuse"); + + let mut node = tip_node(3).await; + let b1 = dummy_header(1, node.genesis, U256::from(1)); + node.service.on_new_block(sender, whole(b1.clone())).await; + for (_, inbox) in &mut node.inboxes { + let _ = sent(inbox); + } + node.service + .on_new_block(B512::repeat_byte(2), whole(b1)) + .await; + for (id, inbox) in &mut node.inboxes { + assert!( + sent(inbox).is_empty(), + "held already: {id:?} was sent something" + ); + } +} + +/// A block that builds on the executed head goes on to execution, as a +/// fetched body does (`drain_follow_buffer`). +#[tokio::test] +async fn a_whole_block_on_the_executed_head_is_handed_to_execution() { + let mut node = tip_node(1).await; + node.store.set_exec_head(node.genesis, B256::ZERO).unwrap(); + let b1 = dummy_header(1, node.genesis, U256::from(1)); + node.service + .on_new_block(B512::repeat_byte(1), whole(b1)) + .await; + assert!(node.service.follow_buffer.is_empty(), "left waiting"); +} + +/// rskj `NodeBlockProcessor.processNewBlockHashesMessage`: the peer that +/// announces a block is remembered as holding it, so the block is not +/// relayed back to it; not for a block further ahead than a skeleton +/// reaches, nor for one held here already. +#[tokio::test] +async fn an_announcer_is_remembered_for_the_blocks_it_could_be_asked_for() { + let mut node = tip_node(1).await; + let h10 = dummy_header(10, B256::repeat_byte(0x10), U256::from(1)); + node.store.put_header(&h10).unwrap(); + node.store.put_body(h10.hash(), &[], &[]).unwrap(); + node.store.set_exec_head(h10.hash(), B256::ZERO).unwrap(); + let announcer = B512::repeat_byte(1); + let (edge, beyond) = (B256::repeat_byte(0x61), B256::repeat_byte(0x62)); + node.service + .on_new_block_hashes( + announcer, + vec![ + BlockIdentifier { + hash: edge, + number: 10 + 3_840, + }, + BlockIdentifier { + hash: beyond, + number: 10 + 3_841, + }, + BlockIdentifier { + hash: h10.hash(), + number: 10, + }, + ], + ) + .await; + assert!( + node.manager.block_holders(edge).contains(&announcer), + "a skeleton ahead" + ); + assert!( + node.manager.block_holders(beyond).is_empty(), + "beyond a skeleton" + ); + assert!( + node.manager.block_holders(h10.hash()).is_empty(), + "held here" + ); +} + +/// A `BlockMessage` is handed whole to the sync service, with its sender, +/// and nothing is sent back. +#[tokio::test] +async fn a_block_a_peer_sends_is_handed_to_the_sync_service() { + use rustock_networking::protocol::{P2pHandler, P2pMessage, RskMessage, RskSubMessage}; + + let dir = tempdir().unwrap(); + let manager = Arc::new(SyncManager::new( + Arc::new(BlockStore::open(dir.path()).unwrap()), + Arc::new(HeaderVerifier::new()), + Arc::new(rustock_networking::peers::PeerStore::new()), + )); + let (event_tx, mut event_rx) = mpsc::unbounded_channel(); + let handler = SyncHandler::new(manager, event_tx); + let sender = B512::repeat_byte(4); + let block = whole(dummy_header(1, B256::repeat_byte(1), U256::from(1))); + let message = P2pMessage::RskMessage(RskMessage::new(RskSubMessage::Block(Box::new( + block.clone(), + )))); + assert!(handler.handle_message(sender, &message).is_none()); + let Ok(crate::SyncEvent::NewBlock { peer, block: got }) = event_rx.try_recv() else { + panic!("not handed on as a new block"); + }; + assert_eq!(peer, sender); + assert_eq!(got.hash(), block.hash()); +} + +/// The edges of rskj's two distance guards are taken: a block a whole +/// skeleton above the best one, and one just young enough to be an uncle. Each +/// has no parent here, so each is taken as an announcement of itself: its +/// headers are asked of the sender, and nothing is relayed. +#[tokio::test] +async fn a_block_at_the_edge_of_either_guard_is_taken() { + for number in [10 + 3_840, 3] { + let mut node = tip_node(2).await; + let h10 = dummy_header(10, B256::repeat_byte(0x10), U256::from(1)); + node.store.put_header(&h10).unwrap(); + node.store.set_exec_head(h10.hash(), B256::ZERO).unwrap(); + let orphan = dummy_header(number, B256::repeat_byte(0x55), U256::from(1)); + node.service + .on_new_block(B512::repeat_byte(1), whole(orphan)) + .await; + let (_, inbox) = &mut node.inboxes[0]; + assert!( + matches!( + sent(inbox).as_slice(), + [rustock_networking::protocol::RskSubMessage::BlockHeadersRequest(_)] + ), + "#{number}: its headers are asked of the sender" + ); + let (_, other) = &mut node.inboxes[1]; + assert!( + sent(other).is_empty(), + "#{number}: relayed without its parent" + ); + } +} + +/// A block fetched at the tip, header then body, is announced once it is +/// whole, to the peers not known to hold it: not to the peer that sent the +/// body, nor to the one that announced it. Once, however often it arrives. +#[tokio::test] +async fn a_body_fetched_at_the_tip_is_relayed_once_to_the_peers_that_lack_it() { + let mut node = tip_node(3).await; + let (provider, announcer, other) = ( + B512::repeat_byte(1), + B512::repeat_byte(2), + B512::repeat_byte(3), + ); + let b1 = dummy_header(1, node.genesis, U256::from(1)); + let hash = b1.hash(); + node.manager + .handle_headers_response(vec![b1.clone()], provider) + .unwrap(); + node.service + .on_new_block_hashes(announcer, vec![BlockIdentifier { hash, number: 1 }]) + .await; + for request in [7u64, 8] { + node.service + .pending_follow_bodies + .insert(request, (hash, b1.clone(), Instant::now(), provider)); + node.service + .on_body_response(provider, request, Vec::new(), Vec::new(), None) + .await; + } + + assert!(node.store.body(hash).unwrap().is_some()); + for (id, inbox) in &mut node.inboxes { + let told = told_of(inbox); + if *id == other { + assert_eq!(told, vec![BlockIdentifier { hash, number: 1 }], "told once"); + } else { + assert!(told.is_empty(), "{id:?} holds the block and was told of it"); + } + } +} + +/// rskj `NodeBlockProcessor.processGetBlock`: a block asked for by hash is +/// sent whole, and its asker is remembered as holding it. Nothing for a block +/// not held whole, or held only compressed, which has no full form to send. +#[tokio::test] +async fn a_block_asked_for_by_hash_is_sent_whole_and_its_asker_remembered() { + use rustock_networking::protocol::{P2pHandler, P2pMessage, RskMessage, RskSubMessage}; + + let handler_over = |store: Arc| { + let manager = Arc::new(SyncManager::new( + store, + Arc::new(HeaderVerifier::new()), + Arc::new(rustock_networking::peers::PeerStore::new()), + )); + let (event_tx, _event_rx) = mpsc::unbounded_channel(); + (SyncHandler::new(manager.clone(), event_tx), manager) + }; + let ask = |hash| P2pMessage::RskMessage(RskMessage::new(RskSubMessage::GetBlock(hash))); + let asker = B512::repeat_byte(5); + + let dir = tempdir().unwrap(); + let store = Arc::new(BlockStore::open(dir.path()).unwrap()); + let genesis = dummy_header(0, B256::ZERO, U256::from(1)); + let b1 = dummy_header(1, genesis.hash(), U256::from(1)); + let hash = b1.hash(); + let headless = dummy_header(2, hash, U256::from(1)); + store.put_header(&b1).unwrap(); + store.put_header(&headless).unwrap(); + store.put_body(hash, &[], &[]).unwrap(); + let (handler, manager) = handler_over(store); + + let Some(P2pMessage::RskMessage(answer)) = handler.handle_message(asker, &ask(hash)) else { + panic!("a block held whole is sent"); + }; + let RskSubMessage::Block(block) = answer.sub_message else { + panic!("sent as a block"); + }; + assert_eq!(block.hash(), hash); + assert!( + manager.block_holders(hash).contains(&asker), + "the asker holds it now" + ); + for (missing, what) in [ + (B256::repeat_byte(0x77), "not held"), + (headless.hash(), "held without its body"), + ] { + assert!( + handler.handle_message(asker, &ask(missing)).is_none(), + "{what}" + ); + assert!( + manager.block_holders(missing).is_empty(), + "{what}: the asker remembered" + ); + } + + let store = rskip351_store(); + let held_compressed = compressed( + dummy_header(1, genesis.hash(), U256::from(1)), + &an_extension(3), + ); + let hash = held_compressed.hash(); + store.put_header(&held_compressed).unwrap(); + store.put_body(hash, &[], &[]).unwrap(); + let (handler, manager) = handler_over(store); + assert!( + handler.handle_message(asker, &ask(hash)).is_none(), + "held compressed" + ); + assert!( + manager.block_holders(hash).is_empty(), + "held compressed: the asker remembered" + ); +} + #[tokio::test] async fn test_reorg_via_headers_response_higher_td() { let dir = tempdir().unwrap(); From bfe1bff425cd3965376c20e4a1202134386f906d Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Tue, 10 Nov 2026 07:59:26 -0800 Subject: [PATCH 077/264] sync: a mined block's committed state becomes the node's in-memory root The miner's hook now runs once the block is stored and, if it won, the head, as rskj's EthereumImpl.addNewMinedBlock runs after tryToConnect. It tells the sync service, which takes the committed state root rather than leaving it at the parent until the next reconcile (upstream #323). --- crates/cli/src/main.rs | 12 +- crates/execution/src/mining/server.rs | 21 ++- crates/execution/src/mining/tests.rs | 49 +++++++ crates/sync/src/announce.rs | 29 +++- crates/sync/src/events.rs | 7 + crates/sync/src/service.rs | 40 ++++++ crates/sync/src/tests.rs | 185 ++++++++++++++++++++++++++ 7 files changed, 327 insertions(+), 16 deletions(-) diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index a24460a..038c450 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -2187,6 +2187,8 @@ async fn run(local_offset: Option) -> Result<()> { }; let (event_tx, event_rx) = tokio::sync::mpsc::unbounded_channel(); + // The miner's: it tells the sync service of every block it commits (#323). + let mined_events = event_tx.clone(); let sync_handler = Arc::new( SyncHandler::new(sync_manager.clone(), event_tx) .with_serve_ceiling(args.simulate_height) @@ -3405,11 +3407,15 @@ async fn run(local_offset: Option) -> Result<()> { // What the sync service's import path does for a block that arrived // from a peer, done here for one this node mined; then what rskj's // `EthereumImpl.addNewMinedBlock` does with a mined block once it is - // stored, best or not: send it to the peers. + // connected: the sync service takes its state as the node's, and the + // peers are sent it, best or not. let on_committed: Arc = { let pool = pool.clone(); - let broadcast = - rustock_sync::on_mined(peer_store.clone(), tokio::runtime::Handle::current()); + let broadcast = rustock_sync::on_mined( + peer_store.clone(), + mined_events.clone(), + tokio::runtime::Handle::current(), + ); Arc::new(move |block: &rustock_core::Block| { pool.remove_mined(&block.transactions); pool.evict_outdated(block.header.number); diff --git a/crates/execution/src/mining/server.rs b/crates/execution/src/mining/server.rs index e832049..920f2ba 100644 --- a/crates/execution/src/mining/server.rs +++ b/crates/execution/src/mining/server.rs @@ -164,6 +164,11 @@ pub struct StoreChainAccess { /// A closure rather than a pool handle because this crate does not /// know what a pool is, and should not have to in order to say "this /// block is now committed". + /// + /// Called once the block is stored and, if it won, the head and the + /// executed head: rskj's `EthereumImpl.addNewMinedBlock` (L60-68) tells + /// the rest of the node after `blockchain.tryToConnect` has connected + /// it, so whatever the hook tells sees the chain with the block on it. pub on_committed: Option>, /// Best blocks between trie flushes: rskj's /// `blockchain.flushNumberOfBlocks`, whose `BlockChainFlusher` flushes @@ -341,11 +346,6 @@ impl ChainAccess for StoreChainAccess { switch_chain += laps.lap(); - if let Some(hook) = &self.on_committed { - hook(block); - } - on_best_block += laps.lap(); - let result = if td > current_td { self.store.update_canonical_chain(hash).map_err(|e| e.to_string())?; self.store.set_exec_head(hash, state_root).map_err(|e| e.to_string())?; @@ -357,12 +357,21 @@ impl ChainAccess for StoreChainAccess { if self.flush_due() { self.trie_store.flush(); } - on_best_block += laps.lap(); ImportResult::ImportedBest } else { ImportResult::ImportedNotBest }; + // Only now, with the block stored and, if it won, the head: rskj's + // `EthereumImpl.addNewMinedBlock` (L60-68) broadcasts a mined block + // once `blockchain.tryToConnect` has connected it, and the node's sync + // service, told from here, reads the executed head this just set + // (#323). + if let Some(hook) = &self.on_committed { + hook(block); + } + on_best_block += laps.lap(); + info!( target: "rustock::mining", "Mined block #{} {:?} imported: {}", number, hash, result.as_str() diff --git a/crates/execution/src/mining/tests.rs b/crates/execution/src/mining/tests.rs index 436e167..0b1cab6 100644 --- a/crates/execution/src/mining/tests.rs +++ b/crates/execution/src/mining/tests.rs @@ -2770,6 +2770,55 @@ fn by_default_every_mined_block_flushes() { } } +/// rskj's `EthereumImpl.addNewMinedBlock` tells the rest of the node of a +/// mined block once `blockchain.tryToConnect` has connected it: the hook sees +/// the block as the head and the executed head, so the sync service it tells +/// reads the block's state as the node's (#323). +#[test] +fn the_commit_hook_runs_once_the_block_is_the_head() { + let dir = tempfile::tempdir().unwrap(); + let store = Arc::new(BlockStore::open(dir.path()).unwrap()); + let trie_store: Arc = Arc::new(MemoryTrieStore::new()); + let root = TrieNode::empty().compute_hash(trie_store.as_ref()); + let parent = header_at(0, B256::ZERO, root); + let parent_hash = parent.hash(); + store.put_header_with_hash(parent_hash, &parent).unwrap(); + store.put_canonical_hash(0, parent_hash).unwrap(); + store + .put_total_difficulty(parent_hash, U256::from(1)) + .unwrap(); + store.set_head(parent_hash).unwrap(); + store.set_exec_head(parent_hash, root).unwrap(); + + let seen = Arc::new(std::sync::Mutex::new(Vec::new())); + let hook: Arc = { + let (store, seen) = (store.clone(), seen.clone()); + Arc::new(move |_block: &rustock_core::Block| { + let exec_head = store.exec_head().unwrap(); + let canonical = store.canonical_hash(1).unwrap(); + seen.lock().unwrap().push((exec_head, canonical)); + }) + }; + let chain = StoreChainAccess::new(store.clone(), trie_store, Some(hook)); + let block = rustock_core::Block { + header: header_at(1, parent_hash, root), + transactions: Vec::new(), + ommers: Vec::new(), + }; + let hash = block.hash(); + + let outcome = chain + .commit_mined_block(&block, root, &mut crate::breakdown::Laps::new(false)) + .unwrap(); + + assert_eq!(outcome.result, ImportResult::ImportedBest); + assert_eq!( + *seen.lock().unwrap(), + vec![(Some((hash, root)), Some(hash))], + "the hook ran once, with the block the executed head and canonical" + ); +} + /// The thread mines on its clock: at a 100 ms median (the floor), a second /// of running yields several blocks, and stopping ends it. #[test] diff --git a/crates/sync/src/announce.rs b/crates/sync/src/announce.rs index f0e78b7..7713e10 100644 --- a/crates/sync/src/announce.rs +++ b/crates/sync/src/announce.rs @@ -63,15 +63,20 @@ pub async fn broadcast_block(peer_store: &PeerStore, block: &Block) -> Vec whole } -/// What this node does with a block it mined once the block is stored, best -/// or not: send it to the peers, as rskj's `EthereumImpl.addNewMinedBlock` -/// (L60-68) does through [`broadcast_block`]. The miner commits from a -/// blocking thread, so the sending is handed to `runtime`. +/// What this node does with a block it mined once the block is stored and, +/// if it won, the head: what rskj's `EthereumImpl.addNewMinedBlock` (L60-68) +/// does after `blockchain.tryToConnect`. The node's own sync service is told +/// ([`crate::SyncEvent::MinedBlock`]), so it takes the state the miner +/// committed (#323), and the block goes to the peers ([`broadcast_block`]), +/// best or not. The miner commits from a blocking thread, so the sending is +/// handed to `runtime`. pub fn on_mined( peer_store: std::sync::Arc, + events: tokio::sync::mpsc::UnboundedSender, runtime: tokio::runtime::Handle, ) -> impl Fn(&Block) + Send + Sync { move |block: &Block| { + let _ = events.send(crate::SyncEvent::MinedBlock { hash: block.hash() }); let peer_store = peer_store.clone(); let block = block.clone(); runtime.spawn(async move { @@ -207,19 +212,29 @@ mod tests { } } - /// The miner commits from a blocking thread: the block still reaches every - /// peer. + /// The miner commits from a blocking thread: the node's own sync service + /// hears of the block, and the block still reaches every peer. #[tokio::test] - async fn a_block_mined_on_a_blocking_thread_reaches_every_peer() { + async fn a_mined_block_reaches_the_sync_service_and_every_peer() { let (store, mut inboxes) = store_of(4).await; + let (events, mut sync_service) = mpsc::unbounded_channel(); let on_mined = on_mined( std::sync::Arc::new(store), + events, tokio::runtime::Handle::current(), ); let mined = block(7); + let hash = mined.hash(); tokio::task::spawn_blocking(move || on_mined(&mined)) .await .unwrap(); + assert!( + matches!( + sync_service.try_recv(), + Ok(crate::SyncEvent::MinedBlock { hash: told }) if told == hash + ), + "the sync service was not told" + ); for (id, inbox) in &mut inboxes { let heard = tokio::time::timeout(std::time::Duration::from_secs(10), inbox.recv()).await; diff --git a/crates/sync/src/events.rs b/crates/sync/src/events.rs index b77c90b..5c6b56f 100644 --- a/crates/sync/src/events.rs +++ b/crates/sync/src/events.rs @@ -57,6 +57,13 @@ pub enum SyncEvent { peer: B512, block: Box, }, + /// A block this node mined, now stored and, if it won, the head and the + /// executed head: rskj connects a mined block through the same + /// `blockchain.tryToConnect` as a peer's (`EthereumImpl.addNewMinedBlock`), + /// so the node's state is the block's once it is best. + MinedBlock { + hash: B256, + }, /// A peer's offer of a state it can serve. The blocks are unverified: /// their headers still have to be checked back to a block this node diff --git a/crates/sync/src/service.rs b/crates/sync/src/service.rs index e418b42..5e2ee71 100644 --- a/crates/sync/src/service.rs +++ b/crates/sync/src/service.rs @@ -2251,6 +2251,39 @@ impl SyncService { } } + /// A block this node mined, committed by the miner: rskj connects a mined + /// block through the same `blockchain.tryToConnect` as a peer's + /// (`EthereumImpl.addNewMinedBlock`, L60-68), so once it is best the + /// node's state is the block's. The miner executed it and committed its + /// state, so the in-memory root takes that state rather than executing + /// the block again, as for any block this node has executed + /// (`fast_forward_state`). + /// + /// Without this the root stayed at the block's parent until the tick's + /// reconcile reloaded it, with a warning for every block mined, and a + /// block from a peer arriving in between met a root behind the executed + /// head (#323). + pub(crate) fn on_mined_block(&mut self, hash: B256) { + let Some(trie_store) = self.trie_store.clone() else { + return; + }; + // Only while it is the executed head: a block that lost to a heavier + // one, or that a later block has already followed, moves nothing. + let Ok(Some((head, committed))) = self.manager.store.exec_head() else { + return; + }; + if head != hash { + return; + } + if let Some(data) = trie_store.get(committed.as_slice()) { + self.current_state_root = Some(TrieNode::from_message(&data, trie_store.as_ref())); + debug!( + target: "rustock::sync", + "Took mined block {hash:?}'s committed state {committed:?} as the in-memory root" + ); + } + } + /// Re-queue canonical blocks this node already holds but has not executed. /// /// `drain_follow_buffer` is reachable from exactly one place: @@ -3561,6 +3594,8 @@ impl SyncService { ); return; } + // Not from a peer: the node's own miner. + SyncEvent::MinedBlock { .. } => {} } } @@ -3587,6 +3622,8 @@ impl SyncService { SyncEvent::SnapStatusResponse { .. } | SyncEvent::SnapChunkResponse { .. } | SyncEvent::SnapBlocksResponse { .. } => true, + // Not chain data from a peer: the node's own miner. + SyncEvent::MinedBlock { .. } => true, SyncEvent::BodyResponse { .. } | SyncEvent::BlockHashResponse { .. } | SyncEvent::NewBlockHashes { .. } @@ -3687,6 +3724,9 @@ impl SyncService { SyncEvent::NewBlock { peer, block } => { self.on_new_block(peer, *block).await; } + SyncEvent::MinedBlock { hash } => { + self.on_mined_block(hash); + } } } diff --git a/crates/sync/src/tests.rs b/crates/sync/src/tests.rs index 3922b76..e704d97 100644 --- a/crates/sync/src/tests.rs +++ b/crates/sync/src/tests.rs @@ -5610,6 +5610,191 @@ async fn a_matching_state_root_is_left_alone() { assert_eq!(service.follow_buffer.len(), 1, "cleared the buffer when nothing was wrong"); } +// -- #323: a block this node mined reaches the in-memory state root --------- + +/// A service whose in-memory root is genesis's state, over a store whose +/// head is `mined`, genesis's child, with a state of its own in the trie. +struct MinedNode { + store: Arc, + trie: Arc, + service: SyncService, + mined: Header, + mined_root: B256, + _dir: tempfile::TempDir, +} + +fn mined_on_genesis() -> MinedNode { + let dir = tempdir().unwrap(); + let store = Arc::new(BlockStore::open(dir.path()).unwrap()); + let genesis = dummy_header(0, B256::ZERO, U256::from(1)); + store.update_head(&genesis, U256::from(1)).unwrap(); + let mined = dummy_header(1, genesis.hash(), U256::from(1)); + store.update_head(&mined, U256::from(2)).unwrap(); + + let trie = Arc::new(rustock_trie::MemoryTrieStore::new()) as Arc; + let before = rustock_trie::TrieNode::empty(); + let before_root = before.compute_hash(trie.as_ref()); + trie.put(before_root.as_slice(), &before.to_message(trie.as_ref())); + let mut after = rustock_trie::TrieNode::empty().put( + &rustock_trie::TrieKeySlice::from_key(&[1u8, 2, 3]), + &[9u8; 8], + trie.as_ref(), + ); + after.save(trie.as_ref(), true); + let mined_root = after.compute_hash(trie.as_ref()); + assert_ne!(mined_root, before_root, "fixture did not create two states"); + + let peer_store = Arc::new(rustock_networking::peers::PeerStore::new()); + let manager = Arc::new(SyncManager::new( + store.clone(), + Arc::new(HeaderVerifier::new()), + peer_store.clone(), + )); + let (_tx, event_rx) = mpsc::unbounded_channel(); + let processor = rustock_execution::BlockProcessor::new( + rustock_execution::RskHardforkConfig::mainnet(), + store.clone(), + ); + let service = SyncService::new(manager, peer_store, event_rx).with_block_processor( + processor, + trie.clone(), + before, + ); + MinedNode { + store, + trie, + service, + mined, + mined_root, + _dir: dir, + } +} + +/// #323. The miner commits the blocks it mines, not this service, so the +/// service is told: rskj connects a mined block through the same +/// `tryToConnect` as a block from a peer. The in-memory root takes the mined +/// block's committed state, quietly: nothing queued is dropped, and the tick's +/// reconcile, which reloaded the root with a warning for every block mined, +/// finds nothing to do. +#[tokio::test] +async fn a_mined_block_becomes_the_in_memory_state_root() { + let MinedNode { + store, + trie, + mut service, + mined, + mined_root, + _dir, + } = mined_on_genesis(); + store.set_exec_head(mined.hash(), mined_root).unwrap(); + service.follow_buffer.insert( + 2, + ( + B256::repeat_byte(2), + dummy_header(2, mined.hash(), U256::from(1)), + vec![], + vec![], + ), + ); + + service + .handle_event(crate::SyncEvent::MinedBlock { hash: mined.hash() }) + .await; + + assert_eq!( + service + .current_state_root + .as_ref() + .map(|r| r.compute_hash(trie.as_ref())), + Some(mined_root), + "the mined block's state is the in-memory root" + ); + service.resync_state_root_with_exec_head(); + assert_eq!( + service.follow_buffer.len(), + 1, + "the reconcile still found the two apart" + ); +} + +/// Only while the mined block is the executed head: one that lost to a +/// heavier block leaves the in-memory root to the block that won. +#[tokio::test] +async fn a_mined_block_that_is_not_the_executed_head_moves_nothing() { + let MinedNode { + store, + trie, + mut service, + mined, + mined_root, + _dir, + } = mined_on_genesis(); + let held = |service: &SyncService| { + service + .current_state_root + .as_ref() + .map(|r| r.compute_hash(trie.as_ref())) + }; + let before = held(&service); + // A heavier sibling is the executed head, with a state of its own. + let heavier = fork_header(1, mined.parent_hash, U256::from(5), 0xbb); + store.update_head(&heavier, U256::from(6)).unwrap(); + store.set_exec_head(heavier.hash(), mined_root).unwrap(); + + service + .handle_event(crate::SyncEvent::MinedBlock { hash: mined.hash() }) + .await; + + assert_eq!( + held(&service), + before, + "the root moved for a block that is not the head" + ); +} + +/// `--follow-up false` drops chain data from peers, and a snapshot session +/// keeps it from the ordinary sync, but a block this node mined is not that: +/// its state is still the node's either way. +#[tokio::test] +async fn a_serving_or_snapshotting_node_still_takes_the_state_of_a_block_it_mined() { + for snapshotting in [false, true] { + let MinedNode { + store, + trie, + mut service, + mined, + mined_root, + _dir, + } = mined_on_genesis(); + if snapshotting { + service.start_snap_sync( + crate::snap::SnapConfig { + client_enabled: true, + ..Default::default() + }, + Arc::new(HeaderVerifier::new()), + ); + assert!(service.is_suspended_for_snap_for_test()); + } else { + service.follow_up = false; + } + store.set_exec_head(mined.hash(), mined_root).unwrap(); + + service + .handle_event(crate::SyncEvent::MinedBlock { hash: mined.hash() }) + .await; + + assert_eq!( + service + .current_state_root + .as_ref() + .map(|r| r.compute_hash(trie.as_ref())), + Some(mined_root), + "snapshotting {snapshotting}" + ); + } +} + /// The 2026-09-24 15:41 stall: follow mode holding canonical headers above the /// executed head with no bodies, and nothing in flight. /// From b22c3db3bc0949d7dd00dbb6017b8eaec9abcec6 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Tue, 10 Nov 2026 07:59:26 -0800 Subject: [PATCH 078/264] sync: a reorg while a block executes is a warning, not an error The block isn't recorded, and is executed again or re-adopted once the chain settles: the chain moved, nothing failed. Anything else that stops the record is still an error. --- crates/sync/src/service.rs | 59 +++++++-- crates/sync/src/tests.rs | 239 ++++++++++++++++++++++++++++++++++++- 2 files changed, 290 insertions(+), 8 deletions(-) diff --git a/crates/sync/src/service.rs b/crates/sync/src/service.rs index 5e2ee71..3625d4a 100644 --- a/crates/sync/src/service.rs +++ b/crates/sync/src/service.rs @@ -238,10 +238,23 @@ pub(crate) fn fast_forward_state( /// be logged as `Executed block` one line after the store refused it, on /// 2026-09-24 at 14:01:15, and why the coherence check then had to reload the /// committed root five seconds later. +/// +/// That refusal is the reorg being handled, not a failure: the block is not +/// recorded, and is executed again or re-adopted once the chain settles. So +/// it is a warning; anything else that stops the record is an error. Testnet +/// logged #8,274,238's as an error on 9 November. pub(crate) fn record_execution(store: &BlockStore, hash: B256, state_root: B256) -> bool { match Validated::prove(store, hash) { Ok(at) => match store.apply(&Transition::Executed { at, state_root }) { Ok(_) => true, + Err(e) if no_longer_canonical(store, hash) => { + warn!( + target: "rustock::sync", + "Executed {:?}, but a reorg moved the chain while it ran; not recording it: {}", + hash, e + ); + false + } Err(e) => { error!(target: "rustock::sync", "Recording execution of {:?} failed: {:?}", hash, e); false @@ -258,6 +271,16 @@ pub(crate) fn record_execution(store: &BlockStore, hash: B256, state_root: B256) } } +/// Whether `hash` has stopped being the canonical block at its height: what a +/// reorg landing between executing a block and recording it leaves. A store +/// that cannot say is not taken for one. +pub(crate) fn no_longer_canonical(store: &BlockStore, hash: B256) -> bool { + let Ok(Some(header)) = store.header(hash) else { + return false; + }; + matches!(store.canonical_hash(header.number), Ok(canonical) if canonical != Some(hash)) +} + pub(crate) fn backlog_needs_executing(has_processor: bool, downloaded: u64, executed: u64) -> bool { has_processor && downloaded > executed } @@ -444,6 +467,10 @@ pub(crate) struct ExecOutcome { /// `false` when a block failed to execute (or a parent/body was missing); /// the sync must not advance past it. pub(crate) all_ok: bool, + /// The batch stopped because a reorg made a block of it non-canonical + /// while it executed, so the block was not recorded: the chain moved, + /// nothing failed. + pub(crate) superseded: bool, /// State root after the last successfully executed block in the batch. /// Becomes the starting root for the next batch's execution. pub(crate) new_state_root: TrieNode, @@ -4669,7 +4696,7 @@ impl SyncService { Ok(o) => o, Err(e) => { error!(target: "rustock::sync", "Block execution task panicked: {e}"); - self.halt_after_exec_failure(); + self.halt_after_exec_failure(false); return; } }; @@ -4677,7 +4704,7 @@ impl SyncService { self.current_state_root = Some(outcome.new_state_root); if !outcome.all_ok { - self.halt_after_exec_failure(); + self.halt_after_exec_failure(outcome.superseded); return; } @@ -4709,7 +4736,11 @@ impl SyncService { /// (it must not execute on the wrong parent state — its already-stored /// bodies are kept and reused), and go Idle. Mirrors rskj treating such a /// block as invalid and stopping there. - fn halt_after_exec_failure(&mut self) { + /// + /// `superseded`: the batch stopped because a reorg made one of its blocks + /// non-canonical while it ran. The same retry, but nothing failed, so a + /// warning rather than an error. + fn halt_after_exec_failure(&mut self, superseded: bool) { let exec_number = self .manager .store @@ -4726,10 +4757,18 @@ impl SyncService { } else { self.futile_exec_halts = (exec_number, 1); } - error!( - target: "rustock::sync", - "Sync halted: block execution failed; will retry from executed head #{exec_number}" - ); + if superseded { + warn!( + target: "rustock::sync", + "Sync paused: a reorg moved the chain while a block executed; will retry \ + from executed head #{exec_number}" + ); + } else { + error!( + target: "rustock::sync", + "Sync halted: block execution failed; will retry from executed head #{exec_number}" + ); + } self.state = SyncState::Idle; } @@ -5755,6 +5794,7 @@ pub(crate) fn process_downloaded_blocks( // Of `processed`, how many were adopted without re-running the EVM. let mut fast_forwarded = 0u64; let mut all_ok = true; + let mut superseded = false; let mut last_executed: Option<(B256, B256)> = None; // Lineage guard: the first block must extend the executed head and @@ -5903,6 +5943,10 @@ pub(crate) fn process_downloaded_blocks( // the only thing that can make this block canonical or replace // it. Reporting the batch as clean would leave the executed // head behind the blocks just written, with nothing to notice. + // `finish` records the last executed block again, one + // `after_block` was refused included, so whether a reorg left + // it behind is read here. + superseded = no_longer_canonical(store, hash); all_ok = false; } } @@ -5923,6 +5967,7 @@ pub(crate) fn process_downloaded_blocks( ExecOutcome { all_ok, + superseded, new_state_root: current_root, blocks_since_flush: committer.pending(), } diff --git a/crates/sync/src/tests.rs b/crates/sync/src/tests.rs index e704d97..ee2c6b5 100644 --- a/crates/sync/src/tests.rs +++ b/crates/sync/src/tests.rs @@ -2635,6 +2635,198 @@ fn record_execution_reports_whether_the_marker_moved() { assert_eq!(exec, canonical.hash(), "the executed head stays on the canonical block"); } +/// What this thread logs on `rustock::sync` while the guard lives, as +/// `(level, message)`. +fn record_sync_log() -> ( + tracing::subscriber::DefaultGuard, + Arc>>, +) { + struct Message(String); + impl tracing::field::Visit for Message { + fn record_debug(&mut self, field: &tracing::field::Field, value: &dyn std::fmt::Debug) { + if field.name() == "message" { + self.0 = format!("{value:?}"); + } + } + } + struct Recorder(Arc>>); + impl tracing::Subscriber for Recorder { + fn enabled(&self, metadata: &tracing::Metadata<'_>) -> bool { + metadata.target() == "rustock::sync" + } + fn new_span(&self, _: &tracing::span::Attributes<'_>) -> tracing::span::Id { + tracing::span::Id::from_u64(1) + } + fn record(&self, _: &tracing::span::Id, _: &tracing::span::Record<'_>) {} + fn record_follows_from(&self, _: &tracing::span::Id, _: &tracing::span::Id) {} + fn event(&self, event: &tracing::Event<'_>) { + let mut message = Message(String::new()); + event.record(&mut message); + self.0 + .lock() + .unwrap() + .push((*event.metadata().level(), message.0)); + } + fn enter(&self, _: &tracing::span::Id) {} + fn exit(&self, _: &tracing::span::Id) {} + } + let log = Arc::new(std::sync::Mutex::new(Vec::new())); + let guard = tracing::subscriber::set_default(Recorder(log.clone())); + (guard, log) +} + +/// Whether `log` holds a message at `level` containing `text`. +fn logged(log: &[(tracing::Level, String)], level: tracing::Level, text: &str) -> bool { + log.iter().any(|(l, m)| *l == level && m.contains(text)) +} + +/// A reorg that lands between executing a block and recording it is handled: +/// the block is not recorded, and is executed again or re-adopted. It is a +/// warning, not an error, as testnet logged #8,274,238's on 9 November. +/// Either shape: a sibling took the block's height, or a shorter, heavier +/// chain took the head below it, leaving its height without a block. +#[test] +fn a_reorg_while_a_block_executes_is_a_warning() { + for shorter in [false, true] { + let dir = tempdir().unwrap(); + let store = Arc::new(BlockStore::open(dir.path()).unwrap()); + let genesis = dummy_header(0, B256::ZERO, U256::from(1)); + store.update_head(&genesis, U256::from(1)).unwrap(); + let canonical = dummy_header(1, genesis.hash(), U256::from(2)); + store.update_head(&canonical, U256::from(3)).unwrap(); + let executed = if shorter { + let above = dummy_header(2, canonical.hash(), U256::from(2)); + store.update_head(&above, U256::from(5)).unwrap(); + let heavier = fork_header(1, genesis.hash(), U256::from(9), 0xEE); + store.put_header(&heavier).unwrap(); + let proved = rustock_storage::Validated::prove(&store, heavier.hash()).unwrap(); + store + .apply(&rustock_storage::Transition::Adopt { head: proved }) + .unwrap(); + assert_eq!(store.canonical_hash(2).unwrap(), None); + above + } else { + let sibling = fork_header(1, genesis.hash(), U256::from(2), 0xEE); + store.put_header(&sibling).unwrap(); + sibling + }; + + let (_guard, log) = record_sync_log(); + let recorded = + crate::service::record_execution(&store, executed.hash(), executed.state_root); + + assert!(!recorded); + let log = log.lock().unwrap().clone(); + assert!( + logged(&log, tracing::Level::WARN, "a reorg moved the chain"), + "shorter {shorter}: {log:?}" + ); + assert!( + !logged(&log, tracing::Level::ERROR, ""), + "shorter {shorter}: {log:?}" + ); + } +} + +/// A refused record that no reorg explains is still an error: here a block +/// canonical at its height but above the head, which only a broken index +/// leaves. +#[test] +fn a_refused_record_no_reorg_explains_is_still_an_error() { + let dir = tempdir().unwrap(); + let store = Arc::new(BlockStore::open(dir.path()).unwrap()); + let genesis = dummy_header(0, B256::ZERO, U256::from(1)); + store.update_head(&genesis, U256::from(1)).unwrap(); + let above = dummy_header(1, genesis.hash(), U256::from(2)); + store.put_header(&above).unwrap(); + store.put_canonical_hash(1, above.hash()).unwrap(); + + let (_guard, log) = record_sync_log(); + let recorded = crate::service::record_execution(&store, above.hash(), above.state_root); + + assert!(!recorded); + let log = log.lock().unwrap().clone(); + assert!( + logged(&log, tracing::Level::ERROR, "Recording execution"), + "{log:?}" + ); + assert!(!logged(&log, tracing::Level::WARN, ""), "{log:?}"); + // Nor is a block the store does not hold taken for one a reorg left behind. + assert!(!crate::service::no_longer_canonical( + &store, + B256::repeat_byte(0x99) + )); +} + +/// The batch path reports a block a reorg left behind as superseded, so the +/// halt that follows is a warning. The block was executed by this node +/// before (its receipts and state are held), so the batch adopts it and +/// only recording it is refused. +#[test] +fn a_batch_reports_a_block_a_reorg_left_behind_as_superseded() { + let dir = tempdir().unwrap(); + let store = Arc::new(BlockStore::open(dir.path()).unwrap()); + let trie = Arc::new(rustock_trie::MemoryTrieStore::new()) as Arc; + let empty = rustock_trie::TrieNode::empty(); + let root = empty.compute_hash(trie.as_ref()); + trie.put(root.as_slice(), &empty.to_message(trie.as_ref())); + + let genesis = dummy_header(0, B256::ZERO, U256::from(1)); + store.update_head(&genesis, U256::from(1)).unwrap(); + store.set_exec_head(genesis.hash(), root).unwrap(); + let canonical = dummy_header(1, genesis.hash(), U256::from(2)); + store.update_head(&canonical, U256::from(3)).unwrap(); + + let hardfork = rustock_execution::RskHardforkConfig::mainnet(); + let receipts = vec![rustock_core::Receipt { + post_tx_state: vec![0x01], + cumulative_gas_used: 0, + gas_used: 0, + logs_bloom: alloy_primitives::Bloom::ZERO, + logs: vec![], + status: true, + }]; + let remasc = rustock_core::Transaction::remasc(1); + let mut left_behind = fork_header(1, genesis.hash(), U256::from(1), 0xEE); + left_behind.state_root = root; + left_behind.receipts_root = + rustock_core::ordered_trie_root(&receipts, hardfork.has_unitrie_state_root(1)); + store.put_header(&left_behind).unwrap(); + store + .put_body(left_behind.hash(), std::slice::from_ref(&remasc), &[]) + .unwrap(); + store.put_receipts(left_behind.hash(), &receipts).unwrap(); + + let processor = rustock_execution::BlockProcessor::new(hardfork, store.clone()); + let block = rustock_core::types::block::Block { + header: left_behind.clone(), + transactions: vec![remasc], + ommers: Vec::new(), + }; + assert_eq!( + processor.validate_block(&block).map_err(|e| e.to_string()), + Ok(()) + ); + + let outcome = crate::service::process_downloaded_blocks( + &processor, + trie, + empty, + &store, + None, + 0, + 1, + &[(left_behind.hash(), left_behind)], + &crate::chain_activity::ChainActivity::new(), + &crate::gas_price::GasPriceTracker::default(), + ); + assert!(!outcome.all_ok, "the block was not recorded"); + assert!( + outcome.superseded, + "and the batch says a reorg left it behind" + ); +} + #[tokio::test] async fn test_reconcile_leaves_a_genuine_tip_alone() { // The same code path must not fire at the tip. Nothing is stored above our @@ -4435,6 +4627,7 @@ impl ExecController { this.cv.notify_all(); crate::service::ExecOutcome { all_ok, + superseded: false, new_state_root: state_root, blocks_since_flush, } @@ -4672,7 +4865,12 @@ async fn test_pipeline_two_batches_advance_exec_head() { if let Some((hash, h)) = pending.last() { let _ = store_for_exec.set_exec_head(*hash, B256::from_slice(&[h.number as u8; 32])); } - crate::service::ExecOutcome { all_ok: true, new_state_root: state_root, blocks_since_flush: bsf } + crate::service::ExecOutcome { + all_ok: true, + superseded: false, + new_state_root: state_root, + blocks_since_flush: bsf, + } }); let mut service = make_service(store.clone()); @@ -4703,6 +4901,45 @@ async fn test_pipeline_two_batches_advance_exec_head() { assert_eq!(service.last_body_height_for_test(), 6, "download cursor at #6"); } +/// A batch a reorg stopped halts as a failed one does, but says why at the +/// level it merits: nothing failed, the chain moved. A batch whose task +/// panicked failed. +#[tokio::test] +async fn a_batch_a_reorg_stopped_halts_with_a_warning_and_a_failed_one_with_an_error() { + for (superseded, panics) in [(true, false), (false, false), (false, true)] { + let dir = tempdir().unwrap(); + let store = Arc::new(BlockStore::open(dir.path()).unwrap()); + let headers = store_chain(&store, 3); + let exec_fn: crate::service::ExecFn = + Arc::new(move |state_root, bsf, _pending: Vec<(B256, Header)>| { + assert!(!panics, "the execution task panicked, as the test asks"); + crate::service::ExecOutcome { + all_ok: false, + superseded, + new_state_root: state_root, + blocks_since_flush: bsf, + } + }); + let mut service = make_service(store.clone()); + service.set_test_exec(exec_fn); + + let (_guard, log) = record_sync_log(); + service + .finish_batch(&pending_of(&headers[0..3]), 1000) + .await; + drain_executions(&mut service).await; + + let log = log.lock().unwrap().clone(); + let paused = logged(&log, tracing::Level::WARN, "a reorg moved the chain"); + let halted = logged(&log, tracing::Level::ERROR, "Sync halted"); + assert_eq!( + (paused, halted), + (superseded, !superseded), + "superseded {superseded}: {log:?}" + ); + } +} + /// Poll on_tick until no background execution remains in flight. async fn drain_executions(service: &mut SyncService) { // Bounded by wall-clock, not by iteration count. Execution runs on a From f6db1e27e1826bde981905d314aa806e9384a3a2 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Tue, 10 Nov 2026 07:59:26 -0800 Subject: [PATCH 079/264] sync: the pool's quota reads values as rskj's longs rskj's TxQuotaChecker reads gas limits, gas prices and nonces with BigInteger.longValue() and computes in long and double: from 2^63 a value reads negative, from 2^64 it wraps, and its rounding and minimum are Java's Math.round and Math.min. A minimum gas price raised 1% a block, as fast as the rules allow, passes 2^63 in about a day. --- crates/sync/src/quota.rs | 479 +++++++++++++++++++++++++++++++------- crates/sync/src/txpool.rs | 29 +-- 2 files changed, 410 insertions(+), 98 deletions(-) diff --git a/crates/sync/src/quota.rs b/crates/sync/src/quota.rs index 82d6aee..817874a 100644 --- a/crates/sync/src/quota.rs +++ b/crates/sync/src/quota.rs @@ -44,8 +44,17 @@ //! `co.rsk.net.handler.quota.{TxQuotaChecker, TxQuota, TxVirtualGasCalculator}`. //! Where rustock cannot yet supply an input (see `avg_gas_price`), it takes the //! same branch rskj takes in that situation rather than inventing one. +//! +//! rskj reads each value it weighs (gas limits, gas prices, nonces) as a Java +//! `long`, the low 64 bits of the whole value (`BigInteger.longValue()`), and +//! computes in `long` and `double`: from 2^63 a value reads negative, from +//! 2^64 it wraps around, and a `long` sum or product can overflow. So does +//! this ([`java_long`]), or the two part once a value passes 2^63, which a +//! minimum gas price raised 1% a block, as fast as `PrevMinGasPriceRule` +//! allows, does in about a day. use alloy_primitives::{Address, U256}; +use rustock_core::Header; use std::collections::HashMap; use std::time::{Duration, Instant}; @@ -94,14 +103,78 @@ impl Default for QuotaConfig { } impl QuotaConfig { - /// rskj `getMaxGasPerSecond`: `Math.round(blockGasLimit * 0.9)`. - pub fn max_gas_per_second(&self, block_gas_limit: u64) -> u64 { - (block_gas_limit as f64 * self.max_gas_per_second_percent).round() as u64 + /// rskj `getMaxGasPerSecond` (`TxQuotaChecker` L203-205): + /// `Math.round(blockGasLimit * 0.9)`, of the gas limit as a `long`. + pub fn max_gas_per_second(&self, block_gas_limit: i64) -> i64 { + java_round(block_gas_limit as f64 * self.max_gas_per_second_percent) } - /// rskj `getMaxQuota`: `maxGasPerSecond * MAX_QUOTA_GAS_MULTIPLIER`. - pub fn max_quota(&self, max_gas_per_second: u64) -> u64 { - max_gas_per_second.saturating_mul(self.max_quota_gas_multiplier) + /// rskj `getMaxQuota` (L207-209): `maxGasPerSecond * + /// MAX_QUOTA_GAS_MULTIPLIER`, a `long` product, which wraps. + pub fn max_quota(&self, max_gas_per_second: i64) -> i64 { + max_gas_per_second.wrapping_mul(self.max_quota_gas_multiplier as i64) + } +} + +// --------------------------------------------------------------------------- +// Java's arithmetic +// --------------------------------------------------------------------------- + +/// `BigInteger.longValue()`, how rskj's checker reads each value it weighs: +/// the low 64 bits of the whole value, as a two's-complement `long`. +pub fn java_long(value: U256) -> i64 { + value.as_limbs()[0] as i64 +} + +/// `new BigInteger(1, bytes).longValue()`: the same, of a value held as its +/// big-endian bytes, of any length. +fn java_long_of_bytes(bytes: &[u8]) -> i64 { + let low = &bytes[bytes.len().saturating_sub(8)..]; + let mut word = [0u8; 8]; + word[8 - low.len()..].copy_from_slice(low); + i64::from_be_bytes(word) +} + +/// Java's `Math.round(double)`, as the JDK computes it: the closest `long`, +/// ties toward positive infinity, so -4.5 is -4 where Rust's `round` gives +/// -5; NaN is 0, and the ends of `long`'s range stand for what lies past +/// them. +fn java_round(a: f64) -> i64 { + const SIGNIFICAND_WIDTH: i64 = 53; + const EXP_BIAS: i64 = 1023; + const EXP_BIT_MASK: i64 = 0x7FF0_0000_0000_0000; + const SIGNIF_BIT_MASK: i64 = 0x000F_FFFF_FFFF_FFFF; + let long_bits = a.to_bits() as i64; + let biased_exp = (long_bits & EXP_BIT_MASK) >> (SIGNIFICAND_WIDTH - 1); + let shift = (SIGNIFICAND_WIDTH - 2 + EXP_BIAS) - biased_exp; + if (shift & -64) == 0 { + // A finite `a` with 2^-64 <= ulp(a) < 1: `r` is a / ulp(a), + // `r >> shift` is floor(2a), and the rest floor(a + 1/2). + let mut r = (long_bits & SIGNIF_BIT_MASK) | (SIGNIF_BIT_MASK + 1); + if long_bits < 0 { + r = -r; + } + ((r >> shift) + 1) >> 1 + } else { + // Too small to round to anything but zero, already an integer, or + // not finite: `(long) a`, which `as` is (NaN to 0, saturating). + a as i64 + } +} + +/// Java's `Math.min(double, double)`: NaN if either is (Rust's `min` gives +/// the other), and -0.0 below 0.0. +fn java_min(a: f64, b: f64) -> f64 { + if a.is_nan() { + return a; + } + if a == 0.0 && b == 0.0 && b.to_bits() == (-0.0f64).to_bits() { + return b; + } + if a <= b { + a + } else { + b } } @@ -131,57 +204,64 @@ mod factors { pub const SIZE_DIVISOR: f64 = 25_000.0; } -/// The facts about a transaction the calculator needs. Grouped so the call -/// sites cannot transpose two `u64` arguments, which is the failure mode a +/// The facts about a transaction the calculator needs, as the transaction +/// holds them: the calculator reads them as rskj does. Grouped so the call +/// sites cannot transpose two arguments, which is the failure mode a /// six-parameter function invites. #[derive(Debug, Clone, Copy)] pub struct TxFacts { pub nonce: u64, - pub gas_limit: u64, + pub gas_limit: U256, pub gas_price: U256, /// Encoded size in bytes — rskj `Transaction.getSize()`. pub size: usize, } -/// rskj `TxVirtualGasCalculator`. +/// rskj `TxVirtualGasCalculator`, over the `long`s rskj's checker hands it +/// (`TxQuotaChecker.calculateConsumedVirtualGas`, L211-225). #[derive(Debug, Clone, Copy)] pub struct VirtualGasCalculator { - account_nonce: u64, - block_gas_limit: u64, - block_min_gas_price: u64, + account_nonce: i64, + block_gas_limit: i64, + block_min_gas_price: i64, /// `None` reproduces rskj `createSkippingGasPriceFactor`, which it uses /// whenever `GasPriceTracker.isFeeMarketWorking()` is false. - avg_gas_price: Option, + avg_gas_price: Option, } impl VirtualGasCalculator { /// rskj `createWithAllFactors`. pub fn with_all_factors( - account_nonce: u64, - block_gas_limit: u64, - block_min_gas_price: u64, - avg_gas_price: u64, + account_nonce: i64, + block_gas_limit: i64, + block_min_gas_price: i64, + avg_gas_price: i64, ) -> Self { Self { account_nonce, block_gas_limit, block_min_gas_price, avg_gas_price: Some(avg_gas_price) } } /// rskj `createSkippingGasPriceFactor`. pub fn skipping_gas_price_factor( - account_nonce: u64, - block_gas_limit: u64, - block_min_gas_price: u64, + account_nonce: i64, + block_gas_limit: i64, + block_min_gas_price: i64, ) -> Self { Self { account_nonce, block_gas_limit, block_min_gas_price, avg_gas_price: None } } - /// rskj `calculate(newTx, replacedTx)`. + /// rskj `calculate(newTx, replacedTx)` (L81-104). pub fn calculate(&self, new_tx: &TxFacts, replaced_tx: Option<&TxFacts>) -> f64 { - let tx_gas_limit = new_tx.gas_limit; + let tx_gas_limit = java_long(new_tx.gas_limit); // `newTxNonce == accountNonce ? 1 : 2` — a nonce ahead of the account's // is a transaction that cannot execute yet, so it occupies the pool for // longer and costs double. - let future_nonce_factor = if new_tx.nonce == self.account_nonce { 1.0 } else { 2.0 }; + let new_tx_nonce = new_tx.nonce as i64; + let future_nonce_factor = if new_tx_nonce == self.account_nonce { + 1.0 + } else { + 2.0 + }; let low_gas_price_factor = match self.avg_gas_price { None => 1.0, @@ -189,8 +269,9 @@ impl VirtualGasCalculator { }; // Fresh accounts are cheap to create and so are the natural vehicle for - // a flood; they pay up to 5x until their nonce climbs. - let nonce_factor = 1.0 + factors::NONCE_WEIGHT / (self.account_nonce as f64 + 1.0); + // a flood; they pay up to 5x until their nonce climbs. `accountNonce + + // 1` is a `long` sum, which wraps. + let nonce_factor = 1.0 + factors::NONCE_WEIGHT / self.account_nonce.wrapping_add(1) as f64; let size_factor = 1.0 + new_tx.size as f64 / factors::SIZE_DIVISOR; @@ -209,19 +290,18 @@ impl VirtualGasCalculator { Self::cap_result(tx_gas_limit as f64, composite) } - /// rskj `calculateLowGasPriceFactor`. A transaction priced below the market - /// average is one the sender is in no hurry to have mined, so it may sit in - /// the pool a long time. - fn low_gas_price_factor(&self, tx_gas_price: U256, avg_gas_price: u64) -> f64 { - // rskj reads the gas price through `asBigInteger().longValue()`, which - // truncates to 64 bits; saturating matches for every realistic value and - // does not panic for absurd ones. - let tx_gas_price = u64::try_from(tx_gas_price).unwrap_or(u64::MAX); + /// rskj `calculateLowGasPriceFactor` (L106-115). A transaction priced below + /// the market average is one the sender is in no hurry to have mined, so + /// it may sit in the pool a long time. + fn low_gas_price_factor(&self, tx_gas_price: U256, avg_gas_price: i64) -> f64 { + let tx_gas_price = java_long(tx_gas_price); if tx_gas_price >= avg_gas_price { return 1.0; } - let denominator = avg_gas_price as f64 - self.block_min_gas_price as f64; - let factor = (avg_gas_price as f64 - tx_gas_price as f64) / denominator; + // `(avg - txPrice) / (avg - (double) blockMin)`: the numerator is a + // `long` difference, which wraps, and the denominator a `double` one. + let factor = avg_gas_price.wrapping_sub(tx_gas_price) as f64 + / (avg_gas_price as f64 - self.block_min_gas_price as f64); 1.0 + factors::LOW_GAS_PRICE_WEIGH * factor } @@ -259,15 +339,23 @@ impl VirtualGasCalculator { } } -/// rskj reads gas prices as Java `double` via `BigInteger.doubleValue()`. +/// rskj reads gas prices as Java `double` via `BigInteger.doubleValue()`, +/// which rounds the whole value to the nearest `double`, ties to even. fn u256_to_f64(v: U256) -> f64 { - // f64 holds 2^53 exactly; above that rskj loses precision identically, - // because it goes through `BigInteger.doubleValue()`. - let mut out = 0f64; - for limb in v.as_limbs().iter().rev() { - out = out * 2f64.powi(64) + *limb as f64; - } - out + let bits = v.bit_len(); + if bits <= 64 { + return v.as_limbs()[0] as f64; + } + // The top 64 bits round as the whole value does once any bit below them + // is folded into their lowest: a `double` keeps 53, so the rounding falls + // at bit 11 of the 64, and below that only whether anything is set counts. + let shift = bits - 64; + let top = v >> shift; + let mut word = top.as_limbs()[0]; + if top << shift != v { + word |= 1; + } + word as f64 * 2f64.powi(shift as i32) } // --------------------------------------------------------------------------- @@ -282,8 +370,8 @@ pub struct TxQuota { } impl TxQuota { - /// rskj `TxQuota.createNew`. - pub fn create_new(initial_quota: u64, now: Instant) -> Self { + /// rskj `TxQuota.createNew`, of a `long` it holds as a `double` (L44-47). + pub fn create_new(initial_quota: i64, now: Instant) -> Self { Self { last_refresh: now, available_virtual_gas: initial_quota as f64 } } @@ -314,14 +402,16 @@ impl TxQuota { false } - /// rskj `refresh`: accrue `max_gas_per_second` for every second since the - /// last refresh, capped at `max_quota`. - pub fn refresh(&mut self, max_gas_per_second: u64, max_quota: u64, now: Instant) -> f64 { + /// rskj `refresh` (L126-138): accrue `max_gas_per_second` for every second + /// since the last refresh, capped at `max_quota`. + pub fn refresh(&mut self, max_gas_per_second: i64, max_quota: i64, now: Instant) -> f64 { let elapsed_seconds = now.saturating_duration_since(self.last_refresh).as_secs_f64(); let add_to_quota = elapsed_seconds * max_gas_per_second as f64; self.last_refresh = now; + // `Math.min(maxQuota, availableVirtualGas + addToQuota)`, which keeps a + // NaN where Rust's `min` would drop it. self.available_virtual_gas = - (self.available_virtual_gas + add_to_quota).min(max_quota as f64); + java_min(max_quota as f64, self.available_virtual_gas + add_to_quota); self.available_virtual_gas } } @@ -342,14 +432,33 @@ pub trait AccountView { } /// Contextual facts about the moment a transaction is being processed — -/// rskj `TxQuotaChecker.CurrentContext`. +/// rskj `TxQuotaChecker.CurrentContext` — as its checker reads them: each a +/// `long`, the low 64 bits of the whole value. #[derive(Debug, Clone, Copy)] pub struct QuotaContext { - pub block_gas_limit: u64, - pub block_min_gas_price: u64, - /// `Some` only when a fee-market average is available; `None` takes rskj's + /// `bestBlock.getGasLimitAsInteger().longValue()`. + pub block_gas_limit: i64, + /// `bestBlock.getMinimumGasPrice().asBigInteger().longValue()`. + pub block_min_gas_price: i64, + /// `gasPriceTracker.getGasPrice().asBigInteger().longValue()`, `Some` + /// only when a fee-market average is available; `None` takes rskj's /// `createSkippingGasPriceFactor` branch. - pub avg_gas_price: Option, + pub avg_gas_price: Option, +} + +impl QuotaContext { + /// The context of a transaction arriving over the best block `best`, + /// `avg_gas_price` the tracker's price when the fee market is working, + /// read as rskj's checker reads them (`TxQuotaChecker` L77, L178-179, + /// L212-218): the gas limit's bytes and the minimum gas price whole, + /// whatever their size, and then their low 64 bits. + pub fn of(best: &Header, avg_gas_price: Option) -> Self { + Self { + block_gas_limit: best.gas_limit_low_long(), + block_min_gas_price: java_long_of_bytes(&best.minimum_gas_price_bytes()), + avg_gas_price: avg_gas_price.map(java_long), + } + } } // --------------------------------------------------------------------------- @@ -367,17 +476,23 @@ pub struct QuotaChecker { /// Access order, oldest first — the eviction policy of rskj's /// `MaxSizeHashMap(maxSize, accessOrder = true)`. access_order: Vec
, - /// rskj `UNKNOWN_LAST_BLOCK_GAS_LIMIT` is -1; `None` is the same thing. - last_block_gas_limit: Option, + /// rskj's `lastBlockGasLimit`, a `long`: [`UNKNOWN_LAST_BLOCK_GAS_LIMIT`] + /// until a transaction has been processed. + last_block_gas_limit: i64, } +/// rskj `TxQuotaChecker.UNKNOWN_LAST_BLOCK_GAS_LIMIT`. A gas limit whose low +/// 64 bits read -1 reads as unknown there too, and the clean waits for +/// another. +const UNKNOWN_LAST_BLOCK_GAS_LIMIT: i64 = -1; + impl QuotaChecker { pub fn new(config: QuotaConfig) -> Self { Self { config, quotas: HashMap::new(), access_order: Vec::new(), - last_block_gas_limit: None, + last_block_gas_limit: UNKNOWN_LAST_BLOCK_GAS_LIMIT, } } @@ -435,7 +550,7 @@ impl QuotaChecker { // Kept so `clean_max_quotas` has a gas limit to work from; it runs on a // timer and has no transaction context of its own. - self.last_block_gas_limit = Some(ctx.block_gas_limit); + self.last_block_gas_limit = ctx.block_gas_limit; // Evaluated BEFORE the quota is created, or the map lookup below would // always find the entry we are about to insert. @@ -444,7 +559,8 @@ impl QuotaChecker { self.update_quota(sender, true, ctx, accounts, now); self.update_receiver_quota_if_required(receiver, ctx, accounts, now); - let account_nonce = accounts.nonce(&sender); + // `state.getNonce(sender).longValue()` (L212). + let account_nonce = accounts.nonce(&sender) as i64; let calculator = match ctx.avg_gas_price { Some(avg) => VirtualGasCalculator::with_all_factors( account_nonce, @@ -540,7 +656,8 @@ impl QuotaChecker { return; } - let account_nonce = accounts.nonce(&address); + // `state.getNonce(address).longValue()` (L186). + let account_nonce = accounts.nonce(&address) as i64; let initial = Self::new_item_quota(account_nonce, is_tx_source, max_gas_per_second, max_quota); self.quotas.insert(address, TxQuota::create_new(initial, now)); @@ -554,11 +671,11 @@ impl QuotaChecker { /// the full quota immediately. A brand-new account, or any receiver, starts /// with one second's worth. fn new_item_quota( - account_nonce: u64, + account_nonce: i64, is_tx_source: bool, - max_gas_per_second: u64, - max_quota: u64, - ) -> u64 { + max_gas_per_second: i64, + max_quota: i64, + ) -> i64 { let is_new_account = account_nonce == 0; let grant_max_quota = is_tx_source && !is_new_account; if grant_max_quota { @@ -593,16 +710,18 @@ impl QuotaChecker { /// seen, so keeping it costs memory and says nothing. Intended to run on a /// timer roughly as often as an account takes to reach the ceiling. pub fn clean_max_quotas(&mut self, now: Instant) -> usize { - let Some(block_gas_limit) = self.last_block_gas_limit else { + if self.last_block_gas_limit == UNKNOWN_LAST_BLOCK_GAS_LIMIT { return 0; // no transaction processed yet - }; - let max_gas_per_second = self.config.max_gas_per_second(block_gas_limit); + } + let max_gas_per_second = self.config.max_gas_per_second(self.last_block_gas_limit); let max_quota = self.config.max_quota(max_gas_per_second); let before = self.quotas.len(); let mut to_remove = Vec::new(); for (address, quota) in self.quotas.iter_mut() { let accumulated = quota.refresh(max_gas_per_second, max_quota, now); + // rskj compares `BigDecimal.valueOf` of each (L163), which is + // this for any ceiling a `double` holds exactly, below 2^53. if accumulated == max_quota as f64 { to_remove.push(*address); } @@ -637,8 +756,8 @@ mod tests { /// Mainnet-shaped numbers: a 6.8M block gas limit gives 6,120,000 virtual /// gas per second and a ceiling of 12,240,000,000. - const BLOCK_GAS_LIMIT: u64 = 6_800_000; - const MIN_GAS_PRICE: u64 = 59_240_000; + const BLOCK_GAS_LIMIT: i64 = 6_800_000; + const MIN_GAS_PRICE: i64 = 59_240_000; fn addr(b: u8) -> Address { Address::repeat_byte(b) @@ -684,8 +803,13 @@ mod tests { } } - fn tx(nonce: u64, gas_limit: u64, gas_price: u64, size: usize) -> TxFacts { - TxFacts { nonce, gas_limit, gas_price: U256::from(gas_price), size } + fn tx(nonce: u64, gas_limit: i64, gas_price: i64, size: usize) -> TxFacts { + TxFacts { + nonce, + gas_limit: U256::from(gas_limit as u64), + gas_price: U256::from(gas_price as u64), + size, + } } // ----------------------------------------------------------------------- @@ -756,9 +880,9 @@ mod tests { /// are the cheap vehicle for a flood, which is why they are penalised. #[test] fn nonce_factor_penalises_new_accounts() { - let cost_at = |account_nonce: u64| { + let cost_at = |account_nonce: i64| { VirtualGasCalculator::skipping_gas_price_factor(account_nonce, BLOCK_GAS_LIMIT, MIN_GAS_PRICE) - .calculate(&tx(account_nonce, 21_000, MIN_GAS_PRICE, 110), None) + .calculate(&tx(account_nonce as u64, 21_000, MIN_GAS_PRICE, 110), None) }; let fresh = cost_at(0); let established = cost_at(999); @@ -816,7 +940,7 @@ mod tests { /// the average. #[test] fn low_gas_price_factor_applies_only_below_the_average() { - let avg = 100u64; + let avg = 100i64; let calc = VirtualGasCalculator::with_all_factors(10, BLOCK_GAS_LIMIT, 0, avg); let at_avg = calc.calculate(&tx(10, 21_000, 100, 0), None); @@ -860,16 +984,17 @@ mod tests { assert_eq!(VirtualGasCalculator::cap_result(gas_limit, 3.0), 63_000.0); } - /// The most extreme transaction that can be represented — maximum gas - /// limit, maximum size, smallest possible block gas limit, and a - /// replacement ratio as close to zero as `U256` allows — still produces a - /// finite cost. The fallback above is defence in depth, not a live path. + /// The most extreme transaction that can be represented — the largest gas + /// limit rskj reads (`Long.MAX_VALUE`; from 2^63 it reads negative), + /// maximum size, smallest possible block gas limit, and a replacement + /// ratio as close to zero as `U256` allows — still produces a finite + /// cost. The fallback above is defence in depth, not a live path. #[test] fn the_most_extreme_transaction_still_produces_a_finite_cost() { let calc = VirtualGasCalculator::skipping_gas_price_factor(0, 1, 0); let new = TxFacts { nonce: 1, - gas_limit: u64::MAX, + gas_limit: U256::from(i64::MAX as u64), gas_price: U256::from(1u64), size: usize::MAX, }; @@ -1022,7 +1147,7 @@ mod tests { let accounts = FakeAccounts::default().with_nonce(addr(1), 7); let t0 = Instant::now(); - let mut price = 100u64; + let mut price = 100i64; let mut accepted = 0; // No time passes between attempts, so nothing accrues. for _ in 0..10_000 { @@ -1219,4 +1344,196 @@ mod tests { }; assert!(count_accepted(strict) < count_accepted(QuotaConfig::default())); } + + // ----------------------------------------------------------------------- + // Values as rskj reads them: `long`s (task 279) + // ----------------------------------------------------------------------- + + /// 2^63 + `n`, which rskj reads as `Long.MIN_VALUE + n`. + fn past_2_63(n: u64) -> U256 { + U256::from(1u64 << 63) + U256::from(n) + } + + /// 2^64 + `n`, of which rskj reads `n`. + fn past_2_64(n: u64) -> U256 { + (U256::from(1u64) << 64) + U256::from(n) + } + + /// The best block's gas limit and minimum gas price, and the tracker's + /// price, are read as rskj's checker reads them: the low 64 bits of the + /// whole value, a `long`, whatever its size (`TxQuotaChecker` L77, + /// L178-179, L212-218). + #[test] + fn the_context_reads_each_value_as_its_low_64_bits() { + let mut best = crate::tests::dummy_header(1, alloy_primitives::B256::ZERO, U256::from(1)); + best.gas_limit = past_2_64(6_800_000); + best.minimum_gas_price = past_2_63(5); + let ctx = QuotaContext::of(&best, Some(past_2_64(3_000))); + assert_eq!(ctx.block_gas_limit, 6_800_000); + assert_eq!(ctx.block_min_gas_price, i64::MIN + 5); + assert_eq!(ctx.avg_gas_price, Some(3_000)); + + // Past 256 bits the header keeps the bytes, and they are what is read. + let mut beyond = vec![1u8]; + beyond.extend([0u8; 39]); + beyond.push(7); + best.kept = Some(Box::new(rustock_core::types::header::KeptFields { + gas_limit: Some(beyond.clone().into()), + minimum_gas_price: Some(beyond.into()), + ..Default::default() + })); + let ctx = QuotaContext::of(&best, None); + assert_eq!((ctx.block_gas_limit, ctx.block_min_gas_price), (7, 7)); + assert_eq!(ctx.avg_gas_price, None); + } + + /// rskj reads a price from 2^63 as a negative `long`. Once the average has + /// passed 2^63 and a transaction's price has not, the transaction is not + /// below the average (L109) and pays no low-price factor; read unsigned, + /// it would pay 3.25 times over. + #[test] + fn a_price_past_2_63_reads_negative() { + let min = java_long(U256::from((1u64 << 63) - 1_000)); + let avg = java_long(past_2_63(1_000)); + assert!(min > 0 && avg < 0); + let with_avg = VirtualGasCalculator::with_all_factors(10, BLOCK_GAS_LIMIT, min, avg); + let skipping = VirtualGasCalculator::skipping_gas_price_factor(10, BLOCK_GAS_LIMIT, min); + let below = TxFacts { + gas_price: U256::from((1u64 << 63) - 500), + ..tx(10, 21_000, 0, 110) + }; + assert_eq!( + with_avg.calculate(&below, None), + skipping.calculate(&below, None) + ); + } + + /// From 2^64 a price wraps around: rskj reads its low 64 bits (L107). + #[test] + fn a_price_past_2_64_reads_as_its_low_64_bits() { + let calc = VirtualGasCalculator::with_all_factors(10, BLOCK_GAS_LIMIT, 1_000, 3_000); + let wrapped = TxFacts { + gas_price: past_2_64(2_000), + ..tx(10, 21_000, 0, 110) + }; + let low = calc.calculate(&tx(10, 21_000, 2_000, 110), None); + assert_eq!(calc.calculate(&wrapped, None), low); + // 2,000 halfway from the minimum to the average: 1 + 3 * 1/2. + let at_avg = calc.calculate(&tx(10, 21_000, 3_000, 110), None); + assert!((low / at_avg - 2.5).abs() < 1e-9, "{low} / {at_avg}"); + } + + /// A transaction's gas limit too (L82). + #[test] + fn a_gas_limit_past_2_64_reads_as_its_low_64_bits() { + let calc = + VirtualGasCalculator::skipping_gas_price_factor(10, BLOCK_GAS_LIMIT, MIN_GAS_PRICE); + let wrapped = TxFacts { + gas_limit: past_2_64(21_000), + ..tx(10, 0, MIN_GAS_PRICE, 110) + }; + assert_eq!( + calc.calculate(&wrapped, None), + calc.calculate(&tx(10, 21_000, MIN_GAS_PRICE, 110), None) + ); + } + + /// `avg - txPrice` is a `long` difference and `avg - (double) blockMin` a + /// `double` one (L110). Where a `double` can't tell the prices apart, the + /// numerator is still 2 and the denominator 0: the factor is infinite and + /// the cost falls back to `gasLimit * MAX_FACTOR`, where a `double` + /// difference would make it NaN. + #[test] + fn the_price_difference_is_a_long_difference() { + let avg = (1i64 << 62) + 3; + let calc = VirtualGasCalculator::with_all_factors(10, BLOCK_GAS_LIMIT, 1 << 62, avg); + let below = tx(10, 21_000, (1 << 62) + 1, 110); + assert_eq!(calc.calculate(&below, None), 21_000.0 * factors::MAX); + } + + /// `accountNonce + 1` is a `long` sum (L89). At the largest nonce a `u64` + /// holds, which rskj reads as -1, it is 0: the nonce factor is infinite, + /// and the cost falls back to `gasLimit * MAX_FACTOR` (L133-141). + #[test] + fn the_nonce_factor_adds_in_long_arithmetic() { + let mut checker = QuotaChecker::new(QuotaConfig::default()); + let accounts = FakeAccounts::default().with_nonce(addr(1), u64::MAX); + let new = tx(u64::MAX, 21_000, MIN_GAS_PRICE, 110); + let now = Instant::now(); + assert!(checker.accept_tx(addr(1), None, &new, None, &ctx(), &accounts, now)); + + let cfg = QuotaConfig::default(); + let max_quota = cfg.max_quota(cfg.max_gas_per_second(BLOCK_GAS_LIMIT)) as f64; + assert_eq!( + checker.quota_of(&addr(1)), + Some(max_quota - 21_000.0 * factors::MAX) + ); + } + + /// `Math.round` takes a tie toward positive infinity and Rust's `round` + /// away from zero: they part on a negative tie, which a gas limit rskj + /// reads as negative gives (L203-205). + #[test] + fn the_rate_rounds_as_java_rounds() { + let cfg = QuotaConfig::default(); + assert_eq!(cfg.max_gas_per_second(-5), -4, "Math.round(-4.5)"); + assert_eq!(cfg.max_gas_per_second(5), 5, "Math.round(4.5)"); + assert_eq!(java_round(-0.5), 0); + assert_eq!(java_round(2.5), 3); + assert_eq!(java_round(0.5 - f64::EPSILON / 4.0), 0); + assert_eq!(java_round(f64::NAN), 0); + assert_eq!(java_round(1e300), i64::MAX); + assert_eq!(java_round(-1e300), i64::MIN); + } + + /// `maxGasPerSecond * MAX_QUOTA_GAS_MULTIPLIER` is a `long` product, + /// which wraps rather than saturating (L207-209). + #[test] + fn the_ceiling_multiplies_in_long_arithmetic() { + let cfg = QuotaConfig::default(); + assert_eq!(cfg.max_quota(1 << 62), 0, "2^62 x 2,000 wraps to 0"); + assert_eq!(cfg.max_quota(i64::MAX), i64::MAX.wrapping_mul(2_000)); + } + + /// `Math.min` keeps a NaN where Rust's `min` drops it (TxQuota L132): a + /// quota a NaN cost spoiled stays spoiled through a refresh, as on rskj. + #[test] + fn a_refresh_keeps_a_nan_as_math_min_does() { + let t0 = Instant::now(); + let mut q = TxQuota::create_new(1_000, t0); + assert!(q.accept_virtual_gas_consumption(f64::NAN)); + assert!(q.refresh(100, 5_000, t0 + Duration::from_secs(10)).is_nan()); + assert!(java_min(0.0, -0.0).is_sign_negative()); + } + + /// rskj keeps the last gas limit as a `long`, -1 meaning none seen (L44, + /// L147-150): a gas limit whose low 64 bits read -1 leaves the clean + /// waiting, as there. + #[test] + fn a_gas_limit_read_as_minus_one_leaves_the_clean_waiting() { + let mut checker = QuotaChecker::new(QuotaConfig::default()); + let accounts = FakeAccounts::default().with_nonce(addr(1), 9); + let t0 = Instant::now(); + let unknown = QuotaContext { + block_gas_limit: -1, + ..ctx() + }; + let new = tx(9, 21_000, MIN_GAS_PRICE, 110); + checker.accept_tx(addr(1), None, &new, None, &unknown, &accounts, t0); + assert_eq!(checker.tracked_accounts(), 1); + + assert_eq!(checker.clean_max_quotas(t0 + Duration::from_secs(3_600)), 0); + assert_eq!(checker.tracked_accounts(), 1); + } + + /// rskj weighs a replacement by prices it reads as `double`s through + /// `BigInteger.doubleValue()`, rounded once (L118-121). Past 2^64, + /// adding limb by limb rounds twice and can land a `double` away. + #[test] + fn a_price_past_2_64_rounds_once_to_a_double() { + let v = past_2_64((1u64 << 53) + 2_049); + assert_eq!(u256_to_f64(v), 2f64.powi(64) + 2f64.powi(53) + 4_096.0); + assert_eq!(u256_to_f64(U256::from(u64::MAX)), u64::MAX as f64); + assert_eq!(u256_to_f64(U256::MAX), 2f64.powi(256)); + } } diff --git a/crates/sync/src/txpool.rs b/crates/sync/src/txpool.rs index f428d90..45013f4 100644 --- a/crates/sync/src/txpool.rs +++ b/crates/sync/src/txpool.rs @@ -632,22 +632,17 @@ impl TransactionPool { // invalid transaction consume an honest account's virtual gas. { let accounts = TrieAccountView::new(head_header.state_root, &*self.trie_store); - let ctx = crate::quota::QuotaContext { - block_gas_limit: head_header.gas_limit.try_into().unwrap_or(u64::MAX), - block_min_gas_price: head_header - .minimum_gas_price - .try_into() - .unwrap_or(u64::MAX), - // rskj supplies the average only when the fee market is - // working -- `isFeeMarketWorking()` false takes its - // `createSkippingGasPriceFactor` branch, which pins the - // low-gas-price factor to 1. On a chain with spare capacity - // that is the usual path on rskj too; the difference is that - // it recovers when blocks fill up, which this now does. - avg_gas_price: self.gas_price.as_ref().and_then(|t| { - t.is_fee_market_working().then(|| t.gas_price().try_into().unwrap_or(u64::MAX)) - }), - }; + // rskj supplies the average only when the fee market is working -- + // `isFeeMarketWorking()` false takes its + // `createSkippingGasPriceFactor` branch, which pins the + // low-gas-price factor to 1. On a chain with spare capacity that + // is the usual path on rskj too; the difference is that it + // recovers when blocks fill up, which this now does. + let avg_gas_price = self + .gas_price + .as_ref() + .and_then(|t| t.is_fee_market_working().then(|| t.gas_price())); + let ctx = crate::quota::QuotaContext::of(&head_header, avg_gas_price); let receiver = (tx.to.len() == 20).then(|| Address::from_slice(tx.to.as_ref())); let accepted = self .quota @@ -1085,7 +1080,7 @@ impl crate::quota::AccountView for TrieAccountView<'_> { fn tx_facts(tx: &Transaction, encoded_size: usize) -> crate::quota::TxFacts { crate::quota::TxFacts { nonce: tx.nonce, - gas_limit: tx.gas_limit.try_into().unwrap_or(u64::MAX), + gas_limit: tx.gas_limit, gas_price: tx.gas_price, size: encoded_size, } From 2365c62cbcaf9a2e2c9a0c434e0e810348436bf0 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Tue, 10 Nov 2026 07:59:27 -0800 Subject: [PATCH 080/264] sync: refuse whole blocks from rskj's peer.bannedMiners --banned-miners and the config file's banned_miners name coinbases whose blocks a peer sends whole are not taken (NodeMessageHandler.allowByMinerNotBanned). Entries are read as rskj's RskAddress reads them, and a bad one stops the start, as on rskj. --- crates/cli/src/config_file.rs | 4 ++ crates/cli/src/main.rs | 125 ++++++++++++++++++++++++++++++++-- crates/sync/src/handler.rs | 61 ++++++++++++++++- crates/sync/src/lib.rs | 2 +- crates/sync/src/tests.rs | 122 +++++++++++++++++++++++++++++++++ node-light.example.toml | 3 + node.example.toml | 3 + 7 files changed, 312 insertions(+), 8 deletions(-) diff --git a/crates/cli/src/config_file.rs b/crates/cli/src/config_file.rs index 80045a8..dead970 100644 --- a/crates/cli/src/config_file.rs +++ b/crates/cli/src/config_file.rs @@ -196,6 +196,9 @@ pub struct PeersSection { /// Addresses or CIDR blocks refused at connection time, as rskj's /// `peer.bannedPeerIPs`. pub banned_peers: Option>, + /// Coinbases whose blocks are refused when a peer sends them whole, as + /// rskj's `peer.bannedMiners`. + pub banned_miners: Option>, /// Discovery bootstrap addresses, `HOST:PORT`. **Replaces** the chain's /// built-in list rather than adding to it, which is what makes a private /// two-node network possible. @@ -446,6 +449,7 @@ pub const CONFIGURABLE: &[&str] = &[ "max_inbound_per_cidr", "inbound_cidr_prefix", "banned_peers", + "banned_miners", "bootnodes", "no_peer_punishment", "btc_block_cache_entries", diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index 038c450..8f5de6b 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -216,6 +216,16 @@ struct Args { #[arg(long, value_name = "ADDRESS_OR_CIDR")] banned_peers: Vec, + /// Coinbases whose blocks this node won't take from peers, as rskj's + /// `peer.bannedMiners`. Repeatable; each a 20-byte address in hex, with + /// or without `0x`. + /// + /// What is refused is a block a peer sends whole + /// (`NodeMessageHandler.allowByMinerNotBanned`); the same block fetched + /// while syncing is taken, as on rskj. + #[arg(long, value_name = "ADDRESS")] + banned_miners: Vec, + /// Record peer scoring but never act on it. /// /// rskj's `scoring.punishmentEnabled = false`. Events are still counted @@ -1474,6 +1484,14 @@ fn apply_file_config( } } } + // Combined in the same way. + if let Some(banned) = &f.peers.banned_miners { + for entry in banned { + if !a.banned_miners.contains(entry) { + a.banned_miners.push(entry.clone()); + } + } + } // Unlike `banned_peers` this one **replaces** rather than combines: the // flag exists to say "bootstrap from these and nothing else", and merging // it with a file's list would quietly defeat that. @@ -2189,12 +2207,14 @@ async fn run(local_offset: Option) -> Result<()> { let (event_tx, event_rx) = tokio::sync::mpsc::unbounded_channel(); // The miner's: it tells the sync service of every block it commits (#323). let mined_events = event_tx.clone(); - let sync_handler = Arc::new( - SyncHandler::new(sync_manager.clone(), event_tx) - .with_serve_ceiling(args.simulate_height) - .with_checkpoint_defence(checkpoint_defence) - .with_checkpoint(snap_checkpoint_for(args.network_id)), - ); + let sync_handler = Arc::new(sync_handler_for( + sync_manager.clone(), + event_tx, + args.simulate_height, + checkpoint_defence, + args.network_id, + &args.banned_miners, + )?); let gc_config = rustock_storage::epoch_store::EpochConfig { epochs: args.gc_epochs, burial_depth: args.gc_burial, @@ -4854,6 +4874,28 @@ fn snap_checkpoint_for( } } +/// The handler the node serves peers through, as the arguments shape it: the +/// serve ceiling (`--simulate-height`), the checkpoint defences, the chain's +/// checkpoint, and rskj's `peer.bannedMiners` (`--banned-miners`). rskj +/// reads the banned miners as addresses where its `NodeMessageHandler` is +/// built, and doesn't start with one that isn't; nor does this. +fn sync_handler_for( + manager: Arc, + events: tokio::sync::mpsc::UnboundedSender, + simulate_height: Option, + checkpoint_defence: rustock_core::checkpoint::CheckpointDefence, + network_id: u64, + banned_miners: &[String], +) -> anyhow::Result { + let banned_miners = rustock_sync::parse_banned_miners(banned_miners) + .map_err(|e| anyhow::anyhow!("--banned-miners: {e}"))?; + Ok(SyncHandler::new(manager, events) + .with_serve_ceiling(simulate_height) + .with_checkpoint_defence(checkpoint_defence) + .with_checkpoint(snap_checkpoint_for(network_id)) + .with_banned_miners(banned_miners)) +} + /// Seconds as `2h 04m 31s`, for progress lines a human reads. fn fmt_duration(secs: f64) -> String { let s = secs.max(0.0) as u64; @@ -5675,3 +5717,74 @@ mod genesis_tests { assert_ne!(m.compute_hash(&store_m), t.compute_hash(&store_t)); } } + +#[cfg(test)] +mod sync_handler_tests { + use super::*; + use rustock_networking::protocol::{P2pHandler, P2pMessage, RskMessage, RskSubMessage}; + + type Built = ( + SyncHandler, + tokio::sync::mpsc::UnboundedReceiver, + tempfile::TempDir, + ); + + /// The handler `run` builds from `argv`, its events, and its store's + /// directory. + fn handler_from(argv: &[&str]) -> anyhow::Result { + let dir = tempfile::tempdir().unwrap(); + let manager = Arc::new(SyncManager::new( + Arc::new(BlockStore::open(dir.path()).unwrap()), + Arc::new(HeaderVerifier::new()), + Arc::new(rustock_networking::peers::PeerStore::new()), + )); + let (events, received) = tokio::sync::mpsc::unbounded_channel(); + let args = Args::parse_from(argv); + let handler = sync_handler_for( + manager, + events, + args.simulate_height, + rustock_core::checkpoint::CheckpointDefence::NONE, + args.network_id, + &args.banned_miners, + )?; + Ok((handler, received, dir)) + } + + /// `--banned-miners` reaches the handler: a block a peer sends whole, + /// mined by a coinbase named there, is not taken, and one mined by + /// anyone else is. + #[tokio::test] + async fn a_miner_banned_on_the_command_line_is_refused_by_the_handler() { + let banned = "0xbabababababababababababababababababababa"; + let (handler, mut received, _dir) = + handler_from(&["rustock-cli", "--banned-miners", banned]).unwrap(); + let sent = |coinbase: alloy_primitives::Address| { + let mut header = ChainConfig::regtest().genesis_header(); + header.number = 1; + header.beneficiary = coinbase; + let block = rustock_core::Block { + header, + transactions: Vec::new(), + ommers: Vec::new(), + }; + P2pMessage::RskMessage(RskMessage::new(RskSubMessage::Block(Box::new(block)))) + }; + let peer = alloy_primitives::B512::repeat_byte(4); + + handler.handle_message(peer, &sent(banned.parse().unwrap())); + let taken = received.try_recv().is_ok(); + assert!(!taken, "the banned miner's block was taken"); + handler.handle_message(peer, &sent(alloy_primitives::Address::repeat_byte(0x0c))); + let taken = received.try_recv().is_ok(); + assert!(taken, "another miner's block was refused"); + } + + /// An entry that isn't an address stops the node starting, as on rskj. + #[test] + fn a_banned_miner_that_is_not_an_address_stops_the_start() { + let refused = handler_from(&["rustock-cli", "--banned-miners", "0xbaba"]); + let err = refused.err().expect("a two-byte address was taken"); + assert!(err.to_string().contains("--banned-miners"), "{err}"); + } +} diff --git a/crates/sync/src/handler.rs b/crates/sync/src/handler.rs index 299206b..2a6dc53 100644 --- a/crates/sync/src/handler.rs +++ b/crates/sync/src/handler.rs @@ -1,12 +1,13 @@ use crate::events::SyncEvent; use crate::manager::SyncManager; -use alloy_primitives::B512; +use alloy_primitives::{Address, B512}; use crate::snap::server::SnapServer; use rustock_networking::protocol::{ BlockHashResponse, BlockHeadersResponse, BlockHeadersWithUnclesResponse, BlockIdentifier, BodyResponse, HeaderWithUncles, P2pHandler, P2pMessage, RskMessage, RskSubMessage, SkeletonResponse, }; +use std::collections::HashSet; use std::sync::Arc; use tokio::sync::mpsc; use tracing::{debug, trace, warn}; @@ -70,6 +71,39 @@ pub struct SyncHandler { /// applying mainnet's to a testnet peer is a category error that happens to /// be harmless only because testnet totals are too small to trip it. checkpoint: Option, + /// Coinbases whose blocks are not taken from a peer that sends them + /// whole: rskj's `peer.bannedMiners` (`NodeMessageHandler.bannedMiners`). + banned_miners: HashSet
, +} + +/// rskj's `peer.bannedMiners` entries as its `NodeMessageHandler` reads them, +/// `new RskAddress(String)` each (L134-136): a `0x` prefix dropped, an odd +/// number of digits read with a leading zero (`HexUtils.stringHexToByteArray`), +/// and anything but 20 bytes refused, which keeps rskj from starting. +pub fn parse_banned_miners(entries: &[String]) -> Result, String> { + entries + .iter() + .map(|entry| { + let digits = entry.strip_prefix("0x").unwrap_or(entry); + let digits = if digits.len() % 2 == 1 { + format!("0{digits}") + } else { + digits.to_string() + }; + // Checked here, so a second `0x` is refused as rskj's decoder + // refuses it rather than dropped as `hex::decode` would. + if !digits.chars().all(|c| c.is_ascii_hexdigit()) { + return Err(format!("{entry:?} is not hex")); + } + let bytes = alloy_primitives::hex::decode(&digits) + .map_err(|e| format!("{entry:?} is not hex: {e}"))?; + if bytes.len() != Address::len_bytes() { + let len = bytes.len(); + return Err(format!("{entry:?} is {len} bytes; an address is 20")); + } + Ok(Address::from_slice(&bytes)) + }) + .collect() } impl SyncHandler { @@ -82,6 +116,7 @@ impl SyncHandler { serve_ceiling: None, checkpoint_defence: rustock_core::checkpoint::CheckpointDefence::NONE, checkpoint: None, + banned_miners: HashSet::new(), } } @@ -110,6 +145,13 @@ impl SyncHandler { self } + /// Refuse a block a peer sends whole when its coinbase is one of these: + /// rskj's `peer.bannedMiners`. See [`parse_banned_miners`]. + pub fn with_banned_miners(mut self, banned_miners: HashSet
) -> Self { + self.banned_miners = banned_miners; + self + } + /// Whether a block at this height may be named or served. fn may_serve(&self, number: u64) -> bool { self.serve_ceiling.is_none_or(|c| number <= c) @@ -647,6 +689,23 @@ impl P2pHandler for SyncHandler { }); } RskSubMessage::Block(block) => { + // rskj `NodeMessageHandler.allowByMinerNotBanned` + // (L244-256), which `controlMessageIngress` asks before the + // block is queued: one mined by a banned coinbase is not + // taken. An empty coinbase is rskj's null address, which + // no listed address is, the zero address included. + let banned = block + .header + .coinbase() + .filter(|miner| self.banned_miners.contains(miner)); + if let Some(miner) = banned { + trace!( + target: "rustock::sync", + "Received block mined by banned miner {miner} from peer {:?}, not taken", + &id.0[..4] + ); + return None; + } let _ = self.event_tx.send(SyncEvent::NewBlock { peer: id, block: block.clone(), diff --git a/crates/sync/src/lib.rs b/crates/sync/src/lib.rs index 27f386b..18283f4 100644 --- a/crates/sync/src/lib.rs +++ b/crates/sync/src/lib.rs @@ -27,7 +27,7 @@ pub mod watchdog; // Re-exports for external consumers pub use announce::{broadcast_block, on_mined}; pub use events::SyncEvent; -pub use handler::SyncHandler; +pub use handler::{parse_banned_miners, SyncHandler}; pub use manager::SyncManager; pub use service::{BestBlockListeners, ExternalBlockSource, SyncService}; pub use state::{InFlightBody, SyncState}; diff --git a/crates/sync/src/tests.rs b/crates/sync/src/tests.rs index ee2c6b5..9dd977c 100644 --- a/crates/sync/src/tests.rs +++ b/crates/sync/src/tests.rs @@ -4043,6 +4043,128 @@ async fn a_block_a_peer_sends_is_handed_to_the_sync_service() { assert_eq!(got.hash(), block.hash()); } +// -- rskj's peer.bannedMiners (task 285) ------------------------------------ + +/// A handler refusing blocks mined by `banned`, and its event channel. +fn handler_banning( + banned: Address, +) -> ( + SyncHandler, + mpsc::UnboundedReceiver, + tempfile::TempDir, +) { + let dir = tempdir().unwrap(); + let manager = Arc::new(SyncManager::new( + Arc::new(BlockStore::open(dir.path()).unwrap()), + Arc::new(HeaderVerifier::new()), + Arc::new(rustock_networking::peers::PeerStore::new()), + )); + let (event_tx, event_rx) = mpsc::unbounded_channel(); + let handler = SyncHandler::new(manager, event_tx) + .with_banned_miners(std::collections::HashSet::from([banned])); + (handler, event_rx, dir) +} + +/// A `BlockMessage` carrying `header`'s block. +fn block_message(header: Header) -> rustock_networking::protocol::P2pMessage { + use rustock_networking::protocol::{P2pMessage, RskMessage, RskSubMessage}; + P2pMessage::RskMessage(RskMessage::new(RskSubMessage::Block(Box::new(whole( + header, + ))))) +} + +/// rskj `NodeMessageHandler.allowByMinerNotBanned`: a block a peer sends +/// whole is not taken when its coinbase is on `peer.bannedMiners`, and one +/// mined by anyone else is. +#[tokio::test] +async fn a_block_from_a_banned_miner_is_not_taken() { + use rustock_networking::protocol::P2pHandler; + + let banned = Address::repeat_byte(0xBA); + let (handler, mut event_rx, _dir) = handler_banning(banned); + let mined_by = |coinbase: Address| { + let mut header = dummy_header(1, B256::repeat_byte(1), U256::from(1)); + header.beneficiary = coinbase; + block_message(header) + }; + + let sender = B512::repeat_byte(4); + assert!(handler.handle_message(sender, &mined_by(banned)).is_none()); + let taken = event_rx.try_recv().is_ok(); + assert!(!taken, "a banned miner's block was taken"); + + handler.handle_message(sender, &mined_by(Address::repeat_byte(0x0C))); + assert!(matches!( + event_rx.try_recv(), + Ok(crate::SyncEvent::NewBlock { .. }) + )); +} + +/// The coinbase compared is the one rskj reads (`Header::coinbase`): an +/// empty one is its null address, which a banned zero address doesn't match. +/// The zero address written out is banned like any other. +#[tokio::test] +async fn an_empty_coinbase_is_not_a_banned_zero_address() { + use rustock_networking::protocol::P2pHandler; + + let (handler, mut event_rx, _dir) = handler_banning(Address::ZERO); + let mut empty = dummy_header(1, B256::repeat_byte(1), U256::from(1)); + empty.kept = Some(Box::new(rustock_core::types::header::KeptFields { + null_coinbase: true, + ..Default::default() + })); + assert_eq!(empty.beneficiary, Address::ZERO); + + handler.handle_message(B512::repeat_byte(4), &block_message(empty)); + assert!( + matches!(event_rx.try_recv(), Ok(crate::SyncEvent::NewBlock { .. })), + "a block with an empty coinbase was refused" + ); + + let zero = dummy_header(2, B256::repeat_byte(1), U256::from(1)); + handler.handle_message(B512::repeat_byte(4), &block_message(zero)); + let taken = event_rx.try_recv().is_ok(); + assert!(!taken, "the zero address's block was taken"); +} + +/// `peer.bannedMiners` entries read as rskj reads them, `new +/// RskAddress(String)` (`HexUtils.stringHexToByteArray`, then exactly 20 +/// bytes); one that doesn't read stops the node starting, as rskj's does. +#[test] +fn banned_miners_are_read_as_rskj_reads_them() { + let read = |entries: &[&str]| { + let entries: Vec = entries.iter().map(|e| e.to_string()).collect(); + crate::parse_banned_miners(&entries) + }; + let ones = Address::repeat_byte(0x11); + let set = |a: Address| Ok(std::collections::HashSet::from([a])); + for entry in [ + "0x1111111111111111111111111111111111111111", + "1111111111111111111111111111111111111111", + ] { + assert_eq!(read(&[entry]), set(ones), "{entry:?}"); + } + // An odd number of digits is read with a leading zero. + let mut padded = [0x11u8; 20]; + padded[0] = 0x01; + assert_eq!( + read(&["0x111111111111111111111111111111111111111"]), + set(Address::from(padded)) + ); + // A lowercase prefix, once; hex; twenty bytes, no more and no fewer. + for bad in [ + "0X1111111111111111111111111111111111111111", + "0x0x1111111111111111111111111111111111111111", + "0xzz11111111111111111111111111111111111111", + "0x11111111111111111111111111111111111111", + "0x111111111111111111111111111111111111111111", + "0x", + "", + ] { + assert!(read(&[bad]).is_err(), "{bad:?} was read as an address"); + } +} + /// The edges of rskj's two distance guards are taken: a block a whole /// skeleton above the best one, and one just young enough to be an uncle. Each /// has no parent here, so each is taken as an announcement of itself: its diff --git a/node-light.example.toml b/node-light.example.toml index 6e31a02..92d6f6d 100644 --- a/node-light.example.toml +++ b/node-light.example.toml @@ -120,6 +120,9 @@ # inbound_cidr_prefix = 24 # default # bootnodes = ["127.0.0.1:30304"] # default: unset — replaces the chain's built-in list; port is listen+1 # banned_peers = ["203.0.113.0/24"] # default: unset — refused at connection time; rskj's peer.bannedPeerIPs +# A block a peer sends whole is refused when its coinbase is one of these +# 20-byte addresses, hex with or without 0x (rskj's peer.bannedMiners). +# banned_miners = ["0x1111111111111111111111111111111111111111"] # default: unset # no_peer_punishment = false # default — count scoring events but never act on them # Never learn node ids from peers and never dial them. With `bootnodes` this # pins the node to exactly the addresses named there, which is what makes a diff --git a/node.example.toml b/node.example.toml index 8b2a56c..73dc8a2 100644 --- a/node.example.toml +++ b/node.example.toml @@ -115,6 +115,9 @@ # inbound_cidr_prefix = 24 # default # bootnodes = ["127.0.0.1:30304"] # default: unset — replaces the chain's built-in list; port is listen+1 # banned_peers = ["203.0.113.0/24"] # default: unset — refused at connection time; rskj's peer.bannedPeerIPs +# A block a peer sends whole is refused when its coinbase is one of these +# 20-byte addresses, hex with or without 0x (rskj's peer.bannedMiners). +# banned_miners = ["0x1111111111111111111111111111111111111111"] # default: unset # no_peer_punishment = false # default — count scoring events but never act on them # Never learn node ids from peers and never dial them. With `bootnodes` this # pins the node to exactly the addresses named there, which is what makes a From 85b9df5ae8c8edf20c39f0f10095a5404e8dd2f9 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Tue, 10 Nov 2026 07:59:27 -0800 Subject: [PATCH 081/264] execution: traces and estimates run the chain's exec loop revm's inspected run used its own mainnet loop, which leaves out every rule rskj's VM keeps there: RSKIP150's depth of 400, a created contract's nonce, the empty deploy before RSKIP453, a nested constructor's refund, the halt of a precompile's caller before RSKIP197, a failed frame taking back what its precompiles wrote, and more. debug_traceTransaction, trace_*, a traced block's replay and eth_estimateGas now run the chain's loop with the inspector's hooks. The chain's own run is unchanged. --- crates/execution/src/executor.rs | 452 ++++++++++++++++++++++++++++ crates/execution/src/rsk_handler.rs | 223 ++++++++++++-- 2 files changed, 653 insertions(+), 22 deletions(-) diff --git a/crates/execution/src/executor.rs b/crates/execution/src/executor.rs index 3b15553..6905244 100644 --- a/crates/execution/src/executor.rs +++ b/crates/execution/src/executor.rs @@ -2952,6 +2952,458 @@ mod tests { assert!(trace.truncated, "and the trace says it was cut short"); } + /// An unsigned transaction from a test's funded sender, at no gas price. + fn unsigned_tx( + nonce: u64, + to: Option
, + value: u64, + gas_limit: u64, + input: Vec, + ) -> rustock_core::Transaction { + rustock_core::Transaction { + nonce, + gas_price: U256::ZERO, + gas_limit: U256::from(gas_limit), + to: to.map_or_else(Bytes::new, |to| Bytes::copy_from_slice(to.as_slice())), + value: U256::from(value), + input: Bytes::from(input), + v: 0, + r: U256::ZERO, + s: U256::ZERO, + cached_rlp: None, + } + } + + /// What a block's run leaves, to hold one run against another: the root + /// its changes give the parent's state, precompiles' writes and markers + /// included, its gas and fees, and each transaction's gas, success, + /// output and logs. + #[allow(clippy::type_complexity)] + fn block_outcome( + result: &BlockExecutionResult, + parent: &TrieNode, + store: &MemoryTrieStore, + ) -> ( + B256, + u64, + U256, + Vec<(u64, bool, Vec, Vec)>, + ) { + let root = crate::state::apply_state_changes( + parent, + store, + &result.state_changes, + &result.markers, + ); + let receipts = result + .tx_results + .iter() + .map(|r| (r.gas_used, r.success, r.output.clone(), r.logs.clone())) + .collect(); + ( + root.compute_hash(store), + result.gas_used, + result.paid_fees, + receipts, + ) + } + + /// Runs a block as the chain does, as `trace_*` does (every transaction + /// traced) and as `debug_traceTransaction` does (each transaction in + /// turn), and requires the same state and receipts of all of them. The + /// traced runs go through `inspect_run`, so they agree only while it runs + /// the loop the chain runs (task 277). Returns the chain's run. + fn assert_traced_runs_agree( + executor: &RskExecutor, + header: &Header, + transactions: &[(rustock_core::Transaction, Address)], + parent: &TrieNode, + store: &Arc, + ) -> BlockExecutionResult { + let plain = executor + .execute_block(header, transactions, parent, store.clone()) + .expect("the block executes"); + let expected = block_outcome(&plain, parent, store); + let (every, traces) = executor + .execute_block_call_traced(header, transactions, parent, store.clone()) + .expect("the block executes with every transaction traced"); + assert_eq!(traces.len(), transactions.len()); + assert_eq!( + block_outcome(&every, parent, store), + expected, + "trace_* ran another execution" + ); + for index in 0..transactions.len() { + let (one, trace) = executor + .execute_block_traced( + header, + transactions, + parent, + store.clone(), + index, + crate::tracer::DEFAULT_MAX_STRUCT_LOGS, + ) + .expect("the block executes with one transaction traced"); + assert!(trace.is_some()); + assert_eq!( + block_outcome(&one, parent, store), + expected, + "debug_traceTransaction of transaction {index} ran another execution" + ); + } + plain + } + + /// An estimate's run of a transaction against the local call's, which + /// serves `eth_call`: the same gas, success, output and logs. Returns + /// what the estimate's inspector saw. + fn assert_estimate_run_agrees( + executor: &RskExecutor, + header: &Header, + tx: &rustock_core::Transaction, + sender: Address, + state: &TrieNode, + store: &Arc, + ) -> crate::gas_estimator::EstimateTelemetry { + let call = executor + .execute_local_call(header, tx, sender, state, store.clone()) + .expect("the call executes"); + let (estimated, telemetry) = executor + .execute_tx_for_estimate(header, tx, sender, state, store.clone(), true) + .expect("the estimate executes"); + assert_eq!( + ( + estimated.gas_used, + estimated.success, + &estimated.output, + &estimated.logs + ), + (call.gas_used, call.success, &call.output, &call.logs), + "eth_estimateGas ran another execution" + ); + telemetry + } + + /// A create transaction's data deploying `runtime` (at most 255 bytes): + /// CODECOPY it from behind this 12-byte prefix and RETURN it. + fn deploying(runtime: &[u8]) -> Vec { + let size = u8::try_from(runtime.len()).unwrap(); + let mut init = vec![ + 0x60, size, 0x60, 0x0c, 0x60, 0x00, 0x39, 0x60, size, 0x60, 0x00, 0xf3, + ]; + init.extend_from_slice(runtime); + init + } + + /// A trace and an estimate run the loop the chain runs (task 277): here + /// RSKIP150's call depth of 400, where revm's own loop recurses to 1024, + /// and the nonce a top-level create leaves its contract, 0, where revm's + /// loop leaves 1. The contract counts its depth in slot 0 and calls + /// itself, with value only at the depth RSKIP150 refuses, and rskj counts + /// that call for the estimate: `VM.getMessageCall` sets + /// `callWithValuePerformed` before `Program.callToAddress` checks the + /// depth. + #[test] + fn a_trace_or_an_estimate_stops_at_rskip150s_depth() { + let store = Arc::new(MemoryTrieStore::new()); + let sender = Address::repeat_byte(0xAD); + let one_rbtc = U256::from(10u64).pow(U256::from(18)); + let root = put_account(&TrieNode::empty(), store.as_ref(), &sender, 0, one_rbtc); + let block_store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); + + // n = SLOAD(0) + 1; SSTORE(0, n); CALL(GAS, ADDRESS, n == 401), no + // data and no return area; POP; STOP. + let runtime = [ + 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, // return area, data + 0x60, 0x00, 0x54, 0x60, 0x01, 0x01, // n + 0x80, 0x60, 0x00, 0x55, // SSTORE(0, n) + 0x61, 0x01, 0x91, 0x14, // the value: n == 401 + 0x30, 0x5a, 0xf1, 0x50, 0x00, // ADDRESS GAS CALL POP STOP + ]; + // The contract holds a wei, so that revm's loop, which doesn't refuse + // the call at depth 401, makes it. + let deploy = unsigned_tx(0, None, 1, 1_000_000, deploying(&runtime)); + let deployed = assert_traced_runs_agree( + &executor, + &dummy_header(2_500_000), + &[(deploy, sender)], + &root, + &store, + ); + let contract = sender.create(0); + assert_eq!(deployed.state_changes[&contract].info.nonce, 0); + let root = crate::state::apply_state_changes( + &root, + store.as_ref(), + &deployed.state_changes, + &deployed.markers, + ); + + let call = unsigned_tx(1, Some(contract), 0, 6_000_000, Vec::new()); + let header = dummy_header(2_500_001); + let called = + assert_traced_runs_agree(&executor, &header, &[(call.clone(), sender)], &root, &store); + assert!(called.tx_results[0].success); + assert_eq!( + called.state_changes[&contract].storage[&U256::ZERO].present_value, + U256::from(401), + "frames at depths 0..=400 run; the call at depth 400 is refused" + ); + let telemetry = + assert_estimate_run_agrees(&executor, &header, &call, sender, &root, &store); + assert!(telemetry.call_with_value, "the refused call carried value"); + + // The call tree: 400 calls, each inside the one before. + let (_, traces) = executor + .execute_block_call_traced(&header, &[(call, sender)], &root, store.clone()) + .unwrap(); + let mut level = &traces[0].subtraces; + let mut depth = 0; + while let [only] = level.as_slice() { + if only.error.is_some() { + break; + } + depth += 1; + level = &only.subtraces; + } + assert_eq!(depth, 400); + } + + /// A trace and an estimate run the loop the chain runs (task 277): before + /// RSKIP453 an internal create whose code deposit can't be paid deploys + /// empty code (`Program.finalizeContractCreation`), where revm's loop + /// fails the create. Mainnet #1,071,481's RNS Deed. + #[test] + fn a_trace_or_an_estimate_deploys_empty_code_before_rskip453() { + let store = Arc::new(MemoryTrieStore::new()); + let sender = Address::repeat_byte(0xAA); + let one_rbtc = U256::from(10u64).pow(U256::from(18)); + let root = put_account(&TrieNode::empty(), store.as_ref(), &sender, 0, one_rbtc); + let block_store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); + + // The child's constructor RETURNs 65,536 bytes, whose deposit no + // transaction here can pay. The caller CREATEs it and stores the + // address it gets in slot 0. + let runtime = [ + 0x66, 0x62, 0x01, 0x00, 0x00, 0x60, 0x00, 0xf3, // PUSH7 the child's init + 0x60, 0x00, 0x52, // MSTORE at word 0 + 0x60, 0x07, 0x60, 0x19, 0x60, 0x00, 0xf0, // CREATE(0, 25, 7) + 0x60, 0x00, 0x55, // SSTORE(0, created) + 0x00, + ]; + let deploy = unsigned_tx(0, None, 0, 1_000_000, deploying(&runtime)); + let deployed = assert_traced_runs_agree( + &executor, + &dummy_header(1_071_481), + &[(deploy, sender)], + &root, + &store, + ); + let root = crate::state::apply_state_changes( + &root, + store.as_ref(), + &deployed.state_changes, + &deployed.markers, + ); + + let caller = sender.create(0); + let call = unsigned_tx(1, Some(caller), 0, 1_000_000, Vec::new()); + let header = dummy_header(1_071_482); + let created = + assert_traced_runs_agree(&executor, &header, &[(call.clone(), sender)], &root, &store); + let child = caller.create(0); + assert_eq!( + created.state_changes[&caller].storage[&U256::ZERO].present_value, + U256::from_be_slice(child.as_slice()), + "the create succeeds, with empty code" + ); + assert_estimate_run_agrees(&executor, &header, &call, sender, &root, &store); + } + + /// A trace and an estimate run the loop the chain runs (task 277): a + /// nested constructor's refund is dropped (`Program.finalizeContractCreation` + /// passes on no `futureRefund`), where revm's loop keeps it. Mainnet + /// #6,362,186. + #[test] + fn a_trace_or_an_estimate_drops_a_nested_constructors_refund() { + let store = Arc::new(MemoryTrieStore::new()); + let sender = Address::repeat_byte(0xAB); + let one_rbtc = U256::from(10u64).pow(U256::from(18)); + let root = put_account(&TrieNode::empty(), store.as_ref(), &sender, 0, one_rbtc); + let block_store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); + + // The child's constructor sets slot 0 and clears it, which earns a + // refund, and returns no code. The factory MSTOREs it right-aligned + // and CREATEs it. + let child_init = [ + 0x60, 0x01, 0x60, 0x00, 0x55, // SSTORE(0, 1) + 0x60, 0x00, 0x60, 0x00, 0x55, // SSTORE(0, 0) + 0x60, 0x00, 0x60, 0x00, 0xf3, // RETURN nothing + ]; + let size = u8::try_from(child_init.len()).unwrap(); + let mut runtime = vec![0x5f + size]; + runtime.extend_from_slice(&child_init); + runtime.extend_from_slice(&[0x60, 0x00, 0x52, 0x60, size, 0x60, 32 - size]); + runtime.extend_from_slice(&[0x60, 0x00, 0xf0, 0x00]); // CREATE, STOP + let deploy = unsigned_tx(0, None, 0, 2_000_000, deploying(&runtime)); + let deployed = assert_traced_runs_agree( + &executor, + &dummy_header(6_362_186), + &[(deploy, sender)], + &root, + &store, + ); + let root = crate::state::apply_state_changes( + &root, + store.as_ref(), + &deployed.state_changes, + &deployed.markers, + ); + + let call = unsigned_tx(1, Some(sender.create(0)), 0, 2_000_000, Vec::new()); + let header = dummy_header(6_362_187); + let created = + assert_traced_runs_agree(&executor, &header, &[(call.clone(), sender)], &root, &store); + assert!(created.tx_results[0].success); + assert_estimate_run_agrees(&executor, &header, &call, sender, &root, &store); + } + + /// A trace and an estimate run the loop the chain runs (task 277): before + /// RSKIP197 a contract whose call to a precompile throws stops there with + /// all its gas spent (`VM.steps`), where revm's loop lets it go on. And + /// the trace stops at its CALL, as rskj's VM traces no step after it. + #[test] + fn a_trace_or_an_estimate_stops_a_precompiles_caller_before_rskip197() { + let store = Arc::new(MemoryTrieStore::new()); + let sender = Address::repeat_byte(0xAA); + let one_rbtc = U256::from(10u64).pow(U256::from(18)); + let root = put_account(&TrieNode::empty(), store.as_ref(), &sender, 0, one_rbtc); + // The inner contract calls HDWalletUtils with no payload, which names + // no method and throws, and stops. The outer one gives it 200,000 gas + // and returns what its CALL pushed. + let outer = Address::repeat_byte(0xB1); + let inner = Address::repeat_byte(0xB2); + let mut inner_code = vec![ + 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x73, + ]; + inner_code.extend_from_slice(crate::precompiles::HD_WALLET_UTILS_ADDR.as_slice()); + inner_code.extend_from_slice(&[0x5a, 0xf1, 0x00]); // GAS, CALL, STOP + let outer_code = call_and_return_pushed(inner, 200_000); + let root = put_account_with_code(&root, store.as_ref(), &outer, 1, U256::ZERO, &outer_code); + let root = put_account_with_code(&root, store.as_ref(), &inner, 1, U256::ZERO, &inner_code); + let block_store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); + let header = dummy_header(2_000_000); + + let to_outer = unsigned_tx(0, Some(outer), 0, 1_000_000, Vec::new()); + let block = assert_traced_runs_agree( + &executor, + &header, + &[(to_outer.clone(), sender)], + &root, + &store, + ); + assert_eq!( + U256::from_be_slice(&block.tx_results[0].output), + U256::ZERO, + "the outer contract's CALL pushes 0" + ); + assert_estimate_run_agrees(&executor, &header, &to_outer, sender, &root, &store); + + // The transaction's own frame is the caller: it fails, and its trace + // ends at the CALL. + let to_inner = unsigned_tx(0, Some(inner), 0, 1_000_000, Vec::new()); + let plain = executor + .execute_tx(&header, &to_inner, sender, &root, store.clone()) + .unwrap(); + assert!(!plain.success); + let (traced, trace) = executor + .execute_tx_traced( + &header, + &to_inner, + sender, + &root, + store.clone(), + false, + crate::tracer::DEFAULT_MAX_STRUCT_LOGS, + ) + .unwrap(); + assert_eq!( + (traced.gas_used, traced.success), + (plain.gas_used, plain.success) + ); + assert_eq!( + trace.struct_logs.last().map(|step| step.op.as_str()), + Some("CALL") + ); + } + + /// A trace and an estimate run the loop the chain runs (task 277): a frame + /// that fails takes back what the precompiles under it wrote outside + /// revm's journal (`9821143`), where revm's loop keeps it. A contract + /// seeds the Bridge's BTC store through a getter and reverts. + #[test] + fn a_trace_or_an_estimate_takes_back_a_failed_frames_precompile_writes() { + // Code that CALLs `to` with `data` (32 bytes at most), all its gas + // and no value, and drops what the CALL pushed. + fn call(to: Address, data: &[u8]) -> Vec { + let mut word = [0u8; 32]; + word[..data.len()].copy_from_slice(data); + let mut code = vec![0x7f]; // PUSH32 the data, left-aligned + code.extend_from_slice(&word); + code.extend_from_slice(&[0x60, 0x00, 0x52]); // MSTORE it at 0 + let size = u8::try_from(data.len()).unwrap(); + code.extend_from_slice(&[0x60, 0x00, 0x60, 0x00, 0x60, size, 0x60, 0x00, 0x60, 0x00]); + code.push(0x73); // PUSH20 to + code.extend_from_slice(to.as_slice()); + code.extend_from_slice(&[0x5a, 0xf1, 0x50]); // GAS, CALL, POP + code + } + let bridge = crate::precompiles::BRIDGE_ADDR; + let seed = crate::abi::selector("getBtcBlockchainBestChainHeight()"); + let seeds_and_reverts = Address::repeat_byte(0xC2); + let mut seeds_and_reverts_code = call(bridge, &seed); + seeds_and_reverts_code.extend_from_slice(&[0x60, 0x00, 0x60, 0x00, 0xfd]); // REVERT + let entry = Address::repeat_byte(0xE0); + let mut entry_code = call(seeds_and_reverts, &[]); + entry_code.push(0x00); // STOP + + let store = Arc::new(MemoryTrieStore::new()); + let sender = Address::repeat_byte(0xAA); + let one_rbtc = U256::from(10u64).pow(U256::from(18)); + let root = put_account(&TrieNode::empty(), store.as_ref(), &sender, 0, one_rbtc); + let root = put_account_with_code(&root, store.as_ref(), &entry, 1, U256::ZERO, &entry_code); + let root = put_account_with_code( + &root, + store.as_ref(), + &seeds_and_reverts, + 1, + U256::ZERO, + &seeds_and_reverts_code, + ); + let block_store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let executor = RskExecutor::new(RskHardforkConfig::all_active(33), block_store); + let header = dummy_header(8_000_000); + + let tx = unsigned_tx(0, Some(entry), 0, 1_000_000, Vec::new()); + let block = + assert_traced_runs_agree(&executor, &header, &[(tx.clone(), sender)], &root, &store); + assert!(block.tx_results[0].success); + assert!( + !block + .markers + .raw_storage + .iter() + .any(|(address, _, _)| *address == bridge), + "the Bridge's seeding went back with the frame" + ); + assert!(!block.markers.precompiles.contains(&bridge)); + assert_estimate_run_agrees(&executor, &header, &tx, sender, &root, &store); + } + #[test] fn test_rskj_call_stipend_is_charged_to_caller() { let store = Arc::new(MemoryTrieStore::new()); diff --git a/crates/execution/src/rsk_handler.rs b/crates/execution/src/rsk_handler.rs index 1f4ec1f..dca90a9 100644 --- a/crates/execution/src/rsk_handler.rs +++ b/crates/execution/src/rsk_handler.rs @@ -365,23 +365,53 @@ where Ok(()) } + /// The mainnet exec loop with rskj's rules added (`exec_loop`). + fn run_exec_loop( + &mut self, + evm: &mut Self::Evm, + first_frame_input: FrameInit, + ) -> Result { + self.exec_loop::(evm, first_frame_input) + } +} + +impl RskHandler> +where + EVM: EvmTr< + Context: ContextTr< + Chain = crate::raw_storage::RskChainExt, + Journal: JournalTr + JournalExt, + >, + Frame = EthFrame, + Instructions: InstructionProvider< + Context = ::Context, + InterpreterTypes = EthInterpreter, + >, + Precompiles: PrecompileProvider<::Context>, + >, + ERROR: EvmTrError, +{ /// The mainnet exec loop with rskj's rules added, among them: /// - immediately after a CREATE frame is initialized, reset the created /// account's nonce to 0 (rskj has no EIP-161; revm seeds it to 1 from /// the per-era CfgEnv spec). Must happen before the constructor runs — /// addresses the new contract derives via CREATE depend on its nonce. - /// - frames run through `rsk_frame_run`, which applies the pre-RSKIP453 - /// empty-deploy quirk to internal creates. + /// - frames return through `rsk_frame_return`, which applies the + /// pre-RSKIP453 empty-deploy quirk to internal creates. /// - before RSKIP197, a contract whose call to a precompile threw is /// halted as it takes the call's result (see `aborts_caller`). /// - what the precompiles called under a frame of contract code write /// outside revm's journal goes back with the frame if it fails (see /// `frames`). - fn run_exec_loop( + /// + /// The chain's run (`run_exec_loop`) and a trace's or an estimate's + /// (`inspect_run_exec_loop`) both run this loop. They differ only in how + /// `D` opens and runs a frame: without the inspector's hooks or with them. + fn exec_loop>( &mut self, - evm: &mut Self::Evm, + evm: &mut EVM, first_frame_input: FrameInit, - ) -> Result { + ) -> Result { // rskj builds a program's invocation only for a transaction that runs // code (`TransactionExecutor.call` and `create`); revm opens a frame // for a creation with no data too, which runs nothing. @@ -391,8 +421,7 @@ where } _ => true, }; - let res = evm.frame_init(first_frame_input)?; - if let ItemOrResult::Result(frame_result) = res { + if let ItemOrResult::Result(frame_result) = D::init(evm, first_frame_input)? { return Ok(frame_result); } if self.fail_programs && runs_code { @@ -408,7 +437,7 @@ where self.open_frame(evm); loop { - let call_or_result = rsk_frame_run(evm, self.empty_create_on_unpayable_deposit)?; + let call_or_result = D::run(evm, self.empty_create_on_unpayable_deposit)?; // Pre-RSKIP150 unbounded precompile output write, applied to the // caller after `frame_return_result` (see field docs). @@ -452,6 +481,9 @@ where init.depth ); } + // An inspector hears of the refusal as revm tells it + // of a frame refused at its own limit of 1024. + let synth = D::refuse(evm, init, synth); note_attempted_writes(evm); let returned = evm.frame_return_result(synth)?; forget_reverted_entries(evm); @@ -468,8 +500,8 @@ where continue; } } - match evm.frame_init(init)? { - ItemOrResult::Item(_) => { + match D::init(evm, init)? { + ItemOrResult::Item(()) => { // Internal CREATE/CREATE2 opcode: post-RSKIP125 // (wasabi) rskj's Program.createContract bumps the // created contract's nonce to 1; before, it is 0. @@ -534,7 +566,7 @@ where } } // A frame of contract code ended, having committed exactly - // when its result is ok (`rsk_frame_run`). + // when its result is ok (`rsk_frame_return`). ItemOrResult::Result(result) => { self.close_frame(evm, result.instruction_result().is_ok()); result @@ -594,6 +626,117 @@ where } } +/// How `exec_loop` opens and runs a frame. The chain's run does it plainly; +/// a trace's or an estimate's does it as revm's `inspect_run_exec_loop` does, +/// telling the inspector of every frame that starts or ends and of every +/// step. Every rule rskj's VM keeps in the loop is the loop's, the same for +/// both. +trait FrameDriver>> { + /// `EvmTr::frame_init`: a frame opened, or a result reached without one. + fn init( + evm: &mut EVM, + init: FrameInit, + ) -> Result, revm::handler::evm::ContextDbError>; + /// Runs the top frame until it asks for another or returns. + fn run( + evm: &mut EVM, + empty_create_on_unpayable_deposit: bool, + ) -> Result>, ContextTrDbError>; + /// A frame the loop refuses itself (RSKIP150's depth), with the result + /// revm gives a frame it refuses at its own limit. + fn refuse(evm: &mut EVM, init: FrameInit, refused: FrameResult) -> FrameResult; +} + +/// The chain's run: no inspector. +struct Plain; + +impl FrameDriver for Plain +where + EVM: EvmTr< + Context: ContextTr>, + Frame = EthFrame, + Instructions: InstructionProvider< + Context = ::Context, + InterpreterTypes = EthInterpreter, + >, + >, +{ + fn init( + evm: &mut EVM, + init: FrameInit, + ) -> Result, revm::handler::evm::ContextDbError> + { + Ok(evm.frame_init(init)?.map_item(|_| ())) + } + + fn run( + evm: &mut EVM, + empty_create_on_unpayable_deposit: bool, + ) -> Result>, ContextTrDbError> { + rsk_frame_run(evm, empty_create_on_unpayable_deposit) + } + + fn refuse(_evm: &mut EVM, _init: FrameInit, refused: FrameResult) -> FrameResult { + refused + } +} + +/// A trace's or an estimate's run: the inspector is told what revm's +/// `inspect_frame_init` and `inspect_frame_run` tell it. +struct Inspected; + +impl FrameDriver for Inspected +where + EVM: revm::inspector::InspectorEvmTr< + Context: ContextTr>, + Frame = EthFrame, + >, +{ + fn init( + evm: &mut EVM, + init: FrameInit, + ) -> Result, revm::handler::evm::ContextDbError> + { + Ok(evm.inspect_frame_init(init)?.map_item(|_| ())) + } + + fn run( + evm: &mut EVM, + empty_create_on_unpayable_deposit: bool, + ) -> Result>, ContextTrDbError> { + use revm::inspector::handler::frame_end; + use revm::inspector::inspect_instructions; + use revm::interpreter::interpreter_types::LoopControl; + let (ctx, inspector, frame, instructions) = evm.ctx_inspector_frame_instructions(); + // A frame the loop halted as it took a call's result (a precompile + // it called threw before RSKIP197) runs no instruction more, and the + // inspector is told of none: rskj's VM stops it at the CALL. + let action = if frame.interpreter.bytecode.is_end() { + frame.interpreter.take_next_action() + } else { + inspect_instructions( + ctx, + &mut frame.interpreter, + &mut *inspector, + instructions.instruction_table(), + ) + }; + let mut result = rsk_frame_return(ctx, frame, action, empty_create_on_unpayable_deposit); + if let ItemOrResult::Result(frame_result) = &mut result { + frame_end(ctx, inspector, &frame.input, frame_result); + } + Ok(result) + } + + fn refuse(evm: &mut EVM, mut init: FrameInit, refused: FrameResult) -> FrameResult { + use revm::inspector::handler::{frame_end, frame_start}; + let (ctx, inspector) = evm.ctx_inspector(); + let mut result = frame_start(ctx, inspector, &mut init.frame_input).unwrap_or(refused); + frame_end(ctx, inspector, &init.frame_input, &mut result); + result + } +} + impl RskHandler> where EVM: EvmTr, @@ -683,13 +826,34 @@ where .interpreter .run_plain(instructions.instruction_table(), ctx); + Ok(rsk_frame_return( + ctx, + frame, + action, + empty_create_on_unpayable_deposit, + )) +} + +/// `EthFrame::process_next_action` with the create-return path replaced by +/// `rsk_return_create`: what a frame's run ends in, a frame to open or the +/// frame's result, the frame marked finished once it has one. The chain's +/// run and an inspected one share it. +fn rsk_frame_return( + ctx: &mut CTX, + frame: &mut EthFrame, + action: InterpreterAction, + empty_create_on_unpayable_deposit: bool, +) -> FrameInitOrResult> +where + CTX: ContextTr>, +{ let mut interpreter_result = match action { InterpreterAction::NewFrame(frame_input) => { - return Ok(ItemOrResult::Item(FrameInit { + return ItemOrResult::Item(FrameInit { frame_input, depth: frame.depth + 1, memory: frame.interpreter.memory.new_child_context(), - })); + }); } InterpreterAction::Return(result) => result, }; @@ -734,7 +898,7 @@ where } }; frame.set_finished(true); - Ok(result) + result } /// rskj `Program.finalizeContractCreation` before RSKIP453: a constructor @@ -872,17 +1036,24 @@ where } } -/// Inspection support, for `debug_traceTransaction` and the `trace_*` methods. +/// Inspection support, for `debug_traceTransaction`, the `trace_*` methods, +/// a traced block's replay and `eth_estimateGas`. /// -/// Every method that matters is defaulted by the trait: `inspect_run` mirrors -/// `Handler::run` while driving the inspector's `step` / `call` / `create` -/// hooks. So the traced execution goes through **this same handler** -- the -/// same RSKIP125 and RSKIP150 behaviour, the same invisible-exception -/// handling, the same precompile set -- rather than a second implementation -/// that could drift from it. +/// `inspect_run` mirrors `Handler::run` while driving the inspector's `step` +/// / `call` / `create` hooks, and calls this handler's methods -- all but the +/// loop, which revm defaults to its own mainnet loop. That loop would leave +/// out every rule rskj's VM keeps there: RSKIP150's depth of 400, a created +/// contract's nonce, the empty deploy before RSKIP453, a precompile's whole +/// output before RSKIP150, the return-data buffer before RSKIP171, a nested +/// constructor's refund, RSKIP-144's attempted writes, the halt of a +/// precompile's caller before RSKIP197, and a failed frame taking back what +/// its precompiles wrote. So `inspect_run_exec_loop` runs `exec_loop`, the +/// loop the chain runs, with the inspector told of each frame and step. /// /// That matters more than the convenience: a tracer describing a different -/// execution from the one the chain performed is worse than no tracer. +/// execution from the one the chain performed is worse than no tracer, an +/// estimate of one is no estimate, and a traced block's replay would drift +/// from the chain after the traced transaction. impl revm::inspector::InspectorHandler for RskHandler> where EVM: revm::inspector::InspectorEvmTr< @@ -901,4 +1072,12 @@ where ERROR: EvmTrError, { type IT = EthInterpreter; + + fn inspect_run_exec_loop( + &mut self, + evm: &mut Self::Evm, + first_frame_input: FrameInit, + ) -> Result { + self.exec_loop::(evm, first_frame_input) + } } From 357fb6c2137e07a8b9b384432ef98c025ce85ec9 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Tue, 10 Nov 2026 07:59:28 -0800 Subject: [PATCH 082/264] rpc: answer a transaction from the main chain only A reorg can put a transaction in a block of each branch. The index kept only the entry written last, which after a reorg back is the block that lost. It now keeps one entry per block, as rskj's ReceiptStoreImplV2 does, and the transaction, receipt and trace methods read the canonical one, as rskj's getInMainChain does (upstream #343). --- crates/rpc/src/debug.rs | 9 ++- crates/rpc/src/rsk.rs | 18 +---- crates/rpc/src/tests.rs | 105 ++++++++++++++++++++++++ crates/rpc/src/trace.rs | 18 ++++- crates/rpc/src/tx.rs | 31 +++++-- crates/storage/src/lib.rs | 165 ++++++++++++++++++++++++++++++++++---- 6 files changed, 307 insertions(+), 39 deletions(-) diff --git a/crates/rpc/src/debug.rs b/crates/rpc/src/debug.rs index b44bf40..1f0da06 100644 --- a/crates/rpc/src/debug.rs +++ b/crates/rpc/src/debug.rs @@ -96,7 +96,14 @@ pub fn debug_trace_transaction(id: Value, params: &Value, state: &RpcState) -> J else { return JsonRpcResponse::error(id, INVALID_PARAMS, "Missing or invalid transaction hash"); }; - let Some((block_hash, index)) = state.store.tx_location(tx_hash).ok().flatten() else { + // On the main chain only, as rskj's tracer looks it up + // (`receiptStore.getInMainChain`): a block that lost a reorg is no answer. + let Some((block_hash, index)) = state + .store + .tx_location_in_main_chain(tx_hash) + .ok() + .flatten() + else { return JsonRpcResponse::success(id, Value::Null); }; diff --git a/crates/rpc/src/rsk.rs b/crates/rpc/src/rsk.rs index a684e1f..7d183b7 100644 --- a/crates/rpc/src/rsk.rs +++ b/crates/rpc/src/rsk.rs @@ -324,23 +324,13 @@ pub fn rsk_get_raw_transaction_receipt_by_hash( return JsonRpcResponse::error(id, INVALID_PARAMS, "Missing or invalid transaction hash"); }; - let Ok(Some((block_hash, index))) = store.tx_location(tx_hash) else { + // rskj's `receiptStore.getInMainChain`: the canonical block holding the + // transaction, though a block that lost a reorg holds it too and may be + // the one the index wrote last. + let Ok(Some((block_hash, index))) = store.tx_location_in_main_chain(tx_hash) else { return JsonRpcResponse::success(id, Value::Null); }; - // `tx_location` indexes the canonical chain, which is rskj's - // `getInMainChain`; re-checking the canonical pointer keeps that true if - // the index is ever widened to cover forks. - let on_main_chain = store - .header(block_hash) - .ok() - .flatten() - .and_then(|h| store.canonical_hash(h.number).ok().flatten()) - .is_some_and(|canonical| canonical == block_hash); - if !on_main_chain { - return JsonRpcResponse::success(id, Value::Null); - } - let Ok(Some(receipts)) = store.receipts(block_hash) else { return JsonRpcResponse::success(id, Value::Null); }; diff --git a/crates/rpc/src/tests.rs b/crates/rpc/src/tests.rs index 7a62a80..430d384 100644 --- a/crates/rpc/src/tests.rs +++ b/crates/rpc/src/tests.rs @@ -3884,6 +3884,111 @@ fn setup_state_for_tracing() -> (RpcState, tempfile::TempDir, B256, Address, Add (state, tmp, tx_hash, sender, caller, callee) } +/// A transaction is looked up on the main chain only, as rskj's +/// `ReceiptStore.getInMainChain` looks it up (upstream #343). A block that +/// lost a reorg keeps its body and receipts, and may be the block the index +/// wrote last, but answers for nothing: a transaction both branches hold is +/// found in the canonical one, and one no canonical block holds is null to +/// every method, or pending to `eth_getTransactionByHash` if the pool holds +/// it. The main chain comes before the pool. +#[tokio::test] +async fn a_transaction_is_looked_up_on_the_main_chain_only() { + let (mut state, _tmp, tx_hash, sender, _, _) = setup_state_for_tracing(); + let hash = format!("{tx_hash:#x}"); + let methods = [ + ("eth_getTransactionByHash", json!([hash])), + ("eth_getTransactionReceipt", json!([hash])), + ("debug_traceTransaction", json!([hash])), + ("trace_transaction", json!([hash])), + ("trace_get", json!([hash, ["0x0"]])), + ("rsk_getRawTransactionReceiptByHash", json!([hash])), + ]; + let canonical = state.store.canonical_hash(42).unwrap().unwrap(); + let parent_hash = state.store.canonical_hash(41).unwrap().unwrap(); + let (transactions, _) = state.store.body(canonical).unwrap().unwrap(); + let receipts = state.store.receipts(canonical).unwrap().unwrap(); + // The block `method` names the transaction in. + async fn block_of(state: &RpcState, method: &str, hash: &str) -> Value { + let resp = dispatch_for_test(state, make_request(method, json!([hash]))).await; + resp.result.unwrap()["blockHash"].clone() + } + + // A rival block at #42 holds it too, and the index writes it last. + let rival = Header { + parent_hash, + minimum_gas_price: U256::ZERO, + extra_data: Bytes::from_static(b"rival"), + ..test_header(42) + }; + let rival_hash = rival.hash(); + state.store.put_header(&rival).unwrap(); + state + .store + .put_body(rival_hash, &transactions, &[]) + .unwrap(); + state.store.put_receipts(rival_hash, &receipts).unwrap(); + state + .store + .index_block_transactions(rival_hash, &transactions) + .unwrap(); + + for (method, params) in &methods { + let resp = dispatch_for_test(&state, make_request(method, params.clone())).await; + assert!( + resp.result.as_ref().is_some_and(|result| !result.is_null()), + "{method} on the chain: {:?}", + resp.error + ); + } + for method in ["eth_getTransactionByHash", "eth_getTransactionReceipt"] { + let block = block_of(&state, method, &hash).await; + assert_eq!(block, json!(format!("{canonical:#x}")), "{method}"); + } + + // The rival takes #42, and is where it's found. + state.store.put_canonical_hash(42, rival_hash).unwrap(); + for method in ["eth_getTransactionByHash", "eth_getTransactionReceipt"] { + let block = block_of(&state, method, &hash).await; + assert_eq!(block, json!(format!("{rival_hash:#x}")), "{method}"); + } + + // A third block takes #42, and no block holding it is canonical. + state + .store + .put_canonical_hash(42, B256::repeat_byte(0x42)) + .unwrap(); + for (method, params) in &methods { + let resp = dispatch_for_test(&state, make_request(method, params.clone())).await; + assert_eq!( + resp.result, + Some(Value::Null), + "{method} off the chain: {:?}", + resp.error + ); + } + + // Pending in the pool, it is found there, with no block; back on the + // chain, it is the mined one that's found. + state.tx_pool = Some(Arc::new(OnePendingTx { + tx: transactions[0].clone(), + hash: tx_hash, + sender, + })); + let resp = dispatch_for_test( + &state, + make_request("eth_getTransactionByHash", json!([hash])), + ) + .await; + let pending = resp.result.unwrap(); + assert_eq!(pending["hash"], json!(hash)); + assert_eq!(pending["blockHash"], Value::Null); + state.store.put_canonical_hash(42, canonical).unwrap(); + assert_eq!( + block_of(&state, "eth_getTransactionByHash", &hash).await, + json!(format!("{canonical:#x}")) + ); +} + /// `eth_getBlockBy*` with full transactions must name the real sender. /// /// It used to hardcode `from` to the zero address, so every transaction in diff --git a/crates/rpc/src/trace.rs b/crates/rpc/src/trace.rs index 864f528..6be6dbf 100644 --- a/crates/rpc/src/trace.rs +++ b/crates/rpc/src/trace.rs @@ -48,7 +48,14 @@ pub fn trace_transaction(id: Value, params: &Value, state: &RpcState) -> JsonRpc else { return JsonRpcResponse::error(id, INVALID_PARAMS, "Missing or invalid transaction hash"); }; - let Some((block_hash, index)) = state.store.tx_location(tx_hash).ok().flatten() else { + // On the main chain only, as rskj's `traceTransaction` looks it up + // (`receiptStore.getInMainChain`): a block that lost a reorg is no answer. + let Some((block_hash, index)) = state + .store + .tx_location_in_main_chain(tx_hash) + .ok() + .flatten() + else { return JsonRpcResponse::success(id, Value::Null); }; @@ -112,7 +119,14 @@ pub fn trace_get(id: Value, params: &Value, state: &RpcState) -> JsonRpcResponse return JsonRpcResponse::error(id, INVALID_PARAMS, "Invalid position"); }; - let Some((block_hash, _)) = state.store.tx_location(tx_hash).ok().flatten() else { + // On the main chain only, as rskj's `traceGet` looks it up + // (`receiptStore.getInMainChain`). + let Some((block_hash, _)) = state + .store + .tx_location_in_main_chain(tx_hash) + .ok() + .flatten() + else { return JsonRpcResponse::success(id, Value::Null); }; match block_traces(state, block_hash, None) { diff --git a/crates/rpc/src/tx.rs b/crates/rpc/src/tx.rs index 89d8a2c..c0dc5c1 100644 --- a/crates/rpc/src/tx.rs +++ b/crates/rpc/src/tx.rs @@ -5,20 +5,29 @@ use crate::types::*; use alloy_primitives::{Address, B256}; use serde_json::Value; +/// `eth_getTransactionByHash`, as rskj's `Web3Impl` answers it: the +/// transaction as the main chain holds it (`receiptStore.getInMainChain`), +/// else as the pool holds it pending, with no block, else null. A block that +/// lost a reorg is no answer, though its body is still stored (upstream #343). pub fn eth_get_transaction_by_hash(id: Value, params: &Value, state: &RpcState) -> JsonRpcResponse { let Some(tx_hash) = params.get(0).and_then(|v| v.as_str()).and_then(parse_b256) else { return JsonRpcResponse::error(id, INVALID_PARAMS, "Missing or invalid tx hash"); }; - // Check pool first for pending transactions - if let Some(pool) = &state.tx_pool { - if let Some((tx, sender, hash)) = pool.get_pending_tx(&tx_hash) { + let Some((block_hash, tx_index)) = state + .store + .tx_location_in_main_chain(tx_hash) + .ok() + .flatten() + else { + if let Some((tx, sender, hash)) = state + .tx_pool + .as_ref() + .and_then(|pool| pool.get_pending_tx(&tx_hash)) + { let dto = TransactionDto::from_tx(&tx, hash, None, None, None, sender); return JsonRpcResponse::success(id, serde_json::to_value(dto).unwrap()); } - } - - let Some((block_hash, tx_index)) = state.store.tx_location(tx_hash).ok().flatten() else { return JsonRpcResponse::success(id, Value::Null); }; @@ -68,12 +77,20 @@ pub fn eth_get_transaction_by_block_number_and_index(id: Value, params: &Value, tx_by_block_and_index(id, block_hash, index, state) } +/// `eth_getTransactionReceipt`: the receipt the main chain holds, rskj's +/// `receiptStore.getInMainChain`, else null. A block that lost a reorg is no +/// answer, though its receipts are still stored (upstream #343). pub fn eth_get_transaction_receipt(id: Value, params: &Value, state: &RpcState) -> JsonRpcResponse { let Some(tx_hash) = params.get(0).and_then(|v| v.as_str()).and_then(parse_b256) else { return JsonRpcResponse::error(id, INVALID_PARAMS, "Missing or invalid tx hash"); }; - let Some((block_hash, tx_index)) = state.store.tx_location(tx_hash).ok().flatten() else { + let Some((block_hash, tx_index)) = state + .store + .tx_location_in_main_chain(tx_hash) + .ok() + .flatten() + else { return JsonRpcResponse::success(id, Value::Null); }; diff --git a/crates/storage/src/lib.rs b/crates/storage/src/lib.rs index 8060ab7..5589237 100644 --- a/crates/storage/src/lib.rs +++ b/crates/storage/src/lib.rs @@ -1485,32 +1485,81 @@ impl BlockStore { } impl BlockStore { - /// Stores a transaction index entry: tx_hash -> (block_hash, tx_index). + /// The bytes one block takes in a transaction's index entry: the block's + /// hash, then the transaction's position in it, big-endian. + const TX_INDEX_ENTRY: usize = 36; + + /// Records that a block holds a transaction, at `tx_index`. + /// + /// A transaction keeps one entry per block it was committed in, oldest + /// first, as rskj's `ReceiptStoreImplV2.add` appends each block's hash to + /// the transaction's list. A reorg can put the same transaction in a block + /// of each branch, and which of them is canonical can change after both + /// were written; overwriting kept only the newest, which after a reorg + /// back is the one that lost. A block already listed isn't listed again. pub fn put_tx_index(&self, tx_hash: B256, block_hash: B256, tx_index: u32) -> Result<()> { - let mut val = [0u8; 36]; - val[..32].copy_from_slice(block_hash.as_slice()); - val[32..].copy_from_slice(&tx_index.to_be_bytes()); + let mut entries = self.tx_index_bytes(tx_hash)?.unwrap_or_default(); + if entries + .chunks_exact(Self::TX_INDEX_ENTRY) + .any(|entry| &entry[..32] == block_hash.as_slice()) + { + return Ok(()); + } + entries.extend_from_slice(block_hash.as_slice()); + entries.extend_from_slice(&tx_index.to_be_bytes()); self.db - .put_cf(self.cf(CF_TX_INDEX)?, tx_hash.as_slice(), val) + .put_cf(self.cf(CF_TX_INDEX)?, tx_hash.as_slice(), entries) .context("Failed to write tx index") } - /// Looks up which block contains a transaction and at what index. - pub fn tx_location(&self, tx_hash: B256) -> Result> { + /// A transaction's index entry as stored, refused if it isn't whole. + fn tx_index_bytes(&self, tx_hash: B256) -> Result>> { let bytes = self.db .get_cf(self.cf(CF_TX_INDEX)?, tx_hash.as_slice()) .context("Failed to read tx index")?; match bytes { - None => Ok(None), - Some(b) => { - if b.len() < 36 { - return Err(anyhow!("corrupt tx index entry: {} bytes", b.len())); - } - let block_hash = B256::from_slice(&b[..32]); - let tx_index = u32::from_be_bytes([b[32], b[33], b[34], b[35]]); - Ok(Some((block_hash, tx_index))) + Some(b) if b.is_empty() || b.len() % Self::TX_INDEX_ENTRY != 0 => { + Err(anyhow!("corrupt tx index entry: {} bytes", b.len())) + } + bytes => Ok(bytes), + } + } + + /// Every block a transaction was committed in, oldest first, with its + /// index in each. + pub fn tx_locations(&self, tx_hash: B256) -> Result> { + let entries = self.tx_index_bytes(tx_hash)?.unwrap_or_default(); + Ok(entries + .chunks_exact(Self::TX_INDEX_ENTRY) + .map(|entry| { + let block_hash = B256::from_slice(&entry[..32]); + let tx_index = u32::from_be_bytes([entry[32], entry[33], entry[34], entry[35]]); + (block_hash, tx_index) + }) + .collect()) + } + + /// The block a transaction was last committed in, and its index there. + /// Not necessarily canonical: see [`Self::tx_location_in_main_chain`]. + pub fn tx_location(&self, tx_hash: B256) -> Result> { + Ok(self.tx_locations(tx_hash)?.pop()) + } + + /// The canonical block holding a transaction, and its index there: rskj's + /// `ReceiptStore.getInMainChain`, which looks through every block the + /// transaction was committed in for the one the chain has at that block's + /// height. `None` when none of them is, as for a transaction whose only + /// block lost a reorg (upstream #343). + pub fn tx_location_in_main_chain(&self, tx_hash: B256) -> Result> { + for (block_hash, tx_index) in self.tx_locations(tx_hash)? { + let Some(header) = self.header(block_hash)? else { + continue; + }; + if self.canonical_hash(header.number)? == Some(block_hash) { + return Ok(Some((block_hash, tx_index))); } } + Ok(None) } /// Indexes all transactions in a block body for fast lookup by tx hash. @@ -3365,6 +3414,92 @@ mod tests { } } + /// A transaction a reorg put in a block of each branch is found in the + /// canonical one, whichever was written last, as rskj's + /// `ReceiptStoreImplV2.getInMainChain` finds it, and in none while no block + /// holding it is canonical (upstream #343). The index keeps both blocks, + /// each once. + #[test] + fn a_transaction_is_found_in_the_canonical_block_that_holds_it() { + let dir = tempdir().unwrap(); + let store = BlockStore::open(dir.path()).unwrap(); + let tx = dummy_tx(0); + let hash = tx.tx_hash(); + // Two blocks at #5, one on each branch; the second holds it second. + let first = dummy_header(5); + let second = Header { + timestamp: 1, + ..dummy_header(5) + }; + store.put_header(&first).unwrap(); + store.put_header(&second).unwrap(); + store + .index_block_transactions(first.hash(), std::slice::from_ref(&tx)) + .unwrap(); + store + .index_block_transactions(second.hash(), &[dummy_tx(1), tx.clone()]) + .unwrap(); + store + .index_block_transactions(first.hash(), std::slice::from_ref(&tx)) + .unwrap(); + assert_eq!( + store.tx_locations(hash).unwrap(), + [(first.hash(), 0), (second.hash(), 1)] + ); + assert_eq!(store.tx_location(hash).unwrap(), Some((second.hash(), 1))); + + assert_eq!(store.tx_location_in_main_chain(hash).unwrap(), None); + store.put_canonical_hash(5, first.hash()).unwrap(); + assert_eq!( + store.tx_location_in_main_chain(hash).unwrap(), + Some((first.hash(), 0)) + ); + store.put_canonical_hash(5, second.hash()).unwrap(); + assert_eq!( + store.tx_location_in_main_chain(hash).unwrap(), + Some((second.hash(), 1)) + ); + store + .put_canonical_hash(5, B256::repeat_byte(0x99)) + .unwrap(); + assert_eq!(store.tx_location_in_main_chain(hash).unwrap(), None); + + // A block the store holds no header of is passed over, as rskj's + // `getInMainChain` passes over a block it can't read. + let other = dummy_tx(2); + store + .index_block_transactions(B256::repeat_byte(0x77), std::slice::from_ref(&other)) + .unwrap(); + store + .index_block_transactions(first.hash(), std::slice::from_ref(&other)) + .unwrap(); + store.put_canonical_hash(5, first.hash()).unwrap(); + assert_eq!( + store.tx_location_in_main_chain(other.tx_hash()).unwrap(), + Some((first.hash(), 0)) + ); + + // An entry that isn't whole is refused. + let cf = store.cf(CF_TX_INDEX).unwrap(); + store + .db + .put_cf(cf, B256::repeat_byte(0x55).as_slice(), [0u8; 35]) + .unwrap(); + assert!(store.tx_locations(B256::repeat_byte(0x55)).is_err()); + store + .db + .put_cf(cf, B256::repeat_byte(0x55).as_slice(), [0u8; 73]) + .unwrap(); + assert!(store.tx_location(B256::repeat_byte(0x55)).is_err()); + store + .db + .put_cf(cf, B256::repeat_byte(0x55).as_slice(), [0u8; 0]) + .unwrap(); + assert!(store + .tx_location_in_main_chain(B256::repeat_byte(0x55)) + .is_err()); + } + #[test] fn test_tx_index_overwrite() { let dir = tempdir().unwrap(); From 718402b09558530adf0e00289e24090bc32f4ae4 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Tue, 10 Nov 2026 07:59:28 -0800 Subject: [PATCH 083/264] execution: a local call passes the gates a block's transaction is held to rskj runs eth_call and eth_estimateGas with localCall set: init() returns once it has the intrinsic cost, before the block gas limit, nonce, balance and initcode checks; nothing is charged, the nonce stays and no fee is paid. A local call whose gas can't pay its intrinsic cost fails with all its gas where a program runs and throws where none does, and from RSKIP136 a call to a precompile it can't pay throws. A block's transactions run as before (upstream #294). --- crates/execution/src/executor.rs | 346 +++++++++++++++++++++++++++- crates/execution/src/rsk_handler.rs | 148 ++++++++++++ crates/rpc/src/tests.rs | 36 +++ 3 files changed, 520 insertions(+), 10 deletions(-) diff --git a/crates/execution/src/executor.rs b/crates/execution/src/executor.rs index 6905244..2ce3f66 100644 --- a/crates/execution/src/executor.rs +++ b/crates/execution/src/executor.rs @@ -344,13 +344,16 @@ impl RskExecutor { /// `eth_estimateGas` and marks the transaction with /// `TransactionExecutor.setLocalCall(true)`. /// - /// The only behavioural difference is that Bridge methods marked - /// `LocalOnly` run instead of throwing (`Bridge.validateLocalCall`, - /// Bridge.java:431). Those are the read-only getters -- the federation - /// address, the BTC blockchain head, the peg-out queue sizes -- which rskj - /// deliberately refuses to answer on-chain so that no contract can depend - /// on them. Nothing here is reachable from block execution, so it cannot - /// affect consensus. + /// It differs from a transaction of a block in two ways, neither of them + /// reachable from block execution: + /// - it passes every gate `TransactionExecutor.init()` holds a transaction + /// to, is charged no gas and keeps the sender's nonce (see + /// `RskHandler`'s `local_call`); + /// - Bridge methods marked `LocalOnly` run instead of throwing + /// (`Bridge.validateLocalCall`, Bridge.java:431). Those are the read-only + /// getters -- the federation address, the BTC blockchain head, the + /// peg-out queue sizes -- which rskj deliberately refuses to answer + /// on-chain so that no contract can depend on them. pub fn execute_local_call( &self, header: &Header, @@ -451,7 +454,8 @@ impl RskExecutor { active_precompiles, self.hardfork_cfg.has_rskip171(header.number), ) - .failing_programs(!header_fits_program_invoke(header)); + .failing_programs(!header_fits_program_invoke(header)) + .as_local_call(local_call, self.hardfork_cfg.has_rskip136(header.number)); let out = handler .run(&mut evm) .map_err(|e: revm::context::result::EVMError<_>| { @@ -588,7 +592,8 @@ impl RskExecutor { active_precompiles, self.hardfork_cfg.has_rskip171(header.number), ) - .failing_programs(!header_fits_program_invoke(header)); + .failing_programs(!header_fits_program_invoke(header)) + .as_local_call(local_call, self.hardfork_cfg.has_rskip136(header.number)); use revm::inspector::InspectorHandler; let out = handler .inspect_run(&mut evm) @@ -760,7 +765,8 @@ impl RskExecutor { active_precompiles, self.hardfork_cfg.has_rskip171(header.number), ) - .failing_programs(!header_fits_program_invoke(header)); + .failing_programs(!header_fits_program_invoke(header)) + .as_local_call(local_call, self.hardfork_cfg.has_rskip136(header.number)); use revm::inspector::InspectorHandler; let out = handler .inspect_run(&mut evm) @@ -3404,6 +3410,326 @@ mod tests { assert_estimate_run_agrees(&executor, &header, &tx, sender, &root, &store); } + /// An executor over an empty block store, at mainnet's heights. + fn mainnet_executor() -> RskExecutor { + let block_store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + RskExecutor::new(RskHardforkConfig::mainnet(), block_store) + } + + /// A local call passes every gate rskj's `TransactionExecutor.init()` + /// holds a transaction of a block to (upstream #294): with `localCall` set + /// it returns as soon as it has the intrinsic cost + /// (TransactionExecutor.java:165-167). Each case breaks one gate, and the + /// local call runs all the same. It's charged no gas either (`execute()`, + /// 292-301): the contract it calls reads the caller's balance less only + /// the value, where the chain's transaction has paid for its gas. Nor is + /// any fee paid after it (`localCallFinalization`, 631-673), which at a + /// price no balance could pay would overflow. A trace and an estimate of + /// a local call run the same way. + #[test] + fn a_local_call_passes_the_gates_a_transaction_of_a_block_is_held_to() { + // BALANCE(CALLER), then CALLVALUE: two words. + let reader = [ + 0x33, 0x31, 0x60, 0x00, 0x52, 0x34, 0x60, 0x20, 0x52, 0x60, 0x40, 0x60, 0x00, 0xf3, + ]; + let contract = Address::repeat_byte(0xC0); + let sender = Address::repeat_byte(0x5E); + // A sender with code of its own, which rskj never refuses (revm's + // EIP-3607 does, on chain). + let coded = Address::repeat_byte(0xC5); + let balance = U256::from(10u64).pow(U256::from(18)); + let store = Arc::new(MemoryTrieStore::new()); + let root = TrieNode::empty(); + let root = put_account_with_code(&root, store.as_ref(), &contract, 0, U256::ZERO, &reader); + let root = put_account(&root, store.as_ref(), &sender, 5, balance); + let root = put_account_with_code(&root, store.as_ref(), &coded, 5, balance, &[0x00]); + let executor = mainnet_executor(); + let header = Header { + minimum_gas_price: U256::from(10), + ..dummy_header(8_000_000) + }; + let call = + |nonce: u64, gas_price: u128, gas_limit: u64, value: U256| rustock_core::Transaction { + nonce, + gas_price: U256::from(gas_price), + gas_limit: U256::from(gas_limit), + to: Bytes::copy_from_slice(contract.as_slice()), + value, + input: Bytes::new(), + v: 0, + r: U256::ZERO, + s: U256::ZERO, + cached_rlp: None, + }; + let words = |result: &TxExecutionResult| { + assert_eq!(result.output.len(), 64); + ( + U256::from_be_slice(&result.output[..32]), + U256::from_be_slice(&result.output[32..]), + ) + }; + let one = U256::from(1); + + // The chain takes this one, and charges its gas up front. + let fits = call(5, 10, 100_000, one); + let on_chain = executor + .execute_tx(&header, &fits, sender, &root, store.clone()) + .unwrap(); + assert_eq!( + words(&on_chain), + (balance - one - U256::from(10 * 100_000), one) + ); + let local = executor + .execute_local_call(&header, &fits, sender, &root, store.clone()) + .unwrap(); + assert_eq!(words(&local), (balance - one, one)); + + // What the caller is left with, where rskj and this node agree on it: + // a sender short of the value goes below zero in rskj, which a balance + // here can't. + let cases = [ + ( + "a nonce ahead of the sender's", + sender, + call(6, 10, 100_000, one), + Some(balance - one), + one, + ), + ( + "a nonce behind the sender's", + sender, + call(4, 10, 100_000, one), + Some(balance - one), + one, + ), + ( + "a gas price under the block's minimum", + sender, + call(5, 9, 100_000, one), + Some(balance - one), + one, + ), + ( + "a gas price no balance could pay", + sender, + call(5, u128::MAX, 100_000, one), + Some(balance - one), + one, + ), + ( + "a gas limit over the block's", + sender, + call(5, 10, 6_800_001, one), + Some(balance - one), + one, + ), + ( + "the whole balance and the gas", + sender, + call(5, 10, 100_000, balance), + Some(U256::ZERO), + balance, + ), + ( + "more than the balance", + sender, + call(5, 10, 100_000, balance + one), + None, + balance + one, + ), + ( + "a sender with code", + coded, + call(5, 10, 100_000, one), + Some(balance - one), + one, + ), + ]; + for (case, from, tx, left, value) in cases { + if from == sender { + assert!( + executor + .execute_tx(&header, &tx, from, &root, store.clone()) + .is_err(), + "the chain takes {case}" + ); + } + let local = executor + .execute_local_call(&header, &tx, from, &root, store.clone()) + .unwrap_or_else(|e| panic!("a local call with {case}: {e:?}")); + assert!(local.success, "{case}"); + let (caller, sent) = words(&local); + assert_eq!(sent, value, "{case}"); + if let Some(left) = left { + assert_eq!(caller, left, "{case}"); + } + let (traced, _) = executor + .execute_tx_traced(&header, &tx, from, &root, store.clone(), true, 50) + .unwrap_or_else(|e| panic!("a traced local call with {case}: {e:?}")); + let (estimated, _) = executor + .execute_tx_for_estimate(&header, &tx, from, &root, store.clone(), true) + .unwrap_or_else(|e| panic!("an estimate with {case}: {e:?}")); + assert_eq!(traced.output, local.output, "{case}"); + assert_eq!(estimated.output, local.output, "{case}"); + } + } + + /// A local creation isn't held to RSKIP438's limit on initcode, which + /// `init()` checks after a local call has returned + /// (TransactionExecutor.java:169). + #[test] + fn a_local_creation_passes_the_limit_on_initcode() { + let sender = Address::repeat_byte(0x5E); + let store = Arc::new(MemoryTrieStore::new()); + let root = put_account( + &TrieNode::empty(), + store.as_ref(), + &sender, + 0, + U256::from(10u64).pow(U256::from(18)), + ); + let executor = mainnet_executor(); + let header = dummy_header(8_000_000); + // A STOP, and past 49,152 bytes. + let tx = unsigned_tx(0, None, 0, 1_000_000, vec![0; 49_153]); + assert!(executor + .execute_tx(&header, &tx, sender, &root, store.clone()) + .is_err()); + let local = executor + .execute_local_call(&header, &tx, sender, &root, store) + .unwrap(); + assert!(local.success); + } + + /// A local call leaves its sender's nonce as it found it (`execute()`, + /// TransactionExecutor.java:292-301), where a transaction of a block bumps + /// it before its call runs. Seen from a sender with code that creates a + /// contract: the address comes from the nonce the sender holds. + #[test] + fn a_local_call_leaves_its_senders_nonce() { + // CREATE(0, 0, 0), and return the address it made. + let creator = [ + 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0xf0, 0x60, 0x00, 0x52, 0x60, 0x20, 0x60, 0x00, + 0xf3, + ]; + let sender = Address::repeat_byte(0xC5); + let store = Arc::new(MemoryTrieStore::new()); + let root = put_account_with_code( + &TrieNode::empty(), + store.as_ref(), + &sender, + 7, + U256::ZERO, + &creator, + ); + let tx = unsigned_tx(7, Some(sender), 0, 1_000_000, Vec::new()); + let local = mainnet_executor() + .execute_local_call(&dummy_header(8_000_000), &tx, sender, &root, store) + .unwrap(); + assert!(local.success); + assert_eq!(Address::from_slice(&local.output[12..]), sender.create(7)); + } + + /// A local call's sender has an account by the time its program runs: + /// rskj's `Repository.transfer` of the value makes one for it, even of + /// nothing (`call()`, TransactionExecutor.java:397). So EXTCODEHASH reads + /// it as the empty code's hash, not the zero a missing account reads as. + #[test] + fn a_local_calls_sender_has_an_account() { + // EXTCODEHASH(CALLER) + let hasher = [0x33, 0x3f, 0x60, 0x00, 0x52, 0x60, 0x20, 0x60, 0x00, 0xf3]; + let contract = Address::repeat_byte(0xC0); + let store = Arc::new(MemoryTrieStore::new()); + let root = put_account_with_code( + &TrieNode::empty(), + store.as_ref(), + &contract, + 0, + U256::ZERO, + &hasher, + ); + let tx = unsigned_tx(0, Some(contract), 0, 100_000, Vec::new()); + let local = mainnet_executor() + .execute_local_call( + &dummy_header(8_000_000), + &tx, + Address::repeat_byte(0x99), + &root, + store, + ) + .unwrap(); + assert_eq!( + B256::from_slice(&local.output), + revm::primitives::KECCAK_EMPTY + ); + } + + /// A local call whose gas limit can't pay its intrinsic cost: where a + /// program runs, rskj's `go()` fails it with all its gas as it charges + /// the cost, before it runs (TransactionExecutor.java:455), so not even a + /// STOP succeeds; where none does, `GasCost.subtract` throws out of + /// `executeTransaction`, for an account without code (384), a precompile + /// (360) and a creation without data (406). The chain refuses all of + /// them. And from RSKIP136 a local call's own call to a precompile whose + /// cost its gas can't pay throws too (350, 360). + #[test] + fn a_local_call_short_of_its_costs_fails_or_throws_as_rskj_does() { + let contract = Address::repeat_byte(0xC0); + let sender = Address::repeat_byte(0x5E); + let store = Arc::new(MemoryTrieStore::new()); + let root = TrieNode::empty(); + let root = put_account_with_code(&root, store.as_ref(), &contract, 0, U256::ZERO, &[0x00]); + let root = put_account( + &root, + store.as_ref(), + &sender, + 0, + U256::from(10u64).pow(U256::from(18)), + ); + let executor = mainnet_executor(); + let header = dummy_header(8_000_000); + let identity = Address::with_last_byte(4); + let run = |to: Option
, gas: u64, input: Vec| { + let tx = unsigned_tx(0, to, 0, gas, input); + ( + executor.execute_tx(&header, &tx, sender, &root, store.clone()), + executor.execute_local_call(&header, &tx, sender, &root, store.clone()), + ) + }; + + for (case, to, input) in [ + ("a call to code", Some(contract), vec![]), + ("a creation", None, vec![0x00]), + ] { + let (on_chain, local) = run(to, 20_000, input); + assert!(on_chain.is_err(), "the chain takes {case}"); + let local = local.unwrap_or_else(|e| panic!("{case}: {e:?}")); + assert!(!local.success, "{case}"); + assert_eq!(local.gas_used, 20_000, "{case}"); + assert!(local.output.is_empty(), "{case}"); + } + for (case, to, input) in [ + ( + "a call to no code", + Some(Address::repeat_byte(0x77)), + vec![], + ), + ("a call to a precompile", Some(identity), vec![1; 32]), + ("a creation without data", None, vec![]), + ] { + let (on_chain, local) = run(to, 20_000, input); + assert!(on_chain.is_err(), "the chain takes {case}"); + assert!(local.is_err(), "a local call answers {case}"); + } + + // The intrinsic cost, 21,000 and 16 a byte, and 10 of the 18 the + // identity precompile costs a word. + let short = 21_000 + 32 * 16 + 10; + assert!(run(Some(identity), short, vec![1; 32]).1.is_err()); + let (_, local) = run(Some(identity), short + 8, vec![1; 32]); + assert_eq!(local.unwrap().output, vec![1; 32]); + } + #[test] fn test_rskj_call_stipend_is_charged_to_caller() { let store = Arc::new(MemoryTrieStore::new()); diff --git a/crates/execution/src/rsk_handler.rs b/crates/execution/src/rsk_handler.rs index dca90a9..fc4a3fc 100644 --- a/crates/execution/src/rsk_handler.rs +++ b/crates/execution/src/rsk_handler.rs @@ -133,6 +133,25 @@ invisible_exception: Arc, /// create that reverts or fails, with what the calls under it committed /// into it. frames: Vec<(usize, usize)>, + /// A local call: rskj's `TransactionExecutor` with `setLocalCall(true)`, + /// which serves `eth_call` and `eth_estimateGas` + /// (`ReversibleTransactionExecutor.reversibleExecution`). `init()` returns + /// as soon as it has worked out the intrinsic cost, before every gate it + /// holds a transaction of a block to: the block's gas limit, the + /// intrinsic cost, the initcode's size, the nonce and the balance + /// (TransactionExecutor.java:162-217). `execute()` neither bumps the + /// nonce nor charges the gas (292-301), and `localCallFinalization` + /// refunds nothing and pays nobody (631-673). + local_call: bool, + /// RSKIP136, for a local call's own call to a precompile: from it, a gas + /// limit short of the precompile's cost and the intrinsic cost throws out + /// of rskj's `TransactionExecutor.call` (`GasCost.subtract`, 360). + rskip136_active: bool, + /// Set by `validate` for a local call whose gas limit can't pay its + /// intrinsic cost but that runs a program, which rskj's `go()` fails with + /// all its gas as it charges that cost (455): the loop halts it as it + /// opens. + starved: std::cell::Cell, _phantom: core::marker::PhantomData<(CTX, ERROR, FRAME)>, } @@ -173,6 +192,14 @@ impl RskHandler { self } + /// Run the transaction as a local call, as rskj runs `eth_call` and + /// `eth_estimateGas`. See the field. + pub fn as_local_call(mut self, local_call: bool, rskip136_active: bool) -> Self { + self.local_call = local_call; + self.rskip136_active = rskip136_active; + self + } + #[allow(clippy::too_many_arguments)] pub fn with_rskip125( empty_create_on_unpayable_deposit: bool, @@ -196,6 +223,9 @@ impl RskHandler { fail_programs: false, called_precompiles: None, frames: Vec::new(), + local_call: false, + rskip136_active: true, + starved: std::cell::Cell::new(false), _phantom: core::marker::PhantomData, } } @@ -233,6 +263,91 @@ where type Error = ERROR; type HaltReason = HaltReason; + /// A transaction of a block is held to revm's gates. A local call is held + /// to none (see `local_call`), and its intrinsic cost is worked out as for + /// any transaction. Where its gas limit can't pay that cost, rskj throws + /// out of `executeTransaction` if no program runs: `GasCost.subtract` in + /// `call()` for a precompile or an account without code + /// (TransactionExecutor.java:360, 384) and in `create()` for a creation + /// without data (406). If a program runs, `go()` fails it with all its gas + /// as it charges the cost (455), before it runs anything (`starved`). A + /// precompile's account holds no code. + fn validate( + &self, + evm: &mut Self::Evm, + ) -> Result { + if !self.local_call { + self.validate_env(evm)?; + return self.validate_initial_tx_gas(evm); + } + use revm::context_interface::Transaction; + use revm::primitives::TxKind; + let ctx = evm.ctx(); + let mut gas = revm::context_interface::cfg::gas::calculate_initial_tx_gas_for_tx( + ctx.tx(), + ctx.cfg().spec().into(), + ); + let gas_limit = ctx.tx().gas_limit(); + let starved = gas.initial_gas > gas_limit; + if starved { + let runs_program = match ctx.tx().kind() { + TxKind::Create => !ctx.tx().input().is_empty(), + TxKind::Call(to) => ctx + .journal_mut() + .load_account_with_code(to) + .map_err(|e| ERROR::from(e))? + .info + .code + .as_ref() + .is_some_and(|code| !code.is_empty()), + }; + if !runs_program { + return Err( + revm::context_interface::result::InvalidTransaction::CallGasCostMoreThanGasLimit { + gas_limit, + initial_gas: gas.initial_gas, + } + .into(), + ); + } + gas.initial_gas = gas_limit; + } + self.starved.set(starved); + Ok(gas) + } + + /// A local call's sender (see `local_call`): its nonce and balance go + /// unchecked, as does code at its address, which rskj never refuses + /// (revm's EIP-3607). It's charged no gas and keeps its nonce + /// (TransactionExecutor.java:292-301). rskj moves the value with + /// `Repository.transfer` (397, 427), which creates the sender's account + /// and takes its balance below zero rather than fail. One here can't go + /// below zero, so a sender short of the value is lifted to it, as revm + /// lifts one for a simulation (`disable_balance_check`), and holds nothing + /// where rskj's holds less than nothing. + fn validate_against_state_and_deduct_caller( + &self, + evm: &mut Self::Evm, + ) -> Result<(), Self::Error> { + if !self.local_call { + return revm::handler::pre_execution::validate_against_state_and_deduct_caller( + evm.ctx(), + ); + } + use revm::context_interface::Transaction; + let ctx = evm.ctx(); + let (caller, value) = (ctx.tx().caller(), ctx.tx().value()); + let mut sender = ctx + .journal_mut() + .load_account_mut(caller) + .map_err(|e| ERROR::from(e))?; + sender.touch(); + if *sender.balance() < value { + sender.set_balance(value); + } + Ok(()) + } + fn load_accounts(&self, evm: &mut Self::Evm) -> Result<(), Self::Error> { // Clear any stale flag a previous tx left set. self.invisible_exception.store(false, Ordering::Relaxed); @@ -252,6 +367,11 @@ where evm: &mut Self::Evm, exec_result: &mut FrameResult, ) -> Result<(), Self::Error> { + // A local call was charged nothing, and `localCallFinalization` hands + // nothing back (TransactionExecutor.java:631-673). + if self.local_call { + return Ok(()); + } if self.invisible_exception.load(Ordering::Relaxed) { return Ok(()); } @@ -273,6 +393,10 @@ where ) -> Result<(), Self::Error> { use revm::context_interface::journaled_state::account::JournaledAccountTr; use revm::context_interface::{Block, Transaction}; + // A local call pays nobody (`localCallFinalization`). + if self.local_call { + return Ok(()); + } if self.postpone_fees { // The executor accumulates the same total and settles it at the // REMASC transaction, as rskj does where a block runs as its sublists. @@ -347,6 +471,21 @@ where frame_result: &mut FrameResult, ) -> Result<(), Self::Error> { use revm::context_interface::Transaction; + // A local call's own call to a precompile whose cost its gas can't + // pay: rskj leaves out the check for a local call + // (TransactionExecutor.java:350), and from RSKIP136 works out the + // leftover with `GasCost.subtract` (360), which throws out of + // `executeTransaction`. Before RSKIP136 rskj runs the precompile all + // the same, which isn't done here. + if self.local_call + && self.rskip136_active + && frame_result.interpreter_result().result == InstructionResult::PrecompileOOG + { + return Err(ERROR::from_string(format!( + "a local call's gas limit of {} can't pay the precompile it calls", + evm.ctx().tx().gas_limit() + ))); + } if self.invisible_exception.load(Ordering::Relaxed) { frame_result.interpreter_result_mut().result = InstructionResult::Return; } @@ -435,6 +574,15 @@ where // bumps the nonce (even post-RSKIP125), so it stays 0. set_created_contract_nonce(evm, 0); self.open_frame(evm); + // A local call whose gas limit can't pay its intrinsic cost (see + // `validate`): rskj's `go()` throws as it charges the cost, before the + // program runs, and takes back what opening it did. + if self.starved.get() { + evm.frame_stack() + .get() + .interpreter + .halt(InstructionResult::OutOfGas); + } loop { let call_or_result = D::run(evm, self.empty_create_on_unpayable_deposit)?; diff --git a/crates/rpc/src/tests.rs b/crates/rpc/src/tests.rs index 430d384..909eb01 100644 --- a/crates/rpc/src/tests.rs +++ b/crates/rpc/src/tests.rs @@ -3989,6 +3989,42 @@ async fn a_transaction_is_looked_up_on_the_main_chain_only() { ); } +/// `eth_call` and `eth_estimateGas` answer where a transaction of a block is +/// refused, as rskj's local call does (upstream #294): from a sender that +/// can't pay for its gas, and with more gas than the block holds. At #41, +/// which holds the fixture's state. +#[tokio::test] +async fn eth_call_answers_where_a_transaction_would_be_refused() { + let (state, _tmp, _, _, _, callee) = setup_state_for_tracing(); + let to = format!("{callee:#x}"); + let stranger = format!("{:#x}", Address::repeat_byte(0x99)); + for (case, call) in [ + ( + "a sender that can't pay for its gas", + json!({"from": stranger, "to": to, "gas": "0x186a0", "gasPrice": "0x1"}), + ), + ( + "more gas than the block holds", + json!({"to": to, "gas": "0x7a1201"}), + ), + ] { + let params = json!([call, "0x29"]); + let resp = dispatch_for_test(&state, make_request("eth_call", params.clone())).await; + assert_eq!( + resp.result, + Some(json!(format!("0x{:064x}", 0x2a))), + "{case}: {:?}", + resp.error + ); + let resp = dispatch_for_test(&state, make_request("eth_estimateGas", params)).await; + assert!( + resp.result.is_some_and(|gas| gas.is_string()), + "{case}: {:?}", + resp.error + ); + } +} + /// `eth_getBlockBy*` with full transactions must name the real sender. /// /// It used to hardcode `from` to the zero address, so every transaction in From cba45674ebe8f70cf17a5c803bd5f45d56bd6da5 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Tue, 10 Nov 2026 07:59:28 -0800 Subject: [PATCH 084/264] rpc: read block parameters as rskj reads them Tags are matched as written, as every rskj path matches them but a log filter's. eth_call and eth_estimateGas take a decimal height and refuse a number past a long; the state methods refuse what rskj's BlockRefParam refuses; a log filter's blocks are read as rskj's LogFilter reads them (upstream #178). --- crates/rpc/src/call.rs | 12 +- crates/rpc/src/helpers.rs | 305 +++++++++++++++++++++++++++++++++----- crates/rpc/src/logs.rs | 38 +++-- crates/rpc/src/rsk.rs | 3 + crates/rpc/src/state.rs | 12 ++ crates/rpc/src/tests.rs | 93 +++++++++++- 6 files changed, 412 insertions(+), 51 deletions(-) diff --git a/crates/rpc/src/call.rs b/crates/rpc/src/call.rs index c707e9b..e29d801 100644 --- a/crates/rpc/src/call.rs +++ b/crates/rpc/src/call.rs @@ -14,12 +14,16 @@ pub fn eth_call(id: Value, params: &Value, state: &RpcState) -> JsonRpcResponse return JsonRpcResponse::error(id, INVALID_PARAMS, "Missing or invalid call request"); }; let block_param = params.get(1).and_then(|v| v.as_str()).unwrap_or("latest"); + let block_param = match crate::helpers::execution_block(block_param, false) { + Ok(block) => block, + Err((code, message)) => return JsonRpcResponse::error(id, code, message), + }; let Some((trie_store, hardfork_cfg)) = state.trie_store.clone().zip(state.hardfork_cfg.clone()) else { return JsonRpcResponse::error(id, INTERNAL_ERROR, "Execution engine not available"); }; - let Some((root, header)) = resolve_state_root(block_param, state) else { + let Some((root, header)) = resolve_state_root(&block_param, state) else { return JsonRpcResponse::error(id, INTERNAL_ERROR, "Cannot resolve block"); }; @@ -44,12 +48,16 @@ pub fn eth_estimate_gas(id: Value, params: &Value, state: &RpcState) -> JsonRpcR return JsonRpcResponse::error(id, INVALID_PARAMS, "Missing or invalid call request"); }; let block_param = params.get(1).and_then(|v| v.as_str()).unwrap_or("latest"); + let block_param = match crate::helpers::execution_block(block_param, true) { + Ok(block) => block, + Err((code, message)) => return JsonRpcResponse::error(id, code, message), + }; let Some((trie_store, hardfork_cfg)) = state.trie_store.clone().zip(state.hardfork_cfg.clone()) else { return JsonRpcResponse::error(id, INTERNAL_ERROR, "Execution engine not available"); }; - let Some((root, header)) = resolve_state_root(block_param, state) else { + let Some((root, header)) = resolve_state_root(&block_param, state) else { return JsonRpcResponse::error(id, INTERNAL_ERROR, "Cannot resolve block"); }; diff --git a/crates/rpc/src/helpers.rs b/crates/rpc/src/helpers.rs index 2d2b8d6..b456e17 100644 --- a/crates/rpc/src/helpers.rs +++ b/crates/rpc/src/helpers.rs @@ -81,7 +81,8 @@ pub fn parse_storage_slot(s: &str) -> Option { Some(B256::from(out)) } -/// Resolves a block parameter: a tag, or a `0x`-prefixed hex height. +/// Resolves a block parameter: `latest`, `pending` or `earliest` as written, +/// or a `0x`-prefixed hex height. /// /// # The `0x` prefix is required, and that is the whole point /// @@ -92,39 +93,168 @@ pub fn parse_storage_slot(s: &str) -> Option { /// /// rskj rejects an unprefixed height for every method that reaches /// `Web3InformationRetriever` -- `HexUtils.stringHexToBigInteger` demands the -/// prefix and answers `-32602 invalid blocknumber` -- so rejecting matches it -/// for the large majority of the namespace, and errs in the safe direction -/// everywhere else: a client gets an error rather than a wrong block. +/// prefix and answers `-32602 invalid blocknumber`. `eth_call` and +/// `eth_estimateGas` read a decimal height too, through +/// `ExecutionBlockRetriever` ([`execution_block`]), and so does +/// `eth_getBlocksByNumber`, which parses its own parameter (see +/// `eth::eth_get_blocks_by_number`). /// -/// Two deliberate differences from rskj remain, both of them rskj accepting -/// what this rejects: +/// # Tags are matched as written /// -/// - `eth_call` and `eth_estimateGas` resolve through `ExecutionBlockRetriever` -/// there, which parses a decimal height too. Whether a method takes decimal -/// is therefore a property of its retrieval path rather than of the -/// parameter, which is an upstream accident this does not reproduce. -/// - `eth_getBlocksByNumber` takes a decimal height, and does so here as well -/// -- it parses its own parameter (see `eth::eth_get_blocks_by_number`). -/// -/// Tags are matched case-insensitively. rskj is inconsistent about this -- -/// `BlockTag.fromString` uses `equalsIgnoreCase` while `BlockRefParam` matches -/// exactly -- so `"LATEST"` works for half its methods. Accepting it -/// everywhere is the permissive half of that split and cannot break a caller -/// that works against either. +/// As every path of rskj's matches them but a log filter's: `getBlock` +/// switches on the string, `BlockRefParam` lists the three tags, +/// `ExecutionBlockRetriever` compares with `BlockTag.tagEquals` +/// (`contentEquals`) and `Web3Impl.getBlockByNumberOrStr` with `equals`. +/// `BlockIdentifierParam` lets `LATEST`, `finalized` and `safe` through +/// (`BlockTag.fromString` ignores case), but nothing it hands them to resolves +/// them but a log filter (upstream #178), which reads its blocks with +/// [`parse_filter_block`]. pub fn parse_block_number(s: &str, head_number: u64) -> Option { - let trimmed = s.trim(); - if trimmed.eq_ignore_ascii_case("latest") || trimmed.eq_ignore_ascii_case("pending") { - return Some(head_number); + match s { + "latest" | "pending" => Some(head_number), + "earliest" => Some(0), + // No prefix, no answer. Guessing the base is what produced #177. + _ if is_hex_number(s) => u64::from_str_radix(&s[2..], 16).ok(), + _ => None, + } +} + +/// rskj's decimal number in a block parameter, `^\d+$` +/// (`Utils.isDecimalString`). +fn is_decimal(s: &str) -> bool { + !s.is_empty() && s.bytes().all(|b| b.is_ascii_digit()) +} + +/// rskj's hex number in a block parameter, `^0x[\da-fA-F]+$` +/// (`Utils.isHexadecimalString`). +fn is_hex_number(s: &str) -> bool { + s.strip_prefix("0x") + .is_some_and(|hex| !hex.is_empty() && hex.bytes().all(|b| b.is_ascii_hexdigit())) +} + +/// rskj's block tags, in any case (`BlockTag.fromString`). +fn is_block_tag(s: &str) -> bool { + ["pending", "latest", "earliest", "finalized", "safe"] + .iter() + .any(|tag| tag.eq_ignore_ascii_case(s)) +} + +/// What rskj answers a block reference it refuses before it looks a block up, +/// for `eth_getBalance`, `eth_getCode`, `eth_getStorageAt`, +/// `eth_getTransactionCount` and `rsk_getStorageBytesAt`. `BlockRefParam` +/// passes `earliest`, `latest` and `pending` as written, a decimal and a `0x` +/// hex number, and refuses the rest -- `LATEST` among them -- with `-32602 +/// Invalid block identifier`. A decimal goes on to +/// `Web3InformationRetriever.getBlock`, which reads only hex: `-32602 invalid +/// blocknumber`. +pub fn refused_block_ref(id: &serde_json::Value, s: &str) -> Option { + let message = + if !(matches!(s, "earliest" | "latest" | "pending") || is_decimal(s) || is_hex_number(s)) { + format!("Invalid block identifier '{s}'") + } else if is_decimal(s) { + format!("invalid blocknumber {s}") + } else { + return None; + }; + Some(crate::types::JsonRpcResponse::error( + id.clone(), + crate::types::INVALID_PARAMS, + message, + )) +} + +/// The block `eth_call` (`estimate` false) or `eth_estimateGas` (true) runs +/// on, as rskj reads it: in a form `state::resolve_state_root` reads, or the +/// error code and message rskj answers. +/// +/// The parameter is checked first: `eth_call`'s is a `BlockRefParam` +/// (`earliest`, `latest` and `pending` as written), `eth_estimateGas`'s a +/// `BlockIdentifierParam` (rskj's tags in any case); either passes a decimal and +/// a `0x` hex number and refuses the rest, `-32602 Invalid block identifier`. +/// Then `ExecutionBlockRetriever.retrieveExecutionBlock` matches the tags as +/// written and reads a number in hex or decimal as a long, throwing past one +/// (`Utils.hexadecimalStringToLong`, `decimalStringToLong`), which nothing +/// catches: `-32603 Internal server error`. A tag in another case, or +/// `finalized` or `safe`, it refuses: `-32602 Unsupported block specifier`. +pub fn execution_block(s: &str, estimate: bool) -> Result { + use crate::types::{INTERNAL_ERROR, INVALID_PARAMS}; + let tag = if estimate { + is_block_tag(s) + } else { + matches!(s, "earliest" | "latest" | "pending") + }; + if estimate && s.is_empty() { + return Err(( + INVALID_PARAMS, + "Invalid block identifier: empty or null.".into(), + )); } - if trimmed.eq_ignore_ascii_case("earliest") { - return Some(0); + if !(tag || is_decimal(s) || is_hex_number(s)) { + return Err((INVALID_PARAMS, format!("Invalid block identifier '{s}'"))); } - // No prefix, no answer. Guessing the base is what produced #177. - let hex = trimmed.strip_prefix("0x").or_else(|| trimmed.strip_prefix("0X"))?; - if hex.is_empty() { - return None; + let number = if is_hex_number(s) { + i64::from_str_radix(&s[2..], 16) + } else if is_decimal(s) { + s.parse::() + } else if matches!(s, "earliest" | "latest" | "pending") { + return Ok(s.to_string()); + } else { + return Err(( + INVALID_PARAMS, + format!( + "Unsupported block specifier '{s}'. Can only be either 'earliest', 'latest', \ + 'pending' or a specific block number (either hex - prepending '0x' or decimal)." + ), + )); + }; + number + .map(|number| format!("{number:#x}")) + .map_err(|_| (INTERNAL_ERROR, "Internal server error".into())) +} + +/// A log filter's `fromBlock` or `toBlock`, as rskj reads it: the height, or +/// the error code and message rskj answers. +/// +/// `FilterRequestParam` takes the value's text (Jackson's `asText`) into a +/// `BlockIdentifierParam`, which refuses all but rskj's tags in any case, a +/// decimal and a `0x` hex number: `-32602 Invalid block identifier`. Then +/// `LogFilter` matches `latest`, `pending` and `earliest` ignoring case +/// (`isBlockWord`), and reads the rest with `Web3Impl.getBlockByNumberOrStr`, +/// whose `jsonHexToLong` throws on anything not hex that fits a long -- +/// `finalized`, `safe`, a decimal -- which nothing catches: `-32603 Internal +/// server error`. +pub fn parse_filter_block( + value: &serde_json::Value, + head_number: u64, +) -> Result { + use crate::types::{INTERNAL_ERROR, INVALID_PARAMS}; + use serde_json::Value; + let text = match value { + Value::String(s) => s.clone(), + Value::Number(n) => n.to_string(), + Value::Bool(b) => b.to_string(), + Value::Null => "null".to_string(), + Value::Array(_) | Value::Object(_) => String::new(), + }; + if text.is_empty() { + return Err(( + INVALID_PARAMS, + "Invalid block identifier: empty or null.".into(), + )); + } + if !(is_block_tag(&text) || is_decimal(&text) || is_hex_number(&text)) { + return Err((INVALID_PARAMS, format!("Invalid block identifier '{text}'"))); + } + if text.eq_ignore_ascii_case("latest") || text.eq_ignore_ascii_case("pending") { + return Ok(head_number); + } + if text.eq_ignore_ascii_case("earliest") { + return Ok(0); } - u64::from_str_radix(hex, 16).ok() + text.strip_prefix("0x") + .and_then(|hex| i64::from_str_radix(hex, 16).ok()) + .and_then(|number| u64::try_from(number).ok()) + .ok_or((INTERNAL_ERROR, "Internal server error".into())) } /// Parses a 0x-prefixed hex index. Returns None on anything malformed, which @@ -388,14 +518,16 @@ mod tests { assert_eq!(parse_block_number("0x64", 999), Some(100)); } - /// rskj matches tags case-insensitively for half its methods and exactly - /// for the other half. Accepting either is the half that cannot break a - /// caller written against the other. See #178. + /// Tags are matched as written, as rskj matches them everywhere but in a + /// log filter: `LATEST` resolves to nothing, nor does a tag with space + /// around it or an upper-case prefix (upstream #178). #[test] - fn block_tags_are_case_insensitive() { - assert_eq!(parse_block_number("LATEST", 100), Some(100)); - assert_eq!(parse_block_number("Earliest", 100), Some(0)); - assert_eq!(parse_block_number("PENDING", 100), Some(100)); + fn block_tags_are_matched_as_written() { + assert_eq!(parse_block_number("LATEST", 100), None); + assert_eq!(parse_block_number("Earliest", 100), None); + assert_eq!(parse_block_number("PENDING", 100), None); + assert_eq!(parse_block_number(" latest", 100), None); + assert_eq!(parse_block_number("0X10", 100), None); } /// Neither a bare prefix nor rubbish resolves to a block. @@ -405,6 +537,109 @@ mod tests { assert_eq!(parse_block_number("", 100), None); assert_eq!(parse_block_number("finalized", 100), None); assert_eq!(parse_block_number("0xzz", 100), None); + assert_eq!(parse_block_number("0x+5", 100), None); + } + + /// A block reference rskj refuses before it looks anything up: anything + /// but the three tags as written, a decimal and a hex number, and then a + /// decimal, which `getBlock` can't read. + #[test] + fn a_block_reference_is_refused_as_rskj_refuses_it() { + let refused = |s: &str| { + refused_block_ref(&serde_json::json!(1), s) + .and_then(|resp| resp.error) + .map(|error| (error.code, error.message)) + }; + for fine in ["latest", "pending", "earliest", "0x0", "0x1F"] { + assert_eq!(refused(fine), None, "{fine}"); + } + for (s, message) in [ + ("LATEST", "Invalid block identifier 'LATEST'"), + ("finalized", "Invalid block identifier 'finalized'"), + ("0x", "Invalid block identifier '0x'"), + ("12", "invalid blocknumber 12"), + ] { + assert_eq!(refused(s), Some((-32602, message.to_string())), "{s}"); + } + } + + /// The block a call or an estimate runs on: rskj's tags as written, a hex + /// or decimal long; `eth_call` refuses another tag's spelling as an + /// identifier and `eth_estimateGas` as a specifier, both `-32602`, and a + /// number past a long is `-32603`. + #[test] + fn a_calls_block_is_read_as_rskj_reads_it() { + for estimate in [false, true] { + assert_eq!(execution_block("latest", estimate), Ok("latest".into())); + assert_eq!(execution_block("pending", estimate), Ok("pending".into())); + assert_eq!(execution_block("0x1F", estimate), Ok("0x1f".into())); + assert_eq!(execution_block("100", estimate), Ok("0x64".into())); + assert_eq!( + execution_block("0x8000000000000000", estimate), + Err((-32603, "Internal server error".into())) + ); + assert_eq!( + execution_block("9223372036854775808", estimate), + Err((-32603, "Internal server error".into())) + ); + assert_eq!( + execution_block("foo", estimate), + Err((-32602, "Invalid block identifier 'foo'".into())) + ); + } + assert_eq!( + execution_block("", false), + Err((-32602, "Invalid block identifier ''".into())) + ); + assert_eq!( + execution_block("", true), + Err((-32602, "Invalid block identifier: empty or null.".into())) + ); + assert_eq!( + execution_block("LATEST", false), + Err((-32602, "Invalid block identifier 'LATEST'".into())) + ); + let (code, message) = execution_block("safe", true).unwrap_err(); + assert_eq!(code, -32602); + assert!( + message.starts_with("Unsupported block specifier 'safe'."), + "{message}" + ); + } + + /// A log filter's block: rskj's three tags in any case, a hex long; a + /// value that isn't one of rskj's tags or numbers is `-32602`, and + /// `finalized`, `safe`, a decimal or a number past a long `-32603`. + #[test] + fn a_log_filters_block_is_read_as_rskj_reads_it() { + use serde_json::json; + assert_eq!(parse_filter_block(&json!("LATEST"), 100), Ok(100)); + assert_eq!(parse_filter_block(&json!("Pending"), 100), Ok(100)); + assert_eq!(parse_filter_block(&json!("EARLIEST"), 100), Ok(0)); + assert_eq!(parse_filter_block(&json!("0x1F"), 100), Ok(31)); + let internal = Err((-32603, "Internal server error".to_string())); + for value in [ + json!("finalized"), + json!("SAFE"), + json!("12"), + json!(12), + json!("0x8000000000000000"), + ] { + assert_eq!(parse_filter_block(&value, 100), internal, "{value}"); + } + for (value, message) in [ + (json!("foo"), "Invalid block identifier 'foo'"), + (json!(null), "Invalid block identifier 'null'"), + (json!(true), "Invalid block identifier 'true'"), + (json!(""), "Invalid block identifier: empty or null."), + (json!({}), "Invalid block identifier: empty or null."), + ] { + assert_eq!( + parse_filter_block(&value, 100), + Err((-32602, message.to_string())), + "{value}" + ); + } } /// **A slot is a quantity.** Clients send `0x0`, not 64 zeros, and diff --git a/crates/rpc/src/logs.rs b/crates/rpc/src/logs.rs index cd19953..3cebd74 100644 --- a/crates/rpc/src/logs.rs +++ b/crates/rpc/src/logs.rs @@ -1,5 +1,5 @@ use crate::dto::LogDto; -use crate::helpers::{parse_b256, parse_block_number, to_hex_b256, to_hex_u64}; +use crate::helpers::{parse_b256, to_hex_b256, to_hex_u64}; use crate::server::RpcState; use crate::types::*; use alloy_primitives::{Address, B256}; @@ -126,7 +126,10 @@ pub fn eth_get_logs(id: Value, params: &Value, state: &RpcState) -> JsonRpcRespo }; let head_num = head_number(&state.store).unwrap_or(0); - let filter = parse_log_filter(filter_obj, head_num); + let filter = match parse_log_filter(filter_obj, head_num) { + Ok(filter) => filter, + Err((code, message)) => return JsonRpcResponse::error(id, code, message), + }; let from = filter.from_block.unwrap_or(head_num); let to = filter.to_block.unwrap_or(head_num); @@ -152,7 +155,10 @@ pub fn eth_new_filter(id: Value, params: &Value, state: &RpcState) -> JsonRpcRes }; let head_num = head_number(&state.store).unwrap_or(0); - let log_filter = parse_log_filter(filter_obj, head_num); + let log_filter = match parse_log_filter(filter_obj, head_num) { + Ok(filter) => filter, + Err((code, message)) => return JsonRpcResponse::error(id, code, message), + }; let filter = Filter { kind: FilterKind::Log(log_filter), @@ -410,14 +416,17 @@ fn matches_filter(log: &rustock_core::Log, filter: &LogFilter) -> bool { true } -fn parse_log_filter(obj: &Value, head_num: u64) -> LogFilter { - let from_block = obj.get("fromBlock") - .and_then(|v| v.as_str()) - .and_then(|s| parse_block_number(s, head_num)); - - let to_block = obj.get("toBlock") - .and_then(|v| v.as_str()) - .and_then(|s| parse_block_number(s, head_num)); +/// A log filter as rskj reads it. Its blocks are read as +/// `crate::helpers::parse_filter_block` says, and one rskj can't read is an +/// error, not the head. +fn parse_log_filter(obj: &Value, head_num: u64) -> Result { + let block = |key: &str| { + obj.get(key) + .map(|value| crate::helpers::parse_filter_block(value, head_num)) + .transpose() + }; + let from_block = block("fromBlock")?; + let to_block = block("toBlock")?; let addresses = parse_address_filter(obj.get("address")); @@ -441,7 +450,12 @@ fn parse_log_filter(obj: &Value, head_num: u64) -> LogFilter { }) .unwrap_or_default(); - LogFilter { from_block, to_block, addresses, topics } + Ok(LogFilter { + from_block, + to_block, + addresses, + topics, + }) } fn parse_address_filter(v: Option<&Value>) -> Vec
{ diff --git a/crates/rpc/src/rsk.rs b/crates/rpc/src/rsk.rs index 7d183b7..295bed8 100644 --- a/crates/rpc/src/rsk.rs +++ b/crates/rpc/src/rsk.rs @@ -270,6 +270,9 @@ pub fn rsk_get_storage_bytes_at( }; let block_param = params.get(2).and_then(|v| v.as_str()).unwrap_or("latest"); + if let Some(refused) = crate::helpers::refused_block_ref(&id, block_param) { + return refused; + } let Some((root, _header)) = crate::state::resolve_state_root(block_param, state) else { return JsonRpcResponse::error(id, INTERNAL_ERROR, "Cannot resolve block"); }; diff --git a/crates/rpc/src/state.rs b/crates/rpc/src/state.rs index 0fbb608..ff4141c 100644 --- a/crates/rpc/src/state.rs +++ b/crates/rpc/src/state.rs @@ -12,6 +12,9 @@ pub fn eth_get_balance(id: Value, params: &Value, state: &RpcState) -> JsonRpcRe return JsonRpcResponse::error(id, INVALID_PARAMS, "Missing or invalid address"); }; let block_param = params.get(1).and_then(|v| v.as_str()).unwrap_or("latest"); + if let Some(refused) = crate::helpers::refused_block_ref(&id, block_param) { + return refused; + } let Some((root, _header)) = resolve_state_root(block_param, state) else { return JsonRpcResponse::error(id, INTERNAL_ERROR, "Cannot resolve block"); @@ -29,6 +32,9 @@ pub fn eth_get_transaction_count(id: Value, params: &Value, state: &RpcState) -> return JsonRpcResponse::error(id, INVALID_PARAMS, "Missing or invalid address"); }; let block_param = params.get(1).and_then(|v| v.as_str()).unwrap_or("latest"); + if let Some(refused) = crate::helpers::refused_block_ref(&id, block_param) { + return refused; + } // For "pending", return the pool's pending nonce if available if block_param == "pending" { @@ -58,6 +64,9 @@ pub fn eth_get_code(id: Value, params: &Value, state: &RpcState) -> JsonRpcRespo return JsonRpcResponse::error(id, INVALID_PARAMS, "Missing or invalid address"); }; let block_param = params.get(1).and_then(|v| v.as_str()).unwrap_or("latest"); + if let Some(refused) = crate::helpers::refused_block_ref(&id, block_param) { + return refused; + } let Some((root, _header)) = resolve_state_root(block_param, state) else { return JsonRpcResponse::error(id, INTERNAL_ERROR, "Cannot resolve block"); @@ -88,6 +97,9 @@ pub fn eth_get_storage_at(id: Value, params: &Value, state: &RpcState) -> JsonRp return JsonRpcResponse::error(id, INVALID_PARAMS, "Missing or invalid storage slot"); }; let block_param = params.get(2).and_then(|v| v.as_str()).unwrap_or("latest"); + if let Some(refused) = crate::helpers::refused_block_ref(&id, block_param) { + return refused; + } let Some((root, _header)) = resolve_state_root(block_param, state) else { return JsonRpcResponse::error(id, INTERNAL_ERROR, "Cannot resolve block"); diff --git a/crates/rpc/src/tests.rs b/crates/rpc/src/tests.rs index 909eb01..a958a82 100644 --- a/crates/rpc/src/tests.rs +++ b/crates/rpc/src/tests.rs @@ -1368,7 +1368,8 @@ fn answered_within_a_minute(state: &RpcState, request: JsonRpcRequest) -> JsonRp /// **A log range is walked no further than the head.** /// -/// A client may name any block up to 2^64 - 1. `eth_getLogs` measures its span +/// A client may name any block up to 2^63 - 1, the most rskj reads in a log +/// filter (`jsonHexToLong`; past it is `-32603`). `eth_getLogs` measures its span /// against the cap, and that measurement must not overflow; an installed /// filter's range is never measured at all. Either way nothing above the head /// holds a block, and walking those numbers one by one would be a request @@ -1376,7 +1377,7 @@ fn answered_within_a_minute(state: &RpcState, request: JsonRpcRequest) -> JsonRp #[tokio::test] async fn a_log_range_reaching_the_top_of_the_number_space_stops_at_the_head() { let (state, _tmp) = setup_state_with_logs(); - let top = "0xffffffffffffffff"; + let top = "0x7fffffffffffffff"; let at_the_top = answered_within_a_minute( &state, @@ -4025,6 +4026,94 @@ async fn eth_call_answers_where_a_transaction_would_be_refused() { } } +/// Block tags as rskj reads them (upstream #178): as written by every method +/// but a log filter, which reads them in any case. `LATEST` is `-32602` to +/// the rest, as is `finalized` -- rskj's `BlockIdentifierParam` lets both +/// through, but nothing it hands them to resolves them. In a log filter, +/// `finalized`, `safe` and a decimal reach `jsonHexToLong` and throw, +/// `-32603`, and what isn't a tag or a number is `-32602`. `eth_call` and +/// `eth_estimateGas` read a decimal block (`ExecutionBlockRetriever`). +#[tokio::test] +async fn block_tags_are_read_as_rskj_reads_them() { + let (state, _tmp) = setup_state_with_logs(); + let ask = |method: &str, params: Value| { + let request = make_request(method, params); + let state = &state; + async move { dispatch_for_test(state, request).await } + }; + let code = |resp: &JsonRpcResponse| resp.error.as_ref().map(|error| error.code); + let address = "0x1111111111111111111111111111111111111111"; + + let resp = ask("eth_getBlockByNumber", json!(["latest", false])).await; + assert_eq!(resp.result.unwrap()["number"], json!("0x3")); + for tag in ["LATEST", "Earliest", "finalized", "safe"] { + let resp = ask("eth_getBlockByNumber", json!([tag, false])).await; + assert_eq!(code(&resp), Some(-32602), "eth_getBlockByNumber {tag}"); + for method in ["eth_getBalance", "eth_getCode", "eth_getTransactionCount"] { + let resp = ask(method, json!([address, tag])).await; + assert_eq!( + resp.error.map(|error| (error.code, error.message)), + Some((-32602, format!("Invalid block identifier '{tag}'"))), + "{method} {tag}" + ); + } + let resp = ask("eth_call", json!([{"to": address}, tag])).await; + assert_eq!( + resp.error.map(|error| (error.code, error.message)), + Some((-32602, format!("Invalid block identifier '{tag}'"))), + "eth_call {tag}" + ); + let resp = ask("eth_estimateGas", json!([{"to": address}, tag])).await; + let error = resp.error.expect("eth_estimateGas refuses it"); + assert_eq!(error.code, -32602, "eth_estimateGas {tag}"); + assert!( + error + .message + .starts_with(&format!("Unsupported block specifier '{tag}'.")), + "eth_estimateGas {tag}: {}", + error.message + ); + } + let resp = ask("eth_getStorageAt", json!([address, "0x0", "PENDING"])).await; + assert_eq!(code(&resp), Some(-32602)); + let resp = ask("rsk_getStorageBytesAt", json!([address, "0x0", "Latest"])).await; + assert_eq!(code(&resp), Some(-32602)); + let resp = ask("eth_getBalance", json!([address, "3"])).await; + assert_eq!( + resp.error.map(|error| (error.code, error.message)), + Some((-32602, "invalid blocknumber 3".to_string())) + ); + + let logs = |filter: Value| ask("eth_getLogs", json!([filter])); + let resp = logs(json!({"fromBlock": "EARLIEST", "toBlock": "Latest"})).await; + assert_eq!(resp.result.unwrap().as_array().unwrap().len(), 3); + let resp = ask("eth_newFilter", json!([{"fromBlock": "LATEST"}])).await; + assert!(resp.result.is_some(), "{:?}", resp.error); + for block in [json!("finalized"), json!("SAFE"), json!("2"), json!(2)] { + for key in ["fromBlock", "toBlock"] { + let resp = logs(json!({ key: block })).await; + assert_eq!(code(&resp), Some(-32603), "eth_getLogs {key} {block}"); + } + let resp = ask("eth_newFilter", json!([{"fromBlock": block}])).await; + assert_eq!(code(&resp), Some(-32603), "eth_newFilter {block}"); + } + for block in [json!("foo"), json!(null), json!("0x")] { + let resp = logs(json!({"toBlock": block})).await; + assert_eq!(code(&resp), Some(-32602), "eth_getLogs {block}"); + } + + // A decimal block for a call, at the tracing fixture's #41. + let (state, _tmp, _, _, _, callee) = setup_state_for_tracing(); + let call = json!({"to": format!("{callee:#x}")}); + let resp = dispatch_for_test(&state, make_request("eth_call", json!([call, "41"]))).await; + assert_eq!( + resp.result, + Some(json!(format!("0x{:064x}", 0x2a))), + "{:?}", + resp.error + ); +} + /// `eth_getBlockBy*` with full transactions must name the real sender. /// /// It used to hardcode `from` to the zero address, so every transaction in From 3f28e46a79819f2dcb52b531c40dc2e5b54dff95 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Tue, 10 Nov 2026 07:59:28 -0800 Subject: [PATCH 085/264] rpc: a call's and a sent transaction's defaults as rskj's eth_call runs at a gas price of 0 and at most rskj's call gas cap, 10,000,000, which now bounds a local call; eth_estimateGas always runs at rskj's estimation cap, 6,800,000. eth_sendTransaction defaults to a gas price of 0 and 90,000 gas, and reads gasLimit too. --- crates/rpc/src/call.rs | 43 ++++++++---- crates/rpc/src/sign.rs | 33 +++------ crates/rpc/src/tests.rs | 145 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 183 insertions(+), 38 deletions(-) diff --git a/crates/rpc/src/call.rs b/crates/rpc/src/call.rs index e29d801..ad7f07a 100644 --- a/crates/rpc/src/call.rs +++ b/crates/rpc/src/call.rs @@ -9,6 +9,22 @@ use serde_json::{json, Value}; use crate::helpers::to_hex_bytes; +/// The gas `eth_call` runs with when the call names none, and the most it +/// runs with when it names more: rskj's `rpc.callGasCap` as its +/// `reference.conf` ships it. `CallArgumentsToByteArray.gasLimitForCall` +/// takes the call's gas, or `0x5AF3107A4000` when it names none +/// (`getGasLimit`), and the cap when that is more. +/// +/// A local call is no longer held to the block's gas limit (see +/// `RskHandler::as_local_call`), so this is what bounds its run. +pub const CALL_GAS_CAP: u64 = 10_000_000; + +/// The gas every `eth_estimateGas` runs with, whatever the call names, and +/// the most it answers: rskj's `rpc.gasEstimationCap` as its `reference.conf` +/// ships it (`EthModule.estimateGas` passes it as the gas limit; +/// `internalEstimateGas` caps the answer at it). +pub const GAS_ESTIMATION_CAP: u64 = 6_800_000; + pub fn eth_call(id: Value, params: &Value, state: &RpcState) -> JsonRpcResponse { let Some(call_req) = params.get(0).and_then(CallRequest::from_json) else { return JsonRpcResponse::error(id, INVALID_PARAMS, "Missing or invalid call request"); @@ -28,7 +44,7 @@ pub fn eth_call(id: Value, params: &Value, state: &RpcState) -> JsonRpcResponse }; let sender = call_req.from.unwrap_or(Address::ZERO); - let tx = build_call_tx(&call_req, &header, &root, sender, state); + let tx = build_call_tx(&call_req, &root, sender, state); let executor = RskExecutor::new(hardfork_cfg, state.store.clone()); match executor.execute_local_call(&header, &tx, sender, &root, trie_store) { @@ -64,11 +80,10 @@ pub fn eth_estimate_gas(id: Value, params: &Value, state: &RpcState) -> JsonRpcR let sender = call_req.from.unwrap_or(Address::ZERO); let executor = RskExecutor::new(hardfork_cfg, state.store.clone()); - let gas_cap = call_req - .gas - .unwrap_or(header.gas_limit.saturating_to::()); + // rskj runs every estimate at its cap; the call's own gas isn't read. + let gas_cap = GAS_ESTIMATION_CAP; - let mut tx = build_call_tx(&call_req, &header, &root, sender, state); + let mut tx = build_call_tx(&call_req, &root, sender, state); tx.gas_limit = U256::from(gas_cap); if state.gas_estimator_binary_search { @@ -124,7 +139,7 @@ fn estimate_by_binary_search( let mut hi = gas_cap; // First check if the transaction even succeeds at the gas cap - let mut tx = build_call_tx(call_req, header, root, sender, state); + let mut tx = build_call_tx(call_req, root, sender, state); tx.gas_limit = U256::from(hi); match executor.execute_local_call(header, &tx, sender, root, trie_store.clone()) { Ok(result) if result.success => {} @@ -138,7 +153,7 @@ fn estimate_by_binary_search( while lo + 1 < hi { let mid = lo + (hi - lo) / 2; - let mut probe_tx = build_call_tx(call_req, header, root, sender, state); + let mut probe_tx = build_call_tx(call_req, root, sender, state); probe_tx.gas_limit = U256::from(mid); match executor.execute_local_call(header, &probe_tx, sender, root, trie_store.clone()) { @@ -150,13 +165,13 @@ fn estimate_by_binary_search( JsonRpcResponse::success(id, json!(crate::helpers::to_hex_u64(hi))) } -/// eth_call/eth_estimateGas are simulations: default the nonce to the -/// sender's current one and the gas price to the block minimum so the -/// executor's transaction validation passes (rskj skips those checks for -/// local calls). +/// The transaction `eth_call` and `eth_estimateGas` run, with rskj's defaults +/// for what the call leaves out (`CallArgumentsToByteArray`): a gas price of +/// 0 (`getGasPrice`), [`CALL_GAS_CAP`]'s gas (`gasLimitForCall`; an estimate +/// sets its own), no value. The nonce is the sender's, as +/// `ReversibleTransactionExecutor` builds it; a local call doesn't check it. fn build_call_tx( req: &CallRequest, - header: &rustock_core::Header, root: &rustock_trie::TrieNode, sender: Address, state: &RpcState, @@ -170,8 +185,8 @@ fn build_call_tx( .unwrap_or(0); Transaction { nonce: sender_nonce, - gas_price: req.gas_price.unwrap_or(header.minimum_gas_price), - gas_limit: req.gas.map(U256::from).unwrap_or(header.gas_limit), + gas_price: req.gas_price.unwrap_or(U256::ZERO), + gas_limit: U256::from(req.gas.map_or(CALL_GAS_CAP, |gas| gas.min(CALL_GAS_CAP))), to: req.to.map(|a| Bytes::copy_from_slice(a.as_slice())).unwrap_or_default(), value: req.value.unwrap_or(U256::ZERO), input: req.data.as_deref().map(Bytes::copy_from_slice).unwrap_or_default(), diff --git a/crates/rpc/src/sign.rs b/crates/rpc/src/sign.rs index 7eb8b34..0380376 100644 --- a/crates/rpc/src/sign.rs +++ b/crates/rpc/src/sign.rs @@ -128,9 +128,10 @@ impl SignError { /// Fill in what the caller left out, then sign. /// -/// Every default is the one a caller would otherwise have had to fetch -/// with a separate call, and is taken from the same place that call would -/// have taken it. +/// The defaults are rskj's (`TransactionArgumentsUtil.processArguments`): no +/// value, a gas price of 0, the gas named as `gas` or else as `gasLimit`, or +/// else 90,000 (`GasCost.TRANSACTION_DEFAULT`), and the sender's pending nonce +/// (`EthModuleTransactionBase.sendTransaction`). fn build_and_sign(params: &Value, state: &RpcState) -> Result<(Address, Vec), SignError> { let wallet = state.wallet.as_ref().expect("checked by the caller"); @@ -182,18 +183,11 @@ fn build_and_sign(params: &Value, state: &RpcState) -> Result<(Address, Vec) .unwrap_or_else(|| account_nonce(&from, state)), }; - let gas_price = match quantity(obj.get("gasPrice"))? { - Some(p) => p, - None => state.gas_price.as_ref().map(|g| g.gas_price()).unwrap_or(U256::ZERO), - }; + let gas_price = quantity(obj.get("gasPrice"))?.unwrap_or(U256::ZERO); - // Absent, take the block's whole gas limit. A transaction that asks - // for more than it needs pays only for what it uses, so the generous - // default costs the caller nothing; the stingy one would make a - // contract deployment fail for a reason the caller cannot see. let gas_limit = match quantity(obj.get("gas"))? { Some(g) => g, - None => head_gas_limit(state), + None => quantity(obj.get("gasLimit"))?.unwrap_or(U256::from(TRANSACTION_DEFAULT_GAS)), }; let chain_id = state.hardfork_cfg.as_ref().map(|c| c.chain_id).ok_or(SignError::NoChainId)?; @@ -253,15 +247,6 @@ fn account_nonce(address: &Address, state: &RpcState) -> u64 { .unwrap_or(0) } -fn head_gas_limit(state: &RpcState) -> U256 { - state - .store - .head() - .ok() - .flatten() - .and_then(|h| state.store.header(h).ok().flatten()) - .map(|h| h.gas_limit) - // The sequential-sublist floor, which is the smallest limit any - // RSK network configures. Only reached if the store has no head. - .unwrap_or(U256::from(6_800_000u64)) -} +/// The gas a sent transaction names when the caller names none: rskj's +/// `GasCost.TRANSACTION_DEFAULT`. +const TRANSACTION_DEFAULT_GAS: u64 = 90_000; diff --git a/crates/rpc/src/tests.rs b/crates/rpc/src/tests.rs index a958a82..43cf0b4 100644 --- a/crates/rpc/src/tests.rs +++ b/crates/rpc/src/tests.rs @@ -4114,6 +4114,151 @@ async fn block_tags_are_read_as_rskj_reads_them() { ); } +/// A state for a call's defaults: `reader` returns the GASPRICE and the GAS +/// it sees, as two words; `burner` loops `count` times at 26 gas a turn, so +/// running it costs 21,003 + 26 * `count`. The head holds 6,800,000 gas and +/// asks 59,240,000 a unit of it, neither of them a call's default. +fn setup_defaults_state(count: u32) -> (RpcState, tempfile::TempDir, Address, Address) { + use rustock_trie::{account_key, code_key, MemoryTrieStore, TrieKeySlice, TrieNode}; + + let tmp = tempfile::tempdir().unwrap(); + let store = Arc::new(BlockStore::open(tmp.path()).unwrap()); + let trie_store = Arc::new(MemoryTrieStore::new()); + let mut root = TrieNode::empty(); + let reader = Address::repeat_byte(0xaa); + let burner = Address::repeat_byte(0xbb); + // GASPRICE PUSH1 0 MSTORE GAS PUSH1 32 MSTORE PUSH1 64 PUSH1 0 RETURN + let read = [ + 0x3a, 0x60, 0x00, 0x52, 0x5a, 0x60, 0x20, 0x52, 0x60, 0x40, 0x60, 0x00, 0xf3, + ]; + // PUSH3 count; JUMPDEST PUSH1 1 SWAP1 SUB DUP1 PUSH1 4 JUMPI; STOP + let mut burn = vec![0x62]; + burn.extend_from_slice(&count.to_be_bytes()[1..]); + burn.extend_from_slice(&[0x5b, 0x60, 0x01, 0x90, 0x03, 0x80, 0x60, 0x04, 0x57, 0x00]); + for (addr, code) in [(reader, &read[..]), (burner, &burn[..])] { + let acct = rustock_trie::AccountState::new(U256::ZERO, U256::ZERO); + root = root.put( + &TrieKeySlice::from_key(&account_key(&addr)), + &acct.encode(), + &*trie_store, + ); + root = root.put( + &TrieKeySlice::from_key(&code_key(&addr)), + code, + &*trie_store, + ); + } + root.save(&*trie_store, true); + let state_root = root.compute_hash(&*trie_store); + rustock_trie::TrieStore::put( + &*trie_store, + state_root.as_slice(), + &root.to_message(&*trie_store), + ); + + let header = Header { + state_root, + gas_limit: U256::from(6_800_000u64), + ..test_header(42) + }; + let hash = header.hash(); + store.put_header(&header).unwrap(); + store.put_canonical_hash(42, hash).unwrap(); + store.set_head(hash).unwrap(); + store + .put_total_difficulty(hash, U256::from(42_000)) + .unwrap(); + + let (mut state, _discard) = setup_state(); + state.store = store; + state.trie_store = Some(trie_store); + state.hardfork_cfg = Some(rustock_execution::RskHardforkConfig::mainnet()); + std::mem::forget(_discard); + (state, tmp, reader, burner) +} + +/// A call's defaults are rskj's (task 282, `CallArgumentsToByteArray`): a gas +/// price of 0, not the block's minimum, and `rpc.callGasCap`'s 10,000,000 gas, +/// not the block's limit -- the cap on the gas a call names, too. An +/// estimate runs at `rpc.gasEstimationCap`'s 6,800,000, whatever the call +/// names (`EthModule.estimateGas`). +#[tokio::test] +async fn a_calls_defaults_are_rskjs() { + let (state, _tmp, reader, burner) = setup_defaults_state(1 << 18); + let ask = |method: &str, call: Value| { + let request = make_request(method, json!([call, "latest"])); + let state = &state; + async move { dispatch_for_test(state, request).await } + }; + let words = |resp: JsonRpcResponse| { + let out = resp.result.unwrap_or_else(|| panic!("{:?}", resp.error)); + let out = hex::decode(out.as_str().unwrap().trim_start_matches("0x")).unwrap(); + ( + U256::from_be_slice(&out[..32]), + U256::from_be_slice(&out[32..]), + ) + }; + let to = format!("{reader:#x}"); + + // GAS reads the call's gas less the 21,000 and the 13 spent before it. + let resp = ask("eth_call", json!({"to": to})).await; + assert_eq!(words(resp), (U256::ZERO, U256::from(10_000_000 - 21_013))); + let resp = ask("eth_call", json!({"to": to, "gas": "0xffffffffffffffff"})).await; + assert_eq!(words(resp), (U256::ZERO, U256::from(10_000_000 - 21_013))); + let resp = ask( + "eth_call", + json!({"to": to, "gas": "0x30d40", "gasPrice": "0x2"}), + ) + .await; + assert_eq!(words(resp), (U256::from(2), U256::from(200_000 - 21_013))); + + // Not run with the 21,000 the call names. + let resp = ask("eth_estimateGas", json!({"to": to, "gas": "0x5208"})).await; + assert!(resp.result.is_some(), "{:?}", resp.error); + // The burner needs 21,003 + 26 * 2^18 = 6,836,747, over the estimate's cap + // whatever the call names. + let burn = format!("{burner:#x}"); + let resp = ask("eth_estimateGas", json!({"to": burn, "gas": "0x7a1200"})).await; + assert!(resp.error.is_some(), "{:?}", resp.result); + let resp = ask("eth_call", json!({"to": burn})).await; + assert_eq!(resp.result, Some(json!("0x")), "{:?}", resp.error); +} + +/// A sent transaction's defaults are rskj's (task 282, +/// `TransactionArgumentsUtil.processArguments`): a gas price of 0, not the +/// node's price, and the gas named as `gas`, else as `gasLimit`, else 90,000, +/// not the head's whole gas limit. +#[tokio::test] +async fn a_sent_transactions_defaults_are_rskjs() { + let (mut state, _tmp) = setup_state_with_wallet(); + state.gas_price = Some(Arc::new(FixedGasPrice(123_456_789, true))); + let from = format!("{:#x}", test_wallet().accounts()[0]); + for (fields, gas) in [ + (json!({}), 90_000u64), + (json!({"gasLimit": "0x7530"}), 30_000), + (json!({"gas": "0x5208", "gasLimit": "0x7530"}), 21_000), + ] { + let mut request = json!({"from": from, "to": "0x00000000000000000000000000000000000000aa", "nonce": "0x0"}); + request + .as_object_mut() + .unwrap() + .extend(fields.as_object().unwrap().clone()); + let resp = dispatch_for_test( + &state, + make_request("eth_signTransaction", json!([request])), + ) + .await; + let raw = resp + .result + .unwrap_or_else(|| panic!("{fields}: {:?}", resp.error)); + let raw = hex::decode(raw.as_str().unwrap().trim_start_matches("0x")).unwrap(); + use alloy_rlp::Decodable; + let tx = rustock_core::Transaction::decode(&mut raw.as_slice()).unwrap(); + assert_eq!(tx.gas_price, U256::ZERO, "{fields}"); + assert_eq!(tx.gas_limit, U256::from(gas), "{fields}"); + } +} + /// `eth_getBlockBy*` with full transactions must name the real sender. /// /// It used to hardcode `from` to the zero address, so every transaction in From 9a3fcbc1774d6a6102412de87738057502c16d06 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Tue, 10 Nov 2026 07:59:29 -0800 Subject: [PATCH 086/264] rpc: eth_gasPrice in doubles, as rskj computes it Without a working fee market rskj answers the head's minimum gas price times 1.1 in doubles, read back through Java 17's Double.toString, and prints either answer's low 64 bits. A port of FloatingDecimal gives Java 17's digits. --- crates/rpc/src/eth.rs | 83 +++---- crates/rpc/src/java.rs | 519 ++++++++++++++++++++++++++++++++++++++++ crates/rpc/src/lib.rs | 1 + crates/rpc/src/tests.rs | 69 +++++- 4 files changed, 625 insertions(+), 47 deletions(-) create mode 100644 crates/rpc/src/java.rs diff --git a/crates/rpc/src/eth.rs b/crates/rpc/src/eth.rs index fc1e2e5..0cd4e13 100644 --- a/crates/rpc/src/eth.rs +++ b/crates/rpc/src/eth.rs @@ -1,5 +1,5 @@ use crate::helpers::{ - parse_b256, parse_block_number, parse_hex_u32, to_hex_b256, to_hex_u256, to_hex_u64, + parse_b256, parse_block_number, parse_hex_u32, to_hex_b256, to_hex_u64, BlockResultDto, }; use crate::server::TxSubmitter; @@ -96,57 +96,52 @@ pub async fn eth_syncing( /// 26,065,600 wei against a minimum of 587, a factor of 44,000. Wallets that /// trust it do not merely overpay -- they price themselves over RSKIP252's /// 100x cap, and the transactions they produce cannot legally be mined. +/// +/// rskj prints either answer as `Long.toHexString(price.longValue())` +/// (`Web3Impl.eth_gasPrice`): its low 64 bits, unsigned. pub fn eth_gas_price(id: Value, state: &crate::server::RpcState) -> JsonRpcResponse { if let Some(tracker) = &state.gas_price { if tracker.is_fee_market_working() { - return JsonRpcResponse::success( - id, - json!(to_hex_u256(&alloy_primitives::U256::from(tracker.gas_price()))), - ); + let price = tracker.gas_price(); + return JsonRpcResponse::success(id, json!(format!("{:#x}", price.as_limbs()[0]))); } } - let price = head_header(&state.store) - .map(|h| to_hex_u256(&head_minimum_times_multiplier(&h.minimum_gas_price))) - .unwrap_or_else(|| "0x0".to_string()); - JsonRpcResponse::success(id, json!(price)) + let Some(header) = head_header(&state.store) else { + return JsonRpcResponse::success(id, json!("0x0")); + }; + match head_minimum_times_multiplier(&header.minimum_gas_price) { + Some(price) => JsonRpcResponse::success(id, json!(format!("{:#x}", price.as_limbs()[0]))), + // Where rskj's `BigDecimal` would throw, which no 256-bit minimum reaches. + None => JsonRpcResponse::error(id, INTERNAL_ERROR, "Internal server error"), + } } /// The head's `minimumGasPrice` times 1.1, which is what rskj answers when the -/// fee market is not working. -fn head_minimum_times_multiplier(minimum: &alloy_primitives::U256) -> alloy_primitives::U256 { - minimum.saturating_mul(alloy_primitives::U256::from(GAS_PRICE_MULTIPLIER_NUM)) - / alloy_primitives::U256::from(GAS_PRICE_MULTIPLIER_DEN) -} - -/// 1.1 as an exact ratio, so `x * 11 / 10` with truncation rather than a -/// float. -/// -/// Two constants in rskj hold this value and both are 1.1, which is why one -/// pair serves here. They are not the same setting: -/// -/// * `GasPriceTracker.DEFAULT_GAS_PRICE_MULTIPLIER` floors the percentile. -/// * `EthereumImpl.minGasPriceMultiplier` scales the head's minimum on the -/// branch below. It comes from the `gasPriceMultiplier` config key, whose -/// default in `reference.conf` is 1.1 -- so an rskj operator can retune it -/// and this node cannot. A divergence only reachable by changing that -/// setting, and recorded here rather than discovered later. -/// -/// Duplicated from `rustock_sync::gas_price`, which has the same pair, and -/// deliberately: `rpc` does not depend on `sync` -- that is what -/// `GasPriceSource` exists to avoid -- so sharing them would mean adding the -/// dependency this trait was written to prevent. -/// -/// **rskj does this in floating point** -- `BigDecimal.valueOf(minimum -/// .doubleValue() * minGasPriceMultiplier).toBigInteger()` -- and an exact -/// ratio is not the same function. Checked rather than assumed: the two -/// agree on every minimum below 2^53, including all 2,061,855 values under -/// 2e8 that were compared one by one, and part company only above it, where -/// the double runs out of mantissa. A minimum gas price up there is 1 RBTC -/// per gas, so the divergence is unreachable; the integer form is kept for -/// being exact and float-free, and the boundary is written down so the next -/// reader does not have to rediscover where it lies. -const GAS_PRICE_MULTIPLIER_NUM: u64 = 11; -const GAS_PRICE_MULTIPLIER_DEN: u64 = 10; +/// fee market is not working -- in doubles, as `EthereumImpl.getGasPrice` +/// computes it: +/// +/// ```java +/// double estimatedGasPrice = minimumGasPrice.asBigInteger().doubleValue() * minGasPriceMultiplier; +/// return new Coin(BigDecimal.valueOf(estimatedGasPrice).toBigInteger()); +/// ``` +/// +/// That is `x * 11 / 10` only while the double holds the product to well +/// under a unit: from about 2^49 wei the rounding of the multiplication +/// shows (511,772,684,928,469 gives ...316, not ...315), and from 2^54 the +/// digits `Double.toString` keeps do (see [`crate::java`]). `None` where +/// `BigDecimal` would throw. +fn head_minimum_times_multiplier( + minimum: &alloy_primitives::U256, +) -> Option { + let estimated = crate::java::big_integer_double_value(minimum) * MIN_GAS_PRICE_MULTIPLIER; + crate::java::big_decimal_value_of_to_big_integer(estimated) +} + +/// rskj's `minGasPriceMultiplier`: `rpc.minGasPriceMultiplier`, 1.1 when the +/// configuration doesn't set it (`SystemProperties.getMinGasPriceMultiplier`; +/// `reference.conf` doesn't). An rskj operator can retune it and this node +/// can't. Not `rpc.gasPriceMultiplier`, which is the gas price tracker's. +const MIN_GAS_PRICE_MULTIPLIER: f64 = 1.1; pub fn eth_mining(id: Value) -> JsonRpcResponse { JsonRpcResponse::success(id, json!(false)) diff --git a/crates/rpc/src/java.rs b/crates/rpc/src/java.rs new file mode 100644 index 0000000..a232785 --- /dev/null +++ b/crates/rpc/src/java.rs @@ -0,0 +1,519 @@ +//! Java's arithmetic where rskj's answer depends on it. +//! +//! `eth_gasPrice`, when the fee market isn't working, is the head's minimum +//! gas price times 1.1 in doubles, read back through `Double.toString` +//! (`EthereumImpl.getGasPrice`): +//! +//! ```java +//! double estimatedGasPrice = minimumGasPrice.asBigInteger().doubleValue() * minGasPriceMultiplier; +//! return new Coin(BigDecimal.valueOf(estimatedGasPrice).toBigInteger()); +//! ``` +//! +//! `BigDecimal.valueOf(double)` is `new BigDecimal(Double.toString(d))`, and the +//! digits `Double.toString` prints changed in JDK 19. rskj compiles for Java 17 +//! (`rskj-core/build.gradle`, `sourceCompatibility = JavaVersion.VERSION_17`) +//! and its Dockerfile builds and runs it on Temurin 17, so the digits here are +//! JDK 17's: a port of `jdk.internal.math.FloatingDecimal` as OpenJDK 17 has +//! it (`getBinaryToASCIIConverter(double, true)`, `BinaryToASCIIBuffer.dtoa`, +//! `developLongDigits`, `roundup`, `estimateDecExp`). + +use alloy_primitives::{U256, U512}; + +const EXP_SHIFT: i32 = 52; +const FRACT_HOB: i64 = 1 << EXP_SHIFT; +const EXP_ONE: i64 = 1023 << EXP_SHIFT; +const SIGNIF_BIT_MASK: i64 = FRACT_HOB - 1; +const EXP_BIT_MASK: i64 = 0x7FF << EXP_SHIFT; +const SIGN_BIT_MASK: i64 = i64::MIN; +const MAX_SMALL_BIN_EXP: i32 = 62; +const MIN_SMALL_BIN_EXP: i32 = -(63 / 3); + +/// `FDBigInteger.LONG_5_POW.length`: 5^0 to 5^26. +const LONG_5_POW_LEN: i32 = 27; + +/// `FloatingDecimal.N_5_BITS`: about ceil(log2(5^i)). +const N_5_BITS: [i32; 27] = [ + 0, 3, 5, 7, 10, 12, 14, 17, 19, 21, 24, 26, 28, 31, 33, 35, 38, 40, 42, 45, 47, 49, 52, 54, 56, + 59, 61, +]; + +/// `FloatingDecimal.insignificantDigitsNumber`. +#[rustfmt::skip] +const INSIGNIFICANT_DIGITS_NUMBER: [i32; 64] = [ + 0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, + 4, 4, 4, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, + 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 11, 11, 11, + 12, 12, 12, 12, 13, 13, 13, 14, 14, 14, + 15, 15, 15, 15, 16, 16, 16, 17, 17, 17, + 18, 18, 18, 19, +]; + +/// `BigInteger.doubleValue()`: the double nearest `x`, ties to even. +pub fn big_integer_double_value(x: &U256) -> f64 { + let bits = x.bit_len(); + if bits <= 64 { + // Rust's integer-to-float cast rounds to nearest, ties to even. + return x.to::() as f64; + } + let shift = bits - 64; + // The top 64 bits, the lowest of them set when anything below them is, + // so that a tie at the 53rd bit with more under it rounds up. + let below = *x & ((U256::from(1u8) << shift) - U256::from(1u8)); + let top = (*x >> shift).to::() | u64::from(!below.is_zero()); + top as f64 * f64::from_bits((1023 + shift as u64) << EXP_SHIFT) +} + +/// `BigDecimal.valueOf(d).toBigInteger()` on Java 17: the value of the digits +/// `Double.toString(d)` prints, its fraction dropped. +/// +/// `None` where Java throws -- `Double.toString` of an infinity or a NaN +/// isn't a number to `BigDecimal` -- and where this doesn't reach: a +/// negative `d`, or one from 2^400 up. +pub fn big_decimal_value_of_to_big_integer(d: f64) -> Option { + if !d.is_finite() || d < 0.0 || d >= f64::from_bits((1023 + 400) << EXP_SHIFT) { + return None; + } + if d == 0.0 { + return Some(U512::ZERO); + } + let (digits, dec_exponent) = double_to_string_digits(d); + // The value is 0.d1 d2 ... dn x 10^dec_exponent; keep its integer part. + let mut value = U512::ZERO; + for i in 0..dec_exponent.max(0) as usize { + let digit = digits.get(i).copied().unwrap_or(0); + value = value * U512::from(10u8) + U512::from(digit); + } + Some(value) +} + +/// The digits of Java 17's `Double.toString(d)` for a positive finite `d`, +/// and its decimal exponent: `d` prints as 0.d1 d2 ... dn x 10^exponent. +/// `FloatingDecimal.getBinaryToASCIIConverter(d, true)`. +fn double_to_string_digits(d: f64) -> (Vec, i32) { + let d_bits = d.to_bits() as i64; + let mut fract_bits = d_bits & SIGNIF_BIT_MASK; + let mut bin_exp = ((d_bits & EXP_BIT_MASK) >> EXP_SHIFT) as i32; + let n_significant_bits; + if bin_exp == 0 { + // A subnormal: normalize it. + let leading_zeros = fract_bits.leading_zeros() as i32; + let shift = leading_zeros - (63 - EXP_SHIFT); + fract_bits <<= shift; + bin_exp = 1 - shift; + n_significant_bits = 64 - leading_zeros; + } else { + fract_bits |= FRACT_HOB; + n_significant_bits = EXP_SHIFT + 1; + } + bin_exp -= 1023; + dtoa(bin_exp, fract_bits, n_significant_bits) +} + +/// `BinaryToASCIIBuffer.dtoa` with `isCompatibleFormat` true, Java's `int` +/// and `long` arithmetic wrapping where Java's does. +fn dtoa(bin_exp: i32, mut fract_bits: i64, n_significant_bits: i32) -> (Vec, i32) { + let tail_zeros = fract_bits.trailing_zeros() as i32; + let n_fract_bits = EXP_SHIFT + 1 - tail_zeros; + let n_tiny_bits = (n_fract_bits - bin_exp - 1).max(0); + if (MIN_SMALL_BIN_EXP..=MAX_SMALL_BIN_EXP).contains(&bin_exp) + && n_tiny_bits < LONG_5_POW_LEN + && n_fract_bits + N_5_BITS[n_tiny_bits as usize] < 64 + && n_tiny_bits == 0 + { + // The easy case: an integer that fits a long, less the digits below + // its precision. + let insignificant = if bin_exp > n_significant_bits { + insignificant_digits_for_pow2(bin_exp - n_significant_bits - 1) + } else { + 0 + }; + if bin_exp >= EXP_SHIFT { + fract_bits <<= bin_exp - EXP_SHIFT; + } else { + fract_bits = ((fract_bits as u64) >> (EXP_SHIFT - bin_exp)) as i64; + } + return develop_long_digits(0, fract_bits, insignificant); + } + + // The hard case: d = B / S * 10^dec_exp with 1 <= B / S < 10, and M half + // an ulp of d scaled as B is. + let mut dec_exp = estimate_dec_exp(fract_bits, bin_exp); + let b5 = (-dec_exp).max(0); + let mut b2 = b5 + n_tiny_bits + bin_exp; + let s5 = dec_exp.max(0); + let mut s2 = s5 + n_tiny_bits; + let m5 = b5; + let mut m2 = b2 - n_significant_bits; + fract_bits = ((fract_bits as u64) >> tail_zeros) as i64; + b2 -= n_fract_bits - 1; + let common2factor = b2.min(s2); + b2 -= common2factor; + s2 -= common2factor; + m2 -= common2factor; + if n_fract_bits == 1 { + m2 -= 1; + } + if m2 < 0 { + b2 -= m2; + s2 -= m2; + m2 = 0; + } + let bits_of_5 = |p5: i32| N_5_BITS.get(p5 as usize).copied().unwrap_or(p5 * 3); + let b_bits = n_fract_bits + b2 + bits_of_5(b5); + let ten_s_bits = s2 + 1 + bits_of_5(s5 + 1); + + let mut digits = Vec::with_capacity(20); + let (low, high, low_digit_difference); + if b_bits < 64 && ten_s_bits < 64 { + if b_bits < 32 && ten_s_bits < 32 { + // All ints. + let mut b = (fract_bits as i32) + .wrapping_mul(5i32.pow(b5 as u32)) + .wrapping_shl(b2 as u32); + let s = 5i32.pow(s5 as u32).wrapping_shl(s2 as u32); + let mut m = 5i32.pow(m5 as u32).wrapping_shl(m2 as u32); + let tens = s.wrapping_mul(10); + let mut q = b / s; + b = (b % s).wrapping_mul(10); + m = m.wrapping_mul(10); + let mut lo = b < m; + let mut hi = b.wrapping_add(m) > tens; + if q == 0 && !hi { + // The estimate was one too high: drop the leading zero. + dec_exp -= 1; + } else { + digits.push(q as u8); + } + // E-form prints at least one digit after the point. + if !(-3..8).contains(&dec_exp) { + hi = false; + lo = false; + } + while !lo && !hi { + q = b / s; + b = (b % s).wrapping_mul(10); + m = m.wrapping_mul(10); + if m > 0 { + lo = b < m; + hi = b.wrapping_add(m) > tens; + } else { + // m overflowed: Java takes both as reached. + lo = true; + hi = true; + } + digits.push(q as u8); + } + low_digit_difference = i64::from(b.wrapping_shl(1).wrapping_sub(tens)); + (low, high) = (lo, hi); + } else { + // All longs. + let mut b = fract_bits + .wrapping_mul(5i64.pow(b5 as u32)) + .wrapping_shl(b2 as u32); + let s = 5i64.pow(s5 as u32).wrapping_shl(s2 as u32); + let mut m = 5i64.pow(m5 as u32).wrapping_shl(m2 as u32); + let tens = s.wrapping_mul(10); + let mut q = b / s; + b = (b % s).wrapping_mul(10); + m = m.wrapping_mul(10); + let mut lo = b < m; + let mut hi = b.wrapping_add(m) > tens; + if q == 0 && !hi { + dec_exp -= 1; + } else { + digits.push(q as u8); + } + if !(-3..8).contains(&dec_exp) { + hi = false; + lo = false; + } + while !lo && !hi { + q = b / s; + b = (b % s).wrapping_mul(10); + m = m.wrapping_mul(10); + if m > 0 { + lo = b < m; + hi = b.wrapping_add(m) > tens; + } else { + lo = true; + hi = true; + } + digits.push(q as u8); + } + low_digit_difference = b.wrapping_shl(1).wrapping_sub(tens); + (low, high) = (lo, hi); + } + } else { + // FDBigInteger, exactly. Its normalizing shift scales B, S, M and 10S + // alike, which no quotient or comparison below can see; and here the + // test for reaching 10S is `>=`, where the int and long paths use `>`. + let pow52 = |p5: i32, p2: i32| U512::from(5u8).pow(U512::from(p5)) << (p2 as usize); + let s = pow52(s5, s2); + let mut b = U512::from(fract_bits as u64) * pow52(b5, b2); + let mut m = pow52(m5 + 1, m2 + 1); + let ten_s = pow52(s5 + 1, s2 + 1); + let ten = U512::from(10u8); + // `quoRemIteration`: the quotient, and B becomes 10 * (B mod S). + let mut q = (b / s).to::(); + b = (b % s) * ten; + let mut lo = b < m; + let mut hi = ten_s <= b + m; + if q == 0 && !hi { + dec_exp -= 1; + } else { + digits.push(q); + } + if !(-3..8).contains(&dec_exp) { + hi = false; + lo = false; + } + while !lo && !hi { + q = (b / s).to::(); + b = (b % s) * ten; + m *= ten; + lo = b < m; + hi = ten_s <= b + m; + digits.push(q); + } + low_digit_difference = if hi && lo { + match (b << 1usize).cmp(&ten_s) { + std::cmp::Ordering::Less => -1, + std::cmp::Ordering::Equal => 0, + std::cmp::Ordering::Greater => 1, + } + } else { + 0 + }; + (low, high) = (lo, hi); + } + + // The last digit is rounded by how the loop stopped. + let mut dec_exponent = dec_exp + 1; + if high { + if low { + if low_digit_difference == 0 { + // A tie: round to an even last digit. + if digits.last().is_some_and(|digit| digit & 1 != 0) { + roundup(&mut digits, &mut dec_exponent); + } + } else if low_digit_difference > 0 { + roundup(&mut digits, &mut dec_exponent); + } + } else { + roundup(&mut digits, &mut dec_exponent); + } + } + (digits, dec_exponent) +} + +/// `BinaryToASCIIBuffer.developLongDigits`: the digits of `lvalue`, less +/// `insignificant_digits` low ones rounded half up, trailing zeros dropped. +fn develop_long_digits( + mut dec_exponent: i32, + mut lvalue: i64, + insignificant_digits: i32, +) -> (Vec, i32) { + if insignificant_digits != 0 { + let pow10 = 5i64.pow(insignificant_digits as u32) << insignificant_digits; + let residue = lvalue % pow10; + lvalue /= pow10; + dec_exponent += insignificant_digits; + if residue >= pow10 >> 1 { + lvalue += 1; + } + } + let mut reversed = Vec::with_capacity(20); + let mut c = lvalue % 10; + lvalue /= 10; + // Java asserts lvalue > 0; the guard only keeps a zero from looping. + while c == 0 && lvalue != 0 { + dec_exponent += 1; + c = lvalue % 10; + lvalue /= 10; + } + while lvalue != 0 { + reversed.push(c as u8); + dec_exponent += 1; + c = lvalue % 10; + lvalue /= 10; + } + reversed.push(c as u8); + reversed.reverse(); + (reversed, dec_exponent + 1) +} + +/// `BinaryToASCIIBuffer.roundup`: add one to the last digit, carrying. +fn roundup(digits: &mut [u8], dec_exponent: &mut i32) { + let Some(mut i) = digits.len().checked_sub(1) else { + return; + }; + let mut q = digits[i]; + if q == 9 { + while q == 9 && i > 0 { + digits[i] = 0; + i -= 1; + q = digits[i]; + } + if q == 9 { + // Carried out: a 1 and zeros, one place up. + *dec_exponent += 1; + digits[0] = 1; + return; + } + } + digits[i] = q + 1; +} + +/// `BinaryToASCIIBuffer.estimateDecExp`: about floor(log10(d)). +/// +/// `0.301029995663981` is Java's literal, a different double from +/// `f64::consts::LOG10_2`, and the estimate has to round as Java's does. +#[allow(clippy::approx_constant)] +fn estimate_dec_exp(fract_bits: i64, bin_exp: i32) -> i32 { + let d2 = f64::from_bits((EXP_ONE | (fract_bits & SIGNIF_BIT_MASK)) as u64); + let d = (d2 - 1.5) * 0.289529654 + 0.176091259 + f64::from(bin_exp) * 0.301029995663981; + let d_bits = d.to_bits() as i64; + let exponent = ((d_bits & EXP_BIT_MASK) >> EXP_SHIFT) as i32 - 1023; + let is_negative = (d_bits & SIGN_BIT_MASK) != 0; + if (0..52).contains(&exponent) { + let mask = SIGNIF_BIT_MASK >> exponent; + let r = (((d_bits & SIGNIF_BIT_MASK) | FRACT_HOB) >> (EXP_SHIFT - exponent)) as i32; + if !is_negative { + r + } else if mask & d_bits == 0 { + -r + } else { + -r - 1 + } + } else if exponent < 0 { + if d_bits & !SIGN_BIT_MASK == 0 || !is_negative { + 0 + } else { + -1 + } + } else { + d as i32 + } +} + +/// `BinaryToASCIIBuffer.insignificantDigitsForPow2`. +fn insignificant_digits_for_pow2(p2: i32) -> i32 { + if p2 > 1 && (p2 as usize) < INSIGNIFICANT_DIGITS_NUMBER.len() { + INSIGNIFICANT_DIGITS_NUMBER[p2 as usize] + } else { + 0 + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn to_big_integer(d: f64) -> u128 { + big_decimal_value_of_to_big_integer(d).unwrap().to::() + } + + /// The digits as `Double.toString` would print them, read back. + fn read_back(d: f64) -> f64 { + let (digits, exponent) = double_to_string_digits(d); + let digits: String = digits + .iter() + .map(|digit| char::from(b'0' + digit)) + .collect(); + format!("0.{digits}e{exponent}").parse().unwrap() + } + + /// `BigInteger.doubleValue` rounds to nearest, ties to even, below 2^64 + /// and above it. + #[test] + fn a_big_integer_becomes_the_nearest_double() { + let two_64 = U256::from(1u8) << 64; + assert_eq!( + big_integer_double_value(&U256::from(59_240_000u64)), + 59_240_000.0 + ); + assert_eq!( + big_integer_double_value(&U256::from(u64::MAX)), + 18446744073709551616.0 + ); + // A tie between 2^64 and 2^64 + 4096 goes to the even one ... + let tie = two_64 + U256::from(2048u64); + assert_eq!(big_integer_double_value(&tie), 18446744073709551616.0); + // ... and one past it, to the nearer. + let past = tie + U256::from(1u8); + assert_eq!(big_integer_double_value(&past), 18446744073709555712.0); + let below = tie - U256::from(1u8); + assert_eq!(big_integer_double_value(&below), 18446744073709551616.0); + assert_eq!(big_integer_double_value(&U256::MAX), 2f64.powi(256)); + } + + /// Java 17's digits: an integer below 2^58 prints whole (where JDK 19's + /// shortest digits would round it: 2.82879384806159008E17 is JDK 17's + /// output, 2.82879384806159E17 JDK 19's); from 2^58 one digit, from 2^61 + /// two, are dropped rounding half up (`insignificantDigitsForPow2`). + #[test] + fn an_integer_double_keeps_java_17s_digits() { + assert_eq!(to_big_integer(2.82879384806159e17), 282_879_384_806_159_008); + assert_eq!( + to_big_integer(143194535060849088.0), + 143_194_535_060_849_088 + ); + assert_eq!( + to_big_integer(410705390902863872.0), + 410_705_390_902_863_870 + ); + assert_eq!( + to_big_integer(2269733182554162688.0), + 2_269_733_182_554_162_690 + ); + assert_eq!( + to_big_integer(8218640729469351936.0), + 8_218_640_729_469_351_900 + ); + assert_eq!(double_to_string_digits(1.0e7), (vec![1], 8)); + assert_eq!(to_big_integer(65164000.0), 65_164_000); + // Past a long, the hard case's digits as JDK 17 prints them -- the + // outputs JDK-4511638 (fixed in JDK 19) gives for these, where the + // shortest digits are 8.41E21, 2.0E23 and 1.0E23. + let printed = |d: f64| { + let (digits, exponent) = double_to_string_digits(d); + let digits: String = digits + .iter() + .map(|digit| char::from(b'0' + digit)) + .collect(); + format!("{}.{}E{}", &digits[..1], &digits[1..], exponent - 1) + }; + assert_eq!(printed(8.41e21), "8.409999999999999E21"); + assert_eq!(printed(2.0e23), "1.9999999999999998E23"); + assert_eq!(printed(1.0e23), "9.999999999999999E22"); + assert_eq!(to_big_integer(8.41e21), 8_409_999_999_999_999_000_000); + } + + /// A fraction is dropped, and the digits round-trip: what Java 17 prints + /// for a double reads back as that double. + #[test] + fn a_doubles_digits_read_back_as_it() { + assert_eq!(to_big_integer(59_240_000.0 * 1.1), 65_164_000); + assert_eq!(to_big_integer(0.5), 0); + assert_eq!(to_big_integer(0.0), 0); + assert_eq!(to_big_integer(-0.0), 0); + assert_eq!(big_decimal_value_of_to_big_integer(f64::INFINITY), None); + assert_eq!(big_decimal_value_of_to_big_integer(f64::NAN), None); + let mut x: u64 = 0x2545_f491_4f6c_dd1d; + for _ in 0..20_000 { + // xorshift: minimums spread over every size to 2^256. + x ^= x << 13; + x ^= x >> 7; + x ^= x << 17; + let minimum = U256::from(x) << (x % 200) as usize; + let d = big_integer_double_value(&minimum) * 1.1; + assert_eq!(read_back(d), d, "{d:e}"); + if d < 2f64.powi(53) { + assert_eq!(to_big_integer(d), d.trunc() as u128, "{d:e}"); + } + let small = (x >> 11) as f64 * 1.1; + assert_eq!(read_back(small), small, "{small:e}"); + assert_eq!(to_big_integer(small), small.trunc() as u128, "{small:e}"); + } + } +} diff --git a/crates/rpc/src/lib.rs b/crates/rpc/src/lib.rs index 866212d..184ad12 100644 --- a/crates/rpc/src/lib.rs +++ b/crates/rpc/src/lib.rs @@ -3,6 +3,7 @@ pub mod debug; pub mod dto; pub mod eth; pub mod helpers; +pub mod java; pub mod evm; pub mod logs; pub mod mnr; diff --git a/crates/rpc/src/tests.rs b/crates/rpc/src/tests.rs index 43cf0b4..c18a310 100644 --- a/crates/rpc/src/tests.rs +++ b/crates/rpc/src/tests.rs @@ -3725,9 +3725,8 @@ async fn test_eth_gas_price_ignores_the_tracker_without_a_fee_market() { /// walked down to 587 wei; rskj answered 645 and this node answered the /// tracker's 26,065,600. 587 x 1.1 is 645.7, and rskj truncates it through /// `BigDecimal.toBigInteger()`, so 645 is the answer and 646 would be wrong. -/// The exact ratio this node uses truncates identically here -- see -/// `GAS_PRICE_MULTIPLIER_NUM` for where the two forms part company, which is -/// nowhere a real chain reaches. +/// The exact ratio this node used to compute truncates identically here; see +/// `eth::head_minimum_times_multiplier` for where the two part company. #[tokio::test] async fn test_eth_gas_price_truncates_the_multiplier_like_rskj() { let (mut state, tmp) = setup_state(); @@ -3754,6 +3753,70 @@ async fn test_eth_gas_price_falls_back_to_the_block_minimum() { assert_eq!(r.result.unwrap(), json!("0x3e252e0"), "59,240,000 x 1.1"); } +/// rskj's arithmetic, not an exact 1.1 (task 280): the minimum as a double, +/// times 1.1 in doubles, read back through Java 17's `Double.toString`; and +/// either answer printed as its low 64 bits (`Long.toHexString(longValue())`). +#[tokio::test] +async fn test_eth_gas_price_multiplies_in_doubles_like_rskj() { + let (mut state, _tmp) = setup_state(); + let mut header = state + .store + .header(state.store.head().unwrap().unwrap()) + .unwrap() + .unwrap(); + let huge = U256::from(1u8) << 80; + let huge_price = crate::java::big_decimal_value_of_to_big_integer( + crate::java::big_integer_double_value(&huge) * 1.1, + ) + .unwrap(); + for (minimum, price) in [ + // x * 11 / 10 is ...315; the double product rounds up to ...316. + (U256::from(511_772_684_928_469u64), 562_949_953_421_316u64), + // An integer double under 2^58 prints whole on Java 17 (...090 on 19). + ( + U256::from(130_176_850_055_317_349u64), + 143_194_535_060_849_088, + ), + // From 2^61 Java 17 drops two digits, rounding half up. + ( + U256::from(7_471_491_572_244_865_272u64), + 8_218_640_729_469_351_900, + ), + // Past a long, the low 64 bits. + (huge, huge_price.as_limbs()[0]), + ] { + header.minimum_gas_price = minimum; + header.cached_hash = None; + let hash = header.hash(); + state.store.put_header(&header).unwrap(); + state.store.put_canonical_hash(header.number, hash).unwrap(); + state.store.set_head(hash).unwrap(); + let r = dispatch_for_test(&state, make_request("eth_gasPrice", json!([]))).await; + assert_eq!(r.result.unwrap(), json!(format!("{price:#x}")), "{minimum}"); + } + assert!( + huge_price > alloy_primitives::U512::from(u64::MAX), + "{huge_price}" + ); + + struct HugeGasPrice; + impl crate::server::GasPriceSource for HugeGasPrice { + fn gas_price(&self) -> U256 { + (U256::from(1u8) << 64) + U256::from(5u8) + } + fn is_fee_market_working(&self) -> bool { + true + } + } + state.gas_price = Some(Arc::new(HugeGasPrice)); + let r = dispatch_for_test(&state, make_request("eth_gasPrice", json!([]))).await; + assert_eq!( + r.result.unwrap(), + json!("0x5"), + "2^64 + 5 prints as its low 64 bits" + ); +} + // ========== trace_* namespace ========== /// A parent block holding a funded sender and two contracts, and a child From f3f58a5d1ac91c7d4c5a471f4d9e0546916adbe4 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Tue, 10 Nov 2026 07:59:29 -0800 Subject: [PATCH 087/264] execution: the Bridge's and REMASC's transfers as CALL subtraces rskj's BridgeSupport.transferTo and RemascFeesPayer.transferPayment add a CALL subtrace for every transfer, which a transaction sent straight to the precompile carries in its trace. Recorded for traces only. --- crates/execution/src/bridge/peg.rs | 9 + crates/execution/src/bridge/union.rs | 2 + crates/execution/src/executor.rs | 259 +++++++++++++++++++++++++-- crates/execution/src/raw_storage.rs | 7 + crates/execution/src/remasc.rs | 3 + crates/execution/src/tracer.rs | 55 ++++++ 6 files changed, 325 insertions(+), 10 deletions(-) diff --git a/crates/execution/src/bridge/peg.rs b/crates/execution/src/bridge/peg.rs index 2912a46..60bf3db 100644 --- a/crates/execution/src/bridge/peg.rs +++ b/crates/execution/src/bridge/peg.rs @@ -881,6 +881,7 @@ pub fn register_btc_transaction( let _ = ctx.journal_mut().load_account(dest); ctx.journal_mut().touch_account(dest); } + crate::tracer::record_precompile_transfer(ctx, BRIDGE_ADDR, dest, rbtc_amount); // RSKIP170 is required for v1, so pegin_btc (protocolVersion = 1). super::events::log_pegin_btc(ctx, dest, &btc_txid_event_bytes(&btc_tx), total_value, 1); record_pegin( @@ -1099,6 +1100,7 @@ pub fn register_btc_transaction( let _ = ctx.journal_mut().load_account(dest); ctx.journal_mut().touch_account(dest); } + crate::tracer::record_precompile_transfer(ctx, BRIDGE_ADDR, dest, rbtc_amount); // Peg-in events: lock_btc from RSKIP146, pegin_btc from RSKIP170 // (a legacy peg-in past RSKIP170 logs pegin_btc with protocolVersion 0). @@ -2013,6 +2015,7 @@ pub fn register_fast_bridge_btc_transaction( if !rbtc_amount.is_zero() { let _ = ctx.journal_mut().transfer(BRIDGE_ADDR, lbc_address, rbtc_amount); } + crate::tracer::record_precompile_transfer(ctx, BRIDGE_ADDR, lbc_address, rbtc_amount); // saveFlyoverActiveFederationDataInStorage: // - mark the flyover hash used (keyed by the legacy txid) @@ -2973,6 +2976,12 @@ pub fn update_collections( BURN_ADDR, btc_satoshi_to_rbtc_wei(to_burn), ); + crate::tracer::record_precompile_transfer( + ctx, + BRIDGE_ADDR, + BURN_ADDR, + btc_satoshi_to_rbtc_wei(to_burn), + ); } } }; diff --git a/crates/execution/src/bridge/union.rs b/crates/execution/src/bridge/union.rs index e3dafa2..3317cc1 100644 --- a/crates/execution/src/bridge/union.rs +++ b/crates/execution/src/bridge/union.rs @@ -536,6 +536,8 @@ fn transfer_from_bridge( ctx.journal_mut() .transfer(crate::precompiles::BRIDGE_ADDR, to, amount) .map_err(|e| PrecompileError::other(format!("union bridge transfer: {e:?}")))?; + // rskj `BridgeSupport.transferTo` reports it as a CALL subtrace. + crate::tracer::record_precompile_transfer(ctx, crate::precompiles::BRIDGE_ADDR, to, amount); Ok(()) } diff --git a/crates/execution/src/executor.rs b/crates/execution/src/executor.rs index 2ce3f66..b3ab50d 100644 --- a/crates/execution/src/executor.rs +++ b/crates/execution/src/executor.rs @@ -622,6 +622,14 @@ impl RskExecutor { // reverted transaction looks like. let mut trace = evm.inspector.take_trace(); outcome.apply_to(&mut trace); + let transfers = { + use revm::context_interface::ContextTr; + std::mem::take(&mut evm.ctx.chain_mut().precompile_transfers) + }; + let to = Address::try_from(tx.to.as_ref()).ok(); + if let Some(subtraces) = crate::tracer::precompile_subtraces(to, success, transfers) { + trace.subtraces = subtraces; + } Ok(( TxExecutionResult { @@ -1636,15 +1644,6 @@ impl RskExecutor { if is_remasc { debug!(tx_index = i, "executing REMASC system call"); - if traced.includes(i) { - // REMASC never enters the EVM here, so the tracer records - // nothing -- but rskj still produces a trace for it - // (`TransactionExecutor.extractTrace`'s `program == null` - // branch), and `trace_block` would otherwise be short one - // entry per block. An empty trace renders as the - // zero-gas, no-subtrace `call` rskj emits. - traces.insert(i, crate::tracer::ProgramTrace::default()); - } // rskj `TransactionListExecutor.addFeesToRemascIfEnabled`: // with the fees postponed, the block's whole total is // credited to REMASC immediately BEFORE the REMASC @@ -1683,6 +1682,28 @@ impl RskExecutor { Some(&self.block_store), ) .map_err(|e| ExecutionError::Evm(format!("REMASC: {e:?}")))?; + let transfers = { + use revm::context_interface::ContextTr; + std::mem::take(&mut evm.ctx.chain_mut().precompile_transfers) + }; + if traced.includes(i) { + // REMASC never enters the EVM here, so the tracer records + // nothing -- but rskj still produces a trace for it + // (`TransactionExecutor.extractTrace`'s `program == null` + // branch), and `trace_block` would otherwise be short one + // entry per block: the zero-gas `call` rskj emits, with a + // CALL subtrace for each payment REMASC made + // (`RemascFeesPayer.transferPayment`). + let subtraces = + crate::tracer::precompile_subtraces(Some(REMASC_ADDR), true, transfers); + traces.insert( + i, + crate::tracer::ProgramTrace { + subtraces: subtraces.unwrap_or_default(), + ..Default::default() + }, + ); + } { use revm::context_interface::ContextTr; // rskj `RemascStorageProvider.saveSiblings` runs on every @@ -2112,12 +2133,23 @@ impl RskExecutor { let success = result.is_success(); let gas_used = result.gas_used(); let output = result.output().map(|o| o.to_vec()).unwrap_or_default(); + // Every transaction's, traced or not, so that one's can't reach the + // next transaction's trace. + let transfers = { + use revm::context_interface::ContextTr; + std::mem::take(&mut evm.ctx.chain_mut().precompile_transfers) + }; if traced.includes(i) { // Taken here, not after the block: `take_trace` resets the // tracer so the next transaction starts with an empty tree, // and `result` is about to be consumed by `into_logs`. let mut trace = evm.inspector.take_trace(); TracedOutcome::of(tx, *sender, &result, &output).apply_to(&mut trace); + let to = Address::try_from(tx.to.as_ref()).ok(); + if let Some(subtraces) = crate::tracer::precompile_subtraces(to, success, transfers) + { + trace.subtraces = subtraces; + } traces.insert(i, trace); } let logs = result.into_logs(); @@ -12820,6 +12852,89 @@ bf09f6e52420834e8e0e0b1a6df563aba550cf7f99e9724264187c45dcf3d73e8585a0e35196\ .and_then(|data| AccountState::decode(&data).ok()) } + /// REMASC's payments are CALL subtraces of the REMASC transaction's trace + /// (task 283): rskj's `RemascFeesPayer.payMiningFees` makes one in + /// `transferPayment` for every payment it logs, from REMASC with gas 0, + /// to the payee's word -- zero for the null address -- and + /// `TransactionExecutor.call` keeps them for `extractTrace`. + #[test] + fn remasc_payments_are_call_subtraces_of_its_trace() { + use alloy_rlp::Decodable; + let miner = Address::repeat_byte(0x11); + let fees = U256::from(21_000); + let cases: [(fn(Header) -> Header, Address); 2] = + [(|block| block, miner), (with_null_coinbase, Address::ZERO)]; + for (coinbase, payee) in cases { + let chain: Vec<(Header, Vec
)> = (0..15) + .map(|number| { + let mut block = dummy_header(number); + if number == 5 { + block.beneficiary = miner; + block.paid_fees = fees; + block = coinbase(block); + } + (block, vec![]) + }) + .collect(); + let blocks = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let mut parent_hash = B256::ZERO; + for (number, (block, _)) in chain.iter().enumerate() { + let mut block = block.clone(); + block.parent_hash = parent_hash; + parent_hash = block.hash(); + blocks.put_header_with_hash(parent_hash, &block).unwrap(); + blocks + .put_canonical_hash(number as u64, parent_hash) + .unwrap(); + } + let store = Arc::new(MemoryTrieStore::new()); + let sender = Address::repeat_byte(0xAA); + let root = put_account(&TrieNode::empty(), store.as_ref(), &REMASC_ADDR, 0, fees); + let header = dummy_header(chain.len() as u64); + let remasc = rustock_core::Transaction::remasc(header.number); + let (result, traces) = RskExecutor::new(RskHardforkConfig::all_active(33), blocks) + .with_remasc_config(crate::remasc::RemascConfig::regtest()) + .with_bridge_constants(crate::bridge::constants::BridgeConstants::mainnet()) + .execute_block_call_traced(&header, &[(remasc, sender)], &root, store.clone()) + .unwrap(); + + let topic = crate::bridge::events::legacy_topic("mining_fee_topic"); + let paid: Vec<_> = result.tx_results[0] + .logs + .iter() + .filter(|log| log.topics()[0] == topic) + .map(|log| { + let mut data = &log.data.data[..]; + alloy_rlp::Header::decode(&mut data).unwrap(); + B256::decode(&mut data).unwrap(); + ( + Address::from_word(log.topics()[1]), + U256::decode(&mut data).unwrap(), + ) + }) + .collect(); + assert!(paid.contains(&(payee, U256::from(3_327))), "{paid:?}"); + let call = Some(crate::tracer::CallKind::Call); + let subtraces: Vec<_> = traces[0] + .subtraces + .iter() + .map(|s| { + let shape = ( + s.call_kind, + s.caller, + s.gas, + s.gas_used, + &s.input, + &s.output, + ); + assert_eq!(shape, (call, REMASC_ADDR, 0, 0, &vec![], &vec![])); + (s.owner, s.value) + }) + .collect(); + assert_eq!(subtraces, paid, "one CALL per payment, in order"); + } + } + /// rskj pays an empty coinbase at its null address, an account of no /// address bytes, what it would pay any coinbase: the same sums, the same /// log but for a payee word of zero, and an account at its own trie key. @@ -13075,7 +13190,7 @@ bf09f6e52420834e8e0e0b1a6df563aba550cf7f99e9724264187c45dcf3d73e8585a0e35196\ let header = dummy_header(3_103_055); let result = executor - .execute_block(&header, &[(tx, sender)], &root, store.clone()) + .execute_block(&header, &[(tx.clone(), sender)], &root, store.clone()) .expect("block executes"); assert!(result.tx_results[0].success, "updateCollections succeeds"); @@ -13095,6 +13210,130 @@ bf09f6e52420834e8e0e0b1a6df563aba550cf7f99e9724264187c45dcf3d73e8585a0e35196\ .map(|a| a.info.balance) .unwrap_or_default(); assert_eq!(bridge_balance, one_rbtc - burned, "burn debits the Bridge"); + + // rskj's `transferTo(BURN_ADDRESS, ...)` is a CALL subtrace of the + // transaction's trace, from the Bridge with gas 0 (task 283). + let (_, traces) = executor + .execute_block_call_traced(&header, &[(tx, sender)], &root, store.clone()) + .expect("block executes traced"); + let subtraces: Vec<_> = traces[0] + .subtraces + .iter() + .map(|s| { + ( + s.call_kind, + s.caller, + s.owner, + s.value, + s.gas, + s.gas_used, + s.input.len(), + ) + }) + .collect(); + let call = Some(crate::tracer::CallKind::Call); + let bridge = crate::precompiles::BRIDGE_ADDR; + assert_eq!(subtraces, vec![(call, bridge, burn_addr, burned, 0, 0, 0)]); + } + + /// A transaction sent straight to the Bridge carries the Bridge's + /// transfers as CALL subtraces (task 283): rskj's `BridgeSupport.transferTo` + /// adds one, `TransactionExecutor.call` keeps them once `execute` returns + /// and `extractTrace` hangs them under the transaction's trace. Here the + /// refund of what a sender other than the union bridge sent + /// `releaseUnionBridgeRbtc()` (`BridgeSupport.releaseUnionBridgeRbtc`). The + /// same call from a contract carries none: `callToPrecompiledAddress` + /// reads no subtraces. + #[test] + fn a_bridge_transfer_is_a_call_subtrace_of_a_transaction_to_the_bridge() { + let store = Arc::new(MemoryTrieStore::new()); + let bridge = crate::precompiles::BRIDGE_ADDR; + let sender = Address::repeat_byte(0x5e); + let contract = Address::repeat_byte(0xca); + let one_rbtc = U256::from(10u64).pow(U256::from(18)); + let selector = alloy_primitives::keccak256(b"releaseUnionBridgeRbtc()")[..4].to_vec(); + // MSTORE the selector at 0; CALL(100000, Bridge, 5, 0, 4, 0, 0); STOP. + let mut code = vec![0x7f]; + code.extend_from_slice(&selector); + code.extend_from_slice(&[0; 28]); + code.extend_from_slice(&[0x60, 0x00, 0x52, 0x60, 0x00, 0x60, 0x00, 0x60, 0x04]); + code.extend_from_slice(&[0x60, 0x00, 0x60, 0x05, 0x73]); + code.extend_from_slice(bridge.as_slice()); + code.extend_from_slice(&[0x62, 0x01, 0x86, 0xa0, 0xf1, 0x00]); + let root = put_account(&TrieNode::empty(), store.as_ref(), &sender, 0, one_rbtc); + let root = put_account_with_code(&root, store.as_ref(), &contract, 0, one_rbtc, &code); + let executor = RskExecutor::new( + // reed810, for RSKIP502's union bridge methods. + RskHardforkConfig::all_active_parallel(33), + Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()), + ); + let tx = |nonce: u64, to: Address, input: &[u8]| rustock_core::Transaction { + nonce, + gas_price: U256::ZERO, + gas_limit: U256::from(300_000), + to: Bytes::copy_from_slice(to.as_slice()), + value: U256::from(5), + input: Bytes::copy_from_slice(input), + v: 0, + r: U256::ZERO, + s: U256::ZERO, + cached_rlp: None, + }; + let subtraces = |trace: &crate::tracer::ProgramTrace| { + trace + .subtraces + .iter() + .map(|s| (s.call_kind, s.caller, s.owner, s.value, s.gas, s.gas_used)) + .collect::>() + }; + let call = Some(crate::tracer::CallKind::Call); + let refund = vec![(call, bridge, sender, U256::from(5), 0, 0)]; + + // As `trace_*` runs a block. The contract's call comes first, and what + // the Bridge transferred in it stays out of the next one's trace. + let header = dummy_header(1_000); + let transactions = [ + (tx(0, contract, &[]), sender), + (tx(1, bridge, &selector), sender), + ]; + let (result, traces) = executor + .execute_block_call_traced(&header, &transactions, &root, store.clone()) + .expect("the block executes"); + assert!(result.tx_results.iter().all(|r| r.success)); + assert_eq!( + subtraces(&traces[0]), + vec![], + "nothing from a contract's call" + ); + assert_eq!( + subtraces(&traces[1]), + refund, + "the refund, under its own trace" + ); + + // And as one transaction is traced. + let (one, trace) = executor + .execute_tx_traced( + &header, + &tx(0, bridge, &selector), + sender, + &root, + store.clone(), + false, + 0, + ) + .expect("the transaction executes"); + assert!(one.success); + assert_eq!(subtraces(&trace), refund); + + // Only a successful call straight to a precompile that transfers. + let transfers = || vec![(bridge, sender, U256::from(5))]; + let attached = |to, success| crate::tracer::precompile_subtraces(to, success, transfers()); + assert!(attached(Some(bridge), true).is_some()); + assert!(attached(Some(crate::precompiles::REMASC_ADDR), true).is_some()); + assert!(attached(Some(bridge), false).is_none()); + assert!(attached(Some(contract), true).is_none()); + assert!(attached(None, true).is_none()); } /// Deploying onto an address that already holds a balance destroys that diff --git a/crates/execution/src/raw_storage.rs b/crates/execution/src/raw_storage.rs index ff6cd5f..4a793ac 100644 --- a/crates/execution/src/raw_storage.rs +++ b/crates/execution/src/raw_storage.rs @@ -46,6 +46,13 @@ pub struct RskChainExt { /// has no bytes, so revm can't hold its account, and the trie is /// credited at the block's end instead (`ContractMarkers::null_account`). pub null_account: Option, + /// The value transfers rskj's Bridge and REMASC each report as a CALL + /// subtrace (`BridgeSupport.transferTo`, `RemascFeesPayer.transferPayment`), + /// in order: from, to, value. Trace-only: the executor takes it after every + /// transaction and hangs it under the transaction's trace when the + /// transaction called the precompile itself + /// (`crate::tracer::record_precompile_transfer`). + pub precompile_transfers: Vec<(Address, Address, U256)>, } /// Raw-bytes storage overlay for the block being executed. diff --git a/crates/execution/src/remasc.rs b/crates/execution/src/remasc.rs index 448ee89..a75637a 100644 --- a/crates/execution/src/remasc.rs +++ b/crates/execution/src/remasc.rs @@ -425,6 +425,9 @@ fn pay_mining_fees( } None => pay_null_address(ctx, value), } + // rskj `transferPayment` reports the payment as a CALL subtrace to the + // payee, whose word is zero for the null address. + crate::tracer::record_precompile_transfer(ctx, REMASC_ADDR, to.unwrap_or(Address::ZERO), value); let mut inner = Vec::with_capacity(40); processing_block_hash.as_slice().encode(&mut inner); diff --git a/crates/execution/src/tracer.rs b/crates/execution/src/tracer.rs index 688e262..6ccfa86 100644 --- a/crates/execution/src/tracer.rs +++ b/crates/execution/src/tracer.rs @@ -195,6 +195,61 @@ pub struct Subtrace { pub subtraces: Vec, } +/// Notes a value transfer that rskj reports as a CALL subtrace of a call to +/// a precompile: `BridgeSupport.transferTo` and +/// `RemascFeesPayer.transferPayment` add one for every transfer they make, a +/// zero one included. +pub fn record_precompile_transfer( + ctx: &mut CTX, + from: Address, + to: Address, + value: U256, +) { + ctx.chain_mut().precompile_transfers.push((from, to, value)); +} + +/// The subtraces rskj hangs under a transaction sent straight to a +/// precompile, if `success` and `to` is one that transfers: one CALL per +/// transfer it made, from the precompile to the payee, with gas 0 and an empty +/// result (`TransferInvoke(from, to, 0L, value)`, +/// `ProgramSubtrace.newCallSubtrace`). `TransactionExecutor.call` takes +/// `precompiledContract.getSubtraces()` once `execute` returns and +/// `extractTrace` hangs them under the transaction's trace; a precompile a +/// contract calls adds nothing (`Program.callToPrecompiledAddress` reads no +/// subtraces), so its transfers are dropped. +pub fn precompile_subtraces( + to: Option
, + success: bool, + transfers: Vec<(Address, Address, U256)>, +) -> Option> { + let to = to?; + if !success || (to != crate::BRIDGE_ADDR && to != crate::REMASC_ADDR) { + return None; + } + Some( + transfers + .into_iter() + .map(|(from, to, value)| Subtrace { + kind: SubtraceKind::Call, + call_kind: Some(CallKind::Call), + is_create2: false, + caller: from, + owner: to, + code_address: None, + gas: 0, + gas_used: 0, + value, + input: Vec::new(), + output: Vec::new(), + created_address: None, + error: None, + reverted: false, + subtraces: Vec::new(), + }) + .collect(), + ) +} + /// A frame the tracer has entered and not yet left. struct OpenFrame { kind: SubtraceKind, From 2bffcc11e0103d01d2090c67e05f00de51d6fee0 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Tue, 10 Nov 2026 07:59:29 -0800 Subject: [PATCH 088/264] rpc: a block's fields as rskj's BlockResultDTO prints them mixHash, nonce, the merged-mining fields, hashForMergedMining, paidFees, cumulativeDifficulty, rskPteEdges and baseEvent, in rskj's order, and an uncle's roots and hashes from the bytes it kept. --- crates/rpc/src/helpers.rs | 149 +++++++++++++++++++++--- crates/rpc/src/tests.rs | 231 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 365 insertions(+), 15 deletions(-) diff --git a/crates/rpc/src/helpers.rs b/crates/rpc/src/helpers.rs index b456e17..8a1aae8 100644 --- a/crates/rpc/src/helpers.rs +++ b/crates/rpc/src/helpers.rs @@ -265,30 +265,55 @@ pub fn parse_hex_u32(s: &str) -> Option { u32::from_str_radix(s, 16).ok() } -/// Block result DTO matching rskj's `BlockResultDTO` JSON format. +/// Block result DTO matching rskj's `BlockResultDTO` JSON format: a property +/// for each of its getters (`BlockResultDTO.java` L218-333), filled as +/// `fromBlock` fills them (L137-197), for a block and for an uncle +/// (`Web3Impl.getUncleResultDTO`). rskj's `ObjectMapper` writes a null +/// property as `null`, as serde writes a `None` here. +/// +/// Not ported: `fromBlock` prints `number`, `hash`, `logsBloom` and `miner` +/// as null for a block other than genesis without a merged-mining header, +/// the pending block rskj builds. A block a node stores has one, merged +/// mining's rule sees to it, and rustock answers `pending` with its best +/// block. #[derive(Debug, Serialize)] #[serde(rename_all = "camelCase")] pub struct BlockResultDto { pub number: String, pub hash: String, pub parent_hash: String, + /// `Keccak256.ZERO_HASH`, there for compatibility with Ethereum. + pub mix_hash: String, + /// `0x0000000000000000`, hardcoded for compatibility with Ethereum. + pub nonce: String, #[serde(rename = "sha3Uncles")] pub sha3_uncles: String, - pub miner: String, - pub state_root: String, + pub logs_bloom: String, pub transactions_root: String, + pub state_root: String, pub receipts_root: String, - pub logs_bloom: String, + pub miner: String, pub difficulty: String, pub total_difficulty: String, + pub extra_data: String, + pub size: String, pub gas_limit: String, pub gas_used: String, pub timestamp: String, - pub extra_data: String, - pub minimum_gas_price: String, pub transactions: Vec, pub uncles: Vec, - pub size: String, + pub minimum_gas_price: String, + pub bitcoin_merged_mining_header: String, + pub bitcoin_merged_mining_coinbase_transaction: String, + pub bitcoin_merged_mining_merkle_proof: String, + pub hash_for_merged_mining: String, + pub paid_fees: String, + /// The block's difficulty and its uncles' (`Block.getCumulativeDifficulty`). + pub cumulative_difficulty: String, + /// RSKIP-144's sublist edges, a `short[]`, or null. + pub rsk_pte_edges: Option>, + /// RSKIP-535's base event, or null where it is empty. + pub base_event: Option, } impl BlockResultDto { @@ -332,34 +357,128 @@ impl BlockResultDto { vec![] }; + // rskj prints a hash or root from the bytes it holds + // (`toUnformattedJsonHex`): an uncle's of another length as it kept + // them, where the typed field holds zero. + let kept = header.kept(); + let held = |bytes: Option<&alloy_primitives::Bytes>, typed: &B256| { + bytes.map_or_else(|| to_hex_b256(typed), |bytes| to_hex_bytes(bytes)) + }; + // `toUnformattedJsonHex` of a merged-mining field: `0x` for none. + let merged_mining = |field: &Option| { + to_hex_bytes(field.as_ref().map_or(&[][..], |bytes| bytes.as_ref())) + }; + let version_fields = header.version_fields.as_ref(); + Self { number: to_hex_u64(header.number), hash: to_hex_b256(&hash), parent_hash: to_hex_b256(&header.parent_hash), - sha3_uncles: to_hex_b256(&header.ommers_hash), + mix_hash: to_hex_b256(&B256::ZERO), + nonce: "0x0000000000000000".to_owned(), + sha3_uncles: held( + kept.and_then(|kept| kept.ommers_hash.as_ref()), + &header.ommers_hash, + ), + logs_bloom: match kept.and_then(|kept| kept.logs_bloom.as_ref()) { + Some(bloom) => to_hex_bytes(bloom), + None => to_hex_bytes(header.logs_bloom.as_ref()), + }, + transactions_root: held( + kept.and_then(|kept| kept.transactions_root.as_ref()), + &header.transactions_root, + ), + state_root: held( + kept.and_then(|kept| kept.state_root.as_ref()), + &header.state_root, + ), + receipts_root: held( + kept.and_then(|kept| kept.receipts_root.as_ref()), + &header.receipts_root, + ), // `toUnformattedJsonHex(getCoinbase().getBytes())`: `0x` for the // null address an empty coinbase reads as. miner: header .coinbase() .map_or_else(|| "0x".to_owned(), |coinbase| format!("{coinbase:#x}")), - state_root: to_hex_b256(&header.state_root), - transactions_root: to_hex_b256(&header.transactions_root), - receipts_root: to_hex_b256(&header.receipts_root), - logs_bloom: to_hex_bytes(header.logs_bloom.as_ref()), difficulty: to_hex_u256(&header.difficulty), total_difficulty: to_hex_u256(&total_difficulty), + extra_data: to_hex_bytes(&header.extra_data), + size: to_hex_u64(size), gas_limit: to_hex_quantity_bytes(&header.gas_limit_bytes()), gas_used: to_hex_u64(header.gas_used), timestamp: to_hex_u64(header.timestamp), - extra_data: to_hex_bytes(&header.extra_data), - minimum_gas_price: to_hex_quantity_bytes(&header.minimum_gas_price_bytes()), transactions, uncles, - size: to_hex_u64(size), + minimum_gas_price: to_hex_quantity_bytes(&header.minimum_gas_price_bytes()), + bitcoin_merged_mining_header: merged_mining(&header.bitcoin_merged_mining_header), + bitcoin_merged_mining_coinbase_transaction: merged_mining( + &header.bitcoin_merged_mining_coinbase_transaction, + ), + bitcoin_merged_mining_merkle_proof: merged_mining( + &header.bitcoin_merged_mining_merkle_proof, + ), + hash_for_merged_mining: to_hex_b256(&hash_for_merged_mining(header)), + // `toQuantityJsonHex(Coin.getBytes())`; an uncle's past 256 bits + // from the bytes it kept. + paid_fees: match kept.and_then(|kept| kept.paid_fees.as_ref()) { + Some(fees) => to_hex_quantity_bytes(fees), + None => to_hex_u256(&header.paid_fees), + }, + cumulative_difficulty: format!("{:#x}", cumulative_difficulty(header, body)), + rsk_pte_edges: version_fields.and_then(|fields| fields.edges.clone()), + // `baseEvent != null && baseEvent.length > 0 ? ... : null`. + base_event: version_fields + .and_then(|fields| fields.base_event.as_ref()) + .filter(|event| !event.is_empty()) + .map(|event| to_hex_bytes(event)), } } } +/// rskj `BlockHeader.getHashForMergedMining` (L537-554): the merged-mining +/// hash, its last 12 bytes the fork-detection data the coinbase commits to +/// where the header includes it -- from RSKIP110 (wasabi100), never below +/// block 449 (`BlockFactory` L257-258) -- as `getMiningForkDetectionData` +/// reads it out of the coinbase (L578-616). The heights are this process's, +/// which its headers hash under. +/// +/// Where rskj throws, finding no data in a header that should carry it, +/// this is the hash without it: merged mining's rule refuses such a header, +/// so no block or uncle the node stores is one. +fn hash_for_merged_mining(header: &Header) -> B256 { + let wasabi100 = rustock_core::config::ActivationHeights::current().wasabi100; + hash_for_merged_mining_from(header, wasabi100) +} + +/// [`hash_for_merged_mining`] on a chain whose RSKIP110 is at `wasabi100`. +pub(crate) fn hash_for_merged_mining_from(header: &Header, wasabi100: u64) -> B256 { + use rustock_core::validation::{fork_detection, merged_mining}; + let mut hash = header.hash_for_merged_mining(); + let includes = header.number >= wasabi100 && header.number >= fork_detection::REQUIRED_BLOCKS; + let data = includes.then(|| merged_mining::extract_fork_detection_data(header)); + if let Some(data) = data.flatten() { + // `arraycopy(data, 0, hash, HASH_FOR_MERGED_MINING_PREFIX_LENGTH, 12)`. + hash[20..].copy_from_slice(&data); + } + hash +} + +/// rskj `Block.getCumulativeDifficulty` (Block.java L165-171): the block's +/// difficulty and each of its uncles'. It reads nothing the store keeps; +/// what the store keeps for a block is its total difficulty, which +/// `totalDifficulty` prints. +fn cumulative_difficulty( + header: &Header, + body: Option<&(Vec, Vec
)>, +) -> alloy_primitives::U512 { + let uncles = body.map_or(&[][..], |(_, uncles)| uncles.as_slice()); + uncles.iter().fold( + alloy_primitives::U512::from(header.difficulty), + |sum, uncle| sum + alloy_primitives::U512::from(uncle.difficulty), + ) +} + /// Length of the block's RLP encoding, which is what `size` reports. /// /// rskj answers with `block.getEncoded().length` -- the whole block, i.e. the diff --git a/crates/rpc/src/tests.rs b/crates/rpc/src/tests.rs index c18a310..cbc5303 100644 --- a/crates/rpc/src/tests.rs +++ b/crates/rpc/src/tests.rs @@ -115,6 +115,237 @@ fn the_null_address_prints_as_no_bytes() { assert_eq!(notified["miner"], json!(twenty)); } +/// RSK mainnet block #9,257,552, the header `rustock-core` checks its +/// hashes on, as public-node.rsk.co holds it. +fn mainnet_block_9257552() -> Header { + let bytes = |hex: &str| Bytes::from(hex::decode(hex).unwrap()); + Header { + parent_hash: alloy_primitives::b256!("8b46403b2df22f81f450bd631db76cb47fe104ed48ab9b94a441f64be823c292"), + ommers_hash: alloy_primitives::b256!("e4d62e0f38c8a425952f64f56c6897c8fd4d9fdd09faeaddb904ec3a8362ae3b"), + beneficiary: alloy_primitives::address!("4e5dabc28e4a0f5e5b19fcb56b28c5a1989352c1"), + state_root: alloy_primitives::b256!("1f0d9418721c8a28c019e21a8af2af203ba43f68bfb8fa3b884642a55b5eedaf"), + transactions_root: alloy_primitives::b256!("2af4a2f53589d9f78b09adfefe9dd8e34f22143743dd0d3b584af8f6ac4d21ad"), + receipts_root: alloy_primitives::b256!("fa059fb22aad8c60ca36bc9d9820ae3c4310dc3d9363bcd2b2624eccb88ed1a6"), + logs_bloom: Bloom::from_slice(&hex::decode("00000000000000000000301800100020000100000010000000000200000000100000000000010000000000000000000000000000000000000000800000000000000000000004000000000000400040000000000000000000200200000000000000000000000000000040000000000100000000020000000000000000000000000000400000000001000000000004000000000000000000000000000020000000000000000200000000000000000000000080400000000180000000000000000000000000000800000000040000008000000000000000000000000000000000000000000000000000002000000000000000000400000000000000200000000000").unwrap()), + extension_data: None, + difficulty: U256::from(0x175cf1ea6738d50d56au128), + number: 9_257_552, + gas_limit: U256::from(0x989680), + gas_used: 0x22f96, + timestamp: 0x6ab06c78, + extra_data: bytes("d30191564554495645522d643430323166636532"), + paid_fees: U256::from(0x3656c63bb76u64), + minimum_gas_price: U256::from(0x1699280), + uncle_count: 2, + umm_root: Some(Bytes::new()), + version_fields: None, + bitcoin_merged_mining_header: Some(bytes("00e0f327307a19d17c1e20f51c1190b40d9fc53e7f056272a91d02000000000000000000cd81b87bdc182ac40f11daa46886c0b93b7e6497ca0e541ab5775f9c8fdd3d98936cb06ac51e02172a5c3c6e")), + bitcoin_merged_mining_merkle_proof: Some(bytes("c91bd5fe20d82da439eba4770fb50b6792cc06f083eb88b6795f0c89fc8ca427df7cc40dc64602e3fb3cf5c267664be19d148552f01cc28b6d3907d472008d15ba45ca54695cb6cbceac33003f9582450081de9ac6de608f0d49a78f2cae66c5c65cc0e8ac0674edfad8b00027c9d26b64ebe2476b5e91a95800b6786d91df004aa35390a8e996bf2cf9cc57555a12fcb76abcc376b20ebb5a441b1f94839653472ac163740f33c5e88a33003c73726de57da577d9e57d305937269421c1b0be94af8aa6a13a431fb5425ad871e9675edff825e8b49dc016ab6903bf63d376386dbb6c55d84d449f6147763855259dc20a09f72adfb25195c692bd8da719d1d8e9b227b874cbec4f58fb88dc6b21d810e995b4531346f37b0a3a7e54b3327d79450fa01b5a3420bd772f4f0605c829facdb88bc9f2945aa897aef9e6585c80fa62d683e84d1b8bd228f8c985a8269e312e7d0b135cf5aeb50132e1f2592a112db15c3e521d92fb7184af28a0adc8008fa3eb839d1b6d9e5d5c802160ffa7522649c18aa20502e09c50804448465889141227a0a82c85a0ce6876163ae60cc3ac")), + bitcoin_merged_mining_coinbase_transaction: Some(bytes("000000000000014013a11ba36836286d0115b1c5c58a42ad692d3aeeaedae25686175928146d440f8931d770ea29b3d5f1dd2eabaf3724518831d656f447230000000000000000002b6a2952534b424c4f434b3afe829f5c494b038c03268d18d0e338bb162f06b69ebc2a5e8f871a1d008d425000000000")), + cached_hash: None, + cached_hash_for_merged_mining: None, + kept: None, + } +} + +/// A block prints each property rskj's `BlockResultDTO` has, as `fromBlock` +/// fills them (task 284). Mainnet #9,257,552 against what +/// public-node.rsk.co answered for it: the merged-mining fields as held, +/// the merged-mining hash with the fork-detection data its coinbase +/// commits to in its last 12 bytes (RSKIP110), the paid fees, and the +/// cumulative difficulty of it and its two uncles, each of difficulty +/// 0x177af6546a47de99bb3 (`Block.getCumulativeDifficulty`). +#[test] +fn a_block_prints_each_property_rskj_prints() { + let header = mainnet_block_9257552(); + assert_eq!( + header.hash(), + alloy_primitives::b256!("0e7f9744b001ec59e68f0d60e8bde08ae6e9b9b57113e9a372b2058cc144c9fa") + ); + let uncle = Header { + difficulty: U256::from(0x177af6546a47de99bb3u128), + ..test_header(9_257_550) + }; + let body = (vec![], vec![uncle.clone(), uncle]); + let block = crate::helpers::BlockResultDto::from_header_with_body( + &header, + header.hash(), + U256::from(7), + Some(&body), + false, + 30, + ); + let block = serde_json::to_value(block).unwrap(); + let printed = |key: &str| block[key].clone(); + assert_eq!(printed("mixHash"), json!(format!("{:#x}", B256::ZERO))); + assert_eq!(printed("nonce"), json!("0x0000000000000000")); + assert_eq!(printed("totalDifficulty"), json!("0x7")); + assert_eq!( + printed("hashForMergedMining"), + json!("0xfe829f5c494b038c03268d18d0e338bb162f06b69ebc2a5e8f871a1d008d4250") + ); + assert_eq!(printed("paidFees"), json!("0x3656c63bb76")); + assert_eq!( + printed("cumulativeDifficulty"), + json!("0x4652de933bc89240cd0") + ); + assert_eq!(printed("bitcoinMergedMiningHeader"), json!("0x00e0f327307a19d17c1e20f51c1190b40d9fc53e7f056272a91d02000000000000000000cd81b87bdc182ac40f11daa46886c0b93b7e6497ca0e541ab5775f9c8fdd3d98936cb06ac51e02172a5c3c6e")); + assert_eq!(printed("bitcoinMergedMiningCoinbaseTransaction"), json!("0x000000000000014013a11ba36836286d0115b1c5c58a42ad692d3aeeaedae25686175928146d440f8931d770ea29b3d5f1dd2eabaf3724518831d656f447230000000000000000002b6a2952534b424c4f434b3afe829f5c494b038c03268d18d0e338bb162f06b69ebc2a5e8f871a1d008d425000000000")); + assert_eq!(printed("bitcoinMergedMiningMerkleProof"), json!("0xc91bd5fe20d82da439eba4770fb50b6792cc06f083eb88b6795f0c89fc8ca427df7cc40dc64602e3fb3cf5c267664be19d148552f01cc28b6d3907d472008d15ba45ca54695cb6cbceac33003f9582450081de9ac6de608f0d49a78f2cae66c5c65cc0e8ac0674edfad8b00027c9d26b64ebe2476b5e91a95800b6786d91df004aa35390a8e996bf2cf9cc57555a12fcb76abcc376b20ebb5a441b1f94839653472ac163740f33c5e88a33003c73726de57da577d9e57d305937269421c1b0be94af8aa6a13a431fb5425ad871e9675edff825e8b49dc016ab6903bf63d376386dbb6c55d84d449f6147763855259dc20a09f72adfb25195c692bd8da719d1d8e9b227b874cbec4f58fb88dc6b21d810e995b4531346f37b0a3a7e54b3327d79450fa01b5a3420bd772f4f0605c829facdb88bc9f2945aa897aef9e6585c80fa62d683e84d1b8bd228f8c985a8269e312e7d0b135cf5aeb50132e1f2592a112db15c3e521d92fb7184af28a0adc8008fa3eb839d1b6d9e5d5c802160ffa7522649c18aa20502e09c50804448465889141227a0a82c85a0ce6876163ae60cc3ac")); + assert_eq!(printed("rskPteEdges"), Value::Null); + assert_eq!(printed("baseEvent"), Value::Null); + let mut keys: Vec<&str> = block + .as_object() + .unwrap() + .keys() + .map(String::as_str) + .collect(); + keys.sort_unstable(); + let mut rskj = vec![ + "number", + "hash", + "parentHash", + "mixHash", + "nonce", + "sha3Uncles", + "logsBloom", + "transactionsRoot", + "stateRoot", + "receiptsRoot", + "miner", + "difficulty", + "totalDifficulty", + "extraData", + "size", + "gasLimit", + "gasUsed", + "timestamp", + "transactions", + "uncles", + "minimumGasPrice", + "bitcoinMergedMiningHeader", + "bitcoinMergedMiningCoinbaseTransaction", + "bitcoinMergedMiningMerkleProof", + "hashForMergedMining", + "paidFees", + "cumulativeDifficulty", + "rskPteEdges", + "baseEvent", + ]; + rskj.sort_unstable(); + assert_eq!(keys, rskj, "the getters of rskj's BlockResultDTO"); + + // A coinbase that commits to `RSKBLOCK:`, the hash's first 20 bytes and + // twelve of 0xfd: those are the data from wasabi100 (mainnet's + // #1,591,000) on, and below it the coinbase commits to the whole hash. + let committing = |number: u64| { + let header = Header { + number, + ..header.clone() + }; + let mut coinbase = vec![0; 40]; + coinbase.extend_from_slice(b"RSKBLOCK:"); + coinbase.extend_from_slice(&header.hash_for_merged_mining()[..20]); + coinbase.extend_from_slice(&[0xfd; 12]); + Header { + bitcoin_merged_mining_coinbase_transaction: Some(coinbase.into()), + ..header + } + }; + let printed = |header: &Header| { + crate::helpers::BlockResultDto::from_header(header, B256::ZERO, U256::ZERO) + .hash_for_merged_mining + }; + let mut with_data = committing(1_591_000).hash_for_merged_mining(); + with_data[20..].copy_from_slice(&[0xfd; 12]); + assert_eq!(printed(&committing(1_591_000)), format!("{with_data:#x}")); + let below = committing(1_590_999); + assert_eq!( + printed(&below), + format!("{:#x}", below.hash_for_merged_mining()) + ); + // Where RSKIP110 is from genesis, as on testnet and regtest, the data + // still start at block 449 (`MiningConfig.REQUIRED_NUMBER_OF_BLOCKS_FOR_ + // FORK_DETECTION_CALCULATION`). + let from_genesis = |number: u64| { + let header = committing(number); + let hash = crate::helpers::hash_for_merged_mining_from(&header, 0); + ( + hash[20..] == [0xfd; 12], + hash[..20] == header.hash_for_merged_mining()[..20], + ) + }; + assert_eq!(from_genesis(448), (false, true)); + assert_eq!(from_genesis(449), (true, true)); + // Nor are there data to read without a coinbase. + let without = Header { + bitcoin_merged_mining_coinbase_transaction: None, + ..header.clone() + }; + assert_eq!( + printed(&without), + format!("{:#x}", without.hash_for_merged_mining()) + ); + // Without a body, the cumulative difficulty is the block's own. + let block = crate::helpers::BlockResultDto::from_header(&header, B256::ZERO, U256::ZERO); + assert_eq!(block.cumulative_difficulty, "0x175cf1ea6738d50d56a"); + assert_eq!(block.bitcoin_merged_mining_header.len(), 2 + 160); +} + +/// An uncle's hash, roots or bloom kept at another length print as the +/// bytes rskj holds (`toUnformattedJsonHex`), where rustock's typed field +/// holds zero, and its paid fees past 256 bits as the quantity of the bytes +/// kept. A versioned header's sublist edges print as rskj's `short[]`, its +/// base event as hex, or null where empty; a header without merged-mining +/// fields prints `0x` for each. +#[test] +fn an_uncles_kept_fields_print_as_rskj_holds_them() { + use rustock_core::types::header::{HeaderVersionFields, KeptFields}; + let mut uncle = test_header(5); + uncle.kept = Some(Box::new(KeptFields { + ommers_hash: Some(Bytes::from_static(&[0xaa; 3])), + state_root: Some(Bytes::from_static(&[0xbb; 31])), + transactions_root: Some(Bytes::new()), + receipts_root: Some(Bytes::from_static(&[0xcc; 33])), + logs_bloom: Some(Bytes::from_static(&[0xdd; 2])), + paid_fees: Some(Bytes::from(vec![0x01; 33])), + ..Default::default() + })); + uncle.version_fields = Some(HeaderVersionFields { + version: 2, + edges: Some(vec![2, 5]), + base_event: Some(Bytes::from_static(&[0xee])), + }); + let block = crate::helpers::BlockResultDto::from_header(&uncle, B256::ZERO, U256::ZERO); + let block = serde_json::to_value(block).unwrap(); + assert_eq!(block["sha3Uncles"], json!("0xaaaaaa")); + assert_eq!(block["stateRoot"], json!(format!("0x{}", "bb".repeat(31)))); + assert_eq!(block["transactionsRoot"], json!("0x")); + assert_eq!( + block["receiptsRoot"], + json!(format!("0x{}", "cc".repeat(33))) + ); + assert_eq!(block["logsBloom"], json!("0xdddd")); + assert_eq!(block["paidFees"], json!(format!("0x1{}", "01".repeat(32)))); + assert_eq!(block["rskPteEdges"], json!([2, 5])); + assert_eq!(block["baseEvent"], json!("0xee")); + for field in [ + "bitcoinMergedMiningHeader", + "bitcoinMergedMiningCoinbaseTransaction", + "bitcoinMergedMiningMerkleProof", + ] { + assert_eq!(block[field], json!("0x"), "{field}"); + } + + uncle.version_fields = Some(HeaderVersionFields { + version: 2, + edges: None, + base_event: Some(Bytes::new()), + }); + let block = crate::helpers::BlockResultDto::from_header(&uncle, B256::ZERO, U256::ZERO); + assert_eq!((block.rsk_pte_edges, block.base_event), (None, None)); +} + fn setup_state() -> (RpcState, tempfile::TempDir) { setup_state_with(|store| store) } From 0a0eaee68c87437d27cf685b1cfeaf763374900e Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Tue, 10 Nov 2026 07:59:29 -0800 Subject: [PATCH 089/264] rpc: the methods rskj stubs or refuses, answered as rskj answers them eth_getCompilers, rsk_protocolVersion and eth_protocolVersion answer rskj's values; the compile, submit and db methods are not available; the personal and signing methods answer as with rskj's wallet off; and jsonrpc4j's parameter count and "method not found" come first, as there. --- crates/cli/src/main.rs | 8 +- crates/rpc/src/eth.rs | 4 +- crates/rpc/src/lib.rs | 1 + crates/rpc/src/rsk.rs | 10 +- crates/rpc/src/server.rs | 39 +++--- crates/rpc/src/sign.rs | 39 +++--- crates/rpc/src/stubs.rs | 155 ++++++++++++++++++++++++ crates/rpc/src/tests.rs | 252 +++++++++++++++++++++++++++++++++------ crates/rpc/src/wallet.rs | 3 +- 9 files changed, 431 insertions(+), 80 deletions(-) create mode 100644 crates/rpc/src/stubs.rs diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index 8f5de6b..c4b9f83 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -1278,10 +1278,10 @@ struct Args { /// Private keys this node may sign with, one 32-byte hex key per line. /// /// Off by default, and the only way the node ever holds a key for - /// signing. Without it `eth_accounts` is empty and `eth_sign`, - /// `eth_signTransaction` and `eth_sendTransaction` report as unknown - /// methods, so a node with no wallet is indistinguishable from one - /// built without the feature. + /// signing. Without it `eth_accounts` is empty, `eth_sign` and + /// `eth_sendTransaction` are refused as rskj refuses them with its + /// wallet off, and `eth_signTransaction`, which rskj doesn't have, is an + /// unknown method. /// /// A file rather than a flag taking the key itself, because a private /// key on the command line is visible in `ps` to every user on the diff --git a/crates/rpc/src/eth.rs b/crates/rpc/src/eth.rs index 0cd4e13..e4995ce 100644 --- a/crates/rpc/src/eth.rs +++ b/crates/rpc/src/eth.rs @@ -9,8 +9,10 @@ use rustock_storage::BlockStore; use serde_json::{json, Value}; use std::sync::Arc; +/// rskj's `eth_protocolVersion` answers `rsk_protocolVersion()` (`Web3Impl` +/// L300-303). pub fn eth_protocol_version(id: Value) -> JsonRpcResponse { - JsonRpcResponse::success(id, json!("0x3e")) + crate::rsk::rsk_protocol_version(id) } pub fn eth_chain_id(id: Value, config: &ChainConfig) -> JsonRpcResponse { diff --git a/crates/rpc/src/lib.rs b/crates/rpc/src/lib.rs index 184ad12..47098ae 100644 --- a/crates/rpc/src/lib.rs +++ b/crates/rpc/src/lib.rs @@ -16,6 +16,7 @@ pub mod sco; pub mod server; pub mod sign; pub mod state; +pub mod stubs; pub mod subscribe; pub mod tx; pub mod trace; diff --git a/crates/rpc/src/rsk.rs b/crates/rpc/src/rsk.rs index 295bed8..800fb7e 100644 --- a/crates/rpc/src/rsk.rs +++ b/crates/rpc/src/rsk.rs @@ -6,8 +6,16 @@ use rustock_core::Header; use rustock_storage::BlockStore; use serde_json::{json, Value}; +/// The highest `rsk` capability rskj's configuration offers +/// (`Web3Impl.rsk_protocolVersion` L280-298). `ConfigCapabilitiesImpl` takes +/// them from `EthVersion.supported()` (L52), where the only version is V62 +/// (`EthVersion` L33-36). This node also offers rsk/63, its own extension, +/// which rskj never takes (`Handshake::capabilities`), so it answers as rskj +/// does. +const RSK_PROTOCOL_VERSION: u64 = 62; + pub fn rsk_protocol_version(id: Value) -> JsonRpcResponse { - JsonRpcResponse::success(id, json!("0x1")) + JsonRpcResponse::success(id, json!(format!("{RSK_PROTOCOL_VERSION:#x}"))) } pub fn rsk_get_raw_block_header_by_hash( diff --git a/crates/rpc/src/server.rs b/crates/rpc/src/server.rs index b5bd1bb..1b38b84 100644 --- a/crates/rpc/src/server.rs +++ b/crates/rpc/src/server.rs @@ -241,6 +241,11 @@ async fn route(state: &RpcState, req: JsonRpcRequest) -> JsonRpcResponse { let id = req.id.unwrap_or(Value::Null); let params = &req.params; + // jsonrpc4j counts a call's parameters before the method runs. + if let Some(refused) = crate::stubs::wrong_arity(&id, &req.method, params) { + return refused; + } + match req.method.as_str() { // -- web3 -- "web3_clientVersion" => web3::web3_client_version(id), @@ -345,7 +350,7 @@ async fn route(state: &RpcState, req: JsonRpcRequest) -> JsonRpcResponse { | "rsk_flush" | "rsk_shutdown" if !state.admin_enabled => { - JsonRpcResponse::error(id, METHOD_NOT_FOUND, "Method not found") + crate::stubs::method_not_found(id) } "rsk_collectTrie" => admin::rsk_collect_trie( id, @@ -403,17 +408,26 @@ async fn route(state: &RpcState, req: JsonRpcRequest) -> JsonRpcResponse { "eth_getFilterLogs" => logs::eth_get_filter_logs(id, params, state), "eth_uninstallFilter" => logs::eth_uninstall_filter(id, params, state), - // Answered out of the operator-supplied wallet, and refused with - // METHOD_NOT_FOUND when there is none -- which is what they did - // before a wallet was possible. + // Answered out of the operator-supplied wallet. Without one they're + // refused as rskj refuses them with its wallet off (`sign.rs`). "eth_sendTransaction" => sign::eth_send_transaction(id, params, state).await, "eth_sign" => sign::eth_sign(id, params, state), "eth_signTransaction" => sign::eth_sign_transaction(id, params, state), - // -- unsupported methods -- - "eth_getCompilers" | "eth_compileSolidity" => { - execution_not_available(id, &req.method) + // -- what rskj's Web3Impl answers with a fixed value or as unsupported -- + "eth_getCompilers" => crate::stubs::eth_get_compilers(id), + "eth_compileLLL" + | "eth_compileSerpent" + | "eth_compileSolidity" + | "eth_submitWork" + | "eth_submitHashrate" => crate::stubs::not_available(id, &req.method), + // No-ops in Web3Impl, but `db` isn't one of the modules rskj's + // reference.conf turns on, so JsonRpcMethodFilter refuses them first, + // in the same words. + "db_putString" | "db_getString" | "db_putHex" | "db_getHex" => { + crate::stubs::not_available(id, &req.method) } + m if m.starts_with("personal_") => crate::stubs::personal(id, m, params), "debug_wireProtocolQueueSize" => debug::debug_wire_protocol_queue_size(id, state), "debug_traceTransaction" => debug::debug_trace_transaction(id, params, state), @@ -438,16 +452,7 @@ async fn route(state: &RpcState, req: JsonRpcRequest) -> JsonRpcResponse { "txpool_inspect" => txpool::txpool_inspect(id, state), "txpool_status" => txpool::txpool_status(id, state), - m if m.starts_with("debug_") - || m.starts_with("trace_") - || m.starts_with("personal_") - || m.starts_with("evm_") - || m.starts_with("txpool_") - || m.starts_with("db_") => { - execution_not_available(id, m) - } - - _ => JsonRpcResponse::error(id, METHOD_NOT_FOUND, "Method not found"), + _ => crate::stubs::method_not_found(id), } } diff --git a/crates/rpc/src/sign.rs b/crates/rpc/src/sign.rs index 0380376..853dbe4 100644 --- a/crates/rpc/src/sign.rs +++ b/crates/rpc/src/sign.rs @@ -1,11 +1,12 @@ //! `eth_accounts`, `eth_sign`, `eth_signTransaction`, `eth_sendTransaction`. //! -//! All four answer out of [`crate::wallet::Wallet`], and all four behave -//! as they did before it existed when the operator supplied no wallet: -//! an empty account list, and `METHOD_NOT_FOUND` for the three that sign. -//! A node that was never given keys is indistinguishable from one built -//! without the feature, which is the property the tests that predate this -//! module were written to hold. +//! All four answer out of [`crate::wallet::Wallet`]. When the operator +//! supplied none they answer as rskj does with its wallet off, which is its +//! default (`EthModuleWalletDisabled`, `EthModuleTransactionDisabled`): an +//! empty account list, and `eth_sign` and `eth_sendTransaction` refused, +//! -32602 "Local wallet is disabled in this node". rskj has no +//! `eth_signTransaction`, so without a wallet that one is "method not +//! found", as rskj answers it. //! //! `eth_sendTransaction` does not have its own path into the pool. It //! fills in what the caller left out, signs, and hands the bytes to the @@ -19,16 +20,6 @@ use serde_json::{json, Value}; use crate::server::RpcState; use crate::types::{JsonRpcResponse, INTERNAL_ERROR, INVALID_PARAMS, METHOD_NOT_FOUND}; -/// The refusal a node with no wallet gives, unchanged from before there -/// was one to give it. -fn no_wallet(id: Value, method: &str) -> JsonRpcResponse { - JsonRpcResponse::error( - id, - METHOD_NOT_FOUND, - format!("Method {method} requires a wallet, which this node was not given"), - ) -} - /// rskj returns the wallet's accounts; with no wallet the list is empty, /// which is what it has always been here. pub fn eth_accounts(id: Value, state: &RpcState) -> JsonRpcResponse { @@ -41,7 +32,16 @@ pub fn eth_accounts(id: Value, state: &RpcState) -> JsonRpcResponse { /// `eth_sign(address, message)` — 65 bytes over the EIP-191 digest. pub fn eth_sign(id: Value, params: &Value, state: &RpcState) -> JsonRpcResponse { - let Some(wallet) = &state.wallet else { return no_wallet(id, "eth_sign") }; + // rskj's `Web3EthModule.eth_sign` reads both arguments before its wallet + // module sees them (L46-48), so a null one is a NullPointerException, + // which `RskErrorResolver` answers as an internal error (L56-58), wallet + // or no wallet. + if params[0].is_null() || params[1].is_null() { + return JsonRpcResponse::error(id, INTERNAL_ERROR, "Internal server error"); + } + let Some(wallet) = &state.wallet else { + return crate::stubs::local_wallet_disabled(id); + }; let Some(address) = params .get(0) @@ -63,7 +63,8 @@ pub fn eth_sign(id: Value, params: &Value, state: &RpcState) -> JsonRpcResponse /// `eth_signTransaction` — the signed RLP, not sent. pub fn eth_sign_transaction(id: Value, params: &Value, state: &RpcState) -> JsonRpcResponse { if state.wallet.is_none() { - return no_wallet(id, "eth_signTransaction"); + // rskj has no such method. + return crate::stubs::method_not_found(id); } match build_and_sign(params, state) { Ok((_from, raw)) => { @@ -76,7 +77,7 @@ pub fn eth_sign_transaction(id: Value, params: &Value, state: &RpcState) -> Json /// `eth_sendTransaction` — sign, then go through the ordinary relay path. pub async fn eth_send_transaction(id: Value, params: &Value, state: &RpcState) -> JsonRpcResponse { if state.wallet.is_none() { - return no_wallet(id, "eth_sendTransaction"); + return crate::stubs::local_wallet_disabled(id); } // Built before the relay is looked for, so a request naming an diff --git a/crates/rpc/src/stubs.rs b/crates/rpc/src/stubs.rs new file mode 100644 index 0000000..16d8d6d --- /dev/null +++ b/crates/rpc/src/stubs.rs @@ -0,0 +1,155 @@ +//! The methods rskj answers with a fixed value or as unsupported, and the +//! refusals its JSON-RPC server gives before a method runs. +//! +//! rskj serves JSON-RPC through jsonrpc4j 1.6's `JsonRpcBasicServer`, under +//! its own `JsonRpcCustomServer`, with `JsonRpcMethodFilter` as the request +//! interceptor and `RskErrorResolver` first among the error resolvers +//! (`JsonRpcWeb3ServerHandler` L59-65). A call meets, in this order: +//! +//! 1. its name: one the `Web3` service doesn't have is -32601 "method not +//! found" (`JsonRpcCustomServer` L57-60); +//! 2. the count of its parameters: one that no method of that name takes is +//! -32602 "method parameters invalid" (`JsonRpcBasicServer.handleObject` +//! L440-443, with `allowExtraParams` and `allowLessParams` false, L73-74); +//! 3. the modules: a method whose module is off makes `JsonRpcMethodFilter` +//! throw "Method not supported: " (L50-58), which `RskErrorResolver` +//! answers as it answers an `UnsupportedOperationException`, -32601 "the +//! method does not exist/is not available" (L51-55); +//! 4. the method itself. +//! +//! This node has no module settings, so this follows rskj's defaults: the +//! modules `reference.conf` lists (L486-534), where `db` isn't one, and the +//! wallet off (`RskSystemProperties.isWalletEnabled` L205-207). + +use serde_json::{json, Value}; + +use crate::types::{JsonRpcResponse, INVALID_PARAMS, METHOD_NOT_FOUND}; + +/// rskj's answer to a name its `Web3` service doesn't have +/// (`JsonRpcCustomServer.METHOD_NOT_FOUND_MSG`, L37). +pub const METHOD_NOT_FOUND_MESSAGE: &str = "method not found"; + +/// `DisabledWalletException`'s code (L23). +const DISABLED_WALLET: i64 = -32604; + +/// The answer to a name rskj's `Web3` service doesn't have. +pub fn method_not_found(id: Value) -> JsonRpcResponse { + JsonRpcResponse::error(id, METHOD_NOT_FOUND, METHOD_NOT_FOUND_MESSAGE) +} + +/// How many parameters rskj's method of this name takes, for the methods +/// whose answer this module gives. Each has one signature in rskj: `Web3` +/// L46 (`rsk_protocolVersion`), L51-55 (`db_`) and L57-64 (`personal_`), +/// and `Web3EthModule` L42, L46, L64, L108, L123, L130-136 and L154-156. +pub fn arity(method: &str) -> Option { + Some(match method { + "eth_accounts" + | "eth_pendingTransactions" + | "eth_protocolVersion" + | "rsk_protocolVersion" + | "eth_getCompilers" + | "db_putString" + | "db_getString" + | "db_putHex" + | "db_getHex" + | "personal_listAccounts" => 0, + "eth_sendTransaction" + | "eth_compileLLL" + | "eth_compileSerpent" + | "eth_compileSolidity" + | "personal_newAccountWithSeed" + | "personal_newAccount" + | "personal_lockAccount" + | "personal_dumpRawKey" => 1, + "eth_sign" + | "eth_submitHashrate" + | "personal_importRawKey" + | "personal_sendTransaction" => 2, + "eth_submitWork" | "personal_unlockAccount" => 3, + _ => return None, + }) +} + +/// How many parameters jsonrpc4j counts in `params`, or `None` where no +/// method of rskj's can take them. +/// +/// Absent or null is none (`hasNoParameters`, L749-751), and an array is its +/// length (L727-728). An object is matched by the parameters' names, and +/// rskj names none (it has no `@JsonRpcParam`), so only an empty one is +/// taken, as none (`findBestMethodUsingParamNames` L872-896, `ParameterCount` +/// L1247-1270 and L1315-1320). jsonrpc4j throws at any other value outside +/// its error handling, and rskj answers that -32099 "Unexpected error" with +/// no id (`JsonRpcWeb3ServerHandler` L105-110); here it's a count no method +/// takes. +fn param_count(params: &Value) -> Option { + match params { + Value::Null => Some(0), + Value::Array(items) => Some(items.len()), + Value::Object(fields) if fields.is_empty() => Some(0), + _ => None, + } +} + +/// jsonrpc4j's refusal of a call whose parameters rskj's method of this name +/// can't take, which comes before anything the method says. `None` lets the +/// call through, and so does a method `arity` doesn't know. +pub fn wrong_arity(id: &Value, method: &str, params: &Value) -> Option { + let arity = arity(method)?; + (param_count(params) != Some(arity)) + .then(|| JsonRpcResponse::error(id.clone(), INVALID_PARAMS, "method parameters invalid")) +} + +/// `RskErrorResolver`'s answer to an `UnsupportedOperationException`, and to +/// a method `JsonRpcMethodFilter` refuses (L51-55), naming the method. +pub fn not_available(id: Value, method: &str) -> JsonRpcResponse { + JsonRpcResponse::error( + id, + METHOD_NOT_FOUND, + format!("the method {method} does not exist/is not available"), + ) +} + +/// `eth_getCompilers` (`Web3Impl` L952-962). rskj compiles nothing: its +/// `eth_compileSolidity` throws (L974-977). +pub fn eth_get_compilers(id: Value) -> JsonRpcResponse { + JsonRpcResponse::success(id, json!(["solidity"])) +} + +/// `eth_sign` and `eth_sendTransaction` with the wallet off: +/// `EthModuleWalletDisabled.sign` (L49-53) and +/// `EthModuleTransactionDisabled.sendTransaction` (L38-42) throw +/// `invalidParamError`, -32602. +pub fn local_wallet_disabled(id: Value) -> JsonRpcResponse { + JsonRpcResponse::error(id, INVALID_PARAMS, "Local wallet is disabled in this node") +} + +/// The `personal_` methods as rskj answers them with the wallet off: +/// `PersonalModuleWalletDisabled` throws `DisabledWalletException` from every +/// one (L27-74), -32604 "The local wallet feature is disabled", once +/// `Web3Impl` has refused a null seed or passphrase (L1130-1144). A name rskj +/// doesn't have is "method not found". +/// +/// This node has no personal module, so it answers so whether or not +/// `--wallet-file` gave it keys, which serve only the `eth_` methods that +/// sign. +pub fn personal(id: Value, method: &str, params: &Value) -> JsonRpcResponse { + match method { + "personal_newAccountWithSeed" if params[0].is_null() => { + JsonRpcResponse::error(id, INVALID_PARAMS, "Seed is null") + } + "personal_newAccount" if params[0].is_null() => { + JsonRpcResponse::error(id, INVALID_PARAMS, "Passphrase is null") + } + "personal_newAccountWithSeed" + | "personal_newAccount" + | "personal_listAccounts" + | "personal_importRawKey" + | "personal_sendTransaction" + | "personal_unlockAccount" + | "personal_lockAccount" + | "personal_dumpRawKey" => { + JsonRpcResponse::error(id, DISABLED_WALLET, "The local wallet feature is disabled") + } + _ => method_not_found(id), + } +} diff --git a/crates/rpc/src/tests.rs b/crates/rpc/src/tests.rs index cbc5303..2a137c5 100644 --- a/crates/rpc/src/tests.rs +++ b/crates/rpc/src/tests.rs @@ -729,12 +729,16 @@ async fn test_rpc_modules() { // ========== rsk ========== +/// rskj's `rsk_protocolVersion` is the highest `rsk` capability its +/// configuration offers, 62, and its `eth_protocolVersion` answers the same +/// (`Web3Impl` L280-303), as rskj's public nodes answer it. #[tokio::test] async fn test_rsk_protocol_version() { let (state, _tmp) = setup_state(); - let req = make_request("rsk_protocolVersion", json!([])); - let resp = dispatch_for_test(&state, req).await; - assert_eq!(resp.result.unwrap(), json!("0x1")); + for method in ["rsk_protocolVersion", "eth_protocolVersion"] { + let resp = dispatch_for_test(&state, make_request(method, json!([]))).await; + assert_eq!(resp.result.unwrap(), json!("0x3e"), "{method}"); + } } #[tokio::test] @@ -761,27 +765,57 @@ async fn test_rsk_get_raw_block_header_by_hash() { // ========== dispatch / error handling ========== +/// A name rskj's `Web3` service doesn't have is -32601 "method not found", +/// whatever its parameters and in every namespace (`JsonRpcCustomServer` +/// L57-60), as rskj's public nodes answer `eth_foo` and `personal_sign`. #[tokio::test] async fn test_method_not_found() { let (state, _tmp) = setup_state(); - let req = make_request("nonexistent_method", json!([])); - let resp = dispatch_for_test(&state, req).await; - assert!(resp.error.is_some()); - assert_eq!(resp.error.unwrap().code, METHOD_NOT_FOUND); + for method in [ + "nonexistent_method", + "eth_foo", + "debug_foo", + "trace_call", + "txpool_foo", + "evm_foo", + "db_foo", + "personal_sign", + ] { + let resp = dispatch_for_test(&state, make_request(method, json!([1, 2]))).await; + assert!(resp.result.is_none(), "{method}"); + let err = resp.error.unwrap(); + assert_eq!( + (err.code, err.message.as_str()), + (METHOD_NOT_FOUND, "method not found"), + "{method}" + ); + } } -/// `eth_sendTransaction` moved out of this list when the node learned to -/// hold keys -- it is refused for want of a wallet now, not for want of -/// an execution engine. The compilation methods are what is left. +/// rskj's `Web3Impl` throws `UnsupportedOperationException` from the +/// compilers and from `eth_submitWork` and `eth_submitHashrate` (L964-978, +/// L1112-1120), and `RskErrorResolver` answers it -32601, naming the method +/// (L51-55), as rskj's public nodes answer `eth_compileSolidity` and +/// `eth_compileLLL`. #[tokio::test] async fn test_unsupported_eth_method() { let (state, _tmp) = setup_state(); - let req = make_request("eth_getCompilers", json!([])); - let resp = dispatch_for_test(&state, req).await; - assert!(resp.error.is_some()); - let err = resp.error.unwrap(); - assert_eq!(err.code, METHOD_NOT_FOUND); - assert!(err.message.contains("execution engine")); + for (method, params) in [ + ("eth_compileLLL", json!(["(seq)"])), + ("eth_compileSerpent", json!(["def f(): return(1)"])), + ("eth_compileSolidity", json!(["contract A {}"])), + ("eth_submitWork", json!(["0x01", "0x02", "0x03"])), + ("eth_submitHashrate", json!(["0x01", "0x02"])), + ] { + let resp = dispatch_for_test(&state, make_request(method, params)).await; + assert!(resp.result.is_none(), "{method}"); + let err = resp.error.unwrap(); + assert_eq!(err.code, METHOD_NOT_FOUND, "{method}"); + assert_eq!( + err.message, + format!("the method {method} does not exist/is not available") + ); + } } #[tokio::test] @@ -823,12 +857,53 @@ async fn test_debug_trace_block_without_a_body_is_null() { assert_eq!(resp.result.unwrap(), Value::Null); } +/// rskj's personal module with its wallet off, which is its default: +/// `PersonalModuleWalletDisabled` throws `DisabledWalletException` from every +/// method, -32604 "The local wallet feature is disabled", once `Web3Impl` has +/// refused a null seed or passphrase (L1130-1144). This node has no personal +/// module, so the keys `--wallet-file` gives it change nothing here. #[tokio::test] async fn test_unsupported_personal_method() { - let (state, _tmp) = setup_state(); - let req = make_request("personal_unlockAccount", json!([])); - let resp = dispatch_for_test(&state, req).await; - assert!(resp.error.is_some()); + let address = "0x0000000000000000000000000000000000000001"; + for (state, _tmp) in [setup_state(), setup_state_with_wallet()] { + for (method, params) in [ + ("personal_newAccountWithSeed", json!(["seed"])), + ("personal_newAccount", json!(["passphrase"])), + ("personal_listAccounts", json!([])), + ("personal_importRawKey", json!(["0x0123", "passphrase"])), + ( + "personal_sendTransaction", + json!([{"from": address}, "passphrase"]), + ), + ( + "personal_unlockAccount", + json!([address, "passphrase", "0x3c"]), + ), + ("personal_lockAccount", json!([address])), + ("personal_dumpRawKey", json!([address])), + ] { + let resp = dispatch_for_test(&state, make_request(method, params)).await; + assert!(resp.result.is_none(), "{method}"); + let err = resp.error.unwrap(); + assert_eq!( + (err.code, err.message.as_str()), + (-32604, "The local wallet feature is disabled"), + "{method}" + ); + } + for (method, message) in [ + ("personal_newAccountWithSeed", "Seed is null"), + ("personal_newAccount", "Passphrase is null"), + ] { + let resp = dispatch_for_test(&state, make_request(method, json!([null]))).await; + let err = resp.error.unwrap(); + assert_eq!( + (err.code, err.message.as_str()), + (INVALID_PARAMS, message), + "{method}" + ); + } + } } #[tokio::test] @@ -2434,7 +2509,7 @@ async fn test_admin_methods_are_unknown_without_the_flag() { let err = resp.error.expect("an error is expected"); assert_eq!(err.code, -32601, "{method} must report METHOD_NOT_FOUND"); assert_eq!( - err.message, "Method not found", + err.message, "method not found", "{method} must not disclose that it exists but is disabled" ); } @@ -2470,7 +2545,7 @@ async fn test_admin_methods_dispatch_when_enabled() { let refused_by_gate = resp .error .as_ref() - .is_some_and(|e| e.code == -32601 && e.message == "Method not found"); + .is_some_and(|e| e.code == -32601 && e.message == "method not found"); assert!( !refused_by_gate, "with admin enabled the method must reach its handler, not the gate" @@ -2489,21 +2564,33 @@ async fn test_admin_methods_dispatch_when_enabled() { /// No key-holding method may answer. A node with no wallet must refuse to /// sign rather than sign with something it happens to have -- the node /// identity key, a miner key -- which would be a signature the operator never -/// authorised. +/// authorised. It refuses as rskj does with its wallet off, which is rskj's +/// default: `eth_sign` and `eth_sendTransaction` are -32602 "Local wallet is +/// disabled in this node" (`EthModuleWalletDisabled` L49-53, +/// `EthModuleTransactionDisabled` L38-42), the transaction unread, and +/// `eth_signTransaction`, which rskj doesn't have, is "method not found". #[tokio::test] async fn test_the_node_holds_no_keys_and_refuses_to_sign() { let (state, _tmp) = setup_state(); + let from = "0x0000000000000000000000000000000000000001"; + let disabled = (INVALID_PARAMS, "Local wallet is disabled in this node"); - for (method, params) in [ - ("eth_sign", json!(["0x0000000000000000000000000000000000000001", "0xdeadbeef"])), - ("eth_signTransaction", json!([{"from": "0x0000000000000000000000000000000000000001"}])), - ("eth_sendTransaction", json!([{"from": "0x0000000000000000000000000000000000000001"}])), + for (method, params, refusal) in [ + ("eth_sign", json!([from, "0xdeadbeef"]), disabled), + ("eth_sendTransaction", json!([{"from": from}]), disabled), + ("eth_sendTransaction", json!([null]), disabled), + ( + "eth_signTransaction", + json!([{"from": from}]), + (METHOD_NOT_FOUND, "method not found"), + ), ] { let resp = dispatch_for_test(&state, make_request(method, params)).await; assert!(resp.result.is_none(), "{method} must not return a result"); + let err = resp.error.expect("an error is expected"); assert_eq!( - resp.error.expect("an error is expected").code, - -32601, + (err.code, err.message.as_str()), + refusal, "{method} must be refused" ); } @@ -2633,16 +2720,104 @@ fn setup_state_with_pending_tx() -> (RpcState, B256, tempfile::TempDir) { (state, hash, tmp) } -/// The Solidity compiler methods are refused rather than answered with an -/// empty list. rskj removed these upstream; answering "no compilers" would -/// invite a caller to treat compilation as supported-but-unavailable. +/// rskj answers `eth_getCompilers` with `["solidity"]` (`Web3Impl` +/// L952-962), though it compiles nothing: `eth_compileSolidity` throws +/// (L974-977), as `test_unsupported_eth_method` checks. #[tokio::test] -async fn test_compiler_methods_are_refused() { +async fn test_eth_get_compilers_names_solidity_as_rskj_does() { let (state, _tmp) = setup_state(); - for method in ["eth_getCompilers", "eth_compileSolidity"] { + let resp = dispatch_for_test(&state, make_request("eth_getCompilers", json!([]))).await; + assert!(resp.error.is_none(), "{:?}", resp.error); + assert_eq!(resp.result.unwrap(), json!(["solidity"])); +} + +/// `db_putString`, `db_getString`, `db_putHex` and `db_getHex` do nothing in +/// rskj's `Web3Impl` (L1104-1128), and `db` isn't one of the modules +/// `reference.conf` turns on (L486-534), so `JsonRpcMethodFilter` refuses +/// them (L50-58) and `RskErrorResolver` answers -32601, naming the method +/// (L51-55), as rskj's public nodes answer `db_getString`. +#[tokio::test] +async fn the_db_namespace_is_refused_as_rskjs_default_modules_refuse_it() { + let (state, _tmp) = setup_state(); + for method in ["db_putString", "db_getString", "db_putHex", "db_getHex"] { let resp = dispatch_for_test(&state, make_request(method, json!([]))).await; - assert!(resp.result.is_none(), "{method} must not answer"); - assert_eq!(resp.error.unwrap().code, -32601); + assert!(resp.result.is_none(), "{method}"); + let err = resp.error.unwrap(); + assert_eq!(err.code, METHOD_NOT_FOUND, "{method}"); + assert_eq!( + err.message, + format!("the method {method} does not exist/is not available") + ); + } +} + +/// jsonrpc4j counts a call's parameters before the method runs, and a count +/// rskj's method of that name doesn't take is -32602 "method parameters +/// invalid" (`JsonRpcBasicServer.handleObject` L440-443), even where the +/// method would refuse anyway. No parameters, `null` and an empty object +/// count as none; an object with fields matches none of rskj's methods. +/// rskj's public nodes answer `eth_getCompilers` with `[1]`, +/// `eth_compileSolidity` with `[]` and with `{"contract": ...}`, `db_getHex` +/// with two parameters and `eth_protocolVersion` with `{"a": 1}` so. +#[tokio::test] +async fn a_stubbed_methods_parameters_are_counted_as_jsonrpc4j_counts_them() { + let address = "0x0000000000000000000000000000000000000001"; + for (state, _tmp) in [setup_state(), setup_state_with_wallet()] { + for (method, params) in [ + ("eth_getCompilers", json!([1])), + ("eth_getCompilers", json!({"a": 1})), + ("eth_compileSolidity", json!([])), + ("eth_compileSolidity", json!(["contract A {}", "x"])), + ("eth_compileSolidity", json!({"contract": "contract A {}"})), + ("eth_submitWork", json!(["0x01", "0x02"])), + ("eth_submitHashrate", json!(["0x01", "0x02", "0x03"])), + ("db_getHex", json!(["a", "b"])), + ("db_putString", json!(["testDB", "key", "value"])), + ("eth_protocolVersion", json!({"a": 1})), + ("rsk_protocolVersion", json!([1])), + ("eth_accounts", json!([1])), + ("eth_pendingTransactions", json!([1])), + ("eth_sign", json!([address])), + ("eth_sign", json!([address, "0xdeadbeef", "x"])), + ("eth_sendTransaction", json!([])), + ("personal_unlockAccount", json!([address, "passphrase"])), + ("personal_listAccounts", json!(["x"])), + ] { + let resp = dispatch_for_test(&state, make_request(method, params.clone())).await; + assert!(resp.result.is_none(), "{method} {params}"); + let err = resp.error.unwrap(); + assert_eq!( + (err.code, err.message.as_str()), + (INVALID_PARAMS, "method parameters invalid"), + "{method} {params}" + ); + } + for params in [json!([]), Value::Null, json!({})] { + let resp = + dispatch_for_test(&state, make_request("eth_getCompilers", params.clone())).await; + assert_eq!(resp.result, Some(json!(["solidity"])), "{params}"); + } + } +} + +/// rskj's `Web3EthModule.eth_sign` reads both arguments before its wallet +/// module sees them (L46-48), so a null one is a NullPointerException, which +/// `RskErrorResolver` answers -32603 "Internal server error" (L56-58), wallet +/// or no wallet. +#[tokio::test] +async fn a_null_argument_to_eth_sign_is_rskjs_internal_error() { + let account = format!("{:#x}", test_wallet().accounts()[0]); + for (state, _tmp) in [setup_state(), setup_state_with_wallet()] { + for params in [json!([null, "0xdeadbeef"]), json!([account, null])] { + let resp = dispatch_for_test(&state, make_request("eth_sign", params.clone())).await; + assert!(resp.result.is_none(), "{params}"); + let err = resp.error.unwrap(); + assert_eq!( + (err.code, err.message.as_str()), + (INTERNAL_ERROR, "Internal server error"), + "{params}" + ); + } } } @@ -5843,7 +6018,10 @@ async fn flush_and_shutdown_are_invisible_without_rpc_admin() { for method in ["rsk_flush", "rsk_shutdown"] { let resp = dispatch_for_test(&state, make_request(method, json!([]))).await; let err = resp.error.expect("must not be served"); - assert_eq!(err.message, "Method not found", "{method} leaked its existence"); + assert_eq!( + err.message, "method not found", + "{method} leaked its existence" + ); } } diff --git a/crates/rpc/src/wallet.rs b/crates/rpc/src/wallet.rs index ba99e3b..08eadb7 100644 --- a/crates/rpc/src/wallet.rs +++ b/crates/rpc/src/wallet.rs @@ -10,7 +10,8 @@ //! That reasoning still holds, and this does not weaken it: **a wallet //! exists only when the operator supplies one.** Without //! `--wallet-file`, `RpcState::wallet` is `None`, `eth_accounts` is empty -//! and the three signing methods answer exactly as before. The keys are +//! and the three signing methods are refused, as rskj refuses them with its +//! wallet off. The keys are //! the ones in the file and nothing else; nothing here can reach the node //! identity key or a miner key. //! From 88f92e629ec483ba0437cb0a5b7863797ed941bb Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Tue, 10 Nov 2026 08:24:18 -0800 Subject: [PATCH 090/264] style: rustfmt the lines this branch changed Layout only: rustfmt's changes to the lines changed since upstream's main, chunk by chunk. A chunk that would also rewrite one of upstream's own lines is left as it is. --- crates/cli/examples/loadtest.rs | 6 +- crates/cli/examples/snap_holes.rs | 25 +- crates/core/src/types/header.rs | 12 +- crates/core/src/validation/difficulty.rs | 9 +- crates/core/src/validation/merged_mining.rs | 5 +- crates/core/src/validation/mod.rs | 4 +- crates/core/src/validation/tests.rs | 144 +++++-- crates/core/src/validation/uncles.rs | 12 +- crates/execution/src/bridge/mod.rs | 6 +- crates/execution/src/executor.rs | 191 +++++---- crates/execution/src/mining/tests.rs | 6 +- crates/execution/src/processor.rs | 31 +- crates/execution/tests/mainnet_block_rules.rs | 4 +- crates/networking/src/peers.rs | 18 +- crates/rpc/src/tests.rs | 91 ++++- crates/storage/src/position.rs | 17 +- crates/sync/src/manager.rs | 10 +- crates/sync/src/sampler.rs | 29 +- crates/sync/src/service.rs | 26 +- crates/sync/src/snap/client.rs | 11 +- crates/sync/src/snap/driver.rs | 17 +- crates/sync/src/snap/header_tests.rs | 183 +++++++-- crates/sync/src/snap/headers.rs | 27 +- crates/sync/src/snap/session.rs | 4 +- crates/sync/src/snap/session_tests.rs | 238 ++++++++--- crates/sync/src/snap/tests.rs | 80 +++- crates/sync/src/tests.rs | 373 ++++++++++++++---- crates/trie/src/node.rs | 42 +- crates/trie/src/snapshot_legacy.rs | 19 +- crates/trie/tests/rskj_vectors.rs | 125 ++++-- 30 files changed, 1360 insertions(+), 405 deletions(-) diff --git a/crates/cli/examples/loadtest.rs b/crates/cli/examples/loadtest.rs index 8e2502d..020ccea 100644 --- a/crates/cli/examples/loadtest.rs +++ b/crates/cli/examples/loadtest.rs @@ -786,7 +786,11 @@ fn seal_merged_mining(header: &mut Header) -> Result<()> { // From block 449 the hash's last twelve bytes are fork-detection data, // and any twelve do here: this measures proof of work, not fork // detection. Below 449 rskj wants the whole hash. - let fork_data: &[u8] = if header.number >= REQUIRED_BLOCKS { &[0u8; 12] } else { &[] }; + let fork_data: &[u8] = if header.number >= REQUIRED_BLOCKS { + &[0u8; 12] + } else { + &[] + }; let payload = fork_detection::apply_to_hash(header.hash_for_merged_mining(), fork_data); let cb = coinbase::build_coinbase(&payload, 0); diff --git a/crates/cli/examples/snap_holes.rs b/crates/cli/examples/snap_holes.rs index 9e4dd32..1cf1c53 100644 --- a/crates/cli/examples/snap_holes.rs +++ b/crates/cli/examples/snap_holes.rs @@ -126,7 +126,9 @@ fn walk(node: &TrieNode, start: u64, store: &dyn TrieStore, h: &mut Holes) { fn main() -> anyhow::Result<()> { let a: Vec = std::env::args().collect(); if a.len() < 3 { - eprintln!("usage: snap_holes [limit-bytes] [grid] [served]"); + eprintln!( + "usage: snap_holes [limit-bytes] [grid] [served]" + ); std::process::exit(2); } let dir = &a[1]; @@ -148,7 +150,8 @@ fn main() -> anyhow::Result<()> { header.state_root }; - let trie_dir = std::env::var("RUSTOCK_TRIE_DIR").unwrap_or_else(|_| format!("{dir}/trie-epochs")); + let trie_dir = + std::env::var("RUSTOCK_TRIE_DIR").unwrap_or_else(|_| format!("{dir}/trie-epochs")); let store = EpochTrieStore::open_read_only(&trie_dir, EpochConfig::default())?; let message = store .get(root_hash.as_slice()) @@ -178,7 +181,11 @@ fn main() -> anyhow::Result<()> { .expect("walk") }); - println!("state root {root_hash:?}, {total} bytes; walked offsets 0..{} in {:?}", h.limit, started.elapsed()); + println!( + "state root {root_hash:?}, {total} bytes; walked offsets 0..{} in {:?}", + h.limit, + started.elapsed() + ); println!( "present: {} nodes, {} bytes of entries; missing: {} subtree(s), {} bytes ({:.1}% of the span walked)", h.present_nodes, @@ -210,9 +217,17 @@ fn main() -> anyhow::Result<()> { (grid.saturating_sub(served)) as f64 * 100.0 / grid as f64 ); println!(); - println!("{:>14} {:>14} {:>9} {:>8} {:>8} exact", "start", "end", "bytes", "start%g", "end%g"); + println!( + "{:>14} {:>14} {:>9} {:>8} {:>8} exact", + "start", "end", "bytes", "start%g", "end%g" + ); for &(s, e, exact) in h.found.iter().take(40) { - println!("{s:>14} {e:>14} {:>9} {:>8} {:>8} {exact}", e - s, s % grid, e % grid); + println!( + "{s:>14} {e:>14} {:>9} {:>8} {:>8} {exact}", + e - s, + s % grid, + e % grid + ); } Ok(()) } diff --git a/crates/core/src/types/header.rs b/crates/core/src/types/header.rs index 28344bf..ecfa1f6 100644 --- a/crates/core/src/types/header.rs +++ b/crates/core/src/types/header.rs @@ -1400,7 +1400,7 @@ impl Header { #[cfg(test)] mod tests { use super::*; - use alloy_primitives::{Address, B256, U256, Bytes, keccak256, b256, address, hex}; + use alloy_primitives::{address, b256, hex, keccak256, Address, Bytes, B256, U256}; use alloy_rlp::{Decodable, Encodable}; /// A mainnet header from below RSKIPUMM, as the heights `Header::decode` @@ -1444,11 +1444,12 @@ mod tests { header.encode(&mut buffer); // Decode - let decoded_header = Header::decode(&mut buffer.as_slice()).expect("Failed to decode header"); + let decoded_header = + Header::decode(&mut buffer.as_slice()).expect("Failed to decode header"); // Assert assert_eq!(header, decoded_header); - + // Hash check (just to ensure it doesn't panic) let hash = header.hash(); assert_ne!(hash, B256::ZERO); @@ -2904,7 +2905,10 @@ mod versioned_wire_header_tests { // Testnet headers, so testnet's RSKIP-144 height. let testnet = crate::config::ActivationHeights::testnet(); let h = Header::decode_with_activations(&mut slice, &testnet).unwrap(); - assert_eq!((h.cached_hash, h.cached_hash_for_merged_mining), (None, None)); + assert_eq!( + (h.cached_hash, h.cached_hash_for_merged_mining), + (None, None) + ); h } diff --git a/crates/core/src/validation/difficulty.rs b/crates/core/src/validation/difficulty.rs index fecfe2f..5478a4b 100644 --- a/crates/core/src/validation/difficulty.rs +++ b/crates/core/src/validation/difficulty.rs @@ -230,12 +230,13 @@ mod rskip97_tests { /// and its difficulty falls. #[test] fn the_expected_time_wraps_as_a_java_int() { - let rule = DifficultyRule { config: Arc::new(ChainConfig::mainnet()) }; + let rule = DifficultyRule { + config: Arc::new(ChainConfig::mainnet()), + }; let parent = header_at(5_000_000, 1_000_000, U256::from(10u128.pow(21))); let step = parent.difficulty / U256::from(400); - let child = |uncles: u64| { - rule.difficulty_for_child(&parent, 5_000_001, 1_000_000 + 10, uncles) - }; + let child = + |uncles: u64| rule.difficulty_for_child(&parent, 5_000_001, 1_000_000 + 10, uncles); // (1 + 1) * 14: 28 seconds expected, and 10 is sooner. assert_eq!(child(1), parent.difficulty + step); diff --git a/crates/core/src/validation/merged_mining.rs b/crates/core/src/validation/merged_mining.rs index 0a4cbcc..0f3427a 100644 --- a/crates/core/src/validation/merged_mining.rs +++ b/crates/core/src/validation/merged_mining.rs @@ -121,7 +121,6 @@ pub fn difficulty_to_target(difficulty: alloy_primitives::U256) -> alloy_primiti below + U256::from(1) } else { below - } } @@ -305,7 +304,9 @@ impl HeaderValidator for MergedMiningRule { // to look for. if let Some(umm_root) = &header.umm_root { if !umm_root.is_empty() && umm_root.len() != UMM_ROOT_LENGTH { - return Err(ValidationError::UmmRootInvalid { len: umm_root.len() }); + return Err(ValidationError::UmmRootInvalid { + len: umm_root.len(), + }); } } let rsk_hash = header.hash_for_merged_mining(); diff --git a/crates/core/src/validation/mod.rs b/crates/core/src/validation/mod.rs index 70da361..1b387b1 100644 --- a/crates/core/src/validation/mod.rs +++ b/crates/core/src/validation/mod.rs @@ -123,7 +123,9 @@ pub enum ValidationError { #[error("Fork detection data could not be read from the coinbase")] ForkDetectionDataUnreadable, - #[error("The uncle counts of the 32 blocks below sum to {sum} as a Java int, which rskj refuses")] + #[error( + "The uncle counts of the 32 blocks below sum to {sum} as a Java int, which rskj refuses" + )] UncleCountsSumBelowZero { sum: i32 }, #[error("Invalid transaction execution sublist edges: {reason}")] diff --git a/crates/core/src/validation/tests.rs b/crates/core/src/validation/tests.rs index 915faae..87b6950 100644 --- a/crates/core/src/validation/tests.rs +++ b/crates/core/src/validation/tests.rs @@ -1368,7 +1368,9 @@ fn no_gas_limit_is_too_large() { /// parent's limit, the lower bound is one below `parent - parent / 1024`. #[test] fn the_parent_gas_limit_bounds_are_rskjs_when_the_divisor_leaves_a_remainder() { - let rule = BlockParentGasLimitRule { config: Arc::new(crate::config::ChainConfig::mainnet()) }; + let rule = BlockParentGasLimitRule { + config: Arc::new(crate::config::ChainConfig::mainnet()), + }; let mut parent = create_dummy_header(10, 1000, B256::ZERO); parent.gas_limit = U256::from(6_800_000); let with = |limit: u64| { @@ -1384,7 +1386,9 @@ fn the_parent_gas_limit_bounds_are_rskjs_when_the_divisor_leaves_a_remainder() { Err(ValidationError::GasLimitInvalid { .. }) )); assert_eq!(rule.validate_with_parent(&with(6_806_640), &parent), Ok(())); - assert!(rule.validate_with_parent(&with(6_806_641), &parent).is_err()); + assert!(rule + .validate_with_parent(&with(6_806_641), &parent) + .is_err()); } /// Past 256 bits, where the typed field holds `U256::MAX`, rskj still reads @@ -1448,7 +1452,9 @@ fn the_parent_gas_limit_bounds_hold_past_256_bits() { /// merkle proof, which is why it needs a check of its own. #[test] fn merged_mining_wants_exactly_a_bitcoin_header() { - let rule = MergedMiningRule { config: Arc::new(crate::config::ChainConfig::regtest()) }; + let rule = MergedMiningRule { + config: Arc::new(crate::config::ChainConfig::regtest()), + }; let mut header = create_dummy_header(10, 1000, B256::ZERO); header.difficulty = U256::from(1); let (btc_h, btc_cb, btc_m) = build_mm_proof(&header, 0x207fffff, None); @@ -1458,7 +1464,10 @@ fn merged_mining_wants_exactly_a_bitcoin_header() { let mut longer = btc_h.to_vec(); longer.push(0); header.bitcoin_merged_mining_header = Some(Bytes::from(longer)); - assert_eq!(rule.validate(&header), Err(ValidationError::BitcoinHeaderDecodeError)); + assert_eq!( + rule.validate(&header), + Err(ValidationError::BitcoinHeaderDecodeError) + ); header.bitcoin_merged_mining_header = Some(btc_h); assert_eq!(rule.validate(&header), Ok(())); @@ -1516,7 +1525,10 @@ impl fork_detection::MainchainView for NoChain { fn fork_detection_starts_above_block_449() { for number in [100, 449] { let header = create_dummy_header(number, 1000, B256::ZERO); - assert_eq!(fork_detection::validate(&header, Some([7; 12]), &NoChain, 0), Ok(())); + assert_eq!( + fork_detection::validate(&header, Some([7; 12]), &NoChain, 0), + Ok(()) + ); } let header = create_dummy_header(450, 1000, B256::ZERO); assert_eq!( @@ -1643,8 +1655,10 @@ fn uncle_chain(base: u64) -> (Ancestry, Header, Header) { fn uncle_of(parent: &Header, timestamp: u64) -> Header { let mut uncle = create_dummy_header(parent.number + 1, timestamp, parent.hash()); uncle.beneficiary = Address::repeat_byte(0x0b); - uncle.difficulty = DifficultyRule { config: uncle_config() } - .difficulty_for_child(parent, uncle.number, timestamp, 0); + uncle.difficulty = DifficultyRule { + config: uncle_config(), + } + .difficulty_for_child(parent, uncle.number, timestamp, 0); uncle } @@ -1775,7 +1789,10 @@ fn the_uncle_rule_runs_fork_detection_when_asked() { let mut uncle = uncle_of(&parent, 1010); seal(&mut uncle, 1010); - let asked = uncles::UncleForkDetection { view: &NoChain, rskip110_height: 0 }; + let asked = uncles::UncleForkDetection { + view: &NoChain, + rskip110_height: 0, + }; assert_eq!( judge(&ancestry, &block, uncle.clone(), Some(asked)), Err(ValidationError::ForkDetectionDataUnreadable) @@ -1814,11 +1831,21 @@ fn default_rsk_holds_uncles_to_rskjs_uncle_rules() { let mut adrift = uncle_of(&parent, 1010); seal(&mut adrift, 1010 + 300); - for verdict in [verifier.verify_uncle(&adrift), verifier.verify(&adrift, None)] { - assert!(matches!(verdict, Err(ValidationError::BitcoinTimestampTooFar { .. }))); + for verdict in [ + verifier.verify_uncle(&adrift), + verifier.verify(&adrift, None), + ] { + assert!(matches!( + verdict, + Err(ValidationError::BitcoinTimestampTooFar { .. }) + )); } - assert_eq!(verifier.uncle_parent_rules().len(), 5, "rskj's five, in its order"); + assert_eq!( + verifier.uncle_parent_rules().len(), + 5, + "rskj's five, in its order" + ); } /// rskj's trunk takes the timestamp rule on the header alone. A block no @@ -1907,7 +1934,9 @@ fn wire(display: [u8; 32]) -> [u8; 32] { } fn regtest_merged_mining() -> MergedMiningRule { - MergedMiningRule { config: Arc::new(crate::config::ChainConfig::regtest()) } + MergedMiningRule { + config: Arc::new(crate::config::ChainConfig::regtest()), + } } /// rskj compares the root its proof folds to in bitcoinj's display order, @@ -1940,23 +1969,40 @@ fn the_coinbase_must_run_past_64_bytes() { let header = header_at(10); let hash = header.hash_for_merged_mining(); let judge = |absorbed: u64, after: usize| { - let coinbase = compressed_coinbase(absorbed, &tail_committing_to(hash.as_slice(), 0, after)); + let coinbase = + compressed_coinbase(absorbed, &tail_committing_to(hash.as_slice(), 0, after)); let root = merged_mining::compute_coinbase_hash(&coinbase); - regtest_merged_mining().validate(&mined_through(header.clone(), coinbase, Vec::new(), wire(root))) + regtest_merged_mining().validate(&mined_through( + header.clone(), + coinbase, + Vec::new(), + wire(root), + )) }; // The tag and the hash are 41 bytes of tail. - assert_eq!(judge(0, 23), Err(ValidationError::CoinbaseTooShort { length: 64 })); + assert_eq!( + judge(0, 23), + Err(ValidationError::CoinbaseTooShort { length: 64 }) + ); assert_eq!(judge(0, 24), Ok(())); - assert_eq!(judge(23, 0), Err(ValidationError::CoinbaseTooShort { length: 64 })); + assert_eq!( + judge(23, 0), + Err(ValidationError::CoinbaseTooShort { length: 64 }) + ); assert_eq!(judge(24, 0), Ok(())); // `u64::MAX` is -1 to a Java `long`, and takes a byte off. - assert_eq!(judge(u64::MAX, 24), Err(ValidationError::CoinbaseTooShort { length: 64 })); + assert_eq!( + judge(u64::MAX, 24), + Err(ValidationError::CoinbaseTooShort { length: 64 }) + ); assert_eq!(judge(u64::MAX, 25), Ok(())); // A count that carries the sum past `i64::MAX` wraps it negative. assert_eq!( judge(i64::MAX as u64, 25), - Err(ValidationError::CoinbaseTooShort { length: i64::MAX.wrapping_add(66) }) + Err(ValidationError::CoinbaseTooShort { + length: i64::MAX.wrapping_add(66) + }) ); } @@ -2023,7 +2069,12 @@ fn a_coinbase_hashes_from_its_midstate_as_bouncycastle_does() { let header = header_at(10); let tail = tail_committing_to(header.hash_for_merged_mining().as_slice(), 0, 24); let root = bouncycastle_coinbase_hash(1, &tail); - let mined = mined_through(header, compressed_coinbase(1, &tail), Vec::new(), wire(root)); + let mined = mined_through( + header, + compressed_coinbase(1, &tail), + Vec::new(), + wire(root), + ); assert_eq!(regtest_merged_mining().validate(&mined), Ok(())); } @@ -2051,7 +2102,11 @@ fn rskj_refuses_merkle_nodes_that_parse_as_a_transaction() { for input in 0..=4 { let (left, right) = transaction_shaped(input, 4 - input); - assert!(parses(&left, &right), "{input} + {} bytes of script", 4 - input); + assert!( + parses(&left, &right), + "{input} + {} bytes of script", + 4 - input + ); } for (input, output) in [(1, 2), (2, 3)] { let (left, right) = transaction_shaped(input, output); @@ -2090,7 +2145,11 @@ fn rskj_refuses_merkle_nodes_that_parse_as_a_transaction() { (2_100_000_000_000_000, true), (2_100_000_000_000_001, false), ] { - assert_eq!(edited(49, &value.to_le_bytes()), parses_as_one, "{value} satoshis"); + assert_eq!( + edited(49, &value.to_le_bytes()), + parses_as_one, + "{value} satoshis" + ); } } @@ -2115,10 +2174,21 @@ fn a_merkle_proof_through_a_transaction_shaped_pair_is_refused() { .expect("a coinbase hash with 1 as its fifth byte"); let (_, sibling) = transaction_shaped(2, 2); - assert!(merged_mining::parses_as_a_64_byte_transaction(&coinbase_hash, &sibling)); - assert_eq!(merged_mining::rskip92_merkle_root(&coinbase_hash, &sibling), None); + assert!(merged_mining::parses_as_a_64_byte_transaction( + &coinbase_hash, + &sibling + )); + assert_eq!( + merged_mining::rskip92_merkle_root(&coinbase_hash, &sibling), + None + ); let root = merged_mining::combine_left_right(&coinbase_hash, &sibling); - let mined = mined_through(header.clone(), coinbase.clone(), sibling.to_vec(), wire(root)); + let mined = mined_through( + header.clone(), + coinbase.clone(), + sibling.to_vec(), + wire(root), + ); assert_eq!( regtest_merged_mining().validate(&mined), Err(ValidationError::MerkleNodesFormTransaction) @@ -2128,7 +2198,10 @@ fn a_merkle_proof_through_a_transaction_shaped_pair_is_refused() { // block, with the root that sibling folds to, is merge-mined. let mut other = sibling; other[57 - 32] = 3; - assert!(!merged_mining::parses_as_a_64_byte_transaction(&coinbase_hash, &other)); + assert!(!merged_mining::parses_as_a_64_byte_transaction( + &coinbase_hash, + &other + )); let root = merged_mining::combine_left_right(&coinbase_hash, &other); let mined = mined_through(header, coinbase, other.to_vec(), wire(root)); assert_eq!(regtest_merged_mining().validate(&mined), Ok(())); @@ -2146,7 +2219,11 @@ fn a_umm_root_is_twenty_bytes_or_none() { if valid { assert_eq!(verdict, Ok(()), "{len} bytes"); } else { - assert_eq!(verdict, Err(ValidationError::UmmRootInvalid { len }), "{len} bytes"); + assert_eq!( + verdict, + Err(ValidationError::UmmRootInvalid { len }), + "{len} bytes" + ); } } } @@ -2162,7 +2239,10 @@ fn fork_detection_data_joins_the_hash_from_wasabi100_and_block_449() { // Regtest has RSKIP110 from genesis, so there the height alone decides; // with wasabi100 at 1,000, it is wasabi100 that does. let late = ChainConfig { - activation_heights: ActivationHeights { wasabi100: 1_000, ..ActivationHeights::regtest() }, + activation_heights: ActivationHeights { + wasabi100: 1_000, + ..ActivationHeights::regtest() + }, ..ChainConfig::regtest() }; for (config, number, altered_valid) in [ @@ -2171,7 +2251,9 @@ fn fork_detection_data_joins_the_hash_from_wasabi100_and_block_449() { (late.clone(), 999, false), (late, 1_000, true), ] { - let rule = MergedMiningRule { config: Arc::new(config) }; + let rule = MergedMiningRule { + config: Arc::new(config), + }; let header = header_at(number); let hash = header.hash_for_merged_mining(); let mut altered = hash; @@ -2187,7 +2269,11 @@ fn fork_detection_data_joins_the_hash_from_wasabi100_and_block_449() { assert_eq!(judge(hash), Ok(()), "block {number}, the whole hash"); if altered_valid { - assert_eq!(judge(altered), Ok(()), "block {number}, fork-detection data"); + assert_eq!( + judge(altered), + Ok(()), + "block {number}, fork-detection data" + ); } else { assert_eq!( judge(altered), diff --git a/crates/core/src/validation/uncles.rs b/crates/core/src/validation/uncles.rs index 57bf5c4..5da7f10 100644 --- a/crates/core/src/validation/uncles.rs +++ b/crates/core/src/validation/uncles.rs @@ -8,8 +8,8 @@ use super::fork_detection::MainchainView; use super::{ BitcoinTimestampRule, BlockNumberRule, BlockParentGasLimitRule, DifficultyRule, GasUsedRule, - HeaderValidator, MergedMiningRule, ParentHeaderValidator, - PrevMinGasPriceRule, TimestampRule, ValidationError, + HeaderValidator, MergedMiningRule, ParentHeaderValidator, PrevMinGasPriceRule, TimestampRule, + ValidationError, }; use crate::config::ChainConfig; use crate::types::block::Block; @@ -35,7 +35,9 @@ pub const UNCLE_GENERATION_LIMIT: u64 = 7; /// one. pub fn rsk_header_rules(config: Arc) -> Vec> { vec![ - Box::new(MergedMiningRule { config: config.clone() }), + Box::new(MergedMiningRule { + config: config.clone(), + }), Box::new(TimestampRule::new(config.max_future_block_time)), Box::new(BitcoinTimestampRule { config }), Box::new(GasUsedRule), @@ -49,7 +51,9 @@ pub fn rsk_parent_rules(config: Arc) -> Vec BridgeMethodEnabled { | "releaseUnionBridgeRbtc" | "setUnionBridgeTransferPermissions" => Rule(RskHardforkConfig::has_rskip502), // RSKIP529 (reed810): the union bridge's super and base events. - "getSuperEvent" - | "setSuperEvent" - | "clearSuperEvent" - | "getBaseEvent" - | "setBaseEvent" + "getSuperEvent" | "setSuperEvent" | "clearSuperEvent" | "getBaseEvent" | "setBaseEvent" | "clearBaseEvent" => Rule(RskHardforkConfig::has_rskip529), // !RSKIP87 / !RSKIP89 "addLockWhitelistAddress" | "getBtcBlockchainBlockLocator" => Until(Orchid), diff --git a/crates/execution/src/executor.rs b/crates/execution/src/executor.rs index b3ab50d..63ac8e1 100644 --- a/crates/execution/src/executor.rs +++ b/crates/execution/src/executor.rs @@ -3829,7 +3829,10 @@ mod tests { /// 21,000 intrinsic + 9 (3 PUSH1) + 3 (PUSH20) + 700 EXTCODECOPY = 21,712. #[test] fn test_extcodecopy_flat_700_at_lovell700() { - assert_eq!(RskHardforkConfig::mainnet().spec_id(7_338_024), SpecId::SHANGHAI); + assert_eq!( + RskHardforkConfig::mainnet().spec_id(7_338_024), + SpecId::SHANGHAI + ); let store = Arc::new(MemoryTrieStore::new()); let root = TrieNode::empty(); @@ -3960,7 +3963,11 @@ mod tests { }; assert_eq!( RskExecutor::extract_created_address(&create_tx, sender, &true), - Some("0xcd234a471b72ba2f1ccf0a70fcaba648a5eecd8d".parse().unwrap()), + Some( + "0xcd234a471b72ba2f1ccf0a70fcaba648a5eecd8d" + .parse() + .unwrap() + ), ); // A reverted CREATE tx leaves no account. assert_eq!( @@ -3999,8 +4006,7 @@ mod tests { let root = put_account(&root, store.as_ref(), &voter1, 0, one_rbtc); let root = put_account(&root, store.as_ref(), &voter2, 0, one_rbtc); - let block_store = - Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let block_store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); let executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); let vote_tx = |nonce: u64| { @@ -4029,7 +4035,12 @@ mod tests { .execute_block(&header1, &[(vote_tx(0), voter1)], &root, store.clone()) .expect("block 1"); assert!(r1.tx_results[0].success); - let root2 = crate::state::apply_state_changes(&root, store.as_ref(), &r1.state_changes, &r1.markers); + let root2 = crate::state::apply_state_changes( + &root, + store.as_ref(), + &r1.state_changes, + &r1.markers, + ); let header2 = dummy_header(632_051); let r2 = executor @@ -4037,9 +4048,8 @@ mod tests { .expect("block 2"); assert!(r2.tx_results[0].success); - let fee_key = crate::bridge::storage::bridge_storage_key( - crate::bridge::storage::FEE_PER_KB_KEY, - ); + let fee_key = + crate::bridge::storage::bridge_storage_key(crate::bridge::storage::FEE_PER_KB_KEY); let fee_write = r2 .markers .raw_storage @@ -4149,8 +4159,7 @@ mod tests { let sender = Address::repeat_byte(0xAA); let one_rbtc = U256::from(10u64).pow(U256::from(18)); let root = put_account(&root, store.as_ref(), &sender, 0, one_rbtc); - let block_store = - Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let block_store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); let executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); // Init code: COINBASE; SSTORE(0, coinbase); RETURN empty runtime. @@ -4168,14 +4177,17 @@ mod tests { to: Bytes::new(), value: U256::ZERO, input: Bytes::from(init), - v: 0, r: U256::ZERO, s: U256::ZERO, cached_rlp: None, + v: 0, + r: U256::ZERO, + s: U256::ZERO, + cached_rlp: None, }; let mut header = dummy_header(892_000); // A distinctive miner address, distinct from both REMASC_ADDR and the // default repeat_byte(0x01). - let miner = Address::from_slice( - &alloy_primitives::hex!("31fe561eb2c628cd32ec52573d7c4b7e4c278bfa"), - ); + let miner = Address::from_slice(&alloy_primitives::hex!( + "31fe561eb2c628cd32ec52573d7c4b7e4c278bfa" + )); header.beneficiary = miner; let r1 = executor @@ -4210,8 +4222,7 @@ mod tests { let sender = Address::repeat_byte(0xAA); let one_rbtc = U256::from(10u64).pow(U256::from(18)); let root = put_account(&root, store.as_ref(), &sender, 0, one_rbtc); - let block_store = - Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let block_store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); let executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); // Runtime: CREATE(0,0,0) (empty init → no-code child); SSTORE(0, child). @@ -4231,7 +4242,10 @@ mod tests { to: Bytes::new(), value: U256::ZERO, input: Bytes::from(init), - v: 0, r: U256::ZERO, s: U256::ZERO, cached_rlp: None, + v: 0, + r: U256::ZERO, + s: U256::ZERO, + cached_rlp: None, }; let header1 = dummy_header(1_591_500); let r1 = executor @@ -4240,11 +4254,16 @@ mod tests { assert!(r1.tx_results[0].success); let deployed = sender.create(0); assert_eq!( - r1.state_changes.get(&deployed).expect("creator").info.nonce, 0, + r1.state_changes.get(&deployed).expect("creator").info.nonce, + 0, "a top-level CREATE tx keeps nonce 0 even post-RSKIP125" ); - let root2 = - crate::state::apply_state_changes(&root, store.as_ref(), &r1.state_changes, &r1.markers); + let root2 = crate::state::apply_state_changes( + &root, + store.as_ref(), + &r1.state_changes, + &r1.markers, + ); // Call the creator → it runs an internal CREATE. let call = rustock_core::Transaction { @@ -4254,7 +4273,10 @@ mod tests { to: Bytes::copy_from_slice(deployed.as_slice()), value: U256::ZERO, input: Bytes::new(), - v: 0, r: U256::ZERO, s: U256::ZERO, cached_rlp: None, + v: 0, + r: U256::ZERO, + s: U256::ZERO, + cached_rlp: None, }; let header2 = dummy_header(1_591_501); let r2 = executor @@ -4264,7 +4286,12 @@ mod tests { let child = deployed.create(0); assert_eq!( - r2.state_changes.get(&child).expect("child contract").info.nonce, 1, + r2.state_changes + .get(&child) + .expect("child contract") + .info + .nonce, + 1, "internal CREATE post-RSKIP125 bumps the created contract nonce to 1" ); } @@ -4294,14 +4321,11 @@ mod tests { let sender = Address::repeat_byte(0xAA); let one_rbtc = U256::from(10u64).pow(U256::from(18)); let root = put_account(&root, store.as_ref(), &sender, 0, one_rbtc); - let block_store = - Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let block_store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); let executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); // Runtime: PUSH1 4; PUSH1 0; SHR (4 >> 0 = 4); PUSH1 0; SSTORE; STOP. - let runtime: Vec = vec![ - 0x60, 0x04, 0x60, 0x00, 0x1c, 0x60, 0x00, 0x55, 0x00, - ]; + let runtime: Vec = vec![0x60, 0x04, 0x60, 0x00, 0x1c, 0x60, 0x00, 0x55, 0x00]; let mut init = vec![ 0x60, 0x09, 0x60, 0x0c, 0x60, 0x00, 0x39, 0x60, 0x09, 0x60, 0x00, 0xf3, ]; @@ -4314,7 +4338,10 @@ mod tests { to: Bytes::new(), value: U256::ZERO, input: Bytes::from(init), - v: 0, r: U256::ZERO, s: U256::ZERO, cached_rlp: None, + v: 0, + r: U256::ZERO, + s: U256::ZERO, + cached_rlp: None, }; let header1 = dummy_header(1_591_500); let r1 = executor @@ -4322,8 +4349,12 @@ mod tests { .expect("deploy block"); assert!(r1.tx_results[0].success); let deployed = sender.create(0); - let root2 = - crate::state::apply_state_changes(&root, store.as_ref(), &r1.state_changes, &r1.markers); + let root2 = crate::state::apply_state_changes( + &root, + store.as_ref(), + &r1.state_changes, + &r1.markers, + ); let call = rustock_core::Transaction { nonce: 1, @@ -4332,13 +4363,19 @@ mod tests { to: Bytes::copy_from_slice(deployed.as_slice()), value: U256::ZERO, input: Bytes::new(), - v: 0, r: U256::ZERO, s: U256::ZERO, cached_rlp: None, + v: 0, + r: U256::ZERO, + s: U256::ZERO, + cached_rlp: None, }; let header2 = dummy_header(1_591_501); let r2 = executor .execute_block(&header2, &[(call, sender)], &root2, store.clone()) .expect("call block"); - assert!(r2.tx_results[0].success, "SHR must be a valid opcode at wasabi"); + assert!( + r2.tx_results[0].success, + "SHR must be a valid opcode at wasabi" + ); assert!( r2.tx_results[0].gas_used < 1_000_000, "SHR call must not consume the whole gas limit (invalid opcode)" @@ -8688,10 +8725,7 @@ mod tests { cached_rlp: None, }; - let executor = RskExecutor::new( - RskHardforkConfig::all_active(33), - block_store, - ); + let executor = RskExecutor::new(RskHardforkConfig::all_active(33), block_store); let result = executor .execute_tx(&header, &tx, sender, &root, store) @@ -8716,9 +8750,7 @@ mod tests { let one_rbtc = U256::from(10u64).pow(U256::from(18)); let root = put_account(&root, store.as_ref(), &sender, 0, one_rbtc); - let block_store = Arc::new( - BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap(), - ); + let block_store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); let header = dummy_header(8_000_000); @@ -8743,29 +8775,34 @@ mod tests { // PUSH1 0x40 ; 60 40 offset // RETURN ; F3 let runtime: Vec = vec![ - 0x63, 0xe8, 0xce, 0x22, 0x74, // PUSH4 0xe8ce2274 - 0x60, 0x00, // PUSH1 0 - 0x52, // MSTORE - 0x60, 0x20, // PUSH1 32 (retSize) - 0x60, 0x40, // PUSH1 64 (retOffset) - 0x60, 0x04, // PUSH1 4 (argsSize) - 0x60, 0x1c, // PUSH1 28 (argsOffset) - 0x63, 0x01, 0x00, 0x00, 0x11, // PUSH4 0x01000011 (Environment addr) - 0x5A, // GAS - 0xFA, // STATICCALL - 0x60, 0x20, // PUSH1 32 (return size) - 0x60, 0x40, // PUSH1 64 (return offset) - 0xF3, // RETURN + 0x63, 0xe8, 0xce, 0x22, 0x74, // PUSH4 0xe8ce2274 + 0x60, 0x00, // PUSH1 0 + 0x52, // MSTORE + 0x60, 0x20, // PUSH1 32 (retSize) + 0x60, 0x40, // PUSH1 64 (retOffset) + 0x60, 0x04, // PUSH1 4 (argsSize) + 0x60, 0x1c, // PUSH1 28 (argsOffset) + 0x63, 0x01, 0x00, 0x00, 0x11, // PUSH4 0x01000011 (Environment addr) + 0x5A, // GAS + 0xFA, // STATICCALL + 0x60, 0x20, // PUSH1 32 (return size) + 0x60, 0x40, // PUSH1 64 (return offset) + 0xF3, // RETURN ]; let runtime_len = runtime.len() as u8; let mut initcode: Vec = vec![ - 0x60, runtime_len, - 0x60, 0x0c, - 0x60, 0x00, + 0x60, + runtime_len, + 0x60, + 0x0c, + 0x60, + 0x00, 0x39, - 0x60, runtime_len, - 0x60, 0x00, + 0x60, + runtime_len, + 0x60, + 0x00, 0xF3, ]; initcode.extend_from_slice(&runtime); @@ -8783,19 +8820,21 @@ mod tests { cached_rlp: None, }; - let executor = RskExecutor::new( - RskHardforkConfig::all_active(33), - block_store.clone(), - ); + let executor = RskExecutor::new(RskHardforkConfig::all_active(33), block_store.clone()); // Deploy let deploy_result = executor .execute_block(&header, &[(tx, sender)], &root, store.clone()) .unwrap(); - assert!(deploy_result.tx_results[0].success, "deployment should succeed"); + assert!( + deploy_result.tx_results[0].success, + "deployment should succeed" + ); - let contract_addr = deploy_result.state_changes.iter() + let contract_addr = deploy_result + .state_changes + .iter() .find(|(addr, acct)| { **addr != sender && **addr != header.beneficiary @@ -8808,16 +8847,28 @@ mod tests { let root2 = TrieNode::empty(); let root2 = put_account(&root2, store.as_ref(), &sender, 1, one_rbtc); - let code_bytes = deploy_result.state_changes.get(&contract_addr) - .unwrap().info.code.as_ref().unwrap().bytes_slice().to_vec(); + let code_bytes = deploy_result + .state_changes + .get(&contract_addr) + .unwrap() + .info + .code + .as_ref() + .unwrap() + .bytes_slice() + .to_vec(); - use rustock_trie::{TrieKeySlice, code_key, AccountState}; + use rustock_trie::{code_key, AccountState, TrieKeySlice}; let ckey = code_key(&contract_addr); let root2 = root2.put(&TrieKeySlice::from_key(&ckey), &code_bytes, store.as_ref()); let caccount = AccountState::new(U256::ZERO, U256::ZERO); let akey = account_key(&contract_addr); - let root2 = root2.put(&TrieKeySlice::from_key(&akey), &caccount.encode(), store.as_ref()); + let root2 = root2.put( + &TrieKeySlice::from_key(&akey), + &caccount.encode(), + store.as_ref(), + ); let call_tx = rustock_core::Transaction { nonce: 1, @@ -8837,7 +8888,11 @@ mod tests { .unwrap(); assert!(call_result.success, "contract call should succeed"); - assert_eq!(call_result.output.len(), 32, "should return 32-byte ABI-encoded uint32"); + assert_eq!( + call_result.output.len(), + 32, + "should return 32-byte ABI-encoded uint32" + ); let depth = u32::from_be_bytes([ call_result.output[28], diff --git a/crates/execution/src/mining/tests.rs b/crates/execution/src/mining/tests.rs index 0b1cab6..21ccb75 100644 --- a/crates/execution/src/mining/tests.rs +++ b/crates/execution/src/mining/tests.rs @@ -353,7 +353,11 @@ fn merkle_proofs_fold_back_to_the_root() { assert_eq!(proof.len() % 32, 0); let root = merkle::root_from_proof(&txids[0], &proof); - assert_eq!(root, Some(reference_merkle_root(&txids)), "{count} transactions"); + assert_eq!( + root, + Some(reference_merkle_root(&txids)), + "{count} transactions" + ); } } diff --git a/crates/execution/src/processor.rs b/crates/execution/src/processor.rs index e1b0c17..a258608 100644 --- a/crates/execution/src/processor.rs +++ b/crates/execution/src/processor.rs @@ -753,10 +753,12 @@ impl BlockProcessor { parent_rules: &parent_rules, // Under the same switch as the block's own fork-detection check // below, and for the same cost: 449 headers per uncle. - fork_detection: self.validate_fork_detection_data.then_some(UncleForkDetection { - view: &ancestry, - rskip110_height: self.rskip110_height(), - }), + fork_detection: self + .validate_fork_detection_data + .then_some(UncleForkDetection { + view: &ancestry, + rskip110_height: self.rskip110_height(), + }), } .validate(block)?; @@ -3153,7 +3155,10 @@ mod tests { ); seal(&mut uncle, &[]); - assert_eq!(processor.validate_block_rules(&naming(3, &b2, &uncle, &[]), Some(&b2)), Ok(())); + assert_eq!( + processor.validate_block_rules(&naming(3, &b2, &uncle, &[]), Some(&b2)), + Ok(()) + ); // Proof of work is not all: against its parent it must come later. let mut early = child_of(2, &b1, b1.timestamp); @@ -3204,7 +3209,11 @@ mod tests { let tip = chain.len() - 1; let parent = &chain[tip]; let uncle_parent = &chain[tip - 2]; - let mut uncle = child_of(uncle_parent.number + 1, uncle_parent, uncle_parent.timestamp + 5); + let mut uncle = child_of( + uncle_parent.number + 1, + uncle_parent, + uncle_parent.timestamp + 5, + ); uncle.beneficiary = Address::repeat_byte(0x0b); let mut right = uncle.clone(); seal(&mut right, &data_from(tip - 2)); @@ -3214,12 +3223,18 @@ mod tests { let number = parent.number + 1; let block = |uncle: &Header| naming(number, parent, uncle, &data_from(tip)); - assert_eq!(processor(true).validate_block_rules(&block(&right), Some(parent)), Ok(())); + assert_eq!( + processor(true).validate_block_rules(&block(&right), Some(parent)), + Ok(()) + ); assert!(matches!( processor(true).validate_block_rules(&block(&wrong), Some(parent)), Err(ValidationError::ForkDetectionDataMismatch { .. }) )); - assert_eq!(processor(false).validate_block_rules(&block(&wrong), Some(parent)), Ok(())); + assert_eq!( + processor(false).validate_block_rules(&block(&wrong), Some(parent)), + Ok(()) + ); } } diff --git a/crates/execution/tests/mainnet_block_rules.rs b/crates/execution/tests/mainnet_block_rules.rs index 3da59e5..69979cf 100644 --- a/crates/execution/tests/mainnet_block_rules.rs +++ b/crates/execution/tests/mainnet_block_rules.rs @@ -188,7 +188,9 @@ fn block_rules_accept_every_sampled_mainnet_block() { #[test] #[ignore = "requires a synced block store (RUSTOCK_BLOCKS_DB)"] fn uncle_rules_accept_real_uncle_bearing_blocks() { - use rustock_core::validation::{fork_detection::REQUIRED_BLOCKS, uncles::UNCLE_GENERATION_LIMIT}; + use rustock_core::validation::{ + fork_detection::REQUIRED_BLOCKS, uncles::UNCLE_GENERATION_LIMIT, + }; let Some(path) = db_path() else { eprintln!("RUSTOCK_BLOCKS_DB not set; skipping"); diff --git a/crates/networking/src/peers.rs b/crates/networking/src/peers.rs index 560ba4e..668b2a7 100644 --- a/crates/networking/src/peers.rs +++ b/crates/networking/src/peers.rs @@ -568,10 +568,24 @@ mod snap_capability_tests { let rsk63 = B512::repeat_byte(1); let rsk62_snap = B512::repeat_byte(2); store - .add_peer_with(rsk63, tx.clone(), PeerCapabilities { rsk_version: 63, snap: false }) + .add_peer_with( + rsk63, + tx.clone(), + PeerCapabilities { + rsk_version: 63, + snap: false, + }, + ) .await; store - .add_peer_with(rsk62_snap, tx, PeerCapabilities { rsk_version: 62, snap: true }) + .add_peer_with( + rsk62_snap, + tx, + PeerCapabilities { + rsk_version: 62, + snap: true, + }, + ) .await; assert_eq!(store.peers_sending_header_uncles().await, vec![rsk63]); diff --git a/crates/rpc/src/tests.rs b/crates/rpc/src/tests.rs index 2a137c5..df39dc9 100644 --- a/crates/rpc/src/tests.rs +++ b/crates/rpc/src/tests.rs @@ -4743,13 +4743,24 @@ async fn a_block_with_full_transactions_names_the_real_sender() { for (method, arg) in [ ("eth_getBlockByNumber", json!(["0x2a", true])), - ("eth_getBlockByHash", json!([format!("{:#x}", state.store.canonical_hash(42).unwrap().unwrap()), true])), + ( + "eth_getBlockByHash", + json!([ + format!("{:#x}", state.store.canonical_hash(42).unwrap().unwrap()), + true + ]), + ), ] { let resp = dispatch_for_test(&state, make_request(method, arg)).await; let block = resp.result.expect("the block must be returned"); - let txs = block["transactions"].as_array().expect("full transaction objects"); + let txs = block["transactions"] + .as_array() + .expect("full transaction objects"); assert_eq!(txs.len(), 1, "{method}"); - assert_eq!(txs[0]["from"], expected, "{method} reported the wrong sender"); + assert_eq!( + txs[0]["from"], expected, + "{method} reported the wrong sender" + ); assert_ne!( txs[0]["from"], json!("0x0000000000000000000000000000000000000000"), @@ -7137,14 +7148,22 @@ fn assert_hangs_from(nodes: &Value, state_root: B256, trie_store: &dyn rustock_t .map(|bytes| TrieNode::from_message(&bytes, trie_store)) .collect(); assert!(parsed.len() > 1, "the path should span more than one level"); - assert_eq!(parsed.last().unwrap().compute_hash(trie_store), state_root, "not the block's state root"); + assert_eq!( + parsed.last().unwrap().compute_hash(trie_store), + state_root, + "not the block's state root" + ); for pair in parsed.windows(2) { let (child, parent) = (&pair[0], &pair[1]); let message = child.to_message(trie_store); - let linked = [&parent.left, &parent.right] - .iter() - .any(|r| r.resolve(trie_store).is_some_and(|c| c.to_message(trie_store) == message)); - assert!(linked, "a node on the path is not a child of the one above it"); + let linked = [&parent.left, &parent.right].iter().any(|r| { + r.resolve(trie_store) + .is_some_and(|c| c.to_message(trie_store) == message) + }); + assert!( + linked, + "a node on the path is not a child of the one above it" + ); } } @@ -7207,8 +7226,16 @@ async fn a_light_client_reads_a_contracts_storage_from_the_answer_alone() { .put(&TrieKeySlice::from_key(&key(3)), &[7], &*trie_store); root.save(&*trie_store, true); let root_43 = root.compute_hash(&*trie_store); - TrieStore::put(&*trie_store, root_43.as_slice(), &root.to_message(&*trie_store)); - let header = Header { parent_hash: hash_42, state_root: root_43, ..test_header(43) }; + TrieStore::put( + &*trie_store, + root_43.as_slice(), + &root.to_message(&*trie_store), + ); + let header = Header { + parent_hash: hash_42, + state_root: root_43, + ..test_header(43) + }; let hash_43 = header.hash(); state.store.put_header(&header).unwrap(); state.store.put_canonical_hash(43, hash_43).unwrap(); @@ -7219,13 +7246,20 @@ async fn a_light_client_reads_a_contracts_storage_from_the_answer_alone() { ("0x2a", hash_42, [Some(42u8), Some(99), None]), ("0x2b", hash_43, [Some(43), None, Some(7)]), ] { - let raw = dispatch_for_test(&state, make_request("rsk_getRawBlockHeaderByNumber", json!([block]))) - .await - .result - .unwrap(); + let raw = dispatch_for_test( + &state, + make_request("rsk_getRawBlockHeaderByNumber", json!([block])), + ) + .await + .result + .unwrap(); let raw = hex::decode(raw.as_str().unwrap().trim_start_matches("0x")).unwrap(); let header = Header::decode_with_hash(&mut raw.as_slice()).unwrap(); - assert_eq!(header.hash(), trusted, "block {block}: the header is the trusted block's"); + assert_eq!( + header.hash(), + trusted, + "block {block}: the header is the trusted block's" + ); let req = make_request( "rsk_getStorageNodes", @@ -7235,7 +7269,11 @@ async fn a_light_client_reads_a_contracts_storage_from_the_answer_alone() { for (i, want) in want.iter().enumerate() { let slot = i as u8 + 1; assert_eq!( - read_slot_as_a_light_client(&r["storage"][i]["nodes"], header.state_root, &key(slot)), + read_slot_as_a_light_client( + &r["storage"][i]["nodes"], + header.state_root, + &key(slot) + ), Ok(want.map(|v| vec![v])), "block {block}, slot {slot}" ); @@ -7268,10 +7306,17 @@ async fn a_light_client_reads_a_contracts_storage_from_the_answer_alone() { async fn storage_nodes_prove_a_slot_that_is_there() { let (state, _tmp, addr, state_root) = setup_proof_state(); let trie_store = state.trie_store.clone().unwrap(); - let req = make_request("rsk_getStorageNodes", json!([format!("{addr:#x}"), ["0x1"], "latest"])); + let req = make_request( + "rsk_getStorageNodes", + json!([format!("{addr:#x}"), ["0x1"], "latest"]), + ); let r = dispatch_for_test(&state, req).await.result.unwrap(); - assert_eq!(r["blockNumber"], json!("0x2a"), "the block the nodes hang from is named"); + assert_eq!( + r["blockNumber"], + json!("0x2a"), + "the block the nodes hang from is named" + ); assert_eq!(r["stateRoot"], json!(format!("{state_root:#x}"))); let slot = &r["storage"][0]; assert_eq!(slot["present"], json!(true)); @@ -7286,7 +7331,10 @@ async fn storage_nodes_prove_a_slot_that_is_there() { async fn storage_nodes_prove_a_slot_that_is_not() { let (state, _tmp, addr, state_root) = setup_proof_state(); let trie_store = state.trie_store.clone().unwrap(); - let req = make_request("rsk_getStorageNodes", json!([format!("{addr:#x}"), ["0x3"], "latest"])); + let req = make_request( + "rsk_getStorageNodes", + json!([format!("{addr:#x}"), ["0x3"], "latest"]), + ); let r = dispatch_for_test(&state, req).await.result.unwrap(); let slot = &r["storage"][0]; @@ -7294,7 +7342,10 @@ async fn storage_nodes_prove_a_slot_that_is_not() { assert_eq!(slot["value"], json!("0x0")); assert_hangs_from(&slot["nodes"], state_root, &*trie_store); - let req = make_request("eth_getProof", json!([format!("{addr:#x}"), ["0x3"], "latest"])); + let req = make_request( + "eth_getProof", + json!([format!("{addr:#x}"), ["0x3"], "latest"]), + ); let r = dispatch_for_test(&state, req).await.result.unwrap(); assert_eq!( r["storageProof"][0]["proof"].as_array().unwrap().len(), diff --git a/crates/storage/src/position.rs b/crates/storage/src/position.rs index b07e776..e21aa46 100644 --- a/crates/storage/src/position.rs +++ b/crates/storage/src/position.rs @@ -835,7 +835,11 @@ mod tests { parent = h.hash(); hs.push(h); } - assert_eq!(store.exec_head().unwrap(), None, "precondition: nothing executed"); + assert_eq!( + store.exec_head().unwrap(), + None, + "precondition: nothing executed" + ); (store, dir, hs) } @@ -849,7 +853,10 @@ mod tests { let v = Validated::prove(&store, hs[9].hash()).unwrap(); let cursor = store.apply(&Transition::Retreat { to: v }).unwrap(); - assert_eq!(cursor.validated_head.number, 9, "the retreat itself happens"); + assert_eq!( + cursor.validated_head.number, 9, + "the retreat itself happens" + ); assert_eq!( store.exec_head().unwrap(), None, @@ -870,7 +877,11 @@ mod tests { stash(&store, h); } let v = Validated::prove(&store, c.hash()).unwrap(); - assert_eq!(v.lineage()[0].0, 5, "precondition: the fork rewrites from #5"); + assert_eq!( + v.lineage()[0].0, + 5, + "precondition: the fork rewrites from #5" + ); let cursor = store.apply(&Transition::Adopt { head: v }).unwrap(); diff --git a/crates/sync/src/manager.rs b/crates/sync/src/manager.rs index e641d47..5804ebd 100644 --- a/crates/sync/src/manager.rs +++ b/crates/sync/src/manager.rs @@ -180,7 +180,8 @@ impl SyncManager { let Some(uncles) = crate::total_difficulty::held_uncles(&self.store, hash, header) else { return header.difficulty; }; - match crate::total_difficulty::credited_uncles(&self.store, &self.verifier, header, &uncles) { + match crate::total_difficulty::credited_uncles(&self.store, &self.verifier, header, &uncles) + { Ok(credited) => header.difficulty.saturating_add(credited), Err(why) => { debug!( @@ -214,7 +215,12 @@ impl SyncManager { /// Settle one block's total difficulty from the uncles its body names. /// `false` when it cannot be settled: its parent has no total, or the /// store failed. - pub(crate) fn settle_total_difficulty(&self, hash: B256, header: &Header, uncles: &[Header]) -> bool { + pub(crate) fn settle_total_difficulty( + &self, + hash: B256, + header: &Header, + uncles: &[Header], + ) -> bool { match crate::total_difficulty::settle(&self.store, &self.verifier, hash, header, uncles) { Ok(settled) => settled.is_some(), Err(e) => { diff --git a/crates/sync/src/sampler.rs b/crates/sync/src/sampler.rs index 098f10b..8b26974 100644 --- a/crates/sync/src/sampler.rs +++ b/crates/sync/src/sampler.rs @@ -795,7 +795,9 @@ impl SamplingGate { // starting with any skeleton nobody answered: until it is in, the // window is not covered, and nothing else will ask for it again. while self.skeletons_outstanding < 4 { - let Some(start) = self.retry_skeletons.pop() else { break }; + let Some(start) = self.retry_skeletons.pop() else { + break; + }; out.push(GateAction::RequestSkeleton { start }); self.skeletons_outstanding += 1; } @@ -1017,7 +1019,18 @@ mod gate_tests { let blocks = 20_000u64; let head = cp.number + blocks; let honest = cp.cumulative_difficulty + cp.difficulty * U256::from(blocks); - let mut gate = SamplingGate::new(cp, honest, head, DIV, MIN, SPAN, CheckpointDefence { verify_hash: false, bound_work: true }); + let mut gate = SamplingGate::new( + cp, + honest, + head, + DIV, + MIN, + SPAN, + CheckpointDefence { + verify_hash: false, + bound_work: true, + }, + ); let verifier = HeaderVerifier::new(); // The first skeleton goes unanswered and is handed back; the rest of @@ -1031,7 +1044,11 @@ mod gate_tests { } let lost = lost.expect("a skeleton is asked for"); gate.release_skeleton(lost); - assert_eq!(gate.outcome(), GateOutcome::Pending, "judged without the lost skeleton"); + assert_eq!( + gate.outcome(), + GateOutcome::Pending, + "judged without the lost skeleton" + ); let mut asked_again = false; for _ in 0..64 { @@ -1045,7 +1062,11 @@ mod gate_tests { } } assert!(asked_again, "the lost skeleton was never asked again"); - assert!(matches!(gate.outcome(), GateOutcome::Passed { .. }), "{:?}", gate.outcome()); + assert!( + matches!(gate.outcome(), GateOutcome::Passed { .. }), + "{:?}", + gate.outcome() + ); } /// The gate does not reach a verdict before the window is covered — diff --git a/crates/sync/src/service.rs b/crates/sync/src/service.rs index 3625d4a..5617f0d 100644 --- a/crates/sync/src/service.rs +++ b/crates/sync/src/service.rs @@ -1350,7 +1350,11 @@ impl SyncService { let outbound = step( driver, - crate::snap::driver::Peers { snap: &snap, uncles: &uncles, any: &any }, + crate::snap::driver::Peers { + snap: &snap, + uncles: &uncles, + any: &any, + }, ); // Charge whatever the step turned up before anything else: a peer that @@ -1461,7 +1465,8 @@ impl SyncService { /// `start_snap_sync` starts a session for, and one ordinary sync can do /// nothing for once a session has written its window of headers. fn executed_nothing(&self) -> bool { - self.recorded_execution().is_none_or(|executed| executed.number == 0) + self.recorded_execution() + .is_none_or(|executed| executed.number == 0) } /// Try snapshot sync again, after the wait `finish_snap_sync` set. @@ -3639,13 +3644,22 @@ impl SyncService { // blocks deep into the middle of a mainnet walk, which the walk then // took for ground this node already held, and stopped there. if self.suspended_for_snap { - let ours = |driver: Option<&crate::snap::driver::SnapDriver>, id: u64, headers: bool| { - driver.is_some_and(|d| if headers { d.awaits_headers(id) } else { d.awaits_skeleton(id) }) - }; + let ours = + |driver: Option<&crate::snap::driver::SnapDriver>, id: u64, headers: bool| { + driver.is_some_and(|d| { + if headers { + d.awaits_headers(id) + } else { + d.awaits_skeleton(id) + } + }) + }; let claimed = match &event { SyncEvent::SkeletonResponse { id, .. } => ours(self.snap.as_ref(), *id, false), SyncEvent::HeadersResponse { id, .. } - | SyncEvent::HeadersWithUnclesResponse { id, .. } => ours(self.snap.as_ref(), *id, true), + | SyncEvent::HeadersWithUnclesResponse { id, .. } => { + ours(self.snap.as_ref(), *id, true) + } SyncEvent::SnapStatusResponse { .. } | SyncEvent::SnapChunkResponse { .. } | SyncEvent::SnapBlocksResponse { .. } => true, diff --git a/crates/sync/src/snap/client.rs b/crates/sync/src/snap/client.rs index 3d491ec..fe1b2cd 100644 --- a/crates/sync/src/snap/client.rs +++ b/crates/sync/src/snap/client.rs @@ -470,7 +470,10 @@ impl StateDownload { // would leave a gap behind the cursor. if starts > from { self.release(from); - return Err(ChunkError::Misplaced { asked: from, starts }); + return Err(ChunkError::Misplaced { + asked: from, + starts, + }); } for (hash, message) in &chunk.nodes { @@ -500,7 +503,11 @@ impl StateDownload { chunk.nodes.len() ); - Ok(Progress { nodes: chunk.nodes.len(), advanced, complete: self.is_complete() }) + Ok(Progress { + nodes: chunk.nodes.len(), + advanced, + complete: self.is_complete(), + }) } /// Replace the hinted size with the one the root proves, and reshape the diff --git a/crates/sync/src/snap/driver.rs b/crates/sync/src/snap/driver.rs index f35eaa8..7ab442d 100644 --- a/crates/sync/src/snap/driver.rs +++ b/crates/sync/src/snap/driver.rs @@ -96,7 +96,11 @@ pub struct Peers<'a> { impl<'a> Peers<'a> { /// One set of peers for every kind of request. pub fn all(peers: &'a [B512]) -> Self { - Self { snap: peers, uncles: peers, any: peers } + Self { + snap: peers, + uncles: peers, + any: peers, + } } fn is_empty(&self) -> bool { @@ -329,7 +333,10 @@ impl SnapDriver { } let (strikes, until) = self.benched.entry(peer).or_insert((0, now)); *strikes = strikes.saturating_add(1); - *until = now + BENCH.saturating_mul(1 << (*strikes - 1).min(4)).min(BENCH_MAX); + *until = now + + BENCH + .saturating_mul(1 << (*strikes - 1).min(4)) + .min(BENCH_MAX); } /// Of `peers`, those not sitting out -- or all of them, if every one is. @@ -433,7 +440,11 @@ impl SnapDriver { // plain headers, which every peer serves. let now = Instant::now(); let header_peers = self.available( - if self.session.phase() == Phase::AscendingHeaders { peers.uncles } else { peers.any }, + if self.session.phase() == Phase::AscendingHeaders { + peers.uncles + } else { + peers.any + }, now, ); let snap_peers = self.available(&self.worth_asking(peers.snap), now); diff --git a/crates/sync/src/snap/header_tests.rs b/crates/sync/src/snap/header_tests.rs index 7606cd1..394a2d5 100644 --- a/crates/sync/src/snap/header_tests.rs +++ b/crates/sync/src/snap/header_tests.rs @@ -159,9 +159,9 @@ impl Fixture { log.push((frontier, want)); match want { Want::Skeleton { start } => walk.on_skeleton(&self.skeleton(start)), - Want::Headers { point, count, .. } => { - walk.on_headers(point, &self.headers(point, count), &Default::default()).expect("honest") - } + Want::Headers { point, count, .. } => walk + .on_headers(point, &self.headers(point, count), &Default::default()) + .expect("honest"), } } walk.advance().expect("honest answers link"); @@ -202,7 +202,11 @@ fn heaviest_uncles(chain: &[Header], blocks: std::ops::RangeInclusive) -> let mut total = U256::ZERO; for n in blocks { let parents = n.saturating_sub(7)..=n.saturating_sub(2); - let heaviest = chain[parents].iter().map(|h| h.difficulty).max().expect("a parent"); + let heaviest = chain[parents] + .iter() + .map(|h| h.difficulty) + .max() + .expect("a parent"); total += U256::from(chain[n].uncle_count) * (heaviest + heaviest / U256::from(50u64)); } total @@ -468,8 +472,15 @@ fn the_walk_asks_from_the_top_down() { Want::Headers { .. } => None, }) .collect(); - assert_eq!(starts.first(), Some(&19_200), "the first skeleton is the one under the checkpoint"); - assert!(starts.windows(2).all(|w| w[0] > w[1]), "not asked from the top down: {starts:?}"); + assert_eq!( + starts.first(), + Some(&19_200), + "the first skeleton is the one under the checkpoint" + ); + assert!( + starts.windows(2).all(|w| w[0] > w[1]), + "not asked from the top down: {starts:?}" + ); let (frontier, _) = log .iter() @@ -509,21 +520,45 @@ fn a_frontier_answered_from_another_chain_is_asked_for_again() { let f = fixture(1000); let forked = another_chain(1000); let mut walk = f.walk(1000, HeaderVerifier::new()); - walk.on_headers(1000, &f.headers(1000, (1000 - 960) as u32), &Default::default()).expect("honest"); + walk.on_headers( + 1000, + &f.headers(1000, (1000 - 960) as u32), + &Default::default(), + ) + .expect("honest"); walk.advance().expect("links"); assert_eq!(walk.frontier(), 960); - let frontier = Want::Headers { from: f.chain[960].hash(), count: HEADER_CHUNK as u32, point: 960 }; - assert!(walk.wants(8).contains(&frontier), "the walk asks for its frontier from the hash it holds"); + let frontier = Want::Headers { + from: f.chain[960].hash(), + count: HEADER_CHUNK as u32, + point: 960, + }; + assert!( + walk.wants(8).contains(&frontier), + "the walk asks for its frontier from the hash it holds" + ); // Right height, wrong chain. - let run: Vec
= (769..=960).rev().map(|i| forked[i as usize].clone()).collect(); - walk.on_headers(960, &run, &Default::default()).expect("internally valid"); + let run: Vec
= (769..=960) + .rev() + .map(|i| forked[i as usize].clone()) + .collect(); + walk.on_headers(960, &run, &Default::default()) + .expect("internally valid"); walk.advance().expect("no error, just no progress"); assert_eq!(walk.frontier(), 960, "a foreign run moved the walk"); - assert!(walk.wants(8).contains(&frontier), "the frontier was not asked for again"); - walk.on_headers(960, &f.headers(960, HEADER_CHUNK as u32), &Default::default()).expect("honest"); + assert!( + walk.wants(8).contains(&frontier), + "the frontier was not asked for again" + ); + walk.on_headers( + 960, + &f.headers(960, HEADER_CHUNK as u32), + &Default::default(), + ) + .expect("honest"); walk.advance().expect("links"); assert_eq!(walk.frontier(), 768); } @@ -538,11 +573,16 @@ fn work_counts_once_and_only_for_the_chain() { let f = fixture(1000); let forked = another_chain(1000); let mut walk = f.walk(1000, HeaderVerifier::new()); - walk.on_headers(1000, &f.headers(1000, 40), &Default::default()).expect("honest"); + walk.on_headers(1000, &f.headers(1000, 40), &Default::default()) + .expect("honest"); walk.advance().expect("links"); - let foreign: Vec
= (769..=960).rev().map(|i| forked[i as usize].clone()).collect(); + let foreign: Vec
= (769..=960) + .rev() + .map(|i| forked[i as usize].clone()) + .collect(); for _ in 0..2 { - walk.on_headers(960, &foreign, &Default::default()).expect("internally valid"); + walk.on_headers(960, &foreign, &Default::default()) + .expect("internally valid"); walk.advance().expect("no progress, and no error"); } f.run(&mut walk, 8); @@ -551,7 +591,11 @@ fn work_counts_once_and_only_for_the_chain() { // Genesis's own 100, and #1 to #1000 at 100 each, the checkpoint among them. let work = U256::from(100_100u64); assert_eq!(walk.established_difficulty(), Some(work)); - assert_eq!(walk.work_ceiling(U256::ZERO), Some(work), "without uncles the work is exact"); + assert_eq!( + walk.work_ceiling(U256::ZERO), + Some(work), + "without uncles the work is exact" + ); } /// **The ceiling covers the heaviest uncles the rules allow.** rskj peers send @@ -566,7 +610,11 @@ fn work_counts_once_and_only_for_the_chain() { #[test] fn the_ceiling_covers_the_heaviest_uncles_the_rules_allow() { let chain = shaped_chain(1000, |n| { - let difficulty = if (762..=767).contains(&n) { 10_000_000 } else { 1_000_000 + (n % 10) * 50_000 }; + let difficulty = if (762..=767).contains(&n) { + 10_000_000 + } else { + 1_000_000 + (n % 10) * 50_000 + }; (difficulty, if n >= 8 { n % 3 } else { 0 }) }); let f = fixture_of(chain); @@ -576,9 +624,16 @@ fn the_ceiling_covers_the_heaviest_uncles_the_rules_allow() { let seen = sum(&f.chain); let carried = seen + heaviest_uncles(&f.chain, 1..=1000); - assert_eq!(walk.established_difficulty(), Some(seen), "the walk counts the trunk it sees"); + assert_eq!( + walk.established_difficulty(), + Some(seen), + "the walk counts the trunk it sees" + ); let ceiling = walk.work_ceiling(U256::ZERO).expect("anchored"); - assert!(ceiling >= carried, "the ceiling {ceiling} is under the {carried} the chain can carry"); + assert!( + ceiling >= carried, + "the ceiling {ceiling} is under the {carried} the chain can carry" + ); assert!( ceiling * U256::from(100u64) <= carried * U256::from(101u64), "the ceiling {ceiling} is loose against {carried}" @@ -591,7 +646,11 @@ fn the_ceiling_covers_the_heaviest_uncles_the_rules_allow() { #[test] fn the_ceiling_reaches_under_the_anchor() { let chain = shaped_chain(1000, |n| { - let difficulty = if (762..=767).contains(&n) { 10_000_000 } else { 1_000_000 }; + let difficulty = if (762..=767).contains(&n) { + 10_000_000 + } else { + 1_000_000 + }; (difficulty, u64::from(n >= 8)) }); let f = fixture_of(chain); @@ -600,8 +659,12 @@ fn the_ceiling_reaches_under_the_anchor() { let mut td = f.chain[0].difficulty; for header in &f.chain[1..=anchor] { td += header.difficulty * U256::from(1 + header.uncle_count); - f.store.put_header_with_hash(header.hash(), header).expect("header"); - f.store.put_canonical_hash(header.number, header.hash()).expect("index"); + f.store + .put_header_with_hash(header.hash(), header) + .expect("header"); + f.store + .put_canonical_hash(header.number, header.hash()) + .expect("index"); f.store.put_total_difficulty(header.hash(), td).expect("td"); } let mut walk = f.walk(1000, HeaderVerifier::new()); @@ -609,9 +672,15 @@ fn the_ceiling_reaches_under_the_anchor() { assert!(walk.is_done()); let carried = td + sum(&f.chain[anchor + 1..]) + heaviest_uncles(&f.chain, anchor + 1..=1000); - assert_eq!(walk.established_difficulty(), Some(td + sum(&f.chain[anchor + 1..]))); + assert_eq!( + walk.established_difficulty(), + Some(td + sum(&f.chain[anchor + 1..])) + ); let ceiling = walk.work_ceiling(U256::ZERO).expect("anchored"); - assert!(ceiling >= carried, "the ceiling {ceiling} is under the {carried} the chain can carry"); + assert!( + ceiling >= carried, + "the ceiling {ceiling} is under the {carried} the chain can carry" + ); assert!( ceiling * U256::from(100u64) <= carried * U256::from(101u64), "the ceiling {ceiling} is loose against {carried}" @@ -627,9 +696,15 @@ fn a_missing_header_under_the_anchor_leaves_the_uncles_unbounded() { let f = fixture_of(chain); // This node holds #768 as canonical, but none of the headers under it. let anchor = &f.chain[768]; - f.store.put_header_with_hash(anchor.hash(), anchor).expect("header"); - f.store.put_canonical_hash(768, anchor.hash()).expect("index"); - f.store.put_total_difficulty(anchor.hash(), U256::from(1_000_000_000u64)).expect("td"); + f.store + .put_header_with_hash(anchor.hash(), anchor) + .expect("header"); + f.store + .put_canonical_hash(768, anchor.hash()) + .expect("index"); + f.store + .put_total_difficulty(anchor.hash(), U256::from(1_000_000_000u64)) + .expect("td"); let mut walk = f.walk(1000, HeaderVerifier::new()); // One request at a time, so that the walk asks only for its frontier and // never fetches, and stores, the run under the anchor itself. @@ -647,17 +722,31 @@ fn a_missing_header_under_the_anchor_leaves_the_uncles_unbounded() { fn a_short_answer_does_not_strand_the_walk() { let f = fixture(1000); let mut walk = f.walk(1000, HeaderVerifier::new()); - walk.on_headers(1000, &f.headers(1000, (1000 - 960) as u32), &Default::default()).expect("honest"); + walk.on_headers( + 1000, + &f.headers(1000, (1000 - 960) as u32), + &Default::default(), + ) + .expect("honest"); walk.advance().expect("links"); // 100 of the 192 asked for. - walk.on_headers(960, &f.headers(960, 100), &Default::default()).expect("honest, if short"); + walk.on_headers(960, &f.headers(960, 100), &Default::default()) + .expect("honest, if short"); walk.advance().expect("links"); assert_eq!(walk.frontier(), 860); - let rest = Want::Headers { from: f.chain[860].hash(), count: 860 - 768, point: 860 }; - assert!(walk.wants(8).contains(&rest), "the walk did not ask for the rest of the stretch"); - walk.on_headers(860, &f.headers(860, 860 - 768), &Default::default()).expect("honest"); + let rest = Want::Headers { + from: f.chain[860].hash(), + count: 860 - 768, + point: 860, + }; + assert!( + walk.wants(8).contains(&rest), + "the walk did not ask for the rest of the stretch" + ); + walk.on_headers(860, &f.headers(860, 860 - 768), &Default::default()) + .expect("honest"); walk.advance().expect("links"); assert_eq!(walk.frontier(), 768, "not back on the grid"); @@ -675,7 +764,9 @@ fn a_walk_that_anchors_above_genesis_leaves_the_chain_below_alone() { let anchor = 15_360u64; for h in &f.chain[1..=anchor as usize] { f.store.put_header_with_hash(h.hash(), h).expect("header"); - f.store.put_canonical_hash(h.number, h.hash()).expect("index"); + f.store + .put_canonical_hash(h.number, h.hash()) + .expect("index"); } let mut walk = f.walk(20_000, HeaderVerifier::new()); @@ -701,11 +792,27 @@ fn an_identifier_off_the_grid_is_not_asked_for() { let f = fixture(1000); let mut walk = f.walk(1000, HeaderVerifier::new()); walk.on_skeleton(&[ - BlockIdentifier { number: 768, hash: f.chain[768].hash() }, - BlockIdentifier { number: 900, hash: f.chain[900].hash() }, + BlockIdentifier { + number: 768, + hash: f.chain[768].hash(), + }, + BlockIdentifier { + number: 900, + hash: f.chain[900].hash(), + }, ]); let wants = walk.wants(8); - assert!(wants.iter().any(|w| matches!(w, Want::Headers { point: 768, .. })), "{wants:?}"); - assert!(!wants.iter().any(|w| matches!(w, Want::Headers { point: 900, .. })), "{wants:?}"); + assert!( + wants + .iter() + .any(|w| matches!(w, Want::Headers { point: 768, .. })), + "{wants:?}" + ); + assert!( + !wants + .iter() + .any(|w| matches!(w, Want::Headers { point: 900, .. })), + "{wants:?}" + ); } diff --git a/crates/sync/src/snap/headers.rs b/crates/sync/src/snap/headers.rs index b15321f..ee14992 100644 --- a/crates/sync/src/snap/headers.rs +++ b/crates/sync/src/snap/headers.rs @@ -396,7 +396,11 @@ impl HeaderWalk { if !self.headers_sent.contains(&need) && !self.runs.contains_key(&need) { let floor = ((need - 1) / HEADER_CHUNK) * HEADER_CHUNK; self.headers_sent.insert(need); - out.push(Want::Headers { from: self.need_hash, count: (need - floor) as u32, point: need }); + out.push(Want::Headers { + from: self.need_hash, + count: (need - floor) as u32, + point: need, + }); if out.len() >= budget { return out; } @@ -537,10 +541,15 @@ impl HeaderWalk { .unwrap_or(U256::ZERO); let below = (i + UNCLE_GENERATIONS + 1).saturating_sub(headers.len()); if below == 0 { - uncle_weight = uncle_weight - .saturating_add(U256::from(header.uncle_count).saturating_mul(highest)); + uncle_weight = uncle_weight.saturating_add( + U256::from(header.uncle_count).saturating_mul(highest), + ); } else { - reaching.push(Reaching { count: header.uncle_count, highest, below }); + reaching.push(Reaching { + count: header.uncle_count, + highest, + below, + }); } } } @@ -728,7 +737,8 @@ impl HeaderWalk { if uncles.below > 0 { return true; } - settled = settled.saturating_add(U256::from(uncles.count).saturating_mul(uncles.highest)); + settled = + settled.saturating_add(U256::from(uncles.count).saturating_mul(uncles.highest)); false }); self.uncle_weight = self.uncle_weight.saturating_add(settled); @@ -737,7 +747,12 @@ impl HeaderWalk { /// Settle the uncles whose possible parents reach below the walk, on this /// node's own chain under the anchor, which the store holds. fn settle_on_own_chain(&mut self) { - let wanted = self.reaching.iter().map(|uncles| uncles.below).max().unwrap_or(0); + let wanted = self + .reaching + .iter() + .map(|uncles| uncles.below) + .max() + .unwrap_or(0); let mut difficulties = Vec::with_capacity(wanted); let mut hash = self.need_hash; let mut whole = true; diff --git a/crates/sync/src/snap/session.rs b/crates/sync/src/snap/session.rs index a9603d8..946650a 100644 --- a/crates/sync/src/snap/session.rs +++ b/crates/sync/src/snap/session.rs @@ -349,7 +349,9 @@ impl SnapSession { /// What may be outstanding at once in the current phase. fn budget(&self) -> usize { let per_peer = |peers: usize| { - peers.saturating_mul(HEADER_REQUESTS_PER_PEER).min(MAX_HEADER_REQUESTS) + peers + .saturating_mul(HEADER_REQUESTS_PER_PEER) + .min(MAX_HEADER_REQUESTS) }; let cap = match self.phase { Phase::VerifyingHeaders => per_peer(self.header_peers), diff --git a/crates/sync/src/snap/session_tests.rs b/crates/sync/src/snap/session_tests.rs index f7bc42e..255e380 100644 --- a/crates/sync/src/snap/session_tests.rs +++ b/crates/sync/src/snap/session_tests.rs @@ -122,11 +122,16 @@ fn chain_with_uncles( let uncle = header(number - 1, B256::repeat_byte(0x0c), B256::ZERO, difficulty); let mut h = header(number, parent, state_root, difficulty); h.uncle_count = 1; - h.ommers_hash = rustock_execution::processor::compute_ommers_hash(std::slice::from_ref(&uncle)); + h.ommers_hash = + rustock_execution::processor::compute_ommers_hash(std::slice::from_ref(&uncle)); parent = h.hash(); td += 2 * difficulty; tds.push(U256::from(td)); - blocks.push(Block { header: h, transactions: Vec::new(), ommers: vec![uncle] }); + blocks.push(Block { + header: h, + transactions: Vec::new(), + ommers: vec![uncle], + }); } (blocks, tds) } @@ -1006,14 +1011,26 @@ fn an_honest_rskj_chunk_is_kept_and_not_charged() { let (mut driver, id, from, server) = driver_awaiting_a_chunk(&f, &blocks, &tds, &peers); driver.take_blame(); - driver.on_chunk(id, server, &rskj_chunk(&f, from, 4_000), Refusal::None, &peers); + driver.on_chunk( + id, + server, + &rskj_chunk(&f, from, 4_000), + Refusal::None, + &peers, + ); - assert!(driver.take_blame().is_empty(), "an honest rskj peer was charged"); + assert!( + driver.take_blame().is_empty(), + "an honest rskj peer was charged" + ); let (covered, total) = driver.state_progress(); assert!(covered > 0, "the chunk moved nothing"); assert_eq!( total, - Some(rustock_trie::snapshot::total_size(&f.trie_root, f.trie.as_ref())), + Some(rustock_trie::snapshot::total_size( + &f.trie_root, + f.trie.as_ref() + )), "the size of the trie comes from the rebuilt root, not the peer's hint" ); } @@ -1039,7 +1056,11 @@ fn an_rskj_chunk_for_another_offset_is_charged() { assert_eq!(blame.len(), 1, "got {blame:?}"); assert_eq!(blame[0].peer, server); assert_eq!(blame[0].event, EventType::InvalidMessage); - assert_eq!(driver.state_progress().0, 0, "a chunk for another offset moved the download"); + assert_eq!( + driver.state_progress().0, + 0, + "a chunk for another offset moved the download" + ); } /// An rskj chunk with nothing of its own -- what rskj answers when the node at @@ -1057,14 +1078,28 @@ fn an_rskj_chunk_with_nothing_of_its_own_is_not_charged() { // A one-byte cell: no node fits, so the chunk carries only the path to it. let nothing = rskj_chunk(&f, from, 1); - let ChunkPayload::Legacy(blob) = ¬hing else { unreachable!() }; + let ChunkPayload::Legacy(blob) = ¬hing else { + unreachable!() + }; let chunk = rustock_trie::snapshot_legacy::decode_blob(blob).expect("decodes"); - assert!(chunk.nodes.is_empty() && !chunk.post_root.is_empty(), "not the chunk this test is about"); + assert!( + chunk.nodes.is_empty() && !chunk.post_root.is_empty(), + "not the chunk this test is about" + ); driver.on_chunk(id, server, ¬hing, Refusal::None, &peers); - assert!(driver.take_blame().is_empty(), "charged a peer for the offset it was asked"); - assert!(!driver.unhelpful().contains(&server), "stopped asking a peer that can serve"); - assert!(driver.state_progress().0 > 0, "the node at the offset, sent whole, was not kept"); + assert!( + driver.take_blame().is_empty(), + "charged a peer for the offset it was asked" + ); + assert!( + !driver.unhelpful().contains(&server), + "stopped asking a peer that can serve" + ); + assert!( + driver.state_progress().0 > 0, + "the node at the offset, sent whole, was not kept" + ); } /// **Answers that move nothing end the session**, however well they rebuild. @@ -1115,8 +1150,12 @@ fn answers_that_move_nothing_end_the_session() { fn chunk_requests(out: &[super::driver::Outbound]) -> Vec<(u64, u64, B512)> { out.iter() .filter_map(|o| { - let P2pMessage::RskMessage(m) = &o.message else { return None }; - let RskSubMessage::SnapChunkRequest(req) = &m.sub_message else { return None }; + let P2pMessage::RskMessage(m) = &o.message else { + return None; + }; + let RskSubMessage::SnapChunkRequest(req) = &m.sub_message else { + return None; + }; Some((req.id, req.from, o.peer)) }) .collect() @@ -1133,7 +1172,13 @@ fn an_offset_refused_as_off_the_grid_is_asked_on_the_grid_next() { let peers = vec![B512::repeat_byte(1), B512::repeat_byte(2)]; let (mut driver, id, from, server) = driver_awaiting_a_chunk(&f, &blocks, &tds, &peers); - driver.on_chunk(id, server, &rskj_chunk(&f, from, 2_000), Refusal::None, &peers); + driver.on_chunk( + id, + server, + &rskj_chunk(&f, from, 2_000), + Refusal::None, + &peers, + ); let (cursor, _) = driver.state_progress(); assert!(cursor > 0, "the chunk moved nothing"); // Let whatever else the setup left outstanding time out, so the budget @@ -1148,14 +1193,25 @@ fn an_offset_refused_as_off_the_grid_is_asked_on_the_grid_next() { .iter() .find(|(_, asked, _)| *asked == cursor + 1) .unwrap_or_else(|| panic!("nothing asked just past the cursor {cursor}: {exact:?}")); - assert_ne!(asked % 100_000, 0, "the exact offset happens to be on the grid; the test proves nothing"); + assert_ne!( + asked % 100_000, + 0, + "the exact offset happens to be on the grid; the test proves nothing" + ); - let refused = ChunkPayload::Proved { entries: Vec::new(), witness: Vec::new() }; + let refused = ChunkPayload::Proved { + entries: Vec::new(), + witness: Vec::new(), + }; let out = driver.on_chunk(id, peer, &refused, Refusal::OffsetNotOnGrid, &peers); - assert!(driver.take_blame().is_empty(), "a refusal about the offset was charged"); + assert!( + driver.take_blame().is_empty(), + "a refusal about the offset was charged" + ); let next = chunk_requests(&out); assert!( - next.iter().any(|(_, asked, _)| *asked == cursor / 100_000 * 100_000), + next.iter() + .any(|(_, asked, _)| *asked == cursor / 100_000 * 100_000), "the refused offset was not replaced by its cell: {next:?}" ); assert!( @@ -1247,18 +1303,35 @@ fn the_header_walk_asks_peers_without_snap() { let plain = B512::repeat_byte(2); let mut driver = SnapDriver::new(f.session(HeaderVerifier::new())); - let out = driver.poll(Peers { snap: &[server], uncles: &[], any: &[server, plain] }); + let out = driver.poll(Peers { + snap: &[server], + uncles: &[], + any: &[server, plain], + }); let (status_id, kind) = asked(&out[0]); assert_eq!(kind, "status"); - assert_eq!(out[0].peer, server, "a status request goes only to a snap peer"); + assert_eq!( + out[0].peer, server, + "a status request goes only to a snap peer" + ); // Only an ordinary peer is left to ask, as when the server stops answering. - let only_plain = Peers { snap: &[], uncles: &[], any: &[plain] }; + let only_plain = Peers { + snap: &[], + uncles: &[], + any: &[plain], + }; let out = driver.on_status(status_id, server, &blocks, &tds, 5_000, 0, only_plain); - assert!(!out.is_empty(), "the walk stalled with an ordinary peer to ask"); + assert!( + !out.is_empty(), + "the walk stalled with an ordinary peer to ask" + ); for request in &out { let (_, kind) = asked(request); - assert!(matches!(kind, "headers" | "skeleton"), "asked an ordinary peer for a {kind}"); + assert!( + matches!(kind, "headers" | "skeleton"), + "asked an ordinary peer for a {kind}" + ); assert_eq!(request.peer, plain); } } @@ -1273,11 +1346,22 @@ fn snapshot_requests_go_only_to_snap_peers() { let plain = B512::repeat_byte(2); let mut driver = SnapDriver::new(f.session(HeaderVerifier::new())); - let out = driver.poll(Peers { snap: &[], uncles: &[plain], any: &[plain] }); - assert!(out.is_empty(), "asked a peer without snap for a snapshot message"); + let out = driver.poll(Peers { + snap: &[], + uncles: &[plain], + any: &[plain], + }); + assert!( + out.is_empty(), + "asked a peer without snap for a snapshot message" + ); assert!(driver.outstanding_ids().is_empty()); - let out = driver.poll(Peers { snap: &[server], uncles: &[], any: &[server, plain] }); + let out = driver.poll(Peers { + snap: &[server], + uncles: &[], + any: &[server, plain], + }); let (_, kind) = asked(&out[0]); assert_eq!(kind, "status"); assert_eq!(out[0].peer, server); @@ -1295,8 +1379,20 @@ fn a_request_nobody_can_take_is_asked_once_somebody_can() { let out = driver.poll(&[server]); let (status_id, _) = asked(&out[0]); - let nobody_for_headers = Peers { snap: &[server], uncles: &[], any: &[] }; - let out = driver.on_status(status_id, server, &blocks, &tds, 5_000, 0, nobody_for_headers); + let nobody_for_headers = Peers { + snap: &[server], + uncles: &[], + any: &[], + }; + let out = driver.on_status( + status_id, + server, + &blocks, + &tds, + 5_000, + 0, + nobody_for_headers, + ); assert!(out.is_empty()); assert!( driver.outstanding_ids().is_empty(), @@ -1304,7 +1400,11 @@ fn a_request_nobody_can_take_is_asked_once_somebody_can() { ); let plain = B512::repeat_byte(2); - let out = driver.poll(Peers { snap: &[], uncles: &[], any: &[plain] }); + let out = driver.poll(Peers { + snap: &[], + uncles: &[], + any: &[plain], + }); assert!(!out.is_empty(), "the walk's requests were lost"); assert!(out.iter().all(|request| request.peer == plain)); } @@ -1327,10 +1427,15 @@ fn an_empty_header_answer_is_asked_again() { .find(|a| matches!(a, Action::RequestHeaders { .. })) .cloned() .expect("the walk asks for headers"); - let Action::RequestHeaders { point, .. } = asked else { unreachable!() }; + let Action::RequestHeaders { point, .. } = asked else { + unreachable!() + }; let again = session.on_headers(point, &[]); - assert!(again.contains(&asked), "the range was not asked for again: {again:?}"); + assert!( + again.contains(&asked), + "the range was not asked for again: {again:?}" + ); } /// A peer that left a request unanswered sits out while somebody else can be @@ -1348,13 +1453,21 @@ fn a_peer_that_left_a_request_unanswered_sits_out() { let out = driver.poll(&[server]); let (status_id, _) = asked(&out[0]); // At first only `silent` can be asked for headers, and it answers nothing. - let only_silent = Peers { snap: &[server], uncles: &[], any: &[silent] }; + let only_silent = Peers { + snap: &[server], + uncles: &[], + any: &[silent], + }; let out = driver.on_status(status_id, server, &blocks, &tds, 5_000, 0, only_silent); assert!(!out.is_empty() && out.iter().all(|request| request.peer == silent)); let later = std::time::Instant::now() + std::time::Duration::from_secs(31); assert_eq!(driver.expire(later), out.len()); - let out = driver.poll(Peers { snap: &[server], uncles: &[], any: &[server, silent] }); + let out = driver.poll(Peers { + snap: &[server], + uncles: &[], + any: &[server, silent], + }); assert!(!out.is_empty(), "the walk's requests were not asked again"); assert!( out.iter().all(|request| request.peer == server), @@ -1614,14 +1727,21 @@ fn the_header_walk_asks_more_of_more_peers() { for _ in 0..10 { let mut next = Vec::new(); for request in &out { - let P2pMessage::RskMessage(m) = &request.message else { continue }; - let RskSubMessage::SkeletonRequest(r) = &m.sub_message else { continue }; + let P2pMessage::RskMessage(m) = &request.message else { + continue; + }; + let RskSubMessage::SkeletonRequest(r) = &m.sub_message else { + continue; + }; let ids: Vec = blocks .iter() .map(|b| &b.header) .filter(|h| h.number % 192 == 0) .filter(|h| h.number >= r.start_number && h.number <= r.start_number + 3_840) - .map(|h| rustock_networking::protocol::BlockIdentifier { number: h.number, hash: h.hash() }) + .map(|h| rustock_networking::protocol::BlockIdentifier { + number: h.number, + hash: h.hash(), + }) .collect(); next.extend(driver.on_skeleton(r.id, request.peer, &ids, &peers)); } @@ -1632,8 +1752,14 @@ fn the_header_walk_asks_more_of_more_peers() { } let outstanding = driver.outstanding_ids().len(); - assert!(outstanding > 2, "held to a state download's budget: {outstanding} outstanding"); - assert!(outstanding <= 2 * peers.len(), "{outstanding} outstanding, more than two per peer"); + assert!( + outstanding > 2, + "held to a state download's budget: {outstanding} outstanding" + ); + assert!( + outstanding <= 2 * peers.len(), + "{outstanding} outstanding, more than two per peer" + ); } /// **A refused claim leaves nothing behind.** Making the checkpoint this @@ -1655,14 +1781,18 @@ fn a_refused_claim_leaves_nothing_behind() { let mut session = f.session(HeaderVerifier::new()); let actions = session.on_status(&blocks, &tds, 8_000, 0); assert_eq!(session.phase(), Phase::VerifyingHeaders); - let Some(Action::RequestHeaders { point, count, .. }) = - actions.into_iter().find(|a| matches!(a, Action::RequestHeaders { .. })) + let Some(Action::RequestHeaders { point, count, .. }) = actions + .into_iter() + .find(|a| matches!(a, Action::RequestHeaders { .. })) else { panic!("the walk asks for headers"); }; session.on_headers(point, &headers_for(&blocks, point, count)); assert!( - matches!(session.failure(), Some(SnapFailure::OverstatedDifficulty { .. })), + matches!( + session.failure(), + Some(SnapFailure::OverstatedDifficulty { .. }) + ), "{:?}", session.failure() ); @@ -1672,7 +1802,11 @@ fn a_refused_claim_leaves_nothing_behind() { None, "the refused checkpoint was indexed as canonical" ); - assert_ne!(f.store.head().unwrap(), Some(checkpoint.hash()), "it became the head"); + assert_ne!( + f.store.head().unwrap(), + Some(checkpoint.hash()), + "it became the head" + ); assert_eq!( f.store.total_difficulty(checkpoint.hash()).unwrap(), None, @@ -1695,7 +1829,9 @@ async fn the_ordinary_sync_takes_no_headers_during_a_snapshot_session() { let f = fixture(20); let genesis = f.with_genesis(); - f.store.put_total_difficulty(genesis, U256::from(100u64)).expect("td"); + f.store + .put_total_difficulty(genesis, U256::from(100u64)) + .expect("td"); f.store.set_head(genesis).expect("head"); let (blocks, _) = chain(1, 3, genesis, 100, f.state_root); @@ -1706,7 +1842,10 @@ async fn the_ordinary_sync_takes_no_headers_during_a_snapshot_session() { .add_peer_with( peer, tx, - rustock_networking::handshake::PeerCapabilities { rsk_version: 62, snap: true }, + rustock_networking::handshake::PeerCapabilities { + rsk_version: 62, + snap: true, + }, ) .await; let manager = Arc::new(SyncManager::new( @@ -1729,7 +1868,11 @@ async fn the_ordinary_sync_takes_no_headers_during_a_snapshot_session() { }) .await; - assert_eq!(f.store.head().unwrap(), Some(genesis), "the ordinary sync moved the head"); + assert_eq!( + f.store.head().unwrap(), + Some(genesis), + "the ordinary sync moved the head" + ); assert_eq!( f.store.canonical_hash(1).unwrap(), None, @@ -2081,7 +2224,10 @@ fn a_claim_beyond_what_the_unseen_uncles_could_add_is_refused() { let actions = session.on_status(&blocks, &tds, 8_000, 0); answer_header_walk(&mut session, &blocks, actions); assert!( - matches!(session.failure(), Some(SnapFailure::OverstatedDifficulty { .. })), + matches!( + session.failure(), + Some(SnapFailure::OverstatedDifficulty { .. }) + ), "{:?}", session.failure() ); diff --git a/crates/sync/src/snap/tests.rs b/crates/sync/src/snap/tests.rs index f5c1f93..e0e26ac 100644 --- a/crates/sync/src/snap/tests.rs +++ b/crates/sync/src/snap/tests.rs @@ -1610,9 +1610,16 @@ fn rebuilt(peer: &Peer, response: &SnapChunkResponse) -> super::client::RebuiltC /// Downloads a state from rskj servers alone, as mainnet's are, with this /// node's own grid. Returns the client, its store, and how many requests it /// took. -fn download_from_rskj(peer: &Peer, grid: u64, cell: u64) -> (StateDownload, Arc, usize) { +fn download_from_rskj( + peer: &Peer, + grid: u64, + cell: u64, +) -> (StateDownload, Arc, usize) { let local = Arc::new(MemoryTrieStore::new()); - let config = SnapConfig { chunk_grid: grid, ..config(4, grid) }; + let config = SnapConfig { + chunk_grid: grid, + ..config(4, grid) + }; let mut client = StateDownload::new(peer.root_hash, peer.total(), &config, local.clone()); // Several times what a download that moves needs, so that one that has // stopped moving fails at once rather than after a hundred thousand @@ -1621,10 +1628,15 @@ fn download_from_rskj(peer: &Peer, grid: u64, cell: u64) -> (StateDownload, Arc< let mut asked = 0; while !client.is_complete() { asked += 1; - assert!(asked < enough, "{asked} requests and not done: the download has stopped moving"); + assert!( + asked < enough, + "{asked} requests and not done: the download has stopped moving" + ); let request = client.next_request().expect("work left and none in flight"); let chunk = rebuilt(peer, &rskj_serves(peer, request.from, cell)); - client.accept_rebuilt(request.from, &chunk).expect("an honest rskj chunk"); + client + .accept_rebuilt(request.from, &chunk) + .expect("an honest rskj chunk"); } (client, local, asked) } @@ -1639,7 +1651,10 @@ fn holds_everything(peer: &Peer, local: &MemoryTrieStore) { node.offset ); for value in &node.long_values { - assert!(local.get(keccak256(value).as_slice()).is_some(), "a long value was never fetched"); + assert!( + local.get(keccak256(value).as_slice()).is_some(), + "a long value was never fetched" + ); } } } @@ -1658,17 +1673,25 @@ fn an_rskj_server_fills_every_cell_of_a_wider_grid() { let peer = Peer::new(12_000); let total = peer.total(); - assert!(total > 6 * 100_000, "the trie must span several cells, not {total} bytes"); + assert!( + total > 6 * 100_000, + "the trie must span several cells, not {total} bytes" + ); let (client, local, asked) = download_from_rskj(&peer, 100_000, RSKJ_CHUNK_GRID); - client.verify_stored().expect("the downloaded state is whole"); + client + .verify_stored() + .expect("the downloaded state is whole"); holds_everything(&peer, &local); // And at one request per rskj cell, give or take one per slice: an // answer that stops mid-cell is followed by a request for exactly where // it stopped, not by the same cell again. let cells = total.div_ceil(RSKJ_CHUNK_GRID) as usize; - assert!(asked <= cells + cells / 10 + 2 * 4, "{asked} requests for {cells} rskj cells"); + assert!( + asked <= cells + cells / 10 + 2 * 4, + "{asked} requests for {cells} rskj cells" + ); } /// **A node longer than rskj's cell is fetched.** @@ -1785,7 +1808,9 @@ fn a_node_longer_than_an_rskj_cell_is_fetched() { fn a_short_rskj_chunk_is_not_taken_for_the_cell() { let peer = Peer::new(3_000); let (client, local, _) = download_from_rskj(&peer, 100_000, 7_000); - client.verify_stored().expect("the downloaded state is whole"); + client + .verify_stored() + .expect("the downloaded state is whole"); holds_everything(&peer, &local); } @@ -1798,12 +1823,23 @@ fn an_rskj_chunk_that_begins_after_the_offset_asked_is_refused() { use crate::snap::RSKJ_CHUNK_GRID; let peer = Peer::new(3_000); - assert!(peer.total() > 4 * RSKJ_CHUNK_GRID, "the trie must span several rskj cells"); + assert!( + peer.total() > 4 * RSKJ_CHUNK_GRID, + "the trie must span several rskj cells" + ); let local = Arc::new(MemoryTrieStore::new()); - let mut client = StateDownload::new(peer.root_hash, peer.total(), &config(1, 100_000), local.clone()); + let mut client = StateDownload::new( + peer.root_hash, + peer.total(), + &config(1, 100_000), + local.clone(), + ); let request = client.next_request().expect("a request"); - let elsewhere = rebuilt(&peer, &rskj_serves(&peer, request.from + 2 * RSKJ_CHUNK_GRID, RSKJ_CHUNK_GRID)); + let elsewhere = rebuilt( + &peer, + &rskj_serves(&peer, request.from + 2 * RSKJ_CHUNK_GRID, RSKJ_CHUNK_GRID), + ); match client.accept_rebuilt(request.from, &elsewhere) { Err(ChunkError::Misplaced { asked, starts }) => { assert_eq!(asked, request.from); @@ -1813,7 +1849,11 @@ fn an_rskj_chunk_that_begins_after_the_offset_asked_is_refused() { } assert_eq!(local.len(), 0, "a refused chunk wrote to the store"); assert_eq!(client.progress().0, 0, "a refused chunk moved the cursor"); - assert_eq!(client.next_request().map(|r| r.from), Some(request.from), "the range was not asked again"); + assert_eq!( + client.next_request().map(|r| r.from), + Some(request.from), + "the range was not asked again" + ); } /// **After an rskj chunk the next request names the offset just past the @@ -1831,9 +1871,14 @@ fn a_refused_exact_offset_is_asked_on_the_grid_next() { let first = client.next_request().expect("a request"); assert_eq!(first.from, 0); let chunk = rebuilt(&peer, &rskj_serves(&peer, first.from, RSKJ_CHUNK_GRID)); - client.accept_rebuilt(first.from, &chunk).expect("an honest chunk"); + client + .accept_rebuilt(first.from, &chunk) + .expect("an honest chunk"); let (cursor, _) = client.progress(); - assert!(cursor > 0 && cursor < 100_000, "an rskj chunk stops mid-cell, at {cursor}"); + assert!( + cursor > 0 && cursor < 100_000, + "an rskj chunk stops mid-cell, at {cursor}" + ); let exact = client.next_request().expect("more to fetch"); assert_eq!( @@ -1844,7 +1889,10 @@ fn a_refused_exact_offset_is_asked_on_the_grid_next() { client.realign(exact.from); let aligned = client.next_request().expect("more to fetch"); - assert_eq!(aligned.from, 0, "a refused offset was asked again instead of the cell"); + assert_eq!( + aligned.from, 0, + "a refused offset was asked again instead of the cell" + ); } /// **An rskj chunk counts the node its walk stopped at**, which rskj sends diff --git a/crates/sync/src/tests.rs b/crates/sync/src/tests.rs index 9dd977c..753cf8f 100644 --- a/crates/sync/src/tests.rs +++ b/crates/sync/src/tests.rs @@ -4323,15 +4323,23 @@ async fn test_reorg_via_headers_response_higher_td() { let a2 = dummy_header(2, a1.hash(), U256::from(10)); store.put_header(&a1).unwrap(); store.put_canonical_hash(1, a1.hash()).unwrap(); - store.put_total_difficulty(a1.hash(), U256::from(20)).unwrap(); + store + .put_total_difficulty(a1.hash(), U256::from(20)) + .unwrap(); store.put_header(&a2).unwrap(); store.put_canonical_hash(2, a2.hash()).unwrap(); - store.put_total_difficulty(a2.hash(), U256::from(30)).unwrap(); + store + .put_total_difficulty(a2.hash(), U256::from(30)) + .unwrap(); store.update_head(&a2, U256::from(30)).unwrap(); let verifier = Arc::new(HeaderVerifier::new()); let peer_store = Arc::new(rustock_networking::peers::PeerStore::new()); - let manager = Arc::new(SyncManager::new(store.clone(), verifier, peer_store.clone())); + let manager = Arc::new(SyncManager::new( + store.clone(), + verifier, + peer_store.clone(), + )); let (_event_tx, event_rx) = mpsc::unbounded_channel(); let mut service = SyncService::new(manager, peer_store, event_rx); @@ -4343,7 +4351,9 @@ async fn test_reorg_via_headers_response_higher_td() { // Receive fork headers (descending order, like real peers send) let peer = B512::repeat_byte(0x01); - service.on_headers_response(peer, vec![b2.clone(), b1.clone()]).await; + service + .on_headers_response(peer, vec![b2.clone(), b1.clone()]) + .await; // Fork B has TD = 10 + 20 + 20 = 50 vs chain A's TD = 30 // Head should switch to fork B @@ -4366,15 +4376,23 @@ async fn test_no_reorg_via_headers_response_lower_td() { let a2 = dummy_header(2, a1.hash(), U256::from(20)); store.put_header(&a1).unwrap(); store.put_canonical_hash(1, a1.hash()).unwrap(); - store.put_total_difficulty(a1.hash(), U256::from(30)).unwrap(); + store + .put_total_difficulty(a1.hash(), U256::from(30)) + .unwrap(); store.put_header(&a2).unwrap(); store.put_canonical_hash(2, a2.hash()).unwrap(); - store.put_total_difficulty(a2.hash(), U256::from(50)).unwrap(); + store + .put_total_difficulty(a2.hash(), U256::from(50)) + .unwrap(); store.update_head(&a2, U256::from(50)).unwrap(); let verifier = Arc::new(HeaderVerifier::new()); let peer_store = Arc::new(rustock_networking::peers::PeerStore::new()); - let manager = Arc::new(SyncManager::new(store.clone(), verifier, peer_store.clone())); + let manager = Arc::new(SyncManager::new( + store.clone(), + verifier, + peer_store.clone(), + )); let (_event_tx, event_rx) = mpsc::unbounded_channel(); let mut service = SyncService::new(manager, peer_store, event_rx); @@ -4385,7 +4403,9 @@ async fn test_no_reorg_via_headers_response_lower_td() { let b2 = fork_header(2, b1.hash(), U256::from(5), 0xBB); let peer = B512::repeat_byte(0x01); - service.on_headers_response(peer, vec![b2.clone(), b1.clone()]).await; + service + .on_headers_response(peer, vec![b2.clone(), b1.clone()]) + .await; // Fork B has TD = 10 + 5 + 5 = 20 vs chain A's TD = 50 // Head should NOT change @@ -4402,10 +4422,10 @@ async fn test_no_reorg_via_headers_response_lower_td() { #[tokio::test] async fn test_sync_handler_serves_body_request() { - use rustock_networking::protocol::{P2pMessage, RskMessage, RskSubMessage}; - use rustock_networking::protocol::P2pHandler; - use rustock_networking::protocol::rsk::BodyRequest; use rustock_core::Transaction; + use rustock_networking::protocol::rsk::BodyRequest; + use rustock_networking::protocol::P2pHandler; + use rustock_networking::protocol::{P2pMessage, RskMessage, RskSubMessage}; let dir = tempdir().unwrap(); let store = Arc::new(BlockStore::open(dir.path()).unwrap()); @@ -6185,7 +6205,9 @@ async fn test_follow_mode_re_requests_bodies_it_is_missing() { } // Executed up to #1; #2..#5 are canonical, header-only, no body. - store.set_exec_head(headers[0].hash(), headers[0].state_root).unwrap(); + store + .set_exec_head(headers[0].hash(), headers[0].state_root) + .unwrap(); let peer_store = Arc::new(rustock_networking::peers::PeerStore::new()); let peer = B512::repeat_byte(0x01); @@ -6193,7 +6215,11 @@ async fn test_follow_mode_re_requests_bodies_it_is_missing() { peer_store.add_peer(peer, tx).await; let verifier = Arc::new(HeaderVerifier::new()); - let manager = Arc::new(SyncManager::new(store.clone(), verifier, peer_store.clone())); + let manager = Arc::new(SyncManager::new( + store.clone(), + verifier, + peer_store.clone(), + )); let (_event_tx, event_rx) = mpsc::unbounded_channel(); let mut service = SyncService::new(manager, peer_store, event_rx); service.state = SyncState::Following; @@ -6564,7 +6590,11 @@ async fn a_retreat_after_a_failed_snapshot_sync_leaves_it_free_to_try_again() { store.update_head(&h, U256::from(n)).unwrap(); parent = h.hash(); } - assert_eq!(store.exec_head().unwrap(), None, "precondition: nothing executed"); + assert_eq!( + store.exec_head().unwrap(), + None, + "precondition: nothing executed" + ); let trie = Arc::new(rustock_trie::MemoryTrieStore::new()) as Arc; let mut service = service_over(store.clone()).with_trie_store_for_test(trie); @@ -6572,7 +6602,10 @@ async fn a_retreat_after_a_failed_snapshot_sync_leaves_it_free_to_try_again() { // The watchdog's first escalation. service.reverify_head_lineage(1); - let head = store.header(store.head().unwrap().unwrap()).unwrap().unwrap(); + let head = store + .header(store.head().unwrap().unwrap()) + .unwrap() + .unwrap(); assert_eq!(head.number, 1_009, "precondition: the retreat happened"); assert_eq!( store.exec_head().unwrap(), @@ -6582,7 +6615,10 @@ async fn a_retreat_after_a_failed_snapshot_sync_leaves_it_free_to_try_again() { // And the node can still do the one thing that gets it going: try the // snapshot again. - service.start_snap_sync(crate::snap::SnapConfig::default(), Arc::new(HeaderVerifier::new())); + service.start_snap_sync( + crate::snap::SnapConfig::default(), + Arc::new(HeaderVerifier::new()), + ); assert!( service.snap_phase().is_some(), "snapshot sync declined to start on a node that has executed nothing" @@ -6596,7 +6632,12 @@ async fn a_retreat_after_a_failed_snapshot_sync_leaves_it_free_to_try_again() { fn chain_with_an_unverified_state( len: u64, checkpoint: u64, -) -> (Arc, tempfile::TempDir, Vec
, Arc) { +) -> ( + Arc, + tempfile::TempDir, + Vec
, + Arc, +) { let dir = tempdir().unwrap(); let store = Arc::new(BlockStore::open(dir.path()).unwrap()); let unverified = B256::repeat_byte(0x77); @@ -6604,7 +6645,11 @@ fn chain_with_an_unverified_state( let mut parent = B256::ZERO; for n in 0..=len { let mut h = dummy_header(n, parent, U256::from(1)); - h.state_root = if n == checkpoint { unverified } else { B256::repeat_byte(0x5a) }; + h.state_root = if n == checkpoint { + unverified + } else { + B256::repeat_byte(0x5a) + }; store.update_head(&h, U256::from(n + 1)).unwrap(); parent = h.hash(); headers.push(h); @@ -6612,7 +6657,11 @@ fn chain_with_an_unverified_state( let trie = Arc::new(rustock_trie::MemoryTrieStore::new()) as Arc; let root = rustock_trie::TrieNode::empty(); trie.put(unverified.as_slice(), &root.to_message(trie.as_ref())); - assert_eq!(store.exec_head().unwrap(), None, "precondition: nothing executed"); + assert_eq!( + store.exec_head().unwrap(), + None, + "precondition: nothing executed" + ); (store, dir, headers, trie) } @@ -6629,7 +6678,11 @@ async fn a_retreat_does_not_take_up_a_state_that_failed_its_check() { service.reverify_head_lineage(1); - assert_eq!(store.head().unwrap(), Some(headers[9].hash()), "precondition: the retreat happened"); + assert_eq!( + store.head().unwrap(), + Some(headers[9].hash()), + "precondition: the retreat happened" + ); assert_eq!( store.exec_head().unwrap(), None, @@ -6657,7 +6710,10 @@ async fn with_nothing_executed_a_head_off_the_chain_is_reseated_not_rolled_back( let mut service = service_over(store.clone()).with_trie_store_for_test(trie); service.verify_coherence(); - age_violation(&mut service, EXECUTION_OFF_CHAIN_GRACE + Duration::from_secs(1)); + age_violation( + &mut service, + EXECUTION_OFF_CHAIN_GRACE + Duration::from_secs(1), + ); service.verify_coherence(); assert_eq!( @@ -6670,7 +6726,10 @@ async fn with_nothing_executed_a_head_off_the_chain_is_reseated_not_rolled_back( Some(headers[10].hash()), "the head should be back on the canonical chain" ); - assert_eq!(crate::invariant::check(&store, None, crate::Scope::Full), Ok(())); + assert_eq!( + crate::invariant::check(&store, None, crate::Scope::Full), + Ok(()) + ); } // ── snapshot sync owns the peer set while it runs (#185) ───────────────────── @@ -6757,27 +6816,37 @@ fn snapshot_syncing_node() -> (SyncService, Arc, tempfile::TempDir) let store = Arc::new(BlockStore::open(dir.path()).unwrap()); let genesis = dummy_header(0, B256::ZERO, U256::from(1)); store.update_head(&genesis, U256::from(1)).unwrap(); - let mut service = make_service(store.clone()).with_trie_store_for_test(Arc::new( - rustock_trie::MemoryTrieStore::new(), - )); + let mut service = make_service(store.clone()) + .with_trie_store_for_test(Arc::new(rustock_trie::MemoryTrieStore::new())); service.start_snap_sync( - crate::snap::SnapConfig { client_enabled: true, ..Default::default() }, + crate::snap::SnapConfig { + client_enabled: true, + ..Default::default() + }, Arc::new(HeaderVerifier::new()), ); - assert!(service.snap_phase().is_some(), "precondition: a session is running"); + assert!( + service.snap_phase().is_some(), + "precondition: a session is running" + ); (service, store, dir) } /// What failed every attempt on mainnet on 6 November 2026. fn state_not_whole() -> crate::snap::session::SnapFailure { - crate::snap::session::SnapFailure::IncompleteState("trie node 0x751b is not in the store".into()) + crate::snap::session::SnapFailure::IncompleteState( + "trie node 0x751b is not in the store".into(), + ) } /// Fail sessions until no further one opens, and say how many that took. async fn fail_until_spent(service: &mut SyncService) -> u32 { let mut failed = 0; while service.snap_phase().is_some() { - assert!(failed <= crate::service::MAX_SNAP_ATTEMPTS, "sessions kept opening"); + assert!( + failed <= crate::service::MAX_SNAP_ATTEMPTS, + "sessions kept opening" + ); service.fail_snap_session_for_test(state_not_whole()).await; failed += 1; } @@ -6820,19 +6889,34 @@ async fn a_node_that_executed_nothing_tries_snapshot_sync_again_rather_than_halt ); let (at, next_wait) = service.snap_retry_for_test(); assert!(at.is_some(), "no further try was set"); - assert_eq!(next_wait, SNAP_RETRY_FIRST * 2, "the wait after this one doubles"); + assert_eq!( + next_wait, + SNAP_RETRY_FIRST * 2, + "the wait after this one doubles" + ); // A tick before it is due changes nothing. In particular the guard for a // node suspended with no session must not take the wait for one. service.on_tick().await; assert_eq!(service.snap_phase(), None); - assert!(service.is_suspended_for_snap_for_test(), "the wait was taken for a lost session"); + assert!( + service.is_suspended_for_snap_for_test(), + "the wait was taken for a lost session" + ); // Once due, a session opens, and it is a single attempt. make_the_next_try_due(&mut service); service.on_tick().await; - assert_eq!(service.snap_phase(), Some(Phase::AwaitingStatus), "the try that was set did not happen"); - assert_eq!(fail_until_spent(&mut service).await, 1, "a later round is one attempt"); + assert_eq!( + service.snap_phase(), + Some(Phase::AwaitingStatus), + "the try that was set did not happen" + ); + assert_eq!( + fail_until_spent(&mut service).await, + 1, + "a later round is one attempt" + ); let (at, next_wait) = service.snap_retry_for_test(); assert!(at.is_some(), "no try was set after the second round"); assert_eq!(next_wait, SNAP_RETRY_FIRST * 4); @@ -6866,8 +6950,14 @@ async fn a_new_snapshot_try_asks_every_peer_again() { make_the_next_try_due(&mut service); service.on_tick().await; - assert!(service.snap_phase().is_some(), "precondition: the try happened"); - assert!(service.snap_avoid_for_test().is_empty(), "a peer is still set aside"); + assert!( + service.snap_phase().is_some(), + "precondition: the try happened" + ); + assert!( + service.snap_avoid_for_test().is_empty(), + "a peer is still set aside" + ); } /// A node that has executed something hands over to ordinary sync as before: @@ -6880,10 +6970,19 @@ async fn a_node_that_has_executed_still_hands_over_to_ordinary_sync() { store.update_head(&one, U256::from(2)).unwrap(); store.set_exec_head(one.hash(), one.state_root).unwrap(); - assert_eq!(fail_until_spent(&mut service).await, crate::service::MAX_SNAP_ATTEMPTS); + assert_eq!( + fail_until_spent(&mut service).await, + crate::service::MAX_SNAP_ATTEMPTS + ); - assert!(!service.is_suspended_for_snap_for_test(), "the peer set was not handed back"); - assert!(service.snap_retry_for_test().0.is_none(), "a try was set for a node with a way forward"); + assert!( + !service.is_suspended_for_snap_for_test(), + "the peer set was not handed back" + ); + assert!( + service.snap_retry_for_test().0.is_none(), + "a try was set for a node with a way forward" + ); } /// Execution recorded at genesis is execution of nothing, as `start_snap_sync` @@ -6896,8 +6995,14 @@ async fn a_node_executed_only_to_genesis_tries_snapshot_sync_again_too() { fail_until_spent(&mut service).await; - assert!(service.is_suspended_for_snap_for_test(), "handed over with nothing to execute from"); - assert!(service.snap_retry_for_test().0.is_some(), "no further try was set"); + assert!( + service.is_suspended_for_snap_for_test(), + "handed over with nothing to execute from" + ); + assert!( + service.snap_retry_for_test().0.is_some(), + "no further try was set" + ); } // ── serving headers: the canonical fast path (#185) ────────────────────────── @@ -8019,7 +8124,10 @@ async fn a_committed_uncle_without_proof_of_work_proves_nothing() { let verifiers = [ (HeaderVerifier::new(), 1_020u64), (HeaderVerifier::new().with_uncle_rule(NoWorkAt999), 21), - (HeaderVerifier::new().with_uncle_parent_rule(OffItsParentAt999), 21), + ( + HeaderVerifier::new().with_uncle_parent_rule(OffItsParentAt999), + 21, + ), ]; for (verifier, total) in verifiers { let dir = tempfile::tempdir().unwrap(); @@ -8039,7 +8147,11 @@ async fn a_committed_uncle_without_proof_of_work_proves_nothing() { let mut h = h.clone(); h.uncle_count = us.len() as u64; h.ommers_hash = alloy_primitives::keccak256( - HeaderWithUncles { header: h.clone(), uncles: us.to_vec() }.encoded_uncles(), + HeaderWithUncles { + header: h.clone(), + uncles: us.to_vec(), + } + .encoded_uncles(), ); h.cached_hash = None; h @@ -8048,17 +8160,30 @@ async fn a_committed_uncle_without_proof_of_work_proves_nothing() { let mut uncle = dummy_header(1, genesis_hash, U256::from(999)); uncle.gas_used = 1; uncle.cached_hash = None; - let b2 = - commit(&dummy_header(2, b1.hash(), U256::from(10)), std::slice::from_ref(&uncle)); + let b2 = commit( + &dummy_header(2, b1.hash(), U256::from(10)), + std::slice::from_ref(&uncle), + ); let hash = b2.hash(); - let entry = HeaderWithUncles { header: b2, uncles: vec![uncle] }; - assert!(entry.commitment_matches(), "the commitment is honest; the work is not"); + let entry = HeaderWithUncles { + header: b2, + uncles: vec![uncle], + }; + assert!( + entry.commitment_matches(), + "the commitment is honest; the work is not" + ); let headers = manager.note_proven_difficulties(vec![ - HeaderWithUncles { header: b1, uncles: Vec::new() }, + HeaderWithUncles { + header: b1, + uncles: Vec::new(), + }, entry, ]); - manager.handle_headers_response(headers, test_peer()).unwrap(); + manager + .handle_headers_response(headers, test_peer()) + .unwrap(); assert_eq!( store.total_difficulty(hash).unwrap(), @@ -8091,10 +8216,12 @@ struct NoWorkAt999; impl rustock_core::validation::HeaderValidator for NoWorkAt999 { fn validate(&self, h: &Header) -> Result<(), rustock_core::validation::ValidationError> { if h.difficulty == U256::from(999u64) { - return Err(rustock_core::validation::ValidationError::BitcoinPowInvalid { - hash: h.hash(), - target: U256::ZERO, - }); + return Err( + rustock_core::validation::ValidationError::BitcoinPowInvalid { + hash: h.hash(), + target: U256::ZERO, + }, + ); } Ok(()) } @@ -8111,10 +8238,12 @@ impl rustock_core::validation::ParentHeaderValidator for OffItsParentAt999 { _parent: &Header, ) -> Result<(), rustock_core::validation::ValidationError> { if h.difficulty == U256::from(999u64) { - return Err(rustock_core::validation::ValidationError::DifficultyMismatch { - expected: U256::from(10u64), - got: h.difficulty, - }); + return Err( + rustock_core::validation::ValidationError::DifficultyMismatch { + expected: U256::from(10u64), + got: h.difficulty, + }, + ); } Ok(()) } @@ -8139,7 +8268,11 @@ fn genesis_and_siblings(store: &BlockStore, first: u64, second: u64) -> (Header, async fn answer_body_requests(service: &mut SyncService, uncles_of: impl Fn(B256) -> Vec
) { for _ in 0..16 { let asked: Vec<(u64, B256, B512)> = match &service.state { - SyncState::DownloadingBodies { pending_headers, id_index, .. } => id_index + SyncState::DownloadingBodies { + pending_headers, + id_index, + .. + } => id_index .iter() .map(|(id, (i, peer))| (*id, pending_headers[*i].0, *peer)) .collect(), @@ -8167,7 +8300,11 @@ async fn caught_up( let peer_store = Arc::new(rustock_networking::peers::PeerStore::new()); let (tx, _rx) = mpsc::channel(rustock_networking::peers::PEER_CHANNEL_CAPACITY); peer_store.add_peer(B512::repeat_byte(0x01), tx).await; - let manager = Arc::new(SyncManager::new(store.clone(), Arc::new(verifier), peer_store.clone())); + let manager = Arc::new(SyncManager::new( + store.clone(), + Arc::new(verifier), + peer_store.clone(), + )); manager .handle_headers_response(vec![b3.clone(), b2.clone(), b1.clone()], test_peer()) .unwrap(); @@ -8182,10 +8319,18 @@ async fn caught_up( service.start_body_downloads(3).await; let named = b2.hash(); answer_body_requests(&mut service, |hash| { - if hash == named { uncles.to_vec() } else { Vec::new() } + if hash == named { + uncles.to_vec() + } else { + Vec::new() + } }) .await; - assert!(matches!(service.state, SyncState::Following), "{:?}", service.state); + assert!( + matches!(service.state, SyncState::Following), + "{:?}", + service.state + ); [b1, b2, b3] } @@ -8200,9 +8345,18 @@ async fn a_caught_up_batch_counts_the_uncles_its_bodies_bring() { let [b1, b2, b3] = caught_up(&store, HeaderVerifier::new(), b1, &uncles).await; // 1 + 10; then + 10 + 7 + 5; then + 10. - assert_eq!(store.total_difficulty(b1.hash()).unwrap(), Some(U256::from(11))); - assert_eq!(store.total_difficulty(b2.hash()).unwrap(), Some(U256::from(33))); - assert_eq!(store.total_difficulty(b3.hash()).unwrap(), Some(U256::from(43))); + assert_eq!( + store.total_difficulty(b1.hash()).unwrap(), + Some(U256::from(11)) + ); + assert_eq!( + store.total_difficulty(b2.hash()).unwrap(), + Some(U256::from(33)) + ); + assert_eq!( + store.total_difficulty(b3.hash()).unwrap(), + Some(U256::from(43)) + ); } /// A body's uncles count only once they pass the whole uncle rule: the rules @@ -8223,8 +8377,14 @@ async fn a_caught_up_body_whose_uncle_did_no_work_adds_no_uncle() { let [_, b2, b3] = caught_up(&store, verifier, b1, &uncles).await; // Neither uncle: rskj refuses the whole block over one. - assert_eq!(store.total_difficulty(b2.hash()).unwrap(), Some(U256::from(21))); - assert_eq!(store.total_difficulty(b3.hash()).unwrap(), Some(U256::from(31))); + assert_eq!( + store.total_difficulty(b2.hash()).unwrap(), + Some(U256::from(21)) + ); + assert_eq!( + store.total_difficulty(b3.hash()).unwrap(), + Some(U256::from(31)) + ); } } @@ -8240,11 +8400,19 @@ async fn a_followed_block_counts_its_uncles_when_its_body_comes() { let peer_store = Arc::new(rustock_networking::peers::PeerStore::new()); let verifier = Arc::new(HeaderVerifier::new()); - let manager = Arc::new(SyncManager::new(store.clone(), verifier, peer_store.clone())); - manager.handle_headers_response(vec![b1.clone()], test_peer()).unwrap(); + let manager = Arc::new(SyncManager::new( + store.clone(), + verifier, + peer_store.clone(), + )); + manager + .handle_headers_response(vec![b1.clone()], test_peer()) + .unwrap(); store.put_body(b1.hash(), &[], &[]).unwrap(); store.set_exec_head(b1.hash(), B256::ZERO).unwrap(); - manager.handle_headers_response(vec![b2.clone()], test_peer()).unwrap(); + manager + .handle_headers_response(vec![b2.clone()], test_peer()) + .unwrap(); assert_eq!( store.total_difficulty(b2.hash()).unwrap(), Some(U256::from(21)), @@ -8262,7 +8430,10 @@ async fn a_followed_block_counts_its_uncles_when_its_body_comes() { .on_body_response(peer, 7, vec![], uncles, None) .await; - assert_eq!(store.total_difficulty(b2.hash()).unwrap(), Some(U256::from(33))); + assert_eq!( + store.total_difficulty(b2.hash()).unwrap(), + Some(U256::from(33)) + ); } /// A header ingested again with its body held, as a fork announcement does to @@ -8278,11 +8449,15 @@ async fn a_header_ingested_again_credits_only_uncles_that_did_their_work() { let verifier = Arc::new(HeaderVerifier::new().with_uncle_rule(NoWorkAt999)); let peer_store = Arc::new(rustock_networking::peers::PeerStore::new()); let manager = SyncManager::new(store.clone(), verifier, peer_store); - manager.handle_headers_response(vec![b2.clone(), b1.clone()], test_peer()).unwrap(); + manager + .handle_headers_response(vec![b2.clone(), b1.clone()], test_peer()) + .unwrap(); store.put_body(b1.hash(), &[], &[]).unwrap(); store.put_body(b2.hash(), &[], &uncles).unwrap(); - manager.handle_headers_response(vec![b2.clone(), b1.clone()], test_peer()).unwrap(); + manager + .handle_headers_response(vec![b2.clone(), b1.clone()], test_peer()) + .unwrap(); assert_eq!( store.total_difficulty(b2.hash()).unwrap(), Some(U256::from(total)), @@ -8303,20 +8478,33 @@ async fn a_held_header_without_its_parent_keeps_its_total() { let b2 = naming_uncles(dummy_header(2, b1.hash(), U256::from(10)), &uncles); // #2 held with the total that counts its uncles; #1 not held at all. store.put_header(&b2).unwrap(); - store.put_total_difficulty(b2.hash(), U256::from(33)).unwrap(); + store + .put_total_difficulty(b2.hash(), U256::from(33)) + .unwrap(); let peer_store = Arc::new(rustock_networking::peers::PeerStore::new()); let manager = SyncManager::new(store.clone(), Arc::new(HeaderVerifier::new()), peer_store); // A peer at `rsk/63` proves what #2 adds, its uncles with it. let entries = vec![ - HeaderWithUncles { header: b1, uncles: Vec::new() }, - HeaderWithUncles { header: b2.clone(), uncles }, + HeaderWithUncles { + header: b1, + uncles: Vec::new(), + }, + HeaderWithUncles { + header: b2.clone(), + uncles, + }, ]; assert!(entries.iter().all(HeaderWithUncles::commitment_matches)); manager.note_proven_difficulties(entries); - manager.handle_headers_response(vec![b2.clone()], test_peer()).unwrap(); - assert_eq!(store.total_difficulty(b2.hash()).unwrap(), Some(U256::from(33))); + manager + .handle_headers_response(vec![b2.clone()], test_peer()) + .unwrap(); + assert_eq!( + store.total_difficulty(b2.hash()).unwrap(), + Some(U256::from(33)) + ); } /// An uncle is judged against the uncles its block's ancestors named, so it is @@ -8334,7 +8522,9 @@ async fn an_uncle_is_not_credited_while_an_ancestors_uncles_are_not_held() { let b3 = naming_uncles(dummy_header(3, b2.hash(), U256::from(10)), reused); for (header, total) in [(&b1, 11u64), (&b2, 28)] { store.put_header(header).unwrap(); - store.put_total_difficulty(header.hash(), U256::from(total)).unwrap(); + store + .put_total_difficulty(header.hash(), U256::from(total)) + .unwrap(); } store.put_body(b1.hash(), &[], &[]).unwrap(); store.put_header(&b3).unwrap(); @@ -8351,14 +8541,20 @@ async fn an_uncle_is_not_credited_while_an_ancestors_uncles_are_not_held() { // With #2's uncles held, the rule sees the reuse and refuses it. store.put_body(b2.hash(), &[], reused).unwrap(); assert!(manager.settle_total_difficulty(b3.hash(), &b3, reused)); - assert_eq!(store.total_difficulty(b3.hash()).unwrap(), Some(U256::from(38))); + assert_eq!( + store.total_difficulty(b3.hash()).unwrap(), + Some(U256::from(38)) + ); // And an uncle that no ancestor named is credited: 28 + 10 + 5. let fresh = &uncles[1..]; let b3 = naming_uncles(dummy_header(3, b2.hash(), U256::from(10)), fresh); store.put_header(&b3).unwrap(); assert!(manager.settle_total_difficulty(b3.hash(), &b3, fresh)); - assert_eq!(store.total_difficulty(b3.hash()).unwrap(), Some(U256::from(43))); + assert_eq!( + store.total_difficulty(b3.hash()).unwrap(), + Some(U256::from(43)) + ); } /// The oldest ancestor the rule walks, seven generations back or genesis, must @@ -8371,7 +8567,9 @@ async fn the_oldest_ancestor_the_uncle_rule_walks_must_be_held_too() { store.update_head(&genesis, U256::from(1)).unwrap(); let b1 = dummy_header(1, genesis.hash(), U256::from(10)); store.put_header(&b1).unwrap(); - store.put_total_difficulty(b1.hash(), U256::from(11)).unwrap(); + store + .put_total_difficulty(b1.hash(), U256::from(11)) + .unwrap(); store.put_body(b1.hash(), &[], &[]).unwrap(); let uncles = vec![dummy_header(1, genesis.hash(), U256::from(7))]; let b2 = naming_uncles(dummy_header(2, b1.hash(), U256::from(10)), &uncles); @@ -8388,7 +8586,10 @@ async fn the_oldest_ancestor_the_uncle_rule_walks_must_be_held_too() { store.put_body(genesis.hash(), &[], &[]).unwrap(); assert!(manager.settle_total_difficulty(b2.hash(), &b2, &uncles)); - assert_eq!(store.total_difficulty(b2.hash()).unwrap(), Some(U256::from(28))); + assert_eq!( + store.total_difficulty(b2.hash()).unwrap(), + Some(U256::from(28)) + ); } /// A batch stopped by a block that fails consensus validation is not clean, @@ -8420,7 +8621,10 @@ fn a_batch_stopped_by_a_block_that_fails_validation_is_not_clean() { transactions: Vec::new(), ommers: Vec::new(), }; - assert!(processor.validate_block(&block).is_err(), "the block must fail validation"); + assert!( + processor.validate_block(&block).is_err(), + "the block must fail validation" + ); let outcome = crate::service::process_downloaded_blocks( &processor, @@ -8434,7 +8638,10 @@ fn a_batch_stopped_by_a_block_that_fails_validation_is_not_clean() { &crate::chain_activity::ChainActivity::new(), &crate::gas_price::GasPriceTracker::default(), ); - assert!(!outcome.all_ok, "a batch stopped by an invalid block reported itself clean"); + assert!( + !outcome.all_ok, + "a batch stopped by an invalid block reported itself clean" + ); assert_eq!( store.exec_head().unwrap().map(|(hash, _)| hash), Some(genesis.hash()), diff --git a/crates/trie/src/node.rs b/crates/trie/src/node.rs index 7536460..48f4705 100644 --- a/crates/trie/src/node.rs +++ b/crates/trie/src/node.rs @@ -1287,14 +1287,21 @@ mod tests { /// Each node is a child of the one after it, and the last is `root`. fn assert_chained(nodes: &[TrieNode], root: &TrieNode, store: &MemoryTrieStore) { - assert_eq!(nodes.last().expect("a path").compute_hash(store), root.compute_hash(store)); + assert_eq!( + nodes.last().expect("a path").compute_hash(store), + root.compute_hash(store) + ); for pair in nodes.windows(2) { let (child, parent) = (&pair[0], &pair[1]); let message = child.to_message(store); - let linked = [&parent.left, &parent.right] - .iter() - .any(|r| r.resolve(store).is_some_and(|c| c.to_message(store) == message)); - assert!(linked, "a node on the path is not a child of the one above it"); + let linked = [&parent.left, &parent.right].iter().any(|r| { + r.resolve(store) + .is_some_and(|c| c.to_message(store) == message) + }); + assert!( + linked, + "a node on the path is not a child of the one above it" + ); } } @@ -1305,9 +1312,16 @@ mod tests { let root = branching(&store); let key = TrieKeySlice::from_key(&[21, 7]); - let toward = root.try_nodes_toward(&key, &store).expect("a complete store"); + let toward = root + .try_nodes_toward(&key, &store) + .expect("a complete store"); let on_path = root.nodes_on_path(&key, &store).expect("present"); - let messages = |nodes: &[TrieNode]| nodes.iter().map(|n| n.to_message(&store)).collect::>(); + let messages = |nodes: &[TrieNode]| { + nodes + .iter() + .map(|n| n.to_message(&store)) + .collect::>() + }; assert_eq!(messages(&toward), messages(&on_path)); } @@ -1325,8 +1339,14 @@ mod tests { ]; for key in absent { let slice = TrieKeySlice::from_key(key); - assert_eq!(root.try_get(&slice, &store), Ok(None), "{key:?} is in the trie"); - let nodes = root.try_nodes_toward(&slice, &store).expect("a complete store"); + assert_eq!( + root.try_get(&slice, &store), + Ok(None), + "{key:?} is in the trie" + ); + let nodes = root + .try_nodes_toward(&slice, &store) + .expect("a complete store"); assert_chained(&nodes, &root, &store); } @@ -1350,7 +1370,9 @@ mod tests { // descendants. let empty = MemoryTrieStore::new(); let detached = TrieNode::from_message(&root.to_message(&store), &empty); - assert!(detached.try_nodes_toward(&TrieKeySlice::from_key(&[21, 8]), &empty).is_err()); + assert!(detached + .try_nodes_toward(&TrieKeySlice::from_key(&[21, 8]), &empty) + .is_err()); } #[test] diff --git a/crates/trie/src/snapshot_legacy.rs b/crates/trie/src/snapshot_legacy.rs index e4184d7..8f71df7 100644 --- a/crates/trie/src/snapshot_legacy.rs +++ b/crates/trie/src/snapshot_legacy.rs @@ -629,7 +629,11 @@ pub fn rebuild(chunk: &LegacyChunk, expected_root: B256) -> Result Result>, RebuildError> { let unplaced = || RebuildError::Unplaced; - let total = |i: usize| stubs[i].children_size.checked_add(own[i]).ok_or_else(unplaced); + let total = |i: usize| { + stubs[i] + .children_size + .checked_add(own[i]) + .ok_or_else(unplaced) + }; let mut starts: Vec> = vec![None; stubs.len()]; // Each node, with the offset its subtree begins at. @@ -886,7 +895,11 @@ fn parse_stub( let message = consensus_message(&stub, None, None).ok_or_else(bad)?; // `total_size` of the child: its message, the value it keeps // outside it, and whatever hangs below it. - let outside = if stub.has_long_value { stub.value.len() as u64 } else { 0 }; + let outside = if stub.has_long_value { + stub.value.len() as u64 + } else { + 0 + }; embedded_total = (message.len() as u64) .checked_add(outside) .and_then(|size| size.checked_add(stub.children_size)) diff --git a/crates/trie/tests/rskj_vectors.rs b/crates/trie/tests/rskj_vectors.rs index 5abd0d1..f69b32f 100644 --- a/crates/trie/tests/rskj_vectors.rs +++ b/crates/trie/tests/rskj_vectors.rs @@ -474,17 +474,26 @@ fn rskj_chunks_place_themselves_where_the_trie_has_them() { let stream = chunk_from(&root, 0, u64::MAX, &store); let k = locate(&chunk, &stream, &store, from); let last = &stream[k + chunk.nodes.len() - 1]; - assert_eq!(start, stream[k].offset, "{keys} keys {from}..{to}: where it begins"); + assert_eq!( + start, stream[k].offset, + "{keys} keys {from}..{to}: where it begins" + ); assert_eq!(end, last.end(), "{keys} keys {from}..{to}: where it ends"); - assert!(start <= from, "{keys} keys {from}..{to}: begins at {start}, after {from}"); + assert!( + start <= from, + "{keys} keys {from}..{to}: begins at {start}, after {from}" + ); assert!( end < to || end == total, "{keys} keys {from}..{to}: runs to {end}, past the range" ); if let Some((was, until, reached)) = previous { if was == keys && until == from { - assert_eq!(start, reached, "{keys} keys: {from}.. does not pick up at {reached}"); + assert_eq!( + start, reached, + "{keys} keys: {from}.. does not pick up at {reached}" + ); } } previous = Some((keys, to, end)); @@ -532,10 +541,17 @@ fn asked_where_they_stopped_rskj_chunks_tile_the_trie() { let (start, end) = rebuilt .span .unwrap_or_else(|| panic!("cell {cell} at {asked}: nothing of its own")); - assert!(start <= asked, "cell {cell} at {asked}: begins at {start}, after the offset"); + assert!( + start <= asked, + "cell {cell} at {asked}: begins at {start}, after the offset" + ); let k = locate(&chunk, &stream, &store, asked); assert_eq!(start, stream[k].offset, "cell {cell} at {asked}"); - assert_eq!(end, stream[k + chunk.nodes.len() - 1].end(), "cell {cell} at {asked}"); + assert_eq!( + end, + stream[k + chunk.nodes.len() - 1].end(), + "cell {cell} at {asked}" + ); assert!(end > reached, "cell {cell} at {asked}: no progress"); reached = end; asked = reached + 1; @@ -595,7 +611,9 @@ fn a_chunk_that_leaves_out_a_stretch_is_refused() { let k = locate(&chunk, &stream, &store, asked); let last = &stream[k + n - 1]; let node = rustock_trie::TrieNode::from_message(&last.message, &store); - let NodeRef::Hash(left) = node.left.clone() else { continue }; + let NodeRef::Hash(left) = node.left.clone() else { + continue; + }; let left_size = total_size(&NodeRef::Hash(left).resolve(&store).expect("held"), &store); // The last node's left subtree, wholly inside the chunk and not // the whole of what precedes the last node. @@ -683,12 +701,21 @@ fn a_node_hung_where_its_parent_has_none_is_refused() { match rebuild(&tampered, root_hash) { Err(RebuildError::Dangling { .. }) => dangling += 1, Err(_) => {} - Ok(r) => panic!("a node hung where there is no child was taken, spanning {:?}", r.span), + Ok(r) => panic!( + "a node hung where there is no child was taken, spanning {:?}", + r.span + ), } } } - assert!(tampered_with > 0, "no chunk ended in a node without a separate right child"); - assert!(dangling > 0, "the root caught every tampering, so the link check was never reached"); + assert!( + tampered_with > 0, + "no chunk ended in a node without a separate right child" + ); + assert!( + dangling > 0, + "the root caught every tampering, so the link check was never reached" + ); } /// A trie in the shape Rootstock's state has: accounts, and under a contract's @@ -711,15 +738,25 @@ fn unitrie(accounts: usize) -> (TrieNode, MemoryTrieStore) { for i in 0..accounts { let address = Address::from_word(B256::from(alloy_primitives::U256::from(i as u64 + 1))); // An account state: nonce and balance, well under 32 bytes. - root = put(root, account_key(&address), vec![0xc4, (i as u8).wrapping_add(1), 0x82, 0x03, 0xe8]); + root = put( + root, + account_key(&address), + vec![0xc4, (i as u8).wrapping_add(1), 0x82, 0x03, 0xe8], + ); if i % 3 == 0 { continue; } - let code: Vec = (0..(40 + i % 90) as u8).map(|b| b.wrapping_mul(i as u8 | 1)).collect(); + let code: Vec = (0..(40 + i % 90) as u8) + .map(|b| b.wrapping_mul(i as u8 | 1)) + .collect(); root = put(root, code_key(&address), code); for slot in 0..(i % 6) as u64 { let value = vec![(slot as u8).wrapping_add(i as u8) | 1; 1 + (slot as usize * 7) % 32]; - root = put(root, storage_key(&address, &B256::from(alloy_primitives::U256::from(slot))), value); + root = put( + root, + storage_key(&address, &B256::from(alloy_primitives::U256::from(slot))), + value, + ); } } root.save(&store, true); @@ -754,38 +791,69 @@ fn every_node_can_begin_a_chunk_where_it_sits() { let asked = entry.end(); let chunk = legacy_chunk(&root_bytes, asked, asked + 2_000, &store).expect("builds"); let rebuilt = rebuild(&chunk, root_hash).unwrap_or_else(|e| panic!("node {i}: {e}")); - let (start, end) = rebuilt.span.unwrap_or_else(|| panic!("node {i}: nothing of its own")); + let (start, end) = rebuilt + .span + .unwrap_or_else(|| panic!("node {i}: nothing of its own")); let k = locate(&chunk, &stream, &store, asked); - assert_eq!(k, i, "the chunk asked at the end of node {i} begins elsewhere"); - assert_eq!(start, entry.offset, "node {i} is placed at {start}, not {}", entry.offset); - assert_eq!(end, stream[k + chunk.nodes.len() - 1].end(), "node {i}: where the chunk ends"); + assert_eq!( + k, i, + "the chunk asked at the end of node {i} begins elsewhere" + ); + assert_eq!( + start, entry.offset, + "node {i} is placed at {start}, not {}", + entry.offset + ); + assert_eq!( + end, + stream[k + chunk.nodes.len() - 1].end(), + "node {i}: where the chunk ends" + ); let node = TrieNode::from_message(&entry.message, &store); shapes.insert((kind(&node.left), kind(&node.right))); } - for shape in [("separate", "separate"), ("separate", "embedded"), ("embedded", "embedded")] { - assert!(shapes.contains(&shape), "no first node shaped {shape:?} among {shapes:?}"); + for shape in [ + ("separate", "separate"), + ("separate", "embedded"), + ("embedded", "embedded"), + ] { + assert!( + shapes.contains(&shape), + "no first node shaped {shape:?} among {shapes:?}" + ); } } /// The long values a rebuilt chunk's nodes name -- their own, and their /// embedded children's -- and how many of those were embedded, if each is /// among the values the rebuild kept. Panics on one that is not. -fn long_values_named(rebuilt: &rustock_trie::snapshot_legacy::Rebuilt, what: &str) -> (usize, usize) { +fn long_values_named( + rebuilt: &rustock_trie::snapshot_legacy::Rebuilt, + what: &str, +) -> (usize, usize) { use rustock_trie::{MemoryTrieStore, NodeRef, TrieNode}; - let kept: std::collections::HashSet<[u8; 32]> = - rebuilt.long_values.iter().map(|v| alloy_primitives::keccak256(v).0).collect(); + let kept: std::collections::HashSet<[u8; 32]> = rebuilt + .long_values + .iter() + .map(|v| alloy_primitives::keccak256(v).0) + .collect(); let nothing = MemoryTrieStore::new(); let (mut named, mut embedded) = (0, 0); for (_, message) in &rebuilt.nodes { let node = TrieNode::try_from_message(message, ¬hing).expect("a rebuilt node parses"); - let children = [&node.left, &node.right].into_iter().filter_map(|c| match c { - NodeRef::Node(n) => Some(n.as_ref()), - _ => None, - }); + let children = [&node.left, &node.right] + .into_iter() + .filter_map(|c| match c { + NodeRef::Node(n) => Some(n.as_ref()), + _ => None, + }); for (n, inside) in std::iter::once((&node, false)).chain(children.map(|n| (n, true))) { if n.has_long_value() { let hash = n.value_hash.expect("a long value is named by its hash"); - assert!(kept.contains(&hash.0), "{what}: a long value named and not kept"); + assert!( + kept.contains(&hash.0), + "{what}: a long value named and not kept" + ); named += 1; embedded += usize::from(inside); } @@ -837,7 +905,10 @@ fn every_long_value_a_chunk_names_comes_with_it() { from += cell; } } - assert!(embedded > 0, "no embedded long value was exercised ({named} named in all)"); + assert!( + embedded > 0, + "no embedded long value was exercised ({named} named in all)" + ); } /// The Rootstock-shaped trie of [`unitrie`], with long code under some From 73c0f102dcc9cf65259845a886b9aca3b0df45db Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Tue, 10 Nov 2026 08:35:50 -0800 Subject: [PATCH 091/264] execution: a test's switch of the sender emulation stays on its thread the_switch_removes_exactly_the_two_multisig_shapes turned rskj's sender emulation off for the whole process while it ran, so a test classifying a multisig sender beside it saw a refund refused; CI's Linux job hit it once. The test now sets the switch for its own thread, and the process-wide switch is left to the node. --- crates/execution/src/bridge/peg.rs | 8 ++++---- .../src/bridge/rskj_sender_compat.rs | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/crates/execution/src/bridge/peg.rs b/crates/execution/src/bridge/peg.rs index 60bf3db..6b9a615 100644 --- a/crates/execution/src/bridge/peg.rs +++ b/crates/execution/src/bridge/peg.rs @@ -7498,9 +7498,9 @@ mod tests { fn the_switch_removes_exactly_the_two_multisig_shapes() { use super::super::rskj_sender_compat as compat; - // Guard: this flips process-wide state, so restore it. - let was = compat::enabled(); - compat::set_enabled(false); + // On this test's thread alone: flipped for the whole process, the + // switch changed the answers of the tests running beside this one. + compat::set_enabled_on_this_thread(Some(false)); assert!( matches!(classify_pegin_sender(&p2pkh_tx()).unwrap(), Some(PeginSender::P2pkh { .. })), @@ -7519,7 +7519,7 @@ mod tests { "P2SH-P2WSH is no longer classified" ); - compat::set_enabled(was); + compat::set_enabled_on_this_thread(None); assert!( matches!(classify_pegin_sender(&p2sh_multisig_tx()).unwrap(), Some(PeginSender::P2shMultisig { .. })), "and switching back restores it" diff --git a/crates/execution/src/bridge/rskj_sender_compat.rs b/crates/execution/src/bridge/rskj_sender_compat.rs index 2570de2..ca46c81 100644 --- a/crates/execution/src/bridge/rskj_sender_compat.rs +++ b/crates/execution/src/bridge/rskj_sender_compat.rs @@ -129,9 +129,28 @@ pub fn set_enabled(on: bool) { } pub fn enabled() -> bool { + #[cfg(test)] + if let Some(on) = TEST_SWITCH.with(std::cell::Cell::get) { + return on; + } ENABLED.load(Ordering::Relaxed) } +#[cfg(test)] +thread_local! { + /// A test's own setting of the switch, on its own thread alone. A test + /// that flipped the process-wide switch changed the answers of the tests + /// running beside it. + static TEST_SWITCH: std::cell::Cell> = const { std::cell::Cell::new(None) }; +} + +/// Sets the switch for the calling thread alone, or with `None` clears that +/// setting. +#[cfg(test)] +pub(crate) fn set_enabled_on_this_thread(on: Option) { + TEST_SWITCH.with(|switch| switch.set(on)); +} + // --- observer -------------------------------------------------------------- /// A peg-in that exercised the path this node intends to remove. From b8f3e1dcc1cae1cfb5a937106b8a39c29402de4c Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Tue, 10 Nov 2026 08:45:12 -0800 Subject: [PATCH 092/264] execution: the notify test builds on a head its clock can't pass second_build_block_to_mine_turns_notify_flag_off builds work twice and expects one hash, but a child is stamped the wall clock, so two builds either side of a second's turn differ; it failed once under a full suite's load. rskj's test fixes its clock. Here the head is stamped in 2100, so a child is stamped one second after it, whatever the clock. --- crates/execution/src/mining/tests.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/crates/execution/src/mining/tests.rs b/crates/execution/src/mining/tests.rs index 21ccb75..1c134cb 100644 --- a/crates/execution/src/mining/tests.rs +++ b/crates/execution/src/mining/tests.rs @@ -939,7 +939,29 @@ fn is_submission_allowed_when_enabled_and_limit_exceeded_then_should_return_fals #[test] fn second_build_block_to_mine_turns_notify_flag_off() { let fx = fixture(20); + // rskj's test fixes its clock. A child is stamped the wall clock or one + // second past its parent, whichever is later, so on a parent stamped in + // 2100 both builds stamp the same second; on the wall clock, two builds + // either side of a second's turn would differ. + let mut ahead = header_at(21, fx.parent.hash(), fx.parent.state_root); + ahead.timestamp = 4_102_444_800; + ahead.umm_root = fx + .chain_config + .activation_heights + .umm_active(21) + .then(Bytes::new); + let hash = ahead.hash(); + fx.store.put_header_with_hash(hash, &ahead).unwrap(); + fx.store.put_canonical_hash(21, hash).unwrap(); + fx.store.put_total_difficulty(hash, U256::from(22)).unwrap(); + fx.store.set_head(hash).unwrap(); + fx.store.set_exec_head(hash, fx.parent.state_root).unwrap(); + let work = fx.server.get_work().unwrap(); + assert_eq!( + work.parent_block_hash, hash, + "built on the parent stamped in 2100" + ); assert!(work.notify); fx.server.build_work().unwrap(); let again = fx.server.get_work().unwrap(); From a51f6bf15e6f721d9272348ae787ff11fea393e7 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Tue, 10 Nov 2026 08:45:13 -0800 Subject: [PATCH 093/264] execution: the timed-miner test waits for its blocks a_timed_miner_mines_blocks_on_its_own slept a fixed 1.5 s and wanted three blocks, which a loaded machine doesn't always build in time (a macOS runner missed it twice in a row). It now waits up to a minute for three blocks, and after stopping waits for the thread to end, seen when the server's reference count falls back, rather than sleeping and assuming the block in flight is done. The count also shows that a second start takes no second thread. --- crates/execution/src/mining/tests.rs | 45 ++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/crates/execution/src/mining/tests.rs b/crates/execution/src/mining/tests.rs index 1c134cb..2229e62 100644 --- a/crates/execution/src/mining/tests.rs +++ b/crates/execution/src/mining/tests.rs @@ -2845,8 +2845,8 @@ fn the_commit_hook_runs_once_the_block_is_the_head() { ); } -/// The thread mines on its clock: at a 100 ms median (the floor), a second -/// of running yields several blocks, and stopping ends it. +/// The thread mines on its clock: at a 100 ms median (the floor), it yields +/// several blocks, and stopping ends it. #[test] fn a_timed_miner_mines_blocks_on_its_own() { let fx = fixture_without_pow(20, 10, Arc::new(MemoryTrieStore::new())); @@ -2855,22 +2855,49 @@ fn a_timed_miner_mines_blocks_on_its_own() { std::time::Duration::from_millis(100), Pow::Skip, ); + // The thread takes its own handle on the server, the only one taken from + // here on: the count is one higher while it runs and falls back when it + // ends. + let without_thread = Arc::strong_count(&fx.server); assert!(!miner.is_running()); miner.start(); miner.start(); // idempotent: one thread, not two assert!(miner.is_running()); - std::thread::sleep(std::time::Duration::from_millis(1_500)); + assert_eq!( + Arc::strong_count(&fx.server), + without_thread + 1, + "one thread, not two" + ); + + // Waits for the blocks, not a fixed time: a loaded machine builds them + // slower than the clock asks for. + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(60); + while miner.mined() < 3 { + assert!( + std::time::Instant::now() < deadline, + "only {} blocks in 60 s at a 100 ms median", + miner.mined() + ); + std::thread::sleep(std::time::Duration::from_millis(10)); + } miner.stop(); assert!(!miner.is_running()); - std::thread::sleep(std::time::Duration::from_millis(300)); + // The thread finishes the block in flight, if any, and ends. + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(60); + while Arc::strong_count(&fx.server) > without_thread { + assert!( + std::time::Instant::now() < deadline, + "still running 60 s after stop" + ); + std::thread::sleep(std::time::Duration::from_millis(10)); + } + // Pairs with the release in the thread's dropping its handle, so all it + // did before is seen here. + std::sync::atomic::fence(std::sync::atomic::Ordering::Acquire); let mined = miner.mined(); - assert!(mined >= 3, "only {mined} blocks in 1.5 s at a 100 ms median"); let head = fx.store.header(fx.store.head().unwrap().unwrap()).unwrap().unwrap(); - assert_eq!(head.number, 20 + mined); - let after = miner.mined(); - std::thread::sleep(std::time::Duration::from_millis(400)); - assert_eq!(miner.mined(), after, "still mining after stop"); + assert_eq!(head.number, 20 + mined, "the head is the last block it counted"); } /// A header whose recomputed hash is not the hash it is stored under: what a From 2ceb90da48a191221bc3543a32d4de53255eeffe Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Tue, 10 Nov 2026 10:32:01 -0800 Subject: [PATCH 094/264] execution: take the upgrade names of RSKj's release RSKj's latest release, VETIVER-9.0.4, names its one unscheduled upgrade tbd1000 and schedules only RSKIP559 under it. cardamom1000, with RSKIP378 and RSKIP455, is in RSKj's master and in no release. Accept tbd1000 as the release does, refuse cardamom1000 as the release's NetworkUpgrade.named refuses a name it doesn't know, and take regtest's schedule from the release's regtest.conf. The pegout sizer's comment cites the release's BridgeUtils.calculatePegoutTxSize. --- crates/cli/src/main.rs | 7 ++--- crates/execution/src/bridge/peg.rs | 6 ++-- crates/execution/src/hardfork.rs | 49 ++++++++++++++++-------------- 3 files changed, 32 insertions(+), 30 deletions(-) diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index c4b9f83..e4ab1f3 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -1338,10 +1338,9 @@ struct Args { /// rskj's: orchid to vetiver900, and afterBridgeSync, where the /// federators' and authorizers' Bridge transactions start to be paid. /// bahamas and hop401 are accepted at the height this node already reads - /// them at; orchid060, cardamom1000 and tbd1000 are accepted and change - /// nothing (cardamom1000 turns on RSKIP378, RSKIP455 and RSKIP559 in the - /// Bridge's pegout code, which this node doesn't implement yet; tbd1000 - /// is perf-lab's name for RSKIP559's upgrade). + /// them at; orchid060 and tbd1000 are accepted and change nothing + /// (tbd1000 is rskj's upgrade for RSKIP559, which no network schedules + /// and this node doesn't implement). #[arg(long, value_name = "NAME=HEIGHT")] hardfork: Vec, diff --git a/crates/execution/src/bridge/peg.rs b/crates/execution/src/bridge/peg.rs index 6b9a615..22dd9e0 100644 --- a/crates/execution/src/bridge/peg.rs +++ b/crates/execution/src/bridge/peg.rs @@ -5796,9 +5796,9 @@ fn estimated_fees_from_inputs_and_outputs_count( let outputs_count = queued_pegouts + 2; let inputs_count = 2; - // rskj BridgeUtils.simulatePegoutTxSize: RSKIP378 (cardamom1000) is -1 on - // both mainnet and testnet, so the pre-cardamom branch is the live one — - // and it picks the sizer by federation format, not by activation. + // rskj BridgeUtils.calculatePegoutTxSize: from RSKIP271, which every + // network activates before anything reaches here, the sizer goes by the + // federation's format, segwit for a P2SH-P2WSH ERP federation. let size = if format >= 4000 { pegout_tx_size_segwit(&redeem_script, &fed_script, threshold, inputs_count, outputs_count) } else { diff --git a/crates/execution/src/hardfork.rs b/crates/execution/src/hardfork.rs index 2caec27..f1cb4f0 100644 --- a/crates/execution/src/hardfork.rs +++ b/crates/execution/src/hardfork.rs @@ -108,8 +108,8 @@ impl ConsensusRule { impl RskNetworkUpgrade { /// The upgrade rskj calls `name` in `hardforkActivationHeights`, for the /// upgrades this ladder models. `bahamas`, `afterBridgeSync`, `orchid060`, - /// `hop401`, `cardamom1000` and `tbd1000` are rskj names too, but not - /// rungs here; see [`RskHardforkConfig::with_rskj_overrides`]. + /// `hop401` and `tbd1000` are rskj names too, but not rungs here; see + /// [`RskHardforkConfig::with_rskj_overrides`]. pub fn from_rskj_name(name: &str) -> Option { Some(match name { "orchid" => Self::Orchid, @@ -184,8 +184,8 @@ impl RskHardforkConfig { /// RSKIP144, RSKIP351 and RSKIP535 from block 1, so blocks run as their /// sublists from there; and `afterBridgeSync` never, so the federators' /// and authorizers' Bridge transactions stay free. A chain started from - /// an rskj genesis file runs on it. `regtest.conf` has `cardamom1000` at - /// 0 too, which this ladder doesn't model yet. + /// an rskj genesis file runs on it. `regtest.conf` leaves `tbd1000`, the + /// upgrade for RSKIP559, unscheduled, and this ladder doesn't model it. pub fn rskj_regtest() -> Self { Self { activations: vec![ @@ -345,12 +345,10 @@ impl RskHardforkConfig { /// transactions start to be paid, or never. /// - `bahamas`, `hop401`: read at one fixed height per chain, so they are /// accepted at that height and refused at any other. - /// - `orchid060`, `cardamom1000` and `tbd1000`: nothing here reads them. - /// `cardamom1000` turns on RSKIP378, RSKIP455 and RSKIP559 in the - /// Bridge's pegout code, which this node doesn't implement yet. - /// `tbd1000` is the name perf-lab's seed schedule passes for RSKIP559's - /// upgrade, which rskj's master calls `cardamom1000`. Accepted and - /// reported as such. + /// - `orchid060` and `tbd1000`: nothing here reads them. `tbd1000` is + /// rskj's upgrade for RSKIP559, which no network schedules and this + /// node doesn't implement. Accepted and reported as such. Any other + /// name is refused, as rskj's `NetworkUpgrade.named` refuses it. /// - `rskip144`, `rskip351`, `rskip535`, `rskip536`: scheduled apart from /// their upgrade. /// - `rskipUMM`: accepted at any height. Only a header's layout reads it, @@ -375,7 +373,7 @@ impl RskHardforkConfig { cannot move it" )); } - } else if matches!(name.as_str(), "orchid060" | "cardamom1000" | "tbd1000") { + } else if matches!(name.as_str(), "orchid060" | "tbd1000") { unmodelled.push(name.clone()); } else { return Err(format!("hardfork {name}: not an rskj upgrade name")); @@ -1516,11 +1514,12 @@ mod override_tests { /// `afterBridgeSync` is a height this ladder reads and moves, as rskj's /// `hardforkActivationHeights` sets it; at `-1` the free senders' Bridge - /// calls stay free for good, as on rskj's regtest. `cardamom1000`, rskj - /// master's name, and `tbd1000`, perf-lab's, are taken and change - /// nothing: neither is a rung. + /// calls stay free for good, as on rskj's regtest. `tbd1000` is taken + /// and changes nothing: it isn't a rung. `cardamom1000`, the name rskj + /// gave it after its latest release, is refused, as that release refuses + /// it. #[test] - fn after_bridge_sync_moves_and_cardamom1000_is_taken() { + fn after_bridge_sync_moves_and_tbd1000_is_taken() { let mainnet = RskHardforkConfig::mainnet(); assert!(!mainnet.has_are_bridge_txs_paid(369_999)); assert!(mainnet.has_are_bridge_txs_paid(370_000)); @@ -1534,13 +1533,16 @@ mod override_tests { assert!(!paid.has_are_bridge_txs_paid(4) && paid.has_are_bridge_txs_paid(5)); let (named, unmodelled) = RskHardforkConfig::rskj_regtest() - .with_rskj_overrides(&[h("cardamom1000", 0), h("tbd1000", 0)], &[]) + .with_rskj_overrides(&[h("tbd1000", 0)], &[]) .unwrap(); - assert_eq!(unmodelled, ["cardamom1000", "tbd1000"]); + assert_eq!(unmodelled, ["tbd1000"]); assert_eq!( named.activations, RskHardforkConfig::rskj_regtest().activations ); + assert!(RskHardforkConfig::rskj_regtest() + .with_rskj_overrides(&[h("cardamom1000", 0)], &[]) + .is_err()); } } @@ -1726,16 +1728,17 @@ mod rskj_schedule_tests { ); } - /// rskj's `regtest.conf`: its `hardforkActivationHeights`, and the - /// `consensusRules` the ladder takes (`rskip97` is the header's alone, - /// and the CLI takes it), with `-1` for never. + /// rskj's `regtest.conf`, as its latest release (VETIVER-9.0.4) ships it: + /// its `hardforkActivationHeights`, and the `consensusRules` the ladder + /// takes (`rskip97` is the header's alone, and the CLI takes it), with + /// `-1` for never. #[rustfmt::skip] const REGTEST_CONF_HARDFORKS: &[(&str, i64)] = &[ ("bahamas", 0), ("afterBridgeSync", -1), ("orchid", 0), ("orchid060", 0), ("wasabi100", 0), ("twoToThree", 0), ("papyrus200", 0), ("iris300", 0), ("hop400", 0), ("hop401", 0), ("fingerroot500", 0), ("arrowhead600", 0), ("arrowhead631", -1), ("lovell700", 0), ("reed800", 0), ("reed810", 0), - ("vetiver900", 0), ("cardamom1000", 0), + ("vetiver900", 0), ("tbd1000", -1), ]; const REGTEST_CONF_RULES: &[(&str, i64)] = &[ ("rskipUMM", 1), @@ -1747,7 +1750,7 @@ mod rskj_schedule_tests { /// rskj's `regtest.conf`, name by name, on rustock's own regtest ladder /// is `rskj_regtest`, rule for rule: what a node replaying rskj's /// regtest blocks would pass by hand, and what `--genesis` gives it. - /// `cardamom1000` is taken and changes nothing. + /// `orchid060` and `tbd1000` are taken and change nothing. #[test] fn rskj_regtest_conf_by_name_is_rskj_regtest() { let named = |list: &[(&str, i64)]| -> Vec<(String, Option)> { @@ -1758,7 +1761,7 @@ mod rskj_schedule_tests { let (by_name, unmodelled) = RskHardforkConfig::regtest() .with_rskj_overrides(&named(REGTEST_CONF_HARDFORKS), &named(REGTEST_CONF_RULES)) .unwrap(); - assert_eq!(unmodelled, ["orchid060", "cardamom1000"]); + assert_eq!(unmodelled, ["orchid060", "tbd1000"]); let built_in = RskHardforkConfig::rskj_regtest(); for &(rule, active, _, _) in SCHEDULE { for n in [0, 1, 2, LATER] { From b718b4502bfd1bece038579a3de15ce91f6afeb9 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Tue, 10 Nov 2026 10:32:20 -0800 Subject: [PATCH 095/264] bridge: before RSKIP88, an illegal argument is a success with no output RSKj's Bridge.execute answers a BridgeIllegalArgumentException with null before RSKIP88, so the call succeeds with no output and is charged its gas. From RSKIP88 it throws. Two methods throw it there: receiveHeaders, for a header of the wrong size or one it can't parse, and registerBtcTransaction, for a partial merkle tree of the wrong size. Here both failed the call at every height. --- crates/execution/src/bridge/btc_chain.rs | 52 ++++++++++++---- crates/execution/src/bridge/peg.rs | 78 ++++++++++++++++++++---- 2 files changed, 107 insertions(+), 23 deletions(-) diff --git a/crates/execution/src/bridge/btc_chain.rs b/crates/execution/src/bridge/btc_chain.rs index c98cf50..9db41e0 100644 --- a/crates/execution/src/bridge/btc_chain.rs +++ b/crates/execution/src/bridge/btc_chain.rs @@ -523,25 +523,32 @@ pub fn receive_headers( let block_number = revm::context_interface::Block::number(ctx.block()).to::(); // rskj `Bridge.receiveHeaders` refuses the whole call when any header is - // not a header's size, before it parses one (Bridge.java:537-542). + // not a header's size, before it parses one (Bridge.java:537-542), and + // then when it can't parse one (`makeBlock`). Both throw + // `BridgeIllegalArgumentException`, which `Bridge.execute` answers with + // null before RSKIP88: a success with no output, nothing saved. let rskip124 = hardfork_cfg.has_rskip124(block_number); + let refuse = |why: &'static str| -> Result { + if hardfork_cfg.has_rskip88(block_number) { + Err(PrecompileError::other(why)) + } else { + Ok(PrecompileOutput::new( + gas_cost, + alloy_primitives::Bytes::new(), + )) + } + }; if headers .iter() .any(|header| !is_block_header_size(header.len(), rskip124)) { - return Err(PrecompileError::other( - "receiveHeaders: a header is not a header's size", - )); + return refuse("receiveHeaders: a header is not a header's size"); } - // Then it parses every header (`makeBlock`), and refuses the whole call - // for one it can't parse. if headers .iter() .any(|header| !no_transactions_follow(&header[80..])) { - return Err(PrecompileError::other( - "receiveHeaders: a header could not be parsed", - )); + return refuse("receiveHeaders: a header could not be parsed"); } let rskip434 = hardfork_cfg.has_rskip434(block_number); @@ -1206,13 +1213,34 @@ mod tests { } /// rskj's `receiveHeaders` checks every header's size before it takes any, - /// and refuses the whole call for one of the wrong size. + /// and refuses the whole call for one of the wrong size, or for one it + /// can't parse. Both throw `BridgeIllegalArgumentException`, which + /// `Bridge.execute` answers with null before RSKIP88: a success with no + /// output, charged its gas. From RSKIP88 they throw. #[test] fn receive_headers_refuses_them_all_for_one_of_the_wrong_size() { use crate::abi::encode::{self, Arg}; - let args = encode::args(&[Arg::BytesArray(&[&[0u8; 80], &[0u8; 79]])]); let (config, cfg) = (BridgeConstants::mainnet(), RskHardforkConfig::mainnet()); - assert!(receive_headers(&mut empty_ctx(), &args, 0, &config, false, &cfg).is_err()); + assert!(!cfg.has_rskip88(0) && cfg.has_rskip88(729_000) && !cfg.has_rskip124(729_000)); + let wrong_size = encode::args(&[Arg::BytesArray(&[&[0u8; 80], &[0u8; 79]])]); + // 81 bytes, a size bitcoinj took before RSKIP124, counting one + // transaction that isn't there. + let mut counted = [0u8; 81]; + counted[80] = 1; + let unparsed = encode::args(&[Arg::BytesArray(&[&counted])]); + let at = |number: u64, args: &[u8]| { + use revm::MainContext; + let mut ctx = + revm::Context::mainnet().with_chain(crate::raw_storage::RskChainExt::default()); + ctx.block.number = alloy_primitives::U256::from(number); + receive_headers(&mut ctx, args, 5_000, &config, false, &cfg) + }; + for args in [&wrong_size, &unparsed] { + let out = at(0, args).expect("before RSKIP88, a success"); + assert!(out.bytes.is_empty()); + assert_eq!(out.gas_used, 5_000); + assert!(at(729_000, args).is_err(), "from RSKIP88, a throw"); + } } /// rskj runs `ensureBtcBlockStore` before every read of the BTC chain, so a diff --git a/crates/execution/src/bridge/peg.rs b/crates/execution/src/bridge/peg.rs index 22dd9e0..d659c4f 100644 --- a/crates/execution/src/bridge/peg.rs +++ b/crates/execution/src/bridge/peg.rs @@ -102,10 +102,13 @@ fn pegin_below_minimum(live_output_values: &[u64], min: u64, rskip293: bool) -> enum Invalid { /// `return false`. Swallowed(&'static str), - /// A throw: `BridgeIllegalArgumentException`, `VerificationException`, - /// `BlockStoreException`, or the `NullPointerException` of a block the - /// store does not have. + /// A throw: `VerificationException`, `BlockStoreException`, or the + /// `NullPointerException` of a block the store does not have. Thrown(&'static str), + /// A throw of `BridgeIllegalArgumentException`: as `Thrown` from RSKIP88, + /// but before it `Bridge.execute` answers it with null, a success with no + /// output. + IllegalArgument(&'static str), } /// rskj `BridgeSupport.validationsForRegisterBtcTransaction`. Its order is @@ -137,9 +140,9 @@ fn validations_for_register_btc_transaction( return Err(Invalid::Swallowed("insufficient confirmations")); } - // A tree of the wrong size throws. + // A tree of the wrong size throws `BridgeIllegalArgumentException`. if !PartialMerkleTree::has_expected_size(pmt_data) { - return Err(Invalid::Thrown( + return Err(Invalid::IllegalArgument( "PartialMerkleTree doesn't have expected size", )); } @@ -296,10 +299,19 @@ pub fn register_btc_transaction( ); return Ok(PrecompileOutput::new(gas_cost, Bytes::new())); } - Err(Invalid::Thrown(m)) => { + // Before RSKIP88 `Bridge.execute` answers a + // `BridgeIllegalArgumentException` with null: the call succeeds with no + // output and saves nothing. + Err(Invalid::IllegalArgument(m)) if !hardfork_cfg.has_rskip88(block_number) => { + tracing::debug!( + "registerBtcTransaction: {m}; before RSKIP88, a success with no output" + ); + return Ok(PrecompileOutput::new(gas_cost, Bytes::new())); + } + Err(Invalid::Thrown(m) | Invalid::IllegalArgument(m)) => { tracing::debug!( "registerBtcTransaction: {m}; throwing \ - (rskj BridgeIllegalArgumentException escapes the catch)" + (the throw escapes rskj's catch)" ); return Err(PrecompileError::other(m)); } @@ -1827,7 +1839,9 @@ pub fn register_fast_bridge_btc_transaction( flyover_codes::UNPROCESSABLE_VALIDATIONS, )) } - Err(Invalid::Thrown(_)) => return ok(flyover_code_output(flyover_codes::GENERIC_ERROR)), + Err(Invalid::Thrown(_) | Invalid::IllegalArgument(_)) => { + return ok(flyover_code_output(flyover_codes::GENERIC_ERROR)) + } } // Parse the BTC tx and compute the witness-stripped txid (getHash(false)). @@ -8382,6 +8396,7 @@ mod tests { Ok(()) => ("valid", ""), Err(Invalid::Swallowed(why)) => ("false", why), Err(Invalid::Thrown(why)) => ("throws", why), + Err(Invalid::IllegalArgument(why)) => ("illegal argument", why), } }; let garbage = [0xFF; 3]; @@ -8391,9 +8406,9 @@ mod tests { ("false", "insufficient confirmations") ); assert_eq!(run(-1, &garbage, &ONE_INPUT), ("false", "negative height")); - // Then the tree: the wrong size throws, and a tree without the - // transaction is false. - assert_eq!(run(0, &garbage, &ONE_INPUT).0, "throws"); + // Then the tree: the wrong size throws BridgeIllegalArgumentException, + // and a tree without the transaction is false. + assert_eq!(run(0, &garbage, &ONE_INPUT).0, "illegal argument"); assert_eq!( run(0, &one_leaf_tree(&NO_INPUTS), &ONE_INPUT), ("false", "tx not in PMT") @@ -8919,6 +8934,47 @@ mod tests { outcome(&mut ctx, tx, height, &RskHardforkConfig::rskj_regtest()) } + /// rskj's `validationsForRegisterBtcTransaction` throws + /// `BridgeIllegalArgumentException` for a partial merkle tree of the wrong + /// size, which `Bridge.execute` answers with null before RSKIP88: the call + /// succeeds with no output, charged its gas, and registers nothing. From + /// RSKIP88 it throws. + #[test] + fn before_rskip88_a_tree_of_the_wrong_size_is_a_success_with_no_output() { + use revm::MainContext; + let (config, cfg) = (BridgeConstants::mainnet(), RskHardforkConfig::mainnet()); + assert!(!cfg.has_rskip88(728_999) && cfg.has_rskip88(729_000)); + let (_, checkpoint, _) = config.btc_checkpoint.unwrap(); + let tx = tx_with(vec![], vec![]); + let raw = btc_serialize(&tx); + // Confirmed well past the minimum, so the tree's size is the check + // that answers. + let args = encode::args(&[ + Arg::Bytes(&raw), + Arg::Word(encode::int(i64::from(checkpoint) - 200)), + Arg::Bytes(&[0u8; 3]), + ]); + let at = |number: u64| { + let mut ctx = + revm::Context::mainnet().with_chain(crate::raw_storage::RskChainExt::default()); + ctx.block.number = U256::from(number); + let out = register_btc_transaction( + &mut ctx, + &args, + 7_000, + &config, + &cfg, + &BridgeTxContext::default(), + ); + (out, is_btc_tx_processed(&mut ctx, &legacy_btc_txid(&tx))) + }; + let (out, processed) = at(728_999); + let out = out.expect("a success before RSKIP88"); + assert!(out.bytes.is_empty() && !processed); + assert_eq!(out.gas_used, 7_000); + assert!(at(729_000).0.is_err(), "a throw from RSKIP88"); + } + /// `registerBtcTransaction` of `tx` at Bitcoin `height` in `ctx`, with RSKj's /// regtest constants and `cfg`. Answers whether the call succeeded, whether /// it marked the transaction processed, how many events it logged, and how From 9dfd6afc11653ebc42f8a2e50e7f14efab417098 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Tue, 10 Nov 2026 10:32:28 -0800 Subject: [PATCH 096/264] bridge: testnet's early ERP redeem scripts RSKj builds a non-standard ERP federation's redeem script with the builder NonStandardErpRedeemScriptBuilderFactory picks for the block being run: on testnet before RSKIP284 (#2,581,800), one hard-coded script whatever the keys; on any network before RSKIP293 (hop400), the CSV delay as two unsigned big-endian bytes; after, the builder used until now. RSKj rebuilds a stored federation with the block's activations, so the same federation's script, and its address, change at those heights. Build it that way for the active, retiring and proposed federations and for a handover's old federation, whose format still follows its creation block. The three scripts match the real values in RSKj's NonStandardErpFederationsTest byte for byte. --- crates/execution/src/bridge/getters.rs | 5 +- crates/execution/src/bridge/governance.rs | 41 ++-- crates/execution/src/bridge/mod.rs | 4 +- crates/execution/src/bridge/peg.rs | 261 +++++++++++++++++++--- 4 files changed, 258 insertions(+), 53 deletions(-) diff --git a/crates/execution/src/bridge/getters.rs b/crates/execution/src/bridge/getters.rs index f82eacc..b4a5bae 100644 --- a/crates/execution/src/bridge/getters.rs +++ b/crates/execution/src/bridge/getters.rs @@ -673,13 +673,16 @@ pub fn get_proposed_federation_address( ctx: &mut CTX, gas_cost: u64, config: &BridgeConstants, + hardfork_cfg: &crate::hardfork::RskHardforkConfig, ) -> Result { let Some(fed) = super::federation::load_stored_federation(ctx, PROPOSED_FEDERATION_KEY) else { return Ok(PrecompileOutput::new(gas_cost, abi_encode_string("").into())); }; let keys: Vec<[u8; 33]> = fed.members.iter().map(|m| m.btc).collect(); let format = super::peg::federation_format_version_pub(ctx, PROPOSED_FEDERATION_FORMAT_VERSION_KEY); - let redeem = super::peg::federation_redeem_for_format_pub(&keys, format, config); + let block_number = revm::context_interface::Block::number(ctx.block()).to::(); + let redeem = + super::peg::federation_redeem_for_format_pub(&keys, format, config, hardfork_cfg, block_number); let hash160 = super::peg::federation_output_hash160(&redeem, format); let address = super::governance::p2sh_base58_address(&hash160, config); Ok(PrecompileOutput::new(gas_cost, abi_encode_string(&address).into())) diff --git a/crates/execution/src/bridge/governance.rs b/crates/execution/src/bridge/governance.rs index cb3f1ac..d890411 100644 --- a/crates/execution/src/bridge/governance.rs +++ b/crates/execution/src/bridge/governance.rs @@ -517,15 +517,16 @@ pub(crate) fn commit_proposed_federation( // RSKIP186: saveLastRetiredFederationScript (the retiring = old fed) + // setNextFederationCreationBlockHeight(proposed fed's creation block). if hardfork_cfg.has_rskip186(block_number) { - let old_redeem = match &active { - Some(_) => super::peg::build_committed_federation_redeem_script( - &old_keys, - config, - hardfork_cfg, - old_block, - ), - None => super::peg::federation_redeem_for_format_pub(&old_keys, old_format, config), - }; + // Its format is the one its creation gave it, but rskj rebuilds a + // stored federation with this block's activations, which pick the + // non-standard ERP builder. + let old_redeem = super::peg::federation_redeem_for_format_pub( + &old_keys, + old_format, + config, + hardfork_cfg, + block_number, + ); let old_hash160 = super::peg::redeem_script_hash160_pub(&old_redeem); // RSKIP377: getDefaultP2SHScript() = getOutputScript(getDefaultRedeemScript()) // — the *default* (standard multisig) redeem, wrapped P2SH-P2WSH for a @@ -615,17 +616,17 @@ fn commit_pending_federation( // Redeem scripts / hash160s: needed for the commit_federation event in BOTH // paths, and for the retiring fed's lastRetiredFederationP2SHScript in the // legacy handover. The retiring fed's redeem is rebuilt as the type it was - // CREATED with (keyed on its creation block); the built fed's by THIS - // block's activations (rskj PendingFederation.buildFederation). - let old_redeem = match &active { - Some(_) => super::peg::build_committed_federation_redeem_script( - &old_keys, - config, - hardfork_cfg, - old_block, - ), - None => super::peg::federation_redeem_for_format_pub(&old_keys, old_format, config), - }; + // CREATED with (its format, keyed on its creation block), but with THIS + // block's activations, as rskj reads a stored federation: they pick the + // non-standard ERP builder. The built fed's is by THIS block's + // activations too (rskj PendingFederation.buildFederation). + let old_redeem = super::peg::federation_redeem_for_format_pub( + &old_keys, + old_format, + config, + hardfork_cfg, + block_number, + ); let new_redeem = super::peg::build_committed_federation_redeem_script( &new_keys, config, hardfork_cfg, block_number, ); diff --git a/crates/execution/src/bridge/mod.rs b/crates/execution/src/bridge/mod.rs index 49d5c54..e73e00a 100644 --- a/crates/execution/src/bridge/mod.rs +++ b/crates/execution/src/bridge/mod.rs @@ -996,7 +996,9 @@ fn execute_method( "getPendingFederationHash" => getters::get_pending_federation_hash(ctx, gas_cost), "getPendingFederatorPublicKey" => getters::get_pending_federator_public_key(ctx, args, gas_cost), "getPendingFederatorPublicKeyOfType" => getters::get_pending_federator_public_key_of_type(ctx, args, gas_cost), - "getProposedFederationAddress" => getters::get_proposed_federation_address(ctx, gas_cost, config), + "getProposedFederationAddress" => { + getters::get_proposed_federation_address(ctx, gas_cost, config, hardfork_cfg) + } "getProposedFederationSize" => getters::get_proposed_federation_size(ctx, gas_cost), "getProposedFederationCreationTime" => getters::get_proposed_federation_creation_time(ctx, gas_cost), "getProposedFederationCreationBlockNumber" => getters::get_proposed_federation_creation_block_number(ctx, gas_cost), diff --git a/crates/execution/src/bridge/peg.rs b/crates/execution/src/bridge/peg.rs index d659c4f..108c273 100644 --- a/crates/execution/src/bridge/peg.rs +++ b/crates/execution/src/bridge/peg.rs @@ -4338,13 +4338,18 @@ pub(crate) fn active_federation_keys_and_redeem( // federation is built in the format its constants name. (None, _) => return { let keys = genesis_federation_keys(config); - let redeem = - federation_redeem_for_format(&keys, config.genesis_federation_format, config); + let redeem = federation_redeem_for_format( + &keys, + config.genesis_federation_format, + config, + hardfork_cfg, + block_number, + ); (keys, redeem) }, }; let version = federation_format_version(ctx, format_key); - let redeem = federation_redeem_for_format(&keys, version, config); + let redeem = federation_redeem_for_format(&keys, version, config, hardfork_cfg, block_number); (keys, redeem) } @@ -4393,7 +4398,7 @@ pub(crate) fn retiring_federation_keys_and_redeem( ) -> Option<(Vec<[u8; 33]>, Vec)> { let keys = retiring_federation_keys(ctx, config, hardfork_cfg, block_number)?; let version = federation_format_version(ctx, super::storage::OLD_FEDERATION_FORMAT_VERSION_KEY); - let redeem = federation_redeem_for_format(&keys, version, config); + let redeem = federation_redeem_for_format(&keys, version, config, hardfork_cfg, block_number); Some((keys, redeem)) } @@ -4437,8 +4442,10 @@ pub(crate) fn federation_redeem_for_format_pub( keys: &[[u8; 33]], format_version: u64, config: &BridgeConstants, + hardfork_cfg: &RskHardforkConfig, + block_number: u64, ) -> Vec { - federation_redeem_for_format(keys, format_version, config) + federation_redeem_for_format(keys, format_version, config, hardfork_cfg, block_number) } /// rskj `BridgeUtils.isFromFederateMember(rskTx, federation)`: true when the @@ -5253,29 +5260,99 @@ fn signed_long_to_byte_array_le(value: i64) -> Vec { be } -/// rskj `NonStandardErpRedeemScriptBuilder` (post-RSKIP284/293) and +/// Which of rskj's ERP redeem script builders a federation's script comes +/// from. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +enum ErpScript { + /// `NonStandardErpRedeemScriptBuilderHardcoded`: testnet's one script, + /// whatever the keys, before RSKIP284. + NonStandardHardcoded, + /// `NonStandardErpRedeemScriptBuilderWithCsvUnsignedBE`: before RSKIP293, + /// the CSV delay as two unsigned big-endian bytes. + NonStandardCsvUnsignedBe, + /// `NonStandardErpRedeemScriptBuilder`. + NonStandard, + /// `P2shErpRedeemScriptBuilder`. + P2sh, +} + +/// rskj `NonStandardErpRedeemScriptBuilderHardcoded`'s +/// `LEGACY_ERP_TESTNET_REDEEM_SCRIPT_BYTES`. +const LEGACY_ERP_TESTNET_REDEEM_SCRIPT: &str = concat!( + "6453", + "210208f40073a9e43b3e9103acec79767a6de9b0409749884e989960fee578012fce", + "210225e892391625854128c5c4ea4340de0c2a70570f33db53426fc9c746597a03f4", + "2102afc230c2d355b1a577682b07bc2646041b5d0177af0f98395a46018da699b6da", + "210344a3c38cd59afcba3edcebe143e025574594b001700dec41e59409bdbd0f2a09", + "21039a060badbeb24bee49eb2063f616c0f0f0765d4ca646b20a88ce828f259fcdb9", + "55670300cd50b27552", + "210216c23b2ea8e4f11c3f9e22711addb1d16a93964796913830856b568cc3ea21d3", + "210275562901dd8faae20de0a4166362a4f82188db77dbed4ca887422ea1ec185f14", + "21034db69f2112f4fb1bb6141bf6e2bd6631f0484d0bd95b16767902c9fe219d4a6f", + "5368ae", +); + +/// rskj `NonStandardErpRedeemScriptBuilderFactory`: the builder of a +/// non-standard ERP federation's redeem script, by network and by the block +/// being run. rskj rebuilds a stored federation with that block's +/// activations, so the same federation's script changes at RSKIP284 and at +/// RSKIP293. +fn non_standard_erp_script( + config: &BridgeConstants, + hardfork_cfg: &RskHardforkConfig, + block_number: u64, +) -> ErpScript { + if matches!(config.btc_network, super::constants::BtcNetwork::Testnet) + && !hardfork_cfg.has_rskip284(block_number) + { + ErpScript::NonStandardHardcoded + } else if !hardfork_cfg.has_rskip293(block_number) { + ErpScript::NonStandardCsvUnsignedBe + } else { + ErpScript::NonStandard + } +} + +/// bitcoinj `Utils.unsignedLongToByteArrayBE`: `number` in exactly +/// `num_bytes` big-endian bytes. bitcoinj refuses a number that doesn't fit, +/// which a network's CSV delay never is. +fn unsigned_long_to_byte_array_be(number: i64, num_bytes: usize) -> Vec { + (number as u64).to_be_bytes()[8 - num_bytes..].to_vec() +} + +/// rskj's non-standard ERP redeem script builders (see [`ErpScript`]) and /// `P2shErpRedeemScriptBuilder` (post-RSKIP353) emergency-multisig template. /// -/// Both wrap a default N-of-M multisig and an emergency K-of-J multisig in a -/// CSV-gated `OP_NOTIF ... OP_ELSE OP_CHECKSEQUENCEVERIFY OP_DROP ... -/// OP_ENDIF`. The non-standard form strips the trailing `OP_CHECKMULTISIG` -/// from each inner script and appends a single `OP_CHECKMULTISIG` after -/// `OP_ENDIF`; the P2SH-ERP form keeps each inner script intact (incl. its -/// `OP_CHECKMULTISIG`) and emits nothing after `OP_ENDIF`. Inner scripts are -/// standard multisig with keys sorted lexicographically (see -/// [`build_federation_redeem_script`]). The CSV delay is pushed via -/// [`signed_long_to_byte_array_le`]. +/// All but the hard-coded one wrap a default N-of-M multisig and an emergency +/// K-of-J multisig in a CSV-gated `OP_NOTIF ... OP_ELSE +/// OP_CHECKSEQUENCEVERIFY OP_DROP ... OP_ENDIF`. The non-standard form strips +/// the trailing `OP_CHECKMULTISIG` from each inner script and appends a single +/// `OP_CHECKMULTISIG` after `OP_ENDIF`; the P2SH-ERP form keeps each inner +/// script intact (incl. its `OP_CHECKMULTISIG`) and emits nothing after +/// `OP_ENDIF`. Inner scripts are standard multisig with keys sorted +/// lexicographically (see [`build_federation_redeem_script`]). The CSV delay +/// is pushed via [`signed_long_to_byte_array_le`], or before RSKIP293 as two +/// unsigned big-endian bytes. fn build_erp_redeem_script( default_keys: &[[u8; 33]], default_threshold: usize, erp_keys: &[[u8; 33]], erp_threshold: usize, csv_delay: i64, - p2sh: bool, + script: ErpScript, ) -> Vec { + if script == ErpScript::NonStandardHardcoded { + return alloy_primitives::hex::decode(LEGACY_ERP_TESTNET_REDEEM_SCRIPT) + .expect("the hard-coded script is hex"); + } + let p2sh = script == ErpScript::P2sh; let default_redeem = build_federation_redeem_script(default_keys, default_threshold); let erp_redeem = build_federation_redeem_script(erp_keys, erp_threshold); - let csv = signed_long_to_byte_array_le(csv_delay); + let csv = if script == ErpScript::NonStandardCsvUnsignedBe { + unsigned_long_to_byte_array_be(csv_delay, 2) + } else { + signed_long_to_byte_array_le(csv_delay) + }; // `removeOpCheckMultisig`: drop the trailing OP_CHECKMULTISIG (last byte). let default_inner: &[u8] = if p2sh { &default_redeem } else { &default_redeem[..default_redeem.len() - 1] }; @@ -5325,8 +5402,12 @@ pub fn build_committed_federation_redeem_script( .collect(); let erp_threshold = erp_keys.len() / 2 + 1; let csv = config.erp_fed_activation_delay; - let p2sh = hardfork_cfg.has_rskip353(block_number); - build_erp_redeem_script(keys, threshold, &erp_keys, erp_threshold, csv, p2sh) + let script = if hardfork_cfg.has_rskip353(block_number) { + ErpScript::P2sh + } else { + non_standard_erp_script(config, hardfork_cfg, block_number) + }; + build_erp_redeem_script(keys, threshold, &erp_keys, erp_threshold, csv, script) } /// Build a live federation's redeem script from its **stored format version**, @@ -5339,11 +5420,16 @@ pub fn build_committed_federation_redeem_script( /// 2000 NON_STANDARD_ERP → non-standard ERP (no trailing OP_CHECKMULTISIG per inner) /// 3000 P2SH_ERP / 4000 P2SH_P2WSH_ERP → P2SH-ERP template /// (The P2SH-P2WSH-ERP federation reuses the P2SH-ERP redeem template — see -/// `FederationFactory.buildP2shP2wshErpFederation`.) +/// `FederationFactory.buildP2shP2wshErpFederation`.) Within format 2000 the +/// builder follows the block being run (`block_number`), as rskj rebuilds a +/// stored federation with that block's activations: see +/// [`non_standard_erp_script`]. fn federation_redeem_for_format( keys: &[[u8; 33]], format_version: u64, config: &BridgeConstants, + hardfork_cfg: &RskHardforkConfig, + block_number: u64, ) -> Vec { let threshold = keys.len() / 2 + 1; if format_version <= 1000 { @@ -5359,9 +5445,13 @@ fn federation_redeem_for_format( .collect(); let erp_threshold = erp_keys.len() / 2 + 1; let csv = config.erp_fed_activation_delay; - // 2000 = non-standard ERP (p2sh=false); 3000/4000 = P2SH-ERP template (p2sh=true). - let p2sh = format_version >= 3000; - build_erp_redeem_script(keys, threshold, &erp_keys, erp_threshold, csv, p2sh) + // 2000 = non-standard ERP; 3000/4000 = the P2SH-ERP template. + let script = if format_version >= 3000 { + ErpScript::P2sh + } else { + non_standard_erp_script(config, hardfork_cfg, block_number) + }; + build_erp_redeem_script(keys, threshold, &erp_keys, erp_threshold, csv, script) } /// Read a federation's stored format version (rskj @@ -6915,6 +7005,103 @@ mod tests { assert_eq!(signed_long_to_byte_array_le(1), vec![0x01]); } + /// bitcoinj `Utils.unsignedLongToByteArrayBE(csv, 2)`, which the + /// non-standard ERP builder pushes before RSKIP293: 52,560 -> `cd 50`. + #[test] + fn erp_csv_delay_unsigned_be_groundtruth() { + assert_eq!(unsigned_long_to_byte_array_be(52_560, 2), vec![0xcd, 0x50]); + assert_eq!(unsigned_long_to_byte_array_be(500, 2), vec![0x01, 0xf4]); + assert_eq!(unsigned_long_to_byte_array_be(20, 2), vec![0x00, 0x14]); + } + + /// rskj `NonStandardErpFederationsTest`'s real values (its + /// `createdRedeemScriptProgramFrom...WithRealValues...` tests): five + /// federators, three emergency keys, CSV 52,560. The hard-coded builder + /// answers its one script whatever the keys; the builder with the unsigned + /// big-endian CSV, and today's builder, answer these programs. + #[test] + fn nonstandard_erp_redeem_scripts_match_rskjs_real_values() { + let emergency = [ + "0216c23b2ea8e4f11c3f9e22711addb1d16a93964796913830856b568cc3ea21d3", + "0275562901dd8faae20de0a4166362a4f82188db77dbed4ca887422ea1ec185f14", + "034db69f2112f4fb1bb6141bf6e2bd6631f0484d0bd95b16767902c9fe219d4a6f", + ] + .map(key); + let script = |last: &str, kind: ErpScript| { + let federators = [ + "0208f40073a9e43b3e9103acec79767a6de9b0409749884e989960fee578012fce", + "0225e892391625854128c5c4ea4340de0c2a70570f33db53426fc9c746597a03f4", + "025a2f522aea776fab5241ad72f7f05918e8606676461cb6ce38265a52d4ca9ed6", + "02afc230c2d355b1a577682b07bc2646041b5d0177af0f98395a46018da699b6da", + last, + ] + .map(key); + alloy_primitives::hex::encode(build_erp_redeem_script( + &federators, + 3, + &emergency, + 2, + 52_560, + kind, + )) + }; + assert_eq!( + script( + "039a060badbeb24bee49eb2063f616c0f0f0765d4ca646b20a88ce828f259fcdb9", + ErpScript::NonStandardHardcoded + ), + "6453210208f40073a9e43b3e9103acec79767a6de9b0409749884e989960fee578012fce210225e892391625854128c5c4ea4340de0c2a70570f33db53426fc9c746597a03f42102afc230c2d355b1a577682b07bc2646041b5d0177af0f98395a46018da699b6da210344a3c38cd59afcba3edcebe143e025574594b001700dec41e59409bdbd0f2a0921039a060badbeb24bee49eb2063f616c0f0f0765d4ca646b20a88ce828f259fcdb955670300cd50b27552210216c23b2ea8e4f11c3f9e22711addb1d16a93964796913830856b568cc3ea21d3210275562901dd8faae20de0a4166362a4f82188db77dbed4ca887422ea1ec185f1421034db69f2112f4fb1bb6141bf6e2bd6631f0484d0bd95b16767902c9fe219d4a6f5368ae" + ); + assert_eq!( + script( + "0344a3c38cd59afcba3edcebe143e025574594b001700dec41e59409bdbd0f2a09", + ErpScript::NonStandardCsvUnsignedBe + ), + "6453210208f40073a9e43b3e9103acec79767a6de9b0409749884e989960fee578012fce210225e892391625854128c5c4ea4340de0c2a70570f33db53426fc9c746597a03f421025a2f522aea776fab5241ad72f7f05918e8606676461cb6ce38265a52d4ca9ed62102afc230c2d355b1a577682b07bc2646041b5d0177af0f98395a46018da699b6da210344a3c38cd59afcba3edcebe143e025574594b001700dec41e59409bdbd0f2a09556702cd50b27552210216c23b2ea8e4f11c3f9e22711addb1d16a93964796913830856b568cc3ea21d3210275562901dd8faae20de0a4166362a4f82188db77dbed4ca887422ea1ec185f1421034db69f2112f4fb1bb6141bf6e2bd6631f0484d0bd95b16767902c9fe219d4a6f5368ae" + ); + assert_eq!( + script( + "03fb8e1d5d0392d35ca8c3656acb6193dbf392b3e89b9b7b86693f5c80f7ce8581", + ErpScript::NonStandard + ), + "6453210208f40073a9e43b3e9103acec79767a6de9b0409749884e989960fee578012fce210225e892391625854128c5c4ea4340de0c2a70570f33db53426fc9c746597a03f421025a2f522aea776fab5241ad72f7f05918e8606676461cb6ce38265a52d4ca9ed62102afc230c2d355b1a577682b07bc2646041b5d0177af0f98395a46018da699b6da2103fb8e1d5d0392d35ca8c3656acb6193dbf392b3e89b9b7b86693f5c80f7ce858155670350cd00b27552210216c23b2ea8e4f11c3f9e22711addb1d16a93964796913830856b568cc3ea21d3210275562901dd8faae20de0a4166362a4f82188db77dbed4ca887422ea1ec185f1421034db69f2112f4fb1bb6141bf6e2bd6631f0484d0bd95b16767902c9fe219d4a6f5368ae" + ); + } + + /// rskj `NonStandardErpRedeemScriptBuilderFactory`, by network and block: + /// on testnet the hard-coded script before RSKIP284, the unsigned + /// big-endian CSV before RSKIP293, today's builder after; elsewhere the + /// unsigned CSV before RSKIP293. A stored federation of format 2000 is + /// rebuilt by the block being run, not its creation block. + #[test] + fn the_nonstandard_erp_builder_is_picked_as_rskjs_factory_picks_it() { + let (testnet, testnet_cfg) = (BridgeConstants::testnet(), RskHardforkConfig::testnet()); + let (mainnet, mainnet_cfg) = (BridgeConstants::mainnet(), RskHardforkConfig::mainnet()); + let on_testnet = |n| non_standard_erp_script(&testnet, &testnet_cfg, n); + let on_mainnet = |n| non_standard_erp_script(&mainnet, &mainnet_cfg, n); + assert_eq!(on_testnet(2_581_799), ErpScript::NonStandardHardcoded); + assert_eq!(on_testnet(2_581_800), ErpScript::NonStandardCsvUnsignedBe); + assert_eq!(on_testnet(3_102_999), ErpScript::NonStandardCsvUnsignedBe); + assert_eq!(on_testnet(3_103_000), ErpScript::NonStandard); + assert_eq!(on_mainnet(0), ErpScript::NonStandardCsvUnsignedBe); + assert_eq!(on_mainnet(4_598_499), ErpScript::NonStandardCsvUnsignedBe); + assert_eq!(on_mainnet(4_598_500), ErpScript::NonStandard); + + let keys = test_keys(0x10); + let at = |n| federation_redeem_for_format(&keys, 2000, &testnet, &testnet_cfg, n); + assert_eq!( + at(2_581_799), + build_erp_redeem_script(&[], 0, &[], 0, 0, ErpScript::NonStandardHardcoded) + ); + let (unsigned_csv, today) = (at(2_581_800), at(3_103_000)); + assert_eq!( + unsigned_csv.len() + 1, + today.len(), + "two CSV bytes, not three" + ); + assert_ne!(unsigned_csv, today); + } + /// Ground truth: the first real RSK mainnet federation change committed at /// block #4,652,781 produced a NON-STANDARD ERP federation (RSKIP201 active, /// RSKIP353 not). Its base58 P2SH address appears in the block's @@ -6941,7 +7128,8 @@ mod tests { .map(|h| key(h)) .collect::>(); // NonStandard ERP (p2sh = false): default 5-of-9, emergency 3-of-4. - let redeem = build_erp_redeem_script(&new_keys, 5, &erp_keys, 3, 52560, false); + let redeem = + build_erp_redeem_script(&new_keys, 5, &erp_keys, 3, 52560, ErpScript::NonStandard); assert_eq!(p2sh_base58_mainnet(&redeem), "3DsneJha6CY6X9gU2M9uEc4nSdbYECB4Gh"); } @@ -6969,7 +7157,8 @@ mod tests { .iter() .map(|h| key(h)) .collect::>(); - let redeem = build_erp_redeem_script(&default_keys, 5, &erp_keys, 3, 52560, true); + let redeem = + build_erp_redeem_script(&default_keys, 5, &erp_keys, 3, 52560, ErpScript::P2sh); assert_eq!( alloy_primitives::hex::encode(p2sh_output_script(&redeem_script_hash160(&redeem)).as_bytes()), "a9142c1bab6ea51fdaf85c8366bd2b1502eaa69b6ae687" @@ -7001,9 +7190,13 @@ mod tests { ] .map(key); let config = BridgeConstants::mainnet(); + // Where that federation was committed, past RSKIP293. + let (cfg, block) = (RskHardforkConfig::mainnet(), 4_652_781); + let for_format = + |format| federation_redeem_for_format(&active_keys, format, &config, &cfg, block); // format 2000 (NON_STANDARD_ERP) → the active fed's real mainnet P2SH. - let r2000 = federation_redeem_for_format(&active_keys, 2000, &config); + let r2000 = for_format(2000); assert_eq!( p2sh_base58_mainnet(&r2000), "3DsneJha6CY6X9gU2M9uEc4nSdbYECB4Gh", @@ -7011,7 +7204,7 @@ mod tests { ); // format 1000 (STANDARD_MULTISIG) → plain N-of-M, a DIFFERENT address. - let r1000 = federation_redeem_for_format(&active_keys, 1000, &config); + let r1000 = for_format(1000); assert_eq!( r1000, build_federation_redeem_script(&active_keys, active_keys.len() / 2 + 1), @@ -7025,7 +7218,7 @@ mod tests { // format 3000 (P2SH_ERP) → the P2SH-ERP template (p2sh=true), distinct // from the non-standard ERP form. - let r3000 = federation_redeem_for_format(&active_keys, 3000, &config); + let r3000 = for_format(3000); assert_ne!(r3000, r2000, "P2SH-ERP and non-standard ERP differ"); } @@ -8322,7 +8515,13 @@ mod tests { .map(key); let config = BridgeConstants::mainnet(); let plain = build_federation_redeem_script(&keys, keys.len() / 2 + 1); - let erp = federation_redeem_for_format(&keys, 2000, &config); + let erp = federation_redeem_for_format( + &keys, + 2000, + &config, + &RskHardforkConfig::mainnet(), + 4_652_781, + ); assert_ne!( plain, erp, "an ERP retiring federation must not migrate with the plain multisig redeem" @@ -8644,7 +8843,7 @@ mod tests { use super::super::release_tx::{is_erp_redeem, placeholder_scriptsig, segwit_base_input}; let keys = test_keys(0x10); let standard = build_federation_redeem_script(&keys, 2); - let erp = build_erp_redeem_script(&keys, 2, &test_keys(0x40), 2, 52_560, true); + let erp = build_erp_redeem_script(&keys, 2, &test_keys(0x40), 2, 52_560, ErpScript::P2sh); let outputs = [(40_000, p2sh_output_script(&[0x77; 20]))]; for redeem in [&standard, &erp] { let unsigned = spend(vec![legacy(placeholder_scriptsig(redeem, 2)); 2], &outputs); @@ -9134,7 +9333,7 @@ mod tests { fn below_the_index_a_spend_of_the_last_retired_federation_is_no_peg_in() { let keys = test_keys(0x10); let standard = build_federation_redeem_script(&keys, 2); - let erp = build_erp_redeem_script(&keys, 2, &test_keys(0x40), 2, 52_560, true); + let erp = build_erp_redeem_script(&keys, 2, &test_keys(0x40), 2, 52_560, ErpScript::P2sh); let with_retired = |retired: &[u8; 20], tx: &BtcTransaction| { let mut ctx = regtest_bridge_with(tx); let script = p2sh_output_script(retired).into_bytes(); From 80ab5f667ff07b4e2c6e8b3bca992e22966c4ffc Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Tue, 10 Nov 2026 10:32:28 -0800 Subject: [PATCH 097/264] bridge: testnet's block 5,148,285 finds RSKj's hard-coded Bitcoin block At testnet's block #5,148,285, RSKj's getBlockKeepingTestnetConsensus answers Bitcoin block 00000000e8e7...f379 with a hard-coded stored block at height 2,817,125, whatever the store holds: the block had reached RSKj's cache but not its storage, and nodes disagreed on it. getBtcTransactionConfirmations and its gas cost both find the block through it, and now do here too. The test ports RSKj's BuildBlockThatKeepsTestnetConsensusTests. --- crates/execution/src/bridge/btc_store.rs | 32 +++++++++ crates/execution/src/bridge/mod.rs | 91 +++++++++++++++++++++++- crates/execution/src/bridge/tx.rs | 8 +-- 3 files changed, 125 insertions(+), 6 deletions(-) diff --git a/crates/execution/src/bridge/btc_store.rs b/crates/execution/src/bridge/btc_store.rs index c8dfc56..b348223 100644 --- a/crates/execution/src/bridge/btc_store.rs +++ b/crates/execution/src/bridge/btc_store.rs @@ -253,6 +253,38 @@ pub fn get_stored_block(ctx: &mut CTX, hash: &BlockHas Some(block) } +/// rskj `BridgeSupport.getBlockKeepingTestnetConsensus`, the lookup +/// `getBtcTransactionConfirmations` and its gas cost make: the stored block +/// `hash` names, except at testnet's block #5,148,285, where Bitcoin block +/// `00000000e8e7...f379` is the block rskj hard-codes, at height 2,817,125, +/// whatever the store holds. rskj's cache held that block when its storage +/// didn't, so nodes answered differently, and rskj fixed the answer. +pub fn get_block_keeping_testnet_consensus( + ctx: &mut CTX, + config: &super::constants::BridgeConstants, + hash: &BlockHash, +) -> Option { + const RSK_BLOCK: u64 = 5_148_285; + const BTC_BLOCK: &str = "00000000e8e7b540df01a7067e020fd7e2026bf86289def2283a35120c1af379"; + const HEADER: &str = "000000203b5d178405c4e6e7dc07d63d6de5db1342044791721654760c00000000000000796cf6743a036300b43fb3abe6703d04a7999751b6d5744f20327d1175320bd37b954e66ffff001d56dc11ce"; + const CHAIN_WORK: &str = "000000000000000000000000000000000000000000000ddeb5fbcd969312a77c"; + let block_number = revm::context_interface::Block::number(ctx.block()).to::(); + if block_number == RSK_BLOCK + && matches!(config.btc_network, super::constants::BtcNetwork::Testnet) + && hash.to_string() == BTC_BLOCK + { + let header = alloy_primitives::hex::decode(HEADER).expect("the header is hex"); + let header = deserialize::(&header).expect("the header is 80 bytes"); + let work = alloy_primitives::hex::decode(CHAIN_WORK).expect("the chain work is hex"); + return Some(StoredBlock::new( + header, + 2_817_125, + U256::from_be_slice(&work), + )); + } + get_stored_block(ctx, hash) +} + // --------------------------------------------------------------------------- // Raw byte storage: one variable-length value per unitrie key // (rskj Repository.addStorageBytes / getStorageBytes). diff --git a/crates/execution/src/bridge/mod.rs b/crates/execution/src/bridge/mod.rs index e73e00a..6f65af3 100644 --- a/crates/execution/src/bridge/mod.rs +++ b/crates/execution/src/bridge/mod.rs @@ -762,7 +762,8 @@ fn receive_headers_cost(args: &[u8], hardfork_cfg: &RskHardforkConfig, block_num /// rskj `BridgeSupport.getBtcTransactionConfirmationsGetCost`: /// 27_000 base + blockDepth * 315 + merkleBranchHashes * 144, falling back to -/// the base cost when the block is unknown or too deep. +/// the base cost when the block is unknown or too deep. It finds the block +/// as the call does (`getBlockKeepingTestnetConsensus`). fn btc_tx_confirmations_cost( ctx: &mut CTX, args: &[u8], @@ -788,7 +789,9 @@ fn btc_tx_confirmations_cost( // contract's call that runs out of gas drops it. crate::bridge::btc_chain::ensure_btc_block_store(ctx, config, hardfork_cfg); let block_hash = crate::bridge::btc_chain::b256_to_bitcoin_hash(&block_hash); - let Some(block) = crate::bridge::btc_store::get_stored_block(ctx, &block_hash) else { + let Some(block) = + crate::bridge::btc_store::get_block_keeping_testnet_consensus(ctx, config, &block_hash) + else { return BASIC_COST; }; let Some(head) = crate::bridge::btc_store::load_chain_head(ctx) else { @@ -1431,4 +1434,88 @@ mod tests { 27_000 + 2 * 144 ); } + + /// rskj `BuildBlockThatKeepsTestnetConsensusTests`: at testnet's block + /// #5,148,285, `getBtcTransactionConfirmations` and its cost find Bitcoin + /// block `00000000e8e7...f379`, which the store doesn't hold, as rskj's + /// hard-coded block at 2,817,125. Under a head at 2,817,200 the cost + /// charges its depth. On a fresh store, whose head is below it, the call + /// can't look up the main chain at its height and answers -3. At any other + /// block, or on mainnet, the block isn't found. + #[test] + fn the_confirmations_keep_testnet_consensus_at_5_148_285() { + use crate::abi::encode::{self, Arg}; + use revm::MainContext; + let hard_coded: bitcoin::BlockHash = + "00000000e8e7b540df01a7067e020fd7e2026bf86289def2283a35120c1af379" + .parse() + .unwrap(); + let args = encode::args(&[ + Arg::Word([0x11; 32]), + Arg::Word(btc_chain::bitcoin_hash_to_b256(&hard_coded).0), + Arg::Word(encode::int(0)), + Arg::Words(&[]), + ]); + let (testnet, mainnet) = (BridgeConstants::testnet(), BridgeConstants::mainnet()); + let cfg = RskHardforkConfig::testnet(); + let ctx_at = |number: u64| { + let mut ctx = + revm::Context::mainnet().with_chain(crate::raw_storage::RskChainExt::default()); + ctx.block.number = alloy_primitives::U256::from(number); + ctx + }; + + // rskj's header is the block it names. + let block = btc_store::get_block_keeping_testnet_consensus( + &mut ctx_at(5_148_285), + &testnet, + &hard_coded, + ) + .expect("the hard-coded block"); + assert_eq!((block.block_hash(), block.height), (hard_coded, 2_817_125)); + for (number, config) in [ + (5_148_284, &testnet), + (5_148_286, &testnet), + (5_148_285, &mainnet), + ] { + let mut ctx = ctx_at(number); + assert!( + btc_store::get_block_keeping_testnet_consensus(&mut ctx, config, &hard_coded) + .is_none() + ); + } + + // The cost, under rskj's test's head: Bitcoin testnet's block 2,817,200. + let head = btc_store::StoredBlock::new( + bitcoin::consensus::deserialize( + &alloy_primitives::hex::decode("0000642d6b8df2c8ae4a20e20a10ae3b34e485701b8ae7e80ede0828040000000000000063a17c3e5063da2f8f952e496beee729932d35f62c0765baaf12f32931f01c9b49354f66ecd410190f25f402").unwrap(), + ) + .unwrap(), + 2_817_200, + alloy_primitives::U256::from(1), + ); + let cost_at = |number: u64| { + let mut ctx = ctx_at(number); + btc_store::store_chain_head(&mut ctx, &head, cfg.has_stored_block_v2(number)); + btc_tx_confirmations_cost(&mut ctx, &args, &testnet, &cfg) + }; + assert_eq!(cost_at(5_148_285), 27_000 + (2_817_200 - 2_817_125) * 315); + assert_eq!(cost_at(5_148_286), 27_000); + + // The call, on a fresh store. + let answer_at = |number: u64| { + let out = tx::get_btc_transaction_confirmations( + &mut ctx_at(number), + &args, + 0, + &testnet, + cfg.has_stored_block_v2(number), + &cfg, + ) + .unwrap(); + i64::from_be_bytes(out.bytes[24..32].try_into().unwrap()) + }; + assert_eq!(answer_at(5_148_285), -3); + assert_eq!(answer_at(5_148_286), -1); + } } diff --git a/crates/execution/src/bridge/tx.rs b/crates/execution/src/bridge/tx.rs index b0573d1..897f5d0 100644 --- a/crates/execution/src/bridge/tx.rs +++ b/crates/execution/src/bridge/tx.rs @@ -24,7 +24,7 @@ pub fn btc_hash_hex_display(hash: &[u8; 32]) -> String { bytes.reverse(); to_hex(&bytes) } -use super::btc_store::{get_stored_block, load_chain_head}; +use super::btc_store::{get_block_keeping_testnet_consensus, get_stored_block, load_chain_head}; use super::pmt::{MerkleBranch, PartialMerkleTree}; use super::serialization::{rlp_decode_list, rlp_encode_element, rlp_encode_list}; use super::storage::*; @@ -352,11 +352,11 @@ pub fn get_btc_transaction_confirmations( let rskip199 = hardfork_cfg.has_rskip199(block_number); super::btc_chain::ensure_btc_chain_seeded(ctx, config, use_v2, rskip199); - // 1. Block must exist. (rskj `getBlockKeepingTestnetConsensus`; the - // hardcoded testnet block it special-cases is not a mainnet concern.) + // 1. Block must exist (rskj `getBlockKeepingTestnetConsensus`, which at + // testnet's block #5,148,285 answers a hard-coded block). let block_hash = b256_to_bitcoin_hash(&alloy_primitives::B256::from(block_hash_bytes)); - let block = match get_stored_block(ctx, &block_hash) { + let block = match get_block_keeping_testnet_consensus(ctx, config, &block_hash) { Some(b) => b, None => { return Ok(encode_int_output(gas_cost, CONFIRMATION_ERR_INEXISTENT_BLOCK_HASH)) From 9800ddd9b2796276c160ba7df92d0a1812ed46cd Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Tue, 10 Nov 2026 12:09:11 -0800 Subject: [PATCH 098/264] Execution: the accounts a block deleted and created, as rskj tracks them rskj deletes a destroyed account as the transaction that destroyed it ends, and hands the block's deletions so far to every later program (`deletedAccounts`). rustock keeps one journal for the whole block and missed four rules that follow from that: - RSKIP-131: from RSKIP125 a creation at an address an earlier transaction of the block deleted fails as a creation at an address in use does (`Program.createContract`): the creator's nonce goes up, the gas given to the creation is spent and 0 is pushed. A sequential sublist starts with what the parallel ones deleted (`BlockExecutor.executeParallel`). The test that had such a creation succeed is replaced. - An account destroyed earlier in the block doesn't exist until something touches it: EXTCODEHASH answers 0 and a CALL to it pays NEW_ACCT_CALL. - Before RSKIP169 (iris300) EXTCODEHASH answers 0 for a contract whose code key holds nothing (`getCodeHashNonStandard`, audit finding FRCR-279), a contract being an account with the marker `setupContract` writes. RSKIP169 joins the schedule. - A contract created with no code keeps that marker though a later transaction of the block loads it again. Also corrects the RSKIP-144 note on which creations write the marker. --- crates/execution/src/collision.rs | 11 +- crates/execution/src/database.rs | 9 +- crates/execution/src/executor.rs | 408 +++++++++++++++++++++-- crates/execution/src/hardfork.rs | 8 + crates/execution/src/raw_storage.rs | 10 + crates/execution/src/rsk_handler.rs | 123 +++++-- crates/execution/src/rsk_instructions.rs | 103 +++++- crates/execution/src/state.rs | 17 +- 8 files changed, 601 insertions(+), 88 deletions(-) diff --git a/crates/execution/src/collision.rs b/crates/execution/src/collision.rs index f04b69a..14857f6 100644 --- a/crates/execution/src/collision.rs +++ b/crates/execution/src/collision.rs @@ -559,11 +559,12 @@ pub fn record_transaction(transaction_id: usize, state: &EvmState) { // A contract deployed here writes its code cell and the // storage-prefix marker (rskj `setupContract`). Both are plain - // unitrie cells under the same account. Gated on there being - // code, which is what `apply_state_changes` gates the same two - // writes on: a CREATE that returns nothing gets neither cell in - // rskj either (`TransactionExecutor.create`: "without code nor - // storage. It doesn't even call setupContract()"). + // unitrie cells under the same account. rskj writes the marker + // for every creation but a transaction's with no data + // (`TransactionExecutor.create`: "It doesn't even call + // setupContract()"), whatever code the creation returns. Noting + // the two only where it returned code loses no collision: a + // creation writes the account's own cell too, noted above. if account.is_created_locally() && account.info.code.as_ref().is_some_and(|c| !c.is_empty()) { diff --git a/crates/execution/src/database.rs b/crates/execution/src/database.rs index 1d7a486..f93cabd 100644 --- a/crates/execution/src/database.rs +++ b/crates/execution/src/database.rs @@ -10,7 +10,7 @@ use revm::bytecode::Bytecode; use revm::state::AccountInfo; use rustock_trie::{ TrieNode, TrieStore, TrieKeySlice, - account_key, code_key, storage_key, + account_key, code_key, storage_key, storage_prefix_key, AccountState, }; use rustock_storage::BlockStore; @@ -61,6 +61,13 @@ impl RskDatabase { Self { root, store, block_store, code_cache: RefCell::new(HashMap::new()) } } + /// rskj `MutableRepository.isContract`: whether `address` holds the + /// storage-prefix marker `setupContract` writes, in the state this + /// database reads. + pub fn has_contract_marker(&self, address: &Address) -> Result { + Ok(self.trie_get(&storage_prefix_key(address))?.is_some()) + } + /// Reads one key, distinguishing "not in the trie" from "the trie is /// incomplete here". /// diff --git a/crates/execution/src/executor.rs b/crates/execution/src/executor.rs index 63ac8e1..7996307 100644 --- a/crates/execution/src/executor.rs +++ b/crates/execution/src/executor.rs @@ -149,6 +149,12 @@ fn merge_markers(into: &mut crate::state::ContractMarkers, from: crate::state::C } into.bump_remasc_sender |= from.bump_remasc_sender; into.raw_storage.extend(from.raw_storage); + into.created.extend(from.created); + for addr in from.deleted { + if !into.deleted.contains(&addr) { + into.deleted.push(addr); + } + } into.null_account = match (into.null_account, from.null_account) { (Some(paid), Some(more)) => Some(paid + more), (paid, more) => paid.or(more), @@ -429,6 +435,7 @@ impl RskExecutor { crate::rsk_instructions::install( &mut evm.instruction, self.hardfork_cfg.has_rskip140(header.number), + self.hardfork_cfg.has_rskip169(header.number), self.hardfork_cfg.has_chainid(header.number), self.hardfork_cfg.has_push0(header.number), self.hardfork_cfg.has_basefee(header.number), @@ -568,6 +575,7 @@ impl RskExecutor { crate::rsk_instructions::install( &mut evm.instruction, self.hardfork_cfg.has_rskip140(header.number), + self.hardfork_cfg.has_rskip169(header.number), self.hardfork_cfg.has_chainid(header.number), self.hardfork_cfg.has_push0(header.number), self.hardfork_cfg.has_basefee(header.number), @@ -749,6 +757,7 @@ impl RskExecutor { crate::rsk_instructions::install( &mut evm.instruction, self.hardfork_cfg.has_rskip140(header.number), + self.hardfork_cfg.has_rskip169(header.number), self.hardfork_cfg.has_chainid(header.number), self.hardfork_cfg.has_push0(header.number), self.hardfork_cfg.has_basefee(header.number), @@ -916,6 +925,7 @@ impl RskExecutor { crate::tracer::RskTracer::new(0), TracedTxs::None, U256::ZERO, + &[], true, Some(&mut dropped), )?; @@ -1059,6 +1069,7 @@ impl RskExecutor { inspector, traced, U256::ZERO, + &[], true, None, ); @@ -1118,6 +1129,7 @@ impl RskExecutor { inspector.fresh_like(), TracedTxs::None, U256::ZERO, + &[], true, None, )?; @@ -1186,6 +1198,7 @@ impl RskExecutor { template.fresh_like(), traced, U256::ZERO, + &[], false, None, ); @@ -1256,6 +1269,12 @@ impl RskExecutor { .iter() .map(|(r, _)| r.paid_fees) .fold(U256::ZERO, |a, b| a + b); + // What the parallel sublists deleted, where RSKIP-131 refuses a + // creation in the sequential one (rskj `executeParallel`). + let deleted_before: Vec
= runs + .iter() + .flat_map(|(r, _)| r.markers.deleted.iter().copied()) + .collect(); let at_sequential = std::time::Instant::now(); let (sequential_result, mut traces) = self.run_sublist( header, @@ -1266,6 +1285,7 @@ impl RskExecutor { inspector, traced, fees_carried, + &deleted_before, false, None, )?; @@ -1344,6 +1364,10 @@ impl RskExecutor { // `totalPaidFees` into the sequential executor the same way, so that // the settlement at the REMASC transaction covers the whole block. fees_carried: U256, + // `deleted_before`: what the sublists run before this one deleted. + // rskj hands the parallel sublists' `deletedAccounts` to the + // sequential one (`BlockExecutor.executeParallel`), for RSKIP-131. + deleted_before: &[Address], // `run_maintenance`: run the block-level precompile maintenance // first. True for the run that starts the block and false for every // later sublist -- rskj's `maintainPrecompiledContractStorageRoots` @@ -1367,6 +1391,7 @@ impl RskExecutor { let mut chain_ext = crate::raw_storage::RskChainExt::default(); chain_ext.raw_storage.set_reader(trie_store, state_root.clone()); chain_ext.btc_block_cache = self.btc_block_cache.clone(); + chain_ext.deleted_in_block.extend(deleted_before.iter().copied()); let ctx = revm::Context::mainnet() .with_db(WrapDatabaseRef(db)) .with_block(block_env) @@ -1412,6 +1437,7 @@ impl RskExecutor { crate::rsk_instructions::install( &mut evm.instruction, self.hardfork_cfg.has_rskip140(header.number), + self.hardfork_cfg.has_rskip169(header.number), self.hardfork_cfg.has_chainid(header.number), self.hardfork_cfg.has_push0(header.number), self.hardfork_cfg.has_basefee(header.number), @@ -2239,14 +2265,27 @@ impl RskExecutor { // the touched flag. A later tx that touches the address re-marks // Touched, which apply_state_changes reads as "alive again at // block end" (mainnet #3173807). + // + // Until then it does not exist (rskj `isExist`): loaded as not + // existing, like an account the trie never had. And the block + // keeps rskj's `deletedAccounts`, for RSKIP-131, and the contracts + // its transactions created and left standing, which hold rskj's + // marker whatever code they ended with. { - use revm::context_interface::ContextTr; - for account in evm.ctx.journal_mut().inner.state.values_mut() { + let skipped = &markers.skip_created; + let ctx = &mut evm.ctx; + let block = &mut ctx.chain; + for (address, account) in ctx.journaled_state.inner.state.iter_mut() { if account.is_selfdestructed_locally() { account.selfdestruct(); account.unmark_selfdestructed_locally(); account.unmark_created_locally(); account.unmark_touch(); + account.status |= revm::state::AccountStatus::LoadedAsNotExisting; + block.deleted_in_block.insert(*address); + block.created_in_block.remove(address); + } else if account.is_created_locally() && !skipped.contains(address) { + block.created_in_block.insert(*address); } } } @@ -2279,6 +2318,11 @@ impl RskExecutor { { use revm::context_interface::ContextTr; markers.raw_storage = evm.ctx.chain_mut().raw_storage.drain(); + let block = &evm.ctx.chain; + markers.created = block.created_in_block.iter().copied().collect(); + markers.created.sort_unstable(); + markers.deleted = block.deleted_in_block.iter().copied().collect(); + markers.deleted.sort_unstable(); } let state = evm.finalize(); @@ -13779,12 +13823,16 @@ bf09f6e52420834e8e0e0b1a6df563aba550cf7f99e9724264187c45dcf3d73e8585a0e35196\ assert_eq!(sender_acct.balance, one_rbtc, "burned balance goes nowhere"); } - /// Destroy → CREATE2 re-deploy at the same address → destroy again, all - /// in one block (the rskj TransactionExecutor comment: "the remote case - /// there is a CREATE2 creating a deleted account"). The final destruction - /// must win: nothing of the address survives at block end. + /// RSKIP-131: from RSKIP125 rskj's `Program.createContract` refuses a + /// creation at an address an earlier transaction of the block deleted: + /// the `deletedAccounts` that `TransactionExecutor` hands every program + /// "in the remote case there is a CREATE2 creating a deleted account". + /// Destroy, then CREATE2 at the same address in a later transaction: the + /// factory's nonce still goes up, the gas given to the creation is spent, + /// and the address stays deleted, so a transfer to it makes a plain + /// account. #[test] - fn selfdestruct_recreate_via_create2_then_selfdestruct_again() { + fn create2_at_an_address_destroyed_earlier_in_the_block_is_refused() { let store = Arc::new(MemoryTrieStore::new()); let sender = Address::repeat_byte(0xAA); let factory = Address::repeat_byte(0xFA); @@ -13796,11 +13844,11 @@ bf09f6e52420834e8e0e0b1a6df563aba550cf7f99e9724264187c45dcf3d73e8585a0e35196\ let init: [u8; 11] = [0x61, 0x33, 0xFF, 0x60, 0x00, 0x52, 0x60, 0x02, 0x60, 0x1E, 0xF3]; // Factory runtime: PUSH11 init; PUSH1 0; MSTORE (init at mem[21..32]) // PUSH1 0 (salt); PUSH1 11 (size); PUSH1 21 (offset); - // PUSH1 0 (value); CREATE2; POP; STOP + // PUSH1 0 (value); CREATE2; STOP let mut factory_code = vec![0x6A]; factory_code.extend_from_slice(&init); factory_code.extend_from_slice(&[ - 0x60, 0x00, 0x52, 0x60, 0x00, 0x60, 0x0B, 0x60, 0x15, 0x60, 0x00, 0xF5, 0x50, 0x00, + 0x60, 0x00, 0x52, 0x60, 0x00, 0x60, 0x0B, 0x60, 0x15, 0x60, 0x00, 0xF5, 0x00, ]); let victim = factory.create2(B256::ZERO, alloy_primitives::keccak256(init)); @@ -13818,43 +13866,333 @@ bf09f6e52420834e8e0e0b1a6df563aba550cf7f99e9724264187c45dcf3d73e8585a0e35196\ let txs = vec![ (plain_tx(0, &factory, U256::ZERO), sender), // deploy victim (plain_tx(1, &victim, U256::ZERO), sender), // destroy it - (plain_tx(2, &factory, U256::ZERO), sender), // re-deploy at same addr - (plain_tx(3, &victim, U256::from(7)), sender), // destroy again + (plain_tx(2, &factory, U256::ZERO), sender), // CREATE2 at the same address + (plain_tx(3, &victim, U256::from(7)), sender), // a transfer to it ]; let r = executor .execute_block(&dummy_header(3_173_807), &txs, &root, store.clone()) .expect("block"); assert!(r.tx_results.iter().all(|t| t.success)); + // The refused creation was given all the gas there was, and spent it. + assert_eq!(r.tx_results[2].gas_used, 1_000_000); let new_root = crate::state::apply_state_changes(&root, store.as_ref(), &r.state_changes, &r.markers); - // If the second CREATE2 had failed (collision against the stale - // destroyed entry), tx 3 would have been a plain transfer and the - // victim would survive with balance 7. - assert!( - trie_account(&new_root, store.as_ref(), &victim).is_none(), - "victim fully deleted after destroy → CREATE2 re-deploy → destroy" + let victim_acct = trie_account(&new_root, store.as_ref(), &victim).expect("an account"); + assert_eq!( + victim_acct.balance, + U256::from(7), + "the plain account the transfer made" ); - assert!( - new_root - .get( - &TrieKeySlice::from_key(&rustock_trie::code_key(&victim)), - store.as_ref(), - ) - .is_none(), - "victim code deleted" + assert_subtree_deleted(&new_root, store.as_ref(), &victim); + // Both CREATE2s count on the factory's nonce, the refused one too. + let factory_acct = trie_account(&new_root, store.as_ref(), &factory).unwrap(); + assert_eq!(factory_acct.nonce, U256::from(3)); + let sender_acct = trie_account(&new_root, store.as_ref(), &sender).unwrap(); + assert_eq!(sender_acct.balance, one_rbtc - U256::from(7)); + } + + /// RSKIP-131 across sublists: rskj hands what the parallel sublists + /// deleted to the sequential one (`BlockExecutor.executeParallel`), so a + /// CREATE2 there at an address a parallel sublist destroyed is refused, + /// on one thread or two. Two parallel sublists, as one runs as the whole + /// block does. + #[test] + fn create2_in_the_sequential_sublist_where_a_parallel_one_destroyed_is_refused() { + let store: Arc = Arc::new(MemoryTrieStore::new()); + let block_store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let one_rbtc = U256::from(10u64).pow(U256::from(18)); + let destroyer = Address::repeat_byte(0x11); + let bystander = Address::repeat_byte(0x13); + let creator = Address::repeat_byte(0x12); + // The factory and its victim of the test above, the victim deployed. + let factory = Address::repeat_byte(0xFA); + let init: [u8; 11] = [ + 0x61, 0x33, 0xFF, 0x60, 0x00, 0x52, 0x60, 0x02, 0x60, 0x1E, 0xF3, + ]; + let mut factory_code = vec![0x6A]; + factory_code.extend_from_slice(&init); + factory_code.extend_from_slice(&[ + 0x60, 0x00, 0x52, 0x60, 0x00, 0x60, 0x0B, 0x60, 0x15, 0x60, 0x00, 0xF5, 0x00, + ]); + let victim = factory.create2(B256::ZERO, alloy_primitives::keccak256(init)); + + let mut root = put_account(&TrieNode::empty(), store.as_ref(), &destroyer, 0, one_rbtc); + root = put_account(&root, store.as_ref(), &bystander, 0, one_rbtc); + root = put_account(&root, store.as_ref(), &creator, 0, one_rbtc); + root = put_account_with_code( + &root, + store.as_ref(), + &factory, + 2, + U256::ZERO, + &factory_code, ); - assert!( - new_root - .get( - &TrieKeySlice::from_key(&rustock_trie::storage_prefix_key(&victim)), + root = put_account_with_code(&root, store.as_ref(), &victim, 1, U256::ZERO, &[0x33, 0xFF]); + let marker = rustock_trie::storage_prefix_key(&victim); + root = root.put(&TrieKeySlice::from_key(&marker), &[1], store.as_ref()); + + // Testnet, past reed810 (#7,139,600): {tx0} and {tx1} in parallel + // sublists, tx2 sequential. + let mut header = dummy_header(8_000_000); + header.version_fields = Some(rustock_core::types::header::HeaderVersionFields { + version: 1, + edges: Some(vec![1, 2]), + base_event: None, + }); + header.gas_limit = U256::from(12_000_000); + let tx = |to: &Address| rustock_core::Transaction { + gas_limit: U256::from(500_000), + ..plain_tx(0, to, U256::ZERO) + }; + let transactions = vec![ + (tx(&victim), destroyer), + (tx(&Address::repeat_byte(0xA1)), bystander), + (tx(&factory), creator), + ]; + + for threads in [1, 2] { + let executor = RskExecutor::new(RskHardforkConfig::testnet(), block_store.clone()) + .with_execution_threads(threads); + let r = executor + .execute_block(&header, &transactions, &root, store.clone()) + .expect("block"); + assert!( + r.tx_results.iter().all(|t| t.success), + "{threads} thread(s)" + ); + assert_eq!( + r.tx_results[2].gas_used, 500_000, + "{threads} thread(s): refused" + ); + let new_root = r.applied_root.clone().unwrap_or_else(|| { + crate::state::apply_state_changes( + &root, store.as_ref(), + &r.state_changes, + &r.markers, ) - .is_none(), - "victim marker deleted" - ); - // The selfdestruct in tx 3 pays the 7 wei back to the caller. - let sender_acct = trie_account(&new_root, store.as_ref(), &sender).unwrap(); - assert_eq!(sender_acct.balance, one_rbtc, "value round-trips via suicide"); + }); + let gone = trie_account(&new_root, store.as_ref(), &victim).is_none(); + assert!(gone, "{threads} thread(s): the victim stays deleted"); + assert_subtree_deleted(&new_root, store.as_ref(), &victim); + let factory_acct = trie_account(&new_root, store.as_ref(), &factory).unwrap(); + assert_eq!(factory_acct.nonce, U256::from(3), "{threads} thread(s)"); + } + } + + /// FRCR-279: before RSKIP169 (iris300) rskj's EXTCODEHASH answers a + /// contract whose code key holds nothing with 0 (`getCodeHashNonStandard`), + /// and from it with `keccak256("")`, as any account without code. A + /// contract is an account with the marker `setupContract` writes: here + /// one made with no code in the same transaction, one made earlier in the + /// block and one from before it. An account without the marker answers + /// `keccak256("")` either way -- one a creation with no data made too, + /// as rskj's `TransactionExecutor.create` doesn't call `setupContract` for + /// it -- and an address with no account 0. + /// + /// The contract made with no code keeps its marker, too, though a later + /// transaction loads it again, which drops revm's flag for a creation. + #[test] + fn extcodehash_of_a_contract_without_code_is_0_before_rskip169() { + let one_rbtc = U256::from(10u64).pow(U256::from(18)); + let sender = Address::repeat_byte(0xAC); + // Probe: SSTORE(addr, EXTCODEHASH(addr) + 1), the address its calldata. + let probe = Address::repeat_byte(0xB0); + let probe_code = [ + 0x60, 0x00, 0x35, 0x80, 0x3f, 0x60, 0x01, 0x01, 0x90, 0x55, 0x00, + ]; + // Maker: CREATE with no init code; SSTORE(0, EXTCODEHASH(new) + 1). + let maker = Address::repeat_byte(0xB1); + let maker_code = [ + 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0xf0, 0x3f, 0x60, 0x01, 0x01, 0x60, 0x00, 0x55, + 0x00, + ]; + let made = maker.create(1); + let codeless = Address::repeat_byte(0xB2); + let eoa = Address::repeat_byte(0xB3); + let absent = Address::repeat_byte(0xB4); + let no_data = sender.create(5); + + let store = Arc::new(MemoryTrieStore::new()); + let root = put_account(&TrieNode::empty(), store.as_ref(), &sender, 0, one_rbtc); + let root = put_account_with_code(&root, store.as_ref(), &probe, 1, U256::ZERO, &probe_code); + let root = put_account_with_code(&root, store.as_ref(), &maker, 1, U256::ZERO, &maker_code); + // A contract with no code from before the block: its marker, no code. + let root = put_account(&root, store.as_ref(), &codeless, 1, U256::ZERO); + let marker = rustock_trie::storage_prefix_key(&codeless); + let root = root.put(&TrieKeySlice::from_key(&marker), &[1], store.as_ref()); + let root = put_account(&root, store.as_ref(), &eoa, 0, one_rbtc); + + let probe_tx = |nonce: u64, target: &Address| rustock_core::Transaction { + input: Bytes::copy_from_slice(target.into_word().as_slice()), + ..plain_tx(nonce, &probe, U256::ZERO) + }; + let txs = vec![ + (plain_tx(0, &maker, U256::ZERO), sender), + (probe_tx(1, &made), sender), + (probe_tx(2, &codeless), sender), + (probe_tx(3, &eoa), sender), + (probe_tx(4, &absent), sender), + ( + rustock_core::Transaction { + to: Bytes::new(), + ..plain_tx(5, &probe, U256::ZERO) + }, + sender, + ), + (probe_tx(6, &no_data), sender), + ]; + let block_store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); + let plus_one = |hash: B256| U256::from_be_bytes(hash.0) + U256::from(1); + let empty = plus_one(crate::rsk_instructions::KECCAK_EMPTY); + // Between papyrus200 (#2,392,700, EXTCODEHASH) and iris300 (#3,614,800), + // then past it. + for (number, contract) in [(3_000_000u64, plus_one(B256::ZERO)), (3_700_000, empty)] { + let r = executor + .execute_block(&dummy_header(number), &txs, &root, store.clone()) + .expect("block"); + assert!(r.tx_results.iter().all(|t| t.success), "#{number}"); + let slot = |owner: &Address, key: U256| { + r.state_changes + .get(owner) + .and_then(|a| a.storage.get(&key)) + .map_or(U256::ZERO, |s| s.present_value) + }; + let probed = |target: &Address| slot(&probe, U256::from_be_slice(target.as_slice())); + assert_eq!( + slot(&maker, U256::ZERO), + contract, + "#{number}: made in this transaction" + ); + assert_eq!( + probed(&made), + contract, + "#{number}: made earlier in the block" + ); + assert_eq!( + probed(&codeless), + contract, + "#{number}: made before the block" + ); + assert_eq!(probed(&eoa), empty, "#{number}: not a contract"); + assert_eq!( + probed(&absent), + plus_one(B256::ZERO), + "#{number}: no account" + ); + assert_eq!( + probed(&no_data), + empty, + "#{number}: made with no data, no contract" + ); + + let new_root = crate::state::apply_state_changes( + &root, + store.as_ref(), + &r.state_changes, + &r.markers, + ); + let made_marker = rustock_trie::storage_prefix_key(&made); + assert!( + new_root + .get(&TrieKeySlice::from_key(&made_marker), store.as_ref()) + .is_some(), + "#{number}: the contract made with no code keeps its marker" + ); + } + } + + /// An account an earlier transaction of the block destroyed doesn't + /// exist for the rest of the block (rskj deletes it as that transaction + /// ends) until something touches it: EXTCODEHASH answers 0, and a CALL + /// to it pays NEW_ACCT_CALL, as for any address with no account. The + /// CALL makes it again, a plain account, not a contract: before RSKIP169 + /// too, though the parent state still holds its marker. + #[test] + fn an_account_destroyed_earlier_in_the_block_does_not_exist_until_touched() { + let one_rbtc = U256::from(10u64).pow(U256::from(18)); + let sender = Address::repeat_byte(0xAC); + // Victim: CALLER; SELFDESTRUCT. And an account that stays. + let victim = Address::repeat_byte(0xD1); + let eoa = Address::repeat_byte(0xE0); + // Probes: SSTORE(addr, EXTCODEHASH(addr) + 1), the address their calldata. + let probe_code = [ + 0x60, 0x00, 0x35, 0x80, 0x3f, 0x60, 0x01, 0x01, 0x90, 0x55, 0x00, + ]; + let first_probe = Address::repeat_byte(0xB5); + let second_probe = Address::repeat_byte(0xB6); + // Caller: CALL(1M gas, the address its calldata, 0 wei, no data); POP; STOP. + let caller = Address::repeat_byte(0xB7); + let caller_code = [ + 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x35, 0x62, + 0x0f, 0x42, 0x40, 0xf1, 0x50, 0x00, + ]; + + let store = Arc::new(MemoryTrieStore::new()); + let mut root = put_account(&TrieNode::empty(), store.as_ref(), &sender, 0, one_rbtc); + root = put_account_with_code(&root, store.as_ref(), &victim, 1, U256::ZERO, &[0x33, 0xFF]); + let marker = rustock_trie::storage_prefix_key(&victim); + root = root.put(&TrieKeySlice::from_key(&marker), &[1], store.as_ref()); + root = put_account(&root, store.as_ref(), &eoa, 0, U256::ZERO); + for probe in [first_probe, second_probe] { + root = put_account_with_code(&root, store.as_ref(), &probe, 1, U256::ZERO, &probe_code); + } + root = put_account_with_code(&root, store.as_ref(), &caller, 1, U256::ZERO, &caller_code); + + let with_input = |nonce: u64, to: &Address, target: &Address| rustock_core::Transaction { + input: Bytes::copy_from_slice(target.into_word().as_slice()), + ..plain_tx(nonce, to, U256::ZERO) + }; + let txs = vec![ + (plain_tx(0, &victim, U256::ZERO), sender), // destroys it + (with_input(1, &first_probe, &victim), sender), // EXTCODEHASH: no account + (with_input(2, &caller, &victim), sender), // CALL: a new account + (with_input(3, &caller, &eoa), sender), // CALL: an account + (with_input(4, &second_probe, &victim), sender), // EXTCODEHASH: an account + ]; + let block_store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); + let empty = U256::from_be_bytes(crate::rsk_instructions::KECCAK_EMPTY.0); + // Before iris300 (#3,614,800) and after it. + for number in [3_000_000u64, 3_700_000] { + let r = executor + .execute_block(&dummy_header(number), &txs, &root, store.clone()) + .expect("block"); + assert!(r.tx_results.iter().all(|t| t.success), "#{number}"); + let probed = |probe: &Address| { + let key = U256::from_be_slice(victim.as_slice()); + r.state_changes + .get(probe) + .and_then(|a| a.storage.get(&key)) + .map_or(U256::ZERO, |s| s.present_value) + }; + assert_eq!( + probed(&first_probe), + U256::from(1), + "#{number}: no account: 0" + ); + assert_eq!( + probed(&second_probe), + empty + U256::from(1), + "#{number}: an account" + ); + assert_eq!( + r.tx_results[2].gas_used - r.tx_results[3].gas_used, + 25_000, + "#{number}: a CALL to an address with no account pays NEW_ACCT_CALL" + ); + + let new_root = crate::state::apply_state_changes( + &root, + store.as_ref(), + &r.state_changes, + &r.markers, + ); + let again = trie_account(&new_root, store.as_ref(), &victim).is_some(); + assert!(again, "#{number}: made again"); + assert_subtree_deleted(&new_root, store.as_ref(), &victim); + } } } diff --git a/crates/execution/src/hardfork.rs b/crates/execution/src/hardfork.rs index f1cb4f0..cd5a6de 100644 --- a/crates/execution/src/hardfork.rs +++ b/crates/execution/src/hardfork.rs @@ -729,6 +729,13 @@ impl RskHardforkConfig { self.active_upgrade(block_number) >= RskNetworkUpgrade::Iris300 } + /// RSKIP169: EXTCODEHASH of a contract whose code key holds nothing + /// answers `keccak256("")` (rskj `getCodeHashStandard`), where before it + /// answered 0 (`getCodeHashNonStandard`) (Iris300). + pub fn has_rskip169(&self, block_number: u64) -> bool { + self.active_upgrade(block_number) >= RskNetworkUpgrade::Iris300 + } + /// RSKIP174: a top-level contract creation carries over the balance an /// address already held, instead of destroying it (Iris300). /// @@ -1586,6 +1593,7 @@ mod rskj_schedule_tests { ("rskip146", RskHardforkConfig::has_rskip146, Some(2_392_700), Some(863_000)), ("rskip150", RskHardforkConfig::has_rskip150, Some(2_018_000), Some(504_000)), ("rskip152", RskHardforkConfig::has_chainid, Some(2_392_700), Some(863_000)), + ("rskip169", RskHardforkConfig::has_rskip169, Some(3_614_800), Some(2_060_500)), ("rskip170", RskHardforkConfig::has_rskip170, Some(3_614_800), Some(2_060_500)), ("rskip171", RskHardforkConfig::has_rskip171, Some(3_614_800), Some(2_060_500)), ("rskip174", RskHardforkConfig::has_rskip174, Some(3_614_800), Some(2_060_500)), diff --git a/crates/execution/src/raw_storage.rs b/crates/execution/src/raw_storage.rs index 4a793ac..b9d624e 100644 --- a/crates/execution/src/raw_storage.rs +++ b/crates/execution/src/raw_storage.rs @@ -53,6 +53,16 @@ pub struct RskChainExt { /// transaction called the precompile itself /// (`crate::tracer::record_precompile_transfer`). pub precompile_transfers: Vec<(Address, Address, U256)>, + /// rskj's `deletedAccounts`: what the block's earlier transactions + /// destroyed (`TransactionExecutor.finalization` deletes each as its + /// transaction ends). From RSKIP125 a creation at one of them fails + /// (`Program.createContract`). A sequential sublist starts with what the + /// parallel ones deleted, as rskj's `BlockExecutor` hands it on. + pub deleted_in_block: std::collections::HashSet
, + /// The contracts the block's earlier transactions created and left + /// standing. Each holds rskj's storage-prefix marker (`setupContract` + /// writes it as the creation begins), whatever code it ended with. + pub created_in_block: std::collections::HashSet
, } /// Raw-bytes storage overlay for the block being executed. diff --git a/crates/execution/src/rsk_handler.rs b/crates/execution/src/rsk_handler.rs index fc4a3fc..7671651 100644 --- a/crates/execution/src/rsk_handler.rs +++ b/crates/execution/src/rsk_handler.rs @@ -610,43 +610,51 @@ where // (rskj Program.getMaxDepth); revm hard-codes 1024, so // the cap is enforced here. rskj's deep checks push 0, // refund the child gas, and return BEFORE any - // returnDataBuffer reset. + // returnDataBuffer reset. RSKIP-131's refusal of a + // creation at an address the block deleted returns + // before it too, the creation's gas spent. let rskip150_active = self.pre_rskip150_precompiles.is_none(); - if rskip150_active && init.depth > 400 { - if let Some(synth) = call_too_deep_result(&init.frame_input) { - use revm::interpreter::interpreter_types::ReturnData; - preserved_return_buffer = Some( - evm.frame_stack() - .get() - .interpreter - .return_data - .buffer() - .clone(), + let refused = if rskip150_active && init.depth > 400 { + call_too_deep_result(&init.frame_input) + } else if self.rskip125_active { + deleted_address_collision(evm, &init) + } else { + None + }; + if let Some(synth) = refused { + use revm::interpreter::interpreter_types::ReturnData; + preserved_return_buffer = Some( + evm.frame_stack() + .get() + .interpreter + .return_data + .buffer() + .clone(), + ); + if *TRACE_FRAMES { + tracing::debug!( + "FRAME REJECTED {:?} depth={}", + synth.instruction_result(), + init.depth ); - if *TRACE_FRAMES { - tracing::debug!( - "FRAME REJECTED CallTooDeep depth={} (RSKIP150 limit 400)", - init.depth - ); - } - // An inspector hears of the refusal as revm tells it - // of a frame refused at its own limit of 1024. - let synth = D::refuse(evm, init, synth); - note_attempted_writes(evm); - let returned = evm.frame_return_result(synth)?; - forget_reverted_entries(evm); - if let Some(result) = returned { - return Ok(result); - } - if let Some(buffer) = preserved_return_buffer { - evm.frame_stack() - .get() - .interpreter - .return_data - .set_buffer(buffer); - } - continue; } + // An inspector hears of the refusal as revm tells it + // of a frame refused at its own limit of 1024. + let synth = D::refuse(evm, init, synth); + note_attempted_writes(evm); + let returned = evm.frame_return_result(synth)?; + forget_reverted_entries(evm); + if let Some(result) = returned { + return Ok(result); + } + if let Some(buffer) = preserved_return_buffer { + evm.frame_stack() + .get() + .interpreter + .return_data + .set_buffer(buffer); + } + continue; } match D::init(evm, init)? { ItemOrResult::Item(()) => { @@ -1137,6 +1145,53 @@ fn call_too_deep_result( } } +/// RSKIP-131, with RSKIP125: rskj's `Program.createContract` refuses a +/// creation at an address an earlier transaction of the block deleted +/// (`deletedAccountsInBlock`) as it refuses one at an address in use: the +/// creator's nonce is bumped, the gas forwarded to the creation is spent, +/// and 0 is pushed. `None` lets revm open the frame, or refuse it first for +/// depth or endowment, as rskj refuses those first. +fn deleted_address_collision(evm: &mut EVM, init: &FrameInit) -> Option +where + EVM: EvmTr< + Context: ContextTr< + Chain = crate::raw_storage::RskChainExt, + Journal: JournalTr, + >, + >, +{ + use revm::interpreter::interpreter_action::FrameInput; + use revm::interpreter::InterpreterResult; + use revm::primitives::constants::CALL_STACK_LIMIT; + let FrameInput::Create(inputs) = &init.frame_input else { + return None; + }; + let ctx = evm.ctx(); + if init.depth > CALL_STACK_LIMIT as usize || ctx.chain().deleted_in_block.is_empty() { + return None; + } + let (balance, nonce) = { + let caller = ctx.journal_mut().load_account_mut(inputs.caller()).ok()?; + (*caller.balance(), caller.nonce()) + }; + let address = inputs.created_address(nonce); + if balance < inputs.value() || !ctx.chain().deleted_in_block.contains(&address) { + return None; + } + let mut caller = ctx.journal_mut().load_account_mut(inputs.caller()).ok()?; + if !caller.bump_nonce() { + return None; + } + Some(FrameResult::Create(CreateOutcome::new( + InterpreterResult { + result: InstructionResult::CreateCollision, + gas: Gas::new(inputs.gas_limit()), + output: Bytes::new(), + }, + None, + ))) +} + /// If the top (just-initialized) frame is a CREATE frame, reset the created /// account's nonce to 0. On revert the AccountCreated journal entry discards /// the account wholesale, so no extra journaling is needed. diff --git a/crates/execution/src/rsk_instructions.rs b/crates/execution/src/rsk_instructions.rs index eb6d50c..f25f0a8 100644 --- a/crates/execution/src/rsk_instructions.rs +++ b/crates/execution/src/rsk_instructions.rs @@ -66,6 +66,10 @@ thread_local! { /// (rskj `VM.doEXTCODEHASH` precompile branch). Set by `install`. static EXTCODEHASH_PRECOMPILES: core::cell::RefCell> = const { core::cell::RefCell::new(Vec::new()) }; + /// RSKIP169: whether EXTCODEHASH answers a contract whose code key holds + /// nothing with `keccak256("")` (`getCodeHashStandard`), or with 0 as + /// before it (`getCodeHashNonStandard`). Set by `install`. + static EXTCODEHASH_STANDARD: core::cell::Cell = const { core::cell::Cell::new(true) }; /// The block's real miner address (`header.getCoinbase()`), pushed by the /// COINBASE opcode. rustock sets `block.beneficiary = REMASC_ADDR` so revm /// routes gas fees to REMASC; that would make COINBASE return REMASC, but @@ -160,6 +164,9 @@ pub fn set_transaction_index(index: u64) { /// EXTCODEHASH only activates at papyrus — so before that it must throw an /// invalid-opcode exception (consuming all gas), matching rskj's VM. /// +/// `extcodehash_standard` is RSKIP169 (iris300): from it EXTCODEHASH answers a +/// contract whose code key holds nothing as any account without code. +/// /// `dupn_swapn_txindex_enabled` is RSKIP191's absence: before iris300 rskj /// runs RSK's own DUPN, SWAPN and TXINDEX. /// @@ -167,6 +174,7 @@ pub fn set_transaction_index(index: u64) { pub fn install( instructions: &mut EthInstructions, extcodehash_enabled: bool, + extcodehash_standard: bool, istanbul_opcodes_enabled: bool, push0_enabled: bool, basefee_enabled: bool, @@ -179,7 +187,7 @@ pub fn install( block: BlockWords, ) where WIRE: InterpreterTypes, - HOST: Host, + HOST: Host + ContractMarker, { // RSKIP446 (lovell700): transient storage TLOAD/TSTORE (0x5c/0x5d, // EIP-1153, rskj `GasCost.TLOAD`/`TSTORE` = 100 each). RSKIP445 (lovell700): @@ -240,6 +248,7 @@ pub fn install( // active precompiles to `keccak256("")` — see `rsk_extcodehash`. EXTCODEHASH_PRECOMPILES .with(|p| *p.borrow_mut() = extcodehash_precompiles.to_vec()); + EXTCODEHASH_STANDARD.set(extcodehash_standard); instructions.insert_instruction( opcode::EXTCODEHASH, Instruction::new(rsk_extcodehash::, 400), @@ -480,7 +489,9 @@ pub(crate) const KECCAK_EMPTY: B256 = B256::new([ /// - active precompile -> `keccak256("")` (rskj special-cases precompiles); /// - account absent from the trie -> `0`; /// - account present but not a contract (no code) -> `keccak256("")`; -/// - contract -> its code hash. +/// - contract -> its code hash. One whose code key holds nothing answers +/// `keccak256("")` from RSKIP169 (`getCodeHashStandard`) and `0` before +/// it (`getCodeHashNonStandard`), which needs [`ContractMarker`]. /// /// revm's stock EXTCODEHASH instead returns `0` whenever `AccountInfo::is_empty()` /// (EIP-161: balance==0 && nonce==0 && no code), so an existing-but-empty @@ -490,7 +501,7 @@ pub(crate) const KECCAK_EMPTY: B256 = B256::new([ /// host's flag (`true` only when `Database::basic` returned `None`, i.e. the /// trie has no node — see the HOMESTEAD journal pin in `RskHandler`), which is /// exactly rskj's `isExist`. -fn rsk_extcodehash( +fn rsk_extcodehash( context: InstructionContext<'_, H, WIRE>, ) { let Some(([], top)) = context.interpreter.stack.popn_top::<0>() else { @@ -509,18 +520,84 @@ fn rsk_extcodehash( context.interpreter.halt_fatal(); return; }; - // `is_empty` is the host's trie-existence flag (rskj `isExist`): absent -> - // 0; present-but-codeless -> keccak256(""); contract -> its code hash. - let result = if load.is_empty { + // `is_empty` is the host's trie-existence flag (rskj `isExist`). + let exists = !load.is_empty; + let code_hash = load.account.code_hash; + let codeless = load.account.is_empty_code_hash() || code_hash.is_zero(); + let result = if !exists { B256::ZERO - } else if load.account.is_empty_code_hash() || load.account.code_hash.is_zero() { + } else if !codeless { + code_hash + } else if EXTCODEHASH_STANDARD.get() { KECCAK_EMPTY } else { - load.account.code_hash + match context.host.holds_contract_marker(addr) { + Some(true) => B256::ZERO, + Some(false) => KECCAK_EMPTY, + None => { + context.interpreter.halt_fatal(); + return; + } + } }; *top = result.into(); } +/// What EXTCODEHASH asks before RSKIP169 that revm's `Host` can't answer: +/// whether an account is a contract as rskj's `isContract` reads it, by the +/// storage-prefix marker `setupContract` writes. +pub trait ContractMarker { + /// Whether `address` holds the marker. `None` where the state couldn't be + /// read, the error left in the context as revm's own loads leave it. + fn holds_contract_marker(&mut self, address: Address) -> Option; +} + +type RskDb = revm::database::WrapDatabaseRef; + +/// The executor's context. The marker is a creation's of this transaction +/// (the journal), of the block's earlier ones (the chain slot, which also +/// knows what they destroyed, marker and all), or else the parent state's. +impl ContractMarker + for revm::Context< + BLOCK, + TX, + CFG, + RskDb, + revm::context::Journal, + crate::raw_storage::RskChainExt, + LOCAL, + > +where + LOCAL: revm::context_interface::LocalContextTr, +{ + fn holds_contract_marker(&mut self, address: Address) -> Option { + let created_now = self + .journaled_state + .inner + .state + .get(&address) + .is_some_and(|account| account.is_created_locally()); + if created_now || self.chain.created_in_block.contains(&address) { + return Some(true); + } + if self.chain.deleted_in_block.contains(&address) { + return Some(false); + } + match self + .journaled_state + .database + .0 + .has_contract_marker(&address) + { + Ok(holds) => Some(holds), + Err(e) => { + self.error = Err(revm::context_interface::ContextError::Db(e)); + None + } + } + } +} + /// CHAINID without revm's ISTANBUL spec check (RSKIP152 activates it at /// papyrus200 while the spec stays PETERSBURG). fn rsk_chainid( @@ -802,7 +879,7 @@ thread_local! { /// All-ops tracer: log pc/opcode/gas (post-static-charge), then dispatch to /// the real implementation (custom rsk op or default PETERSBURG table). -fn traced_all( +fn traced_all( context: InstructionContext<'_, HOST, WIRE>, ) { use revm::interpreter::interpreter_types::{InputsTr, Jumps, LegacyBytecode}; @@ -1377,6 +1454,7 @@ mod thread_affinity_tests { &mut table, true, true, + true, false, false, true, @@ -1414,11 +1492,14 @@ mod thread_affinity_tests { } /// `DummyHost` only needs to exist for the table to be generic over - /// something; no instruction runs here. + /// something the instructions take, which is the executor's context: it + /// answers EXTCODEHASH's `ContractMarker`. No instruction runs here. type DummyHost = revm::context::Context< revm::context::BlockEnv, revm::context::TxEnv, revm::context::CfgEnv, - revm::database::EmptyDB, + RskDb, + revm::context::Journal, + crate::raw_storage::RskChainExt, >; } diff --git a/crates/execution/src/state.rs b/crates/execution/src/state.rs index 6ec9386..5b2aa7f 100644 --- a/crates/execution/src/state.rs +++ b/crates/execution/src/state.rs @@ -40,6 +40,15 @@ pub struct ContractMarkers { /// credited here, as `bump_remasc_sender` is, and created if it is new, /// even for nothing, as rskj's `addBalance` creates it. pub null_account: Option, + /// The contracts the block created and left standing, each with rskj's + /// marker whatever code it ended with (`setupContract` writes it as the + /// creation begins). revm forgets a creation's local flag once a later + /// transaction loads the account, so one created with no code and + /// touched again would otherwise lose its marker. + pub created: Vec
, + /// What the block's transactions destroyed (rskj's `deletedAccounts`), + /// which a sequential sublist inherits from the parallel ones. + pub deleted: Vec
, } /// Apply all state changes from EVM execution to the trie, returning the new root. @@ -92,10 +101,14 @@ pub fn apply_state_changes( // unitrie (mainnet: ~20 EOAs that took value, by #1,591,000). // A real CREATE goes through `create_account_checkpoint` // (`mark_created_locally` → `CreatedLocal`) and deploys code, so - // gate the marker on having code or being locally created. + // gate the marker on having code or being locally created -- in + // this transaction (`CreatedLocal`) or an earlier one of the + // block's, whose flag revm drops when a later one loads the + // account (`markers.created`). let code_bytes = account.info.code.as_ref().map(|c| c.original_bytes()); let has_code = code_bytes.as_ref().is_some_and(|c| !c.is_empty()); - if !markers.skip_created.contains(addr) && (has_code || account.is_created_locally()) { + let created_here = account.is_created_locally() || markers.created.contains(addr); + if !markers.skip_created.contains(addr) && (has_code || created_here) { batch.push((storage_prefix_key(addr), vec![1])); } if let Some(code_bytes) = code_bytes { From 6d507054ca10b96b31fb46ab8369ffe9a66c706f Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Tue, 10 Nov 2026 12:09:20 -0800 Subject: [PATCH 099/264] Execution: a refused CREATE keeps the return data at every height rskj's `Program.createContract` refuses a creation for depth, balance or an address in use before `getProgramResult` resets the caller's returnDataBuffer, and RSKIP171's `cleanReturnDataBuffer` is only on the call paths. rustock kept the buffer for the first two, and only before RSKIP171. RETURNDATASIZE after a CREATE refused for want of balance now reports the previous call's output before iris300 and after. --- crates/execution/src/executor.rs | 56 +++++++++++++++++++++++++++++ crates/execution/src/hardfork.rs | 6 ++-- crates/execution/src/rsk_handler.rs | 29 ++++++++------- 3 files changed, 77 insertions(+), 14 deletions(-) diff --git a/crates/execution/src/executor.rs b/crates/execution/src/executor.rs index 7996307..35f079b 100644 --- a/crates/execution/src/executor.rs +++ b/crates/execution/src/executor.rs @@ -5140,6 +5140,62 @@ mod tests { ); } + /// A CREATE that rskj's `Program.createContract` refuses before it builds + /// a program -- here for a value its creator hasn't got -- leaves the + /// creator's returnDataBuffer as it was, before iris300 and after: the + /// refusal returns before `getProgramResult` resets the buffer, and + /// RSKIP171's `cleanReturnDataBuffer` is only on the call paths. + /// RETURNDATASIZE after it still reports the previous call's 32 bytes. + #[test] + fn test_refused_create_keeps_return_data_buffer_at_every_height() { + let one_rbtc = U256::from(10u64).pow(U256::from(18)); + let sender = Address::repeat_byte(0xAC); + // Source: RETURN 32 zero bytes. + let source = Address::repeat_byte(0xB8); + let source_code = [0x60, 0x20, 0x60, 0x00, 0xf3]; + // Main: CALL source; POP; CREATE(1 wei, no code); POP; + // SSTORE(0, RETURNDATASIZE); STOP. + let main = Address::repeat_byte(0xB9); + let mut main_code = vec![ + 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x73, + ]; + main_code.extend_from_slice(source.as_slice()); + main_code.extend_from_slice(&[0x62, 0x0f, 0x42, 0x40, 0xf1, 0x50]); + main_code.extend_from_slice(&[0x60, 0x00, 0x60, 0x00, 0x60, 0x01, 0xf0, 0x50]); + main_code.extend_from_slice(&[0x3d, 0x60, 0x00, 0x55, 0x00]); + + let store = Arc::new(MemoryTrieStore::new()); + let root = put_account(&TrieNode::empty(), store.as_ref(), &sender, 0, one_rbtc); + let root = + put_account_with_code(&root, store.as_ref(), &source, 1, U256::ZERO, &source_code); + let root = put_account_with_code(&root, store.as_ref(), &main, 1, U256::ZERO, &main_code); + let block_store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); + // Before iris300 (#3,614,800) and after it. + for number in [3_000_000u64, 3_616_094] { + let call = plain_tx(0, &main, U256::ZERO); + let r = executor + .execute_block( + &dummy_header(number), + &[(call, sender)], + &root, + store.clone(), + ) + .expect("block"); + assert!(r.tx_results[0].success, "#{number}"); + let size = r + .state_changes + .get(&main) + .and_then(|a| a.storage.get(&U256::ZERO)) + .map_or(U256::ZERO, |s| s.present_value); + assert_eq!( + size, + U256::from(32), + "#{number}: RETURNDATASIZE after the refused CREATE" + ); + } + } + /// Regression for mainnet #1,661,324: rskj's VM.doCODESIZE (RSKIP90, orchid) /// reports EXTCODESIZE of an active precompile as 2^256-1. A proxy that /// guards `extcodesize(impl) != 0` before forwarding to the BlockHeader diff --git a/crates/execution/src/hardfork.rs b/crates/execution/src/hardfork.rs index cd5a6de..9214ed3 100644 --- a/crates/execution/src/hardfork.rs +++ b/crates/execution/src/hardfork.rs @@ -722,9 +722,11 @@ impl RskHardforkConfig { self.active_upgrade(block_number) >= RskNetworkUpgrade::Iris300 } - /// RSKIP171: a CALL/CREATE that runs no new frame resets the caller's + /// RSKIP171: a CALL that opens no new frame resets the caller's /// returnDataBuffer (canonical EIP-211), instead of preserving the prior - /// call's output as pre-iris rskj did (Iris300). + /// call's output as pre-iris rskj did (Iris300). Only rskj's call paths + /// run that clean-up (`Program.cleanReturnDataBuffer`): a creation that + /// runs no init code leaves the buffer as it was at every height. pub fn has_rskip171(&self, block_number: u64) -> bool { self.active_upgrade(block_number) >= RskNetworkUpgrade::Iris300 } diff --git a/crates/execution/src/rsk_handler.rs b/crates/execution/src/rsk_handler.rs index 7671651..6a532fa 100644 --- a/crates/execution/src/rsk_handler.rs +++ b/crates/execution/src/rsk_handler.rs @@ -690,22 +690,27 @@ where // previous buffer (mainnet #2,669,886: RETURNDATASIZE // after a value transfer to an empty account still // reports the prior call's 32-byte output). - // For CREATE only the pre-checks (depth/endowment) - // return before rskj's reset. // RSKIP171 (iris300): cleanReturnDataBuffer now // fires on every no-frame call, so the pre-iris // preservation is disabled and revm's EIP-211 // clearing stands. - let rskj_preserves = !self.rskip171_active - && match &result { - FrameResult::Call(_) => callee - .is_some_and(|c| !self.active_precompiles.contains(&c)), - FrameResult::Create(outcome) => matches!( - outcome.instruction_result(), - InstructionResult::CallTooDeep - | InstructionResult::OutOfFunds - ), - }; + // A CREATE that `createContract` refuses before it + // builds a program (depth, endowment, an address in + // use) keeps the buffer at every height: RSKIP171's + // clean-up is only on the call paths. + let rskj_preserves = match &result { + FrameResult::Call(_) => { + !self.rskip171_active + && callee + .is_some_and(|c| !self.active_precompiles.contains(&c)) + } + FrameResult::Create(outcome) => matches!( + outcome.instruction_result(), + InstructionResult::CallTooDeep + | InstructionResult::OutOfFunds + | InstructionResult::CreateCollision + ), + }; if rskj_preserves { use revm::interpreter::interpreter_types::ReturnData; preserved_return_buffer = Some( From b4e0248ceeb5f041e8f3732ba63b2e7854fb7dcc Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Tue, 10 Nov 2026 12:55:47 -0800 Subject: [PATCH 100/264] Comments: no private references, and RSKj's release where they cite it Drops private task references, a commit hash the stack's rebuild would leave dangling and a scratch script's path from comments. Comments that cite bitcoinj-thin name the release's `v0.14.4-rsk-18`, whose `BtcBlock`, `PartialMerkleTree` and `BtcAbstractBlockChain` are those of `rsk-20`, and two that cite `Bridge.java` give the release's lines. `install`'s note on CHAINID and SELFBALANCE before papyrus200 is corrected: revm's NotActivated spends the frame's gas too, as the invalid opcode does. --- crates/core/src/btc_block.rs | 2 +- crates/core/src/partial_merkle_tree.rs | 2 +- crates/execution/src/bridge/btc_chain.rs | 2 +- crates/execution/src/bridge/btc_rules.rs | 2 +- crates/execution/src/bridge/getters.rs | 2 +- crates/execution/src/bridge/peg.rs | 2 +- crates/execution/src/executor.rs | 22 +++++++++++----------- crates/execution/src/rsk_instructions.rs | 7 ++++--- crates/rpc/src/tests.rs | 10 +++++----- crates/sync/src/quota.rs | 2 +- crates/sync/src/tests.rs | 2 +- 11 files changed, 28 insertions(+), 27 deletions(-) diff --git a/crates/core/src/btc_block.rs b/crates/core/src/btc_block.rs index 7cc8252..322b25a 100644 --- a/crates/core/src/btc_block.rs +++ b/crates/core/src/btc_block.rs @@ -1,4 +1,4 @@ -//! Whether bitcoinj-thin's `BtcBlock` (`v0.14.4-rsk-20`) makes a Bitcoin +//! Whether bitcoinj-thin's `BtcBlock` (`v0.14.4-rsk-18`) makes a Bitcoin //! block of some bytes, or throws. //! //! Below RSKIP98 (orchid), rskj's `ProofOfWorkRule` takes a merge-mined diff --git a/crates/core/src/partial_merkle_tree.rs b/crates/core/src/partial_merkle_tree.rs index a6589b3..cd6cb20 100644 --- a/crates/core/src/partial_merkle_tree.rs +++ b/crates/core/src/partial_merkle_tree.rs @@ -1,5 +1,5 @@ //! A Bitcoin partial Merkle tree, as bitcoinj-thin's `PartialMerkleTree` -//! (`v0.14.4-rsk-20`) reads and walks it, and as rskj's +//! (`v0.14.4-rsk-18`) reads and walks it, and as rskj's //! `PartialMerkleTreeFormatUtils.hasExpectedSize` sizes it. //! //! rskj reads one wherever it has to be shown that a Bitcoin transaction is diff --git a/crates/execution/src/bridge/btc_chain.rs b/crates/execution/src/bridge/btc_chain.rs index 9db41e0..e570c3d 100644 --- a/crates/execution/src/bridge/btc_chain.rs +++ b/crates/execution/src/bridge/btc_chain.rs @@ -523,7 +523,7 @@ pub fn receive_headers( let block_number = revm::context_interface::Block::number(ctx.block()).to::(); // rskj `Bridge.receiveHeaders` refuses the whole call when any header is - // not a header's size, before it parses one (Bridge.java:537-542), and + // not a header's size, before it parses one (Bridge.java:539-544), and // then when it can't parse one (`makeBlock`). Both throw // `BridgeIllegalArgumentException`, which `Bridge.execute` answers with // null before RSKIP88: a success with no output, nothing saved. diff --git a/crates/execution/src/bridge/btc_rules.rs b/crates/execution/src/bridge/btc_rules.rs index b81d955..11e1d13 100644 --- a/crates/execution/src/bridge/btc_rules.rs +++ b/crates/execution/src/bridge/btc_rules.rs @@ -1,5 +1,5 @@ //! bitcoinj-thin's checks on a Bitcoin header the Bridge adds to its chain -//! (`BtcAbstractBlockChain.add`, tag `v0.14.4-rsk-20`). The header's target +//! (`BtcAbstractBlockChain.add`, tag `v0.14.4-rsk-18`). The header's target //! must be in range and its hash under it (`BtcBlock.verifyHeader`), and the //! target must be the one its parent's chain calls for //! (`NetworkParameters.checkDifficultyTransitions`, as each network has it). diff --git a/crates/execution/src/bridge/getters.rs b/crates/execution/src/bridge/getters.rs index b4a5bae..bb45b92 100644 --- a/crates/execution/src/bridge/getters.rs +++ b/crates/execution/src/bridge/getters.rs @@ -1472,7 +1472,7 @@ mod tests { /// are standard multisig, as rskj's public nodes answer /// `getFederationAddress` at block 1. rskj's regtest's is a P2SH-P2WSH ERP /// federation, as an independent port of rskj's `P2shErpRedeemScriptBuilder` - /// computes it (scratch `t294/regtest_genesis_fed.py`); before, rustock built + /// computes it; before, rustock built /// every genesis federation as standard multisig. #[test] fn the_genesis_federation_is_built_in_its_networks_format() { diff --git a/crates/execution/src/bridge/peg.rs b/crates/execution/src/bridge/peg.rs index 108c273..30dd117 100644 --- a/crates/execution/src/bridge/peg.rs +++ b/crates/execution/src/bridge/peg.rs @@ -4590,7 +4590,7 @@ pub fn add_signature( config: &BridgeConstants, hardfork_cfg: &RskHardforkConfig, ) -> Result { - // rskj `Bridge.addSignature` (Bridge.java:615) parses the federator key + // rskj `Bridge.addSignature` (Bridge.java:617) parses the federator key // (`BtcECKey.fromPublicOnly`), refuses an empty signature list, runs every // signature through `BtcECKey.ECDSASignature.decodeFromDER` and wraps the // RSK transaction hash in a `Keccak256`, all before `bridgeSupport` sees diff --git a/crates/execution/src/executor.rs b/crates/execution/src/executor.rs index 35f079b..5eb0563 100644 --- a/crates/execution/src/executor.rs +++ b/crates/execution/src/executor.rs @@ -3094,7 +3094,7 @@ mod tests { /// traced) and as `debug_traceTransaction` does (each transaction in /// turn), and requires the same state and receipts of all of them. The /// traced runs go through `inspect_run`, so they agree only while it runs - /// the loop the chain runs (task 277). Returns the chain's run. + /// the loop the chain runs. Returns the chain's run. fn assert_traced_runs_agree( executor: &RskExecutor, header: &Header, @@ -3177,7 +3177,7 @@ mod tests { init } - /// A trace and an estimate run the loop the chain runs (task 277): here + /// A trace and an estimate run the loop the chain runs: here /// RSKIP150's call depth of 400, where revm's own loop recurses to 1024, /// and the nonce a top-level create leaves its contract, 0, where revm's /// loop leaves 1. The contract counts its depth in slot 0 and calls @@ -3252,7 +3252,7 @@ mod tests { assert_eq!(depth, 400); } - /// A trace and an estimate run the loop the chain runs (task 277): before + /// A trace and an estimate run the loop the chain runs: before /// RSKIP453 an internal create whose code deposit can't be paid deploys /// empty code (`Program.finalizeContractCreation`), where revm's loop /// fails the create. Mainnet #1,071,481's RNS Deed. @@ -3304,7 +3304,7 @@ mod tests { assert_estimate_run_agrees(&executor, &header, &call, sender, &root, &store); } - /// A trace and an estimate run the loop the chain runs (task 277): a + /// A trace and an estimate run the loop the chain runs: a /// nested constructor's refund is dropped (`Program.finalizeContractCreation` /// passes on no `futureRefund`), where revm's loop keeps it. Mainnet /// #6,362,186. @@ -3353,7 +3353,7 @@ mod tests { assert_estimate_run_agrees(&executor, &header, &call, sender, &root, &store); } - /// A trace and an estimate run the loop the chain runs (task 277): before + /// A trace and an estimate run the loop the chain runs: before /// RSKIP197 a contract whose call to a precompile throws stops there with /// all its gas spent (`VM.steps`), where revm's loop lets it go on. And /// the trace stops at its CALL, as rskj's VM traces no step after it. @@ -3423,9 +3423,9 @@ mod tests { ); } - /// A trace and an estimate run the loop the chain runs (task 277): a frame + /// A trace and an estimate run the loop the chain runs: a frame /// that fails takes back what the precompiles under it wrote outside - /// revm's journal (`9821143`), where revm's loop keeps it. A contract + /// revm's journal, where revm's loop keeps it. A contract /// seeds the Bridge's BTC store through a getter and reverts. #[test] fn a_trace_or_an_estimate_takes_back_a_failed_frames_precompile_writes() { @@ -13007,8 +13007,8 @@ bf09f6e52420834e8e0e0b1a6df563aba550cf7f99e9724264187c45dcf3d73e8585a0e35196\ .and_then(|data| AccountState::decode(&data).ok()) } - /// REMASC's payments are CALL subtraces of the REMASC transaction's trace - /// (task 283): rskj's `RemascFeesPayer.payMiningFees` makes one in + /// REMASC's payments are CALL subtraces of the REMASC transaction's + /// trace: rskj's `RemascFeesPayer.payMiningFees` makes one in /// `transferPayment` for every payment it logs, from REMASC with gas 0, /// to the payee's word -- zero for the null address -- and /// `TransactionExecutor.call` keeps them for `extractTrace`. @@ -13367,7 +13367,7 @@ bf09f6e52420834e8e0e0b1a6df563aba550cf7f99e9724264187c45dcf3d73e8585a0e35196\ assert_eq!(bridge_balance, one_rbtc - burned, "burn debits the Bridge"); // rskj's `transferTo(BURN_ADDRESS, ...)` is a CALL subtrace of the - // transaction's trace, from the Bridge with gas 0 (task 283). + // transaction's trace, from the Bridge with gas 0. let (_, traces) = executor .execute_block_call_traced(&header, &[(tx, sender)], &root, store.clone()) .expect("block executes traced"); @@ -13392,7 +13392,7 @@ bf09f6e52420834e8e0e0b1a6df563aba550cf7f99e9724264187c45dcf3d73e8585a0e35196\ } /// A transaction sent straight to the Bridge carries the Bridge's - /// transfers as CALL subtraces (task 283): rskj's `BridgeSupport.transferTo` + /// transfers as CALL subtraces: rskj's `BridgeSupport.transferTo` /// adds one, `TransactionExecutor.call` keeps them once `execute` returns /// and `extractTrace` hangs them under the transaction's trace. Here the /// refund of what a sender other than the union bridge sent diff --git a/crates/execution/src/rsk_instructions.rs b/crates/execution/src/rsk_instructions.rs index f25f0a8..bf7870a 100644 --- a/crates/execution/src/rsk_instructions.rs +++ b/crates/execution/src/rsk_instructions.rs @@ -268,9 +268,10 @@ pub fn install( Instruction::new(rsk_selfbalance::, 5), ); } else { - // Pre-papyrus rskj throws invalid-opcode (all frame gas consumed); - // revm's spec-checked impls would halt NotActivated returning the - // remaining gas instead. + // Pre-papyrus rskj throws invalid-opcode (all frame gas consumed). + // revm's spec-checked impls would halt NotActivated, which spends the + // frame's gas too; the invalid-opcode handler halts as for any other + // opcode a height doesn't have (OpcodeNotFound). instructions.insert_instruction( opcode::CHAINID, Instruction::new(invalid_opcode::, 0), diff --git a/crates/rpc/src/tests.rs b/crates/rpc/src/tests.rs index df39dc9..26f16b2 100644 --- a/crates/rpc/src/tests.rs +++ b/crates/rpc/src/tests.rs @@ -149,7 +149,7 @@ fn mainnet_block_9257552() -> Header { } /// A block prints each property rskj's `BlockResultDTO` has, as `fromBlock` -/// fills them (task 284). Mainnet #9,257,552 against what +/// fills them. Mainnet #9,257,552 against what /// public-node.rsk.co answered for it: the merged-mining fields as held, /// the merged-mining hash with the fork-detection data its coinbase /// commits to in its last 12 bytes (RSKIP110), the paid fees, and the @@ -4159,7 +4159,7 @@ async fn test_eth_gas_price_falls_back_to_the_block_minimum() { assert_eq!(r.result.unwrap(), json!("0x3e252e0"), "59,240,000 x 1.1"); } -/// rskj's arithmetic, not an exact 1.1 (task 280): the minimum as a double, +/// rskj's arithmetic, not an exact 1.1: the minimum as a double, /// times 1.1 in doubles, read back through Java 17's `Double.toString`; and /// either answer printed as its low 64 bits (`Long.toHexString(longValue())`). #[tokio::test] @@ -4646,7 +4646,7 @@ fn setup_defaults_state(count: u32) -> (RpcState, tempfile::TempDir, Address, Ad (state, tmp, reader, burner) } -/// A call's defaults are rskj's (task 282, `CallArgumentsToByteArray`): a gas +/// A call's defaults are rskj's (`CallArgumentsToByteArray`): a gas /// price of 0, not the block's minimum, and `rpc.callGasCap`'s 10,000,000 gas, /// not the block's limit -- the cap on the gas a call names, too. An /// estimate runs at `rpc.gasEstimationCap`'s 6,800,000, whatever the call @@ -4693,8 +4693,8 @@ async fn a_calls_defaults_are_rskjs() { assert_eq!(resp.result, Some(json!("0x")), "{:?}", resp.error); } -/// A sent transaction's defaults are rskj's (task 282, -/// `TransactionArgumentsUtil.processArguments`): a gas price of 0, not the +/// A sent transaction's defaults are rskj's +/// (`TransactionArgumentsUtil.processArguments`): a gas price of 0, not the /// node's price, and the gas named as `gas`, else as `gasLimit`, else 90,000, /// not the head's whole gas limit. #[tokio::test] diff --git a/crates/sync/src/quota.rs b/crates/sync/src/quota.rs index 817874a..4cf83a0 100644 --- a/crates/sync/src/quota.rs +++ b/crates/sync/src/quota.rs @@ -1346,7 +1346,7 @@ mod tests { } // ----------------------------------------------------------------------- - // Values as rskj reads them: `long`s (task 279) + // Values as rskj reads them: `long`s // ----------------------------------------------------------------------- /// 2^63 + `n`, which rskj reads as `Long.MIN_VALUE + n`. diff --git a/crates/sync/src/tests.rs b/crates/sync/src/tests.rs index 753cf8f..74179a0 100644 --- a/crates/sync/src/tests.rs +++ b/crates/sync/src/tests.rs @@ -4043,7 +4043,7 @@ async fn a_block_a_peer_sends_is_handed_to_the_sync_service() { assert_eq!(got.hash(), block.hash()); } -// -- rskj's peer.bannedMiners (task 285) ------------------------------------ +// -- rskj's peer.bannedMiners ----------------------------------------------- /// A handler refusing blocks mined by `banned`, and its event channel. fn handler_banning( From 91b24c4da40ab956cf422baadcd5f65655f54146 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Tue, 10 Nov 2026 12:55:53 -0800 Subject: [PATCH 101/264] sync: the uncles held for a block are the ones its header commits to `held_uncles` reads a block's uncles from its body or, once the body is pruned, from the freezer, which keeps them by height alone. What either holds is now checked against the block's uncles hash. A list that doesn't hash to it is another block's, and the block counts as one whose uncles aren't held, with a warning: none of them credited to the total difficulty, none served with its header. --- crates/sync/src/tests.rs | 57 +++++++++++++++++++++++++++++ crates/sync/src/total_difficulty.rs | 26 ++++++++++--- 2 files changed, 77 insertions(+), 6 deletions(-) diff --git a/crates/sync/src/tests.rs b/crates/sync/src/tests.rs index 74179a0..9fe4468 100644 --- a/crates/sync/src/tests.rs +++ b/crates/sync/src/tests.rs @@ -3491,6 +3491,63 @@ async fn headers_with_uncles_are_served_from_the_freezer_once_the_body_is_gone() ); } +/// The uncles held for a block are the ones its header commits to. The +/// freezer keeps uncles by height alone, so what it holds for a height, or a +/// body held for a block, is checked against the uncles hash of the block +/// asked about: a list that doesn't hash to it is not that block's, and the +/// block counts as one whose uncles aren't held. +#[test] +fn the_uncles_held_for_a_block_are_the_ones_its_header_commits_to() { + use crate::total_difficulty::held_uncles; + + let dir = tempdir().unwrap(); + let store = BlockStore::open(dir.path()).unwrap(); + let freezer = + Arc::new(rustock_storage::freezer::Freezer::open(dir.path().join("freezer")).unwrap()); + store.set_freezer(freezer.clone()); + + let uncle = dummy_header(1, B256::repeat_byte(0x0c), U256::from(1)); + let other = dummy_header(1, B256::repeat_byte(0x0d), U256::from(1)); + let naming_the_uncle = |number: u64| { + let mut header = dummy_header(number, B256::repeat_byte(number as u8), U256::from(1)); + header.uncle_count = 1; + header.ommers_hash = + rustock_execution::processor::compute_ommers_hash(std::slice::from_ref(&uncle)); + header + }; + // #2 to #5 all name `uncle`. The freezer holds it for #2 and another + // block's uncle for #3; #4's body holds it, and #5's the other. + let blocks: Vec
= (2..=5).map(naming_the_uncle).collect(); + for header in &blocks { + store.put_header(header).unwrap(); + store + .put_canonical_hash(header.number, header.hash()) + .unwrap(); + } + freezer.put_uncles(2, std::slice::from_ref(&uncle)).unwrap(); + freezer.put_uncles(3, std::slice::from_ref(&other)).unwrap(); + freezer.sync().unwrap(); + store + .put_body(blocks[2].hash(), &[], std::slice::from_ref(&uncle)) + .unwrap(); + store + .put_body(blocks[3].hash(), &[], std::slice::from_ref(&other)) + .unwrap(); + + let held = |header: &Header| { + held_uncles(&store, header.hash(), header) + .map(|uncles| uncles.iter().map(Header::hash).collect::>()) + }; + assert_eq!( + held(&blocks[0]), + Some(vec![uncle.hash()]), + "the freezer's, its own" + ); + assert_eq!(held(&blocks[1]), None, "the freezer's, another block's"); + assert_eq!(held(&blocks[2]), Some(vec![uncle.hash()]), "its body's"); + assert_eq!(held(&blocks[3]), None, "a body with another block's uncles"); +} + #[tokio::test] async fn test_serve_skeleton_request_unknown_start() { use rustock_networking::protocol::{P2pMessage, RskMessage, RskSubMessage}; diff --git a/crates/sync/src/total_difficulty.rs b/crates/sync/src/total_difficulty.rs index cbfadf3..0ac93bc 100644 --- a/crates/sync/src/total_difficulty.rs +++ b/crates/sync/src/total_difficulty.rs @@ -76,15 +76,29 @@ impl AncestorSource for Held<'_> { /// The uncles a held block named: from its body, or once the body is pruned, /// from the freezer, which keeps them by height for the canonical chain. -/// `None` when neither holds them. +/// `None` when neither holds them, or when what is held is not the list the +/// block's header commits to (its uncles hash): the freezer's are found by +/// height alone, and a list that doesn't hash to the header's is another +/// block's. pub(crate) fn held_uncles(store: &BlockStore, hash: B256, header: &Header) -> Option> { - if let Ok(Some(uncles)) = store.uncles(hash) { - return Some(uncles); - } - if store.canonical_hash(header.number).ok().flatten() != Some(hash) { + let uncles = match store.uncles(hash) { + Ok(Some(uncles)) => uncles, + _ if store.canonical_hash(header.number).ok().flatten() == Some(hash) => { + store.freezer()?.uncles(header.number).ok().flatten()? + } + _ => return None, + }; + let hashed = rustock_execution::processor::compute_ommers_hash(&uncles); + if !header.has_ommers_hash(hashed) { + warn!( + target: "rustock::sync", + "Block #{} ({:?}): the uncles held for it are not the ones its header names", + header.number, + hash + ); return None; } - store.freezer()?.uncles(header.number).ok().flatten() + Some(uncles) } /// The difficulty of `uncles`, the uncles a block names, if they pass rskj's From ca23ab975395506b7229ab36b6bee29ca6aa29ef Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Tue, 10 Nov 2026 12:55:53 -0800 Subject: [PATCH 102/264] bridge: a federation of 16 or more writes its size as RSKj's release does RSKj's release builds a federation's multisig with bitcoinj-thin `v0.14.4-rsk-18`, whose `ScriptBuilder.number` writes 0 to 15 as OP_0 to OP_15 and 16 and up as a push (`bigNum`). rustock wrote every count as `0x50 + n`: OP_16 for 16, as `rsk-20` does, and opcodes that aren't numbers past it. A federation of 16 to 20 members, which the release allows, would have had a redeem script and an address of rustock's own. Thresholds stay opcodes: half of 20 members plus one is 11. --- crates/execution/src/bridge/peg.rs | 72 +++++++++++++++++++++++++++--- 1 file changed, 67 insertions(+), 5 deletions(-) diff --git a/crates/execution/src/bridge/peg.rs b/crates/execution/src/bridge/peg.rs index 30dd117..d71e910 100644 --- a/crates/execution/src/bridge/peg.rs +++ b/crates/execution/src/bridge/peg.rs @@ -5217,23 +5217,47 @@ fn p2pkh_output_script(hash160: &[u8; 20]) -> bitcoin::ScriptBuf { /// Build the federation P2SH redeem script from member public keys. /// -/// m-of-n multisig: OP_m ... OP_n OP_CHECKMULTISIG -/// Keys are sorted (matching bitcoinj's createRedeemScript behavior). +/// m-of-n multisig: ... OP_CHECKMULTISIG, the counts +/// written as bitcoinj-thin's `ScriptBuilder.number` writes them +/// ([`script_number`]). Keys are sorted (matching bitcoinj's +/// createRedeemScript behavior). pub fn build_federation_redeem_script(keys: &[[u8; 33]], threshold: usize) -> Vec { let mut sorted_keys = keys.to_vec(); sorted_keys.sort(); - let mut script = Vec::new(); - script.push(0x50 + threshold as u8); // OP_m + let mut script = script_number(threshold); for key in &sorted_keys { script.push(33); // OP_PUSHBYTES_33 script.extend_from_slice(key); } - script.push(0x50 + sorted_keys.len() as u8); // OP_n + script.extend_from_slice(&script_number(sorted_keys.len())); script.push(0xAE); // OP_CHECKMULTISIG script } +/// bitcoinj-thin's `ScriptBuilder.number` as RSKj's release has it +/// (`v0.14.4-rsk-18`): OP_0 to OP_15 for 0 to 15, and from 16 on the +/// number's minimal little-endian bytes as a push (`bigNum`). So a +/// federation of 16 to 20 members writes its size as `0x01 `, not as +/// OP_16 and the opcodes past it. `rsk-20` writes 16 as OP_16, but no +/// release of RSKj runs it. +fn script_number(n: usize) -> Vec { + if n < 16 { + // `Script.encodeToOpN`: OP_0, then OP_1 (0x51) to OP_15. + return vec![if n == 0 { 0x00 } else { 0x50 + n as u8 }]; + } + let mut bytes = (n as u64).to_le_bytes().to_vec(); + while bytes.last() == Some(&0) { + bytes.pop(); + } + if bytes.last().is_some_and(|b| b & 0x80 != 0) { + bytes.push(0x00); + } + let mut push = vec![bytes.len() as u8]; + push.extend_from_slice(&bytes); + push +} + /// rskj `Utils.signedLongToByteArrayLE`: `reverseBytes(BigInteger.valueOf(v) /// .toByteArray())` — the minimal big-endian two's-complement byte array /// (Java `BigInteger.toByteArray`, which keeps a leading 0x00 sign byte when @@ -6976,6 +7000,44 @@ mod tests { assert_eq!(script.len(), 1 + 3 * 34 + 2); } + /// A federation of 16 to 20 members: RSKj's release builds its multisig + /// with bitcoinj-thin `v0.14.4-rsk-18`, whose `ScriptBuilder.number` + /// writes 16 and up as a push (`bigNum`), so the size goes in as + /// `0x01 `. 15 is still OP_15, and so is every threshold up to 20 + /// members' 11. + #[test] + fn a_federation_of_16_or_more_writes_its_size_as_a_push() { + let keys: Vec<[u8; 33]> = (0..20u8) + .map(|i| { + let mut key = [0x02; 33]; + key[32] = i; + key + }) + .collect(); + for (n, size) in [ + (15, vec![0x5f]), + (16, vec![0x01, 0x10]), + (20, vec![0x01, 0x14]), + ] { + let threshold = n / 2 + 1; + let script = build_federation_redeem_script(&keys[..n], threshold); + assert_eq!( + script[0], + 0x50 + threshold as u8, + "{n} members: the threshold" + ); + let mut tail = size.clone(); + tail.push(0xae); + assert_eq!(&script[1 + n * 34..], &tail[..], "{n} members: the size"); + } + assert_eq!(script_number(0), vec![0x00]); + assert_eq!( + script_number(128), + vec![0x02, 0x80, 0x00], + "a sign byte past 0x7f" + ); + } + /// Redeem script hash160. #[test] fn rskj_redeem_script_hash160_non_zero() { From 3189a72ae6e95e6db5d14b4e8306bde2eb72fef1 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Thu, 12 Nov 2026 21:10:06 -0800 Subject: [PATCH 103/264] bridge: four more cases where bitcoinj's redeem-script parsers differ from the port `Script.isSentToMultiSig()` in bitcoinj-thin `v0.14.4-rsk-18` picks a redeem-script parser and asks it for M. The port of that dispatch disagreed with it in four places, each run through the jar the release pins and recorded in the fixture: - An ERP-shaped script whose `OP_ELSE` is third from the end. Both ERP validators accept an `OP_ELSE` with `chunks.size() >= i + 3` and then read `chunks.get(i + 3)`, one past the end: an `IndexOutOfBoundsException` nothing catches, where the port answered "not a multisig". `6467ae68` and `646768ae` are the shortest. - A flyover prefix is a hash pushed by the opcode 32 itself (`hasFlyoverPrefix`). The port took any push of 32 bytes, so the same prefix behind `OP_PUSHDATA1` made a multisig of a script bitcoinj doesn't know. - `decodePositiveN` refuses a negative number and nothing else, so an M pushed as zero passes the structure checks and selects a parser, and `getM() > 0` is then false. The port answered true for it. - The hard-coded testnet script is found with `List.equals`, and a `ScriptChunk` equals another only at the same offset in its program. Behind a flyover prefix it isn't found, and it parses as the non-standard ERP redeem it is, with M = 3. The port compared at every depth. --- .../src/bridge/rskj_sender_compat.rs | 280 +++++++++++++++--- 1 file changed, 245 insertions(+), 35 deletions(-) diff --git a/crates/execution/src/bridge/rskj_sender_compat.rs b/crates/execution/src/bridge/rskj_sender_compat.rs index ca46c81..91ff35e 100644 --- a/crates/execution/src/bridge/rskj_sender_compat.rs +++ b/crates/execution/src/bridge/rskj_sender_compat.rs @@ -114,7 +114,9 @@ pub enum Verdict { /// Demonstrated against the pinned artifact: a redeem script whose first /// chunk is `OP_0` reaches `decodePositiveNConsideringEncoding`, which /// indexes `data[data.length - 1]` on empty data and raises - /// `ArrayIndexOutOfBoundsException`. + /// `ArrayIndexOutOfBoundsException`. So does an ERP-shaped script whose + /// `OP_ELSE` is third from the end: both ERP validators read one chunk + /// past it, and `List.get` raises `IndexOutOfBoundsException`. RskjThrows(&'static str), } @@ -379,8 +381,11 @@ fn has_standard_structure(chunks: &[JChunk]) -> Result { Ok(true) } +/// `hasFlyoverPrefix`. The hash is pushed by the opcode 32 itself: 32 bytes +/// behind an `OP_PUSHDATA` opcode aren't a prefix. fn has_flyover_prefix(chunks: &[JChunk]) -> bool { chunks.len() > 2 + && chunks[0].opcode == 32 && chunks[0].data.as_ref().is_some_and(|d| d.len() == 32) && chunks[1].equals_op_code(OP_DROP) } @@ -389,44 +394,61 @@ fn has_flyover_structure(chunks: &[JChunk]) -> bool { has_flyover_prefix(chunks) && is_redeem_like_script(&chunks[2..]) } -/// The OP_ELSE scan shared by both ERP validators: the first `OP_ELSE` with -/// room for three more chunks, which must be a push, `OP_CSV` and `OP_DROP`. -fn find_erp_else(chunks: &[JChunk]) -> Option { +/// The OP_ELSE scan shared by both ERP validators: the first `OP_ELSE` that is +/// at least third from the end, whose next three chunks must be a push, +/// `OP_CSV` and `OP_DROP`. Java reads all three before it tests any, and its +/// bound lets the third lie one past the end: `Err` is the +/// `IndexOutOfBoundsException` that read raises. +fn find_erp_else(chunks: &[JChunk]) -> Result, &'static str> { for i in 1..chunks.len() { if chunks[i].equals_op_code(OP_ELSE) && chunks.len() >= i + 3 { - let ok = chunks.get(i + 1).is_some_and(|c| c.is_push_data()) - && chunks.get(i + 2).is_some_and(|c| c.equals_op_code(OP_CHECKSEQUENCEVERIFY)) - && chunks.get(i + 3).is_some_and(|c| c.equals_op_code(OP_DROP)); - return if ok { Some(i) } else { None }; + let Some(drop) = chunks.get(i + 3) else { + return Err("IndexOutOfBoundsException reading past an ERP script's OP_ELSE"); + }; + let ok = chunks[i + 1].is_push_data() + && chunks[i + 2].equals_op_code(OP_CHECKSEQUENCEVERIFY) + && drop.equals_op_code(OP_DROP); + return Ok(ok.then_some(i)); } } - None + Ok(None) } -fn has_p2sh_erp_structure(chunks: &[JChunk]) -> Result { +/// `hasP2shErpRedeemScriptStructure`. `Err` is an index error nothing catches, +/// named. +fn has_p2sh_erp_structure(chunks: &[JChunk]) -> Result { if !is_redeem_like_script(chunks) { return Ok(false); } if !chunks[0].equals_op_code(OP_NOTIF) || !chunks[chunks.len() - 1].equals_op_code(OP_ENDIF) { return Ok(false); } - let Some(else_index) = find_erp_else(chunks) else { return Ok(false) }; + let Some(else_index) = find_erp_else(chunks)? else { + return Ok(false); + }; let default_fed = &chunks[1..else_index]; if else_index + 4 > chunks.len() - 1 { return Ok(false); } let erp_fed = &chunks[else_index + 4..chunks.len() - 1]; - Ok(has_standard_structure(default_fed)? && has_standard_structure(erp_fed)?) + let standard = |fed: &[JChunk]| { + has_standard_structure(fed) + .map_err(|()| "ArrayIndexOutOfBoundsException decoding the P2SH-ERP inner redeem") + }; + Ok(standard(default_fed)? && standard(erp_fed)?) } -fn has_non_standard_erp_structure(chunks: &[JChunk]) -> Result { +/// `hasNonStandardErpRedeemScriptStructure`, with the same `Err`. +fn has_non_standard_erp_structure(chunks: &[JChunk]) -> Result { if !is_redeem_like_script(chunks) { return Ok(false); } if chunks[0].opcode != OP_NOTIF || !chunks[chunks.len() - 2].equals_op_code(OP_ENDIF) { return Ok(false); } - let Some(else_index) = find_erp_else(chunks) else { return Ok(false) }; + let Some(else_index) = find_erp_else(chunks)? else { + return Ok(false); + }; // Both halves are validated with OP_CHECKMULTISIG appended, because a // non-standard ERP redeem carries it once at the end rather than in each @@ -440,7 +462,12 @@ fn has_non_standard_erp_structure(chunks: &[JChunk]) -> Result { let mut erp_fed: Vec = chunks[else_index + 4..chunks.len() - 2].to_vec(); erp_fed.push(cms); - Ok(has_standard_structure(&default_fed)? && has_standard_structure(&erp_fed)?) + let standard = |fed: &[JChunk]| { + has_standard_structure(fed).map_err(|()| { + "ArrayIndexOutOfBoundsException decoding the non-standard ERP inner redeem" + }) + }; + Ok(standard(&default_fed)? && standard(&erp_fed)?) } // --- the dispatch ---------------------------------------------------------- @@ -449,10 +476,10 @@ fn has_non_standard_erp_structure(chunks: &[JChunk]) -> Result { /// /// Mirrors `RedeemScriptParserFactory.get` followed by `getM() > 0`. Every /// parser the factory can return delegates `getM()` inward to a standard -/// parser, whose M is `decodePositiveN(chunk 0)` and therefore at least 1 once -/// the structure check has passed -- so selecting a parser *is* the answer. -/// The single exception is the hardcoded testnet script, whose parser returns -/// -1 on purpose. +/// parser, whose M is `decodePositiveN(chunk 0)`. That refuses a negative +/// number and nothing else, so an M pushed as zero passes the structure check +/// and selects a parser, and the answer is then false. The hardcoded testnet +/// script's parser returns -1 on purpose. pub fn is_sent_to_multisig(redeem_script: &[u8]) -> Verdict { let Some(chunks) = parse_script_program(redeem_script) else { return Verdict::NotMultisig; // ScriptException, caught @@ -465,9 +492,13 @@ fn dispatch(chunks: &[JChunk], depth: usize) -> Verdict { return Verdict::NotMultisig; } // The hardcoded testnet script is compared before anything else, and its - // parser reports M = -1. - if let Some(hardcoded) = parse_script_program(&hex_decode(NON_STANDARD_ERP_TESTNET_REDEEM_SCRIPT)) { - if chunks == hardcoded.as_slice() { + // parser reports M = -1. bitcoinj compares the chunks with `List.equals`, + // and a chunk equals another only at the same offset in its program, so + // the script is matched as a whole program alone. Behind a flyover prefix + // its chunks start 34 bytes on, and it is parsed like any other script. + if depth == 0 { + let hardcoded = parse_script_program(&hex_decode(NON_STANDARD_ERP_TESTNET_REDEEM_SCRIPT)); + if hardcoded.is_some_and(|hardcoded| chunks == hardcoded.as_slice()) { return Verdict::NotMultisig; } } @@ -482,26 +513,32 @@ fn dispatch(chunks: &[JChunk], depth: usize) -> Verdict { Err(()) => return Verdict::RskjThrows( "ArrayIndexOutOfBoundsException decoding M or N from an empty push", ), - Ok(true) => return Verdict::Multisig, + Ok(true) => return positive_m(&chunks[0]), Ok(false) => {} } + // An ERP script's M is its default federation's, which follows OP_NOTIF. match has_p2sh_erp_structure(chunks) { - Err(()) => return Verdict::RskjThrows( - "ArrayIndexOutOfBoundsException decoding the P2SH-ERP inner redeem", - ), - Ok(true) => return Verdict::Multisig, + Err(why) => return Verdict::RskjThrows(why), + Ok(true) => return positive_m(&chunks[1]), Ok(false) => {} } match has_non_standard_erp_structure(chunks) { - Err(()) => return Verdict::RskjThrows( - "ArrayIndexOutOfBoundsException decoding the non-standard ERP inner redeem", - ), - Ok(true) => return Verdict::Multisig, + Err(why) => return Verdict::RskjThrows(why), + Ok(true) => return positive_m(&chunks[1]), Ok(false) => {} } Verdict::NotMultisig // factory throws "unknown redeem script", caught } +/// `getM() > 0` over the chunk the selected parser reads M from. The structure +/// check has decoded that chunk already, so only a zero is left to refuse. +fn positive_m(m: &JChunk) -> Verdict { + match m.decode_positive_n() { + Ok(m) if m > 0 => Verdict::Multisig, + _ => Verdict::NotMultisig, + } +} + // --- signature stripping ---------------------------------------------------- /// `BitcoinUtils.extractRedeemScriptFromInput`: the last push of an input's @@ -617,6 +654,39 @@ mod tests { s } + /// A flyover prefix, which is a hash pushed by the opcode 32 and `OP_DROP`, + /// in front of `inner`. + fn flyover(inner: &[u8]) -> Vec { + let mut s = vec![32]; + s.extend_from_slice(&[0xAB; 32]); + s.push(OP_DROP); + s.extend_from_slice(inner); + s + } + + /// What both ERP shapes carry between their two federations. + const ELSE_CSV_DROP: [u8; 6] = [OP_ELSE, 0x02, 0x00, 0x20, OP_CHECKSEQUENCEVERIFY, OP_DROP]; + + fn p2sh_erp(default_fed: &[u8], erp_fed: &[u8]) -> Vec { + let mut s = vec![OP_NOTIF]; + s.extend_from_slice(default_fed); + s.extend_from_slice(&ELSE_CSV_DROP); + s.extend_from_slice(erp_fed); + s.push(OP_ENDIF); + s + } + + /// The older ERP shape: neither multisig keeps its own `OP_CHECKMULTISIG`, + /// and one closes the script. + fn non_standard_erp(default_fed: &[u8], erp_fed: &[u8]) -> Vec { + let mut s = vec![OP_NOTIF]; + s.extend_from_slice(&default_fed[..default_fed.len() - 1]); + s.extend_from_slice(&ELSE_CSV_DROP); + s.extend_from_slice(&erp_fed[..erp_fed.len() - 1]); + s.extend_from_slice(&[OP_ENDIF, OP_CHECKMULTISIG]); + s + } + #[test] fn standard_multisig_is_multisig() { assert_eq!(is_sent_to_multisig(&standard_redeem(2, 3)), Verdict::Multisig); @@ -691,6 +761,113 @@ mod tests { ); } + /// Demonstrated against bitcoinj-thin 0.14.4-rsk-18: + /// new Script(hex("6467ae68")).isSentToMultiSig() + /// -> java.lang.IndexOutOfBoundsException: Index 4 out of bounds for length 4 + /// + /// Both ERP validators look for an `OP_ELSE` with `chunks.size() >= i + 3` + /// and then read `chunks.get(i + 3)`, so an `OP_ELSE` third from the end + /// has them read one chunk past it. + #[test] + fn an_op_else_third_from_the_end_makes_rskj_throw() { + let p2sh_erp_shape = [OP_NOTIF, OP_ELSE, OP_CHECKMULTISIG, OP_ENDIF]; + let non_standard_shape = [OP_NOTIF, OP_ELSE, OP_ENDIF, OP_CHECKMULTISIG]; + // A real default federation in front of the OP_ELSE changes nothing. + let mut after_a_federation = vec![OP_NOTIF]; + after_a_federation.extend_from_slice(&standard_redeem(2, 3)); + after_a_federation.extend_from_slice(&[OP_ELSE, OP_CHECKMULTISIG, OP_ENDIF]); + for script in [ + &p2sh_erp_shape[..], + &non_standard_shape[..], + &after_a_federation, + &flyover(&p2sh_erp_shape), + ] { + assert!( + matches!(is_sent_to_multisig(script), Verdict::RskjThrows(_)), + "{script:02x?}" + ); + } + + // With one more chunk after the OP_ELSE the read stays inside. + let inside = [OP_NOTIF, OP_1, OP_ELSE, OP_1, OP_ENDIF, OP_CHECKMULTISIG]; + assert_eq!(is_sent_to_multisig(&inside), Verdict::NotMultisig); + } + + /// `hasFlyoverPrefix` wants the hash pushed by the opcode 32 itself. Pushed + /// with `OP_PUSHDATA1` or `OP_PUSHDATA2`, the same 32 bytes aren't a flyover + /// prefix, and the script is no redeem script bitcoinj knows. + #[test] + fn a_flyover_prefix_is_a_direct_push_of_32_bytes() { + let wrapped = flyover(&standard_redeem(2, 3)); + assert_eq!(is_sent_to_multisig(&wrapped), Verdict::Multisig); + + let mut pushdata1 = vec![OP_PUSHDATA1]; + pushdata1.extend_from_slice(&wrapped); + assert_eq!(is_sent_to_multisig(&pushdata1), Verdict::NotMultisig); + + let mut pushdata2 = vec![OP_PUSHDATA2, 32, 0]; + pushdata2.extend_from_slice(&wrapped[1..]); + assert_eq!(is_sent_to_multisig(&pushdata2), Verdict::NotMultisig); + } + + /// `decodePositiveN` refuses a negative number and nothing else, so the + /// structure checks pass an M pushed as zero and a parser is selected. + /// `getM() > 0` is then false. + #[test] + fn an_m_pushed_as_zero_is_not_multisig() { + let zero_of_three = |push: &[u8]| { + let mut s = push.to_vec(); + s.extend_from_slice(&standard_redeem(2, 3)[1..]); + s + }; + let standard = zero_of_three(&[0x01, 0x00]); + let one_of_two = standard_redeem(1, 2); + for script in [ + standard.clone(), + zero_of_three(&[0x02, 0x00, 0x00]), + flyover(&standard), + p2sh_erp(&standard, &one_of_two), + non_standard_erp(&standard, &one_of_two), + ] { + assert_eq!( + is_sent_to_multisig(&script), + Verdict::NotMultisig, + "{script:02x?}" + ); + } + // bitcoinj: IllegalStateException("Unsupported script type"). + assert!(signatures_required_to_spend(&standard).is_err()); + + // An ERP script's M is its default federation's. The emergency + // multisig's isn't read. + let erp_side_only = p2sh_erp(&standard_redeem(2, 3), &zero_of_three(&[0x01, 0x00])); + assert_eq!(is_sent_to_multisig(&erp_side_only), Verdict::Multisig); + assert_eq!(signatures_required_to_spend(&erp_side_only), Ok(2)); + } + + /// The factory compares the hardcoded script with `List.equals`, and a + /// `ScriptChunk` equals another only at the same offset in its program. + /// Behind a flyover prefix every chunk starts 34 bytes later, so the script + /// isn't matched there, and it parses as the non-standard ERP redeem it is, + /// with M = 3. + #[test] + fn the_hardcoded_testnet_script_is_matched_only_as_the_whole_program() { + let script = hex_decode(NON_STANDARD_ERP_TESTNET_REDEEM_SCRIPT); + assert_eq!(is_sent_to_multisig(&script), Verdict::NotMultisig); + assert!(signatures_required_to_spend(&script).is_err()); + + let wrapped = flyover(&script); + assert_eq!(is_sent_to_multisig(&wrapped), Verdict::Multisig); + assert_eq!(signatures_required_to_spend(&wrapped), Ok(3)); + } + + #[test] + fn a_non_standard_erp_redeem_is_multisig() { + let script = non_standard_erp(&standard_redeem(2, 3), &standard_redeem(1, 2)); + assert_eq!(is_sent_to_multisig(&script), Verdict::Multisig); + assert_eq!(signatures_required_to_spend(&script), Ok(2)); + } + /// bitcoinj's decodePositiveN accepts a PUSHED number as well as /// OP_1..OP_16, which a shape check that only looks for the opcodes misses. #[test] @@ -770,6 +947,22 @@ mod tests { Verdict::NotMultisig), ("hardcoded testnet script", NON_STANDARD_ERP_TESTNET_REDEEM_SCRIPT, Verdict::NotMultisig), + ("non-standard erp", + "64522102020202020202020202020202020202020202020202020202020202020202020021020202020202020202020202020202020202020202020202020202020202020201210202020202020202020202020202020202020202020202020202020202020202025367020020b2755121020202020202020202020202020202020202020202020202020202020202020200210202020202020202020202020202020202020202020202020202020202020202015268ae", + Verdict::Multisig), + ("p2sh-erp, CSV value pushed as OP_16", + "645221020202020202020202020202020202020202020202020202020202020202020200210202020202020202020202020202020202020202020202020202020202020202012102020202020202020202020202020202020202020202020202020202020202020253ae6760b27551210202020202020202020202020202020202020202020202020202020202020202002102020202020202020202020202020202020202020202020202020202020202020152ae68", + Verdict::Multisig), + ("flyover prefix pushed with OP_PUSHDATA1", + "4c20abababababababababababababababababababababababababababababababab755221020202020202020202020202020202020202020202020202020202020202020200210202020202020202020202020202020202020202020202020202020202020202012102020202020202020202020202020202020202020202020202020202020202020253ae", + Verdict::NotMultisig), + ("M pushed as zero", + "010021020202020202020202020202020202020202020202020202020202020202020200210202020202020202020202020202020202020202020202020202020202020202012102020202020202020202020202020202020202020202020202020202020202020253ae", + Verdict::NotMultisig), + ("p2sh-erp, an OP_ELSE inside the default federation", + "645221020202020202020202020202020202020202020202020202020202020202020200672102020202020202020202020202020202020202020202020202020202020202020253ae67020020b27551210202020202020202020202020202020202020202020202020202020202020202002102020202020202020202020202020202020202020202020202020202020202020152ae68", + Verdict::NotMultisig), + ("OP_ELSE fourth from the end", "6451675168ae", Verdict::NotMultisig), ]; for (label, hex, expected) in vectors { @@ -777,14 +970,31 @@ mod tests { assert_eq!(&got, expected, "diverged from bitcoinj-thin on: {label}"); } + // The hardcoded testnet script behind a flyover prefix: true, and + // getNumberOfSignaturesRequiredToSpend() is 3. + let mut wrapped = + hex_decode("20abababababababababababababababababababababababababababababababab75"); + wrapped.extend_from_slice(&hex_decode(NON_STANDARD_ERP_TESTNET_REDEEM_SCRIPT)); + assert_eq!(is_sent_to_multisig(&wrapped), Verdict::Multisig); + assert_eq!(signatures_required_to_spend(&wrapped), Ok(3)); + // Recorded separately because the Java outcome is an exception rather // than a return value: // new Script(hex("0001aa01bbae")).isSentToMultiSig() // -> java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 0 - assert!( - matches!(is_sent_to_multisig(&hex_decode("0001aa01bbae")), Verdict::RskjThrows(_)), - "diverged from bitcoinj-thin on: OP_0 as M (Java throws)" - ); + // new Script(hex("6467ae68")).isSentToMultiSig() + // -> java.lang.IndexOutOfBoundsException: Index 4 out of bounds for length 4 + // new Script(hex("646768ae")).isSentToMultiSig() + // -> java.lang.IndexOutOfBoundsException: Index 4 out of bounds for length 4 + for hex in ["0001aa01bbae", "6467ae68", "646768ae"] { + assert!( + matches!( + is_sent_to_multisig(&hex_decode(hex)), + Verdict::RskjThrows(_) + ), + "diverged from bitcoinj-thin on: {hex} (Java throws)" + ); + } } #[test] From 7dc40105db5956c11be9cc07f8152886f34d8c81 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Thu, 12 Nov 2026 21:10:06 -0800 Subject: [PATCH 104/264] bridge: registerBtcCoinbaseTransaction takes a coinbase bitcoinj verifies, and nothing else RSKj parses the transaction, runs bitcoinj's `BtcTransaction.verify()` on it, and `findWitnessCommitment` then opens with `Preconditions.checkState(tx.isCoinBase())` (BridgeSupport l.2559-2562, BitcoinUtils l.309-324). rustock only parsed it. Any transaction of a Bitcoin block could therefore name that block's witness root, with an output shaped like a commitment, and a segwit peg-in nobody made could then be proved against the root: rBTC that RSKj doesn't credit. `verify()` is ported: no inputs or outputs, over 1,000,000 bytes, an outpoint spent twice, a negative output value or more than 21 million bitcoins out, a coinbase script outside 2 to 100 bytes, a coinbase input in a transaction that isn't one. Before RSKIP460 (lovell700) RSKj also parsed each output's script on its way to the commitment, last output first, and one bitcoinj can't parse threw: `has_rskip460` joins the schedule. Corrects the release's line numbers in the function's comments, and the note that RSKIP460 isn't active: it has been since lovell700. --- .../src/bridge/rskj_sender_compat.rs | 6 + crates/execution/src/bridge/tx.rs | 327 ++++++++++++++++-- crates/execution/src/hardfork.rs | 7 + 3 files changed, 319 insertions(+), 21 deletions(-) diff --git a/crates/execution/src/bridge/rskj_sender_compat.rs b/crates/execution/src/bridge/rskj_sender_compat.rs index 91ff35e..5873e52 100644 --- a/crates/execution/src/bridge/rskj_sender_compat.rs +++ b/crates/execution/src/bridge/rskj_sender_compat.rs @@ -333,6 +333,12 @@ fn parse_script_program(program: &[u8]) -> Option> { Some(chunks) } +/// Whether bitcoinj parses `program` as a script. `new Script(program)` throws +/// a `ScriptException` for one it can't: a push that runs past the end. +pub fn parses_as_script(program: &[u8]) -> bool { + parse_script_program(program).is_some() +} + // --- RedeemScriptValidator ------------------------------------------------- fn is_redeem_like_script(chunks: &[JChunk]) -> bool { diff --git a/crates/execution/src/bridge/tx.rs b/crates/execution/src/bridge/tx.rs index 897f5d0..fc06b4c 100644 --- a/crates/execution/src/bridge/tx.rs +++ b/crates/execution/src/bridge/tx.rs @@ -224,7 +224,7 @@ pub fn register_btc_coinbase_transaction( })?; // Check that the tx hash is among the matched hashes. rskj - // (BridgeSupport.registerBtcCoinbaseTransaction l.2503-2511) logs a + // (BridgeSupport.registerBtcCoinbaseTransaction l.2505-2513) logs a // warning and `return`s — it does NOT throw — so the call SUCCEEDS as a // no-op (no coinbase info stored). A throw here would mark the summary // failed and bill the full gas limit instead of gasUsed. @@ -243,7 +243,7 @@ pub fn register_btc_coinbase_transaction( *raw.as_byte_array() }; - // rskj (BridgeSupport.registerBtcCoinbaseTransaction l.2546-2555) logs the + // rskj (BridgeSupport.registerBtcCoinbaseTransaction l.2548-2557) logs the // panic message and `return`s on a merkle-root mismatch — it does NOT // throw. The call SUCCEEDS as a no-op (no coinbase info stored), so the // summary is NOT marked failed and the sender is billed gasUsed, not the @@ -252,13 +252,22 @@ pub fn register_btc_coinbase_transaction( return Ok(PrecompileOutput::new(gas_cost, Bytes::new())); } - // rskj verifies the tx parses and is internally consistent - // (`btcTx.verify()`, BridgeSupport l.2557-2558). A parse failure throws. + // rskj parses the transaction and runs bitcoinj's `btcTx.verify()` on it + // (BridgeSupport l.2559-2560). Either throws. let btc_tx: bitcoin::Transaction = bitcoin::consensus::deserialize(&btc_tx_data) .map_err(|_| PrecompileError::other("registerBtcCoinbaseTransaction: invalid btc tx"))?; + verify_btc_transaction(&btc_tx, btc_tx_data.len()).map_err(PrecompileError::other)?; + + // `findWitnessCommitment` opens with `Preconditions.checkState(tx.isCoinBase())`: + // only a block's coinbase names its witness root. + if !btc_tx.is_coinbase() { + return Err(PrecompileError::other( + "registerBtcCoinbaseTransaction: the transaction isn't a coinbase", + )); + } // Validate the witness commitment (rskj `validateWitnessInformation`, - // BridgeSupport l.2568-2587). The commitment embedded in the coinbase tx + // BridgeSupport l.2570-2589). The commitment embedded in the coinbase tx // must equal SHA256d(witnessMerkleRoot.getReversedBytes() || witnessReservedValue). // The ABI arg is the witness merkle root in display order (bitcoinj // `Sha256Hash.wrap` keeps the bytes as given), and the commitment hashes it @@ -270,11 +279,14 @@ pub fn register_btc_coinbase_transaction( let calculated_commitment = *sha256d::Hash::hash(&commitment_data).as_byte_array(); // If the embedded commitment is missing or does not match, rskj throws - // `BridgeIllegalArgumentException` (BridgeSupport l.2576-2584), which + // `BridgeIllegalArgumentException` (BridgeSupport l.2578-2587), which // `Bridge.execute` wraps in a `VMException` — the call FAILS (no store), // unlike the no-op `return` paths above. A `PrecompileError::other` here is // translated by the bridge dispatch into the same VMException gas semantics. - match find_witness_commitment(&btc_tx) { + let block_number = revm::context_interface::Block::number(ctx.block()).to::(); + let found = find_witness_commitment(&btc_tx, !hardfork_cfg.has_rskip460(block_number)) + .map_err(PrecompileError::other)?; + match found { Some(expected) if expected == calculated_commitment => {} _ => { return Err(PrecompileError::other( @@ -478,25 +490,89 @@ const MINIMUM_WITNESS_COMMITMENT_SIZE: usize = 38; /// Find the witness commitment hash embedded in a coinbase transaction's /// outputs, mirroring rskj `BitcoinUtils.findWitnessCommitment` -/// (rskj-core .../peg/bitcoin/BitcoinUtils.java l.300-357). +/// (rskj-core .../peg/bitcoin/BitcoinUtils.java l.309-365). /// /// The commitment lives in an output whose scriptPubKey is /// `OP_RETURN aa21a9ed <32-byte commitment>`. If several such /// outputs exist, BIP141 takes the LAST one, so rskj iterates the outputs /// reversed and returns the first match. Returns the 32-byte commitment in -/// internal byte order (bitcoinj `Sha256Hash.wrap`). Pre-RSKIP460 rskj parses -/// `getScriptPubKey().getProgram()`; for a well-formed commitment output that -/// is byte-identical to the raw script bytes used here. RSKIP460 is not active -/// over the current sync range. -fn find_witness_commitment(tx: &bitcoin::Transaction) -> Option<[u8; 32]> { - tx.output.iter().rev().find_map(|out| { +/// internal byte order (bitcoinj `Sha256Hash.wrap`). +/// +/// Before RSKIP460 rskj reads each script with `getScriptPubKey().getProgram()`, +/// which answers the same bytes but first parses them, and throws a +/// `ScriptException` for a script bitcoinj can't parse: the `Err`. The scan +/// stops at the commitment, so outputs before it are never parsed. +fn find_witness_commitment( + tx: &bitcoin::Transaction, + before_rskip460: bool, +) -> Result, &'static str> { + for out in tx.output.iter().rev() { let spk = out.script_pubkey.as_bytes(); - (spk.len() >= MINIMUM_WITNESS_COMMITMENT_SIZE + if before_rskip460 && !super::rskj_sender_compat::parses_as_script(spk) { + return Err( + "registerBtcCoinbaseTransaction: ScriptException parsing an output's script", + ); + } + if spk.len() >= MINIMUM_WITNESS_COMMITMENT_SIZE && spk[0] == OP_RETURN && spk[1] == WITNESS_COMMITMENT_LENGTH - && spk[2..6] == WITNESS_COMMITMENT_HEADER) - .then(|| spk[6..MINIMUM_WITNESS_COMMITMENT_SIZE].try_into().unwrap()) - }) + && spk[2..6] == WITNESS_COMMITMENT_HEADER + { + return Ok(spk[6..MINIMUM_WITNESS_COMMITMENT_SIZE].try_into().ok()); + } + } + Ok(None) +} + +/// bitcoinj `BtcBlock.MAX_BLOCK_SIZE`, which `BtcTransaction.verify()` holds a +/// transaction's serialized size to. +const MAX_BLOCK_SIZE: usize = 1_000_000; + +/// bitcoinj `NetworkParameters.MAX_MONEY`, in satoshis. +const MAX_MONEY: i64 = 21_000_000 * 100_000_000; + +/// bitcoinj `BtcTransaction.verify()`. Every failure is an exception +/// `Bridge.execute` rethrows. `serialized_size` is the transaction's +/// `getMessageSize()`: the bytes it was parsed from. +fn verify_btc_transaction( + tx: &bitcoin::Transaction, + serialized_size: usize, +) -> Result<(), &'static str> { + if tx.input.is_empty() || tx.output.is_empty() { + return Err("bitcoinj verify: no inputs or no outputs"); + } + if serialized_size > MAX_BLOCK_SIZE { + return Err("bitcoinj verify: larger than a block"); + } + let mut outpoints = std::collections::HashSet::new(); + if !tx + .input + .iter() + .all(|input| outpoints.insert(input.previous_output)) + { + return Err("bitcoinj verify: an outpoint spent twice"); + } + // bitcoinj reads a value as a signed 64-bit number, and holds the running + // sum to the 21 million bitcoins after each output. + let mut value_out: i64 = 0; + for output in &tx.output { + let value = output.value.to_sat() as i64; + if value < 0 { + return Err("bitcoinj verify: a negative output value"); + } + value_out = match value_out.checked_add(value) { + Some(sum) if sum <= MAX_MONEY => sum, + _ => return Err("bitcoinj verify: more output value than there is money"), + }; + } + if tx.is_coinbase() { + if !(2..=100).contains(&tx.input[0].script_sig.len()) { + return Err("bitcoinj verify: coinbase script size out of range"); + } + } else if tx.input.iter().any(|input| input.previous_output.is_null()) { + return Err("bitcoinj verify: a coinbase input in a transaction that isn't one"); + } + Ok(()) } fn encode_int_output(gas_cost: u64, value: i64) -> PrecompileOutput { @@ -630,7 +706,9 @@ mod tests { let tx: bitcoin::Transaction = bitcoin::consensus::deserialize(&raw_tx).unwrap(); // The embedded commitment (32 bytes after the aa21a9ed header), internal order. - let expected = find_witness_commitment(&tx).expect("coinbase has a witness commitment"); + let expected = find_witness_commitment(&tx, false) + .unwrap() + .expect("coinbase has a witness commitment"); assert_eq!( to_hex(&expected), "feb3b9170ae765cc6586edd67229eaa8bc19f9674d64cb10ee8a205f4ccf0bc6" @@ -669,7 +747,7 @@ mod tests { ) .unwrap(); let tx: bitcoin::Transaction = bitcoin::consensus::deserialize(&raw_tx).unwrap(); - let expected = find_witness_commitment(&tx).unwrap(); + let expected = find_witness_commitment(&tx, false).unwrap().unwrap(); // Tweak the last byte of the witness merkle root -> commitment must differ. let mut witness_merkle_root: [u8; 32] = @@ -720,6 +798,211 @@ mod tests { ); } + /// The witness root the registrations below name, as the ABI carries it. + const WITNESS_ROOT: [u8; 32] = [0x77; 32]; + + /// The output that commits to `WITNESS_ROOT` with a reserved value of zeros. + fn commitment_output() -> bitcoin::TxOut { + let mut preimage = [0u8; 64]; + preimage[..32].copy_from_slice(&WITNESS_ROOT); + preimage[..32].reverse(); + let mut script = vec![OP_RETURN, WITNESS_COMMITMENT_LENGTH]; + script.extend_from_slice(&WITNESS_COMMITMENT_HEADER); + script.extend_from_slice(sha256d::Hash::hash(&preimage).as_byte_array()); + output(0, script) + } + + fn output(sats: u64, script: Vec) -> bitcoin::TxOut { + bitcoin::TxOut { + value: bitcoin::Amount::from_sat(sats), + script_pubkey: bitcoin::ScriptBuf::from_bytes(script), + } + } + + /// A serialized transaction of one input, which spends `previous_output`. + fn one_input_tx( + previous_output: bitcoin::OutPoint, + script_sig: Vec, + output: Vec, + ) -> Vec { + bitcoin::consensus::serialize(&bitcoin::Transaction { + version: bitcoin::transaction::Version::TWO, + lock_time: bitcoin::absolute::LockTime::ZERO, + input: vec![bitcoin::TxIn { + previous_output, + script_sig: bitcoin::ScriptBuf::from_bytes(script_sig), + sequence: bitcoin::Sequence::MAX, + witness: bitcoin::Witness::new(), + }], + output, + }) + } + + fn coinbase(script_sig: Vec, output: Vec) -> Vec { + one_input_tx(bitcoin::OutPoint::null(), script_sig, output) + } + + /// Calls `registerBtcCoinbaseTransaction` at mainnet's `block_number` with + /// `raw_tx` as the only transaction of a stored Bitcoin block, naming + /// `WITNESS_ROOT`. Answers whether the call returned, and whether it stored + /// the block's coinbase information. + fn register_sole_transaction(raw_tx: &[u8], block_number: u64) -> (bool, bool) { + use crate::abi::encode::{self, Arg}; + use revm::MainContext; + let config = super::super::constants::BridgeConstants::mainnet(); + let cfg = crate::hardfork::RskHardforkConfig::mainnet(); + let mut ctx = + revm::Context::mainnet().with_chain(crate::raw_storage::RskChainExt::default()); + ctx.block.number = U256::from(block_number); + + let tx_hash = calculate_btc_tx_hash(raw_tx); + let header = bitcoin::block::Header { + version: bitcoin::block::Version::TWO, + prev_blockhash: bitcoin::BlockHash::all_zeros(), + merkle_root: bitcoin::TxMerkleNode::from_byte_array(tx_hash), + time: 1_700_000_000, + bits: bitcoin::CompactTarget::from_consensus(0x1d00_ffff), + nonce: 0, + }; + let block = super::super::btc_store::StoredBlock::new(header, 800_000, U256::from(1)); + super::super::btc_store::put_stored_block( + &mut ctx, + &block, + cfg.has_stored_block_v2(block_number), + ); + let mut block_hash = block.block_hash().to_byte_array(); + block_hash.reverse(); + + // A tree of the one transaction: its hash is the root. + let mut pmt = 1u32.to_le_bytes().to_vec(); + pmt.push(1); + pmt.extend_from_slice(&tx_hash); + pmt.extend_from_slice(&[1, 0x01]); + let args = encode::args(&[ + Arg::Bytes(raw_tx), + Arg::Word(block_hash), + Arg::Bytes(&pmt), + Arg::Word(WITNESS_ROOT), + Arg::Word([0; 32]), + ]); + + let returned = register_btc_coinbase_transaction(&mut ctx, &args, 0, &config, &cfg).is_ok(); + (returned, has_coinbase_information(&mut ctx, &block_hash)) + } + + /// Mainnet heights on either side of lovell700 (7,338,024), where RSKIP460 + /// activates. + const BEFORE_RSKIP460: u64 = 7_000_000; + const AFTER_RSKIP460: u64 = 7_400_000; + + const REGISTERED: (bool, bool) = (true, true); + const THROWN: (bool, bool) = (false, false); + + #[test] + fn a_coinbase_whose_commitment_matches_is_registered() { + let tx = coinbase( + vec![0x51, 0x51], + vec![output(50, vec![0x51]), commitment_output()], + ); + assert_eq!(register_sole_transaction(&tx, BEFORE_RSKIP460), REGISTERED); + assert_eq!(register_sole_transaction(&tx, AFTER_RSKIP460), REGISTERED); + } + + /// rskj's `findWitnessCommitment` opens with + /// `Preconditions.checkState(tx.isCoinBase())`. Without it any transaction + /// of a block could name the block's witness root, with an output that + /// looks like a commitment, and a peg-in nobody made could then be proved + /// against that root. + #[test] + fn a_transaction_that_is_not_a_coinbase_names_no_witness_root() { + let spends = |previous_output| { + one_input_tx(previous_output, vec![0x51, 0x51], vec![commitment_output()]) + }; + let payment = bitcoin::OutPoint { + txid: bitcoin::Txid::from_byte_array([0xaa; 32]), + vout: 0, + }; + // A null hash with any index but 0xffffffff isn't a coinbase's either. + let null_hash = bitcoin::OutPoint { + txid: bitcoin::Txid::all_zeros(), + vout: 0, + }; + for block_number in [BEFORE_RSKIP460, AFTER_RSKIP460] { + assert_eq!( + register_sole_transaction(&spends(payment), block_number), + THROWN + ); + assert_eq!( + register_sole_transaction(&spends(null_hash), block_number), + THROWN + ); + } + } + + /// rskj runs bitcoinj's `BtcTransaction.verify()` on the transaction before + /// it looks for the commitment. Each row was run through bitcoinj-thin + /// `0.14.4-rsk-18`, which threw the exception named. + #[test] + fn a_coinbase_bitcoinj_does_not_verify_is_refused() { + let commits = |script_sig: Vec, first: bitcoin::TxOut| { + coinbase(script_sig, vec![first, commitment_output()]) + }; + let max_money = 21_000_000 * 100_000_000u64; + let refused = [ + // CoinbaseScriptSizeOutOfRange: 2 to 100 bytes. + commits(vec![0x51], output(50, vec![0x51])), + commits(vec![0x51; 101], output(50, vec![0x51])), + // NegativeValueOutput: the value is a signed 64-bit number. + commits(vec![0x51, 0x51], output(u64::MAX, vec![0x51])), + // ExcessiveValue: more than 21 million bitcoins. + commits(vec![0x51, 0x51], output(max_money + 1, vec![0x51])), + // LargerThanMaxBlockSize: over 1,000,000 bytes serialized. + commits(vec![0x51, 0x51], output(50, vec![0x51; 1_000_000])), + ]; + for tx in &refused { + assert_eq!(register_sole_transaction(tx, AFTER_RSKIP460), THROWN); + } + + let accepted = [ + commits(vec![0x51; 100], output(50, vec![0x51])), + commits(vec![0x51, 0x51], output(max_money, vec![0x51])), + ]; + for tx in &accepted { + assert_eq!(register_sole_transaction(tx, AFTER_RSKIP460), REGISTERED); + } + } + + /// Before RSKIP460 rskj reads each output's script with + /// `getScriptPubKey().getProgram()`, which throws a `ScriptException` for + /// one bitcoinj can't parse. It reads them last to first and stops at the + /// commitment, so only an output after the commitment is reached. + #[test] + fn before_rskip460_a_script_bitcoinj_cannot_parse_is_refused() { + // OP_PUSHDATA1 with no length byte after it. + let unparseable = || output(1, vec![0x4c]); + let after_the_commitment = + coinbase(vec![0x51, 0x51], vec![commitment_output(), unparseable()]); + assert_eq!( + register_sole_transaction(&after_the_commitment, BEFORE_RSKIP460), + THROWN + ); + assert_eq!( + register_sole_transaction(&after_the_commitment, AFTER_RSKIP460), + REGISTERED + ); + + let before_the_commitment = + coinbase(vec![0x51, 0x51], vec![unparseable(), commitment_output()]); + assert_eq!( + register_sole_transaction(&before_the_commitment, BEFORE_RSKIP460), + REGISTERED + ); + assert_eq!( + register_sole_transaction(&before_the_commitment, AFTER_RSKIP460), + REGISTERED + ); + } + /// `find_witness_commitment` must take the LAST matching output (BIP141 / rskj /// `findWitnessCommitment` iterates outputs reversed) and ignore non-commitment /// outputs. @@ -753,7 +1036,9 @@ mod tests { mk(&commitment_b), ], }; - assert_eq!(find_witness_commitment(&tx), Some(commitment_b)); + // Parsing the scripts first, as before RSKIP460, finds the same one. + assert_eq!(find_witness_commitment(&tx, false), Ok(Some(commitment_b))); + assert_eq!(find_witness_commitment(&tx, true), Ok(Some(commitment_b))); } /// Ground truth: rskj `BridgeSupport.java:90-94`. These integers are the diff --git a/crates/execution/src/hardfork.rs b/crates/execution/src/hardfork.rs index 9214ed3..248e03e 100644 --- a/crates/execution/src/hardfork.rs +++ b/crates/execution/src/hardfork.rs @@ -805,6 +805,13 @@ impl RskHardforkConfig { self.active_upgrade(block_number) >= RskNetworkUpgrade::Lovell700 } + /// RSKIP460 (Lovell700): the Bridge reads a Bitcoin output's script bytes + /// as they are when it looks for a coinbase's witness commitment. Before + /// it, it parsed each script first, and one bitcoinj can't parse threw. + pub fn has_rskip460(&self, block_number: u64) -> bool { + self.active_upgrade(block_number) >= RskNetworkUpgrade::Lovell700 + } + /// RSKIP459: mark rejected non-refundable peg-ins as processed (Lovell700). /// Disabled again by RSKIP551 at Vetiver900. pub fn has_rskip459(&self, block_number: u64) -> bool { From 8509bbaaa816a90780f92765f9b26980c18e43b7 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Thu, 12 Nov 2026 21:10:06 -0800 Subject: [PATCH 105/264] bridge: addSignature and the old registerBtcTransaction are for federation members RSKj gives four Bridge methods an executor that checks the caller against the federations and throws a `VMException` for anyone else (BridgeMethods.java, Bridge.java l.1575-1616). rustock checked two of them, `updateCollections` and `receiveHeaders`. The other two ran for any caller: - `addSignature`, at every height, is for a member of the active, the retiring or, from RSKIP419, the proposed federation (`activeRetiringAndProposedFederationOnly`). - `registerBtcTransaction`, until RSKIP199 makes it public, is for a member of the active or the retiring federation. A stranger's well-formed call returned as if it had run where RSKj throws, so one transaction from any account gave a different receipt and state. --- crates/execution/src/bridge/mod.rs | 196 +++++++++++++++++++++++++---- crates/execution/src/bridge/peg.rs | 31 ++++- 2 files changed, 201 insertions(+), 26 deletions(-) diff --git a/crates/execution/src/bridge/mod.rs b/crates/execution/src/bridge/mod.rs index 2fe90c3..ce7e72a 100644 --- a/crates/execution/src/bridge/mod.rs +++ b/crates/execution/src/bridge/mod.rs @@ -823,32 +823,46 @@ fn execute_method( call_value: U256, ) -> Result { - // rskj `Bridge.activeAndRetiringFederationOnly` authorization. The executor - // for `updateCollections` (always) and `receiveHeaders` (when not public, - // i.e. post-RSKIP200) throws a `VMException` when the call's sender is not a - // member of the active or retiring federation. Bridge.execute wraps that in - // a VMException which `executePrecompiledAndHandleError` turns into a - // rollback + stackPushZero (the call returns 0 / empty data) while still - // charging the full `requiredGas`. A relay contract forwarding the call is - // checked by its own address, so a non-federation caller is rejected with no - // state change and no event (mainnet #6,223,774 tx[1]: updateCollections via - // relay 0x82494fb1 must NOT emit the update_collections log). - let federation_only = match method_name { - "updateCollections" => true, + // rskj wraps four methods' executors in a check of the caller against the + // federations (BridgeMethods.java). `activeAndRetiringFederationOnly` + // guards `updateCollections` always, `receiveHeaders` when it isn't public + // and `registerBtcTransaction` until RSKIP199 makes it public. + // `activeRetiringAndProposedFederationOnly` guards `addSignature` at every + // height. Each throws a plain `VMException` for anyone else, which nothing + // answers with null before RSKIP88: `executePrecompiledAndHandleError` + // turns it into a rollback + stackPushZero (the call returns 0 / empty + // data) while still charging the full `requiredGas`. A relay contract + // forwarding the call is checked by its own address, so a non-federation + // caller is rejected with no state change and no event (mainnet #6,223,774 + // tx[1]: updateCollections via relay 0x82494fb1 must NOT emit the + // update_collections log). + let block_number = revm::context_interface::Block::number(ctx.block()).to::(); + let (federation_only, proposed_too) = match method_name { + "updateCollections" => (true, false), // receiveHeadersIsPublic() = RSKIP124 && !RSKIP200; restricted otherwise. #[allow(clippy::nonminimal_bool)] // mirrors the rskj formula above - "receiveHeaders" => { - let block_number = revm::context_interface::Block::number(ctx.block()).to::(); - !(hardfork_cfg.has_rskip124(block_number) && !hardfork_cfg.has_rskip200(block_number)) - } - _ => false, + "receiveHeaders" => ( + !(hardfork_cfg.has_rskip124(block_number) && !hardfork_cfg.has_rskip200(block_number)), + false, + ), + // registerBtcTransactionIsPublic() = RSKIP199. + "registerBtcTransaction" => (!hardfork_cfg.has_rskip199(block_number), false), + "addSignature" => (true, true), + _ => (false, false), }; if federation_only { - let block_number = revm::context_interface::Block::number(ctx.block()).to::(); - if !peg::is_sender_active_or_retiring_fed_member(ctx, config, hardfork_cfg, block_number, caller) { - // BridgeIllegalArgumentException-equivalent: a non-OOG throw the - // execute_bridge wrapper maps to the depth-aware bridge-throw marker - // (rollback + only requiredGas spent). + let member = peg::is_sender_active_or_retiring_fed_member( + ctx, + config, + hardfork_cfg, + block_number, + caller, + ) || (proposed_too + && peg::is_sender_proposed_fed_member(ctx, hardfork_cfg, block_number, caller)); + if !member { + // A non-OOG throw the execute_bridge wrapper maps to the + // depth-aware bridge-throw marker (rollback + only requiredGas + // spent). return Err(PrecompileError::other("Bridge: sender is not a federation member")); } } @@ -1389,6 +1403,144 @@ mod tests { assert_eq!(m.name, "releaseBtc"); } + fn mainnet_at( + block_number: u64, + ) -> revm::Context< + revm::context::BlockEnv, + revm::context::TxEnv, + revm::context::CfgEnv, + revm::database::EmptyDB, + revm::Journal, + crate::raw_storage::RskChainExt, + > { + use revm::MainContext; + let mut ctx = + revm::Context::mainnet().with_chain(crate::raw_storage::RskChainExt::default()); + ctx.block.number = U256::from(block_number); + ctx + } + + /// Whether `method`, called by `caller` with no arguments, is turned down + /// as rskj's federation-only executors turn down a caller who is in none + /// of the federations they name. + fn turned_down_as_a_stranger( + ctx: &mut CTX, + method: &str, + caller: alloy_primitives::Address, + ) -> bool { + let (config, cfg) = (BridgeConstants::mainnet(), RskHardforkConfig::mainnet()); + let block_number = revm::context_interface::Block::number(ctx.block()).to::(); + let tx_ctx = BridgeTxContext { + rsk_sender: caller, + ..Default::default() + }; + let result = execute_method( + ctx, + method, + &[], + 0, + &config, + None, + cfg.has_stored_block_v2(block_number), + &cfg, + &tx_ctx, + caller, + 0, + U256::ZERO, + ); + result.is_err_and(|e| format!("{e:?}").contains("not a federation member")) + } + + /// rskj gives four methods an executor that throws a `VMException` for a + /// caller outside the federations (BridgeMethods.java): `addSignature` at + /// every height, `registerBtcTransaction` until RSKIP199 makes it public, + /// `receiveHeaders` while it isn't public, and `updateCollections`. + #[test] + fn the_federations_methods_turn_a_stranger_down() { + let config = BridgeConstants::mainnet(); + // With no federation stored yet the genesis federation is the active one. + let member = + federation::rsk_address_from_public_key(&peg::genesis_federation_keys(&config)[0]) + .unwrap(); + let stranger = alloy_primitives::Address::repeat_byte(0x99); + + for block_number in [1, 3_000_000, 8_000_000] { + let mut ctx = mainnet_at(block_number); + assert!(turned_down_as_a_stranger( + &mut ctx, + "addSignature", + stranger + )); + assert!(!turned_down_as_a_stranger(&mut ctx, "addSignature", member)); + assert!(turned_down_as_a_stranger( + &mut ctx, + "updateCollections", + stranger + )); + } + + // RSKIP199 is iris300's, at 3,614,800. + let mut before = mainnet_at(3_614_799); + assert!(turned_down_as_a_stranger( + &mut before, + "registerBtcTransaction", + stranger + )); + assert!(!turned_down_as_a_stranger( + &mut before, + "registerBtcTransaction", + member + )); + let mut from = mainnet_at(3_614_800); + assert!(!turned_down_as_a_stranger( + &mut from, + "registerBtcTransaction", + stranger + )); + } + + /// `activeRetiringAndProposedFederationOnly` also admits a member of the + /// proposed federation, which exists from RSKIP419 (lovell700, 7,338,024). + /// `activeAndRetiringFederationOnly` doesn't. + #[test] + fn add_signature_admits_a_member_of_the_proposed_federation() { + let key = |hex: &str| -> [u8; 33] { + alloy_primitives::hex::decode(hex) + .unwrap() + .try_into() + .unwrap() + }; + // The curve's generator and its double. + let g = key("0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"); + let two_g = key("02c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5"); + let proposed = federation::StoredMember { + btc: g, + rsk: two_g, + mst: g, + }; + let caller = federation::rsk_address_from_public_key(&two_g).unwrap(); + + for (block_number, admitted) in [(7_338_023, false), (7_338_024, true)] { + let mut ctx = mainnet_at(block_number); + let stored = federation::serialize_federation_multikey( + std::slice::from_ref(&proposed), + 1_700_000_000_000, + 7_000_000, + ); + storage::bridge_store_bytes_named(&mut ctx, storage::PROPOSED_FEDERATION_KEY, &stored); + assert_eq!( + !turned_down_as_a_stranger(&mut ctx, "addSignature", caller), + admitted, + "at {block_number}" + ); + assert!(turned_down_as_a_stranger( + &mut ctx, + "updateCollections", + caller + )); + } + } + /// rskj Bridge.receiveHeadersGetCost groundtruth: 22_000 pre-RSKIP124; /// post-RSKIP132 (mainnet wasabi100) 25_000 + (n-1) * 3_500. #[test] diff --git a/crates/execution/src/bridge/peg.rs b/crates/execution/src/bridge/peg.rs index 8cfc677..dff8eaa 100644 --- a/crates/execution/src/bridge/peg.rs +++ b/crates/execution/src/bridge/peg.rs @@ -4508,10 +4508,11 @@ pub(crate) fn federation_redeem_for_format_pub( /// rskj `BridgeUtils.isFromFederateMember(rskTx, federation)`: true when the /// call's sender RSK address matches the RSK public-key address of any member /// of the ACTIVE or RETIRING federation. This is the authorization enforced by -/// `Bridge.activeAndRetiringFederationOnly` for `updateCollections` (always) -/// and `receiveHeaders` (post-RSKIP200). `sender` is the immediate caller of -/// the Bridge precompile (= rskj's internalTx sender = `getOwnerRskAddress`), -/// so a relay contract forwarding the call is checked, not the original EOA. +/// `Bridge.activeAndRetiringFederationOnly` for `updateCollections` (always), +/// `receiveHeaders` (post-RSKIP200) and `registerBtcTransaction` (until +/// RSKIP199). `sender` is the immediate caller of the Bridge precompile (= +/// rskj's internalTx sender = `getOwnerRskAddress`), so a relay contract +/// forwarding the call is checked, not the original EOA. pub(crate) fn is_sender_active_or_retiring_fed_member( ctx: &mut CTX, config: &BridgeConstants, @@ -4564,6 +4565,28 @@ pub(crate) fn is_sender_active_or_retiring_fed_member( false } +/// Whether `sender` is a member of the proposed federation, whom +/// `Bridge.activeRetiringAndProposedFederationOnly` admits to `addSignature` +/// beside the active and retiring federations' members. Before RSKIP419 +/// `getProposedFederation` answers that there is none. +pub(crate) fn is_sender_proposed_fed_member( + ctx: &mut CTX, + hardfork_cfg: &RskHardforkConfig, + block_number: u64, + sender: alloy_primitives::Address, +) -> bool { + use super::federation::rsk_address_from_public_key; + hardfork_cfg.has_rskip419(block_number) + && super::federation::load_stored_federation(ctx, PROPOSED_FEDERATION_KEY).is_some_and( + |proposed| { + proposed + .members + .iter() + .any(|m| rsk_address_from_public_key(&m.rsk) == Some(sender)) + }, + ) +} + pub(crate) fn genesis_federation_keys(config: &BridgeConstants) -> Vec<[u8; 33]> { // rskj Federation sorts members by compressed BTC public key. let mut keys: Vec<[u8; 33]> = config From 2ad501e755160af0fb48fb4fd2342f6728c39a31 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Thu, 12 Nov 2026 21:10:07 -0800 Subject: [PATCH 106/264] bridge: the four peg-in sender parsers, as RSKj runs them `BtcLockSenderProvider` tries four parsers on a peg-in's first input. Each answers false for what it catches and throws for what it does before its `try`, which nothing catches before `Bridge.execute`. rustock answered "no sender" for all of it, and read keys more narrowly: - P2PKH counts two chunks and reads the second. The first need not be a push: rustock wanted two pushes. - A key is whatever Bouncy Castle's `decodePoint` takes: a hybrid key, which bitcoinj hashes in its uncompressed form, and the one byte 0x00, the point at infinity, whose RSK address is the hash of nothing. - A scriptSig bitcoinj can't parse throws in the first parser. - P2SH-P2WPKH decodes the witness's key before its `try`, and throws for what isn't one. - Both multisig parsers build the redeem script before their `try`: a scriptSig ending in an opcode and a script bitcoinj can't parse throw. A sender now carries the addresses its parser derives rather than the key's bytes. The keys in the tests were decoded by bitcoinj-thin `v0.14.4-rsk-18` and Bouncy Castle, and the hashes are theirs. --- crates/execution/src/bridge/peg.rs | 449 ++++++++++++++---- .../src/bridge/rskj_sender_compat.rs | 13 + 2 files changed, 382 insertions(+), 80 deletions(-) diff --git a/crates/execution/src/bridge/peg.rs b/crates/execution/src/bridge/peg.rs index dff8eaa..c1d8875 100644 --- a/crates/execution/src/bridge/peg.rs +++ b/crates/execution/src/bridge/peg.rs @@ -997,16 +997,19 @@ pub fn register_btc_transaction( // can lock; other processable types are refunded ("Btc tx type not // supported, returning funds to sender") without consulting the // whitelist or the locking cap. - let (sender_hash160, sender_is_p2sh, sender_pubkey, lockable): ([u8; 20], bool, Option<&[u8]>, bool) = - match &sender { - PeginSender::P2pkh { pubkey } => (pubkey_hash160(pubkey), false, Some(pubkey), true), - PeginSender::P2shP2wpkh { pubkey, p2sh_hash } => { - (*p2sh_hash, true, Some(pubkey), true) - } - PeginSender::P2shMultisig { p2sh_hash } | PeginSender::P2shP2wsh { p2sh_hash } => { - (*p2sh_hash, true, None, false) - } - }; + let (sender_hash160, sender_is_p2sh, sender_rsk_address, lockable) = match &sender { + PeginSender::P2pkh { + btc_hash160, + rsk_address, + } => (*btc_hash160, false, Some(*rsk_address), true), + PeginSender::P2shP2wpkh { + p2sh_hash, + rsk_address, + } => (*p2sh_hash, true, Some(*rsk_address), true), + PeginSender::P2shMultisig { p2sh_hash } | PeginSender::P2shP2wsh { p2sh_hash } => { + (*p2sh_hash, true, None, false) + } + }; // Lock whitelist gate (rskj verifyLockSenderIsWhitelisted): the matching // one-off entry is consumed on success; rejection generates a refund @@ -1100,10 +1103,10 @@ pub fn register_btc_transaction( return Ok(PrecompileOutput::new(gas_cost, Bytes::new())); } - // RSK destination: rskj's legacy peg-in credits the address derived from - // the sender's public key (ethereumj ECKey.getAddress over the - // UNCOMPRESSED point — mainnet #267,460 used a 65-byte pubkey). - let rsk_destination = sender_pubkey.and_then(super::federation::rsk_address_from_public_key); + // RSK destination: rskj's legacy peg-in credits the address its sender + // parser derived from the sender's public key (ethereumj ECKey.getAddress + // over the UNCOMPRESSED point — mainnet #267,460 used a 65-byte pubkey). + let rsk_destination = sender_rsk_address; // Credit the derived destination address (transfer from Bridge balance) if let Some(dest) = rsk_destination { @@ -1302,7 +1305,7 @@ fn refund_fields( /// hash160 and whether it is a P2SH address. fn sender_refund_target(sender: &PeginSender) -> ([u8; 20], bool) { match sender { - PeginSender::P2pkh { pubkey } => (pubkey_hash160(pubkey), false), + PeginSender::P2pkh { btc_hash160, .. } => (*btc_hash160, false), PeginSender::P2shP2wpkh { p2sh_hash, .. } | PeginSender::P2shMultisig { p2sh_hash } | PeginSender::P2shP2wsh { p2sh_hash } => (*p2sh_hash, true), @@ -1427,84 +1430,171 @@ fn emit_pegin_rejection_release( } } -/// rskj `BtcLockSender.TxType`: the peg-in sender classification derived -/// from the FIRST input. `hash160` is the sender's BTC address hash (pubkey -/// hash for P2PKH, script hash for the P2SH variants) — what rskj's -/// `senderBtcAddress` carries into the whitelist check and the refund. +/// rskj `BtcLockSender`: what a peg-in's FIRST input says of its sender, as +/// one of the four parsers reads it. Each variant carries what the parser +/// derives. The hash is the sender's Bitcoin address's (`getBTCAddress`: a +/// public key's hash for P2PKH, a script's for the P2SH variants), which is +/// what the whitelist is asked about and what a refund pays. The two +/// single-key shapes also have an RSK address (`getRskAddress`), which a +/// version 0 peg-in credits. enum PeginSender { - P2pkh { pubkey: Vec }, - P2shP2wpkh { pubkey: Vec, p2sh_hash: [u8; 20] }, - P2shMultisig { p2sh_hash: [u8; 20] }, - P2shP2wsh { p2sh_hash: [u8; 20] }, + P2pkh { + btc_hash160: [u8; 20], + rsk_address: alloy_primitives::Address, + }, + P2shP2wpkh { + p2sh_hash: [u8; 20], + rsk_address: alloy_primitives::Address, + }, + P2shMultisig { + p2sh_hash: [u8; 20], + }, + P2shP2wsh { + p2sh_hash: [u8; 20], + }, +} + +/// A public key as rskj's sender parsers read one, with bitcoinj's +/// `BtcECKey.fromPublicOnly` and then its own `ECKey.fromPublicOnly`. Both are +/// Bouncy Castle's `decodePoint`, which takes what libsecp256k1 takes, hybrid +/// keys among it, and also the one byte 0x00, the point at infinity. +struct SenderKey { + /// `BtcECKey.isCompressed()`: the key came in 33 bytes. + compressed: bool, + /// `BtcECKey.getPubKeyHash()`. bitcoinj encodes the point again before it + /// hashes it, compressed if it came compressed and uncompressed if not, + /// so a hybrid key hashes as its uncompressed form. + btc_hash160: [u8; 20], + /// `ECKey.getAddress()`: the hash of the uncompressed point less its first + /// byte. Of the point at infinity that is the hash of nothing. + rsk_address: alloy_primitives::Address, +} + +/// `None` where `decodePoint` throws. +fn decode_sender_key(key: &[u8]) -> Option { + use sha3::Digest; + let rsk_address = |coordinates: &[u8]| { + alloy_primitives::Address::from_slice(&sha3::Keccak256::digest(coordinates)[12..]) + }; + if key == [0x00] { + return Some(SenderKey { + compressed: false, + btc_hash160: pubkey_hash160(key), + rsk_address: rsk_address(&[]), + }); + } + let point = bitcoin::secp256k1::PublicKey::from_slice(key).ok()?; + let uncompressed = point.serialize_uncompressed(); + let compressed = key.len() == 33; + Some(SenderKey { + compressed, + btc_hash160: if compressed { + pubkey_hash160(&point.serialize()) + } else { + pubkey_hash160(&uncompressed) + }, + rsk_address: rsk_address(&uncompressed[1..]), + }) } + +/// `new Script(redeemScript)` and then `isSentToMultiSig()`, as both multisig +/// sender parsers run them before their `try`: a redeem script bitcoinj can't +/// parse throws, and so does one its parser dispatch throws for. +fn is_multisig_redeem(redeem: &[u8]) -> Result { + use super::rskj_sender_compat::{self as bitcoinj, Verdict}; + if !bitcoinj::parses_as_script(redeem) { + return Err("ScriptException parsing the sender's redeem script"); + } + match bitcoinj::is_sent_to_multisig(redeem) { + Verdict::Multisig => Ok(true), + Verdict::NotMultisig => Ok(false), + Verdict::RskjThrows(why) => Err(why), + } +} + /// rskj `BtcLockSenderProvider.tryGetBtcLockSender`: try P2PKH, then /// P2SH-P2WPKH, then P2SH-MULTISIG, then P2SH-P2WSH — all on the first /// input. Returns `None` when no parser matches (rskj then returns from /// registerBtcTransaction WITHOUT marking the tx as processed). +/// +/// Each parser answers false for what it catches and throws for what it does +/// outside its `try`, which nothing between it and `Bridge.execute` catches: +/// the `Err`. fn classify_pegin_sender( tx: &BtcTransaction, ) -> Result, &'static str> { - use super::release_tx::{parse_chunks, Chunk}; + use super::rskj_sender_compat as bitcoinj; let Some(first) = tx.input.first() else { return Ok(None) }; - let Some(chunks) = parse_chunks(first.script_sig.as_bytes()) else { return Ok(None) }; - // P2PKH: scriptSig is exactly [sig, pubkey] with a valid curve point - // (rskj derives both the BTC and RSK addresses from it). - if let [Chunk::Data(_sig), Chunk::Data(pubkey)] = chunks.as_slice() { - if compress_pubkey(pubkey).is_some() { - return Ok(Some(PeginSender::P2pkh { pubkey: pubkey.clone() })); + // The first parser tried opens with `getScriptSig()`, which parses the + // script, and the later ones read the script it parsed. + let Some(chunks) = bitcoinj::chunk_data(first.script_sig.as_bytes()) else { + return Err("ScriptException parsing the first input's scriptSig"); + }; + + // P2PKH: a scriptSig of two chunks whose second is a public key, from + // which rskj derives both the BTC and RSK addresses. The first chunk is + // never looked at. An opcode in the key's place has no data, and decoding + // it throws as decoding what isn't a key does: inside the `try`. + if chunks.len() == 2 { + if let Some(key) = chunks[1].as_deref().and_then(decode_sender_key) { + return Ok(Some(PeginSender::P2pkh { + btc_hash160: key.btc_hash160, + rsk_address: key.rsk_address, + })); } } let witness: Vec<&[u8]> = first.witness.iter().collect(); // P2SH-P2WPKH: single-chunk scriptSig, witness [sig, compressed pubkey]; - // P2SH hash = hash160(0x0014 || hash160(pubkey)). + // P2SH hash = hash160(0x0014 || hash160(pubkey)). The key is decoded + // before the `try`, and one that isn't compressed is not this parser's. if chunks.len() == 1 && witness.len() == 2 { - let pubkey = witness[1]; - if pubkey.len() == 33 && compress_pubkey(pubkey).is_some() { + let Some(key) = decode_sender_key(witness[1]) else { + return Err("IllegalArgumentException decoding the witness's public key"); + }; + if key.compressed { let mut redeem = vec![0x00, 0x14]; - redeem.extend_from_slice(&pubkey_hash160(pubkey)); + redeem.extend_from_slice(&key.btc_hash160); return Ok(Some(PeginSender::P2shP2wpkh { - pubkey: pubkey.to_vec(), p2sh_hash: pubkey_hash160(&redeem), + rsk_address: key.rsk_address, })); } } // P2SH-MULTISIG: scriptSig [.., sigs.., redeem] with a multisig redeem; - // P2SH hash = hash160(redeem). + // P2SH hash = hash160(redeem). A scriptSig that ends in an opcode has no + // data to build the redeem script from: a NullPointerException. // // The predicate lives in `rskj_sender_compat` rather than here: it is a // faithful port of bitcoinj's parser dispatch, kept apart so that it can be // switched off and deleted when the RSKIP retiring this path activates. - if super::rskj_sender_compat::enabled() && chunks.len() >= 3 { - if let Some(Chunk::Data(redeem)) = chunks.last() { - match super::rskj_sender_compat::is_sent_to_multisig(redeem) { - super::rskj_sender_compat::Verdict::Multisig => { - return Ok(Some(PeginSender::P2shMultisig { - p2sh_hash: pubkey_hash160(redeem), - })); - } - super::rskj_sender_compat::Verdict::RskjThrows(why) => return Err(why), - super::rskj_sender_compat::Verdict::NotMultisig => {} - } + if bitcoinj::enabled() && chunks.len() >= 3 { + let Some(redeem) = chunks[chunks.len() - 1].as_deref() else { + return Err( + "NullPointerException: the scriptSig ends in an opcode, not a redeem script", + ); + }; + if is_multisig_redeem(redeem)? { + return Ok(Some(PeginSender::P2shMultisig { + p2sh_hash: pubkey_hash160(redeem), + })); } } // P2SH-P2WSH: single-chunk scriptSig, witness [.., sigs.., redeem] with // a multisig redeem; P2SH hash = hash160(0x0020 || sha256(redeem)). - if super::rskj_sender_compat::enabled() && chunks.len() == 1 && witness.len() >= 3 { + if bitcoinj::enabled() && chunks.len() == 1 && witness.len() >= 3 { let redeem = witness[witness.len() - 1]; - match super::rskj_sender_compat::is_sent_to_multisig(redeem) { - super::rskj_sender_compat::Verdict::Multisig => { - use sha2::Digest; - let mut merged = vec![0x00, 0x20]; - merged.extend_from_slice(&sha2::Sha256::digest(redeem)); - return Ok(Some(PeginSender::P2shP2wsh { p2sh_hash: pubkey_hash160(&merged) })); - } - super::rskj_sender_compat::Verdict::RskjThrows(why) => return Err(why), - super::rskj_sender_compat::Verdict::NotMultisig => {} + if is_multisig_redeem(redeem)? { + use sha2::Digest; + let mut merged = vec![0x00, 0x20]; + merged.extend_from_slice(&sha2::Sha256::digest(redeem)); + return Ok(Some(PeginSender::P2shP2wsh { + p2sh_hash: pubkey_hash160(&merged), + })); } } @@ -7647,7 +7737,13 @@ mod tests { output: vec![], }; match classify_pegin_sender(&tx).unwrap() { - Some(PeginSender::P2pkh { pubkey: parsed }) => assert_eq!(parsed, pubkey), + Some(PeginSender::P2pkh { + btc_hash160, + rsk_address, + }) => { + assert_eq!(rsk_address, addr); + assert_eq!(btc_hash160, pubkey_hash160(&pubkey)); + } other => panic!("expected P2PKH sender, got {}", other.is_some()), } } @@ -7777,13 +7873,20 @@ mod tests { #[test] fn sender_refund_target_distinguishes_p2pkh_from_the_p2sh_shapes() { let pubkey = real_pubkey().to_vec(); - let (hash, is_p2sh) = sender_refund_target(&PeginSender::P2pkh { pubkey: pubkey.clone() }); + let rsk_address = crate::bridge::federation::rsk_address_from_public_key(&pubkey).unwrap(); + let (hash, is_p2sh) = sender_refund_target(&PeginSender::P2pkh { + btc_hash160: pubkey_hash160(&pubkey), + rsk_address, + }); assert_eq!(hash, pubkey_hash160(&pubkey), "P2PKH refunds to hash160(pubkey)"); assert!(!is_p2sh, "P2PKH is not a P2SH address"); let marker = [0xABu8; 20]; for sender in [ - PeginSender::P2shP2wpkh { pubkey, p2sh_hash: marker }, + PeginSender::P2shP2wpkh { + p2sh_hash: marker, + rsk_address, + }, PeginSender::P2shMultisig { p2sh_hash: marker }, PeginSender::P2shP2wsh { p2sh_hash: marker }, ] { @@ -7929,17 +8032,32 @@ mod tests { "witness with a single push matches no shape" ); - // Witness has two pushes but the second is not a valid compressed key. + // Witness has two pushes but the second is a key that isn't compressed. + // One that isn't a key at all throws: see + // `the_p2sh_p2wpkh_parser_throws_for_a_witness_key_it_cannot_decode`. + let mut uncompressed = vec![0x04]; + uncompressed.extend_from_slice(&alloy_primitives::hex::decode(GX).unwrap()); + uncompressed.extend_from_slice(&alloy_primitives::hex::decode(GY).unwrap()); assert!( - classify_pegin_sender(&tx_with(vec![2, 0x00, 0x14], - vec![vec![0x30; 71], vec![0xFFu8; 33]])).unwrap().is_none(), - "P2SH-P2WPKH requires the witness key to be a curve point" + classify_pegin_sender(&tx_with( + vec![2, 0x00, 0x14], + vec![vec![0x30; 71], uncompressed] + )) + .unwrap() + .is_none(), + "P2SH-P2WPKH requires the witness key to be compressed" ); - // Three witness items but the last is not a multisig redeem. + // Three witness items but the last is a script that isn't a multisig + // redeem. One bitcoinj can't parse throws: see + // `the_multisig_parsers_throw_for_a_redeem_script_they_cannot_build`. assert!( - classify_pegin_sender(&tx_with(vec![2, 0x00, 0x20], - vec![vec![], vec![0x30; 71], vec![0x01, 0x02, 0x03]])).unwrap().is_none(), + classify_pegin_sender(&tx_with( + vec![2, 0x00, 0x20], + vec![vec![], vec![0x30; 71], vec![0x51, 0x52, 0x53]] + )) + .unwrap() + .is_none(), "P2SH-P2WSH requires a multisig redeem as the last witness item" ); } @@ -8149,21 +8267,192 @@ mod tests { assert_eq!(varied, seeds.len() * 1_500); } - /// The P2PKH branch accepts only a real curve point. rskj gets this from - /// BtcECKey.fromPublicOnly throwing; rustock from `compress_pubkey` - /// returning None. Without it a two-chunk scriptSig whose second push is - /// arbitrary bytes would be read as P2PKH and refunded to a hash160 of - /// data the sender never controlled. + // --- the four sender parsers, case by case ----------------------------- + // + // Every key below was decoded by the release's own libraries, bitcoinj-thin + // `0.14.4-rsk-18` for `BtcECKey.fromPublicOnly` and Bouncy Castle for + // `org.ethereum.crypto.ECKey.fromPublicOnly`, and the hashes and addresses + // are the ones they answered. + + /// The generator's coordinates. + const GX: &str = "79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"; + const GY: &str = "483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8"; + /// `ECKey.getAddress()` of the generator, however it is encoded. + const G_RSK_ADDRESS: &str = "7e5f4552091a69125d5dfcb7b8c2659029395bdf"; + /// `BtcECKey.getPubKeyHash()` of the generator compressed, and uncompressed. + const G_COMPRESSED_HASH160: &str = "751e76e8199196d454941c45d1b3a323f1433bd6"; + const G_UNCOMPRESSED_HASH160: &str = "91b24bf9f5288532960ac687abb035127b1d28a5"; + + fn sec1(prefix: &str, rest: &[&str]) -> Vec { + alloy_primitives::hex::decode(format!("{prefix}{}", rest.concat())).unwrap() + } + + /// A scriptSig of `first` and then `second` pushed. + fn then_push(first: &[u8], second: &[u8]) -> Vec { + let mut script = first.to_vec(); + script.push(second.len() as u8); + script.extend_from_slice(second); + script + } + + /// The sender rskj reads off a first input: its type, its Bitcoin address's + /// hash and its RSK address, in hex. + fn sender_read_off( + script_sig: Vec, + witness: Vec>, + ) -> Result)>, &'static str> { + Ok( + classify_pegin_sender(&tx_with(script_sig, witness))?.map(|sender| { + ( + sender_type_name(Some(&sender)), + alloy_primitives::hex::encode(sender_refund_target(&sender).0), + rsk_address_of(&sender).map(alloy_primitives::hex::encode), + ) + }), + ) + } + + fn rsk_address_of(sender: &PeginSender) -> Option { + match sender { + PeginSender::P2pkh { rsk_address, .. } + | PeginSender::P2shP2wpkh { rsk_address, .. } => Some(*rsk_address), + _ => None, + } + } + + fn p2pkh(hash160: &str, rsk_address: &str) -> Option<(&'static str, String, Option)> { + Some(("P2PKH", hash160.into(), Some(rsk_address.into()))) + } + + /// `P2pkhBtcLockSender` counts the scriptSig's chunks and reads the second. + /// It never looks at the first, which need not be a signature or a push. #[test] - fn classify_pegin_sender_p2pkh_requires_a_valid_public_key() { - let mut script = vec![71]; - script.extend_from_slice(&[0x30; 71]); - script.push(33); - script.extend_from_slice(&[0xFFu8; 33]); // not on the curve - assert!( - classify_pegin_sender(&tx_with(script, vec![])).unwrap().is_none(), - "a non-curve 33-byte push must not classify as P2PKH" + fn the_p2pkh_parser_reads_the_second_chunk_alone() { + let compressed = sec1("02", &[GX]); + let signature = then_push(&[], &[0x30; 71]); + // OP_0, OP_1 and OP_DUP in the signature's place. + for first in [&signature[..], &[0x00], &[0x51], &[0x76]] { + assert_eq!( + sender_read_off(then_push(first, &compressed), vec![]), + Ok(p2pkh(G_COMPRESSED_HASH160, G_RSK_ADDRESS)), + "{first:02x?}" + ); + } + } + + /// Bouncy Castle's `decodePoint` takes a hybrid key and the point at + /// infinity besides the two usual encodings. bitcoinj encodes a point + /// again before it hashes it, so a hybrid key has its uncompressed form's + /// address, and rskj's address of the point at infinity is the hash of + /// nothing. + #[test] + fn the_p2pkh_parser_takes_every_key_bouncy_castle_decodes() { + let sender_with = |key: &[u8]| sender_read_off(then_push(&[0x00], key), vec![]); + assert_eq!( + sender_with(&sec1("02", &[GX])), + Ok(p2pkh(G_COMPRESSED_HASH160, G_RSK_ADDRESS)) + ); + assert_eq!( + sender_with(&sec1("04", &[GX, GY])), + Ok(p2pkh(G_UNCOMPRESSED_HASH160, G_RSK_ADDRESS)) + ); + // The generator's y is even: 0x06 says so, 0x07 contradicts it. + assert_eq!( + sender_with(&sec1("06", &[GX, GY])), + Ok(p2pkh(G_UNCOMPRESSED_HASH160, G_RSK_ADDRESS)) + ); + assert_eq!( + sender_with(&[0x00]), + Ok(p2pkh( + "9f7fd096d37ed2c0e3f7f0cfc924beef4ffceb68", + "dcc703c0e500b653ca82273b7bfad8045d85a470" + )) ); + + // What it throws for is caught there: the parser answers false, and + // with no witness no other parser takes the input. + let not_keys = [ + sec1("07", &[GX, GY]), + sec1("02", &[&"ff".repeat(32)]), + sec1("00", &[&"00".repeat(32)]), + sec1("04", &[GX]), + sec1("05", &[GX]), + vec![], + ]; + for not_a_key in ¬_keys { + assert_eq!(sender_with(not_a_key), Ok(None), "{not_a_key:02x?}"); + } + // An opcode in the key's place has no data at all. + assert_eq!(sender_read_off(vec![0x00, 0x51], vec![]), Ok(None)); + } + + /// `P2pkhBtcLockSender.tryParse`, the first parser tried, opens with + /// `getScriptSig()`, which parses the script outside every `try`. + #[test] + fn a_script_sig_bitcoinj_cannot_parse_makes_rskj_throw() { + // OP_PUSHDATA1 with nothing after it, and a push longer than the script. + assert!(sender_read_off(vec![0x4c], vec![]).is_err()); + assert!(sender_read_off(vec![0x05, 0x01], vec![]).is_err()); + } + + /// `P2shP2wpkhBtcLockSender` decodes the witness's second push with + /// `BtcECKey.fromPublicOnly` before its `try`: what isn't a key throws. A + /// key that isn't compressed is none of this parser's. + #[test] + fn the_p2sh_p2wpkh_parser_throws_for_a_witness_key_it_cannot_decode() { + let one_chunk = vec![2, 0x00, 0x14]; + let with_key = |key: Vec| sender_read_off(one_chunk.clone(), vec![vec![0x30; 71], key]); + + assert!(with_key(vec![0xff; 33]).is_err()); + assert!(with_key(vec![]).is_err()); + assert!(with_key(sec1("07", &[GX, GY])).is_err()); + + assert_eq!(with_key(sec1("04", &[GX, GY])), Ok(None)); + assert_eq!(with_key(sec1("06", &[GX, GY])), Ok(None)); + assert_eq!(with_key(vec![0x00]), Ok(None)); + + // hash160(0x0014 || hash160(key)), and the key's RSK address. + let mut program = vec![0x00, 0x14]; + program.extend_from_slice(&alloy_primitives::hex::decode(G_COMPRESSED_HASH160).unwrap()); + assert_eq!( + with_key(sec1("02", &[GX])), + Ok(Some(( + "P2SH-P2WPKH", + alloy_primitives::hex::encode(pubkey_hash160(&program)), + Some(G_RSK_ADDRESS.into()) + ))) + ); + } + + /// Both multisig parsers build `new Script(redeemScript)` before their + /// `try`. A scriptSig that ends in an opcode has no data to build it from, + /// a `NullPointerException`, and a redeem script bitcoinj can't parse is a + /// `ScriptException`. One it parses that isn't a multisig is just not this + /// parser's. + #[test] + fn the_multisig_parsers_throw_for_a_redeem_script_they_cannot_build() { + let three_chunks = |last: &[u8]| { + let mut script = vec![0x00, 0x01, 0x30]; + script.extend_from_slice(last); + script + }; + // OP_1 as the last chunk, then OP_PUSHDATA1 alone pushed as the redeem. + assert!(sender_read_off(three_chunks(&[0x51]), vec![]).is_err()); + assert!(sender_read_off(three_chunks(&[0x01, 0x4c]), vec![]).is_err()); + // OP_1 pushed, and nothing pushed: scripts, not multisigs. + assert_eq!( + sender_read_off(three_chunks(&[0x01, 0x51]), vec![]), + Ok(None) + ); + assert_eq!(sender_read_off(three_chunks(&[0x00]), vec![]), Ok(None)); + + let one_chunk = vec![2, 0x00, 0x20]; + let witness_ending = + |last: Vec| sender_read_off(one_chunk.clone(), vec![vec![], vec![0x30; 71], last]); + assert!(witness_ending(vec![0x4c]).is_err()); + assert!(witness_ending(vec![0x01, 0x02, 0x03]).is_err()); + assert_eq!(witness_ending(vec![0x51]), Ok(None)); + assert_eq!(witness_ending(vec![]), Ok(None)); } /// sender address = P2SH(hash160(redeem)) — processable post-RSKIP143 diff --git a/crates/execution/src/bridge/rskj_sender_compat.rs b/crates/execution/src/bridge/rskj_sender_compat.rs index 5873e52..a93ead7 100644 --- a/crates/execution/src/bridge/rskj_sender_compat.rs +++ b/crates/execution/src/bridge/rskj_sender_compat.rs @@ -339,6 +339,19 @@ pub fn parses_as_script(program: &[u8]) -> bool { parse_script_program(program).is_some() } +/// A script's chunks as bitcoinj holds them, each by its data: `None` for an +/// opcode, which has none, and the bytes for a push, which `OP_0` makes of +/// nothing. `None` overall is the `ScriptException` for a script bitcoinj +/// can't parse. +pub fn chunk_data(program: &[u8]) -> Option>>> { + Some( + parse_script_program(program)? + .into_iter() + .map(|chunk| chunk.data) + .collect(), + ) +} + // --- RedeemScriptValidator ------------------------------------------------- fn is_redeem_like_script(chunks: &[JChunk]) -> bool { From 477a944e0cc56bb7665c8a46439004670df1ec9f Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Thu, 12 Nov 2026 21:10:07 -0800 Subject: [PATCH 107/264] bridge: the union bridge's authorizer-only methods throw for anyone else RSKj gives `setUnionBridgeContractAddressForTestnet`, `increaseUnionBridgeLockingCap` and `setUnionBridgeTransferPermissions` an executor that throws a `VMException` unless the caller is the method's authorizer, before the method runs (`Bridge.executeIfAuthorized`, l.1538-1554), and the first also throws on mainnet (`executeIfTestnetAndAuthorized`). rustock answered -1 for the caller and -10 for the network, a call that returned, so a stranger's transaction on testnet got a different receipt than RSKj gives it. `UNAUTHORIZED_CALLER` stays the answer of the two methods that check for the union bridge contract themselves. --- crates/execution/src/bridge/union.rs | 101 ++++++++++++++++++++++----- 1 file changed, 84 insertions(+), 17 deletions(-) diff --git a/crates/execution/src/bridge/union.rs b/crates/execution/src/bridge/union.rs index 3317cc1..12e1d2e 100644 --- a/crates/execution/src/bridge/union.rs +++ b/crates/execution/src/bridge/union.rs @@ -230,7 +230,7 @@ fn int_result(gas: u64, code: i64) -> Result /// `setUnionBridgeContractAddressForTestnet(address)` -> int256. /// /// rskj wraps this in `executeIfTestnetAndAuthorized`, so it is both -/// network-gated and authorizer-gated. +/// network-gated and authorizer-gated, and throws at either gate. pub fn set_contract_address_for_testnet( ctx: &mut CTX, args: &[u8], @@ -239,12 +239,16 @@ pub fn set_contract_address_for_testnet( sender: Address, ) -> Result { if hardfork_cfg.chain_id == RSK_MAINNET_CHAIN_ID { - return int_result(gas_cost, UnionResponse::GenericError.code()); + return Err(PrecompileError::other( + "The setUnionBridgeContractAddressForTestnet function is disabled in Mainnet.", + )); } let constants = union_constants(hardfork_cfg); - if sender != constants.change_address_authorizer { - return int_result(gas_cost, UnionResponse::UnauthorizedCaller.code()); - } + require_authorizer( + sender, + constants.change_address_authorizer, + "The sender is not authorized to call setUnionBridgeContractAddressForTestnet", + )?; // rskj `new RskAddress((DataWord) args[0])`: the address as its decoder // reads it, an empty argument list being the zero address. let Some(new_address) = crate::abi::Args::new(args).address(0) else { @@ -288,9 +292,11 @@ pub fn increase_locking_cap( sender: Address, ) -> Result { let constants = union_constants(hardfork_cfg); - if sender != constants.change_locking_cap_authorizer { - return int_result(gas_cost, UnionResponse::UnauthorizedCaller.code()); - } + require_authorizer( + sender, + constants.change_locking_cap_authorizer, + "The sender is not authorized to call increaseUnionBridgeLockingCap", + )?; let Some(new_cap) = crate::abi::Args::new(args).int(0) else { return int_result(gas_cost, UnionResponse::GenericError.code()); }; @@ -417,9 +423,11 @@ pub fn set_transfer_permissions( sender: Address, ) -> Result { let constants = union_constants(hardfork_cfg); - if sender != constants.change_transfer_permissions_authorizer { - return int_result(gas_cost, UnionResponse::UnauthorizedCaller.code()); - } + require_authorizer( + sender, + constants.change_transfer_permissions_authorizer, + "The sender is not authorized to call setUnionBridgeTransferPermissions", + )?; // rskj's `BoolType` looks at its word's low 32 bits only, and an empty // argument list reads as two falses. let args = crate::abi::Args::new(args); @@ -501,6 +509,21 @@ pub fn clear_event( Ok(PrecompileOutput::new(gas_cost, Bytes::new())) } +/// rskj `Bridge.executeIfAuthorized`: the executor of a method that only its +/// authorizer may call throws a `VMException` for anyone else, before the +/// method runs. `UNAUTHORIZED_CALLER` is not its answer but that of the two +/// methods that check for the union bridge contract themselves. +fn require_authorizer( + sender: Address, + authorizer: Address, + refusal: &'static str, +) -> Result<(), PrecompileError> { + if sender != authorizer { + return Err(PrecompileError::other(refusal)); + } + Ok(()) +} + /// rskj `UnionBridgeSupportImpl.validateCallerIsUnionBridge`, which throws. fn require_union_bridge_caller( ctx: &mut CTX, @@ -646,16 +669,60 @@ mod tests { assert_eq!(cap, current * U256::from(2u64)); } - /// Only the configured authorizer may move the cap. + /// rskj gives three methods an executor that throws for anyone but the + /// method's authorizer (`Bridge.executeIfAuthorized`), before the method + /// runs. None of them answers `UNAUTHORIZED_CALLER`: that is the answer of + /// the two methods that check for the union bridge contract themselves. #[test] - fn an_unauthorized_caller_cannot_move_the_locking_cap() { + fn the_authorizers_methods_throw_for_anyone_else() { let cfg = RskHardforkConfig::testnet(); + let stranger = Address::repeat_byte(0xAA); + let constants = union_constants(&cfg); + let mut c = ctx(); - let args = (one_rbtc() * U256::from(2u64)).to_be_bytes::<32>(); - let out = increase_locking_cap(&mut c, &args, 0, &cfg, Address::repeat_byte(0xAA)).unwrap(); - let code = i64::from_be_bytes(out.bytes[24..32].try_into().unwrap()); - assert_eq!(code, UnionResponse::UnauthorizedCaller.code()); + let cap = (one_rbtc() * U256::from(2u64)).to_be_bytes::<32>(); + assert!(increase_locking_cap(&mut c, &cap, 0, &cfg, stranger).is_err()); assert_eq!(locking_cap(&mut c, &cfg), one_rbtc(), "unchanged"); + // Each authorizer is its own method's alone. + let another = constants.change_transfer_permissions_authorizer; + assert!(increase_locking_cap(&mut c, &cap, 0, &cfg, another).is_err()); + + let both_off = [0u8; 64]; + assert!(set_transfer_permissions(&mut c, &both_off, 0, &cfg, stranger).is_err()); + assert!( + request_enabled(&mut c) && release_enabled(&mut c), + "unchanged" + ); + let authorizer = constants.change_transfer_permissions_authorizer; + let out = set_transfer_permissions(&mut c, &both_off, 0, &cfg, authorizer).unwrap(); + assert_eq!(code(out), UnionResponse::Success.code()); + + let mut address = [0u8; 32]; + address[12..].copy_from_slice(union_bridge().as_slice()); + assert!(set_contract_address_for_testnet(&mut c, &address, 0, &cfg, stranger).is_err()); + assert_eq!( + contract_address(&mut c, &cfg), + constants.address, + "unchanged" + ); + let authorizer = constants.change_address_authorizer; + let out = set_contract_address_for_testnet(&mut c, &address, 0, &cfg, authorizer).unwrap(); + assert_eq!(code(out), UnionResponse::Success.code()); + assert_eq!(contract_address(&mut c, &cfg), union_bridge()); + } + + /// `executeIfTestnetAndAuthorized` throws on mainnet before it asks who is + /// calling. + #[test] + fn the_contract_address_is_never_set_on_mainnet() { + let cfg = RskHardforkConfig::mainnet(); + let mut c = ctx(); + for caller in [ + Address::repeat_byte(0xAA), + union_constants(&cfg).change_address_authorizer, + ] { + assert!(set_contract_address_for_testnet(&mut c, &[0u8; 32], 0, &cfg, caller).is_err()); + } } // -- RSKIP529: the super and base events ------------------------------ From a19ce960e5ceddd573037285f50cf89deed766a7 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Thu, 12 Nov 2026 21:10:07 -0800 Subject: [PATCH 108/264] Execution: the secp256k1 contracts pad a short last word on the right RSKj's `Secp256k1ServiceBC` reads each coordinate and the scalar with `ByteUtil.parseWord`, which copies what the input has of a word to the word's start and leaves zeros after it. rustock's `parse_word` put the bytes at the word's end. An input that ends inside a number therefore gave a different number: a multiplication by the one byte 0x01 is by 2^248 in RSKj and was by 1 here. --- crates/execution/src/precompiles.rs | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/crates/execution/src/precompiles.rs b/crates/execution/src/precompiles.rs index e5988dd..cd97467 100644 --- a/crates/execution/src/precompiles.rs +++ b/crates/execution/src/precompiles.rs @@ -379,14 +379,15 @@ fn secp256k1_mul_run(input: &[u8], gas_limit: u64) -> Result [u8; 32] { let mut word = [0u8; 32]; if offset < data.len() { let end = std::cmp::min(offset + 32, data.len()); let len = end - offset; - word[32 - len..].copy_from_slice(&data[offset..end]); + word[..len].copy_from_slice(&data[offset..end]); } word } @@ -2135,6 +2136,26 @@ mod tests { assert_eq!(result.bytes.as_ref(), expected.as_slice()); } + /// rskj reads each word with `ByteUtil.parseWord`, which pads a word the + /// input ends inside with zeros on the right: the number keeps the bytes + /// it has as its high ones. A scalar of the one byte 0x01 multiplies the + /// point by 2^248, not by 1. + #[test] + fn a_word_the_input_ends_inside_is_padded_on_the_right() { + let mut input = build_mul_input("1", V1Y, "0"); + input[64] = 0x01; + let by_two_to_the_248 = secp256k1_mul_run(&input, 10_000).unwrap(); + let cut_after_one_byte = secp256k1_mul_run(&input[..65], 10_000).unwrap(); + assert_eq!(cut_after_one_byte.bytes, by_two_to_the_248.bytes); + assert_ne!( + cut_after_one_byte.bytes.as_ref(), + build_output("1", V1Y).as_slice() + ); + + assert_eq!(parse_word(&[0xaa, 0xbb], 0)[..3], [0xaa, 0xbb, 0x00]); + assert_eq!(parse_word(&[0xaa; 40], 64), [0; 32]); + } + #[test] fn test_secp_mul_point_not_on_curve_fails() { let bad = decimal_to_32bytes( From 61a4fdda6334e8e06c0e04378261d5d2d00e53fa Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Thu, 12 Nov 2026 21:10:08 -0800 Subject: [PATCH 109/264] bridge: governance votes are the caller's, and a short commit hash throws rskj hands the Bridge the transaction it was called by. For a contract's call that is an internal transaction whose sender is the contract (Program.callToPrecompiledAddress), so every authorizer check in the Bridge (federation changes, the fee per kb, the locking cap, the lock whitelist) asks about the contract, and a federation vote is recorded under it. This node asked about the account that signed the block's transaction, so an authorizer's transaction that reached the Bridge through a contract was counted as the authorizer's vote, where rskj turns the contract away. execute_method now gives the governance methods the caller as their sender. rskj's executeVoteFederationChangeFunction wraps commitFederation's argument in a Keccak256 before anything else, and one of other than 32 bytes throws an IllegalArgumentException that voteFederationChange does not catch, so the call fails. This node answered -1, -2 or -3 from the pending federation instead. vote_federation_change now returns an error for it, after the authorizer check as in rskj. rskj reads a federator's keys with Bouncy Castle's decodePoint, which takes the hybrid encoding beside the compressed and the uncompressed ones. k256 refused it and the vote answered -10; the keys are now read with libsecp256k1, which takes all three. The single byte 00, which decodePoint reads as the point at infinity, is still refused. Tests: governance_authorizes_the_caller_not_the_transactions_sender, commit_federation_throws_for_a_hash_that_is_not_32_bytes and a_federators_key_comes_in_any_encoding_bouncy_castle_decodes, whose cases are recorded from the release's own Bouncy Castle. --- crates/execution/src/bridge/governance.rs | 167 +++++++++++++++++++--- crates/execution/src/bridge/mod.rs | 91 ++++++++++-- 2 files changed, 228 insertions(+), 30 deletions(-) diff --git a/crates/execution/src/bridge/governance.rs b/crates/execution/src/bridge/governance.rs index d890411..350beb9 100644 --- a/crates/execution/src/bridge/governance.rs +++ b/crates/execution/src/bridge/governance.rs @@ -126,7 +126,7 @@ pub fn create_federation( hardfork_cfg: &crate::hardfork::RskHardforkConfig, tx_ctx: &crate::precompiles::BridgeTxContext, ) -> Result { - let code = vote_federation_change(ctx, "create", vec![], config, hardfork_cfg, tx_ctx); + let code = vote_federation_change(ctx, "create", vec![], config, hardfork_cfg, tx_ctx)?; Ok(encode_int_result(gas_cost, code)) } @@ -142,7 +142,7 @@ pub fn add_federator_public_key( let Ok(pubkey) = decode_bytes_arg(args) else { return Ok(encode_int_result(gas_cost, -10)); }; - let code = vote_federation_change(ctx, "add", vec![pubkey], config, hardfork_cfg, tx_ctx); + let code = vote_federation_change(ctx, "add", vec![pubkey], config, hardfork_cfg, tx_ctx)?; Ok(encode_int_result(gas_cost, code)) } @@ -173,7 +173,7 @@ pub fn add_federator_public_key_multikey( config, hardfork_cfg, tx_ctx, - ); + )?; Ok(encode_int_result(gas_cost, code)) } @@ -189,7 +189,7 @@ pub fn commit_federation( let Ok(hash) = decode_bytes_arg(args) else { return Ok(encode_int_result(gas_cost, -10)); }; - let code = vote_federation_change(ctx, "commit", vec![hash], config, hardfork_cfg, tx_ctx); + let code = vote_federation_change(ctx, "commit", vec![hash], config, hardfork_cfg, tx_ctx)?; Ok(encode_int_result(gas_cost, code)) } @@ -201,10 +201,13 @@ pub fn rollback_federation( hardfork_cfg: &crate::hardfork::RskHardforkConfig, tx_ctx: &crate::precompiles::BridgeTxContext, ) -> Result { - let code = vote_federation_change(ctx, "rollback", vec![], config, hardfork_cfg, tx_ctx); + let code = vote_federation_change(ctx, "rollback", vec![], config, hardfork_cfg, tx_ctx)?; Ok(encode_int_result(gas_cost, code)) } +/// rskj `FederationSupportImpl.voteFederationChange`. `Err` is an exception +/// the change's function throws that isn't a `BridgeIllegalArgumentException`, +/// the one kind the vote catches: the call fails. fn vote_federation_change( ctx: &mut CTX, function: &str, @@ -212,28 +215,30 @@ fn vote_federation_change( config: &super::constants::BridgeConstants, hardfork_cfg: &crate::hardfork::RskHardforkConfig, tx_ctx: &crate::precompiles::BridgeTxContext, -) -> i64 { +) -> Result { let authorizers = federation_change_authorizers(config); if !authorizers.contains(&tx_ctx.rsk_sender) { - return -10; // UNAUTHORIZED_CALLER + return Ok(-10); // UNAUTHORIZED_CALLER } // Dry-run first; only register the vote when the call would succeed. - let dry = execute_federation_change(ctx, function, &args, true, config, hardfork_cfg); + let dry = execute_federation_change(ctx, function, &args, true, config, hardfork_cfg) + .map_err(PrecompileError::other)?; if dry != 1 { - return dry; + return Ok(dry); } let spec = super::vote::AbiCallSpec::new(function, args.clone()); let mut election = super::vote::Election::load(ctx, FEDERATION_ELECTION_KEY); if !election.vote(&spec, tx_ctx.rsk_sender) { - return -10; // GENERIC_ERROR (duplicate vote) + return Ok(-10); // GENERIC_ERROR (duplicate vote) } let required = authorizers.len() / 2 + 1; let mut code = 1i64; if let Some(winner) = election.winner(required) { - code = execute_federation_change(ctx, function, &args, false, config, hardfork_cfg); + code = execute_federation_change(ctx, function, &args, false, config, hardfork_cfg) + .map_err(PrecompileError::other)?; // create/commit/rollback clear the whole election inside the action; // clearWinners then removes the winning entry. if matches!(function, "create" | "commit" | "rollback") && code == 1 { @@ -243,10 +248,11 @@ fn vote_federation_change( } } election.store(ctx, FEDERATION_ELECTION_KEY); - code + Ok(code) } -/// rskj FederationSupportImpl.executeVoteFederationChangeFunction. +/// rskj FederationSupportImpl.executeVoteFederationChangeFunction. `Err` is +/// what it throws that `voteFederationChange` doesn't catch. fn execute_federation_change( ctx: &mut CTX, function: &str, @@ -254,14 +260,40 @@ fn execute_federation_change( dry_run: bool, config: &super::constants::BridgeConstants, hardfork_cfg: &crate::hardfork::RskHardforkConfig, +) -> Result { + // COMMIT wraps its argument in a `Keccak256` before it looks at anything, + // and one of other than 32 bytes is an `IllegalArgumentException`. + if function == "commit" && args[0].len() != 32 { + return Err("commitFederation: the pending federation's hash isn't 32 bytes"); + } + Ok(federation_change_code( + ctx, + function, + args, + dry_run, + config, + hardfork_cfg, + )) +} + +/// The code the change's function answers. +fn federation_change_code( + ctx: &mut CTX, + function: &str, + args: &[Vec], + dry_run: bool, + config: &super::constants::BridgeConstants, + hardfork_cfg: &crate::hardfork::RskHardforkConfig, ) -> i64 { let block_number = revm::context_interface::Block::number(ctx.block()).to::(); let multikey = hardfork_cfg.has_rskip123(block_number); - // FederationMember stores all keys COMPRESSED, however they were passed. + // FederationMember stores all keys COMPRESSED, however they were passed: + // compressed, uncompressed or hybrid, which Bouncy Castle's `decodePoint` + // reads alike. It reads the one byte 00 too, as the point at infinity, + // which is no key a member is stored with here. let parse_key = |bytes: &[u8]| -> Option<[u8; 33]> { - use k256::elliptic_curve::sec1::ToEncodedPoint; - let parsed = k256::PublicKey::from_sec1_bytes(bytes).ok()?; - parsed.to_encoded_point(true).as_bytes().try_into().ok() + let parsed = bitcoin::secp256k1::PublicKey::from_slice(bytes).ok()?; + Some(parsed.serialize()) }; match function { "create" => { @@ -1779,6 +1811,107 @@ mod tests { assert_eq!(out.bytes.to_vec(), answer(®test_genesis)); } + /// rskj wraps `commitFederation`'s argument in a `Keccak256` before it + /// looks at anything else (`executeVoteFederationChangeFunction`), and a + /// `Keccak256` of other than 32 bytes throws an `IllegalArgumentException` + /// nothing there catches. The call fails, where a hash of the right length + /// that names no pending federation is answered -1. + #[test] + fn commit_federation_throws_for_a_hash_that_is_not_32_bytes() { + use crate::abi::encode::{self, Arg}; + use crate::precompiles::BridgeTxContext; + use revm::MainContext; + + let config = super::super::constants::BridgeConstants::mainnet(); + let cfg = crate::hardfork::RskHardforkConfig::mainnet(); + let authorizer = + alloy_primitives::hex::decode(config.federation_change_authorizer_keys[0]).unwrap(); + let authorized = BridgeTxContext { + rsk_sender: super::super::federation::rsk_address_from_public_key(&authorizer).unwrap(), + ..BridgeTxContext::default() + }; + let commit = |hash: &[u8], tx_ctx: &BridgeTxContext| { + let mut ctx = + revm::Context::mainnet().with_chain(crate::raw_storage::RskChainExt::default()); + let args = encode::args(&[Arg::Bytes(hash)]); + commit_federation(&mut ctx, &args, 0, &config, &cfg, tx_ctx) + }; + let code = + |out: PrecompileOutput| i64::from_be_bytes(out.bytes[24..32].try_into().unwrap()); + + assert!(commit(&[0x11; 31], &authorized).is_err()); + assert!(commit(&[0x11; 33], &authorized).is_err()); + assert!(commit(&[], &authorized).is_err()); + assert_eq!(code(commit(&[0x11; 32], &authorized).unwrap()), -1); + // A caller who may not vote is turned away before the hash is read. + let stranger = BridgeTxContext::default(); + assert_eq!(code(commit(&[0x11; 31], &stranger).unwrap()), -10); + } + + /// rskj reads a federator's keys with Bouncy Castle's `decodePoint` + /// (`BtcECKey.fromPublicOnly`, `ECKey.fromPublicOnly`), which takes the + /// hybrid encoding, 06 or 07 and then x and y, beside the compressed and + /// the uncompressed ones, and `FederationMember` keeps each key compressed. + #[test] + fn a_federators_key_comes_in_any_encoding_bouncy_castle_decodes() { + use crate::abi::encode::{self, Arg}; + use crate::precompiles::BridgeTxContext; + use revm::MainContext; + + // The generator, whose y is even. + const GX: &str = "79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"; + const GY: &str = "483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8"; + let encoded = |prefix: u8, with_y: bool| { + let mut key = vec![prefix]; + key.extend(alloy_primitives::hex::decode(GX).unwrap()); + if with_y { + key.extend(alloy_primitives::hex::decode(GY).unwrap()); + } + key + }; + let config = super::super::constants::BridgeConstants::mainnet(); + let cfg = crate::hardfork::RskHardforkConfig::mainnet(); + let block = 2_000_000u64; + assert!(cfg.has_rskip123(block)); + let authorizer = + alloy_primitives::hex::decode(config.federation_change_authorizer_keys[0]).unwrap(); + let authorized = BridgeTxContext { + rsk_sender: super::super::federation::rsk_address_from_public_key(&authorizer).unwrap(), + ..BridgeTxContext::default() + }; + // One authorizer's vote to add the key, as all three of a member's, to + // a pending federation that has no members yet. + let add = |key: &[u8]| { + let mut ctx = revm::Context::mainnet() + .with_block(revm::context::BlockEnv { + number: alloy_primitives::U256::from(block), + ..Default::default() + }) + .with_chain(crate::raw_storage::RskChainExt::default()); + store_pending_federation(&mut ctx, Some(&[]), true); + let args = encode::args(&[Arg::Bytes(key), Arg::Bytes(key), Arg::Bytes(key)]); + let out = + add_federator_public_key_multikey(&mut ctx, &args, 0, &config, &cfg, &authorized) + .unwrap(); + i64::from_be_bytes(out.bytes[24..32].try_into().unwrap()) + }; + assert_eq!(add(&encoded(0x02, false)), 1, "compressed"); + assert_eq!(add(&encoded(0x03, false)), 1, "compressed, the other y"); + assert_eq!(add(&encoded(0x04, true)), 1, "uncompressed"); + assert_eq!(add(&encoded(0x06, true)), 1, "hybrid"); + assert_eq!(add(&encoded(0x07, true)), -10, "hybrid, naming the other y"); + assert_eq!( + add(&encoded(0x03, true)), + -10, + "a compressed key's prefix on 65 bytes" + ); + assert_eq!( + add(&encoded(0x04, false)), + -10, + "an uncompressed key's prefix on 33" + ); + } + /// A Bridge call that reads the whitelist writes it back as the call /// returns, whatever it answers (upstream #291): `save()` writes whatever /// the call loaded. The changes read it after the sender check, the adds diff --git a/crates/execution/src/bridge/mod.rs b/crates/execution/src/bridge/mod.rs index ce7e72a..7f67a0e 100644 --- a/crates/execution/src/bridge/mod.rs +++ b/crates/execution/src/bridge/mod.rs @@ -867,6 +867,15 @@ fn execute_method( } } + // rskj hands the Bridge the transaction that called it. For a contract's + // call that is an internal transaction whose sender is the contract + // (`Program.callToPrecompiledAddress`), not the account that signed the + // transaction the block holds. + let as_called = BridgeTxContext { + rsk_sender: caller, + ..tx_ctx.clone() + }; + match method_name { // Phase 2: BTC header chain "receiveHeader" => { @@ -964,20 +973,21 @@ fn execute_method( "updateCollections" => peg::update_collections(ctx, gas_cost, config, hardfork_cfg, tx_ctx), "addSignature" => peg::add_signature(ctx, args, gas_cost, config, hardfork_cfg), - // Phase 6: Governance - "createFederation" => governance::create_federation(ctx, gas_cost, config, hardfork_cfg, tx_ctx), - "commitFederation" => governance::commit_federation(ctx, args, gas_cost, config, hardfork_cfg, tx_ctx), - "rollbackFederation" => governance::rollback_federation(ctx, gas_cost, config, hardfork_cfg, tx_ctx), - "addFederatorPublicKey" => governance::add_federator_public_key(ctx, args, gas_cost, config, hardfork_cfg, tx_ctx), - "addFederatorPublicKeyMultikey" => governance::add_federator_public_key_multikey(ctx, args, gas_cost, config, hardfork_cfg, tx_ctx), - "voteFeePerKbChange" => governance::vote_fee_per_kb_change(ctx, args, gas_cost, config, tx_ctx), - "increaseLockingCap" => governance::increase_locking_cap(ctx, args, gas_cost, config, tx_ctx), - "addLockWhitelistAddress" => governance::add_lock_whitelist_address(ctx, args, gas_cost, config, hardfork_cfg, tx_ctx), - "addOneOffLockWhitelistAddress" => governance::add_one_off_lock_whitelist_address(ctx, args, gas_cost, config, hardfork_cfg, tx_ctx), - "addUnlimitedLockWhitelistAddress" => governance::add_unlimited_lock_whitelist_address(ctx, args, gas_cost, config, hardfork_cfg, tx_ctx), - "removeLockWhitelistAddress" => governance::remove_lock_whitelist_address(ctx, args, gas_cost, config, hardfork_cfg, tx_ctx), + // Phase 6: Governance, each method of which asks an authorizer about + // the transaction it was called by: `as_called`. + "createFederation" => governance::create_federation(ctx, gas_cost, config, hardfork_cfg, &as_called), + "commitFederation" => governance::commit_federation(ctx, args, gas_cost, config, hardfork_cfg, &as_called), + "rollbackFederation" => governance::rollback_federation(ctx, gas_cost, config, hardfork_cfg, &as_called), + "addFederatorPublicKey" => governance::add_federator_public_key(ctx, args, gas_cost, config, hardfork_cfg, &as_called), + "addFederatorPublicKeyMultikey" => governance::add_federator_public_key_multikey(ctx, args, gas_cost, config, hardfork_cfg, &as_called), + "voteFeePerKbChange" => governance::vote_fee_per_kb_change(ctx, args, gas_cost, config, &as_called), + "increaseLockingCap" => governance::increase_locking_cap(ctx, args, gas_cost, config, &as_called), + "addLockWhitelistAddress" => governance::add_lock_whitelist_address(ctx, args, gas_cost, config, hardfork_cfg, &as_called), + "addOneOffLockWhitelistAddress" => governance::add_one_off_lock_whitelist_address(ctx, args, gas_cost, config, hardfork_cfg, &as_called), + "addUnlimitedLockWhitelistAddress" => governance::add_unlimited_lock_whitelist_address(ctx, args, gas_cost, config, hardfork_cfg, &as_called), + "removeLockWhitelistAddress" => governance::remove_lock_whitelist_address(ctx, args, gas_cost, config, hardfork_cfg, &as_called), "setLockWhitelistDisableBlockDelay" => { - governance::set_lock_whitelist_disable_block_delay(ctx, args, gas_cost, config, hardfork_cfg, tx_ctx) + governance::set_lock_whitelist_disable_block_delay(ctx, args, gas_cost, config, hardfork_cfg, &as_called) } // Transaction-callable read-only @@ -1541,6 +1551,61 @@ mod tests { } } + /// rskj hands the Bridge the transaction that called it: for a contract's + /// call, an internal one whose sender is the contract + /// (`Program.callToPrecompiledAddress`). The governance methods ask their + /// authorizers about that sender, so an authorizer's key is not authorized + /// from behind a contract that forwards its call. + #[test] + fn governance_authorizes_the_caller_not_the_transactions_sender() { + use crate::abi::encode::{self, Arg}; + let (config, cfg) = (BridgeConstants::mainnet(), RskHardforkConfig::mainnet()); + let address_of = |key: &str| { + let key = alloy_primitives::hex::decode(key).unwrap(); + federation::rsk_address_from_public_key(&key).unwrap() + }; + let forwarder = alloy_primitives::Address::repeat_byte(0x77); + let code = |method: &str, args: &[u8], origin, caller| { + let mut ctx = mainnet_at(1); + let tx_ctx = BridgeTxContext { + rsk_sender: origin, + ..Default::default() + }; + let out = execute_method( + &mut ctx, + method, + args, + 0, + &config, + None, + false, + &cfg, + &tx_ctx, + caller, + 0, + U256::ZERO, + ) + .unwrap(); + i64::from_be_bytes(out.bytes[24..32].try_into().unwrap()) + }; + + let authorizer = address_of(config.federation_change_authorizer_keys[0]); + assert_eq!(code("createFederation", &[], authorizer, authorizer), 1); + assert_eq!(code("createFederation", &[], authorizer, forwarder), -10); + assert_eq!(code("createFederation", &[], forwarder, authorizer), 1); + + let fee_authorizer = address_of(config.fee_per_kb_authorizer_keys[0]); + let fee = encode::args(&[Arg::Word(encode::int(1_000))]); + assert_eq!( + code("voteFeePerKbChange", &fee, fee_authorizer, fee_authorizer), + 1 + ); + assert_eq!( + code("voteFeePerKbChange", &fee, fee_authorizer, forwarder), + -10 + ); + } + /// rskj Bridge.receiveHeadersGetCost groundtruth: 22_000 pre-RSKIP124; /// post-RSKIP132 (mainnet wasabi100) 25_000 + (n-1) * 3_500. #[test] From a8d274fe48fd43f4f9685a71ac10bb00696e1f54 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Thu, 12 Nov 2026 21:10:08 -0800 Subject: [PATCH 110/264] bridge: a merkle tree bitcoinj refuses is an illegal argument rskj's validationsForRegisterBtcTransaction builds and walks the partial merkle tree inside a try whose catch rethrows bitcoinj's VerificationException, and the ProtocolException that extends it, as a BridgeIllegalArgumentException (BridgeSupport l.3389-3396). Before RSKIP88 Bridge.execute answers that exception with null: the call succeeds with no output and saves nothing. This node treated the two failures as any other throw and failed the call at every height, so a federator's registerBtcTransaction before RSKIP88 with such a tree was charged its whole gas limit here and its gas used in rskj. Both failures are now Invalid::IllegalArgument, as a tree of the wrong size already was. The flyover method answers GENERIC_ERROR either way. Tests: pegin_validations_answer_as_rskj_in_its_order gains a tree of a block with no transactions, and before_rskip88_a_tree_rskj_refuses_is_a_success_with_no_output runs that tree beside the one of the wrong size. --- crates/execution/src/bridge/peg.rs | 100 +++++++++++++++++------------ 1 file changed, 60 insertions(+), 40 deletions(-) diff --git a/crates/execution/src/bridge/peg.rs b/crates/execution/src/bridge/peg.rs index c1d8875..47fafa5 100644 --- a/crates/execution/src/bridge/peg.rs +++ b/crates/execution/src/bridge/peg.rs @@ -102,12 +102,14 @@ fn pegin_below_minimum(live_output_values: &[u64], min: u64, rskip293: bool) -> enum Invalid { /// `return false`. Swallowed(&'static str), - /// A throw: `VerificationException`, `BlockStoreException`, or the - /// `NullPointerException` of a block the store does not have. + /// A throw of anything else: the `VerificationException` of a transaction + /// without inputs, a `BlockStoreException`, or the `NullPointerException` + /// of a block the store does not have. Thrown(&'static str), - /// A throw of `BridgeIllegalArgumentException`: as `Thrown` from RSKIP88, - /// but before it `Bridge.execute` answers it with null, a success with no - /// output. + /// A throw of `BridgeIllegalArgumentException`, which is also what a + /// partial merkle tree's `VerificationException` is rethrown as: as + /// `Thrown` from RSKIP88, but before it `Bridge.execute` answers it with + /// null, a success with no output. IllegalArgument(&'static str), } @@ -146,13 +148,16 @@ fn validations_for_register_btc_transaction( "PartialMerkleTree doesn't have expected size", )); } - // `BridgeUtils.calculateMerkleRoot` builds the tree and walks it, and - // rethrows a VerificationException from either. A transaction that is not - // among its matches gets null back, which is false. - let pmt = PartialMerkleTree::parse(pmt_data).ok_or(Invalid::Thrown("failed to parse PMT"))?; + // `BridgeUtils.calculateMerkleRoot` builds the tree and walks it, and a + // VerificationException from either, bitcoinj's ProtocolException being + // one, is rethrown as a `BridgeIllegalArgumentException` (BridgeSupport + // l.3389-3396). A transaction that is not among its matches gets null + // back, which is false. + let pmt = PartialMerkleTree::parse(pmt_data) + .ok_or(Invalid::IllegalArgument("failed to parse PMT"))?; let pmt_result = pmt .extract_matches() - .ok_or(Invalid::Thrown("PMT verification failed"))?; + .ok_or(Invalid::IllegalArgument("PMT verification failed"))?; if !pmt_result.matched_hashes.contains(btc_tx_hash) { return Err(Invalid::Swallowed("tx not in PMT")); } @@ -9039,6 +9044,15 @@ mod tests { // Then the tree: the wrong size throws BridgeIllegalArgumentException, // and a tree without the transaction is false. assert_eq!(run(0, &garbage, &ONE_INPUT).0, "illegal argument"); + // So does a tree of the right size that bitcoinj will not walk, here + // one of a block with no transactions: rskj rethrows bitcoinj's + // VerificationException as one. + let mut no_transactions = one_leaf_tree(&ONE_INPUT); + no_transactions[..4].copy_from_slice(&0u32.to_le_bytes()); + assert_eq!( + run(0, &no_transactions, &ONE_INPUT), + ("illegal argument", "PMT verification failed") + ); assert_eq!( run(0, &one_leaf_tree(&NO_INPUTS), &ONE_INPUT), ("false", "tx not in PMT") @@ -9566,43 +9580,49 @@ mod tests { /// rskj's `validationsForRegisterBtcTransaction` throws /// `BridgeIllegalArgumentException` for a partial merkle tree of the wrong - /// size, which `Bridge.execute` answers with null before RSKIP88: the call - /// succeeds with no output, charged its gas, and registers nothing. From - /// RSKIP88 it throws. + /// size, and for one bitcoinj will not walk, whose `VerificationException` + /// it rethrows as one. `Bridge.execute` answers that with null before + /// RSKIP88: the call succeeds with no output, charged its gas, and + /// registers nothing. From RSKIP88 it throws. #[test] - fn before_rskip88_a_tree_of_the_wrong_size_is_a_success_with_no_output() { + fn before_rskip88_a_tree_rskj_refuses_is_a_success_with_no_output() { use revm::MainContext; let (config, cfg) = (BridgeConstants::mainnet(), RskHardforkConfig::mainnet()); assert!(!cfg.has_rskip88(728_999) && cfg.has_rskip88(729_000)); let (_, checkpoint, _) = config.btc_checkpoint.unwrap(); let tx = tx_with(vec![], vec![]); let raw = btc_serialize(&tx); - // Confirmed well past the minimum, so the tree's size is the check - // that answers. - let args = encode::args(&[ - Arg::Bytes(&raw), - Arg::Word(encode::int(i64::from(checkpoint) - 200)), - Arg::Bytes(&[0u8; 3]), - ]); - let at = |number: u64| { - let mut ctx = - revm::Context::mainnet().with_chain(crate::raw_storage::RskChainExt::default()); - ctx.block.number = U256::from(number); - let out = register_btc_transaction( - &mut ctx, - &args, - 7_000, - &config, - &cfg, - &BridgeTxContext::default(), - ); - (out, is_btc_tx_processed(&mut ctx, &legacy_btc_txid(&tx))) - }; - let (out, processed) = at(728_999); - let out = out.expect("a success before RSKIP88"); - assert!(out.bytes.is_empty() && !processed); - assert_eq!(out.gas_used, 7_000); - assert!(at(729_000).0.is_err(), "a throw from RSKIP88"); + // Of the right size, and of a block with no transactions. + let mut no_transactions = one_leaf_tree(&raw); + no_transactions[..4].copy_from_slice(&0u32.to_le_bytes()); + for tree in [&[0u8; 3][..], &no_transactions] { + // Confirmed well past the minimum, so the tree is the check that + // answers. + let args = encode::args(&[ + Arg::Bytes(&raw), + Arg::Word(encode::int(i64::from(checkpoint) - 200)), + Arg::Bytes(tree), + ]); + let at = |number: u64| { + let mut ctx = + revm::Context::mainnet().with_chain(crate::raw_storage::RskChainExt::default()); + ctx.block.number = U256::from(number); + let out = register_btc_transaction( + &mut ctx, + &args, + 7_000, + &config, + &cfg, + &BridgeTxContext::default(), + ); + (out, is_btc_tx_processed(&mut ctx, &legacy_btc_txid(&tx))) + }; + let (out, processed) = at(728_999); + let out = out.expect("a success before RSKIP88"); + assert!(out.bytes.is_empty() && !processed); + assert_eq!(out.gas_used, 7_000); + assert!(at(729_000).0.is_err(), "a throw from RSKIP88"); + } } /// `registerBtcTransaction` of `tx` at Bitcoin `height` in `ctx`, with RSKj's From fd526ebb57d049fd2b2d67f14c9afd83b7367ac7 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Thu, 12 Nov 2026 21:10:08 -0800 Subject: [PATCH 111/264] validation: a header without sublist edges fails the edges rule rskj's ValidTxExecutionSublistsEdgesRule reads edges.length with no null check where RSKIP351 and RSKIP144 are active. A header's edges are null where its extension came beside the block's body without them: BlockHeaderExtensionV1.fromEncoded gives null for an extension of one element, BlockHeaderExtensionV2 for one of two. A miner can commit to such an extension, since its hash is well defined. The rule then throws a NullPointerException, BlockChainImpl.tryToConnect answers INVALID_BLOCK, and the block is not imported. validate_tx_execution_sublists_edges answered Ok for a header without edges, so this node imported the block rskj refuses. It now refuses it where the rule is active. Its own blocks are unaffected: the template builder writes an empty edges field wherever the rule is active, and a header read in full has the field by rskj's own size check. Tests: absent_edges_are_refused_and_distinct_from_empty, and the_sublist_edges_are_checked_with_the_body_rules now expects the refusal. --- crates/core/src/validation/block_rules.rs | 35 ++++++++++++++++------- crates/execution/src/processor.rs | 8 ++++-- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/crates/core/src/validation/block_rules.rs b/crates/core/src/validation/block_rules.rs index 3902c16..feaf5a8 100644 --- a/crates/core/src/validation/block_rules.rs +++ b/crates/core/src/validation/block_rules.rs @@ -445,10 +445,14 @@ pub const TX_EXECUTION_THREADS: usize = 2; /// Validates the sublist edges against the block's transaction count. /// -/// `edges` is `None` for a version-0 header, and `Some(&[])` means "all -/// transactions in the sequential sublist" — both are valid and both return -/// `Ok`. The distinction is preserved because the two encode differently and -/// therefore hash differently. +/// `Some(&[])` means "all transactions in the sequential sublist", and is +/// valid. `edges` is `None` for a header that has none: one from before the +/// rule, or one whose extension came beside the block's body without them +/// (rskj `BlockHeaderExtensionV1.fromEncoded`, `BlockHeaderExtensionV2` +/// alike). rskj's rule reads `edges.length` with no null check, so where it +/// is active a header without edges makes it throw, and the block is not +/// imported. The distinction from empty is preserved because the two encode +/// differently and therefore hash differently. pub fn validate_tx_execution_sublists_edges( edges: Option<&[i16]>, transaction_count: usize, @@ -457,7 +461,11 @@ pub fn validate_tx_execution_sublists_edges( if !active { return Ok(()); } - let Some(edges) = edges else { return Ok(()) }; + let Some(edges) = edges else { + return Err(ValidationError::InvalidSublistEdges { + reason: "the header has no execution list edges".to_string(), + }); + }; if edges.is_empty() { return Ok(()); } @@ -536,12 +544,19 @@ mod sublist_edges_tests { assert!(check(Some(&[])).is_ok()); } - /// A version-0 header has no edges at all. Distinct from empty, and also - /// valid -- the two encode and hash differently, so the distinction is - /// carried rather than flattened. + /// Not in rskj's suite. A header has no edges at all where its extension + /// came without them (`BlockHeaderExtensionV1.fromEncoded` gives null for + /// an extension of one element, `V2` for one of two). rskj's rule reads + /// `edges.length` with no null check, so it throws and the block is not + /// imported. Distinct from empty, which is every transaction in the + /// sequential sublist and valid. #[test] - fn absent_edges_are_valid_and_distinct_from_empty() { - assert!(check(None).is_ok()); + fn absent_edges_are_refused_and_distinct_from_empty() { + assert!(matches!( + check(None), + Err(ValidationError::InvalidSublistEdges { .. }) + )); + assert!(check(Some(&[])).is_ok()); } /// rskj `blockWithTooManyEdges`. The bound is rskj's diff --git a/crates/execution/src/processor.rs b/crates/execution/src/processor.rs index 76234e0..668c029 100644 --- a/crates/execution/src/processor.rs +++ b/crates/execution/src/processor.rs @@ -1726,10 +1726,12 @@ mod tests { testnet.validate_block_body_rules(&block(Some(vec![])), None), Ok(()) ); - assert_eq!( + // A header whose extension came without edges: rskj's rule throws on + // the null, and the block is not imported. + assert!(matches!( testnet.validate_block_body_rules(&block(None), None), - Ok(()) - ); + Err(ValidationError::InvalidSublistEdges { .. }) + )); // With rskip351 moved off reed810, rskj runs the block as one list and // never reads its edges. From 2c7420e7810ca63ae026dd69dbf2c03bc36b584d Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Thu, 12 Nov 2026 21:10:09 -0800 Subject: [PATCH 112/264] precompiles: a CALLCODE or DELEGATECALL creates no account for the precompile rskj's Program.callToPrecompiledAddress sends the call's value, zero included, to the context address, and that transfer is what creates a precompile's account the first time it is called. For CALLCODE and DELEGATECALL the context address is the caller itself (msg.getType().isStateless()), so the precompile is left with the storage-prefix marker setupContract writes for the code address and no account. This node touched the precompile's account for every kind of call, so a stateless call to a precompile nobody had called yet wrote an empty account rskj does not have: a different state root, and a first CALL to it that pays NEW_ACCT_CALL in rskj only. Both dispatch paths now touch the context address. With that gone, REMASC's getStateForDebugging would leave REMASC without an account after a DELEGATECALL in a chain's first block, where rskj gives it one: RemascContract.execute saves after every method that returns, RemascStorageProvider.save always writes the siblings cell, and addStorageBytes creates the account of a contract that has none. The method now writes that cell and touches REMASC's account as save does. Tests: a_stateless_call_to_a_precompile_creates_no_account_for_it and remascs_state_for_debugging_saves_as_any_remasc_method_does. --- crates/execution/src/executor.rs | 110 ++++++++++++++++++++++++++++ crates/execution/src/precompiles.rs | 44 ++++++++--- 2 files changed, 142 insertions(+), 12 deletions(-) diff --git a/crates/execution/src/executor.rs b/crates/execution/src/executor.rs index 5eb0563..8e0b5cc 100644 --- a/crates/execution/src/executor.rs +++ b/crates/execution/src/executor.rs @@ -5711,6 +5711,116 @@ mod tests { ); } + /// One transaction to a contract holding `code`, in a state where no + /// precompile has an account yet. Answers, for `precompile` afterwards: + /// whether it has an account, its storage-prefix marker, and the cell at + /// `slot` of its storage. + fn after_a_call_from_code( + code: &[u8], + precompile: Address, + slot: U256, + ) -> (bool, Option>, Option>) { + let sender = Address::repeat_byte(0xAA); + let contract = Address::repeat_byte(0xCC); + let store = Arc::new(MemoryTrieStore::new()); + let root = TrieNode::empty(); + let one_rbtc = U256::from(10u64).pow(U256::from(18)); + let root = put_account(&root, store.as_ref(), &sender, 0, one_rbtc); + let root = put_account(&root, store.as_ref(), &contract, 1, U256::ZERO); + let root = root.put( + &TrieKeySlice::from_key(&rustock_trie::code_key(&contract)), + code, + store.as_ref(), + ); + let block_store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); + let tx = rustock_core::Transaction { + nonce: 0, + gas_price: U256::from(0), + gas_limit: U256::from(200_000), + to: Bytes::copy_from_slice(contract.as_slice()), + value: U256::ZERO, + input: Bytes::new(), + v: 0, + r: U256::ZERO, + s: U256::ZERO, + cached_rlp: None, + }; + let result = executor + .execute_block( + &dummy_header(466_503), + &[(tx, sender)], + &root, + store.clone(), + ) + .expect("block"); + assert!(result.tx_results[0].success); + let root = crate::state::apply_state_changes( + &root, + store.as_ref(), + &result.state_changes, + &result.markers, + ); + let cell = |key: Vec| root.get(&TrieKeySlice::from_key(&key), store.as_ref()); + ( + cell(rustock_trie::account_key(&precompile)).is_some(), + cell(rustock_trie::storage_prefix_key(&precompile)), + cell(rustock_trie::storage_key(&precompile, &B256::from(slot))), + ) + } + + /// rskj `Program.callToPrecompiledAddress`: the zero-value transfer that + /// creates a precompile's account goes to the call's context address, + /// which for CALLCODE and DELEGATECALL is the caller itself + /// (`msg.getType().isStateless()`). Those leave the precompile with the + /// marker `setupContract` writes for the code address and no account, so + /// the first CALL to it still pays NEW_ACCT_CALL. + #[test] + fn a_stateless_call_to_a_precompile_creates_no_account_for_it() { + let identity = Address::with_last_byte(0x04); + // The call with no input, no output and 3000 gas, then STOP. CALL and + // CALLCODE take a value, here zero; DELEGATECALL takes none. + let after = |opcode: u8| { + let mut code = vec![0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x60, 0x00]; + if opcode != 0xf4 { + code.extend_from_slice(&[0x60, 0x00]); + } + code.push(0x73); + code.extend_from_slice(identity.as_slice()); + code.extend_from_slice(&[0x61, 0x0b, 0xb8, opcode, 0x00]); + let (account, marker, _) = after_a_call_from_code(&code, identity, U256::ZERO); + (account, marker) + }; + let marker = Some(vec![1]); + assert_eq!(after(0xf1), (true, marker.clone()), "CALL"); + assert_eq!(after(0xf2), (false, marker.clone()), "CALLCODE"); + assert_eq!(after(0xf4), (false, marker), "DELEGATECALL"); + } + + /// rskj `RemascContract.execute` saves after every method that returns, + /// `getStateForDebugging` included, and `RemascStorageProvider.save` always + /// writes the siblings cell, through `addStorageBytes`, which creates the + /// account of a contract that has none. So REMASC has its account after a + /// DELEGATECALL too, which by itself would create none. + #[test] + fn remascs_state_for_debugging_saves_as_any_remasc_method_does() { + // The selector to memory, then DELEGATECALL(3000, REMASC, 28, 4, 0, 0). + let mut code = vec![0x63]; + code.extend_from_slice(&crate::remasc::GET_STATE_FOR_DEBUGGING); + code.extend_from_slice(&[0x60, 0x00, 0x52]); + code.extend_from_slice(&[0x60, 0x00, 0x60, 0x00, 0x60, 0x04, 0x60, 0x1c, 0x73]); + code.extend_from_slice(REMASC_ADDR.as_slice()); + code.extend_from_slice(&[0x61, 0x0b, 0xb8, 0xf4, 0x00]); + assert_eq!( + after_a_call_from_code( + &code, + REMASC_ADDR, + crate::remasc::remasc_storage_key("siblings") + ), + (true, Some(vec![1]), Some(vec![0xc0])) + ); + } + /// rskj `doSUICIDE` (VM.java) charges NEW_ACCT_SUICIDE (25,000) whenever the /// beneficiary does not exist (`!track.isExist`), UNCONDITIONAL on the /// suiciding contract's balance. revm's selfdestruct instead gates the diff --git a/crates/execution/src/precompiles.rs b/crates/execution/src/precompiles.rs index cd97467..a93eef0 100644 --- a/crates/execution/src/precompiles.rs +++ b/crates/execution/src/precompiles.rs @@ -867,14 +867,17 @@ impl RskPrecompileProvider { }; // rskj Program.callToPrecompiledAddress: the zero-value transfer - // CREATES the precompile account node on first call (isExist becomes - // true, so NEW_ACCT_CALL is charged at most once per address ever). - // Touching the loaded account persists it under the pinned - // pre-spurious journal semantics. + // CREATES the account of the call's context address on first call + // (isExist becomes true, so NEW_ACCT_CALL is charged at most once per + // address ever). That is the precompile for CALL and STATICCALL, and + // the caller itself for CALLCODE and DELEGATECALL + // (`msg.getType().isStateless()`), which leave the precompile without + // an account. Touching the loaded account persists it under the + // pinned pre-spurious journal semantics. { use revm::context_interface::JournalTr; - let _ = context.journal_mut().load_account(*addr); - context.journal_mut().touch_account(*addr); + let _ = context.journal_mut().load_account(inputs.target_address); + context.journal_mut().touch_account(inputs.target_address); } let exec_result = if *addr == BRIDGE_ADDR { @@ -1042,8 +1045,8 @@ impl RskPrecompileProvider { /// here is a user tx or an internal CALL. rskj (Remasc.processMinersFees) /// throws RemascInvalidInvocationException for those, EXCEPT /// getStateForDebugging(), which has no caller guard and answers with the - /// contract's state. (Its `save()` also writes the `siblings` cell, which - /// every block's REMASC transaction has written since block 1.) + /// contract's state, and then saves as every REMASC method that returns + /// does (`RemascContract.execute`). fn run_remasc( &self, context: &mut CTX, @@ -1053,6 +1056,22 @@ impl RskPrecompileProvider { // `RemascContract.getFunction` takes a selector of exactly four bytes. if input == crate::remasc::GET_STATE_FOR_DEBUGGING { let state = crate::remasc::state_for_debugging(context); + // `RemascStorageProvider.save`: the method set no balance and no + // flag, so the siblings cell is all it writes, as it always does + // (`saveSiblings`). It writes through `addStorageBytes`, which + // creates REMASC's account where there is none: the one thing + // of this a later call can tell, since every block's REMASC + // transaction has written the same cell since block 1. + { + use revm::context_interface::JournalTr; + let _ = context.journal_mut().load_account(REMASC_ADDR); + context.journal_mut().touch_account(REMASC_ADDR); + } + context.chain_mut().raw_storage.put( + REMASC_ADDR, + crate::remasc::remasc_storage_key("siblings"), + Some(vec![alloy_rlp::EMPTY_LIST_CODE]), + ); return Ok(PrecompileOutput::new(0, state.into())); } // Everything else throws in rskj: invisible for a direct tx @@ -1219,12 +1238,13 @@ impl PrecompileProvider for RskPrecompileProvider return Ok(None); }; - // rskj creates the precompile account node on first call (see the - // stateful path above). + // rskj creates the account of the call's context address on first + // call: the precompile's, or for CALLCODE and DELEGATECALL the + // caller's own (see the stateful path above). { use revm::context_interface::JournalTr; - let _ = context.journal_mut().load_account(addr); - context.journal_mut().touch_account(addr); + let _ = context.journal_mut().load_account(inputs.target_address); + context.journal_mut().touch_account(inputs.target_address); } let rskip197 = self.rskip197_active(context); From b2767fd1357a98bf8e54ffa464c1f1e48bd42ae2 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Thu, 12 Nov 2026 21:10:09 -0800 Subject: [PATCH 113/264] bridge: a contract's call ends as rskj ends it in each era Bridge.execute's two gates have their own activations, and what the VM makes of the Bridge's answer changed three times. This node applied the gates at every height and treated every answer alike. - Before RSKIP88 Bridge.getGasForData throws a NullPointerException for any call from a contract. Program.callToPrecompiledAddress asks for the gas outside every try, so the contract that made the call ends, its gas spent. This node ran the call. - validateLocalCall asks only from RSKIP88. Before it a transaction's own call to a local-only method runs it; this node threw. - validateCallMessageType asks only from RSKIP417. Before it a STATICCALL, CALLCODE or DELEGATECALL reaches any method; this node refused them. For DELEGATECALL the Bridge is now given the calling contract and no value, as rskj's internal transaction has them, where revm's inputs name the delegating frame's caller and value. - No data is releaseBtc and goes through the same gates. This node returned early for it, so from RSKIP417 a STATICCALL, CALLCODE or DELEGATECALL with no data ran releaseBtc where rskj refuses the call. - A method with nothing to return answers null before RSKIP417 (calculateVoidReturnValue). For a contract's call, Program.memorySave reads the null's length before RSKIP150 and the contract ends; saveOutAfterExecution skips it from RSKIP150; and from RSKIP197 executePrecompiledAndHandleError hands it to Memory.write inside the try, so the call fails for its required gas and its track is rolled back. This node answered success throughout. - The Bridge writes its logs straight into the list of the program that called it (callToPrecompiledAddress hands it result.getLogInfoList()), and the catch rolls back the call's track and nothing else, so the logs of a failed call stay with the caller. revm drops them with the call's checkpoint; they are now written again as the caller's. Tests: a_contracts_call_to_the_bridge_ends_as_rskj_ends_it_in_each_era and before_rskip88_a_transaction_runs_a_local_only_bridge_method, at mainnet's heights. --- crates/execution/src/bridge/btc_chain.rs | 10 +- crates/execution/src/bridge/mod.rs | 106 +++++++++++----- crates/execution/src/executor.rs | 155 +++++++++++++++++++++++ crates/execution/src/hardfork.rs | 8 ++ crates/execution/src/precompiles.rs | 48 ++++++- crates/execution/src/raw_storage.rs | 8 ++ crates/execution/src/rsk_handler.rs | 14 ++ 7 files changed, 313 insertions(+), 36 deletions(-) diff --git a/crates/execution/src/bridge/btc_chain.rs b/crates/execution/src/bridge/btc_chain.rs index e570c3d..3dfb24c 100644 --- a/crates/execution/src/bridge/btc_chain.rs +++ b/crates/execution/src/bridge/btc_chain.rs @@ -593,10 +593,12 @@ pub fn receive_headers( // rskj `receiveHeaders` is a VOID method (BridgeMethods.RECEIVE_HEADERS // declares no output, Bridge::receiveHeaders is BridgeMethodExecutorVoid). // Bridge.execute therefore returns the void value — `null` before RSKIP417 - // and EMPTY_BYTE_ARRAY afterwards (Bridge.calculateVoidReturnValue) — both - // of which give the caller RETURNDATASIZE 0. Returning a 32-byte int here - // makes a calling contract decode 32 bytes where rskj sees 0, forking the - // execution (mainnet #6,223,768 tx[0]: relay 0x82494fb1 OOGs vs success). + // and EMPTY_BYTE_ARRAY afterwards (Bridge.calculateVoidReturnValue). No + // output here stands for both; what a contract's call makes of the null, + // which differs by era, is `execute_bridge`'s to say. Returning a 32-byte + // int here makes a calling contract decode 32 bytes where rskj sees 0, + // forking the execution (mainnet #6,223,768 tx[0]: relay 0x82494fb1 OOGs + // vs success). Ok(PrecompileOutput::new(gas_cost, Bytes::new())) } diff --git a/crates/execution/src/bridge/mod.rs b/crates/execution/src/bridge/mod.rs index 7f67a0e..d649b9c 100644 --- a/crates/execution/src/bridge/mod.rs +++ b/crates/execution/src/bridge/mod.rs @@ -506,6 +506,20 @@ pub fn internal_throw_marker_with_gas(gas: u64) -> PrecompileError { marker_with_gas(INTERNAL_BRIDGE_THROW_MARKER, gas) } +/// Marker for a contract's call to the Bridge that ends the contract itself, +/// in every era: an exception `Program.callToPrecompiledAddress` lets out of +/// the VM, which `VM.steps` turns into the failure of the program that made +/// the call, all its gas spent. `Bridge.getGasForData` throws one for any +/// call from a contract before RSKIP88, and before RSKIP150 +/// `Program.memorySave` throws one on the null a method with nothing to +/// return answers. +pub const CALLER_ABORT_MARKER: &str = "rskj bridge call ends its caller"; + +/// Whether a Bridge error is [`CALLER_ABORT_MARKER`]. +pub fn ends_its_caller(e: &PrecompileError) -> bool { + matches!(e, PrecompileError::Other(msg) if msg == CALLER_ABORT_MARKER) +} + /// Marker prefix for the rskj `Program.callToPrecompiledAddress` insufficient- /// gas branch: when `requiredGas > msg.getGas()` for an INTERNAL (depth>1) /// call, rskj does `refundGas(0); stackPushZero(); track.rollback()` — the @@ -566,30 +580,34 @@ pub fn execute_bridge( call_kind: BridgeCallKind, call_value: U256, ) -> Result { - // Empty input → releaseBtc (legacy behavior matching rskj) - if input.is_empty() { - let gas_cost = 23_000u64; - if gas_limit < gas_cost { - return Err(insufficient_gas_marker(ctx, gas_limit)); - } - return execute_method(ctx, "releaseBtc", &[], gas_cost, config, block_store, use_v2, hardfork_cfg, tx_ctx, caller, call_depth, call_value); + let block_number = revm::context_interface::Block::number(ctx.block()).to::(); + // rskj `BridgeUtils.isContractTx`: the Bridge was given an internal + // transaction, a contract's call. + let from_contract = call_depth > 1; + + // rskj `Bridge.getGasForData` (Bridge.java:297): before RSKIP88 a call + // from a contract throws a NullPointerException before anything is charged + // or run. `Program.callToPrecompiledAddress` asks for the gas outside + // every try, so the exception leaves the VM whatever the era. + if from_contract && !hardfork_cfg.has_rskip88(block_number) { + return Err(PrecompileError::other(CALLER_ABORT_MARKER)); } - // rskj Bridge.parseData: 1-3 byte data, an unknown selector, or a method - // not enabled at this block is a parse failure. Before RSKIP88 (orchid) - // execute() returns null — the tx SUCCEEDS, charged the flat releaseBtc - // cost without executing anything (mainnet #648,914 sent ASCII text as - // calldata). Afterwards it throws BridgeIllegalArgumentException. - let block_number = revm::context_interface::Block::number(ctx.block()).to::(); - let parsed = if input.len() < 4 { - None - } else { - find_bridge_method(&[input[0], input[1], input[2], input[3]]) - .filter(|m| m.enabled.is_enabled(hardfork_cfg, block_number)) + // rskj Bridge.parseData: no data at all is releaseBtc with no arguments. + // 1-3 byte data, an unknown selector, or a method not enabled at this + // block is a parse failure. Before RSKIP88 (orchid) execute() returns + // null — the tx SUCCEEDS, charged the flat releaseBtc cost without + // executing anything (mainnet #648,914 sent ASCII text as calldata). + // Afterwards it throws BridgeIllegalArgumentException. + let parsed = match input.len() { + 0 => find_bridge_method(&compute_selector("releaseBtc()")), + 1..=3 => None, + _ => find_bridge_method(&[input[0], input[1], input[2], input[3]]) // rskj `parseData` ABI-decodes the arguments; a malformed payload // (e.g. an out-of-bounds dynamic offset) is a parse failure too. - .filter(|m| abi_args_decode_ok(&input[4..], m.signature)) - }; + .filter(|m| abi_args_decode_ok(&input[4..], m.signature)), + } + .filter(|m| m.enabled.is_enabled(hardfork_cfg, block_number)); let Some(method) = parsed else { // Which selector, and from where. A Bridge method that exists in rskj // and not here fails exactly like a malformed call, and the two are @@ -636,7 +654,7 @@ pub fn execute_bridge( return Err(marker_with_gas(INTERNAL_BRIDGE_THROW_MARKER, gas_cost)); }; - let args = &input[4..]; + let args = input.get(4..).unwrap_or_default(); let gas_cost = bridge_call_gas_cost(ctx, input, config, hardfork_cfg)?; if gas_limit < gas_cost { @@ -646,15 +664,17 @@ pub fn execute_bridge( // rskj `Bridge.validateCall` (Bridge.java:426) runs two gates after the gas // is charged, both throwing `BridgeIllegalArgumentException` → `VMException` // (consume `requiredGas`, CALL returns empty). The first is - // `validateLocalCall` (Bridge.java:431): post-RSKIP88, a LOCAL-ONLY method + // `validateLocalCall` (Bridge.java:431): from RSKIP88, a LOCAL-ONLY method // reached by a non-local call throws. Only `eth_call` is a local call, so // every on-chain CALL/transaction during block execution is non-local — a // local-only getter invoked on-chain always throws here and returns empty // (mainnet #6,223,900: a relay plain-CALLs `getFederationAddress`, which - // rskj empties so the relay's RETURNDATASIZE check sees 0). + // rskj empties so the relay's RETURNDATASIZE check sees 0). Before RSKIP88 + // rskj does not ask, and a transaction's own call runs such a method. // `!isLocalCall()` in rskj's condition: a local call (eth_call, // estimateGas) reaches these getters, on-chain execution never does. - if !tx_ctx.local_call + if hardfork_cfg.has_rskip88(block_number) + && !tx_ctx.local_call && method_only_allows_local_calls(method.permission, hardfork_cfg, block_number) { use revm::context_interface::JournalTr; @@ -664,18 +684,20 @@ pub fn execute_bridge( return Err(marker_with_gas(INTERNAL_BRIDGE_THROW_MARKER, gas_cost)); } - // rskj `Bridge.validateCallMessageType` (Bridge.java:446): after the gas is - // charged, a method reached via a call type it does not accept throws. The - // default is CALL-only; the read getters also accept STATICCALL; no method - // accepts DELEGATECALL/CALLCODE. The throw consumes `requiredGas` and the - // CALL returns zero (mainnet #6,223,797-adjacent #6,223,812: a relay - // DELEGATECALLs receiveHeader, which rskj rejects so RETURNDATASIZE is 0). + // rskj `Bridge.validateCallMessageType` (Bridge.java:446): from RSKIP417, + // after the gas is charged, a method reached via a call type it does not + // accept throws. The default is CALL-only; the read getters also accept + // STATICCALL; no method accepts DELEGATECALL/CALLCODE. The throw consumes + // `requiredGas` and the CALL returns zero (mainnet #6,223,797-adjacent + // #6,223,812: a relay DELEGATECALLs receiveHeader, which rskj rejects so + // RETURNDATASIZE is 0). Before RSKIP417 rskj does not ask, and every kind + // of call runs the method. let accepts_call = match call_kind { BridgeCallKind::Call => true, BridgeCallKind::StaticCall => method_allows_static_call(method.name), BridgeCallKind::DelegateOrCallCode => false, }; - if !accepts_call { + if hardfork_cfg.has_rskip417(block_number) && !accepts_call { use revm::context_interface::JournalTr; if ctx.journal().depth() == 1 { return Err(marker_with_gas(INVISIBLE_EXCEPTION_MARKER, gas_cost)); @@ -691,6 +713,28 @@ pub fn execute_bridge( // depth-aware markers so the precompile frame records exactly `gas_cost`. // (OOG and Fatal keep their original semantics.) match result { + // rskj `Bridge.calculateVoidReturnValue`: before RSKIP417 a method with + // nothing to return answers null. To a transaction's own call that is + // no output. A contract's call hands the null to the VM. + Ok(output) + if from_contract + && output.bytes.is_empty() + && !output.reverted + && !hardfork_cfg.has_rskip417(block_number) => + { + if hardfork_cfg.has_rskip197(block_number) { + // `Program.executePrecompiledAndHandleError` passes it to + // `Memory.write`, which reads its length inside the try: the + // call fails for its `requiredGas`, its track rolled back. + Err(marker_with_gas(INTERNAL_BRIDGE_THROW_MARKER, gas_cost)) + } else if hardfork_cfg.has_rskip150(block_number) { + // `Program.saveOutAfterExecution` skips a null. + Ok(output) + } else { + // `Program.memorySave` reads its length, outside any try. + Err(PrecompileError::other(CALLER_ABORT_MARKER)) + } + } Err(e) if !e.is_oog() && !matches!(e, PrecompileError::Fatal(_)) => { use revm::context_interface::JournalTr; if ctx.journal().depth() == 1 { diff --git a/crates/execution/src/executor.rs b/crates/execution/src/executor.rs index 8e0b5cc..a4890a2 100644 --- a/crates/execution/src/executor.rs +++ b/crates/execution/src/executor.rs @@ -5821,6 +5821,161 @@ mod tests { ); } + /// One transaction at mainnet's `block` to a contract that reaches the + /// Bridge with `opcode`, `input` and 100,000 gas, then stores the call's + /// flag plus one in its slot 0. Answers whether the transaction + /// succeeded, that slot (2 after a call that succeeded, 1 after one that + /// failed, 0 where the contract never got to store it), and how many logs + /// the transaction left. + fn the_bridge_called_from_a_contract( + block: u64, + opcode: u8, + input: &[u8], + ) -> (bool, u64, usize) { + let sender = Address::repeat_byte(0xAA); + let contract = Address::repeat_byte(0xCC); + let code_with_input_at = |input_at: u16| { + // CODECOPY the input, which follows the code, to memory 0. + let mut code = vec![0x60, input.len() as u8, 0x61]; + code.extend_from_slice(&input_at.to_be_bytes()); + code.extend_from_slice(&[0x60, 0x00, 0x39]); + // No output, the input, and for CALL and CALLCODE no value. + code.extend_from_slice(&[0x60, 0x00, 0x60, 0x00, 0x60, input.len() as u8, 0x60, 0x00]); + if matches!(opcode, 0xf1 | 0xf2) { + code.extend_from_slice(&[0x60, 0x00]); + } + code.push(0x73); + code.extend_from_slice(crate::precompiles::BRIDGE_ADDR.as_slice()); + code.extend_from_slice(&[0x62, 0x01, 0x86, 0xa0, opcode]); + // The flag plus one to slot 0, then STOP. + code.extend_from_slice(&[0x60, 0x01, 0x01, 0x60, 0x00, 0x55, 0x00]); + code + }; + let mut code = code_with_input_at(code_with_input_at(0).len() as u16); + code.extend_from_slice(input); + + let store = Arc::new(MemoryTrieStore::new()); + let root = TrieNode::empty(); + let one_rbtc = U256::from(10u64).pow(U256::from(18)); + let root = put_account(&root, store.as_ref(), &sender, 0, one_rbtc); + let root = put_account(&root, store.as_ref(), &contract, 1, U256::ZERO); + let root = root.put( + &TrieKeySlice::from_key(&rustock_trie::code_key(&contract)), + &code, + store.as_ref(), + ); + let block_store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); + let tx = rustock_core::Transaction { + nonce: 0, + gas_price: U256::from(0), + gas_limit: U256::from(500_000), + to: Bytes::copy_from_slice(contract.as_slice()), + value: U256::ZERO, + input: Bytes::new(), + v: 0, + r: U256::ZERO, + s: U256::ZERO, + cached_rlp: None, + }; + let result = executor + .execute_block(&dummy_header(block), &[(tx, sender)], &root, store.clone()) + .expect("block"); + let stored = result + .state_changes + .get(&contract) + .and_then(|account| account.storage.get(&U256::ZERO)) + .map_or(0, |slot| slot.present_value.to::()); + let outcome = &result.tx_results[0]; + (outcome.success, stored, outcome.logs.len()) + } + + /// How a contract's call to the Bridge ends in rskj, era by era, at + /// mainnet's heights. + #[test] + fn a_contracts_call_to_the_bridge_ends_as_rskj_ends_it_in_each_era() { + let (call, callcode, delegatecall, staticcall) = (0xf1, 0xf2, 0xf4, 0xfa); + let selector = |signature: &str| crate::abi::selector(signature).to_vec(); + // receiveHeaders(bytes[]) of no headers: the array's offset, then its + // length. Anyone may call it between RSKIP124 and RSKIP200, and it + // returns nothing. + let no_headers = { + let mut input = selector("receiveHeaders(bytes[])"); + input.extend_from_slice(&U256::from(32).to_be_bytes::<32>()); + input.extend_from_slice(&[0u8; 32]); + input + }; + let (ended, failed, succeeded) = ((false, 0, 0), (true, 1, 0), (true, 2, 0)); + let after = the_bridge_called_from_a_contract; + + // Before RSKIP88 `Bridge.getGasForData` throws a NullPointerException + // for any call from a contract, which nothing in the VM catches: the + // contract ends there. + assert_eq!(after(700_000, call, &[0xde, 0xad, 0xbe, 0xef]), ended); + // A method with nothing to return answers null before RSKIP417. + // Before RSKIP150 `Program.memorySave` reads the null's length, and + // that too ends the contract. + assert_eq!(after(1_700_000, call, &no_headers), ended); + // From RSKIP150 `saveOutAfterExecution` skips a null. + assert_eq!(after(2_100_000, call, &no_headers), succeeded); + // From RSKIP197 `executePrecompiledAndHandleError` hands the null to + // `Memory.write`, which reads its length inside the try: the call + // fails and its track is rolled back. The Bridge wrote its logs + // straight into the calling program's list, and those stay. No data + // is releaseBtc, which from a contract only logs its refusal. + assert_eq!(after(4_000_000, call, &[]), (true, 1, 1)); + // From RSKIP417 such a method answers an empty array. + assert_eq!(after(7_000_000, call, &[]), (true, 2, 1)); + // And from RSKIP417 the Bridge checks the kind of call it was reached + // by, releaseBtc with no data included: CALL alone. + for kind in [callcode, delegatecall, staticcall] { + assert_eq!(after(7_000_000, kind, &[]), failed, "{kind:#x}"); + } + // Before it, every kind of call reaches a method. + let height = selector("getBtcBlockchainBestChainHeight()"); + for kind in [call, callcode, delegatecall, staticcall] { + assert_eq!(after(4_000_000, kind, &height), succeeded, "{kind:#x}"); + } + assert_eq!(after(7_000_000, delegatecall, &height), failed); + assert_eq!(after(7_000_000, staticcall, &height), succeeded); + } + + /// rskj `Bridge.validateLocalCall` asks only from RSKIP88 whether a method + /// is for local calls alone. Before it a transaction's own call runs such + /// a method and gets its answer; from it the call throws, which a + /// transaction's own call shows as no output. + #[test] + fn before_rskip88_a_transaction_runs_a_local_only_bridge_method() { + let output_at = |block: u64| { + let store = Arc::new(MemoryTrieStore::new()); + let sender = Address::repeat_byte(0xAA); + let one_rbtc = U256::from(10u64).pow(U256::from(18)); + let root = put_account(&TrieNode::empty(), store.as_ref(), &sender, 0, one_rbtc); + let tx = rustock_core::Transaction { + nonce: 0, + gas_price: U256::ZERO, + gas_limit: U256::from(100_000), + to: Bytes::copy_from_slice(crate::precompiles::BRIDGE_ADDR.as_slice()), + value: U256::ZERO, + input: Bytes::copy_from_slice(&crate::abi::selector("getFederationAddress()")), + v: 0, + r: U256::ZERO, + s: U256::ZERO, + cached_rlp: None, + }; + let block_store = + Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); + let result = executor + .execute_block(&dummy_header(block), &[(tx, sender)], &root, store.clone()) + .expect("block"); + assert!(result.tx_results[0].success); + result.tx_results[0].output.clone() + }; + assert!(!output_at(700_000).is_empty(), "before RSKIP88"); + assert!(output_at(764_123).is_empty(), "from RSKIP88"); + } + /// rskj `doSUICIDE` (VM.java) charges NEW_ACCT_SUICIDE (25,000) whenever the /// beneficiary does not exist (`!track.isExist`), UNCONDITIONAL on the /// suiciding contract's balance. revm's selfdestruct instead gates the diff --git a/crates/execution/src/hardfork.rs b/crates/execution/src/hardfork.rs index 248e03e..ddd91c4 100644 --- a/crates/execution/src/hardfork.rs +++ b/crates/execution/src/hardfork.rs @@ -570,6 +570,14 @@ impl RskHardforkConfig { self.active_upgrade(block_number) >= RskNetworkUpgrade::Iris300 } + /// RSKIP417 (Arrowhead600): the Bridge refuses a method reached by a kind + /// of call it does not accept (`Bridge.validateCallMessageType`), and a + /// method with nothing to return answers an empty array where it + /// answered null (`Bridge.calculateVoidReturnValue`). + pub fn has_rskip417(&self, block_number: u64) -> bool { + self.active_upgrade(block_number) >= RskNetworkUpgrade::Arrowhead600 + } + /// RSKIP219: updated minimum peg-in tx value (Iris300). pub fn has_rskip219(&self, block_number: u64) -> bool { self.active_upgrade(block_number) >= RskNetworkUpgrade::Iris300 diff --git a/crates/execution/src/precompiles.rs b/crates/execution/src/precompiles.rs index a93eef0..ab2f912 100644 --- a/crates/execution/src/precompiles.rs +++ b/crates/execution/src/precompiles.rs @@ -880,8 +880,32 @@ impl RskPrecompileProvider { context.journal_mut().touch_account(inputs.target_address); } + // What the journal holds of logs before the call, to tell the call's + // own apart afterwards. + let logs_before = { + use revm::context_interface::JournalTr; + context.journal().logs().len() + }; + let exec_result = if *addr == BRIDGE_ADDR { - self.run_bridge(context, &input_bytes, inputs.gas_limit, inputs.caller, inputs.scheme, inputs.call_value()) + // rskj gives the Bridge an internal transaction from the program + // that made the call (`getOwnerRskAddress`), with the value the + // opcode took off the stack. DELEGATECALL takes none, and revm's + // caller and value for it are those of the frame that delegates. + let (owner, value) = match inputs.scheme { + revm::interpreter::CallScheme::DelegateCall => { + (inputs.target_address, revm::primitives::U256::ZERO) + } + _ => (inputs.caller, inputs.call_value()), + }; + self.run_bridge( + context, + &input_bytes, + inputs.gas_limit, + owner, + inputs.scheme, + value, + ) } else { self.run_remasc(context, &input_bytes, inputs.gas_limit) }; @@ -922,6 +946,17 @@ impl RskPrecompileProvider { assert!(underflow, "gas pre-checked against the cost"); return Ok(Some(result)); } + // A call that ends the contract that made it, in every era + // (`bridge::CALLER_ABORT_MARKER`): `RskHandler` halts the contract + // on the flag, as for a throw before RSKIP197 below. + if crate::bridge::ends_its_caller(e) { + self.aborts_caller + .store(true, std::sync::atomic::Ordering::Relaxed); + let underflow = result.gas.record_cost(inputs.gas_limit); + assert!(underflow, "spending exactly the forwarded gas"); + result.result = InstructionResult::PrecompileError; + return Ok(Some(result)); + } // Internal (depth>1) throw, by a Bridge method or by REMASC: rskj // `Program.executePrecompiledAndHandleError` consumes only // `requiredGas` and the CALL fails (pushes zero). Record that cost @@ -942,6 +977,17 @@ impl RskPrecompileProvider { result.result = InstructionResult::PrecompileError; return Ok(Some(result)); } + // The catch rolls back the call's track and nothing else. The + // Bridge wrote its logs straight into the calling program's + // list (`callToPrecompiledAddress` hands it + // `result.getLogInfoList()`), so those stay with the caller; + // `RskHandler` writes them again once revm has dropped them + // with this call's checkpoint. + { + use revm::context_interface::JournalTr; + let kept = context.journal().logs()[logs_before..].to_vec(); + context.chain_mut().kept_logs.extend(kept); + } let underflow = result.gas.record_cost(spent); assert!(underflow, "gas pre-checked against requiredGas"); result.result = InstructionResult::Revert; diff --git a/crates/execution/src/raw_storage.rs b/crates/execution/src/raw_storage.rs index b9d624e..7ee7097 100644 --- a/crates/execution/src/raw_storage.rs +++ b/crates/execution/src/raw_storage.rs @@ -63,6 +63,14 @@ pub struct RskChainExt { /// standing. Each holds rskj's storage-prefix marker (`setupContract` /// writes it as the creation begins), whatever code it ended with. pub created_in_block: std::collections::HashSet
, + /// The logs a contract's call to the Bridge wrote before the call failed. + /// rskj's Bridge writes its logs straight into the list of the program + /// that called it (`Program.callToPrecompiledAddress` hands it + /// `result.getLogInfoList()`), and `executePrecompiledAndHandleError` + /// rolls back the call's track and nothing else, so the logs stay with + /// the caller. revm drops them with the call's checkpoint; they wait here + /// for `RskHandler` to write them again once that checkpoint is gone. + pub kept_logs: Vec, } /// Raw-bytes storage overlay for the block being executed. diff --git a/crates/execution/src/rsk_handler.rs b/crates/execution/src/rsk_handler.rs index 6a532fa..22a2364 100644 --- a/crates/execution/src/rsk_handler.rs +++ b/crates/execution/src/rsk_handler.rs @@ -671,6 +671,10 @@ where // account, or a failed pre-check (depth/endowment) // resolves. ItemOrResult::Result(result) => { + // A precompile's failed call has had its + // checkpoint reverted by now; the logs rskj leaves + // with the caller go back in as the caller's. + self.keep_precompile_logs(evm); if let (Some(precompiles), Some(callee), FrameResult::Call(outcome)) = (&self.pre_rskip150_precompiles, callee, &result) { @@ -928,6 +932,16 @@ where list.truncate(called); } } + + /// Writes again the logs a contract's failed call to the Bridge leaves + /// with the contract (`RskChainExt::kept_logs`). They are the caller's + /// from here on, and go with it if it fails. + fn keep_precompile_logs(&mut self, evm: &mut EVM) { + use revm::context_interface::JournalTr; + for log in std::mem::take(&mut evm.ctx().chain_mut().kept_logs) { + evm.ctx().journal_mut().log(log); + } + } } /// Hands the journal's entries to the RSKIP-144 collision tracker. From 7f0f9a7cb8497d4a0a19d26a5783eb00521a974a Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Thu, 12 Nov 2026 21:10:09 -0800 Subject: [PATCH 114/264] bridge: a federation Bitcoin could not spend from is not committed rskj builds the federation as commitFederation goes through, and the build throws for two kinds of federation. bitcoinj's ScriptBuilder.createMultiSigOutputScript, which every format's redeem script is made by, takes no more than 20 keys. And an ErpFederation checks its redeem script's size as it is constructed (validateRedeemScript, ScriptValidations): 486 bytes for the formats whose script goes in a scriptSig, 3566 for P2SH-P2WSH. Neither exception is one voteFederationChange catches, so the deciding vote's call fails and nothing is stored. The dry run builds nothing, so the votes before it are taken. This node built any pending federation and rotated or proposed it. execute_federation_change now makes the same two checks before the commit, once the dry run's own checks have passed. Test: a_federation_bitcoin_could_not_spend_from_is_not_committed. --- crates/execution/src/bridge/governance.rs | 132 ++++++++++++++++++++++ 1 file changed, 132 insertions(+) diff --git a/crates/execution/src/bridge/governance.rs b/crates/execution/src/bridge/governance.rs index 350beb9..25dd7b7 100644 --- a/crates/execution/src/bridge/governance.rs +++ b/crates/execution/src/bridge/governance.rs @@ -266,6 +266,20 @@ fn execute_federation_change( if function == "commit" && args[0].len() != 32 { return Err("commitFederation: the pending federation's hash isn't 32 bytes"); } + // The dry run answers before any federation is built. The commit itself + // builds it, in every era, and the build throws for one Bitcoin could + // not spend from. + if function == "commit" + && !dry_run + && federation_change_code(ctx, function, args, true, config, hardfork_cfg) == 1 + { + let block_number = revm::context_interface::Block::number(ctx.block()).to::(); + let pending = bridge_load_bytes_named(ctx, PENDING_FEDERATION_KEY); + let members = deserialize_pending_federation(&pending); + if let Some(why) = unbuildable_federation(&members, config, hardfork_cfg, block_number) { + return Err(why); + } + } Ok(federation_change_code( ctx, function, @@ -276,6 +290,47 @@ fn execute_federation_change( )) } +/// rskj `ScriptValidations.MAX_P2SH_REDEEM_SCRIPT_SIZE`: what a scriptSig +/// element holds (bitcoinj `Script.MAX_SCRIPT_ELEMENT_SIZE`, 520 bytes) less +/// the 34 bytes a flyover prefix adds to a redeem script. +const MAX_P2SH_REDEEM_SCRIPT_SIZE: usize = 520 - 34; + +/// rskj `ScriptValidations.MAX_P2WSH_REDEEM_SCRIPT_SIZE`: a standard P2WSH +/// script (`Script.MAX_STANDARD_P2WSH_SCRIPT_SIZE`, 3600 bytes) less the same. +const MAX_P2WSH_REDEEM_SCRIPT_SIZE: usize = 3600 - 34; + +/// Why rskj can't build the federation these pending members make at +/// `block_number`, where it can't. bitcoinj's +/// `ScriptBuilder.createMultiSigOutputScript`, which every format's redeem +/// script goes through, takes no more than 20 keys, "the max OP_CHECKMULTISIG +/// allows". An `ErpFederation` checks its redeem script's size as it is made +/// (`validateRedeemScript`): against what a scriptSig holds, or for a +/// P2SH-P2WSH federation against what a witness script may be. +fn unbuildable_federation( + members: &[StoredMember], + config: &super::constants::BridgeConstants, + hardfork_cfg: &crate::hardfork::RskHardforkConfig, + block_number: u64, +) -> Option<&'static str> { + if members.len() > 20 { + return Some("commitFederation: a federation of more than 20 keys"); + } + let limit = match federation_format_version(hardfork_cfg, block_number) { + 1000 => return None, + 4000 => MAX_P2WSH_REDEEM_SCRIPT_SIZE, + _ => MAX_P2SH_REDEEM_SCRIPT_SIZE, + }; + let mut keys: Vec<[u8; 33]> = members.iter().map(|m| m.btc).collect(); + keys.sort(); + let redeem = super::peg::build_committed_federation_redeem_script( + &keys, + config, + hardfork_cfg, + block_number, + ); + (redeem.len() > limit).then_some("commitFederation: the redeem script is too long") +} + /// The code the change's function answers. fn federation_change_code( ctx: &mut CTX, @@ -1912,6 +1967,83 @@ mod tests { ); } + /// rskj builds the federation as a commit goes through, and the build + /// throws for one Bitcoin could not spend from: more than 20 keys + /// (`ScriptBuilder.createMultiSigOutputScript`), or an ERP redeem script + /// longer than a scriptSig element holds, less a flyover prefix + /// (`ErpFederation.validateRedeemScript`). The dry run builds nothing, so + /// the votes before the deciding one are taken; the deciding one throws. + #[test] + fn a_federation_bitcoin_could_not_spend_from_is_not_committed() { + use crate::precompiles::BridgeTxContext; + use revm::MainContext; + use sha3::{Digest, Keccak256}; + + let config = super::super::constants::BridgeConstants::mainnet(); + let cfg = crate::hardfork::RskHardforkConfig::mainnet(); + let member = |i: u8| { + let secp = bitcoin::secp256k1::Secp256k1::new(); + let secret = bitcoin::secp256k1::SecretKey::from_slice(&[i + 1; 32]).unwrap(); + let key = bitcoin::secp256k1::PublicKey::from_secret_key(&secp, &secret); + StoredMember::from_btc(key.serialize()) + }; + let authorizers = federation_change_authorizers(&config); + let deciding = authorizers.len() / 2 + 1; + // Each authorizer's vote, up to the deciding one, to commit a pending + // federation of `n` members at `block`: its answer, or none where the + // call throws. + let votes = |block: u64, n: u8| -> Vec> { + let mut ctx = revm::Context::mainnet() + .with_block(revm::context::BlockEnv { + number: alloy_primitives::U256::from(block), + ..Default::default() + }) + .with_chain(crate::raw_storage::RskChainExt::default()); + let members: Vec = (0..n).map(member).collect(); + store_pending_federation(&mut ctx, Some(&members), true); + let hash = Keccak256::digest(serialize_pending_federation_btc_keys(&members)); + let args = crate::abi::encode::args(&[crate::abi::encode::Arg::Bytes(&hash)]); + authorizers[..deciding] + .iter() + .map(|authorizer| { + let tx_ctx = BridgeTxContext { + rsk_sender: *authorizer, + ..BridgeTxContext::default() + }; + commit_federation(&mut ctx, &args, 0, &config, &cfg, &tx_ctx) + .ok() + .map(|out| i64::from_be_bytes(out.bytes[24..32].try_into().unwrap())) + }) + .collect() + }; + let committed = vec![Some(1); deciding]; + let mut thrown = committed.clone(); + *thrown.last_mut().unwrap() = None; + + // A standard multisig, before RSKIP201: 20 keys and no more. + let standard = 2_000_000; + assert_eq!(federation_format_version(&cfg, standard), 1000); + assert_eq!(votes(standard, 20), committed); + assert_eq!(votes(standard, 21), thrown); + + // A P2SH ERP federation's redeem script goes in a scriptSig: 486 + // bytes and no more. + let p2sh_erp = 5_000_000; + assert_eq!(federation_format_version(&cfg, p2sh_erp), 3000); + let size = |n: u8| { + let mut keys: Vec<[u8; 33]> = (0..n).map(|i| member(i).btc).collect(); + keys.sort(); + super::super::peg::build_committed_federation_redeem_script( + &keys, &config, &cfg, p2sh_erp, + ) + .len() + }; + let largest = (2..20u8).rev().find(|n| size(*n) <= 486).unwrap(); + assert!(size(largest + 1) > 486); + assert_eq!(votes(p2sh_erp, largest), committed); + assert_eq!(votes(p2sh_erp, largest + 1), thrown); + } + /// A Bridge call that reads the whitelist writes it back as the call /// returns, whatever it answers (upstream #291): `save()` writes whatever /// the call loaded. The changes read it after the sender check, the adds From 4871a0dc3673c0f714038efa447c396e3f70c490 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Thu, 12 Nov 2026 21:10:10 -0800 Subject: [PATCH 115/264] bridge: regtest has the union bridge authorizers rskj gives it rskj's UnionBridgeRegTestConstants names an account that may set the union bridge contract's address (6c9dfd95...) and one contract that may raise the locking cap and set the transfer permissions (ff6f4ff0...). This node gave every chain but testnet the zero address for all three, so on a chain started from rskj's regtest genesis those calls failed here and went through there. union_constants now has rskj's regtest values for chain id 33, which gets a name beside the other two chain ids. rustock's own regtest schedules no reed810, so the union bridge's methods stay closed there and the values are not read. Test: regtest_has_the_authorizers_rskj_gives_it. --- crates/execution/src/bridge/union.rs | 56 +++++++++++++++++++++++++--- crates/execution/src/hardfork.rs | 8 ++-- 2 files changed, 56 insertions(+), 8 deletions(-) diff --git a/crates/execution/src/bridge/union.rs b/crates/execution/src/bridge/union.rs index 12e1d2e..897bdd3 100644 --- a/crates/execution/src/bridge/union.rs +++ b/crates/execution/src/bridge/union.rs @@ -23,7 +23,9 @@ use alloy_primitives::{Address, Bytes, U256}; use revm::precompile::{PrecompileError, PrecompileOutput}; use super::storage::{bridge_load_bytes, bridge_storage_key_long, bridge_store_bytes}; -use crate::hardfork::{RskHardforkConfig, RSK_MAINNET_CHAIN_ID, RSK_TESTNET_CHAIN_ID}; +use crate::hardfork::{ + RskHardforkConfig, RSK_MAINNET_CHAIN_ID, RSK_REGTEST_CHAIN_ID, RSK_TESTNET_CHAIN_ID, +}; /// rskj `UnionResponseCode`. Returned as an int256 by every mutating method. /// @@ -64,8 +66,8 @@ pub const MAX_EVENT_DATA_LENGTH: usize = 128; /// rskj `UnionBridgeConstants`, per network. /// /// Mainnet's address and all three authorizers are the zero address: the -/// union bridge is configured on testnet and not yet on mainnet, so every -/// authorized method there rejects. +/// union bridge is configured on testnet and regtest and not yet on mainnet, +/// so every authorized method there rejects. pub struct UnionConstants { pub address: Address, pub initial_locking_cap: U256, @@ -89,8 +91,20 @@ pub fn union_constants(hardfork_cfg: &RskHardforkConfig) -> UnionConstants { change_locking_cap_authorizer: addr("1a8109af0f019ed3045fbcdf45e5e90d6b6aafaf"), change_transfer_permissions_authorizer: addr("8db1f83e8119e4dce5bc708ec2f4390ffd910b19"), }, - // Mainnet and regtest alike: zero address throughout, so nothing is - // authorized and the contract address falls back to zero. + // rskj `UnionBridgeRegTestConstants`: an account sets the contract's + // address, and one contract holds the other two permissions. + RSK_REGTEST_CHAIN_ID => UnionConstants { + address: Address::ZERO, + initial_locking_cap: one_rbtc(), + locking_cap_increments_multiplier: 2, + change_address_authorizer: addr("6c9dfd950bf748bb26f893f7e5f693c7f60a8f85"), + change_locking_cap_authorizer: addr("ff6f4ff09c54561e1476203e56fcfc834911aa64"), + change_transfer_permissions_authorizer: addr( + "ff6f4ff09c54561e1476203e56fcfc834911aa64", + ), + }, + // Mainnet: the zero address throughout, so nothing is authorized and + // the contract address is zero. _ => UnionConstants { address: Address::ZERO, initial_locking_cap: one_rbtc(), @@ -725,6 +739,38 @@ mod tests { } } + /// rskj's regtest names its own authorizers (`UnionBridgeRegTestConstants`): + /// an account for the contract's address, and one contract for both the + /// locking cap and the transfer permissions. + #[test] + fn regtest_has_the_authorizers_rskj_gives_it() { + let cfg = RskHardforkConfig::rskj_regtest(); + let for_the_address = addr("6c9dfd950bf748bb26f893f7e5f693c7f60a8f85"); + let for_the_rest = addr("ff6f4ff09c54561e1476203e56fcfc834911aa64"); + + let mut c = ctx(); + let doubled = one_rbtc() * U256::from(2u64); + let cap = doubled.to_be_bytes::<32>(); + assert!(increase_locking_cap(&mut c, &cap, 0, &cfg, for_the_address).is_err()); + let out = increase_locking_cap(&mut c, &cap, 0, &cfg, for_the_rest).unwrap(); + assert_eq!(code(out), UnionResponse::Success.code()); + assert_eq!(locking_cap(&mut c, &cfg), doubled); + + let both_off = [0u8; 64]; + assert!(set_transfer_permissions(&mut c, &both_off, 0, &cfg, for_the_address).is_err()); + let out = set_transfer_permissions(&mut c, &both_off, 0, &cfg, for_the_rest).unwrap(); + assert_eq!(code(out), UnionResponse::Success.code()); + assert!(!request_enabled(&mut c) && !release_enabled(&mut c)); + + let mut address = [0u8; 32]; + address[12..].copy_from_slice(union_bridge().as_slice()); + assert!(set_contract_address_for_testnet(&mut c, &address, 0, &cfg, for_the_rest).is_err()); + let out = + set_contract_address_for_testnet(&mut c, &address, 0, &cfg, for_the_address).unwrap(); + assert_eq!(code(out), UnionResponse::Success.code()); + assert_eq!(contract_address(&mut c, &cfg), union_bridge()); + } + // -- RSKIP529: the super and base events ------------------------------ /// The union bridge contract testnet's tests call from. diff --git a/crates/execution/src/hardfork.rs b/crates/execution/src/hardfork.rs index ddd91c4..bef8d6c 100644 --- a/crates/execution/src/hardfork.rs +++ b/crates/execution/src/hardfork.rs @@ -40,6 +40,8 @@ pub enum RskNetworkUpgrade { pub const RSK_MAINNET_CHAIN_ID: u64 = 30; pub const RSK_TESTNET_CHAIN_ID: u64 = 31; +/// rskj `Constants.REGTEST_CHAIN_ID`: its regtest's, and rustock's own. +pub const RSK_REGTEST_CHAIN_ID: u64 = 33; /// Mainnet activation heights. pub const MAINNET_ACTIVATIONS: &[(u64, RskNetworkUpgrade)] = &[ @@ -173,9 +175,9 @@ impl RskHardforkConfig { pub fn regtest() -> Self { Self { activations: vec![(0, RskNetworkUpgrade::Vetiver900)], - chain_id: 33, + chain_id: RSK_REGTEST_CHAIN_ID, rule_overrides: Vec::new(), - after_bridge_sync: Self::after_bridge_sync_for(33), + after_bridge_sync: Self::after_bridge_sync_for(RSK_REGTEST_CHAIN_ID), } } @@ -192,7 +194,7 @@ impl RskHardforkConfig { (0, RskNetworkUpgrade::Reed810), (0, RskNetworkUpgrade::Vetiver900), ], - chain_id: 33, + chain_id: RSK_REGTEST_CHAIN_ID, rule_overrides: vec![ (ConsensusRule::Rskip144, Some(1)), (ConsensusRule::Rskip351, Some(1)), From a55fb14ce950902c6139b4b43a1ff3b16bed3b8b Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Thu, 12 Nov 2026 21:10:10 -0800 Subject: [PATCH 116/264] config: testnet's genesis header is the one rskj builds rskj builds testnet's genesis from orchid-testnet.json: the coinbase is 0x3333...3333 and each merged-mining field is one zero byte. GenesisLoaderImpl gives the header the empty list's hash for its uncles, and GenesisHeader the empty trie's hash for its transactions and receipts. This node had the zero address, no merged-mining fields and zero for the three hashes. The genesis hash is pinned, so sync never saw the difference. What reads the header did: eth_getBlockByNumber for block 0, and the BlockHeader contract, which reaches the genesis block at depth 0 while block 1 executes and answered its coinbase, Bitcoin header and merged-mining tags from these fields. A database written before this keeps the header it stored. Test: test_testnet_genesis_header now builds the header from the file's values, checks that they hash to testnet's genesis hash as rskj hashes a genesis block, and compares every field. --- crates/core/src/config.rs | 52 +++++++++++++++++++++++++++++++++------ crates/rpc/src/eth.rs | 25 ++++++++++--------- 2 files changed, 57 insertions(+), 20 deletions(-) diff --git a/crates/core/src/config.rs b/crates/core/src/config.rs index bbf8f1c..0eae671 100644 --- a/crates/core/src/config.rs +++ b/crates/core/src/config.rs @@ -456,13 +456,15 @@ impl ChainConfig { cached_hash_for_merged_mining: None, kept: None, }, + // rskj's `orchid-testnet.json`, with what `GenesisLoaderImpl` and + // `GenesisHeader` fill in: the uncles' hash and the two roots. 31 => Header { parent_hash: B256::ZERO, - ommers_hash: B256::ZERO, - beneficiary: Address::ZERO, + ommers_hash: empty_list_hash, + beneficiary: "0x3333333333333333333333333333333333333333".parse().unwrap(), state_root: "0x45bce5168430c42b3d568331753f900a32457b4f3748697cbd8375ff4da72641".parse().unwrap(), - transactions_root: B256::ZERO, - receipts_root: B256::ZERO, + transactions_root: empty_trie_hash, + receipts_root: empty_trie_hash, logs_bloom: Default::default(), extension_data: None, difficulty: U256::from(0x00100000), @@ -476,9 +478,9 @@ impl ChainConfig { uncle_count: 0, umm_root: None, version_fields: None, - bitcoin_merged_mining_header: None, - bitcoin_merged_mining_merkle_proof: None, - bitcoin_merged_mining_coinbase_transaction: None, + bitcoin_merged_mining_header: Some(Bytes::from_static(&[0x00])), + bitcoin_merged_mining_merkle_proof: Some(Bytes::from_static(&[0x00])), + bitcoin_merged_mining_coinbase_transaction: Some(Bytes::from_static(&[0x00])), cached_hash: None, cached_hash_for_merged_mining: None, kept: None, @@ -928,13 +930,47 @@ mod tests { assert_eq!(orchid_state_root(&root, &store), config.genesis_header().state_root); } + /// Testnet's genesis header is the one rskj builds from + /// `orchid-testnet.json`: the file's coinbase and the zero byte it gives + /// each merged-mining field, and what `GenesisLoaderImpl` and + /// `GenesisHeader` fill in, the empty list's hash for the uncles and the + /// empty trie's for the transactions and receipts. Hashed as rskj hashes + /// a genesis block, with the difficulty as the file writes it, those + /// fields give testnet's genesis hash. #[test] fn test_testnet_genesis_header() { let config = ChainConfig::testnet(); let genesis = config.genesis_header(); assert_eq!(genesis.number, 0); assert_eq!(genesis.difficulty, U256::from(0x00100000)); - assert!(genesis.bitcoin_merged_mining_header.is_none()); + + let zero_byte = Bytes::from_static(&[0x00]); + let file = crate::rskj_genesis::GenesisSpec { + parent_hash: B256::ZERO, + coinbase: Address::repeat_byte(0x33), + timestamp: 0, + extra_data: Bytes::from_static(&hex_literal::hex!("434d272841")), + gas_limit: U256::from(0x4c4b40), + difficulty_bytes: Bytes::from_static(&hex_literal::hex!("0000100000")), + minimum_gas_price: U256::ZERO, + bitcoin_merged_mining_header: zero_byte.clone(), + bitcoin_merged_mining_merkle_proof: zero_byte.clone(), + bitcoin_merged_mining_coinbase_transaction: zero_byte, + alloc: Vec::new(), + state_root: Some( + "0x45bce5168430c42b3d568331753f900a32457b4f3748697cbd8375ff4da72641" + .parse() + .unwrap(), + ), + }; + assert_eq!(Some(file.hash()), config.known_genesis_hash()); + // The file's header carries that hash; this one leaves it to + // `known_genesis_hash`. + let as_rskj_builds_it = Header { + cached_hash: None, + ..file.header() + }; + assert_eq!(genesis, as_rskj_builds_it); } #[test] diff --git a/crates/rpc/src/eth.rs b/crates/rpc/src/eth.rs index e4995ce..7ee61af 100644 --- a/crates/rpc/src/eth.rs +++ b/crates/rpc/src/eth.rs @@ -479,19 +479,20 @@ fn held_or_implied_body( // locally by `setup_genesis`, never fetched from a peer, so "no body row" // cannot mean "body not arrived yet" the way it does at any other height. // - // This is load-bearing rather than belt-and-braces. Only mainnet's genesis - // header carries the real empty-list and empty-trie hashes; testnet's and - // regtest's carry `B256::ZERO` for `ommers_hash` and `transactions_root`, - // which is not a root any empty trie produces, so the claim check below - // says "not empty" and the whole block came back `null` -- by number, by - // tag and by hash alike. On a chain whose head *is* genesis that made - // `eth_getBlockByNumber("latest")` answer `null` to every client, which is - // the state of a fresh regtest node until something is mined. + // This is load-bearing rather than belt-and-braces. Mainnet's and + // testnet's genesis headers carry the real empty-list and empty-trie + // hashes, as rskj's do; rustock's own regtest carries `B256::ZERO` for + // `ommers_hash` and `transactions_root`, which is not a root any empty + // trie produces, so the claim check below says "not empty" and the whole + // block came back `null` -- by number, by tag and by hash alike. On a + // chain whose head *is* genesis that made `eth_getBlockByNumber("latest")` + // answer `null` to every client, which is the state of a fresh regtest + // node until something is mined. // - // Fixed here rather than in the headers themselves: mainnet's and - // testnet's genesis hashes are pinned by `known_genesis_hash`, so editing - // their fields would leave a stored header that does not hash to the key - // it is stored under. + // Fixed here rather than in that header: its hash is computed from its + // fields, so editing them would move rustock's regtest to another genesis + // block. And a testnet database written while testnet's header carried + // zeros too still holds that header. if header.number == 0 { return Some(None); } From ab488cba832b7fbb81fbfc4e138c1bc478a2f0f3 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Thu, 12 Nov 2026 21:10:10 -0800 Subject: [PATCH 117/264] precompiles: a native contract's null ends a contract's call as rskj ends it rskj's getBitcoinHeader returns a block's Bitcoin header as the header holds it, and NativeContract.execute returns a null as it is. A block has none only where it is a genesis block read from a file without one: BlockFactory reads an empty field as null, and every later block needs a Bitcoin header for its proof of work. TransactionExecutor takes the null as no output. Program reads the output's length as it writes the output to memory: from RSKIP197 inside the call's own try, so the call fails; before RSKIP150 with nothing to catch it, so the calling contract fails; and between the two it skips a null. This node answered an empty output in every case, which was right for a transaction's own call alone. The null is now its own outcome, and RskPrecompileProvider::finish settles it by who called and where the block is. An rskj node that built its genesis block in the same run still holds the field as the empty bytes its loader made, and answers the encoded empty value until it reads the block back from its store. This follows the stored form, as the header's decoding here does. Tests: a_native_contracts_null_ends_a_contracts_call_as_rskj_ends_it_in_each_era, a_block_without_a_bitcoin_header_answers_null. --- crates/execution/src/executor.rs | 105 ++++++++++++++++++-- crates/execution/src/native/block_header.rs | 18 ++-- crates/execution/src/native/mod.rs | 16 ++- crates/execution/src/precompiles.rs | 29 +++++- 4 files changed, 147 insertions(+), 21 deletions(-) diff --git a/crates/execution/src/executor.rs b/crates/execution/src/executor.rs index a4890a2..4a00c1c 100644 --- a/crates/execution/src/executor.rs +++ b/crates/execution/src/executor.rs @@ -5822,13 +5822,33 @@ mod tests { } /// One transaction at mainnet's `block` to a contract that reaches the - /// Bridge with `opcode`, `input` and 100,000 gas, then stores the call's - /// flag plus one in its slot 0. Answers whether the transaction + /// Bridge with `opcode`, `input` and 100,000 gas: + /// [`a_precompile_called_from_a_contract`] says what this answers. + fn the_bridge_called_from_a_contract( + block: u64, + opcode: u8, + input: &[u8], + ) -> (bool, u64, usize) { + let block_store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + a_precompile_called_from_a_contract( + block_store, + &dummy_header(block), + crate::precompiles::BRIDGE_ADDR, + opcode, + input, + ) + } + + /// One transaction in mainnet's block `header` to a contract that reaches + /// `precompile` with `opcode`, `input` and 100,000 gas, then stores the + /// call's flag plus one in its slot 0. Answers whether the transaction /// succeeded, that slot (2 after a call that succeeded, 1 after one that /// failed, 0 where the contract never got to store it), and how many logs /// the transaction left. - fn the_bridge_called_from_a_contract( - block: u64, + fn a_precompile_called_from_a_contract( + block_store: Arc, + header: &Header, + precompile: Address, opcode: u8, input: &[u8], ) -> (bool, u64, usize) { @@ -5845,7 +5865,7 @@ mod tests { code.extend_from_slice(&[0x60, 0x00]); } code.push(0x73); - code.extend_from_slice(crate::precompiles::BRIDGE_ADDR.as_slice()); + code.extend_from_slice(precompile.as_slice()); code.extend_from_slice(&[0x62, 0x01, 0x86, 0xa0, opcode]); // The flag plus one to slot 0, then STOP. code.extend_from_slice(&[0x60, 0x01, 0x01, 0x60, 0x00, 0x55, 0x00]); @@ -5864,7 +5884,6 @@ mod tests { &code, store.as_ref(), ); - let block_store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); let executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); let tx = rustock_core::Transaction { nonce: 0, @@ -5879,7 +5898,7 @@ mod tests { cached_rlp: None, }; let result = executor - .execute_block(&dummy_header(block), &[(tx, sender)], &root, store.clone()) + .execute_block(header, &[(tx, sender)], &root, store.clone()) .expect("block"); let stored = result .state_changes @@ -5976,6 +5995,78 @@ mod tests { assert!(output_at(764_123).is_empty(), "from RSKIP88"); } + /// rskj's `getBitcoinHeader` answers null for a block with no Bitcoin + /// header, which only a genesis block read from a file without one is: + /// every later block needs one for its proof of work. + /// `NativeContract.execute` passes the null on, and `Program` makes of it + /// what it makes of the Bridge's. + #[test] + fn a_native_contracts_null_ends_a_contracts_call_as_rskj_ends_it_in_each_era() { + let get_bitcoin_header = { + let mut input = crate::abi::selector("getBitcoinHeader(int256)").to_vec(); + input.extend_from_slice(&[0u8; 32]); + input + }; + // Mainnet's block `number`, and a store that holds its parent, which + // has no Bitcoin header. + let on_a_parent_without_one = |number: u64| { + let block_store = + Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let parent = dummy_header(number - 1); + assert!(parent.bitcoin_merged_mining_header.is_none()); + block_store.put_header(&parent).unwrap(); + block_store + .put_canonical_hash(parent.number, parent.hash()) + .unwrap(); + let mut header = dummy_header(number); + header.parent_hash = parent.hash(); + (block_store, header) + }; + let from_a_contract = |number: u64| { + let (block_store, header) = on_a_parent_without_one(number); + a_precompile_called_from_a_contract( + block_store, + &header, + crate::precompiles::BLOCK_HEADER_ADDR, + 0xf1, + &get_bitcoin_header, + ) + }; + // Before RSKIP150 `Program.memorySave` reads the null's length, which + // nothing in the VM catches: the contract ends there. + assert_eq!(from_a_contract(1_700_000), (false, 0, 0)); + // From RSKIP150 `saveOutAfterExecution` skips a null. + assert_eq!(from_a_contract(2_100_000), (true, 2, 0)); + // From RSKIP197 `Memory.write` reads its length inside the try, and + // the call fails. + assert_eq!(from_a_contract(4_000_000), (true, 1, 0)); + + // A transaction's own call succeeds with no output + // (`TransactionExecutor.call`). + let (block_store, header) = on_a_parent_without_one(4_000_000); + let store = Arc::new(MemoryTrieStore::new()); + let sender = Address::repeat_byte(0xAA); + let one_rbtc = U256::from(10u64).pow(U256::from(18)); + let root = put_account(&TrieNode::empty(), store.as_ref(), &sender, 0, one_rbtc); + let tx = rustock_core::Transaction { + nonce: 0, + gas_price: U256::ZERO, + gas_limit: U256::from(100_000), + to: Bytes::copy_from_slice(crate::precompiles::BLOCK_HEADER_ADDR.as_slice()), + value: U256::ZERO, + input: Bytes::from(get_bitcoin_header.clone()), + v: 0, + r: U256::ZERO, + s: U256::ZERO, + cached_rlp: None, + }; + let result = RskExecutor::new(RskHardforkConfig::mainnet(), block_store) + .execute_block(&header, &[(tx, sender)], &root, store.clone()) + .expect("block"); + assert!(result.tx_results[0].success); + assert!(result.tx_results[0].output.is_empty()); + } + /// rskj `doSUICIDE` (VM.java) charges NEW_ACCT_SUICIDE (25,000) whenever the /// beneficiary does not exist (`!track.isExist`), UNCONDITIONAL on the /// suiciding contract's balance. revm's selfdestruct instead gates the diff --git a/crates/execution/src/native/block_header.rs b/crates/execution/src/native/block_header.rs index 96768c5..92a0d19 100644 --- a/crates/execution/src/native/block_header.rs +++ b/crates/execution/src/native/block_header.rs @@ -122,11 +122,10 @@ impl NativeContract for BlockHeader<'_> { let Some((hash, header)) = block else { return Ok(encode_bytes(&[])); }; - Ok(match self.read(&call, hash, &header)? { - Some(value) => encode_bytes(&value), - // A method's `null`: `NativeContract.execute` returns no bytes at all. - None => Vec::new(), - }) + match self.read(&call, hash, &header)? { + Some(value) => Ok(encode_bytes(&value)), + None => Err(Failure::Null), + } } } @@ -663,8 +662,10 @@ mod tests { assert!(fatal(get("getCoinbaseAddress(int256)", &[int(6)]))); } + /// rskj holds a header's field it read from an empty item as null, and + /// `getBitcoinHeader` returns the field as it is. #[test] - fn a_block_without_a_bitcoin_header_answers_with_no_bytes_at_all() { + fn a_block_without_a_bitcoin_header_answers_null() { let (_dir, blocks, hashes) = chain_with(12, |block| match block.number { 11 => block.bitcoin_merged_mining_header = None, // Stored and read back as an empty field, which rskj reads as null. @@ -675,10 +676,7 @@ mod tests { for number in [12, 13] { assert_eq!( call(&at(&blocks, &hashes, number), &get, 1_000_000), - Outcome::Returned { - gas: 4_072, - output: Bytes::new() - }, + Outcome::Null { gas: 4_072 }, "block {}", number - 1 ); diff --git a/crates/execution/src/native/mod.rs b/crates/execution/src/native/mod.rs index c41525e..dcdaa56 100644 --- a/crates/execution/src/native/mod.rs +++ b/crates/execution/src/native/mod.rs @@ -14,10 +14,12 @@ //! caller charges it before anything runs, and a call that cannot pay it is //! out of gas. //! 3. `execute` runs the method and ABI-encodes its result. It throws when -//! there is no call, and wherever the method refuses its arguments. +//! there is no call, and wherever the method refuses its arguments. A +//! method's `null` it returns as it is, with nothing encoded. //! //! What a throw costs depends on who made the call, a transaction or a -//! contract, and on RSKIP197: `RskPrecompileProvider` settles it. +//! contract, and on RSKIP197; what a `null` does depends on the same and on +//! RSKIP150. `RskPrecompileProvider` settles both. pub(crate) mod block_header; pub(crate) mod environment; @@ -31,6 +33,10 @@ pub(crate) enum Outcome { /// `execute` returned `output`, and the call cost `gas`: its /// `getGasForData`. Returned { gas: u64, output: Bytes }, + /// `execute` returned rskj's `null`, and the call cost `gas`: its + /// `getGasForData`. A transaction's own call ends with no output, and a + /// contract's as `Program` takes a null output where the block is. + Null { gas: u64 }, /// `getGasForData` asked for more gas than the call carries, so nothing /// ran. OutOfGas, @@ -42,11 +48,14 @@ pub(crate) enum Outcome { Fatal(String), } -/// Why a method did not return. +/// Why a method has no result to encode. #[derive(Debug, PartialEq, Eq)] pub(crate) enum Failure { /// rskj's method throws. Threw(String), + /// rskj's method returns `null`, which `NativeContract.execute` returns + /// in its turn. + Null, /// The node could not read what rskj's method reads. Fatal(String), } @@ -96,6 +105,7 @@ pub(crate) fn call(contract: &C, input: &[u8], gas_limit: u64 gas, output: output.into(), }, + Err(Failure::Null) => Outcome::Null { gas }, Err(Failure::Threw(reason)) => Outcome::Threw { gas, reason }, Err(Failure::Fatal(reason)) => Outcome::Fatal(reason), } diff --git a/crates/execution/src/precompiles.rs b/crates/execution/src/precompiles.rs index ab2f912..8a3783d 100644 --- a/crates/execution/src/precompiles.rs +++ b/crates/execution/src/precompiles.rs @@ -1188,6 +1188,15 @@ impl RskPrecompileProvider { /// asked for and the call fails. Before it the throw escapes the VM and /// fails the contract that made the call, with all its gas: the call fails /// here, and `RskHandler` halts that contract on the flag set here. + /// + /// A precompile that returns rskj's `null` gives a transaction's own call + /// no output. In a contract's call `Program` reads the output's length as + /// it writes the output to memory. From RSKIP197 it does so inside the + /// call's own `try` (`executePrecompiledAndHandleError`), and the call + /// fails as it does for a throw. Before RSKIP150 `memorySave` does, with + /// nothing to catch it, and the contract that made the call fails as it + /// did for a throw then. Between the two `saveOutAfterExecution` skips a + /// null, and the call succeeds with nothing written. fn finish( &self, context: &CTX, @@ -1201,7 +1210,9 @@ impl RskPrecompileProvider { output: Bytes::new(), }; let gas = match &outcome { - Outcome::Returned { gas, .. } | Outcome::Threw { gas, .. } => *gas, + Outcome::Returned { gas, .. } | Outcome::Null { gas } | Outcome::Threw { gas, .. } => { + *gas + } Outcome::OutOfGas | Outcome::Fatal(_) => 0, }; // Held to the limit before the precompile ran, so this always fits. @@ -1215,6 +1226,22 @@ impl RskPrecompileProvider { self.record_called(addr); result.output = output; } + Outcome::Null { .. } => { + let rskip197 = self.rskip197_active(context); + let skipped = !rskip197 + && self + .hardfork_cfg + .has_rskip150(context.block().number().to::()); + if context.journal().depth() == 1 || skipped { + self.record_called(addr); + } else if rskip197 { + result.result = InstructionResult::Revert; + } else { + self.aborts_caller + .store(true, std::sync::atomic::Ordering::Relaxed); + result.result = InstructionResult::PrecompileError; + } + } Outcome::OutOfGas => result.result = InstructionResult::PrecompileOOG, Outcome::Threw { reason, .. } => { tracing::debug!(target: "rustock::precompile", %addr, %reason, "precompile threw"); From 18253c6ae68fcc59150515225045c4fe15081830 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Thu, 12 Nov 2026 21:10:11 -0800 Subject: [PATCH 118/264] hardfork: a rule moves with its own upgrade when a schedule is overridden rskj reads each rule's height from the upgrade reference.conf puts it at, or from the chain's own consensusRules entry, and hardforkActivationHeights moves one upgrade without touching the others. Five places here answered otherwise once --hardfork moved an upgrade. None changes mainnet's, testnet's or either regtest's schedule as built in. - rskj's regtest listed only its last two upgrades, so moving those left the blocks below them on no upgrade at all, where regtest.conf keeps every other one at block 0. Both regtest ladders now list every upgrade they schedule. - RSKIP305 was at a fixed height per chain id. It is at reed800. - RSKIP252 and RSKIP110 were at fixed heights per chain id in the block processor, the miner and the pool. They are at fingerroot500 and wasabi100, read from the ladder. - RSKIP536 followed whichever of reed810 and vetiver900 came first. main.conf gives it a height of its own, 8,804,200, which now stays put when vetiver900 moves; elsewhere it is at reed810, and off where reed810 is. The ladders without reed810 that have the rule from block 0 hold it as a rule of their own. - hop400 was not among the upgrades the header rules' heights take, so moving it left RSKIP297's Bitcoin timestamp window where it was on a testnet chain. It moves now, and RSKIP290 with it where the chain keeps it at hop400. What stays as it was: an upgrade moved above a later one is in force from the later one's height (active_upgrade), where rskj keeps the two apart. Tests: a_regtest_keeps_its_earlier_upgrades_when_its_last_ones_move, rskip305_moves_with_reed800, rskip536_follows_reed810_or_the_chains_own_height, the_price_cap_and_fork_detection_move_with_their_upgrades, hop400_moves_what_the_chain_keeps_at_it. --- crates/core/src/config.rs | 57 +++++++++- crates/execution/src/hardfork.rs | 169 +++++++++++++++++++++++++----- crates/execution/src/processor.rs | 44 +++++--- crates/sync/src/txpool.rs | 10 +- 4 files changed, 230 insertions(+), 50 deletions(-) diff --git a/crates/core/src/config.rs b/crates/core/src/config.rs index 0eae671..a61d34a 100644 --- a/crates/core/src/config.rs +++ b/crates/core/src/config.rs @@ -113,13 +113,13 @@ impl ActivationHeights { /// Set the height this struct carries for an rskj upgrade name, for the /// names it carries: `orchid`, `wasabi100`, `papyrus200`, `iris300`, - /// `fingerroot500` and `reed810`. `None` is rskj's `-1`. Any other name is - /// the execution ladder's to judge and is left alone here. + /// `hop400`, `fingerroot500` and `reed810`. `None` is rskj's `-1`. Any + /// other name is the execution ladder's to judge and is left alone here. /// /// The rules `reference.conf` puts at an upgrade move with it where the - /// chain keeps them there: RSKIPUMM with papyrus200, RSKIP144 and - /// RSKIP351 with reed810. A chain that sets its own, as rskj's regtest - /// does, keeps them. + /// chain keeps them there: RSKIPUMM with papyrus200, RSKIP290 with + /// hop400, RSKIP144 and RSKIP351 with reed810. A chain that sets its own, + /// as rskj's regtest does and testnet for RSKIP290, keeps them. pub fn set_rskj_hardfork(&mut self, name: &str, height: Option) { let never = u64::MAX; let at = |upgrade: u64| (upgrade != never).then_some(upgrade); @@ -133,6 +133,12 @@ impl ActivationHeights { self.papyrus200 = height.unwrap_or(never); } "iris300" => self.iris300 = height.unwrap_or(never), + "hop400" => { + if self.rskip290 == at(self.hop400) { + self.rskip290 = height; + } + self.hop400 = height.unwrap_or(never); + } "fingerroot500" => self.fingerroot500 = height.unwrap_or(never), "reed810" => { let followed = at(self.reed810); @@ -879,6 +885,47 @@ mod tests { assert_eq!(reed(®test), (5, Some(1), Some(1))); } + /// `reference.conf` has RSKIP297 and RSKIP290 at hop400. Moving hop400 + /// moves RSKIP297's wider Bitcoin timestamp window, which testnet reads + /// at it, and RSKIP290 where the chain keeps it there: `testnet.conf` + /// gives RSKIP290 a height of its own. + #[test] + fn hop400_moves_what_the_chain_keeps_at_it() { + use crate::validation::merged_mining::BitcoinTimestampRule; + + let mut testnet = ChainConfig::testnet(); + testnet + .activation_heights + .set_rskj_hardfork("hop400", Some(3_200_000)); + assert_eq!( + ( + testnet.activation_heights.hop400, + testnet.activation_heights.rskip290 + ), + (3_200_000, Some(2_581_800)) + ); + let rule = BitcoinTimestampRule { + config: std::sync::Arc::new(testnet), + }; + assert_eq!( + rule.max_timestamps_diff(3_199_999), + BitcoinTimestampRule::DEFAULT_MAX_TIMESTAMPS_DIFF_IN_SECS + ); + assert_eq!( + rule.max_timestamps_diff(3_200_000), + BitcoinTimestampRule::TESTNET_MAX_TIMESTAMPS_DIFF_IN_SECS + ); + + let mut mainnet = ActivationHeights::mainnet(); + mainnet.set_rskj_hardfork("hop400", Some(5_000_000)); + assert_eq!( + (mainnet.hop400, mainnet.rskip290), + (5_000_000, Some(5_000_000)) + ); + mainnet.set_rskj_hardfork("hop400", None); + assert_eq!((mainnet.hop400, mainnet.rskip290), (u64::MAX, None)); + } + #[test] fn test_mainnet_chain_config() { let config = ChainConfig::mainnet(); diff --git a/crates/execution/src/hardfork.rs b/crates/execution/src/hardfork.rs index bef8d6c..42b9422 100644 --- a/crates/execution/src/hardfork.rs +++ b/crates/execution/src/hardfork.rs @@ -138,8 +138,9 @@ pub struct RskHardforkConfig { activations: Vec<(u64, RskNetworkUpgrade)>, pub chain_id: u64, /// Rules scheduled apart from their upgrade (`consensusRules`). `None` - /// is rskj's `-1`: never. Empty on every built-in chain but rskj's - /// regtest. + /// is rskj's `-1`: never. Mainnet has RSKIP536 here, as `main.conf` has + /// it, and rskj's regtest the three `regtest.conf` moves; the ladders + /// without reed810 that have RSKIP536 all the same hold it here too. rule_overrides: Vec<(ConsensusRule, Option)>, /// rskj's `afterBridgeSync`, where `areBridgeTxsPaid` activates: before /// it, the federators' and authorizers' Bridge transactions cost nothing @@ -153,7 +154,9 @@ impl RskHardforkConfig { Self { activations: MAINNET_ACTIVATIONS.to_vec(), chain_id: RSK_MAINNET_CHAIN_ID, - rule_overrides: Vec::new(), + // main.conf's `consensusRules`: `rskip536 = 8804200`, a height of + // its own where `reference.conf` has reed810. + rule_overrides: vec![(ConsensusRule::Rskip536, Some(8_804_200))], after_bridge_sync: Self::after_bridge_sync_for(RSK_MAINNET_CHAIN_ID), } } @@ -170,13 +173,17 @@ impl RskHardforkConfig { /// rustock's own regtest: every upgrade on the ladder from block 0, with /// RSKIP535 from block 1 and every Bridge transaction paid from block 0. /// It schedules neither reed810 nor arrowhead631, so its blocks run as - /// one list and the union Bridge's methods stay closed. A chain started + /// one list and the union Bridge's methods stay closed; RSKIP536, which + /// rskj has at reed810, it has from block 0 all the same. A chain started /// from an rskj genesis file is rskj's regtest, [`Self::rskj_regtest`]. pub fn regtest() -> Self { Self { - activations: vec![(0, RskNetworkUpgrade::Vetiver900)], + activations: Self::from_genesis_without(&[ + RskNetworkUpgrade::Arrowhead631, + RskNetworkUpgrade::Reed810, + ]), chain_id: RSK_REGTEST_CHAIN_ID, - rule_overrides: Vec::new(), + rule_overrides: vec![(ConsensusRule::Rskip536, Some(0))], after_bridge_sync: Self::after_bridge_sync_for(RSK_REGTEST_CHAIN_ID), } } @@ -190,10 +197,7 @@ impl RskHardforkConfig { /// upgrade for RSKIP559, unscheduled, and this ladder doesn't model it. pub fn rskj_regtest() -> Self { Self { - activations: vec![ - (0, RskNetworkUpgrade::Reed810), - (0, RskNetworkUpgrade::Vetiver900), - ], + activations: Self::from_genesis_without(&[RskNetworkUpgrade::Arrowhead631]), chain_id: RSK_REGTEST_CHAIN_ID, rule_overrides: vec![ (ConsensusRule::Rskip144, Some(1)), @@ -221,7 +225,7 @@ impl RskHardforkConfig { /// /// Arrowhead631 is named for the same reason: testnet never schedules /// it, so RSKIP-434 asks whether it is scheduled, not whether a later - /// upgrade is active. + /// upgrade is active. RSKIP536 is on without reed810, as on mainnet. pub fn all_active(chain_id: u64) -> Self { Self { activations: vec![ @@ -229,7 +233,7 @@ impl RskHardforkConfig { (0, RskNetworkUpgrade::Vetiver900), ], chain_id, - rule_overrides: Vec::new(), + rule_overrides: vec![(ConsensusRule::Rskip536, Some(0))], after_bridge_sync: Self::after_bridge_sync_for(chain_id), } } @@ -263,6 +267,56 @@ impl RskHardforkConfig { self.activations.iter().find(|(_, u)| *u == upgrade).map(|(h, _)| *h) } + /// Every upgrade at block 0 but `unscheduled`, which stay at rskj's `-1`. + fn from_genesis_without(unscheduled: &[RskNetworkUpgrade]) -> Vec<(u64, RskNetworkUpgrade)> { + use RskNetworkUpgrade::*; + [ + Orchid, + Wasabi100, + TwoToThree, + Papyrus200, + Iris300, + Hop400, + Fingerroot500, + Arrowhead600, + Arrowhead631, + Lovell700, + Reed800, + Reed810, + Vetiver900, + ] + .into_iter() + .filter(|upgrade| !unscheduled.contains(upgrade)) + .map(|upgrade| (0, upgrade)) + .collect() + } + + /// The first block `active_upgrade` is at `upgrade` or past it: the + /// lowest height of that rung or a later one, or `None` where the + /// ladder has none of them. + fn in_force_from(&self, upgrade: RskNetworkUpgrade) -> Option { + self.activations + .iter() + .filter(|(_, rung)| *rung >= upgrade) + .map(|(height, _)| *height) + .min() + } + + /// RSKIP252 (fingerroot500): the first block whose transactions' gas + /// prices are capped, or `u64::MAX` where none is. The block rule, the + /// miner's selection and the pool's cap all read it. + pub fn rskip252_height(&self) -> u64 { + self.in_force_from(RskNetworkUpgrade::Fingerroot500) + .unwrap_or(u64::MAX) + } + + /// RSKIP110 (wasabi100): the first block whose fork detection data is + /// checked, or `u64::MAX` where none is. + pub fn rskip110_height(&self) -> u64 { + self.in_force_from(RskNetworkUpgrade::Wasabi100) + .unwrap_or(u64::MAX) + } + /// rskj `ActivationConfig.ForBlock.isActivating`: the upgrade activates /// exactly at this block. pub fn is_activating(&self, upgrade: RskNetworkUpgrade, block_number: u64) -> bool { @@ -662,15 +716,10 @@ impl RskHardforkConfig { } /// RSKIP305: committed federations become P2SH-P2WSH-ERP federations - /// instead of P2SH-ERP (mainnet reed800 = 8,052,200; testnet 6,835,700; + /// instead of P2SH-ERP (Reed800: mainnet 8,052,200; testnet 6,835,700; /// regtest 0). pub fn has_rskip305(&self, block_number: u64) -> bool { - let height = match self.chain_id { - RSK_MAINNET_CHAIN_ID => 8_052_200, - RSK_TESTNET_CHAIN_ID => 6_835_700, - _ => 0, - }; - block_number >= height + self.active_upgrade(block_number) >= RskNetworkUpgrade::Reed800 } /// RSKIP516 (Reed800): the secp256k1 addition and multiplication @@ -975,18 +1024,20 @@ impl RskHardforkConfig { } /// RSKIP536: additional BlockHeader precompile methods. Maps to reed810 in - /// reference.conf; mainnet overrides it to the vetiver900 height (8_804_200) - /// via main.conf consensusRules. `>= Reed810` is exact on both networks - /// because mainnet jumps Reed800 -> Vetiver900 at that height. + /// reference.conf; mainnet gives it a height of its own, vetiver900's + /// (8_804_200), in main.conf's consensusRules. /// - /// So this one *is* an ordinal comparison, deliberately, and differs from - /// `has_rskip144` right above it: both map to reed810 in reference.conf, - /// but mainnet overrides this one and not that one. + /// Not an ordinal comparison, for `has_rskip144`'s reason. `>= Reed810` + /// is right on both networks as they are, since mainnet jumps from + /// Reed800 to Vetiver900 at that height, and wrong once either upgrade + /// moves: rskj keeps mainnet's rule at its own height when vetiver900 + /// moves, and testnet's at reed810 wherever that goes. pub fn has_rskip536(&self, block_number: u64) -> bool { if let Some(height) = self.rule_override(ConsensusRule::Rskip536) { return height.is_some_and(|h| block_number >= h); } - self.active_upgrade(block_number) >= RskNetworkUpgrade::Reed810 + self.activation_height(RskNetworkUpgrade::Reed810) + .is_some_and(|h| block_number >= h) } /// RSKIP540: min pegout value as extra pegout in fee estimation; @@ -1462,7 +1513,11 @@ mod override_tests { #[test] fn mainnet_without_overrides_is_unchanged() { let cfg = RskHardforkConfig::mainnet(); - assert_eq!(cfg.rule_overrides, Vec::new()); + // main.conf's one rule of its own. + assert_eq!( + cfg.rule_overrides, + vec![(ConsensusRule::Rskip536, Some(8_804_200))] + ); assert_eq!(cfg.activation_height(RskNetworkUpgrade::Reed810), None); assert!(!cfg.has_rskip144(9_003_580)); assert!(!cfg.has_rskip351(9_003_580)); @@ -1492,7 +1547,13 @@ mod override_tests { .with_rskj_overrides(&[], &[h("rskipUMM", 1)]) .unwrap(); assert_eq!(umm.activations, RskHardforkConfig::mainnet().activations); - assert_eq!((umm.rule_overrides, unmodelled), (Vec::new(), Vec::::new())); + assert_eq!( + (umm.rule_overrides, unmodelled), + ( + RskHardforkConfig::mainnet().rule_overrides, + Vec::::new() + ) + ); let unknown = RskHardforkConfig::mainnet() .with_rskj_overrides(&[h("quartz", 1)], &[]); assert!(unknown.is_err()); @@ -1512,6 +1573,58 @@ mod override_tests { assert_eq!(cfg.active_upgrade(7_604_200), RskNetworkUpgrade::Vetiver900); } + /// rskj's `regtest.conf` has every upgrade but arrowhead631 at block 0, + /// and rustock's own regtest every one but that and reed810. Moving the + /// last ones leaves the earlier ones' rules where they were. + #[test] + fn a_regtest_keeps_its_earlier_upgrades_when_its_last_ones_move() { + for regtest in [ + RskHardforkConfig::rskj_regtest(), + RskHardforkConfig::regtest(), + ] { + let (moved, _) = regtest + .with_rskj_overrides(&[h("reed810", 10), h("vetiver900", 20)], &[]) + .unwrap(); + assert_eq!(moved.active_upgrade(0), RskNetworkUpgrade::Reed800); + assert!(moved.has_rskip88(0) && moved.has_rskip197(9) && moved.has_rskip516(0)); + assert_eq!(moved.active_upgrade(10), RskNetworkUpgrade::Reed810); + assert!(!moved.has_rskip540(19) && moved.has_rskip540(20)); + } + } + + /// `reference.conf` has RSKIP305 at reed800, so it moves with reed800. + #[test] + fn rskip305_moves_with_reed800() { + let (moved, _) = RskHardforkConfig::testnet() + .with_rskj_overrides(&[h("reed800", 7_000_000)], &[]) + .unwrap(); + assert!(!moved.has_rskip305(6_999_999) && moved.has_rskip305(7_000_000)); + assert!(!moved.has_rskip516(6_999_999) && moved.has_rskip516(7_000_000)); + } + + /// RSKIP536 is at reed810 (`reference.conf`) unless the chain gives it a + /// height of its own, as `main.conf` does: #8,804,200, which stays where + /// it is when vetiver900 moves. + #[test] + fn rskip536_follows_reed810_or_the_chains_own_height() { + let (mainnet, _) = RskHardforkConfig::mainnet() + .with_rskj_overrides(&[h("vetiver900", 9_000_000)], &[]) + .unwrap(); + assert!(!mainnet.has_rskip536(8_804_199) && mainnet.has_rskip536(8_804_200)); + let (later, _) = RskHardforkConfig::testnet() + .with_rskj_overrides(&[h("reed810", 8_000_000)], &[]) + .unwrap(); + assert!(!later.has_rskip536(7_999_999) && later.has_rskip536(8_000_000)); + let (never, _) = RskHardforkConfig::testnet() + .with_rskj_overrides(&[h("reed810", -1)], &[]) + .unwrap(); + assert!(!never.has_rskip536(u64::MAX)); + // rustock's own regtest and the all-active ladders keep it from block 0. + assert!(RskHardforkConfig::regtest().has_rskip536(0)); + assert!(RskHardforkConfig::all_active(30).has_rskip536(0)); + assert!(RskHardforkConfig::all_active_parallel(33).has_rskip536(0)); + } + /// rskj picks `executeParallel` by RSKIP-351 and RSKIP-144 together. /// Moving rskip351 alone runs testnet's RSKIP-144 blocks as one list. #[test] diff --git a/crates/execution/src/processor.rs b/crates/execution/src/processor.rs index 668c029..aa9ee50 100644 --- a/crates/execution/src/processor.rs +++ b/crates/execution/src/processor.rs @@ -879,15 +879,12 @@ impl BlockProcessor { /// Shared with the miner, which applies the same cap when it selects /// transactions (rskj `MinerUtils.isHighGasPriced`). pub fn rskip252_height(&self) -> u64 { - rskip252_height_for(self.hardfork_cfg.chain_id) + self.hardfork_cfg.rskip252_height() } /// RSKIP110 (`wasabi100`) for this chain. fn rskip110_height(&self) -> u64 { - match self.hardfork_cfg.chain_id { - crate::hardfork::RSK_MAINNET_CHAIN_ID => 1_591_000, - _ => 0, - } + self.hardfork_cfg.rskip110_height() } /// Execute all transactions in a block and validate results against the header. @@ -1744,6 +1741,32 @@ mod tests { ); } + /// `reference.conf` has RSKIP252, the cap on a transaction's gas price, + /// at fingerroot500, and RSKIP110, the fork detection data, at wasabi100. + /// Each is read at its upgrade's height, wherever the chain has it. + #[test] + fn the_price_cap_and_fork_detection_move_with_their_upgrades() { + let heights = |cfg: RskHardforkConfig| { + let block_store = + Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let processor = BlockProcessor::new(cfg, block_store); + (processor.rskip252_height(), processor.rskip110_height()) + }; + assert_eq!( + heights(RskHardforkConfig::mainnet()), + (5_468_000, 1_591_000) + ); + assert_eq!(heights(RskHardforkConfig::testnet()), (4_015_800, 0)); + assert_eq!(heights(RskHardforkConfig::regtest()), (0, 0)); + assert_eq!(heights(RskHardforkConfig::rskj_regtest()), (0, 0)); + + let at = |name: &str, height: u64| (name.to_string(), Some(height)); + let (moved, _) = RskHardforkConfig::testnet() + .with_rskj_overrides(&[at("wasabi100", 100), at("fingerroot500", 4_500_000)], &[]) + .unwrap(); + assert_eq!(heights(moved), (4_500_000, 100)); + } + /// What REMASC pays the null address reaches its account after the /// transactions, outside their state changes, and the block's supply check /// counts it there: the payment moves value and burns none. @@ -3507,14 +3530,3 @@ mod sender_cache_integration { ); } } - -/// RSKIP252 (`fingerroot500`) for a chain id: mainnet's and testnet's -/// heights, genesis anywhere else. The block rule, the miner's selection -/// and the pool's per-transaction cap all read it. -pub fn rskip252_height_for(chain_id: u64) -> u64 { - match chain_id { - crate::hardfork::RSK_MAINNET_CHAIN_ID => 5_468_000, - crate::hardfork::RSK_TESTNET_CHAIN_ID => 4_015_800, - _ => 0, - } -} diff --git a/crates/sync/src/txpool.rs b/crates/sync/src/txpool.rs index 45013f4..1f58cb8 100644 --- a/crates/sync/src/txpool.rs +++ b/crates/sync/src/txpool.rs @@ -74,7 +74,7 @@ impl ChainRules { let constants = rustock_execution::executor::bridge_constants_for(hardfork_cfg.chain_id); Self { free_bridge_senders: rustock_execution::executor::free_bridge_senders(&constants), - rskip252_height: rustock_execution::processor::rskip252_height_for(hardfork_cfg.chain_id), + rskip252_height: hardfork_cfg.rskip252_height(), min_sequential_set_gas_limit, hardfork_cfg, } @@ -2151,6 +2151,14 @@ mod tests { fn validate_when_rskip252_disabled_then_tx_valid_regardless_gas_price() { let rules = ChainRules::for_chain(rustock_execution::hardfork::RskHardforkConfig::mainnet(), 10_000_000); assert_eq!(rules.rskip252_height, 5_468_000); + // It is fingerroot500's height, wherever the chain has that. + let (moved, _) = rustock_execution::hardfork::RskHardforkConfig::mainnet() + .with_rskj_overrides(&[("fingerroot500".to_string(), Some(6_000_000))], &[]) + .unwrap(); + assert_eq!( + ChainRules::for_chain(moved, 10_000_000).rskip252_height, + 6_000_000 + ); let (pool, key, _) = setup_pool_with_rules(U256::from(10u64).pow(U256::from(20)), 0, test_config(), Some(rules)); let cap = 59_240_000 * TX_GAS_PRICE_CAP_MULTIPLIER; From a35a815f63332450c77f866e47b6f6563bf2a6c0 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Thu, 12 Nov 2026 21:10:11 -0800 Subject: [PATCH 119/264] sync: a block sent whole is weighed with the uncles it brings rskj gives a block it connects its parent's total difficulty plus Block.getCumulativeDifficulty, its own difficulty and its uncles' (BlockChainImpl.internalTryToConnect), and picks its best block by that total. on_new_block ingested the header before it stored the body, so the block got the lower bound a bare header gets: its own difficulty alone. A block beside the head whose own difficulty was below the head's, and whose uncles carried it past, stayed beside the chain here while rskj made it the best block. Holding it whole then stopped the node taking it again. The uncles are in hand when a whole block arrives, and the body check has held them to the header. SyncManager::note_whole_block_difficulty puts them through the uncle rule against the ancestry held and records what the block adds, and the header's ingest uses it. What stays a lower bound: a block whose ancestors' uncles this node doesn't hold, a parent beside the chain known by its header alone, can't have its uncles judged, as before. Test: a_whole_block_is_weighed_with_the_uncles_it_brings. --- crates/sync/src/manager.rs | 33 ++++++++++++++++++++++ crates/sync/src/service.rs | 9 ++++-- crates/sync/src/tests.rs | 57 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 3 deletions(-) diff --git a/crates/sync/src/manager.rs b/crates/sync/src/manager.rs index 5804ebd..28577c7 100644 --- a/crates/sync/src/manager.rs +++ b/crates/sync/src/manager.rs @@ -194,6 +194,39 @@ impl SyncManager { } } + /// Record what a block sent whole adds to the chain's work, from the + /// uncles that came with it: its own difficulty and theirs, once they + /// pass the whole uncle rule against the ancestry this node holds. The + /// header's ingest then gives the block the total rskj gives it on + /// connecting it (`Block.getCumulativeDifficulty`), and the head is + /// picked by that, where a bare header's lower bound would leave a block + /// whose uncles carry it past the head beside the chain. + /// + /// The caller has held the body to the header's commitments, so the list + /// is the one the block's miner chose. Uncles that can't be judged, or + /// that fail the rule, are left to the ordinary path, which counts the + /// header alone. + pub(crate) fn note_whole_block_difficulty(&self, header: &Header, uncles: &[Header]) { + if uncles.is_empty() { + return; + } + match crate::total_difficulty::credited_uncles(&self.store, &self.verifier, header, uncles) + { + Ok(credited) => { + let mut proven = self.proven.lock().unwrap_or_else(PoisonError::into_inner); + if proven.len() >= MAX_PROVEN { + proven.clear(); + } + proven.insert(header.hash(), header.difficulty.saturating_add(credited)); + } + Err(why) => debug!( + target: "rustock::sync", + "Block #{}: its uncles are not credited with their difficulty: {why}", + header.number + ), + } + } + /// Settle the total difficulty of each of `blocks`, oldest first, now that /// their bodies are held. See [`crate::total_difficulty`]. /// diff --git a/crates/sync/src/service.rs b/crates/sync/src/service.rs index 5617f0d..797412a 100644 --- a/crates/sync/src/service.rs +++ b/crates/sync/src/service.rs @@ -4327,9 +4327,11 @@ impl SyncService { /// or not. Here the body is held to its header's commitments, the header is /// verified and stored as any header at the tip is (the same rules, the /// extra data's size among them), the block is relayed, and it is executed - /// once it is the canonical block at its height. A block whose parent this - /// node lacks is taken as an announcement of it: the headers up to it are - /// asked of the sender, where rskj asks it for the missing block. + /// once it is the canonical block at its height. Its uncles are in hand, so + /// it is weighed with them, as rskj weighs a block it connects. A block + /// whose parent this node lacks is taken as an announcement of it: the + /// headers up to it are asked of the sender, where rskj asks it for the + /// missing block. pub(crate) async fn on_new_block(&mut self, peer: B512, block: Block) { // rskj processes it while syncing too, but relays nothing then // (`hasBetterBlockToSync`); this node takes nothing unasked at the tip @@ -4370,6 +4372,7 @@ impl SyncService { self.charge_bad_body(peer, number, what); return; } + self.manager.note_whole_block_difficulty(&header, &ommers); let before_height = self.our_head_number(); let before_hash = self.manager.store.head().ok().flatten(); match self diff --git a/crates/sync/src/tests.rs b/crates/sync/src/tests.rs index 9fe4468..442e6b4 100644 --- a/crates/sync/src/tests.rs +++ b/crates/sync/src/tests.rs @@ -8493,6 +8493,63 @@ async fn a_followed_block_counts_its_uncles_when_its_body_comes() { ); } +/// rskj gives a block it is sent whole its parent's total and its cumulative +/// difficulty, uncles included (`BlockChainImpl.internalTryToConnect`), and +/// picks its best block by that. A block beside the head whose own +/// difficulty is below the head's, and whose uncles carry it past, is the +/// new head. +#[tokio::test] +async fn a_whole_block_is_weighed_with_the_uncles_it_brings() { + let dir = tempdir().unwrap(); + let store = Arc::new(BlockStore::open(dir.path()).unwrap()); + let (b1, uncles) = genesis_and_siblings(&store, 7, 5); + let peer_store = Arc::new(rustock_networking::peers::PeerStore::new()); + let manager = Arc::new(SyncManager::new( + store.clone(), + Arc::new(HeaderVerifier::new()), + peer_store.clone(), + )); + manager + .handle_headers_response(vec![b1.clone()], test_peer()) + .unwrap(); + store.put_body(b1.hash(), &[], &[]).unwrap(); + store.set_exec_head(b1.hash(), B256::ZERO).unwrap(); + // The head: #2, worth 10, for a total of 21. + let head = dummy_header(2, b1.hash(), U256::from(10)); + manager + .handle_headers_response(vec![head.clone()], test_peer()) + .unwrap(); + store.put_body(head.hash(), &[], &[]).unwrap(); + assert_eq!(store.head().unwrap(), Some(head.hash())); + + // Its rival: worth 9 itself, and 12 more through the two uncles it names. + let rival = naming_uncles(fork_header(2, b1.hash(), U256::from(9), 0xee), &uncles); + let (_event_tx, event_rx) = mpsc::unbounded_channel(); + let mut service = SyncService::new(manager, peer_store, event_rx); + service.state = SyncState::Following; + service + .on_new_block( + B512::repeat_byte(9), + rustock_core::Block { + header: rival.clone(), + transactions: Vec::new(), + ommers: uncles, + }, + ) + .await; + + assert_eq!( + store.total_difficulty(rival.hash()).unwrap(), + Some(U256::from(32)), + "its parent's 11, its own 9 and its uncles' 12" + ); + assert_eq!( + store.head().unwrap(), + Some(rival.hash()), + "the heavier block is the head" + ); +} + /// A header ingested again with its body held, as a fork announcement does to /// the last few hundred, is credited the body's uncles only once they pass the /// whole uncle rule, as settling does. From ec105660a6e89ba18f599c4456d52d57d3d72b6e Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Thu, 12 Nov 2026 21:10:11 -0800 Subject: [PATCH 120/264] bridge: a refund is built and settled as every other release rskj refunds a peg-in it turns down, and a flyover payment over the locking cap, with ReleaseTransactionBuilder.buildEmptyWalletTo, and settles the refund with settleReleaseRejection. Four things there were not as rskj has them. - settleReleaseRejection ends with processReleaseTransactionInfo, as the settling of a pegout and of a migration does: from RSKIP428 it logs pegout_transaction_created after release_requested, and from RSKIP305 it stores the values of the outputs the refund spends, under the refund's hash. Both refunds stopped at release_requested. One function now does it for all four. - The builder is given the federation's format, and for a P2SH-P2WSH federation it builds a refund as it builds a pegout (setSrConfiguration): the witness program in each input's scriptSig, the unsigned witness, and the fee over the virtual size. The refund was always built in the legacy form, which gave it another fee and another hash. The federation is the active one where the payment pays it, and the retiring one where it pays that one alone (getFederationFromTxOutputs). - Wallet.adjustOutputDownwardsForFee takes the fee per kilobyte over the size, and then raises the fee to bitcoinj's minimum of 5,000 satoshis. The minimum was put under the rate instead, as calculateFee puts it for the other builds, which gives a smaller fee wherever the rate over the size comes to less than the minimum. - A flyover refund spends the outputs that pay the federation's flyover address. They were looked for by the plain P2SH of the flyover redeem script, which a P2SH-P2WSH federation's address is not, so for such a federation no refund was built at all. What stays as it was: a payment to both the active and the retiring federation is refunded with one redeem script for every input. Tests: a_refunded_peg_in_is_registered_as_rskj_registers_it replays what rskj's release did (bridge/testdata/rskj_refunded_pegins.txt, written by running its TransactionExecutor; the file's header says how) and holds the logs, the gas, the Bridge's storage and the state root to it: for a refund at regtest's fee, for one at a fee per kilobyte that brings the minimum in, and for a flyover refund. Two tests that counted a refunded peg-in's events count three. --- crates/execution/src/bridge/peg.rs | 210 +++++++---- crates/execution/src/bridge/release_tx.rs | 40 ++- .../bridge/testdata/rskj_refunded_pegins.txt | 333 ++++++++++++++++++ crates/execution/src/executor.rs | 218 ++++++++++++ 4 files changed, 717 insertions(+), 84 deletions(-) create mode 100644 crates/execution/src/bridge/testdata/rskj_refunded_pegins.txt diff --git a/crates/execution/src/bridge/peg.rs b/crates/execution/src/bridge/peg.rs index 47fafa5..899ffd2 100644 --- a/crates/execution/src/bridge/peg.rs +++ b/crates/execution/src/bridge/peg.rs @@ -1367,13 +1367,28 @@ fn emit_pegin_rejection_release( coinbase: btc_tx.is_coinbase(), }) .collect(); - let refund_redeem = if pegin_utxos - .iter() - .all(|u| retiring.as_ref().is_some_and(|(_, rs)| u.script == rs.to_bytes())) - { - &retiring.as_ref().expect("checked above").0 - } else { - fed_redeem + // rskj `getFederationFromTxOutputs`: the refund is built for the active + // federation where the payment pays it, and for the retiring one where it + // pays that one alone. That federation's format decides where what spends + // an input goes: the scriptSig, or for a P2SH-P2WSH federation the + // witness (`ReleaseTransactionBuilder.setSrConfiguration`). + let retiring_redeem = retiring + .as_ref() + .filter(|(_, script)| { + pegin_utxos + .iter() + .all(|utxo| utxo.script == script.to_bytes()) + }) + .map(|(redeem, _)| redeem.as_slice()); + let (refund_redeem, format) = match retiring_redeem { + Some(redeem) => ( + redeem, + federation_format_version(ctx, super::storage::OLD_FEDERATION_FORMAT_VERSION_KEY), + ), + None => ( + fed_redeem, + active_federation_format(ctx, config, hardfork_cfg, rsk_height), + ), }; let fee_per_kb = get_effective_fee_per_kb(ctx, config); let tx_version = if hardfork_cfg.has_rskip201(rsk_height) { 2 } else { 1 }; @@ -1388,6 +1403,7 @@ fn emit_pegin_rejection_release( refund_redeem, fee_per_kb, tx_version, + format >= 4000, ); tracing::debug!( "pegin rejection release: utxos {} fee_per_kb {} built {:?}", @@ -1412,6 +1428,7 @@ fn emit_pegin_rejection_release( &btc_txid_event_bytes(&built.tx), total_value, ); + process_release_transaction_info(ctx, &built, hardfork_cfg, rsk_height); store_pegout_confirmation_set(ctx, &waiting, use_tx_hash); } else { // The refund was due and could not be built -- in practice the inputs @@ -2023,8 +2040,8 @@ pub fn register_fast_bridge_btc_transaction( // one has activated; its redeem follows the OLD federation's STORED format // version (`federation.getRedeemScript()`), so an ERP retiring federation // gets its ERP flyover redeem. The flyover output script is a plain P2SH for - // every federation format except P2SH-P2WSH-ERP (`getFlyoverFederationOutputScript`), - // which rustock does not yet support — feds at hop400 are P2SH-ERP. + // every federation format except P2SH-P2WSH-ERP, whose is the P2SH of the + // flyover script's witness program (`getFlyoverFederationOutputScript`). let retiring_format = federation_format_version(ctx, super::storage::OLD_FEDERATION_FORMAT_VERSION_KEY); let retiring_flyover = if hardfork_cfg.has_rskip293(block_number) { @@ -2123,9 +2140,17 @@ pub fn register_fast_bridge_btc_transaction( // flyover UTXOs of BOTH the active and (RSKIP293) retiring feds, resolving // each input's redeem from its flyover P2SH (FlyoverCompatibleBtcWallet- // WithMultipleScripts). - let mut flyover_redeems: Vec> = vec![flyover_redeem.clone()]; + let mut flyover_feds = vec![( + flyover_redeem.clone(), + flyover_script.clone(), + active_format, + )]; if let Some(r) = &retiring_flyover { - flyover_redeems.push(r.flyover_redeem.clone()); + flyover_feds.push(( + r.flyover_redeem.clone(), + r.flyover_script.clone(), + retiring_format, + )); } let mut extra = refund_fields(&refund_hash160, refund_is_p2sh, config); extra.push(("peginPath", FieldValue::Text("flyover".into()))); @@ -2143,7 +2168,7 @@ pub fn register_fast_bridge_btc_transaction( ); emit_flyover_rejection_release( ctx, &btc_tx, &refund_hash160, refund_is_p2sh, total_satoshis, - &flyover_redeems, rsk_height, config, hardfork_cfg, tx_ctx, + &flyover_feds, rsk_height, config, hardfork_cfg, tx_ctx, ); return ok(flyover_code_output(if is_lp { flyover_codes::REFUNDED_LP @@ -2359,6 +2384,31 @@ fn save_release_outpoints_values( super::storage::bridge_store_raw(ctx, key, Some(serialized)); } +/// rskj `processReleaseTransactionInfo`, which ends the settling of every +/// release the Bridge builds: a pegout's, a migration's, and the refund of a +/// peg-in it turns down (`settleReleaseRejection`). From RSKIP428 it logs +/// `pegout_transaction_created` with the value of each output the release +/// spends, in the order of its inputs, and from RSKIP305 it stores those +/// values under the release's hash. +fn process_release_transaction_info( + ctx: &mut CTX, + built: &super::release_tx::BuiltPegout, + hardfork_cfg: &RskHardforkConfig, + block_number: u64, +) { + if !hardfork_cfg.has_rskip428(block_number) { + return; + } + let outpoint_values: Vec = built + .used_utxos + .iter() + .map(|utxo| utxo.value_satoshis) + .collect(); + let txid = btc_txid_event_bytes(&built.tx); + super::events::log_pegout_transaction_created(ctx, &txid, &outpoint_values); + save_release_outpoints_values(ctx, &txid, &outpoint_values, hardfork_cfg, block_number); +} + /// rskj `BridgeStorageProvider.getReleaseOutpointsValues`: the per-input spent /// values stored when a release tx is created, keyed by its witness-stripped /// hash. Decodes the bitcoinj `VarInt` concatenation (`UtxoUtils`). @@ -2503,10 +2553,11 @@ fn resolve_flyover_input_redeems( /// empty-wallet refund of the flyover-federation outputs to `refund_hash160` /// using the FLYOVER redeem script(s), enqueue it, and log release_requested. /// -/// `flyover_redeems` is the list of candidate flyover redeem scripts (the active -/// fed's, plus the retiring fed's under RSKIP293). The refund spends every output -/// paying any of their flyover P2SH addresses; each input is resolved to its own -/// redeem (rskj `FlyoverCompatibleBtcWalletWithMultipleScripts`). +/// `flyover_feds` holds, for the active federation and then (under RSKIP293) the +/// retiring one, its flyover redeem script, the output script that pays it, +/// and the federation's format. The refund spends every output paying one of +/// them; each input is resolved to its own redeem (rskj +/// `FlyoverCompatibleBtcWalletWithMultipleScripts`). #[allow(clippy::too_many_arguments)] fn emit_flyover_rejection_release( ctx: &mut CTX, @@ -2514,22 +2565,21 @@ fn emit_flyover_rejection_release( refund_hash160: &[u8; 20], refund_is_p2sh: bool, total_satoshis: u64, - flyover_redeems: &[Vec], + flyover_feds: &[(Vec, bitcoin::ScriptBuf, u64)], rsk_height: u64, config: &BridgeConstants, hardfork_cfg: &RskHardforkConfig, tx_ctx: &BridgeTxContext, ) { - // Map each flyover P2SH output script → its redeem, then gather the spending - // UTXOs in output order with their resolved redeem. - let scripts: Vec<(bitcoin::ScriptBuf, &[u8])> = flyover_redeems - .iter() - .map(|r| (p2sh_output_script(&redeem_script_hash160(r)), r.as_slice())) - .collect(); + // Gather the spending UTXOs in output order, each with the redeem of the + // flyover address it pays. let mut utxos: Vec = Vec::new(); let mut input_redeems: Vec<&[u8]> = Vec::new(); for (index, o) in btc_tx.output.iter().enumerate() { - if let Some((_, redeem)) = scripts.iter().find(|(s, _)| *s == o.script_pubkey) { + if let Some((redeem, _, _)) = flyover_feds + .iter() + .find(|(_, script, _)| *script == o.script_pubkey) + { utxos.push(BridgeUtxo { tx_hash: btc_txid_event_bytes(btc_tx), vout: index as u32, @@ -2541,6 +2591,15 @@ fn emit_flyover_rejection_release( input_redeems.push(redeem); } } + // rskj `getFlyoverFederationFromTxOutputs`: the refund is built for the + // active federation where the payment pays its flyover address, and for + // the retiring one where it pays that one's alone, and in that federation's + // format: with the witness for a P2SH-P2WSH one + // (`ReleaseTransactionBuilder.setSrConfiguration`). + let is_segwit = flyover_feds + .iter() + .find(|(_, script, _)| btc_tx.output.iter().any(|o| o.script_pubkey == *script)) + .is_some_and(|(_, _, format)| *format >= 4000); let fee_per_kb = get_effective_fee_per_kb(ctx, config); let tx_version = if hardfork_cfg.has_rskip201(rsk_height) { 2 } else { 1 }; let refund_script = if refund_is_p2sh { @@ -2555,11 +2614,16 @@ fn emit_flyover_rejection_release( let built = if input_redeems.iter().all(|r| *r == input_redeems.first().copied().unwrap_or(&[])) { let single = input_redeems.first().copied().unwrap_or(&[]); super::release_tx::build_empty_wallet_to( - &utxos, &refund_script, single, fee_per_kb, tx_version, + &utxos, &refund_script, single, fee_per_kb, tx_version, is_segwit, ) } else { build_flyover_empty_wallet_multi( - &utxos, &input_redeems, &refund_script, fee_per_kb, tx_version, + &utxos, + &input_redeems, + &refund_script, + fee_per_kb, + tx_version, + is_segwit, ) }; if let Some(built) = built { @@ -2577,6 +2641,7 @@ fn emit_flyover_rejection_release( &btc_txid_event_bytes(&built.tx), total_satoshis, ); + process_release_transaction_info(ctx, &built, hardfork_cfg, rsk_height); store_pegout_confirmation_set(ctx, &waiting, use_tx_hash); } } @@ -2588,15 +2653,18 @@ fn emit_flyover_rejection_release( /// single flyover tx funds BOTH the active and retiring flyover feds. /// /// The fee sizing mirrors bitcoinj exactly: a single downward fee adjustment of -/// `feePerKb * size / 1000`, where `size` is the empty-scriptSig serialized -/// length plus, per input, `threshold * SIG_SIZE + redeemScript.len()`. The -/// final placeholder-filled tx must not exceed MAX_STANDARD_TX_SIZE. +/// `feePerKb * size / 1000`, no less than the reference minimum, where `size` +/// is the empty-scriptSig serialized length plus, per input, `threshold * +/// SIG_SIZE + redeemScript.len()`, segwit-weighted for a P2SH-P2WSH federation +/// (`is_segwit`), whose inputs carry their witnesses. The final +/// placeholder-filled tx must not exceed MAX_STANDARD_TX_SIZE. fn build_flyover_empty_wallet_multi( utxos: &[BridgeUtxo], input_redeems: &[&[u8]], refund_script: &bitcoin::ScriptBuf, fee_per_kb: u64, version: i32, + is_segwit: bool, ) -> Option { use bitcoin::{ absolute::LockTime, transaction::Version, Amount, OutPoint, Sequence, TxIn, TxOut, Witness, @@ -2649,15 +2717,21 @@ fn build_flyover_empty_wallet_multi( }], }; - let base_size = bitcoin::consensus::serialize(&tx).len(); + // As `release_tx::build_empty_wallet_to` sizes and fills its inputs, with + // each input's own redeem script. + let base_size = + bitcoin::consensus::serialize(&tx).len() + if is_segwit { utxos.len() * 36 } else { 0 }; let sig_estimate: usize = input_redeems .iter() .zip(&thresholds) .map(|(r, t)| t * SIG_SIZE + r.len()) .sum(); - let size = base_size + sig_estimate; - let fee_rate = fee_per_kb.max(REFERENCE_DEFAULT_MIN_TX_FEE); - let fee = fee_rate * size as u64 / 1000; + let size = if is_segwit { + (base_size + sig_estimate + 3 * base_size) / 4 + } else { + base_size + sig_estimate + }; + let fee = (fee_per_kb * size as u64 / 1000).max(REFERENCE_DEFAULT_MIN_TX_FEE); if fee >= gathered { return None; } @@ -2668,8 +2742,15 @@ fn build_flyover_empty_wallet_multi( tx.output[0].value = Amount::from_sat(value); for (input, (redeem, threshold)) in tx.input.iter_mut().zip(input_redeems.iter().zip(&thresholds)) { - input.script_sig = - bitcoin::ScriptBuf::from_bytes(super::release_tx::placeholder_scriptsig(redeem, *threshold)); + if is_segwit { + let (script_sig, witness) = super::release_tx::segwit_base_input(redeem, *threshold); + input.script_sig = script_sig; + input.witness = witness; + } else { + input.script_sig = bitcoin::ScriptBuf::from_bytes( + super::release_tx::placeholder_scriptsig(redeem, *threshold), + ); + } } if bitcoin::consensus::serialize(&tx).len() > MAX_STANDARD_TX_SIZE { return None; @@ -3097,23 +3178,7 @@ pub fn update_collections( // RSKIP428 (lovell700): processReleaseTransactionInfo emits // pegout_transaction_created right after release_requested, // listing each spent input's value (in input order). - if hardfork_cfg.has_rskip428(block_number) { - let outpoint_values: Vec = - built.used_utxos.iter().map(|u| u.value_satoshis).collect(); - let txid = btc_txid_event_bytes(&built.tx); - super::events::log_pegout_transaction_created( - ctx, - &txid, - &outpoint_values, - ); - save_release_outpoints_values( - ctx, - &txid, - &outpoint_values, - hardfork_cfg, - block_number, - ); - } + process_release_transaction_info(ctx, &built, hardfork_cfg, block_number); // rskj BridgeSupport.adjustBalancesIfChangeOutputWasDust // (unconditional since genesis): when a dusty change // output was raised to the non-dust minimum (paid by the @@ -4279,19 +4344,7 @@ fn process_funds_migration( } // RSKIP428 (lovell700): pegout_transaction_created for the migration // tx too (settleReleaseRequest -> processReleaseTransactionInfo). - if hardfork_cfg.has_rskip428(block_number) { - let outpoint_values: Vec = - built.used_utxos.iter().map(|u| u.value_satoshis).collect(); - let txid = btc_txid_event_bytes(&built.tx); - super::events::log_pegout_transaction_created(ctx, &txid, &outpoint_values); - save_release_outpoints_values( - ctx, - &txid, - &outpoint_values, - hardfork_cfg, - block_number, - ); - } + process_release_transaction_info(ctx, &built, hardfork_cfg, block_number); store_pegout_confirmation_set(ctx, &waiting, use_tx_hash); old_utxos.retain(|u| { @@ -8847,11 +8900,13 @@ mod tests { // Uniform case: both inputs use fly_a → must equal the single-redeem build. let utxos = [mk_utxo(1, 5_000_000), mk_utxo(2, 6_000_000)]; let redeems_uniform: Vec<&[u8]> = vec![fly_a.as_slice(), fly_a.as_slice()]; - let multi = build_flyover_empty_wallet_multi(&utxos, &redeems_uniform, &refund, 1000, 2) - .expect("multi build"); - let single = - crate::bridge::release_tx::build_empty_wallet_to(&utxos, &refund, &fly_a, 1000, 2) - .expect("single build"); + let multi = + build_flyover_empty_wallet_multi(&utxos, &redeems_uniform, &refund, 1000, 2, false) + .expect("multi build"); + let single = crate::bridge::release_tx::build_empty_wallet_to( + &utxos, &refund, &fly_a, 1000, 2, false, + ) + .expect("single build"); assert_eq!( bitcoin::consensus::serialize(&multi.tx), bitcoin::consensus::serialize(&single.tx), @@ -8863,8 +8918,9 @@ mod tests { // only if the redeems differ in length; here they are equal length but // distinct content). let redeems_mixed: Vec<&[u8]> = vec![fly_a.as_slice(), fly_b.as_slice()]; - let mixed = build_flyover_empty_wallet_multi(&utxos, &redeems_mixed, &refund, 1000, 2) - .expect("mixed build"); + let mixed = + build_flyover_empty_wallet_multi(&utxos, &redeems_mixed, &refund, 1000, 2, false) + .expect("mixed build"); assert_eq!(mixed.tx.input.len(), 2); assert_eq!(mixed.used_utxos.len(), 2); // Each input's scriptSig is the placeholder built from its own redeem. @@ -9688,9 +9744,10 @@ mod tests { let sig_hash = sig_hash_for_pegout_index(&tx).unwrap(); // Its change is registered as the federation's, and nothing is logged. assert_eq!(registered(&tx, 350, sig_hash, None), (true, true, 0, 1)); - // As a peg-in it's turned down: marked processed, two events, and + // As a peg-in it's turned down and refunded: marked processed, three + // events (the refusal, the refund's request and its creation), and // nothing kept for the federation. - let peg_in = (true, true, 2, 0); + let peg_in = (true, true, 3, 0); assert_eq!(registered(&tx, 350, None, None), peg_in); assert_eq!(registered(&tx, 349, sig_hash, None), peg_in); } @@ -9804,12 +9861,13 @@ mod tests { vec![signed_multisig(&erp)], std::slice::from_ref(&to_federation), ); - // With no federation retired, both are peg-ins. + // With no federation retired, both are peg-ins, and the one that pays + // the federation is refunded, which leaves a third event. assert_eq!( registered(&paying_nothing, 349, None, None), (true, false, 2, 0) ); - assert_eq!(registered(&migrating, 349, None, None), (true, true, 2, 0)); + assert_eq!(registered(&migrating, 349, None, None), (true, true, 3, 0)); let whole = redeem_script_hash160(&erp); assert_eq!(with_retired(&whole, &paying_nothing), (true, false, 0, 0)); assert_eq!(with_retired(&whole, &migrating), (true, false, 0, 0)); diff --git a/crates/execution/src/bridge/release_tx.rs b/crates/execution/src/bridge/release_tx.rs index 15f0a9b..57c0982 100644 --- a/crates/execution/src/bridge/release_tx.rs +++ b/crates/execution/src/bridge/release_tx.rs @@ -476,12 +476,19 @@ where /// rejection refunds: spend ALL the given UTXOs (in provider order — the /// MAX_MONEY selection skips sorting) into a single output to `refund_script` /// worth the gathered total minus the size-based fee. +/// +/// `is_segwit` is `SendRequest.isSegwitCompatible`, which +/// `ReleaseTransactionBuilder.setSrConfiguration` sets for a P2SH-P2WSH +/// federation whatever it builds: the fee is then taken over the virtual size, +/// and each input carries the witness program in its scriptSig and the +/// unsigned witness (`signSegwitTxInputs`), as a pegout's does. pub fn build_empty_wallet_to( utxos: &[BridgeUtxo], refund_script: &ScriptBuf, redeem_script: &[u8], fee_per_kb: u64, version: i32, + is_segwit: bool, ) -> Option { if utxos.is_empty() { return None; @@ -515,12 +522,21 @@ pub fn build_empty_wallet_to( }], }; - // adjustOutputDownwardsForFee: single pass, fee from the empty-scriptSig - // size plus the signing estimate; the output must stay above dust. - let base_size = bitcoin::consensus::serialize(&tx).len(); - let size = base_size + utxos.len() * (threshold * SIG_SIZE + redeem_script.len()); - let fee_rate = fee_per_kb.max(REFERENCE_DEFAULT_MIN_TX_FEE); - let fee = fee_rate * size as u64 / 1000; + // adjustOutputDownwardsForFee: a single pass, over the size + // `calculateTxSize` gives the transaction while its scriptSigs are empty, + // segwit-weighted exactly as in `complete_pegout_tx`. The fee is no less + // than the reference minimum (`ensureMinRequiredFee`): a floor under the + // fee itself here, where `calculateFee` puts one under the rate. The + // output must stay above dust. + let base_size = + bitcoin::consensus::serialize(&tx).len() + if is_segwit { utxos.len() * 36 } else { 0 }; + let signing = utxos.len() * (threshold * SIG_SIZE + redeem_script.len()); + let size = if is_segwit { + (base_size + signing + 3 * base_size) / 4 + } else { + base_size + signing + }; + let fee = (fee_per_kb * size as u64 / 1000).max(REFERENCE_DEFAULT_MIN_TX_FEE); if fee >= gathered { return None; } @@ -530,9 +546,15 @@ pub fn build_empty_wallet_to( } tx.output[0].value = Amount::from_sat(value); - let scriptsig = placeholder_scriptsig(redeem_script, threshold); for input in &mut tx.input { - input.script_sig = ScriptBuf::from_bytes(scriptsig.clone()); + if is_segwit { + let (script_sig, witness) = segwit_base_input(redeem_script, threshold); + input.script_sig = script_sig; + input.witness = witness; + } else { + input.script_sig = + ScriptBuf::from_bytes(placeholder_scriptsig(redeem_script, threshold)); + } } if bitcoin::consensus::serialize(&tx).len() > MAX_STANDARD_TX_SIZE { return None; @@ -1584,6 +1606,7 @@ mod tests { &redeem, 500_000, 1, + false, ) .expect("rejection release builds"); let mut rid = *built.tx.compute_txid().to_raw_hash().as_byte_array(); @@ -1665,6 +1688,7 @@ mod tests { &redeem, 500_000, // mainnet genesis feePerKb 1, // pre-RSKIP201 + false, ) .expect("rejection release builds"); diff --git a/crates/execution/src/bridge/testdata/rskj_refunded_pegins.txt b/crates/execution/src/bridge/testdata/rskj_refunded_pegins.txt new file mode 100644 index 0000000..239659f --- /dev/null +++ b/crates/execution/src/bridge/testdata/rskj_refunded_pegins.txt @@ -0,0 +1,333 @@ +# What RSKj's release VETIVER-9.0.4 does with a peg-in it refunds, registered by a transaction and +# by contracts. Each scenario runs two transactions through TransactionExecutor +# (rskj-core-9.0.4-VETIVER-all.jar, regtest, block 10) over the accounts it lists, as +# BlockExecutor runs the sequential sublist of a block, which pays REMASC the fees after the +# last transaction when they come to more than nothing: receiveHeaders from a member of the +# federation, with six Bitcoin headers, then registerBtcTransaction of a payment to the +# federation from a two-of-three multisig, which the second header holds. The +# Bridge starts with an empty list of its federation's UTXOs, and in the scenario low-fee with +# a fee per kilobyte of 10,000 satoshis, under which the refund's fee is bitcoinj's minimum. +# The last scenario registers instead, through the contract that stands for the liquidity +# bridge, a flyover payment over the locking cap, which the Bridge refunds to its user. +# Lines: +# block number, gas limit, timestamp, coinbase, difficulty +# account address, nonce, balance, code +# store a storage cell before the transactions: address, key, value +# preroot the state root before the transactions +# tx the transaction as RSKj encodes it, its sender, its hash +# result whether it was executed, whether it succeeded, the gas it used +# log a log it left: address, topics, data +# itx an internal transaction of its result: deep, index, note, sender, receiver, value, +# nonce bytes, gas price, gas limit bytes, data, parent hash, whether it ended +# rejected, hash +# cell the Bridge's storage after both: key, value +# root the state root after both +# enc an internal transaction built by itself: nonce, gas price, gas limit, sender, +# receiver, value, data, note, deep, index, parent hash, hash +# A hyphen stands for nothing. Values, balances, gas prices and limits are hex, as every byte +# string is; the block line's numbers, the nonces of account and enc lines, the gas used, deep +# and index are decimal. +block 10 67c280 1700000000 0101010101010101010101010101010101010101 1 +# federation address 2MvQSG61B4LtKLECeo1dRDeDWXgJfnr3fC7 +# peg-in 747a94a37115c4e28a94af355ee9fc48b1e3e0d671a7b9340b16d6233698d81f 0100000001222222222222222222222222222222222222222222222222222222222222222200000000fdfd0000473030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030483131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131314c69522102141481bf1181ed61aa025f1fe708f68cb018c2c9d6eb719ccd94b3f6ff615308210222ca50390e660f601036dd7502bb973bdcd104b5dcb8f2e74de8edc6c292b03a2103cfc18f02cc004640f2116fdd1f6ca2022e39be25df75e27c80bde842e6b6f93853aeffffffff0100ca9a3b0000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78700000000 +scenario direct +account 85845342b0d5260d516d6da5280a7df9a96d1925 0 de0b6b3a7640000 - +account d06da76e0b65671fc4639b624338464ed7b52bbd 0 de0b6b3a7640000 - +account 0000000000000000000000000000000001000006 0 115eec47f6cf7e35000000 - +store 0000000000000000000000000000000001000006 00000000000000000000006e657746656465726174696f6e4274635554584f73 c0 +preroot e33796914989af2dca2ab61b5cb87a58b478d6c0b0a115c00c00d7d547c140d8 +tx f904668000831e848094000000000000000000000000000000000100000680b90404e5400e7b0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000001c0000000000000000000000000000000000000000000000000000000000000024000000000000000000000000000000000000000000000000000000000000002c0000000000000000000000000000000000000000000000000000000000000034000000000000000000000000000000000000000000000000000000000000000500100000006226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f20000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000500100000060cb91643b56cab7f897026f3d42ee576b4b8733ff7c545be174dc30fdca064f1fd8983623d6160b34b9a771d6e0e3b148fce95e35af948ae2c41571a3947a7400f15365ffff7f2001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000050010000003a5cb9cfe36bcf69340bc47ac507f5ac9b7194a7ccb9fa32be893f7cf5524546111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005001000000bf3347e74131c963a32f4b6181a6b583971b180cbbae27a5f7220194636b4b4e111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f20000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000500100000070e9bcc26a313dc2efad66eecc2040a51e205bcfaf481e9e6a0c51587252d020111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f200200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005001000000f6baaea21335a06f28fe2e874ac69601ec4388be7c2f8b34a397bd233faefd64111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f20000000000000000000000000000000000000000066a0312bdf00d8019e71deaa3d4e428edb58e9b017f65acd2f7004cf0b7b84856df4a047fc3dac687371d2be5a716d7f220f5a2ce98f7f8b2724e6976fc38d036de023 85845342b0d5260d516d6da5280a7df9a96d1925 80d341ac624a7f9588ad9e045c21b5b2ee1129cebfdafe8077618f1d654d649f +result 1 1 0 +tx f902a68000832dc6c094000000000000000000000000000000000100000680b9024443dc06560000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000001e000000000000000000000000000000000000000000000000000000000000001520100000001222222222222222222222222222222222222222222222222222222222222222200000000fdfd0000473030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030483131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131314c69522102141481bf1181ed61aa025f1fe708f68cb018c2c9d6eb719ccd94b3f6ff615308210222ca50390e660f601036dd7502bb973bdcd104b5dcb8f2e74de8edc6c292b03a2103cfc18f02cc004640f2116fdd1f6ca2022e39be25df75e27c80bde842e6b6f93853aeffffffff0100ca9a3b0000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c787000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002701000000011fd8983623d6160b34b9a771d6e0e3b148fce95e35af948ae2c41571a3947a7401010000000000000000000000000000000000000000000000000065a041e132f2c25bf123029bdea87c64c3a6a2424c9a4cd7766d4f7a6428c0788792a01271052c0ad805a62b7bdfb114d61595f7fcd45edcaeecdcefedb947e7fb31f6 d06da76e0b65671fc4639b624338464ed7b52bbd 6e0ed1e4894c050d32505ffa8c749199cd0ba0307f67cd447197e78dad6e4759 +result 1 1 50872 +log 0000000000000000000000000000000001000006 708ce1ead20561c5894a93be3fee64b326b2ad6c198f8253e4bb56f1626053d6,747a94a37115c4e28a94af355ee9fc48b1e3e0d671a7b9340b16d6233698d81f 0000000000000000000000000000000000000000000000000000000000000002 +log 0000000000000000000000000000000001000006 7a7c29481528ac8c2b2e93aee658fddd4dc15304fa723a5c2b88514557bcc790,6e0ed1e4894c050d32505ffa8c749199cd0ba0307f67cd447197e78dad6e4759,86bc174bbe4d5209f8cdc70621c2ef604dcca60f61e6ac84a43f7feab7d5d867 000000000000000000000000000000000000000000000000000000003b9aca00 +log 0000000000000000000000000000000001000006 9ee5d520fd5e6eaea3fd2e3ae4e35e9a9c0fb05c9d8f84b507f287da84b5117c,86bc174bbe4d5209f8cdc70621c2ef604dcca60f61e6ac84a43f7feab7d5d867 00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000005fe00ca9a3b000000000000000000000000000000000000000000000000000000 +cell 0000000000000000000000000000000000000062746354784861736865734150 c0 +cell 00000000000000000000000000626c6f636b53746f7265436861696e48656164 000000000000000000000000000000000000000000000000000000000000000e0000000601000000f6baaea21335a06f28fe2e874ac69601ec4388be7c2f8b34a397bd233faefd64111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f2000000000 +cell 00000000000000000000006e657746656465726174696f6e4274635554584f73 c0 +cell 000000000000000000000072656c656173655472616e73616374696f6e536574 c0 +cell 0072656c656173655472616e73616374696f6e53657457697468547848617368 f9019fb9017a020000000001011fd8983623d6160b34b9a771d6e0e3b148fce95e35af948ae2c41571a3947a7400000000232200207c948c1c2c56b847e905c51f36834413e639d1fce1ef7c95cd1f47396c2173ecffffffff0174749a3b0000000017a914c71aee7e058061a1f61317e509c976affed48258870500000000fc64522102cd53fc53a07f211641a677d250f6de99caf620e8e77071e811a28b3bcddf0be1210362634ab57dae9cb373a5d536e66a8c4f67468bbcfb063809bab643072d78a1242103c5946b3fbae03a654237da863c9ed534e0878657175b132b8ca630f245df04db53ae6702f401b2755321029cecea902067992d52c38b28bf0bb2345bda9b21eca76b16a17c477a64e433012103284178e5fbcc63c54c3b38e3ef88adf2da6c526313650041b0ef955763634ebd2103ab0e2cd7ed158687fc13b88019990860cdb72b1f5777b58513312550ea1584bc2103b9fc46657cf72a1afa007ecf431de1cd27ff5cc8829fa625b66ca47b967e6b2454ae68000000000aa06e0ed1e4894c050d32505ffa8c749199cd0ba0307f67cd447197e78dad6e4759 +cell 04e7879f9f786d37bfb9da99ca9ea71f66a545ddf08f8026e18a14c4203f80cd a00f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206 +cell 0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206 0000000000000000000000000000000000000000000000000000000000000002000000000100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4adae5494dffff7f2002000000 +cell 144d40b74626eb8523107d6b6487461aa1d38550ef0c7a154c9d377bc99bb3c7 0a +cell 20d0527258510c6a9e1e48afcf5b201ea54020ccee66adefc23d316ac2bce970 000000000000000000000000000000000000000000000000000000000000000a0000000401000000bf3347e74131c963a32f4b6181a6b583971b180cbbae27a5f7220194636b4b4e111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f2000000000 +cell 3481400172b4addeaaf0248876e6e1769172e8bce17a039e7d9aa3cb25b31bcf 000000000000000000000000000000000000000000000000000000000000000e0000000601000000f6baaea21335a06f28fe2e874ac69601ec4388be7c2f8b34a397bd233faefd64111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f2000000000 +cell 34fa9348faf7dca06d41a200c85ef02b02ab03916a70c8a65b6e3fd84dbe0bde a04e4b6b63940122f7a527aebb0c181b9783b5a681614b2fa363c93141e74733bf +cell 3fee5096e9507962151178d120a30ccdaa2cecc95a10a749a0b540797b21e45b a064fdae3f23bd97a3348b2f7cbe8843ec0196c64a872efe286fa03513a2aebaf6 +cell 464552f57c3f89be32fab9cca794719bacf507c57ac40b3469cf6be3cfb95c3a 0000000000000000000000000000000000000000000000000000000000000006000000020100000060cb91643b56cab7f897026f3d42ee576b4b8733ff7c545be174dc30fdca064f1fd8983623d6160b34b9a771d6e0e3b148fce95e35af948ae2c41571a3947a7400f15365ffff7f2001000000 +cell 4e4b6b63940122f7a527aebb0c181b9783b5a681614b2fa363c93141e74733bf 000000000000000000000000000000000000000000000000000000000000000800000003010000003a5cb9cfe36bcf69340bc47ac507f5ac9b7194a7ccb9fa32be893f7cf5524546111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f2000000000 +cell 4f06cafd30dc74e15b547cff33874b6b57ee423d6f0297f8b7ca563b6491cb60 0000000000000000000000000000000000000000000000000000000000000004000000010100000006226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f2000000000 +cell 62979aa6ab22a67b1abee5022830e036bd87b8881abdaa2c57ee24216e26d93c a0464552f57c3f89be32fab9cca794719bacf507c57ac40b3469cf6be3cfb95c3a +cell 64fdae3f23bd97a3348b2f7cbe8843ec0196c64a872efe286fa03513a2aebaf6 000000000000000000000000000000000000000000000000000000000000000c000000050100000070e9bcc26a313dc2efad66eecc2040a51e205bcfaf481e9e6a0c51587252d020111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f2002000000 +cell 929e75683fff26031f5007c26025afaceeab29dc33e5cf5bb043d646ef67b816 fe00ca9a3b +cell a0b1dbff8c310a5e983e6e603a69b3ee024fe8b229dfca1e0b7f0d41c4768592 a020d0527258510c6a9e1e48afcf5b201ea54020ccee66adefc23d316ac2bce970 +cell cf606e0bbdd62c72533bdc45ae2e61a2b5fa7f5251ec5a0d7e99c6022191a680 a03481400172b4addeaaf0248876e6e1769172e8bce17a039e7d9aa3cb25b31bcf +cell fa504d03ff80daf373e6ce2920ce39ca86dae5077fa09b447cfcb6f0df5835b9 a04f06cafd30dc74e15b547cff33874b6b57ee423d6f0297f8b7ca563b6491cb60 +root f44c453082ba7b5289194e702fbf1c14467e0a6824f1953f18df16405a7c4b84 +end +scenario relay +account 85845342b0d5260d516d6da5280a7df9a96d1925 0 de0b6b3a7640000 - +account d06da76e0b65671fc4639b624338464ed7b52bbd 0 de0b6b3a7640000 - +account 0000000000000000000000000000000001000006 0 115eec47f6cf7e35000000 - +account a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 1 0 3660006000376000600036600060007300000000000000000000000000000000010000065af160010160005500 +store 0000000000000000000000000000000001000006 00000000000000000000006e657746656465726174696f6e4274635554584f73 c0 +preroot 5735e868e625c6441e7d6c1d097342b27c1c2872470a4b1eae2e9b2ed12a7d6e +tx f904668000831e848094000000000000000000000000000000000100000680b90404e5400e7b0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000001c0000000000000000000000000000000000000000000000000000000000000024000000000000000000000000000000000000000000000000000000000000002c0000000000000000000000000000000000000000000000000000000000000034000000000000000000000000000000000000000000000000000000000000000500100000006226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f20000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000500100000060cb91643b56cab7f897026f3d42ee576b4b8733ff7c545be174dc30fdca064f1fd8983623d6160b34b9a771d6e0e3b148fce95e35af948ae2c41571a3947a7400f15365ffff7f2001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000050010000003a5cb9cfe36bcf69340bc47ac507f5ac9b7194a7ccb9fa32be893f7cf5524546111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005001000000bf3347e74131c963a32f4b6181a6b583971b180cbbae27a5f7220194636b4b4e111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f20000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000500100000070e9bcc26a313dc2efad66eecc2040a51e205bcfaf481e9e6a0c51587252d020111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f200200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005001000000f6baaea21335a06f28fe2e874ac69601ec4388be7c2f8b34a397bd233faefd64111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f20000000000000000000000000000000000000000066a0312bdf00d8019e71deaa3d4e428edb58e9b017f65acd2f7004cf0b7b84856df4a047fc3dac687371d2be5a716d7f220f5a2ce98f7f8b2724e6976fc38d036de023 85845342b0d5260d516d6da5280a7df9a96d1925 80d341ac624a7f9588ad9e045c21b5b2ee1129cebfdafe8077618f1d654d649f +result 1 1 0 +tx f902a7808180832dc6c094a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a180b9024443dc06560000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000001e000000000000000000000000000000000000000000000000000000000000001520100000001222222222222222222222222222222222222222222222222222222222222222200000000fdfd0000473030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030483131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131314c69522102141481bf1181ed61aa025f1fe708f68cb018c2c9d6eb719ccd94b3f6ff615308210222ca50390e660f601036dd7502bb973bdcd104b5dcb8f2e74de8edc6c292b03a2103cfc18f02cc004640f2116fdd1f6ca2022e39be25df75e27c80bde842e6b6f93853aeffffffff0100ca9a3b0000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c787000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002701000000011fd8983623d6160b34b9a771d6e0e3b148fce95e35af948ae2c41571a3947a7401010000000000000000000000000000000000000000000000000065a09518a9a64c4b3f24e3ded62ecb9591350fa76a6c76caa4dce1f37585694d5ae9a0091f75006f577b3d8634455278a5ea3bedd1448baf3fb099b2bab2284e3374da d06da76e0b65671fc4639b624338464ed7b52bbd a5f3af07ca8c839a9be5b89fc568a3421f2955eefa5cbf52f54d5c86cccb7337 +result 1 1 71725 +log 0000000000000000000000000000000001000006 708ce1ead20561c5894a93be3fee64b326b2ad6c198f8253e4bb56f1626053d6,747a94a37115c4e28a94af355ee9fc48b1e3e0d671a7b9340b16d6233698d81f 0000000000000000000000000000000000000000000000000000000000000002 +log 0000000000000000000000000000000001000006 7a7c29481528ac8c2b2e93aee658fddd4dc15304fa723a5c2b88514557bcc790,ab35630e78409a21c43cd7292ebf023e909fc2d6dcd22cc0a1d72bb4fe5d07b7,86bc174bbe4d5209f8cdc70621c2ef604dcca60f61e6ac84a43f7feab7d5d867 000000000000000000000000000000000000000000000000000000003b9aca00 +log 0000000000000000000000000000000001000006 9ee5d520fd5e6eaea3fd2e3ae4e35e9a9c0fb05c9d8f84b507f287da84b5117c,86bc174bbe4d5209f8cdc70621c2ef604dcca60f61e6ac84a43f7feab7d5d867 00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000005fe00ca9a3b000000000000000000000000000000000000000000000000000000 +itx 0 0 call a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 0 01 80 000000000000000000000000000000000000000000000000000000000067c280 - a5f3af07ca8c839a9be5b89fc568a3421f2955eefa5cbf52f54d5c86cccb7337 0 ab35630e78409a21c43cd7292ebf023e909fc2d6dcd22cc0a1d72bb4fe5d07b7 +cell 0000000000000000000000000000000000000062746354784861736865734150 c0 +cell 00000000000000000000000000626c6f636b53746f7265436861696e48656164 000000000000000000000000000000000000000000000000000000000000000e0000000601000000f6baaea21335a06f28fe2e874ac69601ec4388be7c2f8b34a397bd233faefd64111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f2000000000 +cell 00000000000000000000006e657746656465726174696f6e4274635554584f73 c0 +cell 000000000000000000000072656c656173655472616e73616374696f6e536574 c0 +cell 0072656c656173655472616e73616374696f6e53657457697468547848617368 f9019fb9017a020000000001011fd8983623d6160b34b9a771d6e0e3b148fce95e35af948ae2c41571a3947a7400000000232200207c948c1c2c56b847e905c51f36834413e639d1fce1ef7c95cd1f47396c2173ecffffffff0174749a3b0000000017a914c71aee7e058061a1f61317e509c976affed48258870500000000fc64522102cd53fc53a07f211641a677d250f6de99caf620e8e77071e811a28b3bcddf0be1210362634ab57dae9cb373a5d536e66a8c4f67468bbcfb063809bab643072d78a1242103c5946b3fbae03a654237da863c9ed534e0878657175b132b8ca630f245df04db53ae6702f401b2755321029cecea902067992d52c38b28bf0bb2345bda9b21eca76b16a17c477a64e433012103284178e5fbcc63c54c3b38e3ef88adf2da6c526313650041b0ef955763634ebd2103ab0e2cd7ed158687fc13b88019990860cdb72b1f5777b58513312550ea1584bc2103b9fc46657cf72a1afa007ecf431de1cd27ff5cc8829fa625b66ca47b967e6b2454ae68000000000aa0ab35630e78409a21c43cd7292ebf023e909fc2d6dcd22cc0a1d72bb4fe5d07b7 +cell 04e7879f9f786d37bfb9da99ca9ea71f66a545ddf08f8026e18a14c4203f80cd a00f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206 +cell 0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206 0000000000000000000000000000000000000000000000000000000000000002000000000100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4adae5494dffff7f2002000000 +cell 144d40b74626eb8523107d6b6487461aa1d38550ef0c7a154c9d377bc99bb3c7 0a +cell 20d0527258510c6a9e1e48afcf5b201ea54020ccee66adefc23d316ac2bce970 000000000000000000000000000000000000000000000000000000000000000a0000000401000000bf3347e74131c963a32f4b6181a6b583971b180cbbae27a5f7220194636b4b4e111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f2000000000 +cell 3481400172b4addeaaf0248876e6e1769172e8bce17a039e7d9aa3cb25b31bcf 000000000000000000000000000000000000000000000000000000000000000e0000000601000000f6baaea21335a06f28fe2e874ac69601ec4388be7c2f8b34a397bd233faefd64111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f2000000000 +cell 34fa9348faf7dca06d41a200c85ef02b02ab03916a70c8a65b6e3fd84dbe0bde a04e4b6b63940122f7a527aebb0c181b9783b5a681614b2fa363c93141e74733bf +cell 3fee5096e9507962151178d120a30ccdaa2cecc95a10a749a0b540797b21e45b a064fdae3f23bd97a3348b2f7cbe8843ec0196c64a872efe286fa03513a2aebaf6 +cell 464552f57c3f89be32fab9cca794719bacf507c57ac40b3469cf6be3cfb95c3a 0000000000000000000000000000000000000000000000000000000000000006000000020100000060cb91643b56cab7f897026f3d42ee576b4b8733ff7c545be174dc30fdca064f1fd8983623d6160b34b9a771d6e0e3b148fce95e35af948ae2c41571a3947a7400f15365ffff7f2001000000 +cell 4e4b6b63940122f7a527aebb0c181b9783b5a681614b2fa363c93141e74733bf 000000000000000000000000000000000000000000000000000000000000000800000003010000003a5cb9cfe36bcf69340bc47ac507f5ac9b7194a7ccb9fa32be893f7cf5524546111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f2000000000 +cell 4f06cafd30dc74e15b547cff33874b6b57ee423d6f0297f8b7ca563b6491cb60 0000000000000000000000000000000000000000000000000000000000000004000000010100000006226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f2000000000 +cell 62979aa6ab22a67b1abee5022830e036bd87b8881abdaa2c57ee24216e26d93c a0464552f57c3f89be32fab9cca794719bacf507c57ac40b3469cf6be3cfb95c3a +cell 64fdae3f23bd97a3348b2f7cbe8843ec0196c64a872efe286fa03513a2aebaf6 000000000000000000000000000000000000000000000000000000000000000c000000050100000070e9bcc26a313dc2efad66eecc2040a51e205bcfaf481e9e6a0c51587252d020111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f2002000000 +cell 929e75683fff26031f5007c26025afaceeab29dc33e5cf5bb043d646ef67b816 fe00ca9a3b +cell a0b1dbff8c310a5e983e6e603a69b3ee024fe8b229dfca1e0b7f0d41c4768592 a020d0527258510c6a9e1e48afcf5b201ea54020ccee66adefc23d316ac2bce970 +cell cf606e0bbdd62c72533bdc45ae2e61a2b5fa7f5251ec5a0d7e99c6022191a680 a03481400172b4addeaaf0248876e6e1769172e8bce17a039e7d9aa3cb25b31bcf +cell fa504d03ff80daf373e6ce2920ce39ca86dae5077fa09b447cfcb6f0df5835b9 a04f06cafd30dc74e15b547cff33874b6b57ee423d6f0297f8b7ca563b6491cb60 +root 323a3e31616eea320cdc94501302f40c579d41e1bbd1baf743ea9f290ade1827 +end +scenario nested +account 85845342b0d5260d516d6da5280a7df9a96d1925 0 de0b6b3a7640000 - +account d06da76e0b65671fc4639b624338464ed7b52bbd 0 de0b6b3a7640000 - +account 0000000000000000000000000000000001000006 0 115eec47f6cf7e35000000 - +account a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2 127 3e8 6000600060006000600073e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e161c350f1506f6000600060006000600060995af15000600052601060106000f0506000600060006000600461c350fa506000600060006000600173b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1620186a0f1506000600060006000600073b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2620186a0f15060006000600060007fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff73e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e161c350f1506000600060006000600073000000000000000000000000000000000100001061c350f1506000600060006000600073000000000000000000000000000000000100000861c350f1506000600060006000600073e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e161c350f2503660006000376000600036600073c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c15af45000 +account b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1 1 0 73e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2ff +account b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2 1 0 6000600060006000600073e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1617530f15060006000fd +account c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1 1 0 3660006000376000600036600060007300000000000000000000000000000000010000065af160010160005500 +store 0000000000000000000000000000000001000006 00000000000000000000006e657746656465726174696f6e4274635554584f73 c0 +preroot 5a86ddd148912bd5fd612df27f5ca84b15e63ab9d007297b571bba017d84cbf2 +tx f904668000831e848094000000000000000000000000000000000100000680b90404e5400e7b0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000001c0000000000000000000000000000000000000000000000000000000000000024000000000000000000000000000000000000000000000000000000000000002c0000000000000000000000000000000000000000000000000000000000000034000000000000000000000000000000000000000000000000000000000000000500100000006226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f20000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000500100000060cb91643b56cab7f897026f3d42ee576b4b8733ff7c545be174dc30fdca064f1fd8983623d6160b34b9a771d6e0e3b148fce95e35af948ae2c41571a3947a7400f15365ffff7f2001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000050010000003a5cb9cfe36bcf69340bc47ac507f5ac9b7194a7ccb9fa32be893f7cf5524546111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005001000000bf3347e74131c963a32f4b6181a6b583971b180cbbae27a5f7220194636b4b4e111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f20000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000500100000070e9bcc26a313dc2efad66eecc2040a51e205bcfaf481e9e6a0c51587252d020111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f200200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005001000000f6baaea21335a06f28fe2e874ac69601ec4388be7c2f8b34a397bd233faefd64111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f20000000000000000000000000000000000000000066a0312bdf00d8019e71deaa3d4e428edb58e9b017f65acd2f7004cf0b7b84856df4a047fc3dac687371d2be5a716d7f220f5a2ce98f7f8b2724e6976fc38d036de023 85845342b0d5260d516d6da5280a7df9a96d1925 80d341ac624a7f9588ad9e045c21b5b2ee1129cebfdafe8077618f1d654d649f +result 1 1 0 +tx f902a68000832dc6c094a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a280b9024443dc06560000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000001e000000000000000000000000000000000000000000000000000000000000001520100000001222222222222222222222222222222222222222222222222222222222222222200000000fdfd0000473030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030483131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131314c69522102141481bf1181ed61aa025f1fe708f68cb018c2c9d6eb719ccd94b3f6ff615308210222ca50390e660f601036dd7502bb973bdcd104b5dcb8f2e74de8edc6c292b03a2103cfc18f02cc004640f2116fdd1f6ca2022e39be25df75e27c80bde842e6b6f93853aeffffffff0100ca9a3b0000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c787000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002701000000011fd8983623d6160b34b9a771d6e0e3b148fce95e35af948ae2c41571a3947a7401010000000000000000000000000000000000000000000000000065a0d1478af34b0b46d0f1eb4b3d7952caecd7512b3216216225f04c2d6fdd9795afa067450389b2be73f491d87c49d3ebd45939493b61c16283e4cfccc43a37c5dd40 d06da76e0b65671fc4639b624338464ed7b52bbd 6015785ec1f9052c4d752e926ca1399a7809b64849c472c7ce7214a987b023b6 +result 1 1 235840 +log 0000000000000000000000000000000001000006 708ce1ead20561c5894a93be3fee64b326b2ad6c198f8253e4bb56f1626053d6,747a94a37115c4e28a94af355ee9fc48b1e3e0d671a7b9340b16d6233698d81f 0000000000000000000000000000000000000000000000000000000000000002 +log 0000000000000000000000000000000001000006 7a7c29481528ac8c2b2e93aee658fddd4dc15304fa723a5c2b88514557bcc790,615e8dd687caca67c9fb6d062d59f54d4e1a0132aa5d929476988d976d34360e,86bc174bbe4d5209f8cdc70621c2ef604dcca60f61e6ac84a43f7feab7d5d867 000000000000000000000000000000000000000000000000000000003b9aca00 +log 0000000000000000000000000000000001000006 9ee5d520fd5e6eaea3fd2e3ae4e35e9a9c0fb05c9d8f84b507f287da84b5117c,86bc174bbe4d5209f8cdc70621c2ef604dcca60f61e6ac84a43f7feab7d5d867 00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000005fe00ca9a3b000000000000000000000000000000000000000000000000000000 +itx 0 0 call a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2 e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1 0 7f 0 000000000000000000000000000000000000000000000000000000000067c280 - 6015785ec1f9052c4d752e926ca1399a7809b64849c472c7ce7214a987b023b6 0 964459708caf9129d11bc108541697ba42180608df87c7fac6dec93dd00279b3 +itx 0 1 create a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2 - 0 7f 0 000000000000000000000000000000000000000000000000000000000067c280 6000600060006000600060995af15000 6015785ec1f9052c4d752e926ca1399a7809b64849c472c7ce7214a987b023b6 0 e73ca05007112a657a1757dc206323b66f72cce5be022c8357ef71d39cce4d9f +itx 0 2 call a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2 b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1 1 0080 0 000000000000000000000000000000000000000000000000000000000067c280 73e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2ff 6015785ec1f9052c4d752e926ca1399a7809b64849c472c7ce7214a987b023b6 0 dc88d753edd86b14bdd7c1e7515602d2a1a92bd0ed6a4f11675a2401bc29eb08 +itx 1 0 suicide b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1 e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2 1 01 0 - - dc88d753edd86b14bdd7c1e7515602d2a1a92bd0ed6a4f11675a2401bc29eb08 0 5640c9bef7500d28c207fafff13fe9540e61fe386fb4ed44d507eca1ba998e1b +itx 0 4 call a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2 b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2 0 0080 0 000000000000000000000000000000000000000000000000000000000067c280 6000600060006000600073e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1617530f15060006000fd 6015785ec1f9052c4d752e926ca1399a7809b64849c472c7ce7214a987b023b6 1 cb7dd712f9360d4c5b17e904dd231992e1034e6edd8419400c0c8155b1516fbe +itx 1 0 call b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2 e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1e1 0 01 0 000000000000000000000000000000000000000000000000000000000067c280 - cb7dd712f9360d4c5b17e904dd231992e1034e6edd8419400c0c8155b1516fbe 1 c5ab77a2cef0281ebe0772494f4f6dcb876f32830d22ac513ea7ca6dfe1ac32e +itx 0 6 call a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2 0000000000000000000000000000000001000010 0 0080 0 000000000000000000000000000000000000000000000000000000000067c280 - 6015785ec1f9052c4d752e926ca1399a7809b64849c472c7ce7214a987b023b6 0 649a2968ad1d4146cff73a1bac06a007b12bc6dc626d3e41b8b8ef1f530342c5 +itx 0 7 call a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2 0000000000000000000000000000000001000008 0 0080 0 000000000000000000000000000000000000000000000000000000000067c280 - 6015785ec1f9052c4d752e926ca1399a7809b64849c472c7ce7214a987b023b6 0 9b7e81d86b453d249db25a8358c9f8933ed452a9e369e4ec93259eb9bc3f2102 +itx 0 8 call a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2 a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2 0 0080 0 000000000000000000000000000000000000000000000000000000000067c280 - 6015785ec1f9052c4d752e926ca1399a7809b64849c472c7ce7214a987b023b6 0 07a12f1c69eea51e17e3b3db3f692db20f17c7a49591938ff991c514cf2cf743 +itx 0 9 call a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2 a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2 0 0080 0 000000000000000000000000000000000000000000000000000000000067c280 3660006000376000600036600060007300000000000000000000000000000000010000065af160010160005500 6015785ec1f9052c4d752e926ca1399a7809b64849c472c7ce7214a987b023b6 0 893173ac6a35ca2c30a0c477683d26b4658bf3521060c9ad011c56d10e3766ed +itx 1 0 call a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2 0000000000000000000000000000000001000006 0 0080 0 000000000000000000000000000000000000000000000000000000000067c280 - 893173ac6a35ca2c30a0c477683d26b4658bf3521060c9ad011c56d10e3766ed 0 615e8dd687caca67c9fb6d062d59f54d4e1a0132aa5d929476988d976d34360e +cell 0000000000000000000000000000000000000062746354784861736865734150 c0 +cell 00000000000000000000000000626c6f636b53746f7265436861696e48656164 000000000000000000000000000000000000000000000000000000000000000e0000000601000000f6baaea21335a06f28fe2e874ac69601ec4388be7c2f8b34a397bd233faefd64111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f2000000000 +cell 00000000000000000000006e657746656465726174696f6e4274635554584f73 c0 +cell 000000000000000000000072656c656173655472616e73616374696f6e536574 c0 +cell 0072656c656173655472616e73616374696f6e53657457697468547848617368 f9019fb9017a020000000001011fd8983623d6160b34b9a771d6e0e3b148fce95e35af948ae2c41571a3947a7400000000232200207c948c1c2c56b847e905c51f36834413e639d1fce1ef7c95cd1f47396c2173ecffffffff0174749a3b0000000017a914c71aee7e058061a1f61317e509c976affed48258870500000000fc64522102cd53fc53a07f211641a677d250f6de99caf620e8e77071e811a28b3bcddf0be1210362634ab57dae9cb373a5d536e66a8c4f67468bbcfb063809bab643072d78a1242103c5946b3fbae03a654237da863c9ed534e0878657175b132b8ca630f245df04db53ae6702f401b2755321029cecea902067992d52c38b28bf0bb2345bda9b21eca76b16a17c477a64e433012103284178e5fbcc63c54c3b38e3ef88adf2da6c526313650041b0ef955763634ebd2103ab0e2cd7ed158687fc13b88019990860cdb72b1f5777b58513312550ea1584bc2103b9fc46657cf72a1afa007ecf431de1cd27ff5cc8829fa625b66ca47b967e6b2454ae68000000000aa0615e8dd687caca67c9fb6d062d59f54d4e1a0132aa5d929476988d976d34360e +cell 04e7879f9f786d37bfb9da99ca9ea71f66a545ddf08f8026e18a14c4203f80cd a00f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206 +cell 0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206 0000000000000000000000000000000000000000000000000000000000000002000000000100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4adae5494dffff7f2002000000 +cell 144d40b74626eb8523107d6b6487461aa1d38550ef0c7a154c9d377bc99bb3c7 0a +cell 20d0527258510c6a9e1e48afcf5b201ea54020ccee66adefc23d316ac2bce970 000000000000000000000000000000000000000000000000000000000000000a0000000401000000bf3347e74131c963a32f4b6181a6b583971b180cbbae27a5f7220194636b4b4e111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f2000000000 +cell 3481400172b4addeaaf0248876e6e1769172e8bce17a039e7d9aa3cb25b31bcf 000000000000000000000000000000000000000000000000000000000000000e0000000601000000f6baaea21335a06f28fe2e874ac69601ec4388be7c2f8b34a397bd233faefd64111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f2000000000 +cell 34fa9348faf7dca06d41a200c85ef02b02ab03916a70c8a65b6e3fd84dbe0bde a04e4b6b63940122f7a527aebb0c181b9783b5a681614b2fa363c93141e74733bf +cell 3fee5096e9507962151178d120a30ccdaa2cecc95a10a749a0b540797b21e45b a064fdae3f23bd97a3348b2f7cbe8843ec0196c64a872efe286fa03513a2aebaf6 +cell 464552f57c3f89be32fab9cca794719bacf507c57ac40b3469cf6be3cfb95c3a 0000000000000000000000000000000000000000000000000000000000000006000000020100000060cb91643b56cab7f897026f3d42ee576b4b8733ff7c545be174dc30fdca064f1fd8983623d6160b34b9a771d6e0e3b148fce95e35af948ae2c41571a3947a7400f15365ffff7f2001000000 +cell 4e4b6b63940122f7a527aebb0c181b9783b5a681614b2fa363c93141e74733bf 000000000000000000000000000000000000000000000000000000000000000800000003010000003a5cb9cfe36bcf69340bc47ac507f5ac9b7194a7ccb9fa32be893f7cf5524546111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f2000000000 +cell 4f06cafd30dc74e15b547cff33874b6b57ee423d6f0297f8b7ca563b6491cb60 0000000000000000000000000000000000000000000000000000000000000004000000010100000006226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f2000000000 +cell 62979aa6ab22a67b1abee5022830e036bd87b8881abdaa2c57ee24216e26d93c a0464552f57c3f89be32fab9cca794719bacf507c57ac40b3469cf6be3cfb95c3a +cell 64fdae3f23bd97a3348b2f7cbe8843ec0196c64a872efe286fa03513a2aebaf6 000000000000000000000000000000000000000000000000000000000000000c000000050100000070e9bcc26a313dc2efad66eecc2040a51e205bcfaf481e9e6a0c51587252d020111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f2002000000 +cell 929e75683fff26031f5007c26025afaceeab29dc33e5cf5bb043d646ef67b816 fe00ca9a3b +cell a0b1dbff8c310a5e983e6e603a69b3ee024fe8b229dfca1e0b7f0d41c4768592 a020d0527258510c6a9e1e48afcf5b201ea54020ccee66adefc23d316ac2bce970 +cell cf606e0bbdd62c72533bdc45ae2e61a2b5fa7f5251ec5a0d7e99c6022191a680 a03481400172b4addeaaf0248876e6e1769172e8bce17a039e7d9aa3cb25b31bcf +cell fa504d03ff80daf373e6ce2920ce39ca86dae5077fa09b447cfcb6f0df5835b9 a04f06cafd30dc74e15b547cff33874b6b57ee423d6f0297f8b7ca563b6491cb60 +root 378eba2048055bdc0c4df1cbc13ed6fed62317fe9e1039bc765f3d2d278f6061 +end +scenario delegate +account 85845342b0d5260d516d6da5280a7df9a96d1925 0 de0b6b3a7640000 - +account d06da76e0b65671fc4639b624338464ed7b52bbd 0 de0b6b3a7640000 - +account 0000000000000000000000000000000001000006 0 115eec47f6cf7e35000000 - +account a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3 1 0 3660006000376000600036600073c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c15af45000 +account c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1 1 0 3660006000376000600036600060007300000000000000000000000000000000010000065af160010160005500 +store 0000000000000000000000000000000001000006 00000000000000000000006e657746656465726174696f6e4274635554584f73 c0 +preroot 01db24b57600090113912e934718da07185e566be7e4e4a4853211c97e3c6ac8 +tx f904668000831e848094000000000000000000000000000000000100000680b90404e5400e7b0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000001c0000000000000000000000000000000000000000000000000000000000000024000000000000000000000000000000000000000000000000000000000000002c0000000000000000000000000000000000000000000000000000000000000034000000000000000000000000000000000000000000000000000000000000000500100000006226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f20000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000500100000060cb91643b56cab7f897026f3d42ee576b4b8733ff7c545be174dc30fdca064f1fd8983623d6160b34b9a771d6e0e3b148fce95e35af948ae2c41571a3947a7400f15365ffff7f2001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000050010000003a5cb9cfe36bcf69340bc47ac507f5ac9b7194a7ccb9fa32be893f7cf5524546111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005001000000bf3347e74131c963a32f4b6181a6b583971b180cbbae27a5f7220194636b4b4e111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f20000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000500100000070e9bcc26a313dc2efad66eecc2040a51e205bcfaf481e9e6a0c51587252d020111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f200200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005001000000f6baaea21335a06f28fe2e874ac69601ec4388be7c2f8b34a397bd233faefd64111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f20000000000000000000000000000000000000000066a0312bdf00d8019e71deaa3d4e428edb58e9b017f65acd2f7004cf0b7b84856df4a047fc3dac687371d2be5a716d7f220f5a2ce98f7f8b2724e6976fc38d036de023 85845342b0d5260d516d6da5280a7df9a96d1925 80d341ac624a7f9588ad9e045c21b5b2ee1129cebfdafe8077618f1d654d649f +result 1 1 0 +tx f902aa80843b9aca00832dc6c094a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a380b9024443dc06560000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000001e000000000000000000000000000000000000000000000000000000000000001520100000001222222222222222222222222222222222222222222222222222222222222222200000000fdfd0000473030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030483131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131314c69522102141481bf1181ed61aa025f1fe708f68cb018c2c9d6eb719ccd94b3f6ff615308210222ca50390e660f601036dd7502bb973bdcd104b5dcb8f2e74de8edc6c292b03a2103cfc18f02cc004640f2116fdd1f6ca2022e39be25df75e27c80bde842e6b6f93853aeffffffff0100ca9a3b0000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c787000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002701000000011fd8983623d6160b34b9a771d6e0e3b148fce95e35af948ae2c41571a3947a7401010000000000000000000000000000000000000000000000000065a0c46d50cd69ff2accbd2544de9bcdfb87f43187877192c267c92e5138ea423a2ca07b98444e6542fba39736a7c402de3c7341ac5a5f75a052ca930fd47796138810 d06da76e0b65671fc4639b624338464ed7b52bbd 6704aa5e5b02002decbce3ad6fb7808b36802a6c84f904f3203daa4d524878e5 +result 1 1 72568 +log 0000000000000000000000000000000001000006 708ce1ead20561c5894a93be3fee64b326b2ad6c198f8253e4bb56f1626053d6,747a94a37115c4e28a94af355ee9fc48b1e3e0d671a7b9340b16d6233698d81f 0000000000000000000000000000000000000000000000000000000000000002 +log 0000000000000000000000000000000001000006 7a7c29481528ac8c2b2e93aee658fddd4dc15304fa723a5c2b88514557bcc790,83305b02e87ece5636f9302a1e06df4f0e6d3a8139aeb0efebcd5b91715756f5,86bc174bbe4d5209f8cdc70621c2ef604dcca60f61e6ac84a43f7feab7d5d867 000000000000000000000000000000000000000000000000000000003b9aca00 +log 0000000000000000000000000000000001000006 9ee5d520fd5e6eaea3fd2e3ae4e35e9a9c0fb05c9d8f84b507f287da84b5117c,86bc174bbe4d5209f8cdc70621c2ef604dcca60f61e6ac84a43f7feab7d5d867 00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000005fe00ca9a3b000000000000000000000000000000000000000000000000000000 +itx 0 0 call a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3 a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3 0 01 3b9aca00 000000000000000000000000000000000000000000000000000000000067c280 3660006000376000600036600060007300000000000000000000000000000000010000065af160010160005500 6704aa5e5b02002decbce3ad6fb7808b36802a6c84f904f3203daa4d524878e5 0 88bbb040bb3f3f4cc6cfb3a4b44b507f81d56d65542a51c88fefb846f882ab28 +itx 1 0 call a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3 0000000000000000000000000000000001000006 0 01 3b9aca00 000000000000000000000000000000000000000000000000000000000067c280 - 88bbb040bb3f3f4cc6cfb3a4b44b507f81d56d65542a51c88fefb846f882ab28 0 83305b02e87ece5636f9302a1e06df4f0e6d3a8139aeb0efebcd5b91715756f5 +cell 0000000000000000000000000000000000000062746354784861736865734150 c0 +cell 00000000000000000000000000626c6f636b53746f7265436861696e48656164 000000000000000000000000000000000000000000000000000000000000000e0000000601000000f6baaea21335a06f28fe2e874ac69601ec4388be7c2f8b34a397bd233faefd64111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f2000000000 +cell 00000000000000000000006e657746656465726174696f6e4274635554584f73 c0 +cell 000000000000000000000072656c656173655472616e73616374696f6e536574 c0 +cell 0072656c656173655472616e73616374696f6e53657457697468547848617368 f9019fb9017a020000000001011fd8983623d6160b34b9a771d6e0e3b148fce95e35af948ae2c41571a3947a7400000000232200207c948c1c2c56b847e905c51f36834413e639d1fce1ef7c95cd1f47396c2173ecffffffff0174749a3b0000000017a914c71aee7e058061a1f61317e509c976affed48258870500000000fc64522102cd53fc53a07f211641a677d250f6de99caf620e8e77071e811a28b3bcddf0be1210362634ab57dae9cb373a5d536e66a8c4f67468bbcfb063809bab643072d78a1242103c5946b3fbae03a654237da863c9ed534e0878657175b132b8ca630f245df04db53ae6702f401b2755321029cecea902067992d52c38b28bf0bb2345bda9b21eca76b16a17c477a64e433012103284178e5fbcc63c54c3b38e3ef88adf2da6c526313650041b0ef955763634ebd2103ab0e2cd7ed158687fc13b88019990860cdb72b1f5777b58513312550ea1584bc2103b9fc46657cf72a1afa007ecf431de1cd27ff5cc8829fa625b66ca47b967e6b2454ae68000000000aa083305b02e87ece5636f9302a1e06df4f0e6d3a8139aeb0efebcd5b91715756f5 +cell 04e7879f9f786d37bfb9da99ca9ea71f66a545ddf08f8026e18a14c4203f80cd a00f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206 +cell 0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206 0000000000000000000000000000000000000000000000000000000000000002000000000100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4adae5494dffff7f2002000000 +cell 144d40b74626eb8523107d6b6487461aa1d38550ef0c7a154c9d377bc99bb3c7 0a +cell 20d0527258510c6a9e1e48afcf5b201ea54020ccee66adefc23d316ac2bce970 000000000000000000000000000000000000000000000000000000000000000a0000000401000000bf3347e74131c963a32f4b6181a6b583971b180cbbae27a5f7220194636b4b4e111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f2000000000 +cell 3481400172b4addeaaf0248876e6e1769172e8bce17a039e7d9aa3cb25b31bcf 000000000000000000000000000000000000000000000000000000000000000e0000000601000000f6baaea21335a06f28fe2e874ac69601ec4388be7c2f8b34a397bd233faefd64111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f2000000000 +cell 34fa9348faf7dca06d41a200c85ef02b02ab03916a70c8a65b6e3fd84dbe0bde a04e4b6b63940122f7a527aebb0c181b9783b5a681614b2fa363c93141e74733bf +cell 3fee5096e9507962151178d120a30ccdaa2cecc95a10a749a0b540797b21e45b a064fdae3f23bd97a3348b2f7cbe8843ec0196c64a872efe286fa03513a2aebaf6 +cell 464552f57c3f89be32fab9cca794719bacf507c57ac40b3469cf6be3cfb95c3a 0000000000000000000000000000000000000000000000000000000000000006000000020100000060cb91643b56cab7f897026f3d42ee576b4b8733ff7c545be174dc30fdca064f1fd8983623d6160b34b9a771d6e0e3b148fce95e35af948ae2c41571a3947a7400f15365ffff7f2001000000 +cell 4e4b6b63940122f7a527aebb0c181b9783b5a681614b2fa363c93141e74733bf 000000000000000000000000000000000000000000000000000000000000000800000003010000003a5cb9cfe36bcf69340bc47ac507f5ac9b7194a7ccb9fa32be893f7cf5524546111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f2000000000 +cell 4f06cafd30dc74e15b547cff33874b6b57ee423d6f0297f8b7ca563b6491cb60 0000000000000000000000000000000000000000000000000000000000000004000000010100000006226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f2000000000 +cell 62979aa6ab22a67b1abee5022830e036bd87b8881abdaa2c57ee24216e26d93c a0464552f57c3f89be32fab9cca794719bacf507c57ac40b3469cf6be3cfb95c3a +cell 64fdae3f23bd97a3348b2f7cbe8843ec0196c64a872efe286fa03513a2aebaf6 000000000000000000000000000000000000000000000000000000000000000c000000050100000070e9bcc26a313dc2efad66eecc2040a51e205bcfaf481e9e6a0c51587252d020111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f2002000000 +cell 929e75683fff26031f5007c26025afaceeab29dc33e5cf5bb043d646ef67b816 fe00ca9a3b +cell a0b1dbff8c310a5e983e6e603a69b3ee024fe8b229dfca1e0b7f0d41c4768592 a020d0527258510c6a9e1e48afcf5b201ea54020ccee66adefc23d316ac2bce970 +cell cf606e0bbdd62c72533bdc45ae2e61a2b5fa7f5251ec5a0d7e99c6022191a680 a03481400172b4addeaaf0248876e6e1769172e8bce17a039e7d9aa3cb25b31bcf +cell fa504d03ff80daf373e6ce2920ce39ca86dae5077fa09b447cfcb6f0df5835b9 a04f06cafd30dc74e15b547cff33874b6b57ee423d6f0297f8b7ca563b6491cb60 +root 4cbd14b64e44059e346bef8c0d6e7db46adc893de6c761af3a26fe64e1e88843 +end +scenario low-fee +account 85845342b0d5260d516d6da5280a7df9a96d1925 0 de0b6b3a7640000 - +account d06da76e0b65671fc4639b624338464ed7b52bbd 0 de0b6b3a7640000 - +account 0000000000000000000000000000000001000006 0 115eec47f6cf7e35000000 - +store 0000000000000000000000000000000001000006 00000000000000000000006e657746656465726174696f6e4274635554584f73 c0 +store 0000000000000000000000000000000001000006 0000000000000000000000000000000000000000000000006665655065724b62 822710 +preroot d48867289ab70d678d62e2f101f74476c5a1b4679ca1afa96fa1525c13a7dc5d +tx f904668000831e848094000000000000000000000000000000000100000680b90404e5400e7b0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000001c0000000000000000000000000000000000000000000000000000000000000024000000000000000000000000000000000000000000000000000000000000002c0000000000000000000000000000000000000000000000000000000000000034000000000000000000000000000000000000000000000000000000000000000500100000006226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f20000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000500100000060cb91643b56cab7f897026f3d42ee576b4b8733ff7c545be174dc30fdca064f1fd8983623d6160b34b9a771d6e0e3b148fce95e35af948ae2c41571a3947a7400f15365ffff7f2001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000050010000003a5cb9cfe36bcf69340bc47ac507f5ac9b7194a7ccb9fa32be893f7cf5524546111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005001000000bf3347e74131c963a32f4b6181a6b583971b180cbbae27a5f7220194636b4b4e111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f20000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000500100000070e9bcc26a313dc2efad66eecc2040a51e205bcfaf481e9e6a0c51587252d020111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f200200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005001000000f6baaea21335a06f28fe2e874ac69601ec4388be7c2f8b34a397bd233faefd64111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f20000000000000000000000000000000000000000066a0312bdf00d8019e71deaa3d4e428edb58e9b017f65acd2f7004cf0b7b84856df4a047fc3dac687371d2be5a716d7f220f5a2ce98f7f8b2724e6976fc38d036de023 85845342b0d5260d516d6da5280a7df9a96d1925 80d341ac624a7f9588ad9e045c21b5b2ee1129cebfdafe8077618f1d654d649f +result 1 1 0 +tx f902a68000832dc6c094000000000000000000000000000000000100000680b9024443dc06560000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000001e000000000000000000000000000000000000000000000000000000000000001520100000001222222222222222222222222222222222222222222222222222222222222222200000000fdfd0000473030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030483131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131314c69522102141481bf1181ed61aa025f1fe708f68cb018c2c9d6eb719ccd94b3f6ff615308210222ca50390e660f601036dd7502bb973bdcd104b5dcb8f2e74de8edc6c292b03a2103cfc18f02cc004640f2116fdd1f6ca2022e39be25df75e27c80bde842e6b6f93853aeffffffff0100ca9a3b0000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c787000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002701000000011fd8983623d6160b34b9a771d6e0e3b148fce95e35af948ae2c41571a3947a7401010000000000000000000000000000000000000000000000000065a041e132f2c25bf123029bdea87c64c3a6a2424c9a4cd7766d4f7a6428c0788792a01271052c0ad805a62b7bdfb114d61595f7fcd45edcaeecdcefedb947e7fb31f6 d06da76e0b65671fc4639b624338464ed7b52bbd 6e0ed1e4894c050d32505ffa8c749199cd0ba0307f67cd447197e78dad6e4759 +result 1 1 50872 +log 0000000000000000000000000000000001000006 708ce1ead20561c5894a93be3fee64b326b2ad6c198f8253e4bb56f1626053d6,747a94a37115c4e28a94af355ee9fc48b1e3e0d671a7b9340b16d6233698d81f 0000000000000000000000000000000000000000000000000000000000000002 +log 0000000000000000000000000000000001000006 7a7c29481528ac8c2b2e93aee658fddd4dc15304fa723a5c2b88514557bcc790,6e0ed1e4894c050d32505ffa8c749199cd0ba0307f67cd447197e78dad6e4759,f5f4baa8bd31f923e7addf7b0029ce7f456ec7d9d071b88d556f27228f6311e2 000000000000000000000000000000000000000000000000000000003b9aca00 +log 0000000000000000000000000000000001000006 9ee5d520fd5e6eaea3fd2e3ae4e35e9a9c0fb05c9d8f84b507f287da84b5117c,f5f4baa8bd31f923e7addf7b0029ce7f456ec7d9d071b88d556f27228f6311e2 00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000005fe00ca9a3b000000000000000000000000000000000000000000000000000000 +cell 0000000000000000000000000000000000000000000000006665655065724b62 822710 +cell 0000000000000000000000000000000000000062746354784861736865734150 c0 +cell 00000000000000000000000000626c6f636b53746f7265436861696e48656164 000000000000000000000000000000000000000000000000000000000000000e0000000601000000f6baaea21335a06f28fe2e874ac69601ec4388be7c2f8b34a397bd233faefd64111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f2000000000 +cell 00000000000000000000006e657746656465726174696f6e4274635554584f73 c0 +cell 000000000000000000000072656c656173655472616e73616374696f6e536574 c0 +cell 0072656c656173655472616e73616374696f6e53657457697468547848617368 f9019fb9017a020000000001011fd8983623d6160b34b9a771d6e0e3b148fce95e35af948ae2c41571a3947a7400000000232200207c948c1c2c56b847e905c51f36834413e639d1fce1ef7c95cd1f47396c2173ecffffffff0178b69a3b0000000017a914c71aee7e058061a1f61317e509c976affed48258870500000000fc64522102cd53fc53a07f211641a677d250f6de99caf620e8e77071e811a28b3bcddf0be1210362634ab57dae9cb373a5d536e66a8c4f67468bbcfb063809bab643072d78a1242103c5946b3fbae03a654237da863c9ed534e0878657175b132b8ca630f245df04db53ae6702f401b2755321029cecea902067992d52c38b28bf0bb2345bda9b21eca76b16a17c477a64e433012103284178e5fbcc63c54c3b38e3ef88adf2da6c526313650041b0ef955763634ebd2103ab0e2cd7ed158687fc13b88019990860cdb72b1f5777b58513312550ea1584bc2103b9fc46657cf72a1afa007ecf431de1cd27ff5cc8829fa625b66ca47b967e6b2454ae68000000000aa06e0ed1e4894c050d32505ffa8c749199cd0ba0307f67cd447197e78dad6e4759 +cell 04e7879f9f786d37bfb9da99ca9ea71f66a545ddf08f8026e18a14c4203f80cd a00f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206 +cell 0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206 0000000000000000000000000000000000000000000000000000000000000002000000000100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4adae5494dffff7f2002000000 +cell 144d40b74626eb8523107d6b6487461aa1d38550ef0c7a154c9d377bc99bb3c7 0a +cell 20d0527258510c6a9e1e48afcf5b201ea54020ccee66adefc23d316ac2bce970 000000000000000000000000000000000000000000000000000000000000000a0000000401000000bf3347e74131c963a32f4b6181a6b583971b180cbbae27a5f7220194636b4b4e111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f2000000000 +cell 3481400172b4addeaaf0248876e6e1769172e8bce17a039e7d9aa3cb25b31bcf 000000000000000000000000000000000000000000000000000000000000000e0000000601000000f6baaea21335a06f28fe2e874ac69601ec4388be7c2f8b34a397bd233faefd64111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f2000000000 +cell 34fa9348faf7dca06d41a200c85ef02b02ab03916a70c8a65b6e3fd84dbe0bde a04e4b6b63940122f7a527aebb0c181b9783b5a681614b2fa363c93141e74733bf +cell 3fee5096e9507962151178d120a30ccdaa2cecc95a10a749a0b540797b21e45b a064fdae3f23bd97a3348b2f7cbe8843ec0196c64a872efe286fa03513a2aebaf6 +cell 464552f57c3f89be32fab9cca794719bacf507c57ac40b3469cf6be3cfb95c3a 0000000000000000000000000000000000000000000000000000000000000006000000020100000060cb91643b56cab7f897026f3d42ee576b4b8733ff7c545be174dc30fdca064f1fd8983623d6160b34b9a771d6e0e3b148fce95e35af948ae2c41571a3947a7400f15365ffff7f2001000000 +cell 4e4b6b63940122f7a527aebb0c181b9783b5a681614b2fa363c93141e74733bf 000000000000000000000000000000000000000000000000000000000000000800000003010000003a5cb9cfe36bcf69340bc47ac507f5ac9b7194a7ccb9fa32be893f7cf5524546111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f2000000000 +cell 4f06cafd30dc74e15b547cff33874b6b57ee423d6f0297f8b7ca563b6491cb60 0000000000000000000000000000000000000000000000000000000000000004000000010100000006226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f2000000000 +cell 62979aa6ab22a67b1abee5022830e036bd87b8881abdaa2c57ee24216e26d93c a0464552f57c3f89be32fab9cca794719bacf507c57ac40b3469cf6be3cfb95c3a +cell 64fdae3f23bd97a3348b2f7cbe8843ec0196c64a872efe286fa03513a2aebaf6 000000000000000000000000000000000000000000000000000000000000000c000000050100000070e9bcc26a313dc2efad66eecc2040a51e205bcfaf481e9e6a0c51587252d020111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f2002000000 +cell 80519179efdd845de618b35d17ec42735caf3ff457164d5c5bdfce0f1b1a912e fe00ca9a3b +cell a0b1dbff8c310a5e983e6e603a69b3ee024fe8b229dfca1e0b7f0d41c4768592 a020d0527258510c6a9e1e48afcf5b201ea54020ccee66adefc23d316ac2bce970 +cell cf606e0bbdd62c72533bdc45ae2e61a2b5fa7f5251ec5a0d7e99c6022191a680 a03481400172b4addeaaf0248876e6e1769172e8bce17a039e7d9aa3cb25b31bcf +cell fa504d03ff80daf373e6ce2920ce39ca86dae5077fa09b447cfcb6f0df5835b9 a04f06cafd30dc74e15b547cff33874b6b57ee423d6f0297f8b7ca563b6491cb60 +root 03143d676052bb5b8091b22ce424391fcc4d5de635612fce1639aff6f230643f +end +# flyover address 2MtJdJKkuy2h3qRh33cd9FpbWBFBFBWf6ea +# flyover payment 35c3e164e0d0d7a3c4fe3083610d6e6888deb42a257030b2332524528026a83f 01000000012323232323232323232323232323232323232323232323232323232323232323010000006a473030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030210221456873b58e368752c7580008d3bcae9efaf1f5c572784225e3d8548fd421cbffffffff010088526a7400000017a9140b9cd52ebf73366be961088531b963be39e7b3cc8700000000 +scenario flyover +account 85845342b0d5260d516d6da5280a7df9a96d1925 0 de0b6b3a7640000 - +account d06da76e0b65671fc4639b624338464ed7b52bbd 0 de0b6b3a7640000 - +account 0000000000000000000000000000000001000006 0 115eec47f6cf7e35000000 - +account a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 1 0 3660006000376000600036600060007300000000000000000000000000000000010000065af160010160005500 +store 0000000000000000000000000000000001000006 00000000000000000000006e657746656465726174696f6e4274635554584f73 c0 +preroot 5735e868e625c6441e7d6c1d097342b27c1c2872470a4b1eae2e9b2ed12a7d6e +tx f904668000831e848094000000000000000000000000000000000100000680b90404e5400e7b0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000001c0000000000000000000000000000000000000000000000000000000000000024000000000000000000000000000000000000000000000000000000000000002c0000000000000000000000000000000000000000000000000000000000000034000000000000000000000000000000000000000000000000000000000000000500100000006226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f20000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000500100000060cb91643b56cab7f897026f3d42ee576b4b8733ff7c545be174dc30fdca064f3fa8268052242533b23070252ab4de88686e0d618330fec4a3d7d0e064e1c33500f15365ffff7f2000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000050010000008a9223759ad99a7ccab8bef4b235c7099f81d487729ce0ea44a45c8414169211111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005001000000d287069a38b3052170239426ad6778e6bfa86a733e910339ca3364065ef23575111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f20010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000500100000082c34a0e85ecd0e960fd4da0aa99ec4cc542688e47afb5dcac5eadbaf04e3803111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f20030000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000500100000021250be8098b730ba5d0cbcd6db92aaf05fd7d368c951aaadc825e4e060b0f5a111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f20000000000000000000000000000000000000000066a0d9bb3c499f74ab5d835270d83772d85504ac9266beecd280489e3eca543a905da03f926149952dd8cd3784624a66d2876ce6f6c3ab6f5529ac5bcd059a98c0c69a 85845342b0d5260d516d6da5280a7df9a96d1925 25bd76d1b34f416dbfeac71c0c3d4e1824e722f8f4459f78e9b5245f70c513ba +result 1 1 0 +tx f903268000832dc6c094a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a180b902c46adc01330000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000001e033333333333333333333333333333333333333333333333333333333333333330000000000000000000000000000000000000000000000000000000000000240000000000000000000000000a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a10000000000000000000000000000000000000000000000000000000000000280000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000bd01000000012323232323232323232323232323232323232323232323232323232323232323010000006a473030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030210221456873b58e368752c7580008d3bcae9efaf1f5c572784225e3d8548fd421cbffffffff010088526a7400000017a9140b9cd52ebf73366be961088531b963be39e7b3cc8700000000000000000000000000000000000000000000000000000000000000000000000000002701000000013fa8268052242533b23070252ab4de88686e0d618330fec4a3d7d0e064e1c33501010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000156f444444444444444444444444444444444444444400000000000000000000000000000000000000000000000000000000000000000000000000000000000015c45555555555555555555555555555555555555555000000000000000000000066a0424fdcf82881298b484ee21d9a15c7a2c45130233519d15a88aaca7c9eeef851a053739547d890be74b6f927533f682a3fb03b17d3f96f66446693780e8b35d3cf d06da76e0b65671fc4639b624338464ed7b52bbd 00c0a4125894c77f62769842604f32a461c97cd8bdd4a549d2b6972728e3a679 +result 1 1 74966 +log 0000000000000000000000000000000001000006 7a7c29481528ac8c2b2e93aee658fddd4dc15304fa723a5c2b88514557bcc790,00c0a4125894c77f62769842604f32a461c97cd8bdd4a549d2b6972728e3a679,0d3f91a035d8bfe3266edaf7d967d1f850330b63e4a21f2ae209eb613138cc9e 000000000000000000000000000000000000000000000000000000746a528800 +log 0000000000000000000000000000000001000006 9ee5d520fd5e6eaea3fd2e3ae4e35e9a9c0fb05c9d8f84b507f287da84b5117c,0d3f91a035d8bfe3266edaf7d967d1f850330b63e4a21f2ae209eb613138cc9e 00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000009ff0088526a740000000000000000000000000000000000000000000000000000 +itx 0 0 call a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 0 01 0 000000000000000000000000000000000000000000000000000000000067c280 - 00c0a4125894c77f62769842604f32a461c97cd8bdd4a549d2b6972728e3a679 0 c15cd04d9298cb3108a8f88715b0ee3ee7d8c7a70e1ef3d080a1697f44d8aa50 +cell 000000000000000000000000000000000000000000006c6f636b696e67436170 85174876e800 +cell 00000000000000000000000000626c6f636b53746f7265436861696e48656164 000000000000000000000000000000000000000000000000000000000000000e000000060100000021250be8098b730ba5d0cbcd6db92aaf05fd7d368c951aaadc825e4e060b0f5a111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f2000000000 +cell 00000000000000000000006e657746656465726174696f6e4274635554584f73 c0 +cell 000000000000000000000072656c656173655472616e73616374696f6e536574 c0 +cell 0072656c656173655472616e73616374696f6e53657457697468547848617368 f901c5b901a0020000000001013fa8268052242533b23070252ab4de88686e0d618330fec4a3d7d0e064e1c3350000000023220020a144ce9377705a946e1f32376463ba72cbbd10c0095463e8474c76b3e8405c30ffffffff01282e526a740000001976a914444444444444444444444444444444444444444488ac0500000000fd1e0120b3d520280f651a509cc3faf7bade5d5ee67a98ed9d7100999d0dc0ba150912677564522102cd53fc53a07f211641a677d250f6de99caf620e8e77071e811a28b3bcddf0be1210362634ab57dae9cb373a5d536e66a8c4f67468bbcfb063809bab643072d78a1242103c5946b3fbae03a654237da863c9ed534e0878657175b132b8ca630f245df04db53ae6702f401b2755321029cecea902067992d52c38b28bf0bb2345bda9b21eca76b16a17c477a64e433012103284178e5fbcc63c54c3b38e3ef88adf2da6c526313650041b0ef955763634ebd2103ab0e2cd7ed158687fc13b88019990860cdb72b1f5777b58513312550ea1584bc2103b9fc46657cf72a1afa007ecf431de1cd27ff5cc8829fa625b66ca47b967e6b2454ae68000000000aa000c0a4125894c77f62769842604f32a461c97cd8bdd4a549d2b6972728e3a679 +cell 03384ef0baad5eacdcb5af478e6842c54cec99aaa04dfd60e9d0ec850e4ac382 000000000000000000000000000000000000000000000000000000000000000a0000000401000000d287069a38b3052170239426ad6778e6bfa86a733e910339ca3364065ef23575111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f2001000000 +cell 04e7879f9f786d37bfb9da99ca9ea71f66a545ddf08f8026e18a14c4203f80cd a00f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206 +cell 0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206 0000000000000000000000000000000000000000000000000000000000000002000000000100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4adae5494dffff7f2002000000 +cell 11921614845ca444eae09c7287d4819f09c735b2f4beb8ca7c9ad99a7523928a 0000000000000000000000000000000000000000000000000000000000000006000000020100000060cb91643b56cab7f897026f3d42ee576b4b8733ff7c545be174dc30fdca064f3fa8268052242533b23070252ab4de88686e0d618330fec4a3d7d0e064e1c33500f15365ffff7f2000000000 +cell 34fa9348faf7dca06d41a200c85ef02b02ab03916a70c8a65b6e3fd84dbe0bde a07535f25e066433ca3903913e736aa8bfe67867ad269423702105b3389a0687d2 +cell 3fee5096e9507962151178d120a30ccdaa2cecc95a10a749a0b540797b21e45b a05a0f0b064e5e82dcaa1a958c367dfd05af2ab96dcdcbd0a50b738b09e80b2521 +cell 49196b034096e400d9d3c5a3cdac052abbda2dd2f96df83e8b573626377e6499 000000000000000000000000000000000000000000000000000000000000000e000000060100000021250be8098b730ba5d0cbcd6db92aaf05fd7d368c951aaadc825e4e060b0f5a111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f2000000000 +cell 4f06cafd30dc74e15b547cff33874b6b57ee423d6f0297f8b7ca563b6491cb60 0000000000000000000000000000000000000000000000000000000000000004000000010100000006226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f2000000000 +cell 5a0f0b064e5e82dcaa1a958c367dfd05af2ab96dcdcbd0a50b738b09e80b2521 000000000000000000000000000000000000000000000000000000000000000c000000050100000082c34a0e85ecd0e960fd4da0aa99ec4cc542688e47afb5dcac5eadbaf04e3803111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f2003000000 +cell 62979aa6ab22a67b1abee5022830e036bd87b8881abdaa2c57ee24216e26d93c a011921614845ca444eae09c7287d4819f09c735b2f4beb8ca7c9ad99a7523928a +cell 7535f25e066433ca3903913e736aa8bfe67867ad269423702105b3389a0687d2 000000000000000000000000000000000000000000000000000000000000000800000003010000008a9223759ad99a7ccab8bef4b235c7099f81d487729ce0ea44a45c8414169211111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f2000000000 +cell a0b1dbff8c310a5e983e6e603a69b3ee024fe8b229dfca1e0b7f0d41c4768592 a003384ef0baad5eacdcb5af478e6842c54cec99aaa04dfd60e9d0ec850e4ac382 +cell a2604acf9bcdccd575516e3d4bc8636a8d19a1e1e77d6f20e826da1f41926adf ff0088526a74000000 +cell cf606e0bbdd62c72533bdc45ae2e61a2b5fa7f5251ec5a0d7e99c6022191a680 a049196b034096e400d9d3c5a3cdac052abbda2dd2f96df83e8b573626377e6499 +cell eb0e24421403c126bc4f1b5efcf8a11f79c70bfcce1220cde462b963864241da 01 +cell fa504d03ff80daf373e6ce2920ce39ca86dae5077fa09b447cfcb6f0df5835b9 a04f06cafd30dc74e15b547cff33874b6b57ee423d6f0297f8b7ca563b6491cb60 +root ac10b69db292a42bc6b120945108f346aa0d4a2fb043d990d9e6544c0442e1c1 +end +enc 0 1 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 0 - call 0 0 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 f64ee69aab5fd5b648c2ca017b0880b610223c0ae21d4089905212c9ffaa6992 +enc 1 1 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 0 - call 0 0 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 bf55a8a523265031c3a9def117bf2d91f605409b862dbb0c20982d12e1110a01 +enc 127 1 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 0 - call 0 0 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 438a42134addf73ea10411211e55d1b0436dd83321eea6957a9251d36ed94d4a +enc 128 1 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 0 - call 0 0 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 77364305db455e0273e8fd382a46db278ba27ddff3ba36667383cfbfc3d8d826 +enc 255 1 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 0 - call 0 0 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 f75fc14a1be34ce465960cfeba4ed18f64bd6d1f96a7b5fa831a1fa405f06f6a +enc 256 1 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 0 - call 0 0 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 3cdfdf07ba1a4cefda734ffd27ac4acdee6cc2d11826715bca2dde2c4f1d7b17 +enc 32768 1 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 0 - call 0 0 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 75c77685538096eafd8b4a6bbe1a3edf171c907254495de668fb5b327b002aa5 +enc 2147483648 1 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 0 - call 0 0 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 9d66754ee38076ccb3047de414319a2cf8bba6fd736b5477dfd35246fe9a526f +enc 9223372036854775807 1 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 0 - call 0 0 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 f8f0059f59b36811bae83ab373669d8e762e41dc287b98c5efa51c879515aa14 +enc 1 0 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 0 - call 0 0 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 15d68f78b211d80f910eda31ac2ebae2ca899434592e3cd30d3ad5908336ea9a +enc 1 0 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 0 - call 0 0 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 15d68f78b211d80f910eda31ac2ebae2ca899434592e3cd30d3ad5908336ea9a +enc 1 0 0 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 0 - call 0 0 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 ba829fbb7400d059b7be89ad41df6c736848f4224acf2832b4c6bb5bb43b00aa +enc 1 1 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 0 - call 0 0 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 bf55a8a523265031c3a9def117bf2d91f605409b862dbb0c20982d12e1110a01 +enc 1 0 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 1 - call 0 0 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 79a501b6b72954631558e8bcb5aab76907cb3c8e310ef3ccbf9fdb85b8d14659 +enc 1 0 1 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 0 - call 0 0 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 80e215dc9b7a0979d7f05dd26590395d8612877fc56014a7e4eba2ce6570827d +enc 1 80 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 0 - call 0 0 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 bb91e44d32c4082284df0641f7aba00553e72cb0d0c00306693f732774abc72e +enc 1 0 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 80 - call 0 0 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 b007fa1d8e54dc5973f8290ff7a5e7db3bec1858cd19e0e3a54c8df8bc7f396e +enc 1 0 80 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 0 - call 0 0 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 f85bc7f78ba87136d019858353e5186f04ca32854b73b28414d767208050660d +enc 1 3b9aca00 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 0 - call 0 0 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 2bdf179fee4ab49c720dac90da28a98672f508f8fc5bc7dcfa3552b19b6328d9 +enc 1 0 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 3b9aca00 - call 0 0 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 c0c1bee229664f0456e5159b5d964a18263c6777f8b53bd8129022b17e7b6659 +enc 1 0 3b9aca00 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 0 - call 0 0 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 565c38d0572610e99bb848bfa5839699cf3601353f1d8dcb618525d3c92cc648 +enc 1 8000000000000000000000000000000000000000000000000000000000000000 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 0 - call 0 0 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 c17009f3646df7cada0810a420be31fa4628c15612b740abc97e46a2646895bd +enc 1 0 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 8000000000000000000000000000000000000000000000000000000000000000 - call 0 0 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 a8eeddeae371bd3d83b7ad2fba383ffb081ebc0dabf6be446d4623fad61bb52f +enc 1 0 8000000000000000000000000000000000000000000000000000000000000000 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 0 - call 0 0 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 f0597529caca8161ab57806388e1f1206ab6c13f916bd0822615c3357608c252 +enc 1 ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 0 - call 0 0 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 96f4e58e265a90fb8081799c325057f77ad039be5839518884a2e51bb77571bc +enc 1 0 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff - call 0 0 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 974186032d5ac8f39984bfae8201d8f29ca84dcd8ea29a888490e84171f39e73 +enc 1 0 ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 0 - call 0 0 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 df306cfffeead88037e61bd5b4e7337d4e2ded972260ad97b6c7c6c0047120f4 +enc 1 0 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 0 - call 0 0 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 15d68f78b211d80f910eda31ac2ebae2ca899434592e3cd30d3ad5908336ea9a +enc 1 0 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 0 - call 0 0 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 15d68f78b211d80f910eda31ac2ebae2ca899434592e3cd30d3ad5908336ea9a +enc 1 0 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 0 - call 1 0 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 db44c6c09a8b4941bd4f81a411384447eedfd8e8a501df9180e21b8809582de0 +enc 1 0 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 0 - call 0 1 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 7c5a91200f95f262f3ab24b20f5989c576330ab3bcdf6b72e36e7b46dce7c8bb +enc 1 0 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 0 - call 127 0 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 02bcf08e237482cdc5402c17ed49b3d8c752c7f71c1805a8ede194e165f0e5dc +enc 1 0 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 0 - call 0 127 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 bd320160a62a1dc118b9c3864cc2de3b90614e7fb69ebcb6a428cb4d3fa1a64e +enc 1 0 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 0 - call 128 0 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 db7d65d717f335bc8b7d13539f2f05028c7481f2d2db5e9ab2e8d842ba91c7a0 +enc 1 0 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 0 - call 0 128 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 5e7b55ee9b604a5488d8763a38ccd0991e277830d930712a1f666e92227471bd +enc 1 0 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 0 - call 255 0 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 d7ce062ea57fbd3664f92efd3cf08295b2dee49d1b959b0825065d37e2bcccf2 +enc 1 0 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 0 - call 0 255 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 08a4f74dfcdbf5d8da70c4640e79d9dc6b15bfb8f7c28bc361023d5d78df0911 +enc 1 0 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 0 - call 256 0 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 63c69551cf69ce736f244270105ddee89b92bc038275f63c53f0d666ee09d01c +enc 1 0 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 0 - call 0 256 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 6d6443ef7ab4c7db191ebb970268805685e47a11641102f840986d116943a0c7 +enc 1 0 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 0 - call 400 0 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 9bb5b5ac811cfe3db0a14b07e211c3956256d7b2e2cb3bf23836529d4457d096 +enc 1 0 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 0 - call 0 400 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 61956a88385c701e0e11b5f39e1f79ecc827c224daaabc6e034d7455ab613d67 +enc 1 0 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 0 - call 1024 0 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 018e07bc577f575620185e84b1fe543267a3f50c6d75cf06fbfc2585f7c4e1f6 +enc 1 0 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 0 - call 0 1024 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 ddc7cbc23e8283e31702f04c2a7874ec8f617514d5d3740cabf3a50bf8438265 +enc 1 0 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 0 - call 65535 0 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 90378ec05d9d0b1d110b0371f1f45d03d8e4bf6698fce98f0c91191e99a83f8d +enc 1 0 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 0 - call 0 65535 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 47f73adf248ea5f69272565073e012fec93ce36adb88e475132cbbe9f922bab2 +enc 1 0 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 0 - call 65536 0 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 494769f86b803401bc917766fb2f4ac3b0736ab651201ad8ccac58867deabf86 +enc 1 0 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 0 - call 0 65536 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 7e980a2d55e289845c86776564ff72d0fdb809ce760a52be539409803e32ab5c +enc 1 0 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 0 - call 16777216 0 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 ae9ae300d47184aae40186fe127f26cb34a1effac6019831c0df3a8998e9a534 +enc 1 0 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 0 - call 0 16777216 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 2931f9bec2101d7c0c09734fe641278a17b6ee169ac76ea8c936ec66c0740502 +enc 1 0 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 0 - call 2147483647 0 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 2d7c2b4a2e403463b62f0d898a22f9c71d0a0cb9119594b059c433e6906b88a9 +enc 1 0 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 0 - call 0 2147483647 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 bae6e581d2e7aca6899de3a8b50a8b1dd9d9ddf9840033699d069e6cac11b772 +enc 1 0 - a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2 7 - suicide 3 2 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 be7706d7a566cb6a955155dfb91fcc237bebf7731acfac6d609e013a69dec468 +enc 5 a 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 - 9 6000600060006000600060995af15000 create 1 4 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 15afd4f82d5052d95efed6a7b0a2737b6537424ae3d90604684fb4d80bdc6b4b +enc 1 0 67c280 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 0 - call 0 0 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 19cc7906f50c4b7714c3c31bc80ac71aa981a9149843594e52b70edfcd1da5da +enc 1 0 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0 5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b call 0 0 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 6997cf3d22baa9d6c2c2a736bda354df73732b2cc7bc4a3a4a3f6523589906c7 +enc 1 0 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0 5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b call 0 0 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 02830fe53b8aa70d0831d9ff4ef1ceefa4805ab032877ecd72f443a573c65daf +enc 1 0 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0 00 call 0 0 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 c784dc187e35a4fb22d33c9fba09590373dd1b1b94478b5b54fe79fcebf2b824 +enc 1 0 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0 7f call 0 0 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 d16aa8933a655961730419ba6e4f8d160dbdb0d656642a410126b952a85d0bca +enc 1 0 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0 80 call 0 0 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 31032875a9d9a495751bc4fa30c909ebe0bfcfaeb068faa541adfb4eb280508a diff --git a/crates/execution/src/executor.rs b/crates/execution/src/executor.rs index 4a00c1c..2c0b7a7 100644 --- a/crates/execution/src/executor.rs +++ b/crates/execution/src/executor.rs @@ -14607,4 +14607,222 @@ bf09f6e52420834e8e0e0b1a6df563aba550cf7f99e9724264187c45dcf3d73e8585a0e35196\ assert_subtree_deleted(&new_root, store.as_ref(), &victim); } } + + /// One of rskj's runs in `bridge/testdata/rskj_refunded_pegins.txt`. + #[derive(Default)] + struct RskjRun { + name: String, + /// Each account's address, nonce, balance and code. + accounts: Vec<(Address, u64, U256, Vec)>, + /// The storage cells it starts with: address, key, value. + stored: Vec<(Address, U256, Vec)>, + preroot: B256, + transactions: Vec<(rustock_core::Transaction, Address)>, + /// For each transaction: whether it succeeded, the gas it used, its logs. + results: Vec<(bool, u64, Vec)>, + /// The Bridge's storage after the transactions. + cells: std::collections::BTreeMap>, + root: B256, + } + + /// The runs a file of rskj's holds, whose header says what each line is. + fn rskj_runs(vectors: &str) -> Vec { + let bytes = |field: &str| { + if field == "-" { + Vec::new() + } else { + hex::decode(field).unwrap() + } + }; + let mut runs: Vec = Vec::new(); + for line in vectors.lines().filter(|line| !line.starts_with('#')) { + let fields: Vec<&str> = line.split(' ').collect(); + if fields[0] == "scenario" { + runs.push(RskjRun { + name: fields[1].into(), + ..RskjRun::default() + }); + continue; + } + let Some(run) = runs.last_mut() else { + continue; + }; + match fields[0] { + "account" => run.accounts.push(( + Address::from_slice(&bytes(fields[1])), + fields[2].parse().unwrap(), + U256::from_str_radix(fields[3], 16).unwrap(), + bytes(fields[4]), + )), + "store" => run.stored.push(( + Address::from_slice(&bytes(fields[1])), + U256::from_be_slice(&bytes(fields[2])), + bytes(fields[3]), + )), + "preroot" => run.preroot = B256::from_slice(&bytes(fields[1])), + "tx" => { + let tx = rustock_core::Transaction::from_rskj(&bytes(fields[1])).unwrap(); + assert_eq!( + alloy_primitives::keccak256(tx.rlp_for_trie()), + B256::from_slice(&bytes(fields[3])), + "{}: the transaction's hash", + run.name + ); + run.transactions + .push((tx, Address::from_slice(&bytes(fields[2])))); + } + "result" => { + assert_eq!(fields[1], "1", "{}: executed", run.name); + run.results + .push((fields[2] == "1", fields[3].parse().unwrap(), Vec::new())); + } + "log" => { + let topics = fields[2] + .split(',') + .filter(|topic| *topic != "-") + .map(|topic| B256::from_slice(&bytes(topic))) + .collect(); + let log = revm::primitives::Log::new_unchecked( + Address::from_slice(&bytes(fields[1])), + topics, + Bytes::from(bytes(fields[3])), + ); + run.results.last_mut().unwrap().2.push(log); + } + "cell" => { + run.cells + .insert(U256::from_be_slice(&bytes(fields[1])), bytes(fields[2])); + } + "root" => run.root = B256::from_slice(&bytes(fields[1])), + "itx" | "end" | "enc" => {} + other => panic!("an unknown line: {other}"), + } + } + runs + } + + /// Runs one of rskj's runs here and holds what comes of it to what came of + /// it in rskj: each transaction's outcome, logs and gas, the Bridge's + /// storage, and the state root. + fn replays_as_rskj_ran_it(run: &RskjRun) { + let store = Arc::new(MemoryTrieStore::new()); + let mut root = TrieNode::empty(); + for (address, nonce, balance, code) in &run.accounts { + root = put_account(&root, store.as_ref(), address, *nonce, *balance); + if !code.is_empty() { + let key = rustock_trie::code_key(address); + root = root.put(&TrieKeySlice::from_key(&key), code, store.as_ref()); + } + } + for (address, key, value) in &run.stored { + let key = rustock_trie::storage_key(address, &B256::from(*key)); + root = root.put(&TrieKeySlice::from_key(&key), value, store.as_ref()); + } + assert_eq!( + root.compute_hash(store.as_ref()), + run.preroot, + "{}: the state before", + run.name + ); + let block_store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let executor = RskExecutor::new(RskHardforkConfig::rskj_regtest(), block_store) + .with_bridge_constants(crate::bridge::constants::BridgeConstants::rskj_regtest()); + let header = Header { + difficulty: U256::from(1), + ..dummy_header(10) + }; + let result = executor + .execute_block(&header, &run.transactions, &root, store.clone()) + .expect("block"); + assert_eq!(result.tx_results.len(), run.results.len(), "{}", run.name); + for (index, (outcome, (success, gas_used, logs))) in + result.tx_results.iter().zip(&run.results).enumerate() + { + let what = format!("{}: transaction {index}", run.name); + assert_eq!(outcome.success, *success, "{what}"); + assert_eq!(outcome.logs.len(), logs.len(), "{what}: how many logs"); + for (at, (log, rskjs)) in outcome.logs.iter().zip(logs).enumerate() { + assert_eq!(log, rskjs, "{what}: log {at}"); + } + assert_eq!(outcome.gas_used, *gas_used, "{what}: gas"); + } + // The Bridge's storage: what it started with, and over it what the + // transactions wrote, in order. + let bridge = crate::precompiles::BRIDGE_ADDR; + let started_with = run + .stored + .iter() + .cloned() + .map(|(address, key, value)| (address, key, Some(value))); + let wrote = result.markers.raw_storage.iter().cloned(); + let mut cells = std::collections::BTreeMap::new(); + for (_, key, value) in started_with + .chain(wrote) + .filter(|(address, _, _)| *address == bridge) + { + match value { + Some(value) if !value.is_empty() => cells.insert(key, value), + _ => cells.remove(&key), + }; + } + let keys: std::collections::BTreeSet<&U256> = + cells.keys().chain(run.cells.keys()).collect(); + for key in keys { + assert_eq!( + cells.get(key).map(hex::encode), + run.cells.get(key).map(hex::encode), + "{}: the Bridge's cell {key:#066x}", + run.name + ); + } + let after = crate::state::apply_state_changes( + &root, + store.as_ref(), + &result.state_changes, + &result.markers, + ); + assert_eq!( + after.compute_hash(store.as_ref()), + run.root, + "{}: the state after", + run.name + ); + } + + /// What rskj's release does with a peg-in it refunds, read from its own + /// `TransactionExecutor` (the testdata's header says how). A member of the + /// federation hands the Bridge six Bitcoin headers, and then a transaction + /// registers a payment to the federation from a multisig, in the second of + /// them. + /// + /// rskj refunds the payment, with a release it builds as it builds every + /// other (`ReleaseTransactionBuilder`): for a P2SH-P2WSH federation, as + /// regtest's is and mainnet's since its first change after RSKIP305, with + /// the witness program in each input's scriptSig, the unsigned witness, and + /// the fee taken over the virtual size. The fee is no less than bitcoinj's + /// minimum, which the second run's fee per kilobyte takes it down to + /// (`Wallet.adjustOutputDownwardsForFee`). And rskj ends the refund's + /// settling as it ends every release's (`settleReleaseRejection`, + /// `processReleaseTransactionInfo`): it logs `pegout_transaction_created` + /// after `release_requested` (RSKIP428) and stores the values of what the + /// refund spends (RSKIP305). + /// + /// The last run registers a flyover payment over the locking cap, through + /// the contract that stands for the liquidity bridge. rskj refunds that one + /// the same way, spending the output that pays the federation's flyover + /// address, which for a P2SH-P2WSH federation is the P2SH of the flyover + /// script's witness program. It names that refund by the hash of the + /// block's transaction, not of the contract's call (`getOriginHash`). + #[test] + fn a_refunded_peg_in_is_registered_as_rskj_registers_it() { + let runs = rskj_runs(include_str!("bridge/testdata/rskj_refunded_pegins.txt")); + let names: Vec<&str> = runs.iter().map(|run| run.name.as_str()).collect(); + assert_eq!( + names, + ["direct", "relay", "nested", "delegate", "low-fee", "flyover"] + ); + replays_as_rskj_ran_it(&runs[0]); + replays_as_rskj_ran_it(&runs[4]); + replays_as_rskj_ran_it(&runs[5]); + } } From 18eea1954278638764c8e95f65f7f32191964664 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Thu, 12 Nov 2026 21:10:12 -0800 Subject: [PATCH 121/264] bridge: a segwit release is sized before its inputs are filled in bitcoinj checks a release's size against its 100,000 bytes where Wallet.completeTx ends. For a legacy federation the inputs are signed by then, with their placeholders, and count. For a P2SH-P2WSH federation ReleaseTransactionBuilder turns signInputs off, so the inputs are still empty, and it fills them in after completeTx has returned (signSegwitTxInputs) without checking again. The four builders here checked every release after filling it in, witnesses included, so a P2SH-P2WSH federation's release with a few hundred inputs was not built where rskj builds it. A refund is the one anyone can bring there: a payment the Bridge turns down, made in a few hundred outputs to the federation. rskj refunds it, and here it was left without its refund. The four now share fill_inputs, which checks the size where bitcoinj does. Tests: a_release_is_sized_where_bitcoinj_sizes_it, for a pegout, a release whose change pays the fee, and a refund; and the replay of rskj's own refund of a payment in 330 outputs, which the testdata gains (many-outputs; its stored release is 110,308 bytes, and the file holds that cell by its hash). --- crates/execution/src/bridge/peg.rs | 21 +-- crates/execution/src/bridge/release_tx.rs | 138 +++++++++++++----- .../bridge/testdata/rskj_refunded_pegins.txt | 44 +++++- crates/execution/src/executor.rs | 51 +++++-- 4 files changed, 191 insertions(+), 63 deletions(-) diff --git a/crates/execution/src/bridge/peg.rs b/crates/execution/src/bridge/peg.rs index 899ffd2..b2be622 100644 --- a/crates/execution/src/bridge/peg.rs +++ b/crates/execution/src/bridge/peg.rs @@ -2656,8 +2656,8 @@ fn emit_flyover_rejection_release( /// `feePerKb * size / 1000`, no less than the reference minimum, where `size` /// is the empty-scriptSig serialized length plus, per input, `threshold * /// SIG_SIZE + redeemScript.len()`, segwit-weighted for a P2SH-P2WSH federation -/// (`is_segwit`), whose inputs carry their witnesses. The final -/// placeholder-filled tx must not exceed MAX_STANDARD_TX_SIZE. +/// (`is_segwit`), whose inputs carry their witnesses. The size limit is +/// `release_tx::fill_inputs`'s. fn build_flyover_empty_wallet_multi( utxos: &[BridgeUtxo], input_redeems: &[&[u8]], @@ -2669,11 +2669,9 @@ fn build_flyover_empty_wallet_multi( use bitcoin::{ absolute::LockTime, transaction::Version, Amount, OutPoint, Sequence, TxIn, TxOut, Witness, }; - // bitcoinj constants (see release_tx.rs): SIG_SIZE=75, min fee 5000 sat/kB, - // MAX_STANDARD_TX_SIZE=100k. + // bitcoinj constants (see release_tx.rs): SIG_SIZE=75, min fee 5000 sat/kB. const SIG_SIZE: usize = 75; const REFERENCE_DEFAULT_MIN_TX_FEE: u64 = 5000; - const MAX_STANDARD_TX_SIZE: usize = 100_000; if utxos.is_empty() { return None; @@ -2741,18 +2739,7 @@ fn build_flyover_empty_wallet_multi( } tx.output[0].value = Amount::from_sat(value); - for (input, (redeem, threshold)) in tx.input.iter_mut().zip(input_redeems.iter().zip(&thresholds)) { - if is_segwit { - let (script_sig, witness) = super::release_tx::segwit_base_input(redeem, *threshold); - input.script_sig = script_sig; - input.witness = witness; - } else { - input.script_sig = bitcoin::ScriptBuf::from_bytes( - super::release_tx::placeholder_scriptsig(redeem, *threshold), - ); - } - } - if bitcoin::consensus::serialize(&tx).len() > MAX_STANDARD_TX_SIZE { + if !super::release_tx::fill_inputs(&mut tx, input_redeems.iter().copied(), is_segwit) { return None; } Some(super::release_tx::BuiltPegout { tx, used_utxos: utxos.to_vec() }) diff --git a/crates/execution/src/bridge/release_tx.rs b/crates/execution/src/bridge/release_tx.rs index 57c0982..bfb5077 100644 --- a/crates/execution/src/bridge/release_tx.rs +++ b/crates/execution/src/bridge/release_tx.rs @@ -20,8 +20,10 @@ //! - scriptSigs: `MissingSigsMode.USE_OP_ZERO` placeholders, //! `OP_0 ` per input. //! - Size: fee sizing uses the empty-scriptSig serialization plus -//! `threshold * SIG_SIZE(75) + redeemScript.len()` per input; the final -//! placeholder-filled tx must not exceed `MAX_STANDARD_TX_SIZE` (100k). +//! `threshold * SIG_SIZE(75) + redeemScript.len()` per input; the tx must +//! not exceed `MAX_STANDARD_TX_SIZE` (100k) where `completeTx` checks it, +//! which is with its placeholders for a legacy federation and with its +//! inputs still empty for a P2SH-P2WSH one (`fill_inputs`). use bitcoin::absolute::LockTime; use bitcoin::transaction::Version; @@ -125,6 +127,37 @@ pub fn segwit_base_input(redeem: &[u8], threshold: usize) -> (ScriptBuf, Witness (ScriptBuf::from_bytes(script_sig), witness) } +/// Fills each input in as rskj leaves it, from that input's redeem script, and +/// answers whether the transaction is within bitcoinj's size where +/// `Wallet.completeTx` checks it. A legacy federation's inputs are signed by +/// then, with their placeholders (`MissingSigsMode.USE_OP_ZERO`), so those +/// count. A P2SH-P2WSH federation's are still empty, as `setSrConfiguration` +/// turns `signInputs` off for it, and rskj fills them in once `completeTx` has +/// returned (`ReleaseTransactionBuilder.signSegwitTxInputs`) and checks +/// nothing again: such a transaction may well come out over the size. +pub(super) fn fill_inputs<'r>( + tx: &mut BtcTransaction, + redeems: impl IntoIterator, + is_segwit: bool, +) -> bool { + let within = + |tx: &BtcTransaction| bitcoin::consensus::serialize(tx).len() <= MAX_STANDARD_TX_SIZE; + if is_segwit && !within(tx) { + return false; + } + for (input, redeem) in tx.input.iter_mut().zip(redeems) { + let threshold = redeem_script_threshold(redeem); + if is_segwit { + let (script_sig, witness) = segwit_base_input(redeem, threshold); + input.script_sig = script_sig; + input.witness = witness; + } else { + input.script_sig = ScriptBuf::from_bytes(placeholder_scriptsig(redeem, threshold)); + } + } + is_segwit || within(tx) +} + /// Append a minimal data push to a script (bitcoinj `ScriptBuilder.data`). pub fn push_data(script: &mut Vec, data: &[u8]) { let len = data.len(); @@ -319,17 +352,11 @@ where // the witness-program scriptSig + base witness; a legacy input the // USE_OP_ZERO placeholder scriptSig (per-input redeem, so a flyover // input embeds the flyover redeem). - for (input, (_, redeem)) in tx.input.iter_mut().zip(&selected) { - let threshold = redeem_script_threshold(redeem); - if is_segwit { - let (script_sig, witness) = segwit_base_input(redeem, threshold); - input.script_sig = script_sig; - input.witness = witness; - } else { - input.script_sig = ScriptBuf::from_bytes(placeholder_scriptsig(redeem, threshold)); - } - } - if bitcoin::consensus::serialize(&tx).len() > MAX_STANDARD_TX_SIZE { + if !fill_inputs( + &mut tx, + selected.iter().map(|(_, redeem)| *redeem), + is_segwit, + ) { return None; } let used_utxos = selected.into_iter().map(|(u, _)| u.clone()).collect(); @@ -450,18 +477,11 @@ where let fee_needed = fee_rate * size as u64 / 1000; if fee >= fee_needed { - for (input, (_, redeem)) in tx.input.iter_mut().zip(&selected) { - let threshold = redeem_script_threshold(redeem); - if is_segwit { - let (script_sig, witness) = segwit_base_input(redeem, threshold); - input.script_sig = script_sig; - input.witness = witness; - } else { - input.script_sig = - ScriptBuf::from_bytes(placeholder_scriptsig(redeem, threshold)); - } - } - if bitcoin::consensus::serialize(&tx).len() > MAX_STANDARD_TX_SIZE { + if !fill_inputs( + &mut tx, + selected.iter().map(|(_, redeem)| *redeem), + is_segwit, + ) { return None; } let used_utxos = selected.into_iter().map(|(u, _)| u.clone()).collect(); @@ -546,17 +566,7 @@ pub fn build_empty_wallet_to( } tx.output[0].value = Amount::from_sat(value); - for input in &mut tx.input { - if is_segwit { - let (script_sig, witness) = segwit_base_input(redeem_script, threshold); - input.script_sig = script_sig; - input.witness = witness; - } else { - input.script_sig = - ScriptBuf::from_bytes(placeholder_scriptsig(redeem_script, threshold)); - } - } - if bitcoin::consensus::serialize(&tx).len() > MAX_STANDARD_TX_SIZE { + if !fill_inputs(&mut tx, std::iter::repeat(redeem_script), is_segwit) { return None; } Some(BuiltPegout { tx, used_utxos: utxos.to_vec() }) @@ -1443,6 +1453,62 @@ mod tests { .is_none()); } + /// bitcoinj checks a release's size where `Wallet.completeTx` ends. A legacy + /// federation's inputs hold their placeholders by then, and count. A + /// P2SH-P2WSH federation's are still empty, and rskj fills them in + /// afterwards without checking again. So 700 inputs make no release for the + /// one, which they take over 100,000 bytes, and for the other a release + /// that is well over them once it is filled in: + /// `testdata/rskj_refunded_pegins.txt` holds one that rskj's release built. + #[test] + fn a_release_is_sized_where_bitcoinj_sizes_it() { + let utxos: Vec = (0..800).map(|vout| utxo(1, vout, 1_000_000)).collect(); + let redeem = erp_redeem(2, 3); + let filled = |built: &BuiltPegout| bitcoin::consensus::serialize(&built.tx).len(); + let to = |script: ScriptBuf| { + [PegoutOutput { + script, + amount_satoshis: 700_000_000, + }] + }; + + // A pegout, whose recipients pay the fee. + let pegout = |segwit| { + let redeem_for = |_: &BridgeUtxo| redeem.as_slice(); + complete_pegout_tx(&utxos, &to(p2pkh(7)), &p2sh(8), redeem_for, 5000, 2, segwit) + }; + assert!(pegout(false).is_none()); + let built = pegout(true).expect("a segwit pegout"); + assert_eq!(built.tx.input.len(), 700); + assert!(filled(&built) > MAX_STANDARD_TX_SIZE); + + // A release whose change pays the fee. + let fund = |segwit| { + let redeem_for = |_: &BridgeUtxo| redeem.as_slice(); + complete_recipients_dont_pay_fees_tx( + &utxos, + &to(p2sh(7)), + &p2sh(8), + redeem_for, + 5000, + 2, + segwit, + ) + }; + assert!(fund(false).is_none()); + let built = fund(true).expect("a segwit release"); + assert_eq!(built.tx.input.len(), 701); + assert!(filled(&built) > MAX_STANDARD_TX_SIZE); + + // A refund, which empties what it is given. + let refund = + |segwit| build_empty_wallet_to(&utxos[..700], &p2pkh(7), &redeem, 5000, 2, segwit); + assert!(refund(false).is_none()); + let built = refund(true).expect("a segwit refund"); + assert_eq!(built.tx.input.len(), 700); + assert!(filled(&built) > MAX_STANDARD_TX_SIZE); + } + #[test] fn dusty_change_raised_from_first_recipient() { // Gathered 103_000 vs target 100_000 leaves change 3_000 > 0 but diff --git a/crates/execution/src/bridge/testdata/rskj_refunded_pegins.txt b/crates/execution/src/bridge/testdata/rskj_refunded_pegins.txt index 239659f..e087f3a 100644 --- a/crates/execution/src/bridge/testdata/rskj_refunded_pegins.txt +++ b/crates/execution/src/bridge/testdata/rskj_refunded_pegins.txt @@ -7,8 +7,11 @@ # federation from a two-of-three multisig, which the second header holds. The # Bridge starts with an empty list of its federation's UTXOs, and in the scenario low-fee with # a fee per kilobyte of 10,000 satoshis, under which the refund's fee is bitcoinj's minimum. -# The last scenario registers instead, through the contract that stands for the liquidity +# The scenario flyover registers instead, through the contract that stands for the liquidity # bridge, a flyover payment over the locking cap, which the Bridge refunds to its user. +# The scenario many-outputs registers a payment to the federation in 330 outputs, whose +# refund is under bitcoinj's 100,000 bytes while its inputs are empty and over them once +# rskj has filled them in. # Lines: # block number, gas limit, timestamp, coinbase, difficulty # account address, nonce, balance, code @@ -21,6 +24,8 @@ # nonce bytes, gas price, gas limit bytes, data, parent hash, whether it ended # rejected, hash # cell the Bridge's storage after both: key, value +# cellhash one of those cells that holds more than 4,096 bytes: key, the keccak-256 of its +# value, the value's length # root the state root after both # enc an internal transaction built by itself: nonce, gas price, gas limit, sender, # receiver, value, data, note, deep, index, parent hash, hash @@ -272,6 +277,43 @@ cell eb0e24421403c126bc4f1b5efcf8a11f79c70bfcce1220cde462b963864241da 01 cell fa504d03ff80daf373e6ce2920ce39ca86dae5077fa09b447cfcb6f0df5835b9 a04f06cafd30dc74e15b547cff33874b6b57ee423d6f0297f8b7ca563b6491cb60 root ac10b69db292a42bc6b120945108f346aa0d4a2fb043d990d9e6544c0442e1c1 end +# peg-in in 330 outputs 43141a6e810e2082504476cca8fad570c0a9c786fe6b4f9ec887407c21759583 +scenario many-outputs +account 85845342b0d5260d516d6da5280a7df9a96d1925 0 de0b6b3a7640000 - +account d06da76e0b65671fc4639b624338464ed7b52bbd 0 de0b6b3a7640000 - +account 0000000000000000000000000000000001000006 0 115eec47f6cf7e35000000 - +store 0000000000000000000000000000000001000006 00000000000000000000006e657746656465726174696f6e4274635554584f73 c0 +preroot e33796914989af2dca2ab61b5cb87a58b478d6c0b0a115c00c00d7d547c140d8 +tx f904668000831e848094000000000000000000000000000000000100000680b90404e5400e7b0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000001c0000000000000000000000000000000000000000000000000000000000000024000000000000000000000000000000000000000000000000000000000000002c0000000000000000000000000000000000000000000000000000000000000034000000000000000000000000000000000000000000000000000000000000000500100000006226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f20000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000500100000060cb91643b56cab7f897026f3d42ee576b4b8733ff7c545be174dc30fdca064f839575217c4087c89e4f6bfe86c7a9c070d5faa8cc76445082200e816e1a144300f15365ffff7f2002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000050010000002da208530c1e69e837a80e8660e3758c4ee956791422348a5e208b6cdf137b74111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f2002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000050010000001757e3004fa4a03623d071d505711e2056d1a25471268255d75b86a101d84e6a111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f200200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005001000000b804ce2e1f1dffdd77a94649d45e940b06b900a72f7581a1b6246370c88a0a0c111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f20010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000500100000002229cb1c0aeb2e9b217927796c75aa258cc8a31a853ef6fd9612ff2b7299c3f111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f20000000000000000000000000000000000000000066a070fec5f4bea23720e9925d95338dcd3a827ac1350966ca2be46599b89c2ad96ba01b80a23276df9fbfc0fdff12046924c398eac0082317044d3ed78a6a39799024 85845342b0d5260d516d6da5280a7df9a96d1925 eb8299167cc3799ab3410a9ba210a32c669163eb05566a75521f42329a079969 +result 1 1 0 +tx f92bc68000832dc6c094000000000000000000000000000000000100000680b92b6443dc0656000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000002b000000000000000000000000000000000000000000000000000000000000002a740100000001242424242424242424242424242424242424242424242424242424242424242400000000fdfd0000473030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030483131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131314c69522102141481bf1181ed61aa025f1fe708f68cb018c2c9d6eb719ccd94b3f6ff615308210222ca50390e660f601036dd7502bb973bdcd104b5dcb8f2e74de8edc6c292b03a2103cfc18f02cc004640f2116fdd1f6ca2022e39be25df75e27c80bde842e6b6f93853aefffffffffd4a0140420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c78740420f000000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c7870000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000270100000001839575217c4087c89e4f6bfe86c7a9c070d5faa8cc76445082200e816e1a144301010000000000000000000000000000000000000000000000000065a0b6da710a3148d6a723b7f0eab18b1e31285f7fc3f5a554dab8940a9c7219b26da069d3dc9bee069a31d275ae67151081c8b969510d0e6516e773182cb26eef0f08 d06da76e0b65671fc4639b624338464ed7b52bbd c4988c97bad4143c8c270a10bc59bb4264d617f236cf401c9692b486c0c3904d +result 1 1 220648 +log 0000000000000000000000000000000001000006 708ce1ead20561c5894a93be3fee64b326b2ad6c198f8253e4bb56f1626053d6,43141a6e810e2082504476cca8fad570c0a9c786fe6b4f9ec887407c21759583 0000000000000000000000000000000000000000000000000000000000000002 +log 0000000000000000000000000000000001000006 7a7c29481528ac8c2b2e93aee658fddd4dc15304fa723a5c2b88514557bcc790,c4988c97bad4143c8c270a10bc59bb4264d617f236cf401c9692b486c0c3904d,5ab80a05531997a64e2cbe49a229b75ed606b7241ce23f1e8e8f36d1c3ec6f70 0000000000000000000000000000000000000000000000000000000013ab6680 +log 0000000000000000000000000000000001000006 9ee5d520fd5e6eaea3fd2e3ae4e35e9a9c0fb05c9d8f84b507f287da84b5117c,5ab80a05531997a64e2cbe49a229b75ed606b7241ce23f1e8e8f36d1c3ec6f70 00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000672fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f000000000000000000000000000000 +cell 0000000000000000000000000000000000000062746354784861736865734150 c0 +cell 00000000000000000000000000626c6f636b53746f7265436861696e48656164 000000000000000000000000000000000000000000000000000000000000000e000000060100000002229cb1c0aeb2e9b217927796c75aa258cc8a31a853ef6fd9612ff2b7299c3f111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f2000000000 +cell 00000000000000000000006e657746656465726174696f6e4274635554584f73 c0 +cell 000000000000000000000072656c656173655472616e73616374696f6e536574 c0 +cellhash 0072656c656173655472616e73616374696f6e53657457697468547848617368 2bc0fe657acd491cc2d4e45e5d1a86957032b240f21f403cb204eb585371fdc9 110308 +cell 04e7879f9f786d37bfb9da99ca9ea71f66a545ddf08f8026e18a14c4203f80cd a00f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206 +cell 0c0a8ac8706324b6a181752fa700b9060b945ed44946a977ddff1d1f2ece04b8 000000000000000000000000000000000000000000000000000000000000000a00000004010000001757e3004fa4a03623d071d505711e2056d1a25471268255d75b86a101d84e6a111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f2002000000 +cell 0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206 0000000000000000000000000000000000000000000000000000000000000002000000000100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4adae5494dffff7f2002000000 +cell 34fa9348faf7dca06d41a200c85ef02b02ab03916a70c8a65b6e3fd84dbe0bde a06a4ed801a1865bd75582267154a2d156201e7105d571d02336a0a44f00e35717 +cell 3f9c29b7f22f61d96fef53a8318acc58a25ac796779217b2e9b2aec0b19c2202 000000000000000000000000000000000000000000000000000000000000000c0000000501000000b804ce2e1f1dffdd77a94649d45e940b06b900a72f7581a1b6246370c88a0a0c111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f2001000000 +cell 3fee5096e9507962151178d120a30ccdaa2cecc95a10a749a0b540797b21e45b a03f9c29b7f22f61d96fef53a8318acc58a25ac796779217b2e9b2aec0b19c2202 +cell 4f06cafd30dc74e15b547cff33874b6b57ee423d6f0297f8b7ca563b6491cb60 0000000000000000000000000000000000000000000000000000000000000004000000010100000006226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f2000000000 +cell 4f10a4328ecbcec2a52d33bc2802fe7a8f8363b0dbcbd5fc9e015af830c8144d fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00fe40420f00 +cell 62979aa6ab22a67b1abee5022830e036bd87b8881abdaa2c57ee24216e26d93c a0747b13df6c8b205e8a3422147956e94e8c75e360860ea837e8691e0c5308a22d +cell 6852a70e9053f3fa0860eab961f8e489ca9651d5f419f310f6927fdc919bcc96 000000000000000000000000000000000000000000000000000000000000000e000000060100000002229cb1c0aeb2e9b217927796c75aa258cc8a31a853ef6fd9612ff2b7299c3f111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f2000000000 +cell 6a4ed801a1865bd75582267154a2d156201e7105d571d02336a0a44f00e35717 000000000000000000000000000000000000000000000000000000000000000800000003010000002da208530c1e69e837a80e8660e3758c4ee956791422348a5e208b6cdf137b74111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f2002000000 +cell 747b13df6c8b205e8a3422147956e94e8c75e360860ea837e8691e0c5308a22d 0000000000000000000000000000000000000000000000000000000000000006000000020100000060cb91643b56cab7f897026f3d42ee576b4b8733ff7c545be174dc30fdca064f839575217c4087c89e4f6bfe86c7a9c070d5faa8cc76445082200e816e1a144300f15365ffff7f2002000000 +cell a0b1dbff8c310a5e983e6e603a69b3ee024fe8b229dfca1e0b7f0d41c4768592 a00c0a8ac8706324b6a181752fa700b9060b945ed44946a977ddff1d1f2ece04b8 +cell ba0616c97174c27e053a97d701f34b19d903eeeaed0d9d98f2dc0892ab3608d9 0a +cell cf606e0bbdd62c72533bdc45ae2e61a2b5fa7f5251ec5a0d7e99c6022191a680 a06852a70e9053f3fa0860eab961f8e489ca9651d5f419f310f6927fdc919bcc96 +cell fa504d03ff80daf373e6ce2920ce39ca86dae5077fa09b447cfcb6f0df5835b9 a04f06cafd30dc74e15b547cff33874b6b57ee423d6f0297f8b7ca563b6491cb60 +root b677178f5b5129390d97374df19c5e2b75a715165e6fd70b2f207b5f3717d5bf +end enc 0 1 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 0 - call 0 0 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 f64ee69aab5fd5b648c2ca017b0880b610223c0ae21d4089905212c9ffaa6992 enc 1 1 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 0 - call 0 0 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 bf55a8a523265031c3a9def117bf2d91f605409b862dbb0c20982d12e1110a01 enc 127 1 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 0 - call 0 0 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 438a42134addf73ea10411211e55d1b0436dd83321eea6957a9251d36ed94d4a diff --git a/crates/execution/src/executor.rs b/crates/execution/src/executor.rs index 2c0b7a7..2794a56 100644 --- a/crates/execution/src/executor.rs +++ b/crates/execution/src/executor.rs @@ -14622,6 +14622,9 @@ bf09f6e52420834e8e0e0b1a6df563aba550cf7f99e9724264187c45dcf3d73e8585a0e35196\ results: Vec<(bool, u64, Vec)>, /// The Bridge's storage after the transactions. cells: std::collections::BTreeMap>, + /// Those of its cells that hold more than 4,096 bytes, each by the + /// hash and the length of what it holds. + long_cells: std::collections::BTreeMap, root: B256, } @@ -14693,6 +14696,15 @@ bf09f6e52420834e8e0e0b1a6df563aba550cf7f99e9724264187c45dcf3d73e8585a0e35196\ run.cells .insert(U256::from_be_slice(&bytes(fields[1])), bytes(fields[2])); } + "cellhash" => { + run.long_cells.insert( + U256::from_be_slice(&bytes(fields[1])), + ( + B256::from_slice(&bytes(fields[2])), + fields[3].parse().unwrap(), + ), + ); + } "root" => run.root = B256::from_slice(&bytes(fields[1])), "itx" | "end" | "enc" => {} other => panic!("an unknown line: {other}"), @@ -14765,15 +14777,27 @@ bf09f6e52420834e8e0e0b1a6df563aba550cf7f99e9724264187c45dcf3d73e8585a0e35196\ _ => cells.remove(&key), }; } - let keys: std::collections::BTreeSet<&U256> = - cells.keys().chain(run.cells.keys()).collect(); + let keys: std::collections::BTreeSet<&U256> = cells + .keys() + .chain(run.cells.keys()) + .chain(run.long_cells.keys()) + .collect(); for key in keys { - assert_eq!( - cells.get(key).map(hex::encode), - run.cells.get(key).map(hex::encode), - "{}: the Bridge's cell {key:#066x}", - run.name - ); + let what = format!("{}: the Bridge's cell {key:#066x}", run.name); + match run.long_cells.get(key) { + // A long cell, which the file holds by its hash and its length. + Some(rskjs) => { + let ours = cells + .get(key) + .map(|value| (alloy_primitives::keccak256(value), value.len())); + assert_eq!(ours.as_ref(), Some(rskjs), "{what}"); + } + None => assert_eq!( + cells.get(key).map(hex::encode), + run.cells.get(key).map(hex::encode), + "{what}" + ), + } } let after = crate::state::apply_state_changes( &root, @@ -14819,10 +14843,19 @@ bf09f6e52420834e8e0e0b1a6df563aba550cf7f99e9724264187c45dcf3d73e8585a0e35196\ let names: Vec<&str> = runs.iter().map(|run| run.name.as_str()).collect(); assert_eq!( names, - ["direct", "relay", "nested", "delegate", "low-fee", "flyover"] + [ + "direct", + "relay", + "nested", + "delegate", + "low-fee", + "flyover", + "many-outputs" + ] ); replays_as_rskj_ran_it(&runs[0]); replays_as_rskj_ran_it(&runs[4]); replays_as_rskj_ran_it(&runs[5]); + replays_as_rskj_ran_it(&runs[6]); } } From 0bf20ce690c4d1ef467e2b89e492f806cfbe1f84 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Thu, 12 Nov 2026 21:10:12 -0800 Subject: [PATCH 122/264] execution: SELFDESTRUCT with no balance leaves the beneficiary alone rskj's Program.suicide transfers the destroyed contract's balance only when it is not zero. With nothing to hand over, the beneficiary is not touched: one that did not exist still does not, although VM.doSUICIDE has charged the 25,000 for a new account, for which it asks isExist and nothing more. revm's journal touches the beneficiary whatever the balance, and under RSK's account rules a touch creates an account that was not there. So a contract holding nothing, destroying itself and naming a new address, left an empty account here that rskj's state does not have, and the state roots differed. Anyone can deploy such a contract. The instruction now takes that touch back where the destruction gave it and there was no balance: the account's flag, and the journal's entry. The entry goes because the sublists' write sets are read from the journal, and rskj only reads the beneficiary here, so a sublist that does this and one that reads the same account do not collide. Whatever else touches the beneficiary, before the destruction or after it, still creates it. Tests: selfdestruct_with_no_balance_leaves_the_beneficiary_as_it_was (the beneficiary stays absent; a balance handed over, and a call for nothing before the destruction or after it, still create it), and a_selfdestruct_with_no_balance_only_reads_its_beneficiary. --- crates/execution/src/executor.rs | 173 +++++++++++++++++++++++ crates/execution/src/rsk_instructions.rs | 87 ++++++++++-- 2 files changed, 252 insertions(+), 8 deletions(-) diff --git a/crates/execution/src/executor.rs b/crates/execution/src/executor.rs index 2794a56..0072624 100644 --- a/crates/execution/src/executor.rs +++ b/crates/execution/src/executor.rs @@ -8121,6 +8121,80 @@ mod tests { ); } + /// A contract that holds nothing and destroys itself only reads the + /// account of the beneficiary it names: rskj `VM.doSUICIDE` asks `isExist` + /// for the gas, and `Program.suicide` then has no balance to transfer. So + /// a sublist that does this and another that reads the same account do + /// not collide. revm journals a touch of the beneficiary, which for an + /// account that does not exist would count as writing it. + #[test] + fn a_selfdestruct_with_no_balance_only_reads_its_beneficiary() { + let store: Arc = Arc::new(MemoryTrieStore::new()); + let block_store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let beneficiary = Address::repeat_byte(0xB1); + let [dies, reads] = [0xC1, 0xC2].map(Address::repeat_byte); + let senders = [0x11, 0x12, 0x13].map(Address::repeat_byte); + + let rbtc = U256::from(10u64).pow(U256::from(18)); + let root = senders.iter().fold(TrieNode::empty(), |root, sender| { + put_account(&root, store.as_ref(), sender, 0, rbtc) + }); + // PUSH20 beneficiary; SELFDESTRUCT + let mut code = vec![0x73]; + code.extend_from_slice(beneficiary.as_slice()); + code.push(0xFF); + let root = seed_contract(&root, store.as_ref(), &dies, U256::ZERO, &code); + // PUSH20 beneficiary; BALANCE; POP + let mut code = vec![0x73]; + code.extend_from_slice(beneficiary.as_slice()); + code.extend_from_slice(&[0x31, 0x50]); + let root = seed_contract(&root, store.as_ref(), &reads, U256::ZERO, &code); + + let mut header = dummy_header(8_000_000); // testnet reed810 is 7,139,600 + header.gas_limit = U256::from(12_000_000); + header.version_fields = Some(rustock_core::types::header::HeaderVersionFields { + version: 1, + edges: Some(vec![1, 2]), + base_event: None, + }); + + let tx = |to: &Address| rustock_core::Transaction { + nonce: 0, + gas_price: U256::from(1_000), + gas_limit: U256::from(100_000), + to: Bytes::copy_from_slice(to.as_slice()), + value: U256::ZERO, + input: Bytes::new(), + v: 0, + r: U256::ZERO, + s: U256::ZERO, + cached_rlp: None, + }; + // One sublist destroys, the other reads, and the last transaction is + // the sequential set's. + let transactions = vec![ + (tx(&dies), senders[0]), + (tx(&reads), senders[1]), + (tx(&Address::repeat_byte(0xA3)), senders[2]), + ]; + + crate::collision::start( + &[1i16, 2], + &[], + crate::collision::write_semantics(header.number), + ); + let executor = + RskExecutor::new(RskHardforkConfig::testnet(), block_store).with_execution_threads(2); + let result = executor.execute_block(&header, &transactions, &root, store); + let collision = crate::collision::finish() + .expect("recording was started") + .first_collision() + .map(|collision| (collision.kind, collision.key)); + + assert!(result.expect("block").tx_results.iter().all(|t| t.success)); + assert_eq!(collision, None, "both sublists only read the beneficiary"); + } + /// Transfers of 1,000 wei, each from a sender of its own with the gas /// limit given, in a testnet block above reed810 -- so one that runs as /// its sublists -- with `block_gas` and `edges`. @@ -14235,6 +14309,105 @@ bf09f6e52420834e8e0e0b1a6df563aba550cf7f99e9724264187c45dcf3d73e8585a0e35196\ assert_eq!(sender_acct.balance, one_rbtc, "burned balance goes nowhere"); } + /// rskj `Program.suicide` transfers only a balance that is not zero + /// (`if (!balance.equals(Coin.ZERO))`), so a contract that holds nothing + /// leaves the beneficiary it names as it was: one that did not exist still + /// does not, though `VM.doSUICIDE` charged the 25,000 for it. revm touches + /// the beneficiary whatever the balance, and under RSK's frontier account + /// rules a touch creates the account. What else touches it still creates + /// it: a balance handed over, and a call for nothing (`addBalance` of + /// zero), whether before the destruction or after it. + #[test] + fn selfdestruct_with_no_balance_leaves_the_beneficiary_as_it_was() { + let store = Arc::new(MemoryTrieStore::new()); + let sender = Address::repeat_byte(0xAA); + let one_rbtc = U256::from(10u64).pow(U256::from(18)); + + // PUSH20 to; SELFDESTRUCT + let destroys = |to: &Address| { + let mut code = vec![0x73]; + code.extend_from_slice(to.as_slice()); + code.push(0xFF); + code + }; + // CALL(GAS, to, 0, 0, 0, 0, 0); POP + let calls = |to: &Address| { + let mut code = vec![ + 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x73, + ]; + code.extend_from_slice(to.as_slice()); + code.extend_from_slice(&[0x5A, 0xF1, 0x50]); + code + }; + + // The beneficiaries, none of which exists, and the contracts. + let [untouched, paid, called_before, called_after] = + [0xB1, 0xB2, 0xB3, 0xB4].map(Address::repeat_byte); + let [empty, funded, calls_first, destroyed_inside, calls_last] = + [0xC1, 0xC2, 0xC3, 0xC4, 0xC5].map(Address::repeat_byte); + + let root = put_account(&TrieNode::empty(), store.as_ref(), &sender, 0, one_rbtc); + let contracts = [ + (empty, U256::ZERO, destroys(&untouched)), + (funded, U256::from(1_000), destroys(&paid)), + ( + calls_first, + U256::ZERO, + [calls(&called_before), destroys(&called_before)].concat(), + ), + (destroyed_inside, U256::ZERO, destroys(&called_after)), + ( + calls_last, + U256::ZERO, + [calls(&destroyed_inside), calls(&called_after)].concat(), + ), + ]; + let root = contracts.iter().fold(root, |root, (addr, balance, code)| { + seed_contract(&root, store.as_ref(), addr, *balance, code) + }); + + let block_store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); + let txs: Vec<_> = [empty, funded, calls_first, calls_last] + .iter() + .enumerate() + .map(|(nonce, to)| (plain_tx(nonce as u64, to, U256::ZERO), sender)) + .collect(); + let r = executor + .execute_block(&dummy_header(3_173_807), &txs, &root, store.clone()) + .expect("block"); + assert!(r.tx_results.iter().all(|t| t.success)); + + let new_root = + crate::state::apply_state_changes(&root, store.as_ref(), &r.state_changes, &r.markers); + let account = |addr: &Address| { + trie_account(&new_root, store.as_ref(), addr).map(|a| (a.nonce, a.balance)) + }; + for gone in [empty, funded, calls_first, destroyed_inside] { + assert_eq!(account(&gone), None, "{gone} destroyed itself"); + } + assert_eq!( + account(&untouched), + None, + "nothing was handed over, so nothing made the beneficiary" + ); + assert_eq!( + account(&paid), + Some((U256::ZERO, U256::from(1_000))), + "a balance handed over makes the beneficiary" + ); + assert_eq!( + account(&called_before), + Some((U256::ZERO, U256::ZERO)), + "the call before the destruction made it, and it stays" + ); + assert_eq!( + account(&called_after), + Some((U256::ZERO, U256::ZERO)), + "the call after the destruction makes it" + ); + } + /// RSKIP-131: from RSKIP125 rskj's `Program.createContract` refuses a /// creation at an address an earlier transaction of the block deleted: /// the `deletedAccounts` that `TransactionExecutor` hands every program diff --git a/crates/execution/src/rsk_instructions.rs b/crates/execution/src/rsk_instructions.rs index bf7870a..ec6981f 100644 --- a/crates/execution/src/rsk_instructions.rs +++ b/crates/execution/src/rsk_instructions.rs @@ -187,7 +187,7 @@ pub fn install( block: BlockWords, ) where WIRE: InterpreterTypes, - HOST: Host + ContractMarker, + HOST: Host + ContractMarker + BeneficiaryTouch, { // RSKIP446 (lovell700): transient storage TLOAD/TSTORE (0x5c/0x5d, // EIP-1153, rskj `GasCost.TLOAD`/`TSTORE` = 100 each). RSKIP445 (lovell700): @@ -599,6 +599,66 @@ where } } +/// What SELFDESTRUCT asks that revm's `Host` can't do: leave the beneficiary +/// as it was. revm's journal touches it whatever the destroyed account held, +/// and under RSK's frontier account rules a touch creates an account that +/// wasn't there. +pub trait BeneficiaryTouch { + /// Whether `address` is touched already, so that touching it again changes + /// nothing. + fn is_touched(&self, address: Address) -> bool; + + /// Takes back the touch that the SELFDESTRUCT which has just run gave + /// `beneficiary`: the account's flag, and the journal's entry for it. + fn take_back_touch(&mut self, beneficiary: Address); +} + +impl BeneficiaryTouch + for revm::Context< + BLOCK, + TX, + CFG, + RskDb, + revm::context::Journal, + crate::raw_storage::RskChainExt, + LOCAL, + > +where + LOCAL: revm::context_interface::LocalContextTr, +{ + fn is_touched(&self, address: Address) -> bool { + self.journaled_state + .inner + .state + .get(&address) + .is_some_and(|account| account.is_touched()) + } + + fn take_back_touch(&mut self, beneficiary: Address) { + let journal = &mut self.journaled_state.inner; + if let Some(account) = journal.state.get_mut(&beneficiary) { + account.unmark_touch(); + } + // revm's `JournalInner::selfdestruct` journals the touch and then the + // destruction, so the touch is the last entry but one. It goes too: + // the sublists' write sets are read from these entries, and rskj + // only reads this account (`isExist`, for the gas). + let touch = journal.journal.len().checked_sub(2).filter(|&at| { + matches!( + journal.journal[at], + revm::context::JournalEntry::AccountTouched { address } if address == beneficiary + ) + }); + debug_assert!( + touch.is_some(), + "SELFDESTRUCT journals its touch of {beneficiary}" + ); + if let Some(at) = touch { + journal.journal.remove(at); + } + } +} + /// CHAINID without revm's ISTANBUL spec check (RSKIP152 activates it at /// papyrus200 while the spec stays PETERSBURG). fn rsk_chainid( @@ -880,7 +940,7 @@ thread_local! { /// All-ops tracer: log pc/opcode/gas (post-static-charge), then dispatch to /// the real implementation (custom rsk op or default PETERSBURG table). -fn traced_all( +fn traced_all( context: InstructionContext<'_, HOST, WIRE>, ) { use revm::interpreter::interpreter_types::{InputsTr, Jumps, LegacyBytecode}; @@ -1155,7 +1215,14 @@ pub fn rsk_call( /// the 25,000 charge and diverge. This override drops the value condition, /// matching rskj frontier-style suicide gas. Everything else (base 5,000 via /// static gas, cold cost, refund) mirrors revm's instruction. -pub fn rsk_selfdestruct( +/// +/// And with rskj's account semantics: `Program.suicide` transfers only a +/// balance that is not zero, so with nothing to hand over the beneficiary is +/// left as it was, and one that did not exist still does not, though the +/// 25,000 for it was charged. revm's journal touches the beneficiary whatever +/// the balance, which under RSK's frontier account rules would create it, so +/// that touch is taken back ([`BeneficiaryTouch`]). +pub fn rsk_selfdestruct( context: InstructionContext<'_, H, WIRE>, ) { if context.interpreter.runtime_flag.is_static() { @@ -1170,18 +1237,22 @@ pub fn rsk_selfdestruct( }; let target = Address::from_word(B256::from(target)); + let address = context.interpreter.input.target_address(); + let cold_load_gas = context.host.gas_params().selfdestruct_cold_cost(); let skip_cold_load = context.interpreter.gas.remaining() < cold_load_gas; - let res = match context.host.selfdestruct( - context.interpreter.input.target_address(), - target, - skip_cold_load, - ) { + // Whether the touch revm is about to give the beneficiary is a new one. + let touches = address != target && !context.host.is_touched(target); + let res = match context.host.selfdestruct(address, target, skip_cold_load) { Ok(res) => res, Err(LoadError::ColdLoadSkipped) => return context.interpreter.halt_oog(), Err(LoadError::DBError) => return context.interpreter.halt_fatal(), }; + if touches && !res.had_value { + context.host.take_back_touch(target); + } + // rskj charges NEW_ACCT_SUICIDE on beneficiary non-existence regardless of // value (no `had_value &&` gate, unlike revm's EIP-161 path). let should_charge_topup = !res.target_exists; From a82ed18f81c7467d3b52e863c0903c9433f31aec Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Thu, 12 Nov 2026 21:10:12 -0800 Subject: [PATCH 123/264] bridge: a contract's registerBtcTransaction names its refund as rskj's When a contract calls the Bridge, rskj hands the method the internal transaction it makes of the call (Program.callToPrecompiledAddress), and registerBtcTransaction names the refund of a payment it turns down by that transaction's hash (rskTx.getHash()): with the release where it waits for its confirmations, and in release_requested. Here the Bridge was handed the hash of the transaction in the block whoever called it, so a refund that a contract registered was stored and logged under another hash than rskj's. Anyone can make a payment the Bridge refunds and register it through a contract. The hash is over the internal transaction's place as much as over what it carries: the hash of the transaction of the program that made it, that program's depth, and how many internal transactions the program had made before this one. The new internal_tx follows a transaction's programs as the handler opens and closes their frames, counts what each makes (a call, to code or to an account without any; a creation; a call to the Bridge, to REMASC or to a native contract; the balance a SELFDESTRUCT moves), and works a hash out only when the Bridge is reached. No other Bridge method stores the hash for a contract: releaseBtc turns a contract away first, and updateCollections is for federation members. The flyover method is rskj's exception. It names its refund by getOriginHash(), the transaction in the block, and so does this. Tests: a_contract_registers_a_refunded_peg_in_as_rskj_registers_it replays what rskj's release did, for its logs, gas, Bridge storage and state root, when the payment was registered by a contract the transaction calls, by one that had first made internal transactions of every kind, through a DELEGATECALL, and by a constructor: of a contract another creates, and of the one the transaction creates (the testdata gains those two runs). hashes_as_rskj_hashes_them checks the hash against 59 internal transactions rskj hashed. --- crates/execution/src/bridge/peg.rs | 10 +- .../bridge/testdata/rskj_refunded_pegins.txt | 83 +++- crates/execution/src/executor.rs | 75 +++- crates/execution/src/internal_tx.rs | 420 ++++++++++++++++++ crates/execution/src/lib.rs | 1 + crates/execution/src/precompiles.rs | 64 ++- crates/execution/src/raw_storage.rs | 5 + crates/execution/src/rsk_handler.rs | 115 +++++ 8 files changed, 746 insertions(+), 27 deletions(-) create mode 100644 crates/execution/src/internal_tx.rs diff --git a/crates/execution/src/bridge/peg.rs b/crates/execution/src/bridge/peg.rs index b2be622..ac88cbb 100644 --- a/crates/execution/src/bridge/peg.rs +++ b/crates/execution/src/bridge/peg.rs @@ -1237,7 +1237,8 @@ fn record_pegin( } let mut fields = vec![ ("btcTxHash", FieldValue::Bytes(btc_txid_event_bytes(btc_tx).to_vec())), - ("rskTxHash", FieldValue::Bytes(tx_ctx.rsk_tx_hash.to_vec())), + // The transaction in the block, by which a record is found again. + ("rskTxHash", FieldValue::Bytes(tx_ctx.origin_tx_hash.to_vec())), ("amountSat", FieldValue::U64(amount_sat)), ("reason", FieldValue::Text(reason.to_string())), ]; @@ -2627,17 +2628,20 @@ fn emit_flyover_rejection_release( ) }; if let Some(built) = built { + // rskj names a flyover refund by `internalTx.getOriginHash()`, the + // hash of the transaction in the block, where every other release is + // named by `rskTx.getHash()`. let use_tx_hash = hardfork_cfg.has_rskip146(rsk_height); let mut waiting = load_pegout_confirmation_set(ctx, use_tx_hash); waiting.push(PegoutWaitingForConfirmations { btc_tx_raw: btc_serialize(&built.tx), rsk_block_height: rsk_height, - rsk_tx_hash: use_tx_hash.then_some(tx_ctx.rsk_tx_hash), + rsk_tx_hash: use_tx_hash.then_some(tx_ctx.origin_tx_hash), }); release_requested_or_record( ctx, use_tx_hash, - &tx_ctx.rsk_tx_hash, + &tx_ctx.origin_tx_hash, &btc_txid_event_bytes(&built.tx), total_satoshis, ); diff --git a/crates/execution/src/bridge/testdata/rskj_refunded_pegins.txt b/crates/execution/src/bridge/testdata/rskj_refunded_pegins.txt index e087f3a..e223fbd 100644 --- a/crates/execution/src/bridge/testdata/rskj_refunded_pegins.txt +++ b/crates/execution/src/bridge/testdata/rskj_refunded_pegins.txt @@ -11,7 +11,9 @@ # bridge, a flyover payment over the locking cap, which the Bridge refunds to its user. # The scenario many-outputs registers a payment to the federation in 330 outputs, whose # refund is under bitcoinj's 100,000 bytes while its inputs are empty and over them once -# rskj has filled them in. +# rskj has filled them in. In the scenarios constructor and deploy it is a constructor that +# registers the first payment: of a contract another contract creates, and of the contract the +# transaction creates. # Lines: # block number, gas limit, timestamp, coinbase, difficulty # account address, nonce, balance, code @@ -314,6 +316,85 @@ cell cf606e0bbdd62c72533bdc45ae2e61a2b5fa7f5251ec5a0d7e99c6022191a680 a06852a70e cell fa504d03ff80daf373e6ce2920ce39ca86dae5077fa09b447cfcb6f0df5835b9 a04f06cafd30dc74e15b547cff33874b6b57ee423d6f0297f8b7ca563b6491cb60 root b677178f5b5129390d97374df19c5e2b75a715165e6fd70b2f207b5f3717d5bf end +scenario constructor +account 85845342b0d5260d516d6da5280a7df9a96d1925 0 de0b6b3a7640000 - +account d06da76e0b65671fc4639b624338464ed7b52bbd 0 de0b6b3a7640000 - +account 0000000000000000000000000000000001000006 0 115eec47f6cf7e35000000 - +account a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4 1 0 6000600060006000600073b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1620186a0f15060006000600060006000730000000000000000000000000000000001000006620186a0f150602c6062600039366000602c3736602c0160006000f05000602c380380602c6000396000600082600060007300000000000000000000000000000000010000065af15000 +account b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1 1 0 73e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2ff +store 0000000000000000000000000000000001000006 00000000000000000000006e657746656465726174696f6e4274635554584f73 c0 +preroot f8e573ab83c36b34f2a906f42c5f0d366c721f36c2b147bc2dbd16e54313d1c0 +tx f904668000831e848094000000000000000000000000000000000100000680b90404e5400e7b0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000001c0000000000000000000000000000000000000000000000000000000000000024000000000000000000000000000000000000000000000000000000000000002c0000000000000000000000000000000000000000000000000000000000000034000000000000000000000000000000000000000000000000000000000000000500100000006226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f20000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000500100000060cb91643b56cab7f897026f3d42ee576b4b8733ff7c545be174dc30fdca064f1fd8983623d6160b34b9a771d6e0e3b148fce95e35af948ae2c41571a3947a7400f15365ffff7f2001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000050010000003a5cb9cfe36bcf69340bc47ac507f5ac9b7194a7ccb9fa32be893f7cf5524546111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005001000000bf3347e74131c963a32f4b6181a6b583971b180cbbae27a5f7220194636b4b4e111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f20000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000500100000070e9bcc26a313dc2efad66eecc2040a51e205bcfaf481e9e6a0c51587252d020111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f200200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005001000000f6baaea21335a06f28fe2e874ac69601ec4388be7c2f8b34a397bd233faefd64111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f20000000000000000000000000000000000000000066a0312bdf00d8019e71deaa3d4e428edb58e9b017f65acd2f7004cf0b7b84856df4a047fc3dac687371d2be5a716d7f220f5a2ce98f7f8b2724e6976fc38d036de023 85845342b0d5260d516d6da5280a7df9a96d1925 80d341ac624a7f9588ad9e045c21b5b2ee1129cebfdafe8077618f1d654d649f +result 1 1 0 +tx f902a68000832dc6c094a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a480b9024443dc06560000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000001e000000000000000000000000000000000000000000000000000000000000001520100000001222222222222222222222222222222222222222222222222222222222222222200000000fdfd0000473030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030483131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131314c69522102141481bf1181ed61aa025f1fe708f68cb018c2c9d6eb719ccd94b3f6ff615308210222ca50390e660f601036dd7502bb973bdcd104b5dcb8f2e74de8edc6c292b03a2103cfc18f02cc004640f2116fdd1f6ca2022e39be25df75e27c80bde842e6b6f93853aeffffffff0100ca9a3b0000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c787000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002701000000011fd8983623d6160b34b9a771d6e0e3b148fce95e35af948ae2c41571a3947a7401010000000000000000000000000000000000000000000000000065a02e92fa9186d30ee29eae5f491df91717f95195e337c09bcb62ab6cf69fd60c4aa0778bf674a4f5b60fd2ffb28e7feab8fe94d849b40c768f641d5c2b6fac2d067d d06da76e0b65671fc4639b624338464ed7b52bbd 1f0d805e02cf2c2c0feca925b6c2025107a7531f3689931794d348815918b772 +result 1 1 114379 +log 0000000000000000000000000000000001000006 b607c3e1fbe6b38cd145b15b837f7b722b199caa60e3057b36c141adee3b75e7,000000000000000000000000a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002 +log 0000000000000000000000000000000001000006 708ce1ead20561c5894a93be3fee64b326b2ad6c198f8253e4bb56f1626053d6,747a94a37115c4e28a94af355ee9fc48b1e3e0d671a7b9340b16d6233698d81f 0000000000000000000000000000000000000000000000000000000000000002 +log 0000000000000000000000000000000001000006 7a7c29481528ac8c2b2e93aee658fddd4dc15304fa723a5c2b88514557bcc790,1d649dbe2941e91ebd612cb7344678a8678427b5d15c677e08247dfa3302d0f6,86bc174bbe4d5209f8cdc70621c2ef604dcca60f61e6ac84a43f7feab7d5d867 000000000000000000000000000000000000000000000000000000003b9aca00 +log 0000000000000000000000000000000001000006 9ee5d520fd5e6eaea3fd2e3ae4e35e9a9c0fb05c9d8f84b507f287da84b5117c,86bc174bbe4d5209f8cdc70621c2ef604dcca60f61e6ac84a43f7feab7d5d867 00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000005fe00ca9a3b000000000000000000000000000000000000000000000000000000 +itx 0 0 call a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4 b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1 0 01 0 000000000000000000000000000000000000000000000000000000000067c280 73e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2ff 1f0d805e02cf2c2c0feca925b6c2025107a7531f3689931794d348815918b772 0 79bcc9832a60d90d92395dcc642ee1773615b8e43daef4aa9ae47835b58ae657 +itx 0 1 call a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4 0000000000000000000000000000000001000006 0 01 0 000000000000000000000000000000000000000000000000000000000067c280 - 1f0d805e02cf2c2c0feca925b6c2025107a7531f3689931794d348815918b772 0 fa86349f1fe5b1fb39fad8558574a9767304cb8bc55998c711a249468b5e10a1 +itx 0 2 create a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4a4 - 0 01 0 000000000000000000000000000000000000000000000000000000000067c280 602c380380602c6000396000600082600060007300000000000000000000000000000000010000065af1500043dc06560000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000001e000000000000000000000000000000000000000000000000000000000000001520100000001222222222222222222222222222222222222222222222222222222222222222200000000fdfd0000473030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030483131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131314c69522102141481bf1181ed61aa025f1fe708f68cb018c2c9d6eb719ccd94b3f6ff615308210222ca50390e660f601036dd7502bb973bdcd104b5dcb8f2e74de8edc6c292b03a2103cfc18f02cc004640f2116fdd1f6ca2022e39be25df75e27c80bde842e6b6f93853aeffffffff0100ca9a3b0000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c787000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002701000000011fd8983623d6160b34b9a771d6e0e3b148fce95e35af948ae2c41571a3947a74010100000000000000000000000000000000000000000000000000 1f0d805e02cf2c2c0feca925b6c2025107a7531f3689931794d348815918b772 0 fb813ecbc72ad6b07cc17f1dbde5e28c963277e4913b149a16ccca74c815d2c5 +cell 0000000000000000000000000000000000000062746354784861736865734150 c0 +cell 00000000000000000000000000626c6f636b53746f7265436861696e48656164 000000000000000000000000000000000000000000000000000000000000000e0000000601000000f6baaea21335a06f28fe2e874ac69601ec4388be7c2f8b34a397bd233faefd64111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f2000000000 +cell 00000000000000000000006e657746656465726174696f6e4274635554584f73 c0 +cell 000000000000000000000072656c656173655472616e73616374696f6e536574 c0 +cell 0072656c656173655472616e73616374696f6e53657457697468547848617368 f9019fb9017a020000000001011fd8983623d6160b34b9a771d6e0e3b148fce95e35af948ae2c41571a3947a7400000000232200207c948c1c2c56b847e905c51f36834413e639d1fce1ef7c95cd1f47396c2173ecffffffff0174749a3b0000000017a914c71aee7e058061a1f61317e509c976affed48258870500000000fc64522102cd53fc53a07f211641a677d250f6de99caf620e8e77071e811a28b3bcddf0be1210362634ab57dae9cb373a5d536e66a8c4f67468bbcfb063809bab643072d78a1242103c5946b3fbae03a654237da863c9ed534e0878657175b132b8ca630f245df04db53ae6702f401b2755321029cecea902067992d52c38b28bf0bb2345bda9b21eca76b16a17c477a64e433012103284178e5fbcc63c54c3b38e3ef88adf2da6c526313650041b0ef955763634ebd2103ab0e2cd7ed158687fc13b88019990860cdb72b1f5777b58513312550ea1584bc2103b9fc46657cf72a1afa007ecf431de1cd27ff5cc8829fa625b66ca47b967e6b2454ae68000000000aa01d649dbe2941e91ebd612cb7344678a8678427b5d15c677e08247dfa3302d0f6 +cell 04e7879f9f786d37bfb9da99ca9ea71f66a545ddf08f8026e18a14c4203f80cd a00f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206 +cell 0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206 0000000000000000000000000000000000000000000000000000000000000002000000000100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4adae5494dffff7f2002000000 +cell 144d40b74626eb8523107d6b6487461aa1d38550ef0c7a154c9d377bc99bb3c7 0a +cell 20d0527258510c6a9e1e48afcf5b201ea54020ccee66adefc23d316ac2bce970 000000000000000000000000000000000000000000000000000000000000000a0000000401000000bf3347e74131c963a32f4b6181a6b583971b180cbbae27a5f7220194636b4b4e111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f2000000000 +cell 3481400172b4addeaaf0248876e6e1769172e8bce17a039e7d9aa3cb25b31bcf 000000000000000000000000000000000000000000000000000000000000000e0000000601000000f6baaea21335a06f28fe2e874ac69601ec4388be7c2f8b34a397bd233faefd64111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f2000000000 +cell 34fa9348faf7dca06d41a200c85ef02b02ab03916a70c8a65b6e3fd84dbe0bde a04e4b6b63940122f7a527aebb0c181b9783b5a681614b2fa363c93141e74733bf +cell 3fee5096e9507962151178d120a30ccdaa2cecc95a10a749a0b540797b21e45b a064fdae3f23bd97a3348b2f7cbe8843ec0196c64a872efe286fa03513a2aebaf6 +cell 464552f57c3f89be32fab9cca794719bacf507c57ac40b3469cf6be3cfb95c3a 0000000000000000000000000000000000000000000000000000000000000006000000020100000060cb91643b56cab7f897026f3d42ee576b4b8733ff7c545be174dc30fdca064f1fd8983623d6160b34b9a771d6e0e3b148fce95e35af948ae2c41571a3947a7400f15365ffff7f2001000000 +cell 4e4b6b63940122f7a527aebb0c181b9783b5a681614b2fa363c93141e74733bf 000000000000000000000000000000000000000000000000000000000000000800000003010000003a5cb9cfe36bcf69340bc47ac507f5ac9b7194a7ccb9fa32be893f7cf5524546111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f2000000000 +cell 4f06cafd30dc74e15b547cff33874b6b57ee423d6f0297f8b7ca563b6491cb60 0000000000000000000000000000000000000000000000000000000000000004000000010100000006226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f2000000000 +cell 62979aa6ab22a67b1abee5022830e036bd87b8881abdaa2c57ee24216e26d93c a0464552f57c3f89be32fab9cca794719bacf507c57ac40b3469cf6be3cfb95c3a +cell 64fdae3f23bd97a3348b2f7cbe8843ec0196c64a872efe286fa03513a2aebaf6 000000000000000000000000000000000000000000000000000000000000000c000000050100000070e9bcc26a313dc2efad66eecc2040a51e205bcfaf481e9e6a0c51587252d020111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f2002000000 +cell 929e75683fff26031f5007c26025afaceeab29dc33e5cf5bb043d646ef67b816 fe00ca9a3b +cell a0b1dbff8c310a5e983e6e603a69b3ee024fe8b229dfca1e0b7f0d41c4768592 a020d0527258510c6a9e1e48afcf5b201ea54020ccee66adefc23d316ac2bce970 +cell cf606e0bbdd62c72533bdc45ae2e61a2b5fa7f5251ec5a0d7e99c6022191a680 a03481400172b4addeaaf0248876e6e1769172e8bce17a039e7d9aa3cb25b31bcf +cell fa504d03ff80daf373e6ce2920ce39ca86dae5077fa09b447cfcb6f0df5835b9 a04f06cafd30dc74e15b547cff33874b6b57ee423d6f0297f8b7ca563b6491cb60 +root 027a6e2dde35d777a59821ae3a94eba99639c596bca4818625bd1190ca44f87f +end +scenario deploy +account 85845342b0d5260d516d6da5280a7df9a96d1925 0 de0b6b3a7640000 - +account d06da76e0b65671fc4639b624338464ed7b52bbd 0 de0b6b3a7640000 - +account 0000000000000000000000000000000001000006 0 115eec47f6cf7e35000000 - +store 0000000000000000000000000000000001000006 00000000000000000000006e657746656465726174696f6e4274635554584f73 c0 +preroot e33796914989af2dca2ab61b5cb87a58b478d6c0b0a115c00c00d7d547c140d8 +tx f904668000831e848094000000000000000000000000000000000100000680b90404e5400e7b0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000001c0000000000000000000000000000000000000000000000000000000000000024000000000000000000000000000000000000000000000000000000000000002c0000000000000000000000000000000000000000000000000000000000000034000000000000000000000000000000000000000000000000000000000000000500100000006226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f20000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000500100000060cb91643b56cab7f897026f3d42ee576b4b8733ff7c545be174dc30fdca064f1fd8983623d6160b34b9a771d6e0e3b148fce95e35af948ae2c41571a3947a7400f15365ffff7f2001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000050010000003a5cb9cfe36bcf69340bc47ac507f5ac9b7194a7ccb9fa32be893f7cf5524546111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005001000000bf3347e74131c963a32f4b6181a6b583971b180cbbae27a5f7220194636b4b4e111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f20000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000500100000070e9bcc26a313dc2efad66eecc2040a51e205bcfaf481e9e6a0c51587252d020111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f200200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005001000000f6baaea21335a06f28fe2e874ac69601ec4388be7c2f8b34a397bd233faefd64111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f20000000000000000000000000000000000000000066a0312bdf00d8019e71deaa3d4e428edb58e9b017f65acd2f7004cf0b7b84856df4a047fc3dac687371d2be5a716d7f220f5a2ce98f7f8b2724e6976fc38d036de023 85845342b0d5260d516d6da5280a7df9a96d1925 80d341ac624a7f9588ad9e045c21b5b2ee1129cebfdafe8077618f1d654d649f +result 1 1 0 +tx f902be8000832dc6c08080b90270602c380380602c6000396000600082600060007300000000000000000000000000000000010000065af1500043dc06560000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000001e000000000000000000000000000000000000000000000000000000000000001520100000001222222222222222222222222222222222222222222222222222222222222222200000000fdfd0000473030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030483131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131314c69522102141481bf1181ed61aa025f1fe708f68cb018c2c9d6eb719ccd94b3f6ff615308210222ca50390e660f601036dd7502bb973bdcd104b5dcb8f2e74de8edc6c292b03a2103cfc18f02cc004640f2116fdd1f6ca2022e39be25df75e27c80bde842e6b6f93853aeffffffff0100ca9a3b0000000017a91422a65d9485781e6cdc0e49fc405ba628365e28c787000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002701000000011fd8983623d6160b34b9a771d6e0e3b148fce95e35af948ae2c41571a3947a7401010000000000000000000000000000000000000000000000000066a02ffa44e9b8dc243f194a79f9a5c0b82efc9e3ce7aa716ba3a0b47626a220d7eca038cd37e04430d596e2e8f5ca5ae6397f3411cada2123b322fa76ebde6126323f d06da76e0b65671fc4639b624338464ed7b52bbd 13e36e1347978732884460ed09510826f5384d50b23616afa4a1da5e3be6c852 +result 1 1 84184 +log 0000000000000000000000000000000001000006 708ce1ead20561c5894a93be3fee64b326b2ad6c198f8253e4bb56f1626053d6,747a94a37115c4e28a94af355ee9fc48b1e3e0d671a7b9340b16d6233698d81f 0000000000000000000000000000000000000000000000000000000000000002 +log 0000000000000000000000000000000001000006 7a7c29481528ac8c2b2e93aee658fddd4dc15304fa723a5c2b88514557bcc790,80d57b04cdf3e7c6412e85355d1ade3d55d1cf8e7843b9ba999b205dafc780db,86bc174bbe4d5209f8cdc70621c2ef604dcca60f61e6ac84a43f7feab7d5d867 000000000000000000000000000000000000000000000000000000003b9aca00 +log 0000000000000000000000000000000001000006 9ee5d520fd5e6eaea3fd2e3ae4e35e9a9c0fb05c9d8f84b507f287da84b5117c,86bc174bbe4d5209f8cdc70621c2ef604dcca60f61e6ac84a43f7feab7d5d867 00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000005fe00ca9a3b000000000000000000000000000000000000000000000000000000 +itx 0 0 call 4eb40c2c5114a6379609479e98cefa6dc480a653 0000000000000000000000000000000001000006 0 00 0 000000000000000000000000000000000000000000000000000000000067c280 - 13e36e1347978732884460ed09510826f5384d50b23616afa4a1da5e3be6c852 0 80d57b04cdf3e7c6412e85355d1ade3d55d1cf8e7843b9ba999b205dafc780db +cell 0000000000000000000000000000000000000062746354784861736865734150 c0 +cell 00000000000000000000000000626c6f636b53746f7265436861696e48656164 000000000000000000000000000000000000000000000000000000000000000e0000000601000000f6baaea21335a06f28fe2e874ac69601ec4388be7c2f8b34a397bd233faefd64111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f2000000000 +cell 00000000000000000000006e657746656465726174696f6e4274635554584f73 c0 +cell 000000000000000000000072656c656173655472616e73616374696f6e536574 c0 +cell 0072656c656173655472616e73616374696f6e53657457697468547848617368 f9019fb9017a020000000001011fd8983623d6160b34b9a771d6e0e3b148fce95e35af948ae2c41571a3947a7400000000232200207c948c1c2c56b847e905c51f36834413e639d1fce1ef7c95cd1f47396c2173ecffffffff0174749a3b0000000017a914c71aee7e058061a1f61317e509c976affed48258870500000000fc64522102cd53fc53a07f211641a677d250f6de99caf620e8e77071e811a28b3bcddf0be1210362634ab57dae9cb373a5d536e66a8c4f67468bbcfb063809bab643072d78a1242103c5946b3fbae03a654237da863c9ed534e0878657175b132b8ca630f245df04db53ae6702f401b2755321029cecea902067992d52c38b28bf0bb2345bda9b21eca76b16a17c477a64e433012103284178e5fbcc63c54c3b38e3ef88adf2da6c526313650041b0ef955763634ebd2103ab0e2cd7ed158687fc13b88019990860cdb72b1f5777b58513312550ea1584bc2103b9fc46657cf72a1afa007ecf431de1cd27ff5cc8829fa625b66ca47b967e6b2454ae68000000000aa080d57b04cdf3e7c6412e85355d1ade3d55d1cf8e7843b9ba999b205dafc780db +cell 04e7879f9f786d37bfb9da99ca9ea71f66a545ddf08f8026e18a14c4203f80cd a00f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206 +cell 0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206 0000000000000000000000000000000000000000000000000000000000000002000000000100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4adae5494dffff7f2002000000 +cell 144d40b74626eb8523107d6b6487461aa1d38550ef0c7a154c9d377bc99bb3c7 0a +cell 20d0527258510c6a9e1e48afcf5b201ea54020ccee66adefc23d316ac2bce970 000000000000000000000000000000000000000000000000000000000000000a0000000401000000bf3347e74131c963a32f4b6181a6b583971b180cbbae27a5f7220194636b4b4e111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f2000000000 +cell 3481400172b4addeaaf0248876e6e1769172e8bce17a039e7d9aa3cb25b31bcf 000000000000000000000000000000000000000000000000000000000000000e0000000601000000f6baaea21335a06f28fe2e874ac69601ec4388be7c2f8b34a397bd233faefd64111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f2000000000 +cell 34fa9348faf7dca06d41a200c85ef02b02ab03916a70c8a65b6e3fd84dbe0bde a04e4b6b63940122f7a527aebb0c181b9783b5a681614b2fa363c93141e74733bf +cell 3fee5096e9507962151178d120a30ccdaa2cecc95a10a749a0b540797b21e45b a064fdae3f23bd97a3348b2f7cbe8843ec0196c64a872efe286fa03513a2aebaf6 +cell 464552f57c3f89be32fab9cca794719bacf507c57ac40b3469cf6be3cfb95c3a 0000000000000000000000000000000000000000000000000000000000000006000000020100000060cb91643b56cab7f897026f3d42ee576b4b8733ff7c545be174dc30fdca064f1fd8983623d6160b34b9a771d6e0e3b148fce95e35af948ae2c41571a3947a7400f15365ffff7f2001000000 +cell 4e4b6b63940122f7a527aebb0c181b9783b5a681614b2fa363c93141e74733bf 000000000000000000000000000000000000000000000000000000000000000800000003010000003a5cb9cfe36bcf69340bc47ac507f5ac9b7194a7ccb9fa32be893f7cf5524546111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f2000000000 +cell 4f06cafd30dc74e15b547cff33874b6b57ee423d6f0297f8b7ca563b6491cb60 0000000000000000000000000000000000000000000000000000000000000004000000010100000006226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f2000000000 +cell 62979aa6ab22a67b1abee5022830e036bd87b8881abdaa2c57ee24216e26d93c a0464552f57c3f89be32fab9cca794719bacf507c57ac40b3469cf6be3cfb95c3a +cell 64fdae3f23bd97a3348b2f7cbe8843ec0196c64a872efe286fa03513a2aebaf6 000000000000000000000000000000000000000000000000000000000000000c000000050100000070e9bcc26a313dc2efad66eecc2040a51e205bcfaf481e9e6a0c51587252d020111111111111111111111111111111111111111111111111111111111111111100f15365ffff7f2002000000 +cell 929e75683fff26031f5007c26025afaceeab29dc33e5cf5bb043d646ef67b816 fe00ca9a3b +cell a0b1dbff8c310a5e983e6e603a69b3ee024fe8b229dfca1e0b7f0d41c4768592 a020d0527258510c6a9e1e48afcf5b201ea54020ccee66adefc23d316ac2bce970 +cell cf606e0bbdd62c72533bdc45ae2e61a2b5fa7f5251ec5a0d7e99c6022191a680 a03481400172b4addeaaf0248876e6e1769172e8bce17a039e7d9aa3cb25b31bcf +cell fa504d03ff80daf373e6ce2920ce39ca86dae5077fa09b447cfcb6f0df5835b9 a04f06cafd30dc74e15b547cff33874b6b57ee423d6f0297f8b7ca563b6491cb60 +root 6378800a13fdd0fc351312e2cf3b088c780dad53e7978f8bcf3d143075cc7bde +end enc 0 1 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 0 - call 0 0 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 f64ee69aab5fd5b648c2ca017b0880b610223c0ae21d4089905212c9ffaa6992 enc 1 1 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 0 - call 0 0 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 bf55a8a523265031c3a9def117bf2d91f605409b862dbb0c20982d12e1110a01 enc 127 1 67c280 a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1 0000000000000000000000000000000001000006 0 - call 0 0 5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2 438a42134addf73ea10411211e55d1b0436dd83321eea6957a9251d36ed94d4a diff --git a/crates/execution/src/executor.rs b/crates/execution/src/executor.rs index 0072624..8e3d5d5 100644 --- a/crates/execution/src/executor.rs +++ b/crates/execution/src/executor.rs @@ -1803,7 +1803,10 @@ impl RskExecutor { // Update per-transaction Bridge context before execution. // This provides the Bridge precompile with: - // - rsk_tx_hash: used for RSKIP146 release queue entries + // - rsk_tx_hash: used for RSKIP146 release queue entries. A + // contract's call to the Bridge is handed another, the hash of + // the internal transaction rskj makes of it, which + // `RskPrecompileProvider` works out from this one. // - btc_sender_hash160: used as BTC peg-out destination (matches rskj's // BridgeUtils.recoverBtcAddressFromEthTransaction) { @@ -1817,6 +1820,7 @@ impl RskExecutor { if let Ok(mut guard) = bridge_ctx_slot.lock() { *guard = BridgeTxContext { rsk_tx_hash, + origin_tx_hash: rsk_tx_hash, btc_sender_hash160, rsk_sender: *sender, // A transaction in a block is never a local call. @@ -14986,6 +14990,13 @@ bf09f6e52420834e8e0e0b1a6df563aba550cf7f99e9724264187c45dcf3d73e8585a0e35196\ ); } + /// The run of that name among a file's. + fn run_named<'a>(runs: &'a [RskjRun], name: &str) -> &'a RskjRun { + runs.iter() + .find(|run| run.name == name) + .unwrap_or_else(|| panic!("no run {name}")) + } + /// What rskj's release does with a peg-in it refunds, read from its own /// `TransactionExecutor` (the testdata's header says how). A member of the /// federation hands the Bridge six Bitcoin headers, and then a transaction @@ -14997,38 +15008,60 @@ bf09f6e52420834e8e0e0b1a6df563aba550cf7f99e9724264187c45dcf3d73e8585a0e35196\ /// regtest's is and mainnet's since its first change after RSKIP305, with /// the witness program in each input's scriptSig, the unsigned witness, and /// the fee taken over the virtual size. The fee is no less than bitcoinj's - /// minimum, which the second run's fee per kilobyte takes it down to + /// minimum, which the fee per kilobyte of `low-fee` takes it down to /// (`Wallet.adjustOutputDownwardsForFee`). And rskj ends the refund's /// settling as it ends every release's (`settleReleaseRejection`, /// `processReleaseTransactionInfo`): it logs `pegout_transaction_created` /// after `release_requested` (RSKIP428) and stores the values of what the /// refund spends (RSKIP305). /// - /// The last run registers a flyover payment over the locking cap, through - /// the contract that stands for the liquidity bridge. rskj refunds that one + /// `flyover` registers a flyover payment over the locking cap, through the + /// contract that stands for the liquidity bridge. rskj refunds that one /// the same way, spending the output that pays the federation's flyover /// address, which for a P2SH-P2WSH federation is the P2SH of the flyover /// script's witness program. It names that refund by the hash of the /// block's transaction, not of the contract's call (`getOriginHash`). + /// + /// In `many-outputs` the payment is in 330 outputs. rskj builds its + /// refund, which is over bitcoinj's 100,000 bytes once its inputs are + /// filled in and under them while bitcoinj checks + /// (`release_tx::fill_inputs`). #[test] fn a_refunded_peg_in_is_registered_as_rskj_registers_it() { let runs = rskj_runs(include_str!("bridge/testdata/rskj_refunded_pegins.txt")); - let names: Vec<&str> = runs.iter().map(|run| run.name.as_str()).collect(); - assert_eq!( - names, - [ - "direct", - "relay", - "nested", - "delegate", - "low-fee", - "flyover", - "many-outputs" - ] - ); - replays_as_rskj_ran_it(&runs[0]); - replays_as_rskj_ran_it(&runs[4]); - replays_as_rskj_ran_it(&runs[5]); - replays_as_rskj_ran_it(&runs[6]); + for name in ["direct", "low-fee", "flyover", "many-outputs"] { + replays_as_rskj_ran_it(run_named(&runs, name)); + } + } + + /// A contract registers the payment, and rskj hands the Bridge the + /// internal transaction it makes of the contract's call. The refund is + /// named by that transaction's hash, in `release_requested` and with the + /// release where it waits for its confirmations + /// (`BridgeSupport.registerBtcTransaction`, `rskTx.getHash()`), not by the + /// hash of the transaction in the block. + /// + /// That hash is over where the call stands among the internal + /// transactions of the transaction (`crate::internal_tx`). In `relay` the + /// contract the transaction calls hands its call data to the Bridge. In + /// `nested` it first calls an account without code, creates a contract + /// whose constructor calls one too, makes a STATICCALL to the identity + /// precompile, calls a contract that destroys itself and one that calls + /// and reverts, makes a call it has no balance for, calls the BlockHeader + /// contract and REMASC, makes a CALLCODE, and then a DELEGATECALL to the + /// code that reaches the Bridge. In `delegate` it delegates at once. + /// + /// In `constructor` the contract calls one that destroys itself with no + /// balance, which makes no internal transaction, and the Bridge with no + /// data, which makes one though the Bridge turns the call down. Then it + /// creates a contract whose constructor registers the payment, under the + /// creation's internal transaction. In `deploy` the transaction itself + /// creates the contract whose constructor registers. + #[test] + fn a_contract_registers_a_refunded_peg_in_as_rskj_registers_it() { + let runs = rskj_runs(include_str!("bridge/testdata/rskj_refunded_pegins.txt")); + for name in ["relay", "nested", "delegate", "constructor", "deploy"] { + replays_as_rskj_ran_it(run_named(&runs, name)); + } } } diff --git a/crates/execution/src/internal_tx.rs b/crates/execution/src/internal_tx.rs new file mode 100644 index 0000000..16192d2 --- /dev/null +++ b/crates/execution/src/internal_tx.rs @@ -0,0 +1,420 @@ +//! rskj's internal transactions, as far as consensus reads them. +//! +//! rskj records every call a contract makes, and every contract it creates, +//! as an `InternalTransaction` of the program that made it +//! (`Program.addInternalTx`), and the program a call or a creation starts +//! runs with that internal transaction as its own. Consensus reads them in +//! one place. A Bridge method a contract calls is handed the call's internal +//! transaction (`Program.callToPrecompiledAddress`), and +//! `registerBtcTransaction` names the refund of a payment it turns down by +//! that transaction's hash (`BridgeSupport.registerBtcTransaction`, +//! `rskTx.getHash()`): in the pegouts waiting for confirmations, and in +//! `release_requested`. +//! +//! The hash is over the internal transaction's place as much as over what it +//! carries (`InternalTransaction.getEncoded`): the hash of the transaction of +//! the program that made it, that program's depth, and how many internal +//! transactions that program had made before this one. So [`InternalTxs`] +//! follows a transaction's programs as they open and close, counts what each +//! makes, and works a hash out only when the Bridge is reached. +//! +//! What counts as one (`Program`, read at VETIVER-9.0.4, and its release run: +//! `bridge/testdata/rskj_refunded_pegins.txt`): +//! +//! - a call that passes rskj's checks of depth and balance, to a contract or +//! to an account without code (`callToAddress`). CALLCODE and DELEGATECALL +//! go to the caller itself; +//! - a creation that passes its checks, of an address in use among them +//! (`createContract`); +//! - a call to the Bridge, to REMASC or to a native contract, and to no other +//! precompile (`callToPrecompiledAddress`); +//! - a SELFDESTRUCT of an account with a balance (`suicide`). +//! +//! When a call's program ends, what it made is added to what its caller made, +//! whether it failed or not (`ProgramResult.merge`). A creation's is not +//! (`finalizeContractCreation`). + +use alloy_primitives::{keccak256, Address, Bytes, B256, U256}; +use alloy_rlp::Encodable; + +/// What an internal transaction that starts a program carries of its own. +#[derive(Clone, Debug)] +pub struct InternalTx { + /// The program that made it, by the address it runs as (rskj + /// `getOwnerRskAddress`). + pub sender: Address, + /// The sender's nonce as it was made. + pub nonce: u64, + /// Where it goes: the callee, for CALLCODE and DELEGATECALL the sender + /// itself, and nowhere for a creation. + pub receiver: Option
, + /// What it carries. A DELEGATECALL carries nothing. + pub value: U256, + /// The code the program it starts runs: the callee's, or a creation's + /// init code. + pub code: Bytes, + /// Whether it creates a contract, with rskj's note `create`, or calls, + /// with `call`. + pub creates: bool, +} + +/// A program of the transaction that is running or waiting on one it called. +struct Program { + /// The internal transaction that started it, and that one's index among + /// those of the program that made it. `None` for the program the + /// transaction itself starts. + started_by: Option<(InternalTx, usize)>, + /// The hash of the internal transaction that started it, once the Bridge + /// has needed it. + hash: Option, + /// How many internal transactions it has made: the size of rskj's + /// `getResult().getInternalTransactions()`. + made: usize, +} + +/// The programs of the transaction that is running, innermost last. +#[derive(Default)] +pub struct InternalTxs { + /// The transaction's gas price, which every one of its internal + /// transactions carries (`Program.getTxGasPrice`). + gas_price: U256, + /// The block's gas limit, which each carries as its own + /// (`Program.getGasLimit`). + block_gas_limit: U256, + programs: Vec, +} + +impl InternalTxs { + /// A transaction starts. + pub fn begin(&mut self, gas_price: U256, block_gas_limit: U256) { + *self = Self { + gas_price, + block_gas_limit, + programs: Vec::new(), + }; + } + + /// A program opens: the transaction's own, with nothing to start it, or + /// one the running program made `started_by` for, which counts as one it + /// made. + pub fn enter(&mut self, started_by: Option) { + let started_by = match (self.programs.last_mut(), started_by) { + (Some(maker), Some(tx)) => { + maker.made += 1; + Some((tx, maker.made - 1)) + } + _ => None, + }; + self.programs.push(Program { + started_by, + hash: None, + made: 0, + }); + } + + /// The running program made an internal transaction that starts no + /// program: a call to an account without code, to REMASC or to a native + /// contract, or the balance its SELFDESTRUCT moved. + pub fn count(&mut self) { + if let Some(program) = self.programs.last_mut() { + program.made += 1; + } + } + + /// The running program ends. A call's program hands what it made to the + /// one that called it (`merges`); a creation's doesn't. + pub fn leave(&mut self, merges: bool) { + let Some(ended) = self.programs.pop() else { + return; + }; + if let (true, Some(maker)) = (merges, self.programs.last_mut()) { + maker.made += ended.made; + } + } + + /// The running program reaches the Bridge, as `sender` with `nonce`, for + /// `receiver` and with `value`: the hash of the internal transaction rskj + /// hands the Bridge, which counts as one the program made. `None` where + /// no program runs: the transaction calls the Bridge itself, and the + /// Bridge is handed the transaction. + pub fn reach_bridge( + &mut self, + tx_hash: B256, + sender: Address, + nonce: u64, + receiver: Address, + value: U256, + ) -> Option { + let depth = self.programs.len().checked_sub(1)?; + let parent = self.transaction_hash(depth, tx_hash); + let index = self.programs[depth].made; + self.programs[depth].made += 1; + Some(hash( + nonce, + &parent, + &sender, + Some(&receiver), + value, + self.gas_price, + Some(self.block_gas_limit), + &[], + "call", + depth, + index, + )) + } + + /// The hash of the transaction the program at `level` runs: the block's + /// transaction for the outermost, and for any other the internal + /// transaction that started it, whose hash is over that of the program + /// below. + fn transaction_hash(&mut self, level: usize, tx_hash: B256) -> B256 { + let mut parent = tx_hash; + for at in 0..=level { + let program = &self.programs[at]; + parent = match (program.hash, &program.started_by) { + (Some(known), _) => known, + (None, None) => parent, + (None, Some((tx, index))) => { + let worked_out = hash( + tx.nonce, + &parent, + &tx.sender, + tx.receiver.as_ref(), + tx.value, + self.gas_price, + Some(self.block_gas_limit), + &tx.code, + if tx.creates { "create" } else { "call" }, + at - 1, + *index, + ); + self.programs[at].hash = Some(worked_out); + worked_out + } + }; + } + parent + } +} + +/// The hash of an internal transaction: the Keccak-256 of rskj's +/// `InternalTransaction.getEncoded`, while the transaction isn't rejected. +/// rskj keeps the first hash it works out (`Transaction.getHash`), and works +/// out a call's while the call still runs, so a rejection that comes later +/// changes none that anything reads. +/// +/// `gas_limit` is a 32-byte word where there is one, and a SELFDESTRUCT's +/// internal transaction has none. +#[allow(clippy::too_many_arguments)] +fn hash( + nonce: u64, + parent: &B256, + sender: &Address, + receiver: Option<&Address>, + value: U256, + gas_price: U256, + gas_limit: Option, + data: &[u8], + note: &str, + depth: usize, + index: usize, +) -> B256 { + let mut fields = Vec::with_capacity(192 + data.len()); + nonce_bytes(nonce).as_slice().encode(&mut fields); + parent.encode(&mut fields); + sender.encode(&mut fields); + match receiver { + Some(receiver) => receiver.encode(&mut fields), + None => fields.push(alloy_rlp::EMPTY_STRING_CODE), + } + value.encode(&mut fields); + gas_price.encode(&mut fields); + match gas_limit { + Some(limit) => B256::from(limit).encode(&mut fields), + None => fields.push(alloy_rlp::EMPTY_STRING_CODE), + } + data.encode(&mut fields); + note.as_bytes().encode(&mut fields); + (depth as u64).encode(&mut fields); + (index as u64).encode(&mut fields); + // Not rejected. + fields.push(alloy_rlp::EMPTY_STRING_CODE); + let mut encoded = Vec::with_capacity(fields.len() + 4); + alloy_rlp::Header { + list: true, + payload_length: fields.len(), + } + .encode(&mut encoded); + encoded.extend_from_slice(&fields); + keccak256(encoded) +} + +/// A nonce as rskj writes it into an internal transaction: the bytes of +/// `BigInteger.toByteArray()`, which are big-endian, as few as hold it, and +/// led by a zero byte where the first would have its top bit set (128 is +/// `00 80`). Zero is written as no bytes. +fn nonce_bytes(nonce: u64) -> Vec { + let bytes = nonce.to_be_bytes(); + let first = bytes + .iter() + .position(|byte| *byte != 0) + .unwrap_or(bytes.len()); + let mut written = Vec::with_capacity(9); + if bytes.get(first).is_some_and(|byte| *byte >= 0x80) { + written.push(0); + } + written.extend_from_slice(&bytes[first..]); + written +} + +#[cfg(test)] +mod tests { + use super::*; + + fn bytes(field: &str) -> Vec { + if field == "-" { + Vec::new() + } else { + hex::decode(field).unwrap() + } + } + + /// The file's `enc` lines are internal transactions rskj's release built + /// by themselves, each with the hash it gave: every length a nonce, a + /// value, a gas price, a depth and an index take, a SELFDESTRUCT's with + /// no gas limit, a creation's with no receiver, and the zero address, + /// which is twenty bytes and not none. + #[test] + fn hashes_as_rskj_hashes_them() { + let vectors = include_str!("bridge/testdata/rskj_refunded_pegins.txt"); + let mut read = 0; + for line in vectors.lines().filter(|line| line.starts_with("enc ")) { + let fields: Vec<&str> = line.split(' ').collect(); + let number = |field: &str| U256::from_str_radix(field, 16).unwrap(); + let receiver = (fields[5] != "-").then(|| Address::from_slice(&bytes(fields[5]))); + let ours = hash( + fields[1].parse().unwrap(), + &B256::from_slice(&bytes(fields[11])), + &Address::from_slice(&bytes(fields[4])), + receiver.as_ref(), + number(fields[6]), + number(fields[2]), + (fields[3] != "-").then(|| number(fields[3])), + &bytes(fields[7]), + fields[8], + fields[9].parse().unwrap(), + fields[10].parse().unwrap(), + ); + assert_eq!(ours, B256::from_slice(&bytes(fields[12])), "{line}"); + read += 1; + } + assert_eq!(read, 59); + } + + /// A program's internal transactions are numbered as it makes them, and a + /// call's are added to its caller's when it ends, where a creation's are + /// not. The Bridge's hash is over the hash of what started the program + /// that reaches it, and through that over every program below. + #[test] + fn counts_what_each_program_makes() { + let (a, b, bridge) = ( + Address::repeat_byte(0xa1), + Address::repeat_byte(0xb1), + Address::repeat_byte(0x06), + ); + let tx_hash = B256::repeat_byte(0x77); + let (price, limit) = (U256::from(3), U256::from(6_800_000)); + let started = |sender, receiver, creates| InternalTx { + sender, + nonce: 1, + receiver, + value: U256::ZERO, + code: Bytes::from_static(&[0x00]), + creates, + }; + let mut txs = InternalTxs::default(); + txs.begin(price, limit); + // The transaction calls the Bridge itself: no program, no hash. + assert_eq!(txs.reach_bridge(tx_hash, a, 1, bridge, U256::ZERO), None); + + txs.enter(None); + txs.count(); + // A call's program makes two and ends: the caller has made four. + txs.enter(Some(started(a, Some(b), false))); + txs.count(); + txs.count(); + txs.leave(true); + // A creation's makes one and ends: the creator has made five. + txs.enter(Some(started(a, None, true))); + txs.count(); + txs.leave(false); + let from_the_first = txs.reach_bridge(tx_hash, a, 1, bridge, U256::ZERO); + let at = |parent: &B256, depth, index| { + hash( + 1, + parent, + &a, + Some(&bridge), + U256::ZERO, + price, + Some(limit), + &[], + "call", + depth, + index, + ) + }; + assert_eq!(from_the_first, Some(at(&tx_hash, 0, 5))); + + // From a program the first one called, its seventh. + txs.enter(Some(started(a, Some(b), false))); + let starting = hash( + 1, + &tx_hash, + &a, + Some(&b), + U256::ZERO, + price, + Some(limit), + &[0x00], + "call", + 0, + 6, + ); + let from_the_second = txs.reach_bridge(tx_hash, b, 1, bridge, U256::ZERO); + let expected = hash( + 1, + &starting, + &b, + Some(&bridge), + U256::ZERO, + price, + Some(limit), + &[], + "call", + 1, + 0, + ); + assert_eq!(from_the_second, Some(expected)); + } + + #[test] + fn a_nonce_is_written_as_java_writes_a_big_integer() { + let cases: [(u64, &[u8]); 7] = [ + (0, &[]), + (1, &[1]), + (127, &[0x7f]), + (128, &[0, 0x80]), + (255, &[0, 0xff]), + (256, &[1, 0]), + ( + u64::MAX, + &[0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff], + ), + ]; + for (nonce, written) in cases { + assert_eq!(nonce_bytes(nonce), written, "{nonce}"); + } + } +} diff --git a/crates/execution/src/lib.rs b/crates/execution/src/lib.rs index 2c44adb..d53cb5a 100644 --- a/crates/execution/src/lib.rs +++ b/crates/execution/src/lib.rs @@ -7,6 +7,7 @@ pub mod env; pub mod executor; pub mod gas_estimator; pub mod hardfork; +pub mod internal_tx; pub mod mining; pub(crate) mod native; pub mod precompiles; diff --git a/crates/execution/src/precompiles.rs b/crates/execution/src/precompiles.rs index 8a3783d..305fcae 100644 --- a/crates/execution/src/precompiles.rs +++ b/crates/execution/src/precompiles.rs @@ -561,8 +561,15 @@ pub fn rsk_precompiles(hardfork_cfg: &RskHardforkConfig, block_number: u64) -> P /// read by the Bridge precompile inside `run_bridge`. #[derive(Debug, Clone, Default)] pub struct BridgeTxContext { - /// Keccak256 of the RLP-encoded transaction. + /// rskj's `rskTx.getHash()`: the Keccak256 of the RLP-encoded + /// transaction, and for a contract's call to the Bridge the hash of the + /// internal transaction rskj makes of the call, which is the `rskTx` the + /// Bridge is then handed (`crate::internal_tx`). pub rsk_tx_hash: [u8; 32], + /// The hash of the transaction in the block, whoever calls the Bridge: + /// rskj's `InternalTransaction.getOriginHash()`, by which the flyover + /// method names a refund. + pub origin_tx_hash: [u8; 32], /// BTC P2PKH destination: RIPEMD160(SHA256(compressed_pubkey)). /// Derived from the RSK transaction's secp256k1 signature. /// Zero bytes if the pubkey cannot be recovered (system/REMASC transactions). @@ -898,6 +905,11 @@ impl RskPrecompileProvider { } _ => (inputs.caller, inputs.call_value()), }; + // That internal transaction's hash is what the Bridge names a + // refund by. It goes to the call's context address, as the + // endowment does. + let internal_tx_hash = + self.bridge_internal_tx(context, owner, inputs.target_address, value); self.run_bridge( context, &input_bytes, @@ -905,8 +917,12 @@ impl RskPrecompileProvider { owner, inputs.scheme, value, + internal_tx_hash, ) } else { + // REMASC is handed one too, which counts among those the calling + // program has made. + context.chain_mut().internal_txs.count(); self.run_remasc(context, &input_bytes, inputs.gas_limit) }; @@ -1039,8 +1055,39 @@ impl RskPrecompileProvider { Ok(Some(result)) } + /// The hash of the internal transaction rskj hands the Bridge when a + /// contract calls it as `owner`, for `receiver` and with `value` + /// (`crate::internal_tx`). None when the transaction calls the Bridge + /// itself. + fn bridge_internal_tx( + &self, + context: &mut CTX, + owner: Address, + receiver: Address, + value: revm::primitives::U256, + ) -> Option<[u8; 32]> { + use revm::context_interface::JournalTr; + let tx_hash = self + .bridge_tx_context + .lock() + .map(|tx| tx.origin_tx_hash) + .unwrap_or_default(); + // The program that calls is running, so its account is loaded. + let nonce = context + .journal_mut() + .load_account(owner) + .map(|account| account.info.nonce) + .unwrap_or_default(); + context + .chain_mut() + .internal_txs + .reach_bridge(tx_hash.into(), owner, nonce, receiver, value) + .map(|hash| hash.0) + } + /// Bridge precompile (0x01000006). /// BTC–RSK two-way peg with 69 ABI methods. + #[allow(clippy::too_many_arguments)] fn run_bridge( &self, context: &mut CTX, @@ -1049,6 +1096,7 @@ impl RskPrecompileProvider { caller: Address, scheme: revm::interpreter::CallScheme, call_value: revm::primitives::U256, + internal_tx_hash: Option<[u8; 32]>, ) -> Result { use revm::context_interface::JournalTr; use revm::interpreter::CallScheme; @@ -1065,9 +1113,14 @@ impl RskPrecompileProvider { // reached via a contract CALL (rskj `isContractTx`). revm's depth here // already counts the precompile's own frame. let call_depth = context.journal().depth(); - let tx_ctx = self.bridge_tx_context.lock() + let mut tx_ctx = self + .bridge_tx_context + .lock() .map(|g| g.clone()) .unwrap_or_default(); + if let Some(hash) = internal_tx_hash { + tx_ctx.rsk_tx_hash = hash; + } crate::bridge::execute_bridge( context, input, @@ -1311,6 +1364,13 @@ impl PrecompileProvider for RskPrecompileProvider return Ok(None); }; + // rskj makes an internal transaction of a contract's call to one of + // its native contracts, as of one to the Bridge or to REMASC, and of + // a call to no other precompile (`crate::internal_tx`). + if NATIVE_CONTRACTS.contains(&addr) { + context.chain_mut().internal_txs.count(); + } + // rskj creates the account of the call's context address on first // call: the precompile's, or for CALLCODE and DELEGATECALL the // caller's own (see the stateful path above). diff --git a/crates/execution/src/raw_storage.rs b/crates/execution/src/raw_storage.rs index 7ee7097..5757ba3 100644 --- a/crates/execution/src/raw_storage.rs +++ b/crates/execution/src/raw_storage.rs @@ -71,6 +71,11 @@ pub struct RskChainExt { /// the caller. revm drops them with the call's checkpoint; they wait here /// for `RskHandler` to write them again once that checkpoint is gone. pub kept_logs: Vec, + /// The programs of the transaction that is running and the internal + /// transactions each has made, for the hash of the one rskj hands the + /// Bridge when a contract calls it. `RskHandler` follows the programs, + /// and the precompiles tell it of the calls that reach them. + pub internal_txs: crate::internal_tx::InternalTxs, } /// Raw-bytes storage overlay for the block being executed. diff --git a/crates/execution/src/rsk_handler.rs b/crates/execution/src/rsk_handler.rs index 22a2364..3984e43 100644 --- a/crates/execution/src/rsk_handler.rs +++ b/crates/execution/src/rsk_handler.rs @@ -31,7 +31,9 @@ //! and the auction proceeds. Top-level deploy transactions are NOT //! affected (rskj TransactionExecutor.createContract throws, like EIP-2). +use crate::internal_tx::InternalTx; use revm::context::result::HaltReason; +use revm::context::JournalEntry; use revm::context_interface::journaled_state::account::JournaledAccountTr; use revm::context_interface::{Cfg, ContextTr, JournalTr}; use revm::handler::instructions::InstructionProvider; @@ -560,6 +562,17 @@ where } _ => true, }; + // Every internal transaction rskj makes under this one carries its + // gas price and the block's gas limit (`crate::internal_tx`). + { + use revm::context_interface::{Block, Transaction}; + let ctx = evm.ctx(); + let gas_price = ctx.tx().effective_gas_price(ctx.block().basefee() as u128); + let block_gas_limit = ctx.block().gas_limit(); + ctx.chain_mut() + .internal_txs + .begin(U256::from(gas_price), U256::from(block_gas_limit)); + } if let ItemOrResult::Result(frame_result) = D::init(evm, first_frame_input)? { return Ok(frame_result); } @@ -574,6 +587,7 @@ where // bumps the nonce (even post-RSKIP125), so it stays 0. set_created_contract_nonce(evm, 0); self.open_frame(evm); + evm.ctx().chain_mut().internal_txs.enter(None); // A local call whose gas limit can't pay its intrinsic cost (see // `validate`): rskj's `go()` throws as it charges the cost, before the // program runs, and takes back what opening it did. @@ -656,6 +670,10 @@ where } continue; } + // What rskj's internal transaction for this frame is, if it + // comes to make one: read before the frame opens, while a + // creator's nonce is still the one the creation carries. + let made = internal_tx_of(evm, &init.frame_input); match D::init(evm, init)? { ItemOrResult::Item(()) => { // Internal CREATE/CREATE2 opcode: post-RSKIP125 @@ -664,6 +682,18 @@ where let nonce = u64::from(self.rskip125_active); set_created_contract_nonce(evm, nonce); self.open_frame(evm); + // The program runs as that internal transaction, + // which carries the code it runs. + let code = evm + .frame_stack() + .get() + .interpreter + .bytecode + .original_bytes(); + evm.ctx() + .chain_mut() + .internal_txs + .enter(made.map(|tx| InternalTx { code, ..tx })); continue; } // Do not pop the frame since no new frame was created: @@ -671,6 +701,24 @@ where // account, or a failed pre-check (depth/endowment) // resolves. ItemOrResult::Result(result) => { + // A call that ran no code and reached no + // precompile went to an account without code, and + // rskj makes an internal transaction of that one + // too (`Program.callToAddress`). A call or a + // creation it refuses, for depth, balance or an + // address in use, comes back with another result + // and makes none; a precompile counts its own. + if matches!( + &result, + FrameResult::Call(outcome) + if !outcome.was_precompile_called + && matches!( + outcome.instruction_result(), + InstructionResult::Stop + ) + ) { + evm.ctx().chain_mut().internal_txs.count(); + } // A precompile's failed call has had its // checkpoint reverted by now; the logs rskj leaves // with the caller go back in as the caller's. @@ -734,6 +782,24 @@ where // when its result is ok (`rsk_frame_return`). ItemOrResult::Result(result) => { self.close_frame(evm, result.instruction_result().is_ok()); + // The program's internal transactions (`crate::internal_tx`). + // rskj `Program.suicide` makes one of the balance it + // moves, where the account has one: the journal's last + // entry is that SELFDESTRUCT's. And a call's program + // hands what it made to its caller, where a creation's + // doesn't. + let moved_a_balance = + matches!(result.instruction_result(), InstructionResult::SelfDestruct) + && matches!( + evm.ctx().journal().journal().last(), + Some(JournalEntry::AccountDestroyed { had_balance, .. }) + if !had_balance.is_zero() + ); + let internal_txs = &mut evm.ctx().chain_mut().internal_txs; + if moved_a_balance { + internal_txs.count(); + } + internal_txs.leave(matches!(result, FrameResult::Call(_))); result } }; @@ -944,6 +1010,55 @@ where } } +/// The internal transaction rskj makes of the call or the creation a frame +/// is about to start (`Program.callToAddress`, `Program.createContract`), +/// less the code its program runs, which the frame holds once it is open. +fn internal_tx_of( + evm: &mut EVM, + input: &revm::interpreter::interpreter_action::FrameInput, +) -> Option +where + EVM: EvmTr + JournalExt>>, +{ + use revm::interpreter::interpreter_action::FrameInput; + let (sender, receiver, value, creates) = match input { + FrameInput::Call(inputs) => { + // rskj's sender is the program that calls, and its value what the + // opcode took off the stack. For a DELEGATECALL revm's caller and + // value are those of the frame that delegates, where rskj's are + // that frame's own address and nothing. + let delegates = inputs.scheme == revm::interpreter::CallScheme::DelegateCall; + let sender = if delegates { + inputs.target_address + } else { + inputs.caller + }; + let value = if delegates { + U256::ZERO + } else { + inputs.call_value() + }; + (sender, Some(inputs.target_address), value, false) + } + FrameInput::Create(inputs) => (inputs.caller(), None, inputs.value(), true), + _ => return None, + }; + let nonce = evm + .ctx() + .journal() + .evm_state() + .get(&sender) + .map_or(0, |account| account.info.nonce); + Some(InternalTx { + sender, + nonce, + receiver, + value, + code: Bytes::new(), + creates, + }) +} + /// Hands the journal's entries to the RSKIP-144 collision tracker. /// /// Called immediately before every point that can revert a checkpoint. Under From 8953895e4ed529998d610847849ffaf140b27d85 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Thu, 12 Nov 2026 21:10:12 -0800 Subject: [PATCH 124/264] bridge: a Bitcoin transaction is read as bitcoinj reads it, and verified The three methods that are given a Bitcoin transaction (registerBtcTransaction, registerBtcCoinbaseTransaction and registerFlyoverBtcTransaction) parse it with bitcoinj once the partial merkle tree has proved the bytes given, and then run bitcoinj's verify() on it. bitcoinj reads more than the standard encoding: bytes left over after the transaction, a count or a length written in more bytes than it needs (or in nine, of which it drops the high four), and a segwit marker whose flag is 1 before inputs that carry no witness. It writes what it has read in the standard way, so the transaction's hashes are those of the standard form. rust-bitcoin's deserialize refuses all of these, so the call failed here where rskj goes on to register the transaction. And only the coinbase method ran verify(): a peg-in that spends one outpoint twice, or holds an output of more than 21 million bitcoins, was registered where rskj throws. Either takes a Bitcoin header mined over a block that Bitcoin itself refuses, with the confirmations the Bridge asks for on top of it. tx::parse_btc_transaction is BtcTransaction.parse, with its int and long arithmetic. The three methods now share read_btc_transaction: that parse, then verify() over the bytes read, not over those given. Tests: reads_a_transaction_as_bitcoinj_reads_it, over 88 transactions that bitcoinj-thin 0.14.4-rsk-18 was run on (the new testdata file holds its answers; rust-bitcoin refuses 33 of the 58 it reads), and one for each method: a transaction written three such ways is registered as it is when written the standard way, and what verify() refuses fails. --- crates/execution/src/bridge/peg.rs | 156 ++++++++++- .../bridge/testdata/bitcoinj_transactions.txt | 94 +++++++ crates/execution/src/bridge/tx.rs | 258 +++++++++++++++++- 3 files changed, 489 insertions(+), 19 deletions(-) create mode 100644 crates/execution/src/bridge/testdata/bitcoinj_transactions.txt diff --git a/crates/execution/src/bridge/peg.rs b/crates/execution/src/bridge/peg.rs index ac88cbb..198b0f5 100644 --- a/crates/execution/src/bridge/peg.rs +++ b/crates/execution/src/bridge/peg.rs @@ -278,10 +278,9 @@ pub fn register_btc_transaction( // the sender the full gas limit (quirks-frozen-bugs.md §9d). Treating the // two alike diverged mainnet #9,227,292. // - // rskj then parses the transaction and runs bitcoinj's `verify()` on it, - // whose throw fails the transaction too. Only a transaction committed in a - // stored block's merkle tree gets that far, and a block's transactions - // pass `verify()`, so that check is not repeated here. + // rskj then parses the transaction with bitcoinj and runs its `verify()` + // on it (BridgeSupport l.400-401). A throw from either fails the + // transaction too. let validation = if is_btc_tx_processed(ctx, &btc_tx_hash) { Err(Invalid::Swallowed("transaction already processed")) } else { @@ -322,10 +321,10 @@ pub fn register_btc_transaction( } } - // Parse the BTC transaction - let btc_tx: BtcTransaction = deserialize(&btc_tx_data).map_err(|_| { - PrecompileError::other("registerBtcTransaction: invalid BTC transaction") - })?; + // The transaction as bitcoinj reads the bytes, which the tree above has + // proved as they were given: bitcoinj takes more ways of writing one than + // the standard, and every hash from here on is of what it read. + let btc_tx = read_btc_transaction(&btc_tx_data).map_err(PrecompileError::other)?; // rskj re-checks "already processed" using the witness-stripped txid // (BridgeSupport.registerBtcTransaction l.404, `btcTx.getHash(false)`): @@ -2003,9 +2002,10 @@ pub fn register_fast_bridge_btc_transaction( } } - // Parse the BTC tx and compute the witness-stripped txid (getHash(false)). - // A transaction bitcoinj cannot parse throws, which the same catch answers. - let Ok(btc_tx) = deserialize::(&btc_tx_data) else { + // Parse the BTC tx as bitcoinj does, hold it to `verify()` (BridgeSupport + // l.2771-2772), and compute the witness-stripped txid (getHash(false)). A + // throw from either is answered by the same catch. + let Ok(btc_tx) = read_btc_transaction(&btc_tx_data) else { return ok(flyover_code_output(flyover_codes::GENERIC_ERROR)); }; let btc_tx_hash_no_witness = legacy_btc_txid(&btc_tx); @@ -9548,6 +9548,11 @@ mod tests { /// below the height RSKj's regtest reads the pegout index from, and one at /// it. fn regtest_bridge_with(tx: &BtcTransaction) -> impl crate::RskContextTr { + regtest_bridge_with_bytes(&btc_serialize(tx)) + } + + /// The same, with the transaction as the bytes its block's tree holds. + fn regtest_bridge_with_bytes(raw: &[u8]) -> impl crate::RskContextTr { use revm::MainContext; let block = revm::context::BlockEnv { number: U256::from(10u64), @@ -9557,7 +9562,7 @@ mod tests { let mut ctx = revm::Context::mainnet() .with_block(block) .with_chain(crate::raw_storage::RskChainExt::default()); - let root = calculate_btc_tx_hash(&btc_serialize(tx)); + let root = calculate_btc_tx_hash(raw); let mut prev = bitcoin::constants::genesis_block(bitcoin::Network::Regtest) .header .block_hash(); @@ -9682,10 +9687,20 @@ mod tests { height: i64, cfg: &RskHardforkConfig, ) -> (bool, bool, usize, usize) { - let raw = btc_serialize(tx); - let tree = one_leaf_tree(&raw); + outcome_of_bytes(ctx, &btc_serialize(tx), tx, height, cfg) + } + + /// The same, with `tx` given as `raw`. + fn outcome_of_bytes( + ctx: &mut CTX, + raw: &[u8], + tx: &BtcTransaction, + height: i64, + cfg: &RskHardforkConfig, + ) -> (bool, bool, usize, usize) { + let tree = one_leaf_tree(raw); let args = encode::args(&[ - Arg::Bytes(&raw), + Arg::Bytes(raw), Arg::Word(encode::int(height)), Arg::Bytes(&tree), ]); @@ -9758,6 +9773,117 @@ mod tests { assert_eq!(registered(&tx, 349, None, None), (true, false, 2, 0)); } + /// A transaction of two inputs written in ways bitcoinj reads and + /// rust-bitcoin refuses: with a byte left over, with its count of inputs + /// in five bytes, and with segwit's marker and flag before inputs that + /// carry no witness. + fn written_otherwise(tx: &BtcTransaction) -> [Vec; 3] { + assert_eq!(tx.input.len(), 2); + let raw = btc_serialize(tx); + let (version, rest) = raw.split_at(4); + let (body, lock_time) = rest.split_at(rest.len() - 4); + let written = [ + [&raw[..], &[0xFF]].concat(), + [version, &[0xFE, 2, 0, 0, 0], &body[1..], lock_time].concat(), + [version, &[0x00, 0x01], body, &[0x00, 0x00], lock_time].concat(), + ]; + for bytes in &written { + assert!(deserialize::(bytes).is_err()); + } + written + } + + /// rskj reads the transaction with bitcoinj (`tx::parse_btc_transaction`), + /// so one written in a way rust-bitcoin refuses is registered as the same + /// transaction is when written the standard way: the tree proves the bytes + /// given, and the transaction is marked processed by the hash of what was + /// read. And rskj holds what it has read to bitcoinj's `verify()`, whose + /// throw fails the call. + #[test] + fn a_peg_in_is_registered_however_bitcoinj_reads_its_bytes() { + let redeem = build_federation_redeem_script(&test_keys(0x10), 2); + let tx = spend( + vec![signed_multisig(&redeem), signed_multisig(&redeem)], + &[(1_000_000_000, regtest_federation_script())], + ); + // Turned down and refunded, as a multisig's peg-in is: marked + // processed, with three events. + let standard = registered(&tx, 350, None, None); + assert_eq!(standard, (true, true, 3, 0)); + let cfg = RskHardforkConfig::rskj_regtest(); + for bytes in &written_otherwise(&tx) { + let mut ctx = regtest_bridge_with_bytes(bytes); + assert_eq!(outcome_of_bytes(&mut ctx, bytes, &tx, 350, &cfg), standard); + } + + // The same outpoint spent twice: bitcoinj's `DuplicatedOutPoint`. + let mut twice = tx.clone(); + twice.input[1].previous_output = twice.input[0].previous_output; + assert_eq!(registered(&twice, 350, None, None), (false, false, 0, 0)); + } + + /// The flyover method reads its transaction the same way + /// (`BridgeSupport.registerFlyoverBtcTransaction` l.2771-2772): whatever + /// it does with the transaction written the standard way, it does with + /// the others. A throw from bitcoinj it answers with its generic error. + #[test] + fn a_flyover_payment_is_registered_however_bitcoinj_reads_its_bytes() { + let (config, cfg) = ( + BridgeConstants::rskj_regtest(), + RskHardforkConfig::rskj_regtest(), + ); + let lbc = RskAddress::repeat_byte(0x42); + let mut lbc_word = [0u8; 32]; + lbc_word[12..].copy_from_slice(lbc.as_slice()); + let (derivation_arguments, address) = ([0x11; 32], [0x6F; 21]); + // The federation's flyover address for those arguments. + let flyover_address = { + let mut ctx = queue_ctx(); + let (_, redeem) = active_federation_keys_and_redeem(&mut ctx, &config, &cfg, 10); + let format = active_federation_format(&mut ctx, &config, &cfg, 10); + let hash = flyover_derivation_hash(&derivation_arguments, &address, &lbc, &address); + let script = flyover_redeem_script(&hash, &redeem); + p2sh_output_script(&federation_output_hash160(&script, format)) + }; + let payer = build_federation_redeem_script(&test_keys(0x10), 2); + let tx = spend( + vec![signed_multisig(&payer), signed_multisig(&payer)], + &[(1_000_000_000, flyover_address)], + ); + let answer = |bytes: &[u8]| { + let tree = one_leaf_tree(bytes); + let args = encode::args(&[ + Arg::Bytes(bytes), + Arg::Word(encode::int(350)), + Arg::Bytes(&tree), + Arg::Word(derivation_arguments), + Arg::Bytes(&address), + Arg::Word(lbc_word), + Arg::Bytes(&address), + Arg::Word([0; 32]), + ]); + let mut ctx = regtest_bridge_with_bytes(bytes); + let ctx_tx = BridgeTxContext::default(); + register_fast_bridge_btc_transaction(&mut ctx, &args, 0, &config, &cfg, &ctx_tx, lbc, 2) + .unwrap() + .bytes + }; + // This Bridge holds no balance, so every bitcoin counts as locked and + // the payment is over the cap: read, and refunded to its user. + let standard = answer(&btc_serialize(&tx)); + assert_eq!(standard, flyover_code_output(flyover_codes::REFUNDED_USER)); + for bytes in &written_otherwise(&tx) { + assert_eq!(answer(bytes), standard); + } + + let mut twice = tx.clone(); + twice.input[1].previous_output = twice.input[0].previous_output; + assert_eq!( + answer(&btc_serialize(&twice)), + flyover_code_output(flyover_codes::GENERIC_ERROR) + ); + } + /// RSKj looks for its SVP transactions only where it reads the pegout /// index: there a transaction whose stripped hash is the stored fund /// transaction's is registered as it, and below it the legacy rules diff --git a/crates/execution/src/bridge/testdata/bitcoinj_transactions.txt b/crates/execution/src/bridge/testdata/bitcoinj_transactions.txt new file mode 100644 index 0000000..11ff3ee --- /dev/null +++ b/crates/execution/src/bridge/testdata/bitcoinj_transactions.txt @@ -0,0 +1,94 @@ +# What bitcoinj-thin 0.14.4-rsk-18, the Bitcoin library RSKj's release VETIVER-9.0.4 runs on, +# makes of a transaction's bytes: new BtcTransaction(mainnet, bytes), and what that then gives +# for getMessageSize(), getHash(false), getHash(true), verify() and bitcoinSerialize(). +# Lines: a label; the bytes, or a hyphen for none; then "throws" where the constructor threw, +# or "ok", the size, the hash without witnesses, the hash with them, "ok" or the exception +# verify() threw, and the transaction as bitcoinj writes it again. +canonical 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e803000000000000015100000000 ok 63 08d04ba97cb71079aafd2b86214b7faa318fb69ecafc4d512c4cf372bafdbdd1 08d04ba97cb71079aafd2b86214b7faa318fb69ecafc4d512c4cf372bafdbdd1 ok 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e803000000000000015100000000 +two_in_two_out 0100000002aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffffbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb01000000025151ffffffff02e8030000000000000151d00700000000000002515200000000 ok 117 4f60d291cd5571e9cdbe828b0db4e743b77f93d8581db92733dc42fa064c2e0c 4f60d291cd5571e9cdbe828b0db4e743b77f93d8581db92733dc42fa064c2e0c ok 0100000002aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffffbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb01000000025151ffffffff02e8030000000000000151d00700000000000002515200000000 +big_version ffffffff01aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e803000000000000015100000000 ok 63 fa317c8b8fdd3d1660f0212f372bae8cc0ca2965a72cd0d815a4a80f2734cf0d fa317c8b8fdd3d1660f0212f372bae8cc0ca2965a72cd0d815a4a80f2734cf0d ok ffffffff01aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e803000000000000015100000000 +lock_time_set 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e8030000000000000151ffffffff ok 63 840aac856a526db3704b242ce3542e130157006bfac3c5687b8710e8a20470ec 840aac856a526db3704b242ce3542e130157006bfac3c5687b8710e8a20470ec ok 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e8030000000000000151ffffffff +trailing_byte 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e80300000000000001510000000000 ok 63 08d04ba97cb71079aafd2b86214b7faa318fb69ecafc4d512c4cf372bafdbdd1 08d04ba97cb71079aafd2b86214b7faa318fb69ecafc4d512c4cf372bafdbdd1 ok 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e803000000000000015100000000 +trailing_bytes 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e803000000000000015100000000deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef ok 63 08d04ba97cb71079aafd2b86214b7faa318fb69ecafc4d512c4cf372bafdbdd1 08d04ba97cb71079aafd2b86214b7faa318fb69ecafc4d512c4cf372bafdbdd1 ok 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e803000000000000015100000000 +count_in_fd 01000000fd0100aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e803000000000000015100000000 ok 65 08d04ba97cb71079aafd2b86214b7faa318fb69ecafc4d512c4cf372bafdbdd1 08d04ba97cb71079aafd2b86214b7faa318fb69ecafc4d512c4cf372bafdbdd1 ok 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e803000000000000015100000000 +count_in_fe 01000000fe01000000aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e803000000000000015100000000 ok 67 08d04ba97cb71079aafd2b86214b7faa318fb69ecafc4d512c4cf372bafdbdd1 08d04ba97cb71079aafd2b86214b7faa318fb69ecafc4d512c4cf372bafdbdd1 ok 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e803000000000000015100000000 +count_in_ff 01000000ff0100000000000000aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e803000000000000015100000000 ok 71 08d04ba97cb71079aafd2b86214b7faa318fb69ecafc4d512c4cf372bafdbdd1 08d04ba97cb71079aafd2b86214b7faa318fb69ecafc4d512c4cf372bafdbdd1 ok 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e803000000000000015100000000 +script_len_fd 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000fd02005151ffffffff01e803000000000000015100000000 ok 65 08d04ba97cb71079aafd2b86214b7faa318fb69ecafc4d512c4cf372bafdbdd1 08d04ba97cb71079aafd2b86214b7faa318fb69ecafc4d512c4cf372bafdbdd1 ok 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e803000000000000015100000000 +script_len_fe 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000fe020000005151ffffffff01e803000000000000015100000000 ok 67 08d04ba97cb71079aafd2b86214b7faa318fb69ecafc4d512c4cf372bafdbdd1 08d04ba97cb71079aafd2b86214b7faa318fb69ecafc4d512c4cf372bafdbdd1 ok 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e803000000000000015100000000 +script_len_ff 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000ff02000000000000005151ffffffff01e803000000000000015100000000 ok 71 08d04ba97cb71079aafd2b86214b7faa318fb69ecafc4d512c4cf372bafdbdd1 08d04ba97cb71079aafd2b86214b7faa318fb69ecafc4d512c4cf372bafdbdd1 ok 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e803000000000000015100000000 +count_out_fd 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151fffffffffd0100e803000000000000015100000000 ok 65 08d04ba97cb71079aafd2b86214b7faa318fb69ecafc4d512c4cf372bafdbdd1 08d04ba97cb71079aafd2b86214b7faa318fb69ecafc4d512c4cf372bafdbdd1 ok 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e803000000000000015100000000 +count_out_fe 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151fffffffffe01000000e803000000000000015100000000 ok 67 08d04ba97cb71079aafd2b86214b7faa318fb69ecafc4d512c4cf372bafdbdd1 08d04ba97cb71079aafd2b86214b7faa318fb69ecafc4d512c4cf372bafdbdd1 ok 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e803000000000000015100000000 +count_out_ff 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffffff0100000000000000e803000000000000015100000000 ok 71 08d04ba97cb71079aafd2b86214b7faa318fb69ecafc4d512c4cf372bafdbdd1 08d04ba97cb71079aafd2b86214b7faa318fb69ecafc4d512c4cf372bafdbdd1 ok 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e803000000000000015100000000 +out_script_len_fd 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e803000000000000fd01005100000000 ok 65 08d04ba97cb71079aafd2b86214b7faa318fb69ecafc4d512c4cf372bafdbdd1 08d04ba97cb71079aafd2b86214b7faa318fb69ecafc4d512c4cf372bafdbdd1 ok 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e803000000000000015100000000 +out_script_len_fe 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e803000000000000fe010000005100000000 ok 67 08d04ba97cb71079aafd2b86214b7faa318fb69ecafc4d512c4cf372bafdbdd1 08d04ba97cb71079aafd2b86214b7faa318fb69ecafc4d512c4cf372bafdbdd1 ok 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e803000000000000015100000000 +out_script_len_ff 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e803000000000000ff01000000000000005100000000 ok 71 08d04ba97cb71079aafd2b86214b7faa318fb69ecafc4d512c4cf372bafdbdd1 08d04ba97cb71079aafd2b86214b7faa318fb69ecafc4d512c4cf372bafdbdd1 ok 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e803000000000000015100000000 +every_varint_long 01000000fd0100aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000ff02000000000000005151ffffffffff0100000000000000e803000000000000fe010000005100000000 ok 85 08d04ba97cb71079aafd2b86214b7faa318fb69ecafc4d512c4cf372bafdbdd1 08d04ba97cb71079aafd2b86214b7faa318fb69ecafc4d512c4cf372bafdbdd1 ok 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e803000000000000015100000000 +count_in_ff_high_word 01000000ff0100000001000000aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e803000000000000015100000000 throws +count_in_ff_negative 01000000ff0100000000000080aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e803000000000000015100000000 throws +count_in_ff_negative_then_segwit 01000000ff01000000000000800101aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e80300000000000001510000000000 ok 74 08d04ba97cb71079aafd2b86214b7faa318fb69ecafc4d512c4cf372bafdbdd1 08d04ba97cb71079aafd2b86214b7faa318fb69ecafc4d512c4cf372bafdbdd1 ok 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e803000000000000015100000000 +count_in_ff_negative_then_flag0 01000000ff01000000000000800000000000 ok 18 d21633ba23f70118185227be58a63527675641ad37967e2aa461559f577aec43 d21633ba23f70118185227be58a63527675641ad37967e2aa461559f577aec43 EmptyInputsOrOutputs 01000000000000000000 +count_in_ff_low_word_negative 01000000ff0000008000000000aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e803000000000000015100000000 throws +count_in_fe_negative 01000000fe00000080aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e803000000000000015100000000 throws +count_in_ff_all_ones 01000000ffffffffffffffffffaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e803000000000000015100000000 throws +script_len_ff_high_word 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000ff02000000010000005151ffffffff01e803000000000000015100000000 ok 71 08d04ba97cb71079aafd2b86214b7faa318fb69ecafc4d512c4cf372bafdbdd1 08d04ba97cb71079aafd2b86214b7faa318fb69ecafc4d512c4cf372bafdbdd1 ok 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e803000000000000015100000000 +script_len_ff_negative_long 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000ff02000000000000805151ffffffff01e803000000000000015100000000 ok 71 08d04ba97cb71079aafd2b86214b7faa318fb69ecafc4d512c4cf372bafdbdd1 08d04ba97cb71079aafd2b86214b7faa318fb69ecafc4d512c4cf372bafdbdd1 ok 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e803000000000000015100000000 +script_len_fe_negative 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000fe000000805151ffffffff01e803000000000000015100000000 throws +script_len_over_max 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000fe010000025151ffffffff01e803000000000000015100000000 throws +script_len_beyond 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000fd00105151ffffffff01e803000000000000015100000000 throws +count_out_ff_negative 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffffff0100000000000080e803000000000000015100000000 ok 61 65996783f69907f9be218a8bc9217d9daded29474940dc2568a5899aa5495645 65996783f69907f9be218a8bc9217d9daded29474940dc2568a5899aa5495645 EmptyInputsOrOutputs 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff00e8030000 +count_out_ff_low_word_negative 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffffff0000008000000000e803000000000000015100000000 throws +count_out_ff_high_word 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffffff0100000001000000e803000000000000015100000000 throws +out_script_len_ff_high_word 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e803000000000000ff01000000010000005100000000 ok 71 08d04ba97cb71079aafd2b86214b7faa318fb69ecafc4d512c4cf372bafdbdd1 08d04ba97cb71079aafd2b86214b7faa318fb69ecafc4d512c4cf372bafdbdd1 ok 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e803000000000000015100000000 +out_script_len_fe_negative 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e803000000000000fe000000805100000000 throws +out_script_len_over_max 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e803000000000000fe010000025100000000 throws +segwit_witness 01000000000101aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e80300000000000001510102abcd00000000 ok 69 08d04ba97cb71079aafd2b86214b7faa318fb69ecafc4d512c4cf372bafdbdd1 6814eeb10b43253367f1ee4e5a3500eddb1167970a97cd0e82b308c59f3b1c6f ok 01000000000101aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e80300000000000001510102abcd00000000 +segwit_two_pushes_one_empty 01000000000101aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e8030000000000000151020001ff00000000 ok 69 08d04ba97cb71079aafd2b86214b7faa318fb69ecafc4d512c4cf372bafdbdd1 264b09c320a0bd203f63fb02f4560fbba2102284330fe15152603917652932df ok 01000000000101aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e8030000000000000151020001ff00000000 +segwit_empty_witness 01000000000101aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e80300000000000001510000000000 ok 66 08d04ba97cb71079aafd2b86214b7faa318fb69ecafc4d512c4cf372bafdbdd1 08d04ba97cb71079aafd2b86214b7faa318fb69ecafc4d512c4cf372bafdbdd1 ok 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e803000000000000015100000000 +segwit_one_empty_push 01000000000101aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e8030000000000000151010000000000 ok 67 08d04ba97cb71079aafd2b86214b7faa318fb69ecafc4d512c4cf372bafdbdd1 dc494607172f192506348fc9cd0c038e85ada21906fd8253cedf989a172ca48c ok 01000000000101aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e8030000000000000151010000000000 +segwit_two_inputs 01000000000102aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffffbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb01000000025151ffffffff01e80300000000000001510101aa0000000000 ok 112 25037669e4838cf7c916a454cbd4d3db9afe34e06d46ea7aa737c829ee28bc75 5f041f2aef2019490419d584ac4d5e609aaf0e746f27ce44dc966a2ad1db2935 ok 01000000000102aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffffbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb01000000025151ffffffff01e80300000000000001510101aa0000000000 +segwit_two_inputs_no_witnesses 01000000000102aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffffbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb01000000025151ffffffff01e8030000000000000151000000000000 ok 110 25037669e4838cf7c916a454cbd4d3db9afe34e06d46ea7aa737c829ee28bc75 25037669e4838cf7c916a454cbd4d3db9afe34e06d46ea7aa737c829ee28bc75 ok 0100000002aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffffbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb01000000025151ffffffff01e803000000000000015100000000 +segwit_flag_02 01000000000201aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e803000000000000015100000000 throws +segwit_flag_03 01000000000301aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e80300000000000001510000000000 throws +segwit_flag_81 01000000008101aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e80300000000000001510000000000 throws +segwit_flag_ff 0100000000ff01aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e80300000000000001510000000000 throws +segwit_trailing 01000000000101aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e80300000000000001510102abcd000000000102 ok 69 08d04ba97cb71079aafd2b86214b7faa318fb69ecafc4d512c4cf372bafdbdd1 6814eeb10b43253367f1ee4e5a3500eddb1167970a97cd0e82b308c59f3b1c6f ok 01000000000101aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e80300000000000001510102abcd00000000 +wit_count_fd 01000000000101aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e8030000000000000151fd010002abcd00000000 ok 71 08d04ba97cb71079aafd2b86214b7faa318fb69ecafc4d512c4cf372bafdbdd1 6814eeb10b43253367f1ee4e5a3500eddb1167970a97cd0e82b308c59f3b1c6f ok 01000000000101aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e80300000000000001510102abcd00000000 +wit_count_ff 01000000000101aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e8030000000000000151ff010000000000000002abcd00000000 ok 77 08d04ba97cb71079aafd2b86214b7faa318fb69ecafc4d512c4cf372bafdbdd1 6814eeb10b43253367f1ee4e5a3500eddb1167970a97cd0e82b308c59f3b1c6f ok 01000000000101aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e80300000000000001510102abcd00000000 +wit_size_fd 01000000000101aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e803000000000000015101fd0200abcd00000000 ok 71 08d04ba97cb71079aafd2b86214b7faa318fb69ecafc4d512c4cf372bafdbdd1 6814eeb10b43253367f1ee4e5a3500eddb1167970a97cd0e82b308c59f3b1c6f ok 01000000000101aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e80300000000000001510102abcd00000000 +wit_size_fe 01000000000101aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e803000000000000015101fe02000000abcd00000000 ok 73 08d04ba97cb71079aafd2b86214b7faa318fb69ecafc4d512c4cf372bafdbdd1 6814eeb10b43253367f1ee4e5a3500eddb1167970a97cd0e82b308c59f3b1c6f ok 01000000000101aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e80300000000000001510102abcd00000000 +wit_size_ff 01000000000101aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e803000000000000015101ff0200000000000000abcd00000000 ok 77 08d04ba97cb71079aafd2b86214b7faa318fb69ecafc4d512c4cf372bafdbdd1 6814eeb10b43253367f1ee4e5a3500eddb1167970a97cd0e82b308c59f3b1c6f ok 01000000000101aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e80300000000000001510102abcd00000000 +wit_size_ff_high_word 01000000000101aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e803000000000000015101ff0200000001000000abcd00000000 ok 77 08d04ba97cb71079aafd2b86214b7faa318fb69ecafc4d512c4cf372bafdbdd1 6814eeb10b43253367f1ee4e5a3500eddb1167970a97cd0e82b308c59f3b1c6f ok 01000000000101aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e80300000000000001510102abcd00000000 +wit_count_ff_negative 01000000000101aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e8030000000000000151ff010000000000008000000000 ok 74 08d04ba97cb71079aafd2b86214b7faa318fb69ecafc4d512c4cf372bafdbdd1 08d04ba97cb71079aafd2b86214b7faa318fb69ecafc4d512c4cf372bafdbdd1 ok 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e803000000000000015100000000 +wit_count_ff_high_word 01000000000101aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e8030000000000000151ff010000000100000002abcd00000000 throws +wit_count_fe_negative 01000000000101aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e8030000000000000151fe0000008000000000 throws +wit_size_fe_negative 01000000000101aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e803000000000000015101fe0000008000000000 throws +wit_size_over_max 01000000000101aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e803000000000000015101fe0100000200000000 throws +wit_size_beyond 01000000000101aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e80300000000000001510110abcd00000000 throws +wit_missing 01000000000101aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e803000000000000015100000000 throws +zero_inputs_flag0 01000000000000000000 ok 10 d21633ba23f70118185227be58a63527675641ad37967e2aa461559f577aec43 d21633ba23f70118185227be58a63527675641ad37967e2aa461559f577aec43 EmptyInputsOrOutputs 01000000000000000000 +zero_inputs_flag0_trailing 01000000000000000000ffff ok 10 d21633ba23f70118185227be58a63527675641ad37967e2aa461559f577aec43 d21633ba23f70118185227be58a63527675641ad37967e2aa461559f577aec43 EmptyInputsOrOutputs 01000000000000000000 +zero_inputs_flag1_zero_again 0100000000010001e803000000000000015100000000 ok 22 92f06005f51981ae89f855306f6498bc8b764c4d7f6cbdb4881dc56d4d77314f 92f06005f51981ae89f855306f6498bc8b764c4d7f6cbdb4881dc56d4d77314f EmptyInputsOrOutputs 010000000001e803000000000000015100000000 +zero_inputs_flag1_all_empty 010000000001000000000000 ok 12 d21633ba23f70118185227be58a63527675641ad37967e2aa461559f577aec43 d21633ba23f70118185227be58a63527675641ad37967e2aa461559f577aec43 EmptyInputsOrOutputs 01000000000000000000 +zero_inputs_flag2_all_empty 010000000002000000000000 throws +no_outputs 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff0000000000 ok 53 357aa8d907237f4b897e075c2b2ec559b29d23002831d99d4e0feab33150b43f 357aa8d907237f4b897e075c2b2ec559b29d23002831d99d4e0feab33150b43f EmptyInputsOrOutputs 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff0000000000 +negative_value 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01ffffffffffffffff015100000000 ok 63 bd6616c6951296100ef66980e6a4df5754adf94e046131f0a8aecf8bb072b1d8 bd6616c6951296100ef66980e6a4df5754adf94e046131f0a8aecf8bb072b1d8 NegativeValueOutput 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01ffffffffffffffff015100000000 +least_value 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff010000000000000080015100000000 ok 63 ee864c9625de231b2adcc7d1f0302e8b9dd828e009aa65988c0412c3a0357511 ee864c9625de231b2adcc7d1f0302e8b9dd828e009aa65988c0412c3a0357511 NegativeValueOutput 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff010000000000000080015100000000 +max_money 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff010040075af0750700015100000000 ok 63 c411d878a419030c280c86aff71a1c36217ae4aeb41935fb4d491d85b7e84ae8 c411d878a419030c280c86aff71a1c36217ae4aeb41935fb4d491d85b7e84ae8 ok 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff010040075af0750700015100000000 +over_max_money 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff010140075af0750700015100000000 ok 63 39951fe12f5d9f2dd32c9e62e4f83ff85b49c2bd4418bf5630a7ec2c9340f44d 39951fe12f5d9f2dd32c9e62e4f83ff85b49c2bd4418bf5630a7ec2c9340f44d ExcessiveValue 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff010140075af0750700015100000000 +sum_over_max_money 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff020040075af075070001510100000000000000015100000000 ok 73 33aed5ad28cf28b8c5942ef750c81d24ed480cbfd831ebc2e952c504316258fa 33aed5ad28cf28b8c5942ef750c81d24ed480cbfd831ebc2e952c504316258fa ExcessiveValue 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff020040075af075070001510100000000000000015100000000 +greatest_value 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01ffffffffffffff7f015100000000 ok 63 c1dc634c89c33fd79c186f2d4e0f3f44d583ba0ac27c62de13daf177c2405f10 c1dc634c89c33fd79c186f2d4e0f3f44d583ba0ac27c62de13daf177c2405f10 ExcessiveValue 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01ffffffffffffff7f015100000000 +same_outpoint_twice 0100000002aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffffaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e803000000000000015100000000 ok 106 1a1f2990bbc217baef581449704dd893f995e5b9ae0baccbf6cac0f00d724ba7 1a1f2990bbc217baef581449704dd893f995e5b9ae0baccbf6cac0f00d724ba7 DuplicatedOutPoint 0100000002aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffffaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e803000000000000015100000000 +coinbase 01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff025151ffffffff01e803000000000000015100000000 ok 63 e60829719d256cbec1e3e3ac4c86606c18fa612bf2eaa874521ba81e55124dab e60829719d256cbec1e3e3ac4c86606c18fa612bf2eaa874521ba81e55124dab ok 01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff025151ffffffff01e803000000000000015100000000 +coinbase_script_of_1 01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0151ffffffff01e803000000000000015100000000 ok 62 d4083656c3d987caa269be4beab7b084d56841e225e045c782ed9c7cbc437fa3 d4083656c3d987caa269be4beab7b084d56841e225e045c782ed9c7cbc437fa3 CoinbaseScriptSizeOutOfRange 01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0151ffffffff01e803000000000000015100000000 +coinbase_script_of_100 01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff6451515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151ffffffff01e803000000000000015100000000 ok 161 3d35773ea9d75c46ea7649ca3c45bc5cad1008131f6f86640e93d4130ac8ab49 3d35773ea9d75c46ea7649ca3c45bc5cad1008131f6f86640e93d4130ac8ab49 ok 01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff6451515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151ffffffff01e803000000000000015100000000 +coinbase_script_of_101 01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff655151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151ffffffff01e803000000000000015100000000 ok 162 9616442f94636e2e1363ad6774eaf01b317860528fc5a0900f74903b1abd461d 9616442f94636e2e1363ad6774eaf01b317860528fc5a0900f74903b1abd461d CoinbaseScriptSizeOutOfRange 01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff655151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151ffffffff01e803000000000000015100000000 +coinbase_input_among_others 0100000002aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff0000000000000000000000000000000000000000000000000000000000000000ffffffff025151ffffffff01e803000000000000015100000000 ok 106 2c0b184f549a2beac6cad7c6f5f19624ca01a90733908d27bffd697dfeb988d1 2c0b184f549a2beac6cad7c6f5f19624ca01a90733908d27bffd697dfeb988d1 UnexpectedCoinbaseInput 0100000002aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff0000000000000000000000000000000000000000000000000000000000000000ffffffff025151ffffffff01e803000000000000015100000000 +two_coinbase_inputs 01000000020000000000000000000000000000000000000000000000000000000000000000ffffffff025151ffffffff0000000000000000000000000000000000000000000000000000000000000000ffffffff025151feffffff01e803000000000000015100000000 ok 106 1cfebceb133d7b9793c4475e6d0fc0fa268f853d7053c7bc072f750f4950d7e7 1cfebceb133d7b9793c4475e6d0fc0fa268f853d7053c7bc072f750f4950d7e7 DuplicatedOutPoint 01000000020000000000000000000000000000000000000000000000000000000000000000ffffffff025151ffffffff0000000000000000000000000000000000000000000000000000000000000000ffffffff025151feffffff01e803000000000000015100000000 +null_hash_index_0 0100000001000000000000000000000000000000000000000000000000000000000000000000000000025151ffffffff01e803000000000000015100000000 ok 63 cccc3042d5e1748976d3ec9cecc63be9d9aa8a97ad72616c39f6adc0e534f4a8 cccc3042d5e1748976d3ec9cecc63be9d9aa8a97ad72616c39f6adc0e534f4a8 ok 0100000001000000000000000000000000000000000000000000000000000000000000000000000000025151ffffffff01e803000000000000015100000000 +truncated_lock_time 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e8030000000000000151000000 throws +truncated_in_input 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa throws +truncated_in_output 0100000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00000000025151ffffffff01e803000000000000 throws +nothing - throws +version_only 01000000 throws +version_and_zero 0100000000 throws +three_bytes 010000 throws diff --git a/crates/execution/src/bridge/tx.rs b/crates/execution/src/bridge/tx.rs index fc06b4c..83da6a8 100644 --- a/crates/execution/src/bridge/tx.rs +++ b/crates/execution/src/bridge/tx.rs @@ -254,9 +254,7 @@ pub fn register_btc_coinbase_transaction( // rskj parses the transaction and runs bitcoinj's `btcTx.verify()` on it // (BridgeSupport l.2559-2560). Either throws. - let btc_tx: bitcoin::Transaction = bitcoin::consensus::deserialize(&btc_tx_data) - .map_err(|_| PrecompileError::other("registerBtcCoinbaseTransaction: invalid btc tx"))?; - verify_btc_transaction(&btc_tx, btc_tx_data.len()).map_err(PrecompileError::other)?; + let btc_tx = read_btc_transaction(&btc_tx_data).map_err(PrecompileError::other)?; // `findWitnessCommitment` opens with `Preconditions.checkState(tx.isCoinBase())`: // only a block's coinbase names its witness root. @@ -533,7 +531,8 @@ const MAX_MONEY: i64 = 21_000_000 * 100_000_000; /// bitcoinj `BtcTransaction.verify()`. Every failure is an exception /// `Bridge.execute` rethrows. `serialized_size` is the transaction's -/// `getMessageSize()`: the bytes it was parsed from. +/// `getMessageSize()`: how many bytes it was parsed from, not counting any +/// left over after it. fn verify_btc_transaction( tx: &bitcoin::Transaction, serialized_size: usize, @@ -575,6 +574,156 @@ fn verify_btc_transaction( Ok(()) } +/// bitcoinj `Message.MAX_SIZE`: the most bytes `readBytes` reads at once. +const MAX_READ_SIZE: i32 = 0x0200_0000; + +/// A transaction's bytes, read as bitcoinj's `Message` reads them. A `None` +/// is a throw. +struct BitcoinjReader<'a> { + payload: &'a [u8], + cursor: usize, +} + +impl<'a> BitcoinjReader<'a> { + /// `readBytes`. The length is an `int`, and one that is negative, over + /// `MAX_SIZE` or past the end throws. + fn bytes(&mut self, length: i32) -> Option<&'a [u8]> { + if !(0..=MAX_READ_SIZE).contains(&length) { + return None; + } + let end = self.cursor.checked_add(length as usize)?; + let bytes = self.payload.get(self.cursor..end)?; + self.cursor = end; + Some(bytes) + } + + fn array(&mut self) -> Option<[u8; N]> { + self.bytes(N as i32)?.try_into().ok() + } + + /// `readVarInt`: a `long` from one, three, five or nine bytes, whichever + /// the first byte says and however small the number. So a number may be + /// written longer than it needs, and in nine bytes it may be negative. + fn var_int(&mut self) -> Option { + Some(match self.array::<1>()?[0] { + 253 => i64::from(u16::from_le_bytes(self.array()?)), + 254 => i64::from(u32::from_le_bytes(self.array()?)), + 255 => i64::from_le_bytes(self.array()?), + short => i64::from(short), + }) + } + + /// How many inputs, outputs or pushes follow. bitcoinj makes room for + /// them with `new ArrayList<>(Math.min((int) count, 20))`, which throws + /// for a count whose low 32 bits are negative as an `int`, and then loops + /// while a `long` index is under the count: for a negative one, never. + fn count(&mut self) -> Option { + let count = self.var_int()?; + ((count as i32) >= 0).then_some(count.max(0) as u64) + } + + /// A script or a push: its length, which bitcoinj casts to an `int`, and + /// that many bytes. + fn var_bytes(&mut self) -> Option<&'a [u8]> { + let length = self.var_int()? as i32; + self.bytes(length) + } + + /// `readInputs`. A count is no promise: the loop ends where the bytes do. + fn inputs(&mut self) -> Option> { + let mut inputs = Vec::new(); + for _ in 0..self.count()? { + let txid = bitcoin::Txid::from_byte_array(self.array()?); + let vout = u32::from_le_bytes(self.array()?); + let script_sig = bitcoin::ScriptBuf::from_bytes(self.var_bytes()?.to_vec()); + let sequence = bitcoin::Sequence(u32::from_le_bytes(self.array()?)); + inputs.push(bitcoin::TxIn { + previous_output: bitcoin::OutPoint { txid, vout }, + script_sig, + sequence, + witness: bitcoin::Witness::new(), + }); + } + Some(inputs) + } + + /// `readOutputs`. bitcoinj reads a value as a signed number. Here it + /// keeps its bits, which `verify_btc_transaction` reads as bitcoinj does. + fn outputs(&mut self) -> Option> { + let mut outputs = Vec::new(); + for _ in 0..self.count()? { + let value = bitcoin::Amount::from_sat(u64::from_le_bytes(self.array()?)); + let script_pubkey = bitcoin::ScriptBuf::from_bytes(self.var_bytes()?.to_vec()); + outputs.push(bitcoin::TxOut { + value, + script_pubkey, + }); + } + Some(outputs) + } +} + +/// A Bitcoin transaction as bitcoinj reads the bytes a Bridge method is given +/// (`new BtcTransaction(params, bytes)`, which is `BtcTransaction.parse`), +/// and how many of them it read, its `getMessageSize()`. `None` where +/// bitcoinj throws. +/// +/// bitcoinj takes what rust-bitcoin's `deserialize` refuses: bytes left over +/// after the transaction; a count or a length written in more bytes than it +/// needs, or in nine of which it then drops the high four; and a segwit +/// marker whose flag is 1 before inputs that carry no witness, or 0 before +/// nothing at all. What it has read it writes again in the one standard way, +/// so the transaction's hashes are those of the standard form, whatever the +/// bytes given hash to. +pub fn parse_btc_transaction(payload: &[u8]) -> Option<(bitcoin::Transaction, usize)> { + let mut reader = BitcoinjReader { payload, cursor: 0 }; + let version = bitcoin::transaction::Version(i32::from_le_bytes(reader.array()?)); + let mut input = reader.inputs()?; + // No inputs is segwit's marker, and the byte after it holds the flags. + let mut flags = 0; + let output = if input.is_empty() { + flags = reader.array::<1>()?[0]; + if flags != 0 { + input = reader.inputs()?; + reader.outputs()? + } else { + Vec::new() + } + } else { + reader.outputs()? + }; + if flags & 1 != 0 { + flags ^= 1; + for spent in &mut input { + for _ in 0..reader.count()? { + spent.witness.push(reader.var_bytes()?); + } + } + } + if flags != 0 { + return None; + } + let lock_time = + bitcoin::absolute::LockTime::from_consensus(u32::from_le_bytes(reader.array()?)); + let tx = bitcoin::Transaction { + version, + lock_time, + input, + output, + }; + Some((tx, reader.cursor)) +} + +/// The transaction a Bridge method is given, as rskj takes it: parsed by +/// bitcoinj and then held to `BtcTransaction.verify()`. An error is a throw of +/// either. +pub fn read_btc_transaction(payload: &[u8]) -> Result { + let (tx, size) = + parse_btc_transaction(payload).ok_or("bitcoinj can't parse the transaction")?; + verify_btc_transaction(&tx, size)?; + Ok(tx) +} + fn encode_int_output(gas_cost: u64, value: i64) -> PrecompileOutput { let mut output = [0u8; 32]; if value >= 0 { @@ -972,6 +1121,107 @@ mod tests { } } + /// The file's transactions as bitcoinj reads them, or refuses to: the size + /// it counts, both hashes, what `verify()` makes of each, and the bytes it + /// writes again. They are written every way a count or a length can be, + /// with bytes left over, with segwit's marker and each kind of flag and + /// witness, cut short, and with the values and inputs `verify()` refuses. + #[test] + fn reads_a_transaction_as_bitcoinj_reads_it() { + let vectors = include_str!("testdata/bitcoinj_transactions.txt"); + let (mut read, mut refused_by_rust_bitcoin) = (0, 0); + for line in vectors.lines().filter(|line| !line.starts_with('#')) { + let fields: Vec<&str> = line.split(' ').collect(); + let label = fields[0]; + let payload = match fields[1] { + "-" => Vec::new(), + given => hex::decode(given).unwrap(), + }; + let ours = parse_btc_transaction(&payload); + read += 1; + if fields[2] == "throws" { + assert!(ours.is_none(), "{label}: bitcoinj throws"); + assert!(read_btc_transaction(&payload).is_err(), "{label}"); + continue; + } + let (tx, size) = ours.unwrap_or_else(|| panic!("{label}: bitcoinj reads it")); + assert_eq!(size.to_string(), fields[3], "{label}: its size"); + assert_eq!( + tx.compute_txid().to_string(), + fields[4], + "{label}: its hash" + ); + // rust-bitcoin writes a transaction without inputs in segwit's form, + // which bitcoinj doesn't. `verify()` refuses one before anything + // asks for more than the hash above. + if !tx.input.is_empty() { + assert_eq!( + tx.compute_wtxid().to_string(), + fields[5], + "{label}: its hash with witnesses" + ); + assert_eq!( + hex::encode(bitcoin::consensus::serialize(&tx)), + fields[7], + "{label}: written again" + ); + } + let verified = verify_btc_transaction(&tx, size).is_ok(); + assert_eq!(verified, fields[6] == "ok", "{label}: verify()"); + assert_eq!( + read_btc_transaction(&payload).ok(), + verified.then_some(tx.clone()) + ); + // What rust-bitcoin reads as well, it reads the same. + match bitcoin::consensus::deserialize::(&payload) { + Ok(theirs) => assert_eq!(theirs, tx, "{label}"), + Err(_) => refused_by_rust_bitcoin += 1, + } + } + assert_eq!(read, 88); + // Of the 58 that bitcoinj reads. + assert_eq!(refused_by_rust_bitcoin, 33); + } + + /// rskj reads the coinbase with bitcoinj, so one written in a way + /// rust-bitcoin refuses is registered all the same: it is the bytes given + /// that the block's tree holds, and the transaction read from them that + /// carries the commitment. `verify()` holds it to a block's size by the + /// bytes read, not by those given. + #[test] + fn a_coinbase_is_registered_however_bitcoinj_reads_its_bytes() { + let tx = coinbase( + vec![0x51, 0x51], + vec![output(50, vec![0x51]), commitment_output()], + ); + let (version, rest) = tx.split_at(4); + let (body, lock_time) = rest.split_at(rest.len() - 4); + let written = [ + // Bytes left over after the transaction. + [&tx[..], &[0x00]].concat(), + // The count of inputs in three bytes. + [version, &[0xfd, 0x01, 0x00], &body[1..], lock_time].concat(), + // Segwit's marker and flag, and an input without a witness. + [version, &[0x00, 0x01], body, &[0x00], lock_time].concat(), + ]; + for raw in &written { + assert!(bitcoin::consensus::deserialize::(raw).is_err()); + assert_eq!(register_sole_transaction(raw, AFTER_RSKIP460), REGISTERED); + } + + // A transaction of exactly a block's size, and ten bytes after it. + let largest = coinbase( + vec![0x51, 0x51], + vec![output(50, vec![0x51; 999_887]), commitment_output()], + ); + assert_eq!(largest.len(), MAX_BLOCK_SIZE); + let followed = [&largest[..], &[0u8; 10]].concat(); + assert_eq!( + register_sole_transaction(&followed, AFTER_RSKIP460), + REGISTERED + ); + } + /// Before RSKIP460 rskj reads each output's script with /// `getScriptPubKey().getProgram()`, which throws a `ScriptException` for /// one bitcoinj can't parse. It reads them last to first and stops at the From f7ab2e40fab2fbc70fceb9f50bd9d1f81a1729f9 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Thu, 12 Nov 2026 21:10:13 -0800 Subject: [PATCH 125/264] execution: a transaction that runs no program has rskj's trace rskj traces every transaction of a block, also one that no program runs for: a plain transfer, a call straight to a precompile, a creation with no init code. TransactionExecutor.extractTrace makes that trace from the transaction alone: no gas, no error, no revert flag, and for subtraces the transfers the precompile made. Three things differed here. A transaction to the Bridge that runs outside the EVM had no trace at all. All of them run there before RSKIP136, and the federation's free ones until Bridge transactions are paid for (areBridgeTxsPaid). trace_block, trace_filter and trace_transaction pair a block's traces with its transactions by position, so every later transaction of the block was answered with the trace of the one after it, and the last one with nothing. It now has the trace rskj gives it, made as REMASC's is. A call straight to a precompile with too little gas for it fails before anything runs. Its trace carried the halt as an error, where rskj's carries a result and no error. ProgramTrace now says whether a program ran, and only then takes the error and the revert flag. A creation with no init code was traced with the gas it was given: revm builds an interpreter for it and rskj builds no Program, so rskj's trace has no gas. Tests: a_bridge_transaction_run_outside_the_evm_has_its_trace and a_transaction_that_runs_no_program_is_traced_without_gas_or_error. --- crates/execution/src/executor.rs | 118 ++++++++++++++++++++++++++++++- crates/execution/src/tracer.rs | 19 +++-- docs/trace-namespace.md | 16 ++++- 3 files changed, 144 insertions(+), 9 deletions(-) diff --git a/crates/execution/src/executor.rs b/crates/execution/src/executor.rs index 8e3d5d5..5a77598 100644 --- a/crates/execution/src/executor.rs +++ b/crates/execution/src/executor.rs @@ -1974,6 +1974,32 @@ impl RskExecutor { (false, Vec::new(), Vec::new()) }; + // Every transaction's, traced or not, so that one's can't reach + // the next transaction's trace. + let transfers = std::mem::take(&mut evm.ctx.chain_mut().precompile_transfers); + if traced.includes(i) { + // The Bridge never enters the EVM here, so the tracer + // records nothing, but rskj traces the transaction all + // the same (`TransactionExecutor.extractTrace`'s + // `program == null` branch, as for REMASC above): its own + // call with no gas, and a CALL subtrace for each transfer + // the Bridge made. Left out, every later transaction of + // the block would be answered with the trace of the one + // after it. + let subtraces = crate::tracer::precompile_subtraces( + Some(crate::precompiles::BRIDGE_ADDR), + success, + transfers, + ); + traces.insert( + i, + crate::tracer::ProgramTrace { + subtraces: subtraces.unwrap_or_default(), + ..Default::default() + }, + ); + } + // Fees go to REMASC like the revm path (block beneficiary). // An invisible exception charges the full gas limit // (rskj TransactionExecutionSummary.getFee when failed). @@ -2454,8 +2480,14 @@ impl TracedOutcome { fn apply_to(self, trace: &mut crate::tracer::ProgramTrace) { trace.contract_address = self.contract_address; trace.result = self.result; - trace.error = self.error; - trace.reverted = self.reverted; + // rskj sets these two on the program's trace (`extractTrace`: + // `program.getTrace().result(..).error(..).revert(..)`). Where no + // program ran it makes a new trace and sets neither, so a call + // straight to a precompile that fails is traced with no error. + if trace.ran_program { + trace.error = self.error; + trace.reverted = self.reverted; + } } } @@ -14412,6 +14444,88 @@ bf09f6e52420834e8e0e0b1a6df563aba550cf7f99e9724264187c45dcf3d73e8585a0e35196\ ); } + /// rskj traces a transaction sent straight to the Bridge though no + /// program runs for it (`TransactionExecutor.extractTrace`), so a block's + /// traces are one to a transaction, in the transactions' order. Before + /// RSKIP136, and for the federation's free transactions, the Bridge is + /// called outside the EVM, where no tracer hears of it. + #[test] + fn a_bridge_transaction_run_outside_the_evm_has_its_trace() { + let store = Arc::new(MemoryTrieStore::new()); + let sender = Address::repeat_byte(0xAA); + let payee = Address::repeat_byte(0xEE); + let one_rbtc = U256::from(10u64).pow(U256::from(18)); + let root = put_account(&TrieNode::empty(), store.as_ref(), &sender, 0, one_rbtc); + + let block_store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); + // Mainnet's block 3,000 is below bahamas, where RSKIP136 starts. + let txs = vec![ + ( + plain_tx(0, &crate::precompiles::BRIDGE_ADDR, U256::ZERO), + sender, + ), + (plain_tx(1, &payee, U256::from(1_000)), sender), + ]; + let (result, traces) = executor + .execute_block_call_traced(&dummy_header(3_000), &txs, &root, store.clone()) + .expect("block"); + assert!(result.tx_results[1].success); + assert_eq!(traces.len(), 2, "one trace to a transaction"); + // The Bridge's is the one rskj makes where no program ran, and the + // transfer's is its own. + assert_eq!(traces[0].contract_address, ""); + assert_eq!(traces[1].contract_address, hex::encode(payee)); + } + + /// Where no program runs for a transaction, rskj's trace of it is a bare + /// `SummarizedProgramTrace` (`TransactionExecutor.extractTrace`): no gas, + /// no error and no revert, whatever became of the transaction. That is a + /// call straight to a precompile, also one that fails for want of gas + /// (`call()` returns before any program), and a creation with no init + /// code (`create()` builds a `Program` only for data that isn't empty). + #[test] + fn a_transaction_that_runs_no_program_is_traced_without_gas_or_error() { + let store = Arc::new(MemoryTrieStore::new()); + let sender = Address::repeat_byte(0xAA); + let one_rbtc = U256::from(10u64).pow(U256::from(18)); + let root = put_account(&TrieNode::empty(), store.as_ref(), &sender, 0, one_rbtc); + + let block_store = Arc::new(BlockStore::open(tempfile::tempdir().unwrap().path()).unwrap()); + let executor = RskExecutor::new(RskHardforkConfig::mainnet(), block_store); + // ECRECOVER asks 3,000 gas, and this leaves it 100. + let starved = rustock_core::Transaction { + gas_limit: U256::from(21_100), + ..plain_tx(0, &Address::with_last_byte(1), U256::ZERO) + }; + let codeless = rustock_core::Transaction { + to: Bytes::new(), + ..plain_tx(1, &Address::ZERO, U256::ZERO) + }; + let txs = vec![(starved, sender), (codeless, sender)]; + let (result, traces) = executor + .execute_block_call_traced(&dummy_header(3_173_807), &txs, &root, store.clone()) + .expect("block"); + assert!( + !result.tx_results[0].success, + "the precompile had too little gas" + ); + assert!( + result.tx_results[1].success, + "a creation with no code succeeds" + ); + assert_eq!(traces.len(), 2); + for (trace, what) in traces + .iter() + .zip(["the starved call", "the codeless creation"]) + { + assert_eq!(trace.root_gas, 0, "{what}"); + assert_eq!(trace.error, "", "{what}"); + assert!(!trace.reverted, "{what}"); + assert!(trace.subtraces.is_empty(), "{what}"); + } + } + /// RSKIP-131: from RSKIP125 rskj's `Program.createContract` refuses a /// creation at an address an earlier transaction of the block deleted: /// the `deletedAccounts` that `TransactionExecutor` hands every program diff --git a/crates/execution/src/tracer.rs b/crates/execution/src/tracer.rs index 6ccfa86..6f3d160 100644 --- a/crates/execution/src/tracer.rs +++ b/crates/execution/src/tracer.rs @@ -108,6 +108,13 @@ pub struct ProgramTrace { /// `invoke.getGas()`, which is the transaction gas limit less the /// intrinsic cost, and what the top-level trace reports as `action.gas`. pub root_gas: u64, + /// Whether a program ran for the transaction itself -- rskj's + /// `program != null`. Where none did (a plain transfer, a call straight to + /// a precompile, a creation with no init code), rskj's trace is a bare + /// `SummarizedProgramTrace`, which `TransactionExecutor.extractTrace` + /// gives no gas, no error and no revert, whatever became of the + /// transaction. + pub ran_program: bool, /// The transaction's call tree: rskj's `SummarizedProgramTrace.subtraces`. /// /// The transaction's own frame is **not** here. rskj renders the top-level @@ -380,6 +387,7 @@ impl RskTracer { // reporting the real figure here would disagree with every rskj // node. root_gas: if self.root_has_program { self.root_gas } else { 0 }, + ran_program: self.root_has_program, ..Default::default() }; self.pending_storage = None; @@ -490,10 +498,13 @@ where INTR: InterpreterTypes, { fn initialize_interp(&mut self, _interp: &mut Interpreter, _context: &mut CTX) { - // Fires exactly when revm builds an interpreter for a frame, which is - // rskj's `program != null`. See `take_trace` for why that matters. - if self.open_frames.len() == 1 { - self.root_has_program = true; + // Fires when revm builds an interpreter for a frame, which is rskj's + // `program != null` but for one case: revm builds one for a creation + // with no init code as well, where rskj's `create()` builds no + // `Program` (`isEmpty(tx.getData())`). See `take_trace` for why that + // matters. + if let [root] = self.open_frames.as_slice() { + self.root_has_program = root.kind != SubtraceKind::Create || !root.input.is_empty(); } } diff --git a/docs/trace-namespace.md b/docs/trace-namespace.md index 32fcf8a..f7f3ce3 100644 --- a/docs/trace-namespace.md +++ b/docs/trace-namespace.md @@ -207,9 +207,19 @@ else { ``` That hard-coded `0L` is what `action.gas` reports for a plain rBTC transfer -to a codeless account, and for a call straight to a precompile. The tracer -reproduces it by watching `initialize_interp`, which revm fires exactly when -it builds an interpreter for a frame — rskj's `program != null`. +to a codeless account, for a call straight to a precompile, and for a +creation with no init code (`create()` builds a `Program` only for data that +is not empty). The tracer reproduces it by watching `initialize_interp`, +which revm fires when it builds an interpreter for a frame — rskj's +`program != null`, but for that creation, where revm builds one and rskj +does not. + +The second branch sets no error and no revert flag either, so such a trace +never has an `error`, whatever became of the transaction. A call straight to +a precompile with too little gas for it fails, its receipt says so, and its +trace still carries a `result`. A transaction the Bridge runs before RSKIP136, +or free of charge for the federation, never enters the EVM here; its trace is +made the same way, as REMASC's is. **Every RSK block ends with REMASC, and it gets a trace.** It takes the `program == null` branch above, so `trace_block` on any mainnet block returns From 80c4bfc0cbb1de1b9d9430f4b0e4a93e73a14ca0 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Thu, 12 Nov 2026 21:10:13 -0800 Subject: [PATCH 126/264] rpc: a transaction's own trace carries no output rskj builds the first trace of a transaction from its receipt and a new, empty ProgramResult (TraceTransformer.addTrace): the gas the receipt names, the revert flag, and nothing else. So result.output of a call's own trace is 0x whatever the call returned, and only the subtraces show their output. A creation's code is read from the trace itself and is shown. trace_transaction, trace_block, trace_filter and trace_get printed the call's return data there. Test: a_transactions_own_trace_carries_no_output. --- crates/rpc/src/tests.rs | 47 +++++++++++++++++++++++++++++++++++++++++ crates/rpc/src/trace.rs | 8 ++++--- docs/trace-namespace.md | 7 ++++++ 3 files changed, 59 insertions(+), 3 deletions(-) diff --git a/crates/rpc/src/tests.rs b/crates/rpc/src/tests.rs index 26f16b2..3919875 100644 --- a/crates/rpc/src/tests.rs +++ b/crates/rpc/src/tests.rs @@ -4820,6 +4820,10 @@ async fn trace_transaction_renders_the_call_tree() { // The receipt's gas, not the frame's: rskj builds the top-level result // from `txInfo.getReceipt().getGasUsed()`. assert_eq!(top["result"]["gasUsed"], "0x9c40"); + // And nothing else from the call: that result is a fresh, empty + // `ProgramResult` (`TraceTransformer.addTrace`), so a transaction's own + // trace has no output, whatever its call returned. + assert_eq!(top["result"]["output"], "0x"); // A JSON number, because rskj's `blockNumber` is a Java `long`. assert_eq!(top["blockNumber"], json!(42)); assert!(top["blockNumber"].is_number(), "blockNumber must not be a hex string"); @@ -4837,6 +4841,49 @@ async fn trace_transaction_renders_the_call_tree() { ); } +/// rskj builds a transaction's own trace from its receipt and a fresh, empty +/// `ProgramResult` (`TraceTransformer.addTrace`): the gas the receipt names +/// and no output, whatever the call returned. A creation's result is the +/// code it left, which rskj takes from the trace (`trace.getResult()`). +#[test] +fn a_transactions_own_trace_carries_no_output() { + let call = rustock_core::Transaction { + nonce: 0, + gas_price: U256::ZERO, + gas_limit: U256::from(100_000), + to: Bytes::copy_from_slice(&[0xCC; 20]), + value: U256::ZERO, + input: Bytes::new(), + v: 0, + r: U256::ZERO, + s: U256::ZERO, + cached_rlp: None, + }; + let creation = rustock_core::Transaction { + to: Bytes::new(), + ..call.clone() + }; + let block = rustock_core::Block { + header: test_header(42), + transactions: vec![call, creation], + ommers: vec![], + }; + // What the call returned, and what the creation left. + let trace = rustock_execution::tracer::ProgramTrace { + result: "2a".into(), + ..Default::default() + }; + let mut out = Vec::new(); + for index in 0..2 { + crate::trace::render_transaction(&mut out, &block, B256::ZERO, index, 21_000, &trace, 30); + } + assert_eq!( + out[0]["result"], + json!({"gasUsed": "0x5208", "output": "0x"}) + ); + assert_eq!(out[1]["result"]["code"], "0x2a"); +} + /// `trace_block` accepts a hash or a block id, and returns the same traces. #[tokio::test] async fn trace_block_accepts_a_number_or_a_hash() { diff --git a/crates/rpc/src/trace.rs b/crates/rpc/src/trace.rs index 6be6dbf..dc32d47 100644 --- a/crates/rpc/src/trace.rs +++ b/crates/rpc/src/trace.rs @@ -367,7 +367,7 @@ fn replay( /// Flatten one transaction's trace into rskj's depth-first `TransactionTrace` /// list: the transaction's own frame first, then each subtree in order. -fn render_transaction( +pub(crate) fn render_transaction( out: &mut Vec, block: &rustock_core::Block, block_hash: B256, @@ -382,7 +382,8 @@ fn render_transaction( // rskj's top-level trace is built from the receipt, not from a frame: a // synthetic `ProgramResult` carrying only `receipt.getGasUsed()` and the - // revert flag. + // revert flag (`TraceTransformer.addTrace`). So a call's own trace never + // shows what the call returned; only its subtraces show their output. let error = if trace.reverted { Some("Reverted".to_string()) } else if trace.error.is_empty() { @@ -420,9 +421,10 @@ fn render_transaction( &created, ) } else { + // The synthetic result's `getHReturn()` is the empty array. json!({ "gasUsed": quantity(receipt_gas_used), - "output": Value::String(format!("0x{}", trace.result)), + "output": "0x", }) }; diff --git a/docs/trace-namespace.md b/docs/trace-namespace.md index f7f3ce3..e729240 100644 --- a/docs/trace-namespace.md +++ b/docs/trace-namespace.md @@ -195,6 +195,13 @@ the intrinsic 21,000 — while every subtrace reports its own frame's spend. `action.gas` is the frame's gas (limit less intrinsic), which is why the tracer records `root_gas` separately. +Its `result.output` is always `0x`, for the same reason: `addTrace` starts +from `ProgramResult.empty()` and adds only the gas and the revert flag, so +what the transaction's call returned is nowhere in its own trace. A caller +that wants the return data has to replay the call. A creation's `code` is the +exception, because `addTrace` reads it from the trace (`trace.getResult()`) +and not from that result. + **…except when no `Program` was built, where `action.gas` is `0x0`.** `TransactionExecutor.extractTrace` has two branches: From 2a86c589f97d6981d74f71964499c7c134b78082 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Thu, 12 Nov 2026 21:10:13 -0800 Subject: [PATCH 127/264] rpc: a block named by an object is read as rskj reads it Six methods take rskj's BlockRefParam for their block: eth_call, eth_getBalance, eth_getCode, eth_getStorageAt, eth_getTransactionCount and rsk_getStorageBytesAt. It is a string, or an object naming the block by blockHash or by blockNumber (EIP-1898). Each of them read the parameter as a string and took anything else for "latest", so a caller that named a block by an object, or by a JSON number, was answered about the head and told nothing. helpers::block_ref now reads it as BlockRefParam does. An object's values are checked as rskj checks them; one that isn't a string is an exception nobody catches there, -32603, so requireCanonical has to be the string "true" or "false". A hash is looked up, -32600 where the node has no such block or requireCanonical asks for one of the main chain, and the method goes on with that block's number, as Web3Impl.invokeByBlockRef does: a block beside the chain answers as the main chain's block of its height. What is neither a string nor an object is -32602. The lookup follows rskj's too. A state query answers -32600 for a block the node doesn't have and for a state it doesn't hold (Web3InformationRetriever), where it answered -32603; 64 to 66 characters of lower-case hex are a hash, anything else a number of which the low 64 bits count. A call answers -32602 for a number with no block (ExecutionBlockRetriever). eth_estimateGas takes a BlockIdentifierParam, made of the parameter's text whatever JSON it is: a number is a decimal block and an object is refused. eth_getProof and rsk_getStorageNodes, which rskj doesn't have, read their block the same way. rskj's release was run on the parameters the tests use: its BlockRefParam, BlockIdentifierParam and RskErrorResolver answer each as the tests expect. One thing isn't followed: rskj stops at the first entry of an object it refuses, in the order written, and serde_json sorts the keys. Tests: a_block_named_by_an_object_is_read_as_rskj_reads_it and a_block_reference_is_refused_as_rskj_refuses_it. --- crates/rpc/src/call.rs | 18 +-- crates/rpc/src/helpers.rs | 302 +++++++++++++++++++++++++++++++---- crates/rpc/src/proof.rs | 16 +- crates/rpc/src/rsk.rs | 9 +- crates/rpc/src/state.rs | 130 +++++++++++----- crates/rpc/src/tests.rs | 320 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 705 insertions(+), 90 deletions(-) diff --git a/crates/rpc/src/call.rs b/crates/rpc/src/call.rs index ad7f07a..f7e93cb 100644 --- a/crates/rpc/src/call.rs +++ b/crates/rpc/src/call.rs @@ -1,6 +1,6 @@ use crate::dto::CallRequest; use crate::server::RpcState; -use crate::state::resolve_state_root; + use crate::types::*; use alloy_primitives::{Address, Bytes, U256}; use rustock_core::Transaction; @@ -29,8 +29,7 @@ pub fn eth_call(id: Value, params: &Value, state: &RpcState) -> JsonRpcResponse let Some(call_req) = params.get(0).and_then(CallRequest::from_json) else { return JsonRpcResponse::error(id, INVALID_PARAMS, "Missing or invalid call request"); }; - let block_param = params.get(1).and_then(|v| v.as_str()).unwrap_or("latest"); - let block_param = match crate::helpers::execution_block(block_param, false) { + let block_param = match crate::helpers::call_block(params.get(1), &state.store) { Ok(block) => block, Err((code, message)) => return JsonRpcResponse::error(id, code, message), }; @@ -39,8 +38,9 @@ pub fn eth_call(id: Value, params: &Value, state: &RpcState) -> JsonRpcResponse return JsonRpcResponse::error(id, INTERNAL_ERROR, "Execution engine not available"); }; - let Some((root, header)) = resolve_state_root(&block_param, state) else { - return JsonRpcResponse::error(id, INTERNAL_ERROR, "Cannot resolve block"); + let (root, header) = match crate::state::execution_state(&id, &block_param, state) { + Ok(found) => found, + Err(refused) => return refused, }; let sender = call_req.from.unwrap_or(Address::ZERO); @@ -63,8 +63,7 @@ pub fn eth_estimate_gas(id: Value, params: &Value, state: &RpcState) -> JsonRpcR let Some(call_req) = params.get(0).and_then(CallRequest::from_json) else { return JsonRpcResponse::error(id, INVALID_PARAMS, "Missing or invalid call request"); }; - let block_param = params.get(1).and_then(|v| v.as_str()).unwrap_or("latest"); - let block_param = match crate::helpers::execution_block(block_param, true) { + let block_param = match crate::helpers::estimate_block(params.get(1)) { Ok(block) => block, Err((code, message)) => return JsonRpcResponse::error(id, code, message), }; @@ -73,8 +72,9 @@ pub fn eth_estimate_gas(id: Value, params: &Value, state: &RpcState) -> JsonRpcR return JsonRpcResponse::error(id, INTERNAL_ERROR, "Execution engine not available"); }; - let Some((root, header)) = resolve_state_root(&block_param, state) else { - return JsonRpcResponse::error(id, INTERNAL_ERROR, "Cannot resolve block"); + let (root, header) = match crate::state::execution_state(&id, &block_param, state) { + Ok(found) => found, + Err(refused) => return refused, }; let sender = call_req.from.unwrap_or(Address::ZERO); diff --git a/crates/rpc/src/helpers.rs b/crates/rpc/src/helpers.rs index 3e7526f..47bc5dd 100644 --- a/crates/rpc/src/helpers.rs +++ b/crates/rpc/src/helpers.rs @@ -139,28 +139,227 @@ fn is_block_tag(s: &str) -> bool { .any(|tag| tag.eq_ignore_ascii_case(s)) } -/// What rskj answers a block reference it refuses before it looks a block up, -/// for `eth_getBalance`, `eth_getCode`, `eth_getStorageAt`, -/// `eth_getTransactionCount` and `rsk_getStorageBytesAt`. `BlockRefParam` -/// passes `earliest`, `latest` and `pending` as written, a decimal and a `0x` -/// hex number, and refuses the rest -- `LATEST` among them -- with `-32602 -/// Invalid block identifier`. A decimal goes on to -/// `Web3InformationRetriever.getBlock`, which reads only hex: `-32602 invalid -/// blocknumber`. -pub fn refused_block_ref(id: &serde_json::Value, s: &str) -> Option { - let message = - if !(matches!(s, "earliest" | "latest" | "pending") || is_decimal(s) || is_hex_number(s)) { - format!("Invalid block identifier '{s}'") - } else if is_decimal(s) { - format!("invalid blocknumber {s}") - } else { - return None; +/// rskj's lower-case hex (`HexUtils.isHex`): digits and `a` to `f`, and no +/// characters at all. +fn is_lower_hex(s: &str) -> bool { + s.bytes() + .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b)) +} + +/// A value as rskj's messages name it (`co.rsk.util.StringUtils.trim`): its +/// first 66 characters, and three dots for the rest. +pub(crate) fn abbreviated(s: &str) -> String { + match s.char_indices().nth(66) { + Some((end, _)) => format!("{}...", &s[..end]), + None => s.to_string(), + } +} + +/// The bytes rskj's `HexUtils.stringHexToByteArray` reads: a `0x` taken off, +/// and an odd count of digits padded with a zero in front. `None` where its +/// decoder throws. +fn hex_to_bytes(s: &str) -> Option> { + let digits = s.strip_prefix("0x").unwrap_or(s); + if digits.len().is_multiple_of(2) { + hex::decode(digits).ok() + } else { + hex::decode(format!("0{digits}")).ok() + } +} + +/// A block parameter as rskj's `BlockRefParam` reads it, for `eth_call`, +/// `eth_getBalance`, `eth_getCode`, `eth_getStorageAt`, +/// `eth_getTransactionCount` and `rsk_getStorageBytesAt`: the identifier the +/// method goes on with and whether an object named it, or the error code and +/// message rskj answers. +/// +/// A string is `earliest`, `latest` or `pending` as written, a decimal or a +/// `0x` hex number; the rest -- `LATEST` among them -- is `-32602 Invalid +/// block identifier`. +/// +/// An object (EIP-1898) names the block by `blockHash` or by `blockNumber`. +/// Its values are checked one by one (`validateMapItems`), and one that isn't +/// a string is a `ClassCastException` nobody catches, `-32603`: the map is a +/// `Map` in name only, so `requireCanonical` has to be the +/// string `"true"` or `"false"`. Then `Web3Impl.invokeByBlockRef` looks a +/// hash up, `-32600` where the node has no such block or `requireCanonical` +/// asks for one of the main chain, and goes on **with that block's number**: +/// a block beside the chain answers as the main chain's block of its height. +/// A number goes on as the object wrote it. +/// +/// Anything else is `-32602 Invalid input`, but a `null`, which Jackson hands +/// the method as no parameter at all, `-32603`. A parameter left out is the +/// latest block here. +/// +/// rskj's release answers each of these so: its own `BlockRefParam` and +/// `RskErrorResolver` were run on them. One thing can't be followed: rskj +/// checks an object's entries in the order they were written and stops at +/// the first it refuses, and `serde_json` hands them over sorted by key, so +/// of two entries rskj refuses, the one answered for may be the other. +pub fn block_ref( + value: Option<&serde_json::Value>, + store: &rustock_storage::BlockStore, +) -> Result<(String, bool), (i64, String)> { + use crate::types::{INTERNAL_ERROR, INVALID_PARAMS, INVALID_REQUEST}; + use serde_json::Value; + let invalid = |message: &str| (INVALID_PARAMS, message.to_string()); + let uncaught = || (INTERNAL_ERROR, "Internal server error".to_string()); + let inputs = match value { + None => return Ok(("latest".to_string(), false)), + Some(Value::String(s)) => { + let tag = matches!(s.as_str(), "earliest" | "latest" | "pending"); + if !(tag || is_decimal(s) || is_hex_number(s)) { + return Err((INVALID_PARAMS, format!("Invalid block identifier '{s}'"))); + } + return Ok((s.clone(), false)); + } + Some(Value::Object(inputs)) => inputs, + Some(Value::Null) => return Err(uncaught()), + Some(_) => return Err(invalid("Invalid input")), + }; + if !(inputs.contains_key("blockHash") || inputs.contains_key("blockNumber")) { + return Err(invalid("Invalid block input")); + } + let mut hash = None; + for (key, value) in inputs { + let text = match value { + Value::String(text) => Some(text.as_str()), + Value::Null => None, + _ => return Err(uncaught()), }; - Some(crate::types::JsonRpcResponse::error( - id.clone(), - crate::types::INVALID_PARAMS, - message, - )) + match key.as_str() { + "requireCanonical" => { + let text = text.ok_or_else(uncaught)?; + if !(text.eq_ignore_ascii_case("true") || text.eq_ignore_ascii_case("false")) { + return Err(invalid( + "Invalid input: requireCanonical must be a String \"true\" or \"false\"", + )); + } + } + // `BlockHashParam`: 64 to 66 characters that decode to 32 bytes. + "blockHash" => { + let length = || invalid("Invalid block hash: incorrect length."); + let text = text + .filter(|text| (64..=66).contains(&text.len())) + .ok_or_else(length)?; + let bytes = hex_to_bytes(text) + .ok_or_else(|| invalid("Invalid block hash format: invalid hex value"))?; + if bytes.len() != 32 { + return Err(length()); + } + hash = Some((B256::from_slice(&bytes), text)); + } + // `HexNumberParam`: hex in either case, with the prefix or + // without, or what `new BigInteger(String)` reads. + "blockNumber" => { + let Some(text) = text.filter(|text| text.len() <= 66) else { + let named = text.map_or("null".to_string(), abbreviated); + return Err((INVALID_PARAMS, format!("Invalid param: {named}"))); + }; + let digits = text.strip_prefix("0x").unwrap_or(text); + let decimal = text.strip_prefix(['-', '+']).unwrap_or(text); + if !(digits.bytes().all(|b| b.is_ascii_hexdigit()) || is_decimal(decimal)) { + return Err(( + INVALID_PARAMS, + format!( + "Invalid param {text}: value must be a valid hex or string number." + ), + )); + } + } + _ => {} + } + } + if let Some((hash, text)) = hash { + let Some(header) = store.header(hash).ok().flatten() else { + return Err((INVALID_REQUEST, format!("Block with hash {text} not found"))); + }; + let require_canonical = matches!( + inputs.get("requireCanonical"), + Some(Value::String(text)) if text.eq_ignore_ascii_case("true") + ); + if require_canonical && store.canonical_hash(header.number).ok().flatten() != Some(hash) { + return Err(( + INVALID_REQUEST, + format!("Block with hash {text} is not canonical and it is required"), + )); + } + return Ok((format!("{:#x}", header.number), true)); + } + match inputs.get("blockNumber") { + Some(Value::String(text)) => Ok((text.clone(), true)), + _ => Err(invalid("Invalid block input")), + } +} + +/// The block a state query reads, as rskj's `Web3InformationRetriever.getBlock` +/// reads the identifier [`block_ref`] gave: in a form +/// `state::resolve_state_root` reads, or the error code and message rskj +/// answers. +/// +/// What is 64 to 66 characters of lower-case hex is a block's hash, and +/// `-32602 invalid block hash` if it isn't 32 bytes. The rest is a number in +/// hex, which needs its `0x` -- a decimal is `-32602 invalid blocknumber` -- +/// and of which rskj keeps the low 64 bits as a signed number +/// (`BigInteger.longValue`): no block has a number below zero, `-32600`. +pub fn information_block(s: &str) -> Result { + use crate::types::{INVALID_PARAMS, INVALID_REQUEST}; + if matches!(s, "earliest" | "latest" | "pending") { + return Ok(s.to_string()); + } + if (64..=66).contains(&s.len()) && is_lower_hex(s.strip_prefix("0x").unwrap_or(s)) { + return match hex_to_bytes(s) { + Some(bytes) if bytes.len() == 32 => Ok(format!("0x{}", hex::encode(bytes))), + _ => Err((INVALID_PARAMS, format!("invalid block hash {s}"))), + }; + } + if s.len() > 66 { + return Err(( + INVALID_PARAMS, + format!("invalid block identifier {}", abbreviated(s)), + )); + } + let digits = s + .strip_prefix("0x") + .filter(|digits| !digits.is_empty() && digits.bytes().all(|b| b.is_ascii_hexdigit())); + let Some(digits) = digits else { + return Err((INVALID_PARAMS, format!("invalid blocknumber {s}"))); + }; + let low = &digits[digits.len().saturating_sub(16)..]; + match u64::from_str_radix(low, 16).map(|number| number as i64) { + Ok(number) if number >= 0 => Ok(format!("{number:#x}")), + _ => Err((INVALID_REQUEST, format!("Block {s} not found"))), + } +} + +/// The block `eth_call` runs on: its `BlockRefParam` ([`block_ref`]), then +/// [`execution_block`]. What an object names goes through a +/// `BlockIdentifierParam` on its way (`Web3Impl.eth_call`), as an estimate's +/// block does. +pub fn call_block( + value: Option<&serde_json::Value>, + store: &rustock_storage::BlockStore, +) -> Result { + let (identifier, from_object) = block_ref(value, store)?; + execution_block(&identifier, from_object) +} + +/// The block `eth_estimateGas` runs on. Its parameter is a +/// `BlockIdentifierParam`, which rskj makes of the parameter's text whatever +/// JSON it is (`jp.getText()`): a number is a decimal block, and an object is +/// its opening brace, refused. Left out or `null`, it is the latest block +/// (`Web3Impl.eth_estimateGas`). +pub fn estimate_block(value: Option<&serde_json::Value>) -> Result { + use serde_json::Value; + let text = match value { + None | Some(Value::Null) => return Ok("latest".to_string()), + Some(Value::String(s)) => s.clone(), + Some(Value::Number(n)) => n.to_string(), + Some(Value::Bool(b)) => b.to_string(), + Some(Value::Array(_)) => "[".to_string(), + Some(Value::Object(_)) => "{".to_string(), + }; + execution_block(&text, true) } /// The block `eth_call` (`estimate` false) or `eth_estimateGas` (true) runs @@ -670,17 +869,25 @@ mod tests { } /// A block reference rskj refuses before it looks anything up: anything - /// but the three tags as written, a decimal and a hex number, and then a - /// decimal, which `getBlock` can't read. + /// but the three tags as written, a decimal and a hex number + /// (`BlockRefParam`), and then what `getBlock` can't read, a decimal + /// among it. And what `getBlock` takes for a hash. #[test] fn a_block_reference_is_refused_as_rskj_refuses_it() { - let refused = |s: &str| { - refused_block_ref(&serde_json::json!(1), s) - .and_then(|resp| resp.error) - .map(|error| (error.code, error.message)) + let tmp = tempfile::tempdir().unwrap(); + let store = rustock_storage::BlockStore::open(tmp.path()).unwrap(); + let read = |s: &str| { + block_ref(Some(&serde_json::json!(s)), &store) + .and_then(|(identifier, _)| information_block(&identifier)) }; - for fine in ["latest", "pending", "earliest", "0x0", "0x1F"] { - assert_eq!(refused(fine), None, "{fine}"); + for (fine, lookup) in [ + ("latest", "latest"), + ("pending", "pending"), + ("earliest", "earliest"), + ("0x0", "0x0"), + ("0x1F", "0x1f"), + ] { + assert_eq!(read(fine), Ok(lookup.to_string()), "{fine}"); } for (s, message) in [ ("LATEST", "Invalid block identifier 'LATEST'"), @@ -688,8 +895,43 @@ mod tests { ("0x", "Invalid block identifier '0x'"), ("12", "invalid blocknumber 12"), ] { - assert_eq!(refused(s), Some((-32602, message.to_string())), "{s}"); + assert_eq!(read(s), Err((-32602, message.to_string())), "{s}"); } + + // 64 to 66 characters of lower-case hex are a hash, with the prefix + // or without, and an odd count of digits is padded: 63 are a hash + // too, and 62 are a byte short. + let hash = "ab".repeat(32); + assert_eq!(read(&format!("0x{hash}")), Ok(format!("0x{hash}"))); + assert_eq!(information_block(&hash), Ok(format!("0x{hash}"))); + assert_eq!( + read(&format!("0x{}", &hash[1..])), + Ok(format!("0x0{}", &hash[1..])) + ); + let short = format!("0x{}", &hash[2..]); + assert_eq!( + read(&short), + Err((-32602, format!("invalid block hash {short}"))) + ); + // In upper case it isn't one (`HexUtils.isHex`). It is a number, of + // which the low 64 bits count, as a signed long. + let upper = format!("0x{}", "AB".repeat(32)); + assert_eq!( + read(&upper), + Err((-32600, format!("Block {upper} not found"))) + ); + let wrapped = format!("0x{}{:016x}", "F".repeat(48), 5); + assert_eq!(read(&wrapped), Ok("0x5".to_string())); + // Past 66 characters it is neither, and rskj names it by its first + // 66. + let long = format!("0x{}", "1".repeat(65)); + assert_eq!( + read(&long), + Err(( + -32602, + format!("invalid block identifier {}...", &long[..66]) + )) + ); } /// The block a call or an estimate runs on: rskj's tags as written, a hex diff --git a/crates/rpc/src/proof.rs b/crates/rpc/src/proof.rs index c3b80e8..3715078 100644 --- a/crates/rpc/src/proof.rs +++ b/crates/rpc/src/proof.rs @@ -63,7 +63,7 @@ //! truncated inclusion path would look like one without being one. use crate::server::RpcState; -use crate::state::resolve_state_root; + use crate::types::*; use alloy_primitives::{Address, B256, U256}; use rustock_trie::{ @@ -127,9 +127,11 @@ pub fn eth_get_proof(id: Value, params: &Value, state: &RpcState) -> JsonRpcResp slots.push((raw.to_string(), slot)); } - let block_param = params.get(2).and_then(|v| v.as_str()).unwrap_or("latest"); - let Some((root, _header)) = resolve_state_root(block_param, state) else { - return JsonRpcResponse::error(id, INTERNAL_ERROR, "Cannot resolve block"); + // The block as the state queries read it: a tag, a number, or an + // EIP-1898 object. + let (root, _header) = match crate::state::queried_state(&id, params.get(2), state) { + Ok(found) => found, + Err(refused) => return refused, }; let Some(trie_store) = state.trie_store.clone() else { return JsonRpcResponse::error(id, INTERNAL_ERROR, "State not available"); @@ -250,9 +252,9 @@ pub fn rsk_get_storage_nodes(id: Value, params: &Value, state: &RpcState) -> Jso slots.push((raw.to_string(), slot)); } - let block_param = params.get(2).and_then(|v| v.as_str()).unwrap_or("latest"); - let Some((root, header)) = resolve_state_root(block_param, state) else { - return JsonRpcResponse::error(id, INTERNAL_ERROR, "Cannot resolve block"); + let (root, header) = match crate::state::queried_state(&id, params.get(2), state) { + Ok(found) => found, + Err(refused) => return refused, }; let Some(trie_store) = state.trie_store.clone() else { return JsonRpcResponse::error(id, INTERNAL_ERROR, "State not available"); diff --git a/crates/rpc/src/rsk.rs b/crates/rpc/src/rsk.rs index 800fb7e..f6fa2c9 100644 --- a/crates/rpc/src/rsk.rs +++ b/crates/rpc/src/rsk.rs @@ -277,12 +277,9 @@ pub fn rsk_get_storage_bytes_at( return JsonRpcResponse::error(id, INVALID_PARAMS, "Missing or invalid storage key"); }; - let block_param = params.get(2).and_then(|v| v.as_str()).unwrap_or("latest"); - if let Some(refused) = crate::helpers::refused_block_ref(&id, block_param) { - return refused; - } - let Some((root, _header)) = crate::state::resolve_state_root(block_param, state) else { - return JsonRpcResponse::error(id, INTERNAL_ERROR, "Cannot resolve block"); + let (root, _header) = match crate::state::queried_state(&id, params.get(2), state) { + Ok(found) => found, + Err(refused) => return refused, }; let Some(trie_store) = state.trie_store.clone() else { diff --git a/crates/rpc/src/state.rs b/crates/rpc/src/state.rs index ff4141c..6dbd61d 100644 --- a/crates/rpc/src/state.rs +++ b/crates/rpc/src/state.rs @@ -11,13 +11,9 @@ pub fn eth_get_balance(id: Value, params: &Value, state: &RpcState) -> JsonRpcRe let Some(addr) = params.get(0).and_then(|v| v.as_str()).and_then(|s| s.parse::
().ok()) else { return JsonRpcResponse::error(id, INVALID_PARAMS, "Missing or invalid address"); }; - let block_param = params.get(1).and_then(|v| v.as_str()).unwrap_or("latest"); - if let Some(refused) = crate::helpers::refused_block_ref(&id, block_param) { - return refused; - } - - let Some((root, _header)) = resolve_state_root(block_param, state) else { - return JsonRpcResponse::error(id, INTERNAL_ERROR, "Cannot resolve block"); + let (root, _header) = match queried_state(&id, params.get(1), state) { + Ok(found) => found, + Err(refused) => return refused, }; let key = account_key(&addr); @@ -31,13 +27,8 @@ pub fn eth_get_transaction_count(id: Value, params: &Value, state: &RpcState) -> let Some(addr) = params.get(0).and_then(|v| v.as_str()).and_then(|s| s.parse::
().ok()) else { return JsonRpcResponse::error(id, INVALID_PARAMS, "Missing or invalid address"); }; - let block_param = params.get(1).and_then(|v| v.as_str()).unwrap_or("latest"); - if let Some(refused) = crate::helpers::refused_block_ref(&id, block_param) { - return refused; - } - // For "pending", return the pool's pending nonce if available - if block_param == "pending" { + if params.get(1).and_then(|v| v.as_str()) == Some("pending") { if let Some(pool) = &state.tx_pool { if let Some(pending_nonce) = pool.pending_nonce(&addr) { return JsonRpcResponse::success( @@ -48,8 +39,9 @@ pub fn eth_get_transaction_count(id: Value, params: &Value, state: &RpcState) -> } } - let Some((root, _header)) = resolve_state_root(block_param, state) else { - return JsonRpcResponse::error(id, INTERNAL_ERROR, "Cannot resolve block"); + let (root, _header) = match queried_state(&id, params.get(1), state) { + Ok(found) => found, + Err(refused) => return refused, }; let key = account_key(&addr); @@ -63,13 +55,9 @@ pub fn eth_get_code(id: Value, params: &Value, state: &RpcState) -> JsonRpcRespo let Some(addr) = params.get(0).and_then(|v| v.as_str()).and_then(|s| s.parse::
().ok()) else { return JsonRpcResponse::error(id, INVALID_PARAMS, "Missing or invalid address"); }; - let block_param = params.get(1).and_then(|v| v.as_str()).unwrap_or("latest"); - if let Some(refused) = crate::helpers::refused_block_ref(&id, block_param) { - return refused; - } - - let Some((root, _header)) = resolve_state_root(block_param, state) else { - return JsonRpcResponse::error(id, INTERNAL_ERROR, "Cannot resolve block"); + let (root, _header) = match queried_state(&id, params.get(1), state) { + Ok(found) => found, + Err(refused) => return refused, }; let key = code_key(&addr); @@ -96,13 +84,9 @@ pub fn eth_get_storage_at(id: Value, params: &Value, state: &RpcState) -> JsonRp let Some(slot) = params.get(1).and_then(|v| v.as_str()).and_then(parse_storage_slot) else { return JsonRpcResponse::error(id, INVALID_PARAMS, "Missing or invalid storage slot"); }; - let block_param = params.get(2).and_then(|v| v.as_str()).unwrap_or("latest"); - if let Some(refused) = crate::helpers::refused_block_ref(&id, block_param) { - return refused; - } - - let Some((root, _header)) = resolve_state_root(block_param, state) else { - return JsonRpcResponse::error(id, INTERNAL_ERROR, "Cannot resolve block"); + let (root, _header) = match queried_state(&id, params.get(2), state) { + Ok(found) => found, + Err(refused) => return refused, }; let key = storage_key(&addr, &slot); @@ -131,9 +115,73 @@ pub fn eth_get_storage_at(id: Value, params: &Value, state: &RpcState) -> JsonRp } } +/// Why a block parameter gives no state to read. +pub(crate) enum NoState { + /// The node has no such block. + Block, + /// It has the block, and not the state the block left. + State(B256), +} + +/// The state a query reads. Its block parameter is read as rskj's +/// `BlockRefParam` reads it (`helpers::block_ref`) and looked up as +/// `Web3InformationRetriever.getInformationProvider` looks it up, which +/// answers `-32600` for a block the node doesn't have and for a state it +/// doesn't hold. Otherwise the answer rskj gives in its place. +pub(crate) fn queried_state( + id: &Value, + block: Option<&Value>, + state: &RpcState, +) -> Result<(TrieNode, Header), JsonRpcResponse> { + let refuse = |(code, message): (i64, String)| JsonRpcResponse::error(id.clone(), code, message); + let (identifier, _) = crate::helpers::block_ref(block, &state.store).map_err(refuse)?; + let lookup = crate::helpers::information_block(&identifier).map_err(refuse)?; + state_at(&lookup, state).map_err(|none| { + refuse(match none { + NoState::Block => (INVALID_REQUEST, format!("Block {identifier} not found")), + NoState::State(hash) => (INVALID_REQUEST, stateless(hash)), + }) + }) +} + +/// The state a local call runs on, at the block `helpers::call_block` or +/// `estimate_block` gave, or the answer rskj gives where there is none. +/// `ExecutionBlockRetriever` refuses a number that has no block, `-32602`; +/// a state the node doesn't hold is an exception nobody catches +/// (`RepositoryLocator.snapshotAt`), `-32603`. +pub(crate) fn execution_state( + id: &Value, + block: &str, + state: &RpcState, +) -> Result<(TrieNode, Header), JsonRpcResponse> { + state_at(block, state).map_err(|none| { + let number = block + .strip_prefix("0x") + .and_then(|hex| u64::from_str_radix(hex, 16).ok()); + let (code, message) = match (none, number) { + (NoState::Block, Some(number)) => { + (INVALID_PARAMS, format!("Invalid block number {number}")) + } + (NoState::Block, None) => (INTERNAL_ERROR, "Internal server error".to_string()), + (NoState::State(hash), _) => (INTERNAL_ERROR, stateless(hash)), + }; + JsonRpcResponse::error(id.clone(), code, message) + }) +} + +/// rskj's words for a block whose state the node doesn't hold. It prints the +/// hash bare (`Keccak256.toString`). +fn stateless(block: B256) -> String { + format!("State not found for block with hash {}", hex::encode(block)) +} + /// Resolves a block parameter to a state root and header. pub fn resolve_state_root(block_param: &str, state: &RpcState) -> Option<(TrieNode, Header)> { - let trie_store = state.trie_store.as_ref()?; + state_at(block_param, state).ok() +} + +/// [`resolve_state_root`], saying what was missing where it finds no state. +pub(crate) fn state_at(block_param: &str, state: &RpcState) -> Result<(TrieNode, Header), NoState> { let store = &state.store; // State queries against "latest" must use the EXECUTED head — the @@ -145,8 +193,13 @@ pub fn resolve_state_root(block_param: &str, state: &RpcState) -> Option<(TrieNo .ok() .flatten() .and_then(|(exec_hash, _)| store.header(exec_hash).ok().flatten()) - .or_else(|| resolve_header(block_param, store))?, - _ => resolve_header(block_param, store)?, + .or_else(|| resolve_header(block_param, store)), + _ => resolve_header(block_param, store), + } + .ok_or(NoState::Block)?; + let no_state = NoState::State(header.hash()); + let Some(trie_store) = state.trie_store.as_ref() else { + return Err(no_state); }; // Pre-RSKIP126 (wasabi100) headers carry the legacy Ethereum-style state @@ -156,16 +209,17 @@ pub fn resolve_state_root(block_param: &str, state: &RpcState) -> Option<(TrieNo let root_hash = if trie_store.get(header.state_root.as_slice()).is_some() { header.state_root } else { - let (exec_hash, exec_root) = store.exec_head().ok().flatten()?; - if exec_hash != header.hash() { - return None; + match store.exec_head().ok().flatten() { + Some((exec_hash, exec_root)) if exec_hash == header.hash() => exec_root, + _ => return Err(no_state), } - exec_root }; - let root_data = trie_store.get(root_hash.as_slice())?; + let Some(root_data) = trie_store.get(root_hash.as_slice()) else { + return Err(no_state); + }; let root_node = TrieNode::from_message(&root_data, &**trie_store); - Some((root_node, header)) + Ok((root_node, header)) } fn resolve_header(block_param: &str, store: &BlockStore) -> Option
{ diff --git a/crates/rpc/src/tests.rs b/crates/rpc/src/tests.rs index 3919875..dd54e87 100644 --- a/crates/rpc/src/tests.rs +++ b/crates/rpc/src/tests.rs @@ -4583,6 +4583,326 @@ async fn block_tags_are_read_as_rskj_reads_them() { ); } +/// A block named by an object, as rskj's `BlockRefParam` reads it (EIP-1898) +/// for `eth_call`, `eth_getBalance`, `eth_getCode`, `eth_getStorageAt`, +/// `eth_getTransactionCount` and `rsk_getStorageBytesAt`: by `blockNumber`, +/// or by `blockHash`, which rskj turns into that block's number -- so a block +/// beside the chain answers as the main chain's block of its height, unless +/// `requireCanonical` is the string "true". What is neither a string nor an +/// object is refused, where it used to be read as the latest block. A block +/// the node doesn't have is `-32600` to a state query and `-32602` to a call. +#[tokio::test] +async fn a_block_named_by_an_object_is_read_as_rskj_reads_it() { + use rustock_trie::{account_key, TrieKeySlice, TrieNode}; + + let (state, _tmp) = setup_state_with_trie(); + let address = "0xcd2a3d9f938e13cd947ec05abc7fe734df8dd826"; + let trie_store = state.trie_store.clone().unwrap(); + let store = state.store.clone(); + + // #42 holds the account with 10,000. #43, the new head, holds it with 7, + // and so does a second block of height 42, beside the chain. + let at_42 = store.canonical_hash(42).unwrap().unwrap(); + let old_root = store.header(at_42).unwrap().unwrap().state_root; + let root = TrieNode::from_message(&trie_store.get(old_root.as_slice()).unwrap(), &*trie_store); + let poorer = rustock_trie::AccountState::new(U256::from(2), U256::from(7)); + let key = account_key(&address.parse::
().unwrap()); + let mut root = root.put( + &TrieKeySlice::from_key(&key), + &poorer.encode(), + &*trie_store, + ); + root.save(&*trie_store, true); + let state_root = root.compute_hash(&*trie_store); + rustock_trie::TrieStore::put( + &*trie_store, + state_root.as_slice(), + &root.to_message(&*trie_store), + ); + let head = Header { + state_root, + parent_hash: at_42, + ..test_header(43) + }; + let at_43 = head.hash(); + store.put_header(&head).unwrap(); + store.put_canonical_hash(43, at_43).unwrap(); + store.set_head(at_43).unwrap(); + let beside = Header { + state_root, + timestamp: 7, + ..test_header(42) + }; + let beside_42 = beside.hash(); + store.put_header(&beside).unwrap(); + + let ask = |method: &str, params: Value| { + let request = make_request(method, params); + let state = &state; + async move { dispatch_for_test(state, request).await } + }; + let error = |resp: JsonRpcResponse| resp.error.map(|error| (error.code, error.message)); + let balance = |block: Value| ask("eth_getBalance", json!([address, block])); + let hex = |hash: B256| format!("{hash:#x}"); + + assert_eq!(balance(json!("latest")).await.result, Some(json!("0x7"))); + for block in [ + json!({"blockNumber": "0x2a"}), + json!({"blockHash": hex(at_42)}), + json!({"blockHash": hex(at_42), "requireCanonical": "true"}), + // Without its prefix, as `stringHexToByteArray` reads it. + json!({"blockHash": hex(at_42)[2..]}), + // The block beside the chain: its number is all rskj keeps of it. + json!({"blockHash": hex(beside_42)}), + json!({"blockHash": hex(beside_42), "requireCanonical": "FALSE"}), + // The hash is looked at first. + json!({"blockHash": hex(at_42), "blockNumber": "0x2b"}), + ] { + let resp = balance(block.clone()).await; + assert_eq!( + resp.result, + Some(json!("0x2710")), + "{block}: {:?}", + resp.error + ); + } + let resp = balance(json!({"blockNumber": "0x2b"})).await; + assert_eq!(resp.result, Some(json!("0x7")), "{:?}", resp.error); + let resp = ask( + "eth_getTransactionCount", + json!([address, {"blockNumber": "0x2a"}]), + ) + .await; + assert_eq!(resp.result, Some(json!("0x1")), "{:?}", resp.error); + + // Each of the state queries, at a block the node doesn't have. + let nowhere = json!({"blockNumber": "0x63"}); + for (method, params) in [ + ("eth_getBalance", json!([address, nowhere])), + ("eth_getTransactionCount", json!([address, nowhere])), + ("eth_getCode", json!([address, nowhere])), + ("eth_getStorageAt", json!([address, "0x1", nowhere])), + ("rsk_getStorageBytesAt", json!([address, "0x1", nowhere])), + ] { + assert_eq!( + error(ask(method, params).await), + Some((-32600, "Block 0x63 not found".to_string())), + "{method}" + ); + } + + let unknown = hex(B256::repeat_byte(0x77)); + let (known, beside) = (hex(at_42), hex(beside_42)); + let canonical = "Invalid input: requireCanonical must be a String \"true\" or \"false\""; + let number = "value must be a valid hex or string number."; + for (block, code, message) in [ + (json!("0x63"), -32600, "Block 0x63 not found".to_string()), + (json!(unknown), -32600, format!("Block {unknown} not found")), + ( + json!({"blockHash": unknown}), + -32600, + format!("Block with hash {unknown} not found"), + ), + ( + json!({"blockHash": beside, "requireCanonical": "true"}), + -32600, + format!("Block with hash {beside} is not canonical and it is required"), + ), + (json!({}), -32602, "Invalid block input".to_string()), + ( + json!({"requireCanonical": "true"}), + -32602, + "Invalid block input".to_string(), + ), + ( + json!({"blockHash": known, "requireCanonical": "yes"}), + -32602, + canonical.to_string(), + ), + // The map's values are strings in name only: one that isn't can't + // be cast to a string, and nothing catches that. + ( + json!({"blockHash": known, "requireCanonical": true}), + -32603, + "Internal server error".to_string(), + ), + ( + json!({"blockNumber": 42}), + -32603, + "Internal server error".to_string(), + ), + ( + json!({"blockNumber": "0x2a", "other": 7}), + -32603, + "Internal server error".to_string(), + ), + ( + json!({"blockHash": known, "requireCanonical": null}), + -32603, + "Internal server error".to_string(), + ), + ( + json!({"blockNumber": null}), + -32602, + "Invalid param: null".to_string(), + ), + ( + json!({"blockHash": null}), + -32602, + "Invalid block hash: incorrect length.".to_string(), + ), + ( + json!({"blockNumber": "0x-5"}), + -32602, + format!("Invalid param 0x-5: {number}"), + ), + ( + json!({"blockNumber": "0X1"}), + -32602, + format!("Invalid param 0X1: {number}"), + ), + // A bare prefix and no digits at all pass the object's check, and + // `getBlock` then reads no number in them. + ( + json!({"blockNumber": "0x"}), + -32602, + "invalid blocknumber 0x".to_string(), + ), + ( + json!({"blockNumber": ""}), + -32602, + "invalid blocknumber ".to_string(), + ), + ( + json!({"blockNumber": "+5"}), + -32602, + "invalid blocknumber +5".to_string(), + ), + ( + json!({"blockHash": "0x1234"}), + -32602, + "Invalid block hash: incorrect length.".to_string(), + ), + ( + json!({"blockHash": format!("0x{}", "zz".repeat(32))}), + -32602, + "Invalid block hash format: invalid hex value".to_string(), + ), + ( + json!({"blockNumber": "latest"}), + -32602, + format!("Invalid param latest: {number}"), + ), + // Numbers an object may write and `getBlock` then can't read. + ( + json!({"blockNumber": "42"}), + -32602, + "invalid blocknumber 42".to_string(), + ), + ( + json!({"blockNumber": "2a"}), + -32602, + "invalid blocknumber 2a".to_string(), + ), + (json!(42), -32602, "Invalid input".to_string()), + (json!(true), -32602, "Invalid input".to_string()), + (json!(["latest"]), -32602, "Invalid input".to_string()), + (json!(null), -32603, "Internal server error".to_string()), + ] { + assert_eq!( + error(balance(block.clone()).await), + Some((code, message)), + "{block}" + ); + } + + // A call, at the tracing fixture's #41. Its head, #42, has no state. + let (state, _tmp, _, _, _, callee) = setup_state_for_tracing(); + let call = json!({"to": format!("{callee:#x}")}); + let at_41 = hex(state.store.canonical_hash(41).unwrap().unwrap()); + let at_42 = state.store.canonical_hash(42).unwrap().unwrap(); + let ask = |method: &str, block: Value| { + let request = make_request(method, json!([call, block])); + let state = &state; + async move { dispatch_for_test(state, request).await } + }; + for block in [ + json!({"blockNumber": "0x29"}), + json!({"blockNumber": "41"}), + json!({"blockHash": at_41}), + ] { + let resp = ask("eth_call", block.clone()).await; + assert_eq!( + resp.result, + Some(json!(format!("0x{:064x}", 0x2a))), + "{block}: {:?}", + resp.error + ); + } + let stateless = format!("State not found for block with hash {}", &hex(at_42)[2..]); + for (block, code, message) in [ + (json!("0x63"), -32602, "Invalid block number 99".to_string()), + ( + json!({"blockNumber": "0x63"}), + -32602, + "Invalid block number 99".to_string(), + ), + ( + json!({"blockNumber": "2a"}), + -32602, + "Invalid block identifier '2a'".to_string(), + ), + ( + json!({"blockHash": unknown}), + -32600, + format!("Block with hash {unknown} not found"), + ), + (json!(41), -32602, "Invalid input".to_string()), + (json!("latest"), -32603, stateless.clone()), + ] { + assert_eq!( + error(ask("eth_call", block.clone()).await), + Some((code, message)), + "{block}" + ); + } + // An estimate's block is a `BlockIdentifierParam`, made of the + // parameter's text whatever JSON it is: a number is a decimal block, and + // an object is its opening brace. + let resp = ask("eth_estimateGas", json!(41)).await; + assert!( + resp.result.is_some_and(|gas| gas.is_string()), + "{:?}", + resp.error + ); + for (block, code, message) in [ + ( + json!({"blockNumber": "0x29"}), + -32602, + "Invalid block identifier '{'".to_string(), + ), + ( + json!(true), + -32602, + "Invalid block identifier 'true'".to_string(), + ), + (json!("0x63"), -32602, "Invalid block number 99".to_string()), + (json!(99), -32602, "Invalid block number 99".to_string()), + ] { + assert_eq!( + error(ask("eth_estimateGas", block.clone()).await), + Some((code, message)), + "{block}" + ); + } + let resp = dispatch_for_test( + &state, + make_request("eth_getBalance", json!([format!("{callee:#x}"), "latest"])), + ) + .await; + assert_eq!(error(resp), Some((-32600, stateless))); +} + /// A state for a call's defaults: `reader` returns the GASPRICE and the GAS /// it sees, as two words; `burner` loops `count` times at 26 gas a turn, so /// running it costs 21,003 + 26 * `count`. The head holds 6,800,000 gas and From 3b0d096dcec1d1804a08dbbb80837b6d5460ef2b Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Thu, 12 Nov 2026 21:10:14 -0800 Subject: [PATCH 128/264] sync: the pool reckons a transaction's cost in whole numbers rskj's pool works a transaction's cost out with Coin, a BigInteger: the gas price times the gas limit (TxValidatorAccountBalanceValidator), that plus the value, summed over the sender's pending transactions (TransactionPoolImpl.senderCanPayPendingTransactionsAndNewTx), and the old price raised by the bump (isBumpingGasPriceForSameNonceTx). The pool here did each in 256 bits, where a product or a sum that doesn't fit wraps: a transaction asking 2^240 a gas for 65,536 gas cost nothing, and an account holding 1 wei was let in with it. A cost past 256 bits is now more than any balance, as it is to rskj. It showed only where no ceiling on the gas price stops such a transaction first: before RSKIP252, or on a chain whose minimum gas price is zero. The replacement rule is rskj's in full now, too: a transaction replaces a pending one of its nonce only if it pays more, and no less than the old price raised by the bump. Where the bump rounds to nothing, a price of 0, 1 or 2, the pool took the same price again for a replacement, and rskj doesn't. Tests: a_cost_past_256_bits_is_more_than_any_balance and a_replacement_has_to_pay_more. --- crates/sync/src/txpool.rs | 137 +++++++++++++++++++++++++++++++++----- 1 file changed, 122 insertions(+), 15 deletions(-) diff --git a/crates/sync/src/txpool.rs b/crates/sync/src/txpool.rs index 1f58cb8..b952c7d 100644 --- a/crates/sync/src/txpool.rs +++ b/crates/sync/src/txpool.rs @@ -590,10 +590,17 @@ impl TransactionPool { if existing.hash == hash { return Err(PoolError::AlreadyKnown); } - let bump_threshold = existing.tx.gas_price - * U256::from(100 + self.config.gas_price_bump) - / U256::from(100); - if tx.gas_price < bump_threshold { + // rskj `isBumpingGasPriceForSameNonceTx`: a higher price, and + // no less than the old one raised by the bump, which rskj + // reckons in whole numbers (`Coin.multiply`). Where the bump + // rounds to nothing, the same price again doesn't replace. + let old_price = existing.tx.gas_price; + let bumped = alloy_primitives::U512::from(old_price) + * alloy_primitives::U512::from(100 + self.config.gas_price_bump) + / alloy_primitives::U512::from(100); + let pays_more = old_price < tx.gas_price + && bumped <= alloy_primitives::U512::from(tx.gas_price); + if !pays_more { return Err(PoolError::ReplacementGasPriceTooLow); } old_hash_to_remove = Some(existing.hash); @@ -606,22 +613,30 @@ impl TransactionPool { // rskj `getTxBaseCost`: the value, plus the gas only when the // transaction costs anything -- a free Bridge transaction carries // its value and nothing else. - let base_cost = |t: &Transaction| -> U256 { + let base_cost = |t: &Transaction| -> Option { let free = self .chain_rules .as_ref() .is_some_and(|r| r.is_free_tx(t, &sender, current_block)); - if free { t.value } else { tx_cost(t) } + if free { + Some(t.value) + } else { + tx_cost(t) + } }; let mut total_cost = base_cost(&tx); if let Some(pending_map) = inner.pending.get(&sender) { for (nonce, ptx) in pending_map { if *nonce != tx.nonce { - total_cost += base_cost(&ptx.tx); + total_cost = total_cost + .zip(base_cost(&ptx.tx)) + .and_then(|(total, cost)| total.checked_add(cost)); } } } - if total_cost > balance { + // rskj adds these up in whole numbers (`Coin`): a sum past 256 + // bits is more than any balance. + if total_cost.is_none_or(|total| total > balance) { return Err(PoolError::InsufficientFundsForPendingAndNew); } } @@ -743,10 +758,12 @@ impl TransactionPool { } // 5. Balance >= gas cost (`TxValidatorAccountBalanceValidator`), - // which a free transaction skips. + // which a free transaction skips. rskj multiplies in whole + // numbers (`Coin.multiply`), so a cost past 256 bits is more + // than any balance. if !is_free { - let gas_cost = tx.gas_price * tx.gas_limit; - if balance < gas_cost { + let gas_cost = tx.gas_price.checked_mul(tx.gas_limit); + if gas_cost.is_none_or(|gas_cost| balance < gas_cost) { return Err(PoolError::InsufficientFunds); } } @@ -1086,9 +1103,13 @@ fn tx_facts(tx: &Transaction, encoded_size: usize) -> crate::quota::TxFacts { } } -/// Compute the cost of a transaction: value + gas_price * gas_limit. -fn tx_cost(tx: &Transaction) -> U256 { - tx.value + tx.gas_price * tx.gas_limit +/// Compute the cost of a transaction: value + gas_price * gas_limit. `None` +/// where that is past 256 bits, and so more than any balance: rskj reckons it +/// in whole numbers (`TransactionPoolImpl.getTxBaseCost`). +fn tx_cost(tx: &Transaction) -> Option { + tx.gas_price + .checked_mul(tx.gas_limit)? + .checked_add(tx.value) } /// Compute the intrinsic gas cost of a transaction (matching rskj). @@ -1179,6 +1200,17 @@ mod tests { nonce: u64, config: PoolConfig, rules: Option, + ) -> (TransactionPool, SigningKey, Address) { + setup_pool_on(test_header(), balance, nonce, config, rules) + } + + /// [`setup_pool_with_rules`] on a head of the test's own choosing. + fn setup_pool_on( + mut header: Header, + balance: U256, + nonce: u64, + config: PoolConfig, + rules: Option, ) -> (TransactionPool, SigningKey, Address) { let dir = tempdir().unwrap(); let store = Arc::new(BlockStore::open(dir.path()).unwrap()); @@ -1198,7 +1230,6 @@ mod tests { let root_data = root.to_message(&*trie_store); rustock_trie::TrieStore::put(&*trie_store, state_root.as_slice(), &root_data); - let mut header = test_header(); header.state_root = state_root; store.update_head(&header, U256::from(100_000)).unwrap(); @@ -1961,6 +1992,82 @@ mod tests { assert!(matches!(err, PoolError::InsufficientFundsForPendingAndNew)); } + /// rskj reckons what a transaction costs in whole numbers (`Coin`, a + /// `BigInteger`), so a cost past 256 bits is more than any balance. In + /// 256 bits it wraps: 2^240 a gas for 65,536 gas comes to nothing, and so + /// do two transactions of 2^255 each. + #[test] + fn a_cost_past_256_bits_is_more_than_any_balance() { + let (pool, key, _addr) = setup_pool_with_account(U256::from(1), 0); + let mut tx = make_tx(0, 0); + tx.gas_price = U256::from(1) << 240; + tx.gas_limit = U256::from(65_536); + tx.value = U256::ZERO; + sign_tx(&mut tx, &key, 33); + let err = pool.add_transaction(&encode_tx(&tx)).unwrap_err(); + assert!(matches!(err, PoolError::InsufficientFunds), "{err}"); + + let half = U256::from(1) << 255; + let gas = U256::from(59_240_000u64 * 21_000); + let (pool, key, _addr) = setup_pool_with_account(half, 0); + let costing_half = |nonce: u64| { + let mut tx = make_tx(nonce, 59_240_000); + tx.value = half - gas; + sign_tx(&mut tx, &key, 33); + pool.add_transaction(&encode_tx(&tx)) + }; + costing_half(0).expect("the account holds what the first costs"); + let err = costing_half(1).unwrap_err(); + assert!( + matches!(err, PoolError::InsufficientFundsForPendingAndNew), + "{err}" + ); + } + + /// rskj replaces a pending transaction only with one that pays more + /// (`isBumpingGasPriceForSameNonceTx`: `oldGasPrice < newGasPrice`, and + /// no less than the old price raised by the bump). Where the bump rounds + /// to nothing -- a price of 0, 1 or 2 -- the same price again is not + /// enough. + #[test] + fn a_replacement_has_to_pay_more() { + let mut header = test_header(); + header.minimum_gas_price = U256::ZERO; + let (pool, key, _addr) = + setup_pool_on(header, U256::from(10u64.pow(18)), 0, test_config(), None); + let mut value = 1_000; + let mut offer = |gas_price: u64| { + // A new value each time: another transaction, of another hash. + value += 1; + let mut tx = make_tx(0, gas_price); + tx.value = U256::from(value); + sign_tx(&mut tx, &key, 33); + pool.add_transaction(&encode_tx(&tx)) + }; + offer(0).expect("the first"); + for (gas_price, replaces) in [ + (0, false), + (1, true), + (1, false), + (2, true), + (2, false), + (3, true), + // 3 raised by 40% is 4. + (3, false), + (4, true), + ] { + let added = offer(gas_price); + if replaces { + added.unwrap_or_else(|err| panic!("{gas_price} replaces: {err}")); + } else { + assert!( + matches!(added, Err(PoolError::ReplacementGasPriceTooLow)), + "{gas_price} does not replace: {added:?}" + ); + } + } + } + // ========== rskj: removeObsoletePendingTransactionsByBlock ========== #[test] From c2bd763d8db5d5ed46a7fc80592125b65fc3175c Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Thu, 12 Nov 2026 21:10:14 -0800 Subject: [PATCH 129/264] rpc: a call's arguments are read as rskj reads them eth_call and eth_estimateGas read their first parameter with a reader of their own, which left a field out when it could not read it. A mistyped `to` made the call a creation, a number without its 0x was read as hex, and a JSON number was dropped. rskj reads the arguments in two steps. CallArgumentsParam checks each field it knows before the method runs, in its own order, and refuses an address, a number or data it cannot take with -32602. Then, with the block in hand, CallArgumentsToByteArray reads the gas price, the gas and the value: hex behind a 0x, and a decimal without one, a negative one as the bytes BigInteger.toByteArray gives it. Both steps are followed now, in rskj's order: the fields, the block, then the numbers, with eth_call looking for the block's state last and eth_estimateGas before the numbers. No arguments at all (null) and a gas written in more than eight bytes are errors nothing catches in rskj, -32603. Three things under it are followed too. Hex is decoded as bouncycastle decodes it, passing over a space, a tab and the line ends. A JSON number with a fraction or an exponent is the double it reads as, printed by Double.toString, so 1e5 is "100000.0" and no parameter takes it for a number; java.rs prints one for every double now, where its digits stopped at 512 bits. And `input` is read over `data`. Two things are not followed, and the code says so where it reads them. A gas of 2^63 or more gets past rskj's own cap; here it is held to the cap. BigInteger reads the digits of every script; here a digit is 0 to 9. The answers in the tests are the release's own: its parameter classes, its converter and its error resolver were run on the tests' values, and the printing of doubles was compared with Java 11's, which prints as 17 does, on 1.5 million of them. --- crates/rpc/src/call.rs | 60 ++++++- crates/rpc/src/dto.rs | 148 +++++++++++----- crates/rpc/src/helpers.rs | 45 +++-- crates/rpc/src/java.rs | 103 ++++++++++- crates/rpc/src/lib.rs | 1 + crates/rpc/src/params.rs | 351 ++++++++++++++++++++++++++++++++++++++ crates/rpc/src/state.rs | 38 +++-- crates/rpc/src/tests.rs | 242 ++++++++++++++++++++++++++ 8 files changed, 889 insertions(+), 99 deletions(-) create mode 100644 crates/rpc/src/params.rs diff --git a/crates/rpc/src/call.rs b/crates/rpc/src/call.rs index f7e93cb..1b58dd0 100644 --- a/crates/rpc/src/call.rs +++ b/crates/rpc/src/call.rs @@ -1,4 +1,5 @@ -use crate::dto::CallRequest; +use crate::dto::{CallArguments, CallRequest}; +use crate::params::{uncaught, Refusal}; use crate::server::RpcState; use crate::types::*; @@ -25,22 +26,52 @@ pub const CALL_GAS_CAP: u64 = 10_000_000; /// `internalEstimateGas` caps the answer at it). pub const GAS_ESTIMATION_CAP: u64 = 6_800_000; +/// A call's arguments, checked as rskj's `CallArgumentsParam` checks them +/// before the method runs. `None` for `null`, which reaches the method as no +/// arguments at all: it throws when it comes to read them, and nothing +/// catches that. +fn call_arguments(params: &Value) -> Result, Refusal> { + match params.get(0) { + None => Err(( + INVALID_PARAMS, + "Missing or invalid call request".to_string(), + )), + Some(Value::Null) => Ok(None), + Some(arguments) => CallArguments::read(arguments).map(Some), + } +} + pub fn eth_call(id: Value, params: &Value, state: &RpcState) -> JsonRpcResponse { - let Some(call_req) = params.get(0).and_then(CallRequest::from_json) else { - return JsonRpcResponse::error(id, INVALID_PARAMS, "Missing or invalid call request"); + let refuse = |id: Value, (code, message): Refusal| JsonRpcResponse::error(id, code, message); + let arguments = match call_arguments(params) { + Ok(arguments) => arguments, + Err(refusal) => return refuse(id, refusal), }; let block_param = match crate::helpers::call_block(params.get(1), &state.store) { Ok(block) => block, - Err((code, message)) => return JsonRpcResponse::error(id, code, message), + Err(refusal) => return refuse(id, refusal), }; let Some((trie_store, hardfork_cfg)) = state.trie_store.clone().zip(state.hardfork_cfg.clone()) else { return JsonRpcResponse::error(id, INTERNAL_ERROR, "Execution engine not available"); }; - let (root, header) = match crate::state::execution_state(&id, &block_param, state) { + // rskj has the block in hand before it reads the call's numbers, and + // looks for the block's state only as it runs the call (`EthModule.call`). + let found = crate::state::state_at(&block_param, state); + if let Err(none @ crate::state::NoState::Block) = found { + return crate::state::no_execution_state(&id, &block_param, none); + } + let call_req = match arguments + .ok_or_else(uncaught) + .and_then(CallArguments::request) + { + Ok(request) => request, + Err(refusal) => return refuse(id, refusal), + }; + let (root, header) = match found { Ok(found) => found, - Err(refused) => return refused, + Err(none) => return crate::state::no_execution_state(&id, &block_param, none), }; let sender = call_req.from.unwrap_or(Address::ZERO); @@ -60,12 +91,14 @@ pub fn eth_call(id: Value, params: &Value, state: &RpcState) -> JsonRpcResponse } pub fn eth_estimate_gas(id: Value, params: &Value, state: &RpcState) -> JsonRpcResponse { - let Some(call_req) = params.get(0).and_then(CallRequest::from_json) else { - return JsonRpcResponse::error(id, INVALID_PARAMS, "Missing or invalid call request"); + let refuse = |id: Value, (code, message): Refusal| JsonRpcResponse::error(id, code, message); + let arguments = match call_arguments(params) { + Ok(arguments) => arguments, + Err(refusal) => return refuse(id, refusal), }; let block_param = match crate::helpers::estimate_block(params.get(1)) { Ok(block) => block, - Err((code, message)) => return JsonRpcResponse::error(id, code, message), + Err(refusal) => return refuse(id, refusal), }; let Some((trie_store, hardfork_cfg)) = state.trie_store.clone().zip(state.hardfork_cfg.clone()) else { @@ -76,6 +109,15 @@ pub fn eth_estimate_gas(id: Value, params: &Value, state: &RpcState) -> JsonRpcR Ok(found) => found, Err(refused) => return refused, }; + // An estimate has its state in hand before it reads the call's numbers + // (`EthModule.estimateGas`). + let call_req = match arguments + .ok_or_else(uncaught) + .and_then(CallArguments::estimate_request) + { + Ok(request) => request, + Err(refusal) => return refuse(id, refusal), + }; let sender = call_req.from.unwrap_or(Address::ZERO); let executor = RskExecutor::new(hardfork_cfg, state.store.clone()); diff --git a/crates/rpc/src/dto.rs b/crates/rpc/src/dto.rs index 8e8e592..3170667 100644 --- a/crates/rpc/src/dto.rs +++ b/crates/rpc/src/dto.rs @@ -180,6 +180,108 @@ impl LogDto { } } +/// A call's arguments as rskj's `CallArgumentsParam` holds them: each field +/// checked, and the numbers still as they were written. +/// +/// rskj reads a call's arguments in two steps. Its deserializer checks every +/// field it knows, in its own order whatever the order written, and what a +/// field's type refuses is `-32602`: an address that isn't one, a number that +/// isn't hex or a decimal, data that isn't hex. A field that is absent or +/// `null` is not there, and what isn't an object has no fields. Then, with the +/// block in hand, `CallArgumentsToByteArray` reads the numbers +/// ([`CallArguments::request`]). +#[derive(Debug)] +pub struct CallArguments { + from: Option
, + to: Option
, + gas: Option, + gas_price: Option, + value: Option, + data: Option>, +} + +impl CallArguments { + /// `CallArgumentsParam.Deserializer`. `arguments` is not `null`: that + /// reaches the method as no arguments at all, and what the method makes of + /// that is its own. + pub fn read(arguments: &serde_json::Value) -> Result { + use crate::params::{field_text, hex_address, hex_data, hex_number}; + let field = |name: &str| arguments.get(name).and_then(field_text); + let number = |name: &str| match field(name) { + Some(text) => hex_number(&text).map(|()| Some(text)), + None => Ok(None), + }; + let from = field("from").map(|text| hex_address(&text)).transpose()?; + let to = field("to").map(|text| hex_address(&text)).transpose()?; + let gas = number("gas")?; + let gas_price = number("gasPrice")?; + // Checked, and read by no call. + number("gasLimit")?; + number("nonce")?; + number("chainId")?; + let value = number("value")?; + let data = field("data").map(|text| hex_data(&text)).transpose()?; + let input = field("input").map(|text| hex_data(&text)).transpose()?; + Ok(Self { + from, + to, + gas, + gas_price, + value, + // `CallArguments.setInput` writes over what `setData` wrote. + data: input.or(data), + }) + } + + /// `CallArgumentsToByteArray`: the gas price, the gas and the value read + /// as numbers, in that order ([`crate::params::number_bytes`]). One + /// written as nothing is not there. + /// + /// A call's gas is held to `rpc.callGasCap` whatever it names, and one + /// written in more than eight bytes is an error nobody catches + /// (`ByteUtil.byteArrayToLong`). A gas of 2^63 or more in eight bytes + /// gets past rskj's own cap, which compares it as a `long` + /// (`gasLimitForCall`): a contract then runs on 2^63 - 1 gas, and a call + /// to anything else is an error. That isn't followed. It is held to the + /// cap like any other. + pub fn request(self) -> Result { + use crate::params::{number, number_bytes, uncaught}; + let written = |text: Option| text.filter(|text| !text.is_empty()); + let gas_price = written(self.gas_price) + .map(|text| number(&text)) + .transpose()?; + let gas = match written(self.gas) + .map(|text| number_bytes(&text)) + .transpose()? + { + Some(bytes) if bytes.len() > 8 => return Err(uncaught()), + Some(bytes) => Some( + bytes + .iter() + .fold(0u64, |gas, byte| gas << 8 | u64::from(*byte)), + ), + None => None, + }; + let value = written(self.value).map(|text| number(&text)).transpose()?; + Ok(CallRequest { + from: self.from, + to: self.to, + gas, + gas_price, + value, + data: self.data, + }) + } + + /// [`CallArguments::request`] for an estimate, which reads the gas price + /// and the value and not the gas (`EthModule.estimateGas` runs every + /// estimate at its cap). + pub fn estimate_request(mut self) -> Result { + self.gas = None; + self.request() + } +} + /// Call request object for eth_call / eth_estimateGas. #[derive(Debug)] pub struct CallRequest { @@ -191,52 +293,6 @@ pub struct CallRequest { pub data: Option>, } -impl CallRequest { - pub fn from_json(v: &serde_json::Value) -> Option { - let obj = v.as_object()?; - - let from = obj.get("from") - .and_then(|v| v.as_str()) - .and_then(|s| s.parse::
().ok()); - - let to = obj.get("to") - .and_then(|v| v.as_str()) - .and_then(|s| s.parse::
().ok()); - - let gas = obj.get("gas") - .and_then(|v| v.as_str()) - .and_then(parse_hex_u64); - - let gas_price = obj.get("gasPrice") - .and_then(|v| v.as_str()) - .and_then(parse_hex_u256); - - let value = obj.get("value") - .and_then(|v| v.as_str()) - .and_then(parse_hex_u256); - - let data = obj.get("data") - .or_else(|| obj.get("input")) - .and_then(|v| v.as_str()) - .and_then(|s| { - let s = s.strip_prefix("0x").unwrap_or(s); - hex::decode(s).ok() - }); - - Some(Self { from, to, gas, gas_price, value, data }) - } -} - -fn parse_hex_u64(s: &str) -> Option { - let s = s.strip_prefix("0x").unwrap_or(s); - u64::from_str_radix(s, 16).ok() -} - -fn parse_hex_u256(s: &str) -> Option { - let s = s.strip_prefix("0x").unwrap_or(s); - U256::from_str_radix(s, 16).ok() -} - fn compute_contract_address(sender: &[u8], nonce: u64) -> Address { use alloy_rlp::Encodable; use sha3::{Digest, Keccak256}; diff --git a/crates/rpc/src/helpers.rs b/crates/rpc/src/helpers.rs index 47bc5dd..fa8255e 100644 --- a/crates/rpc/src/helpers.rs +++ b/crates/rpc/src/helpers.rs @@ -156,15 +156,25 @@ pub(crate) fn abbreviated(s: &str) -> String { } /// The bytes rskj's `HexUtils.stringHexToByteArray` reads: a `0x` taken off, -/// and an odd count of digits padded with a zero in front. `None` where its -/// decoder throws. -fn hex_to_bytes(s: &str) -> Option> { - let digits = s.strip_prefix("0x").unwrap_or(s); - if digits.len().is_multiple_of(2) { - hex::decode(digits).ok() - } else { - hex::decode(format!("0{digits}")).ok() +/// and an odd count of characters padded with a zero in front. `None` where +/// its decoder throws. +/// +/// The decoder is bouncycastle's `Hex.decode`, which passes over a space, a +/// tab, a line feed and a carriage return wherever they stand. The zero goes +/// in front by the count of characters, those four counted: `12 34 ` is two +/// bytes, and `12 34` is padded to five digits and refused. +pub(crate) fn hex_to_bytes(s: &str) -> Option> { + let written = s.strip_prefix("0x").unwrap_or(s); + let mut digits = Vec::with_capacity(written.len() + 1); + if !written.len().is_multiple_of(2) { + digits.push(b'0'); } + digits.extend( + written + .bytes() + .filter(|byte| !matches!(byte, b' ' | b'\t' | b'\n' | b'\r')), + ); + hex::decode(digits).ok() } /// A block parameter as rskj's `BlockRefParam` reads it, for `eth_call`, @@ -350,14 +360,8 @@ pub fn call_block( /// its opening brace, refused. Left out or `null`, it is the latest block /// (`Web3Impl.eth_estimateGas`). pub fn estimate_block(value: Option<&serde_json::Value>) -> Result { - use serde_json::Value; - let text = match value { - None | Some(Value::Null) => return Ok("latest".to_string()), - Some(Value::String(s)) => s.clone(), - Some(Value::Number(n)) => n.to_string(), - Some(Value::Bool(b)) => b.to_string(), - Some(Value::Array(_)) => "[".to_string(), - Some(Value::Object(_)) => "{".to_string(), + let Some(text) = value.and_then(crate::params::text) else { + return Ok("latest".to_string()); }; execution_block(&text, true) } @@ -427,14 +431,7 @@ pub fn parse_filter_block( head_number: u64, ) -> Result { use crate::types::{INTERNAL_ERROR, INVALID_PARAMS}; - use serde_json::Value; - let text = match value { - Value::String(s) => s.clone(), - Value::Number(n) => n.to_string(), - Value::Bool(b) => b.to_string(), - Value::Null => "null".to_string(), - Value::Array(_) | Value::Object(_) => String::new(), - }; + let text = crate::params::field_text(value).unwrap_or_else(|| "null".to_string()); if text.is_empty() { return Err(( INVALID_PARAMS, diff --git a/crates/rpc/src/java.rs b/crates/rpc/src/java.rs index a232785..4419e29 100644 --- a/crates/rpc/src/java.rs +++ b/crates/rpc/src/java.rs @@ -16,6 +16,10 @@ //! JDK 17's: a port of `jdk.internal.math.FloatingDecimal` as OpenJDK 17 has //! it (`getBinaryToASCIIConverter(double, true)`, `BinaryToASCIIBuffer.dtoa`, //! `developLongDigits`, `roundup`, `estimateDecExp`). +//! +//! A parameter written as a JSON number with a fraction or an exponent goes +//! through `Double.toString` as well: Jackson holds it as a double and gives +//! the parameter's reader that double's text (`params::text`). use alloy_primitives::{U256, U512}; @@ -86,6 +90,47 @@ pub fn big_decimal_value_of_to_big_integer(d: f64) -> Option { Some(value) } +/// Java 17's `Double.toString(d)` (`BinaryToASCIIBuffer.getChars`): from +/// 10^-3 to below 10^7 the digits around a point, and outside that one digit, +/// a point, the rest and an `E` with the exponent. A digit always follows the +/// point. +pub fn double_to_string(d: f64) -> String { + if d.is_nan() { + return "NaN".to_string(); + } + let sign = if d.is_sign_negative() { "-" } else { "" }; + if d.is_infinite() { + return format!("{sign}Infinity"); + } + if d == 0.0 { + return format!("{sign}0.0"); + } + let (digits, dec_exponent) = double_to_string_digits(d.abs()); + let digits: String = digits + .iter() + .map(|digit| char::from(b'0' + digit)) + .collect(); + let count = digits.len() as i32; + if (1..8).contains(&dec_exponent) { + let whole = count.min(dec_exponent) as usize; + let zeros = "0".repeat((dec_exponent - count).max(0) as usize); + let fraction = match &digits[whole..] { + "" => "0", + fraction => fraction, + }; + format!("{sign}{}{zeros}.{fraction}", &digits[..whole]) + } else if (-2..=0).contains(&dec_exponent) { + let zeros = "0".repeat(-dec_exponent as usize); + format!("{sign}0.{zeros}{digits}") + } else { + let fraction = match &digits[1..] { + "" => "0", + fraction => fraction, + }; + format!("{sign}{}.{fraction}E{}", &digits[..1], dec_exponent - 1) + } +} + /// The digits of Java 17's `Double.toString(d)` for a positive finite `d`, /// and its decimal exponent: `d` prints as 0.d1 d2 ... dn x 10^exponent. /// `FloatingDecimal.getBinaryToASCIIConverter(d, true)`. @@ -247,12 +292,16 @@ fn dtoa(bin_exp: i32, mut fract_bits: i64, n_significant_bits: i32) -> (Vec, // FDBigInteger, exactly. Its normalizing shift scales B, S, M and 10S // alike, which no quotient or comparison below can see; and here the // test for reaching 10S is `>=`, where the int and long paths use `>`. - let pow52 = |p5: i32, p2: i32| U512::from(5u8).pow(U512::from(p5)) << (p2 as usize); + // + // 1024 bits hold it for every double: the least and the greatest keep + // B, S, M and 10S below 2^830. + type Wide = alloy_primitives::Uint<1024, 16>; + let pow52 = |p5: i32, p2: i32| Wide::from(5u8).pow(Wide::from(p5)) << (p2 as usize); let s = pow52(s5, s2); - let mut b = U512::from(fract_bits as u64) * pow52(b5, b2); + let mut b = Wide::from(fract_bits as u64) * pow52(b5, b2); let mut m = pow52(m5 + 1, m2 + 1); let ten_s = pow52(s5 + 1, s2 + 1); - let ten = U512::from(10u8); + let ten = Wide::from(10u8); // `quoRemIteration`: the quotient, and B becomes 10 * (B mod S). let mut q = (b / s).to::(); b = (b % s) * ten; @@ -489,6 +538,54 @@ mod tests { assert_eq!(to_big_integer(8.41e21), 8_409_999_999_999_999_000_000); } + /// `Double.toString` as Java 17 lays a double out, whatever its size. + /// Each line is Java's own output: Java 11, which prints a double as 17 + /// does, printed these. The three past 10^21 are ones JDK 19 prints + /// shorter. + #[test] + fn a_double_prints_as_java_17_prints_it() { + for (d, printed) in [ + (0.0, "0.0"), + (0.1, "0.1"), + (5.5, "5.5"), + (5.0, "5.0"), + (123.456, "123.456"), + (100000.0, "100000.0"), + (9999999.999, "9999999.999"), + (1.0e7, "1.0E7"), + (12345678.9, "1.23456789E7"), + (0.001, "0.001"), + (0.00123, "0.00123"), + (0.0001, "1.0E-4"), + (1.5e-7, "1.5E-7"), + (1.0e30, "1.0E30"), + (8.41e21, "8.409999999999999E21"), + (1.0e23, "9.999999999999999E22"), + (2.0e23, "1.9999999999999998E23"), + (f64::MAX, "1.7976931348623157E308"), + (f64::MIN_POSITIVE, "2.2250738585072014E-308"), + (5e-324, "4.9E-324"), + (f64::INFINITY, "Infinity"), + ] { + assert_eq!(double_to_string(d), printed); + assert_eq!(double_to_string(-d), format!("-{printed}")); + } + assert_eq!(double_to_string(f64::NAN), "NaN"); + // And whatever the double, the least of them included, what is + // printed reads back as it. + let mut x: u64 = 0x9e37_79b9_7f4a_7c15; + for _ in 0..20_000 { + x ^= x << 13; + x ^= x >> 7; + x ^= x << 17; + for d in [f64::from_bits(x), f64::from_bits(x >> 12)] { + if d.is_finite() { + assert_eq!(double_to_string(d).parse(), Ok(d), "{d:e}"); + } + } + } + } + /// A fraction is dropped, and the digits round-trip: what Java 17 prints /// for a double reads back as that double. #[test] diff --git a/crates/rpc/src/lib.rs b/crates/rpc/src/lib.rs index 47098ae..1c5b77e 100644 --- a/crates/rpc/src/lib.rs +++ b/crates/rpc/src/lib.rs @@ -10,6 +10,7 @@ pub mod mnr; pub mod metrics_server; pub mod admin; pub mod net; +pub mod params; pub mod proof; pub mod rsk; pub mod sco; diff --git a/crates/rpc/src/params.rs b/crates/rpc/src/params.rs new file mode 100644 index 0000000..ffdbcdf --- /dev/null +++ b/crates/rpc/src/params.rs @@ -0,0 +1,351 @@ +//! rskj's typed parameters (`org.ethereum.rpc.parameters`): what each takes, +//! and the error rskj answers for what it doesn't. +//! +//! jsonrpc4j converts a call's parameters to the method's parameter types +//! before the method runs (`JsonRpcBasicServer.convertJsonToParameters`), so +//! these refusals come before anything the method itself says. What a +//! parameter's constructor throws is an `RskJsonRpcRequestException`, which +//! `RskErrorResolver` answers with its own code and message; anything else +//! thrown on the way is `-32603 Internal server error`. +//! +//! Each answer here is the release's own: its parameter classes and its +//! error resolver were run on the values the tests use. + +use alloy_primitives::{Address, U256}; +use serde_json::Value; + +use crate::helpers::{abbreviated, hex_to_bytes}; +use crate::types::{INTERNAL_ERROR, INVALID_PARAMS}; + +/// The code and message rskj answers in place of a result. +pub type Refusal = (i64, String); + +fn invalid(message: impl Into) -> Refusal { + (INVALID_PARAMS, message.into()) +} + +/// What rskj answers where something is thrown that nothing catches. +pub fn uncaught() -> Refusal { + (INTERNAL_ERROR, "Internal server error".to_string()) +} + +/// The text a parameter's deserializer reads (`jp.getText()`): a string as it +/// is, a number as Jackson prints it back ([`number_text`]), a boolean as +/// written, and of an object or an array its opening bracket. `None` for +/// `null`, which reaches the method as no parameter at all, unchecked. +pub fn text(value: &Value) -> Option { + Some(match value { + Value::Null => return None, + Value::String(s) => s.clone(), + Value::Number(n) => number_text(&n.to_string()), + Value::Bool(b) => b.to_string(), + Value::Array(_) => "[".to_string(), + Value::Object(_) => "{".to_string(), + }) +} + +/// A JSON number as Jackson's tree holds it and prints it back: a whole +/// number as its digits, and one written with a fraction or an exponent as +/// the double it reads as, printed by `Double.toString`. So `1e5` is +/// `100000.0`, which no parameter takes for a number. +/// +/// `written` is the number as the request wrote it, which is how this +/// crate's `serde_json` keeps one (`arbitrary_precision`). +fn number_text(written: &str) -> String { + if written.contains(['.', 'e', 'E']) { + let double = written.parse().unwrap_or(f64::NAN); + return crate::java::double_to_string(double); + } + match written { + "-0" => "0".to_string(), + whole => whole.to_string(), + } +} + +/// The text of a field of an object (`JsonNode.asText()`): as [`text`], but +/// an object or an array is the empty string. +pub fn field_text(value: &Value) -> Option { + match value { + Value::Array(_) | Value::Object(_) => Some(String::new()), + other => text(other), + } +} + +/// `HexAddressParam`: 40 to 42 characters that `RskAddress` reads as 20 +/// bytes -- with the prefix or without, in either case, and 39 digits behind +/// a prefix, which are padded. +pub fn hex_address(text: &str) -> Result { + if !(40..=42).contains(&text.len()) { + return Err(invalid( + "Invalid address: null, empty or invalid hex value.", + )); + } + hex_to_bytes(text) + .filter(|bytes| bytes.len() == 20) + .map(|bytes| Address::from_slice(&bytes)) + .ok_or_else(|| invalid("Invalid address format: invalid hex value.")) +} + +/// `HexNumberParam`, which checks a number and doesn't read it: at most 66 +/// characters, of hex in either case, with the prefix or without, or what +/// `new BigInteger(String)` reads, a decimal with or without a sign. +pub fn hex_number(text: &str) -> Result<(), Refusal> { + if text.len() > 66 { + return Err(invalid(format!("Invalid param: {}", abbreviated(text)))); + } + let digits = text.strip_prefix("0x").unwrap_or(text); + let hex = digits.bytes().all(|b| b.is_ascii_hexdigit()); + if hex || is_decimal(text) { + return Ok(()); + } + Err(invalid(format!( + "Invalid param {text}: value must be a valid hex or string number." + ))) +} + +/// What `new BigInteger(String)` reads: digits, behind a sign or not. +/// +/// Java reads the digits of every script there (`Character.digit`), so that +/// rskj takes `"\u{665}"`, an Arabic-Indic five, for 5. That isn't followed: +/// a digit here is one of `0` to `9`. +fn is_decimal(text: &str) -> bool { + let digits = text.strip_prefix(['-', '+']).unwrap_or(text); + !digits.is_empty() && digits.bytes().all(|b| b.is_ascii_digit()) +} + +/// `HexDataParam`: what `HexUtils.stringHexToByteArray` decodes. +pub fn hex_data(text: &str) -> Result, Refusal> { + hex_to_bytes(text).ok_or_else(|| invalid("Invalid data format: invalid hex value.")) +} + +/// The bytes of a number as rskj reads one a [`hex_number`] let through +/// (`HexUtils.strHexOrStrNumberToByteArray`): behind a `0x` the bytes its +/// digits write, leading zeros and all, and without one a decimal in the +/// bytes `ByteUtil.bigIntegerToBytes` gives it, so `"10"` is ten. Hex without +/// its prefix passed the check and is no decimal: `-32602`. +/// +/// A decimal below zero comes through `BigInteger.toByteArray`, its two's +/// complement in as few bytes as hold it, so -1 is the byte `0xff`: 255. +/// One of more digits than [`hex_number`] lets through isn't read. +pub fn number_bytes(text: &str) -> Result, Refusal> { + let unreadable = + || invalid("Number values should not contain letters or hex values should start with 0x."); + if text.starts_with("0x") { + // Nothing catches what the decoder throws here. + return hex_to_bytes(text).ok_or_else(uncaught); + } + if !is_decimal(text) { + return Err(unreadable()); + } + let digits = text.strip_prefix(['-', '+']).unwrap_or(text); + let size = U256::from_str_radix(digits, 10).map_err(|_| unreadable())?; + if !text.starts_with('-') || size.is_zero() { + let bytes = size.to_be_bytes_trimmed_vec(); + return Ok(if bytes.is_empty() { vec![0] } else { bytes }); + } + // -n is the low k bytes of its two's complement, where k bytes are the + // fewest whose sign bit is set for it: n - 1 has to fit below that bit. + let count = (size - U256::from(1)).bit_len() / 8 + 1; + if count > 32 { + return Err(unreadable()); + } + Ok(size.wrapping_neg().to_be_bytes::<32>()[32 - count..].to_vec()) +} + +/// [`number_bytes`] as the number they write. +pub fn number(text: &str) -> Result { + U256::try_from_be_slice(&number_bytes(text)?).ok_or_else(uncaught) +} + +#[cfg(test)] +mod tests { + use super::*; + + /// `strHexOrStrNumberToByteArray`, number by number, as rskj's release + /// answers each. + #[test] + fn a_number_is_hex_behind_its_prefix_and_a_decimal_without() { + for (text, bytes) in [ + ("0x10", "10"), + ("0x000a", "000a"), + ("0xfff", "0fff"), + ("0xFf", "ff"), + ("0x", ""), + ("10", "0a"), + ("+7", "07"), + ("007", "07"), + ("0", "00"), + ("-0", "00"), + ("20000000000000000000", "01158e460913d00000"), + ("9223372036854775808", "8000000000000000"), + // Two's complement, in the fewest bytes. + ("-1", "ff"), + ("-5", "fb"), + ("-128", "80"), + ("-129", "ff7f"), + ("-256", "ff00"), + ("-32768", "8000"), + ("-32769", "ff7fff"), + ] { + assert_eq!( + number_bytes(text).map(hex::encode), + Ok(bytes.to_string()), + "{text}" + ); + } + assert_eq!(number("-1"), Ok(U256::from(255))); + assert_eq!(number("0x"), Ok(U256::ZERO)); + // An upper-case prefix is no prefix, an underscore no digit, and a + // digit of another script isn't read. + for text in ["ff", "1f", "", "-", "5.5", "0X10", "1_0", " 5", "\u{665}"] { + assert_eq!( + number(text).map_err(|(code, _)| code), + Err(-32602), + "{text}" + ); + } + // Behind a prefix, what the decoder throws nothing catches. + for text in ["0x-5", "0x1_0", "0xzz"] { + assert_eq!(number(text), Err(uncaught()), "{text}"); + } + } + + /// A parameter's text as Jackson gives it: a number with a fraction or an + /// exponent is the double it reads as, printed as Java prints one. + #[test] + fn a_json_number_is_read_back_as_jackson_prints_it() { + for (written, printed) in [ + ("5", "5"), + ("-0", "0"), + ("-5", "-5"), + ( + "123456789012345678901234567890", + "123456789012345678901234567890", + ), + ("5.5", "5.5"), + ("5.0", "5.0"), + ("1e5", "100000.0"), + ("1E5", "100000.0"), + ("1e30", "1.0E30"), + ("0.001", "0.001"), + ("0.0001", "1.0E-4"), + ("12345678.9", "1.23456789E7"), + ("-0.0", "-0.0"), + ("1e999", "Infinity"), + ("-1e999", "-Infinity"), + ("1e-999", "0.0"), + ] { + let value: Value = serde_json::from_str(written).unwrap(); + assert_eq!(text(&value).as_deref(), Some(printed), "{written}"); + } + assert_eq!(text(&Value::Null), None); + assert_eq!(text(&serde_json::json!({"a": 1})).as_deref(), Some("{")); + assert_eq!(field_text(&serde_json::json!([1])).as_deref(), Some("")); + } + + /// `HexDataParam`: bouncycastle's decoder passes over a space, a tab and + /// the two line ends, and the zero in front goes by the count of + /// characters. + #[test] + fn data_is_hex_with_its_blanks_passed_over() { + for (text, bytes) in [ + ("0x1234", "1234"), + ("abc", "0abc"), + ("5", "05"), + ("", ""), + ("0x", ""), + ("0x12 34 ", "1234"), + ("0x1234 \n", "1234"), + ("0x12\n34\n", "1234"), + ("0x12\t34\r", "1234"), + ("12 34 ", "1234"), + (" ", ""), + ] { + assert_eq!( + hex_data(text).map(hex::encode), + Ok(bytes.to_string()), + "{text:?}" + ); + } + for text in [ + "0x12 34", + "0x1234\n", + " 0x1234", + " ", + "0xzz", + "zz", + "0x1234\u{b}\u{b}", + "0x1234\u{a0}\u{a0}", + ] { + assert_eq!( + hex_data(text).map_err(|(code, _)| code), + Err(-32602), + "{text:?}" + ); + } + } + + /// An address in the forms `RskAddress` reads. + #[test] + fn an_address_is_twenty_bytes_however_it_is_written() { + let one = Address::with_last_byte(1); + let digits = format!("{}1", "0".repeat(39)); + for text in [ + format!("0x{digits}"), + digits.clone(), + format!("0x{}", &digits[1..]), + // 40 digits and two blanks, which the decoder passes over. + format!("{digits} "), + format!(" {digits}\n"), + ] { + assert_eq!(hex_address(&text), Ok(one), "{text}"); + } + assert_eq!( + hex_address(&"AB".repeat(20)), + Ok(Address::repeat_byte(0xab)) + ); + for (text, message) in [ + ( + "0x1".to_string(), + "Invalid address: null, empty or invalid hex value.", + ), + ( + String::new(), + "Invalid address: null, empty or invalid hex value.", + ), + // 38 digits behind a prefix are 19 bytes, and 41 without one are + // padded to 21. + ( + format!("0x{}", &digits[2..]), + "Invalid address format: invalid hex value.", + ), + ( + format!("{digits}0"), + "Invalid address format: invalid hex value.", + ), + ( + format!("0x{}", "zz".repeat(20)), + "Invalid address format: invalid hex value.", + ), + ( + format!("0X{digits}"), + "Invalid address format: invalid hex value.", + ), + // 40 digits and one blank are padded to 41 digits. + ( + format!("{digits} "), + "Invalid address format: invalid hex value.", + ), + ( + format!("0x{digits}\n"), + "Invalid address: null, empty or invalid hex value.", + ), + ] { + assert_eq!( + hex_address(&text), + Err((-32602, message.to_string())), + "{text}" + ); + } + } +} diff --git a/crates/rpc/src/state.rs b/crates/rpc/src/state.rs index 6dbd61d..1f5a88f 100644 --- a/crates/rpc/src/state.rs +++ b/crates/rpc/src/state.rs @@ -145,28 +145,32 @@ pub(crate) fn queried_state( } /// The state a local call runs on, at the block `helpers::call_block` or -/// `estimate_block` gave, or the answer rskj gives where there is none. -/// `ExecutionBlockRetriever` refuses a number that has no block, `-32602`; -/// a state the node doesn't hold is an exception nobody catches -/// (`RepositoryLocator.snapshotAt`), `-32603`. +/// `estimate_block` gave, or the answer rskj gives where there is none +/// ([`no_execution_state`]). pub(crate) fn execution_state( id: &Value, block: &str, state: &RpcState, ) -> Result<(TrieNode, Header), JsonRpcResponse> { - state_at(block, state).map_err(|none| { - let number = block - .strip_prefix("0x") - .and_then(|hex| u64::from_str_radix(hex, 16).ok()); - let (code, message) = match (none, number) { - (NoState::Block, Some(number)) => { - (INVALID_PARAMS, format!("Invalid block number {number}")) - } - (NoState::Block, None) => (INTERNAL_ERROR, "Internal server error".to_string()), - (NoState::State(hash), _) => (INTERNAL_ERROR, stateless(hash)), - }; - JsonRpcResponse::error(id.clone(), code, message) - }) + state_at(block, state).map_err(|none| no_execution_state(id, block, none)) +} + +/// What rskj answers a local call that has no state to run on. +/// `ExecutionBlockRetriever` refuses a number that has no block, `-32602`; +/// a state the node doesn't hold is an exception nobody catches +/// (`RepositoryLocator.snapshotAt`), `-32603`. +pub(crate) fn no_execution_state(id: &Value, block: &str, none: NoState) -> JsonRpcResponse { + let number = block + .strip_prefix("0x") + .and_then(|hex| u64::from_str_radix(hex, 16).ok()); + let (code, message) = match (none, number) { + (NoState::Block, Some(number)) => { + (INVALID_PARAMS, format!("Invalid block number {number}")) + } + (NoState::Block, None) => (INTERNAL_ERROR, "Internal server error".to_string()), + (NoState::State(hash), _) => (INTERNAL_ERROR, stateless(hash)), + }; + JsonRpcResponse::error(id.clone(), code, message) } /// rskj's words for a block whose state the node doesn't hold. It prints the diff --git a/crates/rpc/src/tests.rs b/crates/rpc/src/tests.rs index dd54e87..1150c12 100644 --- a/crates/rpc/src/tests.rs +++ b/crates/rpc/src/tests.rs @@ -5013,6 +5013,248 @@ async fn a_calls_defaults_are_rskjs() { assert_eq!(resp.result, Some(json!("0x")), "{:?}", resp.error); } +/// A call's arguments as rskj reads them: `CallArgumentsParam` checks each +/// field, and `CallArgumentsToByteArray` then reads the numbers. An address +/// that isn't one, a number or data that isn't hex is refused, `-32602`, +/// where each used to be left out without a word -- a mistyped `to` made the +/// call a creation. A number without its `0x` is a decimal, where it used to +/// be read as hex, and so is a JSON number, which used to be left out. +/// rskj's release answers each so: its `CallArgumentsParam` and its +/// `CallArgumentsToByteArray` were run on these. +#[tokio::test] +async fn a_calls_arguments_are_read_as_rskj_reads_them() { + let (state, _tmp, reader, _burner) = setup_defaults_state(1); + let ask = |method: &str, params: Value| { + let request = make_request(method, params); + let state = &state; + async move { dispatch_for_test(state, request).await } + }; + let call = |call: Value| ask("eth_call", json!([call, "latest"])); + let words = |resp: JsonRpcResponse| { + let out = resp.result.unwrap_or_else(|| panic!("{:?}", resp.error)); + let out = hex::decode(out.as_str().unwrap().trim_start_matches("0x")).unwrap(); + ( + U256::from_be_slice(&out[..32]), + U256::from_be_slice(&out[32..]), + ) + }; + let error = |resp: JsonRpcResponse| resp.error.map(|error| (error.code, error.message)); + let to = format!("{reader:#x}"); + // A number as the request writes it, which `json!` would write its own way. + let written = |field: &str, number: &str| -> Value { + serde_json::from_str(&format!(r#"{{"to":"{to}","{field}":{number}}}"#)).unwrap() + }; + + // The reader answers the gas price and the gas it sees: the call's gas + // less the 21,000, what its data costs -- 68 a byte, and 4 a byte of + // zero -- and the 13 spent before it. + let all = 10_000_000 - 21_013u64; + let some = 200_000 - 21_013u64; + for (arguments, gas_price, gas) in [ + ( + json!({"to": to, "gasPrice": "0x10", "gas": "0x30d40"}), + 16u64, + some, + ), + ( + json!({"to": to, "gasPrice": "0x000a", "gas": "0x00030d40"}), + 10, + some, + ), + // Without the prefix a number is a decimal, and so is a JSON number. + ( + json!({"to": to, "gasPrice": "10", "gas": "200000"}), + 10, + some, + ), + (json!({"to": to, "gasPrice": 7, "gas": 200000}), 7, some), + (json!({"to": to, "gasPrice": "+7"}), 7, all), + // `BigInteger.toByteArray` writes -1 as the one byte 0xff. + (json!({"to": to, "gasPrice": "-1"}), 255, all), + (json!({"to": to, "gasPrice": -1}), 255, all), + // Nothing written, an object or an array is no number, and a bare + // prefix is zero. + (json!({"to": to, "gasPrice": "", "gas": {}}), 0, all), + (json!({"to": to, "gasPrice": "0x", "gas": null}), 0, all), + // A JSON number of any length is its digits. + (written("value", "20000000000000000000"), 0, all), + // More gas than the cap is the cap, and so is 2^63, which gets past + // rskj's own cap: that isn't followed. + (json!({"to": to, "gas": "0x989681"}), 0, all), + (json!({"to": to, "gas": "0x8000000000000000"}), 0, all), + // An address with no prefix, one in upper case, and one of 40 + // digits and two spaces, which the decoder passes over. + ( + json!({"to": to[2..], "from": to.to_uppercase().replace('X', "x")}), + 0, + all, + ), + (json!({"to": format!("{} ", &to[2..])}), 0, all), + // Data of an odd count of digits, with no prefix: 0x0abc. + (json!({"to": to, "data": "abc"}), 0, all - 2 * 68), + // `input` is read over `data`, written as nothing too. + (json!({"to": to, "data": "abc", "input": 5}), 0, all - 68), + (json!({"to": to, "data": "0x1234", "input": ""}), 0, all), + ( + json!({"to": to, "input": "0x1234", "data": {}}), + 0, + all - 2 * 68, + ), + // A space and a line feed in data are passed over: 0x1200. + (json!({"to": to, "data": "0x12 00\n"}), 0, all - 68 - 4), + ] { + let resp = call(arguments.clone()).await; + assert_eq!( + words(resp), + (U256::from(gas_price), U256::from(gas)), + "{arguments}" + ); + } + + let address = "Invalid address: null, empty or invalid hex value."; + let malformed = "Invalid address format: invalid hex value."; + let number = "value must be a valid hex or string number."; + let data = "Invalid data format: invalid hex value."; + let letters = "Number values should not contain letters or hex values should start with 0x."; + let long = "1".repeat(68); + for (arguments, message) in [ + (json!({"to": "0x123"}), address.to_string()), + (json!({"to": 5}), address.to_string()), + (json!({"to": to, "from": {}}), address.to_string()), + ( + json!({"to": format!("0x{}", "zz".repeat(20))}), + malformed.to_string(), + ), + ( + json!({"to": format!("0x{}", "11".repeat(19))}), + malformed.to_string(), + ), + (json!({"to": to.replace("0x", "0X")}), malformed.to_string()), + // 40 digits and one space are padded to 41 digits. + ( + json!({"to": format!("{} ", &to[2..])}), + malformed.to_string(), + ), + ( + json!({"to": to, "gas": "zz"}), + format!("Invalid param zz: {number}"), + ), + ( + json!({"to": to, "gas": "0X10"}), + format!("Invalid param 0X10: {number}"), + ), + ( + json!({"to": to, "gas": " 5"}), + format!("Invalid param 5: {number}"), + ), + ( + json!({"to": to, "gas": true}), + format!("Invalid param true: {number}"), + ), + // A JSON number with a fraction or an exponent is the double it + // reads as, printed as Java prints one. + ( + written("gas", "5.5"), + format!("Invalid param 5.5: {number}"), + ), + ( + written("gas", "1e5"), + format!("Invalid param 100000.0: {number}"), + ), + ( + written("gas", "0.0001"), + format!("Invalid param 1.0E-4: {number}"), + ), + // Checked, though a call reads none of the three. + ( + json!({"to": to, "nonce": "zz"}), + format!("Invalid param zz: {number}"), + ), + ( + json!({"to": to, "chainId": "zz"}), + format!("Invalid param zz: {number}"), + ), + ( + json!({"to": to, "gasLimit": "zz"}), + format!("Invalid param zz: {number}"), + ), + ( + written("nonce", "1e5"), + format!("Invalid param 100000.0: {number}"), + ), + ( + json!({"to": to, "value": long}), + format!("Invalid param: {}...", &long[..66]), + ), + (json!({"to": to, "data": "0xzz"}), data.to_string()), + (json!({"to": to, "input": "zz"}), data.to_string()), + // Five characters are padded to six, and five digits are left. + (json!({"to": to, "data": "0x12 34"}), data.to_string()), + // The sender is checked first, wherever it is written. + (json!({"gas": "zz", "from": "0x1"}), address.to_string()), + // Hex without its prefix passes the check and is then no decimal. + (json!({"to": to, "gas": "ff"}), letters.to_string()), + (json!({"to": to, "gasPrice": "ff"}), letters.to_string()), + (json!({"to": to, "value": "ff"}), letters.to_string()), + ] { + assert_eq!( + error(call(arguments.clone()).await), + Some((-32602, message)), + "{arguments}" + ); + } + // A gas written in more than eight bytes, and no arguments at all, which + // the method reads and finds nothing to read: what rskj throws there + // nothing catches. + let uncaught = Some((-32603, "Internal server error".to_string())); + for arguments in [ + json!({"to": to, "gas": "0x000000000000989680"}), + json!({"to": to, "gas": "18446744073709551616"}), + written("gas", "18446744073709551616"), + json!(null), + ] { + assert_eq!( + error(call(arguments.clone()).await), + uncaught, + "{arguments}" + ); + } + // What isn't an object has no fields: a creation with no code. + let resp = call(json!("x")).await; + assert_eq!(resp.result, Some(json!("0x")), "{:?}", resp.error); + + // The fields are checked before the block is looked for, and the + // arguments read after it. + let nowhere = Some((-32602, "Invalid block number 99".to_string())); + let resp = ask("eth_call", json!([{"to": "0x123"}, "0x63"])).await; + assert_eq!(error(resp), Some((-32602, address.to_string()))); + let resp = ask("eth_call", json!([{"to": to, "gas": "ff"}, "0x63"])).await; + assert_eq!(error(resp), nowhere); + let resp = ask("eth_call", json!([null, "0x63"])).await; + assert_eq!(error(resp), nowhere); + + // An estimate reads the same arguments, but for the gas, which it + // checks and then doesn't read. + let estimate = |call: Value| ask("eth_estimateGas", json!([call, "latest"])); + assert_eq!( + error(estimate(json!({"to": "0x123"})).await), + Some((-32602, address.to_string())) + ); + assert_eq!( + error(estimate(json!({"to": to, "value": "ff"})).await), + Some((-32602, letters.to_string())) + ); + assert_eq!( + error(estimate(json!({"to": to, "gas": "zz"})).await), + Some((-32602, format!("Invalid param zz: {number}"))) + ); + assert_eq!(error(estimate(json!(null)).await), uncaught); + for gas in ["ff", "18446744073709551616"] { + let resp = estimate(json!({"to": to, "gas": gas})).await; + assert!(resp.result.is_some(), "{gas}: {:?}", resp.error); + } +} + /// A sent transaction's defaults are rskj's /// (`TransactionArgumentsUtil.processArguments`): a gas price of 0, not the /// node's price, and the gas named as `gas`, else as `gasLimit`, else 90,000, From e45e9316b68c1ae16a6b481f66c86562971e7b44 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Thu, 12 Nov 2026 21:10:14 -0800 Subject: [PATCH 130/264] rpc: the methods that sign read a request as rskj reads it eth_sign, eth_signTransaction, eth_sendTransaction and the personal_ methods read their parameters with readers of their own, and what they made of a request was not what rskj makes of it. A number written without its 0x was read as hex, where rskj reads a decimal: a value of "1000000000000000000" was signed as 2^72, about 4,722 times what was asked for. `data` was read over `input`, where rskj reads `input` over `data`. A chain named in the arguments was not read, where rskj signs for no chain but its own and refuses another. A zero gas price was written as the empty item, as canonical RLP writes it. rskj writes the gas price of a transaction it builds as 0x00 when it is zero (RLP.encodeCoinNonNullZero), which is another transaction with another hash, and zero is what a sent transaction's gas price is unless its arguments name one. And a parameter rskj refuses before the method runs was answered by the method, or, with the wallet off, as the wallet being off. jsonrpc4j makes each parameter of its type first, in the method's order: an address, data, a key, a duration and a transaction's arguments refuse with -32602, and a seed or a passphrase given as an object or an array is -32700 "JSON parse error", answered with the id "null", a string. The arguments are now read by the reader eth_call uses, and then as TransactionArgumentsUtil.processArguments reads them: the nonce, the value, the gas price, the gas or else the gasLimit or else 90,000, the data and the chain, a bare "0x" for a number being an error nothing catches, -32603. The transaction is written field by field as rskj writes one, and signed over the digest rskj signs. The errors are rskj's: "from is null", "Could not find account for address", "Account not found", "Invalid chainId". eth_signTransaction is not rskj's. It reads its transaction as eth_sendTransaction does and returns the bytes that would be sent. The answers in the test are the release's own, 197 of them: its JSON-RPC server was run over its web3 implementation with the wallet off and on, and the steps of its sendTransaction on 45 sets of arguments, whose signed bytes are compared whole. --- crates/rpc/src/dto.rs | 91 +++++- crates/rpc/src/helpers.rs | 2 +- crates/rpc/src/params.rs | 90 +++++- crates/rpc/src/sign.rs | 296 +++++++++--------- crates/rpc/src/stubs.rs | 62 +++- .../rpc/src/testdata/rskj_wallet_methods.txt | 210 +++++++++++++ crates/rpc/src/tests.rs | 140 ++++++++- crates/rpc/src/wallet.rs | 14 + 8 files changed, 726 insertions(+), 179 deletions(-) create mode 100644 crates/rpc/src/testdata/rskj_wallet_methods.txt diff --git a/crates/rpc/src/dto.rs b/crates/rpc/src/dto.rs index 3170667..a3356e6 100644 --- a/crates/rpc/src/dto.rs +++ b/crates/rpc/src/dto.rs @@ -189,17 +189,57 @@ impl LogDto { /// isn't hex or a decimal, data that isn't hex. A field that is absent or /// `null` is not there, and what isn't an object has no fields. Then, with the /// block in hand, `CallArgumentsToByteArray` reads the numbers -/// ([`CallArguments::request`]). +/// ([`CallArguments::request`]). A transaction the node is to sign has them +/// read another way ([`CallArguments::transaction`]). #[derive(Debug)] pub struct CallArguments { from: Option
, to: Option
, gas: Option, gas_price: Option, + gas_limit: Option, + nonce: Option, + chain_id: Option, value: Option, data: Option>, } +/// What rskj makes of a call's arguments to build a transaction it signs +/// (`TransactionArgumentsUtil.processArguments`). +#[derive(Debug)] +pub struct TransactionArguments { + pub to: Option
, + /// `None` where the arguments name none: the sender's next. + pub nonce: Option, + pub value: U256, + pub gas_price: U256, + pub gas_limit: U256, + pub data: Vec, + /// The chain the arguments name, and how they wrote it. `None` where they + /// name none, or name 0: the node's own. + pub chain_id: Option<(u8, String)>, +} + +/// The gas a sent transaction names when the caller names none: rskj's +/// `GasCost.TRANSACTION_DEFAULT`. +const TRANSACTION_DEFAULT_GAS: u64 = 90_000; + +/// The chain a transaction's arguments name (`TransactionArgumentsUtil +/// .hexToChainId`): the one byte their `chainId` writes, read as a call's +/// numbers are ([`crate::params::number_bytes`]). `None` for 0, which is the +/// node's own chain. Anything that isn't one byte, and anything thrown on the +/// way, is `-32602 Invalid chainId`. +fn named_chain(written: String) -> Result, crate::params::Refusal> { + match crate::params::number_bytes(&written).as_deref() { + Ok([0]) => Ok(None), + Ok(&[chain]) => Ok(Some((chain, written))), + _ => Err(( + crate::types::INVALID_PARAMS, + format!("Invalid chainId: {written}"), + )), + } +} + impl CallArguments { /// `CallArgumentsParam.Deserializer`. `arguments` is not `null`: that /// reaches the method as no arguments at all, and what the method makes of @@ -215,10 +255,10 @@ impl CallArguments { let to = field("to").map(|text| hex_address(&text)).transpose()?; let gas = number("gas")?; let gas_price = number("gasPrice")?; - // Checked, and read by no call. - number("gasLimit")?; - number("nonce")?; - number("chainId")?; + // Checked, and read only for a transaction the node signs. + let gas_limit = number("gasLimit")?; + let nonce = number("nonce")?; + let chain_id = number("chainId")?; let value = number("value")?; let data = field("data").map(|text| hex_data(&text)).transpose()?; let input = field("input").map(|text| hex_data(&text)).transpose()?; @@ -227,12 +267,53 @@ impl CallArguments { to, gas, gas_price, + gas_limit, + nonce, + chain_id, value, // `CallArguments.setInput` writes over what `setData` wrote. data: input.or(data), }) } + /// The sender the arguments name. + pub fn from(&self) -> Option
{ + self.from + } + + /// `TransactionArgumentsUtil.processArguments`: the arguments of a + /// transaction the node is to sign, read in rskj's order -- the nonce, + /// the value, the gas price, the gas, the chain -- each number as + /// [`crate::params::transaction_number`] reads it. + /// + /// The defaults are rskj's: no value, a gas price of 0, and the gas + /// named as `gas`, or else as `gasLimit`, which is read only then, or + /// else 90,000. + pub fn transaction(self) -> Result { + use crate::params::transaction_number; + let read = |text: Option| text.map(|text| transaction_number(&text)).transpose(); + let nonce = read(self.nonce)?; + let value = read(self.value)?.unwrap_or_default(); + let gas_price = read(self.gas_price)?.unwrap_or_default(); + let gas_limit = match read(self.gas)? { + Some(gas) => gas, + None => read(self.gas_limit)?.unwrap_or(U256::from(TRANSACTION_DEFAULT_GAS)), + }; + let chain_id = match self.chain_id { + Some(written) => named_chain(written)?, + None => None, + }; + Ok(TransactionArguments { + to: self.to, + nonce, + value, + gas_price, + gas_limit, + data: self.data.unwrap_or_default(), + chain_id, + }) + } + /// `CallArgumentsToByteArray`: the gas price, the gas and the value read /// as numbers, in that order ([`crate::params::number_bytes`]). One /// written as nothing is not there. diff --git a/crates/rpc/src/helpers.rs b/crates/rpc/src/helpers.rs index fa8255e..cec14ae 100644 --- a/crates/rpc/src/helpers.rs +++ b/crates/rpc/src/helpers.rs @@ -141,7 +141,7 @@ fn is_block_tag(s: &str) -> bool { /// rskj's lower-case hex (`HexUtils.isHex`): digits and `a` to `f`, and no /// characters at all. -fn is_lower_hex(s: &str) -> bool { +pub(crate) fn is_lower_hex(s: &str) -> bool { s.bytes() .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b)) } diff --git a/crates/rpc/src/params.rs b/crates/rpc/src/params.rs index ffdbcdf..7c24eb0 100644 --- a/crates/rpc/src/params.rs +++ b/crates/rpc/src/params.rs @@ -6,16 +6,19 @@ //! these refusals come before anything the method itself says. What a //! parameter's constructor throws is an `RskJsonRpcRequestException`, which //! `RskErrorResolver` answers with its own code and message; anything else -//! thrown on the way is `-32603 Internal server error`. +//! thrown on the way is `-32603 Internal server error`, but for what Jackson +//! itself throws where it can't make a parameter of its type, which +//! jsonrpc4j takes for a request it couldn't parse ([`unparsed`]). //! //! Each answer here is the release's own: its parameter classes and its -//! error resolver were run on the values the tests use. +//! error resolver were run on the values the tests use, and so was its +//! server, over its `Web3RskImpl`, on whole requests. use alloy_primitives::{Address, U256}; use serde_json::Value; -use crate::helpers::{abbreviated, hex_to_bytes}; -use crate::types::{INTERNAL_ERROR, INVALID_PARAMS}; +use crate::helpers::{abbreviated, hex_to_bytes, is_lower_hex}; +use crate::types::{INTERNAL_ERROR, INVALID_PARAMS, PARSE_ERROR}; /// The code and message rskj answers in place of a result. pub type Refusal = (i64, String); @@ -29,6 +32,18 @@ pub fn uncaught() -> Refusal { (INTERNAL_ERROR, "Internal server error".to_string()) } +/// What rskj answers where Jackson can't make a parameter of its type: a +/// `JsonMappingException`, which jsonrpc4j takes for a request it couldn't +/// parse (`JsonRpcBasicServer.handleRequest`). The answer names no request: +/// its id is the string [`UNPARSED_ID`], whatever the request's was. +pub fn unparsed() -> Refusal { + (PARSE_ERROR, "JSON parse error".to_string()) +} + +/// The id of jsonrpc4j's answer to a request it couldn't parse: the word +/// `null` as a string, not a JSON `null`. +pub const UNPARSED_ID: &str = "null"; + /// The text a parameter's deserializer reads (`jp.getText()`): a string as it /// is, a number as Jackson prints it back ([`number_text`]), a boolean as /// written, and of an object or an array its opening bracket. `None` for @@ -71,11 +86,25 @@ pub fn field_text(value: &Value) -> Option { } } +/// A string's length as Java counts it: its UTF-16 units. +fn java_length(text: &str) -> usize { + text.encode_utf16().count() +} + +/// A `String` parameter as Jackson reads one: a scalar is its text, and an +/// object or an array is none ([`unparsed`]). `None` for `null`. +pub fn string(value: &Value) -> Result, Refusal> { + match value { + Value::Array(_) | Value::Object(_) => Err(unparsed()), + scalar => Ok(text(scalar)), + } +} + /// `HexAddressParam`: 40 to 42 characters that `RskAddress` reads as 20 /// bytes -- with the prefix or without, in either case, and 39 digits behind /// a prefix, which are padded. pub fn hex_address(text: &str) -> Result { - if !(40..=42).contains(&text.len()) { + if !(40..=42).contains(&java_length(text)) { return Err(invalid( "Invalid address: null, empty or invalid hex value.", )); @@ -90,7 +119,7 @@ pub fn hex_address(text: &str) -> Result { /// characters, of hex in either case, with the prefix or without, or what /// `new BigInteger(String)` reads, a decimal with or without a sign. pub fn hex_number(text: &str) -> Result<(), Refusal> { - if text.len() > 66 { + if java_length(text) > 66 { return Err(invalid(format!("Invalid param: {}", abbreviated(text)))); } let digits = text.strip_prefix("0x").unwrap_or(text); @@ -118,6 +147,43 @@ pub fn hex_data(text: &str) -> Result, Refusal> { hex_to_bytes(text).ok_or_else(|| invalid("Invalid data format: invalid hex value.")) } +/// `HexKeyParam`: hex in either case, with the prefix or without, and +/// nothing at all. +pub fn hex_key(text: &str) -> Result<(), Refusal> { + let digits = text.strip_prefix("0x").unwrap_or(text); + if digits.bytes().all(|b| b.is_ascii_hexdigit()) { + return Ok(()); + } + Err(invalid(format!( + "Invalid param {text}: value must be a valid hex." + ))) +} + +/// `HexDurationParam`: nothing at all, or a `0x` and lower-case hex digits +/// (`HexStringParam`) that `Long.parseLong` reads. +pub fn hex_duration(text: &str) -> Result<(), Refusal> { + if text.is_empty() { + return Ok(()); + } + if java_length(text) > 66 { + return Err(invalid(format!("Invalid argument: {}", abbreviated(text)))); + } + let Some(digits) = text + .strip_prefix("0x") + .filter(|digits| is_lower_hex(digits)) + else { + return Err(invalid(format!( + "Invalid argument \"{text}\": param should be a hex value string." + ))); + }; + match i64::from_str_radix(digits, 16) { + Ok(_) => Ok(()), + Err(_) => Err(invalid( + "Invalid duration param: value must be a valid hex number.", + )), + } +} + /// The bytes of a number as rskj reads one a [`hex_number`] let through /// (`HexUtils.strHexOrStrNumberToByteArray`): behind a `0x` the bytes its /// digits write, leading zeros and all, and without one a decimal in the @@ -157,6 +223,18 @@ pub fn number(text: &str) -> Result { U256::try_from_be_slice(&number_bytes(text)?).ok_or_else(uncaught) } +/// A number as rskj reads one for a transaction it signs +/// (`HexUtils.strHexOrStrNumberToBigInteger`, then the bytes its builder +/// makes of it): as [`number`], but that a bare `0x` leaves `new BigInteger` +/// no digits to read, which nothing catches. Nothing written is no decimal: +/// `-32602`. +pub fn transaction_number(text: &str) -> Result { + if text == "0x" { + return Err(uncaught()); + } + number(text) +} + #[cfg(test)] mod tests { use super::*; diff --git a/crates/rpc/src/sign.rs b/crates/rpc/src/sign.rs index 853dbe4..15040ec 100644 --- a/crates/rpc/src/sign.rs +++ b/crates/rpc/src/sign.rs @@ -8,17 +8,33 @@ //! `eth_signTransaction`, so without a wallet that one is "method not //! found", as rskj answers it. //! +//! With one they answer as rskj does with its wallet on +//! (`EthModuleWalletEnabled`, `EthModuleTransactionBase`). Either way their +//! parameters are read as rskj reads them, before the method runs +//! ([`crate::params`], [`CallArguments`]), and a transaction is written as +//! rskj writes one it builds: what is signed here is what rskj would sign +//! for the same request. +//! //! `eth_sendTransaction` does not have its own path into the pool. It //! fills in what the caller left out, signs, and hands the bytes to the //! same [`crate::server::TxSubmitter`] that `eth_sendRawTransaction` //! uses, so a locally-signed transaction is validated, priced and relayed //! by exactly the code a peer's transaction goes through. -use alloy_primitives::{Address, U256}; +use alloy_primitives::{Address, B256, U256}; +use alloy_rlp::Encodable; use serde_json::{json, Value}; +use sha3::{Digest, Keccak256}; +use crate::dto::{CallArguments, TransactionArguments}; +use crate::params::{hex_address, hex_data, text, uncaught, Refusal}; use crate::server::RpcState; use crate::types::{JsonRpcResponse, INTERNAL_ERROR, INVALID_PARAMS, METHOD_NOT_FOUND}; +use crate::wallet::Wallet; + +fn refuse(id: Value, (code, message): Refusal) -> JsonRpcResponse { + JsonRpcResponse::error(id, code, message) +} /// rskj returns the wallet's accounts; with no wallet the list is empty, /// which is what it has always been here. @@ -32,61 +48,70 @@ pub fn eth_accounts(id: Value, state: &RpcState) -> JsonRpcResponse { /// `eth_sign(address, message)` — 65 bytes over the EIP-191 digest. pub fn eth_sign(id: Value, params: &Value, state: &RpcState) -> JsonRpcResponse { + // jsonrpc4j makes an address and data of the two before the method runs + // (`HexAddressParam`, `HexDataParam`), and leaves a `null` as it is. + let address = params.get(0).and_then(text).map(|text| hex_address(&text)); + let address = match address.transpose() { + Ok(address) => address, + Err(refusal) => return refuse(id, refusal), + }; + let message = params.get(1).and_then(text).map(|text| hex_data(&text)); + let message = match message.transpose() { + Ok(message) => message, + Err(refusal) => return refuse(id, refusal), + }; // rskj's `Web3EthModule.eth_sign` reads both arguments before its wallet // module sees them (L46-48), so a null one is a NullPointerException, // which `RskErrorResolver` answers as an internal error (L56-58), wallet // or no wallet. - if params[0].is_null() || params[1].is_null() { - return JsonRpcResponse::error(id, INTERNAL_ERROR, "Internal server error"); - } + let (Some(address), Some(message)) = (address, message) else { + return refuse(id, uncaught()); + }; let Some(wallet) = &state.wallet else { return crate::stubs::local_wallet_disabled(id); }; - let Some(address) = params - .get(0) - .and_then(|v| v.as_str()) - .and_then(|s| s.parse::
().ok()) - else { - return JsonRpcResponse::error(id, INVALID_PARAMS, "Missing or invalid address"); - }; - let Some(message) = params.get(1).and_then(|v| v.as_str()).and_then(parse_hex) else { - return JsonRpcResponse::error(id, INVALID_PARAMS, "Missing or invalid message hex"); - }; - match wallet.sign_message(&address, &message) { Some(sig) => JsonRpcResponse::success(id, json!(format!("0x{}", hex::encode(sig)))), - None => unknown_account(id, &address), + // `EthModuleWalletEnabled.sign`. + None => JsonRpcResponse::error(id, INVALID_PARAMS, "Account not found"), } } -/// `eth_signTransaction` — the signed RLP, not sent. +/// `eth_signTransaction` — the signed RLP, not sent. rskj has no such +/// method; its transaction is read as `eth_sendTransaction` reads one. pub fn eth_sign_transaction(id: Value, params: &Value, state: &RpcState) -> JsonRpcResponse { - if state.wallet.is_none() { + let Some(wallet) = &state.wallet else { // rskj has no such method. return crate::stubs::method_not_found(id); + }; + if params.get(0).is_none() { + return JsonRpcResponse::error(id, INVALID_PARAMS, "Missing transaction object"); } - match build_and_sign(params, state) { - Ok((_from, raw)) => { - JsonRpcResponse::success(id, json!(format!("0x{}", hex::encode(raw)))) - } - Err(e) => e.into_response(id), + match arguments(params).and_then(|arguments| sign(arguments, wallet, state)) { + Ok(raw) => JsonRpcResponse::success(id, json!(format!("0x{}", hex::encode(raw)))), + Err(refusal) => refuse(id, refusal), } } /// `eth_sendTransaction` — sign, then go through the ordinary relay path. pub async fn eth_send_transaction(id: Value, params: &Value, state: &RpcState) -> JsonRpcResponse { - if state.wallet.is_none() { + // The arguments are checked before the method runs, wallet or no wallet. + let arguments = match arguments(params) { + Ok(arguments) => arguments, + Err(refusal) => return refuse(id, refusal), + }; + let Some(wallet) = &state.wallet else { return crate::stubs::local_wallet_disabled(id); - } + }; // Built before the relay is looked for, so a request naming an // account this node has no key for is told that, rather than being // told the relay is missing -- which is true but is not the caller's // problem and sends them looking in the wrong place. - let raw = match build_and_sign(params, state) { - Ok((_from, raw)) => raw, - Err(e) => return e.into_response(id), + let raw = match sign(arguments, wallet, state) { + Ok(raw) => raw, + Err(refusal) => return refuse(id, refusal), }; let Some(submitter) = &state.tx_submitter else { @@ -99,139 +124,120 @@ pub async fn eth_send_transaction(id: Value, params: &Value, state: &RpcState) - } } -fn unknown_account(id: Value, address: &Address) -> JsonRpcResponse { - JsonRpcResponse::error( - id, - INVALID_PARAMS, - format!("No key for {address:#x}; this node signs only for the accounts it was given"), - ) -} - -enum SignError { - Params(String), - Unknown(Address), - NoChainId, -} - -impl SignError { - fn into_response(self, id: Value) -> JsonRpcResponse { - match self { - SignError::Params(m) => JsonRpcResponse::error(id, INVALID_PARAMS, m), - SignError::Unknown(a) => unknown_account(id, &a), - SignError::NoChainId => JsonRpcResponse::error( - id, - INTERNAL_ERROR, - "No chain configuration, so no chain id to sign against", - ), - } +/// A transaction's arguments, checked as rskj's `CallArgumentsParam` checks +/// them before the method runs. `None` for `null`, and for none given, which +/// reach the method as no arguments at all. +fn arguments(params: &Value) -> Result, Refusal> { + match params.get(0) { + None | Some(Value::Null) => Ok(None), + Some(arguments) => CallArguments::read(arguments).map(Some), } } -/// Fill in what the caller left out, then sign. -/// -/// The defaults are rskj's (`TransactionArgumentsUtil.processArguments`): no -/// value, a gas price of 0, the gas named as `gas` or else as `gasLimit`, or -/// else 90,000 (`GasCost.TRANSACTION_DEFAULT`), and the sender's pending nonce -/// (`EthModuleTransactionBase.sendTransaction`). -fn build_and_sign(params: &Value, state: &RpcState) -> Result<(Address, Vec), SignError> { - let wallet = state.wallet.as_ref().expect("checked by the caller"); - - let obj = params - .get(0) - .filter(|v| v.is_object()) - .ok_or_else(|| SignError::Params("Missing transaction object".into()))?; - - let from = obj - .get("from") - .and_then(|v| v.as_str()) - .and_then(|s| s.parse::
().ok()) - .ok_or_else(|| SignError::Params("Missing or invalid `from`".into()))?; +/// `EthModuleTransactionBase.sendTransaction`, as far as the signed bytes: +/// the sender, who has to be named and held; the arguments +/// ([`CallArguments::transaction`]); the sender's next nonce where they name +/// none; and the signature, for the node's chain, which is the only chain the +/// arguments may name (`acceptTransactionSignature`). +fn sign( + arguments: Option, + wallet: &Wallet, + state: &RpcState, +) -> Result, Refusal> { + let invalid = |message: String| (INVALID_PARAMS, message); + // No arguments at all is a NullPointerException where the method reads + // them, which nothing catches. + let arguments = arguments.ok_or_else(uncaught)?; + let from = arguments + .from() + .ok_or_else(|| invalid("from is null".to_string()))?; + let unknown = || invalid(format!("Could not find account for address: {from:#x}")); if !wallet.holds(&from) { - return Err(SignError::Unknown(from)); + return Err(unknown()); } - - // `to` absent or null is a contract creation, which RSK encodes as an - // empty recipient rather than a zero address. - let to = match obj.get("to") { - None | Some(Value::Null) => Vec::new(), - Some(v) => { - let s = v.as_str().ok_or_else(|| SignError::Params("Invalid `to`".into()))?; - s.parse::
() - .map_err(|_| SignError::Params("Invalid `to`".into()))? - .to_vec() - } - }; - - let input = match obj.get("data").or_else(|| obj.get("input")) { - Some(Value::Null) | None => Vec::new(), - Some(v) => v - .as_str() - .and_then(parse_hex) - .ok_or_else(|| SignError::Params("Invalid `data`".into()))?, - }; - - let value = quantity(obj.get("value"))?.unwrap_or(U256::ZERO); + let arguments = arguments.transaction()?; // The pending nonce, not the latest: a harness that fires several // transactions from one sender without waiting would otherwise give // them all the same nonce and have all but one rejected. - let nonce = match quantity(obj.get("nonce"))? { - Some(n) => n.to::(), - None => state + let nonce = arguments.nonce.unwrap_or_else(|| { + let pending = state .tx_pool .as_ref() - .and_then(|p| p.pending_nonce(&from)) - .unwrap_or_else(|| account_nonce(&from, state)), - }; - - let gas_price = quantity(obj.get("gasPrice"))?.unwrap_or(U256::ZERO); - - let gas_limit = match quantity(obj.get("gas"))? { - Some(g) => g, - None => quantity(obj.get("gasLimit"))?.unwrap_or(U256::from(TRANSACTION_DEFAULT_GAS)), - }; - - let chain_id = state.hardfork_cfg.as_ref().map(|c| c.chain_id).ok_or(SignError::NoChainId)?; - - let mut tx = rustock_core::Transaction { - nonce, - gas_price, - gas_limit, - to: alloy_primitives::Bytes::from(to), - value, - input: alloy_primitives::Bytes::from(input), - v: 0, - r: U256::ZERO, - s: U256::ZERO, - cached_rlp: None, - }; + .and_then(|pool| pool.pending_nonce(&from)) + .unwrap_or_else(|| account_nonce(&from, state)); + U256::from(pending) + }); - if !wallet.sign_transaction(&from, &mut tx, chain_id) { - return Err(SignError::Unknown(from)); + let chain = state + .hardfork_cfg + .as_ref() + .map(|config| config.chain_id) + .ok_or_else(|| { + ( + INTERNAL_ERROR, + "No chain configuration, so no chain id to sign against".to_string(), + ) + })?; + if let Some((named, written)) = &arguments.chain_id { + if u64::from(*named) != chain { + return Err(invalid(format!("Invalid chainId: {written}"))); + } } - Ok((from, tx.rlp_for_trie())) + + let fields = signed_fields(nonce, &arguments); + // What the signature signs (`getEncodedRaw`): the fields, the chain + // (`RLP.encodeByte`) and two empty items. + let mut unsigned = fields.clone(); + chain.encode(&mut unsigned); + unsigned.extend_from_slice(&[alloy_rlp::EMPTY_STRING_CODE; 2]); + let digest = B256::from_slice(&Keccak256::digest(list(&unsigned))); + let (recovery_id, signature) = wallet.sign_digest(&from, digest).ok_or_else(unknown)?; + + // The transaction (`getEncoded`): the fields, v by the chain, and r and + // s in as few bytes as hold them. + let mut signed = fields; + (chain * 2 + 35 + u64::from(recovery_id)).encode(&mut signed); + U256::from_be_slice(&signature[..32]).encode(&mut signed); + U256::from_be_slice(&signature[32..]).encode(&mut signed); + Ok(list(&signed)) } -/// A hex quantity, as every numeric field of the transaction object is. -fn quantity(v: Option<&Value>) -> Result, SignError> { - match v { - None | Some(Value::Null) => Ok(None), - Some(Value::String(s)) => { - let body = s.strip_prefix("0x").unwrap_or(s); - U256::from_str_radix(body, 16) - .map(Some) - .map_err(|_| SignError::Params(format!("Invalid quantity {s}"))) - } - Some(Value::Number(n)) => n - .as_u64() - .map(|x| Some(U256::from(x))) - .ok_or_else(|| SignError::Params("Invalid quantity".into())), - Some(_) => Err(SignError::Params("Invalid quantity".into())), +/// The six fields a signature covers, of a transaction rskj builds from +/// `arguments`, each as rskj writes it (`TransactionBuilder +/// .withTransactionArguments`, then `Transaction.encode`). +/// +/// Each number is written in as few bytes as hold it, and none for zero, +/// but for the gas price: rskj writes a gas price of zero `0x00` +/// (`RLP.encodeCoinNonNullZero`), where canonical RLP writes it empty, which +/// is another transaction with another hash. A sent transaction's gas price +/// is zero unless its arguments name one. +fn signed_fields(nonce: U256, arguments: &TransactionArguments) -> Vec { + let to: &[u8] = arguments.to.as_ref().map_or(&[], |to| to.as_slice()); + let mut fields = Vec::with_capacity(128 + arguments.data.len()); + nonce.encode(&mut fields); + if arguments.gas_price.is_zero() { + fields.push(0); + } else { + arguments.gas_price.encode(&mut fields); } + arguments.gas_limit.encode(&mut fields); + to.encode(&mut fields); + arguments.value.encode(&mut fields); + arguments.data.as_slice().encode(&mut fields); + fields } -fn parse_hex(s: &str) -> Option> { - hex::decode(s.strip_prefix("0x").unwrap_or(s)).ok() +/// The RLP list of `payload`. +fn list(payload: &[u8]) -> Vec { + let mut out = Vec::with_capacity(payload.len() + 9); + alloy_rlp::Header { + list: true, + payload_length: payload.len(), + } + .encode(&mut out); + out.extend_from_slice(payload); + out } fn account_nonce(address: &Address, state: &RpcState) -> u64 { @@ -247,7 +253,3 @@ fn account_nonce(address: &Address, state: &RpcState) -> u64 { .and_then(|s| u64::from_str_radix(s.strip_prefix("0x").unwrap_or(s), 16).ok()) .unwrap_or(0) } - -/// The gas a sent transaction names when the caller names none: rskj's -/// `GasCost.TRANSACTION_DEFAULT`. -const TRANSACTION_DEFAULT_GAS: u64 = 90_000; diff --git a/crates/rpc/src/stubs.rs b/crates/rpc/src/stubs.rs index 16d8d6d..c7539b7 100644 --- a/crates/rpc/src/stubs.rs +++ b/crates/rpc/src/stubs.rs @@ -123,16 +123,58 @@ pub fn local_wallet_disabled(id: Value) -> JsonRpcResponse { JsonRpcResponse::error(id, INVALID_PARAMS, "Local wallet is disabled in this node") } -/// The `personal_` methods as rskj answers them with the wallet off: +/// The `personal_` methods as rskj answers them with the wallet off. +/// +/// jsonrpc4j makes each parameter of its type before the method runs, in the +/// method's order (`Web3` L57-64), and what a type refuses is the answer: an +/// address, a key, a duration and a transaction's arguments refuse with +/// -32602 ([`crate::params`]), and a seed or a passphrase that is an object +/// or an array is -32700 "JSON parse error", answered to no request +/// ([`crate::params::unparsed`]). A `null` is made of nothing, and passes. +/// +/// Then `Web3Impl` refuses a null seed or passphrase (L1130-1144), and /// `PersonalModuleWalletDisabled` throws `DisabledWalletException` from every -/// one (L27-74), -32604 "The local wallet feature is disabled", once -/// `Web3Impl` has refused a null seed or passphrase (L1130-1144). A name rskj -/// doesn't have is "method not found". +/// method (L27-74), -32604 "The local wallet feature is disabled". A name +/// rskj doesn't have is "method not found". /// /// This node has no personal module, so it answers so whether or not /// `--wallet-file` gave it keys, which serve only the `eth_` methods that /// sign. pub fn personal(id: Value, method: &str, params: &Value) -> JsonRpcResponse { + use crate::params::{hex_address, hex_duration, hex_key, string, text, Refusal}; + type Made = Result<(), Refusal>; + let of_text = |index: usize, check: &dyn Fn(&str) -> Made| -> Made { + match params.get(index).and_then(text) { + Some(text) => check(&text), + None => Ok(()), + } + }; + let address = |index| of_text(index, &|text| hex_address(text).map(drop)); + let words = |index: usize| -> Made { string(¶ms[index]).map(drop) }; + let arguments = |index: usize| -> Made { + match params.get(index) { + None | Some(Value::Null) => Ok(()), + Some(arguments) => crate::dto::CallArguments::read(arguments).map(drop), + } + }; + let made = match method { + "personal_newAccountWithSeed" | "personal_newAccount" => words(0), + "personal_listAccounts" => Ok(()), + "personal_importRawKey" => of_text(0, &hex_key).and_then(|()| words(1)), + "personal_sendTransaction" => arguments(0).and_then(|()| words(1)), + "personal_unlockAccount" => address(0) + .and_then(|()| words(1)) + .and_then(|()| of_text(2, &hex_duration)), + "personal_lockAccount" | "personal_dumpRawKey" => address(0), + _ => return method_not_found(id), + }; + if let Err((code, message)) = made { + let id = match code { + crate::types::PARSE_ERROR => json!(crate::params::UNPARSED_ID), + _ => id, + }; + return JsonRpcResponse::error(id, code, message); + } match method { "personal_newAccountWithSeed" if params[0].is_null() => { JsonRpcResponse::error(id, INVALID_PARAMS, "Seed is null") @@ -140,16 +182,6 @@ pub fn personal(id: Value, method: &str, params: &Value) -> JsonRpcResponse { "personal_newAccount" if params[0].is_null() => { JsonRpcResponse::error(id, INVALID_PARAMS, "Passphrase is null") } - "personal_newAccountWithSeed" - | "personal_newAccount" - | "personal_listAccounts" - | "personal_importRawKey" - | "personal_sendTransaction" - | "personal_unlockAccount" - | "personal_lockAccount" - | "personal_dumpRawKey" => { - JsonRpcResponse::error(id, DISABLED_WALLET, "The local wallet feature is disabled") - } - _ => method_not_found(id), + _ => JsonRpcResponse::error(id, DISABLED_WALLET, "The local wallet feature is disabled"), } } diff --git a/crates/rpc/src/testdata/rskj_wallet_methods.txt b/crates/rpc/src/testdata/rskj_wallet_methods.txt new file mode 100644 index 0000000..36c4bea --- /dev/null +++ b/crates/rpc/src/testdata/rskj_wallet_methods.txt @@ -0,0 +1,210 @@ +# What RSKj's release VETIVER-9.0.4 answers for the methods that sign, and for its personal +# module. Its own classes were run (rskj-core-9.0.4-VETIVER-all.jar): JsonRpcCustomServer over +# jsonrpc4j, with the error resolvers JsonRpcWeb3ServerHandler gives it, over a Web3RskImpl +# that has an eth module and a personal module and nothing else. HELD is the address of the +# one key the wallet holds, 32 bytes of 0x11, and the chain is regtest's, 33. +# Lines: +# off a request with the wallet off, which is rskj's default: the method, its +# parameters, "=>", and the answer +# on the same with the wallet on, holding the one key, the personal module still off, +# and a pool that takes every transaction +# signed the arguments of an eth_sendTransaction, "=>", and the transaction rskj signs for +# them, with its hash, or the error it answers and what it threw. The steps are +# EthModuleTransactionBase.sendTransaction's own, with a pending nonce of 7. +off personal_lockAccount ["0x1"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid address: null, empty or invalid hex value."}} +off personal_lockAccount [null] => {"jsonrpc":"2.0","id":1,"error":{"code":-32604,"message":"The local wallet feature is disabled"}} +off personal_lockAccount ["HELD"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32604,"message":"The local wallet feature is disabled"}} +off personal_lockAccount [5] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid address: null, empty or invalid hex value."}} +off personal_lockAccount [{}] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid address: null, empty or invalid hex value."}} +off personal_lockAccount [] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"method parameters invalid"}} +off personal_dumpRawKey ["0xzz2a3d9f938e13cd947ec05abc7fe734df8dd826"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid address format: invalid hex value."}} +off personal_dumpRawKey ["HELD"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32604,"message":"The local wallet feature is disabled"}} +off personal_unlockAccount ["HELD","pass","0x3c"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32604,"message":"The local wallet feature is disabled"}} +off personal_unlockAccount ["0x1",{},"60"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid address: null, empty or invalid hex value."}} +off personal_unlockAccount ["HELD",{},"60"] => {"jsonrpc":"2.0","id":"null","error":{"code":-32700,"message":"JSON parse error"}} +off personal_unlockAccount ["HELD","pass","60"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid argument \"60\": param should be a hex value string."}} +off personal_unlockAccount ["HELD","pass","0x"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid duration param: value must be a valid hex number."}} +off personal_unlockAccount ["HELD","pass","0x8000000000000000"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid duration param: value must be a valid hex number."}} +off personal_unlockAccount ["HELD","pass","0x7fffffffffffffff"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32604,"message":"The local wallet feature is disabled"}} +off personal_unlockAccount ["HELD","pass",""] => {"jsonrpc":"2.0","id":1,"error":{"code":-32604,"message":"The local wallet feature is disabled"}} +off personal_unlockAccount ["HELD","pass",null] => {"jsonrpc":"2.0","id":1,"error":{"code":-32604,"message":"The local wallet feature is disabled"}} +off personal_unlockAccount [null,null,null] => {"jsonrpc":"2.0","id":1,"error":{"code":-32604,"message":"The local wallet feature is disabled"}} +off personal_unlockAccount ["HELD",5,"0x1F"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid argument \"0x1F\": param should be a hex value string."}} +off personal_unlockAccount ["HELD","pass",60] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid argument \"60\": param should be a hex value string."}} +off personal_unlockAccount ["HELD","pass","0x11111111111111111111111111111111111111111111111111111111111111111111"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid argument: 0x1111111111111111111111111111111111111111111111111111111111111111..."}} +off personal_unlockAccount ["HELD","pass"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"method parameters invalid"}} +off personal_importRawKey ["0x0123","pass"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32604,"message":"The local wallet feature is disabled"}} +off personal_importRawKey ["key","pass"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid param key: value must be a valid hex."}} +off personal_importRawKey ["0x0123",{}] => {"jsonrpc":"2.0","id":"null","error":{"code":-32700,"message":"JSON parse error"}} +off personal_importRawKey ["key",{}] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid param key: value must be a valid hex."}} +off personal_importRawKey [null,null] => {"jsonrpc":"2.0","id":1,"error":{"code":-32604,"message":"The local wallet feature is disabled"}} +off personal_importRawKey [5,["x"]] => {"jsonrpc":"2.0","id":"null","error":{"code":-32700,"message":"JSON parse error"}} +off personal_importRawKey ["0X1F","pass"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid param 0X1F: value must be a valid hex."}} +off personal_importRawKey ["1F",5] => {"jsonrpc":"2.0","id":1,"error":{"code":-32604,"message":"The local wallet feature is disabled"}} +off personal_sendTransaction [{"from":"HELD"},"pass"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32604,"message":"The local wallet feature is disabled"}} +off personal_sendTransaction [{"from":"0x1"},"pass"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid address: null, empty or invalid hex value."}} +off personal_sendTransaction [{"from":"0x1"},{}] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid address: null, empty or invalid hex value."}} +off personal_sendTransaction [{"gas":"zz"},"pass"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid param zz: value must be a valid hex or string number."}} +off personal_sendTransaction [null,"pass"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32604,"message":"The local wallet feature is disabled"}} +off personal_sendTransaction ["x",null] => {"jsonrpc":"2.0","id":1,"error":{"code":-32604,"message":"The local wallet feature is disabled"}} +off personal_sendTransaction [{"from":"HELD"},[]] => {"jsonrpc":"2.0","id":"null","error":{"code":-32700,"message":"JSON parse error"}} +off personal_newAccountWithSeed ["seed"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32604,"message":"The local wallet feature is disabled"}} +off personal_newAccountWithSeed [null] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Seed is null"}} +off personal_newAccountWithSeed [{}] => {"jsonrpc":"2.0","id":"null","error":{"code":-32700,"message":"JSON parse error"}} +off personal_newAccountWithSeed [5] => {"jsonrpc":"2.0","id":1,"error":{"code":-32604,"message":"The local wallet feature is disabled"}} +off personal_newAccount ["pass"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32604,"message":"The local wallet feature is disabled"}} +off personal_newAccount [null] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Passphrase is null"}} +off personal_newAccount [[]] => {"jsonrpc":"2.0","id":"null","error":{"code":-32700,"message":"JSON parse error"}} +off personal_listAccounts [] => {"jsonrpc":"2.0","id":1,"error":{"code":-32604,"message":"The local wallet feature is disabled"}} +off personal_listAccounts ["x"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"method parameters invalid"}} +off personal_sign ["x"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32601,"message":"method not found"}} +off eth_accounts [] => {"jsonrpc":"2.0","id":1,"result":[]} +off eth_sign ["HELD","0xdeadbeef"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Local wallet is disabled in this node"}} +off eth_sign ["HELD","0x"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Local wallet is disabled in this node"}} +off eth_sign ["HELD","abc"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Local wallet is disabled in this node"}} +off eth_sign ["0x00000000000000000000000000000000000000ff","0xdeadbeef"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Local wallet is disabled in this node"}} +off eth_sign ["0x1","0xdeadbeef"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid address: null, empty or invalid hex value."}} +off eth_sign ["0x1",null] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid address: null, empty or invalid hex value."}} +off eth_sign [null,"0xzz"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid data format: invalid hex value."}} +off eth_sign ["HELD","0xzz"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid data format: invalid hex value."}} +off eth_sign [null,"0xdeadbeef"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32603,"message":"Internal server error"}} +off eth_sign ["HELD",null] => {"jsonrpc":"2.0","id":1,"error":{"code":-32603,"message":"Internal server error"}} +off eth_sign [null,null] => {"jsonrpc":"2.0","id":1,"error":{"code":-32603,"message":"Internal server error"}} +off eth_sign ["HELD",5] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Local wallet is disabled in this node"}} +off eth_sign ["HELD",{}] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid data format: invalid hex value."}} +off eth_sign ["HELD"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"method parameters invalid"}} +off eth_sendTransaction [{"from":"HELD","to":"0x00000000000000000000000000000000000000aa","nonce":"0x0"}] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Local wallet is disabled in this node"}} +off eth_sendTransaction [{"from":"HELD","to":"0x00000000000000000000000000000000000000aa","value":"0x1","gas":"0x5208","gasPrice":"0x1","nonce":"0x0"}] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Local wallet is disabled in this node"}} +off eth_sendTransaction [{"from":"HELD","to":"0x00000000000000000000000000000000000000aa","value":"1000000000000000000","gasPrice":"60000000","gas":"21000","nonce":"10"}] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Local wallet is disabled in this node"}} +off eth_sendTransaction [{"from":"HELD","to":"0x00000000000000000000000000000000000000aa","nonce":"0x0","chainId":"0x1e"}] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Local wallet is disabled in this node"}} +off eth_sendTransaction [{"from":"HELD","to":"0x00000000000000000000000000000000000000aa","nonce":"0x0","chainId":"0x21"}] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Local wallet is disabled in this node"}} +off eth_sendTransaction [{"from":"HELD","to":"0x00000000000000000000000000000000000000aa","nonce":"0x"}] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Local wallet is disabled in this node"}} +off eth_sendTransaction [{"from":"HELD","to":"0x00000000000000000000000000000000000000aa","nonce":""}] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Local wallet is disabled in this node"}} +off eth_sendTransaction [{"from":"0x00000000000000000000000000000000000000ff"}] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Local wallet is disabled in this node"}} +off eth_sendTransaction [{"to":"0x00000000000000000000000000000000000000aa"}] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Local wallet is disabled in this node"}} +off eth_sendTransaction [null] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Local wallet is disabled in this node"}} +off eth_sendTransaction [{"from":"0x1"}] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid address: null, empty or invalid hex value."}} +off eth_sendTransaction [{"gas":"zz"}] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid param zz: value must be a valid hex or string number."}} +off eth_sendTransaction ["x"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Local wallet is disabled in this node"}} +off eth_sendTransaction [] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"method parameters invalid"}} +on personal_lockAccount ["0x1"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid address: null, empty or invalid hex value."}} +on personal_lockAccount [null] => {"jsonrpc":"2.0","id":1,"error":{"code":-32604,"message":"The local wallet feature is disabled"}} +on personal_lockAccount ["HELD"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32604,"message":"The local wallet feature is disabled"}} +on personal_lockAccount [5] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid address: null, empty or invalid hex value."}} +on personal_lockAccount [{}] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid address: null, empty or invalid hex value."}} +on personal_lockAccount [] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"method parameters invalid"}} +on personal_dumpRawKey ["0xzz2a3d9f938e13cd947ec05abc7fe734df8dd826"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid address format: invalid hex value."}} +on personal_dumpRawKey ["HELD"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32604,"message":"The local wallet feature is disabled"}} +on personal_unlockAccount ["HELD","pass","0x3c"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32604,"message":"The local wallet feature is disabled"}} +on personal_unlockAccount ["0x1",{},"60"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid address: null, empty or invalid hex value."}} +on personal_unlockAccount ["HELD",{},"60"] => {"jsonrpc":"2.0","id":"null","error":{"code":-32700,"message":"JSON parse error"}} +on personal_unlockAccount ["HELD","pass","60"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid argument \"60\": param should be a hex value string."}} +on personal_unlockAccount ["HELD","pass","0x"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid duration param: value must be a valid hex number."}} +on personal_unlockAccount ["HELD","pass","0x8000000000000000"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid duration param: value must be a valid hex number."}} +on personal_unlockAccount ["HELD","pass","0x7fffffffffffffff"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32604,"message":"The local wallet feature is disabled"}} +on personal_unlockAccount ["HELD","pass",""] => {"jsonrpc":"2.0","id":1,"error":{"code":-32604,"message":"The local wallet feature is disabled"}} +on personal_unlockAccount ["HELD","pass",null] => {"jsonrpc":"2.0","id":1,"error":{"code":-32604,"message":"The local wallet feature is disabled"}} +on personal_unlockAccount [null,null,null] => {"jsonrpc":"2.0","id":1,"error":{"code":-32604,"message":"The local wallet feature is disabled"}} +on personal_unlockAccount ["HELD",5,"0x1F"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid argument \"0x1F\": param should be a hex value string."}} +on personal_unlockAccount ["HELD","pass",60] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid argument \"60\": param should be a hex value string."}} +on personal_unlockAccount ["HELD","pass","0x11111111111111111111111111111111111111111111111111111111111111111111"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid argument: 0x1111111111111111111111111111111111111111111111111111111111111111..."}} +on personal_unlockAccount ["HELD","pass"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"method parameters invalid"}} +on personal_importRawKey ["0x0123","pass"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32604,"message":"The local wallet feature is disabled"}} +on personal_importRawKey ["key","pass"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid param key: value must be a valid hex."}} +on personal_importRawKey ["0x0123",{}] => {"jsonrpc":"2.0","id":"null","error":{"code":-32700,"message":"JSON parse error"}} +on personal_importRawKey ["key",{}] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid param key: value must be a valid hex."}} +on personal_importRawKey [null,null] => {"jsonrpc":"2.0","id":1,"error":{"code":-32604,"message":"The local wallet feature is disabled"}} +on personal_importRawKey [5,["x"]] => {"jsonrpc":"2.0","id":"null","error":{"code":-32700,"message":"JSON parse error"}} +on personal_importRawKey ["0X1F","pass"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid param 0X1F: value must be a valid hex."}} +on personal_importRawKey ["1F",5] => {"jsonrpc":"2.0","id":1,"error":{"code":-32604,"message":"The local wallet feature is disabled"}} +on personal_sendTransaction [{"from":"HELD"},"pass"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32604,"message":"The local wallet feature is disabled"}} +on personal_sendTransaction [{"from":"0x1"},"pass"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid address: null, empty or invalid hex value."}} +on personal_sendTransaction [{"from":"0x1"},{}] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid address: null, empty or invalid hex value."}} +on personal_sendTransaction [{"gas":"zz"},"pass"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid param zz: value must be a valid hex or string number."}} +on personal_sendTransaction [null,"pass"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32604,"message":"The local wallet feature is disabled"}} +on personal_sendTransaction ["x",null] => {"jsonrpc":"2.0","id":1,"error":{"code":-32604,"message":"The local wallet feature is disabled"}} +on personal_sendTransaction [{"from":"HELD"},[]] => {"jsonrpc":"2.0","id":"null","error":{"code":-32700,"message":"JSON parse error"}} +on personal_newAccountWithSeed ["seed"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32604,"message":"The local wallet feature is disabled"}} +on personal_newAccountWithSeed [null] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Seed is null"}} +on personal_newAccountWithSeed [{}] => {"jsonrpc":"2.0","id":"null","error":{"code":-32700,"message":"JSON parse error"}} +on personal_newAccountWithSeed [5] => {"jsonrpc":"2.0","id":1,"error":{"code":-32604,"message":"The local wallet feature is disabled"}} +on personal_newAccount ["pass"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32604,"message":"The local wallet feature is disabled"}} +on personal_newAccount [null] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Passphrase is null"}} +on personal_newAccount [[]] => {"jsonrpc":"2.0","id":"null","error":{"code":-32700,"message":"JSON parse error"}} +on personal_listAccounts [] => {"jsonrpc":"2.0","id":1,"error":{"code":-32604,"message":"The local wallet feature is disabled"}} +on personal_listAccounts ["x"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"method parameters invalid"}} +on personal_sign ["x"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32601,"message":"method not found"}} +on eth_accounts [] => {"jsonrpc":"2.0","id":1,"result":["0x19e7e376e7c213b7e7e7e46cc70a5dd086daff2a"]} +on eth_sign ["HELD","0xdeadbeef"] => {"jsonrpc":"2.0","id":1,"result":"0x0a4ee10cbf4152a3270bb891f40a779f380dfe9e16436abb06ea67d9810a1b672c574216577cd2ee9e42908e48649452457ae675e4b3dacda70c720ed03bb8311c"} +on eth_sign ["HELD","0x"] => {"jsonrpc":"2.0","id":1,"result":"0x2a78cca8e3c7653cb7943898806b3b9d74811a26fa9f9db6c4ba07bf714664cb2f841b084f5264df14711a2cc77a6cf0c0cecb6691a132a2a8c6e9321f23d55d1b"} +on eth_sign ["HELD","abc"] => {"jsonrpc":"2.0","id":1,"result":"0xcad801255c23a8fb60a30741b2227be94c3e6cfa8df9e0973f6b4c5052df84fb3e598fc9bcce11b95d57b6b620518ab822a58f97ae2fb5aa80a198c5efb312211b"} +on eth_sign ["0x00000000000000000000000000000000000000ff","0xdeadbeef"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Account not found"}} +on eth_sign ["0x1","0xdeadbeef"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid address: null, empty or invalid hex value."}} +on eth_sign ["0x1",null] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid address: null, empty or invalid hex value."}} +on eth_sign [null,"0xzz"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid data format: invalid hex value."}} +on eth_sign ["HELD","0xzz"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid data format: invalid hex value."}} +on eth_sign [null,"0xdeadbeef"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32603,"message":"Internal server error"}} +on eth_sign ["HELD",null] => {"jsonrpc":"2.0","id":1,"error":{"code":-32603,"message":"Internal server error"}} +on eth_sign [null,null] => {"jsonrpc":"2.0","id":1,"error":{"code":-32603,"message":"Internal server error"}} +on eth_sign ["HELD",5] => {"jsonrpc":"2.0","id":1,"result":"0xd34c8e7743fae108d9a740b7103bf3c8f0428b6f7fbece0bb215e26c53c416422fd64d4a4d1a15c6badb39218619aa45cb15383cb7290529a2c9fb3a1b370aed1b"} +on eth_sign ["HELD",{}] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid data format: invalid hex value."}} +on eth_sign ["HELD"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"method parameters invalid"}} +on eth_sendTransaction [{"from":"HELD","to":"0x00000000000000000000000000000000000000aa","nonce":"0x0"}] => {"jsonrpc":"2.0","id":1,"result":"0xf889bc4a2e380df0ab0217f428416e9ac6f48b67f2c66ebc4d3622f98738a1c7"} +on eth_sendTransaction [{"from":"HELD","to":"0x00000000000000000000000000000000000000aa","value":"0x1","gas":"0x5208","gasPrice":"0x1","nonce":"0x0"}] => {"jsonrpc":"2.0","id":1,"result":"0x6e845ae403365af1b8585934057741dea0c55b6ffba2e9e1f5c972ba643fbfeb"} +on eth_sendTransaction [{"from":"HELD","to":"0x00000000000000000000000000000000000000aa","value":"1000000000000000000","gasPrice":"60000000","gas":"21000","nonce":"10"}] => {"jsonrpc":"2.0","id":1,"result":"0x6ae7f589cf364aaf4fe4abd2d359f1f7c96483ad09a45c3bba6a81584524b014"} +on eth_sendTransaction [{"from":"HELD","to":"0x00000000000000000000000000000000000000aa","nonce":"0x0","chainId":"0x1e"}] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid chainId: 0x1e"}} +on eth_sendTransaction [{"from":"HELD","to":"0x00000000000000000000000000000000000000aa","nonce":"0x0","chainId":"0x21"}] => {"jsonrpc":"2.0","id":1,"result":"0xf889bc4a2e380df0ab0217f428416e9ac6f48b67f2c66ebc4d3622f98738a1c7"} +on eth_sendTransaction [{"from":"HELD","to":"0x00000000000000000000000000000000000000aa","nonce":"0x"}] => {"jsonrpc":"2.0","id":1,"error":{"code":-32603,"message":"Internal server error"}} +on eth_sendTransaction [{"from":"HELD","to":"0x00000000000000000000000000000000000000aa","nonce":""}] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Number values should not contain letters or hex values should start with 0x."}} +on eth_sendTransaction [{"from":"0x00000000000000000000000000000000000000ff"}] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Could not find account for address: 0x00000000000000000000000000000000000000ff"}} +on eth_sendTransaction [{"to":"0x00000000000000000000000000000000000000aa"}] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"from is null"}} +on eth_sendTransaction [null] => {"jsonrpc":"2.0","id":1,"error":{"code":-32603,"message":"Internal server error"}} +on eth_sendTransaction [{"from":"0x1"}] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid address: null, empty or invalid hex value."}} +on eth_sendTransaction [{"gas":"zz"}] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid param zz: value must be a valid hex or string number."}} +on eth_sendTransaction ["x"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"from is null"}} +on eth_sendTransaction [] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"method parameters invalid"}} +signed {"from":"HELD","to":"0x00000000000000000000000000000000000000aa","value":"0x1","gas":"0x5208","gasPrice":"0x1","nonce":"0x0"} => f85f80018252089400000000000000000000000000000000000000aa018065a08f041ac7fb73fbcf70ebea8433813363501f327c2212c2a990c142c204d487e4a076dfd0cd10ce9de76770214ac5300e5a23bedbe065eacad0d1ea4bd164032c16 hash 0x6e845ae403365af1b8585934057741dea0c55b6ffba2e9e1f5c972ba643fbfeb +signed {"from":"HELD","to":"0x00000000000000000000000000000000000000aa"} => f860070083015f909400000000000000000000000000000000000000aa808066a03259a5b7560b2fb4f339b6a19ba887f767d2f205aa81839a0ad3f47e957941a4a05337a1bf1667ff6713b04e0c2f30c87b1af7c61be5524db5122be00883ce28ac hash 0xb3677825eae944e3111f5d7d500d3de73bf615f492a6630d1da0a4f1eee6b0aa +signed {"from":"HELD","to":"0x00000000000000000000000000000000000000aa","value":"1000000000000000000","gasPrice":"60000000","gas":"21000","nonce":"10"} => f86b0a84039387008252089400000000000000000000000000000000000000aa880de0b6b3a76400008066a0120acfba0458c328cb0a60bbbc41863eae0042ef213ed10808f673df61260d8aa06aeed87b19b4846527ea14af2a1ba2e65b48ec48739e74a04091110fb7edc19d hash 0x6ae7f589cf364aaf4fe4abd2d359f1f7c96483ad09a45c3bba6a81584524b014 +signed {"from":"HELD","to":"0x00000000000000000000000000000000000000aa","value":5,"gas":21000,"gasPrice":60000000,"nonce":1} => f8630184039387008252089400000000000000000000000000000000000000aa058065a05ea2ba1eaca639441d2d0ed441d1e2acb81a1d5349d26bc5017f207ddb95e590a00f1399a7660852f7f548bc6f015bab4076640ec74398c7aff4f09002d79cb663 hash 0xc16a26aeebaf9b6b1ed0afd6099c3ec84293bdb5c47d99f02b4b38fc7b187520 +signed {"from":"HELD","data":"0x6000","gas":"0x100000","gasPrice":"0x1","nonce":"0x0"} => f84e800183100000808082600066a0b01790f392889b8dec1d99131714d497b4e0bd041f50b25e6fd702459759c0eda03b243f84b5044d5d8dca7e8928279a623f64276daa16e23052374ca22f6b1ac0 hash 0x8866c31f6400cd4d756d72cdb65d604288580339fd5647ae8c031cac2a344c9f +signed {"from":"HELD","to":"0x00000000000000000000000000000000000000aa","gasLimit":"0x7530"} => f85f07008275309400000000000000000000000000000000000000aa808066a05a630664d2022060fda2ba1b0ae832e0fcd3f1cf1a3846e54564ca5b8abd69caa070a51dcdb5abf7d3c9f9242f0333aecafaed768ddfa1508ed4085fd211884b63 hash 0xfcd7042a04f9544afb4dbadaec1a8f20bfa9a84028d11479f8cf8c195d503db0 +signed {"from":"HELD","to":"0x00000000000000000000000000000000000000aa","gas":"0x5208","gasLimit":"ff"} => f85f07008252089400000000000000000000000000000000000000aa808066a08215b96a9f4da38dedd112cbbcf5e18d530f2e3c2eadf160baf85c137ad23bdaa060e11fc8d49dee9c802aadbd0533be8eadd943d58cb936c8c22ba71d5890a3a8 hash 0x60fc3f2b854d445587369b9341751da44c652472f01e1c9a3de93af06dd48857 +signed {"from":"HELD","to":"0x00000000000000000000000000000000000000aa","gasLimit":"ff"} => -32602 Number values should not contain letters or hex values should start with 0x. [RskJsonRpcRequestException] +signed {"from":"HELD","to":"0x00000000000000000000000000000000000000aa","nonce":""} => -32602 Number values should not contain letters or hex values should start with 0x. [RskJsonRpcRequestException] +signed {"from":"HELD","to":"0x00000000000000000000000000000000000000aa","nonce":"0x"} => -32603 Internal server error [NumberFormatException] +signed {"from":"HELD","to":"0x00000000000000000000000000000000000000aa","value":"0x"} => -32603 Internal server error [NumberFormatException] +signed {"from":"HELD","to":"0x00000000000000000000000000000000000000aa","value":""} => -32602 Number values should not contain letters or hex values should start with 0x. [RskJsonRpcRequestException] +signed {"from":"HELD","to":"0x00000000000000000000000000000000000000aa","gasPrice":""} => -32602 Number values should not contain letters or hex values should start with 0x. [RskJsonRpcRequestException] +signed {"from":"HELD","to":"0x00000000000000000000000000000000000000aa","gas":""} => -32602 Number values should not contain letters or hex values should start with 0x. [RskJsonRpcRequestException] +signed {"from":"HELD","to":"0x00000000000000000000000000000000000000aa","value":"-1"} => f861070083015f909400000000000000000000000000000000000000aa81ff8066a03908d1a217ceafac95c2f3cea5d6986bfb5b643459a2a9d60ee20e1e6dfde5a8a063f8f8613f3036be890ba305edcd563b3dfa7c3ac18e0a23b8b4596e9c33d665 hash 0xea84e6593fae808c25a3dedf0956a346087d9e3c6559cc11f7d8df8df8f20e3c +signed {"from":"HELD","to":"0x00000000000000000000000000000000000000aa","gasPrice":"-1","gas":"-1","nonce":"-1"} => f86081ff81ff81ff9400000000000000000000000000000000000000aa808066a006a3108f9cd4f9358f1b796d6b1248ae43672c022923369c444b8f842ebb6f01a0744b947927cd4404801560ed5837b1948e3c2b5de9db438093b0a2d84fc961f5 hash 0x096e5144e9a5ef572a3c9e0865d8add40f69be92d597075f6e33ab310478c9cd +signed {"from":"HELD","to":"0x00000000000000000000000000000000000000aa","gasPrice":"-129","gas":"-32769","nonce":"-256","value":"-128"} => f86582ff0082ff7f83ff7fff9400000000000000000000000000000000000000aa81808065a007c27f24aa7c5f7eb7df86b98205558b5c7fa91d61bbd8fa7a358ac1ba60993ea07d00abedf6ab44675d117b31adcb942eb03936ff5a32222388deaf15b2ce9ec9 hash 0x0d40aaa835d0a08fb16e1c00dd46e73d320bb3f56016c850cb43a93dec0daf5a +signed {"from":"HELD","to":"0x00000000000000000000000000000000000000aa","chainId":"0x21"} => f860070083015f909400000000000000000000000000000000000000aa808066a03259a5b7560b2fb4f339b6a19ba887f767d2f205aa81839a0ad3f47e957941a4a05337a1bf1667ff6713b04e0c2f30c87b1af7c61be5524db5122be00883ce28ac hash 0xb3677825eae944e3111f5d7d500d3de73bf615f492a6630d1da0a4f1eee6b0aa +signed {"from":"HELD","to":"0x00000000000000000000000000000000000000aa","chainId":"33"} => f860070083015f909400000000000000000000000000000000000000aa808066a03259a5b7560b2fb4f339b6a19ba887f767d2f205aa81839a0ad3f47e957941a4a05337a1bf1667ff6713b04e0c2f30c87b1af7c61be5524db5122be00883ce28ac hash 0xb3677825eae944e3111f5d7d500d3de73bf615f492a6630d1da0a4f1eee6b0aa +signed {"from":"HELD","to":"0x00000000000000000000000000000000000000aa","chainId":33} => f860070083015f909400000000000000000000000000000000000000aa808066a03259a5b7560b2fb4f339b6a19ba887f767d2f205aa81839a0ad3f47e957941a4a05337a1bf1667ff6713b04e0c2f30c87b1af7c61be5524db5122be00883ce28ac hash 0xb3677825eae944e3111f5d7d500d3de73bf615f492a6630d1da0a4f1eee6b0aa +signed {"from":"HELD","to":"0x00000000000000000000000000000000000000aa","chainId":"0"} => f860070083015f909400000000000000000000000000000000000000aa808066a03259a5b7560b2fb4f339b6a19ba887f767d2f205aa81839a0ad3f47e957941a4a05337a1bf1667ff6713b04e0c2f30c87b1af7c61be5524db5122be00883ce28ac hash 0xb3677825eae944e3111f5d7d500d3de73bf615f492a6630d1da0a4f1eee6b0aa +signed {"from":"HELD","to":"0x00000000000000000000000000000000000000aa","chainId":"0x00"} => f860070083015f909400000000000000000000000000000000000000aa808066a03259a5b7560b2fb4f339b6a19ba887f767d2f205aa81839a0ad3f47e957941a4a05337a1bf1667ff6713b04e0c2f30c87b1af7c61be5524db5122be00883ce28ac hash 0xb3677825eae944e3111f5d7d500d3de73bf615f492a6630d1da0a4f1eee6b0aa +signed {"from":"HELD","to":"0x00000000000000000000000000000000000000aa","chainId":"0x1e"} => -32602 Invalid chainId: 0x1e [RskJsonRpcRequestException] +signed {"from":"HELD","to":"0x00000000000000000000000000000000000000aa","chainId":"0x0021"} => -32602 Invalid chainId: 0x0021 [RskJsonRpcRequestException] +signed {"from":"HELD","to":"0x00000000000000000000000000000000000000aa","chainId":""} => -32602 Invalid chainId: [RskJsonRpcRequestException] +signed {"from":"HELD","to":"0x00000000000000000000000000000000000000aa","chainId":"0x"} => -32602 Invalid chainId: 0x [RskJsonRpcRequestException] +signed {"from":"HELD","to":"0x00000000000000000000000000000000000000aa","chainId":"-1"} => -32602 Invalid chainId: -1 [RskJsonRpcRequestException] +signed {"from":"HELD","to":"0x00000000000000000000000000000000000000aa","chainId":"ff"} => -32602 Invalid chainId: ff [RskJsonRpcRequestException] +signed {"from":"HELD","to":"0x00000000000000000000000000000000000000aa","chainId":"289"} => -32602 Invalid chainId: 289 [RskJsonRpcRequestException] +signed {"from":"HELD","to":"0x00000000000000000000000000000000000000aa","data":"0x1234","input":"0xabcd"} => f862070083015f909400000000000000000000000000000000000000aa8082abcd66a0372a3cb96949f91d48b80995f62cdf34d8f90f41b5ba9fadb353d8285ffe1a62a007a3a6f2492d4b4d5b12a5b1f1409d919b22fe5bcf42a0a5b0322df10a3d8b1c hash 0x8e25f6d0769dcb2954052fc7f378a3755db1d67a59347b79fc0a5fa4b4f840a2 +signed {"from":"HELD","to":"0x00000000000000000000000000000000000000aa","data":"0x1234","input":""} => f860070083015f909400000000000000000000000000000000000000aa808066a03259a5b7560b2fb4f339b6a19ba887f767d2f205aa81839a0ad3f47e957941a4a05337a1bf1667ff6713b04e0c2f30c87b1af7c61be5524db5122be00883ce28ac hash 0xb3677825eae944e3111f5d7d500d3de73bf615f492a6630d1da0a4f1eee6b0aa +signed {"from":"HELD","to":"0x00000000000000000000000000000000000000aa","data":"abc"} => f862070083015f909400000000000000000000000000000000000000aa80820abc66a01f6066ed74ca0db5f55e9e7e3afb9d4b6e76bd935b01ad61d515347765a278e9a05a5e7f6f185475fc0d75b6267086f6c759641bff98452d941b800378a963268c hash 0xf6cd893fdc70ee9f395d45d548abfc9a12322d0d9a48099cd3f8593bc72bda77 +signed {"from":"HELD","to":"0x00000000000000000000000000000000000000aa","gas":"0x0"} => f85d0700809400000000000000000000000000000000000000aa808066a05c88a4aa45a63595818a6244fa652745e7b3a621047134f685eadf2c70c62372a03bad81a139b093e255a7c11828deb3a02bbce5fb089c99e89faebd3ba18c72f6 hash 0x1573570877c87b246afb6d93440d92d3d82da3c6cfc0a55652118d490503f7d5 +signed {"from":"HELD","to":"0x00000000000000000000000000000000000000aa","gasPrice":"0x0","value":"0x0","nonce":"0x0"} => f860800083015f909400000000000000000000000000000000000000aa808065a0a05867550596b0cbf6af344888ac3a7815c429bf5471bb1a667717ce6cc4178aa05b73e7cf48410d5407a1545efe835919634ddca40aa20e85dbb68be6d2cba82c hash 0xf889bc4a2e380df0ab0217f428416e9ac6f48b67f2c66ebc4d3622f98738a1c7 +signed {"from":"HELD","to":"0x00000000000000000000000000000000000000aa","nonce":"0x10000000000000000"} => f869890100000000000000000083015f909400000000000000000000000000000000000000aa808065a05e009b4d288d48682747d6c1960ad7c09d92764b0b36f9d3d49b7f1007a55faba0767627e50023a285d60bcc448710c65f2fd7d6287880633d39c0a6e12ea12928 hash 0xc3616d990e7f6f5f9f4dcfa95695e1cdaa37b6a08ed8c66b3c66ff671131ceca +signed {"from":"HELD","to":"0x00000000000000000000000000000000000000aa","value":"0x00000000000000000000000000000000000000000000000000000000000000ff"} => f861070083015f909400000000000000000000000000000000000000aa81ff8066a03908d1a217ceafac95c2f3cea5d6986bfb5b643459a2a9d60ee20e1e6dfde5a8a063f8f8613f3036be890ba305edcd563b3dfa7c3ac18e0a23b8b4596e9c33d665 hash 0xea84e6593fae808c25a3dedf0956a346087d9e3c6559cc11f7d8df8df8f20e3c +signed {"from":"HELD","to":"0x00000000000000000000000000000000000000aa","gasPrice":"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"} => f88007a0ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff83015f909400000000000000000000000000000000000000aa808065a0ac48c3bc4f0d772fe9209d2abc2b460ef2bee2a9b958c1b933f80330d33d92e5a021e0acde5700a34bb77303cedf94cdb8b7ea4321acc58727b7de3059f0349a29 hash 0x3cacf4f7cccd942f9e053817726d13faa4f8f33bf8857bcafa128df71420214e +signed {"from":"HELD","to":"0x00000000000000000000000000000000000000aa","gas":"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"} => f87d0700a0ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff9400000000000000000000000000000000000000aa808066a0ec3562e646e0d118711910cefa3451cfc59a9adf52eca149409608e0699ad6d0a023def7caf2982c5578b0175a9139246fdd17e97fbddf6b9d08011619920c44d6 hash 0x140815a0fc74debb23223b590b2bd2266ada4dee13f8735f371940acae51ea27 +signed {"to":"0x00000000000000000000000000000000000000aa"} => -32602 from is null [RskJsonRpcRequestException] +signed "x" => -32602 from is null [RskJsonRpcRequestException] +signed {"from":"0x00000000000000000000000000000000000000ff"} => -32602 Could not find account for address: 0x00000000000000000000000000000000000000ff [RskJsonRpcRequestException] +signed {"from":"0x00000000000000000000000000000000000000FF"} => -32602 Could not find account for address: 0x00000000000000000000000000000000000000ff [RskJsonRpcRequestException] +signed {"from":"HELD","to":"0x00000000000000000000000000000000000000aa","value":"ff"} => -32602 Number values should not contain letters or hex values should start with 0x. [RskJsonRpcRequestException] +signed {"from":"HELD","to":"0x00000000000000000000000000000000000000aa","nonce":"ff","value":"ee"} => -32602 Number values should not contain letters or hex values should start with 0x. [RskJsonRpcRequestException] +signed {"from":"HELD","to":"0x00000000000000000000000000000000000000aa","value":"ee","gasPrice":"0x"} => -32602 Number values should not contain letters or hex values should start with 0x. [RskJsonRpcRequestException] diff --git a/crates/rpc/src/tests.rs b/crates/rpc/src/tests.rs index 1150c12..d14ad65 100644 --- a/crates/rpc/src/tests.rs +++ b/crates/rpc/src/tests.rs @@ -6857,16 +6857,31 @@ async fn test_signing_is_refused_for_an_account_the_wallet_does_not_hold() { let (state, _tmp) = setup_state_with_wallet(); let stranger = "0x00000000000000000000000000000000000000ff"; - for (method, params) in [ - ("eth_sign", json!([stranger, "0xdeadbeef"])), - ("eth_signTransaction", json!([{"from": stranger}])), - ("eth_sendTransaction", json!([{"from": stranger}])), + // rskj's words for it: `EthModuleWalletEnabled.sign`, and + // `EthModuleTransactionBase.sendTransaction`. + let unknown = format!("Could not find account for address: {stranger}"); + for (method, params, message) in [ + ( + "eth_sign", + json!([stranger, "0xdeadbeef"]), + "Account not found", + ), + ( + "eth_signTransaction", + json!([{"from": stranger}]), + unknown.as_str(), + ), + ( + "eth_sendTransaction", + json!([{"from": stranger}]), + unknown.as_str(), + ), ] { let resp = dispatch_for_test(&state, make_request(method, params)).await; assert!(resp.result.is_none(), "{method} must not sign for a stranger"); let err = resp.error.expect("an error"); assert_eq!(err.code, INVALID_PARAMS, "{method}"); - assert!(err.message.contains("No key for"), "{method}: {}", err.message); + assert_eq!(err.message, message, "{method}"); } } @@ -6943,6 +6958,121 @@ async fn test_a_null_recipient_is_a_contract_creation() { assert!(tx.to.is_empty(), "a creation has an empty recipient, not a zero address"); } +/// A pool in which every sender's next nonce is 7. +struct SeventhNonce; + +impl crate::server::TxPoolReader for SeventhNonce { + fn get_pending_tx( + &self, + _: &alloy_primitives::B256, + ) -> Option<(rustock_core::Transaction, Address, alloy_primitives::B256)> { + None + } + + fn pending_nonce(&self, _: &Address) -> Option { + Some(7) + } + + fn pool_status(&self) -> (usize, usize) { + (0, 0) + } + + fn pool_content(&self) -> crate::server::PoolContent { + crate::server::PoolContent::default() + } + + fn quota_report(&self, _: &Address) -> Option<(f64, u64)> { + None + } +} + +/// A relay that takes every transaction, and names it by its hash. +struct TakesEvery; + +#[async_trait::async_trait] +impl crate::server::TxSubmitter for TakesEvery { + async fn submit_transaction( + &self, + raw: alloy_primitives::Bytes, + ) -> Result { + use sha3::Digest; + Ok(alloy_primitives::B256::from_slice( + &sha3::Keccak256::digest(&raw), + )) + } +} + +/// The methods that sign, and the personal module, answer as rskj's release +/// answers them, request by request: `testdata/rskj_wallet_methods.txt` is +/// its own server's answers with its wallet off and on, and the transactions +/// it signs. +/// +/// What used to differ: a number written without its `0x` was read as hex +/// where rskj reads a decimal, so that a value of "1000000000000000000" was +/// signed as some 4,722 times that; a chain the arguments named went unread; +/// `data` was read over `input`; a gas price of zero was written empty, which +/// is another transaction than rskj's; and a malformed parameter was answered +/// by the method, or as the wallet being off, where rskj refuses it first. +#[tokio::test] +async fn the_wallet_methods_answer_as_rskjs_release_answers() { + let key = secp256k1::SecretKey::from_byte_array([0x11; 32]).unwrap(); + let wallet = crate::wallet::Wallet::from_keys([key]); + let held = format!("{:#x}", wallet.accounts()[0]); + let (off, _tmp_off) = setup_state(); + let (mut on, _tmp_on) = setup_state(); + on.wallet = Some(Arc::new(wallet)); + on.hardfork_cfg = Some(rustock_execution::hardfork::RskHardforkConfig::all_active_parallel(33)); + on.tx_pool = Some(Arc::new(SeventhNonce)); + on.tx_submitter = Some(Arc::new(TakesEvery)); + + let mut lines = 0; + for line in include_str!("testdata/rskj_wallet_methods.txt").lines() { + if line.starts_with('#') { + continue; + } + lines += 1; + let line = line.replace("HELD", &held); + let (kind, rest) = line.split_once(' ').unwrap(); + let (asked, answer) = rest.split_once(" => ").unwrap(); + if kind == "signed" { + // `eth_signTransaction` gives the transaction, and + // `eth_sendTransaction` its hash. + let arguments: Value = serde_json::from_str(asked).unwrap(); + let request = |method| make_request(method, json!([arguments])); + let signed = dispatch_for_test(&on, request("eth_signTransaction")).await; + let sent = dispatch_for_test(&on, request("eth_sendTransaction")).await; + match answer.split_once(" hash ") { + Some((raw, hash)) => { + assert_eq!(signed.result, Some(json!(format!("0x{raw}"))), "{line}"); + assert_eq!(sent.result, Some(json!(hash)), "{line}"); + } + None => { + // A code, the message, and what rskj threw in brackets. + let (code, message) = answer.split_once(' ').unwrap(); + let message = &message[..message.rfind(" [").unwrap()]; + let refusal = Some((code.parse::().unwrap(), message.to_string())); + for resp in [signed, sent] { + let error = resp.error.map(|error| (error.code, error.message)); + assert_eq!(error, refusal, "{line}"); + } + } + } + continue; + } + let state = match kind { + "off" => &off, + "on" => &on, + other => panic!("{other}"), + }; + let (method, params) = asked.split_once(' ').unwrap(); + let params: Value = serde_json::from_str(params).unwrap(); + let answer: Value = serde_json::from_str(answer).unwrap(); + let ours = dispatch_for_test(state, make_request(method, params)).await; + assert_eq!(serde_json::to_value(&ours).unwrap(), answer, "{line}"); + } + assert_eq!(lines, 197, "every line of the file was asked"); +} + /// Reading a key file is the only way a key gets in, so the parsing has /// to accept what an operator will actually write. #[test] diff --git a/crates/rpc/src/wallet.rs b/crates/rpc/src/wallet.rs index 08eadb7..bf945ac 100644 --- a/crates/rpc/src/wallet.rs +++ b/crates/rpc/src/wallet.rs @@ -152,6 +152,20 @@ impl Wallet { Some(out) } + /// A signature over `digest` by the key for `address`: its recovery id, + /// then r and s. `None` when this wallet does not hold the key. + /// + /// For the hash a transaction is signed by. A message goes through + /// [`Wallet::sign_message`], which hashes it under its prefix first. + pub fn sign_digest(&self, address: &Address, digest: B256) -> Option<(u8, [u8; 64])> { + let secret = self.keys.get(address)?; + let (recid, sig) = self + .secp + .sign_ecdsa_recoverable(Message::from_digest(digest.0), secret) + .serialize_compact(); + Some((i32::from(recid) as u8, sig)) + } + /// Sign a transaction in place, EIP-155 style. /// /// Returns `false` when this wallet does not hold the key, so the From 96e9f537961df136a2abec711701269b22c77eb6 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Thu, 12 Nov 2026 21:10:15 -0800 Subject: [PATCH 131/264] sync, rpc: a sent transaction is taken or refused as rskj's pool does A transaction the pool refused was answered -32602 over RPC. rskj answers its own -32010 (transactionError) with the pool's reason, from eth_sendRawTransaction and eth_sendTransaction both. The pool took and refused in an order of its own, so that of two faults it named another than rskj names, and some of rskj's checks it did not make: - A transaction whose nonce lay among the sender's pending ones, and was not the first of them, went to the queue beside the one it was to replace, however little it paid. rskj replaces a pending transaction of any nonce, for the bump, and queues only a nonce past the sender's next. - A sender the state holds no account for was "insufficient funds". rskj says "the sender account doesn't exist", before anything else. - A creation's init code past RSKIP438's limit was taken. Its basic cost left out RSKIP438's 2 gas for every 32 bytes of init code, and counted a data byte at 16 gas before RSKIP400 too. - A queued transaction needed the bump to take a queued one's place, and was counted by the rate limiter. rskj puts a queued one in the place of another without asking, and asks the limiter only of a transaction on its way to pending, before it counts the sender's funds. - A queued transaction whose turn came was moved to pending as it was. rskj offers it again as a new one, and drops it if it is refused then. The pool now follows TransactionPoolImpl.internalAddTransaction and TxPendingValidator.isValid step by step, with rskj's messages: which of the two sets holds a hash already, a gas limit and the limit it had to fit, a size and the most it may be, a nonce of more than eight bytes. eth_sendRawTransaction reads its parameter, and the transaction, as rskj does before its pool sees it: "method parameters invalid" for any count of parameters but one; "Invalid input: " and the decoder's own words for bytes that are no RLP, for which the decoder's errors now say why; an internal error for RLP that is no transaction; "Missing parameter, gasPrice, gas or value" for no gas limit; and "Invalid chainId: " with the chain the signature names for a signature rskj does not take, a missing one and one in the upper half of the curve's order among them. Not followed: a transaction that core's reader does not hold (a nonce past 64 bits, a field past 32 bytes) is refused by the pool in this pool's words, where rskj's pool has its own for each. And the pool keeps its ceiling on how many transactions it holds, which rskj's has none of. The answers in the tests are the release's own. Its JSON-RPC server was run over the Web3 its RskContext builds on regtest, with its real pool, on 115 requests; the RPC test holds the two methods to its answers, and the pool's test holds this pool to that one's answers and to what it held after each. --- crates/core/src/rskj_rlp.rs | 88 +- crates/rpc/src/eth.rs | 38 +- crates/rpc/src/lib.rs | 1 + crates/rpc/src/raw_tx.rs | 153 +++ crates/rpc/src/server.rs | 2 +- crates/rpc/src/sign.rs | 3 +- crates/rpc/src/tests.rs | 122 ++- crates/rpc/src/types.rs | 3 + .../sync/src/testdata/rskj_pool_answers.txt | 156 ++++ crates/sync/src/txpool.rs | 873 +++++++++++++----- 10 files changed, 1179 insertions(+), 260 deletions(-) create mode 100644 crates/rpc/src/raw_tx.rs create mode 100644 crates/sync/src/testdata/rskj_pool_answers.txt diff --git a/crates/core/src/rskj_rlp.rs b/crates/core/src/rskj_rlp.rs index 0b7353a..621072c 100644 --- a/crates/core/src/rskj_rlp.rs +++ b/crates/core/src/rskj_rlp.rs @@ -9,9 +9,44 @@ //! and `bytesToLength`, with `RLPItem` and `RLPList`, rather than decoding //! canonically. -/// Where rskj's decoder throws. +/// Where rskj's decoder throws, and for which reason. #[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub struct RlpError; +pub enum RlpError { + /// An element runs past the end of the bytes. + PastEnd, + /// A length's own bytes run past the end. + LengthPastEnd, + /// A length with its sign bit set. + NegativeLength, + /// A list's length, with its prefix or with where it starts, is past a + /// Java `int`. + TooLong, + /// An item where a list is read. rskj throws no `RLPException` here: its + /// cast to `RLPList` fails. + NotAList, + /// No bytes to read an element from, where rskj reads past its array. + Empty, +} + +impl RlpError { + /// What rskj's `RLPException` says, where rskj throws one: what a caller + /// that catches it, as `eth_sendRawTransaction` does, reports. + pub fn message(&self) -> Option<&'static str> { + match self { + Self::PastEnd => Some( + "The RLP byte array doesn't have enough space to hold an element with the specified length", + ), + Self::LengthPastEnd => { + Some("The length of the RLP item length can't possibly fit the data byte array") + } + Self::NegativeLength => Some("The length of the RLP item can't be negative"), + Self::TooLong => Some( + "The current implementation doesn't support lengths longer than Integer.MAX_VALUE because that is the largest number of elements an array can have. integer overflow", + ), + Self::NotAList | Self::Empty => None, + } + } +} /// One element, as `RLP.decodeElement` cuts it. #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -45,7 +80,7 @@ impl<'a> Element<'a> { pub fn elements(&self) -> Result>, RlpError> { match *self { Element::List { encoding, offset } => decode2(&encoding[offset..]), - Element::Item(_) => Err(RlpError), + Element::Item(_) => Err(RlpError::NotAList), } } } @@ -66,7 +101,7 @@ pub fn decode2(data: &[u8]) -> Result>, RlpError> { /// starts. pub fn first_element(data: &[u8]) -> Result<(Element<'_>, usize), RlpError> { if data.is_empty() { - return Err(RlpError); + return Err(RlpError::Empty); } decode_element(data, 0) } @@ -82,11 +117,16 @@ fn decode_element(data: &[u8], position: usize) -> Result<(Element<'_>, usize), let nbytes = usize::from(b0 - 0xf7); let offset = 1 + nbytes as i32; let payload = bytes_to_length(data, position + 1, nbytes)?; - (offset.checked_add(payload).ok_or(RlpError)?, offset) + ( + offset.checked_add(payload).ok_or(RlpError::TooLong)?, + offset, + ) }; - let ending = (position as i32).checked_add(length).ok_or(RlpError)? as usize; + let ending = (position as i32) + .checked_add(length) + .ok_or(RlpError::TooLong)? as usize; if ending > data.len() { - return Err(RlpError); + return Err(RlpError::PastEnd); } let encoding = &data[position..ending]; return Ok(( @@ -114,7 +154,7 @@ fn decode_element(data: &[u8], position: usize) -> Result<(Element<'_>, usize), let from = position + offset; let to = from + length as usize; if to > data.len() { - return Err(RlpError); + return Err(RlpError::PastEnd); } Ok((Element::Item(&data[from..to]), to)) } @@ -123,14 +163,14 @@ fn decode_element(data: &[u8], position: usize) -> Result<(Element<'_>, usize), /// four bytes keeps only its low 32 bits, and a negative one is refused. fn bytes_to_length(data: &[u8], position: usize, size: usize) -> Result { if position + size > data.len() { - return Err(RlpError); + return Err(RlpError::LengthPastEnd); } let mut length = 0i32; for byte in &data[position..position + size] { length = length.wrapping_shl(8).wrapping_add(i32::from(*byte)); } if length < 0 { - return Err(RlpError); + return Err(RlpError::NegativeLength); } Ok(length) } @@ -165,7 +205,11 @@ mod tests { ); let inner = decoded[0].elements().unwrap(); assert_eq!(inner, vec![Element::Item(&[0x1b]), Element::Item(&[0x80])]); - assert_eq!(inner[0].elements(), Err(RlpError), "an item is no list"); + assert_eq!( + inner[0].elements(), + Err(RlpError::NotAList), + "an item is no list" + ); } /// rskj takes what canonical RLP refuses: a byte below 0x80 as a string, @@ -192,23 +236,29 @@ mod tests { fn a_long_length_wraps_and_a_negative_one_is_refused() { let wrapped = [0xbc, 0x01, 0x00, 0x00, 0x00, 0x02, 0xaa, 0xbb]; assert_eq!(items(&wrapped), Ok(vec![Some(vec![0xaa, 0xbb])])); - assert_eq!(items(&[0xbb, 0x80, 0x00, 0x00, 0x00]), Err(RlpError)); - assert_eq!(items(&[0xfb, 0x80, 0x00, 0x00, 0x00]), Err(RlpError)); + let negative = Err(RlpError::NegativeLength); + assert_eq!(items(&[0xbb, 0x80, 0x00, 0x00, 0x00]), negative); + assert_eq!(items(&[0xfb, 0x80, 0x00, 0x00, 0x00]), negative); // A list's length counts its prefix, so a length of -1 would net the // five-byte prefix a list of four bytes, and reading would go on to // the well-formed list after it. rskj refuses it before that. let minus_one = [0xfb, 0xff, 0xff, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0]; - assert_eq!(items(&minus_one), Err(RlpError)); + assert_eq!(items(&minus_one), negative); + // A list's length and its prefix together past an `int`. + assert_eq!( + items(&[0xfb, 0x7f, 0xff, 0xff, 0xff]), + Err(RlpError::TooLong) + ); } /// Anything that runs past the end is refused, an item's length bytes too. #[test] fn an_overrun_is_refused() { - assert_eq!(items(&[0x83, 0x01]), Err(RlpError)); - assert_eq!(items(&[0xb9, 0x01]), Err(RlpError)); - assert_eq!(items(&[0xc3, 0x01]), Err(RlpError)); - assert_eq!(items(&[0xf9, 0x00]), Err(RlpError)); - assert_eq!(items(&[0x01, 0x82, 0xaa]), Err(RlpError)); + assert_eq!(items(&[0x83, 0x01]), Err(RlpError::PastEnd)); + assert_eq!(items(&[0xb9, 0x01]), Err(RlpError::LengthPastEnd)); + assert_eq!(items(&[0xc3, 0x01]), Err(RlpError::PastEnd)); + assert_eq!(items(&[0xf9, 0x00]), Err(RlpError::LengthPastEnd)); + assert_eq!(items(&[0x01, 0x82, 0xaa]), Err(RlpError::PastEnd)); } /// No variation on nested lists makes reading them, at any depth, panic: diff --git a/crates/rpc/src/eth.rs b/crates/rpc/src/eth.rs index 7ee61af..be10518 100644 --- a/crates/rpc/src/eth.rs +++ b/crates/rpc/src/eth.rs @@ -2,12 +2,10 @@ use crate::helpers::{ parse_b256, parse_block_number, parse_hex_u32, to_hex_b256, to_hex_u64, BlockResultDto, }; -use crate::server::TxSubmitter; use crate::types::*; use rustock_core::config::ChainConfig; use rustock_storage::BlockStore; use serde_json::{json, Value}; -use std::sync::Arc; /// rskj's `eth_protocolVersion` answers `rsk_protocolVersion()` (`Web3Impl` /// L300-303). @@ -518,30 +516,46 @@ fn header_claims_an_empty_body(header: &rustock_core::types::header::Header) -> empty_txs && empty_ommers } +/// `eth_sendRawTransaction`, answered as rskj answers it +/// (`EthModuleTransactionBase.sendRawTransaction`). +/// +/// The one parameter is data (`HexDataParam`), and a `null` reaches the +/// method as no data at all, which nothing catches. What rskj refuses before +/// its pool sees the transaction is [`crate::raw_tx::refused`]'s to say; what +/// the pool refuses is rskj's `transactionError`, -32010, with the pool's +/// reason; and what it takes is answered with the transaction's hash. pub async fn eth_send_raw_transaction( id: Value, params: &Value, - tx_submitter: &Option>, + state: &crate::server::RpcState, ) -> JsonRpcResponse { - let Some(submitter) = tx_submitter else { + let Some(submitter) = &state.tx_submitter else { return JsonRpcResponse::error(id, METHOD_NOT_FOUND, "Transaction relay not available"); }; - let Some(raw_hex) = params.as_array().and_then(|a| a.first()).and_then(|v| v.as_str()) else { - return JsonRpcResponse::error(id, INVALID_PARAMS, "Missing raw transaction hex"); + // jsonrpc4j finds no method of the name for any other count. + let [sent] = params.as_array().map(Vec::as_slice).unwrap_or_default() else { + return JsonRpcResponse::error(id, INVALID_PARAMS, "method parameters invalid"); }; - - let raw_hex = raw_hex.strip_prefix("0x").unwrap_or(raw_hex); - let Ok(raw_bytes) = hex::decode(raw_hex) else { - return JsonRpcResponse::error(id, INVALID_PARAMS, "Invalid hex"); + let read = match crate::params::text(sent) { + Some(written) => crate::params::hex_data(&written).map(|raw| (raw, written)), + None => Err(crate::params::uncaught()), + }; + let chain = state.hardfork_cfg.as_ref().map(|config| config.chain_id); + let raw = match read.and_then(|(raw, written)| { + crate::raw_tx::refused(&raw, &written, chain)?; + Ok(raw) + }) { + Ok(raw) => raw, + Err((code, message)) => return JsonRpcResponse::error(id, code, message), }; match submitter - .submit_transaction(alloy_primitives::Bytes::from(raw_bytes)) + .submit_transaction(alloy_primitives::Bytes::from(raw)) .await { Ok(tx_hash) => JsonRpcResponse::success(id, json!(format!("0x{:x}", tx_hash))), - Err(e) => JsonRpcResponse::error(id, INVALID_PARAMS, e), + Err(e) => JsonRpcResponse::error(id, TRANSACTION_ERROR, e), } } diff --git a/crates/rpc/src/lib.rs b/crates/rpc/src/lib.rs index 1c5b77e..8929766 100644 --- a/crates/rpc/src/lib.rs +++ b/crates/rpc/src/lib.rs @@ -12,6 +12,7 @@ pub mod admin; pub mod net; pub mod params; pub mod proof; +pub mod raw_tx; pub mod rsk; pub mod sco; pub mod server; diff --git a/crates/rpc/src/raw_tx.rs b/crates/rpc/src/raw_tx.rs new file mode 100644 index 0000000..3baf195 --- /dev/null +++ b/crates/rpc/src/raw_tx.rs @@ -0,0 +1,153 @@ +//! A raw transaction as rskj's `eth_sendRawTransaction` reads it before its +//! pool sees it, for what rskj answers where it refuses one there +//! (`EthModuleTransactionBase.sendRawTransaction`). +//! +//! rskj makes a `Transaction` of the bytes, asks whether it has a gas limit, +//! and asks whether its signature is one this network takes. Of everything +//! that can go wrong on the way it catches one thing, its decoder's +//! `RLPException`, which it answers -32602 "Invalid input: " and the +//! exception's words. An address of the wrong length is answered by its error +//! resolver, in words made for another method; and whatever else is thrown is +//! an internal error, -32603. +//! +//! What gets through is the pool's to judge, and a transaction the pool +//! refuses is rskj's `transactionError`, -32010, with the pool's reason. + +use alloy_primitives::U256; +use rustock_core::rskj_rlp::{decode2, Element, RlpError}; + +use crate::params::{uncaught, Refusal}; +use crate::types::INVALID_PARAMS; + +fn invalid(message: String) -> Refusal { + (INVALID_PARAMS, message) +} + +/// What the method answers where its decoder throws: an `RLPException` is +/// caught and reported, anything else is not. +fn unread(error: RlpError) -> Refusal { + match error.message() { + Some(message) => invalid(format!("Invalid input: {message}")), + None => uncaught(), + } +} + +/// The chain a signature's `v` names (`Transaction.extractChainIdFromV`), in +/// the Java `byte` rskj keeps it in: none for 27 and 28, and otherwise +/// `(v - 35) / 2`, which is below zero for a `v` under 34. +fn chain_named(v: u8) -> i8 { + match v { + 27 | 28 => 0, + v => ((i32::from(v) - 35) / 2) as i8, + } +} + +/// A signature's r or s as rskj reads it, `new BigInteger(1, bytes)`, where +/// it fits 256 bits. One that doesn't is past the curve's order. +fn component(bytes: &[u8]) -> Option { + let first = bytes + .iter() + .position(|byte| *byte != 0) + .unwrap_or(bytes.len()); + let significant = &bytes[first..]; + (significant.len() <= 32).then(|| U256::from_be_slice(significant)) +} + +/// `Err` with rskj's answer where it refuses `raw` before its pool sees it. +/// +/// `written` is the parameter as the request wrote it, which the answer to a +/// misshapen address counts the characters of. `chain` is this network's +/// chain, or none where the node doesn't know it, and then any chain passes +/// here and the pool decides. +/// +/// In rskj's order: the bytes are one RLP list (`RLP.decodeList`) of nine +/// elements; the receiver is no bytes or 20; `v` is absent, for no +/// signature, or one byte, and then r and s are there; the gas limit is +/// there; and the signature is one the network takes +/// (`acceptTransactionSignature`): r from 1 to below the curve's order, s +/// from 1 to below half of it, for no chain or for this one. A signature +/// that isn't is answered "Invalid chainId" and the chain it names, whatever +/// was wrong with it, as a missing one is. +pub fn refused(raw: &[u8], written: &str, chain: Option) -> Result<(), Refusal> { + let elements = decode2(raw).map_err(unread)?; + let [list @ Element::List { .. }] = elements.as_slice() else { + return Err(uncaught()); + }; + let fields = list.elements().map_err(unread)?; + let [_nonce, _gas_price, gas_limit, to, _value, _data, v, r, s] = fields.as_slice() else { + return Err(uncaught()); + }; + + // `new RskAddress(bytes)` throws `InvalidRskAddressException`, which + // `RskErrorResolver` answers for every method alike, by the length of + // the request's first parameter less every "0x" in it. + if to.data().is_some_and(|to| to.len() != 20) { + let length = written.replace("0x", "").encode_utf16().count(); + return Err(invalid(format!( + "invalid argument 0: hex string has length {length}, want 40 for RSK address" + ))); + } + + let signature = match v.data() { + None => None, + // `ECDSASignature.fromComponents` makes numbers of r and s, and of + // a missing one a `NullPointerException`. + Some(&[v]) => match (r.data(), s.data()) { + (Some(r), Some(s)) => Some((v, r, s)), + _ => return Err(uncaught()), + }, + // `TransactionException`, "Signature V is invalid". + Some(_) => return Err(uncaught()), + }; + + if gas_limit.data().is_none() { + return Err(invalid( + "Missing parameter, gasPrice, gas or value".to_string(), + )); + } + + let named = match signature { + None => 0, + Some((v, r, s)) => { + let named = chain_named(v); + let order = U256::from_be_bytes(secp256k1::constants::CURVE_ORDER); + let in_range = |number: Option, below: U256| { + number.is_some_and(|number| !number.is_zero() && number < below) + }; + let ours = named == 0 || chain.is_none_or(|chain| u64::try_from(named) == Ok(chain)); + if ours && in_range(component(r), order) && in_range(component(s), order >> 1) { + return Ok(()); + } + named + } + }; + Err(invalid(format!("Invalid chainId: {named}"))) +} + +#[cfg(test)] +mod tests { + use super::*; + + /// The chain a `v` names, in Java's arithmetic: its division rounds + /// toward zero, so 34 to 36 name none, and what is under 34 names a chain + /// below zero. + #[test] + fn a_v_names_its_chain_as_java_reckons_it() { + for (v, chain) in [ + (27, 0), + (28, 0), + (0, -17), + (26, -4), + (33, -1), + (34, 0), + (36, 0), + (37, 1), + (95, 30), + (101, 33), + (102, 33), + (255, 110), + ] { + assert_eq!(chain_named(v), chain, "v {v}"); + } + } +} diff --git a/crates/rpc/src/server.rs b/crates/rpc/src/server.rs index 64e7083..18b8f06 100644 --- a/crates/rpc/src/server.rs +++ b/crates/rpc/src/server.rs @@ -494,7 +494,7 @@ async fn route(state: &RpcState, req: JsonRpcRequest) -> JsonRpcResponse { "rsk_getRawBlockHeaderByHash" => rsk::rsk_get_raw_block_header_by_hash(id, params, &state.store), "rsk_getRawBlockHeaderByNumber" => rsk::rsk_get_raw_block_header_by_number(id, params, &state.store), - "eth_sendRawTransaction" => eth::eth_send_raw_transaction(id, params, &state.tx_submitter).await, + "eth_sendRawTransaction" => eth::eth_send_raw_transaction(id, params, state).await, // -- state queries -- "eth_getBalance" => state::eth_get_balance(id, params, state), diff --git a/crates/rpc/src/sign.rs b/crates/rpc/src/sign.rs index 15040ec..ea60dc6 100644 --- a/crates/rpc/src/sign.rs +++ b/crates/rpc/src/sign.rs @@ -120,7 +120,8 @@ pub async fn eth_send_transaction(id: Value, params: &Value, state: &RpcState) - match submitter.submit_transaction(alloy_primitives::Bytes::from(raw)).await { Ok(hash) => JsonRpcResponse::success(id, json!(format!("0x{hash:x}"))), - Err(e) => JsonRpcResponse::error(id, INVALID_PARAMS, e), + // rskj `transactionError`: what its pool refuses. + Err(e) => JsonRpcResponse::error(id, crate::types::TRANSACTION_ERROR, e), } } diff --git a/crates/rpc/src/tests.rs b/crates/rpc/src/tests.rs index d14ad65..2c8cc36 100644 --- a/crates/rpc/src/tests.rs +++ b/crates/rpc/src/tests.rs @@ -1034,13 +1034,19 @@ async fn test_eth_send_raw_transaction_with_submitter() { events: None, }; - let req = make_request("eth_sendRawTransaction", json!(["0xdeadbeef"])); + // A transfer signed for regtest: a relay is handed nothing less than a + // transaction rskj would hand its pool. + let raw = "f86480830f424082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb8203e88066\ + a00afb0a237587b9df05d9cc04fac838c19d27e55cb2eb7a7e48d2ed9764d4d339\ + a019fa1a246958cbe41757aa014972af79d4d844bdf67d06c2b2afaa778dd71b9d"; + let req = make_request("eth_sendRawTransaction", json!([format!("0x{raw}")])); let resp = dispatch_for_test(&state, req).await; assert!(resp.error.is_none(), "Should succeed with a submitter"); - let result = resp.result.unwrap(); - let hash_str = result.as_str().unwrap(); - assert!(hash_str.starts_with("0x")); - assert_eq!(hash_str.len(), 66); + let hash = sha3::Keccak256::digest(hex::decode(raw).unwrap()); + assert_eq!( + resp.result.unwrap(), + json!(format!("0x{}", hex::encode(hash))) + ); } #[tokio::test] @@ -1094,6 +1100,112 @@ async fn test_eth_send_raw_transaction_invalid_hex() { assert!(resp.error.is_some(), "Should error on invalid hex"); } +/// The bytes a testdata line writes in hex, a run of one byte as +/// `(bb*count)`. +fn written_bytes(text: &str) -> Vec { + let mut bytes = Vec::new(); + let mut rest = text; + while !rest.is_empty() { + if let Some(run) = rest.strip_prefix('(') { + let (value, after) = run.split_once('*').unwrap(); + let (count, after) = after.split_once(')').unwrap(); + let value = u8::from_str_radix(value, 16).unwrap(); + bytes.extend(std::iter::repeat_n(value, count.parse().unwrap())); + rest = after; + } else { + bytes.push(u8::from_str_radix(&rest[..2], 16).unwrap()); + rest = &rest[2..]; + } + } + bytes +} + +/// A relay that answers a transaction as the testdata line being replayed +/// says rskj's pool answered it: with the reason it was refused for, or with +/// the transaction's hash as rskj reckons it. +#[derive(Default)] +struct AnswersAsRskjsPool { + refusal: std::sync::Mutex>, + asked: std::sync::atomic::AtomicUsize, +} + +#[async_trait::async_trait] +impl crate::server::TxSubmitter for AnswersAsRskjsPool { + async fn submit_transaction( + &self, + raw: alloy_primitives::Bytes, + ) -> Result { + self.asked.fetch_add(1, std::sync::atomic::Ordering::SeqCst); + if let Some(reason) = self.refusal.lock().unwrap().clone() { + return Err(reason); + } + rustock_core::Transaction::from_rskj(&raw) + .map(|tx| tx.tx_hash()) + .map_err(|unread| unread.to_string()) + } +} + +/// `eth_sendRawTransaction` and `eth_sendTransaction` answer as rskj's +/// release answers them, request by request: the testdata, which the pool's +/// own test replays too, is that release's server answering over its real +/// pool. The pool here says what rskj's said, so this is what the methods +/// make of it, and what they refuse before any pool is asked. +/// +/// What used to differ: a transaction the pool refused was answered -32602, +/// where rskj answers -32010; and what rskj refuses first was the pool's to +/// refuse, in the pool's words: bytes that are no RLP, or no transaction, a +/// transaction with no gas limit, and a signature for another chain, in the +/// upper half of the curve's order, or missing. +#[tokio::test] +async fn a_sent_transaction_is_answered_as_rskj_answers_it() { + use std::sync::atomic::Ordering::SeqCst; + let relay = Arc::new(AnswersAsRskjsPool::default()); + let (mut state, _tmp) = setup_state(); + // The key of the account `w`, which rskj's node was given too. + let key = secp256k1::SecretKey::from_byte_array(alloy_primitives::keccak256("w").0).unwrap(); + state.wallet = Some(Arc::new(crate::wallet::Wallet::from_keys([key]))); + state.hardfork_cfg = Some(rustock_execution::hardfork::RskHardforkConfig::rskj_regtest()); + state.tx_submitter = Some(relay.clone()); + + let mut lines = 0; + for line in include_str!("../../sync/src/testdata/rskj_pool_answers.txt").lines() { + let (kind, rest) = line.split_once(' ').unwrap_or_default(); + if !matches!(kind, "tx" | "call") { + continue; + } + lines += 1; + let (asked, rest) = rest.split_once(" => ").unwrap(); + let (answer, _held) = rest.split_once(" | pending ").unwrap(); + let answer: Value = serde_json::from_str(answer).unwrap(); + let (label, asked) = asked.split_once(' ').unwrap(); + let (method, params) = match kind { + "tx" => { + let raw = format!("0x{}", hex::encode(written_bytes(asked))); + ("eth_sendRawTransaction", json!([raw])) + } + _ => { + let (method, params) = asked.split_once(' ').unwrap(); + (method, serde_json::from_str(params).unwrap()) + } + }; + + // What rskj's pool said of it, where it got that far. + let refused_by_pool = answer["error"]["code"] == json!(TRANSACTION_ERROR); + let reason = answer["error"]["message"].as_str().map(str::to_string); + *relay.refusal.lock().unwrap() = reason.filter(|_| refused_by_pool); + let before = relay.asked.load(SeqCst); + let ours = dispatch_for_test(&state, make_request(method, params)).await; + assert_eq!(serde_json::to_value(&ours).unwrap(), answer, "{label}"); + let reached = refused_by_pool || answer.get("result").is_some(); + assert_eq!( + relay.asked.load(SeqCst) - before, + usize::from(reached), + "{label}: whether the pool was asked" + ); + } + assert_eq!(lines, 115, "every request of the file was asked"); +} + // ========== Phase 6: State queries with trie ========== fn setup_state_with_trie() -> (RpcState, tempfile::TempDir) { diff --git a/crates/rpc/src/types.rs b/crates/rpc/src/types.rs index 4bd7685..023d0de 100644 --- a/crates/rpc/src/types.rs +++ b/crates/rpc/src/types.rs @@ -6,6 +6,9 @@ pub const INVALID_REQUEST: i64 = -32600; pub const METHOD_NOT_FOUND: i64 = -32601; pub const INVALID_PARAMS: i64 = -32602; pub const INTERNAL_ERROR: i64 = -32603; +/// rskj's own, `RskJsonRpcRequestException.transactionError`: a transaction +/// its pool refuses. +pub const TRANSACTION_ERROR: i64 = -32010; #[derive(Debug, Deserialize)] pub struct JsonRpcRequest { diff --git a/crates/sync/src/testdata/rskj_pool_answers.txt b/crates/sync/src/testdata/rskj_pool_answers.txt new file mode 100644 index 0000000..efc5455 --- /dev/null +++ b/crates/sync/src/testdata/rskj_pool_answers.txt @@ -0,0 +1,156 @@ +# What RSKj's release VETIVER-9.0.4 answers when it is sent a transaction, and what its +# pool holds afterwards. Its own classes were run (rskj-core-9.0.4-VETIVER-all.jar): its +# JSON-RPC server over the Web3 its RskContext builds on regtest, with its transaction pool. +# Lines: +# scenario a name. Each scenario starts with an empty pool. +# head the block the pool validates against: its number, gas limit and minimum gas +# price. Block 0 is regtest's genesis; block 1 is a block made on it. +# account a sender: a name, its address, and its nonce and balance in that block's +# state, or `none` for an account that is not there. An account's key is the +# keccak-256 of its name. +# tx a label, then the bytes sent as eth_sendRawTransaction's one parameter, in hex +# (a run of 40 or more equal bytes is written `(bb*count)`), then `=>` and the +# answer, then the first eight hex digits of the hash of every transaction the +# pool holds as pending, and as queued, behind their counts. +# call the same for any other request: a label, the method, and its parameters. +# setup a request that only prepares rskj: it gives the node the key of the account w. +# Decimal numbers but for the bytes and the hashes. +scenario main +head 1 10000000 1000000 +account a 8e8161a194db181783245dfae95dbc632eb958cb 0 1000000000000000000 +account b 6a724df99dd9d5c56c07ead5837c75a150c878c3 0 1000000000000000000 +account c f16587770d49a85bb497c74f065a213e2e165aa3 5 1000000000000000000 +account d 167a1fcb46229ab8c5e327b0cfec64e1ce113c5c 0 1000000000000000000 +account e 73da4cf358b4c031f363fb960963c1871fd3c13a 0 1000000000000000000 +account f af38454307fa6a9c9dd57787b8faf1d14f973202 0 1000000000000000000 +account g 8102a32f505d6bcf1ff1ba9944a3365c0a7a1a05 0 1000000000000000000 +account h fedf0985f0dd1922cf3d60e35e5348a6fb37c922 0 1000000000000000000 +account hexy 6761574cbd45df085686965942770cecb721cf3e 0 1000000000000000000 +account i 53eb2269d2f775acb8fcad2519a17df2d72fe3d2 0 1000000000000000000 +account nobody 3bd52ac93106ecf372507350a717905f93b74d5a none +account p ea389c99f5df1e080f228790a676ac5aace98c8a 0 1000000000000000000 +account poor b0a37f25f995693b637931362b837f14a9aaa2de 0 30000000000 +account q 9b7fb7d09f3af2d637f94b863f6ef92a321a4dca 0 50000000000 +account r a2be67f5bd728df59cefd430cc3a454bb4029b45 0 50000000000 +account s 1b2406bf04b9e309fd9c173ab6359150e3508512 0 50000000000 +account t c73c53c66befc15e7d5854d3f8891d7d053f5a3f 0 1000000000000000000 +call no-params eth_sendRawTransaction [] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"method parameters invalid"}} | pending 0 | queued 0 +call two-params eth_sendRawTransaction ["0x00","0x00"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"method parameters invalid"}} | pending 0 | queued 0 +call null eth_sendRawTransaction [null] => {"jsonrpc":"2.0","id":1,"error":{"code":-32603,"message":"Internal server error"}} | pending 0 | queued 0 +call not-hex eth_sendRawTransaction ["zz"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid data format: invalid hex value."}} | pending 0 | queued 0 +call number eth_sendRawTransaction [123] => {"jsonrpc":"2.0","id":1,"error":{"code":-32603,"message":"Internal server error"}} | pending 0 | queued 0 +call object eth_sendRawTransaction [{}] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid data format: invalid hex value."}} | pending 0 | queued 0 +call array eth_sendRawTransaction [[]] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid data format: invalid hex value."}} | pending 0 | queued 0 +call bool eth_sendRawTransaction [true] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid data format: invalid hex value."}} | pending 0 | queued 0 +call empty-string eth_sendRawTransaction [""] => {"jsonrpc":"2.0","id":1,"error":{"code":-32603,"message":"Internal server error"}} | pending 0 | queued 0 +call bare-prefix eth_sendRawTransaction ["0x"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32603,"message":"Internal server error"}} | pending 0 | queued 0 +tx not-a-list 80 => {"jsonrpc":"2.0","id":1,"error":{"code":-32603,"message":"Internal server error"}} | pending 0 | queued 0 +tx one-byte 05 => {"jsonrpc":"2.0","id":1,"error":{"code":-32603,"message":"Internal server error"}} | pending 0 | queued 0 +tx empty-list c0 => {"jsonrpc":"2.0","id":1,"error":{"code":-32603,"message":"Internal server error"}} | pending 0 | queued 0 +tx two-items 0102 => {"jsonrpc":"2.0","id":1,"error":{"code":-32603,"message":"Internal server error"}} | pending 0 | queued 0 +tx list-overrun c5010203 => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid input: The RLP byte array doesn't have enough space to hold an element with the specified length"}} | pending 0 | queued 0 +tx long-list-overrun f8ff010203 => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid input: The RLP byte array doesn't have enough space to hold an element with the specified length"}} | pending 0 | queued 0 +tx length-of-length-overrun f901 => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid input: The length of the RLP item length can't possibly fit the data byte array"}} | pending 0 | queued 0 +tx negative-length fb80000000 => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid input: The length of the RLP item can't be negative"}} | pending 0 | queued 0 +tx length-overflow fb7fffffff => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid input: The current implementation doesn't support lengths longer than Integer.MAX_VALUE because that is the largest number of elements an array can have. integer overflow"}} | pending 0 | queued 0 +tx item-overrun-inside c3830102 => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid input: The RLP byte array doesn't have enough space to hold an element with the specified length"}} | pending 0 | queued 0 +tx item-length-overrun-inside c2b901 => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid input: The length of the RLP item length can't possibly fit the data byte array"}} | pending 0 | queued 0 +tx item-negative-inside c5bb80000000 => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid input: The length of the RLP item can't be negative"}} | pending 0 | queued 0 +tx eight-items f84380830f424082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb8203e88066a00afb0a237587b9df05d9cc04fac838c19d27e55cb2eb7a7e48d2ed9764d4d339 => {"jsonrpc":"2.0","id":1,"error":{"code":-32603,"message":"Internal server error"}} | pending 0 | queued 0 +tx ten-items f86580830f424082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb8203e88066a00afb0a237587b9df05d9cc04fac838c19d27e55cb2eb7a7e48d2ed9764d4d339a019fa1a246958cbe41757aa014972af79d4d844bdf67d06c2b2afaa778dd71b9d80 => {"jsonrpc":"2.0","id":1,"error":{"code":-32603,"message":"Internal server error"}} | pending 0 | queued 0 +tx trailing-byte f86480830f424082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb8203e88066a00afb0a237587b9df05d9cc04fac838c19d27e55cb2eb7a7e48d2ed9764d4d339a019fa1a246958cbe41757aa014972af79d4d844bdf67d06c2b2afaa778dd71b9d00 => {"jsonrpc":"2.0","id":1,"error":{"code":-32603,"message":"Internal server error"}} | pending 0 | queued 0 +tx to-19-bytes f86380830f424082520893bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb8203e88066a0bec405ec5dd94c0764e07f1832e05c7b0c92efa83da7af52e308119683ee5d93a04fd2ba2e91932f540004535228f00c66cee79c0e5b382042ea26cccf03cf3834 => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"invalid argument 0: hex string has length 202, want 40 for RSK address"}} | pending 0 | queued 0 +tx to-21-bytes f86580830f424082520895bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb8203e88065a076854ba89ce6b8905f10f416ae52febe92a8aab8f0fb94b39695200c973fb7f5a00e80a77c949840f0973f0dbc40f8f8709d3025933443060b89e2059aa35acc9b => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"invalid argument 0: hex string has length 206, want 40 for RSK address"}} | pending 0 | queued 0 +tx v-two-bytes f86680830f424082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb8203e880820065a00afb0a237587b9df05d9cc04fac838c19d27e55cb2eb7a7e48d2ed9764d4d339a019fa1a246958cbe41757aa014972af79d4d844bdf67d06c2b2afaa778dd71b9d => {"jsonrpc":"2.0","id":1,"error":{"code":-32603,"message":"Internal server error"}} | pending 0 | queued 0 +tx r-empty f84480830f424082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb8203e8806680a019fa1a246958cbe41757aa014972af79d4d844bdf67d06c2b2afaa778dd71b9d => {"jsonrpc":"2.0","id":1,"error":{"code":-32603,"message":"Internal server error"}} | pending 0 | queued 0 +tx s-empty f84480830f424082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb8203e88066a00afb0a237587b9df05d9cc04fac838c19d27e55cb2eb7a7e48d2ed9764d4d33980 => {"jsonrpc":"2.0","id":1,"error":{"code":-32603,"message":"Internal server error"}} | pending 0 | queued 0 +tx v-only e480830f424082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb8203e880658080 => {"jsonrpc":"2.0","id":1,"error":{"code":-32603,"message":"Internal server error"}} | pending 0 | queued 0 +tx gas-empty f86280830f42408094bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb8203e88066a0b2a0dac7f84193df094f73cbd75d47d7ff702cecee5761e8570c81303bece13aa012cce61f0ee606a363615705c1e1f2123abbecbc6c0b86e1001a448e8b396d09 => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Missing parameter, gasPrice, gas or value"}} | pending 0 | queued 0 +tx gas-empty-unsigned e280830f42408094bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb8203e880808080 => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Missing parameter, gasPrice, gas or value"}} | pending 0 | queued 0 +tx unsigned e480830f424082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb8203e880808080 => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid chainId: 0"}} | pending 0 | queued 0 +tx other-chain f86480830f424082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb8203e88060a02f17c8ce7139c1c1dd57027cb1fee2ddc58f05386428887df8eb261a41ec69d8a07aabfcf51133903586f6bbb4b11470c9527557ae55b3d0c40a1104a7302cd063 => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid chainId: 30"}} | pending 0 | queued 0 +tx chain-one f86480830f424082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb8203e88025a0981d49dd1cd8bb8df72bc99cf072bebeebd69f2f33bb54f5486000efd24dfce5a00d4f0e30f34ca1a6eec0a265d25566976aee973fe0780be139507b4f9c9bed44 => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid chainId: 1"}} | pending 0 | queued 0 +tx v-zero f86480830f424082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb8203e88000a00afb0a237587b9df05d9cc04fac838c19d27e55cb2eb7a7e48d2ed9764d4d339a019fa1a246958cbe41757aa014972af79d4d844bdf67d06c2b2afaa778dd71b9d => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid chainId: -17"}} | pending 0 | queued 0 +tx v-twenty-six f86480830f424082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb8203e8801aa00afb0a237587b9df05d9cc04fac838c19d27e55cb2eb7a7e48d2ed9764d4d339a019fa1a246958cbe41757aa014972af79d4d844bdf67d06c2b2afaa778dd71b9d => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid chainId: -4"}} | pending 0 | queued 0 +tx v-thirty-four f86480830f424082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb8203e88022a00afb0a237587b9df05d9cc04fac838c19d27e55cb2eb7a7e48d2ed9764d4d339a019fa1a246958cbe41757aa014972af79d4d844bdf67d06c2b2afaa778dd71b9d => {"jsonrpc":"2.0","id":1,"error":{"code":-32010,"message":"the sender account doesn't exist"}} | pending 0 | queued 0 +tx v-255 f86580830f424082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb8203e88081ffa00afb0a237587b9df05d9cc04fac838c19d27e55cb2eb7a7e48d2ed9764d4d339a019fa1a246958cbe41757aa014972af79d4d844bdf67d06c2b2afaa778dd71b9d => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid chainId: 110"}} | pending 0 | queued 0 +tx high-s f86480830f424082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb8203e88065a00afb0a237587b9df05d9cc04fac838c19d27e55cb2eb7a7e48d2ed9764d4d339a0e605e5db96a7341be8a855feb68d5084e5d69828b8cb99790d22b415425f25a4 => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid chainId: 33"}} | pending 0 | queued 0 +tx r-zero f84480830f424082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb8203e8806600a019fa1a246958cbe41757aa014972af79d4d844bdf67d06c2b2afaa778dd71b9d => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid chainId: 33"}} | pending 0 | queued 0 +tx s-zero f84480830f424082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb8203e88066a00afb0a237587b9df05d9cc04fac838c19d27e55cb2eb7a7e48d2ed9764d4d33900 => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid chainId: 33"}} | pending 0 | queued 0 +tx r-order f86480830f424082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb8203e88066a0fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141a019fa1a246958cbe41757aa014972af79d4d844bdf67d06c2b2afaa778dd71b9d => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid chainId: 33"}} | pending 0 | queued 0 +tx r-33-bytes f86580830f424082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb8203e88066a1010000000000000000000000000000000000000000000000000000000000000000a019fa1a246958cbe41757aa014972af79d4d844bdf67d06c2b2afaa778dd71b9d => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid chainId: 33"}} | pending 0 | queued 0 +tx legacy f86480830f424082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb8203e8801ba08318d453870710a68d9e2eda190fc12ce2648976655e2662984e57e47e33fd4ca06d3e0a49ef37ece1fa53530a5abaca13a17ab53e8a2024b79ddbb975ef3e8ed3 => {"jsonrpc":"2.0","id":1,"result":"0x7452b8cf259aebea6b6fce1ef550189572f750a1db2692a67379ff1d00c0b69e"} | pending 1 7452b8cf | queued 0 +call hex-upper eth_sendRawTransaction ["0xF86480830F424082520894BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB8203E88066A0FABEFAA1D8A758990BAB46F72C1C3E850F853B5EF4023E20C3AA41465B18CEDAA00F11B44272A921E1783A033DC1B105C41970EA8951E1BA8198C627A9671BF474"] => {"jsonrpc":"2.0","id":1,"result":"0xdd059b187a76d80c4086ccbff85e7d8ec0a3298b23edca13c5b9b1d96a4318e1"} | pending 2 7452b8cf,dd059b18 | queued 0 +call hex-bare eth_sendRawTransaction ["f86401830f424082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb8203e88065a09c67f24e6ed6afaba2476796a95bae0fd09ed2f3b1171585a666b5b10f4d2254a003ca6f738461a0263cb0afe0d17f9bc60c1e3579cd5c5a9ad915006d40a439b2"] => {"jsonrpc":"2.0","id":1,"result":"0x4ebf9f155d3bd1ae4b8107ea782b56faaa9ec734053030f1151ae670d952a5a7"} | pending 3 4ebf9f15,7452b8cf,dd059b18 | queued 0 +call hex-spaced eth_sendRawTransaction ["0xf8 6402830f424082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb8203e88066a070d5e9703db34959f34ecc5eb33c6202a3eb36b24f7559933d4fea6a79e9c870a04378620c4475f8f0505fe4629a7d6d8a8aca13ab56b8a206ad3b3dc6b800e467"] => {"jsonrpc":"2.0","id":1,"result":"0x479a1ae68b06e1cbc4e625dc7cafdbf1323abed782b6f8a5c2f4f0a0c4fc3c45"} | pending 4 479a1ae6,4ebf9f15,7452b8cf,dd059b18 | queued 0 +call hex-odd-space eth_sendRawTransaction ["0xf8 6403830f424082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb8203e88066a08728be9d808994c56b1f5417da771988e2992f984f11f8b42d261176dee05399a02d4397670b42b53611ef490e3d32724745e1995178845d232fe71179b51bda25"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid data format: invalid hex value."}} | pending 4 479a1ae6,4ebf9f15,7452b8cf,dd059b18 | queued 0 +call hex-capital-x eth_sendRawTransaction ["0Xf86403830f424082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb8203e88066a08728be9d808994c56b1f5417da771988e2992f984f11f8b42d261176dee05399a02d4397670b42b53611ef490e3d32724745e1995178845d232fe71179b51bda25"] => {"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid data format: invalid hex value."}} | pending 4 479a1ae6,4ebf9f15,7452b8cf,dd059b18 | queued 0 +tx ghost f86480830f424082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb8203e88066a09309c3a068860ee7c23cd5a4831dc943e779cb3439303953d8af4c37264e5f97a04f810637ed5ecf0432f25885a0ee1d641113d4b1267385d688a83b1165597ccd => {"jsonrpc":"2.0","id":1,"error":{"code":-32010,"message":"the sender account doesn't exist"}} | pending 4 479a1ae6,4ebf9f15,7452b8cf,dd059b18 | queued 0 +tx ghost-free f85f808082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb808065a060c492d8b5e96738751131744bab3b08599297c8338b1afb029880423e794822a068b7613280d22ddeeac52440c8d34738c08d1a607055a23fa5f77063d3f75214 => {"jsonrpc":"2.0","id":1,"error":{"code":-32010,"message":"the sender account doesn't exist"}} | pending 4 479a1ae6,4ebf9f15,7452b8cf,dd059b18 | queued 0 +tx init-code-too-big f9c05280830f4240839896808080b9c001(00*49153)66a0ea7054cb0ddca66e624c27697772b3c95a68622604a9a58831a14860e2e56524a07631a8be9c174c531abd181637a6922c94ded660fb0eb12b4d967178498c281a => {"jsonrpc":"2.0","id":1,"error":{"code":-32010,"message":"transaction's init code size is invalid"}} | pending 4 479a1ae6,4ebf9f15,7452b8cf,dd059b18 | queued 0 +tx init-code-at-limit f9c05180830f42408303db088080b9c0(00*49153)66a05a222d375f4e736f1017826465fb873f37847d112d0d70dba5320c890a11c757a02e6361914f2e4945a448719ecb30cfd7fcfa4fbfb685bcc5b308b9c2b31f839b => {"jsonrpc":"2.0","id":1,"result":"0x297fad43fc93378cf88a76030da3ba6ebdf7b5b64452d20871f84dc6597e74f6"} | pending 5 297fad43,479a1ae6,4ebf9f15,7452b8cf,dd059b18 | queued 0 +tx too-big fa02001e01830f4240830f424094bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb80ba01ffb8(00*131000)65a03b3fd2c6a7e4cfbb7a3261a8c80c13e903544fa366949d9a6921cef1328839bca07545a4faba535a065485d6460f885de603b7bfec3c5d24d8eed062c3245a6cde => {"jsonrpc":"2.0","id":1,"error":{"code":-32010,"message":"transaction's size is higher than defined maximum: 131106 > 131072"}} | pending 5 297fad43,479a1ae6,4ebf9f15,7452b8cf,dd059b18 | queued 0 +tx gas-over-bound f86380830f42408367c28194bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb808066a090ae4ac5ff6d493f62f51d3e8de0b922f902a78f6445e47b1426799ebf2bf571a007f86e7475ddb2f6870abdc337171b3ea0565b8fbae52778bdb8e6d37b4695d7 => {"jsonrpc":"2.0","id":1,"error":{"code":-32010,"message":"transaction's gas limit of 6800001 is higher than the block's gas limit of 6800000"}} | pending 5 297fad43,479a1ae6,4ebf9f15,7452b8cf,dd059b18 | queued 0 +tx gas-at-bound f86380830f42408367c28094bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb808066a00b64b7c79d41ea817c807df232bbbb4bee98e1ba111d3877e6157760a17565d7a05b18024a06b6567c6d915937770c1e78bae0c609585f9f104f200da1e7fdb5a8 => {"jsonrpc":"2.0","id":1,"result":"0x2df552e62b7451ebfbd56a6ec6f2cf1c2d764e287903643c177cdf768182e7a3"} | pending 6 297fad43,2df552e6,479a1ae6,4ebf9f15,7452b8cf,dd059b18 | queued 0 +tx gas-over-cap f86801830f424088100000000000000194bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb808065a052fd89a60eee5d1d7e43a3b5d110967e314c52ce4d96fa9d527008c7d10776afa07bdd69598cb784ff121dae9f0f73f7feee9646be5baefd649beef2aec2b4f189 => {"jsonrpc":"2.0","id":1,"error":{"code":-32010,"message":"transaction's gas limit of 1152921504606846977 is higher than the block's gas limit of 6800000"}} | pending 6 297fad43,2df552e6,479a1ae6,4ebf9f15,7452b8cf,dd059b18 | queued 0 +tx nonce-leading-zero f864820000830f424082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb808065a02662a46702c7bfab8e3c1d4cb6e8bdb8c12693784e58fac2eb3a3ffc96564bd8a040b490af135314f38b34fc4db21acc81cba8cc06474578a668f60ceb81367c09 => {"jsonrpc":"2.0","id":1,"error":{"code":-32010,"message":"transaction nonce has non-canonical encoding"}} | pending 6 297fad43,2df552e6,479a1ae6,4ebf9f15,7452b8cf,dd059b18 | queued 0 +tx nonce-nine-bytes f86b89000000000000000000830f424082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb808066a0339c8aeaa94ec058a7d268a1730806c594d39b417d3da2c4b981c48ba8e82a12a0362fb13612c1e7b5efd218077e35f238d81a7d66abb3998103aee570922d6904 => {"jsonrpc":"2.0","id":1,"error":{"code":-32010,"message":"transaction nonce byte length exceeds maximum: 9 > 8"}} | pending 6 297fad43,2df552e6,479a1ae6,4ebf9f15,7452b8cf,dd059b18 | queued 0 +tx nonce-zero-byte f86200830f424082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb808065a0bc92a184bc4ed26c703fbf9d3aedb4e5183777586513ed99d4b9ca833f5c9b2fa01686d4bc3cf0a57b1f6982280bfcdcc1349bbc352b076c084a88d7b70ca4cdac => {"jsonrpc":"2.0","id":1,"result":"0xb9132e226e78a38fea19611f223e0853fda53eacc265300cb64c248c4f7b32c5"} | pending 7 297fad43,2df552e6,479a1ae6,4ebf9f15,7452b8cf,b9132e22,dd059b18 | queued 0 +tx nonce-too-low f86204830f424082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb808066a00e006ef798b6e6c59c4ac5cae9b5b2103c7e0bc3de306ea38827a024a2bfa62ea0601af81fb56b036591fe12766a4a8dabca9c22aed3c8151a915be977d86fb442 => {"jsonrpc":"2.0","id":1,"error":{"code":-32010,"message":"transaction nonce too low"}} | pending 7 297fad43,2df552e6,479a1ae6,4ebf9f15,7452b8cf,b9132e22,dd059b18 | queued 0 +tx nonce-too-high f86115830f424082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb808066a08a404fe06e60f16604397334976f7e06303db8239f2e0bdfbc31e0c4c50c81cb9fd7a91227b0b324ebfe81c140af88225a44ac58a125be2a318ed00645c5ed27 => {"jsonrpc":"2.0","id":1,"error":{"code":-32010,"message":"transaction nonce too high"}} | pending 7 297fad43,2df552e6,479a1ae6,4ebf9f15,7452b8cf,b9132e22,dd059b18 | queued 0 +tx nonce-last-slot f86214830f424082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb808065a0b913c29685df745ed38d6ba3c88d810dd4d8c645e32a46ead1c49890437398e6a0479297efe817d0fccd1c608b45f67611d95d0f29f2385437f423bbf12b0bf400 => {"jsonrpc":"2.0","id":1,"result":"0x12f3fd37939eb12a570369f2fec06bb69bfe8464abbdd9797d3b91dc153f166e"} | pending 7 297fad43,2df552e6,479a1ae6,4ebf9f15,7452b8cf,b9132e22,dd059b18 | queued 1 12f3fd37 +tx broke f86280830f424082753194bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb808066a0962024d787c1abac93de016b729af386370718844b4eda85f90a735a9100d9afa0158871263a0a82faaf09efb04ad6f6adef72207996e674a74d47fc4197c78fc8 => {"jsonrpc":"2.0","id":1,"error":{"code":-32010,"message":"insufficient funds"}} | pending 7 297fad43,2df552e6,479a1ae6,4ebf9f15,7452b8cf,b9132e22,dd059b18 | queued 1 12f3fd37 +tx price-below-min f86280830f423f82520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb808065a090e22c50332f1f0426dd0afce7224fc05d09591530c8a452cd95471c1bb78193a06a6d1d0a12a2fd3d41e8b7ffaedb3bd9daca6675a0db1e0a7431580a9c314980 => {"jsonrpc":"2.0","id":1,"error":{"code":-32010,"message":"transaction's gas price lower than block's minimum"}} | pending 7 297fad43,2df552e6,479a1ae6,4ebf9f15,7452b8cf,b9132e22,dd059b18 | queued 1 12f3fd37 +tx gas-below-cost f86280830f424082520794bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb808065a0849e83bdc9b60e935190fbb392bd8b814eb87b52a6fa4d6389476813a809040aa04a5124b4ececbce0f586694099b8ae0d5deb6a0f6f5db2d69882bf895858358c => {"jsonrpc":"2.0","id":1,"error":{"code":-32010,"message":"transaction's basic cost is above the gas limit"}} | pending 7 297fad43,2df552e6,479a1ae6,4ebf9f15,7452b8cf,b9132e22,dd059b18 | queued 1 12f3fd37 +tx data-cost f86480830f424082521b94bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb8082010066a0771b59007fab0b2d65e5eab3104eeadf18247b9759d1c4d4e6bb7103628ea867a05780b71a8b92d6ee49cdafd98f89874c26418b46fb7a417715ff0a5ab5397b78 => {"jsonrpc":"2.0","id":1,"error":{"code":-32010,"message":"transaction's basic cost is above the gas limit"}} | pending 7 297fad43,2df552e6,479a1ae6,4ebf9f15,7452b8cf,b9132e22,dd059b18 | queued 1 12f3fd37 +tx data-cost-met f86480830f424082521c94bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb8082010066a0310b7f30f9bfef9d98701dba44ffe76579f9a48d4befac12db294f7d97af553da009389e35b71648fa226825edd7854967bcbcf07092edd1eca9ca186f3a624098 => {"jsonrpc":"2.0","id":1,"result":"0x8b6cfadc13dd92982d99bc22fac82a130fd88757f3c68b0919e992db7538ac62"} | pending 8 297fad43,2df552e6,479a1ae6,4ebf9f15,7452b8cf,8b6cfadc,b9132e22,dd059b18 | queued 1 12f3fd37 +tx create-cost f86e80830f424082d1098080a0010101010101010101010101010101010101010101010101010101010101010165a0c123a1825d59ffa2569d634c5d34a34b2c637a272d8615a3d46746da1bc667c9a04e044fa4289ba1f8ff602571afe9fe21244d1420db525a4e857e46591a89bdde => {"jsonrpc":"2.0","id":1,"error":{"code":-32010,"message":"transaction's basic cost is above the gas limit"}} | pending 8 297fad43,2df552e6,479a1ae6,4ebf9f15,7452b8cf,8b6cfadc,b9132e22,dd059b18 | queued 1 12f3fd37 +tx create-cost-met f86e80830f424082d10a8080a0010101010101010101010101010101010101010101010101010101010101010165a09fbbf934e05c0df4f63d05ddc3305c7782d15d273808d92c6e258b429de7d349a072a58907fc379837b46d457300b3e83e088e4b92c575b5a3c724abb87ea491d5 => {"jsonrpc":"2.0","id":1,"result":"0xd0635b8b2173bb6dbcb05be8079f87ae0c57ae0ec2105c13d80dc130aad04280"} | pending 9 297fad43,2df552e6,479a1ae6,4ebf9f15,7452b8cf,8b6cfadc,b9132e22,d0635b8b,dd059b18 | queued 1 12f3fd37 +tx price-over-cap f863808404c4b40182520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb808066a0d9db6f9fae65dba9f173fb20115931d985a88d7ba1d1268c093673c3f9fe8f49a0640b037cbdc7553bf3fc1f4998b2ae34bf3d4f63c6eb60c31708d8fc926364df => {"jsonrpc":"2.0","id":1,"error":{"code":-32010,"message":"transaction's gas price exceeds cap"}} | pending 9 297fad43,2df552e6,479a1ae6,4ebf9f15,7452b8cf,8b6cfadc,b9132e22,d0635b8b,dd059b18 | queued 1 12f3fd37 +tx price-at-cap f863808404c4b40082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb808066a07b7aa5bea9862477e5445c4898f65ce7e7a8ff6dd646418d9f063ac5aebce9c4a007c89fd205e3bcb0238def5027aa89e188008c92aefff575d354232f6fbba5f1 => {"jsonrpc":"2.0","id":1,"result":"0xe1b5caaf3670fa70cab2a0e6285c4e83da621ba9d548e240290969d6755d34a1"} | pending 10 297fad43,2df552e6,479a1ae6,4ebf9f15,7452b8cf,8b6cfadc,b9132e22,d0635b8b,dd059b18,e1b5caaf | queued 1 12f3fd37 +tx ghost-and-too-big fa02001e80830f4240830f424094bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb80ba01ffb8(00*131000)66a02e42ff434db5c783a04a108a4671b13bf650be57462f8db3bb3e07773c331a9da0535f3e795f40147a4adbca3d0554d8e26c079a47229189b9e67800404f1e3685 => {"jsonrpc":"2.0","id":1,"error":{"code":-32010,"message":"the sender account doesn't exist"}} | pending 10 297fad43,2df552e6,479a1ae6,4ebf9f15,7452b8cf,8b6cfadc,b9132e22,d0635b8b,dd059b18,e1b5caaf | queued 1 12f3fd37 +tx init-and-size fa02006e02830f4240839896808080ba02001c(00*131100)66a0c256dcd523a6cfc2e35b0202ae3063fffdcf06e9d12324c7cd241cfd60065efca02e5195db915503ca092056f23bd161531eccd400d2a399c699aba78d0ad8ea6b => {"jsonrpc":"2.0","id":1,"error":{"code":-32010,"message":"transaction's init code size is invalid"}} | pending 10 297fad43,2df552e6,479a1ae6,4ebf9f15,7452b8cf,8b6cfadc,b9132e22,d0635b8b,dd059b18,e1b5caaf | queued 1 12f3fd37 +tx size-and-gas fa02001e02830f42408398968094bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb80ba01ffb8(00*131000)66a07a6d95e49739f6c04c0e6f5ee09fd475c4c64c343349098845a65b260a8bd567a06935801e10bdd54b22c329162012c2862d9a8c356b6afc5edd5c75766e816ba0 => {"jsonrpc":"2.0","id":1,"error":{"code":-32010,"message":"transaction's size is higher than defined maximum: 131106 > 131072"}} | pending 10 297fad43,2df552e6,479a1ae6,4ebf9f15,7452b8cf,8b6cfadc,b9132e22,d0635b8b,dd059b18,e1b5caaf | queued 1 12f3fd37 +tx gas-and-nonce-form f865820001830f42408367c28194bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb808066a03054f7a1c4b15b07d3c356f268a14b7782bfeec54184e3b8ea94da8a201b2fffa04fcbab9042bebf85040a28f4d3b1de58e8dbcdb3b3797241bfa798a8f74ef269 => {"jsonrpc":"2.0","id":1,"error":{"code":-32010,"message":"transaction's gas limit of 6800001 is higher than the block's gas limit of 6800000"}} | pending 10 297fad43,2df552e6,479a1ae6,4ebf9f15,7452b8cf,8b6cfadc,b9132e22,d0635b8b,dd059b18,e1b5caaf | queued 1 12f3fd37 +tx nonce-form-and-low f864820004830f424082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb808065a03f7ef6d78988a8ff043513244f74ce137c7350847670953c383f8b414b49cf20a06edbde6bd918137354dfbeec457b3237c55ef4375b59a67a51dcf15b590c40d3 => {"jsonrpc":"2.0","id":1,"error":{"code":-32010,"message":"transaction nonce has non-canonical encoding"}} | pending 10 297fad43,2df552e6,479a1ae6,4ebf9f15,7452b8cf,8b6cfadc,b9132e22,d0635b8b,dd059b18,e1b5caaf | queued 1 12f3fd37 +tx low-and-broke f86b048cffffffffffffffffffffffff82520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb808065a05fee6b94a7a60a6fcc9fa91761e2102679be5a51c77ddae4e7a074154ed3894aa06b6fa3750f0e8b9ec46a8c877d5a7fe91c7b56c280da435fcd45f14111b64f28 => {"jsonrpc":"2.0","id":1,"error":{"code":-32010,"message":"transaction nonce too low"}} | pending 10 297fad43,2df552e6,479a1ae6,4ebf9f15,7452b8cf,8b6cfadc,b9132e22,d0635b8b,dd059b18,e1b5caaf | queued 1 12f3fd37 +tx broke-and-cheap f86380830f423f8367c28094bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb808065a019d2eee9bd5ed94a3b17b409a751a1bd782350909838b927483900eb58a8ba79a02885ed3ba5d8e46dbab918cecd3c5d8ffc68e21da7beef42f8e707427d807d91 => {"jsonrpc":"2.0","id":1,"error":{"code":-32010,"message":"insufficient funds"}} | pending 10 297fad43,2df552e6,479a1ae6,4ebf9f15,7452b8cf,8b6cfadc,b9132e22,d0635b8b,dd059b18,e1b5caaf | queued 1 12f3fd37 +tx cheap-and-short f86201830f423f82520794bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb808066a0656a0742acbd8d14c53a2221ad88ba69cd2fb619af52fd073fcd740b27bfad42a0177c7b5d7254f00d068a0ae1977f2832410e9a2115a78227e9da6adfb9b801fc => {"jsonrpc":"2.0","id":1,"error":{"code":-32010,"message":"transaction's gas price lower than block's minimum"}} | pending 10 297fad43,2df552e6,479a1ae6,4ebf9f15,7452b8cf,8b6cfadc,b9132e22,d0635b8b,dd059b18,e1b5caaf | queued 1 12f3fd37 +tx short-and-over-cap f863018404c4b40182520794bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb808065a068779feeb0da5964e12443ce597ad652bab7a855cb1dd713ffa4b2f46fb76afca01d5966b46b529c9ddc1295847508de278f688d5898a815d59f899e2e51beb7ad => {"jsonrpc":"2.0","id":1,"error":{"code":-32010,"message":"transaction's basic cost is above the gas limit"}} | pending 10 297fad43,2df552e6,479a1ae6,4ebf9f15,7452b8cf,8b6cfadc,b9132e22,d0635b8b,dd059b18,e1b5caaf | queued 1 12f3fd37 +tx broke-exact f86280830f424082753094bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb808066a02355916fb5f1be6d6141cda14448b6a487372b2e20bd753caa60f605075a50a3a010eae066ba16e3b216db598cc44608f5a276032c1f6554e8219bfa4a17347d02 => {"jsonrpc":"2.0","id":1,"result":"0x332bb63b687eb4633c4eef3a97e3a60dba56e5364590a60818adc5d1c46b9b26"} | pending 11 297fad43,2df552e6,332bb63b,479a1ae6,4ebf9f15,7452b8cf,8b6cfadc,b9132e22,d0635b8b,dd059b18,e1b5caaf | queued 1 12f3fd37 +tx p0 f86280830f424082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb018066a09f5fc721d8945a9d54b1d8eff59f726454e842abc43b24ae68f5cb6e83910541a00e497a793eaefa344944a2fed65493191ec9395ac58785411078e1f21352c084 => {"jsonrpc":"2.0","id":1,"result":"0x8cc39f5a652ae58ab637b5420bce4cafaf539d7f707ab149919a9ca2f1bd1046"} | pending 12 297fad43,2df552e6,332bb63b,479a1ae6,4ebf9f15,7452b8cf,8b6cfadc,8cc39f5a,b9132e22,d0635b8b,dd059b18,e1b5caaf | queued 1 12f3fd37 +tx p0-again f86280830f424082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb018066a09f5fc721d8945a9d54b1d8eff59f726454e842abc43b24ae68f5cb6e83910541a00e497a793eaefa344944a2fed65493191ec9395ac58785411078e1f21352c084 => {"jsonrpc":"2.0","id":1,"error":{"code":-32010,"message":"pending transaction with same hash already exists"}} | pending 12 297fad43,2df552e6,332bb63b,479a1ae6,4ebf9f15,7452b8cf,8b6cfadc,8cc39f5a,b9132e22,d0635b8b,dd059b18,e1b5caaf | queued 1 12f3fd37 +tx p2 f86202830f424082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb018066a07dfafd9312fecfccd33bebab31abf0e63c98507de5825fd035658d7a3a759bf6a07cec97776a0bb1dc55af97b2742bf442da1bece67b10e06375ea79641348faa2 => {"jsonrpc":"2.0","id":1,"result":"0xcde2479047d37972d890b9cc6e96313f8b798afc54c345404b4b784dac8eabf6"} | pending 12 297fad43,2df552e6,332bb63b,479a1ae6,4ebf9f15,7452b8cf,8b6cfadc,8cc39f5a,b9132e22,d0635b8b,dd059b18,e1b5caaf | queued 2 12f3fd37,cde24790 +tx p2-again f86202830f424082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb018066a07dfafd9312fecfccd33bebab31abf0e63c98507de5825fd035658d7a3a759bf6a07cec97776a0bb1dc55af97b2742bf442da1bece67b10e06375ea79641348faa2 => {"jsonrpc":"2.0","id":1,"error":{"code":-32010,"message":"queued transaction with same hash already exists"}} | pending 12 297fad43,2df552e6,332bb63b,479a1ae6,4ebf9f15,7452b8cf,8b6cfadc,8cc39f5a,b9132e22,d0635b8b,dd059b18,e1b5caaf | queued 2 12f3fd37,cde24790 +tx p2-other f86202830f424082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb028066a08636793f7984b97d6588f113f403a6b618d7e7467a31d579ddb805ed134b43e8a006b9c61578df05a0a52ed4d003220456cfbcaefca13b3cbb190d9ed12cd0b7c9 => {"jsonrpc":"2.0","id":1,"result":"0x5f8cdfb2a36acb51dfa052d049dc22358b91f8250f0ba558fb4113fead0c3bf3"} | pending 12 297fad43,2df552e6,332bb63b,479a1ae6,4ebf9f15,7452b8cf,8b6cfadc,8cc39f5a,b9132e22,d0635b8b,dd059b18,e1b5caaf | queued 2 12f3fd37,5f8cdfb2 +tx p1 f86201830f424082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb018066a0f9e2c9849e784933a42a7dc0bac16672424eb46cdc2d73d280cf964dbb418b50a071d96727a7bd98a770abc914ba0053cd55394a70043a53e4a85cd884fd2690e3 => {"jsonrpc":"2.0","id":1,"result":"0x5b3917268c160ba376e591c2fbb32b311cca89f4585b100ad5cb1c2022d36627"} | pending 14 297fad43,2df552e6,332bb63b,479a1ae6,4ebf9f15,5b391726,5f8cdfb2,7452b8cf,8b6cfadc,8cc39f5a,b9132e22,d0635b8b,dd059b18,e1b5caaf | queued 1 12f3fd37 +tx p1-same-price f86201830f424082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb028066a052641ef99930e798dc30198101fe3cf43b6355874e0d77eeb7b268bc6595874ea0337e57603b6c93a27619160c1dda7b22ddb5586fb30cc679539c54c894d420ab => {"jsonrpc":"2.0","id":1,"error":{"code":-32010,"message":"gas price not enough to bump transaction"}} | pending 14 297fad43,2df552e6,332bb63b,479a1ae6,4ebf9f15,5b391726,5f8cdfb2,7452b8cf,8b6cfadc,8cc39f5a,b9132e22,d0635b8b,dd059b18,e1b5caaf | queued 1 12f3fd37 +tx p1-bump-short f8620183155cbf82520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb028065a0371c2a2613f38a3c893242aec6fa9f284b70966916c44fa7cf97e15b1c10bb90a030aa28da8d7b88f8a696a20fcec9235c0873a7792714e8d1d1c2c5e555151f6d => {"jsonrpc":"2.0","id":1,"error":{"code":-32010,"message":"gas price not enough to bump transaction"}} | pending 14 297fad43,2df552e6,332bb63b,479a1ae6,4ebf9f15,5b391726,5f8cdfb2,7452b8cf,8b6cfadc,8cc39f5a,b9132e22,d0635b8b,dd059b18,e1b5caaf | queued 1 12f3fd37 +tx p1-bump f8620183155cc082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb028065a0e030769fdee9fc9d7843d4ecce4a8a98eb59e06a052f06524ba2c6b3a97600dda0622f1b41c02525dacaba4d70cbf9690078dd08db1879b98306d7997bdac6050b => {"jsonrpc":"2.0","id":1,"result":"0x8f9bb1c81840d1f74c77e26f2068426816b05e0fc27a4780aa90403097236422"} | pending 14 297fad43,2df552e6,332bb63b,479a1ae6,4ebf9f15,5f8cdfb2,7452b8cf,8b6cfadc,8cc39f5a,8f9bb1c8,b9132e22,d0635b8b,dd059b18,e1b5caaf | queued 1 12f3fd37 +tx p0-bump f8628083155cc082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb028065a0bd8483577fb5423326caac3c7d9bde616f16b0e27135d86903d407f19d95bc30a012e39c21229a78237a974a0e0b08d86d6029dd9fb6171d79a5139adb13476ddd => {"jsonrpc":"2.0","id":1,"result":"0x64a6b6bcff964ac2a78d6d9ed98f1d72b51c326fdcec2646e49ba15baebe9cc9"} | pending 14 297fad43,2df552e6,332bb63b,479a1ae6,4ebf9f15,5f8cdfb2,64a6b6bc,7452b8cf,8b6cfadc,8f9bb1c8,b9132e22,d0635b8b,dd059b18,e1b5caaf | queued 1 12f3fd37 +tx p4 f86204830f424082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb018066a04419b4cc0e5df6952619ddecde0884164b197878da8fb1aaa47f8742b5ce7f65a03d94eab00068cf68a5c5ebf31bd46826e7c8f441e479be94e87524f15c36db7e => {"jsonrpc":"2.0","id":1,"result":"0x98c523a9be1444f7ae086c0ab051378289a2c49378f73cb84f98db81bbe60016"} | pending 14 297fad43,2df552e6,332bb63b,479a1ae6,4ebf9f15,5f8cdfb2,64a6b6bc,7452b8cf,8b6cfadc,8f9bb1c8,b9132e22,d0635b8b,dd059b18,e1b5caaf | queued 2 12f3fd37,98c523a9 +tx p3 f86203830f424082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb018065a0d6df26d9553482825c8c2ca3d7c9845ebc70cc4eba6fed21dd3bf75f4af10546a0763d9b1ee194739d2fbe5df8ccf8909170b92b4145ee7010d49d00e386d45e33 => {"jsonrpc":"2.0","id":1,"result":"0xa61ee55c20015a6ba2ed33391adc14dbb39c7d5f68f8dd75856ca53687403c92"} | pending 16 297fad43,2df552e6,332bb63b,479a1ae6,4ebf9f15,5f8cdfb2,64a6b6bc,7452b8cf,8b6cfadc,8f9bb1c8,98c523a9,a61ee55c,b9132e22,d0635b8b,dd059b18,e1b5caaf | queued 1 12f3fd37 +tx q0 f86280830f424082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb808065a0377a3a728f90e474894bd28d6c11e201b50680c5825e7107ddcdb952eaae8e25a016ef25f826198a6ce951fa444e955884f5b8976880e8dbb2bcff0709581b76e1 => {"jsonrpc":"2.0","id":1,"result":"0x7cd9d28c410d8a43d30b0e64a5571e40f9dd1d52b7b675b5e1f70938009c9bbc"} | pending 17 297fad43,2df552e6,332bb63b,479a1ae6,4ebf9f15,5f8cdfb2,64a6b6bc,7452b8cf,7cd9d28c,8b6cfadc,8f9bb1c8,98c523a9,a61ee55c,b9132e22,d0635b8b,dd059b18,e1b5caaf | queued 1 12f3fd37 +tx q1 f86201830f424082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb808066a0c93b6a2a31cdd8f0ba4eeda725a2a685026cacffffef25018e8e8b4c7c56c58fa0104c75ccd321a0b9af33d72f3123d91513842ca3248f9420dbd2d15f9594d357 => {"jsonrpc":"2.0","id":1,"result":"0xe486c44b56308749843eee4e4e6d041a7a940fe2a4051b2cdffe4658e1e73393"} | pending 18 297fad43,2df552e6,332bb63b,479a1ae6,4ebf9f15,5f8cdfb2,64a6b6bc,7452b8cf,7cd9d28c,8b6cfadc,8f9bb1c8,98c523a9,a61ee55c,b9132e22,d0635b8b,dd059b18,e1b5caaf,e486c44b | queued 1 12f3fd37 +tx q2 f86202830f424082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb808065a0e395971810533000e72db8f1ad87f3299079c3eff6a224e0b91837b9d5164fe1a0275812bc759d87e704bb6150605ba12e82746c9d973beb98da9f41444c0b2182 => {"jsonrpc":"2.0","id":1,"error":{"code":-32010,"message":"insufficient funds to pay for pending and new transactions"}} | pending 18 297fad43,2df552e6,332bb63b,479a1ae6,4ebf9f15,5f8cdfb2,64a6b6bc,7452b8cf,7cd9d28c,8b6cfadc,8f9bb1c8,98c523a9,a61ee55c,b9132e22,d0635b8b,dd059b18,e1b5caaf,e486c44b | queued 1 12f3fd37 +tx q3 f86203830f424082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb808066a0eddf33c8c113f8d07788de789d5b88e7f9e9277048223d68d4e27c16f2397d1ca0645471a3daa7ed0b9e5b627d5a1efdab375563442a41a23c5974163880415621 => {"jsonrpc":"2.0","id":1,"result":"0x27cf1e78f189281e6fc6859794c79696b41a49b0189e8820368545bf9997f251"} | pending 18 297fad43,2df552e6,332bb63b,479a1ae6,4ebf9f15,5f8cdfb2,64a6b6bc,7452b8cf,7cd9d28c,8b6cfadc,8f9bb1c8,98c523a9,a61ee55c,b9132e22,d0635b8b,dd059b18,e1b5caaf,e486c44b | queued 2 12f3fd37,27cf1e78 +tx q1-with-value f8670183155cc082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb8504a817c8008066a022bc15bf4c9c28ea665c9d706123fc369d12d663146cfad6a614b0ecec279ed5a0757af68aacce97fa21f1b3c76cee35483ba6d27aa6327269496183d5cef8765a => {"jsonrpc":"2.0","id":1,"error":{"code":-32010,"message":"insufficient funds to pay for pending and new transactions"}} | pending 18 297fad43,2df552e6,332bb63b,479a1ae6,4ebf9f15,5f8cdfb2,64a6b6bc,7452b8cf,7cd9d28c,8b6cfadc,8f9bb1c8,98c523a9,a61ee55c,b9132e22,d0635b8b,dd059b18,e1b5caaf,e486c44b | queued 2 12f3fd37,27cf1e78 +tx r0-value-overdraws f86780830f424082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb8509502f90008066a0e6206c9e3083ca024ea369f13a47494744b434f264e429e6d09f4bd3b1f268aca0567548a5e7c8a522ee5cc8cd851fd60e722b356e2cc1484b776cfa559861e72d => {"jsonrpc":"2.0","id":1,"error":{"code":-32010,"message":"insufficient funds to pay for pending and new transactions"}} | pending 18 297fad43,2df552e6,332bb63b,479a1ae6,4ebf9f15,5f8cdfb2,64a6b6bc,7452b8cf,7cd9d28c,8b6cfadc,8f9bb1c8,98c523a9,a61ee55c,b9132e22,d0635b8b,dd059b18,e1b5caaf,e486c44b | queued 2 12f3fd37,27cf1e78 +tx s1 f86701830f424082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb8506fc23ac008065a0cab4d959284c286bb171343eebdef1cd6dd1c7c90737e2d1d6411aba9493d806a01091499de8b448b92df685092ca92bc7bced4212907decc31b1cb992b975253c => {"jsonrpc":"2.0","id":1,"result":"0x01baa2d926b3d03d6d9bae9b9c8c402fa906a3493bca3ae2423907d25fd5c802"} | pending 18 297fad43,2df552e6,332bb63b,479a1ae6,4ebf9f15,5f8cdfb2,64a6b6bc,7452b8cf,7cd9d28c,8b6cfadc,8f9bb1c8,98c523a9,a61ee55c,b9132e22,d0635b8b,dd059b18,e1b5caaf,e486c44b | queued 3 01baa2d9,12f3fd37,27cf1e78 +tx s0 f86280830f424082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb808066a0548820601a5e7d730850d876042ba17437d53694bbac08c3172fd242656a6a49a0483fc1f40855a0c985b718a71c03157df29be68dcfc1b1e7465fdb520062ce07 => {"jsonrpc":"2.0","id":1,"result":"0x84c95b23729cfaf3931b3ffcaf09ce428d3011d5b0bb81213b54014be5ded99a"} | pending 19 297fad43,2df552e6,332bb63b,479a1ae6,4ebf9f15,5f8cdfb2,64a6b6bc,7452b8cf,7cd9d28c,84c95b23,8b6cfadc,8f9bb1c8,98c523a9,a61ee55c,b9132e22,d0635b8b,dd059b18,e1b5caaf,e486c44b | queued 2 12f3fd37,27cf1e78 +tx t2 f86202830f424082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb808065a0bed2d54d65f582598a126170d4493b716842abe022adb2f3053656d6b5295695a07688126ff356b515ed41667619456e0cccbba514ae99cc2c3581ff931d3b2395 => {"jsonrpc":"2.0","id":1,"result":"0x1d68409d66c1c7a86ffd0d4c13cce05260963aa7fee344ac22aa1335e04a913f"} | pending 19 297fad43,2df552e6,332bb63b,479a1ae6,4ebf9f15,5f8cdfb2,64a6b6bc,7452b8cf,7cd9d28c,84c95b23,8b6cfadc,8f9bb1c8,98c523a9,a61ee55c,b9132e22,d0635b8b,dd059b18,e1b5caaf,e486c44b | queued 3 12f3fd37,1d68409d,27cf1e78 +tx t1 f86201830f424082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb808066a0cffbfeec5b98c7b32ab6eaa021baff5d94bae16c04f6f2083b5139e27ed28aaaa04575f5791611e2c03357f40183d8e6812330d143e6e6538e07d67c9fd660e209 => {"jsonrpc":"2.0","id":1,"result":"0x4cde86919ff61383a56efc925080b9b284793a30b0a509c2f3cb425c649f3854"} | pending 19 297fad43,2df552e6,332bb63b,479a1ae6,4ebf9f15,5f8cdfb2,64a6b6bc,7452b8cf,7cd9d28c,84c95b23,8b6cfadc,8f9bb1c8,98c523a9,a61ee55c,b9132e22,d0635b8b,dd059b18,e1b5caaf,e486c44b | queued 4 12f3fd37,1d68409d,27cf1e78,4cde8691 +tx t0 f86280830f424082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb808065a07f699d70ee8078171ce35438ca46304d774e42c356939785deb48ce82d1f5c93a02ac03543bf615d2b8b44dc12a5c77043e669559a0439e1df36ddb9c6eabf1f9a => {"jsonrpc":"2.0","id":1,"result":"0xbada0b8a820a5f57b4b6d91441be93449e6f3ebbaef55ce56e62939a16ba96d5"} | pending 22 1d68409d,297fad43,2df552e6,332bb63b,479a1ae6,4cde8691,4ebf9f15,5f8cdfb2,64a6b6bc,7452b8cf,7cd9d28c,84c95b23,8b6cfadc,8f9bb1c8,98c523a9,a61ee55c,b9132e22,bada0b8a,d0635b8b,dd059b18,e1b5caaf,e486c44b | queued 2 12f3fd37,27cf1e78 +setup seed personal_newAccountWithSeed ["w"] => {"jsonrpc":"2.0","id":1,"result":"0x5250db9c00566a33380b73aa86a0636cca2d404e"} | pending 22 1d68409d,297fad43,2df552e6,332bb63b,479a1ae6,4cde8691,4ebf9f15,5f8cdfb2,64a6b6bc,7452b8cf,7cd9d28c,84c95b23,8b6cfadc,8f9bb1c8,98c523a9,a61ee55c,b9132e22,bada0b8a,d0635b8b,dd059b18,e1b5caaf,e486c44b | queued 2 12f3fd37,27cf1e78 +call send-cheap eth_sendTransaction [{"from":"0x5250db9c00566a33380b73aa86a0636cca2d404e","to":"0xbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb","gas":"0x5208","gasPrice":"0x1"}] => {"jsonrpc":"2.0","id":1,"error":{"code":-32010,"message":"transaction's gas price lower than block's minimum"}} | pending 22 1d68409d,297fad43,2df552e6,332bb63b,479a1ae6,4cde8691,4ebf9f15,5f8cdfb2,64a6b6bc,7452b8cf,7cd9d28c,84c95b23,8b6cfadc,8f9bb1c8,98c523a9,a61ee55c,b9132e22,bada0b8a,d0635b8b,dd059b18,e1b5caaf,e486c44b | queued 2 12f3fd37,27cf1e78 +call send eth_sendTransaction [{"from":"0x5250db9c00566a33380b73aa86a0636cca2d404e","to":"0xbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb","gas":"0x5208","gasPrice":"0xf4240"}] => {"jsonrpc":"2.0","id":1,"result":"0x5069297bafe44efb049efa60991a479b4d2642b8300791a3b816eca544ff7086"} | pending 23 1d68409d,297fad43,2df552e6,332bb63b,479a1ae6,4cde8691,4ebf9f15,5069297b,5f8cdfb2,64a6b6bc,7452b8cf,7cd9d28c,84c95b23,8b6cfadc,8f9bb1c8,98c523a9,a61ee55c,b9132e22,bada0b8a,d0635b8b,dd059b18,e1b5caaf,e486c44b | queued 2 12f3fd37,27cf1e78 +call send-again eth_sendTransaction [{"from":"0x5250db9c00566a33380b73aa86a0636cca2d404e","to":"0xbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb","gas":"0x5208","gasPrice":"0xf4240","nonce":"0x0"}] => {"jsonrpc":"2.0","id":1,"error":{"code":-32010,"message":"pending transaction with same hash already exists"}} | pending 23 1d68409d,297fad43,2df552e6,332bb63b,479a1ae6,4cde8691,4ebf9f15,5069297b,5f8cdfb2,64a6b6bc,7452b8cf,7cd9d28c,84c95b23,8b6cfadc,8f9bb1c8,98c523a9,a61ee55c,b9132e22,bada0b8a,d0635b8b,dd059b18,e1b5caaf,e486c44b | queued 2 12f3fd37,27cf1e78 +scenario genesis +head 0 10000000 0 +account cow cd2a3d9f938e13cd947ec05abc7fe734df8dd826 0 1000000000000000000000000000000 +account cow1 7986b3df570230288501eea3d890bd66948c9b79 0 1000000000000000000000000000000 +tx gas-over-block f86080808398968194bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb808065a02732f8399bdaa2cb10173750d6e6a0d8f0983d5ee06d901fa4d1e52a6b269c03a0045eb6dbefcb3096f49540b3817adefadb382f69b4f27478e242186ad2eb9aca => {"jsonrpc":"2.0","id":1,"error":{"code":-32010,"message":"transaction's gas limit of 10000001 is higher than the block's gas limit of 10000000"}} | pending 0 | queued 0 +tx gas-at-block f86080808398968094bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb808066a0413b7cd9d1249c87696a33c5d7d22a5b3f8113246d76cd8f9896c16aea3e478fa043519af9e1a121000b82c1086c9f0b3764928109babf8ec00296ba3c4afc985e => {"jsonrpc":"2.0","id":1,"result":"0x5c50ab7e4311cee4e9e90e34ee6bd305a2f7c1b884bb38b970e211152611ae4a"} | pending 1 5c50ab7e | queued 0 +tx price-zero-byte f85f800082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb808065a030d28ccf33993eb3a8b1395100a5ab5268a269e832b58df8720da5228cee9efda078e1ef3db53e17bc4709fbd63f8f179c235417f898142ec05fb83ad63f92b833 => {"jsonrpc":"2.0","id":1,"result":"0xc0f6edd3b946215470d88c41d56b92832f4e1cdd456cfc0a038bd749cf5f979f"} | pending 2 5c50ab7e,c0f6edd3 | queued 0 +tx huge-price f8730194ffffffffffffffffffffffffffffffffffffffff82520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb808065a01b42e02254b739bf5649ea08cb36c2493fd2c4f6a08c5d902c0afa479067dc36a068c835e1cb886e834275bfc4c2f1bcb171ff91cf3127c72b5e4887a533b43702 => {"jsonrpc":"2.0","id":1,"error":{"code":-32010,"message":"insufficient funds"}} | pending 2 5c50ab7e,c0f6edd3 | queued 0 +tx value-zero-byte f85f018082520894bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb008066a0d644ffe95e3f69c058db0ae08e7d8b97971a18541801f4f84f31fc347dbde1fba043918a7c648af0624700bd29b942004af6dc314f63468804d13180c9bcd66272 => {"jsonrpc":"2.0","id":1,"result":"0x9f06a859881ff42feff4b37ee6130a5147b66140956d3a091fdc99aa021304fe"} | pending 3 5c50ab7e,9f06a859,c0f6edd3 | queued 0 diff --git a/crates/sync/src/txpool.rs b/crates/sync/src/txpool.rs index b952c7d..bbb744c 100644 --- a/crates/sync/src/txpool.rs +++ b/crates/sync/src/txpool.rs @@ -11,6 +11,9 @@ use tracing::debug; const TRANSACTION_GAS_CAP: u64 = 1u64 << 60; const MAX_TX_SIZE: usize = 128 * 1024; +/// rskj `Constants.getMaxInitCodeSize()`: twice the largest contract, the most +/// init code a creation may carry once RSKIP438 is active. +const MAX_INIT_CODE_SIZE: usize = 2 * 0x6000; /// Ceiling on the total number of transactions held across every account. /// @@ -127,6 +130,31 @@ impl ChainRules { } Some(header.minimum_gas_price.saturating_mul(U256::from(TX_GAS_PRICE_CAP_MULTIPLIER))) } + + /// rskj `Transaction.transactionCost` of a transaction that isn't free, + /// at block `number`: 21,000, or 53,000 for a creation, plus 4 for every + /// zero byte of its data and 68 for every other, 16 from RSKIP400; and + /// from RSKIP438 a creation pays 2 more for every 32 bytes of its init + /// code, the last ones counted whole (`InitcodeCostCalculator`). + fn transaction_cost(&self, tx: &Transaction, number: u64) -> u64 { + let creates = tx.to.is_empty(); + let nonzero = if self.hardfork_cfg.has_reduced_calldata_cost(number) { + 16 + } else { + 68 + }; + let init_code = if creates && self.hardfork_cfg.has_initcode_metering(number) { + 2 * tx.input.len().div_ceil(32) as u64 + } else { + 0 + }; + let data: u64 = tx + .input + .iter() + .map(|byte| if *byte == 0 { 4 } else { nonzero }) + .sum(); + (if creates { 53_000 } else { 21_000 }) + init_code + data + } } /// rskj `TxGasPriceCap.FOR_TRANSACTION`. @@ -163,15 +191,30 @@ fn raw_nonce_bytes(raw: &[u8]) -> Option<&[u8]> { #[derive(Debug)] pub enum PoolError { - AlreadyKnown, + /// rskj `internalAddTransaction`: the pending transactions hold one of + /// this hash. + AlreadyPending, + /// The same, of the queued ones. + AlreadyQueued, NonceTooLow, NonceTooHigh, InsufficientFunds, - GasLimitExceeded, + /// rskj `TxValidatorGasLimitValidator`: the transaction's gas limit, and + /// the limit it had to fit, which is the block's or, under RSKIP144, the + /// larger sublist's. rskj's message calls it the block's either way. + GasLimitExceeded { + gas_limit: U256, + bound: u64, + }, GasPriceTooLow, IntrinsicGasTooHigh, ReplacementGasPriceTooLow, - TxTooLarge, + /// rskj `TxPendingValidator.isValid`: the transaction's size, and the + /// most it may be. + TxTooLarge { + size: usize, + max: usize, + }, PoolFull, InvalidSignature(String), InsufficientFundsForPendingAndNew, @@ -181,23 +224,35 @@ pub enum PoolError { /// rskj `TxValidatorMaximumGasPriceValidator`: over 80 times the block /// minimum once RSKIP252 is active. GasPriceAboveCap, - /// rskj `TxValidatorNonceEncodingValidator`: a nonce with leading zeros - /// or more than eight bytes. + /// rskj `TxValidatorNonceEncodingValidator`: a nonce with leading zeros. NonCanonicalNonce, + /// The same validator: a nonce of more than eight bytes, this many. + NonceTooLong(usize), + /// rskj `TxPendingValidator.isValid`: the state holds no account for the + /// sender of a transaction that costs something. + SenderMissing, + /// The same: a creation whose init code is past RSKIP438's limit. + InitCodeTooLarge, } impl fmt::Display for PoolError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { - Self::AlreadyKnown => write!(f, "transaction with same hash already exists"), + Self::AlreadyPending => write!(f, "pending transaction with same hash already exists"), + Self::AlreadyQueued => write!(f, "queued transaction with same hash already exists"), Self::NonceTooLow => write!(f, "transaction nonce too low"), Self::NonceTooHigh => write!(f, "transaction nonce too high"), Self::InsufficientFunds => write!(f, "insufficient funds"), - Self::GasLimitExceeded => write!(f, "transaction's gas limit exceeds block gas limit"), + Self::GasLimitExceeded { gas_limit, bound } => write!( + f, + "transaction's gas limit of {gas_limit} is higher than the block's gas limit of {bound}" + ), Self::GasPriceTooLow => write!(f, "transaction's gas price lower than block's minimum"), Self::IntrinsicGasTooHigh => write!(f, "transaction's basic cost is above the gas limit"), Self::ReplacementGasPriceTooLow => write!(f, "gas price not enough to bump transaction"), - Self::TxTooLarge => write!(f, "transaction's size is higher than defined maximum"), + Self::TxTooLarge { size, max } => { + write!(f, "transaction's size is higher than defined maximum: {size} > {max}") + } Self::PoolFull => write!(f, "transaction pool is full"), Self::InvalidSignature(e) => write!(f, "invalid signature: {e}"), Self::InsufficientFundsForPendingAndNew => { @@ -208,6 +263,11 @@ impl fmt::Display for PoolError { Self::StoreError(e) => write!(f, "store error: {e}"), Self::GasPriceAboveCap => write!(f, "transaction's gas price exceeds cap"), Self::NonCanonicalNonce => write!(f, "transaction nonce has non-canonical encoding"), + Self::NonceTooLong(length) => { + write!(f, "transaction nonce byte length exceeds maximum: {length} > 8") + } + Self::SenderMissing => write!(f, "the sender account doesn't exist"), + Self::InitCodeTooLarge => write!(f, "transaction's init code size is invalid"), } } } @@ -219,15 +279,15 @@ impl PoolError { /// `&'static str` so the counters cannot grow without bound. pub fn reason(&self) -> &'static str { match self { - Self::AlreadyKnown => "already-known", + Self::AlreadyPending | Self::AlreadyQueued => "already-known", Self::NonceTooLow => "nonce-too-low", Self::NonceTooHigh => "nonce-too-high", Self::InsufficientFunds => "insufficient-funds", - Self::GasLimitExceeded => "gas-limit-exceeded", + Self::GasLimitExceeded { .. } => "gas-limit-exceeded", Self::GasPriceTooLow => "gas-price-too-low", Self::IntrinsicGasTooHigh => "intrinsic-gas-too-high", Self::ReplacementGasPriceTooLow => "replacement-underpriced", - Self::TxTooLarge => "too-large", + Self::TxTooLarge { .. } => "too-large", Self::PoolFull => "pool-full", Self::InvalidSignature(_) => "invalid-signature", Self::InsufficientFundsForPendingAndNew => "insufficient-funds-pending", @@ -235,7 +295,9 @@ impl PoolError { Self::RlpDecode(_) => "rlp-decode", Self::StoreError(_) => "store-error", Self::GasPriceAboveCap => "gas-price-above-cap", - Self::NonCanonicalNonce => "non-canonical-nonce", + Self::NonCanonicalNonce | Self::NonceTooLong(_) => "non-canonical-nonce", + Self::SenderMissing => "sender-missing", + Self::InitCodeTooLarge => "init-code-too-large", } } } @@ -253,6 +315,14 @@ pub struct PooledTx { /// Transactions grouped by sender, each sender's ordered by nonce. pub type PoolGroups = Vec<(Address, Vec<(u64, Transaction)>)>; +/// Where the pool put a transaction it took: rskj's +/// `TransactionPoolAddResult.okPendingTransaction` and `okQueuedTransaction`. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum Admitted { + Pending, + Queued, +} + struct PoolInner { pending: HashMap>, queued: HashMap>, @@ -267,6 +337,32 @@ impl PoolInner { by_hash: HashMap::new(), } } + + /// rskj `internalAddTransaction`'s first two questions: refused where + /// the pending transactions hold one of this hash, or the queued ones. + fn refuse_held(&self, hash: &B256) -> Result<(), PoolError> { + let Some(sender) = self.by_hash.get(hash) else { + return Ok(()); + }; + let pending = self + .pending + .get(sender) + .is_some_and(|by_nonce| by_nonce.values().any(|held| held.hash == *hash)); + Err(if pending { + PoolError::AlreadyPending + } else { + PoolError::AlreadyQueued + }) + } + + /// rskj `PendingState.getNonce`: the nonce after the sender's last + /// pending transaction, or the state's where that is further on. + fn next_nonce(&self, sender: &Address, state_nonce: u64) -> u64 { + self.pending + .get(sender) + .and_then(|by_nonce| by_nonce.keys().last()) + .map_or(state_nonce, |last| last.saturating_add(1).max(state_nonce)) + } } pub struct TransactionPool { @@ -493,6 +589,9 @@ impl TransactionPool { result } + /// rskj `TransactionPoolImpl.addTransaction`: the transaction itself + /// ([`Self::admit`]) and then, where it went pending, the queued ones + /// that were waiting for it ([`Self::promote_successors`]). fn add_transaction_inner(&self, raw: &[u8]) -> Result { let tx = Transaction::decode(&mut &raw[..]) .map_err(|e| PoolError::RlpDecode(format!("{e}")))?; @@ -501,14 +600,15 @@ impl TransactionPool { // rebuilds rather than of the bytes as sent (`Transaction::from_rskj`), // and measures it by that encoding too (`getSize`). let hash = tx.tx_hash(); - let encoded = tx.rlp_for_trie(); - let encoded_size = encoded.len(); + let encoded_size = tx.rlp_for_trie().len(); - // rskj `TxValidatorNonceEncodingValidator`, on the nonce's bytes as - // rskj holds them (`getNonce`), which that encoding keeps. - if !raw_nonce_bytes(&encoded).is_some_and(nonce_bytes_are_canonical) { - return Err(PoolError::NonCanonicalNonce); - } + // rskj asks whether it holds the hash before anything else, so a + // transaction sent twice is told that, whatever has become of its + // sender since. + self.inner + .read() + .unwrap_or_else(PoisonError::into_inner) + .refuse_held(&hash)?; // A transaction offered twice -- re-broadcast, or seen by both a peer // and the RPC -- is recovered once. The write goes to the pool tier, @@ -534,100 +634,132 @@ impl TransactionPool { }; let head_header = self.head_header()?; - let (state_nonce, balance) = self.account_state(&head_header, &sender); - - self.validate_tx(&tx, &sender, encoded_size, &head_header, state_nonce, balance)?; - - let current_block = head_header.number; + let account = self.account_state(&head_header, &sender); + let nonce = tx.nonce; let pooled = PooledTx { tx: tx.clone(), hash, sender, - added_block: current_block, + added_block: head_header.number, added_time: Instant::now(), encoded_size, }; let mut inner = self.inner.write().unwrap_or_else(PoisonError::into_inner); + if self.admit(&mut inner, pooled, &head_header, account)? == Admitted::Pending { + self.promote_successors(&mut inner, sender, nonce, &head_header, account); + } - if inner.by_hash.contains_key(&hash) { - return Err(PoolError::AlreadyKnown); + debug!(tx_hash = %hash, sender = %sender, nonce, "Transaction added to pool"); + drop(inner); + let listener = self + .on_added + .read() + .unwrap_or_else(PoisonError::into_inner) + .clone(); + if let Some(listener) = listener { + listener(&tx); } + Ok(hash) + } - if inner.by_hash.len() >= self.config.max_pool_transactions { - // Try to make room by dropping the furthest-out queued transaction. - // Queued entries have future nonces, so they are not executable yet - // and are the class an attacker can most cheaply pile up. Pending - // transactions are never evicted here. - if !Self::evict_one_queued(&mut inner) { - return Err(PoolError::PoolFull); + /// rskj `TransactionPoolImpl.internalAddTransaction`, in its order: the + /// hash, the validation against the head's state, the pending + /// transaction it would replace, and then where it goes. A nonce past the + /// sender's next one is queued, in the place of any queued one of that + /// nonce, and nothing more is asked of it. Any other is pending, once the + /// rate limiter takes it and the sender can pay for it along with their + /// other pending transactions. + fn admit( + &self, + inner: &mut PoolInner, + pooled: PooledTx, + header: &Header, + account: Option<(u64, U256)>, + ) -> Result { + inner.refuse_held(&pooled.hash)?; + self.validate_tx( + &pooled.tx, + &pooled.sender, + pooled.encoded_size, + header, + account, + )?; + let (state_nonce, balance) = account.unwrap_or_default(); + let (hash, sender, nonce) = (pooled.hash, pooled.sender, pooled.tx.nonce); + + // rskj `isBumpingGasPriceForSameNonceTx`: a higher price, and no less + // than the old one raised by the bump, which rskj reckons in whole + // numbers (`Coin.multiply`). Where the bump rounds to nothing, the + // same price again doesn't replace. The rate limiter prices a + // replacement by how much it overbids the transaction it displaces, + // so it needs the displaced one. + let replaced = inner + .pending + .get(&sender) + .and_then(|by_nonce| by_nonce.get(&nonce)) + .map(|old| { + ( + old.hash, + old.tx.gas_price, + tx_facts(&old.tx, old.encoded_size), + ) + }); + if let Some((_, old_price, _)) = &replaced { + let new_price = pooled.tx.gas_price; + let bumped = alloy_primitives::U512::from(*old_price) + * alloy_primitives::U512::from(100 + self.config.gas_price_bump) + / alloy_primitives::U512::from(100); + if *old_price >= new_price || bumped > alloy_primitives::U512::from(new_price) { + return Err(PoolError::ReplacementGasPriceTooLow); } } - let goes_to_pending = if tx.nonce == state_nonce { - true - } else if let Some(pending_map) = inner.pending.get(&sender) { - pending_map - .keys() - .last() - .is_some_and(|&last| tx.nonce == last + 1) + let queues = nonce > inner.next_nonce(&sender, state_nonce); + let displaced = if queues { + inner + .queued + .get(&sender) + .and_then(|by_nonce| by_nonce.get(&nonce)) + .map(|old| old.hash) } else { - false + replaced.as_ref().map(|(old_hash, ..)| *old_hash) }; - // Check for replacement (same nonce, different hash) - let target_map = if goes_to_pending { - &inner.pending - } else { - &inner.queued - }; - let mut old_hash_to_remove: Option = None; - // The rate limiter prices a replacement by how much it overbids the - // transaction it displaces, so it needs the displaced one. - let mut replaced_facts: Option = None; - if let Some(nonce_map) = target_map.get(&sender) { - if let Some(existing) = nonce_map.get(&tx.nonce) { - if existing.hash == hash { - return Err(PoolError::AlreadyKnown); - } - // rskj `isBumpingGasPriceForSameNonceTx`: a higher price, and - // no less than the old one raised by the bump, which rskj - // reckons in whole numbers (`Coin.multiply`). Where the bump - // rounds to nothing, the same price again doesn't replace. - let old_price = existing.tx.gas_price; - let bumped = alloy_primitives::U512::from(old_price) - * alloy_primitives::U512::from(100 + self.config.gas_price_bump) - / alloy_primitives::U512::from(100); - let pays_more = old_price < tx.gas_price - && bumped <= alloy_primitives::U512::from(tx.gas_price); - if !pays_more { - return Err(PoolError::ReplacementGasPriceTooLow); - } - old_hash_to_remove = Some(existing.hash); - replaced_facts = Some(tx_facts(&existing.tx, existing.encoded_size)); - } + // This pool's own ceiling, which rskj's has none of. A transaction + // that takes another's place needs no room; any other has the + // furthest-out queued transaction make way for it, once everything + // else is settled. Queued entries have future nonces, so they are not + // executable yet and are the class an attacker can most cheaply pile + // up. Pending transactions are never evicted. + let needs_room = + displaced.is_none() && inner.by_hash.len() >= self.config.max_pool_transactions; + if needs_room && inner.queued.is_empty() { + return Err(PoolError::PoolFull); } - // Aggregate balance check for pending txs - if goes_to_pending { - // rskj `getTxBaseCost`: the value, plus the gas only when the + if !queues { + self.take_quota(&pooled, replaced.as_ref().map(|(.., facts)| facts), header)?; + + // rskj `senderCanPayPendingTransactionsAndNewTx`, after the rate + // limiter. `getTxBaseCost`: the value, plus the gas only when the // transaction costs anything -- a free Bridge transaction carries // its value and nothing else. let base_cost = |t: &Transaction| -> Option { let free = self .chain_rules .as_ref() - .is_some_and(|r| r.is_free_tx(t, &sender, current_block)); + .is_some_and(|r| r.is_free_tx(t, &sender, header.number)); if free { Some(t.value) } else { tx_cost(t) } }; - let mut total_cost = base_cost(&tx); + let mut total_cost = base_cost(&pooled.tx); if let Some(pending_map) = inner.pending.get(&sender) { - for (nonce, ptx) in pending_map { - if *nonce != tx.nonce { + for (other, ptx) in pending_map { + if *other != nonce { total_cost = total_cost .zip(base_cost(&ptx.tx)) .and_then(|(total, cost)| total.checked_add(cost)); @@ -641,115 +773,187 @@ impl TransactionPool { } } - // rskj `TransactionPoolImpl.internalAddTransaction`: the quota check is - // the LAST gate, after every cheaper validation and after the - // replacement has been resolved. Running it earlier would let an - // invalid transaction consume an honest account's virtual gas. - { - let accounts = TrieAccountView::new(head_header.state_root, &*self.trie_store); - // rskj supplies the average only when the fee market is working -- - // `isFeeMarketWorking()` false takes its - // `createSkippingGasPriceFactor` branch, which pins the - // low-gas-price factor to 1. On a chain with spare capacity that - // is the usual path on rskj too; the difference is that it - // recovers when blocks fill up, which this now does. - let avg_gas_price = self - .gas_price - .as_ref() - .and_then(|t| t.is_fee_market_working().then(|| t.gas_price())); - let ctx = crate::quota::QuotaContext::of(&head_header, avg_gas_price); - let receiver = (tx.to.len() == 20).then(|| Address::from_slice(tx.to.as_ref())); - let accepted = self - .quota - .write() - .unwrap_or_else(PoisonError::into_inner) - .accept_tx( - sender, - receiver, - &tx_facts(&tx, encoded_size), - replaced_facts.as_ref(), - &ctx, - &accounts, - Instant::now(), - ); - if !accepted { - debug!( - tx_hash = %hash, sender = %sender, nonce = tx.nonce, - "Transaction refused: account exceeds virtual gas quota" - ); - *self - .stats - .write() - .unwrap_or_else(PoisonError::into_inner) - .quota_by_sender - .entry(sender) - .or_insert(0) += 1; - return Err(PoolError::AccountExceedsQuota); - } + // All checks passed: the pool changes. + if needs_room { + Self::evict_one_queued(inner); } - - // All checks passed — mutate the pool - if let Some(old_hash) = old_hash_to_remove { + if let Some(old_hash) = displaced { inner.by_hash.remove(&old_hash); } - - let sender_map = if goes_to_pending { - inner.pending.entry(sender).or_default() + inner.by_hash.insert(hash, sender); + let side = if queues { + &mut inner.queued } else { - inner.queued.entry(sender).or_default() + &mut inner.pending }; - sender_map.insert(tx.nonce, pooled); - inner.by_hash.insert(hash, sender); + side.entry(sender).or_default().insert(nonce, pooled); + Ok(if queues { + Admitted::Queued + } else { + Admitted::Pending + }) + } - if goes_to_pending { - self.promote_queued(&mut inner, sender, state_nonce); + /// rskj `TxQuotaChecker.acceptTx`, where `internalAddTransaction` asks + /// it: of a transaction on its way to pending, after every validation + /// and after the replacement has been resolved, and before the sender's + /// funds are counted. A queued transaction is not asked about until it + /// is promoted, and an invalid one never, so neither consumes an honest + /// account's virtual gas. + fn take_quota( + &self, + pooled: &PooledTx, + replaced: Option<&crate::quota::TxFacts>, + header: &Header, + ) -> Result<(), PoolError> { + let accounts = TrieAccountView::new(header.state_root, &*self.trie_store); + // rskj supplies the average only when the fee market is working -- + // `isFeeMarketWorking()` false takes its + // `createSkippingGasPriceFactor` branch, which pins the + // low-gas-price factor to 1. On a chain with spare capacity that + // is the usual path on rskj too; the difference is that it + // recovers when blocks fill up, which this now does. + let avg_gas_price = self + .gas_price + .as_ref() + .and_then(|t| t.is_fee_market_working().then(|| t.gas_price())); + let ctx = crate::quota::QuotaContext::of(header, avg_gas_price); + let tx = &pooled.tx; + let receiver = (tx.to.len() == 20).then(|| Address::from_slice(tx.to.as_ref())); + let accepted = self + .quota + .write() + .unwrap_or_else(PoisonError::into_inner) + .accept_tx( + pooled.sender, + receiver, + &tx_facts(tx, pooled.encoded_size), + replaced, + &ctx, + &accounts, + Instant::now(), + ); + if accepted { + return Ok(()); } - - debug!(tx_hash = %hash, sender = %sender, nonce = tx.nonce, "Transaction added to pool"); - drop(inner); - let listener = self - .on_added - .read() + debug!( + tx_hash = %pooled.hash, sender = %pooled.sender, nonce = tx.nonce, + "Transaction refused: account exceeds virtual gas quota" + ); + *self + .stats + .write() .unwrap_or_else(PoisonError::into_inner) - .clone(); - if let Some(listener) = listener { - listener(&tx); + .quota_by_sender + .entry(pooled.sender) + .or_insert(0) += 1; + Err(PoolError::AccountExceedsQuota) + } + + /// rskj `addSuccessors`: the queued transaction next in line after one + /// that went pending is taken out of the queue and offered again, as a + /// new one would be, and so on until one doesn't go pending. One that is + /// refused then is gone. + fn promote_successors( + &self, + inner: &mut PoolInner, + sender: Address, + after: u64, + header: &Header, + account: Option<(u64, U256)>, + ) { + let mut nonce = after; + loop { + let Some(next) = nonce.checked_add(1) else { + return; + }; + nonce = next; + let Some(queue) = inner.queued.get_mut(&sender) else { + return; + }; + let Some(mut successor) = queue.remove(&nonce) else { + return; + }; + if queue.is_empty() { + inner.queued.remove(&sender); + } + inner.by_hash.remove(&successor.hash); + // rskj notes the block and the time again, as for a new one. + successor.added_block = header.number; + successor.added_time = Instant::now(); + if !matches!( + self.admit(inner, successor, header, account), + Ok(Admitted::Pending) + ) { + return; + } } - Ok(hash) } - /// rskj `TxPendingValidator.isValid`, step by step and in its order. + /// rskj `TxPendingValidator.isValid`, step by step and in its order, so + /// that of two faults the one rskj names is the one named here. + /// `account` is the sender's nonce and balance in the head's state, or + /// none where the state holds no such account. fn validate_tx( &self, tx: &Transaction, sender: &Address, encoded_size: usize, header: &Header, - state_nonce: u64, - balance: U256, + account: Option<(u64, U256)>, ) -> Result<(), PoolError> { let rules = self.chain_rules.as_ref(); // rskj `basicTxCost == 0`: a free Bridge transaction. let is_free = rules.is_some_and(|r| r.is_free_tx(tx, sender, header.number)); - // 1. Not REMASC (`TxValidatorNotRemascTxValidator`): rskj refuses a - // `RemascTransaction`, which only a block's last transaction is, - // so never one offered alone. - - // 2. Size limit (`TX_MAX_SIZE`) + // Before the steps: an account to pay for it, unless it costs + // nothing; RSKIP438's limit on a creation's init code; and the size + // (`TX_MAX_SIZE`). + if account.is_none() && !is_free { + return Err(PoolError::SenderMissing); + } + let (state_nonce, balance) = account.unwrap_or_default(); + let metered = rules.is_some_and(|r| r.hardfork_cfg.has_initcode_metering(header.number)); + if metered && tx.to.is_empty() && tx.input.len() > MAX_INIT_CODE_SIZE { + return Err(PoolError::InitCodeTooLarge); + } if encoded_size > self.config.max_tx_size { - return Err(PoolError::TxTooLarge); + return Err(PoolError::TxTooLarge { + size: encoded_size, + max: self.config.max_tx_size, + }); } + // 1. Not null (`TxNotNullValidator`), and 2. not REMASC + // (`TxValidatorNotRemascTxValidator`): rskj refuses a + // `RemascTransaction`, which only a block's last transaction is, + // so never one offered alone. + // 3. Gas limit (`TxValidatorGasLimitValidator`), against the block's // limit or, under RSKIP144, the larger sublist's. - let gas_limit = tx.gas_limit.saturating_to::(); let bound = rules.map_or(header.gas_limit.saturating_to::(), |r| r.gas_limit_bound(header)); - if gas_limit > bound || gas_limit > TRANSACTION_GAS_CAP { - return Err(PoolError::GasLimitExceeded); + if tx.gas_limit > U256::from(bound) || tx.gas_limit > U256::from(TRANSACTION_GAS_CAP) { + return Err(PoolError::GasLimitExceeded { + gas_limit: tx.gas_limit, + bound, + }); } - // 4. Nonce range (`TxValidatorNonceRangeValidator`) + // 4. The account's state (`TxValidatorAccountStateValidator`): a + // missing one was refused above, and a deleted one is a mark only + // a repository being written to carries. + + // 5. The nonce's encoding (`TxValidatorNonceEncodingValidator`), on + // the nonce's bytes as rskj holds them (`getNonce`), which the + // encoding rskj rebuilds keeps. + let encoded = tx.rlp_for_trie(); + match raw_nonce_bytes(&encoded) { + Some(nonce) if nonce.len() > 8 => return Err(PoolError::NonceTooLong(nonce.len())), + Some(nonce) if nonce_bytes_are_canonical(nonce) => {} + _ => return Err(PoolError::NonCanonicalNonce), + } + + // 6. Nonce range (`TxValidatorNonceRangeValidator`) if tx.nonce < state_nonce { return Err(PoolError::NonceTooLow); } @@ -757,7 +961,7 @@ impl TransactionPool { return Err(PoolError::NonceTooHigh); } - // 5. Balance >= gas cost (`TxValidatorAccountBalanceValidator`), + // 7. Balance >= gas cost (`TxValidatorAccountBalanceValidator`), // which a free transaction skips. rskj multiplies in whole // numbers (`Coin.multiply`), so a cost past 256 bits is more // than any balance. @@ -768,19 +972,23 @@ impl TransactionPool { } } - // 6. Minimum gas price (`TxValidatorMinimuGasPriceValidator`) + // 8. Minimum gas price (`TxValidatorMinimuGasPriceValidator`) if tx.gas_price < header.minimum_gas_price { return Err(PoolError::GasPriceTooLow); } - // 7. Intrinsic gas (`TxValidatorIntrinsicGasLimitValidator`): a free + // 9. Intrinsic gas (`TxValidatorIntrinsicGasLimitValidator`): a free // transaction costs nothing, so any limit, zero included, fits. - let intrinsic = if is_free { 0 } else { intrinsic_gas(tx) }; + let intrinsic = match rules { + _ if is_free => 0, + Some(rules) => rules.transaction_cost(tx, header.number), + None => intrinsic_gas(tx), + }; if U256::from(intrinsic) > tx.gas_limit { return Err(PoolError::IntrinsicGasTooHigh); } - // 8. Price cap (`TxValidatorMaximumGasPriceValidator`), RSKIP252. + // 10. Price cap (`TxValidatorMaximumGasPriceValidator`), RSKIP252. if let Some(cap) = rules.and_then(|r| r.gas_price_cap(header)) { if tx.gas_price > cap { return Err(PoolError::GasPriceAboveCap); @@ -918,27 +1126,6 @@ impl TransactionPool { } } - /// Promote any consecutive queued txs for a sender into pending. - fn promote_queued(&self, inner: &mut PoolInner, sender: Address, state_nonce: u64) { - let pending_map = inner.pending.entry(sender).or_default(); - let next_nonce = pending_map - .keys() - .last() - .map(|n| n + 1) - .unwrap_or(state_nonce); - - if let Some(queued_map) = inner.queued.get_mut(&sender) { - let mut nonce = next_nonce; - while let Some(ptx) = queued_map.remove(&nonce) { - pending_map.insert(nonce, ptx); - nonce += 1; - } - if queued_map.is_empty() { - inner.queued.remove(&sender); - } - } - } - /// Get a transaction from the pool by hash. pub fn get(&self, hash: &B256) -> Option { let inner = self.inner.read().unwrap_or_else(PoisonError::into_inner); @@ -1029,22 +1216,16 @@ impl TransactionPool { .ok_or_else(|| PoolError::StoreError("head header not found".into())) } - fn account_state(&self, header: &Header, addr: &Address) -> (u64, U256) { - let root_hash = header.state_root; - let root_data = match self.trie_store.get(root_hash.as_slice()) { - Some(d) => d, - None => return (0, U256::ZERO), - }; + /// The nonce and balance of `addr` in the state `header` names, or none + /// where that state holds no such account: rskj's `getAccountState`, + /// which is null then. + fn account_state(&self, header: &Header, addr: &Address) -> Option<(u64, U256)> { + let root_data = self.trie_store.get(header.state_root.as_slice())?; let root = TrieNode::from_message(&root_data, &*self.trie_store); let key = account_key(addr); - let expanded = TrieKeySlice::from_key(&key); - match root.get(&expanded, &*self.trie_store) { - Some(data) => match AccountState::decode(&data) { - Ok(acct) => (acct.nonce.to::(), acct.balance), - Err(_) => (0, U256::ZERO), - }, - None => (0, U256::ZERO), - } + let data = root.get(&TrieKeySlice::from_key(&key), &*self.trie_store)?; + let account = AccountState::decode(&data).ok()?; + Some((account.nonce.to::(), account.balance)) } } @@ -1582,7 +1763,10 @@ mod tests { // The same one again: rejected as already known, which is ordinary -- // several peers relay the same transaction. - assert!(matches!(pool.add_transaction(&raw), Err(PoolError::AlreadyKnown))); + assert!(matches!( + pool.add_transaction(&raw), + Err(PoolError::AlreadyPending) + )); // One rejected for an unrelated reason. let mut cheap = make_tx(1, 1); @@ -1770,7 +1954,7 @@ mod tests { pool.add_transaction(&raw).unwrap(); let err = pool.add_transaction(&raw).unwrap_err(); - assert!(matches!(err, PoolError::AlreadyKnown)); + assert!(matches!(err, PoolError::AlreadyPending)); } // ========== rskj: checkTxWithSameNonceIsRejected ========== @@ -1818,7 +2002,7 @@ mod tests { tx.gas_limit = U256::from(9_000_000); // > 8M block gas limit sign_tx(&mut tx, &key, 33); let err = pool.add_transaction(&encode_tx(&tx)).unwrap_err(); - assert!(matches!(err, PoolError::GasLimitExceeded)); + assert!(matches!(err, PoolError::GasLimitExceeded { .. })); } // ========== rskj: checkTxWithHighNonceIsRejected ========== @@ -2321,7 +2505,10 @@ mod tests { /// The same rule on the wire: a transaction whose nonce is sent as /// `00 01` is refused by the pool, although it decodes to nonce 1 and its - /// signature is good. + /// signature is its sender's. rskj signs over the nonce's bytes as they + /// came, so the signature is made over them here too: over the canonical + /// form it would be another account's, one the state doesn't hold, and + /// rskj asks for the account before it looks at the nonce. #[test] fn a_non_canonical_nonce_is_refused_on_admission() { let (pool, key, _) = setup_pool_with_account(U256::from(10u64.pow(18)), 1); @@ -2329,15 +2516,15 @@ mod tests { sign_tx(&mut tx, &key, 33); let canonical = encode_tx(&tx); - // Splice the nonce item: `0x01` becomes `0x82 0x00 0x01`. - let mut buf = &canonical[..]; - let list = alloy_rlp::Header::decode(&mut buf).unwrap(); - assert_eq!(buf[0], 0x01, "nonce 1 is a single byte"); - let mut payload = vec![0x82, 0x00, 0x01]; - payload.extend_from_slice(&buf[1..list.payload_length]); - let mut raw = Vec::new(); - alloy_rlp::Header { list: true, payload_length: payload.len() }.encode(&mut raw); - raw.extend_from_slice(&payload); + // The nonce item `0x01` as `0x82 0x00 0x01`, and the other five + // fields as they are. + let mut fields = vec![0x82, 0x00, 0x01]; + tx.gas_price.encode(&mut fields); + tx.gas_limit.encode(&mut fields); + tx.to.encode(&mut fields); + tx.value.encode(&mut fields); + tx.input.encode(&mut fields); + let raw = rskj_signed(&fields, &key, 33); let err = pool.add_transaction(&raw).unwrap_err(); assert!(matches!(err, PoolError::NonCanonicalNonce), "got {err}"); @@ -2380,7 +2567,7 @@ mod tests { assert_eq!(hash, tx.tx_hash(), "rskj's hash, of the minimal encoding"); assert_ne!(hash, B256::from_slice(&Keccak256::digest(&raw))); let err = pool.add_transaction(&canonical).unwrap_err(); - assert!(matches!(err, PoolError::AlreadyKnown), "got {err}"); + assert!(matches!(err, PoolError::AlreadyPending), "got {err}"); } /// A free Bridge transaction, as rskj has them before `areBridgeTxsPaid`: @@ -2507,7 +2694,7 @@ mod tests { tx.gas_limit = U256::from(7_000_000); sign_tx(&mut tx, &key, 33); let err = pool.add_transaction(&encode_tx(&tx)).unwrap_err(); - assert!(matches!(err, PoolError::GasLimitExceeded), "got {err}"); + assert!(matches!(err, PoolError::GasLimitExceeded { .. }), "got {err}"); tx.gas_limit = U256::from(6_800_000); sign_tx(&mut tx, &key, 33); pool.add_transaction(&encode_tx(&tx)).expect("at the sublist limit"); @@ -2546,7 +2733,7 @@ mod tests { tx.gas_limit = U256::from(8_000_000); // high enough for calldata sign_tx(&mut tx, &key, 33); let err = pool.add_transaction(&encode_tx(&tx)).unwrap_err(); - assert!(matches!(err, PoolError::TxTooLarge)); + assert!(matches!(err, PoolError::TxTooLarge { .. })); } // ========== rskj: checkTxMaxSizeAccepted ========== @@ -2601,8 +2788,9 @@ mod tests { // ========== rskj: checkTxFromNullStateIsRejected ========== #[test] - fn test_null_sender_insufficient_funds() { - // An account not in the trie has balance=0. Any tx requiring gas should fail. + fn a_sender_the_state_does_not_hold_is_refused() { + // rskj `TxPendingValidator.isValid`: no account, and the transaction + // costs something, so "the sender account doesn't exist". let dir = tempdir().unwrap(); let store = Arc::new(BlockStore::open(dir.path()).unwrap()); let trie_store: Arc = Arc::new(MemoryTrieStore::new()); @@ -2624,7 +2812,7 @@ mod tests { let mut tx = make_tx(0, 59_240_000); sign_tx(&mut tx, &signing_key, 33); let err = pool.add_transaction(&encode_tx(&tx)).unwrap_err(); - assert!(matches!(err, PoolError::InsufficientFunds)); + assert!(matches!(err, PoolError::SenderMissing), "got {err}"); std::mem::forget(dir); } @@ -2658,7 +2846,7 @@ mod tests { pool.add_transaction(&raw).unwrap(); let err = pool.add_transaction(&raw).unwrap_err(); - assert!(matches!(err, PoolError::AlreadyKnown)); + assert!(matches!(err, PoolError::AlreadyQueued)); } // ========== rskj: pending sorted by nonce ========== @@ -2829,18 +3017,259 @@ mod tests { #[test] fn test_pool_error_display() { - assert_eq!(PoolError::AlreadyKnown.to_string(), "transaction with same hash already exists"); + assert_eq!( + PoolError::AlreadyPending.to_string(), + "pending transaction with same hash already exists" + ); + assert_eq!( + PoolError::AlreadyQueued.to_string(), + "queued transaction with same hash already exists" + ); assert_eq!(PoolError::NonceTooLow.to_string(), "transaction nonce too low"); assert_eq!(PoolError::NonceTooHigh.to_string(), "transaction nonce too high"); assert_eq!(PoolError::InsufficientFunds.to_string(), "insufficient funds"); - assert_eq!(PoolError::GasLimitExceeded.to_string(), "transaction's gas limit exceeds block gas limit"); + let over = PoolError::GasLimitExceeded { + gas_limit: U256::from(9_000_000), + bound: 8_000_000, + }; + assert_eq!( + over.to_string(), + "transaction's gas limit of 9000000 is higher than the block's gas limit of 8000000" + ); assert_eq!(PoolError::GasPriceTooLow.to_string(), "transaction's gas price lower than block's minimum"); assert_eq!(PoolError::IntrinsicGasTooHigh.to_string(), "transaction's basic cost is above the gas limit"); assert_eq!(PoolError::ReplacementGasPriceTooLow.to_string(), "gas price not enough to bump transaction"); - assert_eq!(PoolError::TxTooLarge.to_string(), "transaction's size is higher than defined maximum"); + let large = PoolError::TxTooLarge { + size: 131_106, + max: 131_072, + }; + assert_eq!( + large.to_string(), + "transaction's size is higher than defined maximum: 131106 > 131072" + ); assert_eq!( PoolError::InsufficientFundsForPendingAndNew.to_string(), "insufficient funds to pay for pending and new transactions" ); } + + // ========== rskj's own answers ========== + + /// The bytes a testdata line writes in hex, a run of one byte as + /// `(bb*count)`. `None` for anything else. + fn written_bytes(text: &str) -> Option> { + let mut bytes = Vec::new(); + let mut rest = text; + while !rest.is_empty() { + if let Some(run) = rest.strip_prefix('(') { + let (value, after) = run.split_once('*')?; + let (count, after) = after.split_once(')')?; + let value = u8::from_str_radix(value, 16).ok()?; + bytes.extend(std::iter::repeat_n(value, count.parse().ok()?)); + rest = after; + } else { + bytes.push(u8::from_str_radix(rest.get(..2)?, 16).ok()?); + rest = &rest[2..]; + } + } + Some(bytes) + } + + /// The one parameter of an `eth_sendRawTransaction` a testdata line + /// records, as rskj reads hex: its `0x` optional, blanks passed over. + fn sent_bytes(asked: &str) -> Option> { + let (_, parameter) = asked.split_once(" eth_sendRawTransaction [\"")?; + let written = parameter.strip_suffix("\"]")?; + let digits: String = written + .strip_prefix("0x") + .unwrap_or(written) + .chars() + .filter(|c| *c != ' ') + .collect(); + written_bytes(&digits.to_lowercase()) + } + + /// A pool on rskj's regtest whose head is block `number`, over a state + /// that holds `accounts`. + fn rskj_regtest_pool( + (number, gas_limit, minimum_gas_price): (u64, u64, U256), + accounts: &[(Address, u64, U256)], + ) -> TransactionPool { + let dir = tempdir().unwrap(); + let store = Arc::new(BlockStore::open(dir.path()).unwrap()); + let trie_store: Arc = Arc::new(MemoryTrieStore::new()); + let mut root = TrieNode::empty(); + for (address, nonce, balance) in accounts { + let account = AccountState::new(U256::from(*nonce), *balance); + let key = account_key(address); + root = root.put( + &TrieKeySlice::from_key(&key), + &account.encode(), + &*trie_store, + ); + } + root.save(&*trie_store, true); + let state_root = root.compute_hash(&*trie_store); + let root_data = root.to_message(&*trie_store); + rustock_trie::TrieStore::put(&*trie_store, state_root.as_slice(), &root_data); + + let mut header = test_header(); + header.number = number; + header.gas_limit = U256::from(gas_limit); + header.minimum_gas_price = minimum_gas_price; + header.state_root = state_root; + store.update_head(&header, U256::from(100_000)).unwrap(); + std::mem::forget(dir); + let rules = ChainRules::for_chain( + rustock_execution::hardfork::RskHardforkConfig::rskj_regtest(), + 6_800_000, + ); + TransactionPool::new(test_config(), 33, store, trie_store).with_chain_rules(rules) + } + + /// What a testdata line shows of the transactions one side of the pool + /// holds: how many, and the start of each one's hash, in order. + fn held(side: &PoolGroups) -> String { + let mut hashes: Vec = side + .iter() + .flat_map(|(_, transactions)| transactions) + .map(|(_, tx)| hex_of(&tx.tx_hash()[..4])) + .collect(); + hashes.sort(); + match hashes.len() { + 0 => "0".to_string(), + count => format!("{count} {}", hashes.join(",")), + } + } + + fn hex_of(bytes: &[u8]) -> String { + bytes.iter().map(|byte| format!("{byte:02x}")).collect() + } + + /// The text between `before` and the next `after` of a JSON answer. + fn between<'a>(answer: &'a str, before: &str, after: &str) -> Option<&'a str> { + let (_, rest) = answer.split_once(before)?; + Some(rest.split_once(after)?.0) + } + + /// rskj `Transaction.transactionCost`, upgrade by upgrade, at mainnet's + /// heights: a data byte that isn't zero costs 68 until RSKIP400 + /// (arrowhead600) and 16 from it, and from RSKIP438 (lovell700) a + /// creation pays 2 for every 32 bytes of init code, the last ones + /// counted whole. + #[test] + fn the_basic_cost_follows_the_upgrades() { + let rules = ChainRules::for_chain( + rustock_execution::hardfork::RskHardforkConfig::mainnet(), + 6_800_000, + ); + let mut tx = make_tx(0, 1); + tx.input = Bytes::from(vec![0x01, 0x00, 0x02]); + assert_eq!(rules.transaction_cost(&tx, 6_223_699), 21_000 + 68 + 4 + 68); + assert_eq!(rules.transaction_cost(&tx, 6_223_700), 21_000 + 16 + 4 + 16); + + tx.to = Bytes::new(); + tx.input = Bytes::from(vec![0x01; 33]); + assert_eq!(rules.transaction_cost(&tx, 7_338_023), 53_000 + 33 * 16); + assert_eq!( + rules.transaction_cost(&tx, 7_338_024), + 53_000 + 33 * 16 + 2 * 2 + ); + tx.input = Bytes::new(); + assert_eq!( + rules.transaction_cost(&tx, 7_338_024), + 53_000, + "no init code, nothing more" + ); + } + + /// The pool answers every transaction rskj's release was sent as that + /// release's pool answered it, and holds afterwards what that pool held: + /// `testdata/rskj_pool_answers.txt` is the release's own run. A pool + /// refusal is rskj's error -32010, whose message is the pool's reason; a + /// transaction rskj refused before its pool saw it is not given to this + /// one either. + #[test] + fn the_pool_answers_as_rskjs_pool_answers() { + let data = include_str!("testdata/rskj_pool_answers.txt"); + let mut head = None; + let mut accounts = Vec::new(); + let mut pool: Option = None; + // Set once rskj's pool took a transaction this test has no bytes + // for, which its node signed itself: nothing after it can be compared. + let mut out_of_step = false; + let mut given = 0; + for line in data.lines() { + let mut words = line.split(' '); + let kind = words.next().unwrap_or_default(); + match kind { + "scenario" => { + head = None; + accounts.clear(); + pool = None; + out_of_step = false; + continue; + } + "head" => { + let mut number = || words.next().unwrap().parse::().unwrap(); + let (number, gas_limit) = (number(), number()); + let minimum = U256::from_str_radix(words.next().unwrap(), 10).unwrap(); + head = Some((number, gas_limit, minimum)); + continue; + } + "account" => { + let address: Address = words.nth(1).unwrap().parse().unwrap(); + let nonce = words.next().unwrap(); + if nonce != "none" { + let balance = U256::from_str_radix(words.next().unwrap(), 10).unwrap(); + accounts.push((address, nonce.parse().unwrap(), balance)); + } + continue; + } + "tx" | "call" => {} + _ => continue, + } + let pool = pool.get_or_insert_with(|| rskj_regtest_pool(head.unwrap(), &accounts)); + let (asked, rest) = line.split_once(" => ").unwrap(); + let (answer, rest) = rest.split_once(" | pending ").unwrap(); + let (pending, queued) = rest.split_once(" | queued ").unwrap(); + let raw = match kind { + "tx" => written_bytes(asked.rsplit(' ').next().unwrap()), + _ => sent_bytes(asked), + }; + let refusal = + between(answer, "\"code\":", ",").zip(between(answer, "\"message\":\"", "\"}}")); + let hash = between(answer, "\"result\":\"", "\""); + let answered = |pool: &TransactionPool, raw: &[u8]| { + pool.add_transaction(raw) + .map(|hash| format!("{hash:#x}")) + .map_err(|refusal| refusal.to_string()) + }; + match (raw, refusal, hash) { + // rskj's pool refused it: `transactionError`, with its reason. + (Some(raw), Some(("-32010", reason)), _) => { + assert!(!out_of_step, "{asked}: after a transaction the node signed"); + assert_eq!(answered(pool, &raw), Err(reason.to_string()), "{asked}"); + given += 1; + } + // rskj's pool took it. + (Some(raw), None, Some(hash)) => { + assert!(!out_of_step, "{asked}: after a transaction the node signed"); + assert_eq!(answered(pool, &raw), Ok(hash.to_string()), "{asked}"); + given += 1; + } + // rskj refused it before its pool saw it. + (_, Some(_), _) => {} + // The node signed a transaction itself and its pool took it. + (None, None, _) => out_of_step = true, + (Some(_), None, None) => panic!("{asked}: no answer read"), + } + if !out_of_step { + let (now_pending, now_queued) = pool.content(); + assert_eq!(held(&now_pending), pending, "pending after {asked}"); + assert_eq!(held(&now_queued), queued, "queued after {asked}"); + } + } + assert_eq!(given, 66, "transactions given to the pool"); + } } From 9ff91d182e02637bc7c3053e4d004ee5bfcabde7 Mon Sep 17 00:00:00 2001 From: Colby Serpa <129702033+ColbySerpa@users.noreply.github.com> Date: Thu, 12 Nov 2026 21:10:15 -0800 Subject: [PATCH 132/264] rpc, execution: a call and an estimate answer as rskj's release does eth_call and eth_estimateGas run a transaction that is never sent, and rskj runs one by rules a sent transaction does not have. Several were not followed, and the answer differed from rskj's in what a wallet or an application reads most. What the caller is answered: - A program that ended with REVERT was answered with its data as a result. rskj answers an error of its own, -32015, with the data in "data" and a message: "transaction reverted", or "revert " and the reason where the data is an Error(string). The reason is read as rskj's decoder reads one, which takes the low 32 bits of the offset and of the length and throws where the data ends short, and its bytes as the JDK reads bytes that are not UTF-8. eth_estimateGas answers a revert the same way, where it said "Transaction would revert". - An estimate of a call that fails without reverting was that error too. rskj estimates it from the gas it used: its cap, for a program that failed. And it estimates a creation without data at 0. What the call itself sees: - A sender that holds less than the value it sends. rskj moves the value all the same, and the sender's balance goes below zero. BALANCE and SELFBALANCE then push the fewest bytes of its two's complement, as DataWord.valueOf(Coin.getBytes()) makes them: 0xff for -1, 0xff7f for -129. Here the sender read as holding nothing. - A sender that sends itself the value keeps what it held, whatever that is. Here one short of the value was left holding the value. - A call that names no sender runs from rskj's null address, which has no bytes and is not the zero address: CALLER and ORIGIN push 0, but the value comes out of an account no program can read, the zero address is neither touched nor created, and a contract the call creates is at keccak256(rlp(["", 0])). Here it ran from the zero address in every respect. - A local call's sender keeps its nonce. For a creation revm bumps it, which a sender that holds a program reads when the constructor calls it and it creates in turn. - A gas price of 2^128 or more was refused. rskj runs the call, and GASPRICE pushes the price whole. - A value or a gas price of 2^255 or more can't be made into a program's invocation: where the call runs a program rskj throws, and where it runs none it answers. Here the program ran. - A value that would take the receiver past 2^256 moves, in rskj, whose balances have no top. Here the call failed. - Before RSKIP136 a call straight to a precompile runs whatever gas it has, less than the precompile's cost included. Here it was held to the cost. RSKIP136 can now be moved by --consensus-rule, as rskj's consensusRules moves it. - On a chain from an rskj genesis file a call, an estimate and a trace now run on rskj's regtest Bridge constants, as the node's blocks do. They ran on the ones the chain id alone gives. What an estimate comes to (gas_estimator.rs), each point as the release answers it: - The most a program had held was counted without the transaction's intrinsic cost, which rskj charges the program before it runs. A call with a fixed allowance that hands gas back was estimated up to that cost too low. - Where a call takes all the gas its program has left, rskj answers gasUsed + deductedRefund, and gasUsed is by then less the refund (TransactionExecutor.localCallFinalization): the sum is what was spent. The refund was added to that once more. - A CREATE spends all the gas its program has left and sets no flag, so rskj estimates a program that creates at its cap. It was taken for a call that took all the gas. - The 2,300 was added for a DELEGATECALL of a program that was sent value and for a CREATE with an endowment, which rskj counts neither of. And the flags of a callee that failed, or of a constructor, reached the estimate, where rskj drops them. docs/gas-estimation.md said rskj overestimates by the refund. It does not, and the note now says what it does. Two tests held the old figures and hold rskj's now. Left for a change of its own: what a short sender owes is taken off what it reads as holding, not off what it can spend. A sender that is a contract, paid back within the call, can send on what rskj's could not. The answers in the test are the release's own. Its JSON-RPC server answered 305 requests over the Web3 its RskContext builds on regtest, in seven scenarios, one of them with RSKIP136 switched off: calls to programs, to plain accounts, to the precompiles, the Bridge, REMASC and the native contracts, and creations. --- crates/cli/src/main.rs | 10 +- crates/execution/src/env.rs | 59 ++- crates/execution/src/executor.rs | 145 +++++-- crates/execution/src/gas_estimator.rs | 297 ++++++++------ crates/execution/src/hardfork.rs | 11 +- crates/execution/src/lib.rs | 4 +- crates/execution/src/raw_storage.rs | 12 + crates/execution/src/rsk_handler.rs | 232 ++++++++++- crates/execution/src/rsk_instructions.rs | 178 +++++++- crates/rpc/src/call.rs | 81 ++-- crates/rpc/src/debug.rs | 2 +- crates/rpc/src/lib.rs | 1 + crates/rpc/src/revert.rs | 229 +++++++++++ crates/rpc/src/server.rs | 28 ++ crates/rpc/src/testdata/rskj_local_calls.txt | 410 +++++++++++++++++++ crates/rpc/src/tests.rs | 254 ++++++++++-- crates/rpc/src/trace.rs | 2 +- crates/rpc/src/types.rs | 24 ++ docs/gas-estimation.md | 160 ++++---- 19 files changed, 1792 insertions(+), 347 deletions(-) create mode 100644 crates/rpc/src/revert.rs create mode 100644 crates/rpc/src/testdata/rskj_local_calls.txt diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index 22d4cfb..23be23b 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -1346,8 +1346,8 @@ struct Args { /// Regtest: override an rskj consensus rule, as `name=height` (`-1` /// disables it), like rskj's `blockchain.config.consensusRules`. - /// Repeatable. Supported: rskip97, rskip144, rskip351, rskip535, - /// rskip536 and rskipUMM. + /// Repeatable. Supported: rskip97, rskip136, rskip144, rskip351, + /// rskip535, rskip536 and rskipUMM. #[arg(long, value_name = "NAME=HEIGHT")] consensus_rule: Vec, @@ -3629,6 +3629,12 @@ async fn run(local_offset: Option) -> Result<()> { tx_submitter: Some(Arc::new(TxRelaySubmitter(tx_relay.clone()))), trie_store: Some(trie_store), hardfork_cfg: Some(hardfork_cfg), + // A chain from an rskj genesis file is rskj's regtest, Bridge + // federation included, for a call as for a block. + bridge_constants: config + .genesis_spec + .is_some() + .then(rustock_execution::bridge::constants::BridgeConstants::rskj_regtest), filter_store: Arc::new(rustock_rpc::logs::FilterStore::new()), tx_pool: Some(Arc::new(PoolAdapter(pool.clone()))), diff --git a/crates/execution/src/env.rs b/crates/execution/src/env.rs index d50a906..73cc618 100644 --- a/crates/execution/src/env.rs +++ b/crates/execution/src/env.rs @@ -78,6 +78,34 @@ pub fn tx_env_from_rsk_tx( tx: &rustock_core::Transaction, sender: Address, _hardfork_cfg: &RskHardforkConfig, +) -> Result { + let gas_price = u128::try_from(tx.gas_price) + .map_err(|_| ExecutionError::Evm(format!("gas price {} is 2^128 or more", tx.gas_price)))?; + tx_env(tx, sender, gas_price) +} + +/// A local call's `TxEnv`, and its gas price where that is 2^128 or more. +/// +/// rskj runs a call at whatever price it names: a local call is charged no +/// gas, so no balance has to cover it (`TransactionExecutor.execute`), and +/// GASPRICE pushes the price whole. revm's `TxEnv` can't hold such a price, so +/// its own is 0 then and the whole one goes beside it, in +/// `RskChainExt::whole_gas_price`, where GASPRICE finds it. +pub fn local_call_env( + tx: &rustock_core::Transaction, + sender: Address, +) -> Result<(TxEnv, Option), ExecutionError> { + match u128::try_from(tx.gas_price) { + Ok(gas_price) => Ok((tx_env(tx, sender, gas_price)?, None)), + Err(_) => Ok((tx_env(tx, sender, 0)?, Some(tx.gas_price))), + } +} + +/// The `TxEnv` of `tx` at `gas_price`. See [`tx_env_from_rsk_tx`]. +fn tx_env( + tx: &rustock_core::Transaction, + sender: Address, + gas_price: u128, ) -> Result { let gas_limit = tx.gas().ok_or_else(|| { ExecutionError::Evm(format!( @@ -85,8 +113,6 @@ pub fn tx_env_from_rsk_tx( tx.gas_limit )) })?; - let gas_price = u128::try_from(tx.gas_price) - .map_err(|_| ExecutionError::Evm(format!("gas price {} is 2^128 or more", tx.gas_price)))?; let kind = if tx.to.is_empty() { TxKind::Create } else { @@ -327,4 +353,33 @@ mod tests { u128::MAX ); } + + /// A local call runs at any price, as rskj's does: one revm's `TxEnv` + /// can't hold goes beside it, whole, and the `TxEnv`'s own is 0. The gas + /// limit is read as a transaction's is. + #[test] + fn a_local_call_carries_any_gas_price() { + let tx = |gas_limit: U256, gas_price: U256| rustock_core::Transaction { + nonce: 0, + gas_price, + gas_limit, + to: AlBytes::from(vec![0xAA; 20]), + value: U256::ZERO, + input: AlBytes::new(), + v: 0, + r: U256::ZERO, + s: U256::ZERO, + cached_rlp: None, + }; + let env = |gas_limit, gas_price| local_call_env(&tx(gas_limit, gas_price), Address::ZERO); + let (one, gas) = (U256::from(1u64), U256::from(21_000)); + + let (held, whole) = env(gas, U256::from(u128::MAX)).unwrap(); + assert_eq!((held.gas_price, whole), (u128::MAX, None)); + for price in [one << 128, (one << 255) - one, U256::MAX] { + let (held, whole) = env(gas, price).unwrap(); + assert_eq!((held.gas_price, whole), (0, Some(price))); + } + assert!(env(one << 63, one).is_err(), "eight bytes, top bit set"); + } } diff --git a/crates/execution/src/executor.rs b/crates/execution/src/executor.rs index 5a77598..7eb89c8 100644 --- a/crates/execution/src/executor.rs +++ b/crates/execution/src/executor.rs @@ -33,6 +33,51 @@ pub struct TxExecutionResult { /// (`SEQUENTIAL_ONLY_PRECOMPILES`). True whatever the call returned -- /// reaching the precompile is what counts. pub reached_sequential_only: bool, + /// rskj's `ProgramResult.isRevert`: the transaction's own program ended + /// with REVERT, and `output` is what it reverted with. `eth_call` and + /// `eth_estimateGas` answer a revert as an error of its own. A precompile + /// the transaction calls itself has no program and never ends so: what + /// one throws, `RskHandler` settles as rskj does. + pub reverted: bool, +} + +/// What one execution says of the gas a transaction needs +/// ([`RskExecutor::estimate_gas_exact`]). +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum GasEstimate { + /// The gas to give it. + Gas(u64), + /// Its program ended with REVERT, with this output. + Reverted(Vec), +} + +/// Who a local call is from: the sender it names, or nobody. rskj runs a call +/// that names no sender from its null address, which is not the zero address +/// (`RskHandler`'s `no_sender`). An `Address` is a sender named. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct LocalSender(Option
); + +impl LocalSender { + /// The address the call runs from here: the sender's, or the zero address. + fn address(self) -> Address { + self.0.unwrap_or(Address::ZERO) + } + + fn is_nobody(self) -> bool { + self.0.is_none() + } +} + +impl From
for LocalSender { + fn from(sender: Address) -> Self { + Self(Some(sender)) + } +} + +impl From> for LocalSender { + fn from(sender: Option
) -> Self { + Self(sender) + } } /// What the miner's execution mode left out of the block, by index into the @@ -342,7 +387,7 @@ impl RskExecutor { state_root: &TrieNode, trie_store: Arc, ) -> Result { - self.execute_tx_inner(header, tx, sender, state_root, trie_store, false) + self.execute_tx_inner(header, tx, sender.into(), state_root, trie_store, false) } /// Execute a transaction as a **local call** -- rskj's @@ -360,29 +405,54 @@ impl RskExecutor { /// getters -- the federation address, the BTC blockchain head, the /// peg-out queue sizes -- which rskj deliberately refuses to answer /// on-chain so that no contract can depend on them. + /// + /// `sender` is the sender the call names, or `None` where it names none + /// ([`LocalSender`]). pub fn execute_local_call( &self, header: &Header, tx: &rustock_core::Transaction, - sender: Address, + sender: impl Into, state_root: &TrieNode, trie_store: Arc, ) -> Result { - self.execute_tx_inner(header, tx, sender, state_root, trie_store, true) + self.execute_tx_inner(header, tx, sender.into(), state_root, trie_store, true) + } + + /// A transaction's `TxEnv`, and a local call's gas price where revm's + /// can't hold it (`env::local_call_env`). + fn tx_env_for( + &self, + tx: &rustock_core::Transaction, + sender: Address, + local_call: bool, + ) -> Result<(revm::context::TxEnv, Option), ExecutionError> { + if local_call { + return crate::env::local_call_env(tx, sender); + } + Ok((tx_env_from_rsk_tx(tx, sender, &self.hardfork_cfg)?, None)) + } + + /// rskj's `ProgramResult.isRevert` for a transaction's result: its own + /// program ended with REVERT. + fn program_reverted(exec: &revm::context::result::ExecutionResult) -> bool { + matches!(exec, revm::context::result::ExecutionResult::Revert { .. }) } fn execute_tx_inner( &self, header: &Header, tx: &rustock_core::Transaction, - sender: Address, + sender: LocalSender, state_root: &TrieNode, trie_store: Arc, local_call: bool, ) -> Result { + let no_sender = local_call && sender.is_nobody(); + let sender = sender.address(); let spec_id = self.hardfork_cfg.spec_id(header.number); let block_env = block_env_from_header(header, &self.hardfork_cfg); - let tx_env = tx_env_from_rsk_tx(tx, sender, &self.hardfork_cfg)?; + let (tx_env, whole_gas_price) = self.tx_env_for(tx, sender, local_call)?; let db = RskDatabase::new(state_root.clone(), trie_store.clone(), self.block_store.clone()); let cfg = make_cfg_env(spec_id, self.hardfork_cfg.chain_id, self.hardfork_cfg.has_rskip544(header.number)); @@ -390,6 +460,7 @@ impl RskExecutor { let mut chain_ext = crate::raw_storage::RskChainExt::default(); chain_ext.raw_storage.set_reader(trie_store, state_root.clone()); chain_ext.btc_block_cache = self.btc_block_cache.clone(); + chain_ext.whole_gas_price = whole_gas_price; let ctx = revm::Context::mainnet() .with_db(WrapDatabaseRef(db)) .with_block(block_env) @@ -462,7 +533,8 @@ impl RskExecutor { self.hardfork_cfg.has_rskip171(header.number), ) .failing_programs(!header_fits_program_invoke(header)) - .as_local_call(local_call, self.hardfork_cfg.has_rskip136(header.number)); + .as_local_call(local_call, self.hardfork_cfg.has_rskip136(header.number)) + .from_nobody(no_sender); let out = handler .run(&mut evm) .map_err(|e: revm::context::result::EVMError<_>| { @@ -478,6 +550,7 @@ impl RskExecutor { let exec = result.result; let success = exec.is_success(); + let reverted = Self::program_reverted(&exec); let gas_used = exec.gas_used(); let output = exec.output().map(|o| o.to_vec()).unwrap_or_default(); let logs = exec.into_logs(); @@ -492,6 +565,7 @@ impl RskExecutor { output, logs, created_address, + reverted, }) } @@ -520,7 +594,7 @@ impl RskExecutor { ) -> Result<(TxExecutionResult, crate::tracer::ProgramTrace), ExecutionError> { let spec_id = self.hardfork_cfg.spec_id(header.number); let block_env = block_env_from_header(header, &self.hardfork_cfg); - let tx_env = tx_env_from_rsk_tx(tx, sender, &self.hardfork_cfg)?; + let (tx_env, whole_gas_price) = self.tx_env_for(tx, sender, local_call)?; let db = RskDatabase::new(state_root.clone(), trie_store.clone(), self.block_store.clone()); let cfg = make_cfg_env(spec_id, self.hardfork_cfg.chain_id, self.hardfork_cfg.has_rskip544(header.number)); @@ -528,6 +602,7 @@ impl RskExecutor { let mut chain_ext = crate::raw_storage::RskChainExt::default(); chain_ext.raw_storage.set_reader(trie_store, state_root.clone()); chain_ext.btc_block_cache = self.btc_block_cache.clone(); + chain_ext.whole_gas_price = whole_gas_price; let ctx = revm::Context::mainnet() .with_db(WrapDatabaseRef(db)) .with_block(block_env) @@ -618,6 +693,7 @@ impl RskExecutor { let exec = result.result; let success = exec.is_success(); + let reverted = Self::program_reverted(&exec); let gas_used = exec.gas_used(); let output = exec.output().map(|o| o.to_vec()).unwrap_or_default(); let outcome = TracedOutcome::of(tx, sender, &exec, &output); @@ -646,6 +722,7 @@ impl RskExecutor { output, logs, created_address, + reverted, reached_sequential_only: reached_sequential_only .load(std::sync::atomic::Ordering::Relaxed), }, @@ -656,9 +733,14 @@ impl RskExecutor { /// Estimate a transaction's gas in one execution. /// - /// Returns `(estimate, success, output)`. On failure the estimate is zero - /// and the output is the revert data, so the caller can report the reason - /// rather than a number. + /// A program that ends with REVERT has no estimate: rskj answers with what + /// it reverted with (`EthModule.estimateGas`), and so does + /// [`GasEstimate::Reverted`]. A run that fails any other way is estimated + /// as one that doesn't fail is, from the gas it used + /// (`EthModule.internalEstimateGas`): where a program failed, all it was + /// given, which is the cap. And a creation without data is estimated at + /// 0: rskj's `create()` works out what is left of its gas and spends none + /// in the result the estimate is read from (TransactionExecutor.java:406). /// /// The floor matters: a plain value transfer never enters the /// interpreter, so the inspector sees no opcodes and reports no peak. Its @@ -667,18 +749,23 @@ impl RskExecutor { &self, header: &Header, tx: &rustock_core::Transaction, - sender: Address, + sender: impl Into, state_root: &TrieNode, trie_store: Arc, cap: u64, - ) -> Result<(u64, bool, Vec), ExecutionError> { + ) -> Result { let (result, telemetry) = self.execute_tx_for_estimate(header, tx, sender, state_root, trie_store, true)?; + if result.reverted { + return Ok(GasEstimate::Reverted(result.output)); + } + if tx.to.is_empty() && tx.input.is_empty() { + return Ok(GasEstimate::Gas(0)); + } if !result.success { - return Ok((0, false, result.output)); + return Ok(GasEstimate::Gas(result.gas_used.min(cap))); } - let estimate = telemetry.estimate(cap); - Ok((estimate, true, result.output)) + Ok(GasEstimate::Gas(telemetry.estimate(cap))) } /// The same execution as `execute_tx_inner`, with the gas-estimation @@ -695,14 +782,18 @@ impl RskExecutor { &self, header: &Header, tx: &rustock_core::Transaction, - sender: Address, + sender: impl Into, state_root: &TrieNode, trie_store: Arc, local_call: bool, ) -> Result<(TxExecutionResult, crate::gas_estimator::EstimateTelemetry), ExecutionError> { + let sender = sender.into(); + let no_sender = local_call && sender.is_nobody(); + let sender = sender.address(); let spec_id = self.hardfork_cfg.spec_id(header.number); let block_env = block_env_from_header(header, &self.hardfork_cfg); - let tx_env = tx_env_from_rsk_tx(tx, sender, &self.hardfork_cfg)?; + let (tx_env, whole_gas_price) = self.tx_env_for(tx, sender, local_call)?; + let gas_limit = tx_env.gas_limit; let db = RskDatabase::new(state_root.clone(), trie_store.clone(), self.block_store.clone()); let cfg = make_cfg_env(spec_id, self.hardfork_cfg.chain_id, self.hardfork_cfg.has_rskip544(header.number)); @@ -710,6 +801,7 @@ impl RskExecutor { let mut chain_ext = crate::raw_storage::RskChainExt::default(); chain_ext.raw_storage.set_reader(trie_store, state_root.clone()); chain_ext.btc_block_cache = self.btc_block_cache.clone(); + chain_ext.whole_gas_price = whole_gas_price; let ctx = revm::Context::mainnet() .with_db(WrapDatabaseRef(db)) .with_block(block_env) @@ -783,7 +875,8 @@ impl RskExecutor { self.hardfork_cfg.has_rskip171(header.number), ) .failing_programs(!header_fits_program_invoke(header)) - .as_local_call(local_call, self.hardfork_cfg.has_rskip136(header.number)); + .as_local_call(local_call, self.hardfork_cfg.has_rskip136(header.number)) + .from_nobody(no_sender); use revm::inspector::InspectorHandler; let out = handler .inspect_run(&mut evm) @@ -800,20 +893,19 @@ impl RskExecutor { let exec = result.result; let success = exec.is_success(); + let reverted = Self::program_reverted(&exec); let gas_used = exec.gas_used(); let output = exec.output().map(|o| o.to_vec()).unwrap_or_default(); // `spent` is revm's pre-refund figure -- limit minus remaining -- - // which is the same quantity rskj's `getGasUsed()` holds, and what an - // estimate has to cover. `used()` is after the refund and would - // under-report. + // which is what an estimate has to cover, and what rskj's + // `getGasUsed() + getDeductedRefund()` comes to. `used()` is after + // the refund and would under-report. let spent = exec.gas().spent(); - let refunded = exec.gas().final_refunded(); let logs = exec.into_logs(); let created_address = Self::extract_created_address(tx, sender, &success); - let telemetry = - std::mem::take(&mut evm.inspector).into_telemetry(spent, refunded); + let telemetry = std::mem::take(&mut evm.inspector).into_telemetry(gas_limit, spent); Ok(( TxExecutionResult { @@ -822,6 +914,7 @@ impl RskExecutor { output, logs, created_address, + reverted, reached_sequential_only: reached_sequential_only .load(std::sync::atomic::Ordering::Relaxed), }, @@ -1773,6 +1866,7 @@ impl RskExecutor { output: Vec::new(), logs, created_address: None, + reverted: false, }); if crate::supply::per_transaction_enabled() { use revm::context_interface::ContextTr; @@ -2030,6 +2124,7 @@ impl RskExecutor { output, logs, created_address: None, + reverted: false, }); if crate::supply::per_transaction_enabled() { use revm::context_interface::ContextTr; @@ -2187,6 +2282,7 @@ impl RskExecutor { }; let success = result.is_success(); + let reverted = Self::program_reverted(&result); let gas_used = result.gas_used(); let output = result.output().map(|o| o.to_vec()).unwrap_or_default(); // Every transaction's, traced or not, so that one's can't reach the @@ -2256,6 +2352,7 @@ impl RskExecutor { output, logs, created_address, + reverted, }); if crate::supply::per_transaction_enabled() { diff --git a/crates/execution/src/gas_estimator.rs b/crates/execution/src/gas_estimator.rs index b44b2a4..7b6b30e 100644 --- a/crates/execution/src/gas_estimator.rs +++ b/crates/execution/src/gas_estimator.rs @@ -38,9 +38,10 @@ //! When a callee is handed every last unit of the caller's remaining gas, the //! peak stops saying anything: it is just the cap we happened to run at. //! rskj detects that case (`calleeGas == remainingGas`) and switches to -//! `gasUsed + deductedRefund`, which is the honest total — what was spent, -//! plus the refunds that are credited at the end but must be *supplied* up -//! front. +//! `gasUsed + deductedRefund`. By then `gasUsed` is what the transaction is +//! charged, the refund already off it +//! (`TransactionExecutor.localCallFinalization`), so the sum is what was spent +//! before the refund: the gas that had to be there. //! //! Note this is RSK-specific and the reason the rule is this simple here: RSK //! never adopted EIP-150's 63/64 retention, so a caller really can hand over @@ -54,6 +55,28 @@ //! A CALL that transfers value grants the callee a 2,300-gas stipend it did //! not have to pay for. The caller still has to be able to cover it, so the //! estimate carries it. +//! +//! ## What rskj counts +//! +//! The estimate is rskj's to the unit, so what it counts is counted here as +//! it counts it, each point as its release answers `eth_estimateGas` (the rpc +//! crate's `testdata/rskj_local_calls.txt`): +//! +//! - The peak is that of the transaction's own program, and it starts at the +//! intrinsic cost, which rskj charges the program before it runs +//! (`TransactionExecutor.go`). +//! - A CREATE spends all the gas its program has left, and what the +//! constructor doesn't use comes back (`Program.createContract`). So the +//! peak of a program that creates is all it was given, and the estimate is +//! the cap, unless a call of that program took all it had left. +//! - The two flags belong to a program (`ProgramResult`). A CALL, CALLCODE, +//! DELEGATECALL or STATICCALL sets them in the program that runs it +//! (`VM.doCALL`), whether or not the call is then made. They pass to the +//! program that called it only where it ends without an exception or a +//! REVERT (`ProgramResult.merge`), and never from a constructor to the +//! program that ran its CREATE, which sets neither. +//! - A call carries value where its CALL or CALLCODE names one. A +//! DELEGATECALL names none, whatever value its program was called with. use alloy_primitives::Address; use revm::context::ContextTr; @@ -69,17 +92,16 @@ pub const STIPEND_CALL: u64 = 2_300; #[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] pub struct EstimateTelemetry { /// Gas consumed before the end-of-transaction refund -- revm's - /// `ResultGas::spent`, which is the same quantity rskj's `getGasUsed()` - /// holds. Not `used()`, which is after the refund and would under-report. + /// `ResultGas::spent`. rskj's `getGasUsed() + getDeductedRefund()` is the + /// same quantity: its `getGasUsed()` is less the refund by the time an + /// estimate reads it. Not `used()`, which is after the refund and would + /// under-report. pub gas_used: u64, - /// Refunds credited at the end. They reduce what is *charged* and not - /// what has to be *available*, so an estimate has to add them back. - pub refunded: u64, - /// rskj's `maxGasUsed`: the high-water mark of the root frame's - /// commitment, counting a callee's whole allowance from the moment it is - /// handed over. - pub max_committed: u64, - /// Some call was given every unit the caller had left, so `max_committed` + /// rskj's `maxGasUsed`: the high-water mark of the transaction's own + /// program, from the intrinsic cost up, counting a callee's whole + /// allowance from the moment it is handed over. 0 where no program ran. + pub max_gas_used: u64, + /// Some call was given every unit its caller had left, so `max_gas_used` /// is an artefact of the cap we ran at rather than a property of the /// transaction. pub moved_all_to_child: bool, @@ -97,30 +119,22 @@ impl EstimateTelemetry { /// actually supply is not an estimate, it is a refusal in disguise. pub fn estimate(&self, cap: u64) -> u64 { let mut estimate: u64 = if self.moved_all_to_child { - // rskj's branch, and note what `gas_used` is here: the *pre-refund* - // figure. rskj's `getGasUsed()` is never reduced by the end-of- - // transaction refund -- `TransactionExecutor.refundGas` credits - // `gasLeftover` instead -- so this sum is consumption plus the - // refund on top, and is deliberately the looser of the two - // answers. It is only reached when the peak has stopped meaning - // anything. - self.gas_used.saturating_add(self.refunded) + // rskj's `getGasUsed() + getDeductedRefund()`: what was spent + // before the refund. It is only reached when the peak has + // stopped meaning anything. + self.gas_used } else { - // The peak, alone. The refund is credited back at the end, so the - // caller never has to hold it; the peak is the whole requirement. - self.max_committed + // The peak. It is never below what was spent, but for two cases + // the inspector doesn't see, where what was spent is the whole + // answer: a transfer to an account with no code never enters the + // interpreter, so there is no peak to report; and the cost of + // the code a creation leaves is charged after its last step. + // + // The floor has to be applied *here*, before the stipend: + // flooring afterwards would discard the stipend whenever the + // peak came out below the spend. + self.max_gas_used.max(self.gas_used) }; - // Whatever the branch, the estimate can never be below what the - // transaction actually spent. A transfer to an account with no code - // never enters the interpreter, so there is no peak to report and - // this floor is the entire answer. - // - // It has to be applied *here*, before the stipend: flooring - // afterwards would discard the stipend whenever the peak came out - // below the spend, and the estimate would then be too small to - // execute -- which is the one way an estimator can be useless rather - // than merely imprecise. - estimate = estimate.max(self.gas_used); if self.call_with_value { estimate = estimate.saturating_add(STIPEND_CALL); } @@ -128,34 +142,44 @@ impl EstimateTelemetry { } } -/// Records the three signals `EstimateTelemetry` needs, and nothing else. -/// -/// Scoped to the root frame deliberately. rskj keeps a counter per frame and -/// takes the maximum across them when a child's result is merged into its -/// parent's, but a child's peak is bounded by the allowance its parent -/// committed, and that commitment is already in the parent's own peak. The -/// root's peak therefore dominates every descendant's, and tracking one frame -/// avoids carrying a stack of counters that can only ever agree with it. -/// Frame depth of the transaction's own call. revm announces the root frame -/// through the `call` hook exactly as it announces a nested one, so the root -/// is depth 1 and a child of the root is depth 2. -const ROOT_DEPTH: usize = 1; +/// What rskj's `ProgramResult` holds of one program for the estimate, beside +/// its gas. +#[derive(Debug, Clone, Copy, Default)] +struct ProgramFlags { + /// One of its calls was given every unit it had left. + moved_all_to_child: bool, + /// One of its calls named a value. + call_with_value: bool, +} +/// Records what `EstimateTelemetry` needs, and nothing else. +/// +/// The peak is the transaction's own program's alone. rskj keeps a counter +/// per program and takes the maximum across them when a callee's result is +/// merged into its caller's, but a callee's peak is bounded by the allowance +/// its caller committed, and that commitment is already in the caller's own +/// peak. The flags are kept for every open program, as rskj keeps them, and +/// passed to the caller as rskj passes them (see the module's last section). +/// +/// revm announces the transaction's own call through the `call` or `create` +/// hook exactly as it announces a nested one, so the first program opened is +/// the transaction's own. #[derive(Debug, Default)] pub struct EstimationInspector { - /// Frames entered so far: 0 before the root is announced, 1 while the - /// root runs, 2+ inside a child. - depth: usize, - /// The root frame's gas limit, learned from the first step. + /// The programs that are open, the transaction's own first. That one + /// stays when it ends: its flags are the answer. + open: Vec, + /// The gas limit of the transaction's own program: the transaction's, + /// less its intrinsic cost. Learned from its first step. root_limit: u64, - /// The root frame's remaining gas as of its last completed step. - root_remaining: u64, - /// Whether the root frame has been stepped at all. A transaction that is - /// a plain value transfer never enters the interpreter. + /// The gas left to the program that ran the last completed step. + remaining: u64, + /// Whether the transaction's own program has been stepped at all. A + /// transaction that is a plain value transfer never enters the + /// interpreter. stepped: bool, + /// The most the transaction's own program had spent of `root_limit`. max_committed: u64, - moved_all_to_child: bool, - call_with_value: bool, } impl EstimationInspector { @@ -164,54 +188,45 @@ impl EstimationInspector { } /// Fold the inspector's observations together with the figures only the - /// finished execution knows. - pub fn into_telemetry(self, gas_used: u64, refunded: u64) -> EstimateTelemetry { + /// finished execution knows: the transaction's gas limit, and what it + /// spent of it before the refund. + pub fn into_telemetry(self, gas_limit: u64, gas_used: u64) -> EstimateTelemetry { + let own = self.open.first().copied().unwrap_or_default(); EstimateTelemetry { gas_used, - refunded, // A transaction that never ran bytecode has no peak to report; // its cost is entirely intrinsic, which `gas_used` already holds. - max_committed: if self.stepped { self.max_committed } else { 0 }, - moved_all_to_child: self.moved_all_to_child, - call_with_value: self.call_with_value, + // One that did was charged the intrinsic cost first: what the + // transaction's limit has over its program's. + max_gas_used: if self.stepped { + gas_limit + .saturating_sub(self.root_limit) + .saturating_add(self.max_committed) + } else { + 0 + }, + moved_all_to_child: own.moved_all_to_child, + call_with_value: own.call_with_value, } } - /// Shared by the call and create hooks: a frame is being entered. - fn enter_frame(&mut self, value_is_nonzero: bool) { - // Only a CALL *opcode* counts, which is what rskj's `VM.doCALL` - // flags. The transaction's own value is already paid for by the - // intrinsic cost and grants no stipend, so the root frame's entry - // must not set this. - if value_is_nonzero && self.depth >= ROOT_DEPTH { - self.call_with_value = true; - } - if self.depth == ROOT_DEPTH && self.stepped && self.root_remaining == 0 { - // The opcode has already charged its own cost and the callee's - // allowance, and left the root with nothing. That is rskj's - // `calleeGas == remainingGas`. - self.moved_all_to_child = true; - } - self.depth += 1; + /// A program is being opened: the transaction's own, a callee's or a + /// constructor's. + fn open_program(&mut self) { + self.open.push(ProgramFlags::default()); } - fn leave_frame(&mut self) { - self.depth = self.depth.saturating_sub(1); - } - - fn observe_root(&mut self, limit: u64, remaining: u64) { - if self.depth != ROOT_DEPTH { + /// The program on top has ended. A callee's flags pass to its caller + /// where `merges`; the transaction's own program stays. + fn close_program(&mut self, merges: bool) { + if self.open.len() < 2 { return; } - self.stepped = true; - if self.root_limit == 0 { - self.root_limit = limit; - } - let committed = self.root_limit.saturating_sub(remaining); - if committed > self.max_committed { - self.max_committed = committed; + let ended = self.open.pop().unwrap_or_default(); + if let (true, Some(caller)) = (merges, self.open.last_mut()) { + caller.moved_all_to_child |= ended.moved_all_to_child; + caller.call_with_value |= ended.call_with_value; } - self.root_remaining = remaining; } } @@ -221,9 +236,9 @@ where INTR: InterpreterTypes, { fn step(&mut self, interp: &mut Interpreter, _context: &mut CTX) { - // Before the opcode runs, so the limit is known even for a frame + // Before the opcode runs, so the limit is known even for a program // whose first instruction reverts. - if self.depth == ROOT_DEPTH && self.root_limit == 0 { + if self.open.len() == 1 && !self.stepped { self.root_limit = interp.gas.limit(); self.stepped = true; } @@ -233,20 +248,34 @@ where // After the opcode, which is the point that matters: a CALL has by // now charged both its own cost and the whole allowance it is about // to hand over, so this is where the commitment peaks. - self.observe_root(interp.gas.limit(), interp.gas.remaining()); + self.remaining = interp.gas.remaining(); + if self.open.len() == 1 { + let committed = self.root_limit.saturating_sub(self.remaining); + self.max_committed = self.max_committed.max(committed); + } } fn call(&mut self, _context: &mut CTX, inputs: &mut CallInputs) -> Option { - self.enter_frame(!inputs.value.get().is_zero()); + // With a program open this is a CALL-family opcode it just ran, + // which has charged its own cost and the callee's allowance: one that + // left its program with nothing is rskj's `calleeGas == remainingGas`. + // The transaction's own call sets nothing: its value is paid for by + // the transaction and grants no stipend. + let remaining = self.remaining; + if let Some(caller) = self.open.last_mut() { + caller.moved_all_to_child |= remaining == 0; + caller.call_with_value |= inputs.transfers_value(); + } + self.open_program(); None } - fn call_end(&mut self, _context: &mut CTX, _inputs: &CallInputs, _outcome: &mut CallOutcome) { - self.leave_frame(); + fn call_end(&mut self, _context: &mut CTX, _inputs: &CallInputs, outcome: &mut CallOutcome) { + self.close_program(outcome.result.result.is_ok()); } - fn create(&mut self, _context: &mut CTX, inputs: &mut CreateInputs) -> Option { - self.enter_frame(!inputs.value().is_zero()); + fn create(&mut self, _context: &mut CTX, _inputs: &mut CreateInputs) -> Option { + self.open_program(); None } @@ -256,7 +285,7 @@ where _inputs: &CreateInputs, _outcome: &mut CreateOutcome, ) { - self.leave_frame(); + self.close_program(false); } } @@ -283,57 +312,44 @@ mod tests { fn the_floor_does_not_swallow_the_stipend() { let t = EstimateTelemetry { gas_used: 30_721, - max_committed: 28_000, // below the spend + max_gas_used: 28_000, // below the spend call_with_value: true, ..Default::default() }; assert_eq!(t.estimate(10_000_000), 30_721 + STIPEND_CALL); } - /// With no call in the picture the peak is the whole story, and the - /// refund is credited back rather than held -- so it is not added. - #[test] - fn a_refund_alone_does_not_raise_the_estimate() { - let t = EstimateTelemetry { - gas_used: 30_000, - refunded: 4_800, - max_committed: 30_000, - ..Default::default() - }; - assert_eq!(t.estimate(10_000_000), 30_000); - } - /// The case binary search exists for: a callee handed far more than it /// spends. The caller had to hold the whole allowance. #[test] fn the_peak_wins_over_the_total() { let t = EstimateTelemetry { gas_used: 26_000, // what was actually consumed - max_committed: 121_000, // what had to be available at the CALL + max_gas_used: 121_000, // what had to be available at the CALL ..Default::default() }; assert_eq!(t.estimate(10_000_000), 121_000); } /// When everything was handed over, the peak is just the cap we ran at - /// and says nothing; the total is the honest answer. + /// and says nothing; the total is the honest answer. It is what was spent + /// before the refund, and the refund is not added to it again. #[test] fn handing_over_everything_falls_back_to_the_total() { let t = EstimateTelemetry { gas_used: 26_000, - refunded: 1_000, - max_committed: 9_999_999, // an artefact of the cap + max_gas_used: 9_999_999, // an artefact of the cap moved_all_to_child: true, ..Default::default() }; - assert_eq!(t.estimate(10_000_000), 27_000); + assert_eq!(t.estimate(10_000_000), 26_000); } #[test] fn a_value_bearing_call_carries_the_stipend() { let t = EstimateTelemetry { gas_used: 30_000, - max_committed: 30_000, + max_gas_used: 30_000, call_with_value: true, ..Default::default() }; @@ -344,7 +360,42 @@ mod tests { /// supply is not an estimate. #[test] fn the_estimate_is_clamped_to_the_cap() { - let t = EstimateTelemetry { gas_used: 50_000, max_committed: 90_000, ..Default::default() }; + let t = EstimateTelemetry { + gas_used: 50_000, + max_gas_used: 90_000, + ..Default::default() + }; assert_eq!(t.estimate(60_000), 60_000); } + + /// The flags are a program's own, and pass to its caller as rskj's + /// `ProgramResult.merge` passes them: from a callee that ended well, and + /// from no constructor. The transaction's own program stays when it ends. + #[test] + fn a_programs_flags_pass_to_its_caller_as_rskjs_do() { + let flagged = ProgramFlags { + moved_all_to_child: true, + call_with_value: true, + }; + let run = |merges: bool| { + let mut inspector = EstimationInspector::new(); + inspector.open_program(); + inspector.open_program(); + inspector.open[1] = flagged; + inspector.close_program(merges); + inspector.close_program(true); + assert_eq!( + inspector.open.len(), + 1, + "the transaction's own program stays" + ); + inspector.into_telemetry(100_000, 30_000) + }; + let merged = run(true); + assert!(merged.moved_all_to_child && merged.call_with_value); + let dropped = run(false); + assert!(!dropped.moved_all_to_child && !dropped.call_with_value); + // Nothing was stepped, so there is no peak. + assert_eq!((merged.gas_used, merged.max_gas_used), (30_000, 0)); + } } diff --git a/crates/execution/src/hardfork.rs b/crates/execution/src/hardfork.rs index 42b9422..145b0ca 100644 --- a/crates/execution/src/hardfork.rs +++ b/crates/execution/src/hardfork.rs @@ -88,6 +88,7 @@ pub const TESTNET_ACTIVATIONS: &[(u64, RskNetworkUpgrade)] = &[ /// by name are here; every other rule follows its upgrade. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum ConsensusRule { + Rskip136, Rskip144, Rskip351, Rskip535, @@ -98,6 +99,7 @@ impl ConsensusRule { /// The rule rskj calls `name` in `consensusRules`. pub fn from_rskj_name(name: &str) -> Option { Some(match name { + "rskip136" => Self::Rskip136, "rskip144" => Self::Rskip144, "rskip351" => Self::Rskip351, "rskip535" => Self::Rskip535, @@ -405,8 +407,8 @@ impl RskHardforkConfig { /// rskj's upgrade for RSKIP559, which no network schedules and this /// node doesn't implement. Accepted and reported as such. Any other /// name is refused, as rskj's `NetworkUpgrade.named` refuses it. - /// - `rskip144`, `rskip351`, `rskip535`, `rskip536`: scheduled apart from - /// their upgrade. + /// - `rskip136`, `rskip144`, `rskip351`, `rskip535`, `rskip536`: scheduled + /// apart from their upgrade. /// - `rskipUMM`: accepted at any height. Only a header's layout reads it, /// from the chain's activation heights, which the caller moves. /// @@ -443,7 +445,7 @@ impl RskHardforkConfig { } else { return Err(format!( "consensus rule {name}: not one this node schedules on its own \ - (rskip144, rskip351, rskip535, rskip536, rskipUMM)" + (rskip136, rskip144, rskip351, rskip535, rskip536, rskipUMM)" )); } } @@ -882,6 +884,9 @@ impl RskHardforkConfig { /// the tx gas limit against the precompile cost alone and recorded /// gasUsed = precompileCost + intrinsicCost, which can EXCEED the limit. pub fn has_rskip136(&self, block_number: u64) -> bool { + if let Some(height) = self.rule_override(ConsensusRule::Rskip136) { + return height.is_some_and(|h| block_number >= h); + } let height = match self.chain_id { RSK_MAINNET_CHAIN_ID => 3_397, _ => 0, diff --git a/crates/execution/src/lib.rs b/crates/execution/src/lib.rs index d53cb5a..37d10ae 100644 --- a/crates/execution/src/lib.rs +++ b/crates/execution/src/lib.rs @@ -31,7 +31,9 @@ impl> Rs pub use database::RskDatabase; pub use bridge::btc_block_cache::{BtcBlockCache, CacheStatsSnapshot}; pub use raw_storage::{RawStorage, RskChainExt}; -pub use executor::{BlockExecutionResult, ExecutionError, RskExecutor, TxExecutionResult}; +pub use executor::{ + BlockExecutionResult, ExecutionError, GasEstimate, LocalSender, RskExecutor, TxExecutionResult, +}; pub use hardfork::{RskHardforkConfig, RskNetworkUpgrade, RSK_MAINNET_CHAIN_ID}; pub use precompiles::{ rsk_precompiles, is_rsk_precompile, diff --git a/crates/execution/src/raw_storage.rs b/crates/execution/src/raw_storage.rs index 5757ba3..a899a11 100644 --- a/crates/execution/src/raw_storage.rs +++ b/crates/execution/src/raw_storage.rs @@ -76,6 +76,18 @@ pub struct RskChainExt { /// Bridge when a contract calls it. `RskHandler` follows the programs, /// and the precompiles tell it of the calls that reach them. pub internal_txs: crate::internal_tx::InternalTxs, + /// What a local call's sender owes: the part of the value it sent that it + /// didn't hold. rskj's `Repository.transfer` takes a sender below zero + /// rather than fail (`TransactionExecutor.call`), which no balance here + /// can show, so `RskHandler` lifts the sender by this much before the + /// value moves, and BALANCE and SELFBALANCE take it off again as they + /// read the account (`rsk_instructions::LocalCallWords`). `None` in a + /// block's run. + pub owed: Option<(Address, U256)>, + /// A local call's gas price where revm's transaction can't hold it, at + /// 2^128 or more. rskj carries whatever price a call names, and GASPRICE + /// pushes it whole. + pub whole_gas_price: Option, } /// Raw-bytes storage overlay for the block being executed. diff --git a/crates/execution/src/rsk_handler.rs b/crates/execution/src/rsk_handler.rs index 3984e43..9398c4d 100644 --- a/crates/execution/src/rsk_handler.rs +++ b/crates/execution/src/rsk_handler.rs @@ -154,6 +154,17 @@ invisible_exception: Arc, /// all its gas as it charges that cost (455): the loop halts it as it /// opens. starved: std::cell::Cell, + /// A local call that names no sender. rskj runs it from its null address, + /// which has no bytes (`CallArgumentsToByteArray.getFromAddress`): CALLER + /// and ORIGIN push zero, as they do for the zero address the call runs + /// from here, but the value comes out of the null address's own account, + /// which no program can read, and a contract it creates gets its address + /// from a sender of no bytes (`HashUtil.calcNewAddr`). + no_sender: bool, + /// Set for a local call whose value moves nothing here + /// (`validate_against_state_and_deduct_caller`): its first frame carries + /// the value and transfers none. + moves_nothing: std::cell::Cell, _phantom: core::marker::PhantomData<(CTX, ERROR, FRAME)>, } @@ -202,6 +213,83 @@ impl RskHandler { self } + /// A local call that names no sender. See the field. + pub fn from_nobody(mut self, no_sender: bool) -> Self { + self.no_sender = no_sender; + self + } + + /// Whether a local call to `to` runs whatever gas it has: a precompile, + /// before RSKIP136. rskj leaves out the check that the gas covers a + /// precompile's cost for every local call (TransactionExecutor.java:350). + /// From RSKIP136 it throws as it works out what is left + /// (`GasCost.subtract`, 360); before it, that is a plain subtraction, + /// which goes below zero and throws nothing, and the precompile runs. + fn runs_unmetered(&self, to: &Address) -> bool { + self.local_call && !self.rskip136_active && self.active_precompiles.contains(to) + } + + /// Makes a local call's first frame what rskj's is, where that isn't what + /// a transaction's is. Returns the gas limit of a frame that is lent gas. + /// + /// - A call whose value moves nothing (`moves_nothing`) carries it and + /// transfers none. + /// - A precompile that runs whatever gas the call has (`runs_unmetered`) + /// is lent all it could ask for; [`Self::settle_lent_gas`] puts what it + /// spent back on the limit the call had. + /// - A creation that names no sender is made at the address rskj derives + /// from a sender of no bytes and its nonce, 0 (see `no_sender`). + fn fit_local_call( + &self, + input: &mut revm::interpreter::interpreter_action::FrameInput, + ) -> Option { + use revm::context_interface::CreateScheme; + use revm::interpreter::interpreter_action::FrameInput; + use revm::interpreter::{CallValue, CreateInputs}; + if !self.local_call { + return None; + } + match input { + FrameInput::Call(inputs) => { + if self.moves_nothing.get() { + inputs.value = CallValue::Apparent(inputs.value.get()); + } + if self.runs_unmetered(&inputs.target_address) { + let limit = inputs.gas_limit; + inputs.gas_limit = i64::MAX as u64; + return Some(limit); + } + } + FrameInput::Create(inputs) if self.no_sender => { + // keccak256(rlp(["", 0])), less its first 12 bytes. + let hash = revm::primitives::keccak256([0xc2, 0x80, 0x80]); + let address = Address::from_slice(&hash[12..]); + **inputs = CreateInputs::new( + inputs.caller(), + CreateScheme::Custom { address }, + inputs.value(), + inputs.init_code().clone(), + inputs.gas_limit(), + ); + } + _ => {} + } + None + } + + /// Puts a precompile's result back on the gas limit its call had, after + /// it ran on gas it was lent (`fit_local_call`): what it spent comes off + /// that limit, all of it where the limit can't pay it. + fn settle_lent_gas(result: &mut FrameResult, limit: u64) { + let gas = result.gas_mut(); + let mut settled = Gas::new(limit); + if !settled.record_cost(gas.spent()) { + settled.spend_all(); + } + settled.record_refund(gas.refunded()); + *gas = settled; + } + #[allow(clippy::too_many_arguments)] pub fn with_rskip125( empty_create_on_unpayable_deposit: bool, @@ -228,6 +316,8 @@ impl RskHandler { local_call: false, rskip136_active: true, starved: std::cell::Cell::new(false), + no_sender: false, + moves_nothing: std::cell::Cell::new(false), _phantom: core::marker::PhantomData, } } @@ -273,7 +363,8 @@ where /// (TransactionExecutor.java:360, 384) and in `create()` for a creation /// without data (406). If a program runs, `go()` fails it with all its gas /// as it charges the cost (455), before it runs anything (`starved`). A - /// precompile's account holds no code. + /// precompile's account holds no code, and before RSKIP136 one runs all + /// the same (`runs_unmetered`). fn validate( &self, evm: &mut Self::Evm, @@ -303,7 +394,8 @@ where .as_ref() .is_some_and(|code| !code.is_empty()), }; - if !runs_program { + let unmetered = matches!(ctx.tx().kind(), TxKind::Call(to) if self.runs_unmetered(&to)); + if !runs_program && !unmetered { return Err( revm::context_interface::result::InvalidTransaction::CallGasCostMoreThanGasLimit { gas_limit, @@ -325,8 +417,29 @@ where /// `Repository.transfer` (397, 427), which creates the sender's account /// and takes its balance below zero rather than fail. One here can't go /// below zero, so a sender short of the value is lifted to it, as revm - /// lifts one for a simulation (`disable_balance_check`), and holds nothing - /// where rskj's holds less than nothing. + /// lifts one for a simulation (`disable_balance_check`), and what it was + /// lifted by is kept as what it owes (`RskChainExt::owed`): BALANCE and + /// SELFBALANCE read the account less that, as rskj reads one below zero. + /// It is taken off what the account reads as, not off what it can spend: + /// a sender that is a contract, short of the value and paid back within + /// the call, can there send on what rskj's, still below zero, could not. + /// + /// Where the call names no sender (`no_sender`), rskj takes the value out + /// of its null address's account, which no program can read, and the zero + /// address the call runs from here has no part in it: it is not touched, + /// and has no account where it had none. The account called is given the + /// value here. A creation's value revm moves as it opens the creation, so + /// the zero address is given that first, and `exec_loop` leaves it as it + /// was once the creation is open. + /// + /// So the first frame of such a call carries the value and transfers none + /// (`moves_nothing`), and so does that of two more: + /// - one a named sender sends itself, which leaves it and comes back + /// whatever the sender holds, where revm's transfer refuses a sender + /// that holds less; + /// - one that would take the account it is sent past 2^256. rskj's + /// balances have no top, and revm's transfer fails there. Such a value + /// is 2^255 or more, so no program runs to read what moved (`exec_loop`). fn validate_against_state_and_deduct_caller( &self, evm: &mut Self::Evm, @@ -337,16 +450,57 @@ where ); } use revm::context_interface::Transaction; + use revm::primitives::TxKind; let ctx = evm.ctx(); let (caller, value) = (ctx.tx().caller(), ctx.tx().value()); - let mut sender = ctx - .journal_mut() - .load_account_mut(caller) - .map_err(|e| ERROR::from(e))?; - sender.touch(); - if *sender.balance() < value { - sender.set_balance(value); + let to = match ctx.tx().kind() { + TxKind::Call(to) => Some(to), + TxKind::Create => None, + }; + let past_the_top = match to { + Some(to) if !value.is_zero() => ctx + .journal_mut() + .load_account(to) + .map_err(|e| ERROR::from(e))? + .info + .balance + .checked_add(value) + .is_none(), + _ => false, + }; + let mut moves_nothing = (!self.no_sender && to == Some(caller)) || past_the_top; + let mut owed = None; + { + let journal = ctx.journal_mut(); + if !self.no_sender { + let mut sender = journal + .load_account_mut(caller) + .map_err(|e| ERROR::from(e))?; + sender.touch(); + let held = *sender.balance(); + if !moves_nothing && held < value { + sender.set_balance(value); + owed = Some((caller, value - held)); + } + } else if value.is_zero() || moves_nothing { + journal.load_account(caller).map_err(|e| ERROR::from(e))?; + } else if let Some(to) = to { + journal.load_account(caller).map_err(|e| ERROR::from(e))?; + let _ = journal + .load_account_mut(to) + .map_err(|e| ERROR::from(e))? + .incr_balance(value); + moves_nothing = true; + } else { + let mut payer = journal + .load_account_mut(caller) + .map_err(|e| ERROR::from(e))?; + let held = *payer.balance(); + payer.set_balance(held.saturating_add(value)); + } } + self.moves_nothing.set(moves_nothing); + ctx.chain_mut().owed = owed; Ok(()) } @@ -478,7 +632,7 @@ where // (TransactionExecutor.java:350), and from RSKIP136 works out the // leftover with `GasCost.subtract` (360), which throws out of // `executeTransaction`. Before RSKIP136 rskj runs the precompile all - // the same, which isn't done here. + // the same, on gas it doesn't have (`runs_unmetered`). if self.local_call && self.rskip136_active && frame_result.interpreter_result().result == InstructionResult::PrecompileOOG @@ -553,6 +707,9 @@ where evm: &mut EVM, first_frame_input: FrameInit, ) -> Result { + // What a local call's first frame is that a transaction's isn't. + let mut first_frame_input = first_frame_input; + let lent = self.fit_local_call(&mut first_frame_input.frame_input); // rskj builds a program's invocation only for a transaction that runs // code (`TransactionExecutor.call` and `create`); revm opens a frame // for a creation with no data too, which runs nothing. @@ -562,19 +719,53 @@ where } _ => true, }; + let creates_locally = self.local_call + && matches!( + first_frame_input.frame_input, + revm::interpreter::interpreter_action::FrameInput::Create(_) + ); // Every internal transaction rskj makes under this one carries its // gas price and the block's gas limit (`crate::internal_tx`). - { + let past_a_word = { use revm::context_interface::{Block, Transaction}; let ctx = evm.ctx(); - let gas_price = ctx.tx().effective_gas_price(ctx.block().basefee() as u128); + let gas_price = ctx.chain().whole_gas_price.unwrap_or_else(|| { + U256::from(ctx.tx().effective_gas_price(ctx.block().basefee() as u128)) + }); let block_gas_limit = ctx.block().gas_limit(); ctx.chain_mut() .internal_txs - .begin(U256::from(gas_price), U256::from(block_gas_limit)); + .begin(gas_price, U256::from(block_gas_limit)); + // The value and the gas price go into a program's invocation as + // the header's words do (`fail_programs`), and 2^255 takes a sign + // byte, a 33rd. Only a local call can carry either: no balance + // pays for one. + self.local_call && (gas_price.bit_len() == 256 || ctx.tx().value().bit_len() == 256) + }; + match D::init(evm, first_frame_input)? { + ItemOrResult::Result(mut frame_result) => { + if let Some(limit) = lent { + Self::settle_lent_gas(&mut frame_result, limit); + } + return Ok(frame_result); + } + ItemOrResult::Item(()) => {} } - if let ItemOrResult::Result(frame_result) = D::init(evm, first_frame_input)? { - return Ok(frame_result); + // revm bumps a creator's nonce as it opens the creation, which touches + // the creator. A local call's sender keeps its nonce + // (TransactionExecutor.java:292-301), which a program it holds reads + // when it creates in turn. And a creation that names no sender is + // from rskj's null address (`no_sender`): the zero address it runs + // from here is left as it was, with no account where it had none. + if creates_locally { + use revm::context_interface::Transaction; + let caller = evm.ctx().tx().caller(); + if let Some(account) = evm.ctx().journal_mut().evm_state_mut().get_mut(&caller) { + account.info.nonce = account.info.nonce.saturating_sub(1); + if self.no_sender { + account.unmark_touch(); + } + } } if self.fail_programs && runs_code { return Err(ERROR::from_string( @@ -583,6 +774,13 @@ where .into(), )); } + if past_a_word && runs_code { + return Err(ERROR::from_string( + "rskj can't build this call's program: its value or its gas \ + price is past 32 bytes" + .into(), + )); + } // Top-level CREATE transaction: rskj's TransactionExecutor.create never // bumps the nonce (even post-RSKIP125), so it stays 0. set_created_contract_nonce(evm, 0); diff --git a/crates/execution/src/rsk_instructions.rs b/crates/execution/src/rsk_instructions.rs index ec6981f..db82590 100644 --- a/crates/execution/src/rsk_instructions.rs +++ b/crates/execution/src/rsk_instructions.rs @@ -187,7 +187,7 @@ pub fn install( block: BlockWords, ) where WIRE: InterpreterTypes, - HOST: Host + ContractMarker + BeneficiaryTouch, + HOST: Host + ContractMarker + BeneficiaryTouch + LocalCallWords, { // RSKIP446 (lovell700): transient storage TLOAD/TSTORE (0x5c/0x5d, // EIP-1153, rskj `GasCost.TLOAD`/`TSTORE` = 100 each). RSKIP445 (lovell700): @@ -288,9 +288,11 @@ pub fn install( // charge the bumped Istanbul prices. Pin both back to rskj's `GasCost.SLOAD` // (200) and `GasCost.BALANCE` (400). Pre-Istanbul specs already use these // values, so the override is a no-op there; at SHANGHAI (lovell700+) it also - // corrects the same EIP-1884 over-charge. The instruction bodies are revm's - // own pre-Berlin SLOAD/BALANCE (RSK never reaches Berlin), which charge - // everything via the static gas we set here. (Mainnet #6,223,700.) + // corrects the same EIP-1884 over-charge. SLOAD's body is revm's own, which + // before Berlin (RSK never reaches it) charges everything via the static + // gas we set here. BALANCE's is `rsk_balance`, which reads the account as + // revm's does, at the same static gas, and makes the word rskj makes of the + // balance. (Mainnet #6,223,700.) instructions.insert_instruction( opcode::SLOAD, Instruction::new( @@ -300,10 +302,13 @@ pub fn install( ); instructions.insert_instruction( opcode::BALANCE, - Instruction::new( - revm::interpreter::instructions::host::balance::, - BALANCE_GAS, - ), + Instruction::new(rsk_balance::, BALANCE_GAS), + ); + // GASPRICE at rskj's BASE tier, 2, as revm's: `rsk_gasprice` pushes a + // local call's price whole. + instructions.insert_instruction( + opcode::GASPRICE, + Instruction::new(rsk_gasprice::, 2), ); // rskj `VM.doSSTORE` keeps Petersburg metering forever (SET=20000, @@ -659,6 +664,70 @@ where } } +/// What BALANCE, SELFBALANCE and GASPRICE read of a local call (`eth_call`, +/// `eth_estimateGas`) that revm's `Host` can't tell them: rskj holds a +/// balance with its sign and a gas price at any width. +pub trait LocalCallWords { + /// What `address` owes: the part of a local call's value that its sender + /// didn't hold (`RskChainExt::owed`), and nothing for any other account. + fn owed_by(&self, address: Address) -> U256; + + /// The gas price of a local call, where revm's transaction can't hold it + /// (`RskChainExt::whole_gas_price`). + fn whole_gas_price(&self) -> Option; +} + +impl LocalCallWords + for revm::Context< + BLOCK, + TX, + CFG, + RskDb, + revm::context::Journal, + crate::raw_storage::RskChainExt, + LOCAL, + > +where + LOCAL: revm::context_interface::LocalContextTr, +{ + fn owed_by(&self, address: Address) -> U256 { + match self.chain.owed { + Some((sender, owed)) if sender == address => owed, + _ => U256::ZERO, + } + } + + fn whole_gas_price(&self) -> Option { + self.chain.whole_gas_price + } +} + +/// The word rskj's `Program.getBalance` makes of a balance: `DataWord.valueOf` +/// of `Coin.getBytes()`, which is `BigInteger.toByteArray`, the two's +/// complement in the fewest bytes, set at the low end of the word. So a +/// balance below zero reads as those bytes do without their sign: 255 for -1, +/// 0xff7f for -129. One of 2^255 or more takes a sign byte, a 33rd, and +/// `valueOf` throws, as it does below -2^255: `None`, and the program fails. +/// +/// `held` is what the journal holds for the account and `owed` what the +/// account owes ([`LocalCallWords::owed_by`]); the balance is the one less the +/// other. On the chain nothing is owed and no account holds 2^255, so the +/// word is the balance. +pub(crate) fn balance_word(held: U256, owed: U256) -> Option { + if held >= owed { + let balance = held - owed; + return (balance.bit_len() < 256).then_some(balance); + } + let below = owed - held; + // The fewest bytes whose two's complement holds `-below`. + let bytes = (below - U256::from(1)).bit_len() / 8 + 1; + match bytes { + 33.. => None, + 32 => Some(below.wrapping_neg()), + _ => Some((U256::from(1) << (8 * bytes)) - below), + } +} + /// CHAINID without revm's ISTANBUL spec check (RSKIP152 activates it at /// papyrus200 while the spec stays PETERSBURG). fn rsk_chainid( @@ -862,15 +931,61 @@ fn rsk_txindex(context: InstructionCon } } -/// SELFBALANCE without revm's ISTANBUL spec check (RSKIP151, papyrus200). -fn rsk_selfbalance( +/// SELFBALANCE without revm's ISTANBUL spec check (RSKIP151, papyrus200), +/// pushing the word rskj makes of the balance ([`balance_word`]). +fn rsk_selfbalance( + context: InstructionContext<'_, H, WIRE>, +) { + let address = context.interpreter.input.target_address(); + let Some(balance) = context.host.balance(address) else { + context.interpreter.halt_fatal(); + return; + }; + let Some(word) = balance_word(balance.data, context.host.owed_by(address)) else { + context.interpreter.halt(InstructionResult::OutOfGas); + return; + }; + if !context.interpreter.stack.push(word) { + context.interpreter.halt_overflow(); + } +} + +/// BALANCE, pushing the word rskj makes of the balance ([`balance_word`]). +/// The account is read as revm's BALANCE reads one before Berlin, and the +/// instruction's static gas is all it costs (see `install`). +fn rsk_balance( context: InstructionContext<'_, H, WIRE>, ) { - let Some(balance) = context.host.balance(context.interpreter.input.target_address()) else { + let address = { + let Some(([], top)) = context.interpreter.stack.popn_top::<0>() else { + context.interpreter.halt_underflow(); + return; + }; + Address::from_word(B256::from(*top)) + }; + let Some(balance) = context.host.balance(address) else { context.interpreter.halt_fatal(); return; }; - if !context.interpreter.stack.push(balance.data) { + let Some(word) = balance_word(balance.data, context.host.owed_by(address)) else { + context.interpreter.halt(InstructionResult::OutOfGas); + return; + }; + if let Some(([], top)) = context.interpreter.stack.popn_top::<0>() { + *top = word; + } +} + +/// GASPRICE, pushing a local call's price whole where revm's transaction +/// can't hold it ([`LocalCallWords`]). +fn rsk_gasprice( + context: InstructionContext<'_, H, WIRE>, +) { + let price = context + .host + .whole_gas_price() + .unwrap_or_else(|| context.host.effective_gas_price()); + if !context.interpreter.stack.push(price) { context.interpreter.halt_overflow(); } } @@ -940,7 +1055,10 @@ thread_local! { /// All-ops tracer: log pc/opcode/gas (post-static-charge), then dispatch to /// the real implementation (custom rsk op or default PETERSBURG table). -fn traced_all( +fn traced_all< + WIRE: InterpreterTypes, + HOST: Host + ContractMarker + BeneficiaryTouch + LocalCallWords, +>( context: InstructionContext<'_, HOST, WIRE>, ) { use revm::interpreter::interpreter_types::{InputsTr, Jumps, LegacyBytecode}; @@ -963,6 +1081,8 @@ fn traced_all rsk_extcodesize(context), + opcode::BALANCE => rsk_balance(context), + opcode::GASPRICE => rsk_gasprice(context), opcode::CALL => rsk_call(context), opcode::CALLCODE => rsk_call_code(context), opcode::DELEGATECALL => rsk_delegate_call(context), @@ -1502,6 +1622,38 @@ mod tests { assert_eq!(r.start, usize::MAX); assert!(r.is_empty()); } + + /// The word BALANCE pushes is the one rskj makes of the balance: the + /// balance itself, the bytes of one below zero without their sign + /// (`BigInteger.toByteArray`), and none for one that takes a 33rd byte. + /// rskj's release answered each of those below zero here, down to + /// -10^18, through `eth_call` (the rpc crate's + /// `testdata/rskj_local_calls.txt`). + #[test] + fn a_balance_reads_as_the_word_rskj_makes_of_it() { + let word = |held: u64, owed: u64| balance_word(U256::from(held), U256::from(owed)); + assert_eq!(word(7, 0), Some(U256::from(7))); + assert_eq!(word(7, 7), Some(U256::ZERO)); + assert_eq!(word(0, 1), Some(U256::from(0xff))); + assert_eq!(word(1, 3), Some(U256::from(0xfe))); + assert_eq!(word(0, 127), Some(U256::from(0x81))); + assert_eq!(word(0, 128), Some(U256::from(0x80))); + assert_eq!(word(0, 129), Some(U256::from(0xff7f))); + assert_eq!( + word(0, 1_000_000_000_000_000_000), + Some(U256::from(0xf21f_494c_589c_0000u64)) + ); + + // 32 bytes hold -2^255 to 2^255 - 1. + let (one, top) = (U256::from(1), U256::from(1) << 255); + assert_eq!(balance_word(top - one, U256::ZERO), Some(top - one)); + assert_eq!(balance_word(top, U256::ZERO), None); + assert_eq!(balance_word(U256::MAX, U256::ZERO), None); + assert_eq!(balance_word(U256::ZERO, top - one), Some(top + one)); + assert_eq!(balance_word(U256::ZERO, top), Some(top)); + assert_eq!(balance_word(U256::ZERO, top + one), None); + assert_eq!(balance_word(U256::ZERO, U256::MAX), None); + } } #[cfg(test)] diff --git a/crates/rpc/src/call.rs b/crates/rpc/src/call.rs index 1b58dd0..fd197c9 100644 --- a/crates/rpc/src/call.rs +++ b/crates/rpc/src/call.rs @@ -3,9 +3,9 @@ use crate::params::{uncaught, Refusal}; use crate::server::RpcState; use crate::types::*; -use alloy_primitives::{Address, Bytes, U256}; +use alloy_primitives::{Bytes, U256}; use rustock_core::Transaction; -use rustock_execution::RskExecutor; +use rustock_execution::{GasEstimate, RskExecutor}; use serde_json::{json, Value}; use crate::helpers::to_hex_bytes; @@ -74,18 +74,15 @@ pub fn eth_call(id: Value, params: &Value, state: &RpcState) -> JsonRpcResponse Err(none) => return crate::state::no_execution_state(&id, &block_param, none), }; - let sender = call_req.from.unwrap_or(Address::ZERO); - let tx = build_call_tx(&call_req, &root, sender, state); - let executor = RskExecutor::new(hardfork_cfg, state.store.clone()); + let tx = build_call_tx(&call_req, &root, state); + let executor = state.executor(hardfork_cfg); - match executor.execute_local_call(&header, &tx, sender, &root, trie_store) { - Ok(result) => { - if result.output.is_empty() { - JsonRpcResponse::success(id, json!("0x")) - } else { - JsonRpcResponse::success(id, json!(to_hex_bytes(&result.output))) - } - } + // A program that ends with REVERT is an error of its own; one that fails + // any other way answers with no data, as a call that runs nothing does + // (`EthModule.call`). + match executor.execute_local_call(&header, &tx, call_req.from, &root, trie_store) { + Ok(result) if result.reverted => crate::revert::answer(id, &result.output), + Ok(result) => JsonRpcResponse::success(id, json!(to_hex_bytes(&result.output))), Err(e) => JsonRpcResponse::error(id, INTERNAL_ERROR, format!("Execution failed: {e}")), } } @@ -119,31 +116,28 @@ pub fn eth_estimate_gas(id: Value, params: &Value, state: &RpcState) -> JsonRpcR Err(refusal) => return refuse(id, refusal), }; - let sender = call_req.from.unwrap_or(Address::ZERO); - let executor = RskExecutor::new(hardfork_cfg, state.store.clone()); + let executor = state.executor(hardfork_cfg); // rskj runs every estimate at its cap; the call's own gas isn't read. let gas_cap = GAS_ESTIMATION_CAP; - let mut tx = build_call_tx(&call_req, &root, sender, state); + let mut tx = build_call_tx(&call_req, &root, state); tx.gas_limit = U256::from(gas_cap); if state.gas_estimator_binary_search { return estimate_by_binary_search( - id, &call_req, &header, &root, sender, state, &executor, trie_store, gas_cap, + id, &call_req, &header, &root, state, &executor, trie_store, gas_cap, ); } // One execution, at the cap, with the estimation inspector attached. See // `rustock_execution::gas_estimator` for why the three things it records // are enough to answer without searching. - match executor.estimate_gas_exact(&header, &tx, sender, &root, trie_store, gas_cap) { - Ok((estimate, true, _)) => { + match executor.estimate_gas_exact(&header, &tx, call_req.from, &root, trie_store, gas_cap) { + Ok(GasEstimate::Gas(estimate)) => { JsonRpcResponse::success(id, json!(crate::helpers::to_hex_u64(estimate))) } - Ok((_, false, _)) => { - JsonRpcResponse::error(id, INTERNAL_ERROR, "Transaction would revert") - } + Ok(GasEstimate::Reverted(output)) => crate::revert::answer(id, &output), Err(e) => JsonRpcResponse::error(id, INTERNAL_ERROR, format!("Execution failed: {e}")), } } @@ -161,7 +155,6 @@ fn estimate_by_binary_search( call_req: &CallRequest, header: &rustock_core::types::header::Header, root: &rustock_trie::TrieNode, - sender: Address, state: &RpcState, executor: &RskExecutor, trie_store: std::sync::Arc, @@ -180,13 +173,18 @@ fn estimate_by_binary_search( let mut lo = 0u64; let mut hi = gas_cap; - // First check if the transaction even succeeds at the gas cap - let mut tx = build_call_tx(call_req, root, sender, state); + // First check if the transaction even succeeds at the gas cap. One that + // reverts is answered as a revert, and one that fails any other way has + // no smaller limit to find: it is estimated at what it used, as rskj + // estimates it (`RskExecutor::estimate_gas_exact`). + let mut tx = build_call_tx(call_req, root, state); tx.gas_limit = U256::from(hi); - match executor.execute_local_call(header, &tx, sender, root, trie_store.clone()) { + match executor.execute_local_call(header, &tx, call_req.from, root, trie_store.clone()) { + Ok(result) if result.reverted => return crate::revert::answer(id, &result.output), Ok(result) if result.success => {} - Ok(_) => { - return JsonRpcResponse::error(id, INTERNAL_ERROR, "Transaction would revert"); + Ok(result) => { + let used = result.gas_used.min(gas_cap); + return JsonRpcResponse::success(id, json!(crate::helpers::to_hex_u64(used))); } Err(e) => { return JsonRpcResponse::error(id, INTERNAL_ERROR, format!("Execution failed: {e}")); @@ -195,10 +193,16 @@ fn estimate_by_binary_search( while lo + 1 < hi { let mid = lo + (hi - lo) / 2; - let mut probe_tx = build_call_tx(call_req, root, sender, state); + let mut probe_tx = build_call_tx(call_req, root, state); probe_tx.gas_limit = U256::from(mid); - match executor.execute_local_call(header, &probe_tx, sender, root, trie_store.clone()) { + match executor.execute_local_call( + header, + &probe_tx, + call_req.from, + root, + trie_store.clone(), + ) { Ok(result) if result.success => hi = mid, _ => lo = mid, } @@ -212,19 +216,20 @@ fn estimate_by_binary_search( /// 0 (`getGasPrice`), [`CALL_GAS_CAP`]'s gas (`gasLimitForCall`; an estimate /// sets its own), no value. The nonce is the sender's, as /// `ReversibleTransactionExecutor` builds it; a local call doesn't check it. +/// A call that names no sender is from rskj's null address +/// (`getFromAddress`), which no transaction is ever from: its nonce is 0. fn build_call_tx( req: &CallRequest, root: &rustock_trie::TrieNode, - sender: Address, state: &RpcState, ) -> Transaction { - let sender_nonce = crate::state::trie_get_account( - root, - &rustock_trie::account_key(&sender), - state, - ) - .map(|a| a.nonce.to::()) - .unwrap_or(0); + let sender_nonce = req + .from + .and_then(|sender| { + crate::state::trie_get_account(root, &rustock_trie::account_key(&sender), state) + }) + .map(|a| a.nonce.to::()) + .unwrap_or(0); Transaction { nonce: sender_nonce, gas_price: req.gas_price.unwrap_or(U256::ZERO), diff --git a/crates/rpc/src/debug.rs b/crates/rpc/src/debug.rs index 1f0da06..62ae28d 100644 --- a/crates/rpc/src/debug.rs +++ b/crates/rpc/src/debug.rs @@ -206,7 +206,7 @@ fn trace_block_transaction( transactions, ommers, }; - let processor = rustock_execution::BlockProcessor::new(hardfork_cfg, state.store.clone()); + let processor = state.block_processor(hardfork_cfg); match processor.trace_transaction(&block, &root, trie_store, index, MAX_STRUCT_LOGS) { Ok(Some(trace)) => Ok(Some(render_trace(&trace))), diff --git a/crates/rpc/src/lib.rs b/crates/rpc/src/lib.rs index 8929766..c596afa 100644 --- a/crates/rpc/src/lib.rs +++ b/crates/rpc/src/lib.rs @@ -13,6 +13,7 @@ pub mod net; pub mod params; pub mod proof; pub mod raw_tx; +pub mod revert; pub mod rsk; pub mod sco; pub mod server; diff --git a/crates/rpc/src/revert.rs b/crates/rpc/src/revert.rs new file mode 100644 index 0000000..859c178 --- /dev/null +++ b/crates/rpc/src/revert.rs @@ -0,0 +1,229 @@ +//! What `eth_call` and `eth_estimateGas` answer where the program they ran +//! ended with REVERT, as rskj answers it +//! (`EthModule.handleTransactionRevertIfHappens`). + +use crate::helpers::to_hex_bytes; +use crate::params::{uncaught, Refusal}; +use crate::types::{JsonRpcResponse, EXECUTION_ERROR}; +use serde_json::{json, Value}; + +/// The selector of Solidity's `Error(string)`, rskj's +/// `ERROR_ABI_FUNCTION_SIGNATURE`. +const ERROR_SELECTOR: [u8; 4] = [0x08, 0xc3, 0x79, 0xa0]; + +/// rskj's answer to a revert: its own error, -32015, whose `data` is what the +/// program reverted with, in hex. The message is "transaction reverted", or +/// "revert " and the reason where the data is an `Error(string)`. Where the +/// data says it is one and can't be read as one, rskj's decoder throws, which +/// nothing catches. +pub fn answer(id: Value, data: &[u8]) -> JsonRpcResponse { + let said = match reason(data) { + Ok(Some(reason)) => format!("revert {reason}"), + Ok(None) => "transaction reverted".to_string(), + Err((code, message)) => return JsonRpcResponse::error(id, code, message), + }; + JsonRpcResponse::error_with_data( + id, + EXECUTION_ERROR, + format!("VM Exception while processing transaction: {said}"), + json!(to_hex_bytes(data)), + ) +} + +/// The reason a revert gives, as rskj's `EthModule.decodeProgramRevert` reads +/// it: none unless the data begins with `Error(string)`'s selector, and then +/// what `CallTransaction.Function.decode` makes of the rest. +/// +/// That decoder reads the string's offset, and at the offset its length, each +/// as the low 32 bits of a 32-byte word (`BigInteger.intValue`), and then the +/// string's bytes. A read throws where the data doesn't hold all of it, or +/// where the offset or the length is below zero (`Utils.safeCopyOfRange`). +/// The data need not be padded, and what follows the string is not read. +/// +/// (With no data at all after the selector rskj reads both words as 0, +/// `IntType.decodeInt`, and throws all the same, for the string's bytes.) +fn reason(data: &[u8]) -> Result, Refusal> { + if data.len() < 4 || data[..4] != ERROR_SELECTOR { + return Ok(None); + } + let encoded = &data[4..]; + let read = |from: i32, size: i32| -> Result<&[u8], Refusal> { + let (start, length) = (usize::try_from(from), usize::try_from(size)); + let end = from + .checked_add(size) + .and_then(|end| usize::try_from(end).ok()); + match (start, length, end) { + (Ok(start), Ok(_), Some(end)) if end <= encoded.len() => Ok(&encoded[start..end]), + _ => Err(uncaught()), + } + }; + let word = |at: i32| -> Result { + let word = read(at, 32)?; + Ok(i32::from_be_bytes([word[28], word[29], word[30], word[31]])) + }; + let offset = word(0)?; + let length = word(offset)?; + // No overflow: a word was read at the offset. + let text = read(offset + 32, length)?; + Ok(Some(java_utf8(text))) +} + +/// `new String(bytes, StandardCharsets.UTF_8)`, as the JDK reads bytes that +/// aren't all UTF-8: each run it can't read becomes one U+FFFD. +/// +/// Its runs are those of `String::from_utf8_lossy` but for a surrogate +/// written as three bytes (`ED A0 80` to `ED BF BF`), which the JDK reads as +/// one run, and its first two bytes as one where the third is missing or +/// isn't a continuation. `from_utf8_lossy` replaces each byte of them. +fn java_utf8(bytes: &[u8]) -> String { + let continues = |byte: u8| byte & 0xc0 == 0x80; + let mut text = String::new(); + let mut rest = bytes; + while let [b1, after @ ..] = rest { + let b1 = *b1; + // Whether `b2` can follow a `b1` that leads four bytes. + let second_of_four = |b2: u8| match b1 { + 0xf0 => (0x90..=0xbf).contains(&b2), + 0xf1..=0xf3 => continues(b2), + 0xf4 => b2 & 0xf0 == 0x80, + _ => false, + }; + // The character at the head, where there is one, and the bytes it or + // the run in its place takes. + let (made, taken) = match (b1, after) { + (0x00..=0x7f, _) => (Some(b1 as char), 1), + (0xc2..=0xdf, [b2, ..]) if continues(*b2) => { + let point = (u32::from(b1) & 0x1f) << 6 | (u32::from(*b2) & 0x3f); + (char::from_u32(point), 2) + } + (0xe0..=0xef, [b2, b3, ..]) => { + if (b1 == 0xe0 && b2 & 0xe0 == 0x80) || !continues(*b2) { + (None, 1) + } else if !continues(*b3) { + (None, 2) + } else { + // None for a surrogate, which takes its three bytes. + let point = (u32::from(b1) & 0x0f) << 12 + | (u32::from(*b2) & 0x3f) << 6 + | (u32::from(*b3) & 0x3f); + (char::from_u32(point), 3) + } + } + (0xe0..=0xef, [b2]) if (b1 == 0xe0 && b2 & 0xe0 == 0x80) || !continues(*b2) => { + (None, 1) + } + // Cut short by the end, with what there is of it. + (0xe0..=0xef, _) => (None, rest.len()), + (0xf0..=0xf7, [b2, ..]) if !second_of_four(*b2) => (None, 1), + (0xf5..=0xf7, []) => (None, 1), + (0xf0..=0xf7, [_, b3, ..]) if !continues(*b3) => (None, 2), + (0xf0..=0xf7, [b2, b3, b4, ..]) => { + if continues(*b4) { + let point = (u32::from(b1) & 0x07) << 18 + | (u32::from(*b2) & 0x3f) << 12 + | (u32::from(*b3) & 0x3f) << 6 + | (u32::from(*b4) & 0x3f); + (char::from_u32(point), 4) + } else { + (None, 3) + } + } + (0xf0..=0xf7, _) => (None, rest.len()), + // A byte that leads nothing: a continuation, C0, C1, F8 and up, + // or a lead of two that nothing continues. + _ => (None, 1), + }; + text.push(made.unwrap_or(char::REPLACEMENT_CHARACTER)); + rest = &rest[taken..]; + } + text +} + +#[cfg(test)] +mod tests { + use super::*; + + /// Where the JDK's runs of unreadable bytes are not `from_utf8_lossy`'s, + /// and a few where they are. Each was read by a JVM + /// (`testdata/rskj_local_calls.txt`, the scenario `utf8`, has rskj's own + /// answers for the same bytes). + #[test] + fn bytes_that_are_not_utf8_read_as_the_jdk_reads_them() { + let read = |bytes: &[u8]| java_utf8(bytes); + assert_eq!( + read(b"h\xc3\xa9llo \xe2\x9c\x93 \xf0\x9f\x98\x80"), + "h\u{e9}llo \u{2713} \u{1f600}" + ); + // A surrogate in three bytes is one run; so are its first two. + assert_eq!(read(b"a\xed\xa0\x80b"), "a\u{fffd}b"); + assert_eq!(read(b"a\xed\xbf\xbfb"), "a\u{fffd}b"); + assert_eq!(read(b"a\xed\xa0b"), "a\u{fffd}b"); + assert_eq!(read(b"a\xed\xa0"), "a\u{fffd}"); + assert_eq!( + String::from_utf8_lossy(b"a\xed\xa0\x80b"), + "a\u{fffd}\u{fffd}\u{fffd}b" + ); + // The rest are `from_utf8_lossy`'s. + for bytes in [ + &b"a\xffb"[..], + b"a\xc0\xafb", + b"a\xe2\x82", + b"a\xe0\x80\x80b", + b"a\xe0\x9f", + b"a\xf4\x90\x80\x80b", + b"a\xf8\x88\x80\x80\x80b", + b"a\x80b", + b"a\xf0\x9f\x98", + b"a\xf0\x9f\x98b", + b"a\xf0\x9f", + b"a\xf0", + b"a\xf5", + b"a\xc2", + b"a\xc2b", + b"a\xf4\x8f\xbf\xbf", + b"a\xed\x9f\xbf\xee\x80\x80", + ] { + assert_eq!(read(bytes), String::from_utf8_lossy(bytes), "{bytes:02x?}"); + } + } + + /// An `Error(string)` is read as rskj's decoder reads one, and what it + /// can't read is an exception nobody catches. + #[test] + fn a_reverts_reason_is_read_as_rskj_reads_it() { + let word = |number: u64| { + let mut word = [0u8; 32]; + word[24..].copy_from_slice(&number.to_be_bytes()); + word + }; + let error = |parts: &[&[u8]]| [&ERROR_SELECTOR[..], &parts.concat()].concat(); + + assert_eq!(reason(&[]), Ok(None)); + assert_eq!(reason(&ERROR_SELECTOR[..3]), Ok(None)); + assert_eq!(reason(&[0x4e, 0x48, 0x7b, 0x71, 0, 0, 0, 0x11]), Ok(None)); + let hello = error(&[&word(32), &word(5), b"hello"]); + assert_eq!(reason(&hello), Ok(Some("hello".into()))); + assert_eq!( + reason(&error(&[&word(32), &word(0)])), + Ok(Some(String::new())) + ); + // The offset's low 32 bits, wherever they point. + let high = error(&[&word((1 << 32) + 32), &word(5), b"hello"]); + assert_eq!(reason(&high), Ok(Some("hello".into()))); + assert_eq!( + reason(&error(&[&word(0), &word(0)])), + Ok(Some(String::new())) + ); + + for unread in [ + error(&[]), + error(&[&word(32)]), + error(&[&word(32), &word(6), b"hello"]), + error(&[&[0xff; 32], &word(0)]), + error(&[&word(32), &word(0xffff_ffff), &word(0)]), + error(&[&word(0x7fff_ffff)]), + ] { + assert_eq!(reason(&unread), Err(uncaught()), "{unread:02x?}"); + } + } +} diff --git a/crates/rpc/src/server.rs b/crates/rpc/src/server.rs index 18b8f06..b8c91dd 100644 --- a/crates/rpc/src/server.rs +++ b/crates/rpc/src/server.rs @@ -79,6 +79,11 @@ pub struct RpcState { pub tx_submitter: Option>, pub trie_store: Option>, pub hardfork_cfg: Option, + /// The Bridge's constants where they aren't the chain's own: a chain from + /// an rskj genesis file runs on rskj's regtest ones, as the node's block + /// processor does (`BlockProcessor::with_bridge_constants`). A call, an + /// estimate and a trace run on the same. + pub bridge_constants: Option, pub filter_store: Arc, pub tx_pool: Option>, /// Live depth of the inbound wire-message queue, for @@ -140,6 +145,29 @@ pub struct RpcState { pub events: Option, } +impl RpcState { + /// The executor a call or an estimate runs on, under `config`. + pub(crate) fn executor(&self, config: RskHardforkConfig) -> rustock_execution::RskExecutor { + let executor = rustock_execution::RskExecutor::new(config, self.store.clone()); + match &self.bridge_constants { + Some(constants) => executor.with_bridge_constants(constants.clone()), + None => executor, + } + } + + /// The processor a trace runs a block again on, under `config`. + pub(crate) fn block_processor( + &self, + config: RskHardforkConfig, + ) -> rustock_execution::BlockProcessor { + let processor = rustock_execution::BlockProcessor::new(config, self.store.clone()); + match &self.bridge_constants { + Some(constants) => processor.with_bridge_constants(constants.clone()), + None => processor, + } + } +} + /// Starts the JSON-RPC HTTP server on the given host and port. pub async fn start_rpc_server( host: &str, diff --git a/crates/rpc/src/testdata/rskj_local_calls.txt b/crates/rpc/src/testdata/rskj_local_calls.txt new file mode 100644 index 0000000..5621a6b --- /dev/null +++ b/crates/rpc/src/testdata/rskj_local_calls.txt @@ -0,0 +1,410 @@ +# What rskj's release answers to read-only calls, request by request. +# +# Its own JSON-RPC server (rskj-core 9.0.4-VETIVER, as released) answered +# these over the Web3 its RskContext builds on regtest, with block 1 as the +# head and the accounts listed in its state. The scenario +# "short-before-rskip136" is "short" again, run with +# blockchain.config.consensusRules.rskip136=-1. +# +# scenario +# head +# account
[], or "none" for an +# account the state doesn't hold +# call